12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #ifndef _MPOINT3_H_
- #define _MPOINT3_H_
- #ifndef _MMATHFN_H_
- #include "math/mMathFn.h"
- #endif
- #ifndef _MPOINT2_H_
- #include "math/mPoint2.h"
- #endif
- //------------------------------------------------------------------------------
- /// 3D integer point
- ///
- /// Uses S32 internally.
- class Point3I
- {
- //-------------------------------------- Public data
- public:
- S32 x; ///< X co-ordinate
- S32 y; ///< Y co-ordinate
- S32 z; ///< Z co-ordinate
- //-------------------------------------- Public interface
- public:
- Point3I(); ///< Create an uninitialized point.
- Point3I(const Point3I&); ///< Copy constructor.
- explicit Point3I(S32 xyz); ///< Initializes all elements to the same value.
- Point3I(S32 in_x, S32 in_y, S32 in_z); ///< Create a point from co-ordinates.
- //-------------------------------------- Non-math mutators and misc functions
- void set(S32 xyz); ///< Initializes all elements to the same value.
- void set(S32 in_x, S32 in_y, S32 in_z); ///< Set co-ordinates.
- void setMin(const Point3I&); ///< Store lesser co-ordinates in this point.
- void setMax(const Point3I&); ///< Store greater co-ordinates in this point.
- void zero(); ///< Zero all values
- //-------------------------------------- Math mutators
- void neg(); ///< Invert co-ordinate's signs.
- void convolve(const Point3I&); ///< Convolve by parameter.
- //-------------------------------------- Queries
- bool isZero() const; ///< Check for point at origin. (No epsilon.)
- F32 len() const; ///< Get length.
- //-------------------------------------- Overloaded operators
- public:
- operator S32*() { return &x; }
- operator const S32*() const { return &x; }
- // Comparison operators
- bool operator==(const Point3I&) const;
- bool operator!=(const Point3I&) const;
- // Arithmetic w/ other points
- Point3I operator+(const Point3I&) const;
- Point3I operator-(const Point3I&) const;
- Point3I& operator+=(const Point3I&);
- Point3I& operator-=(const Point3I&);
- // Arithmetic w/ scalars
- Point3I operator*(S32) const;
- Point3I& operator*=(S32);
- Point3I operator/(S32) const;
- Point3I& operator/=(S32);
- // Unary operators
- Point3I operator-() const;
- //-------------------------------------- Public static constants
- public:
- const static Point3I One;
- const static Point3I Zero;
- };
- class Point3D;
- //------------------------------------------------------------------------------
- class Point3F
- {
- //-------------------------------------- Public data
- public:
- F32 x;
- F32 y;
- F32 z;
- public:
- Point3F();
- Point3F(const Point3F&);
- Point3F(F32 _x, F32 _y, F32 _z);
- explicit Point3F(F32 xyz);
- //-------------------------------------- Non-math mutators and misc functions
- public:
- void set(F32 xyz);
- void set(F32 _x, F32 _y, F32 _z);
- void set(const Point3F&);
- void setMin(const Point3F&);
- void setMax(const Point3F&);
- void interpolate(const Point3F&, const Point3F&, F32);
- void zero();
- /// Returns the smallest absolute value.
- F32 least() const;
- /// Returns the greatest absolute value.
- F32 most() const;
- operator F32*() { return &x; }
- operator const F32*() const { return &x; }
- /// Returns the x and y coords as a Point2F.
- Point2F asPoint2F() const { return Point2F( x, y ); }
- //-------------------------------------- Queries
- public:
- bool isZero() const;
- bool isUnitLength() const;
- F32 len() const;
- F32 lenSquared() const;
- F32 magnitudeSafe() const;
- bool equal( const Point3F &compare, F32 epsilon = POINT_EPSILON ) const;
- U32 getLeastComponentIndex() const;
- U32 getGreatestComponentIndex() const;
- //-------------------------------------- Mathematical mutators
- public:
- void neg();
- void normalize();
- void normalizeSafe();
- void normalize(F32 val);
- void convolve(const Point3F&);
- void convolveInverse(const Point3F&);
- //-------------------------------------- Overloaded operators
- public:
- // Comparison operators
- bool operator==(const Point3F&) const;
- bool operator!=(const Point3F&) const;
- // Arithmetic w/ other points
- Point3F operator+(const Point3F&) const;
- Point3F operator-(const Point3F&) const;
- Point3F& operator+=(const Point3F&);
- Point3F& operator-=(const Point3F&);
- // Arithmetic w/ scalars
- Point3F operator*(F32) const;
- Point3F operator/(F32) const;
- Point3F& operator*=(F32);
- Point3F& operator/=(F32);
- Point3F operator*(const Point3F&) const;
- Point3F& operator*=(const Point3F&);
- Point3F operator/(const Point3F&) const;
- Point3F& operator/=(const Point3F&);
- // Unary operators
- Point3F operator-() const;
- Point3F& operator=(const Point3D&);
- //-------------------------------------- Public static constants
- public:
- const static Point3F One;
- const static Point3F Zero;
- const static Point3F Max;
- const static Point3F Min;
- const static Point3F UnitX;
- const static Point3F UnitY;
- const static Point3F UnitZ;
- };
- typedef Point3F VectorF;
- typedef Point3F EulerF;
- //------------------------------------------------------------------------------
- class Point3D
- {
- //-------------------------------------- Public data
- public:
- F64 x;
- F64 y;
- F64 z;
- public:
- Point3D();
- Point3D(const Point3D&);
- Point3D(const Point3F&);
- explicit Point3D(F64 xyz);
- Point3D(F64 _x, F64 _y, F64 _z);
- //-------------------------------------- Non-math mutators and misc functions
- public:
- void set(F64 xyz);
- void set(F64 _x, F64 _y, F64 _z);
- void setMin(const Point3D&);
- void setMax(const Point3D&);
- void interpolate(const Point3D&, const Point3D&, F64);
- void zero();
- operator F64*() { return (&x); }
- operator const F64*() const { return &x; }
- //-------------------------------------- Queries
- public:
- bool isZero() const;
- F64 len() const;
- F64 lenSquared() const;
- F64 magnitudeSafe() const;
- //-------------------------------------- Mathematical mutators
- public:
- void neg();
- void normalize();
- void normalizeSafe();
- void normalize(F64 val);
- void convolve(const Point3D&);
- void convolveInverse(const Point3D&);
- //-------------------------------------- Overloaded operators
- public:
- Point3F toPoint3F() const;
- // Comparison operators
- bool operator==(const Point3D&) const;
- bool operator!=(const Point3D&) const;
- // Arithmetic w/ other points
- Point3D operator+(const Point3D&) const;
- Point3D operator-(const Point3D&) const;
- Point3D& operator+=(const Point3D&);
- Point3D& operator-=(const Point3D&);
- // Arithmetic w/ scalars
- Point3D operator*(F64) const;
- Point3D operator/(F64) const;
- Point3D& operator*=(F64);
- Point3D& operator/=(F64);
- // Unary operators
- Point3D operator-() const;
- //-------------------------------------- Public static constants
- public:
- const static Point3D One;
- const static Point3D Zero;
- };
- //------------------------------------------------------------------------------
- //-------------------------------------- Point3I
- //
- inline Point3I::Point3I()
- : x(0), y(0), z(0)
- {
- }
- inline Point3I::Point3I(const Point3I& _copy)
- : x(_copy.x), y(_copy.y), z(_copy.z)
- {
- //
- }
- inline Point3I::Point3I(S32 xyz)
- : x(xyz), y(xyz), z(xyz)
- {
- //
- }
- inline Point3I::Point3I(S32 _x, S32 _y, S32 _z)
- : x(_x), y(_y), z(_z)
- {
- //
- }
- inline void Point3I::set(S32 xyz)
- {
- x = y = z = xyz;
- }
- inline void Point3I::set(S32 _x, S32 _y, S32 _z)
- {
- x = _x;
- y = _y;
- z = _z;
- }
- inline void Point3I::setMin(const Point3I& _test)
- {
- x = (_test.x < x) ? _test.x : x;
- y = (_test.y < y) ? _test.y : y;
- z = (_test.z < z) ? _test.z : z;
- }
- inline void Point3I::setMax(const Point3I& _test)
- {
- x = (_test.x > x) ? _test.x : x;
- y = (_test.y > y) ? _test.y : y;
- z = (_test.z > z) ? _test.z : z;
- }
- inline void Point3I::zero()
- {
- x = y = z = 0;
- }
- inline void Point3I::neg()
- {
- x = -x;
- y = -y;
- z = -z;
- }
- inline F32 Point3I::len() const
- {
- return mSqrt(F32(x*x + y*y + z*z));
- }
- inline void Point3I::convolve(const Point3I& c)
- {
- x *= c.x;
- y *= c.y;
- z *= c.z;
- }
- inline bool Point3I::isZero() const
- {
- return ((x == 0) && (y == 0) && (z == 0));
- }
- inline bool Point3I::operator==(const Point3I& _test) const
- {
- return ((x == _test.x) && (y == _test.y) && (z == _test.z));
- }
- inline bool Point3I::operator!=(const Point3I& _test) const
- {
- return (operator==(_test) == false);
- }
- inline Point3I Point3I::operator+(const Point3I& _add) const
- {
- return Point3I(x + _add.x, y + _add.y, z + _add.z);
- }
- inline Point3I Point3I::operator-(const Point3I& _rSub) const
- {
- return Point3I(x - _rSub.x, y - _rSub.y, z - _rSub.z);
- }
- inline Point3I& Point3I::operator+=(const Point3I& _add)
- {
- x += _add.x;
- y += _add.y;
- z += _add.z;
- return *this;
- }
- inline Point3I& Point3I::operator-=(const Point3I& _rSub)
- {
- x -= _rSub.x;
- y -= _rSub.y;
- z -= _rSub.z;
- return *this;
- }
- inline Point3I Point3I::operator-() const
- {
- return Point3I(-x, -y, -z);
- }
- inline Point3I Point3I::operator*(S32 mul) const
- {
- return Point3I(x * mul, y * mul, z * mul);
- }
- inline Point3I Point3I::operator/(S32 div) const
- {
- AssertFatal(div != 0, "Error, div by zero attempted");
- return Point3I(x/div, y/div, z/div);
- }
- inline Point3I& Point3I::operator*=(S32 mul)
- {
- x *= mul;
- y *= mul;
- z *= mul;
- return *this;
- }
- inline Point3I& Point3I::operator/=(S32 div)
- {
- AssertFatal(div != 0, "Error, div by zero attempted");
- x /= div;
- y /= div;
- z /= div;
- return *this;
- }
- //------------------------------------------------------------------------------
- //-------------------------------------- Point3F
- //
- inline Point3F::Point3F()
- : x(0.0f), y(0.0f), z(0.0f)
- {
- // Uninitialized points are definitely a problem.
- // Enable the following code to see how often they crop up.
- #ifdef DEBUG_MATH
- *(U32 *)&x = 0x7FFFFFFA;
- *(U32 *)&y = 0x7FFFFFFB;
- *(U32 *)&z = 0x7FFFFFFC;
- #endif
- }
- inline Point3F::Point3F(const Point3F& _copy)
- : x(_copy.x), y(_copy.y), z(_copy.z)
- {
- //
- }
- inline Point3F::Point3F(F32 _x, F32 _y, F32 _z)
- : x(_x), y(_y), z(_z)
- {
- //
- }
- inline Point3F::Point3F(F32 xyz)
- : x(xyz), y(xyz), z(xyz)
- {
- //
- }
- inline void Point3F::set(F32 xyz)
- {
- x = y = z = xyz;
- }
- inline void Point3F::set(F32 _x, F32 _y, F32 _z)
- {
- x = _x;
- y = _y;
- z = _z;
- }
- inline void Point3F::set(const Point3F& copy)
- {
- x = copy.x;
- y = copy.y;
- z = copy.z;
- }
- inline void Point3F::setMin(const Point3F& _test)
- {
- x = (_test.x < x) ? _test.x : x;
- y = (_test.y < y) ? _test.y : y;
- z = (_test.z < z) ? _test.z : z;
- }
- inline void Point3F::setMax(const Point3F& _test)
- {
- x = (_test.x > x) ? _test.x : x;
- y = (_test.y > y) ? _test.y : y;
- z = (_test.z > z) ? _test.z : z;
- }
- inline void Point3F::interpolate(const Point3F& _from, const Point3F& _to, F32 _factor)
- {
- AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
- m_point3F_interpolate( _from, _to, _factor, *this);
- }
- inline void Point3F::zero()
- {
- x = y = z = 0.0f;
- }
- inline bool Point3F::isZero() const
- {
- return ((x*x) <= POINT_EPSILON) && ((y*y) <= POINT_EPSILON) && ((z*z) <= POINT_EPSILON );
- }
- inline bool Point3F::isUnitLength() const
- {
- return ( mFabs( 1.0f - ( x*x + y*y + z*z ) ) < POINT_EPSILON );
- }
- inline bool Point3F::equal( const Point3F &compare, F32 epsilon ) const
- {
- return( ( mFabs( x - compare.x ) < epsilon ) &&
- ( mFabs( y - compare.y ) < epsilon ) &&
- ( mFabs( z - compare.z ) < epsilon ) );
- }
- inline U32 Point3F::getLeastComponentIndex() const
- {
- U32 idx;
- if ( mFabs( x ) < mFabs( y ) )
- {
- if ( mFabs( x ) < mFabs( z ) )
- idx = 0;
- else
- idx = 2;
- }
- else
- {
- if ( mFabs( y ) < mFabs( z ) )
- idx = 1;
- else
- idx = 2;
- }
- return idx;
- }
- inline U32 Point3F::getGreatestComponentIndex() const
- {
- U32 idx;
- if ( mFabs( x ) > mFabs( y ) )
- {
- if ( mFabs( x ) > mFabs( z ) )
- idx = 0;
- else
- idx = 2;
- }
- else
- {
- if ( mFabs( y ) > mFabs( z ) )
- idx = 1;
- else
- idx = 2;
- }
- return idx;
- }
- inline F32 Point3F::least() const
- {
- return getMin( mFabs( x ), getMin( mFabs( y ), mFabs( z ) ) );
- }
- inline F32 Point3F::most() const
- {
- return getMax( mFabs( x ), getMax( mFabs( y ), mFabs( z ) ) );
- }
- inline void Point3F::neg()
- {
- x = -x;
- y = -y;
- z = -z;
- }
- inline void Point3F::convolve(const Point3F& c)
- {
- x *= c.x;
- y *= c.y;
- z *= c.z;
- }
- inline void Point3F::convolveInverse(const Point3F& c)
- {
- x /= c.x;
- y /= c.y;
- z /= c.z;
- }
- inline F32 Point3F::lenSquared() const
- {
- return (x * x) + (y * y) + (z * z);
- }
- inline F32 Point3F::len() const
- {
- return mSqrt(x*x + y*y + z*z);
- }
- inline void Point3F::normalize()
- {
- m_point3F_normalize(*this);
- }
- inline F32 Point3F::magnitudeSafe() const
- {
- if( isZero() )
- {
- return 0.0f;
- }
- else
- {
- return len();
- }
- }
- inline void Point3F::normalizeSafe()
- {
- F32 vmag = magnitudeSafe();
- if( vmag > POINT_EPSILON )
- {
- *this *= F32(1.0 / vmag);
- }
- }
- inline void Point3F::normalize(F32 val)
- {
- m_point3F_normalize_f(*this, val);
- }
- inline bool Point3F::operator==(const Point3F& _test) const
- {
- return (x == _test.x) && (y == _test.y) && (z == _test.z);
- }
- inline bool Point3F::operator!=(const Point3F& _test) const
- {
- return operator==(_test) == false;
- }
- inline Point3F Point3F::operator+(const Point3F& _add) const
- {
- return Point3F(x + _add.x, y + _add.y, z + _add.z);
- }
- inline Point3F Point3F::operator-(const Point3F& _rSub) const
- {
- return Point3F(x - _rSub.x, y - _rSub.y, z - _rSub.z);
- }
- inline Point3F& Point3F::operator+=(const Point3F& _add)
- {
- x += _add.x;
- y += _add.y;
- z += _add.z;
- return *this;
- }
- inline Point3F& Point3F::operator-=(const Point3F& _rSub)
- {
- x -= _rSub.x;
- y -= _rSub.y;
- z -= _rSub.z;
- return *this;
- }
- inline Point3F Point3F::operator*(F32 _mul) const
- {
- return Point3F(x * _mul, y * _mul, z * _mul);
- }
- inline Point3F Point3F::operator/(F32 _div) const
- {
- AssertFatal(_div != 0.0f, "Error, div by zero attempted");
- F32 inv = 1.0f / _div;
- return Point3F(x * inv, y * inv, z * inv);
- }
- inline Point3F& Point3F::operator*=(F32 _mul)
- {
- x *= _mul;
- y *= _mul;
- z *= _mul;
- return *this;
- }
- inline Point3F& Point3F::operator/=(F32 _div)
- {
- AssertFatal(_div != 0.0f, "Error, div by zero attempted");
- F32 inv = 1.0f / _div;
- x *= inv;
- y *= inv;
- z *= inv;
- return *this;
- }
- inline Point3F Point3F::operator*(const Point3F &_vec) const
- {
- return Point3F(x * _vec.x, y * _vec.y, z * _vec.z);
- }
- inline Point3F& Point3F::operator*=(const Point3F &_vec)
- {
- x *= _vec.x;
- y *= _vec.y;
- z *= _vec.z;
- return *this;
- }
- inline Point3F Point3F::operator/(const Point3F &_vec) const
- {
- AssertFatal(_vec.x != 0.0f && _vec.y != 0.0f && _vec.z != 0.0f, "Error, div by zero attempted");
- return Point3F(x / _vec.x, y / _vec.y, z / _vec.z);
- }
- inline Point3F& Point3F::operator/=(const Point3F &_vec)
- {
- AssertFatal(_vec.x != 0.0f && _vec.y != 0.0f && _vec.z != 0.0f, "Error, div by zero attempted");
- x /= _vec.x;
- y /= _vec.y;
- z /= _vec.z;
- return *this;
- }
- inline Point3F Point3F::operator-() const
- {
- return Point3F(-x, -y, -z);
- }
- inline Point3F& Point3F::operator=(const Point3D &_vec)
- {
- x = (F32)_vec.x;
- y = (F32)_vec.y;
- z = (F32)_vec.z;
- return *this;
- }
- //------------------------------------------------------------------------------
- //-------------------------------------- Point3D
- //
- inline Point3D::Point3D()
- : x(0.0), y(0.0), z(0.0)
- {
- //
- }
- inline Point3D::Point3D(const Point3D& _copy)
- : x(_copy.x), y(_copy.y), z(_copy.z)
- {
- //
- }
- inline Point3D::Point3D(const Point3F& _copy)
- : x(_copy.x), y(_copy.y), z(_copy.z)
- {
- //
- }
- inline Point3D::Point3D(F64 xyz)
- : x(xyz), y(xyz), z(xyz)
- {
- //
- }
- inline Point3D::Point3D(F64 _x, F64 _y, F64 _z)
- : x(_x), y(_y), z(_z)
- {
- //
- }
- inline void Point3D::set( F64 xyz )
- {
- x = y = z = xyz;
- }
- inline void Point3D::set(F64 _x, F64 _y, F64 _z)
- {
- x = _x;
- y = _y;
- z = _z;
- }
- inline void Point3D::setMin(const Point3D& _test)
- {
- x = (_test.x < x) ? _test.x : x;
- y = (_test.y < y) ? _test.y : y;
- z = (_test.z < z) ? _test.z : z;
- }
- inline void Point3D::setMax(const Point3D& _test)
- {
- x = (_test.x > x) ? _test.x : x;
- y = (_test.y > y) ? _test.y : y;
- z = (_test.z > z) ? _test.z : z;
- }
- inline void Point3D::interpolate(const Point3D& _from, const Point3D& _to, F64 _factor)
- {
- AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
- m_point3D_interpolate( _from, _to, _factor, *this);
- }
- inline void Point3D::zero()
- {
- x = y = z = 0.0;
- }
- inline bool Point3D::isZero() const
- {
- return (x == 0.0f) && (y == 0.0f) && (z == 0.0f);
- }
- inline void Point3D::neg()
- {
- x = -x;
- y = -y;
- z = -z;
- }
- inline void Point3D::convolve(const Point3D& c)
- {
- x *= c.x;
- y *= c.y;
- z *= c.z;
- }
- inline void Point3D::convolveInverse(const Point3D& c)
- {
- x /= c.x;
- y /= c.y;
- z /= c.z;
- }
- inline F64 Point3D::lenSquared() const
- {
- return (x * x) + (y * y) + (z * z);
- }
- inline F64 Point3D::len() const
- {
- F64 temp = x*x + y*y + z*z;
- return (temp > 0.0) ? mSqrtD(temp) : 0.0;
- }
- inline void Point3D::normalize()
- {
- m_point3D_normalize(*this);
- }
- inline F64 Point3D::magnitudeSafe() const
- {
- if( isZero() )
- {
- return 0.0;
- }
- else
- {
- return len();
- }
- }
- inline void Point3D::normalizeSafe()
- {
- F64 vmag = magnitudeSafe();
- if( vmag > POINT_EPSILON )
- {
- *this *= F64(1.0 / vmag);
- }
- }
- inline void Point3D::normalize(F64 val)
- {
- m_point3D_normalize_f(*this, val);
- }
- inline bool Point3D::operator==(const Point3D& _test) const
- {
- return (x == _test.x) && (y == _test.y) && (z == _test.z);
- }
- inline bool Point3D::operator!=(const Point3D& _test) const
- {
- return operator==(_test) == false;
- }
- inline Point3D Point3D::operator+(const Point3D& _add) const
- {
- return Point3D(x + _add.x, y + _add.y, z + _add.z);
- }
- inline Point3D Point3D::operator-(const Point3D& _rSub) const
- {
- return Point3D(x - _rSub.x, y - _rSub.y, z - _rSub.z);
- }
- inline Point3D& Point3D::operator+=(const Point3D& _add)
- {
- x += _add.x;
- y += _add.y;
- z += _add.z;
- return *this;
- }
- inline Point3D& Point3D::operator-=(const Point3D& _rSub)
- {
- x -= _rSub.x;
- y -= _rSub.y;
- z -= _rSub.z;
- return *this;
- }
- inline Point3D Point3D::operator*(F64 _mul) const
- {
- return Point3D(x * _mul, y * _mul, z * _mul);
- }
- inline Point3D Point3D::operator/(F64 _div) const
- {
- AssertFatal(_div != 0.0f, "Error, div by zero attempted");
- F64 inv = 1.0f / _div;
- return Point3D(x * inv, y * inv, z * inv);
- }
- inline Point3D& Point3D::operator*=(F64 _mul)
- {
- x *= _mul;
- y *= _mul;
- z *= _mul;
- return *this;
- }
- inline Point3D& Point3D::operator/=(F64 _div)
- {
- AssertFatal(_div != 0.0f, "Error, div by zero attempted");
- F64 inv = 1.0f / _div;
- x *= inv;
- y *= inv;
- z *= inv;
- return *this;
- }
- inline Point3D Point3D::operator-() const
- {
- return Point3D(-x, -y, -z);
- }
- inline Point3F Point3D::toPoint3F() const
- {
- return Point3F((F32)x,(F32)y,(F32)z);
- }
- //-------------------------------------------------------------------
- // Non-Member Operators
- //-------------------------------------------------------------------
- inline Point3I operator*(S32 mul, const Point3I& multiplicand)
- {
- return multiplicand * mul;
- }
- inline Point3F operator*(F32 mul, const Point3F& multiplicand)
- {
- return multiplicand * mul;
- }
- inline Point3D operator*(F64 mul, const Point3D& multiplicand)
- {
- return multiplicand * mul;
- }
- inline F32 mDot(const Point3F &p1, const Point3F &p2)
- {
- return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z);
- }
- inline F64 mDot(const Point3D &p1, const Point3D &p2)
- {
- return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z);
- }
- inline void mCross(const Point3F &a, const Point3F &b, Point3F *res)
- {
- res->x = (a.y * b.z) - (a.z * b.y);
- res->y = (a.z * b.x) - (a.x * b.z);
- res->z = (a.x * b.y) - (a.y * b.x);
- }
- inline void mCross(const Point3D &a, const Point3D &b, Point3D *res)
- {
- res->x = (a.y * b.z) - (a.z * b.y);
- res->y = (a.z * b.x) - (a.x * b.z);
- res->z = (a.x * b.y) - (a.y * b.x);
- }
- inline Point3F mCross(const Point3F &a, const Point3F &b)
- {
- Point3F r;
- mCross( a, b, &r );
- return r;
- }
- inline Point3D mCross(const Point3D &a, const Point3D &b)
- {
- Point3D r;
- mCross( a, b, &r );
- return r;
- }
- /// Returns the vector normalized.
- inline Point3F mNormalize( const Point3F &vec )
- {
- Point3F out( vec );
- out.normalize();
- return out;
- }
- /// Returns true if the point is NaN.
- inline bool mIsNaN( const Point3F &p )
- {
- return mIsNaN_F( p.x ) || mIsNaN_F( p.y ) || mIsNaN_F( p.z );
- }
- /// Returns a copy of the vector reflected by a normal
- inline Point3F mReflect( const Point3F &v, const Point3F &n )
- {
- return v - 2 * n * mDot( v, n );
- }
- /// Returns a perpendicular vector to the unit length input vector.
- extern Point3F mPerp( const Point3F &normal );
- #endif // _MPOINT3_H_
|