diff --git a/panda/src/linmath/lpoint2_src.I b/panda/src/linmath/lpoint2_src.I index 0cebd81634..d7281bc0af 100644 --- a/panda/src/linmath/lpoint2_src.I +++ b/panda/src/linmath/lpoint2_src.I @@ -185,6 +185,18 @@ operator / (FLOATTYPE scalar) const { return FLOATNAME(LPoint2)(FLOATNAME(LVecBase2)::operator / (scalar)); } +//////////////////////////////////////////////////////////////////// +// Function: LPoint2::project +// Access: Published +// Description: Returns a new vector representing the projection of +// this vector onto another one. The resulting vector +// will be a scalar multiple of onto. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LPoint2) FLOATNAME(LPoint2):: +project(const FLOATNAME(LVecBase2) &onto) const { + return FLOATNAME(LVecBase2)::project(onto); +} + #ifdef HAVE_PYTHON //////////////////////////////////////////////////////////////////// // Function: LPoint2::python_repr diff --git a/panda/src/linmath/lpoint2_src.h b/panda/src/linmath/lpoint2_src.h index 23b047f126..adbae3b91b 100644 --- a/panda/src/linmath/lpoint2_src.h +++ b/panda/src/linmath/lpoint2_src.h @@ -46,6 +46,7 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LPoint2) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LPoint2) operator / (FLOATTYPE scalar) const; + INLINE_LINMATH FLOATNAME(LPoint2) project(const FLOATNAME(LVecBase2) &onto) const; #ifdef HAVE_PYTHON INLINE_LINMATH void python_repr(ostream &out, const string &class_name) const; diff --git a/panda/src/linmath/lpoint3_src.I b/panda/src/linmath/lpoint3_src.I index 21a3620718..79fd6f9135 100644 --- a/panda/src/linmath/lpoint3_src.I +++ b/panda/src/linmath/lpoint3_src.I @@ -218,6 +218,18 @@ cross(const FLOATNAME(LVecBase3) &other) const { return FLOATNAME(LVecBase3)::cross(other); } +//////////////////////////////////////////////////////////////////// +// Function: LPoint3::project +// Access: Published +// Description: Returns a new vector representing the projection of +// this vector onto another one. The resulting vector +// will be a scalar multiple of onto. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LPoint3) FLOATNAME(LPoint3):: +project(const FLOATNAME(LVecBase3) &onto) const { + return FLOATNAME(LVecBase3)::project(onto); +} + //////////////////////////////////////////////////////////////////// // Function: LPoint3::operator * scalar // Access: Public diff --git a/panda/src/linmath/lpoint3_src.h b/panda/src/linmath/lpoint3_src.h index 12b3843da9..0d3524184f 100644 --- a/panda/src/linmath/lpoint3_src.h +++ b/panda/src/linmath/lpoint3_src.h @@ -55,6 +55,7 @@ PUBLISHED: operator - (const FLOATNAME(LVector3) &other) const; INLINE_LINMATH FLOATNAME(LPoint3) cross(const FLOATNAME(LVecBase3) &other) const; + INLINE_LINMATH FLOATNAME(LPoint3) project(const FLOATNAME(LVecBase3) &onto) const; INLINE_LINMATH FLOATNAME(LPoint3) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LPoint3) operator / (FLOATTYPE scalar) const; diff --git a/panda/src/linmath/lpoint4_src.I b/panda/src/linmath/lpoint4_src.I index f3422b9b89..6a2ec9c08e 100644 --- a/panda/src/linmath/lpoint4_src.I +++ b/panda/src/linmath/lpoint4_src.I @@ -205,6 +205,18 @@ operator / (FLOATTYPE scalar) const { return FLOATNAME(LPoint4)(FLOATNAME(LVecBase4)::operator / (scalar)); } +//////////////////////////////////////////////////////////////////// +// Function: LPoint4::project +// Access: Published +// Description: Returns a new vector representing the projection of +// this vector onto another one. The resulting vector +// will be a scalar multiple of onto. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LPoint4) FLOATNAME(LPoint4):: +project(const FLOATNAME(LVecBase4) &onto) const { + return FLOATNAME(LVecBase4)::project(onto); +} + #ifdef HAVE_PYTHON //////////////////////////////////////////////////////////////////// // Function: LPoint4::python_repr diff --git a/panda/src/linmath/lpoint4_src.h b/panda/src/linmath/lpoint4_src.h index 03cb7c62b2..7c6ceef906 100644 --- a/panda/src/linmath/lpoint4_src.h +++ b/panda/src/linmath/lpoint4_src.h @@ -47,6 +47,7 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LPoint4) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LPoint4) operator / (FLOATTYPE scalar) const; + INLINE_LINMATH FLOATNAME(LPoint4) project(const FLOATNAME(LVecBase4) &onto) const; #ifdef HAVE_PYTHON INLINE_LINMATH void python_repr(ostream &out, const string &class_name) const; diff --git a/panda/src/linmath/lvecBase2_src.I b/panda/src/linmath/lvecBase2_src.I index 39dbb98c1a..188aa90a75 100644 --- a/panda/src/linmath/lvecBase2_src.I +++ b/panda/src/linmath/lvecBase2_src.I @@ -364,6 +364,49 @@ set(FLOATTYPE x, FLOATTYPE y) { _v.v._1 = y; } +//////////////////////////////////////////////////////////////////// +// Function: LVecBase2::length +// Access: Public +// Description: Returns the length of the vector, by the Pythagorean +// theorem. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVecBase2):: +length() const { + return csqrt((*this).dot(*this)); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVecBase2::length_squared +// Access: Public +// Description: Returns the square of the vector's length, cheap and +// easy. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVecBase2):: +length_squared() const { + return (*this).dot(*this); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVecBase2::normalize +// Access: Public +// Description: Normalizes the vector in place. Returns true if the +// vector was normalized, false if it was a zero-length +// vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH bool FLOATNAME(LVecBase2):: +normalize() { + FLOATTYPE l2 = length_squared(); + if (l2 == (FLOATTYPE)0.0f) { + set(0.0f, 0.0f); + return false; + + } else if (!IS_THRESHOLD_EQUAL(l2, 1.0f, NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE))) { + (*this) /= csqrt(l2); + } + + return true; +} + //////////////////////////////////////////////////////////////////// // Function: LVecBase2::dot // Access: Public @@ -375,6 +418,18 @@ dot(const FLOATNAME(LVecBase2) &other) const { return _v.v._0 * other._v.v._0 + _v.v._1 * other._v.v._1; } +//////////////////////////////////////////////////////////////////// +// Function: LVecBase2::project +// Access: Published +// Description: Returns a new vector representing the projection of +// this vector onto another one. The resulting vector +// will be a scalar multiple of onto. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LVecBase2):: +project(const FLOATNAME(LVecBase2) &onto) const { + return onto * (dot(onto) / onto.length_squared()); +} + //////////////////////////////////////////////////////////////////// // Function: LVecBase2::operator < // Access: Public diff --git a/panda/src/linmath/lvecBase2_src.h b/panda/src/linmath/lvecBase2_src.h index a20817ef53..3eace11fb6 100644 --- a/panda/src/linmath/lvecBase2_src.h +++ b/panda/src/linmath/lvecBase2_src.h @@ -78,7 +78,12 @@ PUBLISHED: INLINE_LINMATH void fill(FLOATTYPE fill_value); INLINE_LINMATH void set(FLOATTYPE x, FLOATTYPE y); + INLINE_LINMATH FLOATTYPE length() const; + INLINE_LINMATH FLOATTYPE length_squared() const; + INLINE_LINMATH bool normalize(); + INLINE_LINMATH FLOATTYPE dot(const FLOATNAME(LVecBase2) &other) const; + INLINE_LINMATH FLOATNAME(LVecBase2) project(const FLOATNAME(LVecBase2) &onto) const; INLINE_LINMATH bool operator < (const FLOATNAME(LVecBase2) &other) const; INLINE_LINMATH bool operator == (const FLOATNAME(LVecBase2) &other) const; diff --git a/panda/src/linmath/lvecBase3_src.I b/panda/src/linmath/lvecBase3_src.I index 9f93979853..afae658e0e 100644 --- a/panda/src/linmath/lvecBase3_src.I +++ b/panda/src/linmath/lvecBase3_src.I @@ -511,6 +511,18 @@ cross(const FLOATNAME(LVecBase3) &other) const { _v.v._0 * other._v.v._1 - other._v.v._0 * _v.v._1); } +//////////////////////////////////////////////////////////////////// +// Function: LVecBase3::project +// Access: Published +// Description: Returns a new vector representing the projection of +// this vector onto another one. The resulting vector +// will be a scalar multiple of onto. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LVecBase3):: +project(const FLOATNAME(LVecBase3) &onto) const { + return onto * (dot(onto) / onto.length_squared()); +} + //////////////////////////////////////////////////////////////////// // Function: LVecBase3::operator < // Access: Published diff --git a/panda/src/linmath/lvecBase3_src.h b/panda/src/linmath/lvecBase3_src.h index 2b2fc570bf..d3aaed6d3b 100644 --- a/panda/src/linmath/lvecBase3_src.h +++ b/panda/src/linmath/lvecBase3_src.h @@ -89,6 +89,7 @@ PUBLISHED: INLINE_LINMATH FLOATTYPE dot(const FLOATNAME(LVecBase3) &other) const; INLINE_LINMATH FLOATNAME(LVecBase3) cross(const FLOATNAME(LVecBase3) &other) const; + INLINE_LINMATH FLOATNAME(LVecBase3) project(const FLOATNAME(LVecBase3) &onto) const; INLINE_LINMATH bool operator < (const FLOATNAME(LVecBase3) &other) const; INLINE_LINMATH bool operator == (const FLOATNAME(LVecBase3) &other) const; diff --git a/panda/src/linmath/lvecBase4_src.I b/panda/src/linmath/lvecBase4_src.I index 80d26e7516..b8d3e24e03 100644 --- a/panda/src/linmath/lvecBase4_src.I +++ b/panda/src/linmath/lvecBase4_src.I @@ -454,6 +454,49 @@ set(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z, FLOATTYPE w) { _v.v._3 = w; } +//////////////////////////////////////////////////////////////////// +// Function: LVecBase4::length +// Access: Public +// Description: Returns the length of the vector, by the Pythagorean +// theorem. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVecBase4):: +length() const { + return csqrt((*this).dot(*this)); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVecBase4::length_squared +// Access: Public +// Description: Returns the square of the vector's length, cheap and +// easy. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATTYPE FLOATNAME(LVecBase4):: +length_squared() const { + return (*this).dot(*this); +} + +//////////////////////////////////////////////////////////////////// +// Function: LVecBase4::normalize +// Access: Public +// Description: Normalizes the vector in place. Returns true if the +// vector was normalized, false if it was a zero-length +// vector. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH bool FLOATNAME(LVecBase4):: +normalize() { + FLOATTYPE l2 = length_squared(); + if (l2 == (FLOATTYPE)0.0f) { + set(0.0f, 0.0f, 0.0f, 0.0f); + return false; + + } else if (!IS_THRESHOLD_EQUAL(l2, 1.0f, NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE))) { + (*this) /= csqrt(l2); + } + + return true; +} + //////////////////////////////////////////////////////////////////// // Function: LVecBase4::dot // Access: Public @@ -467,6 +510,18 @@ dot(const FLOATNAME(LVecBase4) &other) const { _v.v._2 * other._v.v._2 + _v.v._3 * other._v.v._3; } +//////////////////////////////////////////////////////////////////// +// Function: LVecBase4::project +// Access: Published +// Description: Returns a new vector representing the projection of +// this vector onto another one. The resulting vector +// will be a scalar multiple of onto. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVecBase4) FLOATNAME(LVecBase4):: +project(const FLOATNAME(LVecBase4) &onto) const { + return onto * (dot(onto) / onto.length_squared()); +} + //////////////////////////////////////////////////////////////////// // Function: LVecBase4::operator < // Access: Public diff --git a/panda/src/linmath/lvecBase4_src.h b/panda/src/linmath/lvecBase4_src.h index 09d754de5a..fcec5484f3 100644 --- a/panda/src/linmath/lvecBase4_src.h +++ b/panda/src/linmath/lvecBase4_src.h @@ -83,7 +83,12 @@ PUBLISHED: INLINE_LINMATH void fill(FLOATTYPE fill_value); INLINE_LINMATH void set(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z, FLOATTYPE w); + INLINE_LINMATH FLOATTYPE length() const; + INLINE_LINMATH FLOATTYPE length_squared() const; + INLINE_LINMATH bool normalize(); + INLINE_LINMATH FLOATTYPE dot(const FLOATNAME(LVecBase4) &other) const; + INLINE_LINMATH FLOATNAME(LVecBase4) project(const FLOATNAME(LVecBase4) &onto) const; INLINE_LINMATH bool operator < (const FLOATNAME(LVecBase4) &other) const; INLINE_LINMATH bool operator == (const FLOATNAME(LVecBase4) &other) const; diff --git a/panda/src/linmath/lvector2_src.I b/panda/src/linmath/lvector2_src.I index 47193931ba..9b64bddb0c 100644 --- a/panda/src/linmath/lvector2_src.I +++ b/panda/src/linmath/lvector2_src.I @@ -154,49 +154,6 @@ operator - (const FLOATNAME(LVector2) &other) const { return FLOATNAME(LVecBase2)::operator - (other); } -//////////////////////////////////////////////////////////////////// -// Function: LVector2::length -// Access: Public -// Description: Returns the length of the vector, by the Pythagorean -// theorem. -//////////////////////////////////////////////////////////////////// -INLINE_LINMATH FLOATTYPE FLOATNAME(LVector2):: -length() const { - return csqrt((*this).dot(*this)); -} - -//////////////////////////////////////////////////////////////////// -// Function: LVector2::length_squared -// Access: Public -// Description: Returns the square of the vector's length, cheap and -// easy. -//////////////////////////////////////////////////////////////////// -INLINE_LINMATH FLOATTYPE FLOATNAME(LVector2):: -length_squared() const { - return (*this).dot(*this); -} - -//////////////////////////////////////////////////////////////////// -// Function: LVector2::normalize -// Access: Public -// Description: Normalizes the vector in place. Returns true if the -// vector was normalized, false if it was a zero-length -// vector. -//////////////////////////////////////////////////////////////////// -INLINE_LINMATH bool FLOATNAME(LVector2):: -normalize() { - FLOATTYPE l2 = length_squared(); - if (l2 == (FLOATTYPE)0.0f) { - set(0.0f, 0.0f); - return false; - - } else if (!IS_THRESHOLD_EQUAL(l2, 1.0f, NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE))) { - (*this) /= csqrt(l2); - } - - return true; -} - //////////////////////////////////////////////////////////////////// // Function: LVector2::operator * scalar // Access: Public @@ -218,6 +175,18 @@ operator / (FLOATTYPE scalar) const { return FLOATNAME(LVector2)(FLOATNAME(LVecBase2)::operator * (recip_scalar)); } +//////////////////////////////////////////////////////////////////// +// Function: LVector2::project +// Access: Published +// Description: Returns a new vector representing the projection of +// this vector onto another one. The resulting vector +// will be a scalar multiple of onto. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVector2) FLOATNAME(LVector2):: +project(const FLOATNAME(LVecBase2) &onto) const { + return FLOATNAME(LVecBase2)::project(onto); +} + //////////////////////////////////////////////////////////////////// // Function: LVector2::signed_angle_rad // Access: Published diff --git a/panda/src/linmath/lvector2_src.h b/panda/src/linmath/lvector2_src.h index 450390b61b..8f27a75011 100644 --- a/panda/src/linmath/lvector2_src.h +++ b/panda/src/linmath/lvector2_src.h @@ -37,12 +37,10 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LVecBase2) operator - (const FLOATNAME(LVecBase2) &other) const; INLINE_LINMATH FLOATNAME(LVector2) operator - (const FLOATNAME(LVector2) &other) const; - INLINE_LINMATH FLOATTYPE length() const; - INLINE_LINMATH FLOATTYPE length_squared() const; - INLINE_LINMATH bool normalize(); INLINE_LINMATH FLOATNAME(LVector2) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LVector2) operator / (FLOATTYPE scalar) const; + INLINE_LINMATH FLOATNAME(LVector2) project(const FLOATNAME(LVecBase2) &onto) const; INLINE_LINMATH FLOATTYPE signed_angle_rad(const FLOATNAME(LVector2) &other) const; INLINE_LINMATH FLOATTYPE signed_angle_deg(const FLOATNAME(LVector2) &other) const; diff --git a/panda/src/linmath/lvector3_src.I b/panda/src/linmath/lvector3_src.I index 4911f08c94..e3846153b7 100644 --- a/panda/src/linmath/lvector3_src.I +++ b/panda/src/linmath/lvector3_src.I @@ -208,6 +208,18 @@ cross(const FLOATNAME(LVecBase3) &other) const { return FLOATNAME(LVecBase3)::cross(other); } +//////////////////////////////////////////////////////////////////// +// Function: LVector3::project +// Access: Published +// Description: Returns a new vector representing the projection of +// this vector onto another one. The resulting vector +// will be a scalar multiple of onto. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: +project(const FLOATNAME(LVecBase3) &onto) const { + return FLOATNAME(LVecBase3)::project(onto); +} + //////////////////////////////////////////////////////////////////// // Function: LVector::angle_rad // Access: Published diff --git a/panda/src/linmath/lvector3_src.h b/panda/src/linmath/lvector3_src.h index b174b2a927..58e5c4ddf5 100644 --- a/panda/src/linmath/lvector3_src.h +++ b/panda/src/linmath/lvector3_src.h @@ -49,6 +49,7 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LVector3) operator - (const FLOATNAME(LVector3) &other) const; INLINE_LINMATH FLOATNAME(LVector3) cross(const FLOATNAME(LVecBase3) &other) const; + INLINE_LINMATH FLOATNAME(LVector3) project(const FLOATNAME(LVecBase3) &onto) const; INLINE_LINMATH FLOATTYPE angle_rad(const FLOATNAME(LVector3) &other) const; INLINE_LINMATH FLOATTYPE angle_deg(const FLOATNAME(LVector3) &other) const; diff --git a/panda/src/linmath/lvector4_src.I b/panda/src/linmath/lvector4_src.I index e2c57d13bf..752b7c33fa 100644 --- a/panda/src/linmath/lvector4_src.I +++ b/panda/src/linmath/lvector4_src.I @@ -175,49 +175,6 @@ operator - (const FLOATNAME(LVector4) &other) const { return FLOATNAME(LVecBase4)::operator - (other); } -//////////////////////////////////////////////////////////////////// -// Function: LVector4::length -// Access: Public -// Description: Returns the length of the vector, by the Pythagorean -// theorem. -//////////////////////////////////////////////////////////////////// -INLINE_LINMATH FLOATTYPE FLOATNAME(LVector4):: -length() const { - return csqrt((*this).dot(*this)); -} - -//////////////////////////////////////////////////////////////////// -// Function: LVector4::length_squared -// Access: Public -// Description: Returns the square of the vector's length, cheap and -// easy. -//////////////////////////////////////////////////////////////////// -INLINE_LINMATH FLOATTYPE FLOATNAME(LVector4):: -length_squared() const { - return (*this).dot(*this); -} - -//////////////////////////////////////////////////////////////////// -// Function: LVector4::normalize -// Access: Public -// Description: Normalizes the vector in place. Returns true if the -// vector was normalized, false if it was a zero-length -// vector. -//////////////////////////////////////////////////////////////////// -INLINE_LINMATH bool FLOATNAME(LVector4):: -normalize() { - FLOATTYPE l2 = length_squared(); - if (l2 == (FLOATTYPE)0.0f) { - set(0.0f, 0.0f, 0.0f, 0.0f); - return false; - - } else if (!IS_THRESHOLD_EQUAL(l2, 1.0f, NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE))) { - (*this) /= csqrt(l2); - } - - return true; -} - //////////////////////////////////////////////////////////////////// // Function: LVector4::operator * scalar // Access: Public @@ -239,6 +196,18 @@ operator / (FLOATTYPE scalar) const { return FLOATNAME(LVector4)(FLOATNAME(LVecBase4)::operator * (recip_scalar)); } +//////////////////////////////////////////////////////////////////// +// Function: LVector4::project +// Access: Published +// Description: Returns a new vector representing the projection of +// this vector onto another one. The resulting vector +// will be a scalar multiple of onto. +//////////////////////////////////////////////////////////////////// +INLINE_LINMATH FLOATNAME(LVector4) FLOATNAME(LVector4):: +project(const FLOATNAME(LVecBase4) &onto) const { + return FLOATNAME(LVecBase4)::project(onto); +} + #ifdef HAVE_PYTHON //////////////////////////////////////////////////////////////////// // Function: LVector4::python_repr diff --git a/panda/src/linmath/lvector4_src.h b/panda/src/linmath/lvector4_src.h index 886390b0f4..9db8c0071f 100644 --- a/panda/src/linmath/lvector4_src.h +++ b/panda/src/linmath/lvector4_src.h @@ -39,12 +39,11 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LVecBase4) operator - (const FLOATNAME(LVecBase4) &other) const; INLINE_LINMATH FLOATNAME(LVector4) operator - (const FLOATNAME(LVector4) &other) const; - INLINE_LINMATH FLOATTYPE length() const; - INLINE_LINMATH FLOATTYPE length_squared() const; - INLINE_LINMATH bool normalize(); INLINE_LINMATH FLOATNAME(LVector4) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LVector4) operator / (FLOATTYPE scalar) const; + INLINE_LINMATH FLOATNAME(LVector4) project(const FLOATNAME(LVecBase4) &onto) const; + #ifdef HAVE_PYTHON INLINE_LINMATH void python_repr(ostream &out, const string &class_name) const; #endif