3D image engine implementation from scratch: (5) 3D coordinate system function library

Source: Internet
Author: User

1. Mathematical Analysis

1) 2D Cartesian coordinate system and 2D POLAR COORDINATE SYSTEM

2d Cartesian coordinate system is a plane Cartesian coordinate system.

The 2D polar coordinate system uses the direction and distance to define the points in the 2D space, rather than the X and Y coordinates, such:

The polar coordinate parameters are represented in red, and the Cartesian coordinate parameters are represented in blue.

Obviously, the conversion relationships between them are as follows:

X = r * Cos (theta)

Y = r * sin (theta)

R = SQRT (X2 + y2)

Theta = arctg (y/X)

 

2) 3D Cartesian coordinate system

The Z axis is added to the 2D Cartesian coordinate system to form the 3D Cartesian coordinate system. It can be divided into the left-hand coordinate system and the right-hand coordinate system. Method of differentiation: Hold the Z axis with the left hand, extend the thumb, and the other four fingers from the fingertip to the Y axis, if the thumb points to the Z axis, it is the left-hand coordinate system, the right hand.

 

3) 3D Cylindrical Coordinate System

The 3D cylindrical coordinate system corresponds to the 2D polar coordinate system, but a Z axis is added to the 2D polar coordinate system. Therefore, the conversion between the 3D Cartesian coordinate system and the 3D cylindrical coordinate system is also very simple: x, Y, R, and theta remain unchanged, but only zcoordinates are added. The representation of the 3D cylindrical coordinate system is P (R, Theta, Z ).

 

4) 3D Spherical Coordinate System

This is the most complex 3D coordinate system, represented by P (P, Phi, theta. Where p is the distance from point P to the origin, Phi is the angle between the straight line from the origin to point P and the positive Z axis, theta is the angle between the projection and the X-axis of the line segment from the origin point to the P on the X-Y plane, which is actually exactly the Polar Coordinate Theta. Due to the complexity ,:

Now we can deduce the relationship between (p, Phi, theta) and (x, y, z.

We can see from the figure that:

Projection length of OP on the X-Y plane r = SQRT (X2 + y2)

P = SQRT (X2 + y2 + Z2)

Sin (PHI) = R/P, so

Phi = arcsin (R/P)

TG (theta) = y/x

Theta = arctg (y/X)

 

How can we get X, Y, and Z from P, Phi, And Theta:

R = p * sin (PHI)

X = r * Cos (theta)

Y = r * sin (theta)

Z = p * Cos (PHI)

Sorted by substitution:

X = p * sin (PHI) * Cos (theta)

Y = p * sin (PHI) * sin (theta)

Z = p * Cos (PHI)

 

After figuring out the relationship above, we can establish the data structure of the points in these Coordinate Systems and the conversion function.

 

2. Code Implementation

1) struct Definition

// Type declaration <br/> typedef struct point2d_type // 2D Cartesian coordinates <br/>{< br/> double X; <br/> double y; <br/>} point2d, * point2d_ptr; </P> <p> typedef struct point3d_type // 3D Cartesian coordinate <br/>{< br/> double X; <br/> double y; <br/> double Z; <br/>} point3d, * point3d_ptr; </P> <p> typedef struct polar2d_type // 2D polar coordinate <br/>{< br/> double r; <br/> double Theta; <br/>} polar2d, * polar2d_ptr; </P> <p> typedef struct cylindrical3d_type // 3D Cylindrical Coordinate <br/>{< br/> double r; <br/> double Theta; <br/> double Z; <br/>} cylindrical3d, * cylindrical3d_ptr; </P> <p> typedef struct spherical3d_type <br/>{< br/> Double P; <br/> double Phi; <br/> double Theta; <br/>} spherical3d, * spherical_ptr;

 

2) define conversion functions

Void _ cppyin_math: vertex (point2d_ptr point2d, polar2d_ptr polar2d) <br/>{< br/> polar2d-> r = SQRT (point2d-> X * point2d-> X) + (point2d-> y * point2d-> Y); <br/> polar2d-> Theta = atan (point2d-> Y)/(point2d-> X )); <br/>}</P> <p> void _ cppyin_math: cootranspolar2dtopoint2d (polar2d_ptr polar2d, point2d_ptr point2d) <br/>{< br/> point2d-> X = polar2d-> r * Cos (polar2d-> theta ); <br/> point2d-> Y = polar2d-> r * sin (polar2d-> theta); <br/>}</P> <p> void _ cppyin_math :: struct (point3d_ptr point3d, cylindrical3d_ptr cylindrical3d) <br/>{< br/> cylindrical3d-> r = SQRT (point3d-> X * point3d-> X) + (point3d-> y * point3d-> Y); <br/> cylindrical3d-> Theta = atan (point3d-> Y)/(point3d-> X )); <br/> cylindrical3d-> Z = point3d-> Z; <br/>}</P> <p> void _ cppyin_math: cylindrical3d_ptr cylindrical3d, point3d_ptr point3d) <br/>{< br/> point3d-> X = cylindrical3d-> r * Cos (cylindrical3d-> theta ); <br/> point3d-> Y = cylindrical3d-> r * sin (cylindrical3d-> theta); <br/> point3d-> Z = cylindrical3d-> Z; <br/>}</P> <p> void _ cppyin_math: cootranspoint3dtospherical3d (point3d_ptr point3d, spherical3d_ptr spherical3d) <br/>{< br/> spherical3d-> P = SQRT (point3d-> X * point3d-> x) + (point3d-> y * point3d-> Y) + (point3d-> Z * point3d-> Z); <br/> double r = SQRT (point3d-> X * point3d-> X) + (point3d-> y * point3d-> Y); <br/> spherical3d-> Phi = Asin (R/spherical3d-> P ); <br/> spherical3d-> Theta = atan (point3d-> Y/point3d-> X); <br/>}</P> <p> void _ cppyin_math :: cootransspherical3dtopoint3d (spherical3d_ptr spherical3d, point3d_ptr point3d) <br/>{< br/> double r = spherical3d-> P * sin (spherical3d-> PHI ); <br/> point3d-> X = r * Cos (spherical3d-> theta); <br/> point3d-> Y = r * sin (spherical3d-> theta ); <br/> point3d-> Z = spherical3d-> P * Cos (spherical3d-> PHI); <br/>}

There is nothing to say, but it's all about the formulas mentioned above.

 

 

3. Download Code

Download complete project source code:> click to enter the download page <

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.