HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_CoordSpaceImpl.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_CoordSpaceImpl.h (UT Library, C++)
7  *
8  * COMMENTS:
9  * A 3D system of coordinates.
10  *
11  */
12 
13 #ifndef __UT_COORDSPACEIMPL_H_INCLUDED__
14 #define __UT_COORDSPACEIMPL_H_INCLUDED__
15 
16 #include "UT_Assert.h"
17 #include "UT_Vector3.h"
18 #include "UT_Matrix3.h"
19 #include "UT_Matrix4.h"
20 #include <SYS/SYS_Math.h>
21 
22 
23 template <typename T>
25  : myPlane(ptype)
26 {
27  switch (ptype)
28  {
29  case UT_PLANE_XY:
30  myXAxis.assign(1.0, 0.0, 0.0); // world X
31  myYAxis.assign(0.0, 1.0, 0.0); // world Y
32  break;
33  case UT_PLANE_YZ:
34  myXAxis.assign(0.0, 1.0, 0.0); // world Y
35  myYAxis.assign(0.0, 0.0, 1.0); // world Z
36  break;
37  default:
38  UT_ASSERT(!"Invalid plane type");
40  case UT_PLANE_XZ:
41  myXAxis.assign(0.0, 0.0, 1.0); // world Z
42  myYAxis.assign(1.0, 0.0, 0.0); // world X
43  break;
44  }
45 }
46 
47 template <typename T>
49  const UT_Vector3T<T> &pt,
50  const UT_Vector3T<T> &axaxis,
51  const UT_Vector3T<T> &ayaxis,
52  const UT_Vector3T<T> *azaxis,
53  bool norm
54  )
55  : myPlane(pt)
56 {
57  myXAxis = axaxis;
58  myYAxis = ayaxis;
59 
60  if (norm)
61  {
62  myXAxis.normalize();
63  myYAxis.normalize();
64  }
65 
67  if (azaxis)
68  {
69  n = *azaxis;
70  if (norm)
71  n.normalize();
72  }
73  else
74  {
75  n = cross(axaxis, ayaxis);
76  n.normalize();
77  }
78  myPlane.setNormal(n);
79 }
80 
81 template <typename T>
83  const UT_Vector3T<T> &p0,
84  const UT_Vector3T<T> &p1,
85  const UT_Vector3T<T> &p2
86  )
87  : myPlane(p0, p1, p2)
88 {
89  myXAxis = p1 - p0;
90  myXAxis.normalize();
91 
92  myYAxis = cross(myPlane.normal(), myXAxis);
93  myYAxis.normalize();
94 }
95 
96 template <typename T>
98 {
99  // nothing else needed
100 }
101 
102 template <typename T>
103 void
105 {
106  UT_Vector3T<T> oldy = myYAxis;
107  myYAxis = myXAxis;
108  myXAxis = oldy;
109 
110  myPlane.negate();
111 }
112 
113 template <typename T>
114 void
115 UT_CoordSpaceT<T>::xaxis(const UT_Vector3T<T> &x, bool norm, bool allow_flip)
116 {
117  UT_Vector3T<T> oldx = myXAxis;
118  UT_Matrix3T<T> matx;
119 
120  myXAxis = x; if (norm) myXAxis.normalize();
121 
122  if (matx.dihedral(oldx, myXAxis, false) != 0)
123  {
124  // New x-axis is opposed to the old. Use a rotation of 180 degrees
125  // about our y-axis, if allowed.
126  if (allow_flip)
127  {
128  matx.identity();
129  matx.rotate(myYAxis, M_PI, /* norm */ 0);
130  }
131  else
132  {
133  myXAxis = oldx;
134  return;
135  }
136  }
137 
138  myYAxis *= matx; myYAxis.normalize();
139  myPlane.rotateNormal(matx, true);
140 }
141 
142 template <typename T>
143 void
144 UT_CoordSpaceT<T>::yaxis(const UT_Vector3T<T> &y, bool norm, bool allow_flip)
145 {
146  UT_Vector3T<T> oldy = myYAxis;
147  UT_Matrix3T<T> matx;
148 
149  myYAxis = y; if (norm) myYAxis.normalize();
150 
151  if (matx.dihedral(oldy, myYAxis, false) != 0)
152  {
153  // New y-axis is opposed to the old. Use a rotation of 180 degrees
154  // about our z-axis, if allowed.
155  if (allow_flip)
156  {
157  matx.identity();
158  UT_Vector3T<T> nml = myPlane.normal();
159  matx.rotate(nml, M_PI, /* norm */ 0);
160  }
161  else
162  {
163  myYAxis = oldy;
164  return;
165  }
166  }
167 
168  myXAxis *= matx; myXAxis.normalize();
169  myPlane.rotateNormal(matx, true);
170 }
171 
172 template <typename T>
173 void
174 UT_CoordSpaceT<T>::zaxis(const UT_Vector3T<T> &z, bool norm, bool allow_flip)
175 {
176  UT_Vector3T<T> oldz = myPlane.normal();
177  UT_Matrix3T<T> matx;
178 
179  if (norm)
180  myPlane.setVectorAsNormal(z);
181  else
182  myPlane.setNormal(z);
183 
184  UT_Vector3T<T> n = myPlane.normal();
185  if (matx.dihedral(oldz, n, false) != 0)
186  {
187  // New z-axis is opposed to the old. Use a rotation of 180 degrees
188  // about our y-axis, if allowed.
189  if (allow_flip)
190  {
191  matx.identity();
192  matx.rotate(myYAxis, M_PI, /* norm */ 0);
193  }
194  else
195  {
196  myPlane.setNormal(oldz);
197  return;
198  }
199  }
200 
201  myXAxis *= matx; myXAxis.normalize();
202  myYAxis *= matx; myYAxis.normalize();
203 }
204 
205 template <typename T>
206 void
208  const UT_Vector3T<T> &x,
209  const UT_Vector3T<T> &y,
210  const UT_Vector3T<T> &z,
211  bool norm)
212 {
213  myXAxis = x;
214  myYAxis = y;
215 
216  if (norm)
217  {
218  myXAxis.normalize();
219  myYAxis.normalize();
220  myPlane.setVectorAsNormal(z);
221  }
222  else
223  {
224  myPlane.setNormal(z);
225  }
226 }
227 
228 template <typename T>
229 void
230 UT_CoordSpaceT<T>::rotate(UT_Vector3T<T> &axis, T theta, bool norm)
231 {
232  UT_Matrix3T<T> rmatx;
233  rmatx = UT_Matrix3T<T>::rotationMat(axis, theta, norm);
234  myPlane.rotatePoint(rmatx);
235 
236  rmatx.invert();
237  rmatx.transpose();
238 
239  myXAxis *= rmatx; myXAxis.normalize();
240  myYAxis *= rmatx; myYAxis.normalize();
241  myPlane.rotateNormal(rmatx, true);
242 }
243 
244 template <typename T>
245 void
247 {
248  UT_Matrix3T<T> rmatx;
249  rmatx = UT_Matrix3T<T>::rotationMat(a, theta);
250  myPlane.rotatePoint(rmatx);
251 
252  rmatx.invert();
253  rmatx.transpose();
254 
255  myXAxis *= rmatx; myXAxis.normalize();
256  myYAxis *= rmatx; myYAxis.normalize();
257  myPlane.rotateNormal(rmatx, true);
258 }
259 
260 template <typename T>
261 void
263 {
264  UT_Matrix4T<T> matx;
265  UT_Matrix4T<T> tmp;
266 
267  tmp = rotmatx;
268 
269  matx.identity();
270  matx.translate(-origin().x(), -origin().y(), -origin().z());
271  matx *= tmp;
272  matx.translate( origin().x(), origin().y(), origin().z());
273  transformAndReturnNormalXform(matx);
274 }
275 
276 template <typename T>
277 void
279 {
280  myPlane.transform(matx);
281 
282  UT_Matrix4F xform(matx);
283 
284  xform.invert();
285  xform.transpose();
286 
287  myXAxis *= xform; myXAxis.normalize();
288  myYAxis *= xform; myYAxis.normalize();
289 }
290 
291 template <typename T>
292 void
294 {
295  // The following call sets matx to the inverted and transposed matrix.
296  myPlane.transformAndReturnNormalXform(matx);
297 
298  myXAxis *= matx; myXAxis.normalize();
299  myYAxis *= matx; myYAxis.normalize();
300 }
301 
302 template <typename T>
303 void
305 {
306  transformAndReturnNormalXform(matx);
307 }
308 
309 template <typename T>
310 void
312 {
313  myPlane.transform(matx);
314 
315  UT_Matrix4D xform(matx);
316 
317  xform.invert();
318  xform.transpose();
319 
320  myXAxis *= xform; myXAxis.normalize();
321  myYAxis *= xform; myYAxis.normalize();
322 }
323 
324 template <typename T>
325 void
327 {
328  // The following call sets matx to the inverted and transposed matrix.
329  myPlane.transformAndReturnNormalXform(matx);
330 
331  myXAxis *= matx; myXAxis.normalize();
332  myYAxis *= matx; myYAxis.normalize();
333 }
334 
335 template <typename T>
336 void
338 {
339  transformAndReturnNormalXform(matx);
340 }
341 
342 template <typename T>
343 void
345 {
346  // Set our transform to the identity matrix.
347  myPlane.setPoint(UT_Vector3T<T>(0.0, 0.0, 0.0));
348  myPlane.setNormal(UT_PLANE_XY);
349  myXAxis.assign(1.0, 0.0, 0.0);
350  myYAxis.assign(0.0, 1.0, 0.0);
351 }
352 
353 // Multiples the input matrix by the change in coordinate space from world
354 // space into this one.
355 template <typename T>
356 template <typename MATX>
357 inline void
358 UT_CoordSpaceT<T>::changeSpace(MATX &matx) const
359 {
360  UT_Vector3T<T> world_x(1.0, 0.0, 0.0);
361  UT_Vector3T<T> world_y(0.0, 1.0, 0.0);
362 
363  matx.changeSpace(world_x, world_y,
364  const_cast<UT_CoordSpaceT<T> *>(this)->myXAxis,
365  const_cast<UT_CoordSpaceT<T> *>(this)->myYAxis,
366  /*norm*/ false);
367 }
368 
369 template <typename T>
370 void
372 {
373  matx.identity();
374  matx.translate(origin().x(), origin().y(), origin().z());
375  changeSpace(matx);
376 }
377 
378 template <typename T>
379 void
381  UT_Matrix4T<T> &matx) const
382 {
383  matx.identity();
384  changeSpace(matx);
385  matx.translate(origin().x(), origin().y(), origin().z());
386 }
387 
388 template <typename T>
389 int
391  const UT_XformOrder &order) const
392 {
393  UT_Vector3T<T> rvals;
394  UT_Matrix3T<T> rmatx;
395 
396  rmatx.identity();
397  changeSpace(rmatx);
398  int ret = rmatx.crack(rvals, order);
399 
400  rx = rvals[0]; ry = rvals[1]; rz = rvals[2];
401 
402  return ret;
403 }
404 
405 template <typename T>
406 int
408  const UT_XformOrder &order) const
409 {
410  UT_Vector3T<T> rvals;
411  UT_Matrix3T<T> rmatx;
412 
413  rmatx.identity();
414  changeSpace(rmatx);
415  rmatx.invert();
416  int ret = rmatx.crack(rvals, order);
417 
418  rx = rvals[0]; ry = rvals[1]; rz = rvals[2];
419 
420  return ret;
421 }
422 
423 template <typename T>
424 void
426 {
428  r.x() = rel.x()*xaxis().x()+rel.y()*yaxis().x()+rel.z()*zaxis().x();
429  r.y() = rel.x()*xaxis().y()+rel.y()*yaxis().y()+rel.z()*zaxis().y();
430  r.z() = rel.x()*xaxis().z()+rel.y()*yaxis().z()+rel.z()*zaxis().z();
431  r += origin();
432  rel = r;
433 }
434 
435 template <typename T>
436 void
438 {
440  r.x() = rel.x()*xaxis().x()+rel.y()*yaxis().x()+rel.z()*zaxis().x();
441  r.y() = rel.x()*xaxis().y()+rel.y()*yaxis().y()+rel.z()*zaxis().y();
442  r.z() = rel.x()*xaxis().z()+rel.y()*yaxis().z()+rel.z()*zaxis().z();
443  rel = r;
444 }
445 
446 #endif // __UT_COORDSPACEIMPL_H_INCLUDED__
static UT_Matrix3T< T > rotationMat(UT_Vector3T< S > &axis, T theta, int norm=1)
void getTransformMatrixPreservingOrigin(UT_Matrix4T< T > &matx) const
const UT_Vector3T< T > & yaxis() const
Definition: UT_CoordSpace.h:60
void convertToWorld(UT_Vector3T< T > &rel) const
void convertToWorldNoOriginAdj(UT_Vector3T< T > &rel) const
int fromWorldRotation(T &rx, T &ry, T &rz, const UT_XformOrder &order) const
SYS_FORCE_INLINE void negate()
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
int crack(UT_Vector3T< S > &rvec, const UT_XformOrder &order, int remove_scales=1)
GLint y
Definition: glcorearb.h:102
SYS_FORCE_INLINE T & x(void)
Definition: UT_Vector3.h:498
void getTransformMatrix(UT_Matrix4T< T > &matx) const
3D Vector class.
#define M_PI
Definition: ImathPlatform.h:51
SYS_FORCE_INLINE T & z(void)
Definition: UT_Vector3.h:502
int invert()
Invert this matrix and return 0 if OK, 1 if singular.
GLdouble n
Definition: glcorearb.h:2007
void identity()
Set the matrix to identity.
Definition: UT_Matrix3.h:945
void transformAndReturnNormalXform(UT_Matrix4F &matx)
static UT_Matrix3T< T > dihedral(UT_Vector3T< S > &a, UT_Vector3T< S > &b, UT_Vector3T< S > &c, int norm=1)
#define SYS_FALLTHROUGH
Definition: SYS_Compiler.h:75
const UT_Vector3T< T > & zaxis() const
Definition: UT_CoordSpace.h:61
void transform(const UT_Matrix4F &matx)
int toWorldRotation(T &rx, T &ry, T &rz, const UT_XformOrder &order) const
void rotate(UT_Vector3T< T > &axis, T theta, bool norm=true)
void identity()
Set the matrix to identity.
Definition: UT_Matrix4.h:1018
SYS_FORCE_INLINE T & y(void)
Definition: UT_Vector3.h:500
const UT_Vector3T< T > & xaxis() const
Definition: UT_CoordSpace.h:59
UT_PlaneType
Definition: UT_Plane.h:26
GLint GLenum GLint x
Definition: glcorearb.h:408
UT_CoordSpaceT(UT_PlaneType p=UT_PLANE_XY)
int invert(T tol=0.0F)
Invert this matrix and return 0 if OK, 1 if singular.
void transpose(void)
Definition: UT_Matrix4.h:466
SYS_FORCE_INLINE Storage::MathFloat normalize()
void setAxes(const UT_Vector3T< T > &x, const UT_Vector3T< T > &y, const UT_Vector3T< T > &z, bool norm=true)
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
GLboolean r
Definition: glcorearb.h:1221
void rotate(UT_Vector3T< S > &axis, T theta, int norm=1)
SYS_FORCE_INLINE void normal(const UT_Vector3T< T > &va, const UT_Vector3T< T > &vb)
Definition: UT_Vector3.h:374
void transpose(void)
Definition: UT_Matrix3.h:635
void translate(T dx, T dy, T dz=0.0f)
Definition: UT_Matrix4.h:700
SIM_DerVector3 cross(const SIM_DerVector3 &lhs, const SIM_DerVector3 &rhs)