Gazebo Common

API Reference

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