public void AddFigures(List <GeometryFigure> figures, List <CombineMode> combineModes) { int start = 0; if (polygons == null) { polygons = ClipperHelper.Simplify(figures[0].Points, figures[0].FillMode, out tree); start = 1; } for (int i = start; i < figures.Count; i++) { CombineMode combineMode = combineModes[i]; FillMode fillMode = figures[i].FillMode; var currentFigures = new List <List <Vector2> >(); currentFigures.Add(figures[i].Points); while ((i + 1 < figures.Count) && (combineModes[i + 1] == combineMode) && (figures[i + 1].FillMode == fillMode)) { i++; currentFigures.Add(figures[i].Points); } polygons = ClipperHelper.Combine(polygons, currentFigures, FillMode.Alternate, fillMode, combineMode, out tree); } }
private SimplifiedGeometryShape(List <GeometryFigure> figures, StrokeStyle strokeStyle, float strokeWidth) { if (figures.Count < 1) { polygons = null; return; } polygons = figures[0].Outline(strokeStyle, strokeWidth, out tree); var currentFigures = new List <List <Vector2> >(); for (int i = 1; i < figures.Count; i++) { currentFigures.AddRange(figures[i].Outline(strokeStyle, strokeWidth, out tree)); } polygons = ClipperHelper.Combine(polygons, currentFigures, FillMode.Alternate, FillMode.Winding, CombineMode.Union, out tree); Triangulated = false; }
private void button8_Click(object sender, EventArgs e) { List <PolygonHelper> phhs = new List <PolygonHelper>(); //if (!checkBox1.Checked) { if (dataModel.SelectedItems.Length < 2) { dataModel.ParentForm.StatusMessage("there are no 2 polygon selected", StatusMessageType.Warning); return; } foreach (var item in dataModel.SelectedItems) { phhs.Add(item as PolygonHelper); } } // else { // phhs.Add((comboBox2.SelectedItem as ComboBoxItem).Tag as PolygonHelper); // phhs.Add((comboBox3.SelectedItem as ComboBoxItem).Tag as PolygonHelper); } var ar1 = phhs.ToArray(); NFP p = new NFP(); NFP p2 = new NFP(); var jType = (JoinType)comboBox1.SelectedIndex; double offset = double.Parse(textBox2.Text.Replace(",", "."), CultureInfo.InvariantCulture); double miterLimit = double.Parse(textBox3.Text.Replace(",", "."), CultureInfo.InvariantCulture); double curveTolerance = double.Parse(textBox4.Text.Replace(",", "."), CultureInfo.InvariantCulture); p.Points = ar1[0].Polygon.Points.Select(z => new SvgPoint(z.X, z.Y)).ToArray(); p2.Points = ar1[1].Polygon.Points.Select(z => new SvgPoint(z.X, z.Y)).ToArray(); var offs = ClipperHelper.intersection(p, p2, offset, jType, curveTolerance: curveTolerance, miterLimit: miterLimit); PolygonHelper ph = new PolygonHelper(); if (offs.Any()) { ph.Polygon.Points = offs.First().Points.Select(z => new SvgPoint(z.X, z.Y)).ToArray(); } foreach (var item in offs.Skip(1)) { var nfp2 = new NFP(); nfp2.Points = item.Points.Select(z => new SvgPoint(z.X, z.Y)).ToArray(); ph.Polygon.Childrens.Add(nfp2); } dataModel.AddItem(ph); }
public static SimplifiedGeometryShape Combine(SimplifiedGeometryShape shape1, SimplifiedGeometryShape shape2, CombineMode combineMode) { PolyTree tree; List <List <Vector2> > polygons = ClipperHelper.Combine(shape1.polygons, shape2.polygons, FillMode.Alternate, FillMode.Alternate, combineMode, out tree); return(new SimplifiedGeometryShape(polygons, tree)); }
private void button1_Click(object sender, EventArgs e) { NFP p = new NFP(); if (!(dataModel.SelectedItem is PolygonHelper ph2)) { return; } p.Points = ph2.Polygon.Points.Select(z => new SvgPoint(z.X, z.Y)).ToArray(); var jType = (JoinType)comboBox1.SelectedIndex; double offset = double.Parse(textBox2.Text.Replace(",", "."), CultureInfo.InvariantCulture); double miterLimit = double.Parse(textBox3.Text.Replace(",", "."), CultureInfo.InvariantCulture); double curveTolerance = double.Parse(textBox4.Text.Replace(",", "."), CultureInfo.InvariantCulture); var offs = ClipperHelper.offset(p, offset, jType, curveTolerance: curveTolerance, miterLimit: miterLimit); //if (offs.Count() > 1) throw new NotImplementedException(); PolygonHelper ph = new PolygonHelper(); foreach (var item in ph2.Polygon.Childrens) { var offs2 = ClipperHelper.offset(item, -offset, jType, curveTolerance: curveTolerance, miterLimit: miterLimit); var nfp1 = new NFP(); if (offs2.Any()) { //if (offs2.Count() > 1) throw new NotImplementedException(); foreach (var zitem in offs2) { nfp1.Points = zitem.Points.Select(z => new SvgPoint(z.X, z.Y)).ToArray(); ph.Polygon.Childrens.Add(nfp1); } } } if (offs.Any()) { ph.Polygon.Points = offs.First().Points.Select(z => new SvgPoint(z.X, z.Y)).ToArray(); } foreach (var item in offs.Skip(1)) { var nfp2 = new NFP(); nfp2.Points = item.Points.Select(z => new SvgPoint(z.X, z.Y)).ToArray(); ph.Polygon.Childrens.Add(nfp2); } ph.OffsetX = ph2.OffsetX; ph.OffsetY = ph2.OffsetY; ph.Rotation = ph2.Rotation; dataModel.AddItem(ph); }
private void button12_Click(object sender, EventArgs e) { var res = dataModel.GetPairOfSelectedNfps(); if (res == null) { return; } NFP offs = null; offs = ClipperHelper.MinkowskiSum(res[0], res[1], checkBox2.Checked, checkBox3.Checked); if (offs != null) { PolygonHelper ph = new PolygonHelper(); //ph.Polygon.Points = offs.Points.Select(z => new SvgPoint(z.X, z.Y)).ToArray(); ph.Polygon = DeepNest.clone2(offs); dataModel.AddItem(ph); } }
public static List <GameObject> GenerateTriangularPieces(GameObject source, int extraPoints = 0, int subshatterSteps = 0, Material mat = null) { List <GameObject> pieces = new List <GameObject>(); if (mat == null) { mat = createFragmentMaterial(source); } //get transform information Vector3 origScale = source.transform.localScale; source.transform.localScale = Vector3.one; Quaternion origRotation = source.transform.localRotation; source.transform.localRotation = Quaternion.identity; //get rigidbody information Rigidbody2D rg = source.GetComponent <Rigidbody2D>(); //Я добавил Vector2 origVelocity = rg.velocity; //get collider information PolygonCollider2D sourcePolyCollider = source.GetComponent <PolygonCollider2D>(); BoxCollider2D sourceBoxCollider = source.GetComponent <BoxCollider2D>(); List <Vector2> points = new List <Vector2>(); List <Vector2> borderPoints = new List <Vector2>(); //add points from the present collider if (sourcePolyCollider != null) { points = getPoints(sourcePolyCollider); borderPoints = getPoints(sourcePolyCollider); } else if (sourceBoxCollider != null) { points = getPoints(sourceBoxCollider); borderPoints = getPoints(sourceBoxCollider); } //create a bounding rectangle based on the polygon points Rect rect = getRect(source); //if the target polygon is a triangle, generate a point in the middle to allow for fracture if (points.Count == 3) { points.Add((points[0] + points[1] + points[2]) / 3); } for (int i = 0; i < extraPoints; i++) { points.Add(new Vector2(Random.Range(rect.width / -2, rect.width / 2), Random.Range(rect.height / -2, rect.height / 2))); } Voronoi voronoi = new Delaunay.Voronoi(points, null, rect); List <List <Vector2> > clippedTriangles = new List <List <Vector2> >(); foreach (Triangle tri in voronoi.Triangles()) { clippedTriangles = ClipperHelper.clip(borderPoints, tri); foreach (List <Vector2> triangle in clippedTriangles) { pieces.Add(generateTriangularPiece(source, triangle, origVelocity, origScale, origRotation, mat, rg)); } } List <GameObject> morePieces = new List <GameObject>(); if (subshatterSteps > 0) { subshatterSteps--; foreach (GameObject piece in pieces) { morePieces.AddRange(SpriteExploder.GenerateTriangularPieces(piece, extraPoints, subshatterSteps, mat)); GameObject.DestroyImmediate(piece); } } else { morePieces = pieces; } //reset transform information source.transform.localScale = origScale; source.transform.localRotation = origRotation; Resources.UnloadUnusedAssets(); return(morePieces); }
public static List <GameObject> GenerateVoronoiPieces(GameObject source, int extraPoints = 0, int subshatterSteps = 0, Material mat = null) { List <GameObject> pieces = new List <GameObject>(); if (mat == null) { mat = createFragmentMaterial(source); } //get transform information Vector3 origScale = source.transform.localScale; source.transform.localScale = Vector3.one; Quaternion origRotation = source.transform.localRotation; source.transform.localRotation = Quaternion.identity; //get rigidbody information Rigidbody2D rigbody = source.GetComponent <Rigidbody2D>(); //Я добавил Vector2 origVelocity = rigbody.velocity; //get collider information PolygonCollider2D sourcePolyCollider = source.GetComponent <PolygonCollider2D>(); BoxCollider2D sourceBoxCollider = source.GetComponent <BoxCollider2D>(); List <Vector2> points = new List <Vector2>(); List <Vector2> borderPoints = new List <Vector2>(); if (sourcePolyCollider != null) { points = getPoints(sourcePolyCollider); borderPoints = getPoints(sourcePolyCollider); } else if (sourceBoxCollider != null) { points = getPoints(sourceBoxCollider); borderPoints = getPoints(sourceBoxCollider); } Rect rect = getRect(source); for (int i = 0; i < extraPoints; i++) { points.Add(new Vector2(Random.Range(rect.width / -2, rect.width / 2), Random.Range(rect.height / -2, rect.height / 2))); } Voronoi voronoi = new Delaunay.Voronoi(points, null, rect); List <List <Vector2> > clippedRegions = new List <List <Vector2> >(); foreach (List <Vector2> region in voronoi.Regions()) { clippedRegions = ClipperHelper.clip(borderPoints, region); foreach (List <Vector2> clippedRegion in clippedRegions) { pieces.Add(generateVoronoiPiece(source, clippedRegion, origVelocity, origScale, origRotation, mat, rigbody)); } } List <GameObject> morePieces = new List <GameObject>(); if (subshatterSteps > 0) { subshatterSteps--; foreach (GameObject piece in pieces) { morePieces.AddRange(SpriteExploder.GenerateVoronoiPieces(piece, extraPoints, subshatterSteps)); GameObject.DestroyImmediate(piece); } } else { morePieces = pieces; } //reset transform information source.transform.localScale = origScale; source.transform.localRotation = origRotation; Resources.UnloadUnusedAssets(); return(morePieces); }
public TriangulatedGlyph(Glyph glyphData, TrueTypeFile.HorizontalMetric horizontalMetric) { indices = new List <int>(); vertices = new List <Vertex>(); advanceWidth = horizontalMetric.AdvanceWidth; leftSideBearing = horizontalMetric.LeftSideBearing; if (glyphData.IsCompound) { } else { if (glyphData.Contours.Length == 0) { return; } List <Triangle> triangles; List <LinkedList <ControlPoint> > contours = CreateContours(glyphData, out triangles); int count = 0; bool split; do { split = SplitTriangles(ref triangles); count++; } while (split && count < 4); foreach (var triangle in triangles) { ControlPoint p = (triangle.B + triangle.C) / 2; int windingNumber = GetWindingNumber(p, glyphData); triangle.IsInside = windingNumber == 0; if (triangle.IsInside) { triangle.Contour.AddAfter(triangle.BNode, triangle.A); } } PolyTree tree = ClipperHelper.Combine(contours.Select(contour => contour.ToList()).ToList()); int pointIndex = 0; var inputGeometry = new InputGeometry(); SearchTree(inputGeometry, ref pointIndex, tree); if (inputGeometry.Count > 0) { var mesh = new Mesh(); mesh.Triangulate(inputGeometry); int highestIndex = 0; var indexDict = new Dictionary <int, int>(); foreach (TriangleNet.Data.Triangle triangle in mesh.Triangles) { int[] newIndices; List <Vertex> newVertices = IndexVertices(triangle, indexDict, ref highestIndex, out newIndices); indices.AddRange(newIndices); vertices.AddRange(newVertices); } foreach (var triangle in triangles) { indices.Add(vertices.Count); vertices.Add(new Vertex((float)triangle.B.X, (float)triangle.B.Y, 0, 0, triangle.IsInside)); indices.Add(vertices.Count); vertices.Add(new Vertex((float)triangle.A.X, (float)triangle.A.Y, 0.5f, 0, triangle.IsInside)); indices.Add(vertices.Count); vertices.Add(new Vertex((float)triangle.C.X, (float)triangle.C.Y, 1, 1, triangle.IsInside)); } } } }
public static List <GameObject> GenerateCavePieces(GameObject source, PointsResult pointsResult, int subshatterSteps = 0, Material mat = null) { List <GameObject> pieces = new List <GameObject> (); if (mat == null) { mat = createFragmentMaterial(source); } //get transform information Vector3 origScale = source.transform.localScale; source.transform.localScale = Vector3.one; Quaternion origRotation = source.transform.localRotation; source.transform.localRotation = Quaternion.identity; //get rigidbody information Vector2 origVelocity = source.GetComponent <Rigidbody2D> ().velocity; Rect rect = getRect(source); List <Vector2> points = new List <Vector2> (); foreach (Point point in pointsResult.points) { points.Add(point.point); } List <Vector2> borderPoints = pointsResult.borderPoints; Voronoi voronoi = new Delaunay.Voronoi(points, null, rect); List <List <Vector2> > clippedRegions = new List <List <Vector2> > (); foreach (Point point in pointsResult.points) { bool isWall = pointsResult.isWall(point); float magnitude = point.point.magnitude; bool isCell = magnitude > 100; bool isInStartingArea = Mathf.Abs(Mathf.Abs(point.point.x) - Mathf.Abs(point.point.y)) < 10; if (!isInStartingArea && (isWall || isCell)) { List <Vector2> region = voronoi.Region(point.point); clippedRegions = ClipperHelper.clip(borderPoints, region); foreach (List <Vector2> clippedRegion in clippedRegions) { pieces.Add(generateVoronoiPiece(source, clippedRegion, origVelocity, origScale, origRotation, mat)); } } } List <GameObject> morePieces = new List <GameObject> (); if (subshatterSteps > 0) { subshatterSteps--; foreach (GameObject piece in pieces) { morePieces.AddRange(SpriteExploder.GenerateVoronoiPieces(piece, 3, subshatterSteps)); GameObject.DestroyImmediate(piece); } } else { morePieces = pieces; } //reset transform information source.transform.localScale = origScale; source.transform.localRotation = origRotation; Resources.UnloadUnusedAssets(); return(morePieces); }
public void AddShape(SimplifiedGeometryShape shape, CombineMode combineMode) { polygons = ClipperHelper.Combine(polygons, shape.polygons, FillMode.Alternate, FillMode.Alternate, combineMode, out tree); }
public static List <GameObject> GenerateTriangularPieces(GameObject source, int extraPoints = 0, int subshatterSteps = 0, Material mat = null) { List <GameObject> list = new List <GameObject>(); if (mat == null) { mat = createFragmentMaterial(source); } Vector3 localScale = source.transform.localScale; source.transform.localScale = Vector3.one; Quaternion localRotation = source.transform.localRotation; source.transform.localRotation = Quaternion.identity; Vector2 velocity = source.GetComponent <Rigidbody2D>().velocity; PolygonCollider2D component = source.GetComponent <PolygonCollider2D>(); BoxCollider2D component2 = source.GetComponent <BoxCollider2D>(); List <Vector2> list2 = new List <Vector2>(); List <Vector2> boundary = new List <Vector2>(); if (component != null) { list2 = getPoints(component); boundary = getPoints(component); } else if (component2 != null) { list2 = getPoints(component2); boundary = getPoints(component2); } Rect rect = getRect(source); if (list2.Count == 3) { list2.Add((list2[0] + list2[1] + list2[2]) / 3f); } for (int i = 0; i < extraPoints; i++) { list2.Add(new Vector2(Random.Range(rect.width / -2f, rect.width / 2f), Random.Range(rect.height / -2f, rect.height / 2f))); } Voronoi voronoi = new Voronoi(list2, null, rect); List <List <Vector2> > list3 = new List <List <Vector2> >(); foreach (Triangle item in voronoi.Triangles()) { list3 = ClipperHelper.clip(boundary, item); foreach (List <Vector2> item2 in list3) { list.Add(generateTriangularPiece(source, item2, velocity, localScale, localRotation, mat)); } } List <GameObject> list4 = new List <GameObject>(); if (subshatterSteps > 0) { subshatterSteps--; foreach (GameObject item3 in list) { list4.AddRange(GenerateTriangularPieces(item3, extraPoints, subshatterSteps, mat)); UnityEngine.Object.DestroyImmediate(item3); } } else { list4 = list; } source.transform.localScale = localScale; source.transform.localRotation = localRotation; Resources.UnloadUnusedAssets(); return(list4); }