internal static Point Point3DInViewport3D(Viewport3D viewport, Matrix3D viewportToWorld, Point3D point)
 {
   Camera camera = viewport.Camera;
   Point3D point1 = viewportToWorld.Transform(point);
   Point3D point3D = Helper3D.CameraRotationTranslationMatrix(camera).Transform(point1);
   Point4D point2 = new Point4D(point3D.X, point3D.Y, point3D.Z, 1.0);
   Point4D point4D = AdornedToolBehavior3D.ProjectionMatrix(viewport.ActualWidth, viewport.ActualHeight, camera).Transform(point2);
   Point point3 = Math.Abs(point4D.W) >= 0.0 / 1.0 ? new Point(point4D.X / point4D.W, point4D.Y / point4D.W) : new Point(0.0, 0.0);
   return new Point((point3.X + 1.0) * viewport.ActualWidth / 2.0, viewport.ActualHeight * ((1.0 - point3.Y) / 2.0));
 }
 internal static Point3D ProjectionPoint3DTranslatedToMatchingOrthographicPosition(Viewport3D viewport, Matrix3D pointToWorldTransform, OrthographicCamera ortho, Point3D point)
 {
   Camera camera = viewport.Camera;
   Point3D point1 = pointToWorldTransform.Transform(point);
   Point3D point3D = Helper3D.CameraRotationTranslationMatrix(camera).Transform(point1);
   Point4D point2 = new Point4D(point3D.X, point3D.Y, point3D.Z, 1.0);
   Point4D point4D1 = AdornedToolBehavior3D.ProjectionMatrix(viewport.ActualWidth, viewport.ActualHeight, camera).Transform(point2);
   Point4D point3 = new Point4D(point4D1.X / point4D1.W, point4D1.Y / point4D1.W, 0.0, 1.0);
   Matrix3D matrix3D1 = AdornedToolBehavior3D.ProjectionMatrix(viewport.ActualWidth, viewport.ActualHeight, (Camera) ortho);
   if (Math.Abs(matrix3D1.Determinant) > 1E-16)
     matrix3D1.Invert();
   Point4D point4D2 = matrix3D1.Transform(point3);
   Point3D point4 = new Point3D(point4D2.X / point4D2.W, point4D2.Y / point4D2.W, point3D.Z);
   Matrix3D matrix3D2 = Helper3D.CameraRotationTranslationMatrix((Camera) ortho);
   matrix3D2.Invert();
   return matrix3D2.Transform(point4);
 }
        public static void DrawCube(DrawingContext drawingContext, Matrix matrix, Base3DElement element, Pen pen)
        {
            Viewport3DElement viewport1 = element.Viewport;

            if (viewport1 == null)
            {
                return;
            }
            Viewport3D viewport2                      = viewport1.ViewObject.PlatformSpecificObject as Viewport3D;
            Rect3D     localSpaceBounds               = element.LocalSpaceBounds;
            Matrix3D   viewport3DtoElement            = element.GetComputedTransformFromViewport3DToElement();
            Matrix3D   matrix3D                       = Helper3D.CameraRotationTranslationMatrix(viewport2.Camera);
            Matrix3D   cameraToObject                 = viewport3DtoElement * matrix3D;
            KeyValuePair <Point, bool> keyValuePair1  = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location);
            KeyValuePair <Point, bool> keyValuePair2  = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(localSpaceBounds.SizeX, 0.0, 0.0));
            KeyValuePair <Point, bool> keyValuePair3  = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(localSpaceBounds.SizeX, localSpaceBounds.SizeY, 0.0));
            KeyValuePair <Point, bool> keyValuePair4  = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(0.0, localSpaceBounds.SizeY, 0.0));
            KeyValuePair <Point, bool> keyValuePair5  = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(0.0, 0.0, localSpaceBounds.SizeZ));
            KeyValuePair <Point, bool> keyValuePair6  = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(localSpaceBounds.SizeX, 0.0, localSpaceBounds.SizeZ));
            KeyValuePair <Point, bool> keyValuePair7  = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(localSpaceBounds.SizeX, localSpaceBounds.SizeY, localSpaceBounds.SizeZ));
            KeyValuePair <Point, bool> keyValuePair8  = HighlightAdorner3D.Calculate2DPoint(viewport2, cameraToObject, viewport3DtoElement, matrix, localSpaceBounds.Location + new Vector3D(0.0, localSpaceBounds.SizeY, localSpaceBounds.SizeZ));
            StreamGeometry             streamGeometry = new StreamGeometry();
            StreamGeometryContext      context        = streamGeometry.Open();

            HighlightAdorner3D.DrawLine(context, keyValuePair1, keyValuePair2);
            HighlightAdorner3D.DrawLine(context, keyValuePair2, keyValuePair3);
            HighlightAdorner3D.DrawLine(context, keyValuePair3, keyValuePair4);
            HighlightAdorner3D.DrawLine(context, keyValuePair4, keyValuePair1);
            HighlightAdorner3D.DrawLine(context, keyValuePair5, keyValuePair6);
            HighlightAdorner3D.DrawLine(context, keyValuePair6, keyValuePair7);
            HighlightAdorner3D.DrawLine(context, keyValuePair7, keyValuePair8);
            HighlightAdorner3D.DrawLine(context, keyValuePair8, keyValuePair5);
            HighlightAdorner3D.DrawLine(context, keyValuePair6, keyValuePair2);
            HighlightAdorner3D.DrawLine(context, keyValuePair7, keyValuePair3);
            HighlightAdorner3D.DrawLine(context, keyValuePair5, keyValuePair1);
            HighlightAdorner3D.DrawLine(context, keyValuePair8, keyValuePair4);
            context.Close();
            streamGeometry.Freeze();
            drawingContext.DrawGeometry((Brush)null, pen, (System.Windows.Media.Geometry)streamGeometry);
        }