08/10/2012

Remove scaling from transformation matrix

If you are trying to create a transformation matrix and e.g. because of imprecision it will get a scaling factor, and this scaling factor is non-uniform (i.e. different value along the X, Y or Z axis) then you cannot use it to transform certain entities like a Polyline. If you try it you'll get an eCannotScaleNonUniformly error. If you do not want to scale at all, you can just remove the scaling factor like so:

PublicSub RemoveScaling(ByRef mx AsMatrix3d)

Dim axes AsNewList(OfVector3d)

For i AsInteger = 0 To 2

Dim vec AsNewVector3d(mx(i, 0), mx(i, 1), mx(i,2))

' This will make the vector have length = 1.0

vec = vec.GetNormal()

axes.Add(vec)

Next

mx = NewMatrix3d(NewDouble() _

{

axes(0).X, axes(0).Y, axes(0).Z, mx(0, 3),

axes(1).X, axes(1).Y, axes(1).Z, mx(1, 3),

axes(2).X, axes(2).Y, axes(2).Z, mx(2, 3),

mx(3, 0), mx(3, 1), mx(3, 2), mx(3,3)

})

EndSub

The same way you could also make sure that the scaling is uniform by making the length of each axis vector the same. You would need to modify the above code like so:

Comments

If you are trying to create a transformation matrix and e.g. because of imprecision it will get a scaling factor, and this scaling factor is non-uniform (i.e. different value along the X, Y or Z axis) then you cannot use it to transform certain entities like a Polyline. If you try it you'll get an eCannotScaleNonUniformly error. If you do not want to scale at all, you can just remove the scaling factor like so:

PublicSub RemoveScaling(ByRef mx AsMatrix3d)

Dim axes AsNewList(OfVector3d)

For i AsInteger = 0 To 2

Dim vec AsNewVector3d(mx(i, 0), mx(i, 1), mx(i,2))

' This will make the vector have length = 1.0

vec = vec.GetNormal()

axes.Add(vec)

Next

mx = NewMatrix3d(NewDouble() _

{

axes(0).X, axes(0).Y, axes(0).Z, mx(0, 3),

axes(1).X, axes(1).Y, axes(1).Z, mx(1, 3),

axes(2).X, axes(2).Y, axes(2).Z, mx(2, 3),

mx(3, 0), mx(3, 1), mx(3, 2), mx(3,3)

})

EndSub

The same way you could also make sure that the scaling is uniform by making the length of each axis vector the same. You would need to modify the above code like so: