LARS
LARS (Light Augmented Reality System) is an open-source framework for light-based interaction and real-time tracking in multi-robot experiments. Inspired by ARK, LARS extends the augmented reality paradigm to robotic collectives by projecting dynamic visual cues and environments onto the arena, enabling new experimental possibilities for collective robotics research, education, and outreach. LARS features integrated tracking, light projection, and modular experiment control with a user-friendly Qt GUI.
Loading...
Searching...
No Matches
matrix_2d.h
Go to the documentation of this file.
1// -*-c++-*-
2
7
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 <geom/vector_2d.h>
36#include <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
98 {
99 M_11 = M_22 = 1.0;
100 M_12 = M_21 = M_dx = M_dy = 0.0;
101 return *this;
102 }
103
114 const
115 Matrix2D & assign( const double & m11, const double & m12,
116 const double & m21, const double & m22,
117 const double & dx, const double & dy )
118 {
119 M_11 = m11; M_12 = m12;
120 M_21 = m21; M_22 = m22;
121 M_dx = dx; M_dy = dy;
122 return *this;
123 }
124
131 static
132 Matrix2D make_translation( const double & dx,
133 const double & dy )
134 {
135 return Matrix2D( 1.0, 0.0,
136 0.0, 1.0,
137 dx, dy );
138 }
139
146 static
147 Matrix2D make_scaling( const double & sx,
148 const double & sy )
149 {
150 return Matrix2D( sx, 0.0,
151 0.0, sy,
152 0.0, 0.0 );
153 }
154
160 static
162 {
163 double cosa = angle.cos();
164 double sina = angle.sin();
165 return Matrix2D( cosa, -sina,
166 sina, cosa,
167 0.0, 0.0 );
168 }
169
174 const
175 double & m11() const
176 {
177 return M_11;
178 }
179
184 const
185 double & m12() const
186 {
187 return M_12;
188 }
189
194 const
195 double & m21() const
196 {
197 return M_21;
198 }
199
204 const
205 double & m22() const
206 {
207 return M_22;
208 }
209
214 const
215 double & dx() const
216 {
217 return M_dx;
218 }
219
224 const
225 double & dy() const
226 {
227 return M_dy;
228 }
229
234 double det() const
235 {
236 return M_11*M_22 - M_12*M_21;
237 }
238
243 bool invertible() const
244 {
245 return ! ( std::fabs( det() ) < 0.00000000001 );
246 }
247
252 Matrix2D inverted() const;
253
263 Matrix2D & translate( const double & dx,
264 const double & dy )
265 {
266 // translation matrix
267 // T = ( 1, 0, dx )
268 // ( 0, 1, dy )
269 // ( 0, 0, 1 )
270
271 /*
272 // this = this * T
273 M_dx += M_11*dx + M_12*dy;
274 M_dy += M_21*dx + M_22*dy;
275 */
276
277 // this = T * this
278 // *this = make_translation(dx,dy) * *this;
279
280 M_dx += dx;
281 M_dy += dy;
282 return *this;
283 }
284
294 Matrix2D & scale( const double & sx,
295 const double & sy )
296 {
297 // scaling matrixa
298 // S = ( Sx, 0, 0 )
299 // ( 0, Sy, 0 )
300 // ( 0, 0, 1 )
301
302 /*
303 this = this * S
304 *this *= make_scaling(sx,sy)
305 M_11 *= sx; M_12 *= sy;
306 M_21 *= sx; M_22 *= sy;
307 */
308
309 // this = S * this
310 // *this = make_scaling(sx,sy) * *this;
311
312 M_11 *= sx; M_12 *= sx; M_dx *= sx;
313 M_21 *= sy; M_22 *= sy; M_dy *= sy;
314 return *this;
315 }
316
317 /*
318 Matrix2D & shear( const double & sh,
319 const double & sv )
320 {
321 double tm11 = sv * M_21;
322 double tm12 = sv * M_22;
323 double tm21 = sh * M_11;
324 double tm22 = sh * M_12;
325 M_11 += tm11; M_12 += tm12;
326 M_21 += tm21; M_22 += tm22;
327 return *this;
328 }
329 */
330
339 Matrix2D & rotate( const AngleDeg & angle );
340
346 const
348 {
349 double tm11 = M_11*m.M_11 + M_12*m.M_21;
350 double tm12 = M_11*m.M_12 + M_12*m.M_22;
351 double tm21 = M_21*m.M_11 + M_22*m.M_21;
352 double tm22 = M_21*m.M_12 + M_22*m.M_22;
353
354 double tdx = M_11*m.M_dx + M_12*m.M_dy + M_dx;
355 double tdy = M_21*m.M_dx + M_22*m.M_dy + M_dy;
356
357 M_11 = tm11; M_12 = tm12;
358 M_21 = tm21; M_22 = tm22;
359 M_dx = tdx; M_dy = tdy;
360 return *this;
361 }
362
368 Vector2D transform( const Vector2D & v ) const
369 {
370 return Vector2D( M_11*v.x + M_12*v.y + M_dx,
371 M_21*v.x + M_22*v.y + M_dy );
372 }
373
380 Vector2D transform( const double & x,
381 const double & y ) const
382 {
383 return Vector2D( M_11*x + M_12*y + M_dx,
384 M_21*x + M_22*y + M_dy );
385 }
386
391 void transform( Vector2D * v ) const
392 {
393 double tx = M_11*v->x + M_12*v->y + M_dx;
394 double ty = M_21*v->x + M_22*v->y + M_dy;
395 v->assign( tx, ty );
396 }
397
398#if 0
399 Segment2D transform( const Segment2D & s ) const
400 {
401 return Segment2D( transform( s.a() ),
402 transform( s.b() ) );
403 }
404
405 /*
406 Line2D transform( const Line2D & l ) const
407 {
408 }
409 */
410
411 Rai2D transform( const Ray2D & r ) const
412 {
413 return Ray2D( transform( r.origin() ),
414 transform( r.origin() + Vector2D::polar2vector( 1.0, r.dir() ) ) );
415 }
416
417 Circle2D transform( const Circle2D & c ) const
418 {
419 return Circle2D( transform( c.center() ),
420 c.radius() );
421 }
422
423 /*
424 Sector2D transform( const Sector2D & s ) const
425 {
426
427 }
428 */
429
430 Triangle2D transform( const Triangle2D & t ) const
431 {
432 return Triangle2D( transform( t.a() ),
433 transform( t.b() ),
434 transform( t.c() ) );
435 }
436#endif
437
443 std::ostream & print( std::ostream & os ) const
444 {
445 os << M_11 << ' '
446 << M_12 << ' '
447 << M_21 << ' '
448 << M_22 << ' '
449 << M_dx << ' '
450 << M_dy;
451 return os;
452 }
453
454};
455
456}
457
464inline
465const
468 const rcsc::Matrix2D & rhs )
469{
470 return rcsc::Matrix2D( lhs ) *= rhs;
471}
472
479inline
482 const rcsc::Vector2D & rhs )
483{
484 return lhs.transform( rhs );
485}
486
493inline
494std::ostream &
495operator<<( std::ostream & os,
496 const rcsc::Matrix2D & m )
497{
498 return m.print( os );
499}
500
501
502#endif
degree wrapper class Header File.
degree wrapper class
Definition angle_deg.h:45
double cos() const
calculate cosine
Definition angle_deg.h:301
double sin() const
calculate sine
Definition angle_deg.h:310
2D translation matrix class
Definition matrix_2d.h:51
Matrix2D & rotate(const AngleDeg &angle)
rotates the coordinate system
Definition matrix_2d.cpp:66
std::ostream & print(std::ostream &os) const
put all elemtns to the output stream
Definition matrix_2d.h:443
Matrix2D & translate(const double &dx, const double &dy)
moves the coordinate system.
Definition matrix_2d.h:263
const double & dx() const
get the horizontal translation factor.
Definition matrix_2d.h:215
double det() const
get the matrix's determinant.
Definition matrix_2d.h:234
bool invertible() const
check if this matrix is invertible (is not isingular).
Definition matrix_2d.h:243
Matrix2D inverted() const
get the inverted matrix.
Definition matrix_2d.cpp:45
static Matrix2D make_translation(const double &dx, const double &dy)
create the translation matrix.
Definition matrix_2d.h:132
const Matrix2D & operator*=(const Matrix2D &m)
multiplied by other matrix
Definition matrix_2d.h:347
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:115
const double & m11() const
get the horizontal scaling factor.
Definition matrix_2d.h:175
void transform(Vector2D *v) const
transform input vector with this matrix
Definition matrix_2d.h:391
const double & m22() const
get the vertical scaling factor.
Definition matrix_2d.h:205
Matrix2D & scale(const double &sx, const double &sy)
scales the coordinate system.
Definition matrix_2d.h:294
Vector2D transform(const Vector2D &v) const
create transformed vector from input vector with this matrix
Definition matrix_2d.h:368
const double & dy() const
get the vertical translation factor.
Definition matrix_2d.h:225
Matrix2D()
create an identity matrix
Definition matrix_2d.h:66
const double & m21() const
get the horizontal shearing factor.
Definition matrix_2d.h:195
static Matrix2D make_scaling(const double &sx, const double &sy)
create the scaling matrix.
Definition matrix_2d.h:147
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:161
const Matrix2D & reset()
reset to the identity matrix
Definition matrix_2d.h:97
const double & m12() const
get the vertical shearing factor.
Definition matrix_2d.h:185
Vector2D transform(const double &x, const double &y) const
create transformed vector from input coordinates with this matrix
Definition matrix_2d.h:380
2d segment line class
Definition segment_2d.h:46
2D point vector class
Definition vector_2d.h:47
static Vector2D polar2vector(const double &mag, const AngleDeg &theta)
get new Vector created by POLAR value.
Definition vector_2d.h:560
double y
Y coordinate.
Definition vector_2d.h:65
double x
X coordinate.
Definition vector_2d.h:64
Vector2D & assign(const double &xx, const double &yy)
assign XY value directly.
Definition vector_2d.h:101
2d vector class Header File.
std::ostream & operator<<(std::ostream &os, const rcsc::Matrix2D &m)
output stream operator.
Definition matrix_2d.h:495
const rcsc::Matrix2D operator*(const rcsc::Matrix2D &lhs, const rcsc::Matrix2D &rhs)
multiplication operator of Matrix x Matrix.
Definition matrix_2d.h:467