Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vector_d.h « core « draco « src « draco « draco « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a0ec2dedf2e8777777d50d0551763a4ad4b42cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2016 The Draco Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef DRACO_CORE_VECTOR_D_H_
#define DRACO_CORE_VECTOR_D_H_

#include <inttypes.h>

#include <algorithm>
#include <array>
#include <cmath>
#include <limits>

#include "draco/core/macros.h"

namespace draco {
// D-dimensional vector class with basic operations.
template <class ScalarT, int dimension_t>
class VectorD {
 public:
  static constexpr int dimension = dimension_t;

  typedef ScalarT Scalar;
  typedef VectorD<Scalar, dimension_t> Self;

  // TODO(b/199760123): Deprecate.
  typedef ScalarT CoefficientType;

  VectorD() {
    for (int i = 0; i < dimension; ++i) {
      (*this)[i] = Scalar(0);
    }
  }

  // The following constructor does not compile in opt mode, which for now led
  // to the constructors further down, which is not ideal.
  // TODO(b/199760123): Fix constructor below and remove others.
  // template <typename... Args>
  // explicit VectorD(Args... args) : v_({args...}) {}

  VectorD(const Scalar &c0, const Scalar &c1) : v_({{c0, c1}}) {
    DRACO_DCHECK_EQ(dimension, 2);
    v_[0] = c0;
    v_[1] = c1;
  }

  VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2)
      : v_({{c0, c1, c2}}) {
    DRACO_DCHECK_EQ(dimension, 3);
  }

  VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2,
          const Scalar &c3)
      : v_({{c0, c1, c2, c3}}) {
    DRACO_DCHECK_EQ(dimension, 4);
  }

  VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2,
          const Scalar &c3, const Scalar &c4)
      : v_({{c0, c1, c2, c3, c4}}) {
    DRACO_DCHECK_EQ(dimension, 5);
  }

  VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2,
          const Scalar &c3, const Scalar &c4, const Scalar &c5)
      : v_({{c0, c1, c2, c3, c4, c5}}) {
    DRACO_DCHECK_EQ(dimension, 6);
  }

  VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2,
          const Scalar &c3, const Scalar &c4, const Scalar &c5,
          const Scalar &c6)
      : v_({{c0, c1, c2, c3, c4, c5, c6}}) {
    DRACO_DCHECK_EQ(dimension, 7);
  }

  VectorD(const Self &o) {
    for (int i = 0; i < dimension; ++i) {
      (*this)[i] = o[i];
    }
  }

  // Constructs the vector from another vector with a different data type or a
  // different number of components. If the |src_vector| has more components
  // than |this| vector, the excess components are truncated. If the
  // |src_vector| has fewer components than |this| vector, the remaining
  // components are padded with 0.
  // Note that the constructor is intentionally explicit to avoid accidental
  // conversions between different vector types.
  template <class OtherScalarT, int other_dimension_t>
  explicit VectorD(const VectorD<OtherScalarT, other_dimension_t> &src_vector) {
    for (int i = 0; i < dimension; ++i) {
      if (i < other_dimension_t) {
        v_[i] = Scalar(src_vector[i]);
      } else {
        v_[i] = Scalar(0);
      }
    }
  }

  Scalar &operator[](int i) { return v_[i]; }
  const Scalar &operator[](int i) const { return v_[i]; }
  // TODO(b/199760123): Remove.
  // Similar to interface of Eigen library.
  Scalar &operator()(int i) { return v_[i]; }
  const Scalar &operator()(int i) const { return v_[i]; }

  // Unary operators.
  Self operator-() const {
    Self ret;
    for (int i = 0; i < dimension; ++i) {
      ret[i] = -(*this)[i];
    }
    return ret;
  }

  // Binary operators.
  Self operator+(const Self &o) const {
    Self ret;
    for (int i = 0; i < dimension; ++i) {
      ret[i] = (*this)[i] + o[i];
    }
    return ret;
  }

  Self operator-(const Self &o) const {
    Self ret;
    for (int i = 0; i < dimension; ++i) {
      ret[i] = (*this)[i] - o[i];
    }
    return ret;
  }

  Self operator*(const Self &o) const {
    Self ret;
    for (int i = 0; i < dimension; ++i) {
      ret[i] = (*this)[i] * o[i];
    }
    return ret;
  }

  Self &operator+=(const Self &o) {
    for (int i = 0; i < dimension; ++i) {
      (*this)[i] += o[i];
    }
    return *this;
  }

  Self &operator-=(const Self &o) {
    for (int i = 0; i < dimension; ++i) {
      (*this)[i] -= o[i];
    }
    return *this;
  }

  Self &operator*=(const Self &o) {
    for (int i = 0; i < dimension; ++i) {
      (*this)[i] *= o[i];
    }
    return *this;
  }

  Self operator*(const Scalar &o) const {
    Self ret;
    for (int i = 0; i < dimension; ++i) {
      ret[i] = (*this)[i] * o;
    }
    return ret;
  }

  Self operator/(const Scalar &o) const {
    Self ret;
    for (int i = 0; i < dimension; ++i) {
      ret[i] = (*this)[i] / o;
    }
    return ret;
  }

  Self operator+(const Scalar &o) const {
    Self ret;
    for (int i = 0; i < dimension; ++i) {
      ret[i] = (*this)[i] + o;
    }
    return ret;
  }

  Self operator-(const Scalar &o) const {
    Self ret;
    for (int i = 0; i < dimension; ++i) {
      ret[i] = (*this)[i] - o;
    }
    return ret;
  }

  bool operator==(const Self &o) const {
    for (int i = 0; i < dimension; ++i) {
      if ((*this)[i] != o[i]) {
        return false;
      }
    }
    return true;
  }

  bool operator!=(const Self &x) const { return !((*this) == x); }

  bool operator<(const Self &x) const {
    for (int i = 0; i < dimension - 1; ++i) {
      if (v_[i] < x.v_[i]) {
        return true;
      }
      if (v_[i] > x.v_[i]) {
        return false;
      }
    }
    // Only one check needed for the last dimension.
    if (v_[dimension - 1] < x.v_[dimension - 1]) {
      return true;
    }
    return false;
  }

  // Functions.
  Scalar SquaredNorm() const { return this->Dot(*this); }

  // Computes L1, the sum of absolute values of all entries.
  Scalar AbsSum() const {
    Scalar result(0);
    for (int i = 0; i < dimension; ++i) {
      Scalar next_value = std::abs(v_[i]);
      if (result > std::numeric_limits<Scalar>::max() - next_value) {
        // Return the max if adding would have caused an overflow.
        return std::numeric_limits<Scalar>::max();
      }
      result += next_value;
    }
    return result;
  }

  Scalar Dot(const Self &o) const {
    Scalar ret(0);
    for (int i = 0; i < dimension; ++i) {
      ret += (*this)[i] * o[i];
    }
    return ret;
  }

  void Normalize() {
    const Scalar magnitude = std::sqrt(this->SquaredNorm());
    if (magnitude == 0) {
      return;
    }
    for (int i = 0; i < dimension; ++i) {
      (*this)[i] /= magnitude;
    }
  }

  Self GetNormalized() const {
    Self ret(*this);
    ret.Normalize();
    return ret;
  }

  const Scalar &MaxCoeff() const {
    return *std::max_element(v_.begin(), v_.end());
  }

  const Scalar &MinCoeff() const {
    return *std::min_element(v_.begin(), v_.end());
  }

  Scalar *data() { return &(v_[0]); }
  const Scalar *data() const { return &(v_[0]); }

 private:
  std::array<Scalar, dimension> v_;
};

