## Inverse Transformation for projection

McGum
10/14/2021 6:13 AM

Hi,

Sorry for this "stupid" Question -- but I'm searching for transformation who can do this:
https://stackoverflow.com/questions/2500499/howto-project-a-planar-polygon-on-a-plane-in-3d-space

Is there a possibility to do this with CAD Lib default methods?

Michael

Wout
10/14/2021 9:07 AM

There's no specific method in CadLib to create such a matrix, but you can create a Matrix4D yourself ofcourse using the values mentioned in the stackoverflow post.

- Wout

rammi
10/14/2021 10:05 AM

Hi Michael,

the solution on stackoverflow provides a transformation which is invertable in most cases, but if all you need is a one-way projection (i.e. something which projects 3D space to the plane's 2D space) things are much easier and CadLib can do a lot to help.

In that case the requirement that all points of the polygon are planar is nolonger necessary, and all we need is a transformation which projects any point to the plane. That may look hard enough at first glance, but let's think of a simple case: how would you project a 3D point to the X-Y plane? Indeed, just discard the Z coordinate.

With that in mind a solution to the problem can be done in 3 steps:

1. Create a transformation that rotates and moves the plane so it becomes the X-Y plane.
3. Do the inverse of step 1.

Step 1 may be sound hard enough, but luckily AutoDesk had a similar problem ages ago: they started with a 2D program, but wanted to make it 3D. Instead of adding different extra properties to their flat entities they invented what they called Arbitrary Axis Algorithm which basically moves the former 2D objects to a 3D plane defined by its normal and an additional elevation (i.e. distance from the coordinate center). As this is what happens above in the 3rd step, we can use it there and its inverse in step 1.

Putting these together in CadLib (sorry if the following doesn't compile as I'm currently on a Linux machine without access to CadLib, but you should get the idea):

C# Code:
```// assuming plane formular: ax + by + cz - d = 0
// (note the -d which is the standard way to write this formular as it defines the plane's distance)
Vector3D normal = new Vector3D(a, b, c);  // assumed to have length 1, otherwise scale both this and d with the inverse length

// step 3: move Z d up, then rotate (see comment on inverse order below)
Matrix4D xy2plane = Transformation4D.GetArbitraryCoordSystem(normal) * Transformation4D.Translation(0, 0, d);
Matrix4D plane2xy = xy2plane.GetInverse(); // step 1, and there is always an inverse
Matrix4D project = Transformation4D.Scaling(1, 1, 0); // step 2: non-invertable projection discarding Z

// put everything together
// (note that transformations in CadLib are multiplied in the inverse order in which they are applied)
Matrix3D complete = xy2plane *  // step 3
project  *  // step 2
plane2xy;   // step 1 ```

The resulting matrix is no longer invertable, otherwise the above code is a lot simpler as the one you linked, and you don't have to take care of corner cases.

- Rammi

McGum
10/14/2021 10:51 AM

Thanks Rami,

will try this!