OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
VectorT.hh
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 
43 /*===========================================================================*\
44  * *
45  * $Revision$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 //=============================================================================
52 //
53 // CLASS VectorT
54 //
55 //=============================================================================
56 
57 // Don't parse this header file with doxygen since
58 // for some reason (obviously due to a bug in doxygen,
59 // bugreport: https://bugzilla.gnome.org/show_bug.cgi?id=629182)
60 // macro expansion and preprocessor defines
61 // don't work properly.
62 #ifndef DOXYGEN
63 
64 #ifndef OPENMESH_VECTOR_HH
65 #define OPENMESH_VECTOR_HH
66 
67 
68 //== INCLUDES =================================================================
69 
70 #include <OpenMesh/Core/System/config.h>
71 #include <ostream>
72 #include <cmath>
73 #include <cassert>
74 #include <cstring>
75 
76 #if defined(__GNUC__) && defined(__SSE__)
77 #include <xmmintrin.h>
78 #endif
79 
80 #if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
81 #include <array>
82 #include <initializer_list>
83 #include <type_traits>
84 #endif
85 
86 //== NAMESPACES ===============================================================
87 
88 
89 namespace OpenMesh {
90 
91 
92 //== CLASS DEFINITION =========================================================
93 
94 
95 #if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
96 /*
97  * Helpers for VectorT
98  */
99 namespace {
100 template<typename... Ts>
101 struct are_convertible_to;
102 
103 template<typename To, typename From, typename... Froms>
104 struct are_convertible_to<To, From, Froms...> {
105  static constexpr bool value = std::is_convertible<From, To>::value && are_convertible_to<To, Froms...>::value;
106 };
107 template<typename To, typename From>
108 struct are_convertible_to<To, From> : public std::is_convertible<From, To> {};
109 }
110 #endif
111 
112 
122 template<typename Scalar, int N> class VectorDataT {
123  public:
124 #if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
125  VectorDataT() {}
126 
127  template<typename... T>
128  constexpr VectorDataT(T... vs) : values_ {{vs...}} {
129  static_assert(sizeof...(vs) == N,
130  "Incorrect number of vector components supplied.");
131  }
132  std::array<Scalar, N> values_;
133 #else
134  Scalar values_[N];
135 #endif
136 };
137 
138 
139 #if defined(__GNUC__) && defined(__SSE__)
140 
142 template<> class VectorDataT<float, 4> {
143  public:
144 #if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
145  VectorDataT() {}
146 
147  template<typename... T>
148  constexpr VectorDataT(T... vs) : values_ {{vs...}} {
149  static_assert(sizeof...(vs) == 4,
150  "Incorrect number of vector components supplied.");
151  }
152 #endif
153  union {
154  __m128 m128;
155 #if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
156  std::array<float, 4> values_;
157 #else
158  float values_[4];
159 #endif
160  };
161 };
162 
163 #endif
164 
165 
166 
167 
168 //== CLASS DEFINITION =========================================================
169 
170 
171 #define DIM N
172 #define TEMPLATE_HEADER template <typename Scalar, int N>
173 #define CLASSNAME VectorT
174 #define DERIVED VectorDataT<Scalar,N>
175 #define unroll(expr) for (int i=0; i<N; ++i) expr(i)
176 
182 #include "VectorT_inc.hh"
183 
184 #undef DIM
185 #undef TEMPLATE_HEADER
186 #undef CLASSNAME
187 #undef DERIVED
188 #undef unroll
189 
190 
191 
192 
193 //== PARTIAL TEMPLATE SPECIALIZATIONS =========================================
194 #if OM_PARTIAL_SPECIALIZATION
195 
196 
197 #define TEMPLATE_HEADER template <typename Scalar>
198 #define CLASSNAME VectorT<Scalar,DIM>
199 #define DERIVED VectorDataT<Scalar,DIM>
200 
201 
202 #define DIM 2
203 #define unroll(expr) expr(0) expr(1)
204 #define unroll_comb(expr, op) expr(0) op expr(1)
205 #define unroll_csv(expr) expr(0), expr(1)
206 #include "VectorT_inc.hh"
207 #undef DIM
208 #undef unroll
209 #undef unroll_comb
210 #undef unroll_csv
211 
212 
213 #define DIM 3
214 #define unroll(expr) expr(0) expr(1) expr(2)
215 #define unroll_comb(expr, op) expr(0) op expr(1) op expr(2)
216 #define unroll_csv(expr) expr(0), expr(1), expr(2)
217 #include "VectorT_inc.hh"
218 #undef DIM
219 #undef unroll
220 #undef unroll_comb
221 #undef unroll_csv
222 
223 
224 #define DIM 4
225 #define unroll(expr) expr(0) expr(1) expr(2) expr(3)
226 #define unroll_comb(expr, op) expr(0) op expr(1) op expr(2) op expr(3)
227 #define unroll_csv(expr) expr(0), expr(1), expr(2), expr(3)
228 #include "VectorT_inc.hh"
229 #undef DIM
230 #undef unroll
231 #undef unroll_comb
232 #undef unroll_csv
233 
234 #define DIM 5
235 #define unroll(expr) expr(0) expr(1) expr(2) expr(3) expr(4)
236 #define unroll_comb(expr, op) expr(0) op expr(1) op expr(2) op expr(3) op expr(4)
237 #define unroll_csv(expr) expr(0), expr(1), expr(2), expr(3), expr(4)
238 #include "VectorT_inc.hh"
239 #undef DIM
240 #undef unroll
241 #undef unroll_comb
242 #undef unroll_csv
243 
244 #define DIM 6
245 #define unroll(expr) expr(0) expr(1) expr(2) expr(3) expr(4) expr(5)
246 #define unroll_comb(expr, op) expr(0) op expr(1) op expr(2) op expr(3) op expr(4) op expr(5)
247 #define unroll_csv(expr) expr(0), expr(1), expr(2), expr(3), expr(4), expr(5)
248 #include "VectorT_inc.hh"
249 #undef DIM
250 #undef unroll
251 #undef unroll_comb
252 #undef unroll_csv
253 
254 
255 #undef TEMPLATE_HEADER
256 #undef CLASSNAME
257 #undef DERIVED
258 
259 
260 
261 
262 //== FULL TEMPLATE SPECIALIZATIONS ============================================
263 #else
264 
266 template<>
267 inline VectorT<float,3>
269 {
270  return
271  VectorT<float,3>(values_[1]*_rhs.values_[2]-values_[2]*_rhs.values_[1],
272  values_[2]*_rhs.values_[0]-values_[0]*_rhs.values_[2],
273  values_[0]*_rhs.values_[1]-values_[1]*_rhs.values_[0]);
274 }
275 
276 
278 template<>
279 inline VectorT<double,3>
281 {
282  return
283  VectorT<double,3>(values_[1]*_rhs.values_[2]-values_[2]*_rhs.values_[1],
284  values_[2]*_rhs.values_[0]-values_[0]*_rhs.values_[2],
285  values_[0]*_rhs.values_[1]-values_[1]*_rhs.values_[0]);
286 }
287 
288 #endif
289 
290 
291 
292 //== GLOBAL FUNCTIONS =========================================================
293 
294 
297 template<typename Scalar1, typename Scalar2,int N>
298 inline VectorT<Scalar1,N> operator*(Scalar2 _s, const VectorT<Scalar1,N>& _v) {
299  return _v*_s;
300 }
301 
302 
305 template<typename Scalar, int N>
306 inline Scalar
307 dot(const VectorT<Scalar,N>& _v1, const VectorT<Scalar,N>& _v2) {
308  return (_v1 | _v2);
309 }
310 
311 
314 template<typename Scalar, int N>
315 inline VectorT<Scalar,N>
316 cross(const VectorT<Scalar,N>& _v1, const VectorT<Scalar,N>& _v2) {
317  return (_v1 % _v2);
318 }
319 
320 
321 
322 
323 //== TYPEDEFS =================================================================
324 
326 typedef VectorT<signed char,1> Vec1c;
328 typedef VectorT<unsigned char,1> Vec1uc;
330 typedef VectorT<signed short int,1> Vec1s;
332 typedef VectorT<unsigned short int,1> Vec1us;
334 typedef VectorT<signed int,1> Vec1i;
336 typedef VectorT<unsigned int,1> Vec1ui;
338 typedef VectorT<float,1> Vec1f;
340 typedef VectorT<double,1> Vec1d;
341 
343 typedef VectorT<signed char,2> Vec2c;
345 typedef VectorT<unsigned char,2> Vec2uc;
347 typedef VectorT<signed short int,2> Vec2s;
349 typedef VectorT<unsigned short int,2> Vec2us;
351 typedef VectorT<signed int,2> Vec2i;
353 typedef VectorT<unsigned int,2> Vec2ui;
355 typedef VectorT<float,2> Vec2f;
357 typedef VectorT<double,2> Vec2d;
358 
360 typedef VectorT<signed char,3> Vec3c;
362 typedef VectorT<unsigned char,3> Vec3uc;
364 typedef VectorT<signed short int,3> Vec3s;
366 typedef VectorT<unsigned short int,3> Vec3us;
368 typedef VectorT<signed int,3> Vec3i;
370 typedef VectorT<unsigned int,3> Vec3ui;
372 typedef VectorT<float,3> Vec3f;
374 typedef VectorT<double,3> Vec3d;
376 typedef VectorT<bool,3> Vec3b;
377 
379 typedef VectorT<signed char,4> Vec4c;
381 typedef VectorT<unsigned char,4> Vec4uc;
383 typedef VectorT<signed short int,4> Vec4s;
385 typedef VectorT<unsigned short int,4> Vec4us;
387 typedef VectorT<signed int,4> Vec4i;
389 typedef VectorT<unsigned int,4> Vec4ui;
391 typedef VectorT<float,4> Vec4f;
393 typedef VectorT<double,4> Vec4d;
394 
396 typedef VectorT<signed char, 5> Vec5c;
398 typedef VectorT<unsigned char, 5> Vec5uc;
400 typedef VectorT<signed short int, 5> Vec5s;
402 typedef VectorT<unsigned short int, 5> Vec5us;
404 typedef VectorT<signed int, 5> Vec5i;
406 typedef VectorT<unsigned int, 5> Vec5ui;
408 typedef VectorT<float, 5> Vec5f;
410 typedef VectorT<double, 5> Vec5d;
411 
413 typedef VectorT<signed char,6> Vec6c;
415 typedef VectorT<unsigned char,6> Vec6uc;
417 typedef VectorT<signed short int,6> Vec6s;
419 typedef VectorT<unsigned short int,6> Vec6us;
421 typedef VectorT<signed int,6> Vec6i;
423 typedef VectorT<unsigned int,6> Vec6ui;
425 typedef VectorT<float,6> Vec6f;
427 typedef VectorT<double,6> Vec6d;
428 
429 
430 //=============================================================================
431 } // namespace OpenMesh
432 //=============================================================================
433 
434 
435 #if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
436 
444 constexpr OpenMesh::Vec4f operator"" _htmlColor(unsigned long long raw_color) {
445  return OpenMesh::Vec4f(
446  ((raw_color >> 24) & 0xFF) / 255.0f,
447  ((raw_color >> 16) & 0xFF) / 255.0f,
448  ((raw_color >> 8) & 0xFF) / 255.0f,
449  ((raw_color >> 0) & 0xFF) / 255.0f);
450 }
451 #endif
452 
453 #endif // OPENMESH_VECTOR_HH defined
454 //=============================================================================
455 #endif // DOXYGEN
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Definition: VectorAdapter.hh:181
osg::Vec3f cross(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Definition: VectorAdapter.hh:196
Definition: VectorT_inc.hh:72
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
VectorT< Scalar, 3 > operator%(const VectorT< Scalar, 3 > &_rhs) const
cross product: only defined for Vec3* as specialization

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .