soma-io 6.0.6
affinetransformation3d_base.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
34/*
35 * Non elastic AffineTransformation3d ( rotation + translation )
36 */
37#ifndef SOMAIO_TRANSFORMATION_AFFINETRANSFORMATION3D_BASE_H
38#define SOMAIO_TRANSFORMATION_AFFINETRANSFORMATION3D_BASE_H
39
42#include <iosfwd>
43
44namespace soma
45{
46
48 {
49 public:
50
51 template <typename T>
52 class Table : public std::vector<T>
53 {
54 public:
55 Table(unsigned ncols, unsigned nlines)
56 : std::vector<T>(ncols*nlines, 0), ncols(ncols) {}
57 Table(unsigned nitems)
58 : std::vector<T>(nitems, 0), ncols(4) {}
59 Table( const Table & other )
60 : std::vector<T>()
61 {
62 *this = other;
63 }
64 Table( const std::vector<T> & other )
65 : std::vector<T>( other ), ncols( 4 )
66 {
67 }
68
69 T & operator () ( int x, int y=0 )
70 {
71 return (*this)[ y*ncols + x ];
72 }
73
74 T operator () ( int x, int y=0 ) const
75 {
76 return (*this)[ y*ncols + x ];
77 }
78
79 Table & operator = ( const Table & other )
80 {
81 if( &other == this )
82 return *this;
83 ncols = other.ncols;
84 this->std::vector<T>::operator = ( other );
85 return *this;
86 }
87
88 unsigned dimX() const { return ncols; }
89 unsigned ncols;
90 };
91
95 AffineTransformationBase( const std::vector<float> & mat );
99
100 virtual AffineTransformationBase &operator = ( const AffineTransformationBase& other );
101 virtual AffineTransformationBase &operator = ( const std::vector<float> & mat );
102 virtual AffineTransformationBase &operator = ( const carto::Object mat );
103
104 virtual bool operator == ( const AffineTransformationBase & ) const;
105
107 const AffineTransformationBase & trans );
109 void negate();
110
112 virtual void setToIdentity();
113
114 bool isDirect() const override;
115
119
120 int order() const { return _matrix.ncols - 1; }
121 virtual void extendOrder( unsigned n );
122 virtual void squeezeOrder( unsigned n, bool check = true,
123 bool notify_fail = true );
124
125 virtual std::vector<double> transform(
126 const std::vector<double>& pos ) const;
127 virtual std::vector<float>
128 transform( const std::vector<float> & pos ) const
129 { return this->Transformation::transform( pos ); }
130 virtual std::vector<int> transform( const std::vector<int> & pos ) const
131 { return this->Transformation::transform( pos ); }
132
134 const Table<float> & matrix() const { return _matrix; }
135
137 std::vector<float> toVector() const;
140 std::vector<float> toColumnVector() const;
143 void fromColumnVector( const std::vector<float> & vec )
144 {
145 return fromColumnVector( &vec[0], vec.size() );
146 }
147
149 void fromColumnVector( const float* vec, unsigned size );
150
152 static float epsilon;
153
154 protected:
155
156 // column-vector to be compatible with the former Volume storage with
157 // math notation
159 };
160
161 //---------------------------------------------------------------------------
162
163 //--------------------------//
164 // AffineTransformation3d //
165 //--------------------------//
166 //---------------------------------------------------------------------------
184 {
185 public:
186
191 AffineTransformation3dBase( const std::vector<float> & mat );
195 virtual AffineTransformation3dBase &operator = (
196 const AffineTransformation3dBase& other );
197 virtual AffineTransformation3dBase &operator = (
198 const std::vector<float> & other );
199 virtual AffineTransformation3dBase &operator = (
200 const carto::Object other );
201
203 const AffineTransformation3dBase & trans );
205
206 virtual bool operator == ( const AffineTransformation3dBase & ) const;
207
208 virtual void extendOrder( unsigned n );
209
210 Point3dd transform( double x, double y, double z ) const
211 { return this->Transformation3d::transform( x, y, z ); }
212 Point3dd transform( const Point3dd & pos ) const
213 { return this->Transformation3d::transform( pos ); }
214 Point3df transform( const Point3df & pos ) const
215 { return this->Transformation3d::transform( pos ); }
216 Point3df transform( float x, float y, float z ) const
217 { return this->Transformation3d::transform( x, y, z ); }
218 Point3d transform( const Point3d & p ) const
219 { return this->Transformation3d::transform( p ); }
220 Point3di transform( const Point3di & p ) const
221 { return this->Transformation3d::transform( p ); }
222 Point3di transform( int x, int y, int z ) const
223 { return this->Transformation3d::transform( x, y, z ); }
224 virtual std::vector<double>
225 transform( const std::vector<double> & pos ) const
226 { return this->AffineTransformationBase::transform( pos ); }
227 virtual std::vector<float>
228 transform( const std::vector<float> & pos ) const
229 { return this->Transformation::transform( pos ); }
230 virtual std::vector<int> transform( const std::vector<int> & pos ) const
231 { return this->Transformation::transform( pos ); }
232
233 Point3dd transformVector( const Point3dd & vec ) const;
234 Point3df transformVector( const Point3df & dir ) const;
235 Point3df transformVector( const Point3di & dir ) const;
236 Point3dd transformVector( double x, double y, double z ) const;
237 Point3df transformVector( float x, float y, float z ) const;
238 Point3di transformVector( int x, int y, int z ) const;
239 virtual std::vector<double>
240 transformVector( const std::vector<double> & pos ) const;
241 virtual std::vector<float>
242 transformVector( const std::vector<float> & pos ) const;
243 virtual std::vector<int>
244 transformVector( const std::vector<int> & pos ) const;
245
246 Point3dd transformNormal( const Point3dd & dir ) const;
247 Point3df transformNormal( const Point3df & dir ) const;
248 Point3dd transformNormal( double x, double y, double z ) const;
249 Point3df transformNormal( float x, float y, float z ) const;
250 Point3dd transformUnitNormal( const Point3dd & dir ) const;
251 Point3df transformUnitNormal( const Point3df & dir ) const;
252 Point3dd transformUnitNormal( double x, double y, double z ) const;
253 Point3df transformUnitNormal( float x, float y, float z ) const;
254
256 virtual void setToIdentity();
257
258 // AffineTransformation3d algebraic operation
262 virtual void scale( const Point3df& sizeFrom, const Point3df& sizeTo );
263
264 //Initialisation
268 void fromColumnVector( const float* vec );
269
270 protected:
271 Point3dd transformDouble( double x, double y, double z ) const CARTO_OVERRIDE;
272 Point3df transformFloat( float x, float y, float z ) const CARTO_OVERRIDE;
273 Point3di transformInt( int x, int y, int z ) const CARTO_OVERRIDE;
274
275 virtual Point3dd transformVectorPoint3dd( const Point3dd & vec ) const;
276 virtual Point3df transformVectorPoint3df( const Point3df & dir ) const;
277 virtual Point3dd transformVectorDouble( double x, double y, double z ) const;
278 virtual Point3df transformVectorFloat( float x, float y, float z ) const;
279 virtual Point3di transformVectorInt( int x, int y, int z ) const;
280 virtual Point3dd transformNormalPoint3dd( const Point3dd & dir ) const;
281 virtual Point3df transformNormalPoint3df( const Point3df & dir ) const;
282 virtual Point3dd transformNormalDouble( double x, double y, double z ) const;
283 virtual Point3df transformNormalFloat( float x, float y, float z ) const;
284
285 };
286
287
288 // Compose AffineTransformation3d
290 operator * ( const soma::AffineTransformation3dBase&
291 affineTransformation3d1,
293 affineTransformation3d2 );
294
296 operator * ( const soma::AffineTransformationBase&
297 affineTransformation1,
299 affineTransformation2 );
300
301 std::ostream&
302 operator << ( std::ostream& os,
303 const soma::AffineTransformationBase & thing );
304
305 inline Point3dd
306 AffineTransformation3dBase::transformVector( double x, double y, double z ) const
307 {
308 return transformVectorDouble( x, y, z );
309 }
310
311
312 inline Point3df
313 AffineTransformation3dBase::transformVector( float x, float y, float z ) const
314 {
315 return transformVectorFloat( x, y, z );
316 }
317
318
319 inline Point3di
321 {
322 return transformVectorInt( x, y, z );
323 }
324
325
326 inline Point3df
328 {
329 return transformVectorPoint3df( pos );
330 }
331
332
333 inline Point3dd
335 {
336 return transformVectorPoint3dd( pos );
337 }
338
339
340 inline std::vector<double>
342 const std::vector<double> & pos ) const
343 {
344 std::vector<double> tr = pos;
345 Point3dd p = transformVectorDouble( pos[0], pos[1], pos[2] );
346 tr[0] = p[0];
347 tr[1] = p[1];
348 tr[2] = p[2];
349 return tr;
350 }
351
352
353 inline std::vector<float>
355 const std::vector<float> & pos ) const
356 {
357 std::vector<float> tr = pos;
358 Point3df p = transformVectorFloat( pos[0], pos[1], pos[2] );
359 tr[0] = p[0];
360 tr[1] = p[1];
361 tr[2] = p[2];
362 return tr;
363 }
364
365
366 inline std::vector<int>
368 const std::vector<int> & pos ) const
369 {
370 std::vector<int> tr = pos;
371 Point3di p = transformVectorInt( pos[0], pos[1], pos[2] );
372 tr[0] = p[0];
373 tr[1] = p[1];
374 tr[2] = p[2];
375 return tr;
376 }
377
378
379 inline Point3dd
380 AffineTransformation3dBase::transformNormal( double x, double y, double z ) const
381 {
382 return transformNormalDouble( x, y, z );
383 }
384
385
386 inline Point3df
387 AffineTransformation3dBase::transformNormal( float x, float y, float z ) const
388 {
389 return transformNormalFloat( x, y, z );
390 }
391
392
393 inline Point3df
395 {
396 return transformNormalPoint3df( pos );
397 }
398
399
400 inline Point3dd
402 {
403 return transformNormalPoint3dd( pos );
404 }
405
406
407 inline Point3df
409 {
410 return transformVectorFloat( pos[0], pos[1], pos[2] );
411 }
412
413
414 inline Point3dd
416 {
417 return transformVector( pos[0], pos[1], pos[2] );
418 }
419
420
421 inline Point3df
423 {
424 Point3dd transformed = transformNormal( (double) pos[0], (double) pos[1],
425 (double) pos[2] );
426 return Point3df( (float) transformed[0], (float) transformed[1],
427 (float) transformed[2] );
428 }
429
430
431 inline Point3dd
433 {
434 return transformNormal( pos[0], pos[1], pos[2] );
435 }
436
437
438 inline Point3df
439 AffineTransformation3dBase::
440 transformNormalFloat( float x, float y, float z ) const
441 {
442 Point3dd transformed = transformNormal( (double) x, (double) y,
443 (double) z );
444 return Point3df( (float) transformed[0], (float) transformed[1],
445 (float) transformed[2] );
446 }
447
448
449 inline Point3dd
450 AffineTransformation3dBase::
451 transformUnitNormal( double x, double y, double z ) const
452 {
453 return transformNormal( x, y, z ).normalize();
454 }
455
456
457 inline Point3df
459 {
460 Point3dd transformed
461 = transformUnitNormal( (double) pos[0], (double) pos[1],
462 (double) pos[2] );
463 return Point3df( (float) transformed[0], (float) transformed[1],
464 (float) transformed[2] );
465 }
466
467
468 inline Point3dd
470 {
471 return transformUnitNormal( pos[0], pos[1], pos[2] );
472 }
473
474
475 inline Point3df
476 AffineTransformation3dBase::
477 transformUnitNormal( float x, float y, float z ) const
478 {
479 Point3dd transformed = transformUnitNormal( (double) x, (double) y,
480 (double) z );
481 return Point3df( (float) transformed[0], (float) transformed[1],
482 (float) transformed[2] );
483 }
484
485}
486
487
488#endif
489
#define unique_ptr
#define CARTO_OVERRIDE
virtual Point3df transformNormalPoint3df(const Point3df &dir) const
Point3dd transform(double x, double y, double z) const
virtual Point3dd transformVectorDouble(double x, double y, double z) const
Point3di transform(int x, int y, int z) const
AffineTransformation3dBase(const carto::Object mat)
Create a AffineTransformation3d from a 4x4 matrix given as a line vector in an Object.
std::unique_ptr< Transformation > getInverse() const CARTO_OVERRIDE
Obtain the inverse transformation.
Point3dd transformNormal(const Point3dd &dir) const
AffineTransformation3dBase(const AffineTransformation3dBase &other)
virtual std::vector< float > transform(const std::vector< float > &pos) const
virtual std::vector< double > transform(const std::vector< double > &pos) const
virtual Point3dd transformVectorPoint3dd(const Point3dd &vec) const
virtual void scale(const Point3df &sizeFrom, const Point3df &sizeTo)
Point3dd transformUnitNormal(const Point3dd &dir) const
Point3dd transformDouble(double x, double y, double z) const CARTO_OVERRIDE
virtual Point3dd transformNormalDouble(double x, double y, double z) const
bool isIdentity() const CARTO_OVERRIDE
Test if the transformation can safely be omitted.
virtual Point3df transformVectorPoint3df(const Point3df &dir) const
virtual Point3dd transformNormalPoint3dd(const Point3dd &dir) const
Point3df transform(float x, float y, float z) const
virtual Point3df transformNormalFloat(float x, float y, float z) const
Point3d transform(const Point3d &p) const
void setTranslation(Point3df trans)
Point3di transformInt(int x, int y, int z) const CARTO_OVERRIDE
Point3di transform(const Point3di &p) const
Point3df transformFloat(float x, float y, float z) const CARTO_OVERRIDE
Point3dd transformVector(const Point3dd &vec) const
virtual Point3di transformVectorInt(int x, int y, int z) const
virtual std::vector< int > transform(const std::vector< int > &pos) const
std::unique_ptr< AffineTransformation3dBase > inverse() const
bool invertible() const CARTO_OVERRIDE
Test if the transformation can be inverted.
virtual void extendOrder(unsigned n)
void fromColumnVector(const float *vec)
transform a column vector to an AffineTransformation3d (useful for conversions from OpenGL matrices)
Point3dd transform(const Point3dd &pos) const
AffineTransformation3dBase(const std::vector< float > &mat)
Create a AffineTransformation3d from a 4x4 matrix given as a line vector.
virtual Point3df transformVectorFloat(float x, float y, float z) const
AffineTransformation3dBase()
Create an identity transformation.
Point3df transform(const Point3df &pos) const
Point3df transformVector(const Point3di &dir) const
std::unique_ptr< Transformation > getInverse() const CARTO_OVERRIDE
Obtain the inverse transformation.
std::vector< float > toVector() const
conversions and IO)
static float epsilon
used to test equality on elements
AffineTransformationBase(const carto::Object mat)
Create a AffineTransformation from a NxN matrix given as a line vector in an Object.
AffineTransformationBase(const std::vector< float > &mat)
Create a AffineTransformation3d from a matrix given as a line vector.
virtual void squeezeOrder(unsigned n, bool check=true, bool notify_fail=true)
bool isIdentity() const CARTO_OVERRIDE
Test if the transformation can safely be omitted.
const Table< float > & matrix() const
std::vector< float > toColumnVector() const
transform AffineTransformation3d to a column vector (useful for conversions to OpenGL matrices)
bool invertible() const CARTO_OVERRIDE
Test if the transformation can be inverted.
virtual std::vector< int > transform(const std::vector< int > &pos) const
virtual std::vector< double > transform(const std::vector< double > &pos) const
void fromColumnVector(const std::vector< float > &vec)
transform a column vector to an AffineTransformation3d (useful for conversions from OpenGL matrices)
void fromColumnVector(const float *vec, unsigned size)
transform a column vector to an AffineTransformation3d (useful for conversions from OpenGL matrices)
virtual std::vector< float > transform(const std::vector< float > &pos) const
bool isDirect() const override
true if the transformation is direct, false if it changes orientation
virtual void extendOrder(unsigned n)
std::unique_ptr< AffineTransformationBase > inverse() const
AffineTransformationBase(const AffineTransformationBase &other)
Point3dd transform(double x, double y, double z) const
Polymorphic base class for spatial transformations.
virtual std::vector< double > transform(const std::vector< double > &pos) const =0
STL namespace.
AimsVector< float, 3 > Point3df
Definition vector.h:247
AimsVector< int16_t, 3 > Point3d
Definition vector.h:222
AimsVector< int32_t, 3 > Point3di
Definition vector.h:232
AimsVector< T, D > operator-(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
Definition vector.h:887
AimsVector< double, 3 > Point3dd
Definition vector.h:250