OpenMesh
unittests_common_customtraits.hh
1#ifndef UNITTESTS_COMMON_DUMMYTRAITS
2#define UNITTESTS_COMMON_DUMMYTRAITS
3#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
4#include <OpenMesh/Core/Utils/color_cast.hh>
5#include <array>
6
7namespace Custom {
8
11template <int DIM> class Vec {
12 public:
13 // Constructor with DIM components
14 Vec(float x) : data({ x }) {}
15 Vec(float x, float y) : data({ x, y }) {}
16 Vec(float x, float y, float z) : data({{ x, y, z }}) {}
17 Vec(float x, float y, float z, float w) : data({ x, y, z, w }) {}
18
19 Vec() = default;
20 Vec(Vec<DIM> const &) = default;
21
22 float &operator[](int i) { return data[i]; }
23 float operator[](int i) const { return data[i]; }
24
25 private:
26 std::array<float, DIM> data;
27};
28
29template <int DIM> bool operator==(Vec<DIM> const &lhs, Vec<DIM> const &rhs) {
30 for (int i = 0; i < DIM; i++)
31 if (lhs[i] != rhs[i]) return false;
32 return true;
33}
34
35template <int DIM>
36Vec<DIM> operator+(Vec<DIM> const &lhs, Vec<DIM> const &rhs) {
37 Vec<DIM> result;
38 for (int i = 0; i < DIM; i++)
39 result[i] = lhs[i] + rhs[i];
40 return result;
41}
42
43template <int DIM>
44Vec<DIM> operator-(Vec<DIM> const &lhs, Vec<DIM> const &rhs) {
45 Vec<DIM> result;
46 for (int i = 0; i < DIM; i++)
47 result[i] = lhs[i] - rhs[i];
48 return result;
49}
50
51template <int DIM> Vec<DIM> operator*(Vec<DIM> const &lhs, float rhs) {
52 Vec<DIM> result;
53 for (int i = 0; i < DIM; i++)
54 result[i] = lhs[i] * rhs;
55 return result;
56}
57
58template <int DIM> Vec<DIM> operator*(float lhs, Vec<DIM> const &rhs) {
59 return rhs * lhs;
60}
61
62template <int DIM> Vec<DIM> operator/(Vec<DIM> const &lhs, float rhs) {
63 Vec<DIM> result;
64 for (int i = 0; i < DIM; i++)
65 result[i] = lhs[i] / rhs;
66 return result;
67}
68
69template <int DIM> Vec<DIM> &operator+=(Vec<DIM> &lhs, Vec<DIM> const &rhs) {
70 return lhs = lhs + rhs;
71}
72template <int DIM> Vec<DIM> &operator-=(Vec<DIM> &lhs, Vec<DIM> const &rhs) {
73 return lhs = lhs - rhs;
74}
75template <int DIM> Vec<DIM> &operator*=(Vec<DIM> &lhs, float rhs) {
76 return lhs = lhs * rhs;
77}
78template <int DIM> Vec<DIM> &operator/=(Vec<DIM> &lhs, float rhs) {
79 return lhs = lhs / rhs;
80}
81
82template <int DIM> float norm(Vec<DIM> const &v) {
83 float sum = 0.0f;
84 for (int i = 0; i < DIM; i++)
85 sum += v[i] * v[i];
86 return std::sqrt(sum);
87}
88
89template <int DIM> Vec<DIM> &normalize(Vec<DIM> &v) { return v /= norm(v); }
90template <int DIM> Vec<DIM> &vectorize(Vec<DIM> &v, float val) {
91 for (int i = 0; i < DIM; i++)
92 v[i] = val;
93 return v;
94}
95
96template <int DIM> Vec<DIM> &minimize(Vec<DIM> &v1, Vec<DIM> const &v2) {
97 for (int i = 0; i < DIM; i++)
98 v1[i] = std::min(v1[i], v2[i]);
99 return v1;
100}
101
102template <int DIM> Vec<DIM> &maximize(Vec<DIM> &v1, Vec<DIM> const &v2) {
103 for (int i = 0; i < DIM; i++)
104 v1[i] = std::max(v1[i], v2[i]);
105 return v1;
106}
107
108template <int DIM> float dot(Vec<DIM> const &v1, Vec<DIM> const &v2) {
109 float sum = 0.f;
110 for (int i = 0; i < DIM; i++)
111 sum += v1[i] * v2[i];
112 return sum;
113}
114
115inline Vec<3> cross(Vec<3> const &v1, Vec<3> const &v2) {
116 return {v1[1] * v2[2] - v1[2] * v2[1], //
117 v1[2] * v2[0] - v1[0] * v2[2], //
118 v1[0] * v2[1] - v1[1] * v2[0]};
119}
120}
121
122namespace OpenMesh {
123template <int DIM> struct vector_traits<Custom::Vec<DIM>> {
125 using value_type = float;
126 static const size_t size_ = DIM;
127 static size_t size() { return size_; }
128};
129}
130
132 typedef Custom::Vec<3> Point;
133 typedef Custom::Vec<3> Normal;
134
135 typedef Custom::Vec<2> TexCoord2D;
136 typedef Custom::Vec<3> TexCoord3D;
137};
138
139#endif // UNITTESTS_COMMON_DUMMYTRAITS
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
auto operator*(const OtherScalar &_s, const VectorT< Scalar, DIM > &rhs) -> decltype(rhs.operator*(_s))
Component wise multiplication from the left.
Definition: Vector11T.hh:693
Base class for all traits.
Definition: Traits.hh:122
Helper class providing information about a vector type.
Definition: vector_traits.hh:89
static const size_t size_
size/dimension of the vector
Definition: vector_traits.hh:97
static size_t size()
size/dimension of the vector
Definition: vector_traits.hh:100
Definition: unittests_common.hh:17
A Vector class with the absolute minimum of built-in methods to test the interface expected from Vect...
Definition: unittests_common_customtraits.hh:11

Project OpenMesh, ©  Visual Computing Institute, RWTH Aachen. Documentation generated using doxygen .