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