Skip to content

Commit 3251c6a

Browse files
authored
Add Heightmap utility functions (#680)
Signed-off-by: Ian Chen <[email protected]>
1 parent 3e3a47f commit 3251c6a

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2025 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_HEIGHTMAPUTIL_HH_
18+
#define GZ_COMMON_GEOSPATIAL_HEIGHTMAPUTIL_HH_
19+
20+
#include <memory>
21+
#include <string>
22+
23+
#include <gz/math/SphericalCoordinates.hh>
24+
25+
#include <gz/common/geospatial/Export.hh>
26+
#include "gz/common/geospatial/HeightmapData.hh"
27+
28+
namespace gz
29+
{
30+
namespace common
31+
{
32+
/// \brief Load a heightmap from file
33+
/// \param[in] _filename Path to heightmap file
34+
/// \param[in] _sphericalCoordinates The spherical coordinates
35+
/// object contained in the world. This is used if the underlying
36+
/// heightmap type is DEM.
37+
/// \return heightmap
38+
std::unique_ptr<HeightmapData>
39+
GZ_COMMON_GEOSPATIAL_VISIBLE loadHeightmapData(
40+
const std::string &_filename,
41+
const math::SphericalCoordinates &_sphericalCoordinates =
42+
math::SphericalCoordinates());
43+
44+
/// \brief Check if input file has a file extension that can be
45+
/// loaded as an ImageHeightmap
46+
/// \param[in] _filename Path to heightmap file
47+
/// \return True if the filename has a file extension that is supported.
48+
bool GZ_COMMON_GEOSPATIAL_VISIBLE isSupportedImageHeightmapFileExtension(
49+
const std::string &_filename);
50+
}
51+
}
52+
#endif

geospatial/src/HeightmapUtil.cc

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (C) 2025 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+
18+
#include <memory>
19+
#include <ostream>
20+
#include <string>
21+
#include <utility>
22+
23+
#include "gz/common/Console.hh"
24+
#include "gz/common/StringUtils.hh"
25+
#include "gz/common/Util.hh"
26+
27+
#include "gz/common/geospatial/Dem.hh"
28+
#include "gz/common/geospatial/HeightmapUtil.hh"
29+
#include "gz/common/geospatial/ImageHeightmap.hh"
30+
31+
namespace gz
32+
{
33+
namespace common
34+
{
35+
36+
//////////////////////////////////////////////////
37+
bool isSupportedImageHeightmapFileExtension(const std::string &_filename)
38+
{
39+
if (_filename.empty())
40+
return false;
41+
42+
std::string lowerFullPath = common::lowercase(_filename);
43+
return common::EndsWith(lowerFullPath, ".png")
44+
|| common::EndsWith(lowerFullPath, ".jpg")
45+
|| common::EndsWith(lowerFullPath, ".jpeg");
46+
}
47+
48+
//////////////////////////////////////////////////
49+
std::unique_ptr<HeightmapData> loadHeightmapData(
50+
const std::string &_filename,
51+
const math::SphericalCoordinates &_sphericalCoordinates)
52+
{
53+
if (_filename.empty())
54+
return nullptr;
55+
56+
std::unique_ptr<HeightmapData> data;
57+
// check if heightmap is an image
58+
if (isSupportedImageHeightmapFileExtension(_filename))
59+
{
60+
auto imgHeightmap = std::make_unique<ImageHeightmap>();
61+
if (imgHeightmap->Load(_filename) < 0)
62+
{
63+
gzerr << "Failed to load heightmap data from ["
64+
<< _filename << "]" << std::endl;
65+
66+
return nullptr;
67+
}
68+
data = std::move(imgHeightmap);
69+
}
70+
else
71+
{
72+
// try loading as DEM
73+
auto dem = std::make_unique<Dem>();
74+
dem->SetSphericalCoordinates(_sphericalCoordinates);
75+
if (dem->Load(_filename) < 0)
76+
{
77+
gzerr << "Failed to load heightmap data from ["
78+
<< _filename << "]" << std::endl;
79+
return nullptr;
80+
}
81+
data = std::move(dem);
82+
}
83+
84+
return data;
85+
}
86+
87+
}
88+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (C) 2025 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+
18+
#include <gtest/gtest.h>
19+
#include <memory>
20+
21+
#include "gz/common/geospatial/HeightmapUtil.hh"
22+
23+
#include "gz/common/testing/AutoLogFixture.hh"
24+
#include "gz/common/testing/TestPaths.hh"
25+
26+
using namespace gz;
27+
28+
class HeightmapLoaderTest : public common::testing::AutoLogFixture { };
29+
30+
/////////////////////////////////////////////////
31+
TEST_F(HeightmapLoaderTest, SupportedImageHeightmapFileExtension)
32+
{
33+
EXPECT_TRUE(common::isSupportedImageHeightmapFileExtension("f.jpg"));
34+
EXPECT_TRUE(common::isSupportedImageHeightmapFileExtension("f.jpeg"));
35+
EXPECT_TRUE(common::isSupportedImageHeightmapFileExtension("f.png"));
36+
EXPECT_TRUE(common::isSupportedImageHeightmapFileExtension("f.JPG"));
37+
EXPECT_TRUE(common::isSupportedImageHeightmapFileExtension("f.JPEG"));
38+
EXPECT_TRUE(common::isSupportedImageHeightmapFileExtension("f.PNG"));
39+
EXPECT_TRUE(common::isSupportedImageHeightmapFileExtension(".PNG"));
40+
EXPECT_FALSE(common::isSupportedImageHeightmapFileExtension("f.tiff"));
41+
EXPECT_FALSE(common::isSupportedImageHeightmapFileExtension("invalid"));
42+
EXPECT_FALSE(common::isSupportedImageHeightmapFileExtension(""));
43+
}
44+
45+
/////////////////////////////////////////////////
46+
TEST_F(HeightmapLoaderTest, LoadImage)
47+
{
48+
const auto path = common::testing::TestFile("data", "heightmap_bowl.png");
49+
std::unique_ptr<common::HeightmapData> data =
50+
common::loadHeightmapData(path);
51+
EXPECT_NE(nullptr, data);
52+
}
53+
54+
/////////////////////////////////////////////////
55+
TEST_F(HeightmapLoaderTest, LoadDEM)
56+
{
57+
const auto path = common::testing::TestFile("data", "dem_squared.tif");
58+
std::unique_ptr<common::HeightmapData> data =
59+
common::loadHeightmapData(path);
60+
EXPECT_NE(nullptr, data);
61+
}
62+
63+
/////////////////////////////////////////////////
64+
TEST_F(HeightmapLoaderTest, LoadInvalidFile)
65+
{
66+
EXPECT_EQ(nullptr, common::loadHeightmapData("invalid/file.jpg"));
67+
EXPECT_EQ(nullptr, common::loadHeightmapData("invalid/file_no_extension"));
68+
}

0 commit comments

Comments
 (0)