HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_SymMatrix3.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_SymMatrix3.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_SYMMATRIX3_H_INCLUDED__
12 #define __UT_SYMMATRIX3_H_INCLUDED__
13 
14 #include "UT_API.h"
15 
16 #include "UT_Assert.h"
17 #include "UT_VectorTypes.h"
18 #include <SYS/SYS_Inline.h>
19 #include <SYS/SYS_Math.h> // for SYSlerp, ...
20 #include <SYS/SYS_Types.h>
21 
22 #include <string.h> // for memset, ...
23 
24 
25 // Forward declaration
26 template <typename T> class UT_SymMatrix3T;
27 
28 // Free floating methods that operate on UT_SymMatrix3T
29 template <typename T>
31 template <typename T>
33 template <typename T>
35 template <typename T>
37 
38 
39 /// Generic symmetric 3x3 matrix
40 template <typename T>
41 class UT_SymMatrix3T
42 {
43 public:
44  typedef T value_type;
46  static const int tuple_size = 6;
47 
48  /// Construct uninitialized matrix
50 
51  /// Construct matrix with uniform scale
52  explicit UT_SymMatrix3T<T>(T s)
53  {
54  setScale(s, s, s);
55  }
56 
57  /// Construct matrix with arbitrary scale
58  explicit UT_SymMatrix3T<T>(const UT_Vector3T<T>& s)
59  {
60  setScale(s.x(), s.y(), s.z());
61  }
62 
63  /// Construct matrix with full components
65  T q00, T q10, T q11, T q20, T q21, T q22)
66  {
67  myLower.q00 = q00;
68  myLower.q10 = q10; myLower.q11 = q11;
69  myLower.q20 = q20; myLower.q21 = q21; myLower.q22 = q22;
70  }
71 
72  /// Set this to the zero matrix
73  inline void zero()
74  {
75  ::memset(&myV[0], 0, sizeof(myV));
76  }
77 
78  /// Set this to the identity matrix
79  inline void identity()
80  {
81  setScale(1, 1, 1);
82  }
83 
84  /// Return whether this is the identity matrix.
85  inline bool isIdentity() const
86  {
87  return myLower.q00 == 1.0f && myLower.q11 == 1.0f &&
88  myLower.q22 == 1.0f && myLower.q10 == 0.0f &&
89  myLower.q20 == 0.0f && myLower.q21 == 0.0f;
90  }
91 
92  /// Set this to a scale matrix
93  inline void setScale(T sx, T sy, T sz)
94  {
95  myLower.q00 = sx;
96  myLower.q11 = sy;
97  myLower.q22 = sz;
98  myLower.q10 = myLower.q20 = myLower.q21 = 0;
99  }
100  /// Set this to a scale matrix
101  inline void setScale(const UT_Vector3T<T>& s)
102  {
103  setScale(s.x(), s.y(), s.z());
104  }
105 
106  /// Return element (i,j)
107  inline T operator()(const int i, const int j) const
108  {
109  UT_ASSERT_P(i >= 0 && i < 3 && j >= 0 && j < 3);
110  if (i <= j)
111  return myV[(j*(j + 1))/2 + i];
112  else
113  return myV[(i*(i + 1))/2 + j];
114  }
115  /// Return reference to element (i,j)
116  inline T& operator()(const int i, const int j)
117  {
118  UT_ASSERT_P(i >= 0 && i < 3 && j >= 0 && j < 3);
119  if (i <= j)
120  return myV[(j*(j + 1))/2 + i];
121  else
122  return myV[(i*(i + 1))/2 + j];
123  }
124 
125  inline type& operator+=(const type& m)
126  {
127  for (int i = 0; i < tuple_size; ++i)
128  myV[i] += m.myV[i];
129  return *this;
130  }
131  inline type& operator-=(const type& m)
132  {
133  for (int i = 0; i < tuple_size; ++i)
134  myV[i] -= m.myV[i];
135  return *this;
136  }
137  inline type& operator*=(T scalar)
138  {
139  for (int i = 0; i < tuple_size; ++i)
140  myV[i] *= scalar;
141  return *this;
142  }
143  inline type& operator/=(T scalar)
144  {
145  return operator*=(1.0/scalar);
146  }
147 
148  /// Set this to a linear interpolation of the two given transforms:
149  /// *this = a + t*(b - a)
150  void lerp(const type& a, const type& b, T t)
151  {
152  for (int i = 0; i < 6; ++i)
153  myV[i] = SYSlerp(a.myV[i], b.myV[i], t);
154  }
155 
156  /// Inner class to access the elements symbolically
157  struct LowerTri
158  {
160  };
161  struct UpperTri
162  {
164  };
165 
166  /// Return the raw pointer to an array of tuple_size (6) elements
167  /// @{
168  const T* data() const { return &myV[0]; }
169  T* data() { return &myV[0]; }
170  /// @}
171 
172  /// Return reference to the lower triangular elements for symbolic access
173  const LowerTri & lowerTri() const { return myLower; }
174 
175  /// Return reference to the upper triangular elements for symbolic access
176  const UpperTri & upperTri() const { return myUpper; }
177 
178 
179  void transpose() {}
180 
181  // Sets the matrix to v * vT
182  void outerproduct(const UT_Vector3T<T> &v);
183  void outerproduct(T a, T b, T c);
184 
185  // Updates: Matrix += v * vT
186  void outerproductUpdate(const UT_Vector3T<T> &v);
187  void outerproductUpdate(T a, T b, T c);
188 
189  // Updates: Matrix += coef * v * vT
190  void outerproductUpdate(const UT_Vector3T<T> &v, T coef);
191  void outerproductUpdate(T a, T b, T c, T coef);
192 
193  // Calculates vT * Matrix * v
194  T vQv(const UT_Vector3T<T> &v) const;
195 
196 private:
197  // myV stores the elements of upper triangular portion of the symmetric
198  // matrix in a column-major form (or equivalently, the lower triangular
199  // portion in row-major form). The element (I, J) with I <= J, is stored
200  // at index: c = (J * (J + 1))/2 + I
201  // So the matrix is:
202  // myV[0] myV[1] myV[3]
203  // myV[1] myV[2] myV[4]
204  // myV[3] myV[4] myV[5]
205  //
206  union {
207  T myV[6];
210  };
211 
212  template <typename S> friend class UT_Matrix3T;
213  template <typename S> friend class UT_Matrix4T;
214 };
218 
219 ///////////////////////////////////////////////////////////////////////////////
220 //
221 // Implementations
222 //
223 
224 /// Return (m1 + m2)
225 template <typename T>
228 {
229  typedef typename UT_SymMatrix3T<T>::LowerTri LowerTri;
230  const LowerTri& x = m1.lowerTri();
231  const LowerTri& y = m2.lowerTri();
232  return UT_SymMatrix3T<T>(x.q00 + y.q00,
233  x.q10 + y.q10, x.q11 + y.q11,
234  x.q20 + y.q20, x.q21 + y.q21, x.q22 + y.q22);
235 }
236 
237 /// Return (m1 - m2)
238 template <typename T>
241 {
242  typedef typename UT_SymMatrix3T<T>::LowerTri LowerTri;
243  const LowerTri& x = m1.lowerTri();
244  const LowerTri& y = m2.lowerTri();
245  return UT_SymMatrix3T<T>(x.q00 - y.q00,
246  x.q10 - y.q10, x.q11 - y.q11,
247  x.q20 - y.q20, x.q21 - y.q21, x.q22 - y.q22);
248 }
249 
250 /// Return (m * s) for scalar s
251 template <typename T>
254 {
255  typedef typename UT_SymMatrix3T<T>::LowerTri LowerTri;
256  const LowerTri& l = m.lowerTri();
257  return UT_SymMatrix3T<T>(l.q00 * scale,
258  l.q10 * scale, l.q11 * scale,
259  l.q20 * scale, l.q21 * scale, l.q22 * scale);
260 }
261 
262 /// Return (s * m) for scalar s
263 template <typename T>
266 {
267  typedef typename UT_SymMatrix3T<T>::LowerTri LowerTri;
268  const LowerTri& l = m.lowerTri();
269  return UT_SymMatrix3T<T>(l.q00 * scale,
270  l.q10 * scale, l.q11 * scale,
271  l.q20 * scale, l.q21 * scale, l.q22 * scale);
272 }
273 
274 template <typename T>
275 void
277 {
278  outerproduct(v.x(), v.y(), v.z());
279 }
280 
281 template <typename T>
282 void
284 {
285  myUpper.q00 = a * a; myUpper.q01 = a * b; myUpper.q02 = a * c;
286  myUpper.q11 = b * b; myUpper.q12 = b * c;
287  myUpper.q22 = c * c;
288 }
289 
290 template <typename T>
291 void
293 {
294  outerproductUpdate(v.x(), v.y(), v.z());
295 }
296 
297 template <typename T>
298 void
300 {
301  myUpper.q00 += a * a; myUpper.q01 += a * b; myUpper.q02 += a * c;
302  myUpper.q11 += b * b; myUpper.q12 += b * c;
303  myUpper.q22 += c * c;
304 
305 }
306 
307 template <typename T>
308 void
310 {
311  outerproductUpdate(v.x(), v.y(), v.z(), coef);
312 }
313 
314 template <typename T>
315 void
317 {
318  myUpper.q00 += coef * (a * a); myUpper.q01 += coef * (a * b);
319  myUpper.q02 += coef * (a * c);
320  myUpper.q11 += coef * (b * b); myUpper.q12 += coef * (b * c);
321  myUpper.q22 += coef * (c * c);
322 
323 }
324 
325 // Calculates vT this v
326 template <typename T>
327 T
329 {
330  T a = v.x(), b = v.y(), c = v.z();
331 
332  return (a * (a * myUpper.q00 + 2 * (b * myUpper.q01 + c * myUpper.q02)) +
333  b * (b * myUpper.q11 + 2 * (c * myUpper.q12)) +
334  c * (c * myUpper.q22 ));
335 }
336 
337 
338 #endif // __UT_SYMMATRIX3_H_INCLUDED__
UT_SymMatrix3T< float > UT_SymMatrix3F
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:609
T vQv(const UT_Vector3T< T > &v) const
static const int tuple_size
Definition: UT_SymMatrix3.h:46
T & operator()(const int i, const int j)
Return reference to element (i,j)
const GLdouble * v
Definition: glcorearb.h:836
UT_SymMatrix3T< T > type
Definition: UT_SymMatrix3.h:45
Mat3< typename promote< T0, T1 >::type > operator+(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Add corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:625
const LowerTri & lowerTri() const
Return reference to the lower triangular elements for symbolic access.
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
Generic symmetric 3x3 matrix.
Definition: UT_SymMatrix3.h:26
GLint y
Definition: glcorearb.h:102
UT_Matrix2T< T > SYSlerp(const UT_Matrix2T< T > &v1, const UT_Matrix2T< T > &v2, S t)
Definition: UT_Matrix2.h:595
SYS_FORCE_INLINE T & x(void)
Definition: UT_Vector3.h:498
void outerproduct(const UT_Vector3T< T > &v)
3D Vector class.
void outerproductUpdate(T b, const UT_Vector4T< S > &v1, const UT_Vector4T< S > &v2)
png_uint_32 i
Definition: png.h:2877
void zero()
Set this to the zero matrix.
Definition: UT_SymMatrix3.h:73
void identity()
Set this to the identity matrix.
Definition: UT_SymMatrix3.h:79
LowerTri myLower
SYS_FORCE_INLINE T & z(void)
Definition: UT_Vector3.h:502
UT_SymMatrix3T< fpreal > UT_SymMatrix3R
GA_API const UT_StringHolder scale
type & operator+=(const type &m)
void setScale(T sx, T sy, T sz)
Set this to a scale matrix.
Definition: UT_SymMatrix3.h:93
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Subtract corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:635
UT_SymMatrix3T< double > UT_SymMatrix3D
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:125
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
void setScale(const UT_Vector3T< T > &s)
Set this to a scale matrix.
T operator()(const int i, const int j) const
Return element (i,j)
const UpperTri & upperTri() const
Return reference to the upper triangular elements for symbolic access.
const T * data() const
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
bool isIdentity() const
Return whether this is the identity matrix.
Definition: UT_SymMatrix3.h:85
void lerp(const type &a, const type &b, T t)
Inner class to access the elements symbolically.
SYS_FORCE_INLINE T & y(void)
Definition: UT_Vector3.h:500
GLint GLenum GLint x
Definition: glcorearb.h:408
void outerproductUpdate(const UT_Vector3T< T > &v)
UpperTri myUpper
type & operator/=(T scalar)
type & operator*=(T scalar)
type & operator-=(const type &m)