Ocean
TriangleFace.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_RENDERING_TRINAGLE_FACE_H
9 #define META_OCEAN_RENDERING_TRINAGLE_FACE_H
10 
12 
13 #include <map>
14 #include <vector>
15 
16 namespace Ocean
17 {
18 
19 namespace Rendering
20 {
21 
22 /// Foward declaration.
23 class TriangleFace;
24 
25 /**
26  * Definition of a vector holding triangle faces.
27  * @see TriangleFace.
28  * @ingroup rendering
29  */
30 typedef std::vector<TriangleFace> TriangleFaces;
31 
32 /**
33  * Definition of a triangle face with three vertex indices.
34  * @ingroup rendering
35  */
36 class OCEAN_RENDERING_EXPORT TriangleFace
37 {
38  protected:
39 
40  /**
41  * Definition of a map mapping vertices to their corresponding face indices.
42  */
43  typedef std::map<Vertex, VertexIndices> VertexMap;
44 
45  public:
46 
47  /**
48  * Creates a new triangle face object with undefined indices.
49  */
50  inline TriangleFace();
51 
52  /**
53  * Creates a new triangle face object with successive indices.<br>
54  * The first index is given, the following indices will be set to successive values.
55  * @param startIndex Index of the first vertex
56  */
57  inline explicit TriangleFace(const VertexIndex startIndex);
58 
59  /**
60  * Creates a new triangle face object with three given indices.
61  * @param first First vertex index
62  * @param second Second vertex index
63  * @param third Third vertex index
64  */
65  inline TriangleFace(const VertexIndex first, const VertexIndex second, const VertexIndex third);
66 
67  /**
68  * Creates a new triangle face object by an array of at least three indices.
69  * @param arrayValue Array with at least three vertex indices
70  */
71  inline explicit TriangleFace(const VertexIndex* arrayValue);
72 
73  /**
74  * Returns a specific vertex index specified by it's index inside the face.
75  * Beware: No range check will be done!
76  * @param index Index of the vertex index with range [0, 2]
77  * @return Specified vertex index
78  */
79  [[nodiscard]] inline VertexIndex index(const unsigned int index) const;
80 
81  /**
82  * Returns a specific vertex index specified by it's index inside the face.
83  * Beware: No range check will be done!
84  * @param index Index of the vertex index with range [0, 2]
85  * @return Specified vertex index
86  */
87  [[nodiscard]] inline VertexIndex& index(const unsigned int index);
88 
89  /**
90  * Returns a specific vertex index specified by it's index inside the face.
91  * Beware: No range check will be done!
92  * @param index Index of the vertex index with range [0, 2]
93  * @return Specified vertex index
94  */
95  [[nodiscard]] inline VertexIndex operator[](const unsigned int index) const;
96 
97  /**
98  * Returns a specific vertex index specified by it's index inside the face.
99  * Beware: No range check will be done!
100  * @param index Index of the vertex index with range [0, 2]
101  * @return Specified vertex index
102  */
103  [[nodiscard]] inline VertexIndex& operator[](const unsigned int index);
104 
105  /**
106  * Returns the pointer to the first element of the vertex indices.
107  * @return Array with vertex indices
108  */
109  [[nodiscard]] inline const VertexIndex* operator()() const;
110 
111  /**
112  * Returns the pointer to the first element of the vertex indices.
113  * @return Array with vertex indices
114  */
115  [[nodiscard]] inline VertexIndex* operator()();
116 
117  /**
118  * Returns whether two triangle face objects are identical.
119  * @param triangleFace The second object to compare
120  * @return True, if so
121  */
122  inline bool operator==(const TriangleFace& triangleFace) const;
123 
124  /**
125  * Returns whether two triangle face objects are not identical.
126  * @param triangleFace The second object to compare
127  * @return True, if so
128  */
129  inline bool operator!=(const TriangleFace& triangleFace) const;
130 
131  /**
132  * Hash function for a TriangleFace object.
133  * @param triangleFace The triangle face for which the hash will be determined
134  * @return The resulting hash value
135  */
136  inline size_t operator()(const Rendering::TriangleFace& triangleFace) const;
137 
138  /**
139  * Calculates per-face normals for a given set of triangles.
140  * @param faces The triangle faces
141  * @param vertices The triangle vertices corresponding to the faces
142  * @param counterClockWise True, if the faces are provides so that a front face is determined in a counter clockwise order
143  * The resulting per-face normals, one for each faces
144  */
145  [[nodiscard]] static Vectors3 calculatePerFaceNormals(const TriangleFaces& faces, const Vertices& vertices, const bool counterClockWise = true);
146 
147  /**
148  * Calculates smoothed per-vertex normals for a given set of triangles and per-face normals
149  * @param faces The triangle faces
150  * @param vertices The triangle vertices corresponding to the faces
151  * @param perFaceNormals The per-face normals
152  * The resulting per-vertex normals, one for each vertex
153  */
154  [[nodiscard]] static Vectors3 calculateSmoothedPerVertexNormals(const TriangleFaces& faces, const Vertices& vertices, const Vectors3& perFaceNormals);
155 
156  /**
157  * Recalculates smoothed per-vertex normals for a given set of triangles with per vertex normals.
158  * @param faces The triangle faces
159  * @param vertices The triangle vertices corresponding to the faces
160  * @param normals The per-vertex normals, which are not smoothed yet
161  * @param creaseAngle Crease angle defining the seperation between per face and per vertex normals in radian, with range [0, PI/2]
162  * @return True, if succeeded
163  */
164  static bool calculateSmoothedPerVertexNormals(const TriangleFaces& faces, const Vectors3& vertices, Vectors3& normals, const Scalar creaseAngle);
165 
166  /**
167  * Calculates smoothed per-vertexnormals for a given set of triangles.
168  * @param faces The triangle faces
169  * @param vertices The triangle vertices corresponding to the faces
170  * @return The resulting smoothed per-vertex normals
171  */
172  static Vectors3 calculateSmoothedPerVertexNormals(const TriangleFaces& faces, const Vectors3& vertices);
173 
174  /**
175  * Convertes indices of triangles to triangle faces.
176  * @param indices The indices of the triangle, must be valid
177  * @param size The number of indices, with range [3, infinity), must be a multiple of 3
178  * @return The resulting triangle faces
179  */
180  static inline TriangleFaces indices2triangleFaces(const Index32* indices, const size_t size);
181 
182  protected:
183 
184  /// The three vertex indices.
185  VertexIndex indices_[3];
186 };
187 
189 {
190  // nothing to do here
191 }
192 
193 inline TriangleFace::TriangleFace(const VertexIndex startIndex)
194 {
195  indices_[0] = startIndex;
196  indices_[1] = startIndex + 1;
197  indices_[2] = startIndex + 2;
198 }
199 
200 
201 inline TriangleFace::TriangleFace(const VertexIndex first, const VertexIndex second, const VertexIndex third)
202 {
203  indices_[0] = first;
204  indices_[1] = second;
205  indices_[2] = third;
206 }
207 
208 inline TriangleFace::TriangleFace(const VertexIndex* arrayValue)
209 {
210  memcpy(indices_, arrayValue, sizeof(VertexIndex) * 3);
211 }
212 
213 inline VertexIndex TriangleFace::index(const unsigned int index) const
214 {
215  ocean_assert(index < 3);
216  return indices_[index];
217 }
218 
219 inline VertexIndex& TriangleFace::index(const unsigned int index)
220 {
221  ocean_assert(index < 3);
222  return indices_[index];
223 }
224 
225 inline VertexIndex TriangleFace::operator[](const unsigned int index) const
226 {
227  ocean_assert(index < 3);
228  return indices_[index];
229 }
230 
231 inline VertexIndex& TriangleFace::operator[](const unsigned int index)
232 {
233  ocean_assert(index < 3);
234  return indices_[index];
235 }
236 
238 {
239  return indices_;
240 }
241 
243 {
244  return indices_;
245 }
246 
247 inline bool TriangleFace::operator==(const TriangleFace& triangleFace) const
248 {
249  return indices_[0] == triangleFace.indices_[0] && indices_[1] == triangleFace.indices_[1] && indices_[2] == triangleFace.indices_[2];
250 }
251 
252 inline bool TriangleFace::operator!=(const TriangleFace& triangleFace) const
253 {
254  return !(*this == triangleFace);
255 }
256 
257 inline size_t TriangleFace::operator()(const TriangleFace& triangleFace) const
258 {
259  size_t seed = std::hash<Index32>{}(triangleFace[0]);
260  seed ^= std::hash<Index32>{}(triangleFace[1]) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
261  seed ^= std::hash<Index32>{}(triangleFace[2]) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
262 
263  return seed;
264 }
265 
266 inline TriangleFaces TriangleFace::indices2triangleFaces(const Index32* indices, const size_t size)
267 {
268  ocean_assert(indices != nullptr);
269  ocean_assert(size >= 3 && size % 3 == 0);
270 
271  TriangleFaces triangleFaces;
272  triangleFaces.reserve(size / 3);
273 
274  for (size_t n = 0; n < size; n += 3)
275  {
276  triangleFaces.emplace_back(indices[n + 0], indices[n + 1], indices[n + 2]);
277  }
278 
279  return triangleFaces;
280 }
281 
282 }
283 
284 }
285 
286 #endif // META_OCEAN_RENDERING_TRINAGLE_FACE_H
Definition of a triangle face with three vertex indices.
Definition: TriangleFace.h:37
static TriangleFaces indices2triangleFaces(const Index32 *indices, const size_t size)
Convertes indices of triangles to triangle faces.
Definition: TriangleFace.h:266
bool operator!=(const TriangleFace &triangleFace) const
Returns whether two triangle face objects are not identical.
Definition: TriangleFace.h:252
std::map< Vertex, VertexIndices > VertexMap
Definition of a map mapping vertices to their corresponding face indices.
Definition: TriangleFace.h:43
VertexIndex operator[](const unsigned int index) const
Returns a specific vertex index specified by it's index inside the face.
Definition: TriangleFace.h:225
VertexIndex indices_[3]
The three vertex indices.
Definition: TriangleFace.h:185
const VertexIndex * operator()() const
Returns the pointer to the first element of the vertex indices.
Definition: TriangleFace.h:237
static Vectors3 calculateSmoothedPerVertexNormals(const TriangleFaces &faces, const Vectors3 &vertices)
Calculates smoothed per-vertexnormals for a given set of triangles.
static Vectors3 calculatePerFaceNormals(const TriangleFaces &faces, const Vertices &vertices, const bool counterClockWise=true)
Calculates per-face normals for a given set of triangles.
static bool calculateSmoothedPerVertexNormals(const TriangleFaces &faces, const Vectors3 &vertices, Vectors3 &normals, const Scalar creaseAngle)
Recalculates smoothed per-vertex normals for a given set of triangles with per vertex normals.
TriangleFace()
Creates a new triangle face object with undefined indices.
Definition: TriangleFace.h:188
static Vectors3 calculateSmoothedPerVertexNormals(const TriangleFaces &faces, const Vertices &vertices, const Vectors3 &perFaceNormals)
Calculates smoothed per-vertex normals for a given set of triangles and per-face normals.
bool operator==(const TriangleFace &triangleFace) const
Returns whether two triangle face objects are identical.
Definition: TriangleFace.h:247
VertexIndex index(const unsigned int index) const
Returns a specific vertex index specified by it's index inside the face.
Definition: TriangleFace.h:213
uint32_t Index32
Definition of a 32 bit index value.
Definition: Base.h:84
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition: Vector3.h:65
std::vector< Vertex > Vertices
Definition of a vector holding vertices.
Definition: rendering/Rendering.h:119
std::vector< TriangleFace > TriangleFaces
Definition of a vector holding triangle faces.
Definition: TriangleFace.h:23
unsigned int VertexIndex
Definition of a vertex index.
Definition: rendering/Rendering.h:71
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15