00001 // This is gel/vsol/vsol_rectangle_2d.h 00002 #ifndef vsol_rectangle_2d_h_ 00003 #define vsol_rectangle_2d_h_ 00004 //***************************************************************************** 00005 //: 00006 // \file 00007 // \brief Rectangle in 2D space 00008 // 00009 // The "width/abscissa" (long axis) of the rectangle is along p0-p1, p2-p3, 00010 // the "height/ordinate" (short axis) of the rectangle is along p1-p2, p3-p0. 00011 // 00012 // Note, a constructor from 3 vertices is also defined 00013 // The 3 vertices are to be defined in counterclockwise order, 00014 // with a 90 degree corner between p_c-p_abs and p_c-p_ord. 00015 // \verbatim 00016 // p3 p_ord p2 00017 // o ------------o------------o 00018 // | | | 00019 // | | | 00020 // | o------------o p_abs 00021 // | p_c | 00022 // | | 00023 // o -------------------------o 00024 // p0 p1 00025 // \endverbatim 00026 // 00027 // \author François BERTEL 00028 // \date 2000-05-08 00029 // 00030 // \verbatim 00031 // Modifications 00032 // 2000-05-08 François BERTEL Creation 00033 // 2000-06-17 Peter Vanroose Implemented all operator==()s and type info 00034 // 2001-07-03 Peter Vanroose Replaced vnl_double_2 by vgl_vector_2d 00035 // 2004-05-11 Joseph Mundy Changed internal representation to 4 verts, 00036 // which allows consistent polygon operations. 00037 // 3 vert constructor is maintained 00038 // 2004-05-14 Peter Vanroose Added describe() 00039 // \endverbatim 00040 //***************************************************************************** 00041 00042 //***************************************************************************** 00043 // External declarations for values 00044 //***************************************************************************** 00045 #include <vsol/vsol_polygon_2d.h> 00046 #include <vsl/vsl_binary_io.h> 00047 #include <vcl_iosfwd.h> 00048 00049 class vsol_rectangle_2d : public vsol_polygon_2d 00050 { 00051 public: 00052 //*************************************************************************** 00053 // Initialization 00054 //*************************************************************************** 00055 00056 //--------------------------------------------------------------------------- 00057 //: Default Constructor - needed for binary I/O 00058 //--------------------------------------------------------------------------- 00059 vsol_rectangle_2d(); 00060 00061 //--------------------------------------------------------------------------- 00062 //: Constructor from 4 points, the corners of the rectangle 00063 //--------------------------------------------------------------------------- 00064 vsol_rectangle_2d(const vsol_point_2d_sptr &new_p0, 00065 const vsol_point_2d_sptr &new_p1, 00066 const vsol_point_2d_sptr &new_p2, 00067 const vsol_point_2d_sptr &new_p3); 00068 00069 //--------------------------------------------------------------------------- 00070 //: Constructor from 3 points. 00071 // `new_pc' is the origin of the rectangle. `new_pabs' defines the abscissa 00072 // axis and the width/2. `new_pord' defines the ordinate axis and the 00073 // height/2. 00074 // REQUIRE: valid_vertices(new_pc,new_pabs,new_pord) 00075 //--------------------------------------------------------------------------- 00076 vsol_rectangle_2d(const vsol_point_2d_sptr &new_pc, 00077 const vsol_point_2d_sptr &new_pabs, 00078 const vsol_point_2d_sptr &new_pord); 00079 00080 00081 //--------------------------------------------------------------------------- 00082 //: Constructor from center, half_width, half_height, angle(ccw from x axis, in deg/rad) 00083 //--------------------------------------------------------------------------- 00084 vsol_rectangle_2d(const vsol_point_2d_sptr ¢er, 00085 const double half_width, 00086 const double half_height, 00087 const double angle, 00088 const bool deg = true); 00089 00090 //--------------------------------------------------------------------------- 00091 //: Copy constructor 00092 //--------------------------------------------------------------------------- 00093 vsol_rectangle_2d(const vsol_rectangle_2d &other); 00094 00095 //--------------------------------------------------------------------------- 00096 //: Destructor 00097 //--------------------------------------------------------------------------- 00098 virtual ~vsol_rectangle_2d(); 00099 00100 //--------------------------------------------------------------------------- 00101 //: Clone `this': creation of a new object and initialization 00102 // See Prototype pattern 00103 //--------------------------------------------------------------------------- 00104 virtual vsol_spatial_object_2d* clone(void) const; 00105 00106 //*************************************************************************** 00107 // Access 00108 //*************************************************************************** 00109 00110 //--------------------------------------------------------------------------- 00111 //: Return the first vertex 00112 //--------------------------------------------------------------------------- 00113 vsol_point_2d_sptr p0(void) const; 00114 00115 //--------------------------------------------------------------------------- 00116 //: Return the second vertex 00117 //--------------------------------------------------------------------------- 00118 vsol_point_2d_sptr p1(void) const; 00119 00120 //--------------------------------------------------------------------------- 00121 //: Return the third vertex 00122 //--------------------------------------------------------------------------- 00123 vsol_point_2d_sptr p2(void) const; 00124 00125 //--------------------------------------------------------------------------- 00126 //: Return the last vertex 00127 //--------------------------------------------------------------------------- 00128 vsol_point_2d_sptr p3(void) const; 00129 00130 //*************************************************************************** 00131 // Comparison 00132 //*************************************************************************** 00133 00134 //--------------------------------------------------------------------------- 00135 //: Has `this' the same points than `other' in the same order ? 00136 //--------------------------------------------------------------------------- 00137 virtual bool operator==(const vsol_rectangle_2d &other) const; 00138 inline bool operator!=(const vsol_rectangle_2d &other)const{return !operator==(other);} 00139 virtual bool operator==(const vsol_polygon_2d &other) const; // virtual of vsol_polygon_2d 00140 virtual bool operator==(const vsol_spatial_object_2d& obj) const; // virtual of vsol_spatial_object_2d 00141 00142 //*************************************************************************** 00143 // Status report 00144 //*************************************************************************** 00145 00146 //--------------------------------------------------------------------------- 00147 //: Return the width 00148 //--------------------------------------------------------------------------- 00149 double width(void) const; 00150 00151 //--------------------------------------------------------------------------- 00152 //: Return the height 00153 //--------------------------------------------------------------------------- 00154 double height(void) const; 00155 00156 //--------------------------------------------------------------------------- 00157 //: Return the area of `this' 00158 //--------------------------------------------------------------------------- 00159 virtual double area(void) const; 00160 00161 virtual vsol_rectangle_2d* cast_to_rectangle(void) { return this; } 00162 virtual vsol_rectangle_2d const* cast_to_rectangle(void) const { return this; } 00163 // ==== Binary IO methods ====== 00164 00165 //: Binary save self to stream. 00166 void b_write(vsl_b_ostream &os) const; 00167 00168 //: Binary load self from stream. 00169 void b_read(vsl_b_istream &is); 00170 00171 //: Return IO version number; 00172 short version() const; 00173 00174 //: Print an ascii summary to the stream 00175 void print_summary(vcl_ostream &os) const; 00176 00177 //: Return a platform independent string identifying the class 00178 virtual vcl_string is_a() const { return "vsol_rectangle_2d"; } 00179 00180 //: Return true if the argument matches the string identifying the class or any parent class 00181 virtual bool is_class(const vcl_string& cls) const 00182 { return cls==is_a() || vsol_polygon_2d::is_class(cls); } 00183 00184 //--------------------------------------------------------------------------- 00185 //: Are `new_vertices' valid to build a rectangle ? 00186 //--------------------------------------------------------------------------- 00187 virtual bool valid_vertices(const vcl_vector<vsol_point_2d_sptr> new_vertices) const; 00188 00189 //--------------------------------------------------------------------------- 00190 //: output description to stream 00191 //--------------------------------------------------------------------------- 00192 void describe(vcl_ostream &strm, int blanking=0) const; 00193 }; 00194 00195 00196 //: Binary save vsol_rectangle_2d* to stream. 00197 void vsl_b_write(vsl_b_ostream &os, const vsol_rectangle_2d* r); 00198 00199 //: Binary load vsol_rectangle_2d* from stream. 00200 void vsl_b_read(vsl_b_istream &is, vsol_rectangle_2d* &r); 00201 00202 #endif // vsol_rectangle_2d_h_