Changeset View
Changeset View
Standalone View
Standalone View
extern/draco/draco/src/draco/core/vector_d.h
- This file was moved from extern/draco/dracoenc/src/draco/core/vector_d.h.
| Show All 10 Lines | |||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| // See the License for the specific language governing permissions and | // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | // limitations under the License. | ||||
| // | // | ||||
| #ifndef DRACO_CORE_VECTOR_D_H_ | #ifndef DRACO_CORE_VECTOR_D_H_ | ||||
| #define DRACO_CORE_VECTOR_D_H_ | #define DRACO_CORE_VECTOR_D_H_ | ||||
| #include <inttypes.h> | #include <inttypes.h> | ||||
| #include <algorithm> | #include <algorithm> | ||||
| #include <array> | #include <array> | ||||
| #include <cmath> | #include <cmath> | ||||
| #include "draco/core/macros.h" | #include "draco/core/macros.h" | ||||
| namespace draco { | namespace draco { | ||||
| // D-dimensional vector class with basic operations. | // D-dimensional vector class with basic operations. | ||||
| template <class ScalarT, int dimension_t> | template <class ScalarT, int dimension_t> | ||||
| class VectorD { | class VectorD { | ||||
| public: | public: | ||||
| static constexpr int dimension = dimension_t; | static constexpr int dimension = dimension_t; | ||||
| typedef ScalarT Scalar; | typedef ScalarT Scalar; | ||||
| typedef VectorD<Scalar, dimension_t> Self; | typedef VectorD<Scalar, dimension_t> Self; | ||||
| // TODO(hemmer): Deprecate. | // TODO(hemmer): Deprecate. | ||||
| typedef ScalarT CoefficientType; | typedef ScalarT CoefficientType; | ||||
| VectorD() { | VectorD() { | ||||
| for (int i = 0; i < dimension; ++i) | for (int i = 0; i < dimension; ++i) { | ||||
| (*this)[i] = Scalar(0); | (*this)[i] = Scalar(0); | ||||
| } | } | ||||
| } | |||||
| // The following constructor does not compile in opt mode, which for now led | // The following constructor does not compile in opt mode, which for now led | ||||
| // to the constructors further down, which is not ideal. | // to the constructors further down, which is not ideal. | ||||
| // TODO(hemmer): fix constructor below and remove others. | // TODO(hemmer): fix constructor below and remove others. | ||||
| // template <typename... Args> | // template <typename... Args> | ||||
| // explicit VectorD(Args... args) : v_({args...}) {} | // explicit VectorD(Args... args) : v_({args...}) {} | ||||
| VectorD(const Scalar &c0, const Scalar &c1) : v_({{c0, c1}}) { | VectorD(const Scalar &c0, const Scalar &c1) : v_({{c0, c1}}) { | ||||
| Show All 28 Lines | public: | ||||
| VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2, | VectorD(const Scalar &c0, const Scalar &c1, const Scalar &c2, | ||||
| const Scalar &c3, const Scalar &c4, const Scalar &c5, | const Scalar &c3, const Scalar &c4, const Scalar &c5, | ||||
| const Scalar &c6) | const Scalar &c6) | ||||
| : v_({{c0, c1, c2, c3, c4, c5, c6}}) { | : v_({{c0, c1, c2, c3, c4, c5, c6}}) { | ||||
| DRACO_DCHECK_EQ(dimension, 7); | DRACO_DCHECK_EQ(dimension, 7); | ||||
| } | } | ||||
| VectorD(const Self &o) { | VectorD(const Self &o) { | ||||
| for (int i = 0; i < dimension; ++i) | for (int i = 0; i < dimension; ++i) { | ||||
| (*this)[i] = o[i]; | (*this)[i] = o[i]; | ||||
| } | } | ||||
| } | |||||
| // Constructs the vector from another vector with a different data type or a | // Constructs the vector from another vector with a different data type or a | ||||
| // different number of components. If the |src_vector| has more components | // different number of components. If the |src_vector| has more components | ||||
| // than |this| vector, the excess components are truncated. If the | // than |this| vector, the excess components are truncated. If the | ||||
| // |src_vector| has fewer components than |this| vector, the remaining | // |src_vector| has fewer components than |this| vector, the remaining | ||||
| // components are padded with 0. | // components are padded with 0. | ||||
| // Note that the constructor is intentionally explicit to avoid accidental | // Note that the constructor is intentionally explicit to avoid accidental | ||||
| // conversions between different vector types. | // conversions between different vector types. | ||||
| template <class OtherScalarT, int other_dimension_t> | template <class OtherScalarT, int other_dimension_t> | ||||
| explicit VectorD(const VectorD<OtherScalarT, other_dimension_t> &src_vector) { | explicit VectorD(const VectorD<OtherScalarT, other_dimension_t> &src_vector) { | ||||
| for (int i = 0; i < dimension; ++i) { | for (int i = 0; i < dimension; ++i) { | ||||
| if (i < other_dimension_t) | if (i < other_dimension_t) { | ||||
| v_[i] = Scalar(src_vector[i]); | v_[i] = Scalar(src_vector[i]); | ||||
| else | } else { | ||||
| v_[i] = Scalar(0); | v_[i] = Scalar(0); | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| Scalar &operator[](int i) { return v_[i]; } | Scalar &operator[](int i) { return v_[i]; } | ||||
| const Scalar &operator[](int i) const { return v_[i]; } | const Scalar &operator[](int i) const { return v_[i]; } | ||||
| // TODO(hemmer): remove. | // TODO(hemmer): remove. | ||||
| // Similar to interface of Eigen library. | // Similar to interface of Eigen library. | ||||
| Scalar &operator()(int i) { return v_[i]; } | Scalar &operator()(int i) { return v_[i]; } | ||||
| const Scalar &operator()(int i) const { return v_[i]; } | const Scalar &operator()(int i) const { return v_[i]; } | ||||
| Show All 18 Lines | public: | ||||
| Self operator-(const Self &o) const { | Self operator-(const Self &o) const { | ||||
| Self ret; | Self ret; | ||||
| for (int i = 0; i < dimension; ++i) { | for (int i = 0; i < dimension; ++i) { | ||||
| ret[i] = (*this)[i] - o[i]; | ret[i] = (*this)[i] - o[i]; | ||||
| } | } | ||||
| return ret; | 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 operator*(const Scalar &o) const { | ||||
| Self ret; | Self ret; | ||||
| for (int i = 0; i < dimension; ++i) { | for (int i = 0; i < dimension; ++i) { | ||||
| ret[i] = (*this)[i] * o; | ret[i] = (*this)[i] * o; | ||||
| } | } | ||||
| return ret; | return ret; | ||||
| } | } | ||||
| Show All 18 Lines | Self operator-(const Scalar &o) const { | ||||
| for (int i = 0; i < dimension; ++i) { | for (int i = 0; i < dimension; ++i) { | ||||
| ret[i] = (*this)[i] - o; | ret[i] = (*this)[i] - o; | ||||
| } | } | ||||
| return ret; | return ret; | ||||
| } | } | ||||
| bool operator==(const Self &o) const { | bool operator==(const Self &o) const { | ||||
| for (int i = 0; i < dimension; ++i) { | for (int i = 0; i < dimension; ++i) { | ||||
| if ((*this)[i] != o[i]) | if ((*this)[i] != o[i]) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | |||||
| return true; | return true; | ||||
| } | } | ||||
| bool operator!=(const Self &x) const { return !((*this) == x); } | bool operator!=(const Self &x) const { return !((*this) == x); } | ||||
| bool operator<(const Self &x) const { | bool operator<(const Self &x) const { | ||||
| for (int i = 0; i < dimension - 1; ++i) { | for (int i = 0; i < dimension - 1; ++i) { | ||||
| if (v_[i] < x.v_[i]) | if (v_[i] < x.v_[i]) { | ||||
| return true; | return true; | ||||
| if (v_[i] > x.v_[i]) | } | ||||
| if (v_[i] > x.v_[i]) { | |||||
| return false; | return false; | ||||
| } | } | ||||
| } | |||||
| // Only one check needed for the last dimension. | // Only one check needed for the last dimension. | ||||
| if (v_[dimension - 1] < x.v_[dimension - 1]) | if (v_[dimension - 1] < x.v_[dimension - 1]) { | ||||
| return true; | return true; | ||||
| } | |||||
| return false; | return false; | ||||
| } | } | ||||
| // Functions. | // Functions. | ||||
| Scalar SquaredNorm() const { return this->Dot(*this); } | Scalar SquaredNorm() const { return this->Dot(*this); } | ||||
| // Computes L1, the sum of absolute values of all entries. | // Computes L1, the sum of absolute values of all entries. | ||||
| Scalar AbsSum() const { | Scalar AbsSum() const { | ||||
| Show All 17 Lines | void Normalize() { | ||||
| if (magnitude == 0) { | if (magnitude == 0) { | ||||
| return; | return; | ||||
| } | } | ||||
| for (int i = 0; i < dimension; ++i) { | for (int i = 0; i < dimension; ++i) { | ||||
| (*this)[i] /= magnitude; | (*this)[i] /= magnitude; | ||||
| } | } | ||||
| } | } | ||||
| Self GetNormalized() const { | |||||
| Self ret(*this); | |||||
| ret.Normalize(); | |||||
| return ret; | |||||
| } | |||||
| const Scalar &MaxCoeff() const { | const Scalar &MaxCoeff() const { | ||||
| return *std::max_element(v_.begin(), v_.end()); | return *std::max_element(v_.begin(), v_.end()); | ||||
| } | } | ||||
| const Scalar &MinCoeff() const { | const Scalar &MinCoeff() const { | ||||
| return *std::min_element(v_.begin(), v_.end()); | return *std::min_element(v_.begin(), v_.end()); | ||||
| } | } | ||||
| Scalar *data() { return &(v_[0]); } | Scalar *data() { return &(v_[0]); } | ||||
| const Scalar *data() const { return &(v_[0]); } | |||||
| private: | private: | ||||
| std::array<Scalar, dimension> v_; | std::array<Scalar, dimension> v_; | ||||
| }; | }; | ||||
| // Scalar multiplication from the other side too. | // Scalar multiplication from the other side too. | ||||
| template <class ScalarT, int dimension_t> | template <class ScalarT, int dimension_t> | ||||
| VectorD<ScalarT, dimension_t> operator*( | VectorD<ScalarT, dimension_t> operator*( | ||||
| ▲ Show 20 Lines • Show All 64 Lines • Show Last 20 Lines | |||||