![[]](/images/special/trans.gif)
LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference cTriangle3.hGo to the documentation of this file.00001 /************************************************************************ 00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library 00003 Copyright (C) 2002 Aaron Cameron 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library 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 GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 A copy of the GNU Lesser General Public License has been provided with 00020 this library in the file 'COPYING'. 00021 00022 Contact information for the author of this library has been provided 00023 with this library in the file 'AUTHOR'. 00024 ************************************************************************/ 00025 00026 #ifndef _n2l4_cTriangle3_h 00027 #define _n2l4_cTriangle3_h 00028 00029 #include "n2l/n2l.h" 00030 #include "cVector3.h" 00031 #include "cMatrix44.h" 00032 00033 #include <math.h> 00034 00035 /******************************************************************************/ 00036 namespace n2l 00037 { 00041 template <class TComponent> 00042 class cTriangle3 00043 { 00044 public: 00045 typedef TComponent tComponent; 00046 00047 typedef cTriangle3<TComponent> tThisTriangle; 00048 typedef cVector3<TComponent,TComponent,TComponent> tVertex; 00049 typedef cMatrix44<TComponent> tThisMatrix44; 00050 00051 /**********************************************************************/ 00052 cTriangle3() {} 00053 00054 /**********************************************************************/ 00055 cTriangle3( const tVertex &iA, const tVertex &iB, 00056 const tVertex &iC ) 00057 { 00058 mVertex[0] = iA; 00059 mVertex[1] = iB; 00060 mVertex[2] = iC; 00061 } 00062 00063 /**********************************************************************/ 00064 cTriangle3( const tThisTriangle &iTri ) 00065 { 00066 mVertex[0] = iTri.mVertex[0]; 00067 mVertex[1] = iTri.mVertex[1]; 00068 mVertex[2] = iTri.mVertex[2]; 00069 } 00070 00071 /**********************************************************************/ 00072 ~cTriangle3() {} 00073 00074 /**********************************************************************/ 00075 const tVertex &vertex( const tUint &iIndex ) const { 00076 return mVertex[iIndex]; 00077 } 00078 00079 /**********************************************************************/ 00080 const tVertex &a() const { return mVertex[0]; } 00081 const tVertex &b() const { return mVertex[1]; } 00082 const tVertex &c() const { return mVertex[2]; } 00083 00084 /**********************************************************************/ 00085 tVertex &a() { return mVertex[0]; } 00086 tVertex &b() { return mVertex[1]; } 00087 tVertex &c() { return mVertex[2]; } 00088 00089 /**********************************************************************/ 00090 void move( const tVertex &iV ) 00091 { 00092 mVertex[0] += iV; 00093 mVertex[1] += iV; 00094 mVertex[2] += iV; 00095 } 00096 00097 /**********************************************************************/ 00101 void rotate( const tThisMatrix44 &iM ) 00102 { 00103 tVertex tmp; 00104 mVertex[0] = iM.multiply( tmp, mVertex[0] ); 00105 mVertex[1] = iM.multiply( tmp, mVertex[1] ); 00106 mVertex[2] = iM.multiply( tmp, mVertex[2] ); 00107 } 00108 00109 /**********************************************************************/ 00113 void translate( const tThisMatrix44 &iM ) 00114 { 00115 tVertex tmp; 00116 const tVertex Translation( iM[tThisMatrix44::_03], 00117 iM[tThisMatrix44::_13], iM[tThisMatrix44::_23] ); 00118 00119 mVertex[0] = iM.multiply( tmp, mVertex[0] ); 00120 mVertex[1] = iM.multiply( tmp, mVertex[1] ); 00121 mVertex[2] = iM.multiply( tmp, mVertex[2] ); 00122 mVertex[0] += Translation; 00123 mVertex[1] += Translation; 00124 mVertex[2] += Translation; 00125 } 00126 00127 /**********************************************************************/ 00128 void set( const tVertex &iA, const tVertex &iB, const tVertex &iC ) 00129 { 00130 mVertex[0] = iA; 00131 mVertex[1] = iB; 00132 mVertex[2] = iC; 00133 } 00134 00135 /**********************************************************************/ 00136 const tComponent area() const { 00137 tVertex ab = mVertex[1]-mVertex[0]; 00138 tVertex ac = mVertex[2]-mVertex[0]; 00139 return ab.cross(ac).magnitude()/2; 00140 } 00141 00142 /**********************************************************************/ 00143 const tVertex center() const { 00144 tVertex tmp = mVertex[0]; 00145 tmp += mVertex[1]; 00146 tmp += mVertex[2]; 00147 tmp /= 3; 00148 return tmp; 00149 } 00150 00151 /**********************************************************************/ 00152 void subdivide( tThisTriangle &oT1, tThisTriangle &oT2 ) const 00153 { 00154 tVertex ab = mVertex[1]-mVertex[0]; 00155 ab /= 2; 00156 ab += mVertex[0]; 00157 oT1.set( mVertex[0], ab, mVertex[2] ); 00158 oT2.set( ab, mVertex[1], mVertex[2] ); 00159 } 00160 00161 /**********************************************************************/ 00165 void subdivideLongest( tThisTriangle &oT1, tThisTriangle &oT2 ) const 00166 { 00167 tVertex ab = mVertex[1]-mVertex[0]; 00168 tVertex bc = mVertex[2]-mVertex[1]; 00169 tVertex ca = mVertex[0]-mVertex[2]; 00170 00171 const tFloat Mag0 = ab.magnitude(); 00172 const tFloat Mag1 = bc.magnitude(); 00173 const tFloat Mag2 = ca.magnitude(); 00174 tUint a,b,c; 00175 if (Mag0>Mag1 && Mag0>Mag2) { 00176 a = 0; b = 1; c = 2; 00177 } else if (Mag1>Mag0 && Mag1>Mag2) { 00178 a = 1; b = 2; c = 0; 00179 } else { 00180 a = 2; b = 0; c = 1; 00181 } 00182 00183 ab = mVertex[b]-mVertex[a]; 00184 ab /= 2; 00185 ab += mVertex[a]; 00186 oT1.set( mVertex[a], ab, mVertex[c] ); 00187 oT2.set( ab, mVertex[b], mVertex[c] ); 00188 } 00189 00190 /**********************************************************************/ 00191 inline tThisTriangle &operator ==( const tThisTriangle &iTri ) 00192 { 00193 mVertex[0] = iTri.mVertex[0]; 00194 mVertex[1] = iTri.mVertex[1]; 00195 mVertex[2] = iTri.mVertex[2]; 00196 return *this; 00197 } 00198 00199 /**********************************************************************/ 00200 inline void operator +=( const tVertex &iV ) { move(iV); } 00201 00202 /**********************************************************************/ 00203 const tString dump() const { 00204 tString str("tTriangle3(\n\t"); 00205 str += mVertex[0].dump(); 00206 str += "\n\t"; 00207 str += mVertex[1].dump(); 00208 str += "\n\t"; 00209 str += mVertex[2].dump(); 00210 str += "\n)"; 00211 return str; 00212 } 00213 00214 private: 00215 tVertex mVertex[3]; 00216 00217 }; // class 00218 00219 } // namespace n2l 00220 00221 #endif |