OpenLexocad  27.1
Converter.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2019 Werner Mayer <wmayer[at]users.sourceforge.net> *
3  * *
4  * This file is part of the FreeCAD CAx development system. *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Library General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Library General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Library General Public *
17  * License along with this library; see the file COPYING.LIB. If not, *
18  * write to the Free Software Foundation, Inc., 51 Franklin Street, *
19  * Fifth Floor, Boston, MA 02110-1301, USA *
20  * *
21  ***************************************************************************/
22 
23 
24 #ifndef BASE_CONVERTER_H
25 #define BASE_CONVERTER_H
26 
27 #include "Vector3D.h"
28 #include "Rotation.h"
29 #include <tuple>
30 
31 namespace Base {
32 
33 template <class vecT>
34 struct vec_traits { };
35 
36 template <>
38  typedef Vector3f vec_type;
39  typedef float float_type;
40  vec_traits(const vec_type& v) : v(v){}
41  inline std::tuple<float_type,float_type,float_type> get() const {
42  return std::make_tuple(v.x, v.y, v.z);
43  }
44 private:
45  const vec_type& v;
46 };
47 
48 template <>
50  typedef Vector3d vec_type;
51  typedef double float_type;
52  vec_traits(const vec_type& v) : v(v){}
53  inline std::tuple<float_type,float_type,float_type> get() const {
54  return std::make_tuple(v.x, v.y, v.z);
55  }
56 private:
57  const vec_type& v;
58 };
59 
60 template <>
62  typedef Rotation vec_type;
63  typedef double float_type;
64  vec_traits(const vec_type& v) : v(v){}
65  inline std::tuple<float_type,float_type,float_type,float_type> get() const {
66  float_type q1,q2,q3,q4;
67  v.getValue(q1,q2,q3,q4);
68  return std::make_tuple(q1, q2, q3, q4);
69  }
70 private:
71  const vec_type& v;
72 };
73 
74 // type with three floats
75 template <class _Vec, typename float_type>
76 _Vec make_vec(const std::tuple<float_type, float_type, float_type>&& t) {
77  typedef vec_traits<_Vec> traits_type;
78  typedef typename traits_type::float_type float_traits_type;
79  return _Vec(float_traits_type(std::get<0>(t)),
80  float_traits_type(std::get<1>(t)),
81  float_traits_type(std::get<2>(t)));
82 }
83 
84 // type with four floats
85 template <class _Vec, typename float_type>
86 _Vec make_vec(const std::tuple<float_type, float_type, float_type, float_type>&& t) {
87  typedef vec_traits<_Vec> traits_type;
88  typedef typename traits_type::float_type float_traits_type;
89  return _Vec(float_traits_type(std::get<0>(t)),
90  float_traits_type(std::get<1>(t)),
91  float_traits_type(std::get<2>(t)),
92  float_traits_type(std::get<3>(t)));
93 }
94 
95 template <class _Vec1, class _Vec2>
96 inline _Vec1 convertTo(const _Vec2& v)
97 {
98  typedef vec_traits<_Vec2> traits_type;
99  typedef typename traits_type::float_type float_type;
100  traits_type t(v);
101  auto tuple = t.get();
102  return make_vec<_Vec1, float_type>(std::move(tuple));
103 }
104 
105 }
106 
107 #endif // BASE_CONVERTER_H
Vector3f vec_type
Definition: Converter.h:38
_Vec1 convertTo(const _Vec2 &v)
Definition: Converter.h:96
_Vec make_vec(const std::tuple< float_type, float_type, float_type > &&t)
Definition: Converter.h:76
Vector3d vec_type
Definition: Converter.h:50
vec_traits(const vec_type &v)
Definition: Converter.h:52
double float_type
Definition: Converter.h:63
double float_type
Definition: Converter.h:51
std::tuple< float_type, float_type, float_type > get() const
Definition: Converter.h:41
Definition: Converter.h:34
Rotation vec_type
Definition: Converter.h:62
float float_type
Definition: Converter.h:39
Definition: AbstractXMLReader.h:5
Definition: Vector3D.h:78
vec_traits(const vec_type &v)
Definition: Converter.h:64
std::tuple< float_type, float_type, float_type, float_type > get() const
Definition: Converter.h:65
vec_traits(const vec_type &v)
Definition: Converter.h:40
Definition: Rotation.h:34
std::tuple< float_type, float_type, float_type > get() const
Definition: Converter.h:53