public static Matrix4 GetProjectionMatrix(this ViewPort viewPort, double width, double height, ProjectionStyle projectionStyle) { // create transform Matrix4 transform; switch (projectionStyle) { case ProjectionStyle.OriginTopLeft: transform = viewPort.GetTransformationMatrixWindowsStyle(width, height); break; case ProjectionStyle.OriginBottomLeft: transform = viewPort.GetTransformationMatrixCartesianStyle(width, height); break; case ProjectionStyle.OriginCenter: transform = viewPort.GetTransformationMatrixDirect3DStyle(width, height); break; default: throw new NotSupportedException(); } return(transform); }
public static IEnumerable <ProjectedEntity> ProjectTo2D(Drawing drawing, ViewPort viewPort, double width, double height, ProjectionStyle projectionStyle) { // create transform Matrix4 transform; switch (projectionStyle) { case ProjectionStyle.OriginTopLeft: transform = viewPort.GetTransformationMatrixWindowsStyle(width, height); break; case ProjectionStyle.OriginBottomLeft: transform = viewPort.GetTransformationMatrixCartesianStyle(width, height); break; case ProjectionStyle.OriginCenter: transform = viewPort.GetTransformationMatrixDirect3DStyle(width, height); break; default: throw new NotSupportedException(); } // project all entities var entities = new List <ProjectedEntity>(); foreach (var layer in from l in drawing.GetLayers() where l.EntityCount > 0 orderby l.Name select l) { foreach (var entity in layer.GetEntities().OrderBy(x => x.Id)) { var projected = Project(entity, layer, transform); if (projected != null) { entities.Add(projected); } } } return(entities); }
public void TransformationMatrixWindowsNonCenteredTest() { var viewPort = new ViewPort(new Point(100, 100, 0), Vector.ZAxis, Vector.YAxis, 100.0); var transform = viewPort.GetTransformationMatrixWindowsStyle(100, 100); var inverse = transform.Inverse(); // view port // 100,200 200,200 // 150,150 // 100,100 200,100 // // screen // 0,0 100,0 // 50,50 // 0,100 100,100 // center Assert.Equal(new Point(50, 50, 0), transform.Transform(new Point(150, 150, 0))); Assert.Equal(new Point(150, 150, 0), inverse.Transform(new Point(50, 50, 0))); // bottom left Assert.Equal(new Point(0, 100, 0), transform.Transform(new Point(100, 100, 0))); Assert.Equal(new Point(100, 100, 0), inverse.Transform(new Point(0, 100, 0))); // bottom right Assert.Equal(new Point(100, 100, 0), transform.Transform(new Point(200, 100, 0))); Assert.Equal(new Point(200, 100, 0), inverse.Transform(new Point(100, 100, 0))); // top left Assert.Equal(new Point(0, 0, 0), transform.Transform(new Point(100, 200, 0))); Assert.Equal(new Point(100, 200, 0), inverse.Transform(new Point(0, 0, 0))); // top right Assert.Equal(new Point(100, 0, 0), transform.Transform(new Point(200, 200, 0))); Assert.Equal(new Point(200, 200, 0), inverse.Transform(new Point(100, 0, 0))); }
public static ViewPort ShowAllViewPort(this IEnumerable <IPrimitive> primitives, Vector sight, Vector up, double viewPortWidth, double viewPortHeight, int pixelBuffer = DefaultPixelBuffer) { var drawingPlane = new Plane(Point.Origin, sight); var planeProjection = drawingPlane.ToXYPlaneProjection(); var allPoints = primitives .SelectMany(p => p.GetInterestingPoints()) .Select(p => planeProjection.Transform(p)); if (allPoints.Count() < 2) { return(new ViewPort(Point.Origin, Vector.ZAxis, Vector.YAxis, 10.0)); } var first = allPoints.First(); var minx = first.X; var miny = first.Y; var maxx = first.X; var maxy = first.Y; foreach (var point in allPoints.Skip(1)) { if (point.X < minx) { minx = point.X; } if (point.X > maxx) { maxx = point.X; } if (point.Y < miny) { miny = point.Y; } if (point.Y > maxy) { maxy = point.Y; } } var deltaX = maxx - minx; var deltaY = maxy - miny; // translate back out of XY plane var unproj = planeProjection.Inverse(); var bottomLeft = unproj.Transform(new Point(minx, miny, 0)); var topRight = unproj.Transform(new Point(minx + deltaX, miny + deltaY, 0)); var drawingHeight = deltaY; var drawingWidth = deltaX; double viewHeight, drawingToViewScale; var viewRatio = viewPortWidth / viewPortHeight; var drawingRatio = drawingWidth / drawingHeight; if (MathHelper.CloseTo(0.0, drawingHeight) || drawingRatio > viewRatio) { // fit to width var viewWidth = drawingWidth; viewHeight = viewPortHeight * viewWidth / viewPortWidth; drawingToViewScale = viewPortWidth / viewWidth; // add a buffer of some amount of pixels var pixelWidth = drawingWidth * drawingToViewScale; var newPixelWidth = pixelWidth + (pixelBuffer * 2); viewHeight *= newPixelWidth / pixelWidth; } else { // fit to height viewHeight = drawingHeight; drawingToViewScale = viewPortHeight / viewHeight; // add a buffer of some amount of pixels var pixelHeight = drawingHeight * drawingToViewScale; var newPixelHeight = pixelHeight + (pixelBuffer * 2); viewHeight *= newPixelHeight / pixelHeight; } // center viewport var tempViewport = new ViewPort(bottomLeft, sight, up, viewHeight); var pixelMatrix = tempViewport.GetTransformationMatrixWindowsStyle(viewPortWidth, viewPortHeight); var bottomLeftScreen = pixelMatrix.Transform(bottomLeft); var topRightScreen = pixelMatrix.Transform(topRight); // center horizontally var leftXGap = bottomLeftScreen.X; var rightXGap = viewPortWidth - topRightScreen.X; var xAdjust = Math.Abs((rightXGap - leftXGap) / 2.0) / drawingToViewScale; // center vertically var topYGap = topRightScreen.Y; var bottomYGap = viewPortHeight - bottomLeftScreen.Y; var yAdjust = Math.Abs((topYGap - bottomYGap) / 2.0) / drawingToViewScale; var newBottomLeft = new Point(bottomLeft.X - xAdjust, bottomLeft.Y - yAdjust, bottomLeft.Z); var newVp = tempViewport.Update(bottomLeft: newBottomLeft, viewHeight: viewHeight); return(newVp); }