vtf-logo

Line_2.h

00001 // -*- C++ -*-
00002 
00003 #if !defined(__geom_Line_2_h__)
00004 #define __geom_Line_2_h__
00005 
00006 // If we are debugging the whole geom namespace.
00007 #if defined(DEBUG_geom) && !defined(DEBUG_Line_2)
00008 #define DEBUG_Line_2
00009 #endif
00010 
00011 #include <iostream>
00012 #include <cassert>
00013 #include <cmath>
00014 
00015 #include "SegmentMath.h"
00016 
00017 BEGIN_NAMESPACE_GEOM
00018 
00020 
00023 template<typename T = double>
00024 class Line_2 {
00025 public:
00026 
00027   //
00028   // Public types.
00029   //
00030 
00032   typedef T Number;
00034   typedef SegmentMath<2,T> Segment;
00036   typedef typename Segment::Point Point;
00037 
00038 private:
00039 
00040   //
00041   // Member data
00042   //
00043 
00044   Segment _segment;
00045   Point _normal;
00046 
00047 public:
00048 
00049   //--------------------------------------------------------------------------
00051   // @{
00052 
00054   Line_2()
00055   {}
00056 
00058   Line_2(const Point& source, const Point& target) : 
00059     _segment(source, target),
00060     _normal(_segment.getTangent()[1], - _segment.getTangent()[0])
00061   {}
00062 
00064   void
00065   make(const Point& source, const Point& target) {
00066     _segment.make(source, target);
00067     _normal[0] = _segment.getTangent()[1];
00068     _normal[1] = - _segment.getTangent()[0];
00069   }
00070 
00072   Line_2(const Segment& segment) : 
00073     _segment(segment),
00074     _normal(_segment.getTangent()[1], - _segment.getTangent()[0])
00075   {}
00076 
00078   Line_2(const geom::Segment<2,Number>& segment) : 
00079     _segment(segment),
00080     _normal(_segment.getTangent()[1], - _segment.getTangent()[0])
00081   {}
00082 
00084   Line_2(const Line_2& other) : 
00085     _segment(other._segment),
00086     _normal(other._normal)
00087   {}
00088 
00090   Line_2& 
00091   operator=(const Line_2& other);
00092 
00094   ~Line_2()
00095   {}
00096 
00097   // @}
00098   //--------------------------------------------------------------------------
00100   // @{
00101 
00103   const Point& 
00104   getPointOn() const {
00105     return _segment.getSource();
00106   }
00107 
00109   const Point& 
00110   getTangent() const {
00111     return _segment.getTangent();
00112   }
00113 
00115   const Point& 
00116   getNormal() const {
00117     return _normal;
00118   }
00119         
00121   const Segment& 
00122   getSegment() const {
00123     return _segment;
00124   }
00125 
00126   // @}
00127   //--------------------------------------------------------------------------
00129   // @{
00130 
00131   // CONTINUE
00132 #if 0
00134   Segment& 
00135   segment() {
00136     return _segment;
00137   }
00138 #endif
00139 
00140   // @}
00141   //--------------------------------------------------------------------------
00143   // @{
00144 
00146   Line_2& 
00147   operator+=(const Point& p) {
00148     _segment += p;
00149     return *this;
00150   }
00151 
00153   Line_2& 
00154   operator-=(const Point& p) {
00155     _segment -= p;
00156     return *this;
00157   }
00158 
00160   Number 
00161   computeSignedDistance(const Point& p) const {
00162     return computeDotProduct(p - getPointOn(), getNormal());
00163   }
00164 
00166   Number 
00167   computeSignedDistanceAndClosestPoint(const Point& p, Point* cp) const
00168   {
00169     *cp = getPointOn() + computeDotProduct(p - getPointOn(), 
00170                                            getTangent()) * getTangent();
00171     return computeDotProduct(p - getPointOn(), getNormal());
00172   }
00173 
00175   void 
00176   computeIntersection(Point p1, Point p2, Point* intersectionPoint) const;
00177 
00178   // @}    
00179 };
00180 
00181 
00182 //
00183 // Mathematical Functions.
00184 //
00185 
00186 
00188 
00189 template<typename T>
00190 inline
00191 const Line_2<T>& 
00192 operator+(const Line_2<T>& x) {
00193   return x;
00194 }
00195 
00196 
00198 
00199 template<typename T>
00200 inline
00201 Line_2<T>
00202 operator-(const Line_2<T>& x) {
00203   return Line_2<T>(- x.getSegment());
00204 }
00205 
00206 
00207 //
00208 // File I/O.
00209 //
00210 
00211 
00213 
00214 template<typename T>
00215 inline
00216 std::istream& 
00217 operator>>(std::istream& in, Line_2<T>& x) {
00218   typename Line_2<T>::Point p, q;
00219   in >> p >> q;
00220   x = Line_2<T>(p, q);
00221   return in;
00222 }
00223 
00224 
00226 
00227 template<typename T>
00228 inline
00229 std::ostream& 
00230 operator<<(std::ostream& out, const Line_2<T>& x) {
00231   out << x.getSegment() << x.getNormal() << '\n';
00232   return out;
00233 }
00234 
00235 
00236 //
00237 // Equality Operators.
00238 //
00239 
00240 
00242 
00243 template<typename T>
00244 inline
00245 bool
00246 operator==(const Line_2<T>& a, const Line_2<T>& b) {
00247   return a.getSegment() == b.getSegment();
00248 }
00249 
00250 
00252 
00253 template<typename T>
00254 inline
00255 bool
00256 operator!=(const Line_2<T>& a, const Line_2<T>& b) {
00257   return !(a == b);
00258 }
00259 
00260 
00261 END_NAMESPACE_GEOM
00262 
00263 #define __geom_Line_2_ipp__
00264 #include "Line_2.ipp"
00265 #undef __geom_Line_2_ipp__
00266 
00267 #endif

Generated on Fri Aug 24 12:55:55 2007 for Computational Geometry Package by  doxygen 1.4.7