ed
test_heightmap_triangulation.cpp
Go to the documentation of this file.
1 // Shape loader, contains heightmap meshing algorithm
2 #include "../src/models/shape_loader_private.h"
3 
4 // Visualization
5 #include <opencv2/highgui/highgui.hpp>
6 #include <opencv2/imgproc/imgproc.hpp>
7 
8 // Shape traversal
9 #include <geolib/Shape.h>
10 
11 // Config settings
12 #include <tue/config/writer.h>
13 
14 // ----------------------------------------------------------------------------------------------------
15 
16 int main(int argc, char **argv)
17 {
18  if (argc <= 1)
19  {
20  std::cout << "Please provide a heightmap image file (e.g., pgm)" << std::endl;
21  return 0;
22  }
23 
24  std::string image_filename = argv[1];
25 
26  // Read input image
27  cv::Mat viz = cv::imread(image_filename);
28 
29  if (!viz.data)
30  {
31  std::cout << "Could not load file." << std::endl;
32  return 1;
33  }
34 
35  // Set some default config the shape loader needs
37  w.setValue("path", image_filename);
38  w.setValue("origin_x", 0);
39  w.setValue("origin_y", 0);
40  w.setValue("origin_z", 0);
41  w.setValue("resolution", 1);
42  w.setValue("blockheight", 0);
43 
44  tue::config::Reader cfg(w.data()); // Wrap config in reader
45 
46  std::map<std::string, geo::ShapePtr> shape_cache; // necessary for call, not used
47 
48  // Call shape loader. This will generate a mesh from the file
50  geo::ShapePtr shape = ed::models::loadShape("", cfg, shape_cache, error);
51 
52  if (!shape)
53  {
54  std::cout << "Could not generate mesh from file:" << std::endl;
55  std::cout << error.str() << std::endl;
56  return 1;
57  }
58 
59  const std::vector<geo::TriangleI>& triangles = shape->getMesh().getTriangleIs();
60  const std::vector<geo::Vector3>& vertices = shape->getMesh().getPoints();
61 
62  // Display number of vertices and triangles
63  std::cout << vertices.size() << " vertices" << std::endl;
64  std::cout << triangles.size() << " triangles" << std::endl;
65 
66  // Visualize triangles
67  for(std::vector<geo::TriangleI>::const_iterator it = triangles.begin(); it != triangles.end(); ++it)
68  {
69  const geo::TriangleI& t = *it;
70 
71  geo::Vector3 v1 = vertices[t.i1_];
72  geo::Vector3 v2 = vertices[t.i2_];
73  geo::Vector3 v3 = vertices[t.i3_];
74 
75  v1.y = viz.rows - v1.y;
76  v2.y = viz.rows - v2.y;
77  v3.y = viz.rows - v3.y;
78 
79  cv::line(viz, cv::Point(v1.x, v1.y), cv::Point(v2.x, v2.y), cv::Scalar(0, 0, 255), 1);
80  cv::line(viz, cv::Point(v2.x, v2.y), cv::Point(v3.x, v3.y), cv::Scalar(0, 0, 255), 1);
81  cv::line(viz, cv::Point(v3.x, v3.y), cv::Point(v1.x, v1.y), cv::Scalar(0, 0, 255), 1);
82  }
83 
84  cv::imshow("image", viz);
85  cv::waitKey();
86 
87  return 0;
88 }
geo::Vector3::y
const real & y() const
tue::config::Writer
std::string
std::shared_ptr
t
Timer t
std::vector< geo::TriangleI >
std::vector::size
T size(T... args)
Shape.h
std::stringstream
std::cout
geo::TriangleI
ed::log::error
std::ostream & error()
Definition: logging.cpp:74
tue::config::Reader
ed::models::loadShape
geo::ShapePtr loadShape(const std::string &model_path, tue::config::Reader cfg, std::map< std::string, geo::ShapePtr > &shape_cache, std::stringstream &error)
loadShape load the shape of a model.
Definition: shape_loader.cpp:621
geo::Vector3
std::map
std::endl
T endl(T... args)
std::vector::begin
T begin(T... args)
geo::Vector3::x
const real & x() const
tue::config::Writer::data
DataConstPointer data() const
std::vector::end
T end(T... args)
tue::config::Writer::setValue
void setValue(const std::string &name, const T &value)
main
int main(int argc, char **argv)
Definition: test_heightmap_triangulation.cpp:16
writer.h