/// <summary> /// Verifies a PolylineCurve is a rectangle /// </summary> public static bool IsRectangle(PolylineCurve curve) { // Curve should be a valid, closed, planar polyline curve with 5 points if (curve == null || !curve.IsValid || !curve.IsClosed || !curve.IsPlanar() || curve.PointCount != 5) { return(false); } // Angle between each segment should be 90 degrees const double angle = 90.0 * (Math.PI / 180.0); for (var i = 1; i < curve.PointCount - 1; i++) { var p0 = curve.Point(i - 1); var p1 = curve.Point(i); var p2 = curve.Point(i + 1); var v0 = p1 - p0; v0.Unitize(); var v1 = p1 - p2; v1.Unitize(); var a = Vector3d.VectorAngle(v0, v1); if (Math.Abs(angle - a) >= RhinoMath.DefaultAngleTolerance) { return(false); } } return(true); }
internal static double[][] PtsFromOffsetRectangle(List <Point3d> plist, double offsetdistance) { double[][] ptz = new double[4][]; for (int i = 0; i < 4; i++) { ptz[i] = new double[2]; ptz[i][0] = plist[i].X; ptz[i][1] = plist[i].Y; } double[] dblcen = Misc.Centroid(ptz); Point3d cen = new Point3d(dblcen[0], dblcen[1], 0); plist.Add(plist[0]); PolylineCurve crv = new PolylineCurve(plist); crv.MakeClosed(0.001); Curve[] offsetcr = crv.Offset(cen, Vector3d.ZAxis, offsetdistance, 0.001, CurveOffsetCornerStyle.None); PolylineCurve offsetpl = offsetcr[0].ToPolyline(0, 0, 0, System.Double.MaxValue, 0, 0.001, 0, 0, true); Point3d offsetpt0 = offsetpl.Point(0); Point3d offsetpt1 = offsetpl.Point(1); Point3d offsetpt2 = offsetpl.Point(2); Point3d offsetpt3 = offsetpl.Point(3); return(new double[][] { new double[] { offsetpt0.X, offsetpt0.Y }, new double[] { offsetpt1.X, offsetpt1.Y }, new double[] { offsetpt2.X, offsetpt2.Y }, new double[] { offsetpt3.X, offsetpt3.Y } }); }
public static IEnumerable <DB.Curve> ToCurveMany(this PolylineCurve value, double factor) { int pointCount = value.PointCount; if (pointCount > 1) { DB.XYZ end, start = value.Point(0).ToXYZ(factor); for (int p = 1; p < pointCount; start = end, ++p) { end = value.Point(p).ToXYZ(factor); yield return(DB.Line.CreateBound(start, end)); } } }
public static IEnumerable <NXOpen.Line> ToNXLines(PolylineCurve value, double factor) { int pointCount = value.PointCount; if (pointCount > 1) { NXOpen.Point3d end, start = value.Point(0).ToXYZ(factor); for (int p = 1; p < pointCount; start = end, ++p) { end = value.Point(p).ToXYZ(factor); yield return(WorkPart.Curves.CreateLine(start, end)); } } }
public List <Curve> DuplicateSegments(PolylineCurve plinecurve) { List <Curve> lines = new List <Curve>();; for (int i = 1; i < plinecurve.PointCount; i++) { Point3d PtA = plinecurve.Point(i - 1); Point3d PtB = plinecurve.Point(i); lines.Add(new LineCurve(PtA, PtB)); } return(lines); }
private Topologic.Wire ByPolylineCurve(PolylineCurve ghPolylineCurve) { int numPoints = ghPolylineCurve.PointCount; if (numPoints < 1) { return(null); } List <Topologic.Vertex> vertices = new List <Topologic.Vertex>(); List <int> indices = new List <int>(); for (int i = 0; i < numPoints; ++i) { Point3d ghPoint = ghPolylineCurve.Point(i); Topologic.Vertex vertex = ByPoint(ghPoint); vertices.Add(vertex); indices.Add(i); } if (ghPolylineCurve.IsClosed) { //vertices.Add(vertices[0]); //indices.Add(0); List <List <int> > listOfIndices = new List <List <int> >(); listOfIndices.Add(indices); return(Topologic.Topology.ByVerticesIndices(vertices, listOfIndices)[0].Wires[0]); } else { List <List <int> > listOfIndices = new List <List <int> >(); listOfIndices.Add(indices); return(Topologic.Topology.ByVerticesIndices(vertices, listOfIndices)[0] as Topologic.Wire); } }
public static Wire ToTopologic(PolylineCurve polylineCurve) { int count = polylineCurve.PointCount; if (count < 1) { return(null); } List <Vertex> vertices = new List <Vertex>(); List <int> indices = new List <int>(); for (int i = 0; i < count; ++i) { Vertex vertex = polylineCurve.Point(i).ToTopologic(); vertices.Add(vertex); indices.Add(i); } if (polylineCurve.IsClosed) { List <IList <int> > listOfIndices = new List <IList <int> >(); listOfIndices.Add(indices); return(Topology.ByVerticesIndices(vertices, listOfIndices)[0].Wires[0]); } else { List <IList <int> > listOfIndices = new List <IList <int> >(); listOfIndices.Add(indices); return(Topology.ByVerticesIndices(vertices, listOfIndices)[0] as Wire); } }
static IEnumerable <NXOpen.Line> ToEdgeCurveMany(PolylineCurve curve) { NXOpen.Part workPart = _theSession.Parts.Work; int pointCount = curve.PointCount; if (pointCount > 1) { var point = curve.Point(0); NXOpen.Point3d end, start = new NXOpen.Point3d(point.X, point.Y, point.Z); for (int p = 1; p < pointCount; start = end, ++p) { point = curve.Point(p); end = new NXOpen.Point3d(point.X, point.Y, point.Z); yield return(workPart.Curves.CreateLine(start, end)); } } }
//====================================================================// public static Polyline ToPolyline(this PolylineCurve pCurve) { int point_count = pCurve.PointCount; Polyline pLine = new Polyline(point_count); for (int i = 0; i < pCurve.PointCount; ++i) { pLine.Add(pCurve.Point(i)); } return(pLine); }
internal static AM_RegionBoundary Create(PolylineCurve polyline, double space, bool forceToOpened) { bool is_closed = polyline.IsClosed; bool boundary_closed = is_closed; if (forceToOpened) { boundary_closed = false; } AM_RegionBoundary b = new AM_RegionBoundary(boundary_closed); for (int iSegm = 0; iSegm < polyline.SpanCount; iSegm++) { Point3d p0 = polyline.Point(iSegm); Point3d p1 = polyline.Point((iSegm + 1) % polyline.SpanCount); if (!is_closed && iSegm == polyline.SpanCount - 1) { p1 = polyline.Point(iSegm + 1); } b.m_Vertexes.Add(new AM_RegionVertex(new Point2d(p0.X, p0.Y), space)); b.m_Curves.Add(new LineCurve(p0, p1)); if (!is_closed && iSegm == polyline.SpanCount - 1) { b.m_Vertexes.Add(new AM_RegionVertex(new Point2d(p1.X, p1.Y), space)); } else if (is_closed && forceToOpened && iSegm == polyline.SpanCount - 1) { b.m_Vertexes.Add(new AM_RegionVertex(new Point2d(p1.X, p1.Y), space)); } } b.m_GenPoint = new List <Point4d> [polyline.SpanCount]; return(b); }
/// <summary> /// Creates grips /// </summary> public bool CreateGrips(PolylineCurve polylineCurve) { if (!SampleCsRectangleHelper.IsRectangle(polylineCurve)) { return(false); } if (GripCount > 0) { return(false); } Plane plane; if (!polylineCurve.TryGetPlane(out plane)) { return(false); } m_plane = plane; m_active_rectangle = new Point3d[5]; for (var i = 0; i < polylineCurve.PointCount; i++) { m_active_rectangle[i] = polylineCurve.Point(i); } m_original_rectangle = new Point3d[5]; Array.Copy(m_active_rectangle, m_original_rectangle, 5); var line = new Line(); for (var i = 0; i < 4; i++) { var gi = 2 * i; line.From = m_active_rectangle[i]; line.To = m_active_rectangle[i + 1]; m_sample_cs_rectangle_grips[gi].OriginalLocation = line.From; m_sample_cs_rectangle_grips[gi + 1].OriginalLocation = 0.5 * line.From + 0.5 * line.To; m_sample_cs_rectangle_grips[gi].Active = true; m_sample_cs_rectangle_grips[gi + 1].Active = true; } for (var i = 0; i < 8; i++) { AddGrip(m_sample_cs_rectangle_grips[i]); } return(true); }
/// <summary> /// Get the points on a list of curves /// </summary> /// <param name="crvs"></param> /// <returns></returns> internal static List <Point3d> getPointsFromCurves(IEnumerable <Curve> crvs) { var ptList = new List <Point3d>(); foreach (Curve c in crvs) { PolylineCurve pl = null; if (c.HasNurbsForm() > 0) { pl = c.ToNurbsCurve().ToPolyline(1, Math.PI / 20, 1, double.MaxValue); } else { pl = c.ToPolyline(1, Math.PI / 20, 1, double.MaxValue); } for (int i = 0; i < pl.PointCount; i++) { ptList.Add(pl.Point(i)); } } return(ptList); }
/// <summary> /// Test for a curve that is a polyline (or looks like a polyline). /// </summary> bool IsPolyline(Curve curve, ref int point_count, ref bool bClosed, ref bool bPlanar, ref bool bLength, ref bool bAngle, ref bool bIntersect) { if (null == curve) { return(false); } List <Point3d> points = new List <Point3d>(); // Is the curve a polyline curve? PolylineCurve polyline_curve = curve as PolylineCurve; if (null != polyline_curve) { if (polyline_curve.PointCount <= 2) { return(false); } for (int i = 0; i < polyline_curve.PointCount; i++) { points.Add(polyline_curve.Point(i)); } } // Is the curve a polycurve that looks like an polyline? PolyCurve poly_curve = curve as PolyCurve; if (null != poly_curve) { Polyline polyline; if (poly_curve.TryGetPolyline(out polyline)) { if (polyline.Count <= 2) { return(false); } for (int i = 0; i < polyline.Count; i++) { points.Add(polyline[i]); } } } // Is the curve a NURBS curve that looks like an polyline? NurbsCurve nurbs_curve = curve as NurbsCurve; if (null != nurbs_curve) { Polyline polyline; if (nurbs_curve.TryGetPolyline(out polyline)) { if (polyline.Count <= 2) { return(false); } for (int i = 0; i < polyline.Count; i++) { points.Add(polyline[i]); } } } if (0 == points.Count) { return(false); } // Is the curve closed? bClosed = curve.IsClosed; // Is the curve planar? bPlanar = curve.IsPlanar(); // Get the point (vertex) count. point_count = (bClosed ? points.Count - 1 : points.Count); // Test for self-intersection. CurveIntersections intesections = Intersection.CurveSelf(curve, m_tolerance); bIntersect = (intesections.Count > 0) ? true : false; // If the curve is not closed, no reason to continue... if (!bClosed) { return(true); } // Test if the distance between each point is identical. double distance = 0.0; for (int i = 1; i < points.Count; i++) { Point3d p0 = points[i - 1]; Point3d p1 = points[i]; Vector3d v = p0 - p1; double d = v.Length; if (i == 1) { distance = d; continue; } else if (Math.Abs(distance - d) < m_tolerance) { continue; } else { distance = RhinoMath.UnsetValue; break; } } // Set return value. bLength = RhinoMath.IsValidDouble(distance); // Test if the angle between each point is identical. double angle = 0.0; for (int i = 1; i < points.Count - 1; i++) { Point3d p0 = points[i - 1]; Point3d p1 = points[i]; Point3d p2 = points[i + 1]; Vector3d v0 = p1 - p0; Vector3d v1 = p1 - p2; v0.Unitize(); v1.Unitize(); double a = Vector3d.VectorAngle(v0, v1); if (i == 1) { angle = a; continue; } else if (Math.Abs(angle - a) < m_angle_tolerance) { continue; } else { angle = RhinoMath.UnsetValue; break; } } // Set return value. bAngle = RhinoMath.IsValidDouble(angle); return(true); }
// =============================================================================================== // splits curve at kinks (discontinuity) // =============================================================================================== public static bool CurveDiscontinuity(List <Curve> L, Curve crv, int continuity, bool recursive) { if (crv == null) { return(false); } PolyCurve polycurve = crv as PolyCurve; if (polycurve != null) { if (recursive) { polycurve.RemoveNesting(); } Curve[] segments = polycurve.Explode(); if (segments == null) { return(false); } if (segments.Length == 0) { return(false); } if (recursive) { foreach (Curve S in segments) { return(CurveDiscontinuity(L, S, continuity, recursive)); } } else { foreach (Curve S in segments) { L.Add(S.DuplicateShallow() as Curve); } } return(true); } PolylineCurve polyline = crv as PolylineCurve; if (polyline != null) { if (recursive) { for (int i = 0; i < (polyline.PointCount - 1); i++) { L.Add(new LineCurve(polyline.Point(i), polyline.Point(i + 1))); } } else { L.Add(polyline.DuplicateCurve()); } return(true); } Polyline p; if (crv.TryGetPolyline(out p)) { if (recursive) { for (int i = 0; i < (p.Count - 1); i++) { L.Add(new LineCurve(p[i], p[i + 1])); } } else { L.Add(new PolylineCurve()); } return(true); } //Maybe it’s a LineCurve? LineCurve line = crv as LineCurve; if (line != null) { L.Add(line.DuplicateCurve()); return(true); } //It might still be an ArcCurve… ArcCurve arc = crv as ArcCurve; if (arc != null) { L.Add(arc.DuplicateCurve()); return(true); } //Nothing else worked, lets assume it’s a nurbs curve and go from there… NurbsCurve nurbs = crv.ToNurbsCurve(); if (nurbs == null) { return(false); } double t0 = nurbs.Domain.Min; double t1 = nurbs.Domain.Max; double t; int LN = L.Count; do { if (!nurbs.GetNextDiscontinuity((Continuity)continuity, t0, t1, out t)) { break; } Interval trim = new Interval(t0, t); if (trim.Length < 1e-10) { t0 = t; continue; } Curve M = nurbs.DuplicateCurve(); M = M.Trim(trim); if (M.IsValid) { L.Add(M); } t0 = t; } while (true); if (L.Count == LN) { L.Add(nurbs); } return(true); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { int iterations = 3; List <Point3d> iPoints = new List <Point3d>(); GridVectors = new List <Vector3d>(); roadDensity = 0; List <Curve> outputCurves = new List <Curve>(); pointCloudStructureList = new List <PointCloudStructure>(); DA.GetDataList(0, iPoints); DA.GetDataList(1, GridVectors); DA.GetData(2, ref GridFirstPoint); DA.GetData(3, ref GridLastPoint); DA.GetData(4, ref boundary); DA.GetData(5, ref roadDensity); DA.GetData(6, ref angle); DA.GetData(7, ref iterations); DA.GetData(8, ref step); // step = blockWidth; // roadDensity = step - 10; gridRes = (int)Math.Sqrt(GridVectors.Count) - 1; pointCloud = new Point3dList(); int sign = 1; Vector3d prevTensor = new Vector3d(0, 1, 0); Point3d nextPt = new Point3d(); Vector3d nextTensor = new Vector3d(); List <Point3d> currPointList = new List <Point3d>(); List <Point3d> prevPointList = new List <Point3d>(); for (int i = 0; i < 1; i++) {///////////////////////////////////////////////////// List <Curve> currCurves = new List <Curve>(); for (int j = 0; j < iPoints.Count; j++) // J ======= iPoints { sign = 1; currPointList.Clear(); prevPointList.Clear(); for (int k = 0; k < 2; k++) { prevPointList = new List <Point3d>(currPointList); currPointList.Clear(); // prevTensor = new Vector3d((i + 1) % 2, (i) % 2, 0); prevTensor = new Vector3d(0, 0, 0); if (GetTensor(iPoints[j], prevTensor) != Vector3d.Unset) { nextPt = iPoints[j] + sign * GetTensor(iPoints[j], prevTensor); currPointList.Add(iPoints[j]); prevTensor = GetTensor(iPoints[j], prevTensor); } int f = 0; while (CheckPt(nextPt) && f < 40) { currPointList.Add(nextPt); pointCloudStructureList.Add(new PointCloudStructure(nextPt, currPointList.Count - 1)); nextTensor = GetTensor(currPointList[currPointList.Count - 1], prevTensor); nextPt = currPointList[currPointList.Count - 1] + sign * nextTensor; f++; prevTensor = nextTensor; } if (pointCloud.Count > 0) { Point3d pt = pointCloud[pointCloud.ClosestIndex(nextPt)]; // if ((pt.DistanceTo(nextPt) <= roadDensity) && f > 0) currPointList.Add(pt); } outputCurves.Add(new PolylineCurve(currPointList)); currCurves.Add(new PolylineCurve(currPointList)); sign = -1; } pointCloud.AddRange(currPointList); pointCloud.AddRange(prevPointList); } /* iPoints.Clear(); * * foreach (PolylineCurve curve in currCurves) * { * if (curve != null && curve.GetLength() > 0.1) * { * // if (curve.DivideEquidistant(blockWidth) != null) * // iPoints.AddRange(new List<Point3d>(curve.DivideEquidistant(blockWidth))); * List<Point3d> points = new List<Point3d>(); * for (int q = 0; q < curve.PointCount; q++) * points.Add(curve.Point(q)); * iPoints.AddRange(points); * } * } */ angle += 90; }////////////////////////////////////////////// Point3dList tempList1 = pointCloud; pointCloud.Clear(); for (int i = 0; i < 1; i++) {///////////////////////////////////////////////////// List <Curve> currCurves = new List <Curve>(); for (int j = 0; j < iPoints.Count; j++) // J ======= iPoints { sign = 1; currPointList.Clear(); prevPointList.Clear(); for (int k = 0; k < 2; k++) { prevPointList = new List <Point3d>(currPointList); currPointList.Clear(); // prevTensor = new Vector3d((i + 1) % 2, (i) % 2, 0); prevTensor = new Vector3d(0, 0, 0); if (GetTensor(iPoints[j], prevTensor) != Vector3d.Unset) { nextPt = iPoints[j] + sign * GetTensor(iPoints[j], prevTensor); currPointList.Add(iPoints[j]); prevTensor = GetTensor(iPoints[j], prevTensor); } int f = 0; while (CheckPt(nextPt) && f < 40) { currPointList.Add(nextPt); pointCloudStructureList.Add(new PointCloudStructure(nextPt, currPointList.Count - 1)); nextTensor = GetTensor(currPointList[currPointList.Count - 1], prevTensor); nextPt = currPointList[currPointList.Count - 1] + sign * nextTensor; f++; prevTensor = nextTensor; } if (pointCloud.Count > 0) { Point3d pt = pointCloud[pointCloud.ClosestIndex(nextPt)]; // if ((pt.DistanceTo(nextPt) <= roadDensity) && f > 0) currPointList.Add(pt); } outputCurves.Add(new PolylineCurve(currPointList)); currCurves.Add(new PolylineCurve(currPointList)); sign = -1; } pointCloud.AddRange(currPointList); pointCloud.AddRange(prevPointList); } iPoints.Clear(); foreach (PolylineCurve curve in currCurves) { if (curve != null && curve.GetLength() > 0.1) { // if (curve.DivideEquidistant(blockWidth) != null) // iPoints.AddRange(new List<Point3d>(curve.DivideEquidistant(blockWidth))); List <Point3d> points = new List <Point3d>(); for (int q = 0; q < curve.PointCount; q++) { points.Add(curve.Point(q)); } iPoints.AddRange(points); } } angle += 90; }////////////////////////////////////////////// Point3dList tempList2 = pointCloud; pointCloud.Clear(); pointCloud.AddRange(tempList1); pointCloud.AddRange(tempList2); for (int i = 0; i < 2; i++) {///////////////////////////////////////////////////// List <Curve> currCurves = new List <Curve>(); for (int j = 0; j < iPoints.Count; j++) // J ======= iPoints { sign = 1; currPointList.Clear(); prevPointList.Clear(); for (int k = 0; k < 2; k++) { prevPointList = new List <Point3d>(currPointList); currPointList.Clear(); // prevTensor = new Vector3d((i + 1) % 2, (i) % 2, 0); prevTensor = new Vector3d(0, 0, 0); if (GetTensor(iPoints[j], prevTensor) != Vector3d.Unset) { nextPt = iPoints[j] + sign * GetTensor(iPoints[j], prevTensor); currPointList.Add(iPoints[j]); prevTensor = GetTensor(iPoints[j], prevTensor); } int f = 0; while (CheckPt(nextPt) && f < 40) { currPointList.Add(nextPt); pointCloudStructureList.Add(new PointCloudStructure(nextPt, currPointList.Count - 1)); nextTensor = GetTensor(currPointList[currPointList.Count - 1], prevTensor); nextPt = currPointList[currPointList.Count - 1] + sign * nextTensor; f++; prevTensor = nextTensor; } if (pointCloud.Count > 0) { Point3d pt = pointCloud[pointCloud.ClosestIndex(nextPt)]; // if ((pt.DistanceTo(nextPt) <= roadDensity) && f > 0) currPointList.Add(pt); } outputCurves.Add(new PolylineCurve(currPointList)); currCurves.Add(new PolylineCurve(currPointList)); sign = -1; } // pointCloud.AddRange(currPointList); //pointCloud.AddRange(prevPointList); } iPoints.Clear(); PolylineCurve curve = currCurves[0] as PolylineCurve; //foreach (PolylineCurve curve in currCurves) // { if (curve != null && curve.GetLength() > 0.1) { // if (curve.DivideEquidistant(blockWidth) != null) // iPoints.AddRange(new List<Point3d>(curve.DivideEquidistant(blockWidth))); List <Point3d> points = new List <Point3d>(); for (int q = 0; q < curve.PointCount; q++) { points.Add(curve.Point(q)); } iPoints.AddRange(points); } // } angle += 90; }////////////////////////////////////////////// DA.SetDataList(0, outputCurves); }