contrib/gel/vsol/vsol_spatial_object_3d.cxx
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_spatial_object_3d.cxx
00002 #include "vsol_spatial_object_3d.h"
00003 //:
00004 // \file
00005 
00006 #include <vcl_iostream.h>
00007 #include <vsol/vsol_box_3d.h>
00008 #include <vsl/vsl_binary_loader.h>
00009 
00010 const float vsol_spatial_object_3d::eps=1.0e-3f;
00011 //int vsol_spatial_object_3d::tagcount_=0;
00012 
00013 const char * vsol_spatial_object_3d::SpatialTypes[] =
00014 {
00015   "NO_TYPE             ",
00016   "TOPOLOGYOBJECT      ",
00017   "POINT               ",
00018   "CURVE               ",
00019   "REGION              ",
00020   "SPATIALGROUP        ",
00021   "NUM_SPATIALOBJECT_TYPES"
00022 };
00023 
00024 vsol_spatial_object_3d::vsol_spatial_object_3d()
00025   : bounding_box_(0)
00026 {
00027   this->tag_ = 0;
00028   this->id_ = 0;
00029   set_tag_id(++tagcount_);
00030 }
00031 
00032 vsol_spatial_object_3d::vsol_spatial_object_3d(vsol_spatial_object_3d const& s)
00033   : bounding_box_(0)
00034 {
00035   this->tag_ = 0;
00036   this->id_ = s.get_id();
00037   set_tag_id(++tagcount_);
00038 }
00039 
00040 const char * vsol_spatial_object_3d::get_name() const
00041 {
00042   vsol_spatial_object_3d_type type =spatial_type();
00043   if (type > 0 && type < vsol_spatial_object_3d::NUM_SPATIALOBJECT_TYPES)
00044     return SpatialTypes[type];
00045   else
00046     return SpatialTypes[0];
00047 }
00048 
00049 vsol_spatial_object_3d::~vsol_spatial_object_3d()
00050 {
00051 }
00052 
00053 //: Return IO version number;
00054 short vsol_spatial_object_3d::version() const
00055 {
00056   return 1;
00057 }
00058 
00059 void vsol_spatial_object_3d::compute_bounding_box() const
00060 {
00061   if (!bounding_box_) bounding_box_=new vsol_box_3d; bounding_box_->touch();
00062 }
00063 
00064 void vsol_spatial_object_3d::empty_bounding_box() const
00065 {
00066   bounding_box_=new vsol_box_3d;
00067 }
00068 
00069 void vsol_spatial_object_3d::set_bounding_box(double x, double y, double z) const
00070 {
00071   bounding_box_=new vsol_box_3d; bounding_box_->add_point(x,y,z);
00072 }
00073 
00074 void vsol_spatial_object_3d::set_bounding_box(vsol_box_3d_sptr const& box) const
00075 {
00076   bounding_box_=new vsol_box_3d(*box);
00077 }
00078 
00079 void vsol_spatial_object_3d::add_to_bounding_box(double x, double y, double z) const
00080 {
00081   if (!bounding_box_) bounding_box_=new vsol_box_3d; bounding_box_->add_point(x,y,z);
00082 }
00083 
00084 void vsol_spatial_object_3d::add_to_bounding_box(vsol_box_3d_sptr const& comp_box) const
00085 {
00086   if (!bounding_box_)
00087     bounding_box_=new vsol_box_3d;
00088   bounding_box_->grow_minmax_bounds(comp_box);
00089 }
00090 
00091 //: Bounds Accessors:
00092 // min_ and max_ are provided as methods on vsol_spatial_object_3d
00093 // to be consistent with the previous interface
00094 // Additional bounds accessors are available directly
00095 // on vsol_box_3d.  - JLM
00096 
00097 void vsol_spatial_object_3d::check_update_bounding_box() const
00098 {
00099   if (!bounding_box_)
00100   {
00101     bounding_box_ = new vsol_box_3d;
00102     this->compute_bounding_box();
00103     bounding_box_->touch();
00104     return;
00105   }
00106   if (bounding_box_->older(this))
00107   { // NOTE: first touch then compute, to avoid infinite loop!! - PVr
00108     bounding_box_->touch();
00109     this->compute_bounding_box();
00110   }
00111 }
00112 
00113 double vsol_spatial_object_3d::get_min_x() const
00114 {
00115   check_update_bounding_box(); return bounding_box_->get_min_x();
00116 }
00117 
00118 double vsol_spatial_object_3d::get_max_x() const
00119 {
00120   check_update_bounding_box(); return bounding_box_->get_max_x();
00121 }
00122 
00123 double vsol_spatial_object_3d::get_min_y() const
00124 {
00125   check_update_bounding_box(); return bounding_box_->get_min_y();
00126 }
00127 
00128 double vsol_spatial_object_3d::get_max_y() const
00129 {
00130   check_update_bounding_box(); return bounding_box_->get_max_y();
00131 }
00132 
00133 double vsol_spatial_object_3d::get_min_z() const
00134 {
00135   check_update_bounding_box(); return bounding_box_->get_min_z();
00136 }
00137 
00138 double vsol_spatial_object_3d::get_max_z() const
00139 {
00140   check_update_bounding_box(); return bounding_box_->get_max_z();
00141 }
00142 
00143 //: Binary save self to stream.
00144 void
00145 vsol_spatial_object_3d::b_write(vsl_b_ostream &os) const
00146 {
00147   vsl_b_write(os, this->version());
00148   vsl_b_write(os, this->tag_);
00149   vsl_b_write(os, this->id_);
00150 }
00151 
00152 //: Binary load self from stream.
00153 void
00154 vsol_spatial_object_3d::b_read(vsl_b_istream &is)
00155 {
00156   if (!is) return;
00157 
00158   short ver;
00159   vsl_b_read(is, ver);
00160   switch (ver)
00161   {
00162    case 1:
00163     vsl_b_read(is, this->tag_);
00164     vsl_b_read(is, this->id_);
00165     break;
00166 
00167    default:
00168     vcl_cerr << "I/O ERROR: vsol_spatial_object_3d::b_read(vsl_b_istream&)\n"
00169              << "           Unknown version number "<< ver << '\n';
00170     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00171     return;
00172   }
00173 }
00174 
00175 //==============================================
00176 //: Allows derived class to be loaded by base-class pointer.
00177 //  A loader object exists which is invoked by calls
00178 //  of the form "vsl_b_read(os,base_ptr);".  This loads derived class
00179 //  objects from the stream, places them on the heap and
00180 //  returns a base class pointer.
00181 //  In order to work the loader object requires
00182 //  an instance of each derived class that might be
00183 //  found.  This function gives the model class to
00184 //  the appropriate loader.
00185 void vsl_add_to_binary_loader(vsol_spatial_object_3d const& b)
00186 {
00187   vsl_binary_loader<vsol_spatial_object_3d>::instance().add(b);
00188 }