BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Kernel_d/Line_d.h
Go to the documentation of this file.
00001 // Copyright (c) 2000,2001  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Kernel_d/include/CGAL/Kernel_d/Line_d.h $
00019 // $Id: Line_d.h 42932 2008-04-17 10:13:31Z spion $
00020 // 
00021 //
00022 // Author(s)     : Michael Seel
00023 
00024 #ifndef CGAL_LINE_D_H
00025 #define CGAL_LINE_D_H
00026 
00027 #include <CGAL/Kernel_d/Pair_d.h> 
00028 #include <CGAL/Kernel_d/Segment_d.h> 
00029 #include <CGAL/Kernel_d/Ray_d.h>
00030 #include <CGAL/Dimension.h>
00031 
00032 CGAL_BEGIN_NAMESPACE
00033 
00034 template <class R>
00035 std::istream& operator>>(std::istream&, Line_d<R>&);
00036 template <class R>
00037 std::ostream& operator<<(std::ostream&, const Line_d<R>&);
00038 
00039 /*{\Manpage {Line_d}{R}{Lines in d-space}{l}}*/
00040 
00041 template <class p_R>
00042 class Line_d : public Handle_for< Pair_d<p_R> > { 
00043   typedef Pair_d<p_R>      Pair;
00044   typedef Handle_for<Pair> Base;
00045   typedef Line_d<p_R>      Self;
00046 
00047   using Base::ptr;
00048 
00049 /*{\Mdefinition 
00050 An instance of data type |Line_d| is an oriented line in
00051 $d$-dimensional Euclidian space.}*/
00052 
00053 public: 
00054 
00055   typedef CGAL::Dynamic_dimension_tag Ambient_dimension;
00056   typedef CGAL::Dimension_tag<1>      Feature_dimension;
00057 
00058 /*{\Mtypes 5}*/
00059 typedef p_R R;
00060 /*{\Mtypemember the representation type.}*/
00061 typedef typename p_R::RT RT;
00062 /*{\Mtypemember the ring type.}*/
00063 typedef typename p_R::FT FT;
00064 /*{\Mtypemember the field type.}*/
00065 typedef typename p_R::LA LA;
00066 /*{\Mtypemember the linear algebra layer.}*/
00067 
00068 typedef typename Vector_d<R>::Base_vector Base_vector;
00069 
00070 friend class Ray_d<R>; 
00071 friend class Segment_d<R>; 
00072 
00073 private:
00074 Line_d(const Base& b) : Base(b) {}
00075 public: 
00076 /*{\Mcreation 3}*/
00077 
00078 Line_d() : Base( Pair() ) {}
00079 /*{\Mcreate introduces a variable |\Mvar| of type |\Mname| and 
00080 initializes it to some line in $d$ - dimensional space }*/
00081 
00082 Line_d(const Point_d<R>& p, const Point_d<R>& q)
00083 /*{\Mcreate introduces a line through |p| and |q| and oriented
00084 from |p| to |q|. \precond $p$ and $q$ are distinct and have the same 
00085 dimension.}*/
00086  : Base( Pair(p,q) )
00087 { CGAL_assertion_msg(!ptr()->is_degenerate(), 
00088     "Line_d::constructor: the two points must be different." );
00089   CGAL_assertion_msg((p.dimension()==q.dimension()), 
00090     "Line_d::constructor: the two points must have the same dimension." );
00091 }
00092 
00093 Line_d(const Point_d<R>& p, const Direction_d<R>& dir)
00094 /*{\Mcreate introduces a line through |p| with direction |dir|.
00095 \precond |p| and |dir| have the same dimension, |dir| is not trivial. }*/
00096   : Base( Pair(p,p+dir.vector()) )
00097 { CGAL_assertion_msg((p.dimension()==dir.dimension()), 
00098     "Line_d::constructor: the p and dir must have the same dimension." );
00099   CGAL_assertion_msg(!dir.is_degenerate(), 
00100     "Line_d::constructor: dir must be non-degenerate." );
00101 }
00102 
00103 Line_d(const Segment_d<R>& s) 
00104 /*{\Mcreate introduces a variable |\Mvar| of type |\Mname| and 
00105 initializes it to the line through |s.source()| and |s.target()|
00106 with direction from |s.source()| to |s.target()|.
00107 \precond $s$ is not degenerate. }*/
00108   : Base( s ) 
00109 { CGAL_assertion_msg((!s.is_degenerate()), 
00110     "Line_d::constructor: segment is trivial.");
00111 }
00112 
00113 Line_d(const Ray_d<R>& r) : Base(r) {}
00114 /*{\Mcreate introduces a variable |\Mvar| of type |\Mname| and 
00115 initializes it to the line through |r.point(1)| and |r.point(2)|. }*/
00116 
00117 Line_d(const Line_d<R>& l) : Base(l) {}
00118 
00119 /*{\Moperations 3 3}*/
00120 
00121 int dimension() const { return (ptr()->_p[0].dimension()); }
00122 /*{\Mop returns the dimension of the underlying space.}*/
00123 
00124 Point_d<R> point(int i) const 
00125 /*{\Mop returns an arbitrary point on |l|.  It holds that |point(i) ==
00126 point(j)|, iff |i==j|. Furthermore, |l| is directed from |point(i)| to
00127 |point(j)|, for all |i < j|.}*/
00128 { return (ptr()->_p[i%2]); }
00129 
00130 Line_d<R> opposite() const 
00131 /*{\Mop returns the line |(point(2),point(1))|. }*/
00132 { return Line_d<R>(point(1),point(0)); }
00133 
00134 Direction_d<R> direction() const 
00135 /*{\Mop  returns the direction of |\Mvar|. }*/
00136 { return ptr()->direction(); }
00137 
00138 Line_d<R> transform(const Aff_transformation_d<R> & t) const
00139 /*{\Mop returns $t(l)$. }*/
00140 { return Line_d<R>(point(0).transform(t),point(1).transform(t)); }
00141 
00142 Line_d<R> operator+(const Vector_d<R>& v) const
00143 /*{\Mbinop returns |\Mvar+v|, i.e., |\Mvar| translated by vector $v$.}*/ 
00144 { return Line_d<R>(point(0)+v,point(1)+v); }
00145 
00146 Point_d<R> projection(const Point_d<R>& p) const
00147 /*{\Mop returns the point of intersection of |\Mvar| with the hyperplane 
00148 that is orthogonal to |\Mvar| through |p|. }*/
00149 { Vector_d<R> v = direction().vector();
00150   Point_d<R> q = point(0);
00151   FT lambda = ((p-q) * v) / (v*v);
00152   Point_d<R> res = q + lambda * v;
00153   return res;
00154 }
00155 
00156 bool has_on(const Point_d<R>& p) const 
00157 /*{\Mopl returns true if $p$ lies on |\Mvar| and false otherwise. }*/
00158 { typename R::Position_on_line_d pos; FT dummy;
00159   return pos(p,point(0),point(1),dummy); }
00160 
00161 bool operator==(const Line_d<R>& l1) const
00162 { if ( this->identical(l1) ) return true;
00163   if ( dimension() != l1.dimension() ) return false;
00164   return has_on(l1.point(0)) && 
00165          direction() == l1.direction(); 
00166 }
00167 
00168 bool operator!=(const Line_d<R>& l1) const
00169 { return !operator==(l1); }
00170 
00171 friend std::istream& operator>> <> 
00172 (std::istream&, Line_d<R>&);
00173 friend std::ostream& operator<< <> 
00174 (std::ostream&, const Line_d<R>&);
00175 
00176 }; // end of class
00177 
00178 /*{\Mtext \headerline{Non-Member Functions} }*/
00179 
00180 template <class R>
00181 bool weak_equality(const Line_d<R>& l1, const Line_d<R>& l2)
00182 /*{\Mfunc Test for equality as unoriented lines.}*/
00183 { if (l1.identical(l2)) return true;
00184   if (l1.dimension()!=l2.dimension()) return false;
00185   return (l1.has_on(l2.point(0)) && 
00186           l1.has_on(l2.point(1))); 
00187 }
00188 
00189 template <class R>
00190 bool parallel(const Line_d<R>& l1, const Line_d<R>& l2)
00191 /*{\Mfunc returns true if |l1| and |l2| are parallel as unoriented lines
00192 and false otherwise. }*/
00193 { return (l1.direction() == l2.direction() ||
00194           l1.direction() == -l2.direction()); }
00195 
00196 template <class R>
00197 std::istream& operator>>(std::istream& I, Line_d<R>& l) 
00198 { l.copy_on_write(); l.ptr()->read(I); 
00199   CGAL_assertion_msg(l.point(0)!=l.point(1),
00200     "Line_d::operator>>: trivial line.");
00201   CGAL_assertion_msg(l.point(0).dimension()==l.point(1).dimension(),
00202     "Line_d::operator>>: dimensions disagree.");
00203   return I; 
00204 }
00205 
00206 template <class R>
00207 std::ostream& operator<<(std::ostream& O, const Line_d<R>& l)
00208 { l.ptr()->print(O,"Line_d"); return O; }
00209 
00210 /*{\Mimplementation 
00211 Lines are implemented by a pair of points as an item type.  All
00212 operations like creation, initialization, tests, direction
00213 calculation, input and output on a line $l$ take time
00214 $O(|l.dimension()|)$. |dimension()|, coordinate and point access, and
00215 identity test take constant time.  The operations for intersection
00216 calculation also take time $O(|l.dimension()|)$. The space requirement
00217 is $O(|l.dimension()|)$.}*/
00218 
00219 
00220 CGAL_END_NAMESPACE
00221 #endif // CGAL_LINE_D_H
00222 //----------------------- end of file ----------------------------------
00223 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines