opensurgsim
ImageBase-inl.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2016, 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_DATASTRUCTURES_IMAGEBASE_INL_H
17 #define SURGSIM_DATASTRUCTURES_IMAGEBASE_INL_H
18 
20 
21 
22 namespace SurgSim
23 {
24 namespace DataStructures
25 {
26 
27 template<class T>
29 {
30 }
31 
32 template<class T>
33 Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1>> ImageBase<T>::operator()(size_t x, size_t y)
34 {
35  SURGSIM_ASSERT(x < m_width) << "x is larger than the image width (" << x << " >= " << m_width << ")";
36  SURGSIM_ASSERT(y < m_height) << "y is larger than the image height (" << y << " >= " << m_height << ")";
37  return Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1>>
38  (getData() + m_channels * (x + y * m_width), m_channels);
39 }
40 
41 template<class T>
42 Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>> ImageBase<T>::operator()(size_t x, size_t y) const
43 {
44  SURGSIM_ASSERT(x < m_width) << "x is larger than the image width (" << x << " >= " << m_width << ")";
45  SURGSIM_ASSERT(y < m_height) << "y is larger than the image height (" << y << " >= " << m_height << ")";
46  return Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>>
47  (getData() + m_channels * (x + y * m_width), m_channels);
48 }
49 
50 template<class T>
51 Eigen::Map<typename ImageBase<T>::ChannelType, Eigen::Unaligned, Eigen::Stride<-1, -1>>
53 {
54  SURGSIM_ASSERT(index < m_channels) << "Channel number is larger than the number of channels";
55  Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> stride(m_width*m_channels, m_channels);
56  return Eigen::Map<ChannelType, Eigen::Unaligned, Eigen::Stride<-1, -1>>
57  (getData() + index, m_width, m_height, stride);
58 }
59 
60 template<class T>
61 Eigen::Map<const typename ImageBase<T>::ChannelType, Eigen::Unaligned, Eigen::Stride<-1, -1>>
62 ImageBase<T>::getChannel(size_t index) const
63 {
64  SURGSIM_ASSERT(index < m_channels) << "Channel number is larger than the number of channels";
65  Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> stride(m_width*m_channels, m_channels);
66  return Eigen::Map<const ChannelType, Eigen::Unaligned, Eigen::Stride<-1, -1>>
67  (getData() + index, m_width, m_height, stride);
68 }
69 
70 template<class T>
71 void ImageBase<T>::setChannel(size_t index, const Eigen::Ref<const ChannelType>& data)
72 {
73  auto myChannel = getChannel(index);
74  SURGSIM_ASSERT(myChannel.rows() == data.rows() && myChannel.cols() == data.cols())
75  << "Channel data must be of size " << myChannel.rows() << "x" << myChannel.cols() << ". "
76  << data.rows() << "x" << data.cols() << " data was provided.";
77  myChannel = data;
78 }
79 
80 template<class T>
81 Eigen::Map<typename ImageBase<T>::VectorType, Eigen::Unaligned> ImageBase<T>::getAsVector()
82 {
83  return Eigen::Map<VectorType, Eigen::Unaligned>(getData(), m_width * m_height * m_channels);
84 }
85 
86 template<class T>
87 Eigen::Map<const typename ImageBase<T>::VectorType, Eigen::Unaligned> ImageBase<T>::getAsVector() const
88 {
89  return Eigen::Map<const VectorType, Eigen::Unaligned>(getData(), m_width * m_height * m_channels);
90 }
91 
92 template<class T>
93 void ImageBase<T>::setAsVector(const Eigen::Ref<const VectorType>& data)
94 {
95  auto myVector = getAsVector();
96  SURGSIM_ASSERT(myVector.rows() == data.rows() && myVector.cols() == data.cols())
97  << "Vector must be of size " << myVector.rows() << "x" << myVector.cols() << ". "
98  << "A " << data.rows() << "x" << data.cols() << " vector was provided.";
99  myVector = data;
100 }
101 
102 template<class T>
104 {
105  return m_width;
106 }
107 
108 template<class T>
110 {
111  return m_height;
112 }
113 
114 template<class T>
115 std::array<size_t, 3> ImageBase<T>::getSize() const
116 {
117  std::array<size_t, 3> size = {m_width, m_height, m_channels};
118  return size;
119 }
120 
121 template<class T>
123 {
124  return m_channels;
125 }
126 
127 template<class T>
128 void ImageBase<T>::setSize(size_t width, size_t height, size_t channels)
129 {
130  m_width = width;
131  m_height = height;
132  m_channels = channels;
133 }
134 
135 };
136 };
137 
138 #endif //SURGSIM_DATASTRUCTURES_IMAGEBASE_INL_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
std::array< size_t, 3 > getSize() const
Get the Image size.
Definition: ImageBase-inl.h:115
size_t getHeight() const
Get the Image height.
Definition: ImageBase-inl.h:109
void setChannel(size_t index, const Eigen::Ref< const ChannelType > &data)
Set the image data in the channel.
Definition: ImageBase-inl.h:71
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > ChannelType
2D Channel Type;
Definition: ImageBase.h:67
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, 1 > > operator()(size_t x, size_t y)
Get the pixel value at (x, y)
Definition: ImageBase-inl.h:33
Eigen::Map< ChannelType, Eigen::Unaligned, Eigen::Stride<-1, -1 > > getChannel(size_t index)
Get the 2D image channel data.
Definition: ImageBase-inl.h:52
void setSize(size_t width, size_t height, size_t channels)
Set the Image size.
Definition: ImageBase-inl.h:128
The header that provides the assertion API.
size_t getWidth() const
Get the Image width.
Definition: ImageBase-inl.h:103
Eigen::Map< VectorType, Eigen::Unaligned > getAsVector()
Get the data as a 1D Vector.
Definition: ImageBase-inl.h:81
void setAsVector(const Eigen::Ref< const VectorType > &data)
Set the image data as a 1D Vector.
Definition: ImageBase-inl.h:93
virtual ~ImageBase()
Destructor.
Definition: ImageBase-inl.h:28
size_t getNumChannels() const
Get the number of channels in this Image.
Definition: ImageBase-inl.h:122