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