soma-io  5.1.2
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 
44 namespace soma
45 {
46 
47  //---------------------------------------------------------------------------
48 
49  //--------------------------//
50  // AffineTransformation3d //
51  //--------------------------//
52  //---------------------------------------------------------------------------
93  {
94  public:
95 
96  template <typename T>
97  class Table : public std::vector<T>
98  {
99  public:
100  Table(unsigned ncols, unsigned nlines)
101  : std::vector<T>(ncols*nlines, 0), ncols(ncols) {}
102  Table(unsigned nitems)
103  : std::vector<T>(nitems, 0), ncols(4) {}
104  Table( const Table & other )
105  : std::vector<T>()
106  {
107  *this = other;
108  }
109  Table( const std::vector<T> & other )
110  : std::vector<T>( other ), ncols( 4 )
111  {
112  }
113 
114  T & operator () ( int x, int y=0 )
115  {
116  return (*this)[ y*ncols + x ];
117  }
118 
119  T operator () ( int x, int y=0 ) const
120  {
121  return (*this)[ y*ncols + x ];
122  }
123 
124  Table & operator = ( const Table & other )
125  {
126  if( &other == this )
127  return *this;
128  ncols = other.ncols;
129  this->std::vector<T>::operator = ( other );
130  return *this;
131  }
132 
133  unsigned dimX() const { return ncols; }
134  unsigned ncols;
135  };
136 
137 
142  AffineTransformation3dBase( const std::vector<float> & mat );
147  virtual AffineTransformation3dBase &operator = ( const std::vector<float> & mat );
149 
150  virtual bool operator == ( const AffineTransformation3dBase & ) const;
151 
153  const AffineTransformation3dBase & trans );
155 
156  Point3dd transformVector( const Point3dd & vec ) const;
157  Point3df transformVector( const Point3df & dir ) const;
158  Point3dd transformVector( double x, double y, double z ) const;
159  Point3df transformVector( float x, float y, float z ) const;
160  Point3dd transformNormal( const Point3dd & dir ) const;
161  Point3df transformNormal( const Point3df & dir ) const;
162  Point3dd transformNormal( double x, double y, double z ) const;
163  Point3df transformNormal( float x, float y, float z ) const;
164  Point3dd transformUnitNormal( const Point3dd & dir ) const;
165  Point3df transformUnitNormal( const Point3df & dir ) const;
166  Point3dd transformUnitNormal( double x, double y, double z ) const;
167  Point3df transformUnitNormal( float x, float y, float z ) const;
168 
169  bool isIdentity() const CARTO_OVERRIDE;
170  virtual void setToIdentity();
171 
172  // AffineTransformation3d algebraic operation
174  bool invertible() const CARTO_OVERRIDE;
175  std::unique_ptr<Transformation3d> getInverse() const CARTO_OVERRIDE;
176  virtual void scale( const Point3df& sizeFrom, const Point3df& sizeTo );
178  bool isDirect() const;
179 
180  //Initialisation
183  std::vector<float> toVector() const;
186  std::vector<float> toColumnVector() const;
189  void fromColumnVector( const std::vector<float> & vec )
190  {
191  return fromColumnVector( &vec[0] );
192  }
195  void fromColumnVector( const float* vec );
196  Table<float> & matrix() { return _matrix; }
197  const Table<float> & matrix() const { return _matrix; }
198 
199  protected:
200  Point3dd transformDouble( double x, double y, double z ) const CARTO_OVERRIDE;
201  Point3df transformFloat( float x, float y, float z ) const CARTO_OVERRIDE;
202 
203  virtual Point3dd transformVectorPoint3dd( const Point3dd & vec ) const;
204  virtual Point3df transformVectorPoint3df( const Point3df & dir ) const;
205  virtual Point3dd transformVectorDouble( double x, double y, double z ) const;
206  virtual Point3df transformVectorFloat( float x, float y, float z ) const;
207  virtual Point3dd transformNormalPoint3dd( const Point3dd & dir ) const;
208  virtual Point3df transformNormalPoint3df( const Point3df & dir ) const;
209  virtual Point3dd transformNormalDouble( double x, double y, double z ) const;
210  virtual Point3df transformNormalFloat( float x, float y, float z ) const;
211 
212  // column-vector to be compatible with the former Volume storage with
213  // math notation
215  };
216 
217 
218  // Compose AffineTransformation3d
221  affineTransformation3d1,
223  affineTransformation3d2 );
224 
225  std::ostream&
226  operator << ( std::ostream& os,
227  const soma::AffineTransformation3dBase & thing );
228 
229  inline Point3dd
230  AffineTransformation3dBase::transformVector( double x, double y, double z ) const
231  {
232  return transformVectorDouble( x, y, z );
233  }
234 
235 
236  inline Point3df
237  AffineTransformation3dBase::transformVector( float x, float y, float z ) const
238  {
239  return transformVectorFloat( x, y, z );
240  }
241 
242 
243  inline Point3df
245  {
246  return transformVectorPoint3df( pos );
247  }
248 
249 
250  inline Point3dd
252  {
253  return transformVectorPoint3dd( pos );
254  }
255 
256 
257  inline Point3dd
258  AffineTransformation3dBase::transformNormal( double x, double y, double z ) const
259  {
260  return transformNormalDouble( x, y, z );
261  }
262 
263 
264  inline Point3df
265  AffineTransformation3dBase::transformNormal( float x, float y, float z ) const
266  {
267  return transformNormalFloat( x, y, z );
268  }
269 
270 
271  inline Point3df
273  {
274  return transformNormalPoint3df( pos );
275  }
276 
277 
278  inline Point3dd
280  {
281  return transformNormalPoint3dd( pos );
282  }
283 
284 
285  inline Point3df
287  {
288  return transformVectorFloat( pos[0], pos[1], pos[2] );
289  }
290 
291 
292  inline Point3dd
294  {
295  return transformVector( pos[0], pos[1], pos[2] );
296  }
297 
298 
299  inline Point3df
301  {
302  Point3dd transformed = transformNormal( (double) pos[0], (double) pos[1],
303  (double) pos[2] );
304  return Point3df( (float) transformed[0], (float) transformed[1],
305  (float) transformed[2] );
306  }
307 
308 
309  inline Point3dd
311  {
312  return transformNormal( pos[0], pos[1], pos[2] );
313  }
314 
315 
316  inline Point3df
318  transformNormalFloat( float x, float y, float z ) const
319  {
320  Point3dd transformed = transformNormal( (double) x, (double) y,
321  (double) z );
322  return Point3df( (float) transformed[0], (float) transformed[1],
323  (float) transformed[2] );
324  }
325 
326 
327  inline Point3dd
329  transformUnitNormal( double x, double y, double z ) const
330  {
331  return transformNormal( x, y, z ).normalize();
332  }
333 
334 
335  inline Point3df
337  {
338  Point3dd transformed
339  = transformUnitNormal( (double) pos[0], (double) pos[1],
340  (double) pos[2] );
341  return Point3df( (float) transformed[0], (float) transformed[1],
342  (float) transformed[2] );
343  }
344 
345 
346  inline Point3dd
348  {
349  return transformUnitNormal( pos[0], pos[1], pos[2] );
350  }
351 
352 
353  inline Point3df
355  transformUnitNormal( float x, float y, float z ) const
356  {
357  Point3dd transformed = transformUnitNormal( (double) x, (double) y,
358  (double) z );
359  return Point3df( (float) transformed[0], (float) transformed[1],
360  (float) transformed[2] );
361  }
362 
363 }
364 
365 
366 #endif
367 
#define CARTO_OVERRIDE
The template class to implement basic vectors.
Definition: vector.h:137
AimsVector< T, D > & normalize()
Definition: vector.h:719
virtual Point3df transformNormalPoint3df(const Point3df &dir) const
virtual Point3dd transformVectorDouble(double x, double y, double z) const
bool isDirect() const
true if the transformation is direct, false if it changes orientation
AffineTransformation3dBase(const carto::Object mat)
Create a AffineTransformation3d from a 4x4 matrix given as a line vector in an Object.
Point3dd transformNormal(const Point3dd &dir) const
const Table< float > & matrix() const
AffineTransformation3dBase(const AffineTransformation3dBase &other)
std::unique_ptr< Transformation3d > getInverse() const CARTO_OVERRIDE
Obtain the inverse transformation.
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
virtual Point3df transformNormalFloat(float x, float y, float z) const
void setTranslation(Point3df trans)
Point3df transformFloat(float x, float y, float z) const CARTO_OVERRIDE
std::vector< float > toVector() const
conversions and IO)
virtual AffineTransformation3dBase & operator=(const AffineTransformation3dBase &other)
Point3dd transformVector(const Point3dd &vec) const
bool invertible() const CARTO_OVERRIDE
Test if the transformation can be inverted.
AffineTransformation3dBase inverse() const
void fromColumnVector(const float *vec)
transform a column vector to an AffineTransformation3d (useful for conversions from OpenGL matrices)
AffineTransformation3dBase(const std::vector< float > &mat)
Create a AffineTransformation3d from a 4x4 matrix given as a line vector.
AffineTransformation3dBase & operator*=(const AffineTransformation3dBase &trans)
void fromColumnVector(const std::vector< float > &vec)
transform a column vector to an AffineTransformation3d (useful for conversions from OpenGL matrices)
virtual Point3df transformVectorFloat(float x, float y, float z) const
AffineTransformation3dBase()
Create an identity transformation.
std::vector< float > toColumnVector() const
transform AffineTransformation3d to a column vector (useful for conversions to OpenGL matrices)
AffineTransformation3dBase operator-() const
virtual bool operator==(const AffineTransformation3dBase &) const
Polymorphic base class for spatial transformations in 3D.
Definition: allocator.h:49
std::ostream & operator<<(std::ostream &os, const MemoryAllocator &thing)
soma::AffineTransformation3dBase operator*(const soma::AffineTransformation3dBase &affineTransformation3d1, const soma::AffineTransformation3dBase &affineTransformation3d2)
AimsVector< float, 3 > Point3df
Definition: vector.h:247