00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "plane.H"
00028 #include "tensor.H"
00029
00030
00031
00032
00033 void Foam::plane::calcPntAndVec(const scalarList& C)
00034 {
00035 if (mag(C[0]) > VSMALL)
00036 {
00037 basePoint_ = vector((-C[3]/C[0]), 0, 0);
00038 }
00039 else
00040 {
00041 if (mag(C[1]) > VSMALL)
00042 {
00043 basePoint_ = vector(0, (-C[3]/C[1]), 0);
00044 }
00045 else
00046 {
00047 if (mag(C[2]) > VSMALL)
00048 {
00049 basePoint_ = vector(0, 0, (-C[3]/C[2]));
00050 }
00051 else
00052 {
00053 FatalErrorIn("void plane::calcPntAndVec(const scalarList&)")
00054 << "At least one plane coefficient must have a value"
00055 << abort(FatalError);
00056 }
00057 }
00058 }
00059
00060 unitVector_ = vector(C[0], C[1], C[2]);
00061 scalar magUnitVector(mag(unitVector_));
00062
00063 if (magUnitVector < VSMALL)
00064 {
00065 FatalErrorIn("void plane::calcPntAndVec(const scalarList&)")
00066 << "Plane normal defined with zero length"
00067 << abort(FatalError);
00068 }
00069
00070 unitVector_ /= magUnitVector;
00071 }
00072
00073
00074 void Foam::plane::calcPntAndVec
00075 (
00076 const point& point1,
00077 const point& point2,
00078 const point& point3
00079 )
00080 {
00081 basePoint_ = (point1 + point2 + point3)/3;
00082 vector line12 = point1 - point2;
00083 vector line23 = point2 - point3;
00084
00085 if
00086 (
00087 mag(line12) < VSMALL
00088 || mag(line23) < VSMALL
00089 || mag(point3-point1) < VSMALL
00090 )
00091 {
00092 FatalErrorIn
00093 (
00094 "void plane::calcPntAndVec\n"
00095 "(\n"
00096 " const point&,\n"
00097 " const point&,\n"
00098 " const point&\n"
00099 ")\n"
00100 ) << "Bad points." << abort(FatalError);
00101 }
00102
00103 unitVector_ = line12 ^ line23;
00104 scalar magUnitVector(mag(unitVector_));
00105
00106 if (magUnitVector < VSMALL)
00107 {
00108 FatalErrorIn
00109 (
00110 "void plane::calcPntAndVec\n"
00111 "(\n"
00112 " const point&,\n"
00113 " const point&,\n"
00114 " const point&\n"
00115 ")\n"
00116 ) << "Plane normal defined with zero length"
00117 << abort(FatalError);
00118 }
00119
00120 unitVector_ /= magUnitVector;
00121 }
00122
00123
00124
00125
00126
00127 Foam::plane::plane(const vector& normalVector)
00128 :
00129 unitVector_(normalVector),
00130 basePoint_(vector::zero)
00131 {}
00132
00133
00134
00135 Foam::plane::plane(const point& basePoint, const vector& normalVector)
00136 :
00137 unitVector_(normalVector),
00138 basePoint_(basePoint)
00139 {
00140 scalar magUnitVector(mag(unitVector_));
00141
00142 if (magUnitVector > VSMALL)
00143 {
00144 unitVector_ /= magUnitVector;
00145 }
00146 else
00147 {
00148 FatalErrorIn("plane::plane(const point&, const vector&)")
00149 << "plane normal has got zero length"
00150 << abort(FatalError);
00151 }
00152 }
00153
00154
00155
00156 Foam::plane::plane(const scalarList& C)
00157 {
00158 calcPntAndVec(C);
00159 }
00160
00161
00162
00163 Foam::plane::plane
00164 (
00165 const point& a,
00166 const point& b,
00167 const point& c
00168 )
00169 {
00170 calcPntAndVec(a, b, c);
00171 }
00172
00173
00174
00175 Foam::plane::plane(const dictionary& dict)
00176 :
00177 unitVector_(vector::zero),
00178 basePoint_(point::zero)
00179 {
00180 word planeType(dict.lookup("planeType"));
00181
00182 if (planeType == "planeEquation")
00183 {
00184 const dictionary& subDict = dict.subDict("planeEquationDict");
00185 scalarList C(4);
00186
00187 C[0] = readScalar(subDict.lookup("a"));
00188 C[1] = readScalar(subDict.lookup("b"));
00189 C[2] = readScalar(subDict.lookup("c"));
00190 C[3] = readScalar(subDict.lookup("d"));
00191
00192 calcPntAndVec(C);
00193
00194 }
00195 else if (planeType == "embeddedPoints")
00196 {
00197 const dictionary& subDict = dict.subDict("embeddedPoints");
00198
00199 point point1(subDict.lookup("point1"));
00200 point point2(subDict.lookup("point2"));
00201 point point3(subDict.lookup("point3"));
00202
00203 calcPntAndVec(point1, point2, point3);
00204 }
00205 else if (planeType == "pointAndNormal")
00206 {
00207 const dictionary& subDict = dict.subDict("pointAndNormalDict");
00208
00209 basePoint_ = subDict.lookup("basePoint");
00210 unitVector_ = subDict.lookup("normalVector");
00211 unitVector_ /= mag(unitVector_);
00212 }
00213 else
00214 {
00215 FatalIOErrorIn
00216 (
00217 "plane::plane(const dictionary&)",
00218 dict
00219 )
00220 << "Invalid plane type: " << planeType
00221 << abort(FatalIOError);
00222 }
00223 }
00224
00225
00226
00227 Foam::plane::plane(Istream& is)
00228 :
00229 unitVector_(is),
00230 basePoint_(is)
00231 {
00232 scalar magUnitVector(mag(unitVector_));
00233
00234 if (magUnitVector > VSMALL)
00235 {
00236 unitVector_ /= magUnitVector;
00237 }
00238 else
00239 {
00240 FatalErrorIn("plane::plane(Istream& is)")
00241 << "plane normal has got zero length"
00242 << abort(FatalError);
00243 }
00244 }
00245
00246
00247
00248
00249
00250 const Foam::vector& Foam::plane::normal() const
00251 {
00252 return unitVector_;
00253 }
00254
00255
00256
00257 const Foam::point& Foam::plane::refPoint() const
00258 {
00259 return basePoint_;
00260 }
00261
00262
00263
00264 Foam::scalarList Foam::plane::planeCoeffs() const
00265 {
00266 scalarList C(4);
00267
00268 scalar magX = mag(unitVector_.x());
00269 scalar magY = mag(unitVector_.y());
00270 scalar magZ = mag(unitVector_.z());
00271
00272 if (magX > magY)
00273 {
00274 if (magX > magZ)
00275 {
00276 C[0] = 1;
00277 C[1] = unitVector_.y()/unitVector_.x();
00278 C[2] = unitVector_.z()/unitVector_.x();
00279 }
00280 else
00281 {
00282 C[0] = 0;
00283 C[1] = 0;
00284 C[2] = 1;
00285 }
00286 }
00287 else
00288 {
00289 if (magY > magZ)
00290 {
00291 C[0] = 0;
00292 C[1] = 1;
00293 C[2] = unitVector_.z()/unitVector_.y();
00294 }
00295 else
00296 {
00297 C[0] = 0;
00298 C[1] = 0;
00299 C[2] = 1;
00300 }
00301 }
00302
00303 C[3] = - C[0] * basePoint_.x()
00304 - C[1] * basePoint_.y()
00305 - C[2] * basePoint_.z();
00306
00307 return C;
00308 }
00309
00310
00311
00312 Foam::point Foam::plane::nearestPoint(const point& p) const
00313 {
00314 return p - unitVector_*((p - basePoint_) & unitVector_);
00315 }
00316
00317
00318
00319 Foam::scalar Foam::plane::distance(const point& p) const
00320 {
00321 return mag((p - basePoint_) & unitVector_);
00322 }
00323
00324
00325
00326 Foam::scalar Foam::plane::normalIntersect
00327 (
00328 const point& pnt0,
00329 const vector& dir
00330 ) const
00331 {
00332 scalar denom = stabilise((dir & unitVector_), VSMALL);
00333
00334 return ((basePoint_ - pnt0) & unitVector_)/denom;
00335 }
00336
00337
00338
00339 Foam::plane::ray Foam::plane::planeIntersect(const plane& plane2) const
00340 {
00341
00342
00343
00344
00345
00346 const vector& n1 = normal();
00347 const vector& n2 = plane2.normal();
00348
00349 const point& p1 = refPoint();
00350 const point& p2 = plane2.refPoint();
00351
00352 scalar n1p1 = n1&p1;
00353 scalar n2p2 = n2&p2;
00354
00355 vector dir = n1 ^ n2;
00356
00357
00358
00359 scalar magX = mag(dir.x());
00360 scalar magY = mag(dir.y());
00361 scalar magZ = mag(dir.z());
00362
00363 direction iZero, i1, i2;
00364
00365 if (magX > magY)
00366 {
00367 if (magX > magZ)
00368 {
00369 iZero = 0;
00370 i1 = 1;
00371 i2 = 2;
00372 }
00373 else
00374 {
00375 iZero = 2;
00376 i1 = 0;
00377 i2 = 1;
00378 }
00379 }
00380 else
00381 {
00382 if (magY > magZ)
00383 {
00384 iZero = 1;
00385 i1 = 2;
00386 i2 = 0;
00387 }
00388 else
00389 {
00390 iZero = 2;
00391 i1 = 0;
00392 i2 = 1;
00393 }
00394 }
00395
00396 vector pt;
00397
00398 pt[iZero] = 0;
00399 pt[i1] = (n2[i2]*n1p1 - n1[i2]*n2p2) / (n1[i1]*n2[i2] - n2[i1]*n1[i2]);
00400 pt[i2] = (n2[i1]*n1p1 - n1[i1]*n2p2) / (n1[i2]*n2[i1] - n1[i1]*n2[i2]);
00401
00402 return ray(pt, dir);
00403 }
00404
00405
00406
00407 Foam::point Foam::plane::planePlaneIntersect
00408 (
00409 const plane& plane2,
00410 const plane& plane3
00411 ) const
00412 {
00413 List<scalarList> pcs(3);
00414 pcs[0]= planeCoeffs();
00415 pcs[1]= plane2.planeCoeffs();
00416 pcs[2]= plane3.planeCoeffs();
00417
00418 tensor a
00419 (
00420 pcs[0][0],pcs[0][1],pcs[0][2],
00421 pcs[1][0],pcs[1][1],pcs[1][2],
00422 pcs[2][0],pcs[2][1],pcs[2][2]
00423 );
00424
00425 vector b(pcs[0][3],pcs[1][3],pcs[2][3]);
00426
00427 return (inv(a) & (-b));
00428 }
00429
00430
00431
00432
00433 bool Foam::operator==(const plane& a, const plane& b)
00434 {
00435 if (a.basePoint_ == b.basePoint_ && a.unitVector_ == b.unitVector_)
00436 {
00437 return true;
00438 }
00439 else
00440 {
00441 return false;
00442 }
00443 }
00444
00445 bool Foam::operator!=(const plane& a, const plane& b)
00446 {
00447 return !(a == b);
00448 }
00449
00450
00451
00452
00453 Foam::Ostream& Foam::operator<<(Ostream& os, const plane& a)
00454 {
00455 os << a.unitVector_ << token::SPACE << a.basePoint_;
00456
00457 return os;
00458 }
00459
00460
00461