4.2. How to transform a vector or a coordinate in the referential of a StructuredGrid to the world referential

In this example, we will transform a vector expressed in the referential of a StructuredGrid into the world referential, and vice versa. We will also transform a coordinate expressed in the referential of a StructuredGrid into the world referential, and vice versa.

For these examples, the following matrix will be used, as obtained at Transformations of a constructed StructuredGrid:

from ORSModel import orsMatrix
transformationMatrixFromStructuredGridCoordinateSystemToWorld = orsMatrix(0.000866024792, 0, 0.050000106, 2, 0, 0.01, 0, 0, -0.00050000106, 0, 0.0866024792, 0, 0, 0, 0, 1)

4.2.1. Transforming a vector

The method ORSModel.ors.Matrix4x4.getTransformedVector() computes the transformation of a vector. It means that the rotation and scaling in the current instance of Matrix4x4 are used.

To obtain the vector in the world system of a vector in the transformed system:

from ORSModel import orsVect
vectorInStructuredGridCoordinateSystem = orsVect(100, 0, 0)  # 100 voxels in StructuredGrid X axis
vectorInWorld = transformationMatrixFromStructuredGridCoordinateSystemToWorld.getTransformedVector(vectorInStructuredGridCoordinateSystem)
# vectorInWorld: orsVect(0.0866024792, 0, -0.050000106)  # In meters in world

Inversely, to obtain the vector in the transformed system of a vector in the world system, the inverse transformation is first obtained with ORSModel.ors.Matrix4x4.getInverted(), then the vector is transformed:

vectorInWorld = orsVect(1, 0, 0)  # 1 meter in world X axis
vectorInStructuredGridCoordinateSystem = transformationMatrixFromStructuredGridCoordinateSystemToWorld.getInverted().getTransformedVector(vectorInWorld)
# vectorInStructuredGridCoordinateSystem: orsVect(866.024792, 0, 5.0000106)  # In voxels of the StructuredGrid

4.2.2. Transforming a coordinate

The method ORSModel.ors.Matrix4x4.getTransformedCoordinate() computes the transformation of a coordinate. It means that the rotation, scaling and translation in the current instance of Matrix4x4 are used.

To obtain the coordinates in the world system of a coordinate in the transformed system:

from ORSModel import orsVect
coordinateInStructuredGridCoordinateSystem = orsVect(100, 0, 0)  # 100 voxels in StructuredGrid X axis
coordinateInWorld = transformationMatrixFromStructuredGridCoordinateSystemToWorld.getTransformedCoordinate(coordinateInStructuredGridCoordinateSystem)
# coordinateInWorld: orsVect(2.08660248, 0, -0.050000106)  # In meters in world

Inversely, to obtain the coordinates in the transformed system of a coordinate in the world system, the inverse transformation is first obtained with ORSModel.ors.Matrix4x4.getInverted(), then the coordinate is transformed:

coordinateInWorld = orsVect(5, 0, 0)  # 5 meters in world X axis
coordinateInStructuredGridCoordinateSystem = transformationMatrixFromStructuredGridCoordinateSystemToWorld.getInverted().getTransformedCoordinate(coordinateInWorld)
# coordinateInStructuredGridCoordinateSystem: orsVect(2598.07438, 0, 15.0000318)  # In voxels of the StructuredGrid