Community
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
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 !!!