Ejemplo n.º 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);
        }
Ejemplo n.º 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();
        }
Ejemplo n.º 3
0
 public static void ToTess(PolygonSet pset, Tess tess)
 {
     foreach (var poly in pset)
     {
         var v = new ContourVertex[poly.Count];
         for (int i = 0; i < poly.Count; i++)
         {
             v[i].Position = new Vec3(poly[i].X, poly[i].Y, poly[i].Z);
             v[i].Data     = poly[i].Color;
         }
         tess.AddContour(v, poly.Orientation);
     }
 }
Ejemplo n.º 4
0
        private static Poly2Tri.PolygonSet ToP2T(PolygonSet pset)
        {
            var rpset = new Poly2Tri.PolygonSet();

            foreach (var poly in pset)
            {
                var pts = new List <Poly2Tri.PolygonPoint>();
                foreach (var p in poly)
                {
                    pts.Add(new Poly2Tri.PolygonPoint(p.X, p.Y));
                }
                rpset.Add(new Poly2Tri.Polygon(pts));
            }
            return(rpset);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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();
        }
Ejemplo n.º 8
0
        public MainForm()
        {
            InitializeComponent();

            _canvas      = new Canvas();
            _canvas.Dock = DockStyle.Fill;
            panel.Controls.Add(_canvas);

            foreach (var asset in _data.Assets)
            {
                toolStripAssets.Items.Add(asset);
            }
            toolStripAssets.SelectedIndexChanged += delegate(object sender, EventArgs e) {
                if (toolStripAssets.SelectedIndex >= 0)
                {
                    var asset = toolStripAssets.SelectedItem as DataLoader.Asset;
                    _polys = asset.Polygons;
                    RefreshCanvas();
                }
            };

            _windingRules = Enum.GetNames(typeof(WindingRule));
            foreach (var windingRule in _windingRules)
            {
                toolStripWinding.Items.Add(windingRule);
            }
            toolStripWinding.SelectedIndexChanged += delegate(object sender, EventArgs e) {
                _windingRule = (WindingRule)toolStripWinding.SelectedIndex;
                RefreshCanvas();
            };

            toolStripPolySize.KeyDown += delegate(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    PolySizeEvent();
                }
            };
            toolStripPolySize.Leave += delegate(object sender, EventArgs e)
            {
                PolySizeEvent();
            };

            toolStripButtonShowInput.CheckedChanged += delegate(object sender, EventArgs e)
            {
                _canvas.ShowInput = toolStripButtonShowInput.Checked;
                toolStripButtonShowWinding.Enabled = _canvas.ShowInput;
                RefreshCanvas();
            };

            toolStripButtonShowWinding.CheckedChanged += delegate(object sender, EventArgs e)
            {
                _canvas.ShowWinding = toolStripButtonShowWinding.Checked;
                RefreshCanvas();
            };

            toolStripButtonNoEmpty.CheckedChanged += delegate(object sender, EventArgs e)
            {
                _tess.NoEmptyPolygons = toolStripButtonNoEmpty.Checked;
                RefreshCanvas();
            };

            toolStripButtonBench.Click += delegate(object sender, EventArgs e)
            {
                new BenchForm().ShowDialog(this);
            };

            toolStripButtonFile.Click += delegate(object sender, EventArgs e)
            {
                var dialog = new OpenFileDialog();
                dialog.Filter           = "Test Files (*.dat)|*.dat|All Files (*.*)|*.*";
                dialog.FilterIndex      = 1;
                dialog.RestoreDirectory = true;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    var polygons = DataLoader.LoadDat(dialog.OpenFile());
                    _polys = polygons;
                    RefreshCanvas();
                    toolStripAssets.SelectedIndex = -1;
                }
            };

            toolStripButtonFolder.Click += delegate(object sender, EventArgs e)
            {
                var dialog = new FolderBrowserDialog();
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    var files = Directory.GetFiles(dialog.SelectedPath, "*.dat");
                    if (files.Length > 0)
                    {
                        toolStripAssets.Items.Clear();
                        _polys = null;
                        foreach (var file in files)
                        {
                            using (var stream = new FileStream(file, FileMode.Open))
                            {
                                var polygons = DataLoader.LoadDat(stream);
                                if (_polys == null)
                                {
                                    _polys = polygons;
                                }
                                toolStripAssets.Items.Add(new DataLoader.Asset()
                                {
                                    Name = Path.GetFileName(file), Polygons = polygons
                                });
                            }
                        }
                        toolStripAssets.SelectedIndex = 0;
                        RefreshCanvas();
                    }
                }
            };

            SetAsset("redbook-winding");
            SetShowInput(true);
            SetShowWinding(false);
            SetPolySize(3);
            SetWindingRule(WindingRule.EvenOdd);
        }