OpenLexocad  28.0
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 #pragma once
25 #include <Base/Rotation.h>
26 #include <tuple>
27 
28 namespace Base {
29 
30 template <class vecT>
31 struct vec_traits { };
32 
33 template <>
34 struct vec_traits<Vector3f> {
35  typedef Vector3f vec_type;
36  typedef float float_type;
37  vec_traits(const vec_type& v) : v(v){}
38  inline std::tuple<float_type,float_type,float_type> get() const {
39  return std::make_tuple(v.x, v.y, v.z);
40  }
41 private:
42  const vec_type& v;
43 };
44 
45 template <>
46 struct vec_traits<Vector3d> {
47  typedef Vector3d vec_type;
48  typedef double float_type;
49  vec_traits(const vec_type& v) : v(v){}
50  inline std::tuple<float_type,float_type,float_type> get() const {
51  return std::make_tuple(v.x, v.y, v.z);
52  }
53 private:
54  const vec_type& v;
55 };
56 
57 template <>
59  typedef Rotation vec_type;
60  typedef double float_type;
61  vec_traits(const vec_type& v) : v(v){}
62  inline std::tuple<float_type,float_type,float_type,float_type> get() const {
63  float_type q1,q2,q3,q4;
64  v.getValue(q1,q2,q3,q4);
65  return std::make_tuple(q1, q2, q3, q4);
66  }
67 private:
68  const vec_type& v;
69 };
70 
71 // type with three floats
72 template <class _Vec, typename float_type>
73 _Vec make_vec(const std::tuple<float_type, float_type, float_type>&& t) {
74  typedef vec_traits<_Vec> traits_type;
75  typedef typename traits_type::float_type float_traits_type;
76  return _Vec(float_traits_type(std::get<0>(t)),
77  float_traits_type(std::get<1>(t)),
78  float_traits_type(std::get<2>(t)));
79 }
80 
81 // type with four floats
82 template <class _Vec, typename float_type>
83 _Vec make_vec(const std::tuple<float_type, float_type, float_type, float_type>&& t) {
84  typedef vec_traits<_Vec> traits_type;
85  typedef typename traits_type::float_type float_traits_type;
86  return _Vec(float_traits_type(std::get<0>(t)),
87  float_traits_type(std::get<1>(t)),
88  float_traits_type(std::get<2>(t)),
89  float_traits_type(std::get<3>(t)));
90 }
91 
92 template <class _Vec1, class _Vec2>
93 inline _Vec1 convertTo(const _Vec2& v)
94 {
95  typedef vec_traits<_Vec2> traits_type;
96  typedef typename traits_type::float_type float_type;
97  traits_type t(v);
98  auto tuple = t.get();
99  return make_vec<_Vec1, float_type>(std::move(tuple));
100 }
101 
102 }
entt::get
constexpr get_t< Type... > get
Variable template for lists of observed components.
Definition: entt.hpp:6068
Base::vec_traits< Vector3f >::float_type
float float_type
Definition: Converter.h:56
Rotation.h
Base::vec_traits
Definition: Converter.h:51
Base::Vector3
Definition: Vector3D.h:77
Base::Vector3d
Vector3< double > Vector3d
Definition: Vector3D.h:246
Base::Rotation
Definition: Rotation.h:53
Base::make_vec
_Vec make_vec(const std::tuple< float_type, float_type, float_type > &&t)
Definition: Converter.h:93
Base::Vector3f
Vector3< float > Vector3f
Definition: Vector3D.h:245
Base
Definition: AbstractXMLReader.h:5
Base::convertTo
_Vec1 convertTo(const _Vec2 &v)
Definition: Converter.h:113