In the field of engineering, computer graphics, and physics, understanding the relationship between different coordinate systems is crucial for accurate modeling, simulation, and analysis.
The figure attached describes the concept of a
global coordinate system
(GCS) and a
local coordinate system
(LCS),
both of which are commonly used to define the position and orientation of objects in 3D space.
The GCS is represented by the axes
x ,
y , and
z .
This system provides a fixed reference
for describing the location of all objects within the space.
In contrast, the LCS, denoted by the axes
x' ,
y' , and
z' , is
tied to a specific object within the GCS
.
It moves and rotates with the object, providing a reference frame that is local to the object itself.
Global coordinate system and local coordinate system
These transformations between coordinate systems are governed by rotation matrices or
transformation matrices
that contain the cosine and sine functions of the angles between the axes.
The
Plane2d
class has properties that are the origin, x-axis, and y-axis as the 2D coordinate system. In this
class that I implemented, the core functions are as follows:
- _get_local_coords_matrix(): This method constructs a matrix that represents the local coordinate system of the plane in terms of homogeneous coordinates
- get_converted_geometry_by_plane(): Converts a geometry from the plane's coordinate system to another
class Plane2d(AxesVisualizer):
def __init__(self, origin: np.ndarray, x_axis: np.ndarray, y_axis: np.ndarray, normalize: bool = True):
self._origin = origin
self._x_axis = x_axis / np.linalg.norm(x_axis) if normalize else x_axis
self._y_axis = y_axis / np.linalg.norm(y_axis) if normalize else y_axis
def _get_local_coords_matrix(self) -> np.ndarray:
"""Returns a local coordinate matrix expressed in homogeneous coordinates
Returns:
np.ndarray: local coordinates of self
"""
return np.array(
[
[self.x_axis[0], self.y_axis[0], self.origin[0]],
[self.x_axis[1], self.y_axis[1], self.origin[1]],
[0, 0, 1],
]
)
def get_converted_geometry_by_plane(self, plane_to_convert: Plane2d, geometry_to_convert: GeometryCollection) -> GeometryCollection:
"""Converts a given geometry from one plane to another
Args:
plane_to_convert (Plane2d): Target plane to convert the coordinate system
geometry_to_convert (GeometryCollection): Geometry of Shapely to convert
Returns:
GeometryCollection: Converted geometry
"""
matrix_to_map_global_coords = np.linalg.inv(self._get_local_coords_matrix())
matrix_to_map_local_coords = plane_to_convert._get_local_coords_matrix() @ matrix_to_map_global_coords
return affinity.affine_transform(
geom=geometry_to_convert,
matrix=[
matrix_to_map_local_coords[0][0],
matrix_to_map_local_coords[0][1],
matrix_to_map_local_coords[1][0],
matrix_to_map_local_coords[1][1],
matrix_to_map_local_coords[0][2],
matrix_to_map_local_coords[1][2],
]
)
The figure below describes the transformation process easily.
Firstly, it needs to take the inverse of a transformation matrix to convert a polygon from the original plane to another
This process corresponds from the blue polygon to the black polygon, and the code line
matrix_to_map_global_coords = np.linalg.inv(self._get_local_coords_matrix())
above.
By this process, the polygon in the local coordinate system is mapped to the global coordinate system that considers (0, 0) to the origin.
After processing the above, we need to remap from the global coordinate system to the local coordinate system to convert it. This is from the black polygon to the green polygon.
The
matrix_to_map_local_coords = plane_to_convert._get_local_coords_matrix() @ matrix_to_map_global_coords
line is corresponded to it.
In the entire
process of transformation, the polygon keeps the same indices of vertices, the shape of the geometry, and orientation (in this case counter-clockwise).
Coordinate system transformation process
As you saw above, the transformation from GCS to LCS involves
rotation, translation, and reflection matrices
that convert a geometry's coordinates to its position and orientation within the plane.
In architectural design, a transformation matrix can help align building shapes along intended angles and directions within an axial framework.
Let's say we're developing an algorithm to automate apartment layout and we have some building templates.
In this situation, we can situate buildings simply along a given axis that is defined for placing apartments.
You just apply the logic that is described above after defining two planes, and geometries for apartments. That's all!
Applying it in apartment design automation