Files Classes Functions Hierarchy
00001 00002 #include <mathlib.h> 00003 00004 #include <algorithm> 00005 #include <cmath> 00006 #include <sstream> 00007 using namespace std; 00008 00009 00010 00011 00012 void trianglearea 00013 ( 00014 double & f, 00015 doublec x0, 00016 doublec y0, 00017 doublec x1, 00018 doublec y1, 00019 doublec x2, 00020 doublec y2 00021 ) 00022 { 00023 double b = point2<double>(x0-x1,y0-y1).distance(); 00024 double c = point2<double>(x0-x2,y0-y2).distance(); 00025 double dot = x1*x2+y1*y2; 00026 double z(b*c); 00027 f = 0.5*sqrt(z*z-dot*dot); 00028 } 00029 00030 00031 void trianglearea 00032 ( 00033 double & f, 00034 point3<double> const & p0, 00035 point3<double> const & p1, 00036 point3<double> const & p2 00037 ) 00038 { 00039 point3<double> p; 00040 p.crossproduct(p1-p0,p2-p0); 00041 f = p.distance()*0.5; 00042 } 00043 00044 00045 00046 00047 00048 /* 00049 00050 // Solve linear equations for x0,x1. 00051 // 00052 // [ a00 a01 ] [ x0 ] = [ c0 ] 00053 // a10 a11 x1 c1 00054 boolc d2matsolve 00055 ( 00056 double & x0, 00057 double & x1, 00058 doublec a00, 00059 doublec a01, 00060 doublec a10, 00061 doublec a11, 00062 doublec c0, 00063 doublec c1 00064 ) 00065 { 00066 doublec det = a01*a10-a11*a00; 00067 if (det==0) 00068 return false; 00069 00070 x0 = (a01*c1-a11*c0)/det; 00071 x1 = (a10*c0-a00*c1)/det; 00072 00073 return true; 00074 } 00075 */ 00076 00077 /* 00078 // All the vectors are column vectors. 00079 boolc d2matsolve 00080 ( 00081 point2<double> & x, 00082 point2<double> const & a0, 00083 point2<double> const & a1, 00084 point2<double> const & c 00085 ) 00086 { 00087 doublec det = a0.x*a1.y - a1.x*a0.y; 00088 if (det==0.0) 00089 return false; 00090 00091 doublec detInv = 1.0/det; 00092 00093 x.x = (c.x*a1.y - c.y*a1.x)*detInv; 00094 x.y = (c.y*a0.x - c.x*a0.y)*detInv; 00095 00096 return true; 00097 } 00098 */ 00099 00100 00101 boolc d3matsolve 00102 ( 00103 double & x0, 00104 double & x1, 00105 double & x2, 00106 doublec a00, 00107 doublec a01, 00108 doublec a02, 00109 doublec a10, 00110 doublec a11, 00111 doublec a12, 00112 doublec a20, 00113 doublec a21, 00114 doublec a22, 00115 doublec c0, 00116 doublec c1, 00117 doublec c2 00118 ) 00119 { 00120 assert(false); 00121 return false; 00122 } 00123 00124 00125 boolc d3matsolve 00126 ( 00127 point3<double> & x, 00128 point3<double> const & a0, // Column vector. 00129 point3<double> const & a1, // Column vector. 00130 point3<double> const & a2, // Column vector. 00131 point3<double> const & c 00132 ) 00133 { 00134 return 00135 d3matsolve 00136 ( 00137 x.x,x.y,x.z, 00138 a0.x,a1.x,a2.x, 00139 a0.y,a1.y,a2.y, 00140 a0.z,a1.z,a2.z, 00141 c.x,c.y,c.z 00142 ); 00143 } 00144 00145 00146 00147 00148 boolc solvequadratic 00149 ( 00150 double & t0, 00151 double & t1, 00152 doublec a, 00153 doublec b, 00154 doublec c 00155 ) 00156 { 00157 doublec det = b*b - 4.0 * a * c; 00158 if (det<0.0) 00159 return false; 00160 00161 double y = sqrt(det); 00162 double a2 = 0.5/a; 00163 00164 t0 = (-b-y)*a2; 00165 t1 = (-b+y)*a2; 00166 00167 return true; 00168 } 00169 00170 00171 transrotate2D::transrotate2D(doublec theta) 00172 { 00173 doublec cos_t = cos(theta); 00174 doublec sin_t = sin(theta); 00175 r1 = point2<double>(cos_t,-sin_t); 00176 r2 = point2<double>(sin_t,cos_t); 00177 } 00178 00179 void transrotate2D::eval(point2<double> & p) 00180 { 00181 point2<double> const z(p); 00182 p.x = r1.dot(z); 00183 p.y = r2.dot(z); 00184 } 00185 00186 void transrotate2D::eval 00187 ( 00188 point2<double> & p, 00189 point2<double> const & shift 00190 ) 00191 { 00192 p -= shift; 00193 eval(p); 00194 p += shift; 00195 } 00196 00197 void matrixmult 00198 ( 00199 point3<double> & y, 00200 doublec * m, 00201 point3<double> const & x 00202 ) 00203 { 00204 y.x = m[0]*x.x + m[1]*x.y + m[2]*x.z; 00205 y.y = m[3]*x.x + m[4]*x.y + m[5]*x.z; 00206 y.z = m[6]*x.x + m[7]*x.y + m[8]*x.z; 00207 } 00208 00209 void matrixmult 00210 ( 00211 point3<double> & y, 00212 point3<double> const & r0, 00213 point3<double> const & r1, 00214 point3<double> const & r2, 00215 point3<double> const & x 00216 ) 00217 { 00218 y.x = r0.dot(x); 00219 y.y = r1.dot(x); 00220 y.z = r2.dot(x); 00221 } 00222 00223 00224 boolc lineSegmentIntersection 00225 ( 00226 point2<double> const & p1, 00227 point2<double> const & p2, 00228 point2<double> const & q1, 00229 point2<double> const & q2, 00230 doublec zero 00231 ) 00232 { 00233 point2<double> const a(p2-p1); 00234 point2<double> const aInv(-a.y,a.x); 00235 00236 // point2<double> const aInv(p1.y-p2.y,p2.x-p1.x); 00237 00238 // point2<double> const b(q2-q1); 00239 // point2<double> const bInv(b.y*-1.0,b.x); 00240 point2<double> const bInv(q1.y-q2.y,q2.x-q1.x); 00241 00242 double alpha = a.dot(bInv); 00243 00244 // Zero test on alpha. 00245 if (alpha + zero > 0.0) 00246 { 00247 if (alpha < zero) 00248 return false; 00249 } 00250 00251 point2<double> const k(p1-q1); 00252 00253 double w; 00254 00255 if (alpha<0.0) 00256 { 00257 alpha *= -1.0; 00258 00259 w = k.dot(bInv); 00260 if (w<0.0) 00261 return false; 00262 if (w>alpha) 00263 return false; 00264 00265 w = k.dot(aInv); 00266 if (w<0.0) 00267 return false; 00268 00269 if (w>alpha) 00270 return false; 00271 00272 return true; 00273 } 00274 00275 w = k.dot(bInv)*-1.0; 00276 if (w<0.0) 00277 return false; 00278 if (w>alpha) 00279 return false; 00280 00281 w = k.dot(aInv)*-1.0; 00282 if (w<0.0) 00283 return false; 00284 if (w>alpha) 00285 return false; 00286 00287 return true; 00288 } 00289 00290 boolc lineIntersection 00291 ( 00292 double & tp, 00293 double & tq, 00294 point2<double> const & p1, 00295 point2<double> const & p2, 00296 point2<double> const & q1, 00297 point2<double> const & q2, 00298 doublec zero 00299 ) 00300 { 00301 point2<double> const a(p2-p1); 00302 point2<double> const aInv(-a.y,a.x); 00303 point2<double> const b(q2-q1); 00304 point2<double> const bInv(-b.y,b.x); 00305 00306 doublec alpha = b.dot(aInv); 00307 00308 // Zero test on alpha. 00309 if (alpha + zero > 0.0) 00310 { 00311 if (alpha < zero) 00312 return false; 00313 } 00314 00315 doublec c = 1.0/alpha; 00316 00317 point2<double> const w(p1-q1); 00318 tq = c*w.dot(aInv); 00319 tp = c*w.dot(bInv); 00320 00321 return true; 00322 } 00323 00324 00325 00326 00327 boolc lineSegmentIntersection 00328 ( 00329 double & tp, 00330 double & tq, 00331 point2<double> const & p1, 00332 point2<double> const & p2, 00333 point2<double> const & q1, 00334 point2<double> const & q2, 00335 doublec zero 00336 ) 00337 { 00338 point2<double> const a(p2-p1); 00339 point2<double> const aInv(-a.y,a.x); 00340 point2<double> const b(q2-q1); 00341 point2<double> const bInv(-b.y,b.x); 00342 00343 doublec alpha = b.dot(aInv); 00344 00345 // Zero test on alpha. 00346 if (alpha + zero > 0.0) 00347 { 00348 if (alpha < zero) 00349 return false; 00350 } 00351 00352 doublec c = 1.0/alpha; 00353 00354 point2<double> const w(p1-q1); 00355 tq = c*w.dot(aInv); 00356 00357 if (tq<0.0) 00358 return false; 00359 00360 if (tq>1.0) 00361 return false; 00362 00363 tp = c*w.dot(bInv); 00364 00365 if (tp<0.0) 00366 return false; 00367 00368 if (tp>1.0) 00369 return false; 00370 00371 return true; 00372 } 00373 00374 void tetrahedronvolume 00375 ( 00376 double & vol, 00377 point3<double> const & p0, 00378 point3<double> const & p1, 00379 point3<double> const & p2, 00380 point3<double> const & p3 00381 ) 00382 { 00383 //point3<double> a(p1-p0); 00384 //point3<double> b(p2-p0); 00385 //point3<double> c(p3-p0); 00386 00387 /* 00388 cout << SHOW(p0) << endl; 00389 cout << SHOW(p1) << endl; 00390 cout << SHOW(p2) << endl; 00391 cout << SHOW(p3) << endl; 00392 00393 cout << SHOW(a) << endl; 00394 cout << SHOW(b) << endl; 00395 cout << SHOW(c) << endl; 00396 */ 00397 00398 point3<double> q; 00399 crossproduct::eval(q,p1-p0,p2-p0); 00400 //crossprod< point3<double> >()(q,p1-p0,p2-p0); 00401 //cout << SHOW(q) << endl; 00402 vol = (p3-p0).dot(q)/6.0; 00403 } 00404 00405 00406 00407 00408 void polygonconvexarea 00409 ( 00410 double & area, 00411 vector< point2<double> > const & v 00412 ) 00413 { 00414 uintc n = v.size(); 00415 00416 area = v[n-1].x*v[0].y - v[0].x*v[n-1].y; 00417 for (uint i=0; i<n-1; ++i) 00418 { 00419 area += v[i].x*v[i+1].y - v[i+1].x*v[i].y; 00420 } 00421 area *= 0.5; 00422 } 00423 00424 00425 00426 00427 void mathcombination 00428 ( 00429 double & result, 00430 uintc n, 00431 uintc k 00432 ) 00433 { 00434 // (n,k) = prod(i=0..n-k-1, (n-i)/(n-k-i) ) 00435 result = 1.0; 00436 00437 uintc ndiffk = n-k; 00438 for (uint i=0; i<ndiffk; ++i) 00439 { 00440 result *= (n-i); 00441 result /= (ndiffk-i); 00442 } 00443 } 00444 00445 00446 00447 void integersetdiff 00448 ( 00449 vector<uint> & vecnot, 00450 vector<uint> & vec, 00451 uintc N 00452 ) 00453 { 00454 vecnot.clear(); 00455 sort(vec.begin(),vec.end()); 00456 00457 uint k=0; 00458 for (uint i=0; i<N; ++i) 00459 { 00460 if (vec[k]==i) 00461 ++k; 00462 else 00463 vecnot.push_back(i); 00464 } 00465 } 00466 00467 00468 00469 00470 00471 00472 00473 00474 00475 00476 00477 00478 00479
1.5.8