public static List <Point3d> GetAllCurveIntersections(List <Curve> curves, bool unique) { int numCurves = curves.Count; List <Point3d> allIntersectionPoints = new List <Point3d>(); for (int i = 0; i < numCurves; i++) { for (int j = 0; j < numCurves; j++) { if (j != i) { Rhino.Geometry.Intersect.CurveIntersections ccx = Rhino.Geometry.Intersect.Intersection.CurveCurve(curves[i], curves[j], 0.1, 0.1); for (int k = 0; k < ccx.Count; k++) { if (ccx[k].IsPoint) { allIntersectionPoints.Add(ccx[k].PointA); } } } } } return(unique ? new List <Point3d>(Point3d.CullDuplicates(allIntersectionPoints, 0.1)) : allIntersectionPoints); }
private bool CheckErrors(List <Point3d> points) { if (points.Count < 3) { RhinoApp.WriteLine("Convex hull needs at least 3 points"); return(false); } Point3d[] noDuplicates = Point3d.CullDuplicates(points, RhinoDoc.ActiveDoc.ModelAbsoluteTolerance); points = new List <Point3d>(noDuplicates); return(true); }
public static void PointsFromIntersection(CurveToCurveRelation source) { var pts = Intersection.CurveCurve(source.GeometryA, source.GeometryB, 0.1, 0.1) .Where(x => x.IsPoint) .Select(x => x.PointA) .ToList(); source.PropertiesA.AllPointsFromIntersection = pts; source.PropertiesB.AllPointsFromIntersection = pts; var uniquePts = Point3d.CullDuplicates(pts, 0.1).ToList(); source.PropertiesA.UniquePointsFromIntersection = uniquePts; source.PropertiesB.UniquePointsFromIntersection = uniquePts; }
//Create a list of points from bar elements (remove duplicates) Point3d[] extractNodes(List <Line> bars) { //Create a list of all points List <Point3d> nodesAll = new List <Point3d>(); foreach (Line ln in bars) { nodesAll.Add(ln.From); nodesAll.Add(ln.To); } //Cull duplicates Point3d[] nodes = Point3d.CullDuplicates(nodesAll, RhinoDoc.ActiveDoc.ModelAbsoluteTolerance); return(nodes); }
//Create a list of points from bar elements (remove duplicates) Point3d[] extractNodes(List <Line> bars, double tol) { //Create a list of all points List <Point3d> nodesAll = new List <Point3d>(); foreach (Line ln in bars) { nodesAll.Add(ln.From); nodesAll.Add(ln.To); } //Cull duplicates Point3d[] nodes = Point3d.CullDuplicates(nodesAll, tol); return(nodes); }
public static List <Point3d> GetAllCurveIntersections(Curve referenceCurve, List <Curve> otherCurves, bool unique) { //Console.WriteLine("Function called!"); List <Point3d> allIntersectionPoints = new List <Point3d>(); foreach (Curve curve in otherCurves) { Rhino.Geometry.Intersect.CurveIntersections ccx = Rhino.Geometry.Intersect.Intersection.CurveCurve(referenceCurve, curve, 0.1, 0.1); //Console.WriteLine("CCX Run!"); for (int i = 0; i < ccx.Count; i++) { if (ccx[i].IsPoint) { allIntersectionPoints.Add(ccx[i].PointA); } } //Console.WriteLine("Curve"); } if (allIntersectionPoints.Count == 0) { return(allIntersectionPoints); } if (!unique) { return(allIntersectionPoints); } else { return(new List <Point3d>(Point3d.CullDuplicates(allIntersectionPoints, 0.1))); } }
/// <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) { Surface surface = null; int uCount = 10; int vCount = 10; if (!DA.GetData(0, ref surface)) { return; } if (!DA.GetData(1, ref uCount)) { return; } if (!DA.GetData(2, ref vCount)) { return; } ////////////////////////////////////////////// Surface x = surface; int un = uCount; int vn = vCount; x.SetDomain(0, new Interval(0, 1)); x.SetDomain(1, new Interval(0, 1)); double uu = 1.0 / un; double vv = 1.0 / vn; double ustart = 0; double vstart = 0; List <double> us1 = new List <double>(); List <double> us2 = new List <double>(); List <double> vs = new List <double>(); for (int i = 0; i < un + 1; i++) { us1.Add(ustart); ustart += uu; } /////////////////////////////////////// us2.Add(0); us2.Add(uu / 2); ustart = uu / 2; for (int i = 0; i < un - 1; i++) { ustart += uu; us2.Add(ustart); } us2.Add(1); /////////////////////////////////////// for (int i = 0; i < vn + 1; i++) { vs.Add(vstart); vstart += vv; } ////////////////////////////////////// DataTree <Point3d> pts = new DataTree <Point3d>(); int index = 0; for (int i = 0; i < vn + 1; i++) { if ((i + 1) / 2.0 == Convert.ToInt32((i + 1) / 2.0))//奇数 { for (int j = 0; j < us2.Count; j++) { Point3d pt = x.PointAt(us2[j], vs[i]); pts.Add(pt, new GH_Path(0, DA.Iteration, index)); } } else//偶数 { for (int j = 0; j < us1.Count; j++) { Point3d pt = x.PointAt(us1[j], vs[i]); pts.Add(pt, new GH_Path(0, DA.Iteration, index)); } } index++; } ////////////////////////////////////////////////////////////// DataTree <Polyline> pls = new DataTree <Polyline>(); index = 0; for (int i = 0; i < pts.BranchCount - 1; i++) { if ((i + 1) / 2.0 == Convert.ToInt32((i + 1) / 2.0))//奇数 { List <Polyline> pln = PL2(pts.Branch(i), pts.Branch(i + 1)); pls.AddRange(pln, new GH_Path(0, DA.Iteration, index)); } else { List <Polyline> pln = PL1(pts.Branch(i), pts.Branch(i + 1)); pls.AddRange(pln, new GH_Path(0, DA.Iteration, index)); } index++; } ////////////////////////////////////////////////////////////////如果u方向闭合 DataTree <Point3d> pts2 = new DataTree <Point3d>(); index = 0; if (x.IsClosed(0)) { for (int i = 0; i < pts.BranchCount; i++) { Point3d[] ptsnew = Point3d.CullDuplicates(pts.Branch(i), 0.000001); List <Point3d> ptend = ptsnew.ToList(); if ((i + 1) / 2.0 == Convert.ToInt32((i + 1) / 2.0))//奇数 { ptend.RemoveAt(0); } pts2.AddRange(ptend, new GH_Path(0, DA.Iteration, index)); index++; } pls.Clear(); index = 0; for (int i = 0; i < pts2.BranchCount - 1; i++) { if ((i + 1) / 2.0 == Convert.ToInt32((i + 1) / 2.0))//奇数 { List <Polyline> plyss = PL4(pts2.Branch(i), pts2.Branch(i + 1)); pls.AddRange(plyss, new GH_Path(0, DA.Iteration, index)); } else { List <Polyline> plyss = PL3(pts2.Branch(i), pts2.Branch(i + 1)); pls.AddRange(plyss, new GH_Path(0, DA.Iteration, index)); } index++; } DA.SetDataTree(1, pts2); } else { DA.SetDataTree(1, pts); } DA.SetDataTree(0, pls); }
private static List <double> getUniqueParameter(List <Curve> baseCurve, Plot plot, out Curve perpendicularCurve, out List <int> indexOfTheParameter) { List <double> output = new List <double>(); ////////////////////////////////////////// BoundingBox boundaryBox = plot.Boundary.GetBoundingBox(false); double extendLength = boundaryBox.Corner(true, true, true).DistanceTo(boundaryBox.Corner(false, false, true)); Curve tempCurve = baseCurve[(int)(baseCurve.Count() / 2)]; Point3d centerPoint = tempCurve.PointAt(tempCurve.Domain.Mid); Vector3d centerPointPerpVector = tempCurve.TangentAt(tempCurve.Domain.Mid); centerPointPerpVector.Transform(Transform.Rotation(Math.PI / 2, Point3d.Origin)); perpendicularCurve = new LineCurve(centerPoint, centerPoint + centerPointPerpVector); double perpCurveStart = -extendLength; double perpCurveEnd = extendLength; perpendicularCurve = new LineCurve(perpendicularCurve.PointAt(perpCurveStart), perpendicularCurve.PointAt(perpCurveEnd)); List <Point3d> pointOnPerpCurve = new List <Point3d>(); foreach (Curve i in baseCurve) { double tempParameter; perpendicularCurve.ClosestPoint(i.PointAt(i.Domain.Mid), out tempParameter); pointOnPerpCurve.Add(perpendicularCurve.PointAt(tempParameter)); } List <Point3d> uniquePointOnPerpCurve = Point3d.CullDuplicates(pointOnPerpCurve, 1000).ToList(); List <double> uniquePointParameter = new List <double>(); foreach (Point3d i in uniquePointOnPerpCurve) { double tempParameter; perpendicularCurve.ClosestPoint(i, out tempParameter); uniquePointParameter.Add(tempParameter); } RhinoList <Point3d> rhinoUniquePointOnPerpCurve = new RhinoList <Point3d>(uniquePointOnPerpCurve); rhinoUniquePointOnPerpCurve.Sort(uniquePointParameter.ToArray()); uniquePointParameter.Sort(); List <int> tempIndexOfParameter = new List <int>(); foreach (Point3d i in rhinoUniquePointOnPerpCurve) { int index = pointOnPerpCurve.IndexOf(i); tempIndexOfParameter.Add(index); } /// indexOfTheParameter = tempIndexOfParameter; return(uniquePointParameter); }
public static int FacingSouth(Apartment agOut) { //create house outlines and windows with normal facing outwards List <List <List <Curve> > > houseOutline = new List <List <List <Curve> > >(); List <List <List <List <Line> > > > windowLinesOld = agOut.getLightingWindow(); List <List <List <List <Line> > > > windowLines = new List <List <List <List <Line> > > >(); for (int i = 0; i < agOut.Household.Count; i++) { List <List <Curve> > houseOutline_i = new List <List <Curve> >(); List <List <List <Line> > > windowLines_i = new List <List <List <Line> > >(); for (int j = 0; j < agOut.Household[i].Count; j++) { List <Curve> houseOutline_j = new List <Curve>(); List <List <Line> > windowLines_j = new List <List <Line> >(); for (int k = 0; k < agOut.Household[i][j].Count(); k++) { //create house outline curve List <Point3d> outlinePoints = new List <Point3d>(); Point3d pt = new Point3d(agOut.Household[i][j][k].Origin); Vector3d x = new Vector3d(agOut.Household[i][j][k].XDirection); Vector3d y = new Vector3d(agOut.Household[i][j][k].YDirection); double xa = agOut.Household[i][j][k].XLengthA; double xb = agOut.Household[i][j][k].XLengthB; double ya = agOut.Household[i][j][k].YLengthA; double yb = agOut.Household[i][j][k].YLengthB; outlinePoints.Add(pt); pt.Transform(Transform.Translation(Vector3d.Multiply(y, yb))); outlinePoints.Add(pt); pt.Transform(Transform.Translation(Vector3d.Multiply(x, xa - xb))); outlinePoints.Add(pt); pt.Transform(Transform.Translation(Vector3d.Multiply(y, -ya))); outlinePoints.Add(pt); pt.Transform(Transform.Translation(Vector3d.Multiply(x, -xa))); outlinePoints.Add(pt); pt.Transform(Transform.Translation(Vector3d.Multiply(y, ya - yb))); outlinePoints.Add(pt); Point3d.CullDuplicates(outlinePoints, 0); pt.Transform(Transform.Translation(Vector3d.Multiply(x, xb))); outlinePoints.Add(pt); Polyline outlinePolyline = new Polyline(outlinePoints); Curve outlineCurve = outlinePolyline.ToNurbsCurve(); houseOutline_j.Add(outlineCurve); //create windows with normal facing outwards : window normal is defined as window line tangent rotated Pi/2 List <Line> windowLines_k = new List <Line>(); for (int l = 0; l < windowLinesOld[i][j][k].Count; l++) { Line winLine = windowLinesOld[i][j][k][l]; Vector3d normal = winLine.UnitTangent; normal.Rotate(Math.PI / 2, Vector3d.ZAxis); Point3d midpt = winLine.PointAt(0); midpt.Transform(Transform.Translation(Vector3d.Multiply(winLine.UnitTangent, winLine.Length / 2))); midpt.Transform(Transform.Translation(Vector3d.Multiply(normal, 10))); if (outlineCurve.Contains(midpt) == Rhino.Geometry.PointContainment.Inside) { winLine.Flip(); windowLines_k.Add(winLine); } else if (outlineCurve.Contains(midpt) == Rhino.Geometry.PointContainment.Outside) { windowLines_k.Add(winLine); } } windowLines_j.Add(windowLines_k); } houseOutline_i.Add(houseOutline_j); windowLines_i.Add(windowLines_j); } houseOutline.Add(houseOutline_i); windowLines.Add(windowLines_i); } //calculate southward wall and window ratio List <List <double> > southwardWindowRatio = new List <List <double> >(); List <double> ratiosForScore = new List <double>(); for (int i = 0; i < windowLines.Count; i++) { List <double> southwardWindowRatioTemp = new List <double>(); for (int j = 0; j < windowLines[i].Count; j++) { double southwardWindowRatioTempTemp = 0; for (int k = 0; k < windowLines[i][j].Count; k++) { for (int l = 0; l < windowLines[i][j][k].Count(); l++) { southwardWindowRatioTempTemp += southwardWindow(houseOutline[i][j][k], windowLines[i][j][k][l]); } } ratiosForScore.Add(southwardWindowRatioTempTemp); southwardWindowRatioTemp.Add(southwardWindowRatioTempTemp); } southwardWindowRatio.Add(southwardWindowRatioTemp); } //average double avrg = 0; for (int i = 0; i < ratiosForScore.Count; i++) { avrg += ratiosForScore[i] / ratiosForScore.Count; } return((int)(avrg * 100)); }
public static List <Curve> ShatterToSegments(List <Curve> curvesToShatter) { int numCurves = curvesToShatter.Count; List <Point3d> allIntersectionPoints = new List <Point3d>(); for (int i = 0; i < numCurves; i++) { for (int j = 0; j < numCurves; j++) { if (j != i) { Rhino.Geometry.Intersect.CurveIntersections ccx = Rhino.Geometry.Intersect.Intersection.CurveCurve(curvesToShatter[i], curvesToShatter[j], 0.1, 0.1); for (int k = 0; k < ccx.Count; k++) { if (ccx[k].IsPoint) { allIntersectionPoints.Add(ccx[k].PointA); } } } } } List <Point3d> uniqueIntersectionPoints = new List <Point3d>(Point3d.CullDuplicates(allIntersectionPoints, 0.1)); //Console.WriteLine(uniqueIntersectionPoints.Count); List <Curve> allSegments = new List <Curve>(); List <double> validParameters = new List <double>(); foreach (Curve curve in curvesToShatter) { foreach (Point3d point in uniqueIntersectionPoints) { curve.ClosestPoint(point, out double t); Point3d curvePoint = curve.PointAt(t); if (point.DistanceTo(curvePoint) < 0.1) { validParameters.Add(t); } } if (validParameters.Count > 0) { Curve[] segments = curve.Split(validParameters); foreach (Curve segment in segments) { allSegments.Add(segment); } } else { allSegments.Add(curve); } validParameters.Clear(); } return(allSegments); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { #region //initiate variables List <string> curveTypes = new List <string>(); List <double> curveRadiuses = new List <double>(); List <double> superElevations = new List <double>(); List <string> curveSides = new List <string>(); SleeperType sleeperType; string sleeperTypeName = string.Empty; string SpecificationName = string.Empty; string vehicleType = string.Empty; string VechileDataFilePath = string.Empty; string SpecificationPath = string.Empty; bool VerticalTrackTolerance = false; double pointTolerance = 0.001; #endregion #region get data from input DA.GetDataList(0, curveTypes); DA.GetDataList(1, curveRadiuses); DA.GetDataList(2, superElevations); DA.GetDataList(3, curveSides); DA.GetData(4, ref sleeperTypeName); DA.GetData(5, ref SpecificationName); DA.GetData(6, ref vehicleType); DA.GetData(7, ref VechileDataFilePath); DA.GetData(8, ref SpecificationPath); DA.GetData(9, ref VerticalTrackTolerance); DA.GetData(10, ref pointTolerance); //if no data provided, do not do anything if (curveTypes.Count == 0 || curveRadiuses.Count == 0 || superElevations.Count == 0 || curveSides.Count == 0 || string.IsNullOrEmpty(sleeperTypeName) || string.IsNullOrEmpty(SpecificationName) || string.IsNullOrEmpty(vehicleType) || string.IsNullOrEmpty(VechileDataFilePath)) { return; } //if the sleeper type is not avlid, do not do anything try { sleeperType = (SleeperType)Enum.Parse(typeof(SleeperType), sleeperTypeName.ToUpper()); } catch { return; } int no = curveTypes.Count; string assPath = Assembly.GetExecutingAssembly().Location; //use default file, if not given if (string.IsNullOrEmpty(VechileDataFilePath)) { VechileDataFilePath = Path.GetDirectoryName(assPath) + "\\VehicleData.json"; } //use default file, if not given if (string.IsNullOrEmpty(SpecificationPath)) { SpecificationPath = Path.GetDirectoryName(assPath) + "\\RailcorpSpecs.json"; } //if the file is not found if (!File.Exists(VechileDataFilePath)) { return; } if (!File.Exists(SpecificationPath)) { return; } RailSpec specification = LoadSpecs(SpecificationPath); if (specification == null) { return; } TrackSpecs trackSpec = specification.TrackSpecs.Where(x => x.Type == SpecificationName).FirstOrDefault(); if (trackSpec == null) { return; } #endregion #region get vehicle data from spec and by selection List <VehicleData> vehicleDatas = LoadVehicleData(VechileDataFilePath); VehicleData vehicleData = null; if (vehicleDatas.Select(x => x.VehicleType).ToList().Contains(vehicleType)) { vehicleData = vehicleDatas.Where(x => x.VehicleType == vehicleType).FirstOrDefault(); } //terminate if no vechicle data is load if (vehicleData == null) { return; } #endregion List <Polyline> staticKE_pl = new List <Polyline>(); List <Polyline> KE1_pl = new List <Polyline>(); List <Polyline> KE2_pl = new List <Polyline>(); List <Polyline> KE3_pl = new List <Polyline>(); List <Polyline> KE4_pl = new List <Polyline>(); List <Polyline> structuralGauge_pl = new List <Polyline>(); List <string> warnings = new List <string>(); for (int i = 0; i < no; i++) { CurveType cvType = CurveType.CURVE; CurveSide cvSide = CurveSide.NONE; try { cvType = (CurveType)Enum.Parse(typeof(CurveType), curveTypes[i].ToUpper()); } catch { warnings.Add("Curve Type string is not valid. It must be 'Curve' or 'Tagent'"); continue; } try { cvSide = (CurveSide)Enum.Parse(typeof(CurveSide), curveSides[i].ToUpper()); } catch { warnings.Add("Curve Type Side is not valid. It must be 'Left' or 'Right'"); continue; } double cvRadius = curveRadiuses[i]; double e = superElevations[i]; //calculate each Dictionary <string, object> data = KEcalculation.KinematicEnvelopeCal( vehicleData, sleeperType, trackSpec, VerticalTrackTolerance, cvType, cvRadius, e, cvSide); if (data != null) { double[][] _KE0 = (double[][])data["Static"]; //data unit is mm double[][] _ke1 = (double[][])data["KE1"]; //data unit is mm double[][] _ke2 = (double[][])data["KE2"]; //data unit is mm double[][] _ke3 = (double[][])data["KE3"]; //data unit is mm double[][] _ke4 = (double[][])data["KE4"]; //data unit is mm double[][] _ke5 = (double[][])data["Structural Gauge"]; //data unit is mm string[] _warning = (string[])data["Warnings"]; List <Point3d> staticKE = new List <Point3d>(); List <Point3d> KE1 = new List <Point3d>(); List <Point3d> KE2 = new List <Point3d>(); List <Point3d> KE3 = new List <Point3d>(); List <Point3d> KE4 = new List <Point3d>(); List <Point3d> structuralGauge = new List <Point3d>(); List <Point3d> pts = new List <Point3d>(); for (int n = 0; n < _KE0[0].Count(); n++) { Point3d pt = new Point3d(_KE0[0][n] / 1000, _KE0[1][n] / 1000, 0); staticKE.Add(pt); } pts = Point3d.CullDuplicates(staticKE, pointTolerance).ToList(); pts.Add(new Point3d(_KE0[0][0] / 1000, _KE0[1][0] / 1000, 0)); Polyline pl = new Polyline(pts); staticKE_pl.Add(pl); for (int n = 0; n < _ke1[0].Count(); n++) { Point3d pt = new Point3d(_ke1[0][n] / 1000, _ke1[1][n] / 1000, 0); KE1.Add(pt); } pts = Point3d.CullDuplicates(KE1, pointTolerance).ToList(); pts.Add(new Point3d(_ke1[0][0] / 1000, _ke1[1][0] / 1000, 0)); pl = new Polyline(pts); KE1_pl.Add(pl); for (int n = 0; n < _ke2[0].Count(); n++) { Point3d pt = new Point3d(_ke2[0][n] / 1000, _ke2[1][n] / 1000, 0); KE2.Add(pt); } pts = Point3d.CullDuplicates(KE2, pointTolerance).ToList(); pts.Add(new Point3d(_ke2[0][0] / 1000, _ke2[1][0] / 1000, 0)); pl = new Polyline(pts); KE2_pl.Add(pl); for (int n = 0; n < _ke3[0].Count(); n++) { Point3d pt = new Point3d(_ke3[0][n] / 1000, _ke3[1][n] / 1000, 0); KE3.Add(pt); } pts = Point3d.CullDuplicates(KE3, pointTolerance).ToList(); pts.Add(new Point3d(_ke3[0][0] / 1000, _ke3[1][0] / 1000, 0));//add the first point pl = new Polyline(pts); KE3_pl.Add(pl); for (int n = 0; n < _ke4[0].Count(); n++) { Point3d pt = new Point3d(_ke4[0][n] / 1000, _ke4[1][n] / 1000, 0); KE4.Add(pt); } pts = Point3d.CullDuplicates(KE4, pointTolerance).ToList(); pts.Add(new Point3d(_ke4[0][0] / 1000, _ke4[1][0] / 1000, 0));//add the first point pl = new Polyline(pts); KE4_pl.Add(pl); for (int n = 0; n < _ke5[0].Count(); n++) { Point3d pt = new Point3d(_ke5[0][n] / 1000, _ke5[1][n] / 1000, 0); structuralGauge.Add(pt); } pts = Point3d.CullDuplicates(structuralGauge, pointTolerance).ToList(); pts.Add(new Point3d(_ke5[0][0] / 1000, _ke5[1][0] / 1000, 0));//add the first point pl = new Polyline(pts); structuralGauge_pl.Add(pl); warnings.AddRange(_warning.ToList()); } else { staticKE_pl[i] = null; KE1_pl[i] = null; KE2_pl[i] = null; KE3_pl[i] = null; KE4_pl[i] = null; structuralGauge_pl[i] = null; warnings[i] = null; } } DA.SetDataList(0, staticKE_pl); DA.SetDataList(1, KE1_pl); DA.SetDataList(2, KE2_pl); DA.SetDataList(3, KE3_pl); DA.SetDataList(4, KE4_pl); DA.SetDataList(5, structuralGauge_pl); DA.SetDataList(6, warnings); }