© B.J. Korites 2018
B.J. KoritesPython Graphicshttps://doi.org/10.1007/978-1-4842-3378-8_4

4. Perspective

B. J. Korites1 
(1)
Duxbury, Massachusetts, USA
 
I discussed isometric vs. perspective views in the previous chapter. Now you will develop a transformation that will automatically produce a perspective view. It operates much like a camera where rays are traced from the various points that comprise an object onto a plane that you might think of as a film plane. Figure 4-1 shows the geometry. It’s a three-dimensional box in the x,y,z space. The x,y plane represents the film plane. There’s also a focal point that is outside the x,y,z space in front of the x,y plane. Rays are traced from the box’s corners to the focal point. By connecting the points where the rays hit the x,y plane, you can construct a perspective view of the box.
../images/456962_1_En_4_Chapter/456962_1_En_4_Fig1_HTML.jpg
Figure 4-1

Geometry used to project a perspective image of an object on the x,y plane

As shown in Figure 4-2, a primitive camera can be constructed by putting a small hole in an opaque sheet. Rays from an object passing through this hole will produce a photographic-like perspective image on a “film plane.” The perspective transformation you will be producing in this chapter will operate in a somewhat similar manner, except you will be tracing the image on your computer screen. The geometry is geometrically similar, except that the pinhole geometry produces a reversed image. If the focal point is moved far back in the -z direction, the rays from the object become almost parallel and the perspective effect is lost; the image becomes flattened. This phenomenon is well known to photographers when shooting with a long focal length lens.
../images/456962_1_En_4_Chapter/456962_1_En_4_Fig2_HTML.jpg
Figure 4-2

Pinhole camera vs. computer projection geometry

Figures 4-3 and 4-4 show the geometry you will use to construct your transformation. Figure 4-3 shows a three-dimensional object inside the x,y,z space. The focal point is outside the space at global coordinates (xfp,yfp,zfp). It can be anywhere in front of (-z direction) the x,y plane. Different locations will produce different views of the object, much as a camera will produce different images when photographing an object from different locations.

Imaginary rays emanating from the corners of the box pass through the x,y plane, which you can imagine is your computer screen. Each ray hits the x,y plane at a hit point (xh,yh,zh=0) on its way to the focal point of zh=0 since the x,y plane is at z=0. Connecting the hit points produced by the rays coming from the points comprising the object will produce a perspective image.

A typical point on the object is located at (x,y,z). The distance between the point and the focal point is Q. Qh is the distance from the focal point to the hit point. |zfp|+z is the horizontal distance from the focal point to the object point. |z| is the horizontal distance from the focal point to the hit point. û is a unit vector pointing from the focal point toward the object point. Using this geometry, you can derive the following relations:
$$ a=x- xfp $$
(4-1)
$$ b=y- yfp $$
(4-2)
$$ c=z+left| zfp
ight| $$
(4-3)
Since, in Equation 4-3 zfp is negative (it lies in front of the x,y plane), you use its absolute value of |zfp| because it adds to z to give the total z-direction distance between the focal point and the object point. You could, of course, write Equation 4-3 as c=z-zfp, which is equivalent, but the use of the absolute value |zfp| makes the following analysis more understandable. Also, it won’t matter if you forget and enter a positive z value for zfp.
$$ Q=sqrt{a^2+{b}^2+{c}^2} $$
(4-4)
$$ ux=a/Q $$
(4-5)
$$ uy=b/Q $$
(4-6)
$$ uz=c/Q $$
(4-7)
$$ widehat{mathbf{u}}= uxwidehat{mathbf{i}}+ uywidehat{mathbf{j}}+ uzwidehat{mathbf{k}} $$
(4-8)
$$ Qh=frac{Q;left|zfp
ight|}{z+left|zfp
ight|} $$
(4-9)
$$ xh=ux;Qh+xfp $$
(4-10)
$$ yh=uy;Qh+yfp $$
(4-11)
$$ zh=0 $$
(4-12)
You can show zh=0 (i.e. the hit point lies on the x,y plane, as it should), by the following:
$$ left|zh=uz;Qh-left|zfp
ight|
ight. $$
(4-13)
$$ =frac{c}{Q};Qh-left|zfp
ight| $$
(4-14)
$$ =left(z+left|zfp
ight|
ight);frac{Qh}{Q}-left|zfp
ight| $$
(4-15)
$$ =frac{left(z+left|zfp
ight|
ight)}{Q};frac{Qleft|zfp
ight|}{left(z+left|zfp
ight|
ight)}-left|zfp
ight| $$
(4-16)
$$ =left| zfp
ight|-left| zfp
ight| $$
(4-17)
$$ =0 $$
(4-18)
The negative sign in Equation 4-13 is because |zfp| is always positive while you know that the focal point is always in the -z position.
../images/456962_1_En_4_Chapter/456962_1_En_4_Fig3_HTML.jpg
Figure 4-3

