LIBRCSC Docs
Documentation for HELIOS's BASE LIBRCSC library for RoboCup 2D Simulation League.
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
matrix_2d.h
Go to the documentation of this file.
1// -*-c++-*-
2
8/*
9 *Copyright:
10
11 Copyright (C) Hidehisa Akiyama
12
13 This code is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27 *EndCopyright:
28 */
29
31
32#ifndef RCSC_GEOM_MATRIX2D_H
33#define RCSC_GEOM_MATRIX2D_H
34
35#include <rcsc/geom/vector_2d.h>
36#include <rcsc/geom/angle_deg.h>
37
38#include <iostream>
39#include <cmath>
40
41namespace rcsc {
42
51class Matrix2D {
52private:
53
54 double M_11;
55 double M_12;
56 double M_21;
57 double M_22;
58 double M_dx;
59 double M_dy;
60
61public:
62
67 : M_11( 1.0 ),
68 M_12( 0.0 ),
69 M_21( 0.0 ),
70 M_22( 1.0 ),
71 M_dx( 0.0 ),
72 M_dy( 0.0 )
73 { }
74
84 Matrix2D( const double m11, const double m12,
85 const double m21, const double m22,
86 const double dx, const double dy )
87 : M_11( m11 ), M_12( m12 ),
88 M_21( m21 ), M_22( m22 ),
89 M_dx( dx ), M_dy( dy )
90 { }
91
96 const Matrix2D & reset()
97 {
98 M_11 = M_22 = 1.0;
99 M_12 = M_21 = M_dx = M_dy = 0.0;
100 return *this;
101 }
102
113 const Matrix2D & assign( const double m11, const double m12,
114 const double m21, const double m22,
115 const double dx, const double dy )
116 {
117 M_11 = m11; M_12 = m12;
118 M_21 = m21; M_22 = m22;
119 M_dx = dx; M_dy = dy;
120 return *this;
121 }
122
129 static
131 const double dy )
132 {
133 return Matrix2D( 1.0, 0.0,
134 0.0, 1.0,
135 dx, dy );
136 }
137
144 static
145 Matrix2D make_scaling( const double sx,
146 const double sy )
147 {
148 return Matrix2D( sx, 0.0,
149 0.0, sy,
150 0.0, 0.0 );
151 }
152
158 static
160 {
161 double cosa = angle.cos();
162 double sina = angle.sin();
163 return Matrix2D( cosa, -sina,
164 sina, cosa,
165 0.0, 0.0 );
166 }
167
172 double m11() const
173 {
174 return M_11;
175 }
176
181 double m12() const
182 {
183 return M_12;
184 }
185
190 double m21() const
191 {
192 return M_21;
193 }
194
199 double m22() const
200 {
201 return M_22;
202 }
203
208 double dx() const
209 {
210 return M_dx;
211 }
212
217 double dy() const
218 {
219 return M_dy;
220 }
221
226 double det() const
227 {
228 return M_11*M_22 - M_12*M_21;
229 }
230
235 bool invertible() const
236 {
237 return ! ( std::fabs( det() ) < 0.00000000001 );
238 }
239
245
255 Matrix2D & translate( const double dx,
256 const double dy )
257 {
258 // translation matrix
259 // T = ( 1, 0, dx )
260 // ( 0, 1, dy )
261 // ( 0, 0, 1 )
262
263 /*
264 // this = this * T
265 M_dx += M_11*dx + M_12*dy;
266 M_dy += M_21*dx + M_22*dy;
267 */
268
269 // this = T * this
270 // *this = make_translation(dx,dy) * *this;
271
272 M_dx += dx;
273 M_dy += dy;
274 return *this;
275 }
276
286 Matrix2D & scale( const double sx,
287 const double sy )
288 {
289 // scaling matrixa
290 // S = ( Sx, 0, 0 )
291 // ( 0, Sy, 0 )
292 // ( 0, 0, 1 )
293
294 /*
295 this = this * S
296 *this *= make_scaling(sx,sy)
297 M_11 *= sx; M_12 *= sy;
298 M_21 *= sx; M_22 *= sy;
299 */
300
301 // this = S * this
302 // *this = make_scaling(sx,sy) * *this;
303
304 M_11 *= sx; M_12 *= sx; M_dx *= sx;
305 M_21 *= sy; M_22 *= sy; M_dy *= sy;
306 return *this;
307 }
308
309 /*
310 Matrix2D & shear( const double sh,
311 const double sv )
312 {
313 double tm11 = sv * M_21;
314 double tm12 = sv * M_22;
315 double tm21 = sh * M_11;
316 double tm22 = sh * M_12;
317 M_11 += tm11; M_12 += tm12;
318 M_21 += tm21; M_22 += tm22;
319 return *this;
320 }
321 */
322
331 Matrix2D & rotate( const AngleDeg & angle );
332
338 const Matrix2D & operator*=( const Matrix2D & m )
339 {
340 double tm11 = M_11*m.M_11 + M_12*m.M_21;
341 double tm12 = M_11*m.M_12 + M_12*m.M_22;
342 double tm21 = M_21*m.M_11 + M_22*m.M_21;
343 double tm22 = M_21*m.M_12 + M_22*m.M_22;
344
345 double tdx = M_11*m.M_dx + M_12*m.M_dy + M_dx;
346 double tdy = M_21*m.M_dx + M_22*m.M_dy + M_dy;
347
348 M_11 = tm11; M_12 = tm12;
349 M_21 = tm21; M_22 = tm22;
350 M_dx = tdx; M_dy = tdy;
351 return *this;
352 }
353
359 Vector2D transform( const Vector2D & v ) const
360 {
361 return Vector2D( M_11*v.x + M_12*v.y + M_dx,
362 M_21*v.x + M_22*v.y + M_dy );
363 }
364
371 Vector2D transform( const double x,
372 const double y ) const
373 {
374 return Vector2D( M_11*x + M_12*y + M_dx,
375 M_21*x + M_22*y + M_dy );
376 }
377
382 void transform( Vector2D * v ) const
383 {
384 double tx = M_11*v->x + M_12*v->y + M_dx;
385 double ty = M_21*v->x + M_22*v->y + M_dy;
386 v->assign( tx, ty );
387 }
388
389#if 0
390 Segment2D transform( const Segment2D & s ) const
391 {
392 return Segment2D( transform( s.a() ),
393 transform( s.b() ) );
394 }
395
396 /*
397 Line2D transform( const Line2D & l ) const
398 {
399 }
400 */
401
402 Rai2D transform( const Ray2D & r ) const
403 {
404 return Ray2D( transform( r.origin() ),
405 transform( r.origin() + Vector2D::polar2vector( 1.0, r.dir() ) ) );
406 }
407
408 Circle2D transform( const Circle2D & c ) const
409 {
410 return Circle2D( transform( c.center() ),
411 c.radius() );
412 }
413
414 /*
415 Sector2D transform( const Sector2D & s ) const
416 {
417
418 }
419 */
420
421 Triangle2D transform( const Triangle2D & t ) const
422 {
423 return Triangle2D( transform( t.a() ),
424 transform( t.b() ),
425 transform( t.c() ) );
426 }
427#endif
428
434 std::ostream & print( std::ostream & os ) const
435 {
436 os << M_11 << ' '
437 << M_12 << ' '
438 << M_21 << ' '
439 << M_22 << ' '
440 << M_dx << ' '
441 << M_dy;
442 return os;
443 }
444
445};
446
447}
448
455inline
456const
459 const rcsc::Matrix2D & rhs )
460{
461 return rcsc::Matrix2D( lhs ) *= rhs;
462}
463
470inline
473 const rcsc::Vector2D & rhs )
474{
475 return lhs.transform( rhs );
476}
477
484inline
485std::ostream &
486operator<<( std::ostream & os,
487 const rcsc::Matrix2D & m )
488{
489 return m.print( os );
490}
491
492
493#endif
degree wrapper class Header File.
degree wrapper class
Definition: angle_deg.h:45
double cos() const
calculate cosine
Definition: angle_deg.h:292
double sin() const
calculate sine
Definition: angle_deg.h:301
2D translation matrix class
Definition: matrix_2d.h:51
Matrix2D & rotate(const AngleDeg &angle)
rotates the coordinate system
double m21() const
get the horizontal shearing factor.
Definition: matrix_2d.h:190
const Matrix2D & assign(const double m11, const double m12, const double m21, const double m22, const double dx, const double dy)
set a matrix element with the specified values.
Definition: matrix_2d.h:113
double m11() const
get the horizontal scaling factor.
Definition: matrix_2d.h:172
std::ostream & print(std::ostream &os) const
put all elemtns to the output stream
Definition: matrix_2d.h:434
static Matrix2D make_scaling(const double sx, const double sy)
create the scaling matrix.
Definition: matrix_2d.h:145
Matrix2D & translate(const double dx, const double dy)
moves the coordinate system.
Definition: matrix_2d.h:255
double det() const
get the matrix's determinant.
Definition: matrix_2d.h:226
bool invertible() const
check if this matrix is invertible (is not isingular).
Definition: matrix_2d.h:235
Matrix2D inverted() const
get the inverted matrix.
double dx() const
get the horizontal translation factor.
Definition: matrix_2d.h:208
const Matrix2D & operator*=(const Matrix2D &m)
multiplied by other matrix
Definition: matrix_2d.h:338
double m22() const
get the vertical scaling factor.
Definition: matrix_2d.h:199
double dy() const
get the vertical translation factor.
Definition: matrix_2d.h:217
static Matrix2D make_translation(const double dx, const double dy)
create the translation matrix.
Definition: matrix_2d.h:130
double m12() const
get the vertical shearing factor.
Definition: matrix_2d.h:181
void transform(Vector2D *v) const
transform input vector with this matrix
Definition: matrix_2d.h:382
Vector2D transform(const double x, const double y) const
create transformed vector from input coordinates with this matrix
Definition: matrix_2d.h:371
Matrix2D & scale(const double sx, const double sy)
scales the coordinate system.
Definition: matrix_2d.h:286
Vector2D transform(const Vector2D &v) const
create transformed vector from input vector with this matrix
Definition: matrix_2d.h:359
Matrix2D()
create an identity matrix
Definition: matrix_2d.h:66
Matrix2D(const double m11, const double m12, const double m21, const double m22, const double dx, const double dy)
create a matrix with all elements.
Definition: matrix_2d.h:84
static Matrix2D make_rotation(const AngleDeg &angle)
create the rotation matrix.
Definition: matrix_2d.h:159
const Matrix2D & reset()
reset to the identity matrix
Definition: matrix_2d.h:96
2d segment line class
Definition: segment_2d.h:46
2D point vector class
Definition: vector_2d.h:46
Vector2D & assign(const double xx, const double yy)
assign XY value directly.
Definition: vector_2d.h:100
static Vector2D polar2vector(const double mag, const AngleDeg &theta)
get new Vector created by POLAR value.
Definition: vector_2d.h:580
double y
Y coordinate.
Definition: vector_2d.h:64
double x
X coordinate.
Definition: vector_2d.h:63
std::ostream & operator<<(std::ostream &os, const rcsc::Matrix2D &m)
output stream operator.
Definition: matrix_2d.h:486
const rcsc::Matrix2D operator*(const rcsc::Matrix2D &lhs, const rcsc::Matrix2D &rhs)
multiplication operator of Matrix x Matrix.
Definition: matrix_2d.h:458
2d vector class Header File.