contrib/gel/vsol/vsol_cylinder.cxx
Go to the documentation of this file.
00001 #include "vsol_cylinder.h"
00002 //:
00003 // \file
00004 #include <vgl/io/vgl_io_cylinder.h>
00005 #include <vgl/vgl_vector_3d.h>
00006 
00007 //: Return true iff the point p is inside (or on) this cylinder
00008 bool vsol_cylinder::contains(vgl_point_3d<double> const& p)
00009 {
00010   double x= cyl_.center().x() - ((cyl_.length()/2.0) * cyl_.orientation().x());
00011   double y= cyl_.center().y() - ((cyl_.length()/2.0) * cyl_.orientation().y());
00012   double z= cyl_.center().z() - ((cyl_.length()/2.0) * cyl_.orientation().z());
00013   vgl_point_3d<double> bottom_center(x, y, z);
00014 
00015   double x2= cyl_.center().x() + ((cyl_.length()/2.0) * cyl_.orientation().x());
00016   double y2= cyl_.center().y() + ((cyl_.length()/2.0) * cyl_.orientation().y());
00017   double z2= cyl_.center().z() + ((cyl_.length()/2.0) * cyl_.orientation().z());
00018   vgl_point_3d<double> top_center(x2, y2, z2);
00019 
00020   vgl_vector_3d<double> v = p - bottom_center;
00021   vgl_vector_3d<double> d = top_center - bottom_center;
00022   double dot_p = dot_product(v, d);
00023   double length_sqr = cyl_.length()*cyl_.length();
00024 
00025   if ((dot_p < 0.0) || (dot_p > length_sqr))
00026     return false;
00027   else {
00028     double dsq = v.sqr_length() - (dot_p*dot_p/length_sqr);
00029     if (dsq > (cyl_.radius()*cyl_.radius()))
00030       return false;
00031     else
00032       return true;
00033   }
00034 }
00035 
00036 //----------------------------------------------------------------
00037 // ================   Binary I/O Methods ========================
00038 //----------------------------------------------------------------
00039 
00040 //: Binary save self to stream.
00041 void vsol_cylinder::b_write(vsl_b_ostream &os) const
00042 {
00043   vsl_b_write(os, version());
00044   vsl_b_write(os, cyl_);
00045 }
00046 
00047 //: Binary load self from stream
00048 void vsol_cylinder::b_read(vsl_b_istream &is)
00049 {
00050   if (!is)
00051     return;
00052   short ver;
00053   vsl_b_read(is, ver);
00054   switch (ver)
00055   {
00056    case 1:
00057     vsl_b_read(is, cyl_);
00058     break;
00059    default:
00060     vcl_cerr << "vsol_cylinder: unknown I/O version " << ver << '\n';
00061   }
00062 }
00063 
00064 //: Return IO version number;
00065 short vsol_cylinder::version() const
00066 {
00067   return 1;
00068 }
00069 
00070 //: Print an ascii summary to the stream
00071 void vsol_cylinder::print_summary(vcl_ostream &os) const
00072 {
00073   os << *this;
00074 }
00075 
00076 //: describe to the output stream
00077 void vsol_cylinder::describe(vcl_ostream &strm, int blanking) const
00078 {
00079   if (blanking < 0) blanking = 0;
00080   while (blanking--) { strm << ' '; }
00081   strm << "[vsol_cylinder center=" << cyl_.center()
00082        << " radius=" << cyl_.radius() << " length=" << cyl_.length()
00083        << " direction=" << cyl_.orientation()
00084        << ']' << vcl_endl;
00085 }
00086 
00087 //: Binary save vsol_cylinder* to stream.
00088 void
00089 vsl_b_write(vsl_b_ostream &os, const vsol_cylinder* p)
00090 {
00091   if (p==0) {
00092     vsl_b_write(os, false); // Indicate null pointer stored
00093   }
00094   else{
00095     vsl_b_write(os,true); // Indicate non-null pointer stored
00096     p->b_write(os);
00097   }
00098 }
00099 
00100 //: Binary save vsol_cylinder_sptr to stream.
00101 void
00102 vsl_b_write(vsl_b_ostream &os, const vsol_cylinder_sptr &p)
00103 {
00104   if (p==0) {
00105     vsl_b_write(os, false); // Indicate null pointer stored
00106   }
00107   else{
00108     vsl_b_write(os,true); // Indicate non-null pointer stored
00109     p->b_write(os);
00110   }
00111 }
00112 
00113 //: Binary load vsol_cylinder* from stream.
00114 void
00115 vsl_b_read(vsl_b_istream &is, vsol_cylinder* &cyl)
00116 {
00117   delete cyl; cyl=0;
00118   bool not_null_ptr;
00119   vsl_b_read(is, not_null_ptr);
00120   if (not_null_ptr) {
00121     cyl = new vsol_cylinder();
00122     cyl->b_read(is);
00123   }
00124 }
00125 
00126 //: Binary load vsol_cylinder_sptr from stream.
00127 void
00128 vsl_b_read(vsl_b_istream &is, vsol_cylinder_sptr &cyl)
00129 {
00130   bool not_null_ptr;
00131   vsl_b_read(is, not_null_ptr);
00132   if (not_null_ptr) {
00133     cyl = new vsol_cylinder();
00134     cyl->b_read(is);
00135   }
00136 }
00137