00001 #include "brct_volume_processor.h"
00002 #include <vcl_fstream.h>
00003 #include <vsol/vsol_box_3d.h>
00004 #include <vsol/vsol_point_3d_sptr.h>
00005 #include <vsol/vsol_point_3d.h>
00006 #include <bbas/bsol/bsol_algs.h>
00007 #include <brct/brct_algos.h>
00008
00009 brct_volume_processor::brct_volume_processor(brct_volume_processor_params const& vp)
00010 : brct_volume_processor_params(vp)
00011 {
00012 vsol_box_3d_sptr b = new vsol_box_3d;
00013 b->add_point(xmin_, ymin_, zmin_);
00014 b->add_point(xmax_, ymax_, zmax_);
00015 double w = b->width(), h = b->height(), d = b->depth();
00016 double r = 1;
00017 if (cube_edge_length_&&cube_edge_length_>0)
00018 r = 1.0/cube_edge_length_;
00019 ncols_ = int(w*r), nrows_ = int(h*r), nslabs_ = int(d*r);
00020 index_ = new bsol_point_index_3d(ncols_, nrows_, nslabs_, b);
00021 change_index_ = new bsol_point_index_3d(ncols_, nrows_, nslabs_, b);
00022 }
00023
00024 brct_volume_processor::~brct_volume_processor()
00025 {
00026 delete index_;
00027 delete change_index_;
00028 }
00029
00030 bool brct_volume_processor::read_points_3d_vrml(vcl_string const& filename)
00031 {
00032 vcl_ifstream is(filename.c_str());
00033 if (!is)
00034 {
00035 vcl_cout << "In brct_volume_processor::read points vrml -"
00036 << " could not open file " << filename << '\n';
00037 return false;
00038 }
00039 vcl_vector<vsol_point_3d_sptr> pts3d;
00040 brct_algos::read_vrml_points(is, pts3d);
00041 int npts = pts3d.size(),nin = 0;
00042 for (int i = 0; i<npts; i++)
00043 if ((*index_).add_point(pts3d[i]))
00044 nin++;
00045 vcl_cout << "Added " << nin << "out of " << npts << " points\n"
00046 << "Point Bounds\n";
00047 bsol_algs::print((*index_).point_bounds());
00048 return true;
00049 }
00050
00051 bool brct_volume_processor::write_prob_volumes_vrml(vcl_string const& filename)
00052 {
00053 vcl_ofstream os(filename.c_str());
00054 if (!os)
00055 {
00056 vcl_cout << "In brct_volume_processor::write_prob_volumes vrml -"
00057 << " could not open file " << filename << '\n';
00058 return false;
00059 }
00060 brct_algos::write_vrml_header(os);
00061 vcl_vector<vsol_point_3d_sptr> points;
00062 #if 0 // "scal" is not used !?!
00063 float scal = 100.f;
00064 if ((*index_).n_points() != 0)
00065 scal /= (*index_).n_points();
00066 #endif
00067 for (int r = 0; r<nrows_; r++)
00068 for (int c = 0; c<ncols_; c++)
00069 for (int s = 0; s<nslabs_; s++)
00070 {
00071 int n_points = (*index_).n_points(r, c, s);
00072 vsol_box_3d_sptr box = (*index_).index_cell(r, c, s);
00073 float f = 1;
00074 if (n_points>0)
00075 f = 0.5;
00076 brct_algos::write_vrml_box(os, box, 1.0f, 1.0f, 1.0f, f);
00077 }
00078
00079 brct_algos::write_vrml_trailer(os);
00080 return true;
00081 }
00082
00083 bool brct_volume_processor::read_change_data_vrml(vcl_string const& filename)
00084 {
00085 vcl_ifstream is(filename.c_str());
00086 if (!is)
00087 {
00088 vcl_cout << "In brct_volume_processor::read change data vrml -"
00089 << " could not open file " << filename << '\n';
00090 return false;
00091 }
00092 (*change_index_).clear();
00093 vcl_vector<vsol_point_3d_sptr> pts3d;
00094 brct_algos::read_vrml_points(is, pts3d);
00095 int npts = pts3d.size(),nin = 0;
00096 for (int i = 0; i<npts; i++)
00097 if ((*change_index_).add_point(pts3d[i]))
00098 nin++;
00099 if (!npts||!nin)
00100 {
00101 vcl_cout << "In brct_volume_processor::read_change_data_vrml -"
00102 << " no data or can't index data\n";
00103 return false;
00104 }
00105 return true;
00106 }
00107
00108 bool brct_volume_processor::compute_change()
00109 {
00110 change_volumes_.clear();
00111 for (int r = 0; r<nrows_; r++)
00112 for (int c = 0; c<ncols_; c++)
00113 for (int s = 0; s<nslabs_; s++)
00114 {
00115 int ni = (*index_).n_points(r, c, s);
00116 int nc = (*change_index_).n_points(r, c, s);
00117 if (nc>cell_thresh_&&ni<cell_thresh_)
00118 change_volumes_.push_back((*index_).index_cell(r, c, s));
00119 }
00120 vcl_cout << "Found " << change_volumes_.size() << " change cells\n";
00121 return true;
00122 }
00123
00124 bool brct_volume_processor::
00125 write_changed_volumes_vrml(vcl_string const& filename)
00126 {
00127 int nv = change_volumes_.size();
00128 if (!nv)
00129 {
00130 vcl_cout << "In bool brct_volume_processor::write_changed_volumes_vrml -"
00131 << " no change volumes\n";
00132 return false;
00133 }
00134 vcl_ofstream os(filename.c_str());
00135 if (!os)
00136 {
00137 vcl_cout << "In brct_volume_processor::write_changed_volumes_vrml -"
00138 << " could not open file " << filename << '\n';
00139 return false;
00140 }
00141
00142 brct_algos::write_vrml_header(os);
00143 for (int i = 0; i<nv; i++)
00144 brct_algos::write_vrml_box(os, change_volumes_[i], 1.0, 0.0);
00145 brct_algos::write_vrml_trailer(os);
00146 return true;
00147 }