Debian Codegraph @

src/Base/Converter.h

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