A Very Simple Approach for 3-D to 2-D Mapping
Total Page:16
File Type:pdf, Size:1020Kb
Image Processing & Communications, vol. 11, no. 2, pp. 75-82 75 A VERY SIMPLE APPROACH FOR 3-D TO 2-D MAPPING SANDIPAN DEY (1), AJITH ABRAHAM (2),SUGATA SANYAL (3) (1) Anshin Soft ware Pvt. Ltd. INFINITY, Tower - II, 10th Floor, Plot No.- 43. Block - GP, Salt Lake Electronics Complex, Sector - V, Kolkata - 700091 email: [email protected] (2) IITA Professorship Program, School of Computer Science, Yonsei University, 134 Shinchon-dong, Sudaemoon-ku, Seoul 120-749, Republic of Korea email: [email protected] (3) School of Technology & Computer Science Tata Institute of Fundamental Research Homi Bhabha Road, Mumbai - 400005, INDIA email: [email protected] Abstract. libraries with any kind of system is often a tough trial. This article presents a very simple method of Many times we need to plot 3-D functions e.g., in mapping from 3-D to 2-D, that is free from any com- many scientific experiments. To plot this 3-D func- plex pre-operation, also it will work with any graph- tions on 2-D screen it requires some kind of map- ics system where we have some primitive 2-D graph- ping. Though OpenGL, DirectX etc 3-D rendering ics function. Also we discuss the inverse transform libraries have made this job very simple, still these and how to do basic computer graphics transforma- libraries come with many complex pre-operations tions using our coordinate mapping system. that are simply not intended, also to integrate these 76 S. Dey, A. Abraham, S. Sanyal 1 Introduction 2 Proposed approach We have a pictorial representation (Fig.1) of our 3-D to 2-D mapping system: We have a function f : R2 → R, and our intention is to draw the function in 2-D plane. The function z = f (x,y) is a 2-variable function and each tuple (x,y, f (x,y)) ∈ R3. Let’s say we want to graphically Fig. 1: Basic Model of a simple 3-D to 2-D mapping plot f onto computer screen using a primitive system graphics library (like Turbo C graphics), which But, how the function f should look like after supports only the basic putPixel (to draw a pixel mapping and plotting? Here we simulate the 3-rd in 2-D screen) -like 2-D rendering function, but no coordinate (namely Z) in our 2-D x − y plane. We 3-D rendering; i.e., our graphics library’s putPixel’s perform the logical to physical coordinate transform domain is R2 and it’s not R3. and everything by the map function h, which will basically turn out to be a 3 × 2 matrix. The basic mapping technique is shown in Fig. 2, which we are Hence in order to draw the function f using our shortly going to explain. graphics library, we must design a coordinate con- If we have our Origin 0 at (x0,y0) screen coordi- version system, that will provide us with a func- nate, we have, tion that will take as input 3-tuples (x,y, f (x,y)) and ′ ′ produce as output a 2-tuple (x ,y ) that can be di- ′ x = x0 + y − x · sin(θ) rectly passed to our graphics library to plot it onto (1) ′ y = y0 − z + x · cos(θ) the screen, but with 3-D look & feel. As we dis- i.e., we have our 3-D to 2-D transformation ma- cussed, it’s essential that we have a simple coordi- trix: nate mapping system that maps R3 to R2 and still gives us a hypothetical feeling of drawing 3-D func- tions. It’s very easy to find such a map, i.e., a func- −sin(θ) cos(θ) 3 2 M3×2 = 1 0 (2) tion h : R → R and in this paper we try to find such a simple map. 0 −1 Image Processing & Communications, vol. 11, no. 2, pp. 75-82 77 control the dimension along Z − axis by a compres- sion factor ρz and slightly modifying the equations: ′ x = x0 + y − x · sin(θ) (4) ′ y = y0 − ρz · z + x · cos(θ) Obviously, 0.0 < ρz ≤ 1.0 By default we take ρz = 1.0. 3 Sample output surfaces drawn using the above mapping Fig. 2: The basic coordinate mapping Following surfaces (Fig. 3 and Fig. 4) are drawn Again we have shifting (change of origin) by the in Turbo C++ version 3.0 (BGI graphics) using the matrix O2D =[x0,y0] so that O2D + P3D × M3×2 = above simple 3-D to 2-D mapping. P2D, here × denotes matrix multiplication and + de- notes matrix addition, the 3-tuple P3D =[xyz], the ′ ′ 2-tuple P2D =[x y ] i.e. −sin(θ) cos(θ) x y + x y z . 1 0 = x′ y′ h 0 0 i h i h i 0 −1 (3) By default we keep the angle between 4 Inverse Transformation - Obtain- θ π X − axis & Z − axis = = 4 , that one can ing original 3-D coordinates from change if required, but with the following inequality the transformed 2-D coordinates ◦ θ π strictly satisfied: 0 < < 2 . Here, our transformation function (matrix) is de- One can optionally use a compression factor to fined by Eqn. (1). 78 S. Dey, A. Abraham, S. Sanyal Fig. 3: Sine function drawn in TurboC++ Version 3.0 (BGI Graphics) using the 3-D to 2-D mapping Fig. 4: Sync function drawn in TurboC++ Version 3.0 (BGI Graphics) using the 3-D to 2-D mapping Image Processing & Communications, vol. 11, no. 2, pp. 75-82 79 As we can see, it is impossible to re-convert and ob- tain the original set of coordinates, namely (x,y,z), −sin(θ) cos(θ) 0 because we have 3 unknowns and 2 equations. So, M3×3 = 1 0 0 (7) in order to be able to get the original coordinates 0 −1 1 back, we at least need to store 3 tuples as result of with the transformation, for instance, (x,y,z) → (x′,y′,z), the z-coordinate being stored only to get the inverse −sin(θ) cos(θ) 0 transform (x′,y′,z) → (x,y,z) and the (x′,y′) pair is Det(M3×3)= det 1 0 0 = used to plot the point. So, in order to get the inverse 0 −1 1 transformation, we need to solve the equations for θ θ x,y, since we already know z, we have 2-equations −sin( ) cos( ) = 1 · = −cos(θ) (8) 1 0 and 2 unknown variables: θ π θ Now, 0 < < 2 , hence cos( ) =6 0, hence Det(M3×3) =6 0 and the inverse exists. ′ y − x · sin(θ)= x − x0 (5) θ ′ x · cos( )= y − y0 + z −sin(θ) cos(θ) 0 h x0 y0 0 i + h x y z i. 1 0 0 = solving the above 2 equations we get, 0 −1 1 =[x′ y′ z] (9) But, we have, ′ x = (y − y0 + z) · sec(θ) (6) ′ ′ y = x − x0 + (y − y0 + z) ·tan(θ) −1 Ad j(M3×3) Inv(M3×3) = (M3×3) = (10) Det(M3×3) Put it in another way, our transformation matrix Det(M3×3) =6 0 is a 3 × 2 matrix and is done by Eqn. (2) since a non-square matrix, no question of existence of its and, inverse. So, in order to be able to get the inverse 0 −cos(θ) 0 Ad j(M3×3)= −1 −sin(θ) 0 (11) transform as well, we need a 3 × 3 invertible square θ θ matrix, e.g., −1 −sin( ) −cos( ) 80 S. Dey, A. Abraham, S. Sanyal Hence, 5 Rotation and affine transforma- tions 0 1 0 A point in 3-D, after being mapped to 2-D screen, −1 Inv(M3×3) = (M3×3) = sec(θ) tan(θ) 0 following the above mapping procedure, may be θ θ sec( ) tan( ) 1 required to be transformed using standard com- (12) puter graphics transformations (translation, rotation about an axis etc). But in order to undergo such a here, cos(θ) =6 0. graphics transformation and to show the point back So, the inverse transform is: to the screen after the transformation, it needs to go through the following steps in our previously- −sin(θ) cos(θ) 0 described coordinate mapping system: x y z . 1 0 0 = h i • First obtain the inverse coordinate transforma- 0 −1 1 tion to obtain the original 3-D coordinates from = ′ ′ − (13) h x y z i h x0 y0 0 i the mapped 2-D coordinates. • Multiply the 3-D coordinate matrix by proper graphics transformation matrix in order to h x y z i = achieve graphical transformation. 0 1 0 • Use the same 3-D to 2-D map again to plot the = x′ − x y′ − y z . sec(θ) tan(θ) 0 (14) h 0 0 i point onto the screen. θ θ sec( ) tan( ) 1 ′ θ ′ ′ θ h x y z i = h (y − y0 + z)·sec( ) x − x0 + (y − y0 + z)·tan( ) z i (15) This exactly matches with our previous deriva- tion. Image Processing & Communications, vol. 11, no. 2, pp. 75-82 81 ′ These steps can be mathematically represented as: This matrix M is needed to be computed once for a given graphics transformation (e.g., rotation about −1 • P3D = P2D × (M3×3) an axis) and applied to all points on the screen, so ′ • P3D = P3D × T3×3 that using a single matrix multiplication thereafter ′ ′ any point on the screen can undergo graphics trans- • P2D = P3D × M3×3 ′ ′ formation, by, P2D = P2D ×M , where P2D represents Or, by a single line expression, the point mapped before transformation T3×3 and ′ P2D is the point re-mapped after the transformation, ′ −1 P2D = ((P2D × (M3×3) ) × T3×3) × M3×3) as obvious.