示例#1
0
        public static PolygonSet FromTess(Tess tess)
        {
            var output = new PolygonSet();

            for (int i = 0; i < tess.ElementCount; i++)
            {
                var poly = new Polygon();
                for (int j = 0; j < 3; j++)
                {
                    int index = tess.Elements[i * 3 + j];
                    if (index == -1)
                    {
                        continue;
                    }
                    var v = new PolygonPoint
                    {
                        X = tess.Vertices[index].Position.X,
                        Y = tess.Vertices[index].Position.Y
                    };
                    poly.Add(v);
                }
                output.Add(poly);
            }
            return(output);
        }
示例#2
0
        private void RefreshAsset(string name)
        {
            var asset = _data.GetAsset(name);

            _sw.Reset();

            foreach (var poly in asset.Polygons)
            {
                var v = new ContourVertex[poly.Count];
                for (int i = 0; i < poly.Count; i++)
                {
                    v[i].Position = new Vec3 {
                        X = poly[i].X, Y = poly[i].Y
                    };
                    v[i].Data = poly[i].Color;
                }
                _sw.Start();
                _tess.AddContour(v, poly.Orientation);
                _sw.Stop();
            }

            _sw.Start();
            _tess.Tessellate(_windingRule, ElementType.Polygons, _polySize, VertexCombine);
            _sw.Stop();

            var output = new PolygonSet();

            for (int i = 0; i < _tess.ElementCount; i++)
            {
                var poly = new Polygon();
                for (int j = 0; j < _polySize; j++)
                {
                    int index = _tess.Elements[i * _polySize + j];
                    if (index == -1)
                    {
                        continue;
                    }
                    var v = new PolygonPoint {
                        X     = _tess.Vertices[index].Position.X,
                        Y     = _tess.Vertices[index].Position.Y,
                        Color = (Color)_tess.Vertices[index].Data
                    };
                    poly.Add(v);
                }
                output.Add(poly);
            }

            statusMain.Text = string.Format("{0:F3} ms - {1} polygons (of {2} vertices) {3}", _sw.Elapsed.TotalMilliseconds, _tess.ElementCount, _polySize, _polySize == 3 ? "... triangles" : "");

            _canvas.Input  = asset.Polygons;
            _canvas.Output = output;
            _canvas.Invalidate();
        }
示例#3
0
        private static PolygonSet FromP2T(Poly2Tri.PolygonSet pset)
        {
            var result = new PolygonSet();

            foreach (var poly in pset.Polygons)
            {
                foreach (var tri in poly.Triangles)
                {
                    var rtri = new Polygon();
                    rtri.Add(new PolygonPoint {
                        X = tri.Points[0].Xf, Y = tri.Points[0].Yf
                    });
                    rtri.Add(new PolygonPoint {
                        X = tri.Points[1].Xf, Y = tri.Points[1].Yf
                    });
                    rtri.Add(new PolygonPoint {
                        X = tri.Points[2].Xf, Y = tri.Points[2].Yf
                    });
                    result.Add(rtri);
                }
            }
            return(result);
        }
