extend convertTo function to also support SbRotation/Base::Rotation

This commit is contained in:
wmayer
2019-09-06 21:29:51 +02:00
parent 584f97b7db
commit 9f7075bb8f
16 changed files with 162 additions and 64 deletions

View File

@@ -303,6 +303,7 @@ SET(FreeCADBase_HPP_SRCS
BoundBox.h
Builder3D.h
Console.h
Converter.h
CoordinateSystem.h
Debugger.h
Exception.h

110
src/Base/Converter.h Normal file
View File

@@ -0,0 +1,110 @@
/***************************************************************************
* Copyright (c) 2019 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 <tuple>
namespace Base {
template <class vecT>
struct vec_traits { };
template <>
struct vec_traits<Vector3f> {
typedef Vector3f vec_type;
typedef float float_type;
vec_traits(const vec_type& v) : v(v){}
inline std::tuple<float_type,float_type,float_type> get() const {
return std::make_tuple(v.x, v.y, v.z);
}
private:
const vec_type& v;
};
template <>
struct vec_traits<Vector3d> {
typedef Vector3d vec_type;
typedef double float_type;
vec_traits(const vec_type& v) : v(v){}
inline std::tuple<float_type,float_type,float_type> get() const {
return std::make_tuple(v.x, v.y, v.z);
}
private:
const vec_type& v;
};
template <>
struct vec_traits<Rotation> {
typedef Rotation vec_type;
typedef double float_type;
vec_traits(const vec_type& v) : v(v){}
inline std::tuple<float_type,float_type,float_type,float_type> 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 <class _Vec, typename float_type>
_Vec make_vec(const std::tuple<float_type, float_type, float_type>&& 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 <class _Vec, typename float_type>
_Vec make_vec(const std::tuple<float_type, float_type, float_type, float_type>&& 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 <class _Vec1, class _Vec2>
inline _Vec1 convertTo(const _Vec2& v)
{
typedef _Vec1 out_type;
typedef _Vec2 inp_type;
typedef vec_traits<inp_type> traits_type;
typedef vec_traits<out_type> 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

View File

@@ -244,46 +244,6 @@ inline Vector3<_Pr1> toVector(const Vector3<_Pr2>& v)
typedef Vector3<float> Vector3f;
typedef Vector3<double> Vector3d;
template <class vecT>
struct vec_traits { };
template <>
struct vec_traits<Vector3f> {
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<Vector3d> {
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 <class _Vec1, class _Vec2>
inline _Vec1 convertTo(const _Vec2& v)
{
typedef _Vec1 out_type;
typedef _Vec2 inp_type;
typedef vec_traits<inp_type> traits_type;
typedef vec_traits<out_type> 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

View File

@@ -24,12 +24,14 @@
#define GUI_UTILITIES_H
#include <Base/ViewProj.h>
#include <Base/Converter.h>
#include <App/Material.h>
#include <vector>
#include <Inventor/SbColor.h>
#include <Inventor/SbVec2f.h>
#include <Inventor/SbViewVolume.h>
#include <Inventor/SbMatrix.h>
#include <Inventor/SbRotation.h>
class SbViewVolume;
class QAbstractItemView;
@@ -41,9 +43,9 @@ struct vec_traits<SbVec3f> {
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<float_type,float_type,float_type> get() const {
return std::make_tuple(v[0], v[1], v[2]);
}
private:
const vec_type& v;
};
@@ -54,9 +56,22 @@ struct vec_traits<SbVec3d> {
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<float_type,float_type,float_type> get() const {
return std::make_tuple(v[0], v[1], v[2]);
}
private:
const vec_type& v;
};
// Specialization for SbRotation
template <>
struct vec_traits<SbRotation> {
typedef SbRotation vec_type;
typedef float float_type;
vec_traits(const vec_type& v) : v(v){}
inline std::tuple<float_type,float_type,float_type,float_type> 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<SbColor> {
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<float_type,float_type,float_type> get() const {
return std::make_tuple(v[0], v[1], v[2]);
}
private:
const vec_type& v;
};
@@ -80,9 +95,9 @@ struct vec_traits<App::Color> {
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<float_type,float_type,float_type> get() const {
return std::make_tuple(v.r, v.g, v.b);
}
private:
const vec_type& v;
};

View File

@@ -28,6 +28,7 @@
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Sequencer.h>
#include <Base/Converter.h>
#include "Core/Algorithm.h"
#include "Core/Evaluation.h"

View File

@@ -27,6 +27,7 @@
#include <CXX/Objects.hxx>
#include <Base/Console.h>
#include <Base/Converter.h>
#include <Base/Exception.h>
#include <Base/Writer.h>
#include <Base/Reader.h>

View File

@@ -26,6 +26,7 @@
#include <Base/VectorPy.h>
#include <Base/Handle.h>
#include <Base/Builder3D.h>
#include <Base/Converter.h>
#include <Base/GeometryPyCXX.h>
#include <Base/MatrixPy.h>
#include <Base/Tools.h>

View File

@@ -33,6 +33,7 @@
#include <Base/PyObjectBase.h>
#include <Base/Console.h>
#include <Base/Vector3D.h>
#include <Base/Converter.h>
#include <Base/VectorPy.h>
#include <Base/GeometryPyCXX.h>
#include <Mod/Part/App/TopoShapePy.h>

View File

@@ -30,6 +30,7 @@
#include <Mod/Mesh/App/MeshFeature.h>
#include <Base/Converter.h>
#include <App/Application.h>
#include <App/Document.h>
#include <Gui/Application.h>

View File

@@ -44,6 +44,7 @@
#endif
#include "CurveOnMesh.h"
#include <Base/Converter.h>
#include <App/Document.h>
#include <Gui/Document.h>
#include <Gui/MainWindow.h>

View File

@@ -24,6 +24,7 @@
#ifndef PART_TOOLS_H
#define PART_TOOLS_H
#include <Base/Converter.h>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <gp_Dir.hxx>
@@ -41,9 +42,9 @@ struct vec_traits<gp_Pnt> {
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<float_type,float_type,float_type> get() const {
return std::make_tuple(v.X(), v.Y(), v.Z());
}
private:
const vec_type& v;
};
@@ -53,9 +54,9 @@ struct vec_traits<gp_Vec> {
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<float_type,float_type,float_type> get() const {
return std::make_tuple(v.X(), v.Y(), v.Z());
}
private:
const vec_type& v;
};
@@ -65,9 +66,9 @@ struct vec_traits<gp_Dir> {
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<float_type,float_type,float_type> get() const {
return std::make_tuple(v.X(), v.Y(), v.Z());
}
private:
const vec_type& v;
};
@@ -77,9 +78,9 @@ struct vec_traits<gp_XYZ> {
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<float_type,float_type,float_type> get() const {
return std::make_tuple(v.X(), v.Y(), v.Z());
}
private:
const vec_type& v;
};

View File

@@ -33,6 +33,7 @@
#include "PointsAlgos.h"
#include "Points.h"
#include <Base/Converter.h>
#include <Base/Exception.h>
#include <Base/FileInfo.h>
#include <Base/Console.h>

View File

@@ -28,6 +28,7 @@
# include <algorithm>
#endif
#include <Base/Converter.h>
#include <Base/Exception.h>
#include <Base/Matrix.h>
#include <Base/Persistence.h>

View File

@@ -29,6 +29,7 @@
#endif
#include <Base/Console.h>
#include <Base/Converter.h>
#include <Base/Interpreter.h>
#include <Base/PyObjectBase.h>
#include <Base/GeometryPyCXX.h>

View File

@@ -42,6 +42,7 @@
#include <Gui/FileDialog.h>
#include <Gui/Selection.h>
#include <Base/CoordinateSystem.h>
#include <Base/Converter.h>
#include "../App/ApproxSurface.h"
#include "FitBSplineSurface.h"

View File

@@ -80,6 +80,7 @@
/// Here the FreeCAD includes sorted by Base,App,Gui......
#include <Base/Converter.h>
#include <Base/Tools.h>
#include <Base/Parameter.h>
#include <Base/Console.h>