Fermat
matrix.h
1 /*
2  * Copyright (c) 2010-2018, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <cugar/linalg/vector.h>
31 #include <cmath>
32 
33 namespace cugar {
34 
43 
47 
50 
54 template <typename T, int N, int M> struct CUGAR_API_CS Matrix
55 {
56 public:
57  typedef T value_type;
58  typedef T Field_type;
59 
60  typedef Vector<T,M> row_vector;
61  typedef Vector<T,N> column_vector;
62 
63 public:
64 CUGAR_HOST_DEVICE inline Matrix ();
65 CUGAR_HOST_DEVICE inline explicit Matrix (const T s);
66 CUGAR_HOST_DEVICE inline explicit Matrix (const Vector<T,M>& v);
67 CUGAR_HOST_DEVICE inline Matrix (const Matrix<T,N,M>&);
68 CUGAR_HOST_DEVICE inline Matrix (const Vector<T,M> *v);
69 CUGAR_HOST_DEVICE inline Matrix (const T *v);
70 CUGAR_HOST_DEVICE inline Matrix (const T **v);
71 //inline Matrix (const T v[N][M]);
72 
73 CUGAR_HOST_DEVICE inline Matrix<T,N,M>& operator = (const Matrix<T,N,M>&);
74 CUGAR_HOST_DEVICE inline Matrix<T,N,M>& operator += (const Matrix<T,N,M>&);
75 CUGAR_HOST_DEVICE inline Matrix<T,N,M>& operator -= (const Matrix<T,N,M>&);
76 CUGAR_HOST_DEVICE inline Matrix<T,N,M>& operator *= (T);
77 CUGAR_HOST_DEVICE inline Matrix<T,N,M>& operator /= (T);
78 
79 CUGAR_HOST_DEVICE inline const Vector<T,M>& operator [] (int) const;
80 CUGAR_HOST_DEVICE inline Vector<T,M>& operator [] (int);
81 CUGAR_HOST_DEVICE inline const Vector<T,M>& get (int) const;
82 CUGAR_HOST_DEVICE inline void set (int, const Vector<T,M>&);
83 
84 CUGAR_HOST_DEVICE inline const T& operator () (int i, int j) const;
85 CUGAR_HOST_DEVICE inline T& operator () (int i, int j);
86 
87 CUGAR_HOST_DEVICE inline T det() const;
88 
89 CUGAR_HOST_DEVICE static inline Matrix<T,N,M> one();
90 
91 friend CUGAR_HOST_DEVICE int operator == <T,N,M> (const Matrix<T,N,M>&, const Matrix<T,N,M>&);
92 friend CUGAR_HOST_DEVICE int operator != <T,N,M> (const Matrix<T,N,M>&, const Matrix<T,N,M>&);
93 friend CUGAR_HOST_DEVICE Matrix<T,N,M> operator - <T,N,M> (const Matrix<T,N,M>&);
94 friend CUGAR_HOST_DEVICE Matrix<T,N,M> operator + <T,N,M> (const Matrix<T,N,M>&, const Matrix<T,N,M>&);
95 friend CUGAR_HOST_DEVICE Matrix<T,N,M> operator - <T,N,M> (const Matrix<T,N,M>&, const Matrix<T,N,M>&);
96 friend CUGAR_HOST_DEVICE Matrix<T,N,M> operator * <T,N,M> (const Matrix<T,N,M>&, T);
97 friend CUGAR_HOST_DEVICE Matrix<T,N,M> operator * <T,N,M> (T, const Matrix<T,N,M>&);
98 friend CUGAR_HOST_DEVICE Vector<T,M> operator * <T,N,M> (const Vector<T,N>&, const Matrix<T,N,M>&);
99 friend CUGAR_HOST_DEVICE Vector<T,N> operator * <T,N> (const Vector<T,N>&, const Matrix<T,N,N>&);
100 friend CUGAR_HOST_DEVICE Vector<T,N> operator * <T,N,M> (const Matrix<T,N,M>&, const Vector<T,M>&);
101 friend CUGAR_HOST_DEVICE Vector<T,N> operator * <T,N> (const Matrix<T,N,N>&, const Vector<T,N>&);
102 friend CUGAR_HOST_DEVICE Matrix<T,N,M> operator / <T,N,M> (const Matrix<T,N,M>&, T);
103 
104 public:
105  Vector<T,M> r[N];
106 };
107 
118 
119 template <typename T, int N, int M, int Q> CUGAR_HOST_DEVICE Matrix<T,N,Q>& multiply (const Matrix<T,N,M>&, const Matrix<T,M,Q>&, Matrix<T,N,Q>&);
120 template <typename T, int N, int M, int Q> CUGAR_HOST_DEVICE Matrix<T,N,Q> operator * (const Matrix<T,N,M>&, const Matrix<T,M,Q>&);
121 template <typename T, int N, int M> CUGAR_HOST_DEVICE Vector<T,M>& multiply (const Vector<T,N>&, const Matrix<T,N,M>&, Vector<T,M>&);
122 template <typename T, int N, int M> CUGAR_HOST_DEVICE Vector<T,N>& multiply (const Matrix<T,N,M>&, const Vector<T,M>&, Vector<T,N>&);
123 template <typename T, int N, int M> CUGAR_HOST_DEVICE Matrix<T,M,N> transpose (const Matrix<T,N,M>&);
124 template <typename T, int N, int M> CUGAR_HOST_DEVICE Matrix<T,M,N>& transpose (const Matrix<T,N,M>&, Matrix<T,M,N>&);
125 template <typename T, int N, int M> CUGAR_HOST_DEVICE bool invert (const Matrix<T,N,M>&, Matrix<T,M,N>&); // gives inv(A^t * A)*A^t
126 template <typename T, int N, int M> CUGAR_HOST_DEVICE T det (const Matrix<T,N,M>&);
127 template <typename T> CUGAR_HOST_DEVICE void cholesky (const Matrix<T,2,2>&, Matrix<T,2,2>&);
128 
131 template <typename T, uint32 N, uint32 M> CUGAR_API_CS CUGAR_HOST_DEVICE Matrix<T,N,M> outer_product(const Vector<T,N> op1, const Vector<T,M> op2);
132 
135 template <typename T>
136 CUGAR_HOST_DEVICE Matrix<T,4,4> translate(const Vector<T,3>& vec);
137 
140 template <typename T>
141 CUGAR_HOST_DEVICE Matrix<T,4,4> scale(const Vector<T,3>& vec);
142 
145 template <typename T>
146 Matrix<T,4,4> perspective(T fovy, T aspect, T zNear, T zFar);
147 
150 template <typename T>
151 Matrix<T,4,4> look_at(const Vector<T,3>& eye, const Vector<T,3>& center, const Vector<T,3>& up, bool flip_sign = false);
152 
155 template <typename T>
156 Matrix<T,4,4> inverse_look_at(const Vector<T,3>& eye, const Vector<T,3>& center, const Vector<T,3>& up, bool flip_sign = false);
157 
160 template <typename T>
161 CUGAR_HOST_DEVICE Matrix<T,4,4> rotation_around_X(const T q);
162 
165 template <typename T>
166 CUGAR_HOST_DEVICE Matrix<T,4,4> rotation_around_Y(const T q);
167 
170 template <typename T>
171 CUGAR_HOST_DEVICE Matrix<T,4,4> rotation_around_Z(const T q);
172 
175 template <typename T>
176 CUGAR_HOST_DEVICE Matrix<T,4,4> rotation_around_axis(const T q, const Vector3f& axis);
177 
180 CUGAR_HOST_DEVICE inline Vector3f ptrans(const Matrix4x4f& m, const Vector3f& v);
181 
184 CUGAR_HOST_DEVICE inline Vector3f vtrans(const Matrix4x4f& m, const Vector3f& v);
185 
188 CUGAR_HOST_DEVICE inline Vector2f eigen_values(const Matrix2x2f& m);
189 
192 CUGAR_HOST_DEVICE inline Vector2f singular_values(const Matrix2x2f& m);
193 
196 CUGAR_HOST_DEVICE inline void svd(
197  const Matrix2x2f& m,
198  Matrix2x2f& u,
199  Vector2f& s,
200  Matrix2x2f& v);
201 
207 {
208  CUGAR_HOST_DEVICE CUGAR_FORCEINLINE
209  float operator() (const float op1, const float op2) const { return op1 * op2; }
210 
211  template <typename T, uint32 N, uint32 M>
212  CUGAR_HOST_DEVICE CUGAR_FORCEINLINE
213  Matrix<T,N,M> operator() (const Vector<T,N> op1, const Vector<T,M> op2) const
214  {
215  return outer_product( op1, op2 );
216  }
217 };
218 
221 template <typename T, uint32 N, uint32 M>
223 {
224  CUGAR_HOST_DEVICE CUGAR_FORCEINLINE
225  Matrix<T,N,M> operator() (const Vector<T,N> op1, const Vector<T,M> op2) const
226  {
227  return outer_product( op1, op2 );
228  }
229 };
230 
233 template <typename T>
234 struct OuterProduct<T,1,1>
235 {
236  CUGAR_HOST_DEVICE CUGAR_FORCEINLINE
237  T operator() (const T op1, const T op2) const { return op1 * op2; }
238 };
239 
243 
246 
250 
253 
256 
257 } // namespace cugar
258 
259 #include <cugar/linalg/matrix_inline.h>
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator+=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:931
CUGAR_HOST_DEVICE Vector2f eigen_values(const Matrix2x2f &m)
Definition: matrix_inline.h:714
CUGAR_HOST_DEVICE Matrix< T, 4, 4 > rotation_around_axis(const T q, const Vector3f &axis)
Definition: matrix_inline.h:683
CUGAR_HOST_DEVICE Matrix< T, 4, 4 > translate(const Vector< T, 3 > &vec)
Definition: matrix_inline.h:524
CUGAR_HOST_DEVICE Vector2f singular_values(const Matrix2x2f &m)
Definition: matrix_inline.h:726
CUGAR_HOST_DEVICE Vector3f vtrans(const Matrix4x4f &m, const Vector3f &v)
Definition: matrix_inline.h:706
Definition: matrix.h:54
Matrix< T, 4, 4 > perspective(T fovy, T aspect, T zNear, T zFar)
Definition: matrix_inline.h:552
CUGAR_HOST_DEVICE Matrix< T, 4, 4 > rotation_around_Y(const T q)
Definition: matrix_inline.h:638
CUGAR_HOST_DEVICE Vector3f ptrans(const Matrix4x4f &m, const Vector3f &v)
Definition: matrix_inline.h:701
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator*=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:949
CUGAR_HOST_DEVICE Matrix< T, 4, 4 > rotation_around_Z(const T q)
Definition: matrix_inline.h:660
CUGAR_HOST_DEVICE void svd(const Matrix2x2f &m, Matrix2x2f &u, Vector2f &s, Matrix2x2f &v)
Definition: matrix_inline.h:752
Matrix< T, 4, 4 > look_at(const Vector< T, 3 > &eye, const Vector< T, 3 > &center, const Vector< T, 3 > &up, bool flip_sign=false)
Definition: matrix_inline.h:568
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator/=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:958
Definition: matrix.h:222
CUGAR_API_CS CUGAR_HOST_DEVICE Matrix< T, N, M > outer_product(const Vector< T, N > op1, const Vector< T, M > op2)
Definition: matrix_inline.h:503
CUGAR_HOST_DEVICE Matrix< T, 4, 4 > rotation_around_X(const T q)
Definition: matrix_inline.h:616
Definition: matrix.h:206
Definition: vector.h:187
Matrix< T, 4, 4 > inverse_look_at(const Vector< T, 3 > &eye, const Vector< T, 3 > &center, const Vector< T, 3 > &up, bool flip_sign=false)
Definition: matrix_inline.h:592
CUGAR_HOST_DEVICE Matrix< T, 4, 4 > scale(const Vector< T, 3 > &vec)
build a 3d scaling matrix
Definition: matrix_inline.h:539
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator-=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:940