Ejemplo n.º 1
0
        public static _3Matrix DrawBitmap(MouseEventArgs e, PictureBox graphicsDisplayBox, IList <Point <_3Vector, _3Vector> > pointsList, double pivotX, double pivotY)
        {
            decimal scaleX           = 4 / (decimal)graphicsDisplayBox.Width;
            decimal rotationCounterX = scaleX * e.X;

            decimal scaleY           = 4 / (decimal)graphicsDisplayBox.Height;
            decimal rotationCounterY = scaleY * e.Y;

            graphicsDisplayBox.Image = new Bitmap(graphicsDisplayBox.Width, graphicsDisplayBox.Height);
            var rotationResult = RotationMatrices.RotateByAngles((float)rotationCounterY, 0, (float)rotationCounterX);

            foreach (var pointPair in pointsList)
            {
                using (var g = System.Drawing.Graphics.FromImage(graphicsDisplayBox.Image))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    _3Vector pointA, pointB;
                    Pen      pen;
                    EffectColourScheme(rotationResult, pointPair, out pointA, out pointB, out pen);

                    g.DrawLine(pen, (float)((pointA.a) + pointA.b + pivotX)
                               , (float)((pointA.c) + pivotY)
                               , (float)((pointB.a) + pointB.b + pivotX)
                               , (float)((pointB.c) + pivotY));
                }
            }
            return(rotationResult);
        }
Ejemplo n.º 2
0
    void RotateLeft(float rotationAngle)
    {
        var matrix = RotationMatrices.GetLeftHandMatrix(1, 0, 1, rotationAngle, 0f, 0f);

        faceDir           = transform.forward;
        faceDir.z        *= -1;
        faceDir           = MatrixDotVector(matrix, faceDir);
        faceDir.z        *= -1;
        transform.forward = faceDir;
    }
