contrib/brl/bseg/sdet/sdet_vrml_display.cxx
Go to the documentation of this file.
00001 #include "sdet_vrml_display.h"
00002 //
00003 #include <vtol/vtol_intensity_face.h>
00004 #include <vtol/vtol_edge_2d.h>
00005 #include <vsol/vsol_curve_2d_sptr.h>
00006 #include <vdgl/vdgl_digital_curve.h>
00007 #include <vsol/vsol_point_2d.h>
00008 #include <vsol/vsol_point_3d.h>
00009 #include <vsol/vsol_polygon_3d.h>
00010 
00011 void sdet_vrml_display::write_vrml_header(vcl_ofstream& str)
00012 {
00013   str << "#VRML V2.0 utf8\n"
00014       << "Background {\n"
00015       << "  skyColor [ 0 0 0 ]\n"
00016       << "  groundColor [ 0 0 0 ]\n"
00017       << "}\n"
00018       << "PointLight {\n"
00019       << "  on FALSE\n"
00020       << "  intensity 1\n"
00021       << "ambientIntensity 0\n"
00022       << "color 1 1 1\n"
00023       << "location 0 0 0\n"
00024       << "attenuation 1 0 0\n"
00025       << "radius 100\n"
00026       << "}\n";
00027 }
00028 
00029 static void write_index_preamble(vcl_ofstream& str)
00030 {
00031   str << "Transform {\n"
00032       << "translation 0 0  0\n"
00033       << " children [\n"
00034       << " Shape {\n"
00035       << "  appearance Appearance{\n"
00036       << "   material Material\n"
00037       << "  {\n"
00038       << "   diffuseColor 0.0 1.0 0.0\n"
00039       << "   emissiveColor 0.0 1.0 0.0\n"
00040       << "   }\n"
00041       << " }\n"
00042       << " geometry IndexedLineSet\n"
00043       << " {\n"
00044       << "  coord Coordinate{\n"
00045       << "   point[\n";
00046 }
00047 
00048 static void write_coor_index(vcl_ofstream& str, unsigned n)
00049 {
00050   str << " coordIndex [\n";
00051   for (unsigned i = 0; i<n; ++i)
00052     str << i << ',';
00053   str << -1 << ", ]\n"
00054       << "}\n"
00055       << "} ]\n"
00056       << "}\n";
00057 }
00058 
00059 void sdet_vrml_display::
00060 write_intensity_regions_3d(vcl_ofstream& str,
00061                            vcl_vector<vtol_intensity_face_sptr> const& faces)
00062 {
00063   for (vcl_vector<vtol_intensity_face_sptr>::const_iterator fit = faces.begin();
00064       fit != faces.end(); ++fit)
00065   {
00066     vtol_intensity_face_sptr f = (*fit);
00067     if (f->area()==0) continue;
00068     //average region height
00069     double z0 = f->Io();
00070     //get the outer boundary
00071     vtol_one_chain_sptr och = f->get_boundary_cycle();
00072     unsigned nedges = och->num_edges();
00073     for (unsigned i = 0; i<nedges; ++i)
00074     {
00075       vtol_edge_sptr e = och->edge(i);
00076       vtol_edge_2d* e2d = (vtol_edge_2d*)(e.ptr());
00077       vsol_curve_2d_sptr c = e2d->curve();
00078       vdgl_digital_curve* dc = c->cast_to_vdgl_digital_curve();
00079       if (!dc) continue;
00080       write_index_preamble(str);
00081       unsigned n = static_cast<unsigned>(dc->n_pts());
00082       if (n<2) continue;
00083       double ds = 1.0/(n-1);
00084       for (unsigned j = 0; j<n; ++j){
00085         double s = j*ds;
00086         double x = dc->get_x(s), y = dc->get_y(s);
00087         str << x << ' ' << y << ' ' << z0 << '\n';
00088       }
00089       str << "   ]\n";
00090       str << " }\n";
00091       write_coor_index(str, n);
00092     }
00093   }
00094 }
00095 
00096 void sdet_vrml_display::
00097 write_vsol_polys_3d(vcl_ofstream& str,
00098                     vcl_vector<vsol_polygon_3d_sptr> const& polys)
00099 {
00100   for (vcl_vector<vsol_polygon_3d_sptr>::const_iterator pit = polys.begin();
00101       pit != polys.end(); ++pit)
00102   {
00103     vsol_polygon_3d_sptr poly = *pit;
00104     unsigned n = poly->size();
00105     if (!n)
00106       continue;
00107     write_index_preamble(str);
00108     for (unsigned i = 0; i<n; ++i){
00109       vsol_point_3d_sptr p = poly->vertex(i);
00110       str << p->x() << ' ' << p->y() << ' ' << p->z() << '\n';
00111     }
00112     str << "   ]\n";
00113     str << " }\n";
00114     write_coor_index(str, n);
00115   }
00116 }
00117 
00118 void sdet_vrml_display::
00119 write_vrml_height_map(vcl_ofstream& str,
00120                       vil_image_view<float> const & z_of_xy,
00121                       float r, float g, float b)
00122 {
00123   unsigned ni = z_of_xy.ni(), nj = z_of_xy.nj();
00124   //normalize the z values to produce a rough cube of points
00125   unsigned n = ni*nj;
00126   float max = 0;
00127   for (unsigned j = 0; j<nj; ++j)
00128     for (unsigned i = 0; i<ni; ++i)
00129       if (z_of_xy(i,j)>max)
00130         max = z_of_xy(i,j);
00131   unsigned w = ni;
00132   if (nj>w)
00133     w = nj;
00134   float z_scale = 1.0f;
00135   if (max)
00136     z_scale = w/max;
00137   str << "Shape {\n"
00138       << "  appearance NULL\n"
00139       << "    geometry PointSet {\n"
00140       << "      color Color{\n"
00141       << "       color[\n";
00142   for (unsigned i =0; i<n; i++)
00143     str << r << ' '
00144         << g << ' '
00145         << b << '\n';
00146   str << "   ]\n  }\n"
00147       << "      coord Coordinate{\n"
00148       << "       point[\n";
00149 
00150   for (unsigned j = 0; j<nj; ++j)
00151     for (unsigned i = 0; i<ni; ++i)
00152       str << i << ' ' << j << ' ' << z_of_xy(i,j)*z_scale << '\n';
00153   str << "   ]\n  }\n }\n}\n";
00154 }