Browse Source

Add LMatrix*::xform_vec_general(), fix clip plane issues

David Rose 18 years ago
parent
commit
fb973c9cfd

+ 0 - 7
panda/src/char/config_char.cxx

@@ -26,7 +26,6 @@
 #include "characterVertexSlider.h"
 #include "jointVertexTransform.h"
 #include "dconfig.h"
-#include "lmatrix4.h"
 
 Configure(config_char);
 NotifyCategoryDef(char, "");
@@ -68,12 +67,6 @@ init_libchar() {
   CharacterVertexSlider::init_type();
   JointVertexTransform::init_type();
 
-  // This isn't defined in this package, but it *is* essential that it
-  // be initialized.  We have to do it explicitly here since template
-  // statics don't necessarily resolve very well across dynamic
-  // libraries.
-  LMatrix4f::init_type();
-
   //Registration of writeable object's creation
   //functions with BamReader's factory
   Character::register_with_read_factory();

+ 6 - 6
panda/src/linmath/Sources.pp

@@ -14,8 +14,8 @@
      compose_matrix_src.cxx compose_matrix_src.h config_linmath.h  \
      coordinateSystem.h dbl2fltnames.h dblnames.h deg_2_rad.h  \
      flt2dblnames.h fltnames.h lcast_to.h lcast_to_src.h  \
-     lcast_to_src.I lmatrix.h lmatrix3.h lmatrix3_src.I  \
-     lmatrix3_src.cxx lmatrix3_src.h lmatrix4.h lmatrix4_src.I  \
+     lcast_to_src.I lmatrix.h  lmatrix3_src.I  \
+     lmatrix3_src.cxx lmatrix3_src.h lmatrix4_src.I  \
      lmatrix4_src.cxx lmatrix4_src.h lorientation.h  \
      lorientation_src.I lorientation_src.cxx lorientation_src.h  \
      lpoint2.h lpoint2_src.I lpoint2_src.cxx lpoint2_src.h  \
@@ -39,8 +39,8 @@
     
    #define INCLUDED_SOURCES \
      compose_matrix.cxx config_linmath.cxx coordinateSystem.cxx  \
-     lmatrix.cxx lmatrix3.cxx  \
-     lmatrix4.cxx lorientation.cxx lpoint2.cxx  \
+     lmatrix.cxx \
+     lorientation.cxx lpoint2.cxx  \
      lpoint3.cxx lpoint4.cxx lquaternion.cxx lrotation.cxx  \
      luse.cxx lvecBase2.cxx lvecBase3.cxx lvecBase4.cxx  \
      lvector2.cxx lvector3.cxx lvector4.cxx mathNumbers.cxx  \
@@ -53,8 +53,8 @@
     compose_matrix_src.h config_linmath.h coordinateSystem.h \
     dbl2fltnames.h dblnames.h deg_2_rad.h \
     flt2dblnames.h fltnames.h lcast_to.h lcast_to_src.I lcast_to_src.h \
-    lmat_ops.h lmat_ops_src.I lmat_ops_src.h lmatrix.h lmatrix3.h \
-    lmatrix3_src.I lmatrix3_src.h lmatrix4.h lmatrix4_src.I \
+    lmat_ops.h lmat_ops_src.I lmat_ops_src.h lmatrix.h \
+    lmatrix3_src.I lmatrix3_src.h lmatrix4_src.I \
     lmatrix4_src.h lorientation.h lorientation_src.I \
     lorientation_src.h lpoint2.h lpoint2_src.I lpoint2_src.h lpoint3.h \
     lpoint3_src.I lpoint3_src.h lpoint4.h lpoint4_src.I lpoint4_src.h \

+ 0 - 2
panda/src/linmath/linmath_composite2.cxx

@@ -2,8 +2,6 @@
 #include "compose_matrix.cxx"
 #include "config_linmath.cxx"
 #include "coordinateSystem.cxx"
-#include "lmatrix3.cxx"
-#include "lmatrix4.cxx"
 #include "lmatrix.cxx"
 #include "lorientation.cxx"
 #include "lpoint2.cxx"

+ 1 - 2
panda/src/linmath/lmat_ops.h

@@ -25,8 +25,7 @@
 #include "lvecBase4.h"
 #include "lpoint4.h"
 #include "lvector4.h"
-#include "lmatrix3.h"
-#include "lmatrix4.h"
+#include "lmatrix.h"
 
 #include "fltnames.h"
 #include "lmat_ops_src.h"

+ 7 - 6
panda/src/linmath/lmatrix.cxx

@@ -16,11 +16,12 @@
 //
 ////////////////////////////////////////////////////////////////////
 
-#include "pandabase.h"
 #include "lmatrix.h"
 
-// This tells GCC to explicitly instantiate the templates defined in
-// lmatrix.h and leave them here.
-#ifdef __GNUC__
-#pragma implementation
-#endif
+#include "fltnames.h"
+#include "lmatrix3_src.cxx"
+#include "lmatrix4_src.cxx"
+
+#include "dblnames.h"
+#include "lmatrix3_src.cxx"
+#include "lmatrix4_src.cxx"

+ 17 - 12
panda/src/linmath/lmatrix.h

@@ -21,21 +21,26 @@
 
 #include "pandabase.h"
 #include "config_linmath.h"
+#include <math.h>
+#include "typedObject.h"
+#include "datagram.h"
+#include "datagramIterator.h"
+#include "pnotify.h"
+#include "indent.h"
 
-#include "lmatrix3.h"
-#include "lmatrix4.h"
+#include "deg_2_rad.h"
+#include "nearly_zero.h"
 
-/*
-typedef LMatrix3<float> LMatrix3f;
-typedef LMatrix4<float> LMatrix4f;
+#include "coordinateSystem.h"
+#include "lvecBase4.h"
+#include "lvecBase3.h"
+#include "lvecBase2.h"
 
-typedef LMatrix3<double> LMatrix3d;
-typedef LMatrix4<double> LMatrix4d;
-*/
+#include "fltnames.h"
+// lmatrix3_src.h includes lmatrix4_src.h.
+#include "lmatrix3_src.h"
 
-// Tell GCC that we'll take care of the instantiation explicitly here.
-#ifdef __GNUC__
-#pragma interface
-#endif
+#include "dblnames.h"
+#include "lmatrix3_src.h"
 
 #endif

+ 0 - 28
panda/src/linmath/lmatrix3.cxx

@@ -1,28 +0,0 @@
-// Filename: lmatrix3.cxx
-// Created by:  drose (29Jan99)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
-//
-// All use of this software is subject to the terms of the Panda 3d
-// Software license.  You should have received a copy of this license
-// along with this source code; you will also find a current copy of
-// the license at http://etc.cmu.edu/panda3d/docs/license/ .
-//
-// To contact the maintainers of this program write to
-// [email protected] .
-//
-////////////////////////////////////////////////////////////////////
-
-#include "config_linmath.h"
-#include "lmatrix3.h"
-
-#include "fltnames.h"
-#include "lmatrix3_src.cxx"
-
-#include "dblnames.h"
-#include "lmatrix3_src.cxx"
-
-

+ 0 - 41
panda/src/linmath/lmatrix3.h

@@ -1,41 +0,0 @@
-// Filename: lmatrix3.h
-// Created by:  drose (29Jan99)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
-//
-// All use of this software is subject to the terms of the Panda 3d
-// Software license.  You should have received a copy of this license
-// along with this source code; you will also find a current copy of
-// the license at http://etc.cmu.edu/panda3d/docs/license/ .
-//
-// To contact the maintainers of this program write to
-// [email protected] .
-//
-////////////////////////////////////////////////////////////////////
-
-#ifndef LMATRIX3_H
-#define LMATRIX3_H
-
-#include "pandabase.h"
-#include <math.h>
-#include "typedObject.h"
-#include "datagram.h"
-#include "datagramIterator.h"
-#include "pnotify.h"
-#include "indent.h"
-#include "deg_2_rad.h"
-#include "nearly_zero.h"
-#include "coordinateSystem.h"
-#include "lvecBase3.h"
-#include "lvecBase2.h"
-
-#include "fltnames.h"
-#include "lmatrix3_src.h"
-
-#include "dblnames.h"
-#include "lmatrix3_src.h"
-
-#endif

+ 112 - 2
panda/src/linmath/lmatrix3_src.I

@@ -450,8 +450,7 @@ v_res._v.v._2 = v._v.v._0*mat._m.m._02 + v._v.v._1*mat._m.m._12 + v._v.v._2*mat.
 ////////////////////////////////////////////////////////////////////
 //     Function: LMatrix3::xform
 //       Access: Public
-//  Description: 3-component vector or point times matrix.  This is a
-//               fully general operation.
+//  Description: 3-component vector or point times matrix.
 ////////////////////////////////////////////////////////////////////
 INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix3)::
 xform(const FLOATNAME(LVecBase3) &v) const {
@@ -506,6 +505,45 @@ xform_vec(const FLOATNAME(LVecBase2) &v) const {
 //                          v.dot(get_col2(1)));
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: LMatrix3::xform_vec
+//       Access: Public
+//  Description: The matrix transforms a 3-component vector and
+//               returns the result.  This assumes the matrix is an
+//               orthonormal transform.
+//
+//               In practice, this is the same computation as xform().
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix3)::
+xform_vec(const FLOATNAME(LVecBase3) &v) const {
+  TAU_PROFILE("LVecBase3 LMatrix3::xform_vec(const LVecBase3 &)", " ", TAU_USER);
+  FLOATNAME(LVecBase3) v_res;
+  
+  // v._v.v._3 == 0.0f for this case
+  
+  v_res._v.v._0 = v._v.v._0*_m.m._00 + v._v.v._1*_m.m._10 + v._v.v._2*_m.m._20;
+  v_res._v.v._1 = v._v.v._0*_m.m._01 + v._v.v._1*_m.m._11 + v._v.v._2*_m.m._21;
+  v_res._v.v._2 = v._v.v._0*_m.m._02 + v._v.v._1*_m.m._12 + v._v.v._2*_m.m._22;
+  
+  return v_res;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: LMatrix3::xform_vec_general
+//       Access: Public
+//  Description: The matrix transforms a 3-component vector (without
+//               translation component) and returns the result, as a
+//               fully general operation.
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix3)::
+xform_vec_general(const FLOATNAME(LVecBase3) &v) const {
+  TAU_PROFILE("LVecBase3 LMatrix3::xform_vec_general(const LVecBase3 &)", " ", TAU_USER);
+  FLOATNAME(LMatrix3) i;
+  i.invert_transpose_from(*this);
+
+  return i.xform(v);
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: LMatrix3::mult_cel
 //       Access: Private
@@ -833,6 +871,78 @@ invert_in_place() {
   return invert_from(temp);
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: LMatrix3::invert_transpose_from
+//       Access: Public
+//  Description: Simultaneously computes the inverse of the indicated
+//               matrix, and then the transpose of that inverse.
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH bool FLOATNAME(LMatrix3)::
+invert_transpose_from(const FLOATNAME(LMatrix3) &other) {
+  TAU_PROFILE("bool LMatrix3::invert_transpose_from(const LMatrix3 &)", " ", TAU_USER);
+
+  FLOATTYPE other_det = MATRIX3_DETERMINANT(other._m.m);
+
+  if (IS_THRESHOLD_ZERO(other_det, (NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE)))) {
+#ifdef NOTIFY_DEBUG
+    linmath_cat.warning() << "Tried to invert singular LMatrix3.\n";
+#endif
+    (*this) = ident_mat();
+    return false;
+  }
+
+  other_det = 1.0f / other_det;
+  _m.m._00 =  other_det * DET2(other._m.m._11, other._m.m._12, other._m.m._21, other._m.m._22);
+  _m.m._01 = -other_det * DET2(other._m.m._10, other._m.m._12, other._m.m._20, other._m.m._22);
+  _m.m._02 =  other_det * DET2(other._m.m._10, other._m.m._11, other._m.m._20, other._m.m._21);
+
+  _m.m._10 = -other_det * DET2(other._m.m._01, other._m.m._02, other._m.m._21, other._m.m._22);
+  _m.m._11 =  other_det * DET2(other._m.m._00, other._m.m._02, other._m.m._20, other._m.m._22);
+  _m.m._12 = -other_det * DET2(other._m.m._00, other._m.m._01, other._m.m._20, other._m.m._21);
+
+  _m.m._20 =  other_det * DET2(other._m.m._01, other._m.m._02, other._m.m._11, other._m.m._12);
+  _m.m._21 = -other_det * DET2(other._m.m._00, other._m.m._02, other._m.m._10, other._m.m._12);
+  _m.m._22 =  other_det * DET2(other._m.m._00, other._m.m._01, other._m.m._10, other._m.m._11);
+
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: LMatrix3::invert_transpose_from
+//       Access: Public
+//  Description: Simultaneously computes the inverse of the indicated
+//               matrix, and then the transpose of that inverse.
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH bool FLOATNAME(LMatrix3)::
+invert_transpose_from(const FLOATNAME(LMatrix4) &other) {
+  TAU_PROFILE("bool LMatrix3::invert_transpose_from(const LMatrix4 &)", " ", TAU_USER);
+
+  FLOATTYPE other_det = MATRIX3_DETERMINANT(other._m.m);
+
+  if (IS_THRESHOLD_ZERO(other_det, (NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE)))) {
+#ifdef NOTIFY_DEBUG
+    linmath_cat.warning() << "Tried to invert singular LMatrix4.\n";
+#endif
+    (*this) = ident_mat();
+    return false;
+  }
+
+  other_det = 1.0f / other_det;
+  _m.m._00 =  other_det * DET2(other._m.m._11, other._m.m._12, other._m.m._21, other._m.m._22);
+  _m.m._01 = -other_det * DET2(other._m.m._10, other._m.m._12, other._m.m._20, other._m.m._22);
+  _m.m._02 =  other_det * DET2(other._m.m._10, other._m.m._11, other._m.m._20, other._m.m._21);
+
+  _m.m._10 = -other_det * DET2(other._m.m._01, other._m.m._02, other._m.m._21, other._m.m._22);
+  _m.m._11 =  other_det * DET2(other._m.m._00, other._m.m._02, other._m.m._20, other._m.m._22);
+  _m.m._12 = -other_det * DET2(other._m.m._00, other._m.m._01, other._m.m._20, other._m.m._21);
+
+  _m.m._20 =  other_det * DET2(other._m.m._01, other._m.m._02, other._m.m._11, other._m.m._12);
+  _m.m._21 = -other_det * DET2(other._m.m._00, other._m.m._02, other._m.m._10, other._m.m._12);
+  _m.m._22 =  other_det * DET2(other._m.m._00, other._m.m._01, other._m.m._10, other._m.m._11);
+
+  return true;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: LMatrix::set_translate_mat
 //       Access: Public

+ 15 - 0
panda/src/linmath/lmatrix3_src.h

@@ -16,6 +16,8 @@
 //
 ////////////////////////////////////////////////////////////////////
 
+class FLOATNAME(LMatrix4);
+
 ////////////////////////////////////////////////////////////////////
 //       Class : LMatrix3
 // Description : This is a 3-by-3 transform matrix.  It typically will
@@ -103,6 +105,12 @@ PUBLISHED:
   INLINE_LINMATH FLOATNAME(LVecBase2)
   xform_vec(const FLOATNAME(LVecBase2) &v) const;
 
+  INLINE_LINMATH FLOATNAME(LVecBase3)
+  xform_vec(const FLOATNAME(LVecBase3) &v) const;
+
+  INLINE_LINMATH FLOATNAME(LVecBase3)
+  xform_vec_general(const FLOATNAME(LVecBase3) &v) const;
+
   // this = other1 * other2
   INLINE_LINMATH void multiply(
     const FLOATNAME(LMatrix3) &other1, const FLOATNAME(LMatrix3) &other2);
@@ -131,6 +139,9 @@ PUBLISHED:
   INLINE_LINMATH bool invert_from(const FLOATNAME(LMatrix3) &other);
   INLINE_LINMATH bool invert_in_place();
 
+  INLINE_LINMATH bool invert_transpose_from(const FLOATNAME(LMatrix3) &other);
+  INLINE_LINMATH bool invert_transpose_from(const FLOATNAME(LMatrix4) &other);
+
   static INLINE_LINMATH const FLOATNAME(LMatrix3) &ident_mat();
 
   // A 3x3 matrix is likely to be used for one of two purposes.  In
@@ -289,4 +300,8 @@ INLINE_LINMATH FLOATNAME(LMatrix3) transpose(const FLOATNAME(LMatrix3) &a);
 INLINE_LINMATH FLOATNAME(LMatrix3) invert(const FLOATNAME(LMatrix3) &a);
 END_PUBLISH
 
+// We can safely include lmatrix4_src.h down here and avoid circular
+// dependencies.
+#include "lmatrix4_src.h"
+
 #include "lmatrix3_src.I"

+ 0 - 28
panda/src/linmath/lmatrix4.cxx

@@ -1,28 +0,0 @@
-// Filename: lmatrix4.cxx
-// Created by:  drose (29Jan99)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
-//
-// All use of this software is subject to the terms of the Panda 3d
-// Software license.  You should have received a copy of this license
-// along with this source code; you will also find a current copy of
-// the license at http://etc.cmu.edu/panda3d/docs/license/ .
-//
-// To contact the maintainers of this program write to
-// [email protected] .
-//
-////////////////////////////////////////////////////////////////////
-
-#include "config_linmath.h"
-#include "lmatrix4.h"
-
-#include "fltnames.h"
-#include "lmatrix4_src.cxx"
-
-#include "dblnames.h"
-#include "lmatrix4_src.cxx"
-
-

+ 0 - 43
panda/src/linmath/lmatrix4.h

@@ -1,43 +0,0 @@
-// Filename: lmatrix4.h
-// Created by:  drose (29Jan99)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
-//
-// All use of this software is subject to the terms of the Panda 3d
-// Software license.  You should have received a copy of this license
-// along with this source code; you will also find a current copy of
-// the license at http://etc.cmu.edu/panda3d/docs/license/ .
-//
-// To contact the maintainers of this program write to
-// [email protected] .
-//
-////////////////////////////////////////////////////////////////////
-
-#ifndef LMATRIX4_H
-#define LMATRIX4_H
-
-#include "pandabase.h"
-#include <math.h>
-#include "typedObject.h"
-#include "datagram.h"
-#include "datagramIterator.h"
-#include "indent.h"
-
-#include "deg_2_rad.h"
-#include "nearly_zero.h"
-
-#include "coordinateSystem.h"
-#include "lvecBase4.h"
-#include "lvecBase3.h"
-#include "lmatrix3.h"
-
-#include "fltnames.h"
-#include "lmatrix4_src.h"
-
-#include "dblnames.h"
-#include "lmatrix4_src.h"
-
-#endif

+ 17 - 1
panda/src/linmath/lmatrix4_src.I

@@ -656,7 +656,7 @@ xform_point(const FLOATNAME(LVecBase3) &v) const {
 //       Access: Public
 //  Description: The matrix transforms a 3-component vector (without
 //               translation component) and returns the result.  This
-//               assumes the matrix is an affine transform.
+//               assumes the matrix is an orthonormal transform.
 ////////////////////////////////////////////////////////////////////
 INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix4)::
 xform_vec(const FLOATNAME(LVecBase3) &v) const {
@@ -672,6 +672,22 @@ xform_vec(const FLOATNAME(LVecBase3) &v) const {
   return v_res;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: LMatrix4::xform_vec_general
+//       Access: Public
+//  Description: The matrix transforms a 3-component vector (without
+//               translation component) and returns the result, as a
+//               fully general operation.
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix4)::
+xform_vec_general(const FLOATNAME(LVecBase3) &v) const {
+  TAU_PROFILE("LVecBase3 LMatrix4::xform_vec_general(const LVecBase3 &)", " ", TAU_USER);
+  FLOATNAME(LMatrix3) i;
+  i.invert_transpose_from(*this);
+
+  return i.xform(v);
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: LMatrix4::mult_cel
 //       Access: Private

+ 3 - 0
panda/src/linmath/lmatrix4_src.h

@@ -107,6 +107,9 @@ PUBLISHED:
   INLINE_LINMATH FLOATNAME(LVecBase3)
   xform_vec(const FLOATNAME(LVecBase3) &v) const;
 
+  INLINE_LINMATH FLOATNAME(LVecBase3)
+  xform_vec_general(const FLOATNAME(LVecBase3) &v) const;
+
   // this = other1 * other2
   INLINE_LINMATH void multiply(const FLOATNAME(LMatrix4) &other1, const FLOATNAME(LMatrix4) &other2);
 

+ 3 - 4
panda/src/mathutil/plane_src.I

@@ -99,9 +99,8 @@ FLOATNAME(Plane)(FLOATTYPE a, FLOATTYPE b, FLOATTYPE c, FLOATTYPE d) :
 ////////////////////////////////////////////////////////////////////
 INLINE_MATHUTIL FLOATNAME(Plane) FLOATNAME(Plane)::
 operator * (const FLOATNAME(LMatrix3) &mat) const {
-  FLOATNAME(LVector3) new_normal = get_normal() * mat;
-  FLOATNAME(LPoint3) new_point = get_point() * mat;
-  return FLOATNAME(Plane)(new_normal, new_point);
+  FLOATNAME(LVector3) new_normal = mat.xform(get_normal());
+  return FLOATNAME(Plane)(new_normal, get_point());
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -111,7 +110,7 @@ operator * (const FLOATNAME(LMatrix3) &mat) const {
 ////////////////////////////////////////////////////////////////////
 INLINE_MATHUTIL FLOATNAME(Plane) FLOATNAME(Plane)::
 operator * (const FLOATNAME(LMatrix4) &mat) const {
-  FLOATNAME(LVector3) new_normal = get_normal() * mat;
+  FLOATNAME(LVector3) new_normal = mat.xform_vec_general(get_normal());
   FLOATNAME(LPoint3) new_point = get_point() * mat;
   return FLOATNAME(Plane)(new_normal, new_point);
 }

+ 3 - 0
panda/src/pgraph/cullPlanes.cxx

@@ -155,6 +155,9 @@ do_cull(int &result, CPT(RenderState) &state,
   Planes::const_iterator pi;
   for (pi = _planes.begin(); pi != _planes.end(); ++pi) {
     int plane_result = (*pi).second->contains(node_gbv);
+    pgraph_cat.debug()
+      << "comparing " << *(*pi).second << " to " << *node_gbv
+      << ": " << hex << plane_result << dec << "\n";
     if (plane_result == BoundingVolume::IF_no_intersection) {
       // The node is completely behind this clip plane.  Short-circuit
       // the rest of the logic; none of the other planes matter.