00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "mesh3d/geometry.h"
00021 #include "mesh3d/point.h"
00022 #include "mesh3d/polygon.h"
00023 #include <cmath>
00024
00032 namespace mesh3d {
00033
00034 geometry::geometry()
00035 {
00036 }
00037
00038 geometry::~geometry()
00039 {
00040 for(
00041 polygon_collection::iterator i = polygons().begin();
00042 i != polygons().end();
00043 ++i
00044 )
00045 {
00046 polygon* pol = *i;
00047 delete pol;
00048 }
00049 for(
00050 point_collection::iterator i = points().begin();
00051 i != points().end();
00052 ++i
00053 )
00054 {
00055 point *pnt = *i;
00056 delete pnt;
00057 }
00058 }
00059
00060 void geometry::reserve_points(size_t point_count)
00061 {
00062 if(point_count > m_points.size())
00063 {
00064 m_points.reserve(point_count);
00065 }
00066 }
00067
00068 void geometry::reserve_polygons(size_t polygon_count)
00069 {
00070 if(polygon_count > m_polygons.size())
00071 {
00072 m_polygons.reserve(polygon_count);
00073 }
00074 }
00075
00076 point *geometry::make_point()
00077 {
00078 mesh3d::point *pnt = new mesh3d::point();
00079 m_points.push_back(pnt);
00080 return pnt;
00081 }
00082
00083 polygon *geometry::make_polygon()
00084 {
00085 mesh3d::polygon *pol = new mesh3d::polygon();
00086 m_polygons.push_back(pol);
00087 return pol;
00088 }
00089
00090 geometry::point_collection &geometry::points () { return m_points; }
00091 geometry::point_collection const &geometry::points () const { return m_points; }
00092 geometry::polygon_collection &geometry::polygons() { return m_polygons; }
00093 geometry::polygon_collection const &geometry::polygons() const { return m_polygons; }
00094
00095 void geometry::compute_polygon_normals()
00096 {
00097 std::tr1::shared_ptr<attribute_map<polygon*, vec3> > polygon_normals = polygon_attributes().find_or_create<vec3>("polygon_normals");
00098 std::tr1::shared_ptr<attribute_map<point*, vec3> > point_locations = point_attributes().find<vec3>("point_locations");
00099 for(
00100 polygon_collection::iterator i = polygons().begin();
00101 i != polygons().end();
00102 ++i
00103 )
00104 {
00105 polygon *pol = *i;
00106 pol->compute_normal(
00107 polygon_normals,
00108 point_locations
00109 );
00110 }
00111 }
00112 void geometry::compute_polygon_centroids()
00113 {
00114 std::tr1::shared_ptr<attribute_map<polygon*, vec3> > polygon_centroids = polygon_attributes().find_or_create<vec3>("polygon_centroids");
00115 std::tr1::shared_ptr<attribute_map<point*, vec3> > point_locations = point_attributes().find<vec3>("point_locations");
00116
00117 for(
00118 polygon_collection::iterator i = polygons().begin();
00119 i != polygons().end();
00120 ++i
00121 )
00122 {
00123 polygon *pol = *i;
00124 pol->compute_centroid(
00125 polygon_centroids,
00126 point_locations
00127 );
00128 }
00129 }
00130
00131 void geometry::compute_corner_normals(float max_smoothing_angle)
00132 {
00133 std::tr1::shared_ptr<attribute_map<corner*, vec3> > corner_normals = corner_attributes ().find_or_create<vec3>("corner_normals");
00134 std::tr1::shared_ptr<attribute_map<polygon*, vec3> > polygon_normals = polygon_attributes().find<vec3>("polygon_normals");
00135 std::tr1::shared_ptr<attribute_map<point*, vec3> > point_locations = point_attributes ().find<vec3>("point_locations");
00136 float cos_max_smoothing_angle = std::cos(max_smoothing_angle);
00137
00138 for(
00139 polygon_collection::iterator i = polygons().begin();
00140 i != polygons().end();
00141 ++i
00142 )
00143 {
00144 polygon *pol = *i;
00145
00146 if(max_smoothing_angle == 0)
00147 {
00148 pol->copy_polygon_normal_to_corners(
00149 corner_normals,
00150 polygon_normals
00151 );
00152 }
00153 else
00154 {
00155 pol->compute_corner_normals(
00156 corner_normals,
00157 polygon_normals,
00158 point_locations,
00159 cos_max_smoothing_angle
00160 );
00161 }
00162 }
00163 }
00164
00165 point *geometry::make_point(float x, float y, float z)
00166 {
00167 point *pnt = make_point();
00168 std::tr1::shared_ptr<attribute_map<point*, vec3> > point_positions =
00169 point_attributes().find_or_create<vec3>("point_locations");
00170
00171 point_positions->set_value(pnt, vec3(x, y, z));
00172
00173 return pnt;
00174 }
00175
00176 point *geometry::make_point(double x, double y, double z)
00177 {
00178 return make_point(float(x), float(y), float(z));
00179 }
00180
00181 polygon *geometry::make_polygon(point *p0)
00182 {
00183 polygon *p = make_polygon();
00184 p->make_corner(p0);
00185 return p;
00186 }
00187
00188 polygon *geometry::make_polygon(point *p0, point *p1)
00189 {
00190 polygon *p = make_polygon();
00191 p->make_corner(p0);
00192 p->make_corner(p1);
00193 return p;
00194 }
00195
00196 polygon *geometry::make_polygon(point *p0, point *p1, point *p2)
00197 {
00198 polygon *p = make_polygon();
00199 p->make_corner(p0);
00200 p->make_corner(p1);
00201 p->make_corner(p2);
00202 return p;
00203 }
00204
00205 polygon *geometry::make_polygon(point *p0, point *p1, point *p2, point *p3)
00206 {
00207 polygon *p = make_polygon();
00208 p->make_corner(p0);
00209 p->make_corner(p1);
00210 p->make_corner(p2);
00211 p->make_corner(p3);
00212 return p;
00213 }
00214
00215 polygon *geometry::make_polygon(point *p0, point *p1, point *p2, point *p3, point *p4)
00216 {
00217 polygon *p = make_polygon();
00218 p->make_corner(p0);
00219 p->make_corner(p1);
00220 p->make_corner(p2);
00221 p->make_corner(p3);
00222 p->make_corner(p4);
00223 return p;
00224 }
00225
00226 polygon *geometry::make_polygon(point *p0, point *p1, point *p2, point *p3, point *p4, point *p5)
00227 {
00228 polygon *p = make_polygon();
00229 p->make_corner(p0);
00230 p->make_corner(p1);
00231 p->make_corner(p2);
00232 p->make_corner(p3);
00233 p->make_corner(p4);
00234 p->make_corner(p5);
00235 return p;
00236 }
00237
00238 polygon *geometry::make_polygon(point *p0, point *p1, point *p2, point *p3, point *p4, point *p5, point *p6)
00239 {
00240 polygon *p = make_polygon();
00241 p->make_corner(p0);
00242 p->make_corner(p1);
00243 p->make_corner(p2);
00244 p->make_corner(p3);
00245 p->make_corner(p4);
00246 p->make_corner(p5);
00247 p->make_corner(p6);
00248 return p;
00249 }
00250
00251 polygon *geometry::make_polygon(point *p0, point *p1, point *p2, point *p3, point *p4, point *p5, point *p6, point *p7)
00252 {
00253 polygon *p = make_polygon();
00254 p->make_corner(p0);
00255 p->make_corner(p1);
00256 p->make_corner(p2);
00257 p->make_corner(p3);
00258 p->make_corner(p4);
00259 p->make_corner(p5);
00260 p->make_corner(p6);
00261 p->make_corner(p7);
00262 return p;
00263 }
00264
00265 polygon *geometry::make_polygon(point *p0, point *p1, point *p2, point *p3, point *p4, point *p5, point *p6, point *p7, point *p8)
00266 {
00267 polygon *p = make_polygon();
00268 p->make_corner(p0);
00269 p->make_corner(p1);
00270 p->make_corner(p2);
00271 p->make_corner(p3);
00272 p->make_corner(p4);
00273 p->make_corner(p5);
00274 p->make_corner(p6);
00275 p->make_corner(p7);
00276 p->make_corner(p8);
00277 return p;
00278 }
00279
00280 polygon *geometry::make_polygon(point *p0, point *p1, point *p2, point *p3, point *p4, point *p5, point *p6, point *p7, point *p8, point *p9)
00281 {
00282 polygon *p = make_polygon();
00283 p->make_corner(p0);
00284 p->make_corner(p1);
00285 p->make_corner(p2);
00286 p->make_corner(p3);
00287 p->make_corner(p4);
00288 p->make_corner(p5);
00289 p->make_corner(p6);
00290 p->make_corner(p7);
00291 p->make_corner(p8);
00292 p->make_corner(p9);
00293 return p;
00294 }
00295
00296 polygon *geometry::make_polygon(size_t p0)
00297 {
00298 polygon *p = make_polygon();
00299 p->make_corner(m_points[p0]);
00300 return p;
00301 }
00302
00303 polygon *geometry::make_polygon(size_t p0, size_t p1)
00304 {
00305 polygon *p = make_polygon();
00306 p->make_corner(m_points[p0]);
00307 p->make_corner(m_points[p1]);
00308 return p;
00309 }
00310
00311 polygon *geometry::make_polygon(size_t p0, size_t p1, size_t p2)
00312 {
00313 polygon *p = make_polygon();
00314 p->make_corner(m_points[p0]);
00315 p->make_corner(m_points[p1]);
00316 p->make_corner(m_points[p2]);
00317 return p;
00318 }
00319
00320 polygon *geometry::make_polygon(size_t p0, size_t p1, size_t p2, size_t p3)
00321 {
00322 polygon *p = make_polygon();
00323 p->make_corner(m_points[p0]);
00324 p->make_corner(m_points[p1]);
00325 p->make_corner(m_points[p2]);
00326 p->make_corner(m_points[p3]);
00327 return p;
00328 }
00329
00330 polygon *geometry::make_polygon(size_t p0, size_t p1, size_t p2, size_t p3, size_t p4)
00331 {
00332 polygon *p = make_polygon();
00333 p->make_corner(m_points[p0]);
00334 p->make_corner(m_points[p1]);
00335 p->make_corner(m_points[p2]);
00336 p->make_corner(m_points[p3]);
00337 p->make_corner(m_points[p4]);
00338 return p;
00339 }
00340
00341 polygon *geometry::make_polygon(size_t p0, size_t p1, size_t p2, size_t p3, size_t p4, size_t p5)
00342 {
00343 polygon *p = make_polygon();
00344 p->make_corner(m_points[p0]);
00345 p->make_corner(m_points[p1]);
00346 p->make_corner(m_points[p2]);
00347 p->make_corner(m_points[p3]);
00348 p->make_corner(m_points[p4]);
00349 p->make_corner(m_points[p5]);
00350 return p;
00351 }
00352
00353 polygon *geometry::make_polygon(size_t p0, size_t p1, size_t p2, size_t p3, size_t p4, size_t p5, size_t p6)
00354 {
00355 polygon *p = make_polygon();
00356 p->make_corner(m_points[p0]);
00357 p->make_corner(m_points[p1]);
00358 p->make_corner(m_points[p2]);
00359 p->make_corner(m_points[p3]);
00360 p->make_corner(m_points[p4]);
00361 p->make_corner(m_points[p5]);
00362 p->make_corner(m_points[p6]);
00363 return p;
00364 }
00365
00366 polygon *geometry::make_polygon(size_t p0, size_t p1, size_t p2, size_t p3, size_t p4, size_t p5, size_t p6, size_t p7)
00367 {
00368 polygon *p = make_polygon();
00369 p->make_corner(m_points[p0]);
00370 p->make_corner(m_points[p1]);
00371 p->make_corner(m_points[p2]);
00372 p->make_corner(m_points[p3]);
00373 p->make_corner(m_points[p4]);
00374 p->make_corner(m_points[p5]);
00375 p->make_corner(m_points[p6]);
00376 p->make_corner(m_points[p7]);
00377 return p;
00378 }
00379
00380 polygon *geometry::make_polygon(size_t p0, size_t p1, size_t p2, size_t p3, size_t p4, size_t p5, size_t p6, size_t p7, size_t p8)
00381 {
00382 polygon *p = make_polygon();
00383 p->make_corner(m_points[p0]);
00384 p->make_corner(m_points[p1]);
00385 p->make_corner(m_points[p2]);
00386 p->make_corner(m_points[p3]);
00387 p->make_corner(m_points[p4]);
00388 p->make_corner(m_points[p5]);
00389 p->make_corner(m_points[p6]);
00390 p->make_corner(m_points[p7]);
00391 p->make_corner(m_points[p8]);
00392 return p;
00393 }
00394
00395 polygon *geometry::make_polygon(size_t p0, size_t p1, size_t p2, size_t p3, size_t p4, size_t p5, size_t p6, size_t p7, size_t p8, size_t p9)
00396 {
00397 polygon *p = make_polygon();
00398 p->make_corner(m_points[p0]);
00399 p->make_corner(m_points[p1]);
00400 p->make_corner(m_points[p2]);
00401 p->make_corner(m_points[p3]);
00402 p->make_corner(m_points[p4]);
00403 p->make_corner(m_points[p5]);
00404 p->make_corner(m_points[p6]);
00405 p->make_corner(m_points[p7]);
00406 p->make_corner(m_points[p8]);
00407 p->make_corner(m_points[p9]);
00408 return p;
00409 }
00410
00411 }
00412