Example #1
0
        private void ApplyProjection()
        {
            double fovY         = Math.PI / 2.0;
            double translationZ = -Target.ActualHeight / Math.Tan(fovY / 2.0);

            Matrix3D centerImageAtOrigin = TranslationTransform(
                -Target.ActualWidth / 2.0,
                -Target.ActualHeight / 2.0, 0);
            Matrix3D rotateAboutY = RotateYTransform(this.Owner.Transform.Rotation.Y);

            Matrix3D perspective = PerspectiveTransformFovRH(fovY,
                                                             Target.ActualWidth / Target.ActualHeight,
                                                             1.0,
                                                             1000.0);

            Matrix3D viewport = ViewportTransform(Target.ActualWidth, Target.ActualHeight);

            Matrix3D m = centerImageAtOrigin * TranslationTransform(
                -this.Owner.Transform.Position.X + Target.ActualWidth / 2.0,
                -this.Owner.Transform.Position.Y + Target.ActualHeight / 2.0,
                -this.Owner.Transform.Position.Z);

            m = m * CreateScaleTransform(1, 1, -1);
            m = m * rotateAboutY;
            m = m * perspective;
            m = m * viewport;

            Matrix3DProjection m3dProjection = new Matrix3DProjection();

            m3dProjection.ProjectionMatrix = m;

            Target.Projection = m3dProjection;
        }
Example #2
0
        private void Test()
        {
            var test = new TransformGroup
            {
                Children = new TransformCollection
                {
                    new TranslateTransform { X = 10, Y = 20 },
                    new RotateTransform { Angle = 45 }
                }
            };
            var v = test.Value;


            var st = new TransformGroup { Children = new TransformCollection { new SkewTransform { AngleX = 45 } } };
            var tt = new TranslateTransform { X = 1 };

            var tg1 = new TransformGroup { Children = new TransformCollection { st, tt } };
            var tg2 = new TransformGroup { Children = new TransformCollection { tt, st } };

            var tg3 = new TransformGroup { Children = new TransformCollection { st } };
            var tg4 = new TransformGroup { Children = new TransformCollection { tt } };

            var tg5 = new TransformGroup();
            tg5.Children.Add(new TranslateTransform { X = 1, Y = 2 });
            tg5.Children.Add(new RotateTransform { Angle = -90 });

            var tg6 = new TransformGroup();
            tg6.Children.Add(new RotateTransform { Angle = -90 });
            tg6.Children.Add(new TranslateTransform { X = 1, Y = 2 });

            var p = new RotateTransform { Angle = 90 }.Transform(new Point(1, 2));

            var m3 = new Matrix3DProjection();
        }
        // <SnippetMatrix3DProjectionSample_code>
        private void ApplyProjection(Object sender, PointerRoutedEventArgs e)
        {
            // Translate the image along the negative Z-axis such that it occupies 50% of the
            // vertical field of view.
            double fovY         = Math.PI / 2.0;
            double translationZ = -BeachImage.ActualHeight / Math.Tan(fovY / 2.0);
            double theta        = 20.0 * Math.PI / 180.0;

            // You can create a 3D effect by creating a number of simple
            // tranformation Matrix3D matrixes and then multiply them together.
            Matrix3D centerImageAtOrigin = TranslationTransform(
                -BeachImage.ActualWidth / 2.0,
                -BeachImage.ActualHeight / 2.0, 0);
            Matrix3D invertYAxis             = CreateScaleTransform(1.0, -1.0, 1.0);
            Matrix3D rotateAboutY            = RotateYTransform(theta);
            Matrix3D translateAwayFromCamera = TranslationTransform(0, 0, translationZ);
            Matrix3D perspective             = PerspectiveTransformFovRH(fovY,
                                                                         LayoutRoot.ActualWidth / LayoutRoot.ActualHeight, // aspect ratio
                                                                         1.0,                                              // near plane
                                                                         1000.0);                                          // far plane
            Matrix3D viewport = ViewportTransform(LayoutRoot.ActualWidth, LayoutRoot.ActualHeight);

            Matrix3D m = Matrix3DHelper.Multiply(centerImageAtOrigin, invertYAxis);

            m = Matrix3D.Multiply(m, rotateAboutY);
            m = Matrix3D.Multiply(m, translateAwayFromCamera);
            m = Matrix3D.Multiply(m, perspective);
            m = Matrix3D.Multiply(m, viewport);

            Matrix3DProjection m3dProjection = new Matrix3DProjection();

            m3dProjection.ProjectionMatrix = m;

            BeachImage.Projection = m3dProjection;
        }
        // <SnippetMatrix3DProjectionSimple_code>
        private void ApplyProjection(Object sender, PointerRoutedEventArgs e)
        {
            Matrix3D m = new Matrix3D();

            // This matrix simply translates the image 100 pixels
            // down and 100 pixels right.
            m.M11     = 1.0; m.M12 = 0.0; m.M13 = 0.0; m.M14 = 0.0;
            m.M21     = 0.0; m.M22 = 1.0; m.M23 = 0.0; m.M24 = 0.0;
            m.M31     = 0.0; m.M32 = 0.0; m.M33 = 1.0; m.M34 = 0.0;
            m.OffsetX = 100; m.OffsetY = 100; m.OffsetZ = 0; m.M44 = 1.0;

            Matrix3DProjection m3dProjection = new Matrix3DProjection();

            m3dProjection.ProjectionMatrix = m;

            BeachImage.Projection = m3dProjection;
        }
