diff --git a/panda/src/mathutil/plane_src.I b/panda/src/mathutil/plane_src.I index 587b3183c6..c2b54c8ed7 100644 --- a/panda/src/mathutil/plane_src.I +++ b/panda/src/mathutil/plane_src.I @@ -25,10 +25,10 @@ //////////////////////////////////////////////////////////////////// INLINE_MATHUTIL FLOATNAME(Plane):: FLOATNAME(Plane)(void) { - _a = 0.0f; - _b = 0.0f; - _c = 1.0f; - _d = 0.0f; + _v.v._0 = 0.0f; + _v.v._1 = 0.0f; + _v.v._2 = 1.0f; + _v.v._3 = 0.0f; } //////////////////////////////////////////////////////////////////// @@ -38,10 +38,7 @@ FLOATNAME(Plane)(void) { //////////////////////////////////////////////////////////////////// INLINE_MATHUTIL FLOATNAME(Plane):: FLOATNAME(Plane)(const FLOATNAME(Plane) ©) : - _a(copy._a), - _b(copy._b), - _c(copy._c), - _d(copy._d) + FLOATNAME(LVecBase4)(copy) { } @@ -60,10 +57,10 @@ FLOATNAME(Plane)(const FLOATNAME(LPoint3) &a, const FLOATNAME(LPoint3) &b, FLOATNAME(LVector3) v = c - a; FLOATNAME(LVector3) p = normalize(cross(u, v)); - _a = p[0]; - _b = p[1]; - _c = p[2]; - _d = -dot(p, a); + _v.v._0 = p[0]; + _v.v._1 = p[1]; + _v.v._2 = p[2]; + _v.v._3 = -::dot(p, a); } //////////////////////////////////////////////////////////////////// @@ -77,10 +74,10 @@ FLOATNAME(Plane)(const FLOATNAME(LVector3) &normal, const FLOATNAME(LPoint3) &point) { FLOATNAME(LVector3) p = normalize(normal); - _a = p[0]; - _b = p[1]; - _c = p[2]; - _d = -dot(p, point); + _v.v._0 = p[0]; + _v.v._1 = p[1]; + _v.v._2 = p[2]; + _v.v._3 = -::dot(p, point); } //////////////////////////////////////////////////////////////////// @@ -91,27 +88,10 @@ FLOATNAME(Plane)(const FLOATNAME(LVector3) &normal, //////////////////////////////////////////////////////////////////// INLINE_MATHUTIL FLOATNAME(Plane):: FLOATNAME(Plane)(FLOATTYPE a, FLOATTYPE b, FLOATTYPE c, FLOATTYPE d) : - _a(a), - _b(b), - _c(c), - _d(d) + FLOATNAME(LVecBase4)(a, b, c, d) { } -//////////////////////////////////////////////////////////////////// -// Function: Plane::Operator = -// Access: Published -// Description: -//////////////////////////////////////////////////////////////////// -INLINE_MATHUTIL FLOATNAME(Plane)& FLOATNAME(Plane):: -operator = (const FLOATNAME(Plane)& p) { - _a = p._a; - _b = p._b; - _c = p._c; - _d = p._d; - return (*this); -} - //////////////////////////////////////////////////////////////////// // Function: Plane::Operator * LMatrix3 // Access: Published @@ -136,6 +116,16 @@ operator * (const FLOATNAME(LMatrix4) &mat) const { return FLOATNAME(Plane)(new_normal, new_point); } +//////////////////////////////////////////////////////////////////// +// Function: Plane::Unary - +// Access: Published +// Description: Returns the same plane facing the opposite direction. +//////////////////////////////////////////////////////////////////// +INLINE_MATHUTIL FLOATNAME(Plane) FLOATNAME(Plane):: +operator - () const { + return FLOATNAME(Plane)(-_v.v._0, -_v.v._1, -_v.v._2, -_v.v._3); +} + //////////////////////////////////////////////////////////////////// // Function: Plane::get_normal // Access: Published @@ -143,7 +133,7 @@ operator * (const FLOATNAME(LMatrix4) &mat) const { //////////////////////////////////////////////////////////////////// INLINE_MATHUTIL FLOATNAME(LVector3) FLOATNAME(Plane):: get_normal() const { - return FLOATNAME(LVector3)(_a, _b, _c); + return FLOATNAME(LVector3)(_v.v._0, _v.v._1, _v.v._2); } //////////////////////////////////////////////////////////////////// @@ -158,7 +148,7 @@ get_normal() const { //////////////////////////////////////////////////////////////////// INLINE_MATHUTIL FLOATTYPE FLOATNAME(Plane):: dist_to_plane(const FLOATNAME(LPoint3) &point) const { - return (_a * point[0] + _b * point[1] + _c * point[2] + _d); + return (_v.v._0 * point[0] + _v.v._1 * point[1] + _v.v._2 * point[2] + _v.v._3); } //////////////////////////////////////////////////////////////////// @@ -204,7 +194,7 @@ INLINE_MATHUTIL bool FLOATNAME(Plane):: intersects_line(FLOATTYPE &t, const FLOATNAME(LPoint3) &from, const FLOATNAME(LVector3) &delta) const { - FLOATTYPE denom = dot(get_normal(), delta); + FLOATTYPE denom = ::dot(get_normal(), delta); if (IS_NEARLY_ZERO(denom)) { return false; } @@ -213,86 +203,8 @@ intersects_line(FLOATTYPE &t, return true; } -//////////////////////////////////////////////////////////////////// -// Function: Plane::get_data -// Access: Published -// Description: Returns the address of the first of the four data -// elements in the plane equation. The remaining -// elements occupy the next positions consecutively in -// memory. -//////////////////////////////////////////////////////////////////// -INLINE_MATHUTIL const FLOATTYPE *FLOATNAME(Plane):: -get_data() const { - return &_a; -} - -//////////////////////////////////////////////////////////////////// -// Function: Plane::get_num_components -// Access: Published -// Description: Returns the number of elements in the plane equation, -// four. -//////////////////////////////////////////////////////////////////// -INLINE_MATHUTIL int FLOATNAME(Plane):: -get_num_components() const { - return 4; -} - -//////////////////////////////////////////////////////////////////// -// Function: Plane::output -// Access: Published -// Description: -//////////////////////////////////////////////////////////////////// -INLINE_MATHUTIL void FLOATNAME(Plane):: -output(ostream &out) const { - out << "Plane(" << _a << " " << _b << " " << _c << " " << _d << ")"; -} - -//////////////////////////////////////////////////////////////////// -// Function: Plane::write -// Access: Published -// Description: -//////////////////////////////////////////////////////////////////// -INLINE_MATHUTIL void FLOATNAME(Plane):: -write(ostream &out, int indent_level) const { - indent(out, indent_level) << *this << "\n"; -} - -//////////////////////////////////////////////////////////////////// -// Function: Plane::write_datagram -// Access: Public -// Description: -//////////////////////////////////////////////////////////////////// -INLINE_MATHUTIL void FLOATNAME(Plane):: -write_datagram(Datagram &dest) const { -#if FLOATTOKEN == 'f' - dest.add_float32(_a); - dest.add_float32(_b); - dest.add_float32(_c); - dest.add_float32(_d); -#else - dest.add_float64(_a); - dest.add_float64(_b); - dest.add_float64(_c); - dest.add_float64(_d); -#endif -} - -//////////////////////////////////////////////////////////////////// -// Function: Plane::read_datagram -// Access: Public -// Description: -//////////////////////////////////////////////////////////////////// -INLINE_MATHUTIL void FLOATNAME(Plane):: -read_datagram(DatagramIterator &source) { -#if FLOATTOKEN == 'f' - _a = source.get_float32(); - _b = source.get_float32(); - _c = source.get_float32(); - _d = source.get_float32(); -#else - _a = source.get_float64(); - _b = source.get_float64(); - _c = source.get_float64(); - _d = source.get_float64(); -#endif +INLINE_MATHUTIL ostream & +operator << (ostream &out, const FLOATNAME(Plane) &p) { + p.output(out); + return out; } diff --git a/panda/src/mathutil/plane_src.cxx b/panda/src/mathutil/plane_src.cxx index 8fe770e6e5..e9237fd862 100644 --- a/panda/src/mathutil/plane_src.cxx +++ b/panda/src/mathutil/plane_src.cxx @@ -26,10 +26,15 @@ //////////////////////////////////////////////////////////////////// FLOATNAME(LMatrix4) FLOATNAME(Plane):: get_reflection_mat(void) const { - FLOATTYPE aa = _a * _a; FLOATTYPE ab = _a * _b; FLOATTYPE ac = _a * _c; - FLOATTYPE ad = _a * _d; - FLOATTYPE bb = _b * _b; FLOATTYPE bc = _b * _c; FLOATTYPE bd = _b * _d; - FLOATTYPE cc = _c * _c; FLOATTYPE cd = _c * _d; + FLOATTYPE aa = _v.v._0 * _v.v._0; + FLOATTYPE ab = _v.v._0 * _v.v._1; + FLOATTYPE ac = _v.v._0 * _v.v._2; + FLOATTYPE ad = _v.v._0 * _v.v._3; + FLOATTYPE bb = _v.v._1 * _v.v._1; + FLOATTYPE bc = _v.v._1 * _v.v._2; + FLOATTYPE bd = _v.v._1 * _v.v._3; + FLOATTYPE cc = _v.v._2 * _v.v._2; + FLOATTYPE cd = _v.v._2 * _v.v._3; return FLOATNAME(LMatrix4)( 1-2*aa, -2*ab, -2*ac, 0, -2*ab, 1-2*bb, -2*bc, 0, @@ -47,15 +52,15 @@ get_reflection_mat(void) const { FLOATNAME(LPoint3) FLOATNAME(Plane):: get_point() const { // Choose the denominator based on the largest axis in the normal. - if (cabs(_a) >= cabs(_b) && cabs(_a) >= cabs(_c)) { - nassertr(_a != 0.0f, FLOATNAME(LPoint3)(0.0f, 0.0f, 0.0f)); - return FLOATNAME(LPoint3)(-_d / _a, 0.0f, 0.0f); - } else if (cabs(_b) >= cabs(_c)) { - nassertr(_b != 0.0f, FLOATNAME(LPoint3)(0.0f, 0.0f, 0.0f)); - return FLOATNAME(LPoint3)(0.0f, -_d / _b, 0.0f); + if (cabs(_v.v._0) >= cabs(_v.v._1) && cabs(_v.v._0) >= cabs(_v.v._2)) { + nassertr(_v.v._0 != 0.0f, FLOATNAME(LPoint3)(0.0f, 0.0f, 0.0f)); + return FLOATNAME(LPoint3)(-_v.v._3 / _v.v._0, 0.0f, 0.0f); + } else if (cabs(_v.v._1) >= cabs(_v.v._2)) { + nassertr(_v.v._1 != 0.0f, FLOATNAME(LPoint3)(0.0f, 0.0f, 0.0f)); + return FLOATNAME(LPoint3)(0.0f, -_v.v._3 / _v.v._1, 0.0f); } else { - nassertr(_c != 0.0f, FLOATNAME(LPoint3)(0.0f, 0.0f, 0.0f)); - return FLOATNAME(LPoint3)(0.0f, 0.0f, -_d / _c); + nassertr(_v.v._2 != 0.0f, FLOATNAME(LPoint3)(0.0f, 0.0f, 0.0f)); + return FLOATNAME(LPoint3)(0.0f, 0.0f, -_v.v._3 / _v.v._2); } } @@ -86,14 +91,36 @@ intersects_plane(FLOATNAME(LPoint3) &from, return false; } - FLOATTYPE n1n1 = dot(n1, n1); - FLOATTYPE n2n2 = dot(n2, n2); - FLOATTYPE n1n2 = dot(n1, n2); + FLOATTYPE n1n1 = ::dot(n1, n1); + FLOATTYPE n2n2 = ::dot(n2, n2); + FLOATTYPE n1n2 = ::dot(n1, n2); FLOATTYPE determinant_inv = 1.0f / (n1n1 * n2n2 - n1n2 * n1n2); - FLOATTYPE c1 = (other._d * n1n2 - _d * n2n2) * determinant_inv; - FLOATTYPE c2 = (_d * n1n2 - other._d * n1n1) * determinant_inv; + FLOATTYPE c1 = (other._v.v._3 * n1n2 - _v.v._3 * n2n2) * determinant_inv; + FLOATTYPE c2 = (_v.v._3 * n1n2 - other._v.v._3 * n1n1) * determinant_inv; from = n1 * c1 + n2 * c2; return true; } + +//////////////////////////////////////////////////////////////////// +// Function: Plane::output +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void FLOATNAME(Plane):: +output(ostream &out) const { + out << "Plane("; + FLOATNAME(LVecBase4)::output(out); + out << ")"; +} + +//////////////////////////////////////////////////////////////////// +// Function: Plane::write +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void FLOATNAME(Plane):: +write(ostream &out, int indent_level) const { + indent(out, indent_level) << *this << "\n"; +} diff --git a/panda/src/mathutil/plane_src.h b/panda/src/mathutil/plane_src.h index fbacf3ccd5..771f666c7f 100644 --- a/panda/src/mathutil/plane_src.h +++ b/panda/src/mathutil/plane_src.h @@ -21,7 +21,7 @@ // Description : An abstract mathematical description of a plane. A // plane is defined by the equation Ax + By + Cz + D = 0. //////////////////////////////////////////////////////////////////// -class EXPCL_PANDA FLOATNAME(Plane) { +class EXPCL_PANDA FLOATNAME(Plane) : public FLOATNAME(LVecBase4) { PUBLISHED: INLINE_MATHUTIL FLOATNAME(Plane)(void); INLINE_MATHUTIL FLOATNAME(Plane)(const FLOATNAME(Plane) ©); @@ -33,10 +33,9 @@ PUBLISHED: INLINE_MATHUTIL FLOATNAME(Plane)(FLOATTYPE a, FLOATTYPE b, FLOATTYPE c, FLOATTYPE d); - INLINE_MATHUTIL FLOATNAME(Plane)& operator = (const FLOATNAME(Plane)& copy); - INLINE_MATHUTIL FLOATNAME(Plane) operator * (const FLOATNAME(LMatrix3) &mat) const; INLINE_MATHUTIL FLOATNAME(Plane) operator * (const FLOATNAME(LMatrix4) &mat) const; + INLINE_MATHUTIL FLOATNAME(Plane) operator - () const; FLOATNAME(LMatrix4) get_reflection_mat(void) const; @@ -55,23 +54,11 @@ PUBLISHED: FLOATNAME(LVector3) &delta, const FLOATNAME(Plane) &other) const; - INLINE_MATHUTIL const FLOATTYPE *get_data() const; - INLINE_MATHUTIL int get_num_components() const; - - INLINE_MATHUTIL void output(ostream &out) const; - INLINE_MATHUTIL void write(ostream &out, int indent_level = 0) const; - -public: - INLINE_MATHUTIL void write_datagram(Datagram &dest) const; - INLINE_MATHUTIL void read_datagram(DatagramIterator &source); - -public: - FLOATTYPE _a, _b, _c, _d; + void output(ostream &out) const; + void write(ostream &out, int indent_level = 0) const; }; -INLINE_MATHUTIL ostream &operator << (ostream &out, const FLOATNAME(Plane) &p) { - p.output(out); - return out; -} +INLINE_MATHUTIL ostream & +operator << (ostream &out, const FLOATNAME(Plane) &p); #include "plane_src.I"