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
21 float &operator[](int i) { return data[i]; }
22 float operator[](int i) const { return data[i]; }
23
24 private:
25 std::array<float, DIM> data;
26};
27
28template <int DIM> bool operator==(Vec<DIM> const &lhs, Vec<DIM> const &rhs) {
29 for (int i = 0; i < DIM; i++)
30 if (lhs[i] != rhs[i]) return false;
31 return true;
32}
33
34template <int DIM>
35Vec<DIM> operator+(Vec<DIM> const &lhs, Vec<DIM> const &rhs) {
36 Vec<DIM> result;
37 for (int i = 0; i < DIM; i++)
38 result[i] = lhs[i] + rhs[i];
39 return result;
40}
41
42template <int DIM>
43Vec<DIM> operator-(Vec<DIM> const &lhs, Vec<DIM> const &rhs) {
44 Vec<DIM> result;
45 for (int i = 0; i < DIM; i++)
46 result[i] = lhs[i] - rhs[i];
47 return result;
48}
49
50template <int DIM> Vec<DIM> operator*(Vec<DIM> const &lhs, float rhs) {
51 Vec<DIM> result;
52 for (int i = 0; i < DIM; i++)
53 result[i] = lhs[i] * rhs;
54 return result;
55}
56
57template <int DIM> Vec<DIM> operator*(float lhs, Vec<DIM> const &rhs) {
58 return rhs * lhs;
59}
60
61template <int DIM> Vec<DIM> operator/(Vec<DIM> const &lhs, float rhs) {
62 Vec<DIM> result;
63 for (int i = 0; i < DIM; i++)
64 result[i] = lhs[i] / rhs;
65 return result;
66}
67
68template <int DIM> Vec<DIM> &operator+=(Vec<DIM> &lhs, Vec<DIM> const &rhs) {
69 return lhs = lhs + rhs;
70}
71template <int DIM> Vec<DIM> &operator-=(Vec<DIM> &lhs, Vec<DIM> const &rhs) {
72 return lhs = lhs - rhs;
73}
74template <int DIM> Vec<DIM> &operator*=(Vec<DIM> &lhs, float rhs) {
75 return lhs = lhs * rhs;
76}
77template <int DIM> Vec<DIM> &operator/=(Vec<DIM> &lhs, float rhs) {
78 return lhs = lhs / rhs;
79}
80
81template <int DIM> float norm(Vec<DIM> const &v) {
82 float sum = 0.0f;
83 for (int i = 0; i < DIM; i++)
84 sum += v[i] * v[i];
85 return std::sqrt(sum);
86}
87
88template <int DIM> Vec<DIM> &normalize(Vec<DIM> &v) { return v /= norm(v); }
89template <int DIM> Vec<DIM> &vectorize(Vec<DIM> &v, float val) {
90 for (int i = 0; i < DIM; i++)
91 v[i] = val;
92 return v;
93}
94
95template <int DIM> Vec<DIM> &minimize(Vec<DIM> &v1, Vec<DIM> const &v2) {
96 for (int i = 0; i < DIM; i++)
97 v1[i] = std::min(v1[i], v2[i]);
98 return v1;
99}
100
101template <int DIM> Vec<DIM> &maximize(Vec<DIM> &v1, Vec<DIM> const &v2) {
102 for (int i = 0; i < DIM; i++)
103 v1[i] = std::max(v1[i], v2[i]);
104 return v1;
105}
106
107template <int DIM> float dot(Vec<DIM> const &v1, Vec<DIM> const &v2) {
108 float sum = 0.f;
109 for (int i = 0; i < DIM; i++)
110 sum += v1[i] * v2[i];
111 return sum;
112}
113
114inline Vec<3> cross(Vec<3> const &v1, Vec<3> const &v2) {
115 return {v1[1] * v2[2] - v1[2] * v2[1], //
116 v1[2] * v2[0] - v1[0] * v2[2], //
117 v1[0] * v2[1] - v1[1] * v2[0]};
118}
119}
120
121namespace OpenMesh {
122template <int DIM> struct vector_traits<Custom::Vec<DIM>> {
124 using value_type = float;
125 static const size_t size_ = DIM;
126 static size_t size() { return size_; }
127};
128}
129
131 typedef Custom::Vec<3> Point;
132 typedef Custom::Vec<3> Normal;
133
134 typedef Custom::Vec<2> TexCoord2D;
135 typedef Custom::Vec<3> TexCoord3D;
136};
137
138#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 .