30 #ifndef __optixu_optixu_matrix_namespace_h__
31 #define __optixu_optixu_matrix_namespace_h__
39 # define OPTIXU_INLINE_DEFINED 1
40 # define OPTIXU_INLINE __forceinline__
41 #endif // OPTIXU_INLINE
43 #define RT_MATRIX_ACCESS(m,i,j) m[i*N+j]
44 #define RT_MAT_DECL template <unsigned int M, unsigned int N>
49 template <>
struct VectorDim<2> {
typedef float2 VectorType; };
50 template <>
struct VectorDim<3> {
typedef float3 VectorType; };
51 template <>
struct VectorDim<4> {
typedef float4 VectorType; };
54 template <
unsigned int M,
unsigned int N>
class Matrix;
72 template <
unsigned int N>
73 OPTIXU_INLINE RT_HOSTDEVICE float2 operator*(
const Matrix<2,N>& m,
const typename Matrix<2,N>::floatN& vec );
74 template <
unsigned int N>
75 OPTIXU_INLINE RT_HOSTDEVICE float3 operator*(
const Matrix<3,N>& m,
const typename Matrix<3,N>::floatN& vec );
76 template <
unsigned int N>
77 OPTIXU_INLINE RT_HOSTDEVICE float4 operator*(
const Matrix<4,N>& m,
const typename Matrix<4,N>::floatN& vec );
78 OPTIXU_INLINE RT_HOSTDEVICE float4 operator*(
const Matrix<4,4>& m,
const float4& vec );
100 template <
unsigned int M,
unsigned int N>
111 RT_HOSTDEVICE
explicit Matrix(
const float data[M*N] ) {
for(
unsigned int i = 0; i < M*N; ++i) m_data[i] = data[i]; }
120 RT_HOSTDEVICE
float operator[](
unsigned int i )
const {
return m_data[i]; }
123 RT_HOSTDEVICE
float&
operator[](
unsigned int i ) {
return m_data[i]; }
126 RT_HOSTDEVICE floatN
getRow(
unsigned int m )
const;
132 RT_HOSTDEVICE
float*
getData();
135 RT_HOSTDEVICE
const float*
getData()
const;
138 RT_HOSTDEVICE
void setRow(
unsigned int m,
const floatN &r );
141 RT_HOSTDEVICE
void setCol(
unsigned int n,
const floatM &c );
150 RT_HOSTDEVICE
float det()
const;
153 RT_HOSTDEVICE
static Matrix<4,4> rotate(
const float radians,
const float3& axis);
165 RT_HOSTDEVICE
bool operator<( const Matrix<M, N>& rhs )
const;
173 template<
unsigned int M,
unsigned int N>
178 template<
unsigned int M,
unsigned int N>
181 for(
unsigned int i = 0; i < M*N; ++i)
185 template<
unsigned int M,
unsigned int N>
188 for(
unsigned int i = 0; i < M*N; ++i)
210 template<
unsigned int M,
unsigned int N>
213 typename Matrix<M,N>::floatN temp;
214 float* v =
reinterpret_cast<float*
>( &temp );
215 const float* row = &( m_data[m*N] );
216 for(
unsigned int i = 0; i < N; ++i)
223 template<
unsigned int M,
unsigned int N>
227 float* v =
reinterpret_cast<float*
>( &temp );
228 for (
unsigned int i = 0; i < M; ++i )
229 v[i] = RT_MATRIX_ACCESS( m_data, i, n );
235 template<
unsigned int M,
unsigned int N>
242 template<
unsigned int M,
unsigned int N>
249 template<
unsigned int M,
unsigned int N>
250 OPTIXU_INLINE RT_HOSTDEVICE
void Matrix<M,N>::setRow(
unsigned int m,
const typename Matrix<M,N>::floatN &r )
252 const float* v =
reinterpret_cast<const float*
>( &r );
253 float* row = &( m_data[m*N] );
254 for(
unsigned int i = 0; i < N; ++i)
259 template<
unsigned int M,
unsigned int N>
262 const float* v =
reinterpret_cast<const float*
>( &c );
263 for (
unsigned int i = 0; i < M; ++i )
264 RT_MATRIX_ACCESS( m_data, i, n ) = v[i];
269 template<
unsigned int M,
unsigned int N>
279 template<
unsigned int M,
unsigned int N>
280 RT_HOSTDEVICE Matrix<M,N>& operator-=(Matrix<M,N>& m1,
const Matrix<M,N>& m2)
282 for (
unsigned int i = 0; i < M*N; ++i )
289 template<
unsigned int M,
unsigned int N>
290 RT_HOSTDEVICE Matrix<M,N> operator+(
const Matrix<M,N>& m1,
const Matrix<M,N>& m2)
292 Matrix<M,N> temp( m1 );
299 template<
unsigned int M,
unsigned int N>
300 RT_HOSTDEVICE Matrix<M,N>& operator+=(Matrix<M,N>& m1,
const Matrix<M,N>& m2)
302 for (
unsigned int i = 0; i < M*N; ++i )
309 template<
unsigned int M,
unsigned int N,
unsigned int R>
310 RT_HOSTDEVICE Matrix<M,R> operator*(
const Matrix<M,N>& m1,
const Matrix<N,R>& m2)
314 for (
unsigned int i = 0; i < M; ++i ) {
315 for (
unsigned int j = 0; j < R; ++j ) {
317 for (
unsigned int k = 0; k < N; ++k ) {
318 float ik = m1[ i*N+k ];
319 float kj = m2[ k*R+j ];
330 template<
unsigned int M>
331 RT_HOSTDEVICE Matrix<M,M>& operator*=(Matrix<M,M>& m1,
const Matrix<M,M>& m2)
339 template<
unsigned int M,
unsigned int N>
340 RT_HOSTDEVICE
typename Matrix<M,N>::floatM operator*(
const Matrix<M,N>& m,
const typename Matrix<M,N>::floatN& vec )
342 typename Matrix<M,N>::floatM temp;
343 float* t =
reinterpret_cast<float*
>( &temp );
344 const float* v =
reinterpret_cast<const float*
>( &vec );
346 for (
unsigned int i = 0; i < M; ++i) {
348 for (
unsigned int j = 0; j < N; ++j) {
349 sum += RT_MATRIX_ACCESS( m, i, j ) * v[j];
358 template<
unsigned int N>
359 OPTIXU_INLINE RT_HOSTDEVICE float2 operator*(
const Matrix<2,N>& m,
const typename Matrix<2,N>::floatN& vec )
361 float2 temp = { 0.0f, 0.0f };
362 const float* v =
reinterpret_cast<const float*
>( &vec );
365 for (
unsigned int j = 0; j < N; ++j)
366 temp.x += m[index++] * v[j];
368 for (
unsigned int j = 0; j < N; ++j)
369 temp.y += m[index++] * v[j];
375 template<
unsigned int N>
376 OPTIXU_INLINE RT_HOSTDEVICE float3 operator*(
const Matrix<3,N>& m,
const typename Matrix<3,N>::floatN& vec )
378 float3 temp = { 0.0f, 0.0f, 0.0f };
379 const float* v =
reinterpret_cast<const float*
>( &vec );
382 for (
unsigned int j = 0; j < N; ++j)
383 temp.x += m[index++] * v[j];
385 for (
unsigned int j = 0; j < N; ++j)
386 temp.y += m[index++] * v[j];
388 for (
unsigned int j = 0; j < N; ++j)
389 temp.z += m[index++] * v[j];
395 template<
unsigned int N>
396 OPTIXU_INLINE RT_HOSTDEVICE float4 operator*(
const Matrix<4,N>& m,
const typename Matrix<4,N>::floatN& vec )
398 float4 temp = { 0.0f, 0.0f, 0.0f, 0.0f };
400 const float* v =
reinterpret_cast<const float*
>( &vec );
403 for (
unsigned int j = 0; j < N; ++j)
404 temp.x += m[index++] * v[j];
406 for (
unsigned int j = 0; j < N; ++j)
407 temp.y += m[index++] * v[j];
409 for (
unsigned int j = 0; j < N; ++j)
410 temp.z += m[index++] * v[j];
412 for (
unsigned int j = 0; j < N; ++j)
413 temp.w += m[index++] * v[j];
419 OPTIXU_INLINE RT_HOSTDEVICE float4 operator*(
const Matrix<4,4>& m,
const float4& vec )
422 temp.x = m[ 0] * vec.x +
426 temp.y = m[ 4] * vec.x +
430 temp.z = m[ 8] * vec.x +
434 temp.w = m[12] * vec.x +
443 template<
unsigned int M,
unsigned int N>
444 RT_HOSTDEVICE
typename Matrix<M,N>::floatN operator*(
const typename Matrix<M,N>::floatM& vec,
const Matrix<M,N>& m)
446 typename Matrix<M,N>::floatN temp;
447 float* t =
reinterpret_cast<float*
>( &temp );
448 const float* v =
reinterpret_cast<const float*
>( &vec);
450 for (
unsigned int i = 0; i < N; ++i) {
452 for (
unsigned int j = 0; j < M; ++j) {
453 sum += v[j] * RT_MATRIX_ACCESS( m, j, i ) ;
463 template<
unsigned int M,
unsigned int N>
464 RT_HOSTDEVICE Matrix<M,N> operator*(
const Matrix<M,N>& m,
float f)
466 Matrix<M,N> temp( m );
473 template<
unsigned int M,
unsigned int N>
474 RT_HOSTDEVICE Matrix<M,N>& operator*=(Matrix<M,N>& m,
float f)
476 for (
unsigned int i = 0; i < M*N; ++i )
483 template<
unsigned int M,
unsigned int N>
484 RT_HOSTDEVICE Matrix<M,N> operator*(
float f,
const Matrix<M,N>& m)
488 for (
unsigned int i = 0; i < M*N; ++i )
496 template<
unsigned int M,
unsigned int N>
497 RT_HOSTDEVICE Matrix<M,N> operator/(
const Matrix<M,N>& m,
float f)
499 Matrix<M,N> temp( m );
506 template<
unsigned int M,
unsigned int N>
507 RT_HOSTDEVICE Matrix<M,N>& operator/=(Matrix<M,N>& m,
float f)
509 float inv_f = 1.0f / f;
510 for (
unsigned int i = 0; i < M*N; ++i )
516 template<
unsigned int M,
unsigned int N>
520 for(
unsigned int row = 0; row < M; ++row )
521 for(
unsigned int col = 0; col < N; ++col )
522 ret[col*M+row] = m_data[row*N+col];
530 const float* m = m_data;
531 float d = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
532 - m[0]*m[5]*m[7] - m[1]*m[3]*m[8] - m[2]*m[4]*m[6];
540 const float* m = m_data;
542 m[0]*m[5]*m[10]*m[15]-
543 m[0]*m[5]*m[11]*m[14]+m[0]*m[9]*m[14]*m[7]-
544 m[0]*m[9]*m[6]*m[15]+m[0]*m[13]*m[6]*m[11]-
545 m[0]*m[13]*m[10]*m[7]-m[4]*m[1]*m[10]*m[15]+m[4]*m[1]*m[11]*m[14]-
546 m[4]*m[9]*m[14]*m[3]+m[4]*m[9]*m[2]*m[15]-
547 m[4]*m[13]*m[2]*m[11]+m[4]*m[13]*m[10]*m[3]+m[8]*m[1]*m[6]*m[15]-
548 m[8]*m[1]*m[14]*m[7]+m[8]*m[5]*m[14]*m[3]-
549 m[8]*m[5]*m[2]*m[15]+m[8]*m[13]*m[2]*m[7]-
550 m[8]*m[13]*m[6]*m[3]-
551 m[12]*m[1]*m[6]*m[11]+m[12]*m[1]*m[10]*m[7]-
552 m[12]*m[5]*m[10]*m[3]+m[12]*m[5]*m[2]*m[11]-
553 m[12]*m[9]*m[2]*m[7]+m[12]*m[9]*m[6]*m[3];
562 const float* m = m_data;
563 const float d = 1.0f / det();
565 dst[0] = d * (m[5] * (m[10] * m[15] - m[14] * m[11]) + m[9] * (m[14] * m[7] - m[6] * m[15]) + m[13] * (m[6] * m[11] - m[10] * m[7]));
566 dst[4] = d * (m[6] * (m[8] * m[15] - m[12] * m[11]) + m[10] * (m[12] * m[7] - m[4] * m[15]) + m[14] * (m[4] * m[11] - m[8] * m[7]));
567 dst[8] = d * (m[7] * (m[8] * m[13] - m[12] * m[9]) + m[11] * (m[12] * m[5] - m[4] * m[13]) + m[15] * (m[4] * m[9] - m[8] * m[5]));
568 dst[12] = d * (m[4] * (m[13] * m[10] - m[9] * m[14]) + m[8] * (m[5] * m[14] - m[13] * m[6]) + m[12] * (m[9] * m[6] - m[5] * m[10]));
569 dst[1] = d * (m[9] * (m[2] * m[15] - m[14] * m[3]) + m[13] * (m[10] * m[3] - m[2] * m[11]) + m[1] * (m[14] * m[11] - m[10] * m[15]));
570 dst[5] = d * (m[10] * (m[0] * m[15] - m[12] * m[3]) + m[14] * (m[8] * m[3] - m[0] * m[11]) + m[2] * (m[12] * m[11] - m[8] * m[15]));
571 dst[9] = d * (m[11] * (m[0] * m[13] - m[12] * m[1]) + m[15] * (m[8] * m[1] - m[0] * m[9]) + m[3] * (m[12] * m[9] - m[8] * m[13]));
572 dst[13] = d * (m[8] * (m[13] * m[2] - m[1] * m[14]) + m[12] * (m[1] * m[10] - m[9] * m[2]) + m[0] * (m[9] * m[14] - m[13] * m[10]));
573 dst[2] = d * (m[13] * (m[2] * m[7] - m[6] * m[3]) + m[1] * (m[6] * m[15] - m[14] * m[7]) + m[5] * (m[14] * m[3] - m[2] * m[15]));
574 dst[6] = d * (m[14] * (m[0] * m[7] - m[4] * m[3]) + m[2] * (m[4] * m[15] - m[12] * m[7]) + m[6] * (m[12] * m[3] - m[0] * m[15]));
575 dst[10] = d * (m[15] * (m[0] * m[5] - m[4] * m[1]) + m[3] * (m[4] * m[13] - m[12] * m[5]) + m[7] * (m[12] * m[1] - m[0] * m[13]));
576 dst[14] = d * (m[12] * (m[5] * m[2] - m[1] * m[6]) + m[0] * (m[13] * m[6] - m[5] * m[14]) + m[4] * (m[1] * m[14] - m[13] * m[2]));
577 dst[3] = d * (m[1] * (m[10] * m[7] - m[6] * m[11]) + m[5] * (m[2] * m[11] - m[10] * m[3]) + m[9] * (m[6] * m[3] - m[2] * m[7]));
578 dst[7] = d * (m[2] * (m[8] * m[7] - m[4] * m[11]) + m[6] * (m[0] * m[11] - m[8] * m[3]) + m[10] * (m[4] * m[3] - m[0] * m[7]));
579 dst[11] = d * (m[3] * (m[8] * m[5] - m[4] * m[9]) + m[7] * (m[0] * m[9] - m[8] * m[1]) + m[11] * (m[4] * m[1] - m[0] * m[5]));
580 dst[15] = d * (m[0] * (m[5] * m[10] - m[9] * m[6]) + m[4] * (m[9] * m[2] - m[1] * m[10]) + m[8] * (m[1] * m[6] - m[5] * m[2]));
587 OPTIXU_INLINE RT_HOSTDEVICE Matrix<4,4>
Matrix<4,4>::rotate(
const float radians,
const float3& axis)
590 float *m = Mat.getData();
593 float sintheta=sinf(radians);
594 float costheta=cosf(radians);
598 m[0*4+0]=ux*ux+costheta*(1-ux*ux);
599 m[0*4+1]=ux*uy*(1-costheta)-uz*sintheta;
600 m[0*4+2]=uz*ux*(1-costheta)+uy*sintheta;
603 m[1*4+0]=ux*uy*(1-costheta)+uz*sintheta;
604 m[1*4+1]=uy*uy+costheta*(1-uy*uy);
605 m[1*4+2]=uy*uz*(1-costheta)-ux*sintheta;
608 m[2*4+0]=uz*ux*(1-costheta)-uy*sintheta;
609 m[2*4+1]=uy*uz*(1-costheta)+ux*sintheta;
610 m[2*4+2]=uz*uz+costheta*(1-uz*uz);
618 return Matrix<4,4>( m );
627 float *m = Mat.getData();
633 return Matrix<4,4>( m );
642 float *m = Mat.getData();
648 return Matrix<4,4>( m );
653 template<
unsigned int M,
unsigned int N>
657 for(
unsigned int i = 0; i < N*N; ++i)
659 for(
unsigned int i = 0; i < N; ++i )
660 RT_MATRIX_ACCESS( temp,i,i ) = 1.0f;
665 template<
unsigned int M,
unsigned int N>
668 for(
unsigned int i = 0; i < N*M; ++i ) {
669 if( m_data[i] < rhs[i] )
671 else if( m_data[i] > rhs[i] )
692 const float *m4x4 = matrix.
getData();
694 m[0*3+0]=m4x4[0*4+0];
695 m[0*3+1]=m4x4[0*4+1];
696 m[0*3+2]=m4x4[0*4+2];
698 m[1*3+0]=m4x4[1*4+0];
699 m[1*3+1]=m4x4[1*4+1];
700 m[1*3+2]=m4x4[1*4+2];
702 m[2*3+0]=m4x4[2*4+0];
703 m[2*3+1]=m4x4[2*4+1];
704 m[2*3+2]=m4x4[2*4+2];
711 #undef RT_MATRIX_ACCESS
714 #ifdef OPTIXU_INLINE_DEFINED
715 # undef OPTIXU_INLINE_DEFINED
716 # undef OPTIXU_INLINE
719 #endif // __optixu_optixu_matrix_namespace_h__
static RT_HOSTDEVICE Matrix< 4, 4 > translate(const float3 &vec)
RT_HOSTDEVICE Matrix< 4, 4 > inverse() const
RT_HOSTDEVICE float operator[](unsigned int i) const
Definition: optixu_matrix_namespace.h:120
RT_HOSTDEVICE Matrix(const float data[M *N])
Definition: optixu_matrix_namespace.h:111
RT_HOSTDEVICE Matrix & operator=(const Matrix &b)
Definition: optixu_matrix_namespace.h:186
RT_HOSTDEVICE floatN getRow(unsigned int m) const
Definition: optixu_matrix_namespace.h:211
A matrix with M rows and N columns.
Definition: optixu_matrix_namespace.h:54
static RT_HOSTDEVICE Matrix< 4, 4 > rotate(const float radians, const float3 &axis)
Definition: optixu_matrix_namespace.h:48
static RT_HOSTDEVICE Matrix< 4, 4 > scale(const float3 &vec)
static RT_HOSTDEVICE Matrix< N, N > identity()
Definition: optixu_matrix_namespace.h:654
RT_HOSTDEVICE Matrix()
A column of the matrix.
Definition: optixu_matrix_namespace.h:174
RT_HOSTDEVICE float & operator[](unsigned int i)
Definition: optixu_matrix_namespace.h:123
RT_HOSTDEVICE floatM getCol(unsigned int n) const
Definition: optixu_matrix_namespace.h:224
RT_HOSTDEVICE void setRow(unsigned int m, const floatN &r)
Definition: optixu_matrix_namespace.h:250
RT_HOSTDEVICE Matrix< N, M > transpose() const
Definition: optixu_matrix_namespace.h:517
RT_HOSTDEVICE bool operator<(const Matrix< M, N > &rhs) const
Definition: optixu_matrix_namespace.h:666
VectorDim< M >::VectorType floatM
A row of the matrix.
Definition: optixu_matrix_namespace.h:105
RT_HOSTDEVICE float * getData()
Definition: optixu_matrix_namespace.h:236
RT_HOSTDEVICE float det() const
RT_HOSTDEVICE void setCol(unsigned int n, const floatM &c)
Definition: optixu_matrix_namespace.h:260