BWAPI
|
00001 // Copyright (c) 2000 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/Cartesian_kernel/include/CGAL/constructions/kernel_ftC3.h $ 00019 // $Id: kernel_ftC3.h 43442 2008-06-04 08:55:44Z pmachado $ 00020 // 00021 // 00022 // Author(s) : Herve Bronnimann 00023 00024 #ifndef CGAL_CONSTRUCTIONS_KERNEL_FTC3_H 00025 #define CGAL_CONSTRUCTIONS_KERNEL_FTC3_H 00026 00027 #include <CGAL/determinant.h> 00028 00029 CGAL_BEGIN_NAMESPACE 00030 00031 template < class FT > 00032 CGAL_KERNEL_INLINE 00033 void 00034 midpointC3( const FT &px, const FT &py, const FT &pz, 00035 const FT &qx, const FT &qy, const FT &qz, 00036 FT &x, FT &y, FT &z) 00037 { 00038 x = (px + qx) / 2; 00039 y = (py + qy) / 2; 00040 z = (pz + qz) / 2; 00041 } 00042 00043 template < class FT > 00044 void 00045 barycenterC3(const FT &p1x, const FT &p1y, const FT &p1z, const FT &w1, 00046 const FT &p2x, const FT &p2y, const FT &p2z, 00047 FT &x, FT &y, FT &z) 00048 { 00049 FT w2 = 1 - w1; 00050 x = w1 * p1x + w2 * p2x; 00051 y = w1 * p1y + w2 * p2y; 00052 z = w1 * p1z + w2 * p2z; 00053 } 00054 00055 template < class FT > 00056 void 00057 barycenterC3(const FT &p1x, const FT &p1y, const FT &p1z, const FT &w1, 00058 const FT &p2x, const FT &p2y, const FT &p2z, const FT &w2, 00059 FT &x, FT &y, FT &z) 00060 { 00061 FT sum = w1 + w2; 00062 CGAL_kernel_assertion(sum != 0); 00063 x = (w1 * p1x + w2 * p2x) / sum; 00064 y = (w1 * p1y + w2 * p2y) / sum; 00065 z = (w1 * p1z + w2 * p2z) / sum; 00066 } 00067 00068 template < class FT > 00069 void 00070 barycenterC3(const FT &p1x, const FT &p1y, const FT &p1z, const FT &w1, 00071 const FT &p2x, const FT &p2y, const FT &p2z, const FT &w2, 00072 const FT &p3x, const FT &p3y, const FT &p3z, 00073 FT &x, FT &y, FT &z) 00074 { 00075 FT w3 = 1 - w1 - w2; 00076 x = w1 * p1x + w2 * p2x + w3 * p3x; 00077 y = w1 * p1y + w2 * p2y + w3 * p3y; 00078 z = w1 * p1z + w2 * p2z + w3 * p3z; 00079 } 00080 00081 template < class FT > 00082 void 00083 barycenterC3(const FT &p1x, const FT &p1y, const FT &p1z, const FT &w1, 00084 const FT &p2x, const FT &p2y, const FT &p2z, const FT &w2, 00085 const FT &p3x, const FT &p3y, const FT &p3z, const FT &w3, 00086 FT &x, FT &y, FT &z) 00087 { 00088 FT sum = w1 + w2 + w3; 00089 CGAL_kernel_assertion(sum != 0); 00090 x = (w1 * p1x + w2 * p2x + w3 * p3x) / sum; 00091 y = (w1 * p1y + w2 * p2y + w3 * p3y) / sum; 00092 z = (w1 * p1z + w2 * p2z + w3 * p3z) / sum; 00093 } 00094 00095 template < class FT > 00096 void 00097 barycenterC3(const FT &p1x, const FT &p1y, const FT &p1z, const FT &w1, 00098 const FT &p2x, const FT &p2y, const FT &p2z, const FT &w2, 00099 const FT &p3x, const FT &p3y, const FT &p3z, const FT &w3, 00100 const FT &p4x, const FT &p4y, const FT &p4z, 00101 FT &x, FT &y, FT &z) 00102 { 00103 FT w4 = 1 - w1 - w2 - w3; 00104 x = w1 * p1x + w2 * p2x + w3 * p3x + w4 * p4x; 00105 y = w1 * p1y + w2 * p2y + w3 * p3y + w4 * p4y; 00106 z = w1 * p1z + w2 * p2z + w3 * p3z + w4 * p4z; 00107 } 00108 00109 template < class FT > 00110 void 00111 barycenterC3(const FT &p1x, const FT &p1y, const FT &p1z, const FT &w1, 00112 const FT &p2x, const FT &p2y, const FT &p2z, const FT &w2, 00113 const FT &p3x, const FT &p3y, const FT &p3z, const FT &w3, 00114 const FT &p4x, const FT &p4y, const FT &p4z, const FT &w4, 00115 FT &x, FT &y, FT &z) 00116 { 00117 FT sum = w1 + w2 + w3 + w4; 00118 CGAL_kernel_assertion(sum != 0); 00119 x = (w1 * p1x + w2 * p2x + w3 * p3x + w4 * p4x) / sum; 00120 y = (w1 * p1y + w2 * p2y + w3 * p3y + w4 * p4y) / sum; 00121 z = (w1 * p1z + w2 * p2z + w3 * p3z + w4 * p4z) / sum; 00122 } 00123 00124 template < class FT > 00125 void 00126 centroidC3( const FT &px, const FT &py, const FT &pz, 00127 const FT &qx, const FT &qy, const FT &qz, 00128 const FT &rx, const FT &ry, const FT &rz, 00129 const FT &sx, const FT &sy, const FT &sz, 00130 FT &x, FT &y, FT &z) 00131 { 00132 x = (px + qx + rx + sx) / 4; 00133 y = (py + qy + ry + sy) / 4; 00134 z = (pz + qz + rz + sz) / 4; 00135 } 00136 00137 template < class FT > 00138 void 00139 centroidC3( const FT &px, const FT &py, const FT &pz, 00140 const FT &qx, const FT &qy, const FT &qz, 00141 const FT &rx, const FT &ry, const FT &rz, 00142 FT &x, FT &y, FT &z) 00143 { 00144 x = (px + qx + rx) / 3; 00145 y = (py + qy + ry) / 3; 00146 z = (pz + qz + rz) / 3; 00147 } 00148 00149 template < class FT > 00150 CGAL_KERNEL_MEDIUM_INLINE 00151 FT 00152 squared_radiusC3(const FT &px, const FT &py, const FT &pz, 00153 const FT &qx, const FT &qy, const FT &qz, 00154 const FT &rx, const FT &ry, const FT &rz, 00155 const FT &sx, const FT &sy, const FT &sz) 00156 { 00157 // Translate p to origin to simplify the expression. 00158 FT qpx = qx-px; 00159 FT qpy = qy-py; 00160 FT qpz = qz-pz; 00161 FT qp2 = CGAL_NTS square(qpx) + CGAL_NTS square(qpy) + CGAL_NTS square(qpz); 00162 FT rpx = rx-px; 00163 FT rpy = ry-py; 00164 FT rpz = rz-pz; 00165 FT rp2 = CGAL_NTS square(rpx) + CGAL_NTS square(rpy) + CGAL_NTS square(rpz); 00166 FT spx = sx-px; 00167 FT spy = sy-py; 00168 FT spz = sz-pz; 00169 FT sp2 = CGAL_NTS square(spx) + CGAL_NTS square(spy) + CGAL_NTS square(spz); 00170 00171 FT num_x = determinant(qpy,qpz,qp2, 00172 rpy,rpz,rp2, 00173 spy,spz,sp2); 00174 FT num_y = determinant(qpx,qpz,qp2, 00175 rpx,rpz,rp2, 00176 spx,spz,sp2); 00177 FT num_z = determinant(qpx,qpy,qp2, 00178 rpx,rpy,rp2, 00179 spx,spy,sp2); 00180 FT den = determinant(qpx,qpy,qpz, 00181 rpx,rpy,rpz, 00182 spx,spy,spz); 00183 CGAL_kernel_assertion( ! CGAL_NTS is_zero(den) ); 00184 00185 return (CGAL_NTS square(num_x) + CGAL_NTS square(num_y) 00186 + CGAL_NTS square(num_z)) / CGAL_NTS square(2 * den); 00187 } 00188 00189 template < class FT > 00190 CGAL_KERNEL_MEDIUM_INLINE 00191 FT 00192 squared_radiusC3(const FT &px, const FT &py, const FT &pz, 00193 const FT &qx, const FT &qy, const FT &qz, 00194 const FT &sx, const FT &sy, const FT &sz) 00195 { 00196 // Translate s to origin to simplify the expression. 00197 FT psx = px-sx; 00198 FT psy = py-sy; 00199 FT psz = pz-sz; 00200 FT ps2 = CGAL_NTS square(psx) + CGAL_NTS square(psy) + CGAL_NTS square(psz); 00201 FT qsx = qx-sx; 00202 FT qsy = qy-sy; 00203 FT qsz = qz-sz; 00204 FT qs2 = CGAL_NTS square(qsx) + CGAL_NTS square(qsy) + CGAL_NTS square(qsz); 00205 FT rsx = psy*qsz-psz*qsy; 00206 FT rsy = psz*qsx-psx*qsz; 00207 FT rsz = psx*qsy-psy*qsx; 00208 00209 FT num_x = ps2 * determinant(qsy,qsz,rsy,rsz) 00210 - qs2 * determinant(psy,psz,rsy,rsz); 00211 FT num_y = ps2 * determinant(qsx,qsz,rsx,rsz) 00212 - qs2 * determinant(psx,psz,rsx,rsz); 00213 FT num_z = ps2 * determinant(qsx,qsy,rsx,rsy) 00214 - qs2 * determinant(psx,psy,rsx,rsy); 00215 00216 FT den = determinant(psx,psy,psz, 00217 qsx,qsy,qsz, 00218 rsx,rsy,rsz); 00219 00220 CGAL_kernel_assertion( den != 0 ); 00221 00222 return (CGAL_NTS square(num_x) + CGAL_NTS square(num_y) 00223 + CGAL_NTS square(num_z)) / CGAL_NTS square(2 * den); 00224 } 00225 00226 template <class FT> 00227 CGAL_KERNEL_MEDIUM_INLINE 00228 void 00229 plane_from_pointsC3(const FT &px, const FT &py, const FT &pz, 00230 const FT &qx, const FT &qy, const FT &qz, 00231 const FT &rx, const FT &ry, const FT &rz, 00232 FT &pa, FT &pb, FT &pc, FT &pd) 00233 { 00234 FT rpx = px-rx; 00235 FT rpy = py-ry; 00236 FT rpz = pz-rz; 00237 FT rqx = qx-rx; 00238 FT rqy = qy-ry; 00239 FT rqz = qz-rz; 00240 // Cross product rp * rq 00241 pa = rpy*rqz - rqy*rpz; 00242 pb = rpz*rqx - rqz*rpx; 00243 pc = rpx*rqy - rqx*rpy; 00244 pd = - pa*rx - pb*ry - pc*rz; 00245 } 00246 00247 template <class FT> 00248 CGAL_KERNEL_MEDIUM_INLINE 00249 void 00250 plane_from_point_directionC3(const FT &px, const FT &py, const FT &pz, 00251 const FT &dx, const FT &dy, const FT &dz, 00252 FT &pa, FT &pb, FT &pc, FT &pd) 00253 { 00254 // d is the normal direction 00255 pa = dx; pb = dy; pc = dz; pd = -dx*px - dy*py - dz*pz; 00256 } 00257 00258 template <class FT> 00259 CGAL_KERNEL_MEDIUM_INLINE 00260 void 00261 point_on_planeC3(const FT &pa, const FT &pb, const FT &pc, const FT &pd, 00262 FT &x, FT &y, FT &z) 00263 { 00264 x = y = z = 0; 00265 if (! CGAL_NTS is_zero(pa)) x = -pd/pa; 00266 else if (! CGAL_NTS is_zero(pb)) y = -pd/pb; 00267 else z = -pd/pc; 00268 } 00269 00270 template <class FT> 00271 CGAL_KERNEL_MEDIUM_INLINE 00272 void 00273 projection_planeC3(const FT &pa, const FT &pb, const FT &pc, const FT &pd, 00274 const FT &px, const FT &py, const FT &pz, 00275 FT &x, FT &y, FT &z) 00276 { 00277 // the equation of the plane is Ax+By+Cz+D=0 00278 // the normal direction is (A,B,C) 00279 // the projected point is p-lambda(A,B,C) where 00280 // A(x-lambda A) + B(y-lambda B) + C(z-lambda C) + D = 0 00281 00282 FT num = pa*px + pb*py + pc*pz + pd; 00283 FT den = pa*pa + pb*pb + pc*pc; 00284 FT lambda = num / den; 00285 00286 x = px - lambda * pa; 00287 y = py - lambda * pb; 00288 z = pz - lambda * pc; 00289 } 00290 00291 template < class FT > 00292 CGAL_KERNEL_INLINE 00293 FT 00294 squared_distanceC3( const FT &px, const FT &py, const FT &pz, 00295 const FT &qx, const FT &qy, const FT &qz) 00296 { 00297 return CGAL_NTS square(px-qx) + CGAL_NTS square(py-qy) + 00298 CGAL_NTS square(pz-qz); 00299 } 00300 00301 template < class FT > 00302 CGAL_KERNEL_INLINE 00303 FT 00304 squared_radiusC3( const FT &px, const FT &py, const FT &pz, 00305 const FT &qx, const FT &qy, const FT &qz) 00306 { 00307 return squared_distanceC3(px, py, pz, qx, qy, qz) / 4; 00308 } 00309 00310 template < class FT > 00311 CGAL_KERNEL_INLINE 00312 FT 00313 scaled_distance_to_directionC3(const FT &pa, const FT &pb, const FT &pc, 00314 const FT &px, const FT &py, const FT &pz) 00315 { 00316 return pa*px + pb*py + pc*pz; 00317 } 00318 00319 template < class FT > 00320 CGAL_KERNEL_INLINE 00321 FT 00322 scaled_distance_to_planeC3( 00323 const FT &pa, const FT &pb, const FT &pc, const FT &pd, 00324 const FT &px, const FT &py, const FT &pz) 00325 { 00326 return pa*px + pb*py + pc*pz + pd; 00327 } 00328 00329 template < class FT > 00330 CGAL_KERNEL_INLINE 00331 FT 00332 scaled_distance_to_planeC3( 00333 const FT &ppx, const FT &ppy, const FT &ppz, 00334 const FT &pqx, const FT &pqy, const FT &pqz, 00335 const FT &prx, const FT &pry, const FT &prz, 00336 const FT &px, const FT &py, const FT &pz) 00337 { 00338 return determinant(ppx-px,ppy-py,ppz-pz, 00339 pqx-px,pqy-py,pqz-pz, 00340 prx-px,pry-py,prz-pz); 00341 } 00342 00343 template < class FT > 00344 CGAL_KERNEL_INLINE 00345 void 00346 bisector_of_pointsC3(const FT &px, const FT &py, const FT &pz, 00347 const FT &qx, const FT &qy, const FT &qz, 00348 FT &a, FT &b, FT &c, FT &d) 00349 { 00350 a = 2*(px - qx); 00351 b = 2*(py - qy); 00352 c = 2*(pz - qz); 00353 d = CGAL_NTS square(qx) + CGAL_NTS square(qy) + CGAL_NTS square(qz) 00354 - CGAL_NTS square(px) - CGAL_NTS square(py) - CGAL_NTS square(pz); 00355 } 00356 00357 template < class FT > 00358 void 00359 bisector_of_planesC3(const FT &pa, const FT &pb, const FT &pc, const FT &pd, 00360 const FT &qa, const FT &qb, const FT &qc, const FT &qd, 00361 FT &a, FT &b, FT &c, FT &d) 00362 { 00363 // We normalize the equations of the 2 planes, and we then add them. 00364 FT n1 = CGAL_NTS sqrt(CGAL_NTS square(pa) + CGAL_NTS square(pb) + 00365 CGAL_NTS square(pc)); 00366 FT n2 = CGAL_NTS sqrt(CGAL_NTS square(qa) + CGAL_NTS square(qb) + 00367 CGAL_NTS square(qc)); 00368 a = n2 * pa + n1 * qa; 00369 b = n2 * pb + n1 * qb; 00370 c = n2 * pc + n1 * qc; 00371 d = n2 * pd + n1 * qd; 00372 00373 // Care must be taken for the case when this produces a degenerate line. 00374 if (a == 0 && b == 0 && c == 0) { 00375 a = n2 * pa - n1 * qa; 00376 b = n2 * pb - n1 * qb; 00377 c = n2 * pc - n1 * qc; 00378 d = n2 * pd - n1 * qd; 00379 } 00380 } 00381 00382 template < class FT > 00383 FT 00384 squared_areaC3(const FT &px, const FT &py, const FT &pz, 00385 const FT &qx, const FT &qy, const FT &qz, 00386 const FT &rx, const FT &ry, const FT &rz) 00387 { 00388 // Compute vectors pq and pr, then the cross product, 00389 // then 1/4 of its squared length. 00390 FT dqx = qx-px; 00391 FT dqy = qy-py; 00392 FT dqz = qz-pz; 00393 FT drx = rx-px; 00394 FT dry = ry-py; 00395 FT drz = rz-pz; 00396 00397 FT vx = dqy*drz-dqz*dry; 00398 FT vy = dqz*drx-dqx*drz; 00399 FT vz = dqx*dry-dqy*drx; 00400 00401 return (CGAL_NTS square(vx) + CGAL_NTS square(vy) + CGAL_NTS square(vz))/4; 00402 } 00403 00404 CGAL_END_NAMESPACE 00405 00406 #endif // CGAL_CONSTRUCTIONS_KERNEL_FTC3_H