home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1997 June / PC_Shareware-1997-06.iso / programy / genesis / cppmaths.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-12  |  9.4 KB  |  255 lines

  1. /*------------------------------------------------------------------
  2.             Maths Library
  3.             -------------
  4.  
  5.     Header for C++ interface to basic geometry related maths
  6.     functions
  7.  
  8.     (C) Silicon Dream Ltd 1994
  9.  
  10.   ------------------------------------------------------------------
  11.  
  12. Changes:                        Date:
  13. * Created file                        19/08/94
  14. */
  15.  
  16. #ifndef CPPMATHS
  17. #define CPPMATHS
  18.  
  19. extern "C"
  20. {
  21. #include <maths.h>
  22. }
  23.  
  24. /*
  25.     Four new classes are defined by this header:
  26.  
  27.         Vec    A three element cartesian vector
  28.         Polar    A three element polar vector
  29.         LVec    A four element cartesian vector
  30.         Mat    A 4x4 matrix
  31.  
  32.     The following operations are permitted on these types:
  33.  
  34.     Vector operations:    Description:
  35.     ------------------    ------------
  36.     Vec vec(x, y, z)    Initialise a vector up to three values
  37.     Vec vecA(vecB)        Initialise a vector with another vector
  38.     Vec vec(lvec)        Initialise a vector with a long vector (loses last component)
  39.     ((Vec) lvec)        Convert a long vector to a vector (loses last component)
  40.     Vec vec(pol)        Initialise a vector with a polar vector (conversion performed)
  41.     ((Vec) pol)             Convert a polar vector to a vector (conversion performed)
  42.     vecA+vecB        Add two vectors
  43.     vecA-vecB        Subtract a vector from another
  44.     vec*f            Multiply a vector by a scalar
  45.     vec*mat            Multiply a vector by a matrix
  46.     vec/f            Divide a vector by a scalar
  47.     vecA+=vecB        Add two vectors (overwrite original)
  48.     vecA-=vecB        Subtract a vector from another (overwrite original)
  49.     vec*=f            Multiply a vector by a scalar (overwrite original)
  50.     vec*=mat        Multiply a vector by a matrix (overwrite original)
  51.     vec/=f            Divide a vector by a scalar (overwrite original)
  52.     -vec            Returns the vector inverted
  53.     vecA==vecB        Are two vectors equal?
  54.     vecA!=vecB        Are two vectors not equal?
  55.     vec.Set(x, y, z)    Set a vector's components
  56.     vec.SetX(x)        Set a vector's x component
  57.     vec.SetY(y)        Set a vector's y component
  58.     vec.SetZ(z)        Set a vector's z component
  59.     vec.X()            Get x component
  60.     vec.Y()            Get y component
  61.     vec.Z()            Get z component
  62.     vec.Len()        Get length of vector
  63.     vecA.Dot(vecB)        Get dot product of two vectors
  64.     vecA.Cross(vecB)    Get cross product of two vectors
  65.     vec.AddrVec()        Gets address of Vector member (WARNING: Use only to interface to non C++ code)
  66.  
  67.     Long vector operations:    Description:
  68.     -----------------------    ------------
  69.     LVec lvec(x, y, z, w)    Initialise a long vector with up to four values
  70.     LVec lvecA(lvecB)    Initialise a long vector with another long vector
  71.     LVec lvec(vec)        Initialise a long vector with a vector (last component becomes 1.0)
  72.     ((LVec) vec)        Convert a long vector to a vector (last component becomes 1.0)
  73.     lvec*mat        Multiply a long vector with a matrix
  74.     lvec*=mat        Multiply a long vector with a matrix (overwrite original)
  75.     lvecA==lvecB        Are two long vectors equal?
  76.     lvecA!=lvecB        Are two long vectors not equal?
  77.     lvec.Set(x, y, z, w)    Set a long vector's components
  78.     lvec.SetX(x)        Set a long vector's x component
  79.     lvec.SetY(y)        Set a long vector's y component
  80.     lvec.SetZ(z)        Set a long vector's z component
  81.     lvec.SetW(w)        Set a long vector's w component
  82.     lvec.X()        Get x component
  83.     lvec.Y()        Get y component
  84.     lvec.Z()        Get z component
  85.     lvec.W()        Get w component (homogeneous coordinate)
  86.     lvec.AddrLVec()        Gets address of LongVec member (WARNING: Use only to interface to non C++ code)
  87.  
  88.     Polar operations:    Description:
  89.     -----------------    ------------
  90.     Polar pol(t, p, r)    Initialise a polar with up to three values
  91.     Polar polA(polB)    Initialise a polar with another polar
  92.     Polar pol(vec)        Initialise a polar with a vector (conversion performed)
  93.     ((Polar) vec)        Convert a vector to a polar (conversion performed)
  94.     polA==polB        Are two polars equal?
  95.     polA!=polB        Are two polars not equal?
  96.     pol.Set(t, p, r)    Set a polar's components
  97.     vec.SetTheta(t)        Set a vector's theta component
  98.     vec.SetPhi(p)        Set a vector's phi component
  99.     vec.SetRho(r)        Set a vector's rho component
  100.     pol.Theta()        Get theta component
  101.     pol.Phi()        Get phi component
  102.     pol.Rho()        Get rho component
  103.     pol.AddrVec()        Gets address of Vector member (WARNING: Use only to interface to non C++ code)
  104.  
  105.     Matrix operations:    Description:
  106.     ------------------    ------------
  107.     Mat mat            Initialise a matrix with the identity matrix
  108.     Mat matA(matB)        Initialise a matrix with another matrix
  109.     Mat mat(XROT, a)    Initialise a matrix with a rotation or scale value (or leave it unset)
  110.     Mat mat(TRANSL, vec)    Initialise a matrix with a translation value (or leave it unset)
  111.     Mat mat(vecPos, vecDir, vecUp) Initialise a matrix by defining a new coordinate system
  112.     matA*matB        Multiply two matricies
  113.     matA*=matB        Multiply two matricies (overwrite original)
  114.     matA==matB        Are two matricies equal?
  115.     matA!=matB        Are two matricies not equal?
  116.     -mat            Compute inverse of matrix (ie. when multiplied by this gives identity)
  117.     mat.Set(f1,..f16)    Sets elements of a matrix
  118.     mat.MoveParent(vec)    Moves coor sys defined by matrix relative to its parent
  119.     mat.MoveChild(vec)        Moves coor sys defined by matrix relative to itself
  120.     mat.ScaleParent(x, y, z) Scale coor sys defined by matrix relative to its parent
  121.     mat.ScaleChild(x, y, z)    Scale coor sys defined by matrix relative to its own origin
  122.     mat.XRotParent(a)    Rotates coor sys defined by matrix about parents x axis
  123.     mat.XRotChild(a)    Rotates coor sys defined by matrix about its own x axis
  124.     mat.YRotParent(a)    Rotates coor sys defined by matrix about parents y axis
  125.     mat.YRotChild(a)    Rotates coor sys defined by matrix about its own y axis
  126.     mat.ZRotParent(a)    Rotates coor sys defined by matrix about parents z axis
  127.     mat.ZRotChild(a)    Rotates coor sys defined by matrix about its own z axis
  128.     mat.ReverseX()        Reverses direction of x axis of coor sys defined by matrix
  129.     mat.ReverseY()        Reverses direction of y axis of coor sys defined by matrix
  130.     mat.ReverseZ()        Reverses direction of z axis of coor sys defined by matrix
  131.     mat.AddrMat()        Gets address of Matrix member (WARNING: Use only to interface to non C++ code)
  132. */
  133.  
  134. class _cppdyn Vec            // vec
  135.     {
  136.     friend class _cppdyn LVec;
  137.     friend class _cppdyn Polar;
  138.     friend class _cppdyn Mat;
  139.     public:
  140.         Vec (float fX=0.0, float fY=0.0, float fZ=0.0);
  141.         Vec (Vec const &vecOther);
  142.         Vec (LVec const &lvec);
  143.         Vec (Polar const &pol);
  144.         Vec operator+ (Vec const &vecOther) const;
  145.         Vec operator- (Vec const &vecOther) const;
  146.         Vec operator* (float f) const;
  147.         Vec operator* (Mat const &mat) const;
  148.         Vec operator/ (float f) const;
  149.         Vec &operator+= (Vec const &vecOther);
  150.         Vec &operator-= (Vec const &vecOther);
  151.         Vec &operator*= (float f);
  152.         Vec &operator*= (Mat const &mat);
  153.         Vec &operator/= (float f);
  154.         Vec operator- () const;
  155.         bool operator== (Vec const &vecOther) const;
  156.         bool operator!= (Vec const &vecOther) const;
  157.         Vec &Set (float fX, float fY, float fZ);
  158.         Vec &SetX (float fX);
  159.         Vec &SetY (float fY);
  160.         Vec &SetZ (float fZ);
  161.         float X () const;
  162.         float Y () const;
  163.         float Z () const;
  164.         float Len () const;
  165.         float Dot (Vec const &vecOther) const;
  166.         Vec Cross (Vec const &vecOther) const;
  167.         Vector *AddrVec ();
  168.     private:
  169.         Vector        vec;
  170.     };
  171.  
  172. class _cppdyn LVec                 // lvec
  173.     {
  174.     friend class _cppdyn Vec;
  175.     public:
  176.         LVec (float fX=0.0, float fY=0.0, float fZ=0.0, float fW=1.0);
  177.         LVec (LVec const &lvecOther);
  178.         LVec (Vec const &vec);
  179.         LVec operator* (Mat const &mat) const;
  180.         LVec &operator*= (Mat const &mat);
  181.         bool operator== (LVec const &lvecOther) const;
  182.         bool operator!= (LVec const &lvecOther) const;
  183.         LVec &Set (float fX, float fY, float fZ, float fW);
  184.         LVec &SetX (float fX);
  185.         LVec &SetY (float fY);
  186.         LVec &SetZ (float fZ);
  187.         LVec &SetW (float fW);
  188.         float X () const;
  189.         float Y () const;
  190.         float Z () const;
  191.         float W () const;
  192.         LongVec *AddrLVec ();
  193.     private:
  194.         LongVec        lvec;
  195.     };
  196.  
  197. class _cppdyn Polar                 // pol
  198.     {
  199.     friend class _cppdyn Vec;
  200.     public:
  201.         Polar (float fX=0.0, float fY=0.0, float fZ=0.0);
  202.         Polar (Polar const &polOther);
  203.         Polar (Vec const &vecOther);
  204.         bool operator== (Polar const &polOther) const;
  205.         bool operator!= (Polar const &polOther) const;
  206.         Polar &Set (float fTheta, float fPhi, float fRho);
  207.         Polar &SetTheta (float fTheta);
  208.         Polar &SetPhi (float fPhi);
  209.         Polar &SetRho (float fRho);
  210.         float Theta () const;
  211.         float Phi () const;
  212.         float Rho () const;
  213.         Vector *AddrVec ();
  214.     private:
  215.         Vector        vec;
  216.     };
  217.  
  218. enum    MatType {XROT, YROT, ZROT, SCALE, TRANSL, UNSET};
  219.  
  220. class _cppdyn Mat                 // mat
  221.     {
  222.     friend class _cppdyn Vec;
  223.     friend class _cppdyn LVec;
  224.     public:
  225.         Mat ();
  226.         Mat (Mat const &matOther);
  227.         Mat (MatType mt, float f1=1.0, float f2=1.0, float f3=1.0);
  228.         Mat (MatType mt, const Vec &vec);
  229.         Mat (Vec const &vecOrg, Vec const &vecAxZ, Vec const &vecAxY);
  230.         Mat operator* (Mat const &matOther) const;
  231.         Mat &operator*= (Mat const &matOther);
  232.         bool operator== (Mat const &matOther) const;
  233.         bool operator!= (Mat const &matOther) const;
  234.         Mat operator- () const;
  235.         Mat &Set (float f1, float f2, float f3, float f4, float f5, float f6, float f7, float f8, float f9, float f10, float f11, float f12, float f13, float f14, float f15, float f16);
  236.         Mat &MoveParent (Vec const &vec);
  237.         Mat &MoveChild (Vec const &vec);
  238.         Mat &ScaleParent (float fX, float fY, float fZ);
  239.         Mat &ScaleChild (float fX, float fY, float fZ);
  240.         Mat &XRotParent (float fAng);
  241.         Mat &XRotChild (float fAng);
  242.         Mat &YRotParent (float fAng);
  243.         Mat &YRotChild (float fAng);
  244.         Mat &ZRotParent (float fAng);
  245.         Mat &ZRotChild (float fAng);
  246.         Mat &ReverseX ();
  247.         Mat &ReverseY ();
  248.         Mat &ReverseZ ();
  249.         Matrix *AddrMat ();
  250.     private:
  251.         Matrix        mat;
  252.     };
  253.  
  254. #endif            // Do not include this file twice
  255.