GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
vector_d.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#ifndef DRACO_CORE_VECTOR_D_H_
16#define DRACO_CORE_VECTOR_D_H_
17
18#include <inttypes.h>
19
20#include <algorithm>
21#include <array>
22#include <cmath>
23#include <limits>
24
25#include "draco/core/macros.h"
26
27namespace draco {
28// D-dimensional vector class with basic operations.
29template <class ScalarT, int dimension_t>
30class VectorD {
31 public:
32 static constexpr int dimension = dimension_t;
33
34 typedef ScalarT Scalar;
36
37 // TODO(b/199760123): Deprecate.
38 typedef ScalarT CoefficientType;
39
41 for (int i = 0; i < dimension; ++i) {
42 (*this)[i] = Scalar(0);
43 }
44 }
45
46 // The following constructor does not compile in opt mode, which for now led
47 // to the constructors further down, which is not ideal.
48 // TODO(b/199760123): Fix constructor below and remove others.
49 // template <typename... Args>
50 // explicit VectorD(Args... args) : v_({args...}) {}
51
52 VectorD(const Scalar &c0, const Scalar &c1) : v_({{c0, c1}}) {
54 v_[0] = c0;
55 v_[1] = c1;
56 }
57
58 VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2)
59 : v_({{c0, c1, c2}}) {
61 }
62
63 VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2,
64 const Scalar &c3)
65 : v_({{c0, c1, c2, c3}}) {
67 }
68
69 VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2,
70 const Scalar &c3, const Scalar &c4)
71 : v_({{c0, c1, c2, c3, c4}}) {
73 }
74
75 VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2,
76 const Scalar &c3, const Scalar &c4, const Scalar &c5)
77 : v_({{c0, c1, c2, c3, c4, c5}}) {
79 }
80
81 VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2,
82 const Scalar &c3, const Scalar &c4, const Scalar &c5,
83 const Scalar &c6)
84 : v_({{c0, c1, c2, c3, c4, c5, c6}}) {
86 }
87
88 VectorD(const Self &o) {
89 for (int i = 0; i < dimension; ++i) {
90 (*this)[i] = o[i];
91 }
92 }
93
94 // Constructs the vector from another vector with a different data type or a
95 // different number of components. If the |src_vector| has more components
96 // than |this| vector, the excess components are truncated. If the
97 // |src_vector| has fewer components than |this| vector, the remaining
98 // components are padded with 0.
99 // Note that the constructor is intentionally explicit to avoid accidental
100 // conversions between different vector types.
101 template <class OtherScalarT, int other_dimension_t>
103 for (int i = 0; i < dimension; ++i) {
104 if (i < other_dimension_t) {
105 v_[i] = Scalar(src_vector[i]);
106 } else {
107 v_[i] = Scalar(0);
108 }
109 }
110 }
111
112 Scalar &operator[](int i) { return v_[i]; }
113 const Scalar &operator[](int i) const { return v_[i]; }
114 // TODO(b/199760123): Remove.
115 // Similar to interface of Eigen library.
116 Scalar &operator()(int i) { return v_[i]; }
117 const Scalar &operator()(int i) const { return v_[i]; }
118
119 // Unary operators.
120 Self operator-() const {
121 Self ret;
122 for (int i = 0; i < dimension; ++i) {
123 ret[i] = -(*this)[i];
124 }
125 return ret;
126 }
127
128 // Binary operators.
129 Self operator+(const Self &o) const {
130 Self ret;
131 for (int i = 0; i < dimension; ++i) {
132 ret[i] = (*this)[i] + o[i];
133 }
134 return ret;
135 }
136
137 Self operator-(const Self &o) const {
138 Self ret;
139 for (int i = 0; i < dimension; ++i) {
140 ret[i] = (*this)[i] - o[i];
141 }
142 return ret;
143 }
144
145 Self operator*(const Self &o) const {
146 Self ret;
147 for (int i = 0; i < dimension; ++i) {
148 ret[i] = (*this)[i] * o[i];
149 }
150 return ret;
151 }
152
153 Self &operator+=(const Self &o) {
154 for (int i = 0; i < dimension; ++i) {
155 (*this)[i] += o[i];
156 }
157 return *this;
158 }
159
160 Self &operator-=(const Self &o) {
161 for (int i = 0; i < dimension; ++i) {
162 (*this)[i] -= o[i];
163 }
164 return *this;
165 }
166
167 Self &operator*=(const Self &o) {
168 for (int i = 0; i < dimension; ++i) {
169 (*this)[i] *= o[i];
170 }
171 return *this;
172 }
173
174 Self operator*(const Scalar &o) const {
175 Self ret;
176 for (int i = 0; i < dimension; ++i) {
177 ret[i] = (*this)[i] * o;
178 }
179 return ret;
180 }
181
182 Self operator/(const Scalar &o) const {
183 Self ret;
184 for (int i = 0; i < dimension; ++i) {
185 ret[i] = (*this)[i] / o;
186 }
187 return ret;
188 }
189
190 Self operator+(const Scalar &o) const {
191 Self ret;
192 for (int i = 0; i < dimension; ++i) {
193 ret[i] = (*this)[i] + o;
194 }
195 return ret;
196 }
197
198 Self operator-(const Scalar &o) const {
199 Self ret;
200 for (int i = 0; i < dimension; ++i) {
201 ret[i] = (*this)[i] - o;
202 }
203 return ret;
204 }
205
206 bool operator==(const Self &o) const {
207 for (int i = 0; i < dimension; ++i) {
208 if ((*this)[i] != o[i]) {
209 return false;
210 }
211 }
212 return true;
213 }
214
215 bool operator!=(const Self &x) const { return !((*this) == x); }
216
217 bool operator<(const Self &x) const {
218 for (int i = 0; i < dimension - 1; ++i) {
219 if (v_[i] < x.v_[i]) {
220 return true;
221 }
222 if (v_[i] > x.v_[i]) {
223 return false;
224 }
225 }
226 // Only one check needed for the last dimension.
227 if (v_[dimension - 1] < x.v_[dimension - 1]) {
228 return true;
229 }
230 return false;
231 }
232
233 // Functions.
234 Scalar SquaredNorm() const { return this->Dot(*this); }
235
236 // Computes L1, the sum of absolute values of all entries.
237 Scalar AbsSum() const {
238 Scalar result(0);
239 for (int i = 0; i < dimension; ++i) {
240 Scalar next_value = std::abs(v_[i]);
241 if (result > std::numeric_limits<Scalar>::max() - next_value) {
242 // Return the max if adding would have caused an overflow.
243 return std::numeric_limits<Scalar>::max();
244 }
245 result += next_value;
246 }
247 return result;
248 }
249
250 Scalar Dot(const Self &o) const {
251 Scalar ret(0);
252 for (int i = 0; i < dimension; ++i) {
253 ret += (*this)[i] * o[i];
254 }
255 return ret;
256 }
257
258 void Normalize() {
259 const Scalar magnitude = std::sqrt(this->SquaredNorm());
260 if (magnitude == 0) {
261 return;
262 }
263 for (int i = 0; i < dimension; ++i) {
264 (*this)[i] /= magnitude;
265 }
266 }
267
269 Self ret(*this);
270 ret.Normalize();
271 return ret;
272 }
273
274 const Scalar &MaxCoeff() const {
275 return *std::max_element(v_.begin(), v_.end());
276 }
277
278 const Scalar &MinCoeff() const {
279 return *std::min_element(v_.begin(), v_.end());
280 }
281
282 Scalar *data() { return &(v_[0]); }
283 const Scalar *data() const { return &(v_[0]); }
284
285 private:
286 std::array<Scalar, dimension> v_;
287};
288
289// Scalar multiplication from the other side too.
290template <class ScalarT, int dimension_t>
292 const ScalarT &o, const VectorD<ScalarT, dimension_t> &v) {
293 return v * o;
294}
295
296// Calculates the squared distance between two points.
297template <class ScalarT, int dimension_t>
300 ScalarT difference;
301 ScalarT squared_distance = 0;
302 // Check each index separately so difference is never negative and underflow
303 // is avoided for unsigned types.
304 for (int i = 0; i < dimension_t; ++i) {
305 if (v1[i] >= v2[i]) {
306 difference = v1[i] - v2[i];
307 } else {
308 difference = v2[i] - v1[i];
309 }
310 squared_distance += (difference * difference);
311 }
312 return squared_distance;
313}
314
315// Global function computing the cross product of two 3D vectors.
316template <class ScalarT>
318 const VectorD<ScalarT, 3> &v) {
319 // Preventing accidental use with uint32_t and the like.
320 static_assert(std::is_signed<ScalarT>::value,
321 "ScalarT must be a signed type. ");
323 r[0] = (u[1] * v[2]) - (u[2] * v[1]);
324 r[1] = (u[2] * v[0]) - (u[0] * v[2]);
325 r[2] = (u[0] * v[1]) - (u[1] * v[0]);
326 return r;
327}
328
329template <class ScalarT, int dimension_t>
330inline std::ostream &operator<<(
331 std::ostream &out, const draco::VectorD<ScalarT, dimension_t> &vec) {
332 for (int i = 0; i < dimension_t - 1; ++i) {
333 out << vec[i] << " ";
334 }
335 out << vec[dimension_t - 1];
336 return out;
337}
338
345
352
353} // namespace draco
354
355#endif // DRACO_CORE_VECTOR_D_H_
Definition vector_d.h:30
VectorD(const Self &o)
Definition vector_d.h:88
VectorD()
Definition vector_d.h:40
Self operator+(const Scalar &o) const
Definition vector_d.h:190
Self & operator-=(const Self &o)
Definition vector_d.h:160
const Scalar & MinCoeff() const
Definition vector_d.h:278
Self & operator+=(const Self &o)
Definition vector_d.h:153
VectorD(const Scalar &c0, const Scalar &c1)
Definition vector_d.h:52
Scalar & operator()(int i)
Definition vector_d.h:116
const Scalar & operator()(int i) const
Definition vector_d.h:117
Self operator-(const Self &o) const
Definition vector_d.h:137
VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2, const Scalar &c3, const Scalar &c4, const Scalar &c5)
Definition vector_d.h:75
Scalar & operator[](int i)
Definition vector_d.h:112
Self operator-() const
Definition vector_d.h:120
bool operator!=(const Self &x) const
Definition vector_d.h:215
VectorD< Scalar, dimension_t > Self
Definition vector_d.h:35
VectorD(const VectorD< OtherScalarT, other_dimension_t > &src_vector)
Definition vector_d.h:102
Scalar AbsSum() const
Definition vector_d.h:237
Self GetNormalized() const
Definition vector_d.h:268
Self & operator*=(const Self &o)
Definition vector_d.h:167
Self operator-(const Scalar &o) const
Definition vector_d.h:198
Self operator*(const Scalar &o) const
Definition vector_d.h:174
Scalar * data()
Definition vector_d.h:282
static constexpr int dimension
Definition vector_d.h:32
bool operator==(const Self &o) const
Definition vector_d.h:206
const Scalar & MaxCoeff() const
Definition vector_d.h:274
Scalar Dot(const Self &o) const
Definition vector_d.h:250
VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2, const Scalar &c3)
Definition vector_d.h:63
const Scalar * data() const
Definition vector_d.h:283
VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2, const Scalar &c3, const Scalar &c4)
Definition vector_d.h:69
Scalar SquaredNorm() const
Definition vector_d.h:234
Self operator/(const Scalar &o) const
Definition vector_d.h:182
DataTypeT Scalar
Definition vector_d.h:34
VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2, const Scalar &c3, const Scalar &c4, const Scalar &c5, const Scalar &c6)
Definition vector_d.h:81
Self operator*(const Self &o) const
Definition vector_d.h:145
void Normalize()
Definition vector_d.h:258
bool operator<(const Self &x) const
Definition vector_d.h:217
DataTypeT CoefficientType
Definition vector_d.h:38
VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2)
Definition vector_d.h:58
Self operator+(const Self &o) const
Definition vector_d.h:129
const Scalar & operator[](int i) const
Definition vector_d.h:113
#define DRACO_DCHECK_EQ(a, b)
Definition macros.h:81
ret($return)
Definition mesh_login_result.php:5
Definition attribute_octahedron_transform.cc:21
VectorD< ScalarT, dimension_t > operator*(const ScalarT &o, const VectorD< ScalarT, dimension_t > &v)
Definition vector_d.h:291
VectorD< uint32_t, 5 > Vector5ui
Definition vector_d.h:349
VectorD< float, 5 > Vector5f
Definition vector_d.h:342
VectorD< float, 3 > Vector3f
Definition vector_d.h:340
VectorD< float, 2 > Vector2f
Definition vector_d.h:339
VectorD< uint32_t, 7 > Vector7ui
Definition vector_d.h:351
VectorD< float, 7 > Vector7f
Definition vector_d.h:344
VectorD< float, 6 > Vector6f
Definition vector_d.h:343
VectorD< uint32_t, 2 > Vector2ui
Definition vector_d.h:346
VectorD< uint32_t, 6 > Vector6ui
Definition vector_d.h:350
VectorD< uint32_t, 3 > Vector3ui
Definition vector_d.h:347
VectorD< uint32_t, 4 > Vector4ui
Definition vector_d.h:348
ScalarT SquaredDistance(const VectorD< ScalarT, dimension_t > &v1, const VectorD< ScalarT, dimension_t > &v2)
Definition vector_d.h:298
VectorD< ScalarT, 3 > CrossProduct(const VectorD< ScalarT, 3 > &u, const VectorD< ScalarT, 3 > &v)
Definition vector_d.h:317
std::ostream & operator<<(std::ostream &os, IndexType< ValueTypeT, TagT > index)
Definition draco_index_type.h:165
VectorD< float, 4 > Vector4f
Definition vector_d.h:341