GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
mesh_prediction_scheme_tex_coords_decoder.h
Go to the documentation of this file.
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#ifdef DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED
16#ifndef DRACO_COMPRESSION_ATTRIBUTES_PREDICTION_SCHEMES_MESH_PREDICTION_SCHEME_TEX_COORDS_DECODER_H_
17#define DRACO_COMPRESSION_ATTRIBUTES_PREDICTION_SCHEMES_MESH_PREDICTION_SCHEME_TEX_COORDS_DECODER_H_
18
19#include <math.h>
20
24#include "draco/core/vector_d.h"
27
28namespace draco {
29
30// Decoder for predictions of UV coordinates encoded by our specialized texture
31// coordinate predictor. See the corresponding encoder for more details. Note
32// that this predictor is not portable and should not be used anymore. See
33// MeshPredictionSchemeTexCoordsPortableEncoder/Decoder for a portable version
34// of this prediction scheme.
35template <typename DataTypeT, class TransformT, class MeshDataT>
36class MeshPredictionSchemeTexCoordsDecoder
37 : public MeshPredictionSchemeDecoder<DataTypeT, TransformT, MeshDataT> {
38 public:
39 using CorrType = typename MeshPredictionSchemeDecoder<DataTypeT, TransformT,
40 MeshDataT>::CorrType;
41 MeshPredictionSchemeTexCoordsDecoder(const PointAttribute *attribute,
42 const TransformT &transform,
43 const MeshDataT &mesh_data, int version)
44 : MeshPredictionSchemeDecoder<DataTypeT, TransformT, MeshDataT>(
45 attribute, transform, mesh_data),
46 pos_attribute_(nullptr),
47 entry_to_point_id_map_(nullptr),
48 num_components_(0),
49 version_(version) {}
50
51 bool ComputeOriginalValues(const CorrType *in_corr, DataTypeT *out_data,
52 int size, int num_components,
53 const PointIndex *entry_to_point_id_map) override;
54
55 bool DecodePredictionData(DecoderBuffer *buffer) override;
56
57 PredictionSchemeMethod GetPredictionMethod() const override {
59 }
60
61 bool IsInitialized() const override {
62 if (pos_attribute_ == nullptr) {
63 return false;
64 }
65 if (!this->mesh_data().IsInitialized()) {
66 return false;
67 }
68 return true;
69 }
70
71 int GetNumParentAttributes() const override { return 1; }
72
73 GeometryAttribute::Type GetParentAttributeType(int i) const override {
74 DRACO_DCHECK_EQ(i, 0);
75 (void)i;
76 return GeometryAttribute::POSITION;
77 }
78
79 bool SetParentAttribute(const PointAttribute *att) override {
80 if (att == nullptr) {
81 return false;
82 }
83 if (att->attribute_type() != GeometryAttribute::POSITION) {
84 return false; // Invalid attribute type.
85 }
86 if (att->num_components() != 3) {
87 return false; // Currently works only for 3 component positions.
88 }
89 pos_attribute_ = att;
90 return true;
91 }
92
93 protected:
94 Vector3f GetPositionForEntryId(int entry_id) const {
95 const PointIndex point_id = entry_to_point_id_map_[entry_id];
96 Vector3f pos;
97 pos_attribute_->ConvertValue(pos_attribute_->mapped_index(point_id),
98 &pos[0]);
99 return pos;
100 }
101
102 Vector2f GetTexCoordForEntryId(int entry_id, const DataTypeT *data) const {
103 const int data_offset = entry_id * num_components_;
104 return Vector2f(static_cast<float>(data[data_offset]),
105 static_cast<float>(data[data_offset + 1]));
106 }
107
108 bool ComputePredictedValue(CornerIndex corner_id, const DataTypeT *data,
109 int data_id);
110
111 private:
112 const PointAttribute *pos_attribute_;
113 const PointIndex *entry_to_point_id_map_;
114 std::unique_ptr<DataTypeT[]> predicted_value_;
115 int num_components_;
116 // Encoded / decoded array of UV flips.
117 std::vector<bool> orientations_;
118 int version_;
119};
120
121template <typename DataTypeT, class TransformT, class MeshDataT>
122bool MeshPredictionSchemeTexCoordsDecoder<DataTypeT, TransformT, MeshDataT>::
123 ComputeOriginalValues(const CorrType *in_corr, DataTypeT *out_data,
124 int /* size */, int num_components,
125 const PointIndex *entry_to_point_id_map) {
126 if (num_components != 2) {
127 // Corrupt/malformed input. Two output components are req'd.
128 return false;
129 }
130 num_components_ = num_components;
131 entry_to_point_id_map_ = entry_to_point_id_map;
132 predicted_value_ =
133 std::unique_ptr<DataTypeT[]>(new DataTypeT[num_components]);
134 this->transform().Init(num_components);
135
136 const int corner_map_size =
137 static_cast<int>(this->mesh_data().data_to_corner_map()->size());
138 for (int p = 0; p < corner_map_size; ++p) {
139 const CornerIndex corner_id = this->mesh_data().data_to_corner_map()->at(p);
140 if (!ComputePredictedValue(corner_id, out_data, p)) {
141 return false;
142 }
143
144 const int dst_offset = p * num_components;
145 this->transform().ComputeOriginalValue(
146 predicted_value_.get(), in_corr + dst_offset, out_data + dst_offset);
147 }
148 return true;
149}
150
151template <typename DataTypeT, class TransformT, class MeshDataT>
152bool MeshPredictionSchemeTexCoordsDecoder<DataTypeT, TransformT, MeshDataT>::
153 DecodePredictionData(DecoderBuffer *buffer) {
154 // Decode the delta coded orientations.
155 uint32_t num_orientations = 0;
156 if (buffer->bitstream_version() < DRACO_BITSTREAM_VERSION(2, 2)) {
157 if (!buffer->Decode(&num_orientations)) {
158 return false;
159 }
160 } else {
161 if (!DecodeVarint(&num_orientations, buffer)) {
162 return false;
163 }
164 }
165 if (num_orientations == 0) {
166 return false;
167 }
168 if (num_orientations > this->mesh_data().corner_table()->num_corners()) {
169 // We can't have more orientations than the maximum number of decoded
170 // values.
171 return false;
172 }
173 orientations_.resize(num_orientations);
174 bool last_orientation = true;
175 RAnsBitDecoder decoder;
176 if (!decoder.StartDecoding(buffer)) {
177 return false;
178 }
179 for (uint32_t i = 0; i < num_orientations; ++i) {
180 if (!decoder.DecodeNextBit()) {
181 last_orientation = !last_orientation;
182 }
183 orientations_[i] = last_orientation;
184 }
185 decoder.EndDecoding();
186 return MeshPredictionSchemeDecoder<DataTypeT, TransformT,
187 MeshDataT>::DecodePredictionData(buffer);
188}
189
190template <typename DataTypeT, class TransformT, class MeshDataT>
191bool MeshPredictionSchemeTexCoordsDecoder<DataTypeT, TransformT, MeshDataT>::
192 ComputePredictedValue(CornerIndex corner_id, const DataTypeT *data,
193 int data_id) {
194 // Compute the predicted UV coordinate from the positions on all corners
195 // of the processed triangle. For the best prediction, the UV coordinates
196 // on the next/previous corners need to be already encoded/decoded.
197 const CornerIndex next_corner_id =
198 this->mesh_data().corner_table()->Next(corner_id);
199 const CornerIndex prev_corner_id =
200 this->mesh_data().corner_table()->Previous(corner_id);
201 // Get the encoded data ids from the next and previous corners.
202 // The data id is the encoding order of the UV coordinates.
203 int next_data_id, prev_data_id;
204
205 int next_vert_id, prev_vert_id;
206 next_vert_id =
207 this->mesh_data().corner_table()->Vertex(next_corner_id).value();
208 prev_vert_id =
209 this->mesh_data().corner_table()->Vertex(prev_corner_id).value();
210
211 next_data_id = this->mesh_data().vertex_to_data_map()->at(next_vert_id);
212 prev_data_id = this->mesh_data().vertex_to_data_map()->at(prev_vert_id);
213
214 if (prev_data_id < data_id && next_data_id < data_id) {
215 // Both other corners have available UV coordinates for prediction.
216 const Vector2f n_uv = GetTexCoordForEntryId(next_data_id, data);
217 const Vector2f p_uv = GetTexCoordForEntryId(prev_data_id, data);
218 if (p_uv == n_uv) {
219 // We cannot do a reliable prediction on degenerated UV triangles.
220 // Technically floats > INT_MAX are undefined, but compilers will
221 // convert those values to INT_MIN. We are being explicit here for asan.
222 for (const int i : {0, 1}) {
223 if (std::isnan(p_uv[i]) || static_cast<double>(p_uv[i]) > INT_MAX ||
224 static_cast<double>(p_uv[i]) < INT_MIN) {
225 predicted_value_[i] = INT_MIN;
226 } else {
227 predicted_value_[i] = static_cast<int>(p_uv[i]);
228 }
229 }
230 return true;
231 }
232
233 // Get positions at all corners.
234 const Vector3f tip_pos = GetPositionForEntryId(data_id);
235 const Vector3f next_pos = GetPositionForEntryId(next_data_id);
236 const Vector3f prev_pos = GetPositionForEntryId(prev_data_id);
237 // Use the positions of the above triangle to predict the texture coordinate
238 // on the tip corner C.
239 // Convert the triangle into a new coordinate system defined by orthogonal
240 // bases vectors S, T, where S is vector prev_pos - next_pos and T is an
241 // perpendicular vector to S in the same plane as vector the
242 // tip_pos - next_pos.
243 // The transformed triangle in the new coordinate system is then going to
244 // be represented as:
245 //
246 // 1 ^
247 // |
248 // |
249 // | C
250 // | / \
251 // | / \
252 // |/ \
253 // N--------------P
254 // 0 1
255 //
256 // Where next_pos point (N) is at position (0, 0), prev_pos point (P) is
257 // at (1, 0). Our goal is to compute the position of the tip_pos point (C)
258 // in this new coordinate space (s, t).
259 //
260 const Vector3f pn = prev_pos - next_pos;
261 const Vector3f cn = tip_pos - next_pos;
262 const float pn_norm2_squared = pn.SquaredNorm();
263 // Coordinate s of the tip corner C is simply the dot product of the
264 // normalized vectors |pn| and |cn| (normalized by the length of |pn|).
265 // Since both of these vectors are normalized, we don't need to perform the
266 // normalization explicitly and instead we can just use the squared norm
267 // of |pn| as a denominator of the resulting dot product of non normalized
268 // vectors.
269 float s, t;
270 // |pn_norm2_squared| can be exactly 0 when the next_pos and prev_pos are
271 // the same positions (e.g. because they were quantized to the same
272 // location).
273 if (version_ < DRACO_BITSTREAM_VERSION(1, 2) || pn_norm2_squared > 0) {
274 s = pn.Dot(cn) / pn_norm2_squared;
275 // To get the coordinate t, we can use formula:
276 // t = |C-N - (P-N) * s| / |P-N|
277 // Do not use std::sqrt to avoid changes in the bitstream.
278 t = sqrt((cn - pn * s).SquaredNorm() / pn_norm2_squared);
279 } else {
280 s = 0;
281 t = 0;
282 }
283
284 // Now we need to transform the point (s, t) to the texture coordinate space
285 // UV. We know the UV coordinates on points N and P (N_UV and P_UV). Lets
286 // denote P_UV - N_UV = PN_UV. PN_UV is then 2 dimensional vector that can
287 // be used to define transformation from the normalized coordinate system
288 // to the texture coordinate system using a 3x3 affine matrix M:
289 //
290 // M = | PN_UV[0] -PN_UV[1] N_UV[0] |
291 // | PN_UV[1] PN_UV[0] N_UV[1] |
292 // | 0 0 1 |
293 //
294 // The predicted point C_UV in the texture space is then equal to
295 // C_UV = M * (s, t, 1). Because the triangle in UV space may be flipped
296 // around the PN_UV axis, we also need to consider point C_UV' = M * (s, -t)
297 // as the prediction.
298 const Vector2f pn_uv = p_uv - n_uv;
299 const float pnus = pn_uv[0] * s + n_uv[0];
300 const float pnut = pn_uv[0] * t;
301 const float pnvs = pn_uv[1] * s + n_uv[1];
302 const float pnvt = pn_uv[1] * t;
303 Vector2f predicted_uv;
304 if (orientations_.empty()) {
305 return false;
306 }
307
308 // When decoding the data, we already know which orientation to use.
309 const bool orientation = orientations_.back();
310 orientations_.pop_back();
311 if (orientation) {
312 predicted_uv = Vector2f(pnus - pnvt, pnvs + pnut);
313 } else {
314 predicted_uv = Vector2f(pnus + pnvt, pnvs - pnut);
315 }
316 if (std::is_integral<DataTypeT>::value) {
317 // Round the predicted value for integer types.
318 // Technically floats > INT_MAX are undefined, but compilers will
319 // convert those values to INT_MIN. We are being explicit here for asan.
320 const double u = floor(predicted_uv[0] + 0.5);
321 if (std::isnan(u) || u > INT_MAX || u < INT_MIN) {
322 predicted_value_[0] = INT_MIN;
323 } else {
324 predicted_value_[0] = static_cast<int>(u);
325 }
326 const double v = floor(predicted_uv[1] + 0.5);
327 if (std::isnan(v) || v > INT_MAX || v < INT_MIN) {
328 predicted_value_[1] = INT_MIN;
329 } else {
330 predicted_value_[1] = static_cast<int>(v);
331 }
332 } else {
333 predicted_value_[0] = static_cast<int>(predicted_uv[0]);
334 predicted_value_[1] = static_cast<int>(predicted_uv[1]);
335 }
336
337 return true;
338 }
339 // Else we don't have available textures on both corners. For such case we
340 // can't use positions for predicting the uv value and we resort to delta
341 // coding.
342 int data_offset = 0;
343 if (prev_data_id < data_id) {
344 // Use the value on the previous corner as the prediction.
345 data_offset = prev_data_id * num_components_;
346 }
347 if (next_data_id < data_id) {
348 // Use the value on the next corner as the prediction.
349 data_offset = next_data_id * num_components_;
350 } else {
351 // None of the other corners have a valid value. Use the last encoded value
352 // as the prediction if possible.
353 if (data_id > 0) {
354 data_offset = (data_id - 1) * num_components_;
355 } else {
356 // We are encoding the first value. Predict 0.
357 for (int i = 0; i < num_components_; ++i) {
358 predicted_value_[i] = 0;
359 }
360 return true;
361 }
362 }
363 for (int i = 0; i < num_components_; ++i) {
364 predicted_value_[i] = data[data_offset + i];
365 }
366 return true;
367}
368
369} // namespace draco
370
371#endif // DRACO_COMPRESSION_ATTRIBUTES_PREDICTION_SCHEMES_MESH_PREDICTION_SCHEME_TEX_COORDS_DECODER_H_
372#endif
void floor(const FCube &pos, FLine &l1, FLine &l2, FLine &l3, FLine &l4, FPlane &pl, FaceStore &s)
Definition Functions.cc:95
Definition decoder_buffer.h:30
Definition mesh_prediction_scheme_decoder.h:28
Definition rans_bit_decoder.h:28
void EndDecoding()
Definition rans_bit_decoder.h:44
#define DRACO_DCHECK_EQ(a, b)
Definition macros.h:81
#define DRACO_BITSTREAM_VERSION(MAJOR, MINOR)
Definition macros.h:114
lazy data
Definition meshpage.php:1583
n< br > font size
Definition meshpage.php:3536
Definition attribute_octahedron_transform.cc:21
bool DecodeVarint(IntTypeT *out_val, DecoderBuffer *buffer)
Definition varint_decoding.h:63
VectorD< float, 3 > Vector3f
Definition vector_d.h:340
VectorD< float, 2 > Vector2f
Definition vector_d.h:339
CornerIndex CornerIndex
Definition mesh_edgebreaker_encoder_impl.cc:35
PredictionSchemeMethod
Definition compression_shared.h:80
@ MESH_PREDICTION_TEX_COORDS_DEPRECATED
Definition compression_shared.h:88