GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
mesh_prediction_scheme_geometric_normal_predictor_area.h
Go to the documentation of this file.
1// Copyright 2017 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#ifndef DRACO_COMPRESSION_ATTRIBUTES_PREDICTION_SCHEMES_MESH_PREDICTION_SCHEME_GEOMETRIC_NORMAL_PREDICTOR_AREA_H_
16#define DRACO_COMPRESSION_ATTRIBUTES_PREDICTION_SCHEMES_MESH_PREDICTION_SCHEME_GEOMETRIC_NORMAL_PREDICTOR_AREA_H_
17
19
20namespace draco {
21
22// This predictor estimates the normal via the surrounding triangles of the
23// given corner. Triangles are weighted according to their area.
24template <typename DataTypeT, class TransformT, class MeshDataT>
27 DataTypeT, TransformT, MeshDataT> {
29 DataTypeT, TransformT, MeshDataT>
30 Base;
31
32 public:
34 : Base(md) {
36 };
38
39 // Computes predicted octahedral coordinates on a given corner.
41 DataTypeT *prediction) override {
43 typedef typename MeshDataT::CornerTable CornerTable;
44 const CornerTable *const corner_table = this->mesh_data_.corner_table();
45 // Going to compute the predicted normal from the surrounding triangles
46 // according to the connectivity of the given corner table.
47 VertexCornersIterator<CornerTable> cit(corner_table, corner_id);
48 // Position of central vertex does not change in loop.
49 const VectorD<int64_t, 3> pos_cent = this->GetPositionForCorner(corner_id);
50 // Computing normals for triangles and adding them up.
51
53 CornerIndex c_next, c_prev;
54 while (!cit.End()) {
55 // Getting corners.
57 c_next = corner_table->Next(corner_id);
58 c_prev = corner_table->Previous(corner_id);
59 } else {
60 c_next = corner_table->Next(cit.Corner());
61 c_prev = corner_table->Previous(cit.Corner());
62 }
63 const VectorD<int64_t, 3> pos_next = this->GetPositionForCorner(c_next);
64 const VectorD<int64_t, 3> pos_prev = this->GetPositionForCorner(c_prev);
65
66 // Computing delta vectors to next and prev.
67 const VectorD<int64_t, 3> delta_next = pos_next - pos_cent;
68 const VectorD<int64_t, 3> delta_prev = pos_prev - pos_cent;
69
70 // Computing cross product.
71 const VectorD<int64_t, 3> cross = CrossProduct(delta_next, delta_prev);
72
73 // Prevent signed integer overflows by doing math as unsigned.
74 auto normal_data = reinterpret_cast<uint64_t *>(normal.data());
75 auto cross_data = reinterpret_cast<const uint64_t *>(cross.data());
76 normal_data[0] = normal_data[0] + cross_data[0];
77 normal_data[1] = normal_data[1] + cross_data[1];
78 normal_data[2] = normal_data[2] + cross_data[2];
79
80 cit.Next();
81 }
82
83 // Convert to int32_t, make sure entries are not too large.
84 constexpr int64_t upper_bound = 1 << 29;
86 const int32_t abs_sum = static_cast<int32_t>(normal.AbsSum());
87 if (abs_sum > upper_bound) {
88 const int64_t quotient = abs_sum / upper_bound;
89 normal = normal / quotient;
90 }
91 } else {
92 const int64_t abs_sum = normal.AbsSum();
93 if (abs_sum > upper_bound) {
94 const int64_t quotient = abs_sum / upper_bound;
95 normal = normal / quotient;
96 }
97 }
98 DRACO_DCHECK_LE(normal.AbsSum(), upper_bound);
99 prediction[0] = static_cast<int32_t>(normal[0]);
100 prediction[1] = static_cast<int32_t>(normal[1]);
101 prediction[2] = static_cast<int32_t>(normal[2]);
102 }
104 if (mode == ONE_TRIANGLE) {
105 this->normal_prediction_mode_ = mode;
106 return true;
107 } else if (mode == TRIANGLE_AREA) {
108 this->normal_prediction_mode_ = mode;
109 return true;
110 }
111 return false;
112 }
113};
114
115} // namespace draco
116
117#endif // DRACO_COMPRESSION_ATTRIBUTES_PREDICTION_SCHEMES_MESH_PREDICTION_SCHEME_GEOMETRIC_NORMAL_PREDICTOR_AREA_H_
Definition corner_table.h:53
CornerIndex Next(CornerIndex corner) const
Definition corner_table.h:89
CornerIndex Previous(CornerIndex corner) const
Definition corner_table.h:95
MeshPredictionSchemeGeometricNormalPredictorArea(const MeshDataT &md)
Definition mesh_prediction_scheme_geometric_normal_predictor_area.h:33
virtual ~MeshPredictionSchemeGeometricNormalPredictorArea()
Definition mesh_prediction_scheme_geometric_normal_predictor_area.h:37
bool SetNormalPredictionMode(NormalPredictionMode mode) override
Definition mesh_prediction_scheme_geometric_normal_predictor_area.h:103
void ComputePredictedValue(CornerIndex corner_id, DataTypeT *prediction) override
Definition mesh_prediction_scheme_geometric_normal_predictor_area.h:40
MeshDataT mesh_data_
Definition mesh_prediction_scheme_geometric_normal_predictor_base.h:90
VectorD< int64_t, 3 > GetPositionForCorner(CornerIndex ci) const
Definition mesh_prediction_scheme_geometric_normal_predictor_base.h:71
NormalPredictionMode normal_prediction_mode_
Definition mesh_prediction_scheme_geometric_normal_predictor_base.h:91
bool IsInitialized() const
Definition mesh_prediction_scheme_geometric_normal_predictor_base.h:47
MeshPredictionSchemeGeometricNormalPredictorBase(const MeshDataT &md)
Definition mesh_prediction_scheme_geometric_normal_predictor_base.h:34
Definition vector_d.h:30
Scalar AbsSum() const
Definition vector_d.h:237
Scalar * data()
Definition vector_d.h:282
Definition corner_table_iterators.h:210
bool End() const
Definition corner_table_iterators.h:244
CornerIndex Corner() const
Definition corner_table_iterators.h:241
void Next()
Definition corner_table_iterators.h:247
#define DRACO_DCHECK(x)
Definition macros.h:80
#define DRACO_DCHECK_LE(a, b)
Definition macros.h:85
Definition attribute_octahedron_transform.cc:21
NormalPredictionMode
Definition compression_shared.h:138
@ ONE_TRIANGLE
Definition compression_shared.h:139
@ TRIANGLE_AREA
Definition compression_shared.h:140
CornerIndex CornerIndex
Definition mesh_edgebreaker_encoder_impl.cc:35
VectorD< ScalarT, 3 > CrossProduct(const VectorD< ScalarT, 3 > &u, const VectorD< ScalarT, 3 > &v)
Definition vector_d.h:317