Ocean
Loading...
Searching...
No Matches
TextureAtlas.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_TEXTURE_ATLAS_H
9#define META_OCEAN_TRACKING_MAPTEXTURING_TEXTURE_ATLAS_H
10
12
14#include "ocean/math/Vector2.h"
15#include "ocean/math/Vector3.h"
16
17namespace Ocean
18{
19
20namespace Tracking
21{
22
23namespace MapTexturing
24{
25
26/**
27 * This class implements a texture atlas for triangles with regular shape.
28 * @ingroup trackingmaptexturing
29 */
30class OCEAN_TRACKING_MAPTEXTURING_EXPORT TextureAtlas
31{
32 public:
33
34 /**
35 * Default constructor.
36 */
38
39 /**
40 * Returns the camera texture coordinates of a triangle within a camera frame.
41 * @param anyCamera The camera profile defining the projection, must be valid
42 * @param flippedCamera_T_world
43 * @param meshTriangleId The id of the triangle for which the texture coordinates will be returned, with range [0, numberTriangles() - 1]
44 * @param vertices The three vertices for which the texture coordinates will be returned, must be valid
45 * @param textureCoordinates The resulting three texture coordinates, one for each given vertex, must be valid
46 * @param respectBorder True, to respect the border of the triangles; False, to ignore the border of the triangles
47 */
48 void triangleCameraTextureCoordiantes(const AnyCamera& anyCamera, const HomogenousMatrix4& flippedCamera_T_world, const Index32 meshTriangleId, const Vector3* vertices, Vector2* textureCoordinates, const bool respectBorder) const;
49
50 /**
51 * Returns the atlas texture coordinates of a triangle within this atlas.
52 * @param meshTriangleId The id of the triangle for which the texture coordinates will be returned, with range [0, numberTriangles() - 1]
53 * @param textureCoordinates The resulting three texture coordinates, must be valid
54 * @tparam tInnerTriangleWithBorder True, to determine the texture coordinate for the inner triangle; False, to determine the texture coordinate for the outer triangle
55 */
56 template <bool innerTriangleWithBorder>
57 void triangleAtlasTextureCoordinates(const Index32 meshTriangleId, Vector2* textureCoordinates) const;
58
59 /**
60 * Returns the barycentric triangle coordinates for the outer triangle.
61 * @param triangleId The id of the triangle for which the coordinate will be returned
62 * @param index The vertex index for which the barycentric coordinate will be returned, with range [0, 2]
63 * @return The resulting barycentric triangle coordinates
64 */
65 inline const Vector3& outerBorderBarycentric(const Index32 triangleId, const Index32 index) const;
66
67 /**
68 * Returns the texture size of this atlas.
69 * @return The atlas' texture width and height, in pixels
70 */
71 static constexpr unsigned int textureSizePixels();
72
73 /**
74 * Returns the major edge size of all triangles of this atlas.
75 * @return The atlas' triangle major edge size, in pixels
76 */
77 static constexpr unsigned int triangleSizePixels();
78
79 /**
80 * The border size of the triangles.
81 * @return The triangles' border size, in pixels
82 */
83 static constexpr Scalar borderSizePixels();
84
85 /**
86 * Returns the number of bins this atlas holds.
87 * @return The atlas' bins
88 */
89 static constexpr unsigned int numberBins();
90
91 /**
92 * Returns the number of triangles this atlas can hold.
93 * @return The atlas' triangle capacity
94 */
95 static constexpr unsigned int numberTriangles();
96
97 protected:
98
99 /// The barycentric triangle coordinates for the outer triangles with even id.
100 Vector3 outerBorderBarycentricEven_[3];
101
102 /// The barycentric triangle coordinates for the outer triangles with odd id.
103 Vector3 outerBorderBarycentricOdd_[3];
104
105 /// The diagonal border size of the triangles, in pixels.
106 Scalar diagonalSizePixels_ = Scalar(0);
107};
108
109inline const Vector3& TextureAtlas::outerBorderBarycentric(const Index32 triangleId, const Index32 index) const
110{
111 ocean_assert(index < 3u);
112
113 if (triangleId % 2u == 0u)
114 {
115 return outerBorderBarycentricEven_[index];
116 }
117 else
118 {
119 return outerBorderBarycentricOdd_[index];
120 }
121}
122
123constexpr unsigned int TextureAtlas::textureSizePixels()
124{
125#ifdef OCEAN_PLATFORM_BUILD_MOBILE
126 return 1792u;
127#else
128 return 4096u;
129#endif
130}
131
132constexpr unsigned int TextureAtlas::triangleSizePixels()
133{
134#ifdef OCEAN_PLATFORM_BUILD_MOBILE
135 return 14u;
136#else
137 return 32u;
138#endif
139}
140
142{
143 return Scalar(1.25);
144}
145
146constexpr unsigned int TextureAtlas::numberBins()
147{
149}
150
151constexpr unsigned int TextureAtlas::numberTriangles()
152{
153 constexpr unsigned int bins = numberBins();
154
155 return bins * bins * 2u;
156}
157
158template <bool tInnerTriangleWithBorder>
159void TextureAtlas::triangleAtlasTextureCoordinates(const Index32 meshTriangleId, Vector2* textureCoordinates) const
160{
161 constexpr unsigned int bins = numberBins();
162
163 ocean_assert(meshTriangleId < numberTriangles());
164 ocean_assert(textureCoordinates != nullptr);
165
166 const unsigned int binId = meshTriangleId / 2u;
167
168 const unsigned int xBin = binId % bins;
169 const unsigned int yBin = binId / bins;
170
171 const unsigned int leftBinPixels = xBin * triangleSizePixels();
172 const unsigned int topBinPixels = yBin * triangleSizePixels();
173
174 // ------------ ------------
175 // |\ | |\ |
176 // | \ odd | | \ odd |
177 // | \ | | \ | .....
178 // | even \ | | even \ |
179 // | \ | | \ |
180 // ------------ ------------
181 // ....
182
183 const Scalar leftBinTexture = Scalar(leftBinPixels) / Scalar(textureSizePixels());
184 const Scalar rightBinTexture = Scalar(leftBinPixels + triangleSizePixels()) / Scalar(textureSizePixels());
185
186 const Scalar topBinTexture = Scalar(1) - Scalar(topBinPixels) / Scalar(textureSizePixels());
187 const Scalar bottomBinTexture = Scalar(1) - Scalar(topBinPixels + triangleSizePixels()) / Scalar(textureSizePixels());
188
189 if constexpr (tInnerTriangleWithBorder)
190 {
191 const Scalar smallOffsetTexture = borderSizePixels() / Scalar(textureSizePixels());
192 const Scalar bigOffsetTexture = (borderSizePixels() + diagonalSizePixels_) / Scalar(textureSizePixels());
193
194 if (meshTriangleId % 2u == 0u)
195 {
196 // lower left triangle of bin
197
198 textureCoordinates[0] = Vector2(leftBinTexture + smallOffsetTexture, topBinTexture - bigOffsetTexture);
199 textureCoordinates[1] = Vector2(leftBinTexture + smallOffsetTexture, bottomBinTexture + smallOffsetTexture);
200 textureCoordinates[2] = Vector2(rightBinTexture - bigOffsetTexture, bottomBinTexture + smallOffsetTexture);
201 }
202 else
203 {
204 // upper right triangle of bin
205
206 textureCoordinates[0] = Vector2(leftBinTexture + bigOffsetTexture, topBinTexture - smallOffsetTexture);
207 textureCoordinates[1] = Vector2(rightBinTexture - smallOffsetTexture, bottomBinTexture + bigOffsetTexture);
208 textureCoordinates[2] = Vector2(rightBinTexture - smallOffsetTexture, topBinTexture - smallOffsetTexture);
209 }
210 }
211 else
212 {
213 if (meshTriangleId % 2u == 0u)
214 {
215 // lower left triangle of bin
216
217 textureCoordinates[0] = Vector2(leftBinTexture, topBinTexture);
218 textureCoordinates[1] = Vector2(leftBinTexture, bottomBinTexture);
219 textureCoordinates[2] = Vector2(rightBinTexture, bottomBinTexture);
220 }
221 else
222 {
223 // upper right triangle of bin
224
225 textureCoordinates[0] = Vector2(leftBinTexture, topBinTexture);
226 textureCoordinates[1] = Vector2(rightBinTexture, bottomBinTexture);
227 textureCoordinates[2] = Vector2(rightBinTexture, topBinTexture);
228 }
229 }
230}
231
232}
233
234}
235
236}
237
238#endif // META_OCEAN_TRACKING_MAPTEXTURING_TEXTURE_ATLAS_H
This class implements the abstract base class for all AnyCamera objects.
Definition AnyCamera.h:130
This class implements a texture atlas for triangles with regular shape.
Definition TextureAtlas.h:31
Vector3 outerBorderBarycentricOdd_[3]
The barycentric triangle coordinates for the outer triangles with odd id.
Definition TextureAtlas.h:103
static constexpr unsigned int triangleSizePixels()
Returns the major edge size of all triangles of this atlas.
Definition TextureAtlas.h:132
static constexpr unsigned int textureSizePixels()
Returns the texture size of this atlas.
Definition TextureAtlas.h:123
static constexpr unsigned int numberTriangles()
Returns the number of triangles this atlas can hold.
Definition TextureAtlas.h:151
static constexpr unsigned int numberBins()
Returns the number of bins this atlas holds.
Definition TextureAtlas.h:146
static constexpr Scalar borderSizePixels()
The border size of the triangles.
Definition TextureAtlas.h:141
void triangleCameraTextureCoordiantes(const AnyCamera &anyCamera, const HomogenousMatrix4 &flippedCamera_T_world, const Index32 meshTriangleId, const Vector3 *vertices, Vector2 *textureCoordinates, const bool respectBorder) const
Returns the camera texture coordinates of a triangle within a camera frame.
void triangleAtlasTextureCoordinates(const Index32 meshTriangleId, Vector2 *textureCoordinates) const
Returns the atlas texture coordinates of a triangle within this atlas.
Definition TextureAtlas.h:159
Scalar diagonalSizePixels_
The diagonal border size of the triangles, in pixels.
Definition TextureAtlas.h:106
const Vector3 & outerBorderBarycentric(const Index32 triangleId, const Index32 index) const
Returns the barycentric triangle coordinates for the outer triangle.
Definition TextureAtlas.h:109
Vector3 outerBorderBarycentricEven_[3]
The barycentric triangle coordinates for the outer triangles with even id.
Definition TextureAtlas.h:100
uint32_t Index32
Definition of a 32 bit index value.
Definition Base.h:84
float Scalar
Definition of a scalar type.
Definition Math.h:129
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition Vector2.h:28
The namespace covering the entire Ocean framework.
Definition Accessor.h:15