Exemplo n.º 1
0
        public void Transpose_Instance()
        {
            var mat = new float3x3(1, 1, 1, 2, 2, 2, 3, 3, 3);

            var actual = mat.Transpose();

            Assert.Equal(new float3x3(1, 2, 3, 1, 2, 3, 1, 2, 3), actual);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Calculates the vertex position so that it is parallel to the x-y plane.
        /// </summary>
        /// <param name="vertPos">Original vertex position.</param>
        /// <param name="normal">The normal of the polygon the vertex belongs to. Used as new Z axis.</param>
        /// <returns></returns>
        internal static float3 Reduce2D(this float3 vertPos, float3 normal)
        {
            normal = normal.Normalize(); //New z axis

            //If the normal equals the z axis of the world coodrinate system: reflect the point on the y axis.
            if (normal == float3.UnitZ)
            {
                var rot = new float3x3(-1, 0, 0,
                                       0, 1, 0,
                                       0, 0, 1);
                return(vertPos * rot);
            }

            //If the normal equals the negative z axis of the world coodrinate system: use the original coordinates.
            if (normal == float3.UnitZ * -1)
            {
                return(vertPos);
            }

            var v2 = float3.Cross(normal, float3.UnitZ); //rotation axis - new x axis

            v2 = v2.Normalize();
            var v3 = float3.Cross(normal, v2); //new y axis

            v3 = v3.Normalize();

            //Calculate change-of-basis matrix (orthonormal matrix).
            var row1 = new float3(v3.x, v2.x, normal.x);
            var row2 = new float3(v3.y, v2.y, normal.y);
            var row3 = new float3(v3.z, v2.z, normal.z);

            //vector in new basis * changeOfBasisMat = vector in old basis
            var changeOfBasisMat = new float3x3(row1, row2, row3);

            //In an orthonomal matrix the inverse equals the transpose, thus the transpose can be used to calculate vector in new basis (transpose * vector = vector in new basis).
            var transposeMat = new float3x3(changeOfBasisMat.Row0, changeOfBasisMat.Row1, changeOfBasisMat.Row2);

            transposeMat = transposeMat.Transpose();

            var newVert = transposeMat * vertPos;


            //Round, to get rid of potential exponent representation.
            var vecX = System.Math.Round(newVert.x, 5);
            var vecY = System.Math.Round(newVert.y, 5);
            var vecZ = System.Math.Round(newVert.z, 5);

            newVert = new float3((float)vecX, (float)vecY, (float)vecZ);

            return(newVert);
        }