From 812356e83d0fde05e03e5b31ba070a1065c5dce7 Mon Sep 17 00:00:00 2001 From: David Rose Date: Sat, 14 Mar 2009 20:42:24 +0000 Subject: [PATCH] deprecate relative_angle_deg, add signed_angle_deg --- panda/src/linmath/lpoint3.h | 1 + panda/src/linmath/lpoint3_src.I | 33 ++++++++ panda/src/linmath/lpoint3_src.h | 4 + panda/src/linmath/lvecBase2.h | 2 + panda/src/linmath/lvecBase3.h | 1 + panda/src/linmath/lvecBase3_src.I | 33 ++++++++ panda/src/linmath/lvecBase3_src.h | 4 + panda/src/linmath/lvector2_src.I | 23 ++++++ panda/src/linmath/lvector2_src.h | 3 + panda/src/linmath/lvector3.h | 1 + panda/src/linmath/lvector3_src.I | 120 +++++++++++++++++++++++------- panda/src/linmath/lvector3_src.h | 9 +++ 12 files changed, 208 insertions(+), 26 deletions(-) diff --git a/panda/src/linmath/lpoint3.h b/panda/src/linmath/lpoint3.h index 40866874b9..06d90128c4 100644 --- a/panda/src/linmath/lpoint3.h +++ b/panda/src/linmath/lpoint3.h @@ -20,6 +20,7 @@ #include "coordinateSystem.h" #include "lvecBase3.h" #include "lvector3.h" +#include "lpoint2.h" #include "fltnames.h" #include "lpoint3_src.h" diff --git a/panda/src/linmath/lpoint3_src.I b/panda/src/linmath/lpoint3_src.I index 7e5438d1e4..21a3620718 100644 --- a/panda/src/linmath/lpoint3_src.I +++ b/panda/src/linmath/lpoint3_src.I @@ -115,6 +115,39 @@ unit_z() { return (const FLOATNAME(LPoint3) &)FLOATNAME(LVecBase3)::unit_z(); } +//////////////////////////////////////////////////////////////////// +// Function: LPoint3::get_xy +// Access: Public +// Description: Returns a 2-component vector that shares just the +// first two components of this vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LPoint2) FLOATNAME(LPoint3):: +get_xy() const { + return FLOATNAME(LPoint2)(_v.v._0, _v.v._1); +} + +//////////////////////////////////////////////////////////////////// +// Function: LPoint3::get_xz +// Access: Public +// Description: Returns a 2-component vector that shares just the +// first and last components of this vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LPoint2) FLOATNAME(LPoint3):: +get_xz() const { + return FLOATNAME(LPoint2)(_v.v._0, _v.v._2); +} + +//////////////////////////////////////////////////////////////////// +// Function: LPoint3::get_yz +// Access: Public +// Description: Returns a 2-component vector that shares just the +// last two components of this vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LPoint2) FLOATNAME(LPoint3):: +get_yz() const { + return FLOATNAME(LPoint2)(_v.v._1, _v.v._2); +} + //////////////////////////////////////////////////////////////////// // Function: LPoint3::unary - // Access: Public diff --git a/panda/src/linmath/lpoint3_src.h b/panda/src/linmath/lpoint3_src.h index c9d5666a2b..12b3843da9 100644 --- a/panda/src/linmath/lpoint3_src.h +++ b/panda/src/linmath/lpoint3_src.h @@ -36,6 +36,10 @@ PUBLISHED: INLINE_LINMATH static const FLOATNAME(LPoint3) &unit_y(); INLINE_LINMATH static const FLOATNAME(LPoint3) &unit_z(); + INLINE_LINMATH FLOATNAME(LPoint2) get_xy() const; + INLINE_LINMATH FLOATNAME(LPoint2) get_xz() const; + INLINE_LINMATH FLOATNAME(LPoint2) get_yz() const; + INLINE_LINMATH FLOATNAME(LPoint3) operator - () const; INLINE_LINMATH FLOATNAME(LVecBase3) diff --git a/panda/src/linmath/lvecBase2.h b/panda/src/linmath/lvecBase2.h index c36b6d050b..5477236d8c 100644 --- a/panda/src/linmath/lvecBase2.h +++ b/panda/src/linmath/lvecBase2.h @@ -21,6 +21,8 @@ #include "datagram.h" #include "datagramIterator.h" #include "checksumHashGenerator.h" +#include "mathNumbers.h" +#include "deg_2_rad.h" #include "cmath.h" #include "nearly_zero.h" diff --git a/panda/src/linmath/lvecBase3.h b/panda/src/linmath/lvecBase3.h index 2b1b27971a..85d2b081c8 100644 --- a/panda/src/linmath/lvecBase3.h +++ b/panda/src/linmath/lvecBase3.h @@ -23,6 +23,7 @@ #include "checksumHashGenerator.h" #include "mathNumbers.h" #include "deg_2_rad.h" +#include "lvecBase2.h" #include "cmath.h" #include "nearly_zero.h" diff --git a/panda/src/linmath/lvecBase3_src.I b/panda/src/linmath/lvecBase3_src.I index 791cbc83a7..9f93979853 100644 --- a/panda/src/linmath/lvecBase3_src.I +++ b/panda/src/linmath/lvecBase3_src.I @@ -273,6 +273,39 @@ set_z(FLOATTYPE value) { _v.v._2 = value; } +//////////////////////////////////////////////////////////////////// +// Function: LVecBase3::get_xy +// Access: Public +// Description: Returns a 2-component vector that shares just the +// first two components of this vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LVecBase3):: +get_xy() const { + return FLOATNAME(LVecBase2)(_v.v._0, _v.v._1); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVecBase3::get_xz +// Access: Public +// Description: Returns a 2-component vector that shares just the +// first and last components of this vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LVecBase3):: +get_xz() const { + return FLOATNAME(LVecBase2)(_v.v._0, _v.v._2); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVecBase3::get_yz +// Access: Public +// Description: Returns a 2-component vector that shares just the +// last two components of this vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LVecBase3):: +get_yz() const { + return FLOATNAME(LVecBase2)(_v.v._1, _v.v._2); +} + //////////////////////////////////////////////////////////////////// // Function: LVecBase3::add_to_cell // Access: Public diff --git a/panda/src/linmath/lvecBase3_src.h b/panda/src/linmath/lvecBase3_src.h index 5199144f35..2b2fc570bf 100644 --- a/panda/src/linmath/lvecBase3_src.h +++ b/panda/src/linmath/lvecBase3_src.h @@ -56,6 +56,10 @@ PUBLISHED: INLINE_LINMATH void set_y(FLOATTYPE value); INLINE_LINMATH void set_z(FLOATTYPE value); + INLINE_LINMATH FLOATNAME(LVecBase2) get_xy() const; + INLINE_LINMATH FLOATNAME(LVecBase2) get_xz() const; + INLINE_LINMATH FLOATNAME(LVecBase2) get_yz() const; + // These next functions add to an existing value. // i.e. foo.set_x(foo.get_x() + value) // These are useful to reduce overhead in scripting diff --git a/panda/src/linmath/lvector2_src.I b/panda/src/linmath/lvector2_src.I index e7714c1a77..47193931ba 100644 --- a/panda/src/linmath/lvector2_src.I +++ b/panda/src/linmath/lvector2_src.I @@ -218,6 +218,29 @@ operator / (FLOATTYPE scalar) const { return FLOATNAME(LVector2)(FLOATNAME(LVecBase2)::operator * (recip_scalar)); } +//////////////////////////////////////////////////////////////////// +// Function: LVector2::signed_angle_rad +// Access: Published +// Description: returns the signed angled between two vectors. +// normalization is NOT necessary +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVector2):: +signed_angle_rad(const FLOATNAME(LVector2) &other) const { + return catan2((_v.v._0*other._v.v._1)-(_v.v._1*other._v.v._0), dot(other)); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVector2::signed_angle_deg +// Access: Published +// Description: returns the signed angled between two vectors. +// normalization is NOT necessary +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVector2):: +signed_angle_deg(const FLOATNAME(LVector2) &other) const { + return rad_2_deg(signed_angle_rad(other)); +} + + #ifdef HAVE_PYTHON //////////////////////////////////////////////////////////////////// // Function: LVector2::python_repr diff --git a/panda/src/linmath/lvector2_src.h b/panda/src/linmath/lvector2_src.h index 8bb3fabfca..450390b61b 100644 --- a/panda/src/linmath/lvector2_src.h +++ b/panda/src/linmath/lvector2_src.h @@ -43,6 +43,9 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LVector2) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LVector2) operator / (FLOATTYPE scalar) const; + INLINE_LINMATH FLOATTYPE signed_angle_rad(const FLOATNAME(LVector2) &other) const; + INLINE_LINMATH FLOATTYPE signed_angle_deg(const FLOATNAME(LVector2) &other) const; + #ifdef HAVE_PYTHON INLINE_LINMATH void python_repr(ostream &out, const string &class_name) const; #endif diff --git a/panda/src/linmath/lvector3.h b/panda/src/linmath/lvector3.h index 5e4a8301ed..7750cd159f 100644 --- a/panda/src/linmath/lvector3.h +++ b/panda/src/linmath/lvector3.h @@ -21,6 +21,7 @@ #include "cmath.h" #include "config_linmath.h" #include "lvecBase3.h" +#include "lvector2.h" #include "fltnames.h" #include "lvector3_src.h" diff --git a/panda/src/linmath/lvector3_src.I b/panda/src/linmath/lvector3_src.I index 100bee206f..4911f08c94 100644 --- a/panda/src/linmath/lvector3_src.I +++ b/panda/src/linmath/lvector3_src.I @@ -115,6 +115,39 @@ unit_z() { return (const FLOATNAME(LVector3) &)FLOATNAME(LVecBase3)::unit_z(); } +//////////////////////////////////////////////////////////////////// +// Function: LVector3::get_xy +// Access: Public +// Description: Returns a 2-component vector that shares just the +// first two components of this vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVector2) FLOATNAME(LVector3):: +get_xy() const { + return FLOATNAME(LVector2)(_v.v._0, _v.v._1); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVector3::get_xz +// Access: Public +// Description: Returns a 2-component vector that shares just the +// first and last components of this vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVector2) FLOATNAME(LVector3):: +get_xz() const { + return FLOATNAME(LVector2)(_v.v._0, _v.v._2); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVector3::get_yz +// Access: Public +// Description: Returns a 2-component vector that shares just the +// last two components of this vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVector2) FLOATNAME(LVector3):: +get_yz() const { + return FLOATNAME(LVector2)(_v.v._1, _v.v._2); +} + //////////////////////////////////////////////////////////////////// // Function: LVector3::unary - // Access: Published @@ -178,9 +211,9 @@ cross(const FLOATNAME(LVecBase3) &other) const { //////////////////////////////////////////////////////////////////// // Function: LVector::angle_rad // Access: Published -// Description: Returns the angle between this vector and the other -// one, expressed in radians. Both vectors should be -// initially normalized. +// Description: Returns the unsigned angle between this vector and +// the other one, expressed in radians. Both vectors +// should be initially normalized. //////////////////////////////////////////////////////////////////// INLINE_LINMATH FLOATTYPE FLOATNAME(LVector3):: angle_rad(const FLOATNAME(LVector3) &other) const { @@ -195,29 +228,6 @@ angle_rad(const FLOATNAME(LVector3) &other) const { } } -//////////////////////////////////////////////////////////////////// -// Function: LVector::relative_angle_rad -// Access: Published -// Description: returns the signed angled between two vectors. -// normalization is NOT necessary -//////////////////////////////////////////////////////////////////// -INLINE_LINMATH FLOATTYPE FLOATNAME(LVector3):: -relative_angle_rad(const FLOATNAME(LVector3) &other) const { - return atan2((_v.v._0*other._v.v._1)-(_v.v._1*other._v.v._0), dot(other)); -} - -//////////////////////////////////////////////////////////////////// -// Function: LVector::relative_angle_deg -// Access: Published -// Description: returns the signed angled between two vectors. -// normalization is NOT necessary -//////////////////////////////////////////////////////////////////// -INLINE_LINMATH FLOATTYPE FLOATNAME(LVector3):: -relative_angle_deg(const FLOATNAME(LVector3) &other) const { - return relative_angle_rad(other)*180/3.1415926535; -} - - //////////////////////////////////////////////////////////////////// // Function: LVector::angle_deg // Access: Published @@ -230,6 +240,64 @@ angle_deg(const FLOATNAME(LVector3) &other) const { return rad_2_deg(angle_rad(other)); } +//////////////////////////////////////////////////////////////////// +// Function: LVector::signed_angle_rad +// Access: Published +// Description: returns the signed angle between two vectors. +// The angle is positive if the rotation from this +// vector to other is clockwise when looking in the +// direction of the ref vector. +// +// Vectors (except the ref vector) should be initially +// normalized. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVector3):: +signed_angle_rad(const FLOATNAME(LVector3) &other, + const FLOATNAME(LVector3) &ref) const { + FLOATTYPE angle = angle_rad(other); + if (cross(other).dot(ref) < 0.0f) { + angle = -angle; + } + return angle; +} + +//////////////////////////////////////////////////////////////////// +// Function: LVector::signed_angle_deg +// Access: Published +// Description: Returns the signed angle between two vectors. +// The angle is positive if the rotation from this +// vector to other is clockwise when looking in the +// direction of the ref vector. +// +// Vectors (except the ref vector) should be initially +// normalized. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVector3):: +signed_angle_deg(const FLOATNAME(LVector3) &other, + const FLOATNAME(LVector3) &ref) const { + return rad_2_deg(signed_angle_rad(other, ref)); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVector::relative_angle_rad +// Access: Published +// Description: This method is deprecated. Do not use. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVector3):: +relative_angle_rad(const FLOATNAME(LVector3) &other) const { + return atan2((_v.v._0*other._v.v._1)-(_v.v._1*other._v.v._0), dot(other)); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVector::relative_angle_deg +// Access: Published +// Description: This method is deprecated. Do not use. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVector3):: +relative_angle_deg(const FLOATNAME(LVector3) &other) const { + return relative_angle_rad(other)*180/3.1415926535; +} + //////////////////////////////////////////////////////////////////// // Function: LVector3::operator * scalar // Access: Published diff --git a/panda/src/linmath/lvector3_src.h b/panda/src/linmath/lvector3_src.h index 8457a5939a..b174b2a927 100644 --- a/panda/src/linmath/lvector3_src.h +++ b/panda/src/linmath/lvector3_src.h @@ -36,6 +36,10 @@ PUBLISHED: INLINE_LINMATH static const FLOATNAME(LVector3) &unit_y(); INLINE_LINMATH static const FLOATNAME(LVector3) &unit_z(); + INLINE_LINMATH FLOATNAME(LVector2) get_xy() const; + INLINE_LINMATH FLOATNAME(LVector2) get_xz() const; + INLINE_LINMATH FLOATNAME(LVector2) get_yz() const; + INLINE_LINMATH FLOATNAME(LVector3) operator - () const; INLINE_LINMATH FLOATNAME(LVecBase3) operator + (const FLOATNAME(LVecBase3) &other) const; @@ -48,6 +52,11 @@ PUBLISHED: INLINE_LINMATH FLOATTYPE angle_rad(const FLOATNAME(LVector3) &other) const; INLINE_LINMATH FLOATTYPE angle_deg(const FLOATNAME(LVector3) &other) const; + INLINE_LINMATH FLOATTYPE signed_angle_rad(const FLOATNAME(LVector3) &other, + const FLOATNAME(LVector3) &ref) const; + INLINE_LINMATH FLOATTYPE signed_angle_deg(const FLOATNAME(LVector3) &other, + const FLOATNAME(LVector3) &ref) const; + INLINE_LINMATH FLOATTYPE relative_angle_rad(const FLOATNAME(LVector3) &other) const; INLINE_LINMATH FLOATTYPE relative_angle_deg(const FLOATNAME(LVector3) &other) const;