|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Rhino; |
| 7 | +using Rhino.Geometry; |
| 8 | +using Rhino.Collections; |
| 9 | + |
| 10 | +namespace QuadHyp |
| 11 | +{ |
| 12 | + public class PolygonPolygonAnalyticalIntersection2d |
| 13 | + { |
| 14 | + public NurbsCurve testCurve; |
| 15 | + public NurbsCurve trimCurve; |
| 16 | + public List<double> tsTest = new List<double>(); |
| 17 | + public List<double> tsTrim = new List<double>(); |
| 18 | + public List<IntersectionPointRelation> relations = new List<IntersectionPointRelation>(); |
| 19 | + public List<Point3d> intersectionPoints = new List<Point3d>(); |
| 20 | + public bool hasResult = false; |
| 21 | + |
| 22 | + |
| 23 | + int testCurveSpanCount; |
| 24 | + List<Interval> testCurveDomains = new List<Interval>(); |
| 25 | + List<double> testCurveTurningTs = new List<double>(); |
| 26 | + List<Line> testCurveSegments = new List<Line>(); |
| 27 | + |
| 28 | + public List<Span> testSpans = new List<Span>(); |
| 29 | + |
| 30 | + |
| 31 | + int trimCurveSpanCount; |
| 32 | + List<Interval> trimCurveDomains = new List<Interval>(); |
| 33 | + List<double> trimCurveTurningTs = new List<double>(); |
| 34 | + List<Line> trimCurveSegments = new List<Line>(); |
| 35 | + |
| 36 | + public List<Span> trimSpans = new List<Span>(); |
| 37 | + |
| 38 | + public List<Span> resultSpans = new List<Span>(); |
| 39 | + public List<NurbsCurve> resultCurves = new List<NurbsCurve>(); |
| 40 | + |
| 41 | + public NurbsCurve resultCurve; |
| 42 | + |
| 43 | + |
| 44 | + public PolygonPolygonAnalyticalIntersection2d(NurbsCurve _testCurve, NurbsCurve _trimCurve) |
| 45 | + { |
| 46 | + testCurve = _testCurve; |
| 47 | + trimCurve = _trimCurve; |
| 48 | + |
| 49 | + testCurveSpanCount = testCurve.SpanCount; |
| 50 | + for (int i = 0; i < testCurveSpanCount; i++) |
| 51 | + { |
| 52 | + testCurveDomains.Add(testCurve.SpanDomain(i)); |
| 53 | + testCurveSegments.Add(new Line(testCurve.PointAt(testCurve.SpanDomain(i).T0), testCurve.PointAt(testCurve.SpanDomain(i).T1))); |
| 54 | + testCurveTurningTs.Add(testCurve.SpanDomain(i).T0); |
| 55 | + } |
| 56 | + testCurveTurningTs.Add(testCurve.SpanDomain(testCurve.SpanCount - 1).T1); |
| 57 | + |
| 58 | + trimCurveSpanCount = trimCurve.SpanCount; |
| 59 | + for (int i = 0; i < trimCurveSpanCount; i++) |
| 60 | + { |
| 61 | + trimCurveDomains.Add(trimCurve.SpanDomain(i)); |
| 62 | + trimCurveSegments.Add(new Line(trimCurve.PointAt(trimCurve.SpanDomain(i).T0), trimCurve.PointAt(trimCurve.SpanDomain(i).T1))); |
| 63 | + trimCurveTurningTs.Add(trimCurve.SpanDomain(i).T0); |
| 64 | + } |
| 65 | + trimCurveTurningTs.Add(trimCurve.SpanDomain(trimCurve.SpanCount - 1).T1); |
| 66 | + |
| 67 | + CalculateIntersections(); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public PolygonPolygonAnalyticalIntersection2d(NurbsCurve _testCurve, NurbsCurve _trimCurve, bool coarse) |
| 72 | + { |
| 73 | + testCurve = _testCurve; |
| 74 | + trimCurve = _trimCurve; |
| 75 | + |
| 76 | + testCurveSpanCount = testCurve.SpanCount; |
| 77 | + for (int i = 0; i < testCurveSpanCount; i++) |
| 78 | + { |
| 79 | + testCurveDomains.Add(testCurve.SpanDomain(i)); |
| 80 | + testCurveSegments.Add(new Line(testCurve.PointAt(testCurve.SpanDomain(i).T0), testCurve.PointAt(testCurve.SpanDomain(i).T1))); |
| 81 | + testCurveTurningTs.Add(testCurve.SpanDomain(i).T0); |
| 82 | + } |
| 83 | + testCurveTurningTs.Add(testCurve.SpanDomain(testCurve.SpanCount - 1).T1); |
| 84 | + |
| 85 | + trimCurveSpanCount = trimCurve.SpanCount; |
| 86 | + for (int i = 0; i < trimCurveSpanCount; i++) |
| 87 | + { |
| 88 | + trimCurveDomains.Add(trimCurve.SpanDomain(i)); |
| 89 | + trimCurveSegments.Add(new Line(trimCurve.PointAt(trimCurve.SpanDomain(i).T0), trimCurve.PointAt(trimCurve.SpanDomain(i).T1))); |
| 90 | + trimCurveTurningTs.Add(trimCurve.SpanDomain(i).T0); |
| 91 | + } |
| 92 | + trimCurveTurningTs.Add(trimCurve.SpanDomain(trimCurve.SpanCount - 1).T1); |
| 93 | + |
| 94 | + if (coarse) |
| 95 | + { |
| 96 | + int numInside = 0; |
| 97 | + foreach (double t in testCurveTurningTs) |
| 98 | + { |
| 99 | + if (PointInsidePolygon2d(trimCurve, testCurve.PointAt(t)) != PointContainment.Outside) |
| 100 | + { |
| 101 | + numInside++; |
| 102 | + } |
| 103 | + } |
| 104 | + if (numInside == testCurveTurningTs.Count) |
| 105 | + { |
| 106 | + resultCurves.Add(testCurve); |
| 107 | + resultCurve = testCurve; |
| 108 | + hasResult = true; |
| 109 | + return; |
| 110 | + } |
| 111 | + } |
| 112 | + CalculateIntersections(); |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + public PointContainment PointInsidePolygon2d(NurbsCurve targetCurve, Point3d testPoint) |
| 117 | + { |
| 118 | + PointContainment contains = targetCurve.Contains(testPoint, Plane.WorldXY, 0.001); |
| 119 | + return contains; |
| 120 | + } |
| 121 | + |
| 122 | + void sortAndRemoveDuplicates(List<double> list) |
| 123 | + { |
| 124 | + list.Sort(); |
| 125 | + int i = 0; |
| 126 | + while (i < list.Count - 1) |
| 127 | + { |
| 128 | + if (list[i] == list[i + 1]) |
| 129 | + { |
| 130 | + list.RemoveAt(i); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + i++; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + void CalculateIntersections() |
| 140 | + { |
| 141 | + LineLineAnalyticalIntersection2d LLI2; |
| 142 | + List<double> intersectionTestCurveTs = new List<double>(); |
| 143 | + List<double> intersectionTrimCurveTs = new List<double>(); |
| 144 | + List<Point3d> ttpt = new List<Point3d>(); |
| 145 | + |
| 146 | + for (int iTest = 0; iTest < testCurve.SpanCount; iTest++) |
| 147 | + { |
| 148 | + |
| 149 | + for (int iTrim = 0; iTrim < trimCurve.SpanCount; iTrim++) |
| 150 | + { |
| 151 | + LLI2 = new LineLineAnalyticalIntersection2d(testCurveSegments[iTest], trimCurveSegments[iTrim]); |
| 152 | + if (LLI2.isIntersecting) |
| 153 | + { |
| 154 | + intersectionTestCurveTs.Add(LLI2.t1 * (-testCurveDomains[iTest].T0 + testCurveDomains[iTest].T1) + testCurveDomains[iTest].T0); |
| 155 | + intersectionTrimCurveTs.Add(LLI2.t2 * (-trimCurveDomains[iTrim].T0 + trimCurveDomains[iTrim].T1) + trimCurveDomains[iTrim].T0); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + intersectionTestCurveTs.AddRange(testCurveTurningTs); |
| 161 | + sortAndRemoveDuplicates(intersectionTestCurveTs); |
| 162 | + |
| 163 | + intersectionTrimCurveTs.AddRange(trimCurveTurningTs); |
| 164 | + sortAndRemoveDuplicates(intersectionTrimCurveTs); |
| 165 | + |
| 166 | + Span span; |
| 167 | + for (int iTest = 0; iTest < intersectionTestCurveTs.Count - 1; iTest++) |
| 168 | + { |
| 169 | + span = new Span(testCurve, new Interval(intersectionTestCurveTs[iTest], intersectionTestCurveTs[iTest + 1])); |
| 170 | + span.ConfigureRelations(trimCurve); |
| 171 | + testSpans.Add(span); |
| 172 | + } |
| 173 | + |
| 174 | + for (int iTrim = 0; iTrim < intersectionTrimCurveTs.Count - 1; iTrim++) |
| 175 | + { |
| 176 | + span = new Span(trimCurve, new Interval(intersectionTrimCurveTs[iTrim], intersectionTrimCurveTs[iTrim + 1])); |
| 177 | + span.ConfigureRelations(testCurve); |
| 178 | + trimSpans.Add(span); |
| 179 | + } |
| 180 | + |
| 181 | + resultSpans.AddRange(testSpans.FindAll(x => !(x.spanRelation.Equals(SpanRelation.Outside)))); |
| 182 | + resultSpans.AddRange(trimSpans.FindAll(x => !(x.spanRelation.Equals(SpanRelation.Outside)))); |
| 183 | + |
| 184 | + if ((resultSpans.Count != 0) & (resultSpans != null)) |
| 185 | + { |
| 186 | + Console.WriteLine("123");//TODO |
| 187 | + |
| 188 | + foreach (Span s in resultSpans) |
| 189 | + { |
| 190 | + resultCurves.Add(s.line.ToNurbsCurve()); |
| 191 | + } |
| 192 | + resultCurve = Curve.JoinCurves(resultCurves)[0].ToNurbsCurve(); |
| 193 | + hasResult = true; |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + hasResult = false; |
| 198 | + } |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | +} |
0 commit comments