Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages
math3d_d.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1998,1999,2000 by Jorrit Tyberghein 00003 Largely rewritten by Ivan Avramovic <ivan@avramovic.com> 00004 Converted to double by Thomas Hieber 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public 00017 License along with this library; if not, write to the Free 00018 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 */ 00020 00021 #ifndef __CS_MATH3D_D_H__ 00022 #define __CS_MATH3D_D_H__ 00023 00024 #include "csextern.h" 00025 00026 #include "cstypes.h" 00027 00034 #ifndef ABS 00035 #define ABS(x) ((x)<0?-(x):(x)) 00036 #endif 00037 00038 class csDVector3; 00039 class csDMatrix3; 00040 class csVector3; 00041 00042 inline double dSqr (double d) 00043 { 00044 return d * d; 00045 } 00046 00050 class CS_CRYSTALSPACE_EXPORT csDVector3 00051 { 00052 public: 00054 double x; 00056 double y; 00058 double z; 00059 00065 csDVector3 () {} 00066 00072 csDVector3 (double m) : x(m), y(m), z(m) {} 00073 00075 csDVector3 (double ix, double iy, double iz = 0) { x = ix; y = iy; z = iz; } 00076 00078 csDVector3 (const csDVector3& v) { x = v.x; y = v.y; z = v.z; } 00079 00081 csDVector3 (const csVector3&); 00082 00084 inline friend 00085 csDVector3 operator+ (const csDVector3& v1, const csDVector3& v2) 00086 { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); } 00087 00089 inline friend 00090 csDVector3 operator- (const csDVector3& v1, const csDVector3& v2) 00091 { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); } 00092 00094 inline friend double operator* (const csDVector3& v1, const csDVector3& v2) 00095 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; } 00096 00098 inline friend 00099 csDVector3 operator% (const csDVector3& v1, const csDVector3& v2) 00100 { 00101 return csDVector3 (v1.y*v2.z-v1.z*v2.y, 00102 v1.z*v2.x-v1.x*v2.z, 00103 v1.x*v2.y-v1.y*v2.x); 00104 } 00105 00107 void Cross (const csDVector3 & px, const csDVector3 & py) 00108 { 00109 x = px.y*py.z - px.z*py.y; 00110 y = px.z*py.x - px.x*py.z; 00111 z = px.x*py.y - px.y*py.x; 00112 } 00113 00115 inline friend csDVector3 operator* (const csDVector3& v, double f) 00116 { return csDVector3(v.x*f, v.y*f, v.z*f); } 00117 00119 inline friend csDVector3 operator* (double f, const csDVector3& v) 00120 { return csDVector3(v.x*f, v.y*f, v.z*f); } 00121 00123 inline friend csDVector3 operator/ (const csDVector3& v, double f) 00124 { f = 1.0f/f; return csDVector3(v.x*f, v.y*f, v.z*f); } 00125 00127 inline friend bool operator== (const csDVector3& v1, const csDVector3& v2) 00128 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; } 00129 00131 inline friend bool operator!= (const csDVector3& v1, const csDVector3& v2) 00132 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; } 00133 00135 inline friend 00136 csDVector3 operator>> (const csDVector3& v1, const csDVector3& v2) 00137 { return v2*(v1*v2)/(v2*v2); } 00138 00140 inline friend 00141 csDVector3 operator<< (const csDVector3& v1, const csDVector3& v2) 00142 { return v1*(v1*v2)/(v1*v1); } 00143 00145 inline friend bool operator< (const csDVector3& v, double f) 00146 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; } 00147 00149 inline friend bool operator> (double f, const csDVector3& v) 00150 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; } 00151 00153 inline double operator[](int n) const {return !n?x:n&1?y:z;} 00154 00156 inline double & operator[](int n){return !n?x:n&1?y:z;} 00157 00159 inline csDVector3& operator+= (const csDVector3& v) 00160 { 00161 x += v.x; 00162 y += v.y; 00163 z += v.z; 00164 00165 return *this; 00166 } 00167 00169 inline csDVector3& operator-= (const csDVector3& v) 00170 { 00171 x -= v.x; 00172 y -= v.y; 00173 z -= v.z; 00174 00175 return *this; 00176 } 00177 00179 inline csDVector3& operator*= (double f) 00180 { x *= f; y *= f; z *= f; return *this; } 00181 00183 inline csDVector3& operator/= (double f) 00184 { x /= f; y /= f; z /= f; return *this; } 00185 00187 inline csDVector3 operator+ () const { return *this; } 00188 00190 inline csDVector3 operator- () const { return csDVector3(-x,-y,-z); } 00191 00193 inline void Set (double sx, double sy, double sz) { x = sx; y = sy; z = sz; } 00194 00196 double Norm () const; 00197 00199 double SquaredNorm () const; 00200 00206 csDVector3 Unit () const { return (*this)/(this->Norm()); } 00207 00209 inline static double Norm (const csDVector3& v) { return v.Norm(); } 00210 00212 inline static csDVector3 Unit (const csDVector3& v) { return v.Unit(); } 00213 00215 void Normalize(); 00216 }; 00217 00218 00222 class CS_CRYSTALSPACE_EXPORT csDMatrix3 00223 { 00224 public: 00225 double m11, m12, m13; 00226 double m21, m22, m23; 00227 double m31, m32, m33; 00228 00229 public: 00231 csDMatrix3 (); 00232 00234 csDMatrix3 (double m11, double m12, double m13, 00235 double m21, double m22, double m23, 00236 double m31, double m32, double m33); 00237 00239 inline csDVector3 Row1() const { return csDVector3 (m11,m12,m13); } 00240 00242 inline csDVector3 Row2() const { return csDVector3 (m21,m22,m23); } 00243 00245 inline csDVector3 Row3() const { return csDVector3 (m31,m32,m33); } 00246 00248 inline csDVector3 Col1() const { return csDVector3 (m11,m21,m31); } 00249 00251 inline csDVector3 Col2() const { return csDVector3 (m12,m22,m32); } 00252 00254 inline csDVector3 Col3() const { return csDVector3 (m13,m23,m33); } 00255 00257 inline void Set (double m11, double m12, double m13, 00258 double m21, double m22, double m23, 00259 double m31, double m32, double m33) 00260 { 00261 csDMatrix3::m11 = m11; csDMatrix3::m12 = m12; csDMatrix3::m13 = m13; 00262 csDMatrix3::m21 = m21; csDMatrix3::m22 = m22; csDMatrix3::m23 = m23; 00263 csDMatrix3::m31 = m31; csDMatrix3::m32 = m32; csDMatrix3::m33 = m33; 00264 } 00265 00267 csDMatrix3& operator+= (const csDMatrix3& m); 00268 00270 csDMatrix3& operator-= (const csDMatrix3& m); 00271 00273 csDMatrix3& operator*= (const csDMatrix3& m); 00274 00276 csDMatrix3& operator*= (double s); 00277 00279 csDMatrix3& operator/= (double s); 00280 00282 inline csDMatrix3 operator+ () const { return *this; } 00284 inline csDMatrix3 operator- () const 00285 { 00286 return csDMatrix3(-m11,-m12,-m13, 00287 -m21,-m22,-m23, 00288 -m31,-m32,-m33); 00289 } 00290 00292 void Transpose (); 00293 00295 csDMatrix3 GetTranspose () const; 00296 00298 inline csDMatrix3 GetInverse () const 00299 { 00300 csDMatrix3 C( 00301 (m22*m33 - m23*m32), -(m12*m33 - m13*m32), (m12*m23 - m13*m22), 00302 -(m21*m33 - m23*m31), (m11*m33 - m13*m31), -(m11*m23 - m13*m21), 00303 (m21*m32 - m22*m31), -(m11*m32 - m12*m31), (m11*m22 - m12*m21) ); 00304 double s = (double)1./(m11*C.m11 + m12*C.m21 + m13*C.m31); 00305 00306 C *= s; 00307 00308 return C; 00309 } 00310 00312 void Invert() { *this = GetInverse (); } 00313 00315 double Determinant () const; 00316 00318 void Identity (); 00319 00321 friend csDMatrix3 operator+ (const csDMatrix3& m1, const csDMatrix3& m2); 00323 friend csDMatrix3 operator- (const csDMatrix3& m1, const csDMatrix3& m2); 00325 friend csDMatrix3 operator* (const csDMatrix3& m1, const csDMatrix3& m2); 00326 00328 inline friend csDVector3 operator* (const csDMatrix3& m, const csDVector3& v) 00329 { 00330 return csDVector3 (m.m11*v.x + m.m12*v.y + m.m13*v.z, 00331 m.m21*v.x + m.m22*v.y + m.m23*v.z, 00332 m.m31*v.x + m.m32*v.y + m.m33*v.z); 00333 } 00334 00336 friend csDMatrix3 operator* (const csDMatrix3& m, double f); 00338 friend csDMatrix3 operator* (double f, const csDMatrix3& m); 00340 friend csDMatrix3 operator/ (const csDMatrix3& m, double f); 00342 friend bool operator== (const csDMatrix3& m1, const csDMatrix3& m2); 00344 friend bool operator!= (const csDMatrix3& m1, const csDMatrix3& m2); 00346 friend bool operator< (const csDMatrix3& m, double f); 00348 friend bool operator> (double f, const csDMatrix3& m); 00349 }; 00350 00351 00357 class CS_CRYSTALSPACE_EXPORT csDPlane 00358 { 00359 public: 00361 csDVector3 norm; 00362 00364 double DD; 00365 00367 csDPlane () : norm(0,0,1), DD(0) {} 00368 00370 csDPlane (const csDVector3& plane_norm, double d=0) : 00371 norm(plane_norm), DD(d) {} 00372 00374 csDPlane (double a, double b, double c, double d=0) : norm(a,b,c), DD(d) {} 00375 00377 inline csDVector3& Normal () { return norm; } 00379 inline const csDVector3& Normal () const { return norm; } 00380 00382 inline double A () const { return norm.x; } 00384 inline double B () const { return norm.y; } 00386 inline double C () const { return norm.z; } 00388 inline double D () const { return DD; } 00389 00391 inline double& A () { return norm.x; } 00393 inline double& B () { return norm.y; } 00395 inline double& C () { return norm.z; } 00397 inline double& D () { return DD; } 00398 00400 inline void Set (double a, double b, double c, double d) 00401 { norm.x = a; norm.y = b; norm.z = c; DD = d; } 00402 00404 inline double Classify (const csDVector3& pt) const { return norm*pt+DD; } 00405 00407 static double Classify (double A, double B, double C, double D, 00408 const csDVector3& pt) 00409 { return A*pt.x + B*pt.y + C*pt.z + D; } 00410 00416 inline double Distance (const csDVector3& pt) const 00417 { return ABS (Classify (pt)); } 00418 00420 void Invert () { norm = -norm; DD = -DD; } 00421 00423 void Normalize () 00424 { 00425 double f = norm.Norm (); 00426 if (f) { norm /= f; DD /= f; } 00427 } 00428 00429 }; 00430 00435 class CS_CRYSTALSPACE_EXPORT csDMath3 00436 { 00437 public: 00445 static int WhichSide3D (const csDVector3& p, 00446 const csDVector3& v1, const csDVector3& v2) 00447 { 00448 // double s = p * (v1%v2); (original expression: expanded to the below:) 00449 double s = p.x*(v1.y*v2.z-v1.z*v2.y) + p.y*(v1.z*v2.x-v1.x*v2.z) + 00450 p.z*(v1.x*v2.y-v1.y*v2.x); 00451 if (s < 0) return 1; 00452 else if (s > 0) return -1; 00453 else return 0; 00454 } 00455 00461 static bool Visible (const csDVector3& p, const csDVector3& t1, 00462 const csDVector3& t2, const csDVector3& t3); 00463 00469 static bool Visible (const csDVector3& p, const csDPlane& pl) 00470 { return pl.Classify (p) <= 0; } 00471 00481 static void Between (const csDVector3& v1, const csDVector3& v2, 00482 csDVector3& v, double pct, double wid); 00483 00490 static void SetMinMax (const csDVector3& v, 00491 csDVector3& min, csDVector3& max) 00492 { 00493 if (v.x > max.x) max.x = v.x; else if (v.x < min.x ) min.x = v.x; 00494 if (v.y > max.y) max.y = v.y; else if (v.y < min.y ) min.y = v.y; 00495 if (v.z > max.z) max.z = v.z; else if (v.z < min.z ) min.z = v.z; 00496 } 00497 00503 inline static double DoubleArea3 (const csDVector3 &a, const csDVector3 &b, 00504 const csDVector3 &c) 00505 { 00506 csDVector3 v1 = b - a; 00507 csDVector3 v2 = c - a; 00508 return (v1 % v2).Norm (); 00509 } 00510 00516 inline static void CalcNormal (csDVector3& norm, const csDVector3& v1, 00517 const csDVector3& v2, const csDVector3& v3) 00518 { 00519 norm = (v1-v2)%(v1-v3); 00520 } 00521 00527 static void CalcNormal (csDVector3& norm, 00528 const csDVector3& v, const csDVector3& u) 00529 { norm = u%v; /* NOT v%u - vertexes are defined clockwise */ } 00530 00537 static void CalcPlane (const csDVector3& v1, const csDVector3& v2, 00538 const csDVector3& v3, csDVector3& normal, double& D) 00539 { 00540 normal = (v1-v2)%(v1-v3); 00541 D = - (normal * v1); 00542 } 00543 00550 static bool PlanesEqual (const csDPlane& p1, const csDPlane& p2) 00551 { 00552 return ( ( p1.norm - p2.norm) < (double).001 ) && 00553 ( ABS (p1.DD-p2.DD) < (double).001 ); 00554 } 00555 00561 static bool PlanesClose (const csDPlane& p1, const csDPlane& p2); 00562 }; 00563 00568 class CS_CRYSTALSPACE_EXPORT csDSquaredDist 00569 { 00570 public: 00572 static double PointPoint (const csDVector3& p1, const csDVector3& p2) 00573 { return dSqr (p1.x - p2.x) + dSqr (p1.y - p2.y) + dSqr (p1.z - p2.z); } 00574 00576 static double PointLine (const csDVector3& p, 00577 const csDVector3& l1, const csDVector3& l2); 00578 00580 static double PointPlane (const csDVector3& p, const csDPlane& plane) 00581 { double r = plane.Classify (p); return r * r; } 00582 00589 static double PointPoly (const csDVector3& p, csDVector3 *V, int n, 00590 const csDPlane& plane, double sqdist = -1); 00591 }; 00592 00598 class CS_CRYSTALSPACE_EXPORT csDIntersect3 00599 { 00600 public: 00605 static void Plane ( 00606 const csDVector3& u, const csDVector3& v, // segment 00607 const csDVector3& normal, const csDVector3& a, // plane 00608 csDVector3& isect); // intersection point 00609 00618 static bool Plane ( 00619 const csDVector3& u, const csDVector3& v, // segment 00620 double A, double B, double C, double D, // plane Ax+By+Cz+D=0 00621 csDVector3& isect, // intersection point 00622 double& dist); // distance from u to isect 00623 00632 static bool Plane ( 00633 const csDVector3& u, const csDVector3& v, // segment 00634 const csDPlane& p, // plane Ax+By+Cz+D=0 00635 csDVector3& isect, // intersection point 00636 double& dist); // distance from u to isect 00637 00643 static bool Planes(const csDPlane& p1, const csDPlane& p2, 00644 const csDPlane& p3, csDVector3& isect); 00645 00652 static double Z0Plane ( 00653 const csDVector3& u, const csDVector3& v, // segment 00654 csDVector3& isect); // intersection point 00655 00662 static double ZPlane (double zval, // plane z = zval 00663 const csDVector3& u, const csDVector3& v, // segment 00664 csDVector3& isect); // intersection point 00665 00670 static double XFrustum ( 00671 double A, const csDVector3& u, const csDVector3& v, csDVector3& isect); 00672 00677 static double YFrustum ( 00678 double B, const csDVector3& u, const csDVector3& v, csDVector3& isect); 00679 }; 00680 00683 #endif // __CS_MATH3D_D_H__
Generated for Crystal Space by doxygen 1.4.4