Created
November 14, 2021 20:28
-
-
Save iam4722202468/98008bd128b5c48384fa573d07d16827 to your computer and use it in GitHub Desktop.
Check for intersection with bounding boxes and ray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import math | |
vec = (1.3*4, -2.7*4, 0) | |
start = (1,1,0) | |
bb = (2,-3, -1) | |
bbSize = (1,1,2) | |
xStep = 1 | |
yStep = 1 | |
zStep = 1 | |
subX = 0 | |
subY = 0 | |
subZ = 0 | |
if vec[0] > 0: | |
subX = 1 | |
if vec[1] > 0: | |
subY = 1 | |
if vec[2] > 0: | |
subZ = 1 | |
if vec[0] < 0: xStep = -1 | |
if vec[1] < 0: yStep = -1 | |
if vec[2] < 0: zStep = -1 | |
# Calculate intersection planes | |
xPlanes = list(np.arange(0, vec[0], xStep)) + [vec[0]] | |
yPlanes = list(np.arange(0, vec[1], yStep)) + [vec[1]] | |
zPlanes = list(np.arange(0, vec[2], zStep)) + [vec[2]] | |
intercepts = [] | |
if vec[0] != 0: | |
for xi in xPlanes: | |
factor = xi/vec[0] | |
yi = vec[1] * factor | |
zi = vec[2] * factor | |
intercepts.append((math.floor(xi-subX+start[0]),math.floor(yi+start[1]),math.floor(zi+start[2]))) | |
if vec[1] != 0: | |
for yi in yPlanes: | |
factor = yi/vec[1] | |
xi = vec[0] * factor | |
zi = vec[2] * factor | |
intercepts.append((math.floor(xi+start[0]),math.floor(yi-subY+start[1]),math.floor(zi+start[2]))) | |
if vec[2] != 0: | |
for zi in zPlanes: | |
factor = zi/vec[2] | |
xi = vec[0] * factor | |
yi = vec[1] * factor | |
intercepts.append((math.floor(xi+start[0]),math.floor(yi+start[1]),math.floor(zi-subZ+start[2]))) | |
# Translate bounding box | |
bbOff = (bb[0] - start[0], bb[1] - start[1], bb[2] - start[2]) | |
# Intersect X | |
if vec[0] != 0: | |
xFac = bbOff[0] / vec[0] | |
yix = vec[1] * xFac + start[1] | |
zix = vec[2] * xFac + start[2] | |
if (yix > bb[1] and yix < bb[1] + bbSize[1] and zix > bb[2] and zix < bb[2] + bbsize[2]): | |
print("Intercept at (" + str(bbOff[0] + start[0]) + ", " + str(yix) + ", " + str(yix) + ")") | |
# Intersect Y | |
if vec[1] != 0: | |
yFac = bbOff[1] / vec[1] | |
xiy = vec[0] * yFac + start[0] | |
ziy = vec[2] * yFac + start[2] | |
if (xiy > bb[0] and xiy < bb[0] + bbSize[0] and ziy > bb[2] and ziy < bb[2] + bbSize[2]): | |
print("Intercept at (" + str(xiy) + ", " + str(bbOff[1] + start[1]) + ", " + str(ziy) + ")") | |
# Intersect Z | |
if vec[2] != 0: | |
zFac = bbOff[2] / vec[2] | |
xiz = vec[0] * zFac + start[0] | |
yiz = vec[1] * zFac + start[1] | |
if (xiz > bb[0] and xiz < bb[0] + bbSize[0] and yiz > bb[1] and yiz < bb[1] + bbSize[1]): | |
print("Intercept at (" + str(xiz) + ", " + str(yiz) + ", " + str(bbOff[2] + start[2]) + ")") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment