| 1 |
# Copyright(C) 1999-2020 National Technology & Engineering Solutions |
| 2 |
# of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
# NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions are |
| 7 |
# met: |
| 8 |
# |
| 9 |
# * Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# * Redistributions in binary form must reproduce the above |
| 13 |
# copyright notice, this list of conditions and the following |
| 14 |
# disclaimer in the documentation and/or other materials provided |
| 15 |
# with the distribution. |
| 16 |
# |
| 17 |
# * Neither the name of NTESS nor the names of its |
| 18 |
# contributors may be used to endorse or promote products derived |
| 19 |
# from this software without specific prior written permission. |
| 20 |
# |
| 21 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
|
| 33 |
#from phactori import * |
| 34 |
import math |
| 35 |
from .PhactoriVectorLibrary import * |
| 36 |
from phactori import myDebugPrint3AndException |
| 37 |
|
| 38 |
#phactori_combine_to_single_python_file_subpiece_begin_1 |
| 39 |
class PhactoriSegment: |
| 40 |
def __init__(self): |
| 41 |
self.ptA = None |
| 42 |
self.ptB = None |
| 43 |
self.AtoB = None |
| 44 |
self.lengthSquared = None |
| 45 |
self.oneOverLenSqrd = None |
| 46 |
self.boundingBox = None |
| 47 |
|
| 48 |
def SetPoints(self, ptA, ptB): |
| 49 |
self.ptA = list(ptA) |
| 50 |
self.ptB = list(ptB) |
| 51 |
self.AtoB = vecFromAToB(self.ptA, self.ptB) |
| 52 |
self.lengthSquared = vecMagnitudeSquared(self.AtoB) |
| 53 |
if self.lengthSquared == 0.0: |
| 54 |
myDebugPrint3AndException("PhactoriSegment:SetPoints self.lengthSquared is 0.0\n") |
| 55 |
self.oneOverLenSqrd = 1.0 / self.lengthSquared |
| 56 |
|
| 57 |
self.boundingBox = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] |
| 58 |
if self.ptA[0] <= self.ptB[0]: |
| 59 |
self.boundingBox[0] = ptA[0] |
| 60 |
self.boundingBox[1] = ptB[0] |
| 61 |
else: |
| 62 |
self.boundingBox[0] = ptB[0] |
| 63 |
self.boundingBox[1] = ptA[0] |
| 64 |
if self.ptA[1] <= self.ptB[1]: |
| 65 |
self.boundingBox[2] = ptA[1] |
| 66 |
self.boundingBox[3] = ptB[1] |
| 67 |
else: |
| 68 |
self.boundingBox[2] = ptB[1] |
| 69 |
self.boundingBox[3] = ptA[1] |
| 70 |
if self.ptA[2] <= self.ptB[2]: |
| 71 |
self.boundingBox[4] = ptA[2] |
| 72 |
self.boundingBox[5] = ptB[2] |
| 73 |
else: |
| 74 |
self.boundingBox[4] = ptB[2] |
| 75 |
self.boundingBox[5] = ptA[2] |
| 76 |
|
| 77 |
#if PhactoriDbg(100): |
| 78 |
# myDebugPrint3("PhactoriSegment:SetPoints\n" +\ |
| 79 |
# str(self.ptA) + "\n" + \ |
| 80 |
# str(self.ptB) + "\n" + \ |
| 81 |
# str(self.AtoB) + "\n" + \ |
| 82 |
# str(self.lengthSquared) + "\n" + \ |
| 83 |
# str(self.oneOverLenSqrd) + "\n") |
| 84 |
|
| 85 |
def FindDistanceToPoint(self, testPoint): |
| 86 |
return math.sqrt(self.FindDistanceSquaredToPoint(testPoint)) |
| 87 |
|
| 88 |
def FindDistanceToPointProjected(self, testPoint, projectionAxis): |
| 89 |
return math.sqrt(self.FindDistanceSquaredToPointProjected(testPoint, projectionAxis)) |
| 90 |
|
| 91 |
def FindNearestPointOnSegmentToPoint(self, testPoint): |
| 92 |
if self.lengthSquared == 0.0: |
| 93 |
return list(self.ptA) |
| 94 |
AtoTest = vecFromAToB(self.ptA, testPoint) |
| 95 |
tt = vecDotProduct(AtoTest, self.AtoB) * self.oneOverLenSqrd |
| 96 |
if tt <= 0.0: |
| 97 |
return list(self.ptA) |
| 98 |
elif tt >= 1.0: |
| 99 |
return list(self.ptB) |
| 100 |
else: |
| 101 |
testProjectedToSegment = vecMultiplyAdd(self.ptA, self.AtoB, tt) |
| 102 |
return testProjectedToSegment |
| 103 |
|
| 104 |
def ProjectPointOntoPlane(self, pointToChange, projectionAxis): |
| 105 |
if projectionAxis == 0: |
| 106 |
pointToChange[0] = 0.0 |
| 107 |
elif projectionAxis == 1: |
| 108 |
pointToChange[1] = 0.0 |
| 109 |
elif projectionAxis == 2: |
| 110 |
pointToChange[2] = 0.0 |
| 111 |
|
| 112 |
def FindNearestPointOnSegmentToPointProjected(self, testPoint, projectionAxis): |
| 113 |
retPoint = self.FindNearestPointOnSegmentToPoint(testPoint) |
| 114 |
self.ProjectPointOntoPlane(retPoint, projectionAxis) |
| 115 |
return retPoint |
| 116 |
|
| 117 |
def FindDistanceSquaredToPoint(self, testPoint): |
| 118 |
testPointProjectedOntoSegment = self.FindNearestPointOnSegmentToPoint(testPoint) |
| 119 |
return vecDistanceSquared(testPoint, testPointProjectedOntoSegment) |
| 120 |
|
| 121 |
def FindDistanceSquaredToPointProjected(self, testPoint, projectionAxis): |
| 122 |
testPointProjectedOntoSegment = self.FindNearestPointOnSegmentToPointProjected(testPoint, projectionAxis) |
| 123 |
localTestPoint = list(testPoint) |
| 124 |
self.ProjectPointOntoPlane(localTestPoint, projectionAxis) |
| 125 |
return vecDistanceSquared(localTestPoint, testPointProjectedOntoSegment) |
| 126 |
|
| 127 |
def MyBoundingBoxIntersectsTargetBoundingBox(self, testBoundingBox): |
| 128 |
if self.boundingBox[0] > testBoundingBox[1]: |
| 129 |
return False |
| 130 |
if self.boundingBox[1] < testBoundingBox[0]: |
| 131 |
return False |
| 132 |
if self.boundingBox[2] > testBoundingBox[3]: |
| 133 |
return False |
| 134 |
if self.boundingBox[3] < testBoundingBox[2]: |
| 135 |
return False |
| 136 |
if self.boundingBox[4] > testBoundingBox[5]: |
| 137 |
return False |
| 138 |
if self.boundingBox[5] < testBoundingBox[4]: |
| 139 |
return False |
| 140 |
return True |
| 141 |
|
| 142 |
def EitherEndpointIsInBoundingBox(self, testBoundingBox): |
| 143 |
ptIsIn = True |
| 144 |
if (self.ptA[0] < testBoundingBox[0]): |
| 145 |
ptIsIn = False |
| 146 |
if (self.ptA[0] > testBoundingBox[1]): |
| 147 |
ptIsIn = False |
| 148 |
if (self.ptA[1] < testBoundingBox[2]): |
| 149 |
ptIsIn = False |
| 150 |
if (self.ptA[1] > testBoundingBox[3]): |
| 151 |
ptIsIn = False |
| 152 |
if (self.ptA[2] < testBoundingBox[4]): |
| 153 |
ptIsIn = False |
| 154 |
if (self.ptA[2] > testBoundingBox[5]): |
| 155 |
ptIsIn = False |
| 156 |
if ptIsIn == True: |
| 157 |
return True |
| 158 |
ptIsIn = True |
| 159 |
if (self.ptB[0] < testBoundingBox[0]): |
| 160 |
ptIsIn = False |
| 161 |
if (self.ptB[0] > testBoundingBox[1]): |
| 162 |
ptIsIn = False |
| 163 |
if (self.ptB[1] < testBoundingBox[2]): |
| 164 |
ptIsIn = False |
| 165 |
if (self.ptB[1] > testBoundingBox[3]): |
| 166 |
ptIsIn = False |
| 167 |
if (self.ptB[2] < testBoundingBox[4]): |
| 168 |
ptIsIn = False |
| 169 |
if (self.ptB[2] > testBoundingBox[5]): |
| 170 |
ptIsIn = False |
| 171 |
if ptIsIn == True: |
| 172 |
return True |
| 173 |
return False |
| 174 |
|
| 175 |
def IntersectsXYRectangle(self, minU, maxU, minV, maxV, posW): |
| 176 |
#myDebugPrint3("IntersectsXYRectangle entered\n") |
| 177 |
#myDebugPrint3(str([minU, maxU, minV, maxV, posW]) + "\n") |
| 178 |
#myDebugPrint3(str(self.boundingBox) + "\n") |
| 179 |
if self.boundingBox[4] > posW: |
| 180 |
return False |
| 181 |
if self.boundingBox[5] < posW: |
| 182 |
return False |
| 183 |
tt = (posW - self.ptA[2])/(self.ptB[2] - self.ptA[2]) |
| 184 |
#myDebugPrint3(str(tt) + "\n") |
| 185 |
uIntersect = self.ptA[0] + tt * (self.ptB[0] - self.ptA[0]) |
| 186 |
#myDebugPrint3(str(uIntersect) + "\n") |
| 187 |
if uIntersect < minU: |
| 188 |
return False |
| 189 |
if uIntersect > maxU: |
| 190 |
return False |
| 191 |
vIntersect = self.ptA[1] + tt * (self.ptB[1] - self.ptA[1]) |
| 192 |
#myDebugPrint3(str(vIntersect) + "\n") |
| 193 |
if vIntersect < minV: |
| 194 |
return False |
| 195 |
if vIntersect > maxV: |
| 196 |
return False |
| 197 |
return True |
| 198 |
|
| 199 |
def IntersectsXZRectangle(self, minU, maxU, minV, maxV, posW): |
| 200 |
if self.boundingBox[2] > posW: |
| 201 |
return False |
| 202 |
if self.boundingBox[3] < posW: |
| 203 |
return False |
| 204 |
tt = (posW - self.ptA[1])/(self.ptB[1] - self.ptA[1]) |
| 205 |
uIntersect = self.ptA[0] + tt * (self.ptB[0] - self.ptA[0]) |
| 206 |
if uIntersect < minU: |
| 207 |
return False |
| 208 |
if uIntersect > maxU: |
| 209 |
return False |
| 210 |
vIntersect = self.ptA[2] + tt * (self.ptB[2] - self.ptA[2]) |
| 211 |
if vIntersect < minV: |
| 212 |
return False |
| 213 |
if vIntersect > maxV: |
| 214 |
return False |
| 215 |
return True |
| 216 |
|
| 217 |
def IntersectsYZRectangle(self, minU, maxU, minV, maxV, posW): |
| 218 |
if self.boundingBox[0] > posW: |
| 219 |
return False |
| 220 |
if self.boundingBox[1] < posW: |
| 221 |
return False |
| 222 |
tt = (posW - self.ptA[0])/(self.ptB[0] - self.ptA[0]) |
| 223 |
uIntersect = self.ptA[1] + tt * (self.ptB[1] - self.ptA[1]) |
| 224 |
if uIntersect < minU: |
| 225 |
return False |
| 226 |
if uIntersect > maxU: |
| 227 |
return False |
| 228 |
vIntersect = self.ptA[2] + tt * (self.ptB[2] - self.ptA[2]) |
| 229 |
if vIntersect < minV: |
| 230 |
return False |
| 231 |
if vIntersect > maxV: |
| 232 |
return False |
| 233 |
return True |
| 234 |
|
| 235 |
def IntersectsBoundingBox(self, testBoundingBox): |
| 236 |
#myDebugPrint3("PhactoriSegment.IntersectsBoundingBox entered\n") |
| 237 |
#myDebugPrint3("ptA: " + str(self.ptA) + "\n") |
| 238 |
#myDebugPrint3("ptB: " + str(self.ptB) + "\n") |
| 239 |
#myDebugPrint3("box: " + str(testBoundingBox) + "\n") |
| 240 |
|
| 241 |
if self.MyBoundingBoxIntersectsTargetBoundingBox(testBoundingBox) == False: |
| 242 |
#myDebugPrint3("PhactoriSegment.IntersectsBoundingBox returning false due to bounding box test\n") |
| 243 |
return False |
| 244 |
|
| 245 |
if self.EitherEndpointIsInBoundingBox(testBoundingBox) == True: |
| 246 |
#myDebugPrint3("PhactoriSegment.IntersectsBoundingBox returning false due to endpoint in box test\n") |
| 247 |
return True |
| 248 |
|
| 249 |
#myDebugPrint3("PhactoriSegment.IntersectsBoundingBox testing against all six sides\n") |
| 250 |
if self.IntersectsXYRectangle(testBoundingBox[0], testBoundingBox[1], testBoundingBox[2], testBoundingBox[3], testBoundingBox[4]): |
| 251 |
return True |
| 252 |
if self.IntersectsXYRectangle(testBoundingBox[0], testBoundingBox[1], testBoundingBox[2], testBoundingBox[3], testBoundingBox[5]): |
| 253 |
return True |
| 254 |
if self.IntersectsXZRectangle(testBoundingBox[0], testBoundingBox[1], testBoundingBox[4], testBoundingBox[5], testBoundingBox[2]): |
| 255 |
return True |
| 256 |
if self.IntersectsXZRectangle(testBoundingBox[0], testBoundingBox[1], testBoundingBox[4], testBoundingBox[5], testBoundingBox[3]): |
| 257 |
return True |
| 258 |
if self.IntersectsYZRectangle(testBoundingBox[2], testBoundingBox[3], testBoundingBox[4], testBoundingBox[5], testBoundingBox[4]): |
| 259 |
return True |
| 260 |
if self.IntersectsYZRectangle(testBoundingBox[2], testBoundingBox[3], testBoundingBox[4], testBoundingBox[5], testBoundingBox[5]): |
| 261 |
return True |
| 262 |
|
| 263 |
#myDebugPrint3("PhactoriSegment.IntersectsBoundingBox returning false, no side intersection\n") |
| 264 |
return False |
| 265 |
|
| 266 |
def IntersectsBoundingBoxProjected(self, boundingBox, projectionAxis): |
| 267 |
#myDebugPrint3("PhactoriSegment.IntersectsBoundingBoxProjected entered\n") |
| 268 |
if (projectionAxis < 0) or (projectionAxis > 2): |
| 269 |
return self.IntersectsBoundingBox(boundingBox) |
| 270 |
|
| 271 |
#inefficient but simple method of handling projection: make projected |
| 272 |
#bounding box (rectangle) and projected segment and do 3d version |
| 273 |
localProjectedPointA = list(self.ptA) |
| 274 |
localProjectedPointB = list(self.ptB) |
| 275 |
localProjectedBoundingBox = list(boundingBox) |
| 276 |
localProjectedPointA[projectionAxis] = 0.0 |
| 277 |
localProjectedPointB[projectionAxis] = 0.0 |
| 278 |
localProjectedBoundingBox[2*projectionAxis] = 0.0 |
| 279 |
localProjectedBoundingBox[2*projectionAxis+1] = 0.0 |
| 280 |
localProjectedSegment = PhactoriSegment() |
| 281 |
localProjectedSegment.SetPoints(localProjectedPointA, localProjectedPointB) |
| 282 |
intersectionFlag = localProjectedSegment.IntersectsBoundingBox(localProjectedBoundingBox) |
| 283 |
return intersectionFlag |
| 284 |
|
| 285 |
#phactori_combine_to_single_python_file_subpiece_end_1 |
| 286 |
|