53 #define ACG_GLMATRIX_C 59 #include "GLMatrixT.hh" 72 template<
typename Scalar>
75 scale( Scalar _x, Scalar _y, Scalar _z,
85 if (_mult_from == MULT_FROM_RIGHT) *
this *= m;
93 template<
typename Scalar>
106 if (_mult_from == MULT_FROM_RIGHT) *
this *= m;
114 template<
typename Scalar>
123 Scalar ca = cos(_angle * (M_PI/180.0));
124 Scalar sa = sin(_angle * (M_PI/180.0));
129 m(1,1) = m(2,2) = ca; m(2,1) = sa; m(1,2) = -sa;
133 m(0,0) = m(2,2) = ca; m(0,2) = sa; m(2,0) = -sa;
137 m(0,0) = m(1,1) = ca; m(1,0) = sa; m(0,1) = -sa;
142 if (_mult_from == MULT_FROM_RIGHT) *
this *= m;
152 template <
typename Scalar>
155 rotate( Scalar angle, Scalar x, Scalar y, Scalar z,
160 Scalar xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
162 mag = sqrt( x*x + y*y + z*z );
167 s = sin( angle * ( Scalar(M_PI) / Scalar(180.) ) );
168 c = cos( angle * ( Scalar(M_PI) / Scalar(180.) ) );
183 one_c =
static_cast<Scalar
>(1.0) - c;
185 m(0,0) = (one_c * xx) + c;
186 m(0,1) = (one_c * xy) - zs;
187 m(0,2) = (one_c * zx) + ys;
188 m(0,3) =
static_cast<Scalar
>(0.0);
190 m(1,0) = (one_c * xy) + zs;
191 m(1,1) = (one_c * yy) + c;
192 m(1,2) = (one_c * yz) - xs;
193 m(1,3) =
static_cast<Scalar
>(0.0);
195 m(2,0) = (one_c * zx) - ys;
196 m(2,1) = (one_c * yz) + xs;
197 m(2,2) = (one_c * zz) + c;
198 m(2,3) =
static_cast<Scalar
>(0.0);
200 m(3,0) =
static_cast<Scalar
>(0.0);
201 m(3,1) =
static_cast<Scalar
>(0.0);
202 m(3,2) =
static_cast<Scalar
>(0.0);
203 m(3,3) =
static_cast<Scalar
>(1.0);
205 if (_mult_from == MULT_FROM_RIGHT) *
this *= m;
213 template<
typename Scalar>
231 m(0,0)=x[0]; m(0,1)=x[1]; m(0,2)=x[2]; m(0,3)=0.0;
232 m(1,0)=y[0]; m(1,1)=y[1]; m(1,2)=y[2]; m(1,3)=0.0;
233 m(2,0)=z[0]; m(2,1)=z[1]; m(2,2)=z[2]; m(2,3)=0.0;
234 m(3,0)=
static_cast<Scalar
>(0.0); m(3,1)=
static_cast<Scalar
>(0.0); m(3,2)=
static_cast<Scalar
>(0.0); m(3,3)=
static_cast<Scalar
>(1.0);
237 translate(-eye[0], -eye[1], -eye[2]);
244 template<
typename Scalar>
262 m(0,0)=x[0]; m(0,1)=y[0]; m(0,2)=z[0]; m(0,3)=eye[0];
263 m(1,0)=x[1]; m(1,1)=y[1]; m(1,2)=z[1]; m(1,3)=eye[1];
264 m(2,0)=x[2]; m(2,1)=y[2]; m(2,2)=z[2]; m(2,3)=eye[2];
265 m(3,0)=
static_cast<Scalar
>(0.0); m(3,1)=
static_cast<Scalar
>(0.0); m(3,2)=
static_cast<Scalar
>(0.0); m(3,3)=
static_cast<Scalar
>(1.0);
274 template<
typename Scalar>
278 Scalar near_plane, Scalar far_plane)
280 Scalar ymax = near_plane * tan( fovY * static_cast<Scalar>(M_PI) / static_cast<Scalar>(360.0) );
282 Scalar xmin = ymin * aspect;
283 Scalar xmax = ymax * aspect;
284 frustum(xmin, xmax, ymin, ymax, near_plane, far_plane);
291 template<
typename Scalar>
295 Scalar near_plane, Scalar far_plane)
297 Scalar ymax = near_plane * tan( fovY * static_cast<Scalar>(M_PI) / static_cast<Scalar>(360.0) );
299 Scalar xmin = ymin * aspect;
300 Scalar xmax = ymax * aspect;
301 inverse_frustum(xmin, xmax, ymin, ymax, near_plane, far_plane);
308 template<
typename Scalar>
312 Scalar bottom, Scalar top,
313 Scalar near_plane, Scalar far_plane )
315 assert(near_plane > static_cast<Scalar>(0.0) && far_plane > near_plane);
318 Scalar x, y, a, b, c, d;
320 x = (
static_cast<Scalar
>(2.0)*near_plane) / (right-left);
321 y = (
static_cast<Scalar
>(2.0)*near_plane) / (top-bottom);
322 a = (right+left) / (right-left);
323 b = (top+bottom) / (top-bottom);
324 c = -(far_plane+near_plane) / ( far_plane-near_plane);
325 d = -(
static_cast<Scalar
>(2.0)*far_plane*near_plane) / (far_plane-near_plane);
327 m(0,0) = x; m(0,1) =
static_cast<Scalar
>(0.0); m(0,2) = a; m(0,3) =
static_cast<Scalar
>(0.0);
328 m(1,0) =
static_cast<Scalar
>(0.0); m(1,1) = y; m(1,2) = b; m(1,3) =
static_cast<Scalar
>(0.0);
329 m(2,0) =
static_cast<Scalar
>(0.0); m(2,1) =
static_cast<Scalar
>(0.0); m(2,2) = c; m(2,3) = d;
330 m(3,0) =
static_cast<Scalar
>(0.0); m(3,1) =
static_cast<Scalar
>(0.0); m(3,2) =
static_cast<Scalar
>(-1.0); m(3,3) =
static_cast<Scalar
>(0.0);
339 template<
typename Scalar>
343 Scalar bottom, Scalar top,
344 Scalar near_plane, Scalar far_plane)
346 assert(near_plane > 0.0 && far_plane > near_plane);
349 Scalar x, y, a, b, c, d;
351 x = (right-left) / (static_cast<Scalar>(2.0)*near_plane);
352 y = (top-bottom) / (static_cast<Scalar>(2.0)*near_plane);
353 a = (right+left) / (static_cast<Scalar>(2.0)*near_plane);
354 b = (top+bottom) / (static_cast<Scalar>(2.0)*near_plane);
355 c = (far_plane+near_plane) / (static_cast<Scalar>(2.0)*far_plane*near_plane);
356 d = (near_plane-far_plane) / (static_cast<Scalar>(2.0)*far_plane*near_plane);
358 m(0,0)=x; m(0,1)=
static_cast<Scalar
>(0.0); m(0,2)=
static_cast<Scalar
>(0.0); m(0,3)= a;
359 m(1,0)=
static_cast<Scalar
>(0.0); m(1,1)= y; m(1,2)=
static_cast<Scalar
>(0.0); m(1,3)= b;
360 m(2,0)=
static_cast<Scalar
>(0.0); m(2,1)=
static_cast<Scalar
>(0.0); m(2,2)=
static_cast<Scalar
>(0.0); m(2,3)=
static_cast<Scalar
>(-1.0);
361 m(3,0)=
static_cast<Scalar
>(0.0); m(3,1)=
static_cast<Scalar
>(0.0); m(3,2)= d; m(3,3)= c;
370 template<
typename Scalar>
374 Scalar bottom, Scalar top,
375 Scalar near_plane, Scalar far_plane)
379 Scalar r_l = right-left;
380 Scalar t_b = top - bottom;
381 Scalar f_n = far_plane - near_plane;
385 m(1,0) = m(0,1) = m(0,2) =
386 m(2,0) = m(2,1) = m(1,2) =
387 m(3,0) = m(3,1) = m(3,2) =
static_cast<Scalar
>(0.0);
389 m(0,0) =
static_cast<Scalar
>( 2.0) / r_l;
390 m(1,1) =
static_cast<Scalar
>( 2.0) / t_b;
391 m(2,2) =
static_cast<Scalar
>(-2.0) / f_n;
393 m(0,3) = -(right + left) / r_l;
394 m(1,3) = -(top + bottom) / t_b;
395 m(2,3) = -(far_plane + near_plane) / f_n;
397 m(3,3) =
static_cast<Scalar
>(1.0);
405 template<
typename Scalar>
409 Scalar bottom, Scalar top,
410 Scalar near_plane, Scalar far_plane)
414 m(1,0) = m(0,1) = m(0,2) =
415 m(2,0) = m(2,1) = m(1,2) =
416 m(3,0) = m(3,1) = m(3,2) =
static_cast<Scalar
>(0.0);
419 m(0,0) =
static_cast<Scalar
>( 0.5) * (right - left);
420 m(1,1) =
static_cast<Scalar
>( 0.5) * (top - bottom);
421 m(2,2) =
static_cast<Scalar
>(-0.5) * (far_plane - near_plane);
423 m(0,3) =
static_cast<Scalar
>( 0.5) * (right + left);
424 m(1,3) =
static_cast<Scalar
>( 0.5) * (top + bottom);
425 m(2,3) =
static_cast<Scalar
>(-0.5) * (far_plane + near_plane);
427 m(3,3) =
static_cast<Scalar
>(1.0);
435 template<
typename Scalar>
440 Scalar a = (*this)(2,2);
441 Scalar b = (*this)(2,3);
443 return VectorT<Scalar, 2>(b / (
static_cast<Scalar
>(-1.0) + a), b / (static_cast<Scalar>(1.0) + a));
448 template<
typename Scalar>
453 Scalar a = (*this)(2,2);
454 Scalar b = (*this)(2,3);
461 template<
typename Scalar>
498 for (
int i = 0; i < nnz; ++i)
500 if ( checkEpsilon( (*
this)(nze[i*2],nze[i*2+1]) ) )
505 if ( !checkEpsilon( (*
this)(3,2) + 1.0 ) )
509 for (
int i = 0; i < nz; ++i)
511 if ( !checkEpsilon( (*
this)(ze[i*2],ze[i*2+1]) ) )
520 template<
typename Scalar>
529 for (
int i = 0; i < 3; ++i)
531 if ( checkEpsilon( (*
this)(i,i) ) )
536 if ( !checkEpsilon( (*
this)(3,3) - 1.0 ) )
540 if ( checkEpsilon( (*
this)(2,3) ) )
562 for (
int i = 0; i < nz; ++i)
564 if ( !checkEpsilon( (*
this)(ze[i*2],ze[i*2+1]) ) )
573 template<
typename Scalar>
579 return extract_planes_perspective();
582 return extract_planes_ortho();
void frustum(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
Namespace providing different geometric functions concerning angles.
bool isPerspective() const
check if the matrix is a perspective projection matrix
void inverse_perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse of perspective projection matrix
void lookAt(const Vec3 &eye, const Vec3 ¢er, const Vec3 &up)
void inverse_lookAt(const Vec3 &eye, const Vec3 ¢er, const Vec3 &up)
multiply self from left with inverse lookAt matrix
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
VectorT< Scalar, 2 > extract_planes_perspective() const
extract near and far clipping planes from a perspective projection matrix
void inverse_ortho(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse orthographic projection matrix
void scale(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
4x4 matrix implementing OpenGL commands.
void inverse_frustum(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse of perspective projection matrix
void ortho(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with orthographic projection matrix
void perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
void identity()
setup an identity matrix
VectorT< Scalar, 2 > extract_planes_ortho() const
extract near and far clipping planes from an orthographic projection matrix
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
VectorT< Scalar, 2 > extract_planes() const
detect type of projection matrix and extract near and far clipping planes
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
bool isOrtho() const
check if the matrix is an orthographic projection matrix