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