Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/include/objMeshOrientable.h

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "objMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC *
6 * All rights reserved. *
7 * *
8 * Code authors: Jernej Barbic, Christopher Twigg, Daniel Schroeder *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, *
12 * Doug L. James, Jovan Popovic *
13 * *
14 * Funding: National Science Foundation, Link Foundation, *
15 * Singapore-MIT GAMBIT Game Lab, *
16 * Zumberge Research and Innovation Fund at USC *
17 * *
18 * This library is free software; you can redistribute it and/or *
19 * modify it under the terms of the BSD-style license that is *
20 * included with this library in the file LICENSE.txt *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
25 * LICENSE.TXT for more details. *
26 * *
27 *************************************************************************/
28
29 #ifndef _OBJMESHORIENTABLE_H_
30 #define _OBJMESHORIENTABLE_H_
31
32 #include "objMesh.h"
33
34 /*
35 Generates a half-edge datastructure, assuming tha the given obj mesh is orientable.
36 Author: Jernej Barbic, 2004
37 */
38
39 class ObjMeshOrientable
40 {
41 public:
42 // generates the half-edge datastructure
43 // if generateHalfEdges flag is 1 (default), it also generates the half edges (otherwise class is not fully initialized)
44 // if numOrientationFlips is not NULL, returns the number of edges that were flipped to orient the surface coherently
45 // if the mesh is non-orientable, throws an exception
46 ObjMeshOrientable(const std::string & filename, int generateHalfEdges=1, int * numOrientationFlips = NULL, int verbose=0);
47
48 ObjMeshOrientable(ObjMesh * objMesh, int generateHalfEdges=1, int * numOrientationFlips = NULL, int verbose=0);
49
50 ~ObjMeshOrientable();
51
52 // makes an empty structure
53 ObjMeshOrientable() {}
54
55 class HalfEdge
56 {
57 public:
58 explicit HalfEdge(const unsigned int & position_g, const unsigned int& startVertex_g, const unsigned int& endVertex_g, const unsigned int& startV_g, const unsigned int& endV_g, const unsigned int groupID_g, const unsigned int& face_g, const int opposite_g, const unsigned int next_g // value of -1 denotes boundary edge const unsigned int next_g
59 )
60 : position_(position_g), startVertex_(startVertex_g), endVertex_(endVertex_g),
61 startV_(startV_g), endV_(endV_g),
62 groupID_(groupID_g),face_(face_g),
63 opposite_(opposite_g), next_(next_g) {}
64
65 // accessors for getting global edge position
66 unsigned int position() { return position_;}
67
68 // accessors for starting and ending vertices of the edge (global indexing)
69 unsigned int startVertex() { return startVertex_; }
70 unsigned int endVertex() { return endVertex_; }
71
72 // accessors for starting and ending vertices of the edge (local indexing on the local face)
73 unsigned int startV() { return startV_; }
74 unsigned int endV() { return endV_; }
75
76 // accessors for the face on the left of the edge
77 unsigned int groupID() { return groupID_; }
78 unsigned int face() { return face_; }
79
80 // accessors for opposite and next edges
81 int opposite() { return opposite_; }
82 unsigned int next() { return next_; }
83
84 // mutator for opposite and next edges
85 void setOpposite(int opposite_g) { opposite_ = opposite_g; }
86 void setNext(unsigned int next_g) { next_ = next_g; }
87
88 // is this edge a boundary edge
89 bool isBoundary() { return (opposite_ == -1);}
90
91 void flipOrientation(); // flips orientation of the edge (careful, structure not coherent any more now)
92
93 bool operator== (HalfEdge & halfEdge2) { return (position_ == halfEdge2.position_);}
94 bool operator!= (HalfEdge & halfEdge2) { return (position_ != halfEdge2.position_);}
95
96
97 protected:
98 unsigned int position_; // the global position of the half-edge in the data structure
99 unsigned int startVertex_, endVertex_; // global vertex indices
100 unsigned int startV_, endV_; // local vertex indices, on the face
101 unsigned int groupID_;
102 unsigned int face_;
103 int opposite_;
104 unsigned int next_;
105 };
106
107 protected:
108 ObjMesh * objMesh;
109 void Init(int generateHalfEdges, int * numOrientationFlips, int verbose);
110
111 std::vector< HalfEdge > halfEdges_;
112 std::vector< int > boundaryEdges_;
113 unsigned int connectedComponents; // the total number of connected components of the mesh
114
115 bool hasBoundary_; // does the surface have boundary
116
117 std::vector<int> edgesAtVertices_; // for every vertex, contains one half-edge emanating out of it
118 std::vector<std::vector<int> > edgesAtFaces_; // for every face, contains one half-edge on this face
119
120 void determineIfSurfaceHasBoundary();
121
122 public:
123 size_t numHalfEdges() { return halfEdges_.size(); }
124 HalfEdge & halfEdge(unsigned int i) { return halfEdges_[i]; }
125
126 // this function is mostly called internally, but can sometimes also be called from the outside
127 // returns the number of edges that were flipped to orient the surface coherently
128 // (which will be zero if the input mesh already is oriented coherently)
129 // if the data structure cannot be generated (non-manifold geometry), returns -1
130 int GenerateHalfEdgeDataStructure(int verbose=0); // generates the whole datastructure, assuming the base objMesh class has been initialized
131
132 void CopyHalfEdgeTopologyFrom(ObjMeshOrientable * source); // makes the half-edge topological info equal to that of source
133
134 // returns the opposite halfedge to the given half-edge
135 // this function will fail for boundary edges, should always check first with isBoundary()
136 HalfEdge & edgeOpposite(HalfEdge & halfedge) { return halfEdges_[halfedge.opposite()]; }
137
138 // returns the next halfedge to the given half-edge
139 HalfEdge & edgeNext(HalfEdge & halfedge) { return halfEdges_[halfedge.next()]; }
140
141 // returns the previous halfedge to the given half-edge
142 // does so by looping around the face (pointers to previous edges are not explicitly stored), so this is slower than edgeNext
143 HalfEdge & edgePrevious(HalfEdge & halfedge);
144
145 // loops around the vertex (vertex is defined as the ending position of the halfedge)
146 // consists of taking the next edge, then taking the opposite edge
147 // if boundary edge encountered, can't take the opposite edge; it this case flag=1 is returned and the edge returned is the boundary edge pointing away from the vertex
148 // if taking the opposite edge is possible, the returned edge points into the vertex and flag is set to 0
149 // this is effectively looping in the clockwise (negative) orientation
150 HalfEdge & loopVertex(HalfEdge & halfedge, int & flag);
151
152 // returns the the group that contains the given half-edge
153 ObjMesh::Group edgeGroup(HalfEdge & halfedge) { return *(objMesh->getGroupHandle(halfedge.groupID())); }
154
155 // returns the face to the left of the given half-edge
156 ObjMesh::Face edgeFace(HalfEdge & halfedge)
157 { const ObjMesh::Group * getGroupHandle = objMesh->getGroupHandle(halfedge.groupID());
158 return getGroupHandle->getFace(halfedge.face()); }
159
160 // returns the starting vertex of the given half-edge
161 ObjMesh::Vertex edgeStartVertex (HalfEdge & halfedge)
162 { const ObjMesh::Group * getGroupHandle = objMesh->getGroupHandle(halfedge.groupID());
163 return (getGroupHandle->getFace(halfedge.face())).getVertex(halfedge.startV()); }
164
165 // returns the ending vertex of the given half-edge
166 ObjMesh::Vertex edgeEndVertex (HalfEdge & halfedge)
167 { const ObjMesh::Group * getGroupHandle = objMesh->getGroupHandle(halfedge.groupID());
168 return (getGroupHandle->getFace(halfedge.face())).getVertex(halfedge.endV()); }
169
170 unsigned int numConnectedComponents() { return connectedComponents;}
171
172 bool isIsolatedVertex(unsigned int vertex) { return (edgesAtVertices_[vertex] == -1); } // returns true if vertex is isolated
173 bool isBoundaryVertex(unsigned int vertex) { return halfEdges_[edgesAtVertices_[vertex]].isBoundary(); } // returns true if vertex is a mesh boundary vertex
174
175 void PrintHalfEdges(); // prints the half-edges out
176
177 ObjMesh * GenerateOrientedMesh(); // generates oriented mesh (same mesh as the input ObjMesh, but oriented consistently)
178
179 // returns some halfedge emanating out of a given vertex (returns always the same edge)
180 // in case vertex is a boundary vertex, it will return the edge such that there is no clockwise edge to the given edge around the given vertex
181 // this function will fail for isolated vertices; should always check first with isIsolatedVertex()
182 HalfEdge & edgeAtVertex( unsigned int vertex ) { return halfEdges_[edgesAtVertices_[vertex]]; }
183
184 // returns some halfedge on the given face (returns always the same edge)
185 HalfEdge & edgeAtFace( unsigned int groupID, unsigned int faceID ) { return halfEdges_[edgesAtFaces_[groupID][faceID]]; }
186
187 // returns true if surface has boundary and false if it is closed
188 bool hasBoundary() { return hasBoundary_;}
189
190 size_t numBoundaryEdges() { return boundaryEdges_.size(); }
191 int boundaryEdge(int i) { return boundaryEdges_[i]; }
192
193 int internalAllocation;
194 };
195
196 #endif
197