!B (Sandbox) (CE-21795) Importing models with multisubmaterials via fbx switches...
[CRYENGINE.git] / Code / Sandbox / Plugins / LodGeneratorPlugin / Util / TopologyGraph.h
blobdd6b39ac516cb40c2baa6b854588f035d3a2c97a
1 #pragma once
2 #pragma warning( disable:4005)
4 namespace LODGenerator
6 class CHalfedge;
7 class CFacet;
8 class CVertex
10 public:
11 CVertex() : halfedge_(NULL) { }
12 CVertex(const Vec3d& p) : halfedge_(NULL), point_(p) { }
13 ~CVertex() { halfedge_ = NULL; }
15 const Vec3d& point() const { return point_; }
16 Vec3d& point() { return point_; }
17 void set_point(const Vec3d& p) { point_ = p; }
19 CHalfedge* halfedge() const { return halfedge_; }
21 bool is_valid() const;
22 void assert_is_valid() const;
24 int degree() const;
25 bool is_on_border() const;
27 Vec2d vertex_barycenter2d();
28 Vec3d vertex_normal();
30 public:
31 void set_halfedge(CHalfedge* h) { halfedge_ = h; }
33 private:
34 CHalfedge* halfedge_;
35 Vec3d point_;
39 class CTexVertex
41 public:
42 CTexVertex(){}
43 CTexVertex(const CTexVertex* rhs) : tex_coord_(rhs->tex_coord()) {}
44 ~CTexVertex() { }
45 const Vec2d& tex_coord() const { return tex_coord_; }
46 Vec2d& tex_coord() { return tex_coord_; }
47 void set_tex_coord(const Vec2d& p) { tex_coord_ = p; }
49 const Vec3d& normal() const { return normal_; }
50 Vec3d& normal() { return normal_; }
51 void set_normal(const Vec3d& p) { normal_ = p; }
53 const Vec3d& binormal() const { return binormal_; }
54 Vec3d& binormal() { return binormal_; }
55 void set_binormal(const Vec3d& p) { binormal_ = p; }
57 const Vec3d& tangent() const { return tangent_; }
58 Vec3d& tangent() { return tangent_; }
59 void set_tangent(const Vec3d& p) { tangent_ = p; }
61 private:
62 Vec2d tex_coord_;
63 Vec3d normal_;
64 Vec3d binormal_;
65 Vec3d tangent_;
68 class CHalfedge
70 public:
71 CHalfedge() :
72 opposite_(NULL), next_(NULL),
73 prev_(NULL), facet_(NULL), vertex_(NULL) ,tex_vertex_(NULL){
75 ~CHalfedge()
77 opposite_ = NULL; next_ = NULL;
78 prev_ = NULL; facet_ = NULL; vertex_ = NULL;
81 std::shared_ptr<CTexVertex> tex_vertex() const { return tex_vertex_; }
83 const Vec2d& tex_coord() const
85 return tex_vertex()->tex_coord();
88 Vec2d& tex_coord()
90 return tex_vertex()->tex_coord();
93 const Vec3d& normal() const
95 return tex_vertex()->normal();
98 Vec3d& normal()
100 return tex_vertex()->normal();
103 const Vec3d& tangent() const
105 return tex_vertex()->tangent();
108 Vec3d& tangent()
110 return tex_vertex()->tangent();
113 const Vec3d& binormal() const
115 return tex_vertex()->binormal();
118 Vec3d& binormal()
120 return tex_vertex()->binormal();
123 void set_tex_coord(const Vec2d& tex_coord_in)
125 tex_vertex()->set_tex_coord(tex_coord_in);
128 void set_normal(const Vec3d& normal_in)
130 tex_vertex()->set_normal(normal_in);
133 void set_tangent(const Vec3d& tangent_in)
135 tex_vertex()->set_tangent(tangent_in);
138 void set_binormal(const Vec3d& binormal_in)
140 tex_vertex()->set_binormal(binormal_in);
143 CHalfedge* opposite() const { return opposite_; }
144 CHalfedge* next() const { return next_; }
145 CHalfedge* prev() const { return prev_; }
146 CHalfedge* next_around_vertex() const {
147 return opposite()->prev();
149 CHalfedge* prev_around_vertex() const {
150 return next()->opposite();
153 Vec3d direct() const
155 return vertex()->point() - prev()->vertex()->point();
158 CFacet* facet() const { return facet_; }
159 CVertex* vertex() const { return vertex_; }
160 bool is_border() const { return facet_ == NULL; }
161 bool is_border_edge() const {
162 return is_border() || opposite()->is_border();
165 bool is_facet_key() const;
167 bool is_vertex_key() const;
169 bool is_edge_key() const;
170 CHalfedge* edge_key() const {
171 return is_edge_key() ? const_cast<CHalfedge*>(this) : opposite();
174 bool is_valid() const;
175 void assert_is_valid() const;
177 public:
178 void set_opposite(CHalfedge* h) { opposite_ = h; }
179 void set_next(CHalfedge* h) { next_ = h; }
180 void set_prev(CHalfedge* h) { prev_ = h; }
181 void set_facet(CFacet* f) { facet_ = f; }
182 void set_vertex(CVertex* v) { vertex_ = v; }
183 void set_tex_vertex(std::shared_ptr<CTexVertex> t) { tex_vertex_ = t; }
185 private:
186 CHalfedge* opposite_;
187 CHalfedge* next_;
188 CHalfedge* prev_;
189 CFacet* facet_;
190 CVertex* vertex_;
191 //TexVertex* tex_vertex_;
192 std::shared_ptr<CTexVertex> tex_vertex_;
196 class CFacet
198 public:
199 CFacet() : halfedge_(NULL) { }
200 ~CFacet() { halfedge_ = NULL; }
201 CHalfedge* halfedge() const { return halfedge_; }
202 int degree() const;
203 int nb_edges() const { return degree(); }
204 int nb_vertices() const { return degree(); }
205 bool is_on_border() const;
206 bool is_triangle() const;
207 bool is_valid() const;
208 void assert_is_valid() const;
209 Vec3d facet_normal();
210 double facet_area();
211 Vec3d facet_barycenter();
212 double facet_signed_area2d();
213 double facet_area2d();
215 public:
216 void set_halfedge(CHalfedge* h) { halfedge_ = h; }
218 private:
219 CHalfedge* halfedge_;
222 class CTopologyGraph
224 public:
226 typedef std::vector<CVertex*>::iterator Vertex_iterator;
227 typedef std::vector<CHalfedge*>::iterator Halfedge_iterator;
228 typedef std::vector<CFacet*>::iterator Facet_iterator;
230 typedef std::vector<CVertex*>::const_iterator Vertex_const_iterator;
231 typedef std::vector<CHalfedge*>::const_iterator Halfedge_const_iterator;
232 typedef std::vector<CFacet*>::const_iterator Facet_const_iterator;
234 CTopologyGraph();
235 virtual ~CTopologyGraph();
237 Vertex_iterator vertices_begin() { return vertices_.begin(); }
238 Vertex_iterator vertices_end() { return vertices_.end(); }
239 Halfedge_iterator halfedges_begin() { return halfedges_.begin(); }
240 Halfedge_iterator halfedges_end() { return halfedges_.end(); }
241 Facet_iterator facets_begin() { return facets_.begin(); }
242 Facet_iterator facets_end() { return facets_.end(); }
244 Vertex_const_iterator vertices_begin() const {
245 return vertices_.begin();
248 Vertex_const_iterator vertices_end() const {
249 return vertices_.end();
252 Halfedge_const_iterator halfedges_begin() const {
253 return halfedges_.begin();
255 Halfedge_const_iterator halfedges_end() const {
256 return halfedges_.end();
258 Facet_const_iterator facets_begin() const {
259 return facets_.begin();
261 Facet_const_iterator facets_end() const {
262 return facets_.end();
265 int size_of_vertices() const { return vertices_.size(); }
266 int size_of_halfedges() const { return halfedges_.size(); }
267 int size_of_facets() const { return facets_.size(); }
269 double map_area();
271 void clear();
272 void erase_all();
274 void compue_vetex_minmax(Vec3d& vmin,Vec3d& vmax,Vec3d vcenter,float& vradius);
276 void compute_vertex_tangent(bool smooth = true);
278 void compute_normals();
279 void compute_vertex_normals(bool smooth = true);
280 void compute_vertex_normal(CVertex* v);
282 bool is_triangulated() const;
284 bool is_valid() const;
286 void assert_is_valid() const;
288 AABB bbox();
290 void update_from_map(std::map<CVertex*,CVertex*>& orig_vertex);
292 public:
293 CHalfedge* new_edge();
294 void delete_edge(CHalfedge* h);
296 CVertex* new_vertex();
297 CTexVertex* new_tex_vertex();
298 CHalfedge* new_halfedge();
299 CFacet* new_facet();
301 CVertex* new_vertex(const CVertex* rhs);
302 CTexVertex* new_tex_vertex(const CTexVertex* rhs);
303 CHalfedge* new_halfedge(const CHalfedge* rhs);
304 CFacet* new_facet(const CFacet* rhs);
306 void delete_vertex(CVertex* v);
307 void delete_halfedge(CHalfedge* h);
308 void delete_facet(CFacet* f);
309 void delete_tex_vertex(CTexVertex* tv);
311 protected:
312 void reorient_facet( CHalfedge* first);
315 protected:
316 void notify_add_vertex(CVertex* v);
317 void notify_remove_vertex(CVertex* v);
318 void notify_add_halfedge(CHalfedge* h);
319 void notify_remove_halfedge(CHalfedge* h);
320 void notify_add_facet(CFacet* f);
321 void notify_remove_facet(CFacet* f);
323 private:
324 std::vector<CVertex*> vertices_;
325 std::vector<CHalfedge*> halfedges_;
326 std::vector<CFacet*> facets_;
329 class CTopologyGraphMutator
331 public:
333 typedef CTopologyGraph::Vertex_iterator Vertex_iterator;
334 typedef CTopologyGraph::Halfedge_iterator Halfedge_iterator;
335 typedef CTopologyGraph::Facet_iterator Facet_iterator;
337 typedef CTopologyGraph::Vertex_const_iterator Vertex_const_iterator;
338 typedef CTopologyGraph::Halfedge_const_iterator Halfedge_const_iterator;
339 typedef CTopologyGraph::Facet_const_iterator Facet_const_iterator;
341 CTopologyGraphMutator(CTopologyGraph* target = NULL) : target_(target) { }
342 virtual ~CTopologyGraphMutator();
343 CTopologyGraph* target() const { return target_; }
344 virtual void set_target(CTopologyGraph* m);
347 bool can_unglue(CHalfedge* h);
348 bool unglue(CHalfedge* h0, bool check);
350 protected:
353 void set_vertex_on_orbit(CHalfedge* h, CVertex* v);
354 void set_tex_vertex_on_orbit(CHalfedge* h, std::shared_ptr<CTexVertex> tv);
355 void set_facet_on_orbit(CHalfedge* h, CFacet* f);
357 void make_facet_key(CHalfedge* h, CFacet* f) {
358 f->set_halfedge(h);
359 h->set_facet(f);
363 CHalfedge* new_edge() { return target_->new_edge(); }
364 void delete_edge(CHalfedge* h) { target_->delete_edge(h); }
366 CVertex* new_vertex() { return target_->new_vertex(); }
367 CTexVertex* new_tex_vertex() { return target_->new_tex_vertex(); }
368 CHalfedge* new_halfedge() { return target_->new_halfedge(); }
369 CFacet* new_facet() { return target_->new_facet(); }
371 CVertex* new_vertex(const CVertex* rhs) {
372 return target_->new_vertex(rhs);
374 CTexVertex* new_tex_vertex(const CTexVertex* rhs) {
375 return target_->new_tex_vertex(rhs);
377 CHalfedge* new_halfedge(const CHalfedge* rhs) {
378 return target_->new_halfedge(rhs);
380 CHalfedge* new_edge(CHalfedge* rhs);
381 CFacet* new_facet(const CFacet* rhs) {
382 return target_->new_facet(rhs);
386 void delete_vertex(CVertex* v) { target_->delete_vertex(v); }
387 void delete_tex_vertex(CTexVertex* tv) {
388 target_->delete_tex_vertex(tv);
390 void delete_halfedge(CHalfedge* h) {
391 target_->delete_halfedge(h);
393 void delete_facet(CFacet* f) { target_->delete_facet(f); }
395 public:
397 void remove_null_face(CHalfedge* f0);
398 bool can_collapse_edge(CHalfedge* h);
399 bool collapse_edge(CHalfedge* h);
402 static void set_vertex_halfedge(CVertex* v, CHalfedge* h)
404 v->set_halfedge(h);
407 static void set_halfedge_opposite(CHalfedge* h1, CHalfedge* h2)
409 h1->set_opposite(h2);
412 static void set_halfedge_next(CHalfedge* h1, CHalfedge* h2)
414 h1->set_next(h2);
417 static void set_halfedge_prev(CHalfedge* h1, CHalfedge* h2)
419 h1->set_prev(h2);
422 static void set_halfedge_facet(CHalfedge* h, CFacet* f)
424 h->set_facet(f);
427 static void set_halfedge_vertex(CHalfedge* h, CVertex* v)
429 h->set_vertex(v);
432 static void set_facet_halfedge(CFacet* f, CHalfedge* h)
434 f->set_halfedge(h);
437 static void set_halfedge_tex_vertex(CHalfedge* h, std::shared_ptr<CTexVertex> t)
439 h->set_tex_vertex(t);
442 static void link(CHalfedge* h1, CHalfedge* h2, int dim);
444 static void make_vertex_key(CHalfedge* h)
446 h->vertex()->set_halfedge(h);
449 static void make_vertex_key(CHalfedge* h, CVertex* v) {
451 v->set_halfedge(h);
452 h->set_vertex(v);
455 static void make_facet_key(CHalfedge* h)
457 h->facet()->set_halfedge(h);
460 void merge_tex_vertices(const CVertex* v_in)
462 CVertex* v = const_cast<CVertex*>(v_in) ;
463 Vec2d u = v->vertex_barycenter2d() ;
464 set_tex_vertex_on_orbit(v-> halfedge(), std::shared_ptr<CTexVertex>(new_tex_vertex())) ;
465 v->halfedge()->set_tex_coord(u) ;
470 private:
471 CTopologyGraph* target_;
474 #define FOR_EACH_VERTEX(T,map,it) \
475 for( \
476 T::Vertex_iterator it=(map)->vertices_begin(); \
477 it!=(map)->vertices_end(); it++ \
480 #define FOR_EACH_HALFEDGE(T,map,it) \
481 for( \
482 T::Halfedge_iterator it=(map)->halfedges_begin(); \
483 it!=(map)->halfedges_end(); it++ \
486 #define FOR_EACH_EDGE(T,map,it) \
487 for( \
488 T::Halfedge_iterator it=(map)->halfedges_begin(); \
489 it!=(map)->halfedges_end(); it++ \
490 ) if((*it)->is_edge_key()) \
493 #define FOR_EACH_FACET(T,map,it) \
494 for( \
495 T::Facet_iterator it=(map)->facets_begin(); \
496 it!=(map)->facets_end(); it++ \
499 #define FOR_EACH_VERTEX_CONST(T,map,it) \
500 for( \
501 T::Vertex_const_iterator it=map->vertices_begin(); \
502 it!=map->vertices_end(); it++ \
505 #define FOR_EACH_HALFEDGE_CONST(T,map,it) \
506 for( \
507 T::Halfedge_const_iterator it=map->halfedges_begin(); \
508 it!=map->halfedges_end(); it++ \
511 #define FOR_EACH_EDGE_CONST(T,map,it) \
512 for( \
513 T::Halfedge_const_iterator it=(map)->halfedges_begin(); \
514 it!=(map)->halfedges_end(); it++ \
515 ) if(it->is_edge_key()) \
518 #define FOR_EACH_FACET_CONST(T,map,it) \
519 for( \
520 T::Facet_const_iterator it=map->facets_begin(); \
521 it!=map->facets_end(); it++ \