00001 /*!@file MBARI/Geometry2D.H - classes for geometry in the plane 00002 */ 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2002 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Dirk Walther <walther@caltech.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/MBARI/Geometry2D.H $ 00035 // $Id: Geometry2D.H 9422 2008-03-11 07:33:57Z rjpeters $ 00036 // 00037 00038 #ifndef GEOMETRY2D_H_DEFINED 00039 #define GEOMETRY2D_H_DEFINED 00040 00041 #include "Image/Point2D.H" 00042 #include "Util/Types.H" 00043 #include "Util/log.H" 00044 00045 #include <iosfwd> 00046 00047 // ###################################################################### 00048 //! A vector in the 2D plane 00049 class Vector2D 00050 { 00051 public: 00052 //! default constructor, constructs an invalid Vector2D 00053 Vector2D(); 00054 00055 //! construct a Vector2D with coordinates x and y 00056 Vector2D(float x, float y); 00057 00058 //! construct a Vector2D from a Point2D<int> 00059 Vector2D(const Point2D<int>& point); 00060 00061 00062 //! construct a Vector2D from a Point2D<double> 00063 Vector2D(const Point2D<double>& point); 00064 00065 //! Construct from the data contained in the input stream is 00066 Vector2D(std::istream& is); 00067 00068 // default copy constructor, assignment operator and destructor okay 00069 00070 //! reset the entries of the vector 00071 void reset(float x, float y); 00072 00073 //! reset the entries of the vector from a Point2D<int> 00074 void reset(const Point2D<int>& point); 00075 00076 //! write the entire Vector2D to the output stream os 00077 void writeToStream(std::ostream& os) const; 00078 00079 //! read the Vector2D from the input stream is 00080 void readFromStream(std::istream& is); 00081 00082 //! access the x coordinate 00083 float x() const; 00084 00085 //! access the y coordinate 00086 float y() const; 00087 00088 //! return a Point2D<int> (rounding the coordinates to int) 00089 Point2D<int> getPoint2D() const; 00090 00091 //! compute the dot product of this with other 00092 float dotProduct(const Vector2D& other) const; 00093 00094 00095 //! compute the cross product of this with other 00096 float crossProduct(const Vector2D& other) const; 00097 00098 //! compute the length of the vector 00099 float length() const; 00100 00101 //! normalize the vector to length 1 and return its old length 00102 float normalize(); 00103 00104 //! compute the Euclildean distance of this from other 00105 float distance(const Vector2D& other) const; 00106 00107 //! compute the angle (in degrees) between this and other 00108 float angle(const Vector2D& other) const; 00109 00110 //! determine whether this and other are collinear 00111 bool isCollinear(const Vector2D& other) const; 00112 00113 //! determine whether this and and other are orthogonal 00114 bool isOrthogonal(const Vector2D& other) const; 00115 00116 //! whether this vector is zero 00117 bool isZero() const; 00118 00119 //! whether this vector is valid 00120 bool isValid() const; 00121 00122 //! dot product 00123 float operator*(const Vector2D& v) const; 00124 00125 //! component-based addition 00126 Vector2D operator+(const Vector2D& v) const; 00127 //! component-based subtraction 00128 Vector2D operator-(const Vector2D& v) const; 00129 00130 //! component-based addition 00131 Vector2D& operator+=(const Vector2D& v); 00132 //conponent-based subtraction 00133 Vector2D& operator-=(const Vector2D& v); 00134 00135 //! scalar addition 00136 Vector2D operator+(const float f) const; 00137 //! scalar subtraction 00138 Vector2D operator-(const float f) const; 00139 //! scalar multiplication 00140 Vector2D operator*(const float f) const; 00141 //! scalar division 00142 Vector2D operator/(const float f) const; 00143 00144 //! scalar addition 00145 Vector2D& operator+=(const float f); 00146 //! scalar subtraction 00147 Vector2D& operator-=(const float f); 00148 //! scalar multiplication 00149 Vector2D& operator*=(const float f); 00150 //! scalar division 00151 Vector2D& operator/=(const float f); 00152 00153 private: 00154 float itsX, itsY; 00155 bool valid; 00156 }; 00157 00158 //! == operator 00159 bool operator==(const Vector2D& v1, const Vector2D& v2); 00160 //! != operator 00161 bool operator!=(const Vector2D& v1, const Vector2D& v2); 00162 00163 // ###################################################################### 00164 //! A straight line in the 2D plane 00165 class StraightLine2D 00166 { 00167 public: 00168 //! default constructo, constructs an invalid object 00169 StraightLine2D(); 00170 00171 //! construct from two Vector2D 00172 /*!@param point vector determining a point on the line 00173 @param direction vector determing the direction if the line*/ 00174 StraightLine2D(const Vector2D& point, const Vector2D& direction); 00175 00176 //! Construct from the data contained in the input stream is 00177 StraightLine2D(std::istream& is); 00178 00179 // default copy constructor, assignment operator and destructor okay 00180 00181 //! reset the straight line 00182 /*!@param point vector determining a point on the line 00183 @param direction vector determing the direction if the line*/ 00184 void reset(const Vector2D& point, const Vector2D& direction); 00185 00186 //! write the entire StraightLine2D to the output stream os 00187 void writeToStream(std::ostream& os) const; 00188 00189 //! read the StraightLine2D from the input stream is 00190 void readFromStream(std::istream& is); 00191 00192 //! get a point on the line 00193 /*!@param n get point point + n * direction; for n = 0 (default), 00194 this is the point entered in the constructor or in reset*/ 00195 Vector2D point(float n = 0.0F) const; 00196 00197 //! returns the direction vector for this straight line 00198 Vector2D direction() const; 00199 00200 //! returns a vector to the intersection point of this and other 00201 /*! if the lines do not intersect (i.e. they are parallel), an 00202 invalid Vector2D is returned. 00203 @param n in this variable, the scalar is returned, for which the 00204 intersection point is this->point(n); 00205 @param m the scalar, for which the interesection point is 00206 other.point(m)*/ 00207 Vector2D intersect(const StraightLine2D& other, float&n, float&m) const; 00208 00209 //! whether this and other are parallel 00210 bool isParallel(const StraightLine2D& other) const; 00211 00212 //! whether this and other are orthogonal 00213 bool isOrthogonal(const StraightLine2D& other) const; 00214 00215 //! whether pt is on the straight line 00216 bool isPointOnLine(const Vector2D& pt) const; 00217 00218 //! whether this and other describe identical straight lines 00219 bool isIdentical(const StraightLine2D& other) const; 00220 00221 //! whether this object is valid 00222 bool isValid() const; 00223 00224 private: 00225 Vector2D itsPoint, itsDir; 00226 bool valid; 00227 }; 00228 00229 00230 00231 // ###################################################################### 00232 //! A segment of a straight line in the 2D plane 00233 class LineSegment2D 00234 { 00235 public: 00236 00237 00238 //! default constructor, constructs an invalid object 00239 LineSegment2D(); 00240 00241 //! construct from two Point2D<int> 00242 /*!@param p1 an endpoint on the line 00243 @param p2 the second endpoint of the line line*/ 00244 LineSegment2D(const Point2D<int>& p1, const Point2D<int>& p2); 00245 00246 //! Construct from the data contained in the input stream is 00247 LineSegment2D(std::istream& is); 00248 00249 // default copy constructor, assignment operator and destructor okay 00250 00251 //! reset the straight line 00252 /*!@param point vector determining a point on the line 00253 @param direction vector determing the direction if the line*/ 00254 void reset(const Point2D<int>& point, const Point2D<int>& direction); 00255 00256 //! write the entire StraightLine2D to the output stream os 00257 void writeToStream(std::ostream& os) const; 00258 00259 //! read the StraightLine2D from the input stream is 00260 void readFromStream(std::istream& is); 00261 00262 //! get a point on the line 00263 /*!@param n get point point + n * direction; for n = 0 (default), 00264 this is the point entered in the constructor or in reset*/ 00265 Point2D<int> point1() const; 00266 00267 //! returns the direction vector for this straight line 00268 Point2D<int> point2() const; 00269 00270 //!returns the angle of the line 00271 double angle() const; 00272 00273 double angleBetween(LineSegment2D &line) const; 00274 00275 //! returns true if the two line segments intersect. 00276 //! x and y are set to be the point of intersection of the lines, 00277 //! whether or not the segments themselves intersect. 00278 bool intersects(LineSegment2D &line, double &xcoord, double &ycoord) const; 00279 00280 double distance(Point2D<double> point) const; 00281 00282 double distance(Point2D<int> point) const { return distance(Point2D<double>(point)); } 00283 00284 float length() const; 00285 00286 //! whether this object is valid 00287 bool isValid() const; 00288 00289 00290 private: 00291 Point2D<int> itsPoint1; 00292 Point2D<int> itsPoint2; 00293 bool valid; 00294 }; 00295 00296 #endif 00297 00298 // ###################################################################### 00299 /* So things look consistent in everyone's emacs... */ 00300 /* Local Variables: */ 00301 /* indent-tabs-mode: nil */ 00302 /* End: */