.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Paper space to model space conversion matrix

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
1726 Views, 3 Replies

Paper space to model space conversion matrix

Hi

I have a function in ObjectARX (c++) which calculates a matrix for model space to paper space and I have tried to convert the same into
managed objectARX(C#).

But it does not give the right matrix.

Can some look into the same?


//------------------------------C++ ----------------------------------------------------
void ms2ps( AcDbViewport*pVp, AcGeMatrix3d &resultMat)
{
// first get all the data
AcGeVector3d viewDirection = pVp->viewDirection();
AcGePoint2d centre2d = pVp->viewCenter();
AcGePoint3d viewCenter = AcGePoint3d( centre2d.x, centre2d.y, 0);

AcGePoint3d viewTarget = pVp->viewTarget ();
double twistAngle = -pVp->twistAngle();
AcGePoint3d centerPoint = pVp->centerPoint();
double viewHeight = pVp->viewHeight();
double height = pVp->height();
double width = pVp->width();
double scaling = viewHeight / height;
double lensLength = pVp->lensLength();


// prepare the transformation
AcGeVector3d xAxis, yAxis, zAxis;
zAxis = viewDirection.normal();
xAxis = AcGeVector3d::kZAxis.crossProduct( viewDirection );

if( !xAxis.isZeroLength() ) {
xAxis.normalize();
yAxis = zAxis.crossProduct( xAxis );
} else if( zAxis.z < 0 ) {
xAxis = -AcGeVector3d::kXAxis;
yAxis = AcGeVector3d::kYAxis;
zAxis = -AcGeVector3d::kZAxis;
} else {
xAxis = AcGeVector3d::kXAxis;
yAxis = AcGeVector3d::kYAxis;
zAxis = AcGeVector3d::kZAxis;
}

AcGeMatrix3d dcs2wcs; // display coordinate system (DCS) to world coordinate system (WCS)
AcGeMatrix3d ps2Dcs; // paperspace to DCS


// First initialise with a transformation to centerPoint
ps2Dcs = AcGeMatrix3d::translation( AcGePoint3d::kOrigin - centerPoint);

// then scale for the view
ps2Dcs = ps2Dcs * AcGeMatrix3d::scaling( scaling, centerPoint);

// then adjust to the viewCenter
dcs2wcs = AcGeMatrix3d::translation( viewCenter - AcGePoint3d::kOrigin);

// Then transform for the view direction
AcGeMatrix3d matCoords;
matCoords.setCoordSystem( AcGePoint3d::kOrigin, xAxis, yAxis, zAxis);

dcs2wcs = matCoords * dcs2wcs;

// Then adjust for the viewTarget
dcs2wcs = AcGeMatrix3d::translation( viewTarget - AcGePoint3d::kOrigin) * dcs2wcs;

// Then the twist angle
dcs2wcs = AcGeMatrix3d::rotation(twistAngle, zAxis, viewTarget ) *dcs2wcs;

AcGeMatrix3d perspMat;
if( pVp->isPerspectiveOn())
{
// we do special perspective handling
double viewSize = viewHeight;
double aspectRatio = width / height;

double adjustFactor = 1.0 / 42.0;
double adjustedLensLength = viewSize * lensLength * sqrt ( 1.0 +
aspectRatio * aspectRatio ) * adjustFactor;

double eyeDistance = viewDirection.length();
double lensDistance = eyeDistance - adjustedLensLength;


double ed = eyeDistance;
double ll = adjustedLensLength;
double l = lensDistance;


perspMat.entry[2][2] = (ll - l ) / ll;
perspMat.entry[2][3] = l * ( ed - ll ) / ll;
perspMat.entry[3][2] = -1.0 / ll;
perspMat.entry[3][3] = ed / ll;
}

resultMat = ps2Dcs.inverse() * perspMat * dcs2wcs.inverse();
}

//------------------------------C++ ----------------------------------------------------

//------------------------------C#--------------------------------------------- --------------

public static Matrix3d ms2ps(Viewport pVp)
{
Matrix3d resultMat = Matrix3d.Identity;

// first get all the data
Vector3d viewDirection = pVp.ViewDirection;
Point2d centre2d = pVp.ViewCenter;
Point3d viewCenter = new Point3d( centre2d.X, centre2d.Y, 0);

Point3d viewTarget = pVp.ViewTarget;
double twistAngle = -pVp.TwistAngle;
Point3d centerPoint = pVp.CenterPoint;
double viewHeight = pVp.ViewHeight;
double height = pVp.Height;
double width = pVp.Width;
double scaling = viewHeight / height;
double lensLength = pVp.LensLength;

// prepare the transformation
Vector3d xAxis, yAxis, zAxis;
zAxis = viewDirection.GetNormal();
xAxis = kZAxis.CrossProduct(viewDirection);

if( !xAxis.IsZeroLength() )
{
xAxis = xAxis.GetNormal();
yAxis = zAxis.CrossProduct( xAxis );
}
else if( zAxis.Z < 0 )
{
xAxis = kXAxis.Negate();
yAxis = kYAxis;
zAxis = kZAxis.Negate();
}
else
{
xAxis = kXAxis;
yAxis = kYAxis;
zAxis = kZAxis;
}

Matrix3d dcs2wcs; // display coordinate system (DCS) to world coordinate system (WCS)
Matrix3d ps2Dcs; // paper space to DCS


// First initialize with a transformation to centerPoint
ps2Dcs = Matrix3d.Displacement(kOrigin - centerPoint);

// then scale for the view
ps2Dcs = ps2Dcs * Matrix3d.Scaling( scaling, centerPoint);

// then adjust to the viewCenter
dcs2wcs = Matrix3d.Displacement(viewCenter - kOrigin);

//Then transform for the view direction
Matrix3d matCoords = Matrix3d.AlignCoordinateSystem(Matrix3d.Identity.CoordinateSystem3d.Origin, Matrix3d.Identity.CoordinateSystem3d.Xaxis,
Matrix3d.Identity.CoordinateSystem3d.Yaxis, Matrix3d.Identity.CoordinateSystem3d.Zaxis,
Matrix3d.Identity.CoordinateSystem3d.Origin, xAxis, yAxis, zAxis);

dcs2wcs = matCoords * dcs2wcs;

// Then adjust for the viewTarget
dcs2wcs = Matrix3d.Displacement(viewTarget - kOrigin) * dcs2wcs;

// Then the twist angle
dcs2wcs = Matrix3d.Rotation(twistAngle, zAxis, viewTarget ) *dcs2wcs;

Matrix3d perspMat = ps2Dcs.Inverse() * dcs2wcs.Inverse();

return resultMat;
}

//------------------------------C#--------------------------------------------- --------------

Thanks,

Kailas Dhage
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: Anonymous

9 months later.....

internal static Matrix3d MS2PS(Viewport vp)
{
Vector3d viewDirection = vp.ViewDirection;
Point2d center = vp.ViewCenter;
Point3d viewCenter = new Point3d(center.X, center.Y, 0);
Point3d viewTarget = vp.ViewTarget;
double twistAngle = -vp.TwistAngle;
Point3d centerPoint = vp.CenterPoint;

double viewHeight = vp.ViewHeight;

double height = vp.Height;

double width = vp.Width;

double scaling = viewHeight / height;

double lensLength = vp.LensLength;


Vector3d zAxis = viewDirection.GetNormal();

Vector3d xAxis = Vector3d.ZAxis.CrossProduct(viewDirection);


Vector3d yAxis;

if (!xAxis.IsZeroLength())
{
xAxis = NormalizeVector(xAxis);
yAxis = zAxis.CrossProduct(xAxis);
double tmp = xAxis.X;
tmp = xAxis.Y;
tmp = xAxis.Z;
}
else if (zAxis.Z < 0)
{
xAxis = -Vector3d.XAxis;
yAxis = Vector3d.YAxis;
zAxis = -Vector3d.ZAxis;
}
else
{
xAxis = Vector3d.XAxis;
yAxis = Vector3d.YAxis;
zAxis = Vector3d.ZAxis;
}

Matrix3d ps2dcs = Matrix3d.Displacement(Point3d.Origin - centerPoint);
ps2dcs = ps2dcs * Matrix3d.Scaling(scaling, centerPoint);
Matrix3d dcs2wcs = Matrix3d.Displacement(viewCenter - Point3d.Origin);
Matrix3d matCoords = Matrix3d.AlignCoordinateSystem(
Matrix3d.Identity.CoordinateSystem3d.Origin,
Matrix3d.Identity.CoordinateSystem3d.Xaxis,
Matrix3d.Identity.CoordinateSystem3d.Yaxis,
Matrix3d.Identity.CoordinateSystem3d.Zaxis,
Matrix3d.Identity.CoordinateSystem3d.Origin,
xAxis, yAxis, zAxis);
dcs2wcs = matCoords * dcs2wcs;

dcs2wcs = Matrix3d.Displacement(viewTarget - Point3d.Origin) * dcs2wcs;

dcs2wcs = Matrix3d.Rotation(twistAngle, zAxis, viewTarget) * dcs2wcs;

Matrix3d perspMat = Matrix3d.Identity;
if (vp.PerspectiveOn)
{
double viewsize = viewHeight;
double aspectRatio = width / height;
double adjustFactor = 1.0 / 42.0;
double adjustedLensLength = viewsize * lensLength * Math.Sqrt(1.0 + aspectRatio * aspectRatio) * adjustFactor;

double eyeDistance = viewDirection.Length;
double lensDistance = eyeDistance - adjustedLensLength;

double ed = eyeDistance;
double ll = adjustedLensLength;
double l = lensDistance;
perspMat = new Matrix3d(new double[] {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, (ll - l) / ll, l * (ed - ll) / ll,
0, 0, -1.0 / ll, ed / ll });
}
return ps2dcs.Inverse() * perspMat * dcs2wcs.Inverse();
}

internal static Vector3d NormalizeVector(Vector3d vec)
{
double length = Math.Sqrt((vec.X * vec.X) + (vec.Y * vec.Y) + (vec.Z * vec.Z));
double x = vec.X / length;
double y = vec.Y / length;
double z = vec.Z / length;
return new Vector3d(x, y, z);
}
Edited by: SamTrost on Jul 2, 2009 3:56 PM
Message 3 of 4
sandeep_vaal
in reply to: Anonymous

 

Hi

 

Can you please help me how to use Matrix3d transform from MS2PS() ??

 

I am having one sample part which is having one ViewPort. I want to read entities from that ViewPort ?

 

Please help me I am new in this area !!!

Message 4 of 4
SENL1362
in reply to: sandeep_vaal

Google for:
// ViewportExtensionMethods.cs (c) 2007-2012 Tony Tanzillo
or
//Gile: GeometryExtensions (http://www.theswamp.org/index.php?topic=31865.0)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost