public bool Split(Vector3 collPointWs, Vector3 collNormalWs, Shape2 shapeAbove, Shape2 shapeBelow) { var P0 = transform.InverseTransformPoint(collPointWs); var collNormalLocal = transform.InverseTransformDirection(collNormalWs); var n = CalculateSplitPlaneNormal(P0, collNormalLocal); shapeAbove.Clear(); shapeBelow.Clear(); if (SplitPoints(P0, n, shapeAbove, shapeBelow)) { foreach (var e in Edges) { e.Split(P0, n, m_NewPointsGetter, shapeAbove, shapeBelow); } foreach (var f in Faces) { f.Split(m_NewPointsGetter, shapeAbove, shapeBelow); } InitNewShape(shapeAbove, -n); InitNewShape(shapeBelow, n); return(true); } return(false); }
private bool SplitPoints(Vector3 P0, Vector3 n, Shape2 shapeAbove, Shape2 shapeBelow) { var numInside = 0; foreach (var p in Points) { p.Split(P0, n, shapeAbove, shapeBelow, m_NewPointsGetter, ref numInside); } if (numInside >= 3) { Debug.Log(numInside.ToString() + " points inside plane"); foreach (var f in Faces) { if (f.CountNumInside() >= 3) { Debug.LogWarning("Failed to split shape!"); // will need to do something to stop the newly created points from leaking here. // could you existing inside points to get the new ones out of NewPointsGetter, then delete them. return(false); } } } return(true); }
private void DrawPoints(Graphics g, Shape2 polygon, Brush brush) { foreach (var p in polygon.Points) { g.FillEllipse(brush, p.ToRectangle((10, 10))); } }
public void Split(Vector3 P0, Vector3 n, NewPointsGetter newPoints, Shape2 shapeAbove, Shape2 shapeBelow) { if (Point2.PointsBridgePlane(EdgeP1, EdgeP2)) { Vector3 x; Utils.LinePlaneIntersect(n, P0, EdgeP1.Point, EdgeP2.Point, out x); SplitInHalf(x, newPoints, shapeAbove, shapeBelow); } else if (Point2.BothAbove(EdgeP1, EdgeP2)) { shapeAbove.Edges.Add(this); } else if (Point2.BothBelow(EdgeP1, EdgeP2)) { shapeBelow.Edges.Add(this); } else if (Point2.BothInside(EdgeP1, EdgeP2)) { // do nothing - new edges for each shape will be created by the face split } else { SplitAtEnd(newPoints, shapeAbove, shapeBelow); } }
private void SplitAtEnd(NewPointsGetter newPoints, Shape2 shapeAbove, Shape2 shapeBelow) { if (EdgeP1.PlaneRelationship == PointPlaneRelationship.Inside) { if (EdgeP2.PlaneRelationship == PointPlaneRelationship.Above) { EdgeP1 = newPoints.GetPointAbove(EdgeP1); shapeAbove.Edges.Add(this); } else { EdgeP1 = newPoints.GetPointBelow(EdgeP1); shapeBelow.Edges.Add(this); } } else if (EdgeP2.PlaneRelationship == PointPlaneRelationship.Inside) { if (EdgeP1.PlaneRelationship == PointPlaneRelationship.Above) { EdgeP2 = newPoints.GetPointAbove(EdgeP2); shapeAbove.Edges.Add(this); } else { EdgeP2 = newPoints.GetPointBelow(EdgeP2); shapeBelow.Edges.Add(this); } } else { throw new System.Exception(); } }
private void DrawLines(Graphics g, Shape2 polygon, Pen pen) { foreach (var line in polygon.Lines) { g.DrawLine(pen, line.A.ToPoint(), line.B.ToPoint()); } }
private void FindSinglePointFaceCollisionWithOthersFace(Shape2 other, Vector3 P, List <Vector3> collPoints, List <Vector3> collNormals) { var smallestAmount = Mathf.Infinity; Face2 closestFace = null; var faces = other.Faces; for (int j = 0; j < faces.Count; j++) { float amount; if (!faces[j].IsAbovePoint(P, out amount)) { return; } if (amount < smallestAmount) { closestFace = faces[j]; smallestAmount = amount; } } collPoints.Add(other.transform.TransformPoint(P)); collNormals.Add(other.transform.TransformDirection(closestFace.Normal)); }
private void SplitInHalf(Vector3 x, NewPointsGetter newPoints, Shape2 shapeAbove, Shape2 shapeBelow) { var a = new Point2(x); var b = new Point2(x); newPoints.AddPoints(EdgeP1, EdgeP2, a, b); shapeAbove.AddPoint(a); shapeBelow.AddPoint(b); if (EdgeP1.PlaneRelationship == PointPlaneRelationship.Above) { var newForBelow = new Edge2(EdgeP2, b); EdgeP2 = a; shapeAbove.Edges.Add(this); shapeBelow.Edges.Add(newForBelow); } else { var newForAbove = new Edge2(EdgeP2, a); EdgeP2 = b; shapeAbove.Edges.Add(newForAbove); shapeBelow.Edges.Add(this); } }
public static Shape2 CutOutside(this Shape2 shape, Polygon polygon) { var ss = shape.ToSuperShape(); ss.Cut(polygon, false); return(ss.ToShape()); }
public void Split(Vector3 P0, Vector3 n, Shape2 shapeAbove, Shape2 shapeBelow, NewPointsGetter newPoints, ref int numInside) { LinkedPoint1 = null; LinkedPoint2 = null; var comp = Vector3.Dot(Point - P0, n); if (Mathf.Abs(comp) <= Utils.PointInPlaneTol) { var newAbove = this; var newBelow = new Point2(Point); newPoints.AddPoints(this, newAbove, newBelow); shapeAbove.AddPoint(newAbove); shapeBelow.AddPoint(newBelow); PlaneRelationship = PointPlaneRelationship.Inside; numInside++; } else if (comp > 0.0f) { shapeAbove.AddPoint(this); PlaneRelationship = PointPlaneRelationship.Above; } else { shapeBelow.AddPoint(this); PlaneRelationship = PointPlaneRelationship.Below; } }
public void DrawPolygon(bool isFilled, Shape2 shape) { this.baseShape = shape; this.isValid = isFilled; Refresh(); }
private void FillNet(Graphics g, Shape2 shape, Brush brush) { foreach (var convex in shape.Convexes) { g.FillPolygon(brush, convex.Select(i => shape[i].ToPoint()).ToArray()); } }
public override Shape2 Intersect(Shape2 s) { if (s is Rectangle2 rectangle) { return(rectangle.IntersectRectangle(this)); } return(base.Intersect(this)); }
public static Shape ToShape3(this Shape2 shape) { return(new Shape { Points = shape.Points.Select(p => new Vector4(p.x, p.y, 0, 1)).ToArray(), Convexes = shape.Convexes }); }
public static Shape2 TurnOut(this Shape2 shape) { return(new Shape2 { Points = shape.Points, Convexes = shape.Convexes.Select(c => c.Reverse().ToArray()).ToArray() }); }
public static Shape2 Pave(Polygon polygon, Shape2 carpet, bool inside = true) { var super = carpet.ToSuperShape(); super.Cut(polygon, inside); return(super.ToShape()); }
public static Shape2 TriangulateConvexes(this Shape2 shape) { return(new Shape2 { Points = shape.Points, Convexes = FillEngine.Triangulate(shape.Points, shape.Convexes) }); }
public static Shape2 Join(this Shape2 shape, Shape2 another) { return(new Shape2 { Points = shape.Points.Concat(another.Points).ToArray(), Convexes = shape.Convexes.Concat(another.Convexes.Transform(i => i + shape.Points.Length)).ToArray() }); }
public static Shape ToShape3Z(this Shape2 shape, double maxZ = 0.5) { return(new Shape { Points = shape.Points.Index().Select(i => new Vector4(shape.Points[i].x, shape.Points[i].y, maxZ * i / (shape.Points.Length - 1), 1)).ToArray(), Convexes = shape.Convexes }); }
public static Shape2 Transform(this Shape2 shape, Func <Vector2, Vector2> transformFn) { return(new Shape2 { Points = shape.Points.Select(transformFn).ToArray(), Convexes = shape.Convexes }); }
private void InitNewShape(Shape2 shape, Vector3 finalFaceNormal) { var c = shape.CentreAndCache(); shape.InitFaces(finalFaceNormal); shape.transform.position = transform.TransformPoint(c); shape.transform.rotation = transform.rotation; }
public Area(Shape2 inShape, AreaBehavior inBehavior) { _shape = inShape; _type = (inShape is Circle) ? ShapeType.Circle : ((inShape is Polygon2) ? ShapeType.Polygon : ShapeType.Rectangle); AreaColor = Color.Yellow; _behavior = inBehavior; }
private void FindPointCollisionsWithOthersFaces(Shape2 other, List <Vector3> collPoints, List <Vector3> collNormals) { var M = other.transform.worldToLocalMatrix * transform.localToWorldMatrix; for (int i = 0; i < CachedPoints.Count; i++) { FindSinglePointFaceCollisionWithOthersFace(other, M * CachedPoints[i], collPoints, collNormals); } }
private void areaMap1_DragDrop(object sender, DragEventArgs e) { Shape2 shape = null; Area area = null; if (e.Data.GetDataPresent(typeof(Polygon2))) { shape = (Polygon2)e.Data.GetData(typeof(Polygon2)); } else { if (e.Data.GetDataPresent(typeof(Area))) { area = (Area)e.Data.GetData(typeof(Area)); } else { shape = (Circle)e.Data.GetData(typeof(Circle)); } } Point mapDropPoint = AreaMapComponent.PointToClient(new Point(e.X, e.Y)); mapDropPoint.Offset(GeoConverter.ConvertRound(AreaMapComponent.Offset * -1)); if (shape != null) { shape.Offset((new Vector2(mapDropPoint.X, mapDropPoint.Y)) * (1f / AreaMapComponent.Scaling)); shape.Clamp(0, AreaMapComponent.Dimensions.Width, 0, AreaMapComponent.Dimensions.Height); AreaMapComponent.AddShape(shape); RefreshListBox(); } else if (area != null) { //Has a picture with path relative to library if (area.ImgPath != "" && area.ImgPath != libraryUCtrl1.AreaMapComp.CurrentAreaMap.GetAbsolutePath(area.ImgPath)) { if (AreaMapComponent.CurrentAreaMap.Path != "") { area.Map = libraryUCtrl1.AreaMapComp.CurrentAreaMap; AreaMapComponent.Migrate(area, PathHelper.GetFolderPath(AreaMapComponent.CurrentAreaMap.Path)); } else { MessageBox.Show("You must save your AreaMap to be able to use library items with relative pictures !", "File not saved"); return; } } area.Center = (new Vector2(mapDropPoint.X, mapDropPoint.Y)) * (1f / AreaMapComponent.Scaling); area.Clamp(0, AreaMapComponent.Dimensions.Width, 0, AreaMapComponent.Dimensions.Height); area.SetRefCenter(); AreaMapComponent.AddArea(area); RefreshListBox(); } }
public static Shape2 Normalize(this Shape2 shape) { var(bi, filter) = Indexer.DistinctBi(shape.Points); return(new Shape2 { Points = shape.Points.Index().Where(i => filter[i]).Select(i => shape.Points[i]).ToArray(), Convexes = shape.Convexes.Transform(i => bi[i]) }); }
private void DrawNet(Graphics g, Shape2 shape, Pen pen) { foreach (var edge in shape.Convexes.SelectMany(convex => convex.SelectCirclePair((i, j) => (i, j)))) { var a = shape[edge.i]; var b = shape[edge.j]; g.DrawLine(pen, a.ToPoint(), b.ToPoint()); } }
private void DrawPointLabels(Graphics g, Shape2 polygon, Brush brush) { var font = new Font("Arial", 10); for (var i = 0; i < polygon.Points.Length; i++) { var p = polygon[i]; g.DrawString(i.ToString(), font, brush, p.ToPoint()); } }
public static Shape2 Cut(this Shape2 shape, IEnumerable <int> indices) { var backIndices = indices.BackIndices(); var convexes = shape.Convexes.Where(c => c.All(i => backIndices.ContainsKey(i))).Transform(i => backIndices[i]); return(new Shape2 { Points = indices.Select(i => shape[i]).ToArray(), Convexes = convexes }); }
public static Shape2 Reverse(this Shape2 shape) { var points = shape.Points.Reverse().ToArray(); var convexes = shape.Convexes.Transform(i => points.Length - 1 - i); return(new Shape2 { Points = points, Convexes = convexes }); }
public void Split(NewPointsGetter newPoints, Shape2 shapeAbove, Shape2 shapeBelow) { m_InUse = false; var abovePoints = new List <Point2>(); var belowPoints = new List <Point2>(); var newAboveEdge = new Edge2(); var newBelowEdge = new Edge2(); for (int i = 0; i < m_Points.Count; i++) { var next = (i + 1) % m_Points.Count; var p1 = m_Points[i]; var p2 = m_Points[next]; if (p1.PlaneRelationship == PointPlaneRelationship.Above) { abovePoints.Add(p1); } else if (p1.PlaneRelationship == PointPlaneRelationship.Below) { belowPoints.Add(p1); } else { var a = newPoints.GetPointAbove(p1); var b = newPoints.GetPointBelow(p1); abovePoints.Add(a); belowPoints.Add(b); newAboveEdge.AddPoint(a); newBelowEdge.AddPoint(b); } if (Point2.PointsBridgePlane(p1, p2)) { var a = newPoints.GetPointAbove(p1, p2); var b = newPoints.GetPointBelow(p1, p2); abovePoints.Add(a); belowPoints.Add(b); newAboveEdge.AddPoint(a); newBelowEdge.AddPoint(b); } } ProcessNewFace(shapeAbove, abovePoints, newAboveEdge); ProcessNewFace(shapeBelow, belowPoints, newBelowEdge); }