Exemplo n.º 1
0
        public bool IsInSameFrameOfReference(DicomImagePlane other)
        {
            Frame otherFrame = other._sourceFrame;

            if (_sourceFrame.ParentImageSop.StudyInstanceUid != otherFrame.ParentImageSop.StudyInstanceUid)
            {
                return(false);
            }

            return(this._sourceFrame.FrameOfReferenceUid == otherFrame.FrameOfReferenceUid);
        }
 private IEnumerable <DicomImagePlane> GetTargetImagePlanes(IImageBox imageBox)
 {
     for (int i = imageBox.DisplaySet.PresentationImages.Count - 1; i >= 0; --i)
     {
         DicomImagePlane targetImagePlane = DicomImagePlane.FromImage(imageBox.DisplaySet.PresentationImages[i]);
         if (targetImagePlane != null && _referencePlane.IsInSameFrameOfReference(targetImagePlane))
         {
             yield return(targetImagePlane);
         }
     }
 }
        public void Release()
        {
            DicomImagePlane.ReleaseCache();

            --_referenceCount;
            if (_referenceCount <= 0)
            {
                _viewer.EventBroker.PresentationImageSelected -= OnPresentationImageSelected;
                _viewer.EventBroker.TileSelected -= OnTileSelected;

                Coordinators.Remove(_viewer);
            }
        }
 private static IEnumerable <DicomImagePlane> GetAllImagePlanes(IImageBox imageBox)
 {
     if (imageBox.DisplaySet != null)
     {
         for (int index = imageBox.DisplaySet.PresentationImages.Count - 1; index >= 0; --index)
         {
             DicomImagePlane targetPlane = DicomImagePlane.FromImage(imageBox.DisplaySet.PresentationImages[index]);
             if (targetPlane != null)
             {
                 yield return(targetPlane);
             }
         }
     }
 }
 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);
             }
         }
     }
 }
            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;
                }
            }
        public static SynchronizationToolCoordinator Get(IImageViewer viewer)
        {
            if (!Coordinators.ContainsKey(viewer))
            {
                SynchronizationToolCoordinator coordinator = new SynchronizationToolCoordinator(viewer);

                viewer.EventBroker.PresentationImageSelected += coordinator.OnPresentationImageSelected;
                viewer.EventBroker.TileSelected += coordinator.OnTileSelected;

                Coordinators.Add(viewer, coordinator);
            }

            DicomImagePlane.InitializeCache();

            ++Coordinators[viewer]._referenceCount;
            return(Coordinators[viewer]);
        }
            public Vector3D GetOffset(DicomImagePlane referenceImagePlane, DicomImagePlane targetImagePlane)
            {
                Vector3D offset = null;

                Plane referencePlane = FromDicomImagePlane(referenceImagePlane);

                if (_calibrationMatrix.ContainsKey(referencePlane))
                {
                    Plane targetPlane = FromDicomImagePlane(targetImagePlane);
                    if (!referencePlane.Equals(targetPlane))
                    {
                        offset = GetOffset(referencePlane, targetPlane, new List <Plane>());
                    }
                }

                return(offset);
            }
 private void CalibrateFrameOfReferenceForVisibleImageBoxes()
 {
     foreach (IImageBox referenceImageBox in this.ImageViewer.PhysicalWorkspace.ImageBoxes)
     {
         DicomImagePlane referencePlane = DicomImagePlane.FromImage(referenceImageBox.TopLeftPresentationImage);
         if (referencePlane != null)
         {
             foreach (IImageBox imageBox in GetTargetImageBoxes(referenceImageBox))
             {
                 DicomImagePlane targetPlane = DicomImagePlane.FromImage(imageBox.TopLeftPresentationImage);
                 if (targetPlane != null)
                 {
                     _frameOfReferenceCalibrator.Calibrate(referencePlane, targetPlane);
                 }
             }
         }
     }
 }
        private void Stop()
        {
            _referencePlane = null;
            _inUse          = false;

            foreach (CrossHair crosshair in _crosshairs)
            {
                crosshair.Image = null;
            }

            _coordinator.OnSpatialLocatorStopped();

            foreach (CrossHair crosshair in _crosshairs)
            {
                crosshair.Dispose();
            }

            _crosshairs.Clear();
        }
        private void RefreshReferenceLines(IPresentationImage targetImage)
        {
            DicomImagePlane targetImagePlane = DicomImagePlane.FromImage(targetImage);

            if (targetImagePlane == null)
            {
                return;
            }

            ReferenceLineCompositeGraphic referenceLineCompositeGraphic = _coordinator.GetReferenceLineCompositeGraphic(targetImage);

            if (referenceLineCompositeGraphic == null)
            {
                return;
            }

            bool showReferenceLines = this.Active && _currentReferenceImagePlane != null &&
                                      _currentReferenceImagePlane.IsInSameFrameOfReference(targetImagePlane);

            if (!showReferenceLines)
            {
                referenceLineCompositeGraphic.HideAllReferenceLines();
                return;
            }

            int i = 0;

            foreach (ReferenceLine referenceLine in GetAllReferenceLines(targetImagePlane))
            {
                ReferenceLineGraphic referenceLineGraphic = referenceLineCompositeGraphic[i++];
                referenceLineGraphic.Point1  = referenceLine.StartPoint;
                referenceLineGraphic.Point2  = referenceLine.EndPoint;
                referenceLineGraphic.Text    = referenceLine.Label;
                referenceLineGraphic.Visible = true;
            }

            // make any that aren't valid invisible.
            for (int j = i; j < referenceLineCompositeGraphic.Graphics.Count; ++j)
            {
                referenceLineCompositeGraphic[j].Visible = false;
            }
        }
Exemplo n.º 12
0
        public bool GetIntersectionPoints(DicomImagePlane other, out Vector3D intersectionPointPatient1, out Vector3D intersectionPointPatient2)
        {
            intersectionPointPatient1 = intersectionPointPatient2 = null;

            Vector3D[,] lineSegmentsImagePlaneBounds = new Vector3D[, ]
            {
                // Bounding line segments of this (reference) image plane.
                { PositionPatientTopLeft, PositionPatientTopRight },
                { PositionPatientTopLeft, PositionPatientBottomLeft },
                { PositionPatientBottomRight, PositionPatientTopRight },
                { PositionPatientBottomRight, PositionPatientBottomLeft }
            };

            List <Vector3D> planeIntersectionPoints = new List <Vector3D>();

            for (int i = 0; i < 4; ++i)
            {
                // Intersect the bounding line segments of the reference image with the plane of the target image.
                Vector3D intersectionPoint = Vector3D.GetLinePlaneIntersection(other.Normal, other.PositionPatientCenterOfImage,
                                                                               lineSegmentsImagePlaneBounds[i, 0],
                                                                               lineSegmentsImagePlaneBounds[i, 1], true);
                if (intersectionPoint != null)
                {
                    planeIntersectionPoints.Add(intersectionPoint);
                }
            }

            if (planeIntersectionPoints.Count < 2)
            {
                return(false);
            }

            intersectionPointPatient1 = planeIntersectionPoints[0];
            intersectionPointPatient2 = CollectionUtils.SelectFirst(planeIntersectionPoints,
                                                                    delegate(Vector3D point)
            {
                return(!planeIntersectionPoints[0].Equals(point));
            });

            return(intersectionPointPatient1 != null && intersectionPointPatient2 != null);
        }
        private void SynchronizeImageBox(IImageBox referenceImageBox, IImageBox targetImageBox)
        {
            if (referenceImageBox.TopLeftPresentationImage == null)
            {
                return;
            }

            if (targetImageBox.TopLeftPresentationImage == null)
            {
                return;
            }

            DicomImagePlane referenceImagePlane = DicomImagePlane.FromImage(referenceImageBox.TopLeftPresentationImage);

            if (referenceImagePlane == null)
            {
                return;
            }

            IEnumerable <DicomImagePlane> targetImagePlanes = GetAllImagePlanes(targetImageBox);
            DicomImagePlane targetImagePlane = GetClosestParallelImagePlane(referenceImagePlane, targetImagePlanes);

            if (targetImagePlane == null)
            {
                return;
            }

            int lastIndex = targetImageBox.TopLeftPresentationImageIndex;

            targetImageBox.TopLeftPresentationImage = targetImagePlane.SourceImage;

            if (lastIndex != targetImageBox.TopLeftPresentationImageIndex)
            {
                if (!_imageBoxesToDraw.Contains(targetImageBox))
                {
                    _imageBoxesToDraw.Add(targetImageBox);
                }
            }
        }
        private void SetCurrentReferencePlane()
        {
            if (CurrentReferenceImage == this.SelectedPresentationImage)
            {
                return;
            }

            _currentReferenceImagePlane = DicomImagePlane.FromImage(this.SelectedPresentationImage);
            if (_currentReferenceImagePlane == null)
            {
                return;
            }

            ReferenceLineCompositeGraphic referenceLineCompositeGraphic =
                _coordinator.GetReferenceLineCompositeGraphic(CurrentReferenceImage);

            //Hide the current image's reference lines
            if (referenceLineCompositeGraphic != null)
            {
                referenceLineCompositeGraphic.HideAllReferenceLines();
            }
        }
        private void GetFirstAndLastReferenceLines(DicomImagePlane targetImagePlane, out ReferenceLine firstReferenceLine, out ReferenceLine lastReferenceLine)
        {
            firstReferenceLine = lastReferenceLine = null;

            float firstReferenceImageZComponent = float.MaxValue;
            float lastReferenceImageZComponent  = float.MinValue;

            // 1. Find all images in the same plane as the current reference image.
            foreach (DicomImagePlane parallelPlane in GetPlanesParallelToReferencePlane())
            {
                // 2. Use the Image Position (in the coordinate system of the Image Plane without moving the origin!)
                //    to determine the first and last reference line.  By transforming the Image Position (Patient) to
                //    the coordinate system of the image plane, we can then simply take the 2 images with
                //    the smallest and largest z-components, respectively, as the 'first' and 'last' reference images.

                // < keeps the first image as close to the beginning of the display set as possible.
                if (parallelPlane.PositionImagePlaneTopLeft.Z < firstReferenceImageZComponent)
                {
                    ReferenceLine referenceLine = GetReferenceLine(parallelPlane, targetImagePlane);
                    if (referenceLine != null)
                    {
                        firstReferenceImageZComponent = parallelPlane.PositionImagePlaneTopLeft.Z;
                        firstReferenceLine            = referenceLine;
                    }
                }

                // >= keeps the last image as close to the end of the display set as possible.
                if (parallelPlane.PositionImagePlaneTopLeft.Z >= lastReferenceImageZComponent)
                {
                    ReferenceLine referenceLine = GetReferenceLine(parallelPlane, targetImagePlane);
                    if (referenceLine != null)
                    {
                        lastReferenceImageZComponent = parallelPlane.PositionImagePlaneTopLeft.Z;
                        lastReferenceLine            = referenceLine;
                    }
                }
            }
        }
        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;
                        }
                    }
                }
            }
        }
        private static ReferenceLine GetReferenceLine(DicomImagePlane referenceImagePlane, DicomImagePlane targetImagePlane)
        {
            // if planes are parallel within tolerance, then they do not intersect and thus no reference lines should be shown
            float parallelTolerance = SynchronizationToolSettingsHelper.Default.ParallelPlanesToleranceAngleRadians;

            if (referenceImagePlane.IsParallelTo(targetImagePlane, parallelTolerance))
            {
                return(null);
            }

            Vector3D intersectionPatient1, intersectionPatient2;

            if (!referenceImagePlane.GetIntersectionPoints(targetImagePlane, out intersectionPatient1, out intersectionPatient2))
            {
                return(null);
            }

            Vector3D intersectionImagePlane1 = targetImagePlane.ConvertToImagePlane(intersectionPatient1);
            Vector3D intersectionImagePlane2 = targetImagePlane.ConvertToImagePlane(intersectionPatient2);

            //The coordinates need to be converted to pixel coordinates because right now they are in mm.
            PointF intersectionImage1 = targetImagePlane.ConvertToImage(new PointF(intersectionImagePlane1.X, intersectionImagePlane1.Y));
            PointF intersectionImage2 = targetImagePlane.ConvertToImage(new PointF(intersectionImagePlane2.X, intersectionImagePlane2.Y));
            string label = referenceImagePlane.InstanceNumber.ToString();

            return(new ReferenceLine(intersectionImage1, intersectionImage2, label));
        }
 private bool Start()
 {
     _referencePlane = DicomImagePlane.FromImage(base.SelectedPresentationImage);
     return(_referencePlane != null);
 }
Exemplo n.º 19
0
 public float GetAngleBetween(DicomImagePlane other)
 {
     return(Normal.GetAngleBetween(other.Normal));
 }
Exemplo n.º 20
0
 public bool IsOrthogonalTo(DicomImagePlane other, float angleTolerance)
 {
     return(Normal.IsOrthogonalTo(other.Normal, angleTolerance));
 }