opensurgsim
OsgMatrixConversions.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
38 
39 #ifndef SURGSIM_GRAPHICS_OSGMATRIXCONVERSIONS_H
40 #define SURGSIM_GRAPHICS_OSGMATRIXCONVERSIONS_H
41 
42 #include "SurgSim/Math/Matrix.h"
43 
44 #include <osg/Matrixf>
45 #include <osg/Matrixd>
46 #include <osg/Uniform>
47 
48 namespace SurgSim
49 {
50 
51 namespace Graphics
52 {
53 
55 template <int MOpt> inline
56 const osg::Matrix2 toOsg(const Eigen::Matrix<float, 2, 2, MOpt>& matrix)
57 {
58  osg::Matrix2 osgMatrix;
59  Eigen::Map<Eigen::Matrix<float, 2, 2, Eigen::ColMajor>>(osgMatrix.ptr()) = matrix;
60  return osgMatrix;
61 }
62 
64 template <int MOpt> inline
65 const osg::Matrix2d toOsg(const Eigen::Matrix<double, 2, 2, MOpt>& matrix)
66 {
67  osg::Matrix2d osgMatrix;
68  Eigen::Map<Eigen::Matrix<double, 2, 2, Eigen::ColMajor>>(osgMatrix.ptr()) = matrix;
69  return osgMatrix;
70 }
71 
73 inline const Eigen::Matrix<float, 2, 2, Eigen::RowMajor> fromOsg(const osg::Matrix2& matrix)
74 {
75  return Eigen::Map<const Eigen::Matrix<float, 2, 2, Eigen::ColMajor>>(matrix.ptr());
76 }
78 inline const Eigen::Matrix<double, 2, 2, Eigen::RowMajor> fromOsg(const osg::Matrix2d& matrix)
79 {
80  return Eigen::Map<const Eigen::Matrix<double, 2, 2, Eigen::ColMajor>>(matrix.ptr());
81 }
82 
84 template <int MOpt> inline
85 const osg::Matrix3 toOsg(const Eigen::Matrix<float, 3, 3, MOpt>& matrix)
86 {
87  osg::Matrix3 osgMatrix;
88  Eigen::Map<Eigen::Matrix<float, 3, 3, Eigen::ColMajor>>(osgMatrix.ptr()) = matrix;
89  return osgMatrix;
90 }
91 
93 template <int MOpt> inline
94  const osg::Matrix3d toOsg(const Eigen::Matrix<double, 3, 3, MOpt>& matrix)
95 {
96  osg::Matrix3d osgMatrix;
97  Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::ColMajor>>(osgMatrix.ptr()) = matrix;
98  return osgMatrix;
99 }
100 
102 inline const Eigen::Matrix<float, 3, 3, Eigen::RowMajor> fromOsg(const osg::Matrix3& matrix)
103 {
104  return Eigen::Map<const Eigen::Matrix<float, 3, 3, Eigen::ColMajor>>(matrix.ptr());
105 }
106 
108 inline const Eigen::Matrix<double, 3, 3, Eigen::RowMajor> fromOsg(const osg::Matrix3d& matrix)
109 {
110  return Eigen::Map<const Eigen::Matrix<double, 3, 3, Eigen::ColMajor>>(matrix.ptr());
111 }
112 
114 template <int MOpt> inline
115 const osg::Matrixf toOsg(const Eigen::Matrix<float, 4, 4, MOpt>& matrix)
116 {
117  osg::Matrixf osgMatrix;
118  Eigen::Map<Eigen::Matrix<float, 4, 4, Eigen::ColMajor>>(osgMatrix.ptr()) = matrix;
119  return osgMatrix;
120 }
121 
123 inline const Eigen::Matrix<float, 4, 4, Eigen::RowMajor> fromOsg(const osg::Matrixf& matrix)
124 {
125  return Eigen::Map<const Eigen::Matrix<float, 4, 4, Eigen::ColMajor>>(matrix.ptr());
126 }
127 
129 template <int MOpt> inline
130 const osg::Matrixd toOsg(const Eigen::Matrix<double, 4, 4, MOpt>& matrix)
131 {
132  osg::Matrixd osgMatrix;
133  Eigen::Map<Eigen::Matrix<double, 4, 4, Eigen::ColMajor>>(osgMatrix.ptr()) = matrix;
134  return osgMatrix;
135 }
136 
138 inline const Eigen::Matrix<double, 4, 4, Eigen::RowMajor> fromOsg(const osg::Matrixd& matrix)
139 {
140  return Eigen::Map<const Eigen::Matrix<double, 4, 4, Eigen::ColMajor>>(matrix.ptr());
141 }
142 
143 }; // namespace Graphics
144 
145 }; // namespace SurgSim
146 
147 #endif // SURGSIM_GRAPHICS_OSGMATRIXCONVERSIONS_H
const Eigen::Matrix< float, 2, 2, Eigen::RowMajor > fromOsg(const osg::Matrix2 &matrix)
Convert from OSG to a 2x2 matrix of floats.
Definition: OsgMatrixConversions.h:73
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
const osg::Matrix2 toOsg(const Eigen::Matrix< float, 2, 2, MOpt > &matrix)
Convert a fixed-size 2x2 matrix of floats to OSG.
Definition: OsgMatrixConversions.h:56
Definitions of small fixed-size square matrix types.