Perspective image projection geometry

../images/456962_1_En_4_Chapter/456962_1_En_4_Fig4_HTML.jpg
Figure 4-4

Perspective image projection geometry side view

Listing 4-1 illustrates the use of the above model. It enables you to construct an object, rotate it, and then view it in perspective. The object, in this case a house, is defined in lines 14-29. Lines 14-16 establish corner coordinates x,y,z in local coordinates; that is, in relation to a point xc,yc,zc, which is set in lines 18-20. This is at the center of the house and it will be the center of rotation. Lines 22-29 convert x,y,z to global coordinates xg,yg,zg by adding elements to the empty lists set in lines 22-24. Lines 31-47 plot the house by connecting the corner points with lines.

Lines 50-63 define a function that rotates the local coordinates about xc,yc,zc, saving the results as xg,yg,zg. It uses function roty, which is defined in lines 54-63. This function was used in prior programs. It is the only rotation function in this program, which means you can only rotate around the y direction. Next is the perspective transformation perspective(xfp,yfp,zfp); it implements Equations 4-1 through 4-12, developed above. The loop beginning in line 67 calculates the coordinates of the hit point for rays that go to the focal point from each of the object’s corner points. The hit points, in terms of global coordinates, are saved in lines 79-81.

Control of the program takes place in lines 83-95. Lines 83-85 define the location of the focal point; lines 87-89 the house’s center point. Ry in line 91 specifies the angle of rotation about the y direction. Line 93 then invokes function plothouse(xc,yc,zc,Ry), which rotates the house. Line 94 invokes perspective(xfp,yfp,zfp), which performs the perspective transformation. Line 95 plots the house. This could have been incorporated in the function perspective but it has been placed here to illustrate the sequence of operations.

1   """
2   PERSPECTIVE
3   """
4
5   import matplotlib.pyplot as plt
6   import numpy as np
7   from math import sin, cos, radians
8
9   plt.axis([0,150,100,0])
10
11  plt.axis('on')
12  plt.grid(True)
13
14  x=[-20,-20,20,20,-20,-20,20,20,-20,20] #——–object local corner coordinates
15  y=[-10,-10,-10,-10,10,10,10,10,-20,-20]
16  z=[5,-5,-5,5,5,-5,-5,5,0,0]
17
18  xc=30 #———————————————object center coordinates
19  yc=50
20  zc=10
21
22  xg=[ ] #———————————————object global coordinates
23  yg=[ ]
24  zg=[ ]
25
26  for i in np.arange(len(x)):
27        xg.append(x[i]+xc)
28        yg.append(y[i]+yc)
29        zg.append(z[i]+zc)
30
31  #————————————–plot object
32  def plothouse(xg,yg,zg):
33       plt.plot([xg[0],xg[3]],[yg[0],yg[3]],color='k')
34       plt.plot([xg[1],xg[2]],[yg[1],yg[2]],color='k')
35       plt.plot([xg[4],xg[7]],[yg[4],yg[7]],color='k')
36       plt.plot([xg[5],xg[6]],[yg[5],yg[6]],color='k')
37       plt.plot([xg[8],xg[9]],[yg[8],yg[9]],color='k')
38       plt.plot([xg[4],xg[0]],[yg[4],yg[0]],color='k')
39       plt.plot([xg[5],xg[1]],[yg[5],yg[1]],color='k')
40       plt.plot([xg[6],xg[2]],[yg[6],yg[2]],color='r')
41       plt.plot([xg[7],xg[3]],[yg[7],yg[3]],color='r')
42       plt.plot([xg[0],xg[8]],[yg[0],yg[8]],color='k')
43       plt.plot([xg[1],xg[8]],[yg[1],yg[8]],color='k')
44       plt.plot([xg[2],xg[9]],[yg[2],yg[9]],color='r')
45       plt.plot([xg[3],xg[9]],[yg[3],yg[9]],color='r')
46       plt.plot([xg[4],xg[5]],[yg[4],yg[5]],color='k')
47       plt.plot([xg[6],xg[7]],[yg[6],yg[7]],color='r')
48
49  #——————————————rotate object about the Y direction
40  def plothousey(xc,yc,zc,Ry):
51       for i in range(len(x)): #—————rotate 10 corners
52             [xg[i],yg[i],zg[i]]=roty(xc,yc,zc,x[i],y[i],z[i],Ry)
53
54  def roty(xc,yc,zc,x,y,z,Ry):
55       a=[x,y,z]
56       b=[cos(Ry),0,sin(Ry)]
57       xpp=np.inner(a,b)
58       b=[0,1,0]
59       ypp=np.inner(a,b)
60       b=[-sin(Ry),0,cos(Ry)]
61       zpp=np.inner(a,b)
62       [xg,yg,zg]=[xpp+xc,ypp+yc,zpp+zc]
63       return [xg,yg,zg]
64
65  #—————————————————————————————————————perspective transformation
66  def perspective(xfp,yfp,zfp):
67       for i in range(len(x)):
68             a=xg[i]-xfp
69             b=yg[i]-yfp
70             c=zg[i]+abs(zfp)
71             q=np.sqrt(a*a+b*b+c*c)
72             ux=a/q
73             uy=b/q
74             uz=c/q
75             qh=q*abs(zfp)/(zg[i]+abs(zfp))
76             xh=ux*qh+xfp
77             yh=uy*qh+yfp
78             zh=0
79             xg[i]=xh
80             yg[i]=yh
81             zg[i]=zh
82
83  xfp=80 #—————————————————————————focal point coordinates
84  yfp=50
85  zfp=-100
86
87  xc=80 #——————————————redefine center coordinates
88  yc=50
89  zc=50
90
91  Ry=radians(45)  #—————————————————————angle of rotation
92
93  plothousey(xc,yc,zc,Ry)               #—-rotate
94  perspective(xfp,yfp,zfp)              #—-transform
95  plothouse(xg,yg,zg)                   #—-rotate
96
97  plt.show()
Listing 4-1