Example #5
0
        private static Matrix3DProjection EnsureProjection(FrameworkElement view)
        {
            var projection       = view.Projection;
            var matrixProjection = projection as Matrix3DProjection;

            if (projection != null && matrixProjection == null)
            {
                throw new InvalidOperationException("Unknown projection set on framework element.");
            }

            if (matrixProjection == null)
            {
                matrixProjection = new Matrix3DProjection();
                view.Projection  = matrixProjection;
            }

            return(matrixProjection);
        }
Example #6
0
        void UpdateCatcher()
        {
            if (_clickCatcher == null)
            {
                return;
            }

            //_clickCatcher.Background = new SolidColorBrush (Colors.Red);
            //_clickCatcher.Opacity = .5;

            try {
                // In this case Child is the _clickCatcher's parent
                GeneralTransform general_xform = Child.TransformToVisual(null);

                if (general_xform is Transform)
                {
                    var xform = general_xform as Transform;

                    // clear any projections
                    _clickCatcher.Projection      = null;
                    _clickCatcher.RenderTransform = (Transform)xform.Inverse;
                }
                else if (general_xform is InternalTransform)
                {
                    var internal_xform = general_xform as InternalTransform;
                    var projection     = new Matrix3DProjection();

                    projection.ProjectionMatrix = ((InternalTransform)internal_xform.Inverse).Matrix;

                    // clear any render transforms;
                    _clickCatcher.RenderTransform = null;
                    _clickCatcher.Projection      = projection;
                }
                else
                {
                    throw new Exception("Unknown Transform Type");
                }
            } catch (ArgumentException e) {
                // Drop errors looking up the transform
            }

            _clickCatcher.Height = Application.Current.Host.Content.ActualHeight;
            _clickCatcher.Width  = Application.Current.Host.Content.ActualWidth;
        }
Example #7
0
        private void ApplyProjection(Object sender, PointerRoutedEventArgs e)
        {
            // Translate the image along the negative Z-axis such that it occupies 50% of the
            // vertical field of view.
            Matrix3D m = new Matrix3D();

            // This matrix simply translates the image 100 pixels
            // down and 100 pixels right.
            m.M11     = 1.0; m.M12 = 0.0; m.M13 = 0.0; m.M14 = 0.0;
            m.M21     = 0.0; m.M22 = 1.0; m.M23 = 0.0; m.M24 = 0.0;
            m.M31     = 0.0; m.M32 = 0.0; m.M33 = 1.0; m.M34 = 0.0;
            m.OffsetX = 100; m.OffsetY = 100; m.OffsetZ = 0; m.M44 = 1.0;

            Matrix3DProjection m3dProjection = new Matrix3DProjection();

            m3dProjection.ProjectionMatrix = m;

            BeachImage.Projection = m3dProjection;
        }
