Go to the documentation of this file.00001
00002 #include "polygon_mesh.h"
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <vcl_cstdio.h>
00013 #include <vcl_iostream.h>
00014 #include <vcl_cassert.h>
00015 #include <vnl/vnl_cross.h>
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 int PolygonMesh::add_vertex(DPoint &pt)
00028 {
00029 vertex_list.push_back(pt);
00030 return vertex_list.size()-1;
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 int PolygonMesh::set_vertex(int index, DPoint &pt)
00047 {
00048 assert(index>=0);
00049 if (vertex_list.capacity()<=(unsigned int)index)
00050 vertex_list.reserve(index+100);
00051 vertex_list[index] = pt;
00052 return index;
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 int PolygonMesh::add_face(Face &fc)
00067 {
00068 face_list.push_back(fc);
00069 return face_list.size()-1;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 int PolygonMesh::set_face(int index, Face &fc)
00085 {
00086 assert(index>=0);
00087 if (face_list.capacity()<=(unsigned int)index)
00088 face_list.reserve(index+100);
00089 face_list[index] = fc;
00090 return index;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 PolygonMesh::DPoint PolygonMesh::get_vertex(int index)
00105 {
00106 assert(index >= 0);
00107 if ((unsigned int)index>=vertex_list.size())
00108 {
00109 vcl_cerr << "Warning: vertex doesn't exist\n";
00110 return DPoint();
00111 }
00112 return vertex_list[index];
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 PolygonMesh::Polygon PolygonMesh::get_face(int index)
00127 {
00128 assert(index >= 0);
00129 Polygon poly;
00130 if ((unsigned int)index>=face_list.size())
00131 {
00132 vcl_cerr << "Warning: face doesn't exist\n";
00133 return poly;
00134 }
00135 Face& face = face_list[index];
00136 for (unsigned int i=0; i<face.size(); i++)
00137 poly.push_back(vertex_list[face[i]]);
00138 return poly;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 bool PolygonMesh::read_file(char *filename)
00154 {
00155 char start[100];
00156 FILE *fp=vcl_fopen(filename, "r");
00157 if (!fp) return false;
00158
00159 while (vcl_fscanf(fp, "%s", start)!=EOF)
00160 {
00161 if (!vcl_strcmp(start, "Vertex"))
00162 {
00163 int index;
00164 double x, y, z;
00165 int ret = vcl_fscanf(fp, "%d %lf %lf %lf", &index, &x, &y, &z); if (ret<4) return false;
00166
00167 DPoint pt(x,y,z);
00168
00169 set_vertex(index, pt);
00170 }
00171 else if (!vcl_strcmp(start, "Face"))
00172 {
00173 int index;
00174 int vertex;
00175 Face fc;
00176 int ret = vcl_fscanf(fp, "%d", &index); if (ret<1) return false;
00177
00178
00179 while ((vcl_fscanf(fp, "%d", &vertex)))
00180 {
00181
00182 fc.push_back(vertex);
00183 }
00184
00185 set_face(index, fc);
00186 }
00187 else if (!vcl_strcmp(start, "End"))
00188 break;
00189 else
00190 {
00191 char c;
00192 do {
00193 int ret = vcl_fscanf(fp, "%c", &c); if (ret<1) return false;
00194 } while (c!='\n');
00195 }
00196 }
00197
00198 return true;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 PolygonMesh::DVector3D PolygonMesh::get_face_normal
00218 (
00219 int face_index,
00220 int
00221 )
00222 {
00223 assert(face_index>=0);
00224 assert((unsigned int)face_index<face_list.size());
00225
00226
00227
00228 Polygon face = get_face(face_index);
00229 DVector3D v1(face[0].x()-face[1].x(), face[0].y()-face[1].y(),
00230 face[0].z()-face[1].z());
00231 DVector3D v2(face[0].x()-face[2].x(), face[0].y()-face[2].y(),
00232 face[0].z()-face[2].z());
00233 DVector3D cross = vnl_cross_3d(v1, v2);
00234 cross.normalize();
00235 return cross;
00236 }