GteUniqueVerticesTriangles.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <GTEngineDEF.h>
11 #include <algorithm>
12 #include <map>
13 #include <vector>
14 
15 // The VertexType must have an operator< member because it is used as the
16 // key in a std::map<VertexType,int>. For example, if VertexType is
17 // Vector3<float>, the comparison operator already exist. Another example is
18 // that you have a vertex type used for vertex coloring, say,
19 // struct VertexType
20 // {
21 // Vector3<float> position;
22 // Vector4<float> color;
23 // bool operator< (VertexType const& v) const
24 // {
25 // return position < v.position;
26 // }
27 // }
28 // The comparision will guarantee unique vertex positions, although if you
29 // have two VertexType objects with the same position but different colors,
30 // there is no guarantee which color will occur in the final result.
31 
32 namespace gte
33 {
34 
35 template <typename VertexType>
37 {
38 public:
39  // Triangle soup. The input vertex array consists of triples of vertices,
40  // each triple representing a triangle. The array 'inVertices' must have
41  // a multiple of 3 elements. An array 'outVertices' of unique vertices
42  // and an array 'outIndices' of 'inVertices.size()/3' unique index triples
43  // are computed. The indices are relative to the array of unique vertices
44  // and each index triple represents a triangle.
46  std::vector<VertexType> const& inVertices,
47  std::vector<VertexType>& outVertices,
48  std::vector<int>& outIndices);
49 
50  // Indexed triangles. The input vertex array consists of all vertices
51  // referenced by the input index array. The array 'inIndices' must have a
52  // multiple of 3 elements. An array 'outVertices' of unique vertices and
53  // an array 'outIndices' of 'indices.size()/3' unique index triples are
54  // computed. The indices are relative to the array of unique vertices and
55  // each index triple represents a triangle.
57  std::vector<VertexType> const& inVertices,
58  std::vector<int> const& inIndices,
59  std::vector<VertexType>& outVertices,
60  std::vector<int>& outIndices);
61 
62  // The input vertices have indices 0 <= i < VInNum. The output vertices
63  // have indices 0 <= j < VOutNum. The construction leads to a mapping of
64  // input indices i to output indices j. Duplicate vertices have different
65  // input indices but the same output index. The following function gives
66  // you access to the mapping. If the input index is invalid (i < 0 or
67  // i >= VINum), the return value is -1.
68  inline int GetOutputIndexFor(int index) const;
69 
70 private:
71  void ConstructUniqueVertices(std::vector<VertexType> const& inVertices,
72  std::vector<VertexType>& outVertices);
73 
75  std::vector<int> mInToOutMapping;
76 };
77 
78 
79 template <typename VertexType>
81  std::vector<VertexType> const& inVertices,
82  std::vector<VertexType>& outVertices, std::vector<int>& outIndices)
83 {
84  ConstructUniqueVertices(inVertices, outVertices);
85 
86  // The input index array is implicitly {<0,1,2>,<3,4,5>,...,<n-3,n-2,n-1>}
87  // where n is the number of vertices. The output index array is the same
88  // as the mapping array.
89  outIndices.resize(inVertices.size());
90  std::copy(mInToOutMapping.begin(), mInToOutMapping.end(),
91  outIndices.begin());
92 }
93 
94 template <typename VertexType>
96  std::vector<VertexType> const& inVertices,
97  std::vector<int> const& inIndices, std::vector<VertexType>& outVertices,
98  std::vector<int>& outIndices)
99 {
100  ConstructUniqueVertices(inVertices, outVertices);
101 
102  // The input index array needs it indices mapped to the unique vertex
103  // indices.
104  outIndices.resize(inIndices.size());
105  for (size_t i = 0; i < inIndices.size(); ++i)
106  {
107  outIndices[i] = mInToOutMapping[inIndices[i]];
108  }
109 }
110 
111 template <typename VertexType> inline
113 {
114  return mInToOutMapping[index];
115 }
116 
117 template <typename VertexType>
119  std::vector<VertexType> const& inVertices,
120  std::vector<VertexType>& outVertices)
121 {
122  // Construct the unique vertices.
123  mNumInVertices = (int)inVertices.size();
125  std::map<VertexType, int> table;
126  mNumOutVertices = 0;
127  for (int i = 0; i < mNumInVertices; ++i)
128  {
129  auto const iter = table.find(inVertices[i]);
130  if (iter != table.end())
131  {
132  // Vertex i is a duplicate of one inserted earlier into the
133  // table. Map vertex i to the first-found copy.
134  mInToOutMapping[i] = iter->second;
135  }
136  else
137  {
138  // Vertex i is the first occurrence of such a point.
139  table.insert(std::make_pair(inVertices[i], mNumOutVertices));
141  ++mNumOutVertices;
142  }
143  }
144 
145  // Pack the unique vertices into an array in the correct order.
146  outVertices.resize(mNumOutVertices);
147  for (auto const& element : table)
148  {
149  outVertices[element.second] = element.first;
150  }
151 }
152 
153 
154 }
UniqueVerticesTriangles(std::vector< VertexType > const &inVertices, std::vector< VertexType > &outVertices, std::vector< int > &outIndices)
GLenum GLenum GLsizei void * table
Definition: glext.h:2723
void ConstructUniqueVertices(std::vector< VertexType > const &inVertices, std::vector< VertexType > &outVertices)
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
GLuint index
Definition: glcorearb.h:781


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01