Example #8
0
File: Popup.cs Project: dfr0/moon
		void UpdateCatcher ()
		{
			if (_clickCatcher == null)
				return;

			//_clickCatcher.Background = new SolidColorBrush (Colors.Red);
			//_clickCatcher.Opacity = .5;

			try {
				// In this case Child is the _clickCatcher's parent
				GeneralTransform general_xform = Child.TransformToVisual (null);

				if (general_xform is Transform) {
					var xform = general_xform as Transform;
					
					// clear any projections
					_clickCatcher.Projection = null;
					_clickCatcher.RenderTransform = (Transform)xform.Inverse;
				} else if (general_xform is InternalTransform) {
					var internal_xform = general_xform as InternalTransform;
					var projection = new Matrix3DProjection();
					
					projection.ProjectionMatrix = ((InternalTransform)internal_xform.Inverse).Matrix;

					// clear any render transforms;
					_clickCatcher.RenderTransform = null;
					_clickCatcher.Projection = projection;
				} else {
				        throw new Exception ("Unknown Transform Type");
				}
			} catch (ArgumentException e) {
				// Drop errors looking up the transform
			}

			_clickCatcher.Height = Application.Current.Host.Content.ActualHeight;
			_clickCatcher.Width = Application.Current.Host.Content.ActualWidth;		
		}
        private void ApplyPerspectiveProjection(
            FrameworkElement element,
            double centerXOfRotation = 0.5,
            double centerYOfRotation = 0.5,
            double centerZOfRotation = 0.0,
            double rotationX         = 0.0,
            double rotationY         = 0.0,
            double rotationZ         = 0.0,
            double localOffsetX      = 0.0,
            double localOffsetY      = 0.0,
            double localOffsetZ      = 0.0,
            double globalOffsetX     = 0.0,
            double globalOffsetY     = 0.0,
            double globalOffsetZ     = 0.0,
            double fovY = 57.0)         // The field of view of a Planeprojection is 57.0 - thanks to Jaime Rodriguez http://blogs.msdn.com/b/jaimer/archive/2009/06/03/silverlight3-planeprojection-primer.aspx
        {
            var m = new Matrix3D();

            double width  = element.ActualWidth;
            double height = element.ActualHeight;

            // World definition
            //-----------------

            // Defines the central point of rotations for the element
            // (that is the origin of the coordinate system)
            m *= Helper3D.GetTranslationMatrix(
                -width * centerXOfRotation,
                -height * centerYOfRotation,
                -centerZOfRotation);

            // Local translation
            m *= Helper3D.GetTranslationMatrix(
                localOffsetX,
                localOffsetY,
                localOffsetZ);

            // Inverts the Y axis (in 2D, the direction is down, in 3D, it's up)
            // The resulting coordinate system is a left-handed one
            m *= Helper3D.GetScaleMatrix(1.0, -1.0, 1.0);

            //// Rotates the element
            if (rotationX != 0.0)
            {
                m *= Helper3D.GetXRotationMatrix(rotationX);
            }
            if (rotationY != 0.0)
            {
                m *= Helper3D.GetYRotationMatrix(rotationY);
            }
            if (rotationZ != 0.0)
            {
                m *= Helper3D.GetZRotationMatrix(rotationZ);
            }

            // Global translation
            m *= Helper3D.GetTranslationMatrix(
                globalOffsetX,
                globalOffsetY,
                globalOffsetZ);

            // View defintion
            //---------------
            // Camera's position
            // Moves the camera on the Z-axis so that the element fits
            // (the sign is negative because a camera inverts the Z axis in its coordinate system)
            m *= Helper3D.GetTranslationMatrix(0.0, 0.0,
                                               -height / Math.Tan(GeometryHelper.DegreeToRadian(fovY) / 2.0));

            // Projection definition
            //----------------------
            // Represents the 3D scene in perspective
            // The clip planes distance of a Planeprojection is 999.0 - thanks to Jaime Rodriguez http://blogs.msdn.com/b/jaimer/archive/2009/06/03/silverlight3-planeprojection-primer.aspx
            m *= Helper3D.GetPerspectiveMatrix(
                fovY,
                width / height,   // image format
                1.0,              // near plane
                1000.0);          // far plane
            //0.0,              // near plane
            //999.0);          // far plane

            // Viewport definition
            //--------------------
            // Re-inverts the Y axis, to fit in the 2D coordinate system
            // Cancels the initial rotation point translation
            m *= new Matrix3D
            {
                M11     = width,
                M22     = -height,
                OffsetX = width * centerXOfRotation,
                OffsetY = height * centerYOfRotation
            };

            // Applies the projection
            //-----------------------
            Matrix3DProjection m3dProjection = new Matrix3DProjection();

            m3dProjection.ProjectionMatrix = m;
            element.Projection             = m3dProjection;
        }
      private void Detect()
      {
         if (isDetecting || !isInitialized)
         {
            return;
         }

         isDetecting = true;
         var stopwatch = Stopwatch.StartNew();

         try
         {
            // Update buffer size
            var pixelWidth = (int)photoCamera.PreviewResolution.Width;
            var pixelHeight = (int)photoCamera.PreviewResolution.Height;
            if (buffer == null || buffer.Length != pixelWidth * pixelHeight)
            {
               buffer = new byte[pixelWidth * pixelHeight];

               // Create constant transformations instances
               // Center at origin of the 256x256 controls
               centerAtOrigin = Matrix3DFactory.CreateTranslation(-128, -128, 0);
               // Swap the y-axis and scale down by half
               scale = Matrix3DFactory.CreateScale(0.5, -0.5, 0.5);
               // Viewport transformation
               viewport = Matrix3DFactory.CreateViewportTransformation(pixelWidth, pixelHeight);
               matrix3DProjection = new Matrix3DProjection();
               Txt.Projection = matrix3DProjection;
               Img.Projection = matrix3DProjection;
            }

            // Grab snapshot
            photoCamera.GetPreviewBufferY(buffer);

            // Detect
            var dr = arDetector.DetectAllMarkers(buffer, pixelWidth, pixelHeight);

            // Draw the detected squares
            //bitmap.Clear();
            //ViewportOverlay.Source = bitmap;

            // Calculate the projection matrix
            if (dr.HasResults)
            {
               // Calculate the complete transformation matrix based on the first detection result
               var world = centerAtOrigin * scale * dr[0].Transformation;

               // Calculate the final transformation matrix by using the camera projection matrix 
               var m = Matrix3DFactory.CreateViewportProjection(world, Matrix3D.Identity, arDetector.Projection, viewport);

               // Apply the final transformation matrix to the TextBox
               matrix3DProjection.ProjectionMatrix = m;

               //// Draw the detected squares
               //foreach (var r in dr)
               //{
               //   bitmap.DrawQuad((int)r.Square.P1.X, (int)r.Square.P1.Y, (int)r.Square.P2.X, (int)r.Square.P2.Y, (int)r.Square.P3.X, (int)r.Square.P3.Y, (int)r.Square.P4.X, (int)r.Square.P4.Y, Colors.Red);
               //}
            }
         }
         finally
         {
            isDetecting = false;
            stopwatch.Stop();
            TxtDiag.Text = string.Format("{0} ms", stopwatch.ElapsedMilliseconds);
         }
      }
