geolib2
height_image_to_file.cpp
Go to the documentation of this file.
1 #include <geolib/HeightMap.h>
2 #include <geolib/io/export.h>
3 
4 #include <opencv2/highgui/highgui.hpp>
5 
6 #include <vector>
7 
8 int main(int argc, char **argv) {
9 
10  // Parse command-line arguments
11  if (argc < 3 || argc > 7) {
12  std::cerr << "Usage: height_map_to_file INPUT_IMAGE OUTPUT_FILE RESOLUTION [BLOCK_HEIGHT] [ORIGIN_X ORIGIN_Y]" << std::endl;
13  return 1;
14  }
15 
16  std::string filename_img = argv[1];
17  std::string output_file = argv[2];
18 
19  double resolution = 0.2;
20  if (argc > 3) {
21  resolution = atof(argv[3]);
22  }
23 
24  double block_height = 1;
25  if (argc > 4) {
26  block_height = atof(argv[4]);
27  }
28 
29  double origin_x = 0, origin_y = 0;
30  if (argc > 6) {
31  origin_x = atof(argv[5]);
32  origin_y = atof(argv[6]);
33  }
34 
35  cv::Mat image = cv::imread(filename_img, cv::IMREAD_GRAYSCALE); // Read the file
36 
38 
39  if (image.data ) {
40  map.resize(image.cols);
41  for(int x = 0; x < image.cols; ++x) {
42  map[x].resize(image.rows);
43  for(int y = 0; y < image.rows; ++y) {
44  map[x][image.rows - y - 1] = block_height - (double)image.at<unsigned char>(y, x) / 255 * block_height;
45  }
46  }
47  std::cout << "Loaded height map " << filename_img << std::endl;
48  } else {
49  std::cout << "Could not load height map " << filename_img << std::endl;
50  return 1;
51  }
52 
53  geo::HeightMap hmap = geo::HeightMap::fromGrid(map, resolution);
54 
55  // Transform according to given origin
56  geo::Pose3D transform(origin_x, origin_y, 0, 0, 0, 0);
57  geo::Mesh mesh_transformed = hmap.getMesh().getTransformed(transform);
58 
59  geo::Shape shape;
60  shape.setMesh(mesh_transformed);
61 
62  geo::io::writeMeshFile(output_file, shape);
63 
64  std::cout << mesh_transformed.getTriangleIs().size() << " triangles and " << mesh_transformed.getPoints().size() <<
65  " points saved to '" << output_file << "'." << std::endl;
66  return 0;
67 }
geo::Shape::setMesh
virtual void setMesh(const Mesh &mesh)
set the Mesh Any child classes should throw a std::logic_error in case the mesh should not be changed...
Definition: Shape.cpp:429
geo::HeightMap::fromGrid
static HeightMap fromGrid(const std::vector< std::vector< double > > &grid, double resolution)
fromGrid: instantiate a Heightmap from a grid
Definition: HeightMap.cpp:34
std::vector::resize
T resize(T... args)
std::string
geo::Mesh::getTriangleIs
const std::vector< TriangleI > & getTriangleIs() const
Definition: Mesh.cpp:46
geo::HeightMap
A geometric description of a Heightmap using a quad tree.
Definition: HeightMap.h:15
vector
std::vector::size
T size(T... args)
std::cerr
geo::io::writeMeshFile
bool writeMeshFile(const std::string &filename, const Shape &shape, std::string format="")
Definition: export.cpp:28
geo::Transform3T
Definition: math_types.h:19
geo::Shape::getMesh
virtual const Mesh & getMesh() const
return the mesh defining the shape
Definition: Shape.cpp:425
HeightMap.h
std::cout
geo::Mesh::getPoints
const std::vector< geo::Vector3 > & getPoints() const
Definition: Mesh.cpp:42
geo::Mesh::getTransformed
Mesh getTransformed(const geo::Transform t) const
Definition: Mesh.cpp:59
std::vector::at
T at(T... args)
main
int main(int argc, char **argv)
Definition: height_image_to_file.cpp:8
std::endl
T endl(T... args)
geo::Mesh
Definition: Mesh.h:25
geo::Shape
A geometric description of a shape.
Definition: Shape.h:19
export.h