00001 /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com) 00002 All rights reserved. 00003 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 00006 00007 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 00008 00009 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 00010 00011 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00014 */ 00015 #pragma once 00016 #ifndef VHACD_MESH_H 00017 #define VHACD_MESH_H 00018 #include "vhacdSArray.h" 00019 #include "vhacdVector.h" 00020 00021 #define VHACD_DEBUG_MESH 00022 00023 namespace VHACD { 00024 enum AXIS { 00025 AXIS_X = 0, 00026 AXIS_Y = 1, 00027 AXIS_Z = 2 00028 }; 00029 struct Plane { 00030 double m_a; 00031 double m_b; 00032 double m_c; 00033 double m_d; 00034 AXIS m_axis; 00035 short m_index; 00036 }; 00037 #ifdef VHACD_DEBUG_MESH 00038 struct Material { 00039 00040 Vec3<double> m_diffuseColor; 00041 double m_ambientIntensity; 00042 Vec3<double> m_specularColor; 00043 Vec3<double> m_emissiveColor; 00044 double m_shininess; 00045 double m_transparency; 00046 Material(void) 00047 { 00048 m_diffuseColor.X() = 0.5; 00049 m_diffuseColor.Y() = 0.5; 00050 m_diffuseColor.Z() = 0.5; 00051 m_specularColor.X() = 0.5; 00052 m_specularColor.Y() = 0.5; 00053 m_specularColor.Z() = 0.5; 00054 m_ambientIntensity = 0.4; 00055 m_emissiveColor.X() = 0.0; 00056 m_emissiveColor.Y() = 0.0; 00057 m_emissiveColor.Z() = 0.0; 00058 m_shininess = 0.4; 00059 m_transparency = 0.0; 00060 }; 00061 }; 00062 #endif // VHACD_DEBUG_MESH 00063 00065 class Mesh { 00066 public: 00067 void AddPoint(const Vec3<double>& pt) { m_points.PushBack(pt); }; 00068 void SetPoint(size_t index, const Vec3<double>& pt) { m_points[index] = pt; }; 00069 const Vec3<double>& GetPoint(size_t index) const { return m_points[index]; }; 00070 Vec3<double>& GetPoint(size_t index) { return m_points[index]; }; 00071 size_t GetNPoints() const { return m_points.Size(); }; 00072 double* GetPoints() { return (double*)m_points.Data(); } // ugly 00073 const double* const GetPoints() const { return (double*)m_points.Data(); } // ugly 00074 const Vec3<double>* const GetPointsBuffer() const { return m_points.Data(); } // 00075 Vec3<double>* const GetPointsBuffer() { return m_points.Data(); } // 00076 void AddTriangle(const Vec3<int32_t>& tri) { m_triangles.PushBack(tri); }; 00077 void SetTriangle(size_t index, const Vec3<int32_t>& tri) { m_triangles[index] = tri; }; 00078 const Vec3<int32_t>& GetTriangle(size_t index) const { return m_triangles[index]; }; 00079 Vec3<int32_t>& GetTriangle(size_t index) { return m_triangles[index]; }; 00080 size_t GetNTriangles() const { return m_triangles.Size(); }; 00081 int32_t* GetTriangles() { return (int32_t*)m_triangles.Data(); } // ugly 00082 const int32_t* const GetTriangles() const { return (int32_t*)m_triangles.Data(); } // ugly 00083 const Vec3<int32_t>* const GetTrianglesBuffer() const { return m_triangles.Data(); } 00084 Vec3<int32_t>* const GetTrianglesBuffer() { return m_triangles.Data(); } 00085 const Vec3<double>& GetCenter() const { return m_center; } 00086 const Vec3<double>& GetMinBB() const { return m_minBB; } 00087 const Vec3<double>& GetMaxBB() const { return m_maxBB; } 00088 void ClearPoints() { m_points.Clear(); } 00089 void ClearTriangles() { m_triangles.Clear(); } 00090 void Clear() 00091 { 00092 ClearPoints(); 00093 ClearTriangles(); 00094 } 00095 void ResizePoints(size_t nPts) { m_points.Resize(nPts); } 00096 void ResizeTriangles(size_t nTri) { m_triangles.Resize(nTri); } 00097 void CopyPoints(SArray<Vec3<double> >& points) const { points = m_points; } 00098 double GetDiagBB() const { return m_diag; } 00099 double ComputeVolume() const; 00100 void ComputeConvexHull(const double* const pts, 00101 const size_t nPts); 00102 void Clip(const Plane& plane, 00103 SArray<Vec3<double> >& positivePart, 00104 SArray<Vec3<double> >& negativePart) const; 00105 bool IsInside(const Vec3<double>& pt) const; 00106 double ComputeDiagBB(); 00107 Vec3<double> &ComputeCenter(void); 00108 00109 #ifdef VHACD_DEBUG_MESH 00110 bool LoadOFF(const std::string& fileName, bool invert); 00111 bool SaveVRML2(const std::string& fileName) const; 00112 bool SaveVRML2(std::ofstream& fout, const Material& material) const; 00113 bool SaveOFF(const std::string& fileName) const; 00114 #endif // VHACD_DEBUG_MESH 00115 00117 Mesh(); 00119 ~Mesh(void); 00120 00121 private: 00122 SArray<Vec3<double> > m_points; 00123 SArray<Vec3<int32_t> > m_triangles; 00124 Vec3<double> m_minBB; 00125 Vec3<double> m_maxBB; 00126 Vec3<double> m_center; 00127 double m_diag; 00128 }; 00129 } 00130 #endif