BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Kernel_d/Segment_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/Segment_d.h $
00019 // $Id: Segment_d.h 42932 2008-04-17 10:13:31Z spion $
00020 // 
00021 //
00022 // Author(s)     : Michael Seel
00023 
00024 #ifndef CGAL_SEGMENT_D_H
00025 #define CGAL_SEGMENT_D_H 
00026 
00027 #include <CGAL/Kernel_d/Pair_d.h> 
00028 #include <CGAL/Kernel_d/Line_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> std::istream& operator>>
00035   (std::istream&, Segment_d<R>&);
00036 template <class R> std::ostream& operator<<
00037   (std::ostream&, const Segment_d<R>&);
00038 
00039 
00040 /*{\Manpage {Segment_d}{R}{Segments in d-space}{s}}*/
00041 
00042 template <class p_R>
00043 class Segment_d : public Handle_for< Pair_d<p_R> > {
00044   
00045   typedef Pair_d<p_R>      Pair;
00046   typedef Handle_for<Pair> Base;
00047   typedef Segment_d<p_R>   Self;
00048 
00049   using Base::ptr;
00050 
00051 /*{\Mdefinition 
00052 An instance $s$ of the data type |Segment_d| is a directed straight
00053 line segment in $d$-dimensional Euclidian space connecting two points
00054 $p$ and $q$. $p$ is called the source point and $q$ is called
00055 the target point of $s$, both points are called endpoints of $s$. A
00056 segment whose endpoints are equal is called \emph{degenerate}.}*/
00057 public: 
00058 
00059   typedef CGAL::Dynamic_dimension_tag Ambient_dimension;
00060   typedef CGAL::Dimension_tag<1>      Feature_dimension;
00061 
00062 /*{\Mtypes 5}*/
00063 typedef p_R R;
00064 /*{\Mtypemember the representation type.}*/
00065 typedef typename p_R::RT RT;
00066 /*{\Mtypemember the ring type.}*/
00067 typedef typename p_R::FT FT;
00068 /*{\Mtypemember the field type.}*/
00069 typedef typename p_R::LA LA;
00070 /*{\Mtypemember the linear algebra layer.}*/
00071 
00072 friend class Line_d<R>; 
00073 friend class Ray_d<R>; 
00074 
00075 private:
00076 Segment_d(const Base& b) : Base(b) {}
00077 public: 
00078 /*{\Mcreation 3}*/
00079 
00080 Segment_d() : Base( Pair() ) {}
00081 /*{\Mcreate introduces a variable |\Mvar| of type |\Mname|.}*/
00082 
00083 Segment_d(const Point_d<R>& p, const Point_d<R>& q) 
00084 /*{\Mcreate introduces a variable |\Mvar| of type |\Mname| which
00085 is initialized to the segment $(p,q)$. }*/
00086  : Base( Pair(p,q) ) {}
00087 
00088 Segment_d(const Point_d<R>& p, const Vector_d<R>& v)
00089 /*{\Mcreate introduces a variable |\Mvar| of type |\Mname| which 
00090 is initialized to the segment |(p,p+v)|. }*/
00091  : Base( Pair(p,p+v) ) {}
00092 
00093 Segment_d(const Segment_d<R>& s) : Base(s) {}     
00094 
00095 /*{\Moperations 3 3}*/
00096 
00097 int dimension() const { return (ptr()->_p[0].dimension()); }
00098 /*{\Mop returns the dimension of the underlying space. }*/
00099 
00100 Point_d<R>  source() const { return ptr()->_p[0]; }
00101 /*{\Mop    returns the source point of segment |\Mvar|. }*/ 
00102 
00103 Point_d<R>  target() const { return ptr()->_p[1]; } 
00104 /*{\Mop    returns the target point of segment |\Mvar|. }*/ 
00105 
00106 // defined for historical reasons
00107 Point_d<R>  vertex(int i) const 
00108 /*{\Mop returns source or target of |\Mvar|: |vertex(0)| returns the
00109 source, |vertex(1)| returns the target. The parameter $i$ is taken
00110 modulo $2$, which gives easy access to the other vertex.
00111 \precond $i \geq 0$.}*/
00112 { CGAL_assertion(i>=0); return ptr()->_p[i%2]; }
00113 
00114 Point_d<R>  point(int i) const { return vertex(i); }
00115 /*{\Mop returns |vertex(i)|.}*/ 
00116 
00117 Point_d<R>  operator[](int i) const { return vertex(i); }
00118 /*{\Marrop returns |vertex(i)|.}*/
00119 
00120 Point_d<R> min BOOST_PREVENT_MACRO_SUBSTITUTION () const 
00121 /*{\Mop returns the lexicographically smaller vertex.}*/
00122 { typename R::Compare_lexicographically_d cmp; 
00123   if (cmp(source(),target()) == CGAL::SMALLER) return source();
00124   else return target();
00125 }
00126 
00127 Point_d<R> max BOOST_PREVENT_MACRO_SUBSTITUTION () const 
00128 /*{\Mop returns the lexicographically larger vertex.}*/
00129 { typename R::Compare_lexicographically_d cmp; 
00130   if (cmp(source(),target()) == SMALLER) return target();
00131   else return source();
00132 }
00133 
00134 Segment_d<R> opposite() const 
00135 /*{\Mop returns the segment |(target(),source())|.}*/
00136 { return Segment_d<R>(target(),source()); }
00137 
00138 
00139 Direction_d<R> direction() const
00140 /*{\Mop returns the direction from source to target.\\
00141 \precond |\Mvar| is non-degenerate. }*/
00142 { CGAL_assertion_msg((!is_degenerate()), 
00143   "Segment_d::direction(): degenerate segment cannot be converted.");
00144   return ptr()->direction(); 
00145 }
00146 
00147 Vector_d<R> vector() const
00148 /*{\Mop returns the vector from source to target.}*/
00149 { return ptr()->vector(); }
00150 
00151 FT squared_length() const
00152 /*{\Mop returns the square of the length of |\Mvar|.}*/
00153 { return (target()-source()).squared_length(); } 
00154 
00155 bool has_on(const Point_d<R>& p) const 
00156 /*{\Mop returns true if $p$ lies on |\Mvar| and false otherwise. }*/
00157 { if (is_degenerate()) return (source() == p);
00158   typename R::Position_on_line_d pos; FT l;
00159   if ( pos(p,source(),target(),l) ) return (FT(0)<=l && l<=FT(1));
00160   return false;
00161 }
00162 
00163 inline Line_d<R> supporting_line() const;
00164 /*{\Mop returns the supporting line of |\Mvar|.
00165 \precond |\Mvar| is non-degenerate. }*/ 
00166 
00167 Segment_d<R>   
00168 transform(const Aff_transformation_d<R>& t) const
00169 /*{\Mop returns $t(s)$.}*/ 
00170 { return Segment_d<R>(source().transform(t),
00171                       target().transform(t)); }
00172 
00173 Segment_d<R> operator+(const Vector_d<R>& v) const
00174 /*{\Mbinop returns $s+v$, i.e., |\Mvar| translated by vector $v$.}*/ 
00175 { return Segment_d<R>(source()+v,target()+v); }
00176 
00177 bool is_degenerate() const 
00178 /*{\Mop returns true if |\Mvar| is degenerate i.e. 
00179 |\Mvar.source()=\Mvar.target()|. }*/
00180 { return ptr()->is_degenerate(); } 
00181 
00182 bool operator==(const Segment_d<R>& t) const
00183 { if (this->identical(t)) return true;
00184   return ((source() == t.source() && 
00185            target() == t.target())); } 
00186 
00187 bool operator!=(const Segment_d<R>& t) const 
00188 { return !operator==(t); }
00189 
00190 friend std::istream& operator>> <> 
00191 (std::istream&, Segment_d<R>&);
00192 friend std::ostream& operator<< <> 
00193 (std::ostream&, const Segment_d<R>&);
00194 
00195 };  // end of class
00196 
00197 /*{\Mtext \headerline{Non-Member Functions} }*/
00198 
00199 template <class R>
00200 bool weak_equality(const Segment_d<R>& s, const Segment_d<R>& t)
00201 /*{\Mfunc Test for equality as unoriented segments.}*/
00202 { return (s==t || s==t.opposite()); } 
00203 
00204 template <class R>
00205 bool parallel(const Segment_d<R>& s1, const Segment_d<R>& s2)
00206 /*{\Mfunc return true if one of the segments is trivial or
00207 if the unoriented supporting lines are parallel. }*/
00208 { return s1.is_degenerate() || s2.is_degenerate() ||
00209          s1.direction() == s2.direction() || 
00210          s1.direction() == -s2.direction(); } 
00211 
00212 template <class R>
00213 bool common_endpoint(const Segment_d<R>& s1, const Segment_d<R>& s2,
00214   Point_d<R>& common) 
00215 /*{\Mfunc if |s1| and |s2| touch in a common end point, this point
00216 is assigned to |common| and the result is |true|, otherwise the 
00217 result is |false|. }*/
00218 { if (s1.source() == s2.source()) { common = s1.source(); return true; }
00219   if (s1.source() == s2.target()) { common = s1.source(); return true; }
00220   if (s1.target() == s2.source()) { common = s1.target(); return true; }
00221   if (s1.target() == s2.target()) { common = s1.target(); return true; }
00222   return false; 
00223 }
00224 
00225 template <class R> 
00226 std::istream& operator>>(std::istream& I, Segment_d<R>& s)
00227 { s.copy_on_write(); s.ptr()->read(I); 
00228   CGAL_assertion_msg(s.source().dimension()==s.target().dimension(),
00229   "Segment_d::operator>>: dimensions disagree.");
00230   return I; 
00231 }
00232 
00233 template <class R> 
00234 std::ostream& operator<<(std::ostream& O, const Segment_d<R>& s)
00235 { s.ptr()->print(O,"Segment_d"); return O; }
00236 
00237 /*{\Mimplementation 
00238 Segments are implemented by a pair of points as an item type.  All
00239 operations like creation, initialization, tests, the calculation of
00240 the direction and source - target vector, input and output on a
00241 segment $s$ take time $O(|s.dimension()|)$. |dimension()|, coordinate
00242 and end point access, and identity test take constant time.  The
00243 operations for intersection calculation also take time
00244 $O(|s.dimension()|)$. The space requirement is $O(|s.dimension()|)$.
00245 }*/
00246 
00247 
00248 CGAL_END_NAMESPACE
00249 #endif // CGAL_SEGMENT_D_H
00250 //----------------------- end of file ----------------------------------
00251 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines