/// <summary> /// Find nearest points at two sequence. /// </summary> /// <param name="firstSequence">First points sequence.</param> /// <param name="secondSequence">Second points sequence.</param> /// <returns><see cref="NearestPointsEventArgs"/>.</returns> private NearestPointsEventArgs FindNearestPoint(IEnumerable <IDepthPointSource> firstSequence, IEnumerable <IDepthPointSource> secondSequence) { var uniqueBasePoints = firstSequence.GetUniqueDepthPoints(); var uniqueAdjustablePoints = secondSequence.GetUniqueDepthPoints(); var minDistance = double.MaxValue; NearestPointsEventArgs minDistncePointsInfo = null; Parallel.ForEach(uniqueBasePoints, //create local Points info () => new NearestPointsEventArgs() { Distance = double.MaxValue }, //each thread work (uniqueBasePoint, loopState, singleThreadPointInfo) => { //save first point singleThreadPointInfo.FirstPoint = uniqueBasePoint; foreach (var uniqueAdjustablePoint in uniqueAdjustablePoints) { var dim = CoordinatePoint.GetDistanceBetweenPointsOnAnEllipsoid(uniqueBasePoint.Point, uniqueAdjustablePoint.Point).GetMeters(); if (dim < singleThreadPointInfo.Distance) { singleThreadPointInfo.SecondPoint = uniqueAdjustablePoint; singleThreadPointInfo.Distance = dim; } //if we found two points with distance less 0.1m between, then stop calc. if (Math.Abs(singleThreadPointInfo.Distance) < 0.1d) { loopState.Stop(); } } return(singleThreadPointInfo); }, //composition pointInfo => { lock (_syncRoot) { if (pointInfo.Distance < minDistance) { minDistance = pointInfo.Distance; minDistncePointsInfo = pointInfo; } } } ); //foreach (var uniqueBasePoint in uniqueBasePoints) //{ // foreach (var uniqueAdjustablePoint in uniqueAdjustablePoints) // { // var dim = CoordinatePoint.DistanceBetweenPoints(uniqueBasePoint.Point, uniqueAdjustablePoint.Point); // if (dim < distance) // { // distance = dim; // points = new Tuple<IDepthPointSource, IDepthPointSource>(uniqueBasePoint, uniqueAdjustablePoint); // } // } //} OnNearestPointFound(minDistncePointsInfo); return(minDistncePointsInfo); }
protected virtual void OnNearestPointFound(NearestPointsEventArgs e) { NearestPointsFound?.Invoke(this, e); }