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  myPlane.transform(matx);
307 
308  UT_Matrix4D xform(matx);
309 
310  xform.invert();
311  xform.transpose();
312 
313  myXAxis *= xform; myXAxis.normalize();
314  myYAxis *= xform; myYAxis.normalize();
315 }
316 
317 template <typename T>
318 void
320 {
321  // The following call sets matx to the inverted and transposed matrix.
322  myPlane.transformAndReturnNormalXform(matx);
323 
324  myXAxis *= matx; myXAxis.normalize();
325  myYAxis *= matx; myYAxis.normalize();
326 }
327 
328 template <typename T>
329 void
331 {
332  // Set our transform to the identity matrix.
333  myPlane.setPoint(UT_Vector3T<T>(0.0, 0.0, 0.0));
334  myPlane.setNormal(UT_PLANE_XY);
335  myXAxis.assign(1.0, 0.0, 0.0);
336  myYAxis.assign(0.0, 1.0, 0.0);
337 }
338 
339 // Multiples the input matrix by the change in coordinate space from world
340 // space into this one.
341 template <typename T>
342 template <typename MATX>
343 inline void
344 UT_CoordSpaceT<T>::changeSpace(MATX &matx) const
345 {
346  UT_Vector3T<T> world_x(1.0, 0.0, 0.0);
347  UT_Vector3T<T> world_y(0.0, 1.0, 0.0);
348 
349  matx.changeSpace(world_x, world_y,
350  const_cast<UT_CoordSpaceT<T> *>(this)->myXAxis,
351  const_cast<UT_CoordSpaceT<T> *>(this)->myYAxis,
352  /*norm*/ false);
353 }
354 
355 template <typename T>
356 void
358 {
359  matx.identity();
360  matx.translate(origin().x(), origin().y(), origin().z());
361  changeSpace(matx);
362 }
363 
364 template <typename T>
365 void
367  UT_Matrix4T<T> &matx) const
368 {
369  matx.identity();
370  changeSpace(matx);
371  matx.translate(origin().x(), origin().y(), origin().z());
372 }
373 
374 template <typename T>
375 int
377  const UT_XformOrder &order) const
378 {
379  UT_Vector3T<T> rvals;
380  UT_Matrix3T<T> rmatx;
381 
382  rmatx.identity();
383  changeSpace(rmatx);
384  int ret = rmatx.crack(rvals, order);
385 
386  rx = rvals[0]; ry = rvals[1]; rz = rvals[2];
387 
388  return ret;
389 }
390 
391 template <typename T>
392 int
394  const UT_XformOrder &order) const
395 {
396  UT_Vector3T<T> rvals;
397  UT_Matrix3T<T> rmatx;
398 
399  rmatx.identity();
400  changeSpace(rmatx);
401  rmatx.invert();
402  int ret = rmatx.crack(rvals, order);
403 
404  rx = rvals[0]; ry = rvals[1]; rz = rvals[2];
405 
406  return ret;
407 }
408 
409 template <typename T>
410 void
412 {
414  r.x() = rel.x()*xaxis().x()+rel.y()*yaxis().x()+rel.z()*zaxis().x();
415  r.y() = rel.x()*xaxis().y()+rel.y()*yaxis().y()+rel.z()*zaxis().y();
416  r.z() = rel.x()*xaxis().z()+rel.y()*yaxis().z()+rel.z()*zaxis().z();
417  r += origin();
418  rel = r;
419 }
420 
421 template <typename T>
422 void
424 {
426  r.x() = rel.x()*xaxis().x()+rel.y()*yaxis().x()+rel.z()*zaxis().x();
427  r.y() = rel.x()*xaxis().y()+rel.y()*yaxis().y()+rel.z()*zaxis().y();
428  r.z() = rel.x()*xaxis().z()+rel.y()*yaxis().z()+rel.z()*zaxis().z();
429  rel = r;
430 }
431 
432 #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
#define M_PI
Definition: fmath.h:90
Transformation order of scales, rotates, and translates.
Definition: UT_XformOrder.h:23
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
constexpr SYS_FORCE_INLINE T & z() noexcept
Definition: UT_Vector3.h:667
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLint y
Definition: glcorearb.h:103
void getTransformMatrix(UT_Matrix4T< T > &matx) const
3D Vector class.
void transpose()
Definition: UT_Matrix3.h:653
GLdouble n
Definition: glcorearb.h:2008
void identity()
Set the matrix to identity.
Definition: UT_Matrix3.h:1128
void transformAndReturnNormalXform(UT_Matrix4F &matx)
constexpr SYS_FORCE_INLINE void negate() noexcept
Definition: UT_Vector3.h:351
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:68
const UT_Vector3T< T > & zaxis() const
Definition: UT_CoordSpace.h:61
void transform(const UT_Matrix4F &matx)
GLdouble GLdouble GLint GLint order
Definition: glad.h:2676
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:1128
GLint GLenum GLint x
Definition: glcorearb.h:409
const UT_Vector3T< T > & xaxis() const
Definition: UT_CoordSpace.h:59
void translate(T dx, T dy, T dz=0)
Definition: UT_Matrix4.h:773
UT_PlaneType
Definition: UT_Plane.h:26
UT_CoordSpaceT(UT_PlaneType p=UT_PLANE_XY)
int invert(T tol=0.0F)
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:156
SYS_FORCE_INLINE UT_StorageMathFloat_t< T > normalize() noexcept
Definition: UT_Vector3.h:376
GLboolean r
Definition: glcorearb.h:1222
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:539
constexpr SYS_FORCE_INLINE T & y() noexcept
Definition: UT_Vector3.h:665
SIM_DerVector3 cross(const SIM_DerVector3 &lhs, const SIM_DerVector3 &rhs)
int crack(UT_Vector3T< S > &rvec, const UT_XformOrder &order) const
void transpose()
Definition: UT_Matrix4.h:509
constexpr SYS_FORCE_INLINE T & x() noexcept
Definition: UT_Vector3.h:663