contrib/gel/vsol/vsol_box_3d.cxx
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_box_3d.cxx
00002 #include "vsol_box_3d.h"
00003 //:
00004 // \file
00005 
00006 #include <vbl/io/vbl_io_bounding_box.h>
00007 #include <vcl_cassert.h>
00008 
00009 double vsol_box_3d::get_min_x() const
00010 {
00011   assert(!box_.empty());
00012   return (box_.min())[0];
00013 }
00014 
00015 double vsol_box_3d::get_max_x() const
00016 {
00017   assert(!box_.empty());
00018   return (box_.max())[0];
00019 }
00020 
00021 double vsol_box_3d::get_min_y() const
00022 {
00023   assert(!box_.empty());
00024   return (box_.min())[1];
00025 }
00026 
00027 double vsol_box_3d::get_max_y() const
00028 {
00029   assert(!box_.empty());
00030   return (box_.max())[1];
00031 }
00032 
00033 double vsol_box_3d::get_min_z() const
00034 {
00035   assert(!box_.empty());
00036   return (box_.min())[2];
00037 }
00038 
00039 double vsol_box_3d::get_max_z() const
00040 {
00041   assert(!box_.empty());
00042   return (box_.max())[2];
00043 }
00044 
00045 void vsol_box_3d::add_point(double x, double y, double z)
00046 {
00047   box_.update(x, y, z);
00048 }
00049 
00050 //: compare mins and maxs between this and the comp_box, grow to the bounding box
00051 void vsol_box_3d::grow_minmax_bounds(vsol_box_3d_sptr const& comp_box)
00052 {
00053   if (comp_box->box_.empty()) return;
00054   if (box_.empty()) { operator=(*comp_box); return; }
00055   box_.update(comp_box->get_min_x(),comp_box->get_min_y(),comp_box->get_min_z());
00056   box_.update(comp_box->get_max_x(),comp_box->get_max_y(),comp_box->get_max_z());
00057 }
00058 
00059 //-------------------------------------------------------------------
00060 //:   Determines if this box is inside the right hand side box.
00061 //    That is, all boundaries of *this must be on or inside the boundaries of b.
00062 bool vsol_box_3d::operator< (vsol_box_3d& b) const
00063 {
00064   if (box_.empty()) return true;
00065   if (b.box_.empty()) return false;
00066   return
00067     this->get_min_x() >= b.get_min_x() &&
00068     this->get_min_y() >= b.get_min_y() &&
00069     this->get_min_z() >= b.get_min_z() &&
00070     this->get_max_x() <= b.get_max_x() &&
00071     this->get_max_y() <= b.get_max_y() &&
00072     this->get_max_z() <= b.get_max_z();
00073 }
00074 
00075 inline static bool near_same(double f1, double f2, float tolerance)
00076 {
00077   return f1-f2<tolerance && f2-f1<tolerance;
00078 }
00079 
00080 bool vsol_box_3d::near_equal(vsol_box_3d const& b, float tolerance) const
00081 {
00082   if (box_.empty() && b.box_.empty()) return true;
00083   if (b.box_.empty() || b.box_.empty()) return false;
00084   return
00085     near_same(this->get_min_x(), b.get_min_x(), tolerance) &&
00086     near_same(this->get_min_y(), b.get_min_y(), tolerance) &&
00087     near_same(this->get_min_z(), b.get_min_z(), tolerance) &&
00088     near_same(this->get_max_x(), b.get_max_x(), tolerance) &&
00089     near_same(this->get_max_y(), b.get_max_y(), tolerance) &&
00090     near_same(this->get_max_z(), b.get_max_z(), tolerance);
00091 }
00092 
00093 void vsol_box_3d::reset_bounds()
00094 {
00095   box_.reset();
00096 }
00097 
00098 //----------------------------------------------------------------
00099 // ================   Binary I/O Methods ========================
00100 //----------------------------------------------------------------
00101 
00102 //: Binary save self to stream.
00103 void vsol_box_3d::b_write(vsl_b_ostream &os) const
00104 {
00105   vsl_b_write(os, version());
00106   vsl_b_write(os, box_);
00107 }
00108 
00109 //: Binary load self from stream (not typically used)
00110 void vsol_box_3d::b_read(vsl_b_istream &is)
00111 {
00112   if (!is)
00113     return;
00114   short ver;
00115   vsl_b_read(is, ver);
00116   switch (ver)
00117   {
00118    case 1:
00119     vsl_b_read(is, box_);
00120     break;
00121    default:
00122     vcl_cerr << "vsol_box_3d: unknown I/O version " << ver << '\n';
00123   }
00124 }
00125 
00126 //: Return IO version number;
00127 short vsol_box_3d::version() const
00128 {
00129   return 1;
00130 }
00131 
00132 //: Print an ascii summary to the stream
00133 void vsol_box_3d::print_summary(vcl_ostream &os) const
00134 {
00135   os << *this;
00136 }
00137 
00138 //external functions
00139 vcl_ostream& operator<<(vcl_ostream& s, vsol_box_3d const& b)
00140 {
00141   s << "[(" << b.get_min_x() << ' ' << b.get_min_y() << ' ' << b.get_min_z() << ")("
00142     << b.get_max_x() << ' ' << b.get_max_y() << ' ' << b.get_max_z() << ")]";
00143   return s;
00144 }
00145 
00146 //: Binary save vsol_box_3d_sptr to stream.
00147 void
00148 vsl_b_write(vsl_b_ostream &os, vsol_box_3d_sptr const& b)
00149 {
00150   if (!b){
00151     vsl_b_write(os, false); // Indicate null boxer stored
00152   }
00153   else{
00154     vsl_b_write(os,true); // Indicate non-null boxer stored
00155     b->b_write(os);
00156   }
00157 }
00158 
00159 //: Binary load vsol_box_3d_sptr from stream.
00160 void
00161 vsl_b_read(vsl_b_istream &is, vsol_box_3d_sptr &b)
00162 {
00163   bool not_null_ptr;
00164   vsl_b_read(is, not_null_ptr);
00165   if (not_null_ptr)
00166   {
00167     short ver;
00168     vsl_b_read(is, ver);
00169     switch (ver)
00170     {
00171      case 1: {
00172       vbl_bounding_box<double,3> box;
00173       vsl_b_read(is, box);
00174       b = new vsol_box_3d(box);
00175       break;
00176      }
00177      default:
00178       b = 0;
00179     }
00180   }
00181 }