1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 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 _MPOINT_H_
- #define _MPOINT_H_
- #define POINT_EPSILON (1e-4) ///< Epsilon for point types.
- //-------------------------------------- Note: because of a circular dependency,
- // the mMathFn.h header is below the point
- // class declarations... DMM
- #ifndef _PLATFORM_H_
- #include "platform/platform.h"
- #endif
- //------------------------------------------------------------------------------
- /// 2D integer point
- ///
- /// Uses S32 internally.
- class Point2I
- {
- //-------------------------------------- Public data
- public:
- S32 x; ///< X position
- S32 y; ///< Y position
- //-------------------------------------- Public interface
- public:
- Point2I(); ///< Create an uninitialized point.
- Point2I(const Point2I&); ///< Copy constructor
- Point2I(const S32 in_x, const S32 in_y); ///< Create point from two co-ordinates.
- //-------------------------------------- Non-math mutators and misc functions
- void set(const S32 in_x, const S32 in_y);///< Set (x,y) position
- void setMin(const Point2I&); ///< Store lesser co-ordinates from parameter in this point.
- void setMax(const Point2I&); ///< Store greater co-ordinates from parameter in this point.
- //-------------------------------------- Math mutators
- void neg(); ///< Invert sign of point's co-ordinates.
- void convolve(const Point2I&); ///< Convolve this point by parameter.
- //-------------------------------------- Queries
- bool isZero() const; ///< Is this point at the origin? (No epsilon used)
- F32 len() const; ///< Get the length of the point
- //-------------------------------------- Overloaded operators
- public:
- // Comparison operators
- bool operator==(const Point2I&) const;
- bool operator!=(const Point2I&) const;
- // Arithmetic w/ other points
- Point2I operator+(const Point2I&) const;
- Point2I operator-(const Point2I&) const;
- Point2I& operator+=(const Point2I&);
- Point2I& operator-=(const Point2I&);
- // Arithmetic w/ scalars
- Point2I operator*(const S32) const;
- Point2I& operator*=(const S32);
- Point2I operator/(const S32) const;
- Point2I& operator/=(const S32);
- // Unary operators
- Point2I operator-() const;
- //-------------------------------------- Public static constants
- public:
- const static Point2I One;
- const static Point2I Zero;
- const static Point2I Min;
- const static Point2I Max;
- };
- //------------------------------------------------------------------------------
- /// 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.
- Point3I(const S32 in_x, const S32 in_y, const S32 in_z); ///< Create a point from co-ordinates.
- //-------------------------------------- Non-math mutators and misc functions
- void set(const S32 in_x, const S32 in_y, const 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.
- //-------------------------------------- Math mutators
- void neg(); ///< Invert co-ordinate's signs.
- void convolve(const Point3I&); ///< Convolve by paramater.
- //-------------------------------------- Queries
- bool isZero() const; ///< Check for point at origin. (No epsilon.)
- F32 len() const; ///< Get length.
- //-------------------------------------- Overloaded operators
- public:
- // 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*(const S32) const;
- Point3I& operator*=(const S32);
- Point3I operator/(const S32) const;
- Point3I& operator/=(const S32);
- // Unary operators
- Point3I operator-() const;
- //-------------------------------------- Public static constants
- public:
- const static Point3I One;
- const static Point3I Zero;
- };
- //------------------------------------------------------------------------------
- /// 2D floating-point point.
- class Point2F
- {
- //-------------------------------------- Public data
- public:
- F32 x;
- F32 y;
- public:
- Point2F(); ///< Create uninitialized point.
- Point2F(const Point2F&); ///< Copy constructor
- Point2F(const F32 _x, const F32 _y); ///< Create point from co-ordinates.
- //-------------------------------------- Non-math mutators and misc functions
- public:
- void set(const F32 _x, const F32 _y);///< Set point's co-ordinates.
- void setMin(const Point2F&); ///< Store lesser co-ordinates.
- void setMax(const Point2F&); ///< Store greater co-ordinates.
- /// Interpolate from a to b, based on c.
- ///
- /// @param a Starting point.
- /// @param b Ending point.
- /// @param c Interpolation factor (0.0 .. 1.0).
- void interpolate(const Point2F& a, const Point2F& b, const F32 c);
- operator F32*() { return (&x); }
- operator F32*() const { return (F32*)(&x); }
- //-------------------------------------- Queries
- public:
- bool isZero() const; ///< Check for zero coordinates. (No epsilon.)
- F32 len() const; ///< Get length.
- F32 lenSquared() const; ///< Get squared length (one sqrt less than len()).
- //-------------------------------------- Mathematical mutators
- public:
- void neg(); ///< Invert signs of co-ordinates.
- void normalize(); ///< Normalize vector.
- void normalize(F32 val); ///< Normalize, scaling by val.
- void convolve(const Point2F&); ///< Convolve by parameter.
- void convolveInverse(const Point2F&); ///< Inversely convolute by parameter. (ie, divide)
- //Luma: Utility function to round a Point2F
- void round() {x = (F32)((S32)(x + 0.5f)); y = (F32)((S32)(y + 0.5f));}
- //-------------------------------------- Overloaded operators
- public:
- // Comparison operators
- bool operator==(const Point2F&) const;
- bool operator!=(const Point2F&) const;
- // Arithmetic w/ other points
- Point2F operator+(const Point2F&) const;
- Point2F operator-(const Point2F&) const;
- Point2F& operator+=(const Point2F&);
- Point2F& operator-=(const Point2F&);
- // Arithmetic w/ scalars
- Point2F operator*(const F32) const;
- Point2F operator/(const F32) const;
- Point2F& operator*=(const F32);
- Point2F& operator/=(const F32);
- // Unary operators
- Point2F operator-() const;
- //-------------------------------------- Public static constants
- public:
- const static Point2F One;
- const static Point2F Zero;
- const static Point2F Min;
- const static Point2F Max;
- };
- //------------------------------------------------------------------------------
- /// 2D high-precision point.
- ///
- /// Uses F64 internally.
- class Point2D
- {
- //-------------------------------------- Public data
- public:
- F64 x; ///< X co-ordinate.
- F64 y; ///< Y co-ordinate.
- public:
- Point2D(); ///< Create uninitialized point.
- Point2D(const Point2D&); ///< Copy constructor
- Point2D(const F64 _x, const F64 _y); ///< Create point from coordinates.
- //-------------------------------------- Non-math mutators and misc functions
- public:
- void set(const F64 _x, const F64 _y);///< Set point's coordinates.
- void setMin(const Point2D&); ///< Store lesser co-ordinates.
- void setMax(const Point2D&); ///< Store greater co-ordinates.
- /// Interpolate from a to b, based on c.
- ///
- /// @param a Starting point.
- /// @param b Ending point.
- /// @param c Interpolation factor (0.0 .. 1.0).
- void interpolate(const Point2D &a, const Point2D &b, const F64 c);
- operator F64*() { return (&x); }
- operator F64*() const { return (F64*)(&x); }
- //-------------------------------------- Queries
- public:
- bool isZero() const;
- F64 len() const;
- F64 lenSquared() const;
- //-------------------------------------- Mathematical mutators
- public:
- void neg();
- void normalize();
- void normalize(F64 val);
- void convolve(const Point2D&);
- void convolveInverse(const Point2D&);
- //-------------------------------------- Overloaded operators
- public:
- // Comparison operators
- bool operator==(const Point2D&) const;
- bool operator!=(const Point2D&) const;
- // Arithmetic w/ other points
- Point2D operator+(const Point2D&) const;
- Point2D operator-(const Point2D&) const;
- Point2D& operator+=(const Point2D&);
- Point2D& operator-=(const Point2D&);
- // Arithmetic w/ scalars
- Point2D operator*(const F64) const;
- Point2D operator/(const F64) const;
- Point2D& operator*=(const F64);
- Point2D& operator/=(const F64);
- // Unary operators
- Point2D operator-() const;
- //-------------------------------------- Public static constants
- public:
- const static Point2D One;
- const static Point2D Zero;
- };
- //------------------------------------------------------------------------------
- class Point3F
- {
- //-------------------------------------- Public data
- public:
- F32 x;
- F32 y;
- F32 z;
- public:
- Point3F();
- Point3F(const Point3F&);
- Point3F(const F32 _x, const F32 _y, const F32 _z);
- //-------------------------------------- Non-math mutators and misc functions
- public:
- void set(const F32 _x, const F32 _y, const F32 _z);
- void set(const Point3F&);
- void setMin(const Point3F&);
- void setMax(const Point3F&);
- void interpolate(const Point3F&, const Point3F&, const F32);
- void zero();
- F32& operator [](int i) { /*ASSERT HERE??*/ return( *((&x)+i) ); }
- operator F32*() { return (&x); }
- operator F32*() const { return (F32*)(&x); }
- //-------------------------------------- Queries
- public:
- bool isZero() const;
- F32 len() const;
- F32 lenSquared() const;
- F32 magnitudeSafe();
- bool equal( Point3F &compare );
- //-------------------------------------- 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*(const F32) const;
- Point3F operator/(const F32) const;
- Point3F& operator*=(const F32);
- Point3F& operator/=(const F32);
- Point3F operator*(const Point3F&) const;
- Point3F& operator*=(const Point3F&);
- // Unary operators
- Point3F operator-() const;
- //-------------------------------------- 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 F64 _x, const F64 _y, const F64 _z);
- //-------------------------------------- Non-math mutators and misc functions
- public:
- void set(const F64 _x, const F64 _y, const F64 _z);
- void setMin(const Point3D&);
- void setMax(const Point3D&);
- void interpolate(const Point3D&, const Point3D&, const F64);
- operator F64*() { return (&x); }
- operator F64*() const { return (F64*)(&x); }
- //-------------------------------------- Queries
- public:
- bool isZero() const;
- F64 len() const;
- F64 lenSquared() const;
- //-------------------------------------- Mathematical mutators
- public:
- void neg();
- void normalize();
- void normalize(F64 val);
- void convolve(const Point3D&);
- void convolveInverse(const Point3D&);
- //-------------------------------------- Overloaded operators
- public:
- // 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*(const F64) const;
- Point3D operator/(const F64) const;
- Point3D& operator*=(const F64);
- Point3D& operator/=(const F64);
- // Unary operators
- Point3D operator-() const;
- //-------------------------------------- Public static constants
- public:
- const static Point3D One;
- const static Point3D Zero;
- };
- //------------------------------------------------------------------------------
- /// 4D floating-point point.
- ///
- /// Uses F32 internally.
- ///
- /// Useful for representing quaternions and other 4d beasties.
- class Point4F
- {
- //-------------------------------------- Public data
- public:
- F32 x; ///< X co-ordinate.
- F32 y; ///< Y co-ordinate.
- F32 z; ///< Z co-ordinate.
- F32 w; ///< W co-ordinate.
- public:
- Point4F(); ///< Create an unitialized point.
- Point4F(const Point4F&); ///< Copy constructor.
- /// Create point from coordinates.
- Point4F(const F32 _x, const F32 _y, const F32 _z, const F32 _w);
- /// Set point's coordinates.
- void set(const F32 _x, const F32 _y, const F32 _z, const F32 _w);
- /// Interpolate from _pt1 to _pt2, based on _factor.
- ///
- /// @param _pt1 Starting point.
- /// @param _pt2 Ending point.
- /// @param _factor Interpolation factor (0.0 .. 1.0).
- void interpolate(const Point4F& _pt1, const Point4F& _pt2, const F32 _factor);
- operator F32*() { return (&x); }
- operator F32*() const { return (F32*)(&x); }
- //-------------------------------------- Public static constants
- public:
- const static Point4F One;
- const static Point4F Zero;
- };
- typedef Point4F Vector4F; ///< Points can be vectors!
- #ifndef _MMATHFN_H_
- #include "math/mMathFn.h"
- #endif
- //------------------------------------------------------------------------------
- //-------------------------------------- Inline functions inclusions
- //------------------------------------------------------------------------------
- //-------------------------------------- Point2I
- //
- inline Point2I::Point2I()
- {
- //
- }
- inline Point2I::Point2I(const Point2I& _copy)
- : x(_copy.x), y(_copy.y)
- {
- //
- }
- inline Point2I::Point2I(const S32 _x, const S32 _y)
- : x(_x), y(_y)
- {
- //
- }
- inline void Point2I::set(const S32 _x, const S32 _y)
- {
- x = _x;
- y = _y;
- }
- inline void Point2I::setMin(const Point2I& _test)
- {
- x = (_test.x < x) ? _test.x : x;
- y = (_test.y < y) ? _test.y : y;
- }
- inline void Point2I::setMax(const Point2I& _test)
- {
- x = (_test.x > x) ? _test.x : x;
- y = (_test.y > y) ? _test.y : y;
- }
- inline void Point2I::neg()
- {
- x = -x;
- y = -y;
- }
- inline void Point2I::convolve(const Point2I& c)
- {
- x *= c.x;
- y *= c.y;
- }
- inline bool Point2I::isZero() const
- {
- return ((x == 0) && (y == 0));
- }
- inline F32 Point2I::len() const
- {
- return mSqrt(F32(x*x + y*y));
- }
- inline bool Point2I::operator==(const Point2I& _test) const
- {
- return ((x == _test.x) && (y == _test.y));
- }
- inline bool Point2I::operator!=(const Point2I& _test) const
- {
- return (operator==(_test) == false);
- }
- inline Point2I Point2I::operator+(const Point2I& _add) const
- {
- return Point2I(x + _add.x, y + _add.y);
- }
- inline Point2I Point2I::operator-(const Point2I& _rSub) const
- {
- return Point2I(x - _rSub.x, y - _rSub.y);
- }
- inline Point2I& Point2I::operator+=(const Point2I& _add)
- {
- x += _add.x;
- y += _add.y;
- return *this;
- }
- inline Point2I& Point2I::operator-=(const Point2I& _rSub)
- {
- x -= _rSub.x;
- y -= _rSub.y;
- return *this;
- }
- inline Point2I Point2I::operator-() const
- {
- return Point2I(-x, -y);
- }
- inline Point2I Point2I::operator*(const S32 mul) const
- {
- return Point2I(x * mul, y * mul);
- }
- inline Point2I Point2I::operator/(const S32 div) const
- {
- AssertFatal(div != 0, "Error, div by zero attempted");
- return Point2I(x/div, y/div);
- }
- inline Point2I& Point2I::operator*=(const S32 mul)
- {
- x *= mul;
- y *= mul;
- return *this;
- }
- inline Point2I& Point2I::operator/=(const S32 div)
- {
- AssertFatal(div != 0, "Error, div by zero attempted");
- x /= div;
- y /= div;
- return *this;
- }
- //------------------------------------------------------------------------------
- //-------------------------------------- Point3I
- //
- inline Point3I::Point3I()
- {
- //
- }
- inline Point3I::Point3I(const Point3I& _copy)
- : x(_copy.x), y(_copy.y), z(_copy.z)
- {
- //
- }
- inline Point3I::Point3I(const S32 _x, const S32 _y, const S32 _z)
- : x(_x), y(_y), z(_z)
- {
- //
- }
- inline void Point3I::set(const S32 _x, const S32 _y, const 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::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*(const S32 mul) const
- {
- return Point3I(x * mul, y * mul, z * mul);
- }
- inline Point3I Point3I::operator/(const S32 div) const
- {
- AssertFatal(div != 0, "Error, div by zero attempted");
- return Point3I(x/div, y/div, z/div);
- }
- inline Point3I& Point3I::operator*=(const S32 mul)
- {
- x *= mul;
- y *= mul;
- z *= mul;
- return *this;
- }
- inline Point3I& Point3I::operator/=(const S32 div)
- {
- AssertFatal(div != 0, "Error, div by zero attempted");
- x /= div;
- y /= div;
- z /= div;
- return *this;
- }
- //------------------------------------------------------------------------------
- //-------------------------------------- Point2F
- //
- inline Point2F::Point2F()
- {
- //
- }
- inline Point2F::Point2F(const Point2F& _copy)
- : x(_copy.x), y(_copy.y)
- {
- //
- }
- inline Point2F::Point2F(const F32 _x, const F32 _y)
- : x(_x), y(_y)
- {
- }
- inline void Point2F::set(const F32 _x, const F32 _y)
- {
- x = _x;
- y = _y;
- }
- inline void Point2F::setMin(const Point2F& _test)
- {
- x = (_test.x < x) ? _test.x : x;
- y = (_test.y < y) ? _test.y : y;
- }
- inline void Point2F::setMax(const Point2F& _test)
- {
- x = (_test.x > x) ? _test.x : x;
- y = (_test.y > y) ? _test.y : y;
- }
- inline void Point2F::interpolate(const Point2F& _rFrom, const Point2F& _to, const F32 _factor)
- {
- AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
- x = (_rFrom.x * (1.0f - _factor)) + (_to.x * _factor);
- y = (_rFrom.y * (1.0f - _factor)) + (_to.y * _factor);
- }
- inline bool Point2F::isZero() const
- {
- return (x == 0.0f) && (y == 0.0f);
- }
- inline F32 Point2F::lenSquared() const
- {
- return (x * x) + (y * y);
- }
- inline void Point2F::neg()
- {
- x = -x;
- y = -y;
- }
- inline void Point2F::convolve(const Point2F& c)
- {
- x *= c.x;
- y *= c.y;
- }
- inline void Point2F::convolveInverse(const Point2F& c)
- {
- x /= c.x;
- y /= c.y;
- }
- inline bool Point2F::operator==(const Point2F& _test) const
- {
- return (x == _test.x) && (y == _test.y);
- }
- inline bool Point2F::operator!=(const Point2F& _test) const
- {
- return operator==(_test) == false;
- }
- inline Point2F Point2F::operator+(const Point2F& _add) const
- {
- return Point2F(x + _add.x, y + _add.y);
- }
- inline Point2F Point2F::operator-(const Point2F& _rSub) const
- {
- return Point2F(x - _rSub.x, y - _rSub.y);
- }
- inline Point2F& Point2F::operator+=(const Point2F& _add)
- {
- x += _add.x;
- y += _add.y;
- return *this;
- }
- inline Point2F& Point2F::operator-=(const Point2F& _rSub)
- {
- x -= _rSub.x;
- y -= _rSub.y;
- return *this;
- }
- inline Point2F Point2F::operator*(const F32 _mul) const
- {
- return Point2F(x * _mul, y * _mul);
- }
- inline Point2F Point2F::operator/(const F32 _div) const
- {
- AssertFatal(_div != 0.0f, "Error, div by zero attempted");
- F32 inv = 1.0f / _div;
- return Point2F(x * inv, y * inv);
- }
- inline Point2F& Point2F::operator*=(const F32 _mul)
- {
- x *= _mul;
- y *= _mul;
- return *this;
- }
- inline Point2F& Point2F::operator/=(const F32 _div)
- {
- AssertFatal(_div != 0.0f, "Error, div by zero attempted");
- F32 inv = 1.0f / _div;
- x *= inv;
- y *= inv;
- return *this;
- }
- inline Point2F Point2F::operator-() const
- {
- return Point2F(-x, -y);
- }
- inline F32 Point2F::len() const
- {
- return mSqrt(x*x + y*y);
- }
- inline void Point2F::normalize()
- {
- m_point2F_normalize(*this);
- }
- inline void Point2F::normalize(F32 val)
- {
- m_point2F_normalize_f(*this, val);
- }
- //------------------------------------------------------------------------------
- //-------------------------------------- Point2D
- //
- inline Point2D::Point2D()
- {
- //
- }
- inline Point2D::Point2D(const Point2D& _copy)
- : x(_copy.x), y(_copy.y)
- {
- //
- }
- inline Point2D::Point2D(const F64 _x, const F64 _y)
- : x(_x), y(_y)
- {
- }
- inline void Point2D::set(const F64 _x, const F64 _y)
- {
- x = _x;
- y = _y;
- }
- inline void Point2D::setMin(const Point2D& _test)
- {
- x = (_test.x < x) ? _test.x : x;
- y = (_test.y < y) ? _test.y : y;
- }
- inline void Point2D::setMax(const Point2D& _test)
- {
- x = (_test.x > x) ? _test.x : x;
- y = (_test.y > y) ? _test.y : y;
- }
- inline void Point2D::interpolate(const Point2D& _rFrom, const Point2D& _to, const F64 _factor)
- {
- AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
- x = (_rFrom.x * (1.0f - _factor)) + (_to.x * _factor);
- y = (_rFrom.y * (1.0f - _factor)) + (_to.y * _factor);
- }
- inline bool Point2D::isZero() const
- {
- return (x == 0.0f) && (y == 0.0f);
- }
- inline F64 Point2D::lenSquared() const
- {
- return (x * x) + (y * y);
- }
- inline void Point2D::neg()
- {
- x = -x;
- y = -y;
- }
- inline void Point2D::convolve(const Point2D& c)
- {
- x *= c.x;
- y *= c.y;
- }
- inline void Point2D::convolveInverse(const Point2D& c)
- {
- x /= c.x;
- y /= c.y;
- }
- inline bool Point2D::operator==(const Point2D& _test) const
- {
- return (x == _test.x) && (y == _test.y);
- }
- inline bool Point2D::operator!=(const Point2D& _test) const
- {
- return operator==(_test) == false;
- }
- inline Point2D Point2D::operator+(const Point2D& _add) const
- {
- return Point2D(x + _add.x, y + _add.y);
- }
- inline Point2D Point2D::operator-(const Point2D& _rSub) const
- {
- return Point2D(x - _rSub.x, y - _rSub.y);
- }
- inline Point2D& Point2D::operator+=(const Point2D& _add)
- {
- x += _add.x;
- y += _add.y;
- return *this;
- }
- inline Point2D& Point2D::operator-=(const Point2D& _rSub)
- {
- x -= _rSub.x;
- y -= _rSub.y;
- return *this;
- }
- inline Point2D Point2D::operator*(const F64 _mul) const
- {
- return Point2D(x * _mul, y * _mul);
- }
- inline Point2D Point2D::operator/(const F64 _div) const
- {
- AssertFatal(_div != 0.0f, "Error, div by zero attempted");
- F64 inv = 1.0f / _div;
- return Point2D(x * inv, y * inv);
- }
- inline Point2D& Point2D::operator*=(const F64 _mul)
- {
- x *= _mul;
- y *= _mul;
- return *this;
- }
- inline Point2D& Point2D::operator/=(const F64 _div)
- {
- AssertFatal(_div != 0.0f, "Error, div by zero attempted");
- F64 inv = 1.0f / _div;
- x *= inv;
- y *= inv;
- return *this;
- }
- inline Point2D Point2D::operator-() const
- {
- return Point2D(-x, -y);
- }
- inline F64 Point2D::len() const
- {
- return mSqrtD(x*x + y*y);
- }
- inline void Point2D::normalize()
- {
- m_point2D_normalize(*this);
- }
- inline void Point2D::normalize(F64 val)
- {
- m_point2D_normalize_f(*this, val);
- }
- //------------------------------------------------------------------------------
- //-------------------------------------- Point3F
- //
- inline Point3F::Point3F()
- #if defined(TORQUE_OS_LINUX)
- : x(0.f), y(0.f), z(0.f)
- #endif
- {
- // 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(const F32 _x, const F32 _y, const F32 _z)
- : x(_x), y(_y), z(_z)
- {
- //
- }
- inline void Point3F::set(const F32 _x, const F32 _y, const 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, const 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::equal( Point3F &compare )
- {
- return( ( mFabs( x - compare.x ) < POINT_EPSILON ) &&
- ( mFabs( y - compare.y ) < POINT_EPSILON ) &&
- ( mFabs( z - compare.z ) < POINT_EPSILON ) );
- }
- 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()
- {
- 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*(const F32 _mul) const
- {
- return Point3F(x * _mul, y * _mul, z * _mul);
- }
- inline Point3F Point3F::operator*(const Point3F &_vec) const
- {
- return Point3F(x * _vec.x, y * _vec.y, z * _vec.z);
- }
- inline Point3F Point3F::operator/(const 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*=(const F32 _mul)
- {
- x *= _mul;
- y *= _mul;
- z *= _mul;
- return *this;
- }
- inline Point3F& Point3F::operator*=(const Point3F &_vec)
- {
- x *= _vec.x;
- y *= _vec.y;
- z *= _vec.z;
- return *this;
- }
- inline Point3F& Point3F::operator/=(const 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
- {
- return Point3F(-x, -y, -z);
- }
- //------------------------------------------------------------------------------
- //-------------------------------------- Point3D
- //
- inline Point3D::Point3D()
- {
- //
- }
- inline Point3D::Point3D(const Point3D& _copy)
- : x(_copy.x), y(_copy.y), z(_copy.z)
- {
- //
- }
- inline Point3D::Point3D(const F64 _x, const F64 _y, const F64 _z)
- : x(_x), y(_y), z(_z)
- {
- //
- }
- inline void Point3D::set(const F64 _x, const F64 _y, const 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, const F64 _factor)
- {
- AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
- m_point3D_interpolate( _from, _to, _factor, *this);
- }
- 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
- {
- return mSqrtD(x*x + y*y + z*z);
- }
- inline void Point3D::normalize()
- {
- m_point3D_normalize(*this);
- }
- 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*(const F64 _mul) const
- {
- return Point3D(x * _mul, y * _mul, z * _mul);
- }
- inline Point3D Point3D::operator/(const 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*=(const F64 _mul)
- {
- x *= _mul;
- y *= _mul;
- z *= _mul;
- return *this;
- }
- inline Point3D& Point3D::operator/=(const 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);
- }
- //------------------------------------------------------------------------------
- //-------------------------------------- Point4F
- //
- inline Point4F::Point4F()
- {
- //
- }
- inline Point4F::Point4F(const Point4F& _copy)
- : x(_copy.x), y(_copy.y), z(_copy.z), w(_copy.w)
- {
- //
- }
- inline Point4F::Point4F(const F32 _x, const F32 _y, const F32 _z, const F32 _w)
- : x(_x), y(_y), z(_z), w(_w)
- {
- //
- }
- inline void Point4F::set(const F32 _x, const F32 _y, const F32 _z, const F32 _w)
- {
- x = _x;
- y = _y;
- z = _z;
- w = _w;
- }
- inline void Point4F::interpolate(const Point4F& _from, const Point4F& _to, const F32 _factor)
- {
- x = (_from.x * (1.0f - _factor)) + (_to.x * _factor);
- y = (_from.y * (1.0f - _factor)) + (_to.y * _factor);
- z = (_from.z * (1.0f - _factor)) + (_to.z * _factor);
- w = (_from.w * (1.0f - _factor)) + (_to.w * _factor);
- }
- //--------------------------------------------------------------------------
- //-------------------------------------- NON-MEMBER Operators
- //
- inline Point2I operator*(const S32 mul, const Point2I& multiplicand)
- {
- return multiplicand * mul;
- }
- inline Point3I operator*(const S32 mul, const Point3I& multiplicand)
- {
- return multiplicand * mul;
- }
- inline Point2F operator*(const F32 mul, const Point2F& multiplicand)
- {
- return multiplicand * mul;
- }
- inline Point3F operator*(const F32 mul, const Point3F& multiplicand)
- {
- return multiplicand * mul;
- }
- inline Point2D operator*(const F64 mul, const Point2D& multiplicand)
- {
- return multiplicand * mul;
- }
- inline Point3D operator*(const F64 mul, const Point3D& multiplicand)
- {
- return multiplicand * mul;
- }
- #endif // _POINT_H_
|