DART 6.12.2
Loading...
Searching...
No Matches
Utils.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2021, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_GUI_OSG_UTILS_HPP_
34#define DART_GUI_OSG_UTILS_HPP_
35
36#include <Eigen/Geometry>
37#include <osg/Matrix>
38
39//==============================================================================
40template <typename Scalar>
41::osg::Matrix eigToOsgMatrix(
42 const Eigen::Transform<Scalar, 3, Eigen::Isometry>& tf)
43{
44 return ::osg::Matrix(
45 tf(0, 0),
46 tf(1, 0),
47 tf(2, 0),
48 tf(3, 0),
49 tf(0, 1),
50 tf(1, 1),
51 tf(2, 1),
52 tf(3, 1),
53 tf(0, 2),
54 tf(1, 2),
55 tf(2, 2),
56 tf(3, 2),
57 tf(0, 3),
58 tf(1, 3),
59 tf(2, 3),
60 tf(3, 3));
61}
62
63//==============================================================================
64template <typename Derived>
65::osg::Matrix eigToOsgMatrix(const Eigen::DenseBase<Derived>& M)
66{
67 return ::osg::Matrix(
68 M(0, 0),
69 M(1, 0),
70 M(2, 0),
71 M(3, 0),
72 M(0, 1),
73 M(1, 1),
74 M(2, 1),
75 M(3, 1),
76 M(0, 2),
77 M(1, 2),
78 M(2, 2),
79 M(3, 2),
80 M(0, 3),
81 M(1, 3),
82 M(2, 3),
83 M(3, 3));
84}
85
86//==============================================================================
87template <typename Derived>
88::osg::Vec3f eigToOsgVec3f(const Eigen::MatrixBase<Derived>& vec)
89{
90 return ::osg::Vec3f(vec[0], vec[1], vec[2]);
91}
92
93//==============================================================================
94template <typename Derived>
95::osg::Vec3d eigToOsgVec3d(const Eigen::MatrixBase<Derived>& vec)
96{
97 return ::osg::Vec3d(vec[0], vec[1], vec[2]);
98}
99
100//==============================================================================
101template <typename Derived>
102typename std::conditional<
103 std::is_same<typename Derived::Scalar, float>::value,
104 ::osg::Vec3f,
105 ::osg::Vec3d>::type
106eigToOsgVec3(const Eigen::MatrixBase<Derived>& vec)
107{
108 using Vec3 = typename std::conditional<
109 std::is_same<typename Derived::Scalar, float>::value,
110 ::osg::Vec3f,
111 ::osg::Vec3d>::type;
112
113 return Vec3(vec[0], vec[1], vec[2]);
114}
115
116//==============================================================================
117inline Eigen::Vector3f osgToEigVec3(const ::osg::Vec3f& vec)
118{
119 return Eigen::Vector3f(vec[0], vec[1], vec[2]);
120}
121
122//==============================================================================
123inline Eigen::Vector3d osgToEigVec3(const ::osg::Vec3d& vec)
124{
125 return Eigen::Vector3d(vec[0], vec[1], vec[2]);
126}
127
128//==============================================================================
129template <typename Derived>
130::osg::Vec4f eigToOsgVec4f(const Eigen::MatrixBase<Derived>& vec)
131{
132 return ::osg::Vec4f(vec[0], vec[1], vec[2], vec[3]);
133}
134
135//==============================================================================
136template <typename Derived>
137::osg::Vec4d eigToOsgVec4d(const Eigen::MatrixBase<Derived>& vec)
138{
139 return ::osg::Vec4d(vec[0], vec[1], vec[2], vec[3]);
140}
141
142//==============================================================================
143template <typename Derived>
144std::conditional<
145 std::is_same<typename Derived::Scalar, float>::value,
146 ::osg::Vec4f,
147 ::osg::Vec4d>
148eigToOsgVec4(const Eigen::MatrixBase<Derived>& vec)
149{
150 return std::conditional<
151 std::is_same<typename Derived::Scalar, float>::value,
152 ::osg::Vec4f,
153 ::osg::Vec4d>(vec[0], vec[1], vec[2], vec[3]);
154}
155
156//==============================================================================
157inline Eigen::Vector4f osgToEigVec4(const ::osg::Vec4f& vec)
158{
159 return Eigen::Vector4f(vec[0], vec[1], vec[2], vec[3]);
160}
161
162//==============================================================================
163inline Eigen::Vector4d osgToEigVec4(const ::osg::Vec4d& vec)
164{
165 return Eigen::Vector4d(vec[0], vec[1], vec[2], vec[3]);
166}
167
168#endif // DART_GUI_OSG_UTILS_HPP_
std::string type
Definition SdfParser.cpp:82
Eigen::Vector3f osgToEigVec3(const ::osg::Vec3f &vec)
Definition Utils.hpp:117
::osg::Vec4f eigToOsgVec4f(const Eigen::MatrixBase< Derived > &vec)
Definition Utils.hpp:130
std::conditional< std::is_same< typenameDerived::Scalar, float >::value,::osg::Vec3f,::osg::Vec3d >::type eigToOsgVec3(const Eigen::MatrixBase< Derived > &vec)
Definition Utils.hpp:106
std::conditional< std::is_same< typename Derived::Scalar, float >::value, ::osg::Vec4f, ::osg::Vec4d > eigToOsgVec4(const Eigen::MatrixBase< Derived > &vec)
Definition Utils.hpp:148
::osg::Vec3f eigToOsgVec3f(const Eigen::MatrixBase< Derived > &vec)
Definition Utils.hpp:88
::osg::Vec3d eigToOsgVec3d(const Eigen::MatrixBase< Derived > &vec)
Definition Utils.hpp:95
::osg::Vec4d eigToOsgVec4d(const Eigen::MatrixBase< Derived > &vec)
Definition Utils.hpp:137
::osg::Matrix eigToOsgMatrix(const Eigen::Transform< Scalar, 3, Eigen::Isometry > &tf)
Definition Utils.hpp:41
Eigen::Vector4f osgToEigVec4(const ::osg::Vec4f &vec)
Definition Utils.hpp:157