$search
00001 #ifndef TRIMESH_H 00002 #define TRIMESH_H 00003 00004 /* 00005 Szymon Rusinkiewicz 00006 Stanford Graphics Lab 00007 00008 trimesh.h 00009 A triangle mesh, including various properties... 00010 00011 Note that most of this stuff is not allocated when the object is first created, 00012 so if you want to use anything besides vertices and faces, you need to call 00013 need_normals() or whatever... 00014 */ 00015 00016 #include "triutil.h" 00017 #include <vector> 00018 using std::vector; 00019 00020 namespace trimesh { 00021 00022 // Miscellaneous data types 00023 typedef unsigned char color[3]; 00024 typedef float conf; 00025 typedef float point[3]; 00026 typedef float vec[3]; 00027 typedef int face[3]; 00028 typedef float quat[4]; 00029 typedef int edge[2]; 00030 00031 // The TriMesh class 00032 class TriMesh { 00033 public: 00034 // Vertices and per-vertex properties 00035 int numvertices; 00036 point *vertices; 00037 point *verticesorig; 00038 point *verticesref; 00039 color *colors; 00040 conf *confidences; 00041 vec *normals; 00042 float *localpotentials; 00043 vec *planeparams; 00044 vec *facemeans; 00045 vec *planeparamsorig; 00046 int *faceregions; 00047 int numfaceregions; 00048 int *faceregionredirect; 00049 int *faceregionpopulations; 00050 vec *faceregionnormals; 00051 float *faceregionprojdist; 00052 float *faceregionsumdev2; 00053 float *areas; 00054 float minarea,maxarea,meanarea; 00055 00056 // Faces and tstrips 00057 int numfaces; 00058 face *faces; 00059 vec *facenormals; 00060 vec *facenormalsorig; 00061 vec *facenormalsref; 00062 typedef float *facekappalist; 00063 facekappalist *facekappas; 00064 typedef int *facecenterlist; 00065 facecenterlist *facecenters; 00066 00067 int tstripdatalen; 00068 int *tstrips; 00069 00070 // Bounding box and sphere 00071 struct BBox { 00072 float xmin, xmax, ymin, ymax, zmin, zmax; 00073 float xlen, ylen, zlen; 00074 }; 00075 BBox *bbox; 00076 00077 struct BSphere { 00078 point center; 00079 float r; 00080 }; 00081 BSphere *bsphere; 00082 00083 // Connectivity information - neighbors and vertex->face mappings 00084 typedef int *neighborlist; 00085 neighborlist *neighbors; 00086 int *numneighbors; 00087 int *onedge; 00088 int *outlier; 00089 int numoutliers; 00090 point zero_point; 00091 int minneighbors; 00092 int maxneighbors; 00093 00094 typedef int *adjacentfacelist; 00095 adjacentfacelist *adjacentfaces; 00096 typedef int *neighboringfacelist; 00097 neighboringfacelist *neighboringfaces; 00098 typedef edge *neighboringedgelist; 00099 neighboringedgelist *neighboringedges; 00100 typedef float *neighboringedgelengthlist; 00101 neighboringedgelengthlist *neighboringedgelengths; 00102 00103 int *numneighboringfaces; 00104 int *numadjacentfaces; 00105 int minadjacentfaces; 00106 int maxadjacentfaces; 00107 00108 // Edge lengths 00109 void transform(double M[4][4]); 00110 void scale(double sx, double sy, double sz); 00111 float minedgelength(); 00112 float maxedgelength(); 00113 float rmsedgelength(bool random_sample = true); 00114 float meanedgelength(bool random_sample = true); 00115 float medianedgelength(bool random_sample = true); 00116 float percentileedgelength(float percentile = 0.5, bool random_sample = true); 00117 00118 00119 // Smooth per-vertex normals 00120 void SmoothNormals(float smooth); 00121 00122 // Remove vertices and/or faces 00123 void RemoveVertices(const vector<bool> &toremove); 00124 void RemoveUnusedVertices(); 00125 void RemoveFaces(const vector<bool> &toremove); 00126 00127 // .ply file input and output 00128 static bool IsPlyFile(const char *plyfile); 00129 static TriMesh *ReadPly(const char *plyfile); 00130 void WritePly(const char *plyfile); 00131 00132 private: 00133 // Private functions to compute various stuff 00134 void FindBBox(); 00135 void FindBSphere(); 00136 void FindTStrips(); 00137 void UnpackTStrips(); 00138 int Tstrip_Next_Tri(int tri, int v1, int v2, bool flip); 00139 int Tstrip_Next_Vert(int next, int vlast1, int vlast2); 00140 void Tstrip_Crawl(int v1, int v2, int v3, int next); 00141 void Tstrip_Bootstrap(int tri); 00142 00143 public: 00144 // The public interface to the above: checks whether 00145 // the needed info already exists and computes it if it doesn't 00146 void FindNormals(); 00147 void FindAreas(); 00148 void FindNeighbors(); 00149 void FindAdjacentFaces(); 00150 void need_faces() { if (!faces && tstrips) UnpackTStrips(); } 00151 void need_normals() { if (!normals) FindNormals(); } 00152 void need_neighbors() { if (!neighbors) FindNeighbors(); } 00153 void need_adjacentfaces() { if (!adjacentfaces) FindAdjacentFaces(); } 00154 void need_bbox() { if (!bbox) FindBBox(); } 00155 void need_bsphere() { if (!bsphere) FindBSphere(); } 00156 void need_tstrips() { if (!tstrips) FindTStrips(); } 00157 void need_colors() { if (!colors && numvertices) colors = new color[numvertices]; } 00158 void need_confidences() { if (!confidences && numvertices) confidences = new conf[numvertices]; } 00159 00160 // The counterparts to the above - free the storage associated with 00161 // various quantities 00162 void free_confidences() { if (confidences) { delete [] confidences; confidences = NULL; } } 00163 void free_colors() { if (colors) { delete [] colors; colors = NULL; } } 00164 void free_tstrips() 00165 { 00166 if (tstrips) { 00167 delete [] tstrips; 00168 tstrips = NULL; 00169 } 00170 tstripdatalen = 0; 00171 } 00172 void free_bsphere() { if (bsphere) { delete bsphere; bsphere = NULL; } } 00173 void free_bbox() { if (bbox) { delete bbox; bbox = NULL; } } 00174 void free_adjacentfaces() 00175 { 00176 int i; 00177 if (adjacentfaces) { 00178 for (i=0; i < numvertices; i++) 00179 delete [] adjacentfaces[i]; 00180 delete [] adjacentfaces; 00181 for (i=0;i<numfaces;i++) { 00182 delete [] neighboringfaces[i]; 00183 delete [] facekappas[i]; 00184 delete [] facecenters[i]; 00185 delete [] neighboringedges[i]; 00186 delete [] neighboringedgelengths[i]; 00187 } 00188 delete [] facekappas; 00189 delete [] facecenters; 00190 delete [] neighboringfaces; 00191 delete [] neighboringedges; 00192 delete [] neighboringedgelengths; 00193 delete [] numadjacentfaces; 00194 delete [] numneighboringfaces; 00195 adjacentfaces = NULL; 00196 } 00197 minadjacentfaces = maxadjacentfaces = 0; 00198 } 00199 void free_neighbors() 00200 { 00201 if (neighbors) { 00202 for (int i=0; i < numvertices; i++) { 00203 delete [] neighbors[i]; 00204 } 00205 delete [] neighbors; 00206 delete [] numneighbors; 00207 delete [] onedge; 00208 delete [] outlier; 00209 neighbors = NULL; 00210 } 00211 minneighbors = maxneighbors = 0; 00212 } 00213 void free_normals() { 00214 if (normals) { 00215 delete [] normals; 00216 normals = NULL; 00217 delete [] areas; 00218 delete [] planeparams; 00219 planeparams = NULL; 00220 delete [] planeparamsorig; 00221 planeparamsorig = NULL; 00222 delete [] facenormals; 00223 facenormals = NULL; 00224 delete [] facenormalsorig; 00225 facenormalsorig = NULL; 00226 delete [] facemeans; 00227 facemeans = NULL; 00228 delete [] facenormalsref; 00229 delete [] faceregions; 00230 delete [] faceregionredirect; 00231 delete [] faceregionnormals; 00232 delete [] faceregionprojdist; 00233 delete [] faceregionpopulations; 00234 delete [] faceregionsumdev2; 00235 } 00236 } 00237 void free_faces() 00238 { 00239 if (faces) { 00240 delete [] faces; 00241 faces = NULL; 00242 } 00243 numfaces = 0; 00244 } 00245 void free_vertices() 00246 { 00247 if (vertices) { 00248 delete [] vertices; 00249 delete [] verticesorig; 00250 delete [] verticesref; 00251 delete [] localpotentials; 00252 vertices = NULL; 00253 } 00254 numvertices = 0; 00255 } 00256 00257 // Constructor and destructor 00258 TriMesh(int _numvertices = 0, int _numfaces = 0) : 00259 numvertices(_numvertices), vertices(NULL), 00260 numfaces(_numfaces), faces(NULL), 00261 normals(NULL), 00262 neighbors(NULL), numneighbors(NULL), 00263 minneighbors(0), maxneighbors(0), 00264 adjacentfaces(NULL), numadjacentfaces(NULL), 00265 minadjacentfaces(0), maxadjacentfaces(0), 00266 bbox(NULL), bsphere(NULL), 00267 tstrips(NULL), tstripdatalen(0), 00268 colors(NULL), confidences(NULL) 00269 { 00270 zero_point[0]=0;zero_point[1]=0;zero_point[2]=0; 00271 if (numvertices) { 00272 vertices = new point[numvertices]; 00273 verticesorig = new point[numvertices]; 00274 verticesref = new point[numvertices]; 00275 localpotentials = new float[numvertices]; 00276 } 00277 if (numfaces) { 00278 faces = new face[numfaces]; 00279 } 00280 } 00281 00282 ~TriMesh() 00283 { 00284 free_confidences(); 00285 free_colors(); 00286 free_tstrips(); 00287 free_bsphere(); 00288 free_bbox(); 00289 free_adjacentfaces(); 00290 free_neighbors(); 00291 free_normals(); 00292 free_faces(); 00293 free_vertices(); 00294 } 00295 }; 00296 00297 } // namespace trimesh 00298 00299 #endif