Spatial Cartesian coordinate transformation-affine transformation)

Source: Internet
Author: User
Tags ipoint

I. Introduction
In work development, there are often many problems with Coordinate System Conversion. There are many discussions about how to achieve the conversion between different coordinate systems. Based on practical application projects, most of them have proposed a better solution. Two years ago, I also downloaded an article, "Coordinate System Conversion Formula" (translated by Dai Qianyi, Qingdao Institute of Marine Geology) from the Internet. This article describes various Transformation Models in detail, for example, moroginski-badkass conversion model, hull et conversion model, Bosa model, and polynomial conversion are a comprehensive introduction to Coordinate System Conversion.

I think it is not too difficult for you to understand Common conversion models. If you perform secondary development based on the current popular GIS platform (such as hypergraphs, ArcGIS, and MapInfo, I don't think there will be any difficulties. As long as we find the interfaces they provide and straighten out our ideas, we can also meet the user's requirements. However, we have no idea about the Kernel Algorithm and parameter solving process. Many times we feel that we have solved this problem, so we will not focus too much on the underlying implementation algorithm problems. However, to tell the truth, it is a headache to really figure out the relationship between models. Without a certain mathematical skills, I really don't know what it is talking about.

Ii. Affine Transformation
Affine transformation is a type of spatial Cartesian coordinate transformation. It is a linear transformation between two-dimensional coordinates and two-dimensional coordinates to maintain the "straight line" and "Parallel" of two-dimensional images ", it can be achieved through the combination of a series of atomic transformations, including Translation, Scale, Flip, Rotation, and Shear ).

This type of transformation can be represented by a 3 × 3 matrix, and the last row is (0, 0, 1 ). The transformation matrix converts the original coordinates (x, y) to the new coordinates (x ', y'). Here, the original coordinates and the new coordinates are regarded as the three-dimensional column vectors with the last line (1, the original column vector left multiplication transformation matrix obtains the new column vector:

[X'] [m00 m01 m02] [x] [m00 * x + m01 * y + m02]
[Y'] = [m10 m11 m12] [y] = [m10 * x + m11 * y + m12]
[1] [0 0 1] [1] [1]

The following is represented in an algebraic form:

X' = m00 * x + m01 * y + m02;
Y' = m10 * x + m11 * y + m12;

If it is written as a combination of three components: rotation, scaling, and translation, the algebraic formula is as follows:

 

It is as follows:

Typical affine transformations:

1.

Public static AffineTransform getTranslateInstance (double tx, double ty) Translation transformation, moving each point to (x + tx, y + ty), the transformation matrix is:

[1 0 tx]
[0 1 ty]
[0 0 1]

Translation transformation is a kind of "rigid-body transformation". rigid-body transformation, which is an ideal object that does not produce deformation, translation certainly does not change the shape of a two-dimensional image. Similarly, the following "Rotation Transformation" is also a rigid body transformation, and "Scaling" and "miscut" will change the shape of the image .)

2.

Public static AffineTransform getScaleInstance (double sx, double sy) scaling and transformation, scale the horizontal coordinates of each point to sx times, vertical coordinates to sy times, and the transformation matrix is:

[Sx 0 0]
[0 sy 0]
[0 0 1]

3.

Public static AffineTransform getShearInstance (double shx, double shy) shear transformation. The transformation matrix is:

[1 shx 0]
[Shy 1 0]
[0 0 1]

It is equivalent to the combination of a transverse shear and a longitudinal shear.

[1 0 0] [1 shx 0]
[Shy 1 0] [0 1 0]
[0 0 1] [0 0 1]

The "shear transformation", also known as the "staggered transformation", refers to the nature similar to the Quadrilateral instability. Have you ever seen the small shops on the street? Imagine the process of pulling the diamond formed by the iron strip above, that is, the process of "miscutting .)

4.

Public static AffineTransform getRotateInstance (double theta) rotation and transformation. The target image rotates theta radians clockwise around the origin. The conversion matrix is:

[Cos (theta)-sin (theta) 0]
[Sin (theta) cos (theta) 0]
[0 0 1]

5.

Public static affinetransform getrotateinstance (double Theta, double X, Double Y) rotation and transformation. The target image uses (x, y) as the axis to rotate Theta radians clockwise. The transformation matrix is:

[Cos (theta)-sin (theta) x-x * Cos + y * sin]
[Sin (theta) Cos (theta) Y-x * sin-y * Cos]
[0 0 1]

It is equivalent to the combination of two Translation Transformations and one origin Rotation Transformation:

[1 0-x] [cos (theta)-sin (theta) 0] [1 0 x]
[0 1-y] [sin (theta) Cos (theta) 0] [0 1 y]
[0 0 1] [0 0 1] [0 0 1]

Iii. Resolution of the four parameters of the Affine Transformation

A. C # UDF implementation solution:

1. Solve the Rotation Parameter Rotaion:

////
/// Get the Rotation Angle
///
/// Source point 1
/// Target point 1
/// Source 2
/// Target point 2
/// Return the Rotation Angle

Private double GetRotation (CoordPoint fromPoint1, CoordPoint toPoint1, CoordPoint fromPoint2, CoordPoint toPoint2)
{
Double a = (toPoint2.Y-toPoint1.Y) * (fromPoint2.X-fromPoint1.X)-(toPoint2.X-toPoint1.X) * (fromPoint2.Y-fromPoint1.Y );
Double B = (toPoint2.X-toPoint1.X) * (fromPoint2.X-fromPoint1.X) + (toPoint2.Y-toPoint1.Y) * (fromPoint2.Y-fromPoint1.Y );

If (Math. Abs (B)> 0)
Return Math. Tan (a/B );
Else
Return Math. Tan (0 );
}
2. Solve the Scale parameter ):

///
/// Obtain the scaling factor
///
/// Source point 1
/// Target point 1
/// Source 2
/// Target point 2
/// Rotation Angle
/// Returns the rotation factor

Private double GetScale (CoordPoint fromPoint1, CoordPoint toPoint1, CoordPoint fromPoint2, CoordPoint toPoint2, double rotation)
{

Double a = toPoint2.X-toPoint1.X;
Double B = (fromPoint2.X-fromPoint1.X) * Math. Cos (rotation)-(fromPoint2.Y-fromPoint1.Y) * Math. Sin (rotation );

If (Math. Abs (B)> 0)
Return a/B;
Else
Return 0;
} 3. Solve the X-direction offset distance parameter (XTranslate ):

///
/// Get the offset in the X direction
///
/// Source point 1
/// Target point 1
/// Rotation Angle
/// Scaling Factor
/// Returns the X offset.

Private double GetXTranslation (CoordPoint fromPoint1, CoordPoint toPoint1, double rotation, double scale)
{
Return (toPoint1.X-scale * (fromPoint1.X * Math. Cos (rotation)-fromPoint1.Y * Math. Sin (rotation )));
} 4. Solve the Y-direction offset distance parameter (YTranslate ):

///
/// Get the offset in the Y direction
///
/// Source point 1
/// Target point 1
/// Rotation Angle
/// Scaling Factor
/// Returns the Y offset.

Private double GetYTranslation (CoordPoint fromPoint1, CoordPoint toPoint1, double rotation, double scale)
{
Return (toPoint1.Y-scale * (fromPoint1.X * Math. Sin (rotation) + fromPoint1.Y * Math. Cos (rotation )));
} B. C # + AE solutions:

///
/// Define the affine transform program from the Control Point
///
/// Source Control Point
/// Target Control Point
/// Return transformation Definition

Private ITransformation GetAffineTransformation (IPoint [] pFromPoints, IPoint [] pToPoints)
{
// Instantiate an affined transformation object
IAffineTransformation2D3GEN tAffineTransformation = new AffineTransformation2DClass ();

// Define parameters from the source control point
TAffineTransformation. DefineFromControlPoints (ref pFromPoints, ref pToPoints );

// Query the reference Interface
ITransformation tTransformation = tAffineTransformation as ITransformation;
Return tTransformation;
} 4. space object Conversion
After finding the parameters, it is relatively simple to use the formula to convert the corresponding coordinate points.
Sample Code:

////
/// Converted space point
///
/// Point
/// Return the converted Vertex

Private IGeometry TransformPoint (IPoint pPoint)
{
//************************************** ******
// Description: similar transformation model (four-Parameter Transformation Model)
// X = ax-by + c
// Y = bx + ay + d
//************************************** *******

Double A = this. m_Scale * Math. Cos (this. m_RotationAngle );
Double B = this. m_Scale * Math. Sin (this. m_RotationAngle );

IPoint tPoint = new PointClass ();

TPoint. X = A * pPoint. X-B * pPoint. Y + this. m_DX;
TPoint. Y = B * pPoint. X + A * pPoint. Y + this. m_DY;

Return tPoint;
} Summary:
This article mainly introduces how to use the affine transform equation for spatial Cartesian coordinate transformation, explains in detail the typical situations of the affine transform, and provides the key implementation code for how to solve the parameters, A reference example is provided for the transformation of spatial objects. If you are a ArcGIS user, you can use its own interfaces to perform spatial conversion.
When I wrote this article, to be honest, I did not fully understand the various models of coordinate transformation, and there were many problems in my mind, for example, we have never figured out how to use the least squares formula to solve the parameters. I hope you can give me more advice. Thank you very much!
Vi. Remarks
If you want to develop a model based on AE, please note that the algebra of the affine transform model provided in version 9.2 is incorrect:
It provides the following error algebra:

X = AX + by + c
Y =-bx + Ay + F

The correct format should be as follows:

X = Ax-by + c
Y = bx + Ay + d

References:
1. Coordinate System Conversion Formula)
2. affinetransform with the help of Java documentation
3. ESRI development documentation

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/sjzwl/archive/2008/12/31/3668930.aspx

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.