Ocean
HashableTriangle.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_TRACKING_MAPTEXTURING_HASHABLE_TRIANGLE_H
9 #define META_OCEAN_TRACKING_MAPTEXTURING_HASHABLE_TRIANGLE_H
10 
12 
13 #include "ocean/math/Vector3.h"
14 
15 namespace Ocean
16 {
17 
18 namespace Tracking
19 {
20 
21 namespace MapTexturing
22 {
23 
24 // Forward declaration.
25 template <typename T> class HashableTriangleT;
26 
27 /**
28  * Definition of a hash-able triangle with Scalar precision.
29  * @see HashableTriangleT
30  * @ingroup trackingmaptexturing
31  */
33 
34 /**
35  * This class implements a 3D triangle which is hash-able.
36  * @tparam T the scalar data type of the vertex, e.g., 'float' or 'double'
37  * @ingroup trackingmaptexturing
38  */
39 template <typename T>
41 {
42  public:
43 
44  /**
45  * Default constructor for an invalid triangle.
46  */
47  HashableTriangleT() = default;
48 
49  /**
50  * Creates a new triangle.
51  * @param vertices The three vertices of the triangle, must be valid
52  */
53  explicit inline HashableTriangleT(const VectorT3<T>* vertices);
54 
55  /**
56  * Creates a new triangle.
57  * @param vertex0 The first vertex of the triangle
58  * @param vertex1 The first vertex of the triangle
59  * @param vertex2 The first vertex of the triangle
60  */
62 
63  /**
64  * Returns the first vertex of this triangle.
65  * @return The triangle's first vertex
66  */
67  inline const VectorT3<T>& vertex0() const;
68 
69  /**
70  * Returns the second vertex of this triangle.
71  * @return The triangle's second vertex
72  */
73  inline const VectorT3<T>& vertex1() const;
74 
75  /**
76  * Returns the third vertex of this triangle.
77  * @return The triangle's third vertex
78  */
79  inline const VectorT3<T>& vertex2() const;
80 
81  /**
82  * Returns the three vertices of this triangle.
83  * @return The triangle's three vertices
84  */
85  inline const VectorT3<T>* vertices() const;
86 
87  /**
88  * Returns whether two triangles contain exactly the same vertices.
89  * @param triangle The second triangle
90  * @return True, if so
91  */
92  inline bool operator==(const HashableTriangleT<T>& triangle) const;
93 
94  /**
95  * Returns whether two triangles do not contain exactly the same vertices.
96  * @param triangle The second triangle
97  * @return True, if so
98  */
99  inline bool operator!=(const HashableTriangleT<T>& triangle) const;
100 
101  /**
102  * Hash function.
103  * @param triangle The triangle for which the hash value will be determined
104  * @return The resulting hash value
105  */
106  inline size_t operator()(const HashableTriangleT<T>& triangle) const;
107 
108  protected:
109 
110  /**
111  * Determines the hash value for three vertices.
112  * @param vertex0 The first vertex
113  * @param vertex1 The second vertex
114  * @param vertex2 The third vertex
115  * @return The resulting hash value
116  */
117  static inline size_t hashValue(const VectorT3<T>& vertex0, const VectorT3<T>& vertex1, const VectorT3<T>& vertex2);
118 
119  protected:
120 
121  /// The three vertices of this triangle.
123 };
124 
125 template <typename T>
127 {
128  ocean_assert(vertices != nullptr);
129 
130  vertices_[0] = vertices[0];
131  vertices_[1] = vertices[1];
132  vertices_[2] = vertices[2];
133 }
134 
135 template <typename T>
136 inline HashableTriangleT<T>::HashableTriangleT(const VectorT3<T>& vertex0, const VectorT3<T>& vertex1, const VectorT3<T>& vertex2)
137 {
138  vertices_[0] = vertex0;
139  vertices_[1] = vertex1;
140  vertices_[2] = vertex2;
141 }
142 
143 template <typename T>
145 {
146  return vertices_[0];
147 }
148 
149 template <typename T>
151 {
152  return vertices_[1];
153 }
154 
155 template <typename T>
157 {
158  return vertices_[2];
159 }
160 
161 template <typename T>
163 {
164  return vertices_;
165 }
166 
167 template <typename T>
168 inline bool HashableTriangleT<T>::operator==(const HashableTriangleT<T>& triangle) const
169 {
170  return vertices_[0].x() == triangle.vertices_[0].x() && vertices_[0].y() == triangle.vertices_[0].y() && vertices_[0].z() == triangle.vertices_[0].z()
171  && vertices_[1].x() == triangle.vertices_[1].x() && vertices_[1].y() == triangle.vertices_[1].y() && vertices_[1].z() == triangle.vertices_[1].z()
172  && vertices_[2].x() == triangle.vertices_[2].x() && vertices_[2].y() == triangle.vertices_[2].y() && vertices_[2].z() == triangle.vertices_[2].z();
173 }
174 
175 template <typename T>
176 inline bool HashableTriangleT<T>::operator!=(const HashableTriangleT<T>& triangle) const
177 {
178  return !(*this == triangle);
179 }
180 
181 template <typename T>
182 inline size_t HashableTriangleT<T>::operator()(const HashableTriangleT& triangle) const
183 {
184  return hashValue(triangle.vertex0(), triangle.vertex1(), triangle.vertex2());
185 }
186 
187 template <typename T>
188 inline size_t HashableTriangleT<T>::hashValue(const VectorT3<T>& vertex0, const VectorT3<T>& vertex1, const VectorT3<T>& vertex2)
189 {
190  size_t seed = std::hash<T>{}(vertex0.x());
191  seed ^= std::hash<T>{}(vertex0.y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
192  seed ^= std::hash<T>{}(vertex0.z()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
193 
194  seed ^= std::hash<T>{}(vertex1.x()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
195  seed ^= std::hash<T>{}(vertex1.y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
196  seed ^= std::hash<T>{}(vertex1.z()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
197 
198  seed ^= std::hash<T>{}(vertex2.x()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
199  seed ^= std::hash<T>{}(vertex2.y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
200  seed ^= std::hash<T>{}(vertex2.z()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
201 
202  return seed;
203 }
204 
205 }
206 
207 }
208 
209 }
210 
211 #endif // META_OCEAN_TRACKING_MAPTEXTURING_HASHABLE_TRIANGLE_H
This class implements a 3D triangle which is hash-able.
Definition: HashableTriangle.h:41
const VectorT3< T > & vertex2() const
Returns the third vertex of this triangle.
Definition: HashableTriangle.h:156
const VectorT3< T > * vertices() const
Returns the three vertices of this triangle.
Definition: HashableTriangle.h:162
size_t operator()(const HashableTriangleT< T > &triangle) const
Hash function.
Definition: HashableTriangle.h:182
HashableTriangleT()=default
Default constructor for an invalid triangle.
const VectorT3< T > & vertex0() const
Returns the first vertex of this triangle.
Definition: HashableTriangle.h:144
const VectorT3< T > & vertex1() const
Returns the second vertex of this triangle.
Definition: HashableTriangle.h:150
bool operator!=(const HashableTriangleT< T > &triangle) const
Returns whether two triangles do not contain exactly the same vertices.
Definition: HashableTriangle.h:176
VectorT3< T > vertices_[3]
The three vertices of this triangle.
Definition: HashableTriangle.h:122
bool operator==(const HashableTriangleT< T > &triangle) const
Returns whether two triangles contain exactly the same vertices.
Definition: HashableTriangle.h:168
static size_t hashValue(const VectorT3< T > &vertex0, const VectorT3< T > &vertex1, const VectorT3< T > &vertex2)
Determines the hash value for three vertices.
Definition: HashableTriangle.h:188
This class implements a vector with three elements.
Definition: Vector3.h:97
const T & y() const noexcept
Returns the y value.
Definition: Vector3.h:812
const T & x() const noexcept
Returns the x value.
Definition: Vector3.h:800
const T & z() const noexcept
Returns the z value.
Definition: Vector3.h:824
HashableTriangleT< Scalar > HashableTriangle
Definition of a hash-able triangle with Scalar precision.
Definition: HashableTriangle.h:25
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15