Ignition Common

API Reference

4.4.0
ImageHeightmap.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Open Source Robotics Foundation
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 */
17 #ifndef IGNITION_COMMON_IMAGEHEIGHTMAPDATA_HH_
18 #define IGNITION_COMMON_IMAGEHEIGHTMAPDATA_HH_
19 
20 #include <limits>
21 #include <string>
22 #include <vector>
23 #include <ignition/math/Vector3.hh>
24 
25 #include <ignition/common/graphics/Export.hh>
27 #include <ignition/common/Image.hh>
28 
29 namespace ignition
30 {
31  namespace common
32  {
34  class IGNITION_COMMON_GRAPHICS_VISIBLE ImageHeightmap
36  {
39  public: ImageHeightmap();
40 
44  public: int Load(const std::string &_filename = "");
45 
46  // Documentation inherited.
47  public: void FillHeightMap(int _subSampling, unsigned int _vertSize,
48  const ignition::math::Vector3d &_size,
49  const ignition::math::Vector3d &_scale, bool _flipY,
50  std::vector<float> &_heights);
51 
54  public: std::string Filename() const;
55 
56  // Documentation inherited.
57  public: unsigned int Height() const;
58 
59  // Documentation inherited.
60  public: unsigned int Width() const;
61 
62  // Documentation inherited.
63  public: float MaxElevation() const;
64 
66  private: ignition::common::Image img;
67 
78  private: template <typename T>
79  void FillHeights(T *_data, int _imgHeight, int _imgWidth,
80  unsigned int _pitch, int _subSampling, unsigned int _vertSize,
81  const ignition::math::Vector3d &_size,
82  const ignition::math::Vector3d &_scale,
83  bool _flipY, std::vector<float> &_heights)
84  {
85  // bytes per pixel
86  const unsigned int bpp = _pitch / _imgWidth;
87  // number of channels in a pixel
88  const unsigned int channels = bpp / sizeof(T);
89  // number of pixels in a row of image
90  const unsigned int pitchInPixels = _pitch / bpp;
91 
92  const double maxPixelValue =
93  static_cast<double>(std::numeric_limits<T>::max());
94 
95  // Iterate over all the vertices
96  for (unsigned int y = 0; y < _vertSize; ++y)
97  {
98  // yf ranges between 0 and 4
99  const double yf = y / static_cast<double>(_subSampling);
100  const int y1 = static_cast<int>(std::floor(yf));
101  int y2 = static_cast<int>(std::ceil(yf));
102  if (y2 >= _imgHeight)
103  y2 = _imgHeight - 1;
104  const double dy = yf - y1;
105 
106  for (unsigned int x = 0; x < _vertSize; ++x)
107  {
108  const double xf = x / static_cast<double>(_subSampling);
109  const int x1 = static_cast<int>(std::floor(xf));
110  int x2 = static_cast<int>(std::ceil(xf));
111  if (x2 >= _imgWidth)
112  x2 = _imgWidth - 1;
113  const double dx = xf - x1;
114 
115  const double px1 = static_cast<int>(
116  _data[(y1 * pitchInPixels + x1) * channels]) / maxPixelValue;
117  const double px2 = static_cast<int>(
118  _data[(y1 * pitchInPixels + x2) * channels]) / maxPixelValue;
119  const float h1 = (px1 - ((px1 - px2) * dx));
120 
121  const double px3 = static_cast<int>(
122  _data[(y2 * pitchInPixels + x1) * channels]) / maxPixelValue;
123  const double px4 = static_cast<int>(
124  _data[(y2 * pitchInPixels + x2) * channels]) / maxPixelValue;
125  const float h2 = (px3 - ((px3 - px4) * dx));
126  float h = (h1 - ((h1 - h2) * dy)) * _scale.Z();
127 
128  // invert pixel definition so 1=ground, 0=full height,
129  // if the terrain size has a negative z component
130  // this is mainly for backward compatibility
131  if (_size.Z() < 0)
132  h = 1.0 - h;
133 
134  // Store the height for future use
135  if (!_flipY)
136  _heights[y * _vertSize + x] = h;
137  else
138  _heights[(_vertSize - y - 1) * _vertSize + x] = h;
139  }
140  }
141  }
142  };
143  }
144 }
145 #endif
T ceil(T... args)
T floor(T... args)
STL class.
Encapsulates a generic heightmap data file.
Definition: HeightmapData.hh:29
T max(T... args)
STL class.
Encapsulates an image that will be interpreted as a heightmap.
Definition: ImageHeightmap.hh:34
Forward declarations for the common classes.
Encapsulates an image.
Definition: Image.hh:60