HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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");
39  case UT_PLANE_XZ:
40  myXAxis.assign(0.0, 0.0, 1.0); // world Z
41  myYAxis.assign(1.0, 0.0, 0.0); // world X
42  break;
43  }
44 }
45 
46 template <typename T>
48  const UT_Vector3T<T> &pt,
49  const UT_Vector3T<T> &axaxis,
50  const UT_Vector3T<T> &ayaxis,
51  const UT_Vector3T<T> *azaxis,
52  bool norm
53  )
54  : myPlane(pt)
55 {
56  myXAxis = axaxis;
57  myYAxis = ayaxis;
58 
59  if (norm)
60  {
61  myXAxis.normalize();
62  myYAxis.normalize();
63  }
64 
66  if (azaxis)
67  {
68  n = *azaxis;
69  if (norm)
70  n.normalize();
71  }
72  else
73  {
74  n = cross(axaxis, ayaxis);
75  n.normalize();
76  }
77  myPlane.setNormal(n);
78 }
79 
80 template <typename T>
82  const UT_Vector3T<T> &p0,
83  const UT_Vector3T<T> &p1,
84  const UT_Vector3T<T> &p2
85  )
86  : myPlane(p0, p1, p2)
87 {
88  myXAxis = p1 - p0;
89  myXAxis.normalize();
90 
91  myYAxis = cross(myPlane.normal(), myXAxis);
92  myYAxis.normalize();
93 }
94 
95 template <typename T>
97 {
98  // nothing else needed
99 }
100 
101 template <typename T>
102 void
104 {
105  UT_Vector3T<T> oldy = myYAxis;
106  myYAxis = myXAxis;
107  myXAxis = oldy;
108 
109  myPlane.negate();
110 }
111 
112 template <typename T>
113 void
114 UT_CoordSpaceT<T>::xaxis(const UT_Vector3T<T> &x, bool norm, bool allow_flip)
115 {
116  UT_Vector3T<T> oldx = myXAxis;
117  UT_Matrix3T<T> matx;
118 
119  myXAxis = x; if (norm) myXAxis.normalize();
120 
121  if (matx.dihedral(oldx, myXAxis, false) != 0)
122  {
123  // New x-axis is opposed to the old. Use a rotation of 180 degrees
124  // about our y-axis, if allowed.
125  if (allow_flip)
126  {
127  matx.identity();
128  matx.rotate(myYAxis, M_PI, /* norm */ 0);
129  }
130  else
131  {
132  myXAxis = oldx;
133  return;
134  }
135  }
136 
137  myYAxis *= matx; myYAxis.normalize();
138  myPlane.rotateNormal(matx, true);
139 }
140 
141 template <typename T>
142 void
143 UT_CoordSpaceT<T>::yaxis(const UT_Vector3T<T> &y, bool norm, bool allow_flip)
144 {
145  UT_Vector3T<T> oldy = myYAxis;
146  UT_Matrix3T<T> matx;
147 
148  myYAxis = y; if (norm) myYAxis.normalize();
149 
150  if (matx.dihedral(oldy, myYAxis, false) != 0)
151  {
152  // New y-axis is opposed to the old. Use a rotation of 180 degrees
153  // about our z-axis, if allowed.
154  if (allow_flip)
155  {
156  matx.identity();
157  UT_Vector3T<T> nml = myPlane.normal();
158  matx.rotate(nml, M_PI, /* norm */ 0);
159  }
160  else
161  {
162  myYAxis = oldy;
163  return;
164  }
165  }
166 
167  myXAxis *= matx; myXAxis.normalize();
168  myPlane.rotateNormal(matx, true);
169 }
170 
171 template <typename T>
172 void
173 UT_CoordSpaceT<T>::zaxis(const UT_Vector3T<T> &z, bool norm, bool allow_flip)
174 {
175  UT_Vector3T<T> oldz = myPlane.normal();
176  UT_Matrix3T<T> matx;
177 
178  if (norm)
179  myPlane.setVectorAsNormal(z);
180  else
181  myPlane.setNormal(z);
182 
183  UT_Vector3T<T> n = myPlane.normal();
184  if (matx.dihedral(oldz, n, false) != 0)
185  {
186  // New z-axis is opposed to the old. Use a rotation of 180 degrees
187  // about our y-axis, if allowed.
188  if (allow_flip)
189  {
190  matx.identity();
191  matx.rotate(myYAxis, M_PI, /* norm */ 0);
192  }
193  else
194  {
195  myPlane.setNormal(oldz);
196  return;
197  }
198  }
199 
200  myXAxis *= matx; myXAxis.normalize();
201  myYAxis *= matx; myYAxis.normalize();
202 }
203 
204 template <typename T>
205 void
207  const UT_Vector3T<T> &x,
208  const UT_Vector3T<T> &y,
209  const UT_Vector3T<T> &z,
210  bool norm)
211 {
212  myXAxis = x;
213  myYAxis = y;
214 
215  if (norm)
216  {
217  myXAxis.normalize();
218  myYAxis.normalize();
219  myPlane.setVectorAsNormal(z);
220  }
221  else
222  {
223  myPlane.setNormal(z);
224  }
225 }
226 
227 template <typename T>
228 void
229 UT_CoordSpaceT<T>::rotate(UT_Vector3T<T> &axis, T theta, bool norm)
230 {
231  UT_Matrix3T<T> rmatx;
232  rmatx = UT_Matrix3T<T>::rotationMat(axis, theta, norm);
233  myPlane.rotatePoint(rmatx);
234 
235  rmatx.invert();
236  rmatx.transpose();
237 
238  myXAxis *= rmatx; myXAxis.normalize();
239  myYAxis *= rmatx; myYAxis.normalize();
240  myPlane.rotateNormal(rmatx, true);
241 }
242 
243 template <typename T>
244 void
246 {
247  UT_Matrix3T<T> rmatx;
248  rmatx = UT_Matrix3T<T>::rotationMat(a, theta);
249  myPlane.rotatePoint(rmatx);
250 
251  rmatx.invert();
252  rmatx.transpose();
253 
254  myXAxis *= rmatx; myXAxis.normalize();
255  myYAxis *= rmatx; myYAxis.normalize();
256  myPlane.rotateNormal(rmatx, true);
257 }
258 
259 template <typename T>
260 void
262 {
263  UT_Matrix4T<T> matx;
264  UT_Matrix4T<T> tmp;
265 
266  tmp = rotmatx;
267 
268  matx.identity();
269  matx.translate(-origin().x(), -origin().y(), -origin().z());
270  matx *= tmp;
271  matx.translate( origin().x(), origin().y(), origin().z());
272  transform((UT_Matrix4T<T>&) matx);
273 }
274 
275 template <typename T>
276 void
278 {
279  myPlane.transform(matx);
280 
281  UT_Matrix4F xform(matx);
282 
283  xform.invert();
284  xform.transpose();
285 
286  myXAxis *= xform; myXAxis.normalize();
287  myYAxis *= xform; myYAxis.normalize();
288 }
289 
290 template <typename T>
291 void
293 {
294  myPlane.transform(matx); // returned already inverted and transposed
295 
296  myXAxis *= matx; myXAxis.normalize();
297  myYAxis *= matx; myYAxis.normalize();
298 }
299 
300 template <typename T>
301 void
303 {
304  myPlane.transform(matx);
305 
306  UT_Matrix4D xform(matx);
307 
308  xform.invert();
309  xform.transpose();
310 
311  myXAxis *= xform; myXAxis.normalize();
312  myYAxis *= xform; myYAxis.normalize();
313 }
314 
315 template <typename T>
316 void
318 {
319  myPlane.transform(matx); // returned already inverted and transposed
320 
321  myXAxis *= matx; myXAxis.normalize();
322  myYAxis *= matx; myYAxis.normalize();
323 }
324 
325 template <typename T>
326 void
328 {
329  // Set our transform to the identity matrix.
330  myPlane.setPoint(UT_Vector3T<T>(0.0, 0.0, 0.0));
331  myPlane.setNormal(UT_PLANE_XY);
332  myXAxis.assign(1.0, 0.0, 0.0);
333  myYAxis.assign(0.0, 1.0, 0.0);
334 }
335 
336 // Multiples the input matrix by the change in coordinate space from world
337 // space into this one.
338 template <typename T>
339 template <typename MATX>
340 inline void
341 UT_CoordSpaceT<T>::changeSpace(MATX &matx) const
342 {
343  UT_Vector3T<T> world_x(1.0, 0.0, 0.0);
344  UT_Vector3T<T> world_y(0.0, 1.0, 0.0);
345 
346  matx.changeSpace(world_x, world_y,
347  const_cast<UT_CoordSpaceT<T> *>(this)->myXAxis,
348  const_cast<UT_CoordSpaceT<T> *>(this)->myYAxis,
349  /*norm*/ false);
350 }
351 
352 template <typename T>
353 void
355 {
356  matx.identity();
357  matx.translate(origin().x(), origin().y(), origin().z());
358  changeSpace(matx);
359 }
360 
361 template <typename T>
362 void
364  UT_Matrix4T<T> &matx) const
365 {
366  matx.identity();
367  changeSpace(matx);
368  matx.translate(origin().x(), origin().y(), origin().z());
369 }
370 
371 template <typename T>
372 int
374  const UT_XformOrder &order) const
375 {
376  UT_Vector3T<T> rvals;
377  UT_Matrix3T<T> rmatx;
378 
379  rmatx.identity();
380  changeSpace(rmatx);
381  int ret = rmatx.crack(rvals, order);
382 
383  rx = rvals[0]; ry = rvals[1]; rz = rvals[2];
384 
385  return ret;
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  rmatx.invert();
399  int ret = rmatx.crack(rvals, order);
400 
401  rx = rvals[0]; ry = rvals[1]; rz = rvals[2];
402 
403  return ret;
404 }
405 
406 template <typename T>
407 void
409 {
411  r.x() = rel.x()*xaxis().x()+rel.y()*yaxis().x()+rel.z()*zaxis().x();
412  r.y() = rel.x()*xaxis().y()+rel.y()*yaxis().y()+rel.z()*zaxis().y();
413  r.z() = rel.x()*xaxis().z()+rel.y()*yaxis().z()+rel.z()*zaxis().z();
414  r += origin();
415  rel = r;
416 }
417 
418 template <typename T>
419 void
421 {
423  r.x() = rel.x()*xaxis().x()+rel.y()*yaxis().x()+rel.z()*zaxis().x();
424  r.y() = rel.x()*xaxis().y()+rel.y()*yaxis().y()+rel.z()*zaxis().y();
425  r.z() = rel.x()*xaxis().z()+rel.y()*yaxis().z()+rel.z()*zaxis().z();
426  rel = r;
427 }
428 
429 #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:59
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:581
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:585
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:932
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
static UT_Matrix3T< T > dihedral(UT_Vector3T< S > &a, UT_Vector3T< S > &b, UT_Vector3T< S > &c, int norm=1)
const UT_Vector3T< T > & zaxis() const
Definition: UT_CoordSpace.h:60
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:924
GA_API const UT_StringHolder transform
SYS_FORCE_INLINE T & y(void)
Definition: UT_Vector3.h:583
const UT_Vector3T< T > & xaxis() const
Definition: UT_CoordSpace.h:58
UT_PlaneType
Definition: UT_Plane.h:25
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:459
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)
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:457
void transpose(void)
Definition: UT_Matrix3.h:607
void translate(T dx, T dy, T dz=0.0f)
Definition: UT_Matrix4.h:608
SIM_DerVector3 cross(const SIM_DerVector3 &lhs, const SIM_DerVector3 &rhs)