OsgUniform-inl.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 
16 #ifndef SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
17 #define SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
18 
23 
24 
25 namespace SurgSim
26 {
27 
28 namespace Graphics
29 {
30 
36 template <typename T>
37 const T& toOsg(const T& value)
38 {
39  return value;
40 }
41 
42 template <class T>
43 OsgUniform<T>::OsgUniform(const std::string& name) :
44  UniformBase(), Uniform<T>(), OsgUniformBase(name)
45 {
46  osg::Uniform::Type osgUniformType = getOsgUniformType<T>();
47  SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
48  SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
49  m_uniform->setNumElements(1);
50 }
51 
52 template <class T>
53 void OsgUniform<T>::set(const T& value)
54 {
55  SURGSIM_ASSERT(m_uniform->set(toOsg(value))) << "Failed to set OSG uniform value!" <<
56  " Uniform: " << getName() << " value: " << value;
57  m_value = value;
58 }
59 
60 template <class T>
61 void OsgUniform<T>::set(const YAML::Node& node)
62 {
63  set(node.as<T>());
64 }
65 
66 template <class T>
67 const T& OsgUniform<T>::get() const
68 {
69  return m_value;
70 }
71 
72 template <class T>
73 OsgUniform<std::vector<T>>::OsgUniform(const std::string& name, size_t numElements) :
74  UniformBase(), Uniform<std::vector<T>>(), OsgUniformBase(name)
75 {
76  osg::Uniform::Type osgUniformType = getOsgUniformType<T>();
77  SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
78  SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
79  m_value.resize(numElements);
80  m_uniform->setNumElements(numElements);
81 }
82 
83 template <class T>
84 size_t OsgUniform<std::vector<T>>::getNumElements() const
85 {
86  return m_uniform->getNumElements();
87 }
88 
89 template <class T>
90 void OsgUniform<std::vector<T>>::setElement(size_t index, const T& value)
91 {
92  SURGSIM_ASSERT(m_uniform->setElement(index, toOsg(value))) << "Failed to set OSG uniform value!" <<
93  " Uniform: " << getName() << " index: " << index << " value: " << value;
94  m_value[index] = value;
95 }
96 
97 template <class T>
98 void OsgUniform<std::vector<T>>::set(const std::vector<T>& value)
99 {
100  SURGSIM_ASSERT(value.size() == m_uniform->getNumElements()) <<
101  "Number of elements (" << value.size() << ") must match uniform's number of elements (" <<
102  m_uniform->getNumElements() << ")! Uniform: " << getName();
103  for (size_t i = 0; i < value.size(); ++i)
104  {
105  setElement(i, value[i]);
106  }
107 }
108 
109 template <class T>
110 void OsgUniform<std::vector<T>>::set(const YAML::Node& node)
111 {
112  SURGSIM_ASSERT(node.IsSequence()) << "Yaml setter called on vector uniform with non-sequence yaml node.";
113  set(node.as<std::vector<T>>());
114 }
115 
116 template <class T>
117 typename std::vector<T>::const_reference OsgUniform<std::vector<T>>::getElement(size_t index) const
118 {
119  return m_value[index];
120 }
121 
122 template <class T>
123 const std::vector<T>& OsgUniform<std::vector<T>>::get() const
124 {
125  return m_value;
126 }
127 
128 }; // namespace Graphics
129 
130 }; // namespace SurgSim
131 
132 #endif // SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
SurgSim::Graphics::OsgUniformBase
Base OSG implementation of graphics uniforms.
Definition: OsgUniformBase.h:41
SurgSim::Graphics::toOsg
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
Assert.h
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
MathConvert.h
SurgSim::Graphics::OsgUniform
OSG implementation of graphics uniform with a value of type T.
Definition: OsgCamera.h:46
SurgSim::Graphics::UniformBase
Common base class for all graphics uniforms.
Definition: UniformBase.h:33
OsgUniformTypes.h
SurgSim::Graphics::OsgUniform::OsgUniform
OsgUniform(const std::string &name)
Constructor.
Definition: OsgUniform-inl.h:43
SurgSim::Graphics::toOsg
const T & toOsg(const T &value)
Default type conversion to OSG.
Definition: OsgUniform-inl.h:37
SurgSim::Graphics::OsgUniform::m_value
T m_value
Value of the uniform.
Definition: OsgUniform.h:56
SurgSim::Graphics::OsgUniformBase::m_uniform
osg::ref_ptr< osg::Uniform > m_uniform
OSG uniform node.
Definition: OsgUniformBase.h:75
OsgConversions.h
SurgSim::Graphics::Uniform
Base class for a graphics uniform with a value of type T.
Definition: Uniform.h:32