Пример #1
0
        public static double CalculateDistance(Point3D firstPoint, Point3D secondPoint)
        {
            double deltaX = secondPoint.X - firstPoint.X;
            double deltaY = secondPoint.Y - firstPoint.Y;
            double deltaZ = secondPoint.Z - firstPoint.Z;

            double distance = (double)Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
            return distance;
        }
Пример #2
0
        public LRBuilder(Point3D[] vertices, Triangle[] triangles)
        {
            IFLTriangleMesh iflTriangleMesh = new IFLTriangleMesh();
            iflTriangleMesh.V = vertices;
            iflTriangleMesh.Triangles = triangles;

            IConverter<CTTriangleMesh> iflToCt = Converter.Converter.GetSpecificConverter<IFLTriangleMesh, CTTriangleMesh>(iflTriangleMesh);
            DateTime startTime = DateTime.Now;

            this.ctTriangleMesh = iflToCt.Convert();

            DateTime endTime = DateTime.Now;
            TimeSpan span = endTime.Subtract(startTime);

            Init();
        }
Пример #3
0
        public static List<Point3D> ReadFromFile()
        {
            string path = @"..\..\Points.txt";
            List<Point3D> output = new List<Point3D>();

            using (StreamReader reader = new StreamReader(path))
            {
                while (!reader.EndOfStream)
                {
                    var coordinates = reader.ReadLine()
                        .Trim()
                        .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                        .ToArray();

                    Point3D pointToAdd = new Point3D(double.Parse(coordinates[0]), double.Parse(coordinates[1]), double.Parse(coordinates[2]));

                    output.Add(pointToAdd);
                }
            }
            Console.WriteLine("The file is readed!");
            return output;
        }
Пример #4
0
        /// <summary>
        /// Loads the input file.
        /// I property values remain unchanged since the last run, then the file is not reloaded, unless the Realod property is set to true.
        /// </summary>
        public IFLTriangleMesh Execute(string fn)
        {
            // first pass determines the number of vertices and the number of triangles
            StreamReader sr;
            try
            {
                sr = new StreamReader(fn);
            }
            catch (FileNotFoundException fnfe)
            {
                throw new Exception(fnfe.Message);
            }

            int triangleCount = 0;
            int vertexCount = 0;
            int lineCount = 0;
            int textureCoordinateCount = 0;
            string line = sr.ReadLine();

            bool normalsFound = false;

            while (line != null)
            {
                line = line.Trim();

                if (line.StartsWith("v ")) vertexCount++;
                if (line.StartsWith("vt ")) textureCoordinateCount++;
                if (line.StartsWith("vn ")) normalsFound = true;
                if ((line.StartsWith("f ")) || (line.StartsWith("fo ")) || line.StartsWith("f\t"))
                {
                    triangleCount++;
                    if (line.Split(new char[] { ' ', '\t' }).Length == 5)
                        triangleCount++;
                }
                line = sr.ReadLine();
                lineCount++;
            }

            sr.Close();

            // second pass performs the actual parsing
            sr = new StreamReader(fn);
            Point3D[] vertices = new Point3D[vertexCount];
            Triangle[] triangles = new Triangle[triangleCount];

            int vi = 0;
            int ti = 0;
            int ni = 0;
            int tci = 0;
            Point3D point;
            Triangle triangle, textureTriangle;
            NumberFormatInfo nfi = new NumberFormatInfo();
            nfi.NumberDecimalSeparator = ".";
            nfi.NumberGroupSeparator = ",";

            int linePos = 0;
            line = sr.ReadLine();
            while (line != null)
            {
                line = line.Trim();

                //parsing of a vertex
                if (line.StartsWith("v "))
                {
                    string[] coords = line.Split(new char[] { ' ' }, 4, StringSplitOptions.RemoveEmptyEntries);

                    double c1 = double.Parse(coords[1], nfi);
                    double c2 = double.Parse(coords[2], nfi);
                    double c3 = double.Parse(coords[3], nfi);

                    point = new Point3D { X = c1, Y = c2, Z = c3 };
                    vertices[vi] = point;
                    vi++;
                }

                // parsing of a triangle
                if ((line.StartsWith("f ")) || (line.StartsWith("fo ")) || (line.StartsWith("f\t")))
                {
                    string[] indices = line.Split(new char[] { ' ', '\t' }, 5, StringSplitOptions.RemoveEmptyEntries);

                    int t1 = 0;
                    int t2 = 0;
                    int t3 = 0;

                    string[] parts = indices[1].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    int v1 = int.Parse(parts[0]) - 1;
                    if (parts.Length > 1)
                        t1 = int.Parse(parts[1]) - 1;

                    parts = indices[2].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    int v2 = int.Parse(parts[0]) - 1;
                    if (parts.Length > 1)
                        t2 = int.Parse(parts[1]) - 1;

                    parts = indices[3].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    int v3 = int.Parse(parts[0]) - 1;
                    if (parts.Length > 1)
                        t3 = int.Parse(parts[1]) - 1;

                    if (flipNormals)
                    {
                        triangle = new Triangle { Vertex1 = v1, Vertex2 = v3, Vertex3 = v2 };
                        textureTriangle = new Triangle { Vertex1 = t1, Vertex2 = t3, Vertex3 = t2 };
                    }
                    else
                    {
                        triangle = new Triangle { Vertex1 = v1, Vertex2 = v2, Vertex3 = v3 };
                        textureTriangle = new Triangle { Vertex1 = t1, Vertex2 = t2, Vertex3 = t3 };
                    }

                    if (indices.Length == 4)
                    {
                        triangles[ti] = triangle;
                        ti++;
                    }

                    if (indices.Length == 5)
                    {
                        parts = indices[4].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        v2 = int.Parse(parts[0]) - 1;
                        if (parts.Length > 1)
                            t2 = int.Parse(parts[1]) - 1;

                        if (flipNormals)
                        {
                            triangle = new Triangle { Vertex1 = v1, Vertex2 = v3, Vertex3 = v2 };
                            textureTriangle = new Triangle { Vertex1 = t1, Vertex2 = t2, Vertex3 = t3 };
                        }
                        else
                        {
                            triangle = new Triangle { Vertex1 = v1, Vertex2 = v3, Vertex3 = v2 };
                            textureTriangle = new Triangle { Vertex1 = t1, Vertex2 = t2, Vertex3 = t3 };
                        }
                        triangles[ti] = triangle;
                        ti++;
                    }
                }
                line = sr.ReadLine();
                linePos++;
            }

            // creating an output Mesh instance
            output = new IFLTriangleMesh();
            output.V = vertices;
            output.Triangles = triangles;
            return (output);
        }
Пример #5
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.DarkGray);
            c = new Color(15, 15, 15);

            checkInput();

            spriteBatch.Begin();
            spriteBatch.DrawString(font, "N - Next corner, P - Previous corner, O - Opposite corner, S - Next corner around vertex", new Vector2(10, 10), Color.Black);
            spriteBatch.DrawString(font, "F - Next ring vertex, B - Previous ring vertex", new Vector2(10, 30), Color.Black);
            spriteBatch.End();

            // reset what spritebatch has changed
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;

            EffectPass pass = effect.CurrentTechnique.Passes[0];

            pass.Apply();

            foreach (Triangle t in lrTriangleMesh.T)
            {
                drawTriangle(t, lrTriangleMesh.V, GraphicsDevice, Color.Red);
            }

            int mr = lrTriangleMesh.MR;

            for (int i = 0; i < mr; i++)
            {
                Point3D[] p = new Point3D[3];

                //Left triangle
                p[0] = lrTriangleMesh.V[i];
                p[1] = lrTriangleMesh.V[lrTriangleMesh.VN(i)];
                p[2] = lrTriangleMesh.V[lrTriangleMesh.VL(i)];

                drawTriangle(p, GraphicsDevice, Color.Brown);

                drawLine(p, 1, 2, GraphicsDevice, Color.Black);
                drawLine(p, 2, 0, GraphicsDevice, Color.Black);

                //Right triangle
                p[2] = lrTriangleMesh.V[lrTriangleMesh.VR(i)];

                drawTriangle(p, GraphicsDevice, Color.Lavender);

                drawLine(p, GraphicsDevice);
                drawLine(p, 1, 2, GraphicsDevice, Color.Black);
                drawLine(p, 2, 0, GraphicsDevice, Color.Black);
            }

            currentCube.Draw(GraphicsDevice);

            base.Draw(gameTime);
        }
Пример #6
0
        private void drawTriangle(Point3D[] p, GraphicsDevice d, Color c)
        {
            VertexPositionColor[] tri = {new VertexPositionColor(Vector3Extension.CreateV3FromP3(p[0]), c),
                             new VertexPositionColor(Vector3Extension.CreateV3FromP3(p[1]), c),
                             new VertexPositionColor(Vector3Extension.CreateV3FromP3(p[2]), c)};

            d.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, tri, 0, 1);
        }
Пример #7
0
 private void drawTriangle(Point3D[] p, GraphicsDevice d)
 {
     drawTriangle(p, d, nextColor());
 }
Пример #8
0
 private void drawTriangle(Triangle t, Point3D[] points, GraphicsDevice d)
 {
     drawTriangle(t, points, d, nextColor());
 }
Пример #9
0
 private void drawLine(Point3D[] p, GraphicsDevice d, Color c)
 {
     VertexPositionColor[] li = {new VertexPositionColor(Vector3Extension.CreateV3FromP3(p[0]), c),
                      new VertexPositionColor(Vector3Extension.CreateV3FromP3(p[1]), c)};
     d.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, li, 0, 1);
 }
Пример #10
0
 private void drawLine(Point3D[] p, GraphicsDevice d)
 {
     drawLine(p, d, Color.Red);
 }