1 #include "sdet_vrml_display.h"
2 //
3 #include <vtol/vtol_intensity_face.h>
4 #include <vtol/vtol_edge_2d.h>
5 #include <vsol/vsol_curve_2d_sptr.h>
6 #include <vdgl/vdgl_digital_curve.h>
7 #include <vsol/vsol_point_2d.h>
8 #include <vsol/vsol_point_3d.h>
9 #include <vsol/vsol_polygon_3d.h>
10 
write_vrml_header(std::ofstream & str)11 void sdet_vrml_display::write_vrml_header(std::ofstream& str)
12 {
13   str << "#VRML V2.0 utf8\n"
14       << "Background {\n"
15       << "  skyColor [ 0 0 0 ]\n"
16       << "  groundColor [ 0 0 0 ]\n"
17       << "}\n"
18       << "PointLight {\n"
19       << "  on FALSE\n"
20       << "  intensity 1\n"
21       << "ambientIntensity 0\n"
22       << "color 1 1 1\n"
23       << "location 0 0 0\n"
24       << "attenuation 1 0 0\n"
25       << "radius 100\n"
26       << "}\n";
27 }
28 
write_index_preamble(std::ofstream & str)29 static void write_index_preamble(std::ofstream& str)
30 {
31   str << "Transform {\n"
32       << "translation 0 0  0\n"
33       << " children [\n"
34       << " Shape {\n"
35       << "  appearance Appearance{\n"
36       << "   material Material\n"
37       << "  {\n"
38       << "   diffuseColor 0.0 1.0 0.0\n"
39       << "   emissiveColor 0.0 1.0 0.0\n"
40       << "   }\n"
41       << " }\n"
42       << " geometry IndexedLineSet\n"
43       << " {\n"
44       << "  coord Coordinate{\n"
45       << "   point[\n";
46 }
47 
write_coor_index(std::ofstream & str,unsigned n)48 static void write_coor_index(std::ofstream& str, unsigned n)
49 {
50   str << " coordIndex [\n";
51   for (unsigned i = 0; i<n; ++i)
52     str << i << ',';
53   str << -1 << ", ]\n"
54       << "}\n"
55       << "} ]\n"
56       << "}\n";
57 }
58 
59 void sdet_vrml_display::
write_intensity_regions_3d(std::ofstream & str,std::vector<vtol_intensity_face_sptr> const & faces)60 write_intensity_regions_3d(std::ofstream& str,
61                            std::vector<vtol_intensity_face_sptr> const& faces)
62 {
63   for (const auto & face : faces)
64   {
65     const vtol_intensity_face_sptr& f = face;
66     if (f->area()==0) continue;
67     //average region height
68     double z0 = f->Io();
69     //get the outer boundary
70     vtol_one_chain_sptr och = f->get_boundary_cycle();
71     unsigned nedges = och->num_edges();
72     for (unsigned i = 0; i<nedges; ++i)
73     {
74       vtol_edge_sptr e = och->edge(i);
75       auto* e2d = (vtol_edge_2d*)(e.ptr());
76       vsol_curve_2d_sptr c = e2d->curve();
77       vdgl_digital_curve* dc = c->cast_to_vdgl_digital_curve();
78       if (!dc) continue;
79       write_index_preamble(str);
80       auto n = static_cast<unsigned>(dc->n_pts());
81       if (n<2) continue;
82       double ds = 1.0/(n-1);
83       for (unsigned j = 0; j<n; ++j){
84         double s = j*ds;
85         double x = dc->get_x(s), y = dc->get_y(s);
86         str << x << ' ' << y << ' ' << z0 << '\n';
87       }
88       str << "   ]\n";
89       str << " }\n";
90       write_coor_index(str, n);
91     }
92   }
93 }
94 
95 void sdet_vrml_display::
write_vsol_polys_3d(std::ofstream & str,std::vector<vsol_polygon_3d_sptr> const & polys)96 write_vsol_polys_3d(std::ofstream& str,
97                     std::vector<vsol_polygon_3d_sptr> const& polys)
98 {
99   for (const auto& poly : polys)
100   {
101     unsigned n = poly->size();
102     if (!n)
103       continue;
104     write_index_preamble(str);
105     for (unsigned i = 0; i<n; ++i){
106       vsol_point_3d_sptr p = poly->vertex(i);
107       str << p->x() << ' ' << p->y() << ' ' << p->z() << '\n';
108     }
109     str << "   ]\n";
110     str << " }\n";
111     write_coor_index(str, n);
112   }
113 }
114 
115 void sdet_vrml_display::
write_vrml_height_map(std::ofstream & str,vil_image_view<float> const & z_of_xy,float r,float g,float b)116 write_vrml_height_map(std::ofstream& str,
117                       vil_image_view<float> const & z_of_xy,
118                       float r, float g, float b)
119 {
120   unsigned ni = z_of_xy.ni(), nj = z_of_xy.nj();
121   //normalize the z values to produce a rough cube of points
122   unsigned n = ni*nj;
123   float max = 0;
124   for (unsigned j = 0; j<nj; ++j)
125     for (unsigned i = 0; i<ni; ++i)
126       if (z_of_xy(i,j)>max)
127         max = z_of_xy(i,j);
128   unsigned w = ni;
129   if (nj>w)
130     w = nj;
131   float z_scale = 1.0f;
132   if (max)
133     z_scale = w/max;
134   str << "Shape {\n"
135       << "  appearance NULL\n"
136       << "    geometry PointSet {\n"
137       << "      color Color{\n"
138       << "       color[\n";
139   for (unsigned i =0; i<n; i++)
140     str << r << ' '
141         << g << ' '
142         << b << '\n';
143   str << "   ]\n  }\n"
144       << "      coord Coordinate{\n"
145       << "       point[\n";
146 
147   for (unsigned j = 0; j<nj; ++j)
148     for (unsigned i = 0; i<ni; ++i)
149       str << i << ' ' << j << ' ' << z_of_xy(i,j)*z_scale << '\n';
150   str << "   ]\n  }\n }\n}\n";
151 }
152