contrib/gel/vsol/vsol_triangle_2d.cxx
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_triangle_2d.cxx
00002 #include "vsol_triangle_2d.h"
00003 //:
00004 // \file
00005 #include <vbl/io/vbl_io_smart_ptr.h>
00006 #include <vsol/vsol_point_2d.h>
00007 #include <vcl_iostream.h>
00008 
00009 //***************************************************************************
00010 // Initialization
00011 //***************************************************************************
00012 
00013 //---------------------------------------------------------------------------
00014 //: Default Constructor - needed for binary I/O
00015 //---------------------------------------------------------------------------
00016 vsol_triangle_2d::vsol_triangle_2d()
00017  : vsol_polygon_2d()
00018 {
00019   storage_->clear();
00020   storage_->push_back(new vsol_point_2d(0.0,0.0));
00021   storage_->push_back(new vsol_point_2d(0.0,1.0));
00022   storage_->push_back(new vsol_point_2d(1.0,0.0));
00023 }
00024 
00025 //---------------------------------------------------------------------------
00026 //: Constructor from its 3 vertices
00027 //---------------------------------------------------------------------------
00028 vsol_triangle_2d::vsol_triangle_2d(const vsol_point_2d_sptr &new_p0,
00029                                    const vsol_point_2d_sptr &new_p1,
00030                                    const vsol_point_2d_sptr &new_p2)
00031  : vsol_polygon_2d()
00032 {
00033   storage_->clear();
00034   storage_->push_back(new_p0);
00035   storage_->push_back(new_p1);
00036   storage_->push_back(new_p2);
00037 }
00038 
00039 //---------------------------------------------------------------------------
00040 // Copy constructor
00041 //---------------------------------------------------------------------------
00042 vsol_triangle_2d::vsol_triangle_2d(const vsol_triangle_2d &other)
00043   : vsol_polygon_2d(other)
00044 {
00045 }
00046 
00047 //---------------------------------------------------------------------------
00048 // Destructor
00049 //---------------------------------------------------------------------------
00050 vsol_triangle_2d::~vsol_triangle_2d()
00051 {
00052 }
00053 
00054 //---------------------------------------------------------------------------
00055 //: Clone `this': creation of a new object and initialization
00056 // See Prototype pattern
00057 //---------------------------------------------------------------------------
00058 vsol_spatial_object_2d* vsol_triangle_2d::clone(void) const
00059 {
00060   return new vsol_triangle_2d(*this);
00061 }
00062 
00063 //***************************************************************************
00064 // Access
00065 //***************************************************************************
00066 
00067 //---------------------------------------------------------------------------
00068 //: Return the first vertex
00069 //---------------------------------------------------------------------------
00070 vsol_point_2d_sptr vsol_triangle_2d::p0(void) const
00071 {
00072   return (*storage_)[0];
00073 }
00074 
00075 //---------------------------------------------------------------------------
00076 //: Return the second vertex
00077 //---------------------------------------------------------------------------
00078 vsol_point_2d_sptr vsol_triangle_2d::p1(void) const
00079 {
00080   return (*storage_)[1];
00081 }
00082 
00083 //---------------------------------------------------------------------------
00084 //: Return the last vertex
00085 //---------------------------------------------------------------------------
00086 vsol_point_2d_sptr vsol_triangle_2d::p2(void) const
00087 {
00088   return (*storage_)[2];
00089 }
00090 
00091 //***************************************************************************
00092 // Comparison
00093 //***************************************************************************
00094 
00095 //---------------------------------------------------------------------------
00096 //: Has `this' the same points than `other' in the same order ?
00097 //---------------------------------------------------------------------------
00098 bool vsol_triangle_2d::operator==(const vsol_triangle_2d &other) const
00099 {
00100   return vsol_polygon_2d::operator==(other);
00101 }
00102 
00103 bool vsol_triangle_2d::operator==(const vsol_polygon_2d &other) const
00104 {
00105   return vsol_polygon_2d::operator==(other);
00106 }
00107 
00108 //: spatial object equality
00109 
00110 bool vsol_triangle_2d::operator==(const vsol_spatial_object_2d& obj) const
00111 {
00112   return
00113     obj.cast_to_region() && obj.cast_to_region()->cast_to_polygon() &&
00114     *this == *obj.cast_to_region()->cast_to_polygon();
00115 }
00116 
00117 //***************************************************************************
00118 // Status report
00119 //***************************************************************************
00120 
00121 //---------------------------------------------------------------------------
00122 //: Return the area of `this'
00123 //---------------------------------------------------------------------------
00124 double vsol_triangle_2d::area(void) const
00125 {
00126   double result;
00127 
00128   result=(((*storage_)[0]->x()-(*storage_)[1]->x())
00129           *((*storage_)[1]->y()-(*storage_)[2]->y())
00130           -((*storage_)[1]->x()-(*storage_)[2]->x())
00131           *((*storage_)[0]->y()-(*storage_)[1]->y()))/2;
00132 
00133   return result;
00134 }
00135 
00136 //***************************************************************************
00137 // Element change
00138 //***************************************************************************
00139 
00140 //---------------------------------------------------------------------------
00141 //: Set the first vertex
00142 //---------------------------------------------------------------------------
00143 void vsol_triangle_2d::set_p0(const vsol_point_2d_sptr &new_p0)
00144 {
00145   (*storage_)[0]=new_p0;
00146 }
00147 
00148 //---------------------------------------------------------------------------
00149 //: Set the second vertex
00150 //---------------------------------------------------------------------------
00151 void vsol_triangle_2d::set_p1(const vsol_point_2d_sptr &new_p1)
00152 {
00153   (*storage_)[1]=new_p1;
00154 }
00155 
00156 //---------------------------------------------------------------------------
00157 //: Set the last vertex
00158 //---------------------------------------------------------------------------
00159 void vsol_triangle_2d::set_p2(const vsol_point_2d_sptr &new_p2)
00160 {
00161   (*storage_)[2]=new_p2;
00162 }
00163 
00164 
00165 //----------------------------------------------------------------
00166 // ================   Binary I/O Methods ========================
00167 //----------------------------------------------------------------
00168 
00169 //: Binary save self to stream.
00170 void vsol_triangle_2d::b_write(vsl_b_ostream &os) const
00171 {
00172   vsl_b_write(os, version());
00173   vsol_polygon_2d::b_write(os);
00174 }
00175 
00176 //: Binary load self from stream (not typically used)
00177 void vsol_triangle_2d::b_read(vsl_b_istream &is)
00178 {
00179   if (!is)
00180     return;
00181   short ver;
00182   vsl_b_read(is, ver);
00183   switch (ver)
00184   {
00185    case 1:
00186     vsol_polygon_2d::b_read(is);
00187     if (storage_->size()!=3) {
00188       vcl_cerr << "I/O ERROR: vsol_triangle_2d::b_read(vsl_b_istream&)\n"
00189                << "           Incorrect number of vertices: "<< storage_->size() << '\n';
00190       is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00191     }
00192     break;
00193    default:
00194     vcl_cerr << "I/O ERROR: vsol_triangle_2d::b_read(vsl_b_istream&)\n"
00195              << "           Unknown version number "<< ver << '\n';
00196     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00197   }
00198 }
00199 
00200 //: Return IO version number;
00201 short vsol_triangle_2d::version() const
00202 {
00203   return 1;
00204 }
00205 
00206 //: Print an ascii summary to the stream
00207 void vsol_triangle_2d::print_summary(vcl_ostream &os) const
00208 {
00209   os << *this;
00210 }
00211 
00212 //external functions
00213 
00214 //: Binary save vsol_triangle_2d_sptr to stream.
00215 void
00216 vsl_b_write(vsl_b_ostream &os, const vsol_triangle_2d* t)
00217 {
00218   if (!t){
00219     vsl_b_write(os, false); // Indicate null triangle stored
00220   }
00221   else{
00222     vsl_b_write(os,true); // Indicate non-null triangle stored
00223     t->b_write(os);
00224   }
00225 }
00226 
00227 //: Binary load vsol_triangle_2d* from stream.
00228 void
00229 vsl_b_read(vsl_b_istream &is, vsol_triangle_2d* &t)
00230 {
00231   delete t;
00232   bool not_null_ptr;
00233   vsl_b_read(is, not_null_ptr);
00234   if (not_null_ptr) {
00235     t = new vsol_triangle_2d();
00236     t->b_read(is);
00237   }
00238   else
00239     t = 0;
00240 }
00241 
00242 
00243 inline void vsol_triangle_2d::describe(vcl_ostream &strm, int blanking) const
00244 {
00245   if (blanking < 0) blanking = 0; while (blanking--) strm << ' ';
00246   strm << "<vsol_triangle_2d with corners";
00247   for (unsigned int i=0; i<size(); ++i)
00248     strm << ' ' << *(vertex(i));
00249   strm << '>' << vcl_endl;
00250 }
00251