Program PERSPECTIVE

Figures 4-5 through 4-8 show output from Listing 4-1. Figure 4-5 shows the house in its unrotated (Ry=0) orientation. The right side is red. The focal point is at xc=80,yc=50,-100. This is in line with the house’s center but 100 in front of the x,y plane. Figure 4-6 shows the house rotated 45 degrees around the y direction. The perspective effect is apparent. Figure 4-7 shows the house with the same settings but with the focal point moved back from zfp=-100 to zfp=-600. You can see how the image is flattened and the perspective effect is mostly lost. Figure 4-8 shows the house with some random settings. By following the procedure in Listing 4-1, you should be able to create a more elaborate scene quite easily.
../images/456962_1_En_4_Chapter/456962_1_En_4_Fig5_HTML.jpg
Figure 4-5

Perspective image with Ry=0, zfp=-100

../images/456962_1_En_4_Chapter/456962_1_En_4_Fig6_HTML.jpg
Figure 4-6

Perspective image with Ry=45, zfp=-100

../images/456962_1_En_4_Chapter/456962_1_En_4_Fig7_HTML.jpg
Figure 4-7

Perspective image with Ry=45, zfp=-600

../images/456962_1_En_4_Chapter/456962_1_En_4_Fig8_HTML.jpg
Figure 4-8

Perspective image with Ry=-60, zfp=-100, xc=40, yc=70, xfp=100, zfp=-80

The question is, where to place the focal point. If you’re projecting the image onto the x,y plane, clearly it should be in front of that plane (i.e. i the -z direction). But what about the x,y coordinates of the focal point? The best results, most like what would be seen by the human eye, would be to place it at the same x,y coordinates as the house’s center. Of course, if there are many objects in the model, such as more houses and trees, it is not obvious where to place the focal point. The best results will be obtained by situating it in front of the x,y plane at the coordinates that correspond to the approximate center of the model. This is akin to aiming a camera at the center of a scene to be photographed. The painter Vermeer chose this structure in many of his paintings. In fact, in some of his canvases art historians have found a nail hole at the vanishing point where all parallel lines such as room corners and floor tiles converge. The nail hole is in the approximate center of the scene. It is believed he tied a string to a nail and used it to trace the converging lines, much as you have used lines in your algorithm. You can see this structure in many of Vermeer’s interior paintings.

4.1 Summary

In this chapter, you learned how to construct a perspective view. The geometry is based on the simple box camera. You had the perspective image projected onto the x,y plane. You could have used any of the other coordinate planes, for example the x,z plane; the geometry would be similar. You explored the question of where to place the focal point, which corresponds to the observation point of a viewer or a camera. The answer is, unless you are looking for an unusual image, at the approximate center of the model. This was the structure used by Vermeer in many of his paintings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.76.89