Example #11
0
        private void Test()
        {
            var test = new TransformGroup
            {
                Children = new TransformCollection
                {
                    new TranslateTransform {
                        X = 10, Y = 20
                    },
                    new RotateTransform {
                        Angle = 45
                    }
                }
            };
            var v = test.Value;


            var st = new TransformGroup {
                Children = new TransformCollection {
                    new SkewTransform {
                        AngleX = 45
                    }
                }
            };
            var tt = new TranslateTransform {
                X = 1
            };

            var tg1 = new TransformGroup {
                Children = new TransformCollection {
                    st, tt
                }
            };
            var tg2 = new TransformGroup {
                Children = new TransformCollection {
                    tt, st
                }
            };

            var tg3 = new TransformGroup {
                Children = new TransformCollection {
                    st
                }
            };
            var tg4 = new TransformGroup {
                Children = new TransformCollection {
                    tt
                }
            };

            var tg5 = new TransformGroup();

            tg5.Children.Add(new TranslateTransform {
                X = 1, Y = 2
            });
            tg5.Children.Add(new RotateTransform {
                Angle = -90
            });

            var tg6 = new TransformGroup();

            tg6.Children.Add(new RotateTransform {
                Angle = -90
            });
            tg6.Children.Add(new TranslateTransform {
                X = 1, Y = 2
            });

            var p = new RotateTransform {
                Angle = 90
            }.Transform(new Point(1, 2));

            var m3 = new Matrix3DProjection();
        }