// Scalar multiplication from the other side too.
template <class ScalarT, int dimension_t>
VectorD<ScalarT, dimension_t> operator*(
    const ScalarT &o, const VectorD<ScalarT, dimension_t> &v) {
  return v * o;
}

// Calculates the squared distance between two points.
template <class ScalarT, int dimension_t>
ScalarT SquaredDistance(const VectorD<ScalarT, dimension_t> &v1,
                        const VectorD<ScalarT, dimension_t> &v2) {
  ScalarT difference;
  ScalarT squared_distance = 0;
  // Check each index separately so difference is never negative and underflow
  // is avoided for unsigned types.
  for (int i = 0; i < dimension_t; ++i) {
    if (v1[i] >= v2[i]) {
      difference = v1[i] - v2[i];
    } else {
      difference = v2[i] - v1[i];
    }
    squared_distance += (difference * difference);
  }
  return squared_distance;
}

// Global function computing the cross product of two 3D vectors.
template <class ScalarT>
VectorD<ScalarT, 3> CrossProduct(const VectorD<ScalarT, 3> &u,
                                 const VectorD<ScalarT, 3> &v) {
  // Preventing accidental use with uint32_t and the like.
  static_assert(std::is_signed<ScalarT>::value,
                "ScalarT must be a signed type. ");
  VectorD<ScalarT, 3> r;
  r[0] = (u[1] * v[2]) - (u[2] * v[1]);
  r[1] = (u[2] * v[0]) - (u[0] * v[2]);
  r[2] = (u[0] * v[1]) - (u[1] * v[0]);
  return r;
}

template <class ScalarT, int dimension_t>
inline std::ostream &operator<<(
    std::ostream &out, const draco::VectorD<ScalarT, dimension_t> &vec) {
  for (int i = 0; i < dimension_t - 1; ++i) {
    out << vec[i] << " ";
  }
  out << vec[dimension_t - 1];
  return out;
}

typedef VectorD<float, 2> Vector2f;
typedef VectorD<float, 3> Vector3f;
typedef VectorD<float, 4> Vector4f;
typedef VectorD<float, 5> Vector5f;
typedef VectorD<float, 6> Vector6f;
typedef VectorD<float, 7> Vector7f;

typedef VectorD<uint32_t, 2> Vector2ui;
typedef VectorD<uint32_t, 3> Vector3ui;
typedef VectorD<uint32_t, 4> Vector4ui;
typedef VectorD<uint32_t, 5> Vector5ui;
typedef VectorD<uint32_t, 6> Vector6ui;
typedef VectorD<uint32_t, 7> Vector7ui;

}  // namespace draco

#endif  // DRACO_CORE_VECTOR_D_H_