private void SimplifySection(int i, int j, int depth) { depth += 1; int[] sectionIndex = new int[2]; if ((i + 1) == j) { LineSegment newSeg = _line.GetSegment(i); _line.AddToResult(newSeg); // leave this segment in the input index, for efficiency return; } bool isValidToSimplify = true; /** * Following logic ensures that there is enough points in the output line. * If there is already more points than the minimum, there's nothing to check. * Otherwise, if in the worst case there wouldn't be enough points, * don't flatten this segment (which avoids the worst case scenario) */ if (_line.ResultSize < _line.MinimumSize) { int worstCaseSize = depth + 1; if (worstCaseSize < _line.MinimumSize) { isValidToSimplify = false; } } double[] distance = new double[1]; int furthestPtIndex = FindFurthestPoint(_linePts, i, j, distance); // flattening must be less than distanceTolerance if (distance[0] > _distanceTolerance) { isValidToSimplify = false; } // test if flattened section would cause intersection LineSegment candidateSeg = new LineSegment(); candidateSeg.P0 = _linePts[i]; candidateSeg.P1 = _linePts[j]; sectionIndex[0] = i; sectionIndex[1] = j; if (HasBadIntersection(_line, sectionIndex, candidateSeg)) { isValidToSimplify = false; } if (isValidToSimplify) { LineSegment newSeg = Flatten(i, j); _line.AddToResult(newSeg); return; } SimplifySection(i, furthestPtIndex, depth); SimplifySection(furthestPtIndex, j, depth); }
/// <summary> /// /// </summary> /// <param name="i"></param> /// <param name="j"></param> /// <param name="depth"></param> private void SimplifySection(int i, int j, int depth) { depth += 1; int[] sectionIndex = new int[2]; if ((i + 1) == j) { LineSegment newSeg = _line.GetSegment(i); _line.AddToResult(newSeg); // leave this segment in the input index, for efficiency return; } double[] distance = new double[1]; int furthestPtIndex = FindFurthestPoint(_linePts, i, j, distance); bool isValidToFlatten = true; // must have enough points in the output line if (_line.ResultSize < _line.MinimumSize && depth < 2) { isValidToFlatten = false; } // flattening must be less than distanceTolerance if (distance[0] > DistanceTolerance) { isValidToFlatten = false; } // test if flattened section would cause intersection LineSegment candidateSeg = new LineSegment(); candidateSeg.P0 = _linePts[i]; candidateSeg.P1 = _linePts[j]; sectionIndex[0] = i; sectionIndex[1] = j; if (HasBadIntersection(_line, sectionIndex, candidateSeg)) { isValidToFlatten = false; } if (isValidToFlatten) { LineSegment newSeg = Flatten(i, j); _line.AddToResult(newSeg); return; } SimplifySection(i, furthestPtIndex, depth); SimplifySection(furthestPtIndex, j, depth); }