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