Warning, /include/opencascade/Bnd_B3x.gxx is written in an unsupported language. File is not indexed.
0001 // Created on: 2005-09-08
0002 // Created by: Alexander GRIGORIEV
0003 // Copyright (c) 2005-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015
0016 inline Standard_Boolean _compareDist (const RealType aHSize[3],
0017 const RealType aDist [3])
0018 {
0019 return (Abs(aDist[0]) > aHSize[0] ||
0020 Abs(aDist[1]) > aHSize[1] ||
0021 Abs(aDist[2]) > aHSize[2]);
0022 }
0023
0024 inline Standard_Boolean _compareDistD (const gp_XYZ& aHSize,const gp_XYZ& aDist)
0025 {
0026 return (Abs(aDist.X()) > aHSize.X() ||
0027 Abs(aDist.Y()) > aHSize.Y() ||
0028 Abs(aDist.Z()) > aHSize.Z());
0029 }
0030
0031 //=======================================================================
0032 //function : Add
0033 //purpose : Update the box by a point
0034 //=======================================================================
0035
0036 void Bnd_B3x::Add (const gp_XYZ& thePnt) {
0037 if (IsVoid()) {
0038 myCenter[0] = RealType(thePnt.X());
0039 myCenter[1] = RealType(thePnt.Y());
0040 myCenter[2] = RealType(thePnt.Z());
0041 myHSize [0] = 0.;
0042 myHSize [1] = 0.;
0043 myHSize [2] = 0.;
0044 } else {
0045 const RealType aDiff[3] = {
0046 RealType(thePnt.X()) - myCenter[0],
0047 RealType(thePnt.Y()) - myCenter[1],
0048 RealType(thePnt.Z()) - myCenter[2]
0049 };
0050 if (aDiff[0] > myHSize[0]) {
0051 const RealType aShift = (aDiff[0] - myHSize[0]) / 2;
0052 myCenter[0] += aShift;
0053 myHSize [0] += aShift;
0054 } else if (aDiff[0] < -myHSize[0]) {
0055 const RealType aShift = (aDiff[0] + myHSize[0]) / 2;
0056 myCenter[0] += aShift;
0057 myHSize [0] -= aShift;
0058 }
0059 if (aDiff[1] > myHSize[1]) {
0060 const RealType aShift = (aDiff[1] - myHSize[1]) / 2;
0061 myCenter[1] +=aShift;
0062 myHSize [1] +=aShift;
0063 } else if (aDiff[1] < -myHSize[1]) {
0064 const RealType aShift = (aDiff[1] + myHSize[1]) / 2;
0065 myCenter[1] += aShift;
0066 myHSize [1] -= aShift;
0067 }
0068 if (aDiff[2] > myHSize[2]) {
0069 const RealType aShift = (aDiff[2] - myHSize[2]) / 2;
0070 myCenter[2] +=aShift;
0071 myHSize [2] +=aShift;
0072 } else if (aDiff[2] < -myHSize[2]) {
0073 const RealType aShift = (aDiff[2] + myHSize[2]) / 2;
0074 myCenter[2] += aShift;
0075 myHSize [2] -= aShift;
0076 }
0077 }
0078 }
0079
0080 //=======================================================================
0081 //function : Limit
0082 //purpose : limit the current box with the internals of theBox
0083 //=======================================================================
0084
0085 Standard_Boolean Bnd_B3x::Limit (const Bnd_B3x& theBox)
0086 {
0087 Standard_Boolean aResult (Standard_False);
0088 const RealType diffC[3] = {
0089 theBox.myCenter[0] - myCenter[0],
0090 theBox.myCenter[1] - myCenter[1],
0091 theBox.myCenter[2] - myCenter[2]
0092 };
0093 const RealType sumH[3] = {
0094 theBox.myHSize[0] + myHSize[0],
0095 theBox.myHSize[1] + myHSize[1],
0096 theBox.myHSize[2] + myHSize[2]
0097 };
0098 // check the condition IsOut
0099 if (_compareDist (sumH, diffC) == Standard_False) {
0100 const RealType diffH[3] = {
0101 theBox.myHSize[0] - myHSize[0],
0102 theBox.myHSize[1] - myHSize[1],
0103 theBox.myHSize[2] - myHSize[2]
0104 };
0105 if (diffC[0] - diffH[0] > 0.) {
0106 const RealType aShift = (diffC[0] - diffH[0]) / 2; // positive
0107 myCenter[0] += aShift;
0108 myHSize [0] -= aShift;
0109 } else if (diffC[0] + diffH[0] < 0.) {
0110 const RealType aShift = (diffC[0] + diffH[0]) / 2; // negative
0111 myCenter[0] += aShift;
0112 myHSize [0] += aShift;
0113 }
0114 if (diffC[1] - diffH[1] > 0.) {
0115 const RealType aShift = (diffC[1] - diffH[1]) / 2; // positive
0116 myCenter[1] += aShift;
0117 myHSize [1] -= aShift;
0118 } else if (diffC[1] + diffH[1] < 0.) {
0119 const RealType aShift = (diffC[1] + diffH[1]) / 2; // negative
0120 myCenter[1] += aShift;
0121 myHSize [1] += aShift;
0122 }
0123 if (diffC[2] - diffH[2] > 0.) {
0124 const RealType aShift = (diffC[2] - diffH[2]) / 2; // positive
0125 myCenter[2] += aShift;
0126 myHSize [2] -= aShift;
0127 } else if (diffC[2] + diffH[2] < 0.) {
0128 const RealType aShift = (diffC[2] + diffH[2]) / 2; // negative
0129 myCenter[2] += aShift;
0130 myHSize [2] += aShift;
0131 }
0132 aResult = Standard_True;
0133 }
0134 return aResult;
0135 }
0136
0137 //=======================================================================
0138 //function : Transformed
0139 //purpose :
0140 //=======================================================================
0141
0142 Bnd_B3x Bnd_B3x::Transformed (const gp_Trsf& theTrsf) const
0143 {
0144 Bnd_B3x aResult;
0145 const gp_TrsfForm aForm = theTrsf.Form();
0146 const Standard_Real aScale = theTrsf.ScaleFactor();
0147 const Standard_Real aScaleAbs = Abs(aScale);
0148 if (aForm == gp_Identity)
0149 aResult = * this;
0150 else if (aForm== gp_Translation || aForm== gp_PntMirror || aForm== gp_Scale)
0151 {
0152 aResult.myCenter[0] =
0153 (RealType)(myCenter[0] * aScale + theTrsf.TranslationPart().X());
0154 aResult.myCenter[1] =
0155 (RealType)(myCenter[1] * aScale + theTrsf.TranslationPart().Y());
0156 aResult.myCenter[2] =
0157 (RealType)(myCenter[2] * aScale + theTrsf.TranslationPart().Z());
0158 aResult.myHSize[0] = (RealType)(myHSize[0] * aScaleAbs);
0159 aResult.myHSize[1] = (RealType)(myHSize[1] * aScaleAbs);
0160 aResult.myHSize[2] = (RealType)(myHSize[2] * aScaleAbs);
0161 } else {
0162 gp_XYZ aCenter ((Standard_Real)myCenter[0],
0163 (Standard_Real)myCenter[1],
0164 (Standard_Real)myCenter[2]);
0165 theTrsf.Transforms (aCenter);
0166 aResult.myCenter[0] = (RealType)aCenter.X();
0167 aResult.myCenter[1] = (RealType)aCenter.Y();
0168 aResult.myCenter[2] = (RealType)aCenter.Z();
0169
0170 const Standard_Real * aMat = &theTrsf.HVectorialPart().Value(1,1);
0171 aResult.myHSize[0] = (RealType)(aScaleAbs * (Abs(aMat[0]) * myHSize[0]+
0172 Abs(aMat[1]) * myHSize[1]+
0173 Abs(aMat[2]) * myHSize[2]));
0174 aResult.myHSize[1] = (RealType)(aScaleAbs * (Abs(aMat[3]) * myHSize[0]+
0175 Abs(aMat[4]) * myHSize[1]+
0176 Abs(aMat[5]) * myHSize[2]));
0177 aResult.myHSize[2] = (RealType)(aScaleAbs * (Abs(aMat[6]) * myHSize[0]+
0178 Abs(aMat[7]) * myHSize[1]+
0179 Abs(aMat[8]) * myHSize[2]));
0180 }
0181 return aResult;
0182 }
0183
0184 //=======================================================================
0185 //function : IsOut
0186 //purpose : Intersection Box - Sphere
0187 //=======================================================================
0188
0189 Standard_Boolean Bnd_B3x::IsOut (const gp_XYZ& theCenter,
0190 const Standard_Real theRadius,
0191 const Standard_Boolean isSphereHollow) const
0192 {
0193 Standard_Boolean aResult (Standard_True);
0194 if (isSphereHollow == Standard_False) {
0195 // vector from the center of the sphere to the nearest box face
0196 const Standard_Real aDist[3] = {
0197 Abs(theCenter.X()-Standard_Real(myCenter[0])) - Standard_Real(myHSize[0]),
0198 Abs(theCenter.Y()-Standard_Real(myCenter[1])) - Standard_Real(myHSize[1]),
0199 Abs(theCenter.Z()-Standard_Real(myCenter[2])) - Standard_Real(myHSize[2])
0200 };
0201 Standard_Real aD (0.);
0202 if (aDist[0] > 0.)
0203 aD = aDist[0]*aDist[0];
0204 if (aDist[1] > 0.)
0205 aD += aDist[1]*aDist[1];
0206 if (aDist[2] > 0.)
0207 aD += aDist[2]*aDist[2];
0208 aResult = (aD > theRadius*theRadius);
0209 } else {
0210 const Standard_Real aDistC[3] = {
0211 Abs(theCenter.X()-Standard_Real(myCenter[0])),
0212 Abs(theCenter.Y()-Standard_Real(myCenter[1])),
0213 Abs(theCenter.Z()-Standard_Real(myCenter[2]))
0214 };
0215 // vector from the center of the sphere to the nearest box face
0216 Standard_Real aDist[3] = {
0217 aDistC[0] - Standard_Real(myHSize[0]),
0218 aDistC[1] - Standard_Real(myHSize[1]),
0219 aDistC[2] - Standard_Real(myHSize[2])
0220 };
0221 Standard_Real aD (0.);
0222 if (aDist[0] > 0.)
0223 aD = aDist[0]*aDist[0];
0224 if (aDist[1] > 0.)
0225 aD += aDist[1]*aDist[1];
0226 if (aDist[2] > 0.)
0227 aD += aDist[2]*aDist[2];
0228 if (aD < theRadius*theRadius) {
0229 // the box intersects the solid sphere; check if it is completely
0230 // inside the circle (in such case return isOut==True)
0231 aDist[0] = aDistC[0] + Standard_Real(myHSize[0]);
0232 aDist[1] = aDistC[1] + Standard_Real(myHSize[1]);
0233 aDist[2] = aDistC[2] + Standard_Real(myHSize[2]);
0234 if (aDist[0]*aDist[0]+aDist[1]*aDist[1]+aDist[2]*aDist[2]
0235 > theRadius*theRadius)
0236 aResult = Standard_False;
0237 }
0238 }
0239 return aResult;
0240 }
0241
0242 //=======================================================================
0243 //function : IsOut
0244 //purpose : Intersection Box - transformed Box
0245 //=======================================================================
0246
0247 Standard_Boolean Bnd_B3x::IsOut (const Bnd_B3x& theBox,
0248 const gp_Trsf& theTrsf) const
0249 {
0250 Standard_Boolean aResult (Standard_False);
0251 const gp_TrsfForm aForm = theTrsf.Form();
0252 const Standard_Real aScale = theTrsf.ScaleFactor();
0253 const Standard_Real aScaleAbs = Abs(aScale);
0254 if (aForm == gp_Translation || aForm == gp_Identity ||
0255 aForm == gp_PntMirror || aForm == gp_Scale)
0256 {
0257 aResult =
0258 (Abs (RealType(theBox.myCenter[0]*aScale + theTrsf.TranslationPart().X())
0259 - myCenter[0])
0260 > RealType (theBox.myHSize[0]*aScaleAbs) + myHSize[0] ||
0261 Abs (RealType(theBox.myCenter[1]*aScale + theTrsf.TranslationPart().Y())
0262 - myCenter[1])
0263 > RealType (theBox.myHSize[1]*aScaleAbs) + myHSize[1] ||
0264 Abs (RealType(theBox.myCenter[2]*aScale + theTrsf.TranslationPart().Y())
0265 - myCenter[2])
0266 > RealType (theBox.myHSize[2]*aScaleAbs) + myHSize[2]);
0267
0268 }
0269 else {
0270 // theBox is transformed and we check the resulting (enlarged) box against
0271 // 'this' box.
0272 const Standard_Real * aMat = &theTrsf.HVectorialPart().Value(1,1);
0273
0274 gp_XYZ aCenter ((Standard_Real)theBox.myCenter[0],
0275 (Standard_Real)theBox.myCenter[1],
0276 (Standard_Real)theBox.myCenter[2]);
0277 theTrsf.Transforms (aCenter);
0278 const Standard_Real aDist[3] = {
0279 aCenter.X() - (Standard_Real)myCenter[0],
0280 aCenter.Y() - (Standard_Real)myCenter[1],
0281 aCenter.Z() - (Standard_Real)myCenter[2]
0282 };
0283 const Standard_Real aMatAbs[9] = {
0284 Abs(aMat[0]), Abs(aMat[1]), Abs(aMat[2]), Abs(aMat[3]), Abs(aMat[4]),
0285 Abs(aMat[5]), Abs(aMat[6]), Abs(aMat[7]), Abs(aMat[8])
0286 };
0287 if (Abs(aDist[0]) > (aScaleAbs*(aMatAbs[0]*theBox.myHSize[0]+
0288 aMatAbs[1]*theBox.myHSize[1]+
0289 aMatAbs[2]*theBox.myHSize[2]) +
0290 (Standard_Real)myHSize[0]) ||
0291 Abs(aDist[1]) > (aScaleAbs*(aMatAbs[3]*theBox.myHSize[0]+
0292 aMatAbs[4]*theBox.myHSize[1]+
0293 aMatAbs[5]*theBox.myHSize[2]) +
0294 (Standard_Real)myHSize[1]) ||
0295 Abs(aDist[2]) > (aScaleAbs*(aMatAbs[6]*theBox.myHSize[0]+
0296 aMatAbs[7]*theBox.myHSize[1]+
0297 aMatAbs[8]*theBox.myHSize[2]) +
0298 (Standard_Real)myHSize[2]))
0299 aResult = Standard_True;
0300
0301 else {
0302 // theBox is rotated, scaled and translated. We apply the reverse
0303 // translation and scaling then check against the rotated box 'this'
0304 if ((Abs(aMat[0]*aDist[0]+aMat[3]*aDist[1]+aMat[6]*aDist[2])
0305 > theBox.myHSize[0]*aScaleAbs + (aMatAbs[0]*myHSize[0] +
0306 aMatAbs[3]*myHSize[1] +
0307 aMatAbs[6]*myHSize[2])) ||
0308 (Abs(aMat[1]*aDist[0]+aMat[4]*aDist[1]+aMat[7]*aDist[2])
0309 > theBox.myHSize[1]*aScaleAbs + (aMatAbs[1]*myHSize[0] +
0310 aMatAbs[4]*myHSize[1] +
0311 aMatAbs[7]*myHSize[2])) ||
0312 (Abs(aMat[2]*aDist[0]+aMat[5]*aDist[1]+aMat[8]*aDist[2])
0313 > theBox.myHSize[2]*aScaleAbs + (aMatAbs[2]*myHSize[0] +
0314 aMatAbs[5]*myHSize[1] +
0315 aMatAbs[8]*myHSize[2])))
0316 aResult = Standard_True;
0317 }
0318 }
0319 return aResult;
0320 }
0321
0322 //=======================================================================
0323 //function : IsOut
0324 //purpose :
0325 //=======================================================================
0326
0327 Standard_Boolean Bnd_B3x::IsOut (const gp_Ax3& thePlane) const
0328 {
0329 if (IsVoid())
0330 return Standard_True;
0331 const gp_XYZ& anOrigin = thePlane.Location().XYZ();
0332 const gp_XYZ& aDir = thePlane.Direction().XYZ();
0333 const gp_XYZ aBoxCenter ((Standard_Real)myCenter[0],
0334 (Standard_Real)myCenter[1],
0335 (Standard_Real)myCenter[2]);
0336 const Standard_Real aDist0 = (aBoxCenter - anOrigin) * aDir;
0337 // Find the signed distances from two opposite corners of the box to the plane
0338 // If the distances are not the same sign, then the plane crosses the box
0339 const Standard_Real aDist1 = // proj of HSize on aDir
0340 Standard_Real(myHSize[0]) * Abs(aDir.X()) +
0341 Standard_Real(myHSize[1]) * Abs(aDir.Y()) +
0342 Standard_Real(myHSize[2]) * Abs(aDir.Z());
0343 return ((aDist0 + aDist1) * (aDist0 - aDist1) > 0.);
0344 }
0345
0346 //=======================================================================
0347 //function : IsOut
0348 //purpose :
0349 //=======================================================================
0350
0351 Standard_Boolean Bnd_B3x::IsOut (const gp_Ax1& theLine,
0352 const Standard_Boolean isRay,
0353 const Standard_Real theOverthickness) const
0354 {
0355 const Standard_Real aRes = gp::Resolution() * 100.;
0356 if (IsVoid())
0357 return Standard_True;
0358 Standard_Real
0359 anInter0[2] = {-RealLast(), RealLast()},
0360 anInter1[2] = {-RealLast(), RealLast()};
0361 const gp_XYZ& aDir = theLine.Direction().XYZ();
0362 const gp_XYZ aDiff ((Standard_Real)myCenter[0] - theLine.Location().X(),
0363 (Standard_Real)myCenter[1] - theLine.Location().Y(),
0364 (Standard_Real)myCenter[2] - theLine.Location().Z());
0365
0366 // Find the parameter interval in X dimension
0367 Standard_Real aHSize = (Standard_Real)myHSize[0]+theOverthickness;
0368 if (aDir.X() > aRes) {
0369 anInter0[0]= (aDiff.X() - aHSize) / aDir.X();
0370 anInter0[1]= (aDiff.X() + aHSize) / aDir.X();
0371 } else if (aDir.X() < -aRes) {
0372 anInter0[0]= (aDiff.X() + aHSize) / aDir.X();
0373 anInter0[1]= (aDiff.X() - aHSize) / aDir.X();
0374 } else
0375 // the line is orthogonal to OX axis. Test for inclusion in box limits
0376 if (Abs(aDiff.X()) > aHSize)
0377 return Standard_True;
0378
0379 // Find the parameter interval in Y dimension
0380 aHSize = (Standard_Real)myHSize[1]+theOverthickness;
0381 if (aDir.Y() > aRes) {
0382 anInter1[0]= (aDiff.Y() - aHSize) / aDir.Y();
0383 anInter1[1]= (aDiff.Y() + aHSize) / aDir.Y();
0384 } else if (aDir.Y() < -aRes) {
0385 anInter1[0]= (aDiff.Y() + aHSize) / aDir.Y();
0386 anInter1[1]= (aDiff.Y() - aHSize) / aDir.Y();
0387 } else
0388 // the line is orthogonal to OY axis. Test for inclusion in box limits
0389 if (Abs(aDiff.Y()) > aHSize)
0390 return Standard_True;
0391
0392 // Intersect Y-interval with X-interval
0393 if (anInter0[0] > (anInter1[1] + aRes) || anInter0[1] < (anInter1[0] - aRes))
0394 return Standard_True;
0395 if (anInter1[0] > anInter0[0])
0396 anInter0[0] = anInter1[0];
0397 if (anInter1[1] < anInter0[1])
0398 anInter0[1] = anInter1[1];
0399 if (isRay && anInter0[1] < -aRes)
0400 return Standard_True;
0401
0402 // Find the parameter interval in Z dimension
0403 aHSize = (Standard_Real)myHSize[2]+theOverthickness;
0404 if (aDir.Z() > aRes) {
0405 anInter1[0]= (aDiff.Z() - aHSize) / aDir.Z();
0406 anInter1[1]= (aDiff.Z() + aHSize) / aDir.Z();
0407 } else if (aDir.Z() < -aRes) {
0408 anInter1[0]= (aDiff.Z() + aHSize) / aDir.Z();
0409 anInter1[1]= (aDiff.Z() - aHSize) / aDir.Z();
0410 } else
0411 // the line is orthogonal to OZ axis. Test for inclusion in box limits
0412 return (Abs(aDiff.Z()) > aHSize);
0413 if (isRay && anInter1[1] < -aRes)
0414 return Standard_True;
0415
0416 return (anInter0[0] > (anInter1[1]+aRes) || anInter0[1] < (anInter1[0]-aRes));
0417 }
0418
0419 //=======================================================================
0420 //function : IsIn
0421 //purpose : Test the complete inclusion of this box in transformed theOtherBox
0422 //=======================================================================
0423
0424 Standard_Boolean Bnd_B3x::IsIn (const Bnd_B3x& theBox,
0425 const gp_Trsf& theTrsf) const
0426 {
0427 Standard_Boolean aResult (Standard_False);
0428 const gp_TrsfForm aForm = theTrsf.Form();
0429 const Standard_Real aScale = theTrsf.ScaleFactor();
0430 const Standard_Real aScaleAbs = Abs(aScale);
0431 if (aForm == gp_Translation || aForm == gp_Identity ||
0432 aForm == gp_PntMirror || aForm == gp_Scale)
0433 {
0434 aResult =
0435 (Abs (RealType(theBox.myCenter[0]*aScale + theTrsf.TranslationPart().X())
0436 - myCenter[0])
0437 < RealType (theBox.myHSize[0]*aScaleAbs) - myHSize[0] &&
0438 Abs (RealType(theBox.myCenter[1]*aScale + theTrsf.TranslationPart().Y())
0439 - myCenter[1])
0440 < RealType (theBox.myHSize[1]*aScaleAbs) - myHSize[1] &&
0441 Abs (RealType(theBox.myCenter[2]*aScale + theTrsf.TranslationPart().Y())
0442 - myCenter[2])
0443 < RealType (theBox.myHSize[2]*aScaleAbs) - myHSize[2]);
0444
0445 } else {
0446 // theBox is rotated, scaled and translated. We apply the reverse
0447 // translation and scaling then check against the rotated box 'this'
0448 const Standard_Real * aMat = &theTrsf.HVectorialPart().Value(1,1);
0449 gp_XYZ aCenter ((Standard_Real)theBox.myCenter[0],
0450 (Standard_Real)theBox.myCenter[1],
0451 (Standard_Real)theBox.myCenter[2]);
0452 theTrsf.Transforms (aCenter);
0453 const Standard_Real aDist[3] = {
0454 aCenter.X() - (Standard_Real)myCenter[0],
0455 aCenter.Y() - (Standard_Real)myCenter[1],
0456 aCenter.Z() - (Standard_Real)myCenter[2]
0457 };
0458 if ((Abs(aMat[0]*aDist[0]+aMat[3]*aDist[1]+aMat[6]*aDist[2])
0459 < theBox.myHSize[0]*aScaleAbs - (Abs(aMat[0])*myHSize[0] +
0460 Abs(aMat[3])*myHSize[1] +
0461 Abs(aMat[6])*myHSize[2])) &&
0462 (Abs(aMat[1]*aDist[0]+aMat[4]*aDist[1]+aMat[7]*aDist[2])
0463 < theBox.myHSize[1]*aScaleAbs - (Abs(aMat[1])*myHSize[0] +
0464 Abs(aMat[4])*myHSize[1] +
0465 Abs(aMat[7])*myHSize[2])) &&
0466 (Abs(aMat[2]*aDist[0]+aMat[5]*aDist[1]+aMat[8]*aDist[2])
0467 < theBox.myHSize[2]*aScaleAbs - (Abs(aMat[2])*myHSize[0] +
0468 Abs(aMat[5])*myHSize[1] +
0469 Abs(aMat[8])*myHSize[2])))
0470 aResult = Standard_True;
0471 }
0472 return aResult;
0473 }
0474