Ejemplo n.º 3
0
    void RotateUp(float rotationAngle)
    {
        faceDir = transform.forward;
        Vector3 projFaceDir = faceDir;

        projFaceDir.y = 0f;
        float angle  = GetRotationAngle(new Vector3(0, 0, -1), projFaceDir) * Mathf.Deg2Rad;
        var   matrix = RotationMatrices.GetLeftHandMatrix(1, 0, 1, -angle, rotationAngle, angle);

        //var matrix1 = RotationMatrices.GetLeftHandMatrix (1, 0, 1, angle, 0, 0);
        //var matrix2 = RotationMatrices.GetLeftHandMatrix (1, 0, 1, 0, rotationAngle, 0);
        //var matrix3 = RotationMatrices.GetLeftHandMatrix (1, 0, 1, 0, 0, -angle);
        faceDir.z *= -1;
        faceDir    = MatrixDotVector(matrix, faceDir);
        //faceDir = MatrixDotVector (matrix1, faceDir);
        //faceDir = MatrixDotVector (matrix2, faceDir);
        //faceDir = MatrixDotVector (matrix3, faceDir);
        faceDir.z        *= -1;
        transform.forward = faceDir;
    }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates the vertex information and side lengths for all components of a piece according to all orientations
        /// </summary>
        /// <param name="piece">The piece to generate the information for</param>
        private static void GenerateVertexInformation(Piece piece)
        {
            // Init
            IReadOnlyList <Matrix> rotationMatrices = RotationMatrices.GetRotationMatrices();

            // Create vertex info for original
            foreach (var component in piece.Original.Components)
            {
                foreach (var vertexID in MeshConstants.VERTEX_IDS)
                {
                    component[vertexID] = GenerateVertex(vertexID, component, piece);
                }
            }
            foreach (var vertexID in MeshConstants.VERTEX_IDS)
            {
                piece.Original.BoundingBox[vertexID] = GenerateVertex(piece.Original.BoundingBox, vertexID);
            }

            // Create clones for the different orientations
            foreach (var orientation in MeshConstants.ORIENTATIONS)
            {
                piece[orientation] = piece.Original.Clone();
            }

            // Rotate vertices
            foreach (var orientation in MeshConstants.ORIENTATIONS)
            {
                foreach (var component in piece[orientation].Components)
                {
                    // Calculate rotated sides
                    Matrix referenceSidesVector = new Matrix(3, 1);
                    referenceSidesVector[0, 0] = component.Length;
                    referenceSidesVector[1, 0] = component.Width;
                    referenceSidesVector[2, 0] = component.Height;
                    Matrix rotatedSidesVector = rotationMatrices[orientation] * referenceSidesVector;
                    double length             = rotatedSidesVector[0, 0];
                    double width  = rotatedSidesVector[1, 0];
                    double height = rotatedSidesVector[2, 0];

                    // Calculate rotated vertices
                    Dictionary <int, MeshPoint> referencePoints = MeshConstants.VERTEX_IDS.ToDictionary(k => k, v => GenerateVertex(v, component, piece));
                    foreach (var vertexID in MeshConstants.VERTEX_IDS)
                    {
                        MeshPoint referencePoint       = referencePoints[vertexID];
                        Matrix    referencePointVector = new Matrix(3, 1);
                        referencePointVector[0, 0] = referencePoint.X;
                        referencePointVector[1, 0] = referencePoint.Y;
                        referencePointVector[2, 0] = referencePoint.Z;
                        Matrix rotatedPointVector = rotationMatrices[orientation] * referencePointVector;
                        component[vertexID] = new MeshPoint()
                        {
                            ParentPiece = piece
                        };
                        component[vertexID].X = rotatedPointVector[0, 0];
                        component[vertexID].Y = rotatedPointVector[1, 0];
                        component[vertexID].Z = rotatedPointVector[2, 0];
                    }

                    // Set sides (keep them positive - these are lengths after all)
                    component.Length = Math.Abs(length);
                    component.Width  = Math.Abs(width);
                    component.Height = Math.Abs(height);
                }
            }

            // Transform all coordinates into the first octant (for all orientations)
            foreach (var orientation in MeshConstants.ORIENTATIONS)
            {
                // Determine offset
                double minVertexX = Math.Abs(piece[orientation].Components.Min(c => MeshConstants.VERTEX_IDS.Min(v => c[v].X)));
                double minVertexY = Math.Abs(piece[orientation].Components.Min(c => MeshConstants.VERTEX_IDS.Min(v => c[v].Y)));
                double minVertexZ = Math.Abs(piece[orientation].Components.Min(c => MeshConstants.VERTEX_IDS.Min(v => c[v].Z)));
                // Move all components to first octant by applying the offset
                foreach (var component in piece[orientation].Components)
                {
                    // Move vertices
                    foreach (var vertexID in MeshConstants.VERTEX_IDS)
                    {
                        component[vertexID].X += minVertexX;
                        component[vertexID].Y += minVertexY;
                        component[vertexID].Z += minVertexZ;
                    }
                    // Update relative position of component accordingly
                    component.RelPosition.X = component.Vertices.Min(v => v.X);
                    component.RelPosition.Y = component.Vertices.Min(v => v.Y);
                    component.RelPosition.Z = component.Vertices.Min(v => v.Z);
                }
            }
            // Recalibrate vertex IDs
            foreach (var orientation in MeshConstants.ORIENTATIONS)
            {
                foreach (var component in piece[orientation].Components)
                {
                    List <double> x = new List <double>(MeshConstants.VERTEX_IDS.Where(vid => vid != 0).Select(vid => component[vid].X));
                    List <double> y = new List <double>(MeshConstants.VERTEX_IDS.Where(vid => vid != 0).Select(vid => component[vid].Y));
                    List <double> z = new List <double>(MeshConstants.VERTEX_IDS.Where(vid => vid != 0).Select(vid => component[vid].Z));
                    // vid 1
                    component[1].VertexID = 1;
                    component[1].X        = x.Min();
                    component[1].Y        = y.Min();
                    component[1].Z        = z.Min();
                    // vid 2
                    component[2].VertexID = 2;
                    component[2].X        = x.Max();
                    component[2].Y        = y.Min();
                    component[2].Z        = z.Min();
                    // vid 3
                    component[3].VertexID = 3;
                    component[3].X        = x.Min();
                    component[3].Y        = y.Max();
                    component[3].Z        = z.Min();
                    // vid 4
                    component[4].VertexID = 4;
                    component[4].X        = x.Max();
                    component[4].Y        = y.Max();
                    component[4].Z        = z.Min();
                    // vid 5
                    component[5].VertexID = 5;
                    component[5].X        = x.Min();
                    component[5].Y        = y.Min();
                    component[5].Z        = z.Max();
                    // vid 6
                    component[6].VertexID = 6;
                    component[6].X        = x.Max();
                    component[6].Y        = y.Min();
                    component[6].Z        = z.Max();
                    // vid 7
                    component[7].VertexID = 7;
                    component[7].X        = x.Min();
                    component[7].Y        = y.Max();
                    component[7].Z        = z.Max();
                    // vid 8
                    component[8].VertexID = 8;
                    component[8].X        = x.Max();
                    component[8].Y        = y.Max();
                    component[8].Z        = z.Max();
                }
            }

            // Seal the orientation clones
            foreach (var orientation in MeshConstants.ORIENTATIONS)
            {
                piece[orientation].Seal();
            }

            // Create vertex info for bounding box of original
            foreach (var vertexID in MeshConstants.VERTEX_IDS)
            {
                piece.Original.BoundingBox[vertexID] = GenerateVertex(vertexID, piece.Original.BoundingBox, piece);
            }

            // Create vertex info for all orientations bounding boxes
            foreach (var orientation in MeshConstants.ORIENTATIONS)
            {
                foreach (var vertexID in MeshConstants.VERTEX_IDS)
                {
                    piece[orientation].BoundingBox[vertexID] = GenerateVertex(vertexID, piece[orientation].BoundingBox, piece);
                }
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            DoubleMatrix a = new Double[, ] {
                { 6F, 5F },
                { 2F, 3F }
            };
            DoubleMatrix b = new Double[, ] {
                { 3F, 2F, 3F, 4F },
                { 2F, 3F, 4F, 5F }
            };

            Console.WriteLine("A = \n" + a);
            Console.WriteLine("B = \n" + b);
            Console.WriteLine("A x B = \n" + (a * b));
            Console.WriteLine("A.Rows[0] = \n" + a.Rows[0]);
            Console.WriteLine("A.Rows[1] = \n" + a.Rows[1]);
            Console.WriteLine("A.Column[1] = B.Column[0]");
            a.Columns[1] = b.Columns[0];
            Console.WriteLine("A = \n" + a);

            DoubleMatrix c = new Double[, ] {
                { 4F, 2F, 2F },
                { 4F, 6F, 8F },
                { -2F, 2F, 4F }
            };

            Console.WriteLine("C = \n" + c);
            Console.WriteLine("C.Inverse = \n" + c.Inverse);
            Console.WriteLine("C x C.Inverse = \n" + c * c.Inverse);

            double       theta = -45 * Math.PI / 180;
            DoubleMatrix r2_1  = new Double[, ] {
                { Math.Cos(theta), -Math.Sin(theta) }, { Math.Sin(theta), Math.Cos(theta) }
            };
            DoubleMatrix v2_1 = new Double[, ] {
                { 1, 0 }
            };
            var rotated = r2_1 * v2_1.Transposed;

            Console.WriteLine("theta={3},\n{0}\n*\n{1}\n={2}", r2_1, v2_1.Transposed, rotated, theta * 180 / Math.PI);

            double       s   = Math.Sqrt(2) / 2;
            DoubleMatrix zxy = new Double[, ] {
                { 1, 0, s }, { 0, s, s *s }, { -s * s, -s, s *s }
            };
            DoubleMatrix v3_1 = new Double[, ] {
                { 1 }, { 0 }, { 1 }
            };

            rotated = zxy * v3_1;
            Console.WriteLine(rotated);

            Double       deg2rad = Math.PI / 180;
            DoubleMatrix x_rot   = RotationMatrices.GetStandardRotationMatrix(0, 2, 0, -45 * deg2rad, 0, 0);
            DoubleMatrix v101    = new DoubleMatrix(new Double[, ] {
                { 1 }, { 0 }, { 1 }
            });

            rotated = x_rot * v101;
            Console.WriteLine(rotated);

            Console.ReadKey();
        }