DART  6.10.1
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 
38 #include <osg/Matrix>
39 
40 //==============================================================================
41 template <typename Scalar>
42 ::osg::Matrix eigToOsgMatrix(
43  const Eigen::Transform<Scalar, 3, Eigen::Isometry>& tf)
44 {
45  return ::osg::Matrix(
46  tf(0, 0),
47  tf(1, 0),
48  tf(2, 0),
49  tf(3, 0),
50  tf(0, 1),
51  tf(1, 1),
52  tf(2, 1),
53  tf(3, 1),
54  tf(0, 2),
55  tf(1, 2),
56  tf(2, 2),
57  tf(3, 2),
58  tf(0, 3),
59  tf(1, 3),
60  tf(2, 3),
61  tf(3, 3));
62 }
63 
64 //==============================================================================
65 template <typename Derived>
66 ::osg::Matrix eigToOsgMatrix(const Eigen::DenseBase<Derived>& M)
67 {
68  return ::osg::Matrix(
69  M(0, 0),
70  M(1, 0),
71  M(2, 0),
72  M(3, 0),
73  M(0, 1),
74  M(1, 1),
75  M(2, 1),
76  M(3, 1),
77  M(0, 2),
78  M(1, 2),
79  M(2, 2),
80  M(3, 2),
81  M(0, 3),
82  M(1, 3),
83  M(2, 3),
84  M(3, 3));
85 }
86 
87 //==============================================================================
88 template <typename Derived>
89 ::osg::Vec3f eigToOsgVec3f(const Eigen::MatrixBase<Derived>& vec)
90 {
91  return ::osg::Vec3f(vec[0], vec[1], vec[2]);
92 }
93 
94 //==============================================================================
95 template <typename Derived>
96 ::osg::Vec3d eigToOsgVec3d(const Eigen::MatrixBase<Derived>& vec)
97 {
98  return ::osg::Vec3d(vec[0], vec[1], vec[2]);
99 }
100 
101 //==============================================================================
102 template <typename Derived>
103 typename std::conditional<
104  std::is_same<typename Derived::Scalar, float>::value,
105  ::osg::Vec3f,
106  ::osg::Vec3d>::type
107 eigToOsgVec3(const Eigen::MatrixBase<Derived>& vec)
108 {
109  using Vec3 = typename std::conditional<
110  std::is_same<typename Derived::Scalar, float>::value,
111  ::osg::Vec3f,
112  ::osg::Vec3d>::type;
113 
114  return Vec3(vec[0], vec[1], vec[2]);
115 }
116 
117 //==============================================================================
118 inline Eigen::Vector3f osgToEigVec3(const ::osg::Vec3f& vec)
119 {
120  return Eigen::Vector3f(vec[0], vec[1], vec[2]);
121 }
122 
123 //==============================================================================
124 inline Eigen::Vector3d osgToEigVec3(const ::osg::Vec3d& vec)
125 {
126  return Eigen::Vector3d(vec[0], vec[1], vec[2]);
127 }
128 
129 //==============================================================================
130 template <typename Derived>
131 ::osg::Vec4f eigToOsgVec4f(const Eigen::MatrixBase<Derived>& vec)
132 {
133  return ::osg::Vec4f(vec[0], vec[1], vec[2], vec[3]);
134 }
135 
136 //==============================================================================
137 template <typename Derived>
138 ::osg::Vec4d eigToOsgVec4d(const Eigen::MatrixBase<Derived>& vec)
139 {
140  return ::osg::Vec4d(vec[0], vec[1], vec[2], vec[3]);
141 }
142 
143 //==============================================================================
144 template <typename Derived>
145 std::conditional<
146  std::is_same<typename Derived::Scalar, float>::value,
147  ::osg::Vec4f,
148  ::osg::Vec4d>
149 eigToOsgVec4(const Eigen::MatrixBase<Derived>& vec)
150 {
151  return std::conditional<
152  std::is_same<typename Derived::Scalar, float>::value,
153  ::osg::Vec4f,
154  ::osg::Vec4d>(vec[0], vec[1], vec[2], vec[3]);
155 }
156 
157 //==============================================================================
158 inline Eigen::Vector4f osgToEigVec4(const ::osg::Vec4f& vec)
159 {
160  return Eigen::Vector4f(vec[0], vec[1], vec[2], vec[3]);
161 }
162 
163 //==============================================================================
164 inline Eigen::Vector4d osgToEigVec4(const ::osg::Vec4d& vec)
165 {
166  return Eigen::Vector4d(vec[0], vec[1], vec[2], vec[3]);
167 }
168 
169 #endif // DART_GUI_OSG_UTILS_HPP_
std::string type
Definition: SdfParser.cpp:82
Eigen::Vector3f osgToEigVec3(const ::osg::Vec3f &vec)
Definition: Utils.hpp:118
::osg::Vec4f eigToOsgVec4f(const Eigen::MatrixBase< Derived > &vec)
Definition: Utils.hpp:131
::osg::Vec3f eigToOsgVec3f(const Eigen::MatrixBase< Derived > &vec)
Definition: Utils.hpp:89
std::conditional< std::is_same< typename Derived::Scalar, float >::value, ::osg::Vec4f, ::osg::Vec4d > eigToOsgVec4(const Eigen::MatrixBase< Derived > &vec)
Definition: Utils.hpp:149
::osg::Vec3d eigToOsgVec3d(const Eigen::MatrixBase< Derived > &vec)
Definition: Utils.hpp:96
std::conditional< std::is_same< typename Derived::Scalar, float >::value, ::osg::Vec3f, ::osg::Vec3d >::type eigToOsgVec3(const Eigen::MatrixBase< Derived > &vec)
Definition: Utils.hpp:107
::osg::Vec4d eigToOsgVec4d(const Eigen::MatrixBase< Derived > &vec)
Definition: Utils.hpp:138
::osg::Matrix eigToOsgMatrix(const Eigen::Transform< Scalar, 3, Eigen::Isometry > &tf)
Definition: Utils.hpp:42
Eigen::Vector4f osgToEigVec4(const ::osg::Vec4f &vec)
Definition: Utils.hpp:158