00001 /************************************************************************ 00002 * typeDef.h 00003 ************************************************************************ 00004 * Author : Duksu Kim (bluekds@tclab.kaist.ac.kr) 00005 * Affiliation : SGLAB(http://sglab.kaist.ac.kr), Dept. of Computer Science(http://cs.kaist.ac.kr) , KAIST(http://www.kaist.ac.kr) 00006 * Version : 0.5 00007 * Create : 2009/02/23 00008 * Last update : 2009/03/10 00009 ************************************************************************/ 00010 00011 #pragma once 00012 00013 #include <vector> 00014 #include <iostream> 00015 #include "Geometry/vec3f.h" 00016 00017 //#define SHOW_COLLIDE_TRIANGLES 00018 00019 typedef unsigned int UINT ; 00020 /************************************************************************/ 00021 /* edge2f */ 00022 /************************************************************************/ 00023 class edge2f { 00024 unsigned int _vids[2]; 00025 unsigned int _fids[2]; 00026 00027 FORCEINLINE void set(unsigned int id0, unsigned int id1) { 00028 if (id0 > id1) { 00029 _vids[0] = id0; 00030 _vids[1] = id1; 00031 } else { 00032 _vids[1] = id0; 00033 _vids[0] = id1; 00034 } 00035 } 00036 public: 00037 FORCEINLINE edge2f() { 00038 _vids[0] = _vids[1] = UINT_MAX ; 00039 _fids[0] = _fids[1] = UINT_MAX ; 00040 } 00041 00042 FORCEINLINE edge2f(unsigned id0, unsigned int id1, unsigned int fid) { 00043 set(id0, id1) ; 00044 _fids[0] = fid ; 00045 _fids[1] = UINT_MAX ; 00046 } 00047 00048 FORCEINLINE void set_fid2(unsigned id) { 00049 _fids[1] = id ; 00050 } 00051 00052 FORCEINLINE unsigned int vid(int i) { return _vids[i] ; } 00053 FORCEINLINE unsigned int fid(int i) { return _fids[i] ; } 00054 00055 FORCEINLINE bool operator == (const edge2f &other) const { 00056 return (_vids[0] == other._vids[0] && _vids[1] == other._vids[1]); 00057 } 00058 00059 FORCEINLINE bool operator < (const edge2f &other) const { 00060 if (_vids[0] == other._vids[0]) 00061 return _vids[1] < other._vids[1] ; 00062 else 00063 return _vids[0] < other._vids[0] ; 00064 } 00065 }; 00066 00068 00071 typedef struct { 00073 UINT objVtx ; 00075 UINT objFace ; 00077 UINT triVtx ; 00079 UINT triFace ; 00081 UINT indexVtx ; 00083 float collisionTime ; 00084 } intersectingVF ; 00085 00087 00092 typedef struct { 00094 UINT obj1 ; 00096 UINT obj2 ; 00098 UINT tri1 ; 00100 UINT tri2 ; 00102 UINT indexEdge1[2] ; 00104 UINT indexEdge2[2] ; 00106 float collisionTime ; 00107 } intersectingEE ; 00108 00109 class mat4f { 00110 public : 00111 float element[4][4] ; 00112 00113 mat4f() { 00114 identityMat() ; 00115 } 00116 00117 void identityMat (void) { 00118 for ( int i = 0 ; i < 4 ; i++ ) { 00119 for ( int j = 0 ; j < 4 ; j++ ) { 00120 if ( i == j ) { 00121 element[i][j] = 1 ; 00122 } else { 00123 element[i][j] = 0 ; 00124 } 00125 } 00126 } 00127 } 00128 00129 void mulVec3f(Vec3f &v) { 00130 float x, y, z ; 00131 00132 x = element[0][0]*v.x + element[0][1]*v.y + element[0][2]*v.z + element[0][3] ; 00133 y = element[1][0]*v.x + element[1][1]*v.y + element[1][2]*v.z + element[1][3] ; 00134 z = element[2][0]*v.x + element[2][1]*v.y + element[2][2]*v.z + element[2][3] ; 00135 00136 v.x = x ; 00137 v.y = y ; 00138 v.z = z ; 00139 } 00140 } ; 00141 00142 /************************************************************************/ 00143 /* edge2v */ 00144 /************************************************************************/ 00145 class edge2v { 00146 UINT _vids[2] ; 00147 00148 public : 00149 FORCEINLINE edge2v() { 00150 _vids[0] = _vids[1] = UINT_MAX ; 00151 } 00152 00153 FORCEINLINE void set ( UINT id0, UINT id1 ) { 00154 _vids[0] = id0 ; 00155 _vids[1] = id1 ; 00156 } 00157 00158 FORCEINLINE UINT id0() { 00159 return _vids[0] ; 00160 } 00161 00162 FORCEINLINE UINT id1() { 00163 return _vids[1] ; 00164 } 00165 }; 00166 00167 /************************************************************************/ 00168 /* tri3e */ 00169 /************************************************************************/ 00170 class tri3e { 00171 unsigned int _ids[3]; 00172 00173 public: 00174 FORCEINLINE tri3e() { 00175 _ids[0] = _ids[1] = _ids[2] = UINT_MAX; 00176 } 00177 00178 FORCEINLINE tri3e(unsigned int id0, unsigned int id1, unsigned id2) { 00179 set(id0, id1, id2); 00180 } 00181 00182 FORCEINLINE void set(unsigned int id0, unsigned int id1, unsigned int id2) { 00183 _ids[0] = id0; 00184 _ids[1] = id1; 00185 _ids[2] = id2; 00186 } 00187 00188 FORCEINLINE unsigned int id(int i) { return _ids[i]; } 00189 }; 00190 00191 /************************************************************************/ 00192 /* tri3f */ 00193 /************************************************************************/ 00194 class tri3f { 00195 unsigned int _ids[3]; 00196 00197 public: 00198 FORCEINLINE tri3f() { 00199 _ids[0] = _ids[1] = _ids[2] = UINT_MAX; 00200 } 00201 00202 FORCEINLINE tri3f(unsigned int id0, unsigned int id1, unsigned id2) { 00203 set(id0, id1, id2); 00204 } 00205 00206 FORCEINLINE void set(unsigned int id0, unsigned int id1, unsigned int id2) { 00207 _ids[0] = id0; 00208 _ids[1] = id1; 00209 _ids[2] = id2; 00210 } 00211 00212 FORCEINLINE unsigned int id(int i) { return _ids[i]; } 00213 FORCEINLINE unsigned int id0() {return _ids[0];} 00214 FORCEINLINE unsigned int id1() {return _ids[1];} 00215 FORCEINLINE unsigned int id2() {return _ids[2];} 00216 }; 00217 00218 /************************************************************************/ 00219 /* Vector operations */ 00220 /************************************************************************/ 00221 00222 // swap two vectors 00223 #define swap(a, b) {\ 00224 Vec3f *tmp = a;\ 00225 a = b;\ 00226 b = tmp;\ 00227 } 00228 00229 // linear interpolation 00230 inline Vec3f interp(const Vec3f &p1, const Vec3f &p2, float t) { 00231 return p1*(1-t)+p2*t; 00232 } 00233 00234 // get normal from three vertices by using cross product 00235 inline Vec3f update(Vec3f &v1, Vec3f &v2, Vec3f &v3) { 00236 Vec3f s = (v2-v1); 00237 return s.cross(v3-v1); 00238 } 00239 00240 // get normal from three vertices by using cross product 00241 inline Vec3f update(tri3f &tri, Vec3f *vtxs) { 00242 Vec3f &v1 = vtxs[tri.id0()]; 00243 Vec3f &v2 = vtxs[tri.id1()]; 00244 Vec3f &v3 = vtxs[tri.id2()]; 00245 00246 return update(v1, v2, v3); 00247 }