OpenMesh: Unittests/unittests_common_customtraits.hh Source File

| 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
7 namespace Custom {
8
11 template <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
29 template <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
35 template <int DIM>
36 Vec<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
43 template <int DIM>
44 Vec<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
51 template <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
58 template <int DIM> Vec<DIM> operator*(float lhs, Vec<DIM> const &rhs) {
59 return rhs * lhs;
60 }
61
62 template <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
69 template <int DIM> Vec<DIM> &operator+=(Vec<DIM> &lhs, Vec<DIM> const &rhs) {
70 return lhs = lhs + rhs;
71 }
72 template <int DIM> Vec<DIM> &operator-=(Vec<DIM> &lhs, Vec<DIM> const &rhs) {
73 return lhs = lhs - rhs;
74 }
75 template <int DIM> Vec<DIM> &operator*=(Vec<DIM> &lhs, float rhs) {
76 return lhs = lhs * rhs;
77 }
78 template <int DIM> Vec<DIM> &operator/=(Vec<DIM> &lhs, float rhs) {
79 return lhs = lhs / rhs;
80 }
81
82 template <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
89 template <int DIM> Vec<DIM> &normalize(Vec<DIM> &v) { return v /= norm(v); }
90 template <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
96 template <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
102 template <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
108 template <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
115 inline 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
122 namespace OpenMesh {
123 template <int DIM> struct vector_traits<Custom::Vec<DIM>> {
124 using vector_type = 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
131 struct CustomTraits : public OpenMesh::DefaultTraits {
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
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
OpenMesh::operator*
auto operator*(const OtherScalar &_s, const VectorT< Scalar, DIM > &rhs) -> decltype(rhs.operator*(_s))
Component wise multiplication from the left.
Definition: Vector11T.hh:692
OpenMesh::DefaultTraits
Base class for all traits.
Definition: Traits.hh:122
OpenMesh::vector_traits
Helper class providing information about a vector type.
Definition: vector_traits.hh:89
OpenMesh::vector_traits::size_
static const size_t size_
size/dimension of the vector
Definition: vector_traits.hh:97
OpenMesh::vector_traits::size
static size_t size()
size/dimension of the vector
Definition: vector_traits.hh:100
CustomTraits
Definition: unittests_common.hh:17
Custom::Vec
A Vector class with the absolute minimum of built-in methods to test the interface expected from Vect...
Definition: unittests_common_customtraits.hh:11