Example #12
0
        private void Detect()
        {
            if (isDetecting || !isInitialized)
            {
                return;
            }

            isDetecting = true;
            var stopwatch = Stopwatch.StartNew();

            try
            {
                // Update buffer size
                var pixelWidth  = (int)photoCamera.PreviewResolution.Width;
                var pixelHeight = (int)photoCamera.PreviewResolution.Height;
                if (buffer == null || buffer.Length != pixelWidth * pixelHeight)
                {
                    buffer = new byte[pixelWidth * pixelHeight];

                    // Create constant transformations instances
                    // Center at origin of the 256x256 controls
                    centerAtOrigin = Matrix3DFactory.CreateTranslation(-128, -128, 0);
                    // Swap the y-axis and scale down by half
                    scale = Matrix3DFactory.CreateScale(0.5, -0.5, 0.5);
                    // Viewport transformation
                    viewport           = Matrix3DFactory.CreateViewportTransformation(pixelWidth, pixelHeight);
                    matrix3DProjection = new Matrix3DProjection();
                    Txt.Projection     = matrix3DProjection;
                    Img.Projection     = matrix3DProjection;
                }

                // Grab snapshot
                photoCamera.GetPreviewBufferY(buffer);

                // Detect
                var dr = arDetector.DetectAllMarkers(buffer, pixelWidth, pixelHeight);

                // Draw the detected squares
                //bitmap.Clear();
                //ViewportOverlay.Source = bitmap;

                // Calculate the projection matrix
                if (dr.HasResults)
                {
                    // Calculate the complete transformation matrix based on the first detection result
                    var world = centerAtOrigin * scale * dr[0].Transformation;

                    // Calculate the final transformation matrix by using the camera projection matrix
                    var m = Matrix3DFactory.CreateViewportProjection(world, Matrix3D.Identity, arDetector.Projection, viewport);

                    // Apply the final transformation matrix to the TextBox
                    matrix3DProjection.ProjectionMatrix = m;

                    //// Draw the detected squares
                    //foreach (var r in dr)
                    //{
                    //   bitmap.DrawQuad((int)r.Square.P1.X, (int)r.Square.P1.Y, (int)r.Square.P2.X, (int)r.Square.P2.Y, (int)r.Square.P3.X, (int)r.Square.P3.Y, (int)r.Square.P4.X, (int)r.Square.P4.Y, Colors.Red);
                    //}
                }
            }
            finally
            {
                isDetecting = false;
                stopwatch.Stop();
                TxtDiag.Text = string.Format("{0} ms", stopwatch.ElapsedMilliseconds);
            }
        }