示例#4
0
        public static PolygonSet LoadDat(Stream resourceStream)
        {
            var                points  = new List <PolygonPoint>();
            var                polys   = new PolygonSet();
            int                lineNum = 0;
            string             line;
            Color              currentColor       = Color.White;
            ContourOrientation currentOrientation = ContourOrientation.Original;

            using (var stream = new StreamReader(resourceStream))
            {
                while ((line = stream.ReadLine()) != null)
                {
                    ++lineNum;
                    line = line.Trim();
                    if (string.IsNullOrEmpty(line))
                    {
                        if (points.Count > 0)
                        {
                            var p = new Polygon(points)
                            {
                                Orientation = currentOrientation
                            };
                            currentOrientation = ContourOrientation.Original;
                            polys.Add(p);
                            points.Clear();
                        }
                        continue;
                    }
                    if (line.StartsWith("//") ||
                        line.StartsWith("#") ||
                        line.StartsWith(";"))
                    {
                        continue;
                    }
                    if (line.StartsWith("force", true, CultureInfo.InvariantCulture))
                    {
                        var force = line.Split(new[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        if (force.Length == 2)
                        {
                            if (string.Compare(force[1], "cw", true) == 0)
                            {
                                currentOrientation = ContourOrientation.Clockwise;
                            }
                            if (string.Compare(force[1], "ccw", true) == 0)
                            {
                                currentOrientation = ContourOrientation.CounterClockwise;
                            }
                        }
                    }
                    else if (line.StartsWith("color", true, CultureInfo.InvariantCulture))
                    {
                        var rgba = line.Split(new[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        int r = 255, g = 255, b = 255, a = 255;
                        if (rgba != null)
                        {
                            if (rgba.Length == 4 &&
                                int.TryParse(rgba[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out r) &&
                                int.TryParse(rgba[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out g) &&
                                int.TryParse(rgba[3], NumberStyles.Integer, CultureInfo.InvariantCulture, out b))
                            {
                                currentColor    = Color.FromArgb(r, g, b);
                                polys.HasColors = true;
                            }
                            else if (rgba.Length == 5 &&
                                     int.TryParse(rgba[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out r) &&
                                     int.TryParse(rgba[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out g) &&
                                     int.TryParse(rgba[3], NumberStyles.Integer, CultureInfo.InvariantCulture, out b) &&
                                     int.TryParse(rgba[4], NumberStyles.Integer, CultureInfo.InvariantCulture, out a))
                            {
                                currentColor    = Color.FromArgb(a, r, g, b);
                                polys.HasColors = true;
                            }
                        }
                    }
                    else
                    {
                        float x = 0, y = 0, z = 0;
                        var   xyz = line.Split(new[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        if (xyz != null)
                        {
                            if (xyz.Length >= 1)
                            {
                                float.TryParse(xyz[0], NumberStyles.Float, CultureInfo.InvariantCulture, out x);
                            }
                            if (xyz.Length >= 2)
                            {
                                float.TryParse(xyz[1], NumberStyles.Float, CultureInfo.InvariantCulture, out y);
                            }
                            if (xyz.Length >= 3)
                            {
                                float.TryParse(xyz[2], NumberStyles.Float, CultureInfo.InvariantCulture, out z);
                            }
                            points.Add(new PolygonPoint {
                                X = x, Y = y, Z = z, Color = currentColor
                            });
                        }
                        else
                        {
                            throw new InvalidDataException("Invalid input data");
                        }
                    }
                }

                if (points.Count > 0)
                {
                    Polygon p = new Polygon(points)
                    {
                        Orientation = currentOrientation
                    };
                    polys.Add(p);
                }
            }

            return(polys);
        }
示例#5
0
        private void RefreshAsset(PolygonSet polygons)
        {
            _sw.Reset();

            foreach (var poly in polygons)
            {
                var v = new ContourVertex[poly.Count];
                for (int i = 0; i < poly.Count; i++)
                {
                    v[i].Position = new Vec3 {
                        X = poly[i].X, Y = poly[i].Y, Z = poly[i].Z
                    };
                    v[i].Data = poly[i].Color;
                }
                _sw.Start();
                _tess.AddContour(v, poly.Orientation);
                _sw.Stop();
            }

            _sw.Start();
            _tess.Tessellate(_windingRule, ElementType.Polygons, _polySize, VertexCombine);
            _sw.Stop();

            var output = new PolygonSet();

            for (int i = 0; i < _tess.ElementCount; i++)
            {
                var poly = new Polygon();
                for (int j = 0; j < _polySize; j++)
                {
                    int index = _tess.Elements[i * _polySize + j];
                    if (index == -1)
                    {
                        continue;
                    }
                    var proj = Project(_tess.Vertices[index].Position);
                    var v    = new PolygonPoint {
                        X     = proj.X,
                        Y     = proj.Y,
                        Color = (Color)_tess.Vertices[index].Data
                    };
                    poly.Add(v);
                }
                output.Add(poly);
            }

            var input = new PolygonSet();

            foreach (var poly in polygons)
            {
                var projPoly = new Polygon();
                for (int i = 0; i < poly.Count; i++)
                {
                    var proj = Project(new Vec3 {
                        X = poly[i].X, Y = poly[i].Y, Z = poly[i].Z
                    });
                    var v = new PolygonPoint {
                        X     = proj.X,
                        Y     = proj.Y,
                        Color = poly[i].Color
                    };
                    projPoly.Add(v);
                }
                input.Add(projPoly);
            }

            statusMain.Text = string.Format("{0:F3} ms - {1} polygons (of {2} vertices) {3}", _sw.Elapsed.TotalMilliseconds, _tess.ElementCount, _polySize, _polySize == 3 ? "... triangles" : "");

            /*string debugBalance = DebugPoolBalanceChecker.GetDebugAboutPoolBalanceAll();
             * if (!debugBalance.Equals("")) MessageBox.Show("debugBalance: " + debugBalance);*/

            _canvas.Input  = input;
            _canvas.Output = output;
            _canvas.Invalidate();
        }