geolib2
height_image_to_shape.cpp
Go to the documentation of this file.
1 /*
2  * Created by Sjoerd van den Dries (2014)
3  */
4 
5 
6 #include <geolib/HeightMap.h>
7 #include <geolib/serialization.h>
8 
9 #include <opencv2/highgui/highgui.hpp>
10 
11 #include <vector>
12 
13 int main(int argc, char **argv) {
14 
15  // Parse command-line arguments
16  if (argc < 2 || argc > 7) {
17  std::cerr << "Usage: height_map_to_shape INPUT_IMAGE RESOLUTION [BLOCK_HEIGHT] [ORIGIN_X ORIGIN_Y]" << std::endl;
18  return 1;
19  }
20 
21  std::string filename_img = argv[1];
22 
23  double resolution = 0.2;
24  if (argc > 3) {
25  resolution = atof(argv[2]);
26  }
27 
28  double block_height = 1;
29  if (argc > 4) {
30  block_height = atof(argv[3]);
31  }
32 
33  double origin_x = 0, origin_y = 0;
34  if (argc > 6) {
35  origin_x = atof(argv[4]);
36  origin_y = atof(argv[5]);
37  }
38 
39  cv::Mat image = cv::imread(filename_img, cv::IMREAD_GRAYSCALE); // Read the file
40 
42 
43  if (image.data ) {
44  map.resize(image.cols);
45  for(int x = 0; x < image.cols; ++x) {
46  map[x].resize(image.rows);
47  for(int y = 0; y < image.rows; ++y) {
48  map[x][image.rows - y - 1] = block_height - (double)image.at<unsigned char>(y, x) / 255 * block_height;
49  }
50  }
51  std::cout << "Loaded height map " << filename_img << std::endl;
52  } else {
53  std::cout << "Could not load height map " << filename_img << std::endl;
54  return 1;
55  }
56 
57  geo::HeightMap hmap = geo::HeightMap::fromGrid(map, resolution);
58 
59  // Transform according to given origin
60  geo::Pose3D transform(origin_x, origin_y, 0, 0, 0, 0);
61  geo::Mesh mesh_transformed = hmap.getMesh().getTransformed(transform);
62 
63  geo::Shape shape;
64  shape.setMesh(mesh_transformed);
65 
66  std::string filename_shape = filename_img + ".geo";
67 
68  geo::serialization::toFile(shape, filename_shape);
69 
70  std::cout << hmap.getMesh().getTriangleIs().size() << " triangles saved to '" << filename_shape << "'." << std::endl;
71  return 0;
72 }
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::cerr
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::getTransformed
Mesh getTransformed(const geo::Transform t) const
Definition: Mesh.cpp:59
std::vector::at
T at(T... args)
geo::serialization::toFile
static void toFile(ShapeConstPtr shape, const std::string &filename)
Definition: serialization.cpp:54
std::endl
T endl(T... args)
serialization.h
geo::Mesh
Definition: Mesh.h:25
main
int main(int argc, char **argv)
Definition: height_image_to_shape.cpp:13
geo::Shape
A geometric description of a shape.
Definition: Shape.h:19