Exemplo n.º 1
0
        private static List<MeshTriangularFace> getTriangles(List<AreaVertex> outline, int start, int stop)
        {
            List<MeshTriangularFace> triangles = new List<MeshTriangularFace>();
            var i = start;
            var firstNode = outline.ElementAt(start);
            var secondIndex = ++i;

            while(i<stop){
                var thirdIndex = ++i;

                var secondNode = outline.ElementAt(secondIndex);
                var thirdNode=outline.ElementAt(thirdIndex);
                //var t=thirdIndex;
                //bool allRightHanded = true;
                /*while (t < stop)
                {
                    var tNode = outline.ElementAt(t++);

                    if (!isRightHanded(new List<AreaVertex>() { firstNode, secondNode, tNode }))
                    {
                        allRightHanded = false;
                        break;
                    }
                }
                if (!allRightHanded) continue;*/
                if (!isRightHanded(new List<AreaVertex>() { firstNode, secondNode, thirdNode }) ||
                    lineIntersect(firstNode, thirdNode, outline) ||
                    lineIntersect(secondNode, thirdNode, outline))
                {
                    continue;
                }
                MeshTriangularFace triangle;
                triangle = new MeshTriangularFace(start, secondIndex, thirdIndex);
                triangles.AddRange(getTriangles(outline, secondIndex, thirdIndex));
                secondIndex = thirdIndex;
                triangles.Add(triangle);

            }
            return triangles;
        }
Exemplo n.º 2
0
        protected Area ReadArea(Stream ms)
        {
            var currentZone = new Area {Content = new AreaContent()};
            var buffer = new byte[4];

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            int zoneDataVersion = BitConverter.ToInt32(buffer, 0);
            if (zoneDataVersion != 2)
                throw new InvalidDataException("Zone data version != 2 not supported!");

            ms.Read(buffer, 0, buffer.Length);
            currentZone.Id = BitConverter.ToInt32(buffer, 0);

            ms.Read(buffer, 0, buffer.Length);
            int idStrLen = BitConverter.ToInt32(buffer, 0);
            var idStrBuffer = new byte[Utils.RoundToNextDivBy4(idStrLen)];
            ms.Read(idStrBuffer, 0, idStrBuffer.Length);
            currentZone.Name = Encoding.UTF8.GetString(idStrBuffer).TrimEnd('\0');

            ms.AssertAreaMagic();

            var attachmentPt = new Point3D();
            ms.Read(buffer, 0, buffer.Length);
            attachmentPt.X = BitConverter.ToSingle(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            attachmentPt.Y = BitConverter.ToSingle(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            attachmentPt.Z = BitConverter.ToSingle(buffer, 0);
            currentZone.AttachmentPoint = attachmentPt;

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            var subParts = BitConverter.ToInt32(buffer, 0);

            for (int sp = 0; sp < subParts; sp++)
            {
                var aced = new AreaClipped();
                ms.Read(buffer, 0, buffer.Length);
                aced.StartTriangle = BitConverter.ToInt32(buffer, 0);
                ms.Read(buffer, 0, buffer.Length);
                aced.TriangleCount = BitConverter.ToInt32(buffer, 0);
                ms.Read(buffer, 0, buffer.Length);
                aced.StartVertex = BitConverter.ToInt32(buffer, 0);
                ms.Read(buffer, 0, buffer.Length);
                aced.VertexCount = BitConverter.ToInt32(buffer, 0);
                currentZone.Content.ClippedAreas.Add(aced);
            }

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderTriangle.StartTriangle = BitConverter.ToInt32(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderTriangle.TriangleCount = BitConverter.ToInt32(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderTriangle.StartVertex = BitConverter.ToInt32(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderTriangle.VertexCount = BitConverter.ToInt32(buffer, 0);

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderVertex.StartVertex = BitConverter.ToInt32(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderVertex.VertexCount = BitConverter.ToInt32(buffer, 0);

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            int vertexCount = BitConverter.ToInt32(buffer, 0);

            ms.Read(buffer, 0, buffer.Length);
            int trianglesCount = BitConverter.ToInt32(buffer, 0);

            for (int v = 0; v < vertexCount; v++)
            {
                var curVertex = new AreaVertex();

                ms.Read(buffer, 0, buffer.Length);
                curVertex.X = BitConverter.ToSingle(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                curVertex.Y = BitConverter.ToSingle(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                curVertex.Z = BitConverter.ToSingle(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                curVertex.W = BitConverter.ToSingle(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                curVertex.Center = BitConverter.ToSingle(buffer, 0);

                currentZone.Content.Vertices.Add(curVertex);
            }

            ms.AssertAreaMagic();

            for (int f = 0; f < trianglesCount; f++)
            {
                var currentTriangle = new MeshTriangularFace();

                ms.Read(buffer, 0, buffer.Length);
                currentTriangle.Point1 = BitConverter.ToInt32(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                currentTriangle.Point2 = BitConverter.ToInt32(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                currentTriangle.Point3 = BitConverter.ToInt32(buffer, 0);

                currentZone.Content.Triangles.Add(currentTriangle);
            }

            ms.AssertAreaMagic();
            ms.Seek(4, SeekOrigin.Current);

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            if (BitConverter.ToUInt32(buffer, 0) != 809782853)
                throw new InvalidDataException("END0 expected!");

            return currentZone;
        }
Exemplo n.º 3
0
        private static List<MeshTriangularFace> getBorderTriangles(int count)
        {
            var lower = count;
            var forwardLower = count+1;
            var upper = 2*count;
            var forwardUpper = 2 * count + 1;
            var triangles = new List<MeshTriangularFace>();
            for (var i = 0; i < count-1; i++)
            {
                /*
                var triangle1 = new MeshTriangularFace(lower + i, forwardLower + i, upper + i);
                var triangle2 = new MeshTriangularFace(upper + i, forwardLower + i, forwardUpper + i);
              */

                var triangle1 = new MeshTriangularFace(lower + i, upper + i, forwardLower + i);
                var triangle2 = new MeshTriangularFace(upper + i, forwardUpper + i, forwardLower + i);
                triangles.Add(triangle1);
                triangles.Add(triangle2);
            }
            var triangle3 = new MeshTriangularFace(2*count-1, lower, 3*count-1);
            var triangle4 = new MeshTriangularFace(3 * count - 1, lower, upper);
            /*
            var triangle3 = new MeshTriangularFace(2 * count - 1, 3 * count - 1, lower);
            var triangle4 = new MeshTriangularFace(3 * count - 1, upper, lower);*/
            triangles.Add(triangle3);
            triangles.Add(triangle4);
            return triangles;
        }