MeshTriangle.h
Go to the documentation of this file.00001 /**<!--------------------------------------------------------------------> 00002 @class MeshTriangle 00003 @author Travis Fischer (fisch0920@gmail.com) 00004 @author Nong Li (nongli@gmail.com) 00005 @date Spring 2008 00006 00007 @brief 00008 Basic representation of a MeshTriangle with optional UV coordinates 00009 @see also Triangle.h which defines a Triangle class which derives from 00010 Shape and differs from MeshTriangle in that it is a standalone Shape 00011 and doesn't depend on a parent Mesh to exist 00012 <!-------------------------------------------------------------------->**/ 00013 00014 #ifndef MESH_TRIANGLE_H_ 00015 #define MESH_TRIANGLE_H_ 00016 00017 #include <shapes/Intersectable.h> 00018 #include <core/UV.h> 00019 00020 //#define MESH_TRIANGLE_INTERSECTION_TYPE 0 00021 //#define MESH_TRIANGLE_INTERSECTION_TYPE 1 00022 #define MESH_TRIANGLE_INTERSECTION_TYPE 2 00023 00024 typedef Vector3 Vertex; 00025 typedef Vector3 Normal; 00026 00027 class Mesh; 00028 struct Ray; 00029 00030 class MeshTriangle : public Intersectable { 00031 public: 00032 00033 ///@name Fast-access public data 00034 //@{----------------------------------------------------------------- 00035 00036 union { 00037 struct { 00038 unsigned A, B, C; // 0-indexed to vertex list 00039 }; 00040 00041 unsigned data[3]; 00042 }; 00043 00044 union { 00045 struct { 00046 unsigned nA, nB, nC; // 0-indexed to normal list 00047 }; 00048 00049 unsigned nData[3]; 00050 }; 00051 00052 union { 00053 struct { 00054 unsigned tA, tB, tC; // 0-indexed to texture list 00055 }; 00056 00057 unsigned tData[3]; 00058 }; 00059 00060 Mesh *mesh; 00061 00062 00063 //@}----------------------------------------------------------------- 00064 ///@name Constructors 00065 //@{----------------------------------------------------------------- 00066 00067 inline MeshTriangle() { 00068 A = B = C = 0; 00069 nA = nB = nC = 0; 00070 tA = tB = tC = 0; 00071 } 00072 00073 inline MeshTriangle(unsigned A_, unsigned B_, unsigned C_, 00074 unsigned nA_ = 0, unsigned nB_ = 0, unsigned nC_ = 0, 00075 unsigned tA_ = 0, unsigned tB_ = 0, unsigned tC_ = 0) 00076 : A(A_), B(B_), C(C_), 00077 nA(nA_), nB(nB_), nC(nC_), 00078 tA(tA_), tB(tB_), tC(tC_) 00079 { } 00080 00081 inline MeshTriangle(const MeshTriangle ©) 00082 : A(copy.A), B(copy.B), C(copy.C), 00083 nA(copy.nA), nB(copy.nB), nC(copy.nC), 00084 tA(copy.tA), tB(copy.tB), tC(copy.tC) 00085 { } 00086 00087 00088 //@}----------------------------------------------------------------- 00089 ///@name Core functionality 00090 //@{----------------------------------------------------------------- 00091 00092 /// Initializes this triangle for intersection tests 00093 virtual real_t getIntersection(const Ray &ray, SurfacePoint &pt); 00094 00095 virtual AABB getAABB() const; 00096 00097 Vector3 getNormal() const; 00098 00099 Vector3 getBaryocentricCoords(const Point3 &p) const; 00100 00101 00102 //@}----------------------------------------------------------------- 00103 ///@name Sampling functionality 00104 //@{----------------------------------------------------------------- 00105 00106 /** 00107 * @returns a point chosen uniformly random over the surface of this 00108 * shape in 'pt' 00109 * @note the probability density of selecting this point is assumed to 00110 * be (1.0 / getSurfaceArea()) 00111 */ 00112 void getRandomPoint(SurfacePoint &pt); 00113 00114 /** 00115 * @returns the point on the surface of this shape corresponding to the 00116 * given UV coordinates 00117 */ 00118 Point3 getPosition(const UV &uv); 00119 00120 /** 00121 * @returns the total surface area of this shape 00122 */ 00123 real_t getSurfaceArea() const; 00124 00125 00126 //@}----------------------------------------------------------------- 00127 }; 00128 00129 class MeshTriangleFast : public MeshTriangle { 00130 public: 00131 ///@name Constructors 00132 //@{----------------------------------------------------------------- 00133 00134 inline MeshTriangleFast() 00135 : MeshTriangle() 00136 { } 00137 00138 inline MeshTriangleFast(unsigned A_, unsigned B_, unsigned C_, 00139 unsigned nA_ = 0, unsigned nB_ = 0, unsigned nC_ = 0, 00140 unsigned tA_ = 0, unsigned tB_ = 0, unsigned tC_ = 0) 00141 : MeshTriangle(A_, B_, C_, 00142 nA_, nB_, nC_, 00143 tA_, tB_, tC_) 00144 { } 00145 00146 inline MeshTriangleFast(const MeshTriangle ©) 00147 : MeshTriangle(copy) 00148 { } 00149 00150 00151 //@}----------------------------------------------------------------- 00152 ///@name Core functionality 00153 //@{----------------------------------------------------------------- 00154 00155 virtual void init(); 00156 virtual real_t getIntersection(const Ray &ray, SurfacePoint &pt); 00157 00158 00159 //@}----------------------------------------------------------------- 00160 00161 private: 00162 /// Precomputed data for fast intersection using baryocentric coordinates 00163 real_t n_u, n_v, n_d; 00164 real_t b_u, b_v, b_d; 00165 real_t c_u, c_v, c_d; // 8 * 9 + 4 = 76 extra bytes per triangle :( 00166 unsigned n_k; 00167 }; 00168 00169 #endif // MESH_TRIANGLE_H_ 00170
Generated on 28 Feb 2009 for Milton by
1.5.6