- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# CoordGeom.h

    00001 /*
    00002  *      Copyright (C) 2003-2005 Gabest
    00003  *      http://www.gabest.org
    00004  *
    00005  *  This Program is free software; you can redistribute it and/or modify
    00006  *  it under the terms of the GNU General Public License as published by
    00007  *  the Free Software Foundation; either version 2, or (at your option)
    00008  *  any later version.
    00009  *
    00010  *  This Program is distributed in the hope that it will be useful,
    00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    00013  *  GNU General Public License for more details.
    00014  *
    00015  *  You should have received a copy of the GNU General Public License
    00016  *  along with GNU Make; see the file COPYING.  If not, write to
    00017  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
    00018  *  http://www.gnu.org/copyleft/gpl.html
    00019  *
    00020  */
    00021
    00022 #pragma once
    00023
    00024 #ifndef PI
    00025 #define PI (3.141592654)
    00026 #endif
    00027
    00028 #define DegToRad(d) ((d)*PI/180.0)
    00029 #define RadToDeg(r) ((r)*180.0/PI)
    00030 #define Sgn(d) (IsZero(d) ? 0 : (d) > 0 ? 1 : -1)
    00031 #define SgnPow(d, p) (IsZero(d) ? 0 : (pow(fabs(d), p) * Sgn(d)))
    00032
    00033 class Vector
    00034 {
    00035 public:
    00036         double x, y, z;
    00037
    00038         Vector() {x = y = z = 0;}
    00039         Vector(double x, double y, double z);
    00040         void Set(double x, double y, double z);
    00041
    00042         Vector Normal(Vector& a, Vector& b);
    00043         double Angle(Vector& a, Vector& b);
    00044         double Angle(Vector& a);
    00045         void Angle(double& u, double& v); // returns spherical coords in radian, -PI/2 <= u <= PI/2, -PI <= v <= PI
    00046         Vector Angle(); // does like prev., returns 'u' in 'ret.x', and 'v' in 'ret.y'
    00047
    00048         Vector Unit();
    00049         Vector& Unitalize();
    00050         double Length();
    00051         double Sum(); // x + y + z
    00052         double CrossSum(); // xy + xz + yz
    00053         Vector Cross(); // xy, xz, yz
    00054         Vector Pow(double exp);
    00055
    00056         Vector& Min(Vector& a);
    00057         Vector& Max(Vector& a);
    00058         Vector Abs();
    00059
    00060         Vector Reflect(Vector& n);
    00061         Vector Refract(Vector& n, double nFront, double nBack, double* nOut = NULL);
    00062         Vector Refract2(Vector& n, double nFrom, double nTo, double* nOut = NULL);
    00063
    00064         Vector operator - ();
    00065         double& operator [] (int i);
    00066
    00067         double operator | (Vector& v); // dot
    00068         Vector operator % (Vector& v); // cross
    00069
    00070         bool operator == (const Vector& v) const;
    00071         bool operator != (const Vector& v) const;
    00072
    00073         Vector operator + (double d);
    00074         Vector operator + (Vector& v);
    00075         Vector operator - (double d);
    00076         Vector operator - (Vector& v);
    00077         Vector operator * (double d);
    00078         Vector operator * (Vector& v);
    00079         Vector operator / (double d);
    00080         Vector operator / (Vector& v);
    00081         Vector& operator += (double d);
    00082         Vector& operator += (Vector& v);
    00083         Vector& operator -= (double d);
    00084         Vector& operator -= (Vector& v);
    00085         Vector& operator *= (double d);
    00086         Vector& operator *= (Vector& v);
    00087         Vector& operator /= (double d);
    00088         Vector& operator /= (Vector& v);
    00089 };
    00090
    00091 class Ray
    00092 {
    00093 public:
    00094         Vector p, d;
    00095
    00096         Ray() {}
    00097         Ray(Vector& p, Vector& d);
    00098         void Set(Vector& p, Vector& d);
    00099
    00100         double GetDistanceFrom(Ray& r); // r = plane
    00101         double GetDistanceFrom(Vector& v); // v = point
    00102
    00103         Vector operator [] (double t);
    00104 };
    00105
    00106 class XForm
    00107 {
    00108         class Matrix
    00109         {
    00110         public:
    00111                 double mat[4][4];
    00112
    00113                 Matrix();
    00114                 void Initalize();
    00115
    00116                 Matrix operator * (Matrix& m);
    00117                 Matrix& operator *= (Matrix& m);
    00118         } m;
    00119
    00120         bool m_isWorldToLocal;
    00121
    00122 public:
    00123         XForm() {}
    00124         XForm(Ray& r, Vector& s, bool isWorldToLocal = true);
    00125
    00126         void Initalize();
    00127         void Initalize(Ray& r, Vector& s, bool isWorldToLocal = true);
    00128
    00129         void operator *= (Vector& s); // scale
    00130         void operator += (Vector& t); // translate
    00131         void operator <<= (Vector& r); // rotate
    00132
    00133         void operator /= (Vector& s); // scale
    00134         void operator -= (Vector& t); // translate
    00135         void operator >>= (Vector& r); // rotate
    00136
    00137 //      transformations
    00138         Vector operator < (Vector& n); // normal
    00139         Vector operator << (Vector& v); // vector
    00140         Ray operator << (Ray& r); // ray
    00141 };