Defining a rectangular grid in $3D$ space.

529 Views Asked by At

I am implementing a ray tracer. For that I need to make an image plane, which is basically a rectangular grid.

I have been given two points i.e.

  • top-left $(A)$
  • bottom-right $(B)$

and a normal vector (to uniquely identify the rectangle in $3D$ space) as shown here. enter image description here

Now I want to find vectors $\bf{C-A}$ and $\bf{D-A}$ so that I can calculate the co-ordinates of center of pixels, as shown here. enter image description here

The problem is that I am not able to find the points $C$ and $D$. I have calculated the equation of plane (of the form $\bf{ax + by + cz = d}$), and have also formulated the following constraints (equations).

  1. Vector $\bf{(D-C)}$ is perpendicular to Vector $\bf{(B-A)}$
  2. $a\cdot C_x + b\cdot C_y + c\cdot C_z = d$ (to make sure point $C$ lies on plane)
  3. $a\cdot D_x + b\cdot D_y + c\cdot D_z = d$ (to make sure point $D$ lies on plane)
  4. $\frac{Norm\bf{(C-A)}}{Norm\bf{(D-A)}}$ = Aspect_Ratio (to preserve the aspect ratio)
  5. Vector $\bf{(D-A)}$= Vector $\bf{(B-C)}$

The unknowns are $C (x,y,z)$ and $D (x,y,z)$. I am stuck here any help is much appreciated. Thanks.

IMAGE EDIT enter image description here

2

There are 2 best solutions below

7
On

If you have the vectors $\mathbf{B}-\mathbf{A}$ (which obviously lies in the plane) and a normal vector to the plane $\mathbf{n}$, you can construct another vector in the plane by simply taking the cross product $(\mathbf{B}-\mathbf{A}) \times \mathbf{n}$. These two define a coordinate system in the plane, which you can then use to work out $\mathbf{C-A}$ and $\mathbf{D-A}$, or equivalently the coordinates of C and D. One way to do that for C is to use the Pythagorean theorem (I use $|.|$ to denote the norm) $$ |\mathbf{B-A}|^2 = |\mathbf{C-A}|^2 + |\mathbf{B-C}|^2 $$ together with $|\mathbf{C-A}|/|\mathbf{B-C}| = \text{Aspect_ratio}$, which gives you both $|\mathbf{C-A}|$ and $|\mathbf{B-C}|$. From there you can determine the coordinates of C in the coordinate system we set up. D works analogously.

EDIT:

Let's say you construct a new orthonormal basis $\mathbf{b}_1 = \mathbf{B}-\mathbf{A}/|\mathbf{B}-\mathbf{A}|$, $\mathbf{b}_2 = (\mathbf{B}-\mathbf{A}) \times n/|(\mathbf{B}-\mathbf{A}) \times n|$ for the plane and fixed the origin of the plane to A. You then determine the coordinates $C_1, C_2$ and $D_1, D_2$ of the points C and D in the plane, such that $\mathbf{C}-\mathbf{A} = C_1 \mathbf{b}_1 + C_2 \mathbf{b}_2$ and $\mathbf{D}-\mathbf{A} = D_1 \mathbf{b}_1 + D_2 \mathbf{b}_2$. Then you can solve for $\mathbf{C} = \mathbf{A} + C_1 \mathbf{b}_1 + C_2 \mathbf{b}_2$ and $\mathbf{D} = \mathbf{A} + D_1 \mathbf{b}_1 + D_2 \mathbf{b}_2$, and use the representation of $\mathbf{b}_1, \mathbf{b}_2, \mathbf{A}$ in the $x,y,z$-basis.

IMAGE EDIT: enter image description here

Given $a=|\mathbf{B}-\mathbf{A}|$, $b=|\mathbf{C}-\mathbf{A}|$ and $c=|\mathbf{B}-\mathbf{C}|$, $C_1$ and $C_2$ are given by $$ C_1 = a \, \cos(\phi) \\ C_2 = a \, \sin(\phi), $$ where $\phi$ is the angle between $\mathbf{B}-\mathbf{A}$ and $\mathbf{C}-\mathbf{A}$. Therefore $$ \phi = \arctan\left(\frac{c}{b}\right). $$

0
On

Your description of the parameters that you have to work with is a bit inconsistent and missing some important information, but I think that the necessary bits can be teased out of it.

First of all, you haven’t described the orientation of the rectangle with respect to the normal. It appears in none of the three illustrations, which show only the plane of the rectangle, either. There is, however, a clue in the third diagram: $(B-A)\times\mathbf n$ is clockwise from $B-A$, so we can guess that $\mathbf n$ points out of the page. I’ll proceed with this assumption.

Secondly, a single diagonal of a rectangle is not enough to specify the rectagle: the other diagonal could be any other diameter of the circle that has the given segment as one of its diameters. In fact, I’ll be using this in the derivation below. Fortunately, it looks like you do know the rectangle’s aspect ratio, which I’ll designate by $\rho$.

We’ll find the two vectors that you need by direct computation. The basic strategy is to find a rotation about the axis with direction $\mathbf n$ through the midpoint of $AB$ that takes $A$ to $C$. The aspect ratio $\rho$ is equal to $\cot\alpha$, where $\alpha=\operatorname{m}{\angle{BAC}}$. $\triangle{AMC}$ is isosceles, therefore $\theta = \operatorname{m}{\angle{AMC}} = \pi-2\alpha$ is the required rotation angle. We don’t really care about this angle’s specific value, though. We really only need its sine and cosine: $$\cos\theta = \cos(\pi-2\alpha) = -\cos{2\alpha} = \sin^2\alpha-\cos^2\alpha = {1-\rho^2\over1+\rho^2}$$ and $$\sin\theta = sin(\pi-2\alpha) = \sin{2\alpha} = 2\cos\alpha\sin\alpha = {2\rho\over1+\rho^2}.$$

Now let $M$ be the midpoint of $AB$ and translate the origin to $M$, so that we have $A'=A-M$ and $B'=B-M$, and normalize $\mathbf n$ if necessary. We want a clockwise rotation, so will be rotating through an angle of $-\theta$. Rodrigues’ rotation formula gives $$\begin{align} C' &= A'\cos\theta - (\mathbf n\times A')\sin\theta +\mathbf n(\mathbf n\cdot A')(1-\cos\theta) \\ &= {1-\rho^2\over1+\rho^2}A' - {2\rho\over1+\rho^2}(\mathbf n\times A') + {2\rho^2(\mathbf n\cdot A')\over1+\rho^2}\mathbf n.\end{align}$$ Finally, your two basis vectors for the grid are $C'-A'$ and $B'-C'$.

Incidentally, this seems like a rather inconvenient way to represent a rectangle. Storing $C-A$ requires no more space than does storing $B$, and $B$ is more easily and efficiently recovered from this data than is $C$.