Points inside a 3D rectangle

2.7k Views Asked by At

my task is to find, and mark, all the points from a point cloud that are inside a 3D rectangle. I have the coordinates of all the 8 vertices. Is there a mathematical formula which I can use? To be precise, I'm working with python and all the points are inside a np array, so if someone knows a library that can do that, I'm open to suggestions. Thanks a lot.

1

There are 1 best solutions below

1
On BEST ANSWER

(This only applies if the box's edges are aligned with the coordinate axes.)

If the vertices are $(x_1,y_1,z_1),\;(x_2,y_1,z_1),\;(x_1,y_2,z_1),\;\cdots,\;(x_2,y_2,z_2)$, then any point $(x,y,z)$ is inside the box if and only if

$$x_1<x<x_2$$ and $$y_1<y<y_2$$ and $$z_1<z<z_2$$

In other words, the point's coordinates are between the vertices' coordinates.


(This applies regardless of coordinates.)

I'll use the notation $\vec{OA}=\vec A-\vec O$ for a difference of position vectors. $O$ is one vertex, and $A,B,C$ are its three adjacent vertices.

A point $P$ is inside the box if and only if $$\vec O\cdot\vec{OA}<\vec P\cdot\vec{OA}<\vec A\cdot\vec{OA}$$ and $$\vec O\cdot\vec{OB}<\vec P\cdot\vec{OB}<\vec B\cdot\vec{OB}$$ and $$\vec O\cdot\vec{OC}<\vec P\cdot\vec{OC}<\vec C\cdot\vec{OC}$$

This generalizes the previous expression with coordinates.

Equivalently, $\vec{OP}\cdot\vec{OA},\;\vec{OP}\cdot\vec{OB},\;\vec{OP}\cdot\vec{OC},\;\vec{AP}\cdot\vec{AO},\;\vec{BP}\cdot\vec{BO},\;\vec{CP}\cdot\vec{CO}$ are all positive numbers.