2

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

Rohit Singh
  • 1,143
  • It’s easy enough to find a suitable pair of vectors to define a rectangular grid on the plane, but if you want it to be aligned with the edges of the image rectangle, you need more information. The endpoints of a diagonal are not enough to identify a rectangle uniquely. – amd Jul 12 '19 at 19:30
  • @amd can you elaborate, maybe through an illustration, how the endpoints of a diagonal and a normal do not uniquely identify a rectangle? – wsaleem Jul 14 '19 at 16:20
  • I am not sure constraint 1 is valid. – wsaleem Jul 14 '19 at 16:22
  • it is necessary condition for diagonals to be perpandicular in a rectangle. i.e. both (daigonal) vectors have dot product = 0. – Ateeb Ahmed Jul 14 '19 at 16:58
  • 1
    That cannot be true. Diagonals are perpendicular in a square. The square can be stretched arbitrarily to form arbitrary rectangles. The angle between the diagonals cannot remain 90 for all such rectangles. – wsaleem Jul 15 '19 at 15:06
  • @wsaleem There is an infinite number of rectangles that share a diagonal, even if you restrict them to be coplanar: they can all be inscribed in the circle with the given diagonal as its diameter. – amd Jul 15 '19 at 17:49
  • Aspect_ratio is not one of the givens listed in your first paragraph. Is it indeed one of the pieces of information that you’re given? – amd Jul 15 '19 at 17:52

2 Answers2

1

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). $$

janosch
  • 169
  • To even find the co-ordinates of point C using linear combination of newly created basis, we must know the co-ordinates of C in term of standard basis vectors i.e. {(1,0,0),(0,1,0),(0,0,1)}. could you elaborate a bit more please. – Ateeb Ahmed Jul 12 '19 at 14:04
  • I added a note to the answer, hope this helps :) – janosch Jul 12 '19 at 14:54
  • Sorry but it is getting confusing for me.. Let me tell you what I understood from your answer.

    So after setting up the new orthonormal basis and writing C-A = C1b1 + C2b2.. How do we find out what C1 and C2 is.

    And also what is the point of using pythagorean therom to find the |C-A| and |B-C|. Your help is much appreciated

    – Ateeb Ahmed Jul 13 '19 at 02:02
  • Let's focus on the triangle ABC in the plane. Using the Pythagorean theorem exploits the fact that there is a 90 degree angle at C, and from there you can determine all lengths and angles in the triangle. With that, the position of C in the new coordinate system is fixed, and you can work out the coordiantes $C_1$ and $C_2$. – janosch Jul 13 '19 at 12:00
  • Please refer to the image in the edit of my question.. So my point is, even after finding the values of |B-C| and |C-A| using the pythagorean theorm, one still can't find C1 and C2. If you still think, it is possible to find the co-ordinates of C, kindly provide a pictorial method or example. Thanks. – Ateeb Ahmed Jul 14 '19 at 15:41
  • There are infinitely many pairs, C and D. This can be seen through the animation in the first response to this post. – wsaleem Jul 14 '19 at 16:43
  • @wsaleem There are infinitely many if you only demand a right angle at C and D. If you fix the aspect ratio and also the orientation of the rectangle, there's only one solution left – janosch Jul 15 '19 at 08:30
0

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$.

amd
  • 53,693