private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (RefImage != null) { e.Graphics.DrawImage(RefImage, new Point(RefImage.Width / 2, RefImage.Height / 2)); } DrawGrid(e.Graphics); foreach (var mesh in _meshes) { var vecs = mesh.Vertices; var lineColor = (mesh == _currentMesh) ? Color.Blue : Color.Black; try { ConvexDecomposition.Polygon p = new ConvexDecomposition.Polygon(); foreach (var b in vecs) { p.x.Add(b.X); p.y.Add(b.Y); } List <ConvexDecomposition.Polygon> results = new List <ConvexDecomposition.Polygon>(); ConvexDecomposition.Statics.DecomposeConvex(p, results, 24); foreach (var res in results) { List <Vec2> v = res.GetVertexVecs(); for (int i = 0; i < v.Count; ++i) { e.Graphics.FillRectangle(Brushes.Black, v[i].X - 1, v[i].Y - 1, 3, 3); int nextVec = i + 1; if (nextVec == v.Count) { nextVec = 0; } e.Graphics.DrawLine(new Pen(Color.Goldenrod, 2), v[i].X, v[i].Y, v[nextVec].X, v[nextVec].Y); } } for (int i = 0; i < vecs.Count; ++i) { e.Graphics.FillRectangle(Brushes.Black, vecs[i].X - 1, vecs[i].Y - 1, 3, 3); int nextVec = i + 1; if (nextVec == vecs.Count) { nextVec = 0; } e.Graphics.DrawLine(new Pen(lineColor, 2), vecs[i].X, vecs[i].Y, vecs[nextVec].X, vecs[nextVec].Y); } } catch { for (int i = 0; i < vecs.Count; ++i) { e.Graphics.FillRectangle((SolidBrush)Brushes.Black, vecs[i].X - 1, vecs[i].Y - 1, 3, 3); int nextVec = i + 1; if (nextVec == vecs.Count) { nextVec = 0; } e.Graphics.DrawLine(new Pen(Color.Red, 2), vecs[i].X, vecs[i].Y, vecs[nextVec].X, vecs[nextVec].Y); } } } }
private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (RefImage != null) e.Graphics.DrawImage(RefImage, new Point(RefImage.Width / 2, RefImage.Height / 2)); DrawGrid(e.Graphics); foreach (var mesh in _meshes) { var vecs = mesh.Vertices; var lineColor = (mesh == _currentMesh) ? Color.Blue : Color.Black; try { ConvexDecomposition.Polygon p = new ConvexDecomposition.Polygon(); foreach (var b in vecs) { p.x.Add(b.X); p.y.Add(b.Y); } List<ConvexDecomposition.Polygon> results = new List<ConvexDecomposition.Polygon>(); ConvexDecomposition.Statics.DecomposeConvex(p, results, 24); foreach (var res in results) { List<Vec2> v = res.GetVertexVecs(); for (int i = 0; i < v.Count; ++i) { e.Graphics.FillRectangle(Brushes.Black, v[i].X - 1, v[i].Y - 1, 3, 3); int nextVec = i + 1; if (nextVec == v.Count) nextVec = 0; e.Graphics.DrawLine(new Pen(Color.Goldenrod, 2), v[i].X, v[i].Y, v[nextVec].X, v[nextVec].Y); } } for (int i = 0; i < vecs.Count; ++i) { e.Graphics.FillRectangle(Brushes.Black, vecs[i].X - 1, vecs[i].Y - 1, 3, 3); int nextVec = i + 1; if (nextVec == vecs.Count) nextVec = 0; e.Graphics.DrawLine(new Pen(lineColor, 2), vecs[i].X, vecs[i].Y, vecs[nextVec].X, vecs[nextVec].Y); } } catch { for (int i = 0; i < vecs.Count; ++i) { e.Graphics.FillRectangle((SolidBrush)Brushes.Black, vecs[i].X - 1, vecs[i].Y - 1, 3, 3); int nextVec = i + 1; if (nextVec == vecs.Count) nextVec = 0; e.Graphics.DrawLine(new Pen(Color.Red, 2), vecs[i].X, vecs[i].Y, vecs[nextVec].X, vecs[nextVec].Y); } } } }
private void exportToolStripMenuItem_Click(object sender, EventArgs e) { using (SaveFileDialog sf = new SaveFileDialog()) { sf.RestoreDirectory = true; sf.Filter = "All Files (*)|*"; sf.DefaultExt = "bmesh"; if (sf.ShowDialog() == System.Windows.Forms.DialogResult.OK) { using (System.IO.FileStream fs = new System.IO.FileStream(sf.FileName, System.IO.FileMode.Create)) { List <PolygonShape> shapes = new List <PolygonShape>(); float largestX = 0, largestY = 0; foreach (var mesh in _meshes) { for (int i = 0; i < mesh.Vertices.Count; ++i) { if (Math.Abs(mesh.Vertices[i].X) > largestX) { largestX = Math.Abs(mesh.Vertices[i].X); } if (Math.Abs(mesh.Vertices[i].Y) > largestY) { largestY = Math.Abs(mesh.Vertices[i].Y); } } if (largestY > largestX) { largestX = largestY; } else { largestY = largestX; } } foreach (var mesh in _meshes) { ConvexDecomposition.Polygon p = new ConvexDecomposition.Polygon(); foreach (var b in mesh.Vertices) { p.x.Add(b.X); p.y.Add(b.Y); } List <Vec2> newVecs = new List <Vec2>(); for (int i = 0; i < mesh.Vertices.Count; ++i) { newVecs.Add(new Vec2(mesh.Vertices[i].X / largestX, mesh.Vertices[i].Y / largestY)); } List <ConvexDecomposition.Polygon> results = new List <ConvexDecomposition.Polygon>(); ConvexDecomposition.Statics.DecomposeConvex(p, results, 24); for (int i = 0; i < results.Count; ++i) { for (int x = 0; x < results[i].x.Count; ++x) { results[i].x[x] /= largestX; } for (int x = 0; x < results[i].y.Count; ++x) { results[i].y[x] /= largestY; } } foreach (var res in results) { List <Vec2> v = res.GetVertexVecs(); //sw.WriteLine( // "{"); //for (int i = 0; i < v.Count; ++i) // sw.WriteLine("\t" + v[i].X.ToString() + " " + v[i].Y.ToString()); //sw.WriteLine("}"); shapes.Add(new PolygonShape(v.ToArray())); } } new MeshShape(shapes.ToArray()).SaveBinary(fs); } } } }
private void exportToolStripMenuItem_Click(object sender, EventArgs e) { using (SaveFileDialog sf = new SaveFileDialog()) { sf.RestoreDirectory = true; sf.Filter = "All Files (*)|*"; sf.DefaultExt = "bmesh"; if (sf.ShowDialog() == System.Windows.Forms.DialogResult.OK) { using (System.IO.FileStream fs = new System.IO.FileStream(sf.FileName, System.IO.FileMode.Create)) { List<PolygonShape> shapes = new List<PolygonShape>(); float largestX = 0, largestY = 0; foreach (var mesh in _meshes) { for (int i = 0; i < mesh.Vertices.Count; ++i) { if (Math.Abs(mesh.Vertices[i].X) > largestX) largestX = Math.Abs(mesh.Vertices[i].X); if (Math.Abs(mesh.Vertices[i].Y) > largestY) largestY = Math.Abs(mesh.Vertices[i].Y); } if (largestY > largestX) largestX = largestY; else largestY = largestX; } foreach (var mesh in _meshes) { ConvexDecomposition.Polygon p = new ConvexDecomposition.Polygon(); foreach (var b in mesh.Vertices) { p.x.Add(b.X); p.y.Add(b.Y); } List<Vec2> newVecs = new List<Vec2>(); for (int i = 0; i < mesh.Vertices.Count; ++i) newVecs.Add(new Vec2(mesh.Vertices[i].X / largestX, mesh.Vertices[i].Y / largestY)); List<ConvexDecomposition.Polygon> results = new List<ConvexDecomposition.Polygon>(); ConvexDecomposition.Statics.DecomposeConvex(p, results, 24); for (int i = 0; i < results.Count; ++i) { for (int x = 0; x < results[i].x.Count; ++x) results[i].x[x] /= largestX; for (int x = 0; x < results[i].y.Count; ++x) results[i].y[x] /= largestY; } foreach (var res in results) { List<Vec2> v = res.GetVertexVecs(); //sw.WriteLine( // "{"); //for (int i = 0; i < v.Count; ++i) // sw.WriteLine("\t" + v[i].X.ToString() + " " + v[i].Y.ToString()); //sw.WriteLine("}"); shapes.Add(new PolygonShape(v.ToArray())); } } new MeshShape(shapes.ToArray()).SaveBinary(fs); } } } }