BWAPI
|
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/Tuple_d.h $ 00019 // $Id: Tuple_d.h 42940 2008-04-17 13:32:52Z spion $ 00020 // 00021 // Author(s) : Michael Seel 00022 00023 #ifndef CGAL_TUPLE_D_H 00024 #define CGAL_TUPLE_D_H 00025 00026 #include <CGAL/basic.h> 00027 #include <CGAL/Handle_for.h> 00028 #include <CGAL/Quotient.h> 00029 #include <CGAL/Kernel_d/Cartesian_const_iterator_d.h> 00030 #include <sstream> 00031 00032 CGAL_BEGIN_NAMESPACE 00033 #define PointCd PointCd2 00034 #define PointHd PointHd2 00035 00036 template <typename NT, typename LA> class PointHd; 00037 template <typename NT, typename LA> class VectorHd; 00038 template <typename NT, typename LA> class DirectionHd; 00039 template <typename NT, typename LA> class HyperplaneHd; 00040 template <typename NT, typename LA> class Aff_transformationHd; 00041 template <typename FT, typename LA> class PointCd; 00042 template <typename NT, typename LA> class VectorCd; 00043 template <typename FT, typename LA> class DirectionCd; 00044 template <typename FT, typename LA> class HyperplaneCd; 00045 template <typename NT, typename LA> class Aff_transformationCd; 00046 00047 00048 class MatchHelper {}; 00049 00050 template <typename NT, typename LA> 00051 class Tuple_d { 00052 typedef Tuple_d<NT,LA> Self; 00053 typedef typename LA::Vector Vector; 00054 Vector v; 00055 public: 00056 typedef typename Vector::const_iterator const_iterator; 00057 00058 typedef Cartesian_const_iterator_d<const_iterator> Cartesian_const_iterator; 00059 00060 struct Homogeneous_const_iterator { 00061 typedef Homogeneous_const_iterator self; 00062 typedef std::random_access_iterator_tag iterator_category; 00063 typedef NT value_type; 00064 typedef std::ptrdiff_t difference_type; 00065 typedef const value_type* pointer; 00066 typedef const value_type& reference; 00067 00068 Homogeneous_const_iterator() : _it(0), _w(0) {} 00069 Homogeneous_const_iterator(const_iterator it, const_iterator w = 0) 00070 : _it(it), _w(w) {} 00071 00072 value_type operator*() const 00073 { if (_it == _w) return value_type(1); else return *_it; } 00074 00075 self& operator++() { ++_it; return *this; } 00076 self operator++(int) { self tmp = *this; ++_it; return tmp; } 00077 self& operator--() { --_it; return *this; } 00078 self operator--(int) { self tmp = *this; --_it; return tmp; } 00079 00080 self& operator+=(difference_type i) { _it+=i; return *this; } 00081 self& operator-=(difference_type i) { _it-=i; return *this; } 00082 self operator+(difference_type i) const 00083 { self tmp=*this; return tmp += i; } 00084 self operator-(difference_type i) const 00085 { self tmp=*this; return tmp -= i; } 00086 00087 difference_type operator-(self x) const { return _it-x._it; } 00088 value_type operator[](difference_type i) const { return *(*this + i); } 00089 00090 bool operator==(const self& x) const { return _it==x._it; } 00091 bool operator!=(const self& x) const { return ! (*this==x); } 00092 bool operator<(self x) const { return (x - *this) > 0; } 00093 00094 private: 00095 const_iterator _it, _w; 00096 }; // Homogeneous_const_iterator 00097 00098 00099 Tuple_d(int d) : v(d) {} 00100 Tuple_d(const NT& a, const NT& b) : v(2) 00101 { v[0]=a; v[1]=b; } 00102 Tuple_d(const NT& a, const NT& b, const NT& c, const MatchHelper&) : v(3) 00103 { v[0]=a; v[1]=b; v[2]=c; } 00104 Tuple_d(const NT& a, const NT& b, const NT& c, const NT& d) : v(4) 00105 { v[0]=a; v[1]=b; v[2]=c; v[3]=d; } 00106 00107 template <typename I> 00108 Tuple_d(int d, I& start, I end) : v(d) 00109 { int i(0); 00110 while ( i < d && start != end ) v[i++] = *start++; 00111 } 00112 /* this constructor returns the final position of start 00113 to offer access to a possible common denominator as 00114 part of the tuple range */ 00115 00116 template <typename I> 00117 Tuple_d(int d, I start, I end, NT D) : v(d) 00118 { int i(0); 00119 while ( i < d && start != end ) v[i++] = *start++; 00120 v[d-1] = D; 00121 } 00122 00123 int size() const { return v.dimension(); } 00124 const_iterator begin() const { return v.begin(); } 00125 const_iterator last() const { return v.end()-1; } 00126 const_iterator end() const { return v.end(); } 00127 const_iterator beyondend() const { return v.end()+1; } 00128 00129 00130 void invert() 00131 { for (int i=0; i<size(); ++i) v[i]=-v[i]; } 00132 void invert(int d) 00133 { for (int i=0; i<d; ++i) v[i]=-v[i]; } 00134 00135 void print(std::ostream& out, const char*) const; 00136 void read(std::istream& in); 00137 void homogeneous_add(const Self* a, const Self* b) 00138 { int d = a->size()-1; 00139 if ( d < 0 ) return; 00140 CGAL_assertion_msg((d == b->size()-1),"dimensions disagree."); 00141 CGAL_assertion_msg((d == size()-1),"dimensions disagree."); 00142 NT aw = a->v[d], bw = b->v[d]; 00143 for (int i = 0; i < d; ++i) { 00144 v[i] = a->v[i]*bw + b->v[i]*aw; 00145 } 00146 v[d] = aw*bw; 00147 } 00148 00149 void homogeneous_sub(const Self* a, const Self* b) 00150 { int d = a->size()-1; 00151 if ( d < 0 ) return; 00152 CGAL_assertion_msg((d == b->size()-1),"dimensions disagree."); 00153 CGAL_assertion_msg((d == size()-1),"dimensions disagree."); 00154 NT aw = a->v[d], bw = b->v[d]; 00155 for (int i = 0; i < d; ++i) { 00156 v[i] = a->v[i]*bw - b->v[i]*aw; 00157 } 00158 v[d] = aw*bw; 00159 } 00160 00161 void cartesian_add(const Self* a, const Self* b) 00162 { v = a->v + b->v; } 00163 void cartesian_sub(const Self* a, const Self* b) 00164 { v = a->v - b->v; } 00165 00166 friend class PointHd<NT,LA>; 00167 friend class VectorHd<NT,LA>; 00168 friend class DirectionHd<NT,LA>; 00169 friend class HyperplaneHd<NT,LA>; 00170 friend class PointCd<NT,LA>; 00171 friend class VectorCd<NT,LA>; 00172 friend class DirectionCd<NT,LA>; 00173 friend class HyperplaneCd<NT,LA>; 00174 00175 }; // Tuple_d 00176 00177 00178 template <class NT, class LA> 00179 class Compare_homogeneously 00180 { 00181 public: 00182 Comparison_result operator()( 00183 const typename LA::Vector& v1, const typename LA::Vector& v2) 00184 { 00185 CGAL_assertion_msg((v1.dimension() == v2.dimension()), 00186 "Compare_homogeneously: dimensions disagree."); 00187 NT aw = v1[v1.dimension()-1]; 00188 NT bw = v2[v2.dimension()-1]; 00189 CGAL_assertion(aw>0 && bw>0); 00190 for (int i = 0; i < v1.dimension()-1; i++ ) { 00191 NT aibw = v1[i]*bw; 00192 NT biaw = v2[i]*aw; 00193 Comparison_result S = (aibw<biaw ? SMALLER : 00194 (biaw<aibw ? LARGER : EQUAL)); 00195 if (S != EQUAL) return S; 00196 } 00197 return EQUAL; 00198 } 00199 }; // Compare_homogeneously 00200 00201 template <class NT, class LA> 00202 class Compare_componentwise 00203 { public: 00204 Comparison_result operator()( 00205 const typename LA::Vector& v1, const typename LA::Vector& v2) 00206 { 00207 CGAL_assertion_msg((v1.dimension() == v2.dimension()), 00208 "Compare_coefficientwise: dimensions disagree."); 00209 for (int i = 0; i < v1.dimension(); i++ ) { 00210 Comparison_result S = (v1[i]<v2[i] ? SMALLER : 00211 (v2[i]<v1[i] ? LARGER : EQUAL)); 00212 if (S != EQUAL) return S; 00213 } 00214 return EQUAL; 00215 } 00216 }; // Compare_coefficientwise 00217 00218 00219 template <typename NT, typename LA> 00220 void Tuple_d<NT,LA>::print(std::ostream& os, const char* l) const 00221 { int i; 00222 switch( os.iword(CGAL::IO::mode) ) { 00223 case CGAL::IO::ASCII : 00224 os << size() << " "; 00225 for (i = 0; i < size(); ++i) 00226 os << v[i] << " "; break; 00227 case CGAL::IO::BINARY : 00228 CGAL::write(os, size()); 00229 for (i = 0; i < size(); ++i) 00230 CGAL::write(os, v[i]); break; 00231 default : 00232 os << l << "(" << size() << ", "; 00233 for (i = 0; i < size(); ++i) { 00234 os << v[i]; if (i!=size()-1) os<<", "; else os<<")"; 00235 } 00236 } 00237 } 00238 00239 00240 template <typename NT, typename LA> 00241 void Tuple_d<NT,LA>::read(std::istream& is) 00242 { int i = 0, d; 00243 switch( is.iword(CGAL::IO::mode) ) { 00244 case CGAL::IO::ASCII : 00245 is >> d; v = Vector(d); 00246 while (i < d && is >> v[i] ) ++i; 00247 break; 00248 case CGAL::IO::BINARY : 00249 CGAL::read(is, d); v = Vector(d); 00250 while (i < d) { CGAL::read(is, v[i]); ++i; } break; 00251 default: 00252 CGAL_error_msg("\nStream must be in ascii or binary mode\n"); 00253 } 00254 } 00255 00256 template <class ForwardIterator> 00257 void tuple_dim_check(ForwardIterator first, ForwardIterator last, 00258 const char* file, int line, const char* op) 00259 { if (first==last) return; 00260 int d = first->dimension(); ++first; 00261 for (; first!=last; ++first) 00262 if (first->dimension() != d) { 00263 std::ostringstream os; 00264 os << "Tuple Dimension Error " << 00265 "File " << file << "Line " << line << "Operation " << op << '\0'; 00266 CGAL_error_msg(os.str().c_str()); 00267 } 00268 } 00269 00270 #define TUPLE_DIM_CHECK(i1,i2,op) tuple_dim_check(i1,i2,__FILE__,__LINE__,#op) 00271 00272 template <class InputIterator, class OutputIterator> 00273 int copy_and_count(InputIterator first, InputIterator last, 00274 OutputIterator result) 00275 { int n=0; 00276 while (first != last) { ++n; *result++ = *first++; } 00277 return n; 00278 } 00279 00280 #undef PointCd 00281 #undef PointHd 00282 CGAL_END_NAMESPACE 00283 #endif //CGAL_TUPLE_D_H 00284