1 // Copyright 2016 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #ifndef DRACO_MESH_CORNER_TABLE_H_
16 #define DRACO_MESH_CORNER_TABLE_H_
17 
18 #include <array>
19 #include <memory>
20 
21 #include "draco/attributes/geometry_indices.h"
22 #include "draco/core/draco_index_type_vector.h"
23 #include "draco/core/macros.h"
24 #include "draco/mesh/valence_cache.h"
25 
26 namespace draco {
27 
28 // CornerTable is used to represent connectivity of triangular meshes.
29 // For every corner of all faces, the corner table stores the index of the
30 // opposite corner in the neighboring face (if it exists) as illustrated in the
31 // figure below (see corner |c| and it's opposite corner |o|).
32 //
33 //     *
34 //    /c\
35 //   /   \
36 //  /n   p\
37 // *-------*
38 //  \     /
39 //   \   /
40 //    \o/
41 //     *
42 //
43 // All corners are defined by unique CornerIndex and each triplet of corners
44 // that define a single face id always ordered consecutively as:
45 //     { 3 * FaceIndex, 3 * FaceIndex + 1, 3 * FaceIndex +2 }.
46 // This representation of corners allows CornerTable to easily retrieve Next and
47 // Previous corners on any face (see corners |n| and |p| in the figure above).
48 // Using the Next, Previous, and Opposite corners then enables traversal of any
49 // 2-manifold surface.
50 // If the CornerTable is constructed from a non-manifold surface, the input
51 // non-manifold edges and vertices are automatically split.
52 class CornerTable {
53  public:
54   // Corner table face type.
55   typedef std::array<VertexIndex, 3> FaceType;
56 
57   CornerTable();
58   static std::unique_ptr<CornerTable> Create(
59       const IndexTypeVector<FaceIndex, FaceType> &faces);
60 
61   // Initializes the CornerTable from provides set of indexed faces.
62   // The input faces can represent a non-manifold topology, in which case the
63   // non-manifold edges and vertices are going to be split.
64   bool Init(const IndexTypeVector<FaceIndex, FaceType> &faces);
65 
66   // Resets the corner table to the given number of invalid faces.
67   bool Reset(int num_faces);
68 
69   // Resets the corner table to the given number of invalid faces and vertices.
70   bool Reset(int num_faces, int num_vertices);
71 
num_vertices()72   inline int num_vertices() const {
73     return static_cast<int>(vertex_corners_.size());
74   }
num_corners()75   inline int num_corners() const {
76     return static_cast<int>(corner_to_vertex_map_.size());
77   }
num_faces()78   inline int num_faces() const {
79     return static_cast<int>(corner_to_vertex_map_.size() / 3);
80   }
81 
Opposite(CornerIndex corner)82   inline CornerIndex Opposite(CornerIndex corner) const {
83     if (corner == kInvalidCornerIndex)
84       return corner;
85     return opposite_corners_[corner];
86   }
Next(CornerIndex corner)87   inline CornerIndex Next(CornerIndex corner) const {
88     if (corner == kInvalidCornerIndex)
89       return corner;
90     return LocalIndex(++corner) ? corner : corner - 3;
91   }
Previous(CornerIndex corner)92   inline CornerIndex Previous(CornerIndex corner) const {
93     if (corner == kInvalidCornerIndex)
94       return corner;
95     return LocalIndex(corner) ? corner - 1 : corner + 2;
96   }
Vertex(CornerIndex corner)97   inline VertexIndex Vertex(CornerIndex corner) const {
98     if (corner == kInvalidCornerIndex)
99       return kInvalidVertexIndex;
100     return ConfidentVertex(corner);
101   }
ConfidentVertex(CornerIndex corner)102   inline VertexIndex ConfidentVertex(CornerIndex corner) const {
103     DRACO_DCHECK_GE(corner.value(), 0);
104     DRACO_DCHECK_LT(corner.value(), num_corners());
105     return corner_to_vertex_map_[corner];
106   }
Face(CornerIndex corner)107   inline FaceIndex Face(CornerIndex corner) const {
108     if (corner == kInvalidCornerIndex)
109       return kInvalidFaceIndex;
110     return FaceIndex(corner.value() / 3);
111   }
FirstCorner(FaceIndex face)112   inline CornerIndex FirstCorner(FaceIndex face) const {
113     if (face == kInvalidFaceIndex)
114       return kInvalidCornerIndex;
115     return CornerIndex(face.value() * 3);
116   }
AllCorners(FaceIndex face)117   inline std::array<CornerIndex, 3> AllCorners(FaceIndex face) const {
118     const CornerIndex ci = CornerIndex(face.value() * 3);
119     return {{ci, ci + 1, ci + 2}};
120   }
LocalIndex(CornerIndex corner)121   inline int LocalIndex(CornerIndex corner) const { return corner.value() % 3; }
122 
FaceData(FaceIndex face)123   inline FaceType FaceData(FaceIndex face) const {
124     const CornerIndex first_corner = FirstCorner(face);
125     FaceType face_data;
126     for (int i = 0; i < 3; ++i) {
127       face_data[i] = corner_to_vertex_map_[first_corner + i];
128     }
129     return face_data;
130   }
131 
SetFaceData(FaceIndex face,FaceType data)132   void SetFaceData(FaceIndex face, FaceType data) {
133     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
134     const CornerIndex first_corner = FirstCorner(face);
135     for (int i = 0; i < 3; ++i) {
136       corner_to_vertex_map_[first_corner + i] = data[i];
137     }
138   }
139 
140   // Returns the left-most corner of a single vertex 1-ring. If a vertex is not
141   // on a boundary (in which case it has a full 1-ring), this function returns
142   // any of the corners mapped to the given vertex.
LeftMostCorner(VertexIndex v)143   inline CornerIndex LeftMostCorner(VertexIndex v) const {
144     return vertex_corners_[v];
145   }
146 
147   // Returns the parent vertex index of a given corner table vertex.
VertexParent(VertexIndex vertex)148   VertexIndex VertexParent(VertexIndex vertex) const {
149     if (vertex.value() < static_cast<uint32_t>(num_original_vertices_))
150       return vertex;
151     return non_manifold_vertex_parents_[vertex - num_original_vertices_];
152   }
153 
154   // Returns true if the corner is valid.
IsValid(CornerIndex c)155   inline bool IsValid(CornerIndex c) const {
156     return Vertex(c) != kInvalidVertexIndex;
157   }
158 
159   // Returns the valence (or degree) of a vertex.
160   // Returns -1 if the given vertex index is not valid.
161   int Valence(VertexIndex v) const;
162   // Same as above but does not check for validity and does not return -1
163   int ConfidentValence(VertexIndex v) const;
164   // Returns the valence of the vertex at the given corner.
Valence(CornerIndex c)165   inline int Valence(CornerIndex c) const {
166     if (c == kInvalidCornerIndex)
167       return -1;
168     return ConfidentValence(c);
169   }
ConfidentValence(CornerIndex c)170   inline int ConfidentValence(CornerIndex c) const {
171     DRACO_DCHECK_LT(c.value(), num_corners());
172     return ConfidentValence(ConfidentVertex(c));
173   }
174 
175   // Returns true if the specified vertex is on a boundary.
IsOnBoundary(VertexIndex vert)176   inline bool IsOnBoundary(VertexIndex vert) const {
177     const CornerIndex corner = LeftMostCorner(vert);
178     if (SwingLeft(corner) == kInvalidCornerIndex)
179       return true;
180     return false;
181   }
182 
183   //     *-------*
184   //    / \     / \
185   //   /   \   /   \
186   //  /   sl\c/sr   \
187   // *-------v-------*
188   // Returns the corner on the adjacent face on the right that maps to
189   // the same vertex as the given corner (sr in the above diagram).
SwingRight(CornerIndex corner)190   inline CornerIndex SwingRight(CornerIndex corner) const {
191     return Previous(Opposite(Previous(corner)));
192   }
193   // Returns the corner on the left face that maps to the same vertex as the
194   // given corner (sl in the above diagram).
SwingLeft(CornerIndex corner)195   inline CornerIndex SwingLeft(CornerIndex corner) const {
196     return Next(Opposite(Next(corner)));
197   }
198 
199   // Get opposite corners on the left and right faces respectively (see image
200   // below, where L and R are the left and right corners of a corner X.
201   //
202   // *-------*-------*
203   //  \L    /X\    R/
204   //   \   /   \   /
205   //    \ /     \ /
206   //     *-------*
GetLeftCorner(CornerIndex corner_id)207   inline CornerIndex GetLeftCorner(CornerIndex corner_id) const {
208     if (corner_id == kInvalidCornerIndex)
209       return kInvalidCornerIndex;
210     return Opposite(Previous(corner_id));
211   }
GetRightCorner(CornerIndex corner_id)212   inline CornerIndex GetRightCorner(CornerIndex corner_id) const {
213     if (corner_id == kInvalidCornerIndex)
214       return kInvalidCornerIndex;
215     return Opposite(Next(corner_id));
216   }
217 
218   // Returns the number of new vertices that were created as a result of
219   // splitting of non-manifold vertices of the input geometry.
NumNewVertices()220   int NumNewVertices() const { return num_vertices() - num_original_vertices_; }
NumOriginalVertices()221   int NumOriginalVertices() const { return num_original_vertices_; }
222 
223   // Returns the number of faces with duplicated vertex indices.
NumDegeneratedFaces()224   int NumDegeneratedFaces() const { return num_degenerated_faces_; }
225 
226   // Returns the number of isolated vertices (vertices that have
227   // vertex_corners_ mapping set to kInvalidCornerIndex.
NumIsolatedVertices()228   int NumIsolatedVertices() const { return num_isolated_vertices_; }
229 
230   bool IsDegenerated(FaceIndex face) const;
231 
232   // Methods that modify an existing corner table.
233   // Sets the opposite corner mapping between two corners. Caller must ensure
234   // that the indices are valid.
SetOppositeCorner(CornerIndex corner_id,CornerIndex opp_corner_id)235   inline void SetOppositeCorner(CornerIndex corner_id,
236                                 CornerIndex opp_corner_id) {
237     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
238     opposite_corners_[corner_id] = opp_corner_id;
239   }
240 
241   // Sets opposite corners for both input corners.
SetOppositeCorners(CornerIndex corner_0,CornerIndex corner_1)242   inline void SetOppositeCorners(CornerIndex corner_0, CornerIndex corner_1) {
243     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
244     if (corner_0 != kInvalidCornerIndex)
245       SetOppositeCorner(corner_0, corner_1);
246     if (corner_1 != kInvalidCornerIndex)
247       SetOppositeCorner(corner_1, corner_0);
248   }
249 
250   // Updates mapping between a corner and a vertex.
MapCornerToVertex(CornerIndex corner_id,VertexIndex vert_id)251   inline void MapCornerToVertex(CornerIndex corner_id, VertexIndex vert_id) {
252     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
253     corner_to_vertex_map_[corner_id] = vert_id;
254   }
255 
AddNewVertex()256   VertexIndex AddNewVertex() {
257     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
258     // Add a new invalid vertex.
259     vertex_corners_.push_back(kInvalidCornerIndex);
260     return VertexIndex(static_cast<uint32_t>(vertex_corners_.size() - 1));
261   }
262 
263   // Sets a new left most corner for a given vertex.
SetLeftMostCorner(VertexIndex vert,CornerIndex corner)264   void SetLeftMostCorner(VertexIndex vert, CornerIndex corner) {
265     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
266     if (vert != kInvalidVertexIndex)
267       vertex_corners_[vert] = corner;
268   }
269 
270   // Updates the vertex to corner map on a specified vertex. This should be
271   // called in cases where the mapping may be invalid (e.g. when the corner
272   // table was constructed manually).
UpdateVertexToCornerMap(VertexIndex vert)273   void UpdateVertexToCornerMap(VertexIndex vert) {
274     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
275     const CornerIndex first_c = vertex_corners_[vert];
276     if (first_c == kInvalidCornerIndex)
277       return;  // Isolated vertex.
278     CornerIndex act_c = SwingLeft(first_c);
279     CornerIndex c = first_c;
280     while (act_c != kInvalidCornerIndex && act_c != first_c) {
281       c = act_c;
282       act_c = SwingLeft(act_c);
283     }
284     if (act_c != first_c) {
285       vertex_corners_[vert] = c;
286     }
287   }
288 
289   // Sets the new number of vertices. It's a responsibility of the caller to
290   // ensure that no corner is mapped beyond the range of the new number of
291   // vertices.
SetNumVertices(int num_vertices)292   inline void SetNumVertices(int num_vertices) {
293     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
294     vertex_corners_.resize(num_vertices, kInvalidCornerIndex);
295   }
296 
297   // Makes a vertex isolated (not attached to any corner).
MakeVertexIsolated(VertexIndex vert)298   void MakeVertexIsolated(VertexIndex vert) {
299     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
300     vertex_corners_[vert] = kInvalidCornerIndex;
301   }
302 
303   // Returns true if a vertex is not attached to any face.
IsVertexIsolated(VertexIndex v)304   inline bool IsVertexIsolated(VertexIndex v) const {
305     return LeftMostCorner(v) == kInvalidCornerIndex;
306   }
307 
308   // Makes a given face invalid (all corners are marked as invalid).
MakeFaceInvalid(FaceIndex face)309   void MakeFaceInvalid(FaceIndex face) {
310     DRACO_DCHECK(GetValenceCache().IsCacheEmpty());
311     if (face != kInvalidFaceIndex) {
312       const CornerIndex first_corner = FirstCorner(face);
313       for (int i = 0; i < 3; ++i) {
314         corner_to_vertex_map_[first_corner + i] = kInvalidVertexIndex;
315       }
316     }
317   }
318 
319   // Updates mapping between faces and a vertex using the corners mapped to
320   // the provided vertex.
321   void UpdateFaceToVertexMap(const VertexIndex vertex);
322 
323   // Allows access to an internal object for caching valences.  The object can
324   // be instructed to cache or uncache all valences and then its interfaces
325   // queried directly for valences with differing performance/confidence
326   // qualities.  If the mesh or table is modified the cache should be discarded
327   // and not relied on as it does not automatically update or invalidate for
328   // performance reasons.
GetValenceCache()329   const draco::ValenceCache<CornerTable> &GetValenceCache() const {
330     return valence_cache_;
331   }
332 
333  private:
334   // Computes opposite corners mapping from the data stored in
335   // |corner_to_vertex_map_|.
336   bool ComputeOppositeCorners(int *num_vertices);
337 
338   // Finds and breaks non-manifold edges in the 1-ring neighborhood around
339   // vertices (vertices themselves will be split in the ComputeVertexCorners()
340   // function if necessary).
341   bool BreakNonManifoldEdges();
342 
343   // Computes the lookup map for going from a vertex to a corner. This method
344   // can handle non-manifold vertices by splitting them into multiple manifold
345   // vertices.
346   bool ComputeVertexCorners(int num_vertices);
347 
348   // Each three consecutive corners represent one face.
349   IndexTypeVector<CornerIndex, VertexIndex> corner_to_vertex_map_;
350   IndexTypeVector<CornerIndex, CornerIndex> opposite_corners_;
351   IndexTypeVector<VertexIndex, CornerIndex> vertex_corners_;
352 
353   int num_original_vertices_;
354   int num_degenerated_faces_;
355   int num_isolated_vertices_;
356   IndexTypeVector<VertexIndex, VertexIndex> non_manifold_vertex_parents_;
357 
358   draco::ValenceCache<CornerTable> valence_cache_;
359 };
360 
361 // A special case to denote an invalid corner table triangle.
362 static constexpr CornerTable::FaceType kInvalidFace(
363     {{kInvalidVertexIndex, kInvalidVertexIndex, kInvalidVertexIndex}});
364 
365 }  // namespace draco
366 
367 #endif  // DRACO_MESH_CORNER_TABLE_H_
368