From 9f7075bb8f07c1dac9ece4f905a1d3f23eb2f07b Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 6 Sep 2019 21:29:51 +0200 Subject: [PATCH] extend convertTo function to also support SbRotation/Base::Rotation --- src/Base/CMakeLists.txt | 1 + src/Base/Converter.h | 110 ++++++++++++++++++ src/Base/Vector3D.h | 40 ------- src/Gui/Utilities.h | 39 +++++-- src/Mod/Mesh/App/FeatureMeshSegmentByMesh.cpp | 1 + src/Mod/Mesh/App/MeshProperties.cpp | 1 + src/Mod/Mesh/App/MeshPyImp.cpp | 1 + src/Mod/MeshPart/App/AppMeshPartPy.cpp | 1 + src/Mod/MeshPart/Gui/Command.cpp | 1 + src/Mod/MeshPart/Gui/CurveOnMesh.cpp | 1 + src/Mod/Part/App/Tools.h | 25 ++-- src/Mod/Points/App/PointsAlgos.cpp | 1 + src/Mod/Points/App/Properties.cpp | 1 + .../App/AppReverseEngineering.cpp | 1 + src/Mod/ReverseEngineering/Gui/Command.cpp | 1 + src/Mod/Sketcher/Gui/ViewProviderSketch.cpp | 1 + 16 files changed, 162 insertions(+), 64 deletions(-) create mode 100644 src/Base/Converter.h diff --git a/src/Base/CMakeLists.txt b/src/Base/CMakeLists.txt index 9124fa0eb7..dc982dfd20 100644 --- a/src/Base/CMakeLists.txt +++ b/src/Base/CMakeLists.txt @@ -303,6 +303,7 @@ SET(FreeCADBase_HPP_SRCS BoundBox.h Builder3D.h Console.h + Converter.h CoordinateSystem.h Debugger.h Exception.h diff --git a/src/Base/Converter.h b/src/Base/Converter.h new file mode 100644 index 0000000000..a061336718 --- /dev/null +++ b/src/Base/Converter.h @@ -0,0 +1,110 @@ +/*************************************************************************** + * Copyright (c) 2019 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 51 Franklin Street, * + * Fifth Floor, Boston, MA 02110-1301, USA * + * * + ***************************************************************************/ + + +#ifndef BASE_CONVERTER_H +#define BASE_CONVERTER_H + +#include "Vector3D.h" +#include "Rotation.h" +#include + +namespace Base { + +template +struct vec_traits { }; + +template <> +struct vec_traits { + typedef Vector3f vec_type; + typedef float float_type; + vec_traits(const vec_type& v) : v(v){} + inline std::tuple get() const { + return std::make_tuple(v.x, v.y, v.z); + } +private: + const vec_type& v; +}; + +template <> +struct vec_traits { + typedef Vector3d vec_type; + typedef double float_type; + vec_traits(const vec_type& v) : v(v){} + inline std::tuple get() const { + return std::make_tuple(v.x, v.y, v.z); + } +private: + const vec_type& v; +}; + +template <> +struct vec_traits { + typedef Rotation vec_type; + typedef double float_type; + vec_traits(const vec_type& v) : v(v){} + inline std::tuple get() const { + float_type q1,q2,q3,q4; + v.getValue(q1,q2,q3,q4); + return std::make_tuple(q1, q2, q3, q4); + } +private: + const vec_type& v; +}; + +// type with three floats +template +_Vec make_vec(const std::tuple&& t) { + typedef vec_traits<_Vec> traits_type; + typedef typename traits_type::float_type float_traits_type; + return _Vec(float_traits_type(std::get<0>(t)), + float_traits_type(std::get<1>(t)), + float_traits_type(std::get<2>(t))); +} + +// type with four floats +template +_Vec make_vec(const std::tuple&& t) { + typedef vec_traits<_Vec> traits_type; + typedef typename traits_type::float_type float_traits_type; + return _Vec(float_traits_type(std::get<0>(t)), + float_traits_type(std::get<1>(t)), + float_traits_type(std::get<2>(t)), + float_traits_type(std::get<3>(t))); +} + +template +inline _Vec1 convertTo(const _Vec2& v) +{ + typedef _Vec1 out_type; + typedef _Vec2 inp_type; + typedef vec_traits traits_type; + typedef vec_traits traits_out; + typedef typename traits_out::float_type float_type; + traits_type t(v); + auto tuple = t.get(); + return make_vec<_Vec1, typename traits_type::float_type>(std::move(tuple)); +} + +} + +#endif // BASE_CONVERTER_H diff --git a/src/Base/Vector3D.h b/src/Base/Vector3D.h index 52ffd47867..5363428b4b 100644 --- a/src/Base/Vector3D.h +++ b/src/Base/Vector3D.h @@ -244,46 +244,6 @@ inline Vector3<_Pr1> toVector(const Vector3<_Pr2>& v) typedef Vector3 Vector3f; typedef Vector3 Vector3d; -template -struct vec_traits { }; - -template <> -struct vec_traits { - typedef Vector3f vec_type; - typedef float float_type; - vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v.x; } - inline float_type y() { return v.y; } - inline float_type z() { return v.z; } -private: - const vec_type& v; -}; - -template <> -struct vec_traits { - typedef Vector3d vec_type; - typedef double float_type; - vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v.x; } - inline float_type y() { return v.y; } - inline float_type z() { return v.z; } -private: - const vec_type& v; -}; - -template -inline _Vec1 convertTo(const _Vec2& v) -{ - typedef _Vec1 out_type; - typedef _Vec2 inp_type; - typedef vec_traits traits_type; - typedef vec_traits traits_out; - typedef typename traits_out::float_type float_type; - traits_type t(v); - return _Vec1((float_type)t.x(),(float_type)t.y(),(float_type)t.z()); -} - - } // namespace Base #endif // BASE_VECTOR3D_H diff --git a/src/Gui/Utilities.h b/src/Gui/Utilities.h index 5cd4b8d4e5..ecae23a86d 100644 --- a/src/Gui/Utilities.h +++ b/src/Gui/Utilities.h @@ -24,12 +24,14 @@ #define GUI_UTILITIES_H #include +#include #include #include #include #include #include #include +#include class SbViewVolume; class QAbstractItemView; @@ -41,9 +43,9 @@ struct vec_traits { typedef SbVec3f vec_type; typedef float float_type; vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v[0]; } - inline float_type y() { return v[1]; } - inline float_type z() { return v[2]; } + inline std::tuple get() const { + return std::make_tuple(v[0], v[1], v[2]); + } private: const vec_type& v; }; @@ -54,9 +56,22 @@ struct vec_traits { typedef SbVec3d vec_type; typedef double float_type; vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v[0]; } - inline float_type y() { return v[1]; } - inline float_type z() { return v[2]; } + inline std::tuple get() const { + return std::make_tuple(v[0], v[1], v[2]); + } +private: + const vec_type& v; +}; + +// Specialization for SbRotation +template <> +struct vec_traits { + typedef SbRotation vec_type; + typedef float float_type; + vec_traits(const vec_type& v) : v(v){} + inline std::tuple get() const { + return std::make_tuple(v[0], v[1], v[2], v[3]); + } private: const vec_type& v; }; @@ -67,9 +82,9 @@ struct vec_traits { typedef SbColor vec_type; typedef float float_type; vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v[0]; } - inline float_type y() { return v[1]; } - inline float_type z() { return v[2]; } + inline std::tuple get() const { + return std::make_tuple(v[0], v[1], v[2]); + } private: const vec_type& v; }; @@ -80,9 +95,9 @@ struct vec_traits { typedef App::Color vec_type; typedef float float_type; vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v.r; } - inline float_type y() { return v.g; } - inline float_type z() { return v.b; } + inline std::tuple get() const { + return std::make_tuple(v.r, v.g, v.b); + } private: const vec_type& v; }; diff --git a/src/Mod/Mesh/App/FeatureMeshSegmentByMesh.cpp b/src/Mod/Mesh/App/FeatureMeshSegmentByMesh.cpp index 81c6bf41ed..0988b699b3 100644 --- a/src/Mod/Mesh/App/FeatureMeshSegmentByMesh.cpp +++ b/src/Mod/Mesh/App/FeatureMeshSegmentByMesh.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "Core/Algorithm.h" #include "Core/Evaluation.h" diff --git a/src/Mod/Mesh/App/MeshProperties.cpp b/src/Mod/Mesh/App/MeshProperties.cpp index 5554ef1cca..617857fece 100644 --- a/src/Mod/Mesh/App/MeshProperties.cpp +++ b/src/Mod/Mesh/App/MeshProperties.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include diff --git a/src/Mod/Mesh/App/MeshPyImp.cpp b/src/Mod/Mesh/App/MeshPyImp.cpp index 1ca438def6..af922a4943 100644 --- a/src/Mod/Mesh/App/MeshPyImp.cpp +++ b/src/Mod/Mesh/App/MeshPyImp.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include diff --git a/src/Mod/MeshPart/App/AppMeshPartPy.cpp b/src/Mod/MeshPart/App/AppMeshPartPy.cpp index 47d8ed7579..eb733cc2b7 100644 --- a/src/Mod/MeshPart/App/AppMeshPartPy.cpp +++ b/src/Mod/MeshPart/App/AppMeshPartPy.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include diff --git a/src/Mod/MeshPart/Gui/Command.cpp b/src/Mod/MeshPart/Gui/Command.cpp index 1a768cd556..09343bcb37 100644 --- a/src/Mod/MeshPart/Gui/Command.cpp +++ b/src/Mod/MeshPart/Gui/Command.cpp @@ -30,6 +30,7 @@ #include +#include #include #include #include diff --git a/src/Mod/MeshPart/Gui/CurveOnMesh.cpp b/src/Mod/MeshPart/Gui/CurveOnMesh.cpp index 7bcaf079ab..7cec633a34 100644 --- a/src/Mod/MeshPart/Gui/CurveOnMesh.cpp +++ b/src/Mod/MeshPart/Gui/CurveOnMesh.cpp @@ -44,6 +44,7 @@ #endif #include "CurveOnMesh.h" +#include #include #include #include diff --git a/src/Mod/Part/App/Tools.h b/src/Mod/Part/App/Tools.h index 78a4b66fd6..a936c352c6 100644 --- a/src/Mod/Part/App/Tools.h +++ b/src/Mod/Part/App/Tools.h @@ -24,6 +24,7 @@ #ifndef PART_TOOLS_H #define PART_TOOLS_H +#include #include #include #include @@ -41,9 +42,9 @@ struct vec_traits { typedef gp_Pnt vec_type; typedef double float_type; vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v.X(); } - inline float_type y() { return v.Y(); } - inline float_type z() { return v.Z(); } + inline std::tuple get() const { + return std::make_tuple(v.X(), v.Y(), v.Z()); + } private: const vec_type& v; }; @@ -53,9 +54,9 @@ struct vec_traits { typedef gp_Vec vec_type; typedef double float_type; vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v.X(); } - inline float_type y() { return v.Y(); } - inline float_type z() { return v.Z(); } + inline std::tuple get() const { + return std::make_tuple(v.X(), v.Y(), v.Z()); + } private: const vec_type& v; }; @@ -65,9 +66,9 @@ struct vec_traits { typedef gp_Dir vec_type; typedef double float_type; vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v.X(); } - inline float_type y() { return v.Y(); } - inline float_type z() { return v.Z(); } + inline std::tuple get() const { + return std::make_tuple(v.X(), v.Y(), v.Z()); + } private: const vec_type& v; }; @@ -77,9 +78,9 @@ struct vec_traits { typedef gp_XYZ vec_type; typedef double float_type; vec_traits(const vec_type& v) : v(v){} - inline float_type x() { return v.X(); } - inline float_type y() { return v.Y(); } - inline float_type z() { return v.Z(); } + inline std::tuple get() const { + return std::make_tuple(v.X(), v.Y(), v.Z()); + } private: const vec_type& v; }; diff --git a/src/Mod/Points/App/PointsAlgos.cpp b/src/Mod/Points/App/PointsAlgos.cpp index 5049809702..b4fe78d9b8 100644 --- a/src/Mod/Points/App/PointsAlgos.cpp +++ b/src/Mod/Points/App/PointsAlgos.cpp @@ -33,6 +33,7 @@ #include "PointsAlgos.h" #include "Points.h" +#include #include #include #include diff --git a/src/Mod/Points/App/Properties.cpp b/src/Mod/Points/App/Properties.cpp index 0ddd2fb34e..b7d8692b20 100644 --- a/src/Mod/Points/App/Properties.cpp +++ b/src/Mod/Points/App/Properties.cpp @@ -28,6 +28,7 @@ # include #endif +#include #include #include #include diff --git a/src/Mod/ReverseEngineering/App/AppReverseEngineering.cpp b/src/Mod/ReverseEngineering/App/AppReverseEngineering.cpp index 6ccffc40c2..5d618f110a 100644 --- a/src/Mod/ReverseEngineering/App/AppReverseEngineering.cpp +++ b/src/Mod/ReverseEngineering/App/AppReverseEngineering.cpp @@ -29,6 +29,7 @@ #endif #include +#include #include #include #include diff --git a/src/Mod/ReverseEngineering/Gui/Command.cpp b/src/Mod/ReverseEngineering/Gui/Command.cpp index a2e4db5782..ef778750c7 100644 --- a/src/Mod/ReverseEngineering/Gui/Command.cpp +++ b/src/Mod/ReverseEngineering/Gui/Command.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include "../App/ApproxSurface.h" #include "FitBSplineSurface.h" diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp index b3b6870f81..c5dfec4ba1 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp @@ -80,6 +80,7 @@ /// Here the FreeCAD includes sorted by Base,App,Gui...... +#include #include #include #include