private IEnumerable <DicomImagePlane> GetPlanesParallelToReferencePlane()
 {
     foreach (IPresentationImage image in CurrentReferenceImage.ParentDisplaySet.PresentationImages)
     {
         DicomImagePlane plane = DicomImagePlane.FromImage(image);
         if (plane != null)
         {
             if (_currentReferenceImagePlane.IsInSameFrameOfReference(plane) &&
                 _currentReferenceImagePlane.IsParallelTo(plane, _oneDegreeInRadians))
             {
                 yield return(plane);
             }
         }
     }
 }
        private DicomImagePlane GetClosestParallelImagePlane(DicomImagePlane referenceImagePlane, IEnumerable <DicomImagePlane> targetImagePlanes)
        {
            DicomImagePlane closestImagePlane           = null;
            float           distanceToClosestImagePlane = float.MaxValue;

            foreach (DicomImagePlane targetImagePlane in targetImagePlanes)
            {
                if (targetImagePlane.IsParallelTo(referenceImagePlane, SynchronizationToolSettingsHelper.Default.ParallelPlanesToleranceAngleRadians))
                {
                    bool sameFrameOfReference = referenceImagePlane.IsInSameFrameOfReference(targetImagePlane);
                    if (this.StudiesLinked || sameFrameOfReference)
                    {
                        Vector3D calibratedOffset = _frameOfReferenceCalibrator.GetOffset(referenceImagePlane, targetImagePlane) ?? Vector3D.Null;

                        Vector3D distanceToImagePlane = referenceImagePlane.PositionPatientCenterOfImage + calibratedOffset - targetImagePlane.PositionPatientCenterOfImage;

                        float absoluteDistanceToImagePlane = Math.Abs(distanceToImagePlane.Magnitude);

                        if (absoluteDistanceToImagePlane < distanceToClosestImagePlane)
                        {
                            distanceToClosestImagePlane = absoluteDistanceToImagePlane;
                            closestImagePlane           = targetImagePlane;
                        }
                    }
                }
            }

            return(closestImagePlane);
        }
        private void GetPlaneClosestToReferenceImagePoint(PointF referenceImagePoint,
                                                          IEnumerable <DicomImagePlane> targetImagePlanes,
                                                          out DicomImagePlane closestTargetImagePlane,
                                                          out PointF closestTargetImagePoint)
        {
            closestTargetImagePlane = null;
            closestTargetImagePoint = PointF.Empty;

            float distanceToClosestImagePlane = float.MaxValue;

            Vector3D referencePositionPatient = _referencePlane.ConvertToPatient(referenceImagePoint);

            foreach (DicomImagePlane targetImagePlane in targetImagePlanes)
            {
                float halfThickness = Math.Abs(targetImagePlane.Thickness / 2);
                float halfSpacing   = Math.Abs(targetImagePlane.Spacing / 2);
                float toleranceDistanceToImagePlane = Math.Max(halfThickness, halfSpacing);

                if (_referencePlane.IsInSameFrameOfReference(targetImagePlane))
                {
                    if (toleranceDistanceToImagePlane > 0)
                    {
                        Vector3D positionTargetImagePlane   = targetImagePlane.ConvertToImagePlane(referencePositionPatient);
                        float    distanceToTargetImagePlane = Math.Abs(positionTargetImagePlane.Z);

                        if (distanceToTargetImagePlane <= toleranceDistanceToImagePlane && distanceToTargetImagePlane < distanceToClosestImagePlane)
                        {
                            distanceToClosestImagePlane = distanceToTargetImagePlane;
                            //The coordinates need to be converted to pixel coordinates because right now they are in mm.
                            closestTargetImagePoint = targetImagePlane.ConvertToImage(new PointF(positionTargetImagePlane.X, positionTargetImagePlane.Y));
                            closestTargetImagePlane = targetImagePlane;
                        }
                    }
                }
            }
        }
            public void Calibrate(DicomImagePlane referenceImagePlane, DicomImagePlane targetImagePlane)
            {
                if (!referenceImagePlane.IsInSameFrameOfReference(targetImagePlane) && referenceImagePlane.IsParallelTo(targetImagePlane, _angleTolerance))
                {
                    Plane referencePlane = FromDicomImagePlane(referenceImagePlane);
                    Plane targetPlane    = FromDicomImagePlane(targetImagePlane);

                    Dictionary <Plane, Vector3D> referenceOffsets = GetOffsetDictionary(referencePlane);
                    Dictionary <Plane, Vector3D> targetOffsets    = GetOffsetDictionary(targetPlane);

                    Vector3D offset = targetImagePlane.PositionPatientCenterOfImage - referenceImagePlane.PositionPatientCenterOfImage;

                    referenceOffsets[targetPlane] = offset;
                    targetOffsets[referencePlane] = -offset;
                }
            }