00001 //================================================================================================== 00002 // 00003 // File: ttl_util.h 00004 // 00005 // Created: Jan 15 2000 00006 // 00007 // Author: Øyvind Hjelle <oyvind.hjelle@math.sintef.no> 00008 // 00009 // Revision: $Id: ttl_util.h,v 1.2 2006/07/26 12:08:44 oyvindhj Exp $ 00010 // 00011 // Description: 00012 // 00013 //================================================================================================== 00014 // Copyright (C) 2000-2003 SINTEF Applied Mathematics. All rights reserved. 00015 // 00016 // This file may be distributed under the terms of the Q Public License 00017 // as defined by Trolltech AS of Norway and appearing in the file 00018 // LICENSE.QPL included in the packaging of this file. 00019 // 00020 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00021 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00022 // 00023 //================================================================================================== 00024 00025 00026 #ifndef _TTL_UTIL_H_ 00027 #define _TTL_UTIL_H_ 00028 00029 00030 #include <vector> 00031 #include <algorithm> 00032 00033 00034 #ifdef _MSC_VER 00035 # if _MSC_VER < 1300 00036 # include <minmax.h> 00037 # endif 00038 #endif 00039 00040 00041 using namespace std; 00042 00043 00070 namespace ttl_util { 00071 00072 00073 //------------------------------------------------------------------------------------------------ 00074 // ------------------------------ Computational Geometry Group ---------------------------------- 00075 //------------------------------------------------------------------------------------------------ 00076 00079 00080 //------------------------------------------------------------------------------------------------ 00088 template <class real_type> 00089 real_type scalarProduct2d(real_type dx1, real_type dy1, real_type dx2, real_type dy2) { 00090 return dx1*dx2 + dy1*dy2; 00091 } 00092 00093 00094 //------------------------------------------------------------------------------------------------ 00102 template <class real_type> 00103 real_type crossProduct2d(real_type dx1, real_type dy1, real_type dx2, real_type dy2) { 00104 return dx1*dy2 - dy1*dx2; 00105 } 00106 00107 00108 //------------------------------------------------------------------------------------------------ 00118 template <class real_type> 00119 real_type orient2dfast(real_type pa[2], real_type pb[2], real_type pc[2]) { 00120 real_type acx = pa[0] - pc[0]; 00121 real_type bcx = pb[0] - pc[0]; 00122 real_type acy = pa[1] - pc[1]; 00123 real_type bcy = pb[1] - pc[1]; 00124 return acx * bcy - acy * bcx; 00125 } 00126 00127 00128 //------------------------------------------------------------------------------------------------ 00129 /* Scalar product between 2D vectors represented as darts. 00130 * 00131 * \par Requires: 00132 * - real_type DartType::x() 00133 * - real_type DartType::y() 00134 */ 00135 /* 00136 template <class TTLtraits, class DartType> 00137 typename TTLtraits::real_type scalarProduct2d(const DartType& d1, const DartType& d2) { 00138 00139 DartType d10 = d1; 00140 d10.alpha0(); 00141 00142 DartType d20 = d2; 00143 d20.alpha0(); 00144 00145 return scalarProduct2d(d10.x() - d1.x(), d10.y() - d1.y(), d20.x() - d2.x(), d20.y() - d2.y()); 00146 } 00147 */ 00148 00149 00150 //------------------------------------------------------------------------------------------------ 00151 /* Scalar product between 2D vectors. 00152 * The first vector is represented by the given dart, and the second vector has 00153 * direction from the node of the given dart - and to the given point. 00154 * 00155 * \par Requires: 00156 * - real_type DartType::x(), real_type DartType::y() 00157 * - real_type PointType2d::x(), real_type PointType2d::y() 00158 */ 00159 /* 00160 template <class TTLtraits> 00161 typename TTLtraits::real_type scalarProduct2d(const typename TTLtraits::DartType& d, 00162 const typename TTLtraits::PointType2d& p) { 00163 typename TTLtraits::DartType d0 = d; 00164 d0.alpha0(); 00165 00166 return scalarProduct2d(d0.x() - d.x(), d0.y() - d.y(), p.x() - d.x(), p.y() - d.y()); 00167 } 00168 */ 00169 00170 00171 //------------------------------------------------------------------------------------------------ 00172 /* Cross product between 2D vectors represented as darts. 00173 * 00174 * \par Requires: 00175 * - real_type DartType::x(), real_type DartType::y() 00176 */ 00177 /* 00178 template <class TTLtraits> 00179 typename TTLtraits::real_type crossProduct2d(const typename TTLtraits::DartType& d1, 00180 const typename TTLtraits::DartType& d2) { 00181 00182 TTLtraits::DartType d10 = d1; 00183 d10.alpha0(); 00184 00185 TTLtraits::DartType d20 = d2; 00186 d20.alpha0(); 00187 00188 return crossProduct2d(d10.x() - d1.x(), d10.y() - d1.y(), d20.x() - d2.x(), d20.y() - d2.y()); 00189 } 00190 */ 00191 00192 00193 //------------------------------------------------------------------------------------------------ 00194 /* Cross product between 2D vectors. 00195 * The first vector is represented by the given dart, and the second vector has 00196 * direction from the node associated with given dart - and to the given point. 00197 * 00198 * \par Requires: 00199 * - real_type DartType::x() 00200 * - real_type DartType::y() 00201 */ 00202 /* 00203 template <class TTLtraits> 00204 typename TTLtraits::real_type crossProduct2d(const typename TTLtraits::DartType& d, 00205 const typename TTLtraits::PointType2d& p) { 00206 00207 TTLtraits::DartType d0 = d; 00208 d0.alpha0(); 00209 00210 return crossProduct2d(d0.x() - d.x(), d0.y() - d.y(), p.x() - d.x(), p.y() - d.y()); 00211 } 00212 */ 00213 // Geometric predicates; see more robust schemes by Jonathan Richard Shewchuk at 00214 // http://www.cs.cmu.edu/~quake/robust.html 00215 00216 00217 //------------------------------------------------------------------------------------------------ 00218 /* Return a positive value if the 2d nodes/points \e d, \e d.alpha0(), and 00219 * \e p occur in counterclockwise order; a negative value if they occur 00220 * in clockwise order; and zero if they are collinear. The 00221 * result is also a rough approximation of twice the signed 00222 * area of the triangle defined by the three points. 00223 * 00224 * \par Requires: 00225 * - DartType::x(), DartType::y(), 00226 * - PointType2d::x(), PointType2d::y() 00227 */ 00228 /* 00229 template <class TTLtraits, class DartType, class PointType2d> 00230 typename TTLtraits::real_type orient2dfast(const DartType& n1, const DartType& n2, 00231 const PointType2d& p) { 00232 return ((n2.x()-n1.x())*(p.y()-n1.y()) - (p.x()-n1.x())*(n2.y()-n1.y())); 00233 } 00234 */ 00235 00237 00238 00239 //------------------------------------------------------------------------------------------------ 00240 // ---------------------------- Utilities Involving Points Group -------------------------------- 00241 //------------------------------------------------------------------------------------------------ 00242 00245 00246 //------------------------------------------------------------------------------------------------ 00264 template <class PointType> 00265 vector<PointType*>* createRandomData(int noPoints, int seed=1) { 00266 00267 #ifdef _MSC_VER 00268 srand(seed); 00269 #else 00270 srand48((long int)seed); 00271 #endif 00272 00273 double x, y; 00274 vector<PointType*>* points = new vector<PointType*>(noPoints); 00275 typename vector<PointType*>::iterator it; 00276 for (it = points->begin(); it != points->end(); ++it) { 00277 00278 #ifdef _MSC_VER 00279 int random = rand(); 00280 x = ((double)random/(double)RAND_MAX); 00281 random = rand(); 00282 y = ((double)random/(double)RAND_MAX); 00283 *it = new PointType(x,y); 00284 #else 00285 double random = drand48(); 00286 x = random; 00287 random = drand48(); 00288 y = random; 00289 *it = new PointType(x,y); 00290 #endif 00291 00292 } 00293 return points; 00294 } 00295 00297 00298 }; // End of ttl_util namespace scope 00299 00300 #endif // _TTL_UTIL_H_