private Matrix4d getSingleMatrixA(DicomFile file)
        {
            double[] i = file.Dataset.Get <double[]>(DicomTag.ImageOrientationPatient);
            double[,] F =
                new double[, ]
            {
                { i[3], i[0] },
                { i[4], i[1] },
                { i[5], i[2] }
            };

            Point3d F0 = new Point3d(F[0, 0], F[1, 0], F[2, 0]);
            Point3d F1 = new Point3d(F[1, 1], F[1, 1], F[2, 1]);
            var     n  = F0.Cross(F1);

            double[] T1 = file.Dataset.Get <double[]>(DicomTag.ImagePositionPatient);
            double[] TN = file.Dataset.Get <double[]>(DicomTag.ImagePositionPatient);
            double[] px = file.Dataset.Get <double[]>(DicomTag.PixelSpacing);
            double   dr = px[0];
            double   dc = px[1];


            double k1 = n.X * 2;
            double k2 = n.Y * 2;
            double k3 = n.Z * 2;

            Matrix4d A = new Matrix4d(
                new double[] { F[0, 0] * dr, F[1, 0] * dr, F[2, 0] * dr, 0, F[0, 1] * dc, F[1, 1] * dc, F[2, 1] * dc, 0, k1, k2, k3, 0, T1[0], T1[1], T1[2], 1 });

            return(A);
        }
Пример #2
0
        public void Point3dCrossTest()
        {
            var p1 = new Point3d(1, 2, 3);
            var p2 = new Point3d(3, 2, 1);
            var p  = p1.Cross(p2);

            Assert.AreEqual(p.X, -4);
            Assert.AreEqual(p.Y, 8);
            Assert.AreEqual(p.Z, -4);
        }
Пример #3
0
        public void Point3d_Cross_123_456()
        {
            Point3d a = new Point3d(1.0, 2.0, 3.0),
                    b = new Point3d(4.0, 5.0, 6.0);
            Point3d actual = a.Cross(b);
            Point3d expected = new Point3d(-3.0, 6.0, -3.0);

            Assert.That(actual.x, Is.EqualTo(expected.x).Within(1).Ulps);
            Assert.That(actual.y, Is.EqualTo(expected.y).Within(1).Ulps);
            Assert.That(actual.z, Is.EqualTo(expected.z).Within(1).Ulps);
        }
        public void Point3d_Cross_123_456()
        {
            Point3d a        = new Point3d(1.0, 2.0, 3.0),
                    b        = new Point3d(4.0, 5.0, 6.0);
            Point3d actual   = a.Cross(b);
            Point3d expected = new Point3d(-3.0, 6.0, -3.0);

            Assert.That(actual.x, Is.EqualTo(expected.x).Within(1).Ulps);
            Assert.That(actual.y, Is.EqualTo(expected.y).Within(1).Ulps);
            Assert.That(actual.z, Is.EqualTo(expected.z).Within(1).Ulps);
        }
Пример #5
0
        public void Point3d_Cross_Oddball()
        {
            // I started with random numbers for a and b, got the values of
            // 'expected', then used the Visual Studio debugger's Watch panel
            // to get (double)(float)value. This test case will therefore serve
            // only as a regression test in case I do something stupid.
            Point3d a = new Point3d(0.001, 0.0001, 0.00001),
                 b = new Point3d(473.6, 9.0, 1.1);
            Point3d actual = a.Cross(b);
            Point3d expected = new Point3d(0.000020000000000000012, 0.0036360000000000003, -0.038360000000000005);

            Assert.That(actual.x, Is.EqualTo(expected.x).Within(1).Ulps);
            Assert.That(actual.y, Is.EqualTo(expected.y).Within(1).Ulps);
            Assert.That(actual.z, Is.EqualTo(expected.z).Within(1).Ulps);
        }
        public void Point3d_Cross_Oddball()
        {
            // I started with random numbers for a and b, got the values of
            // 'expected', then used the Visual Studio debugger's Watch panel
            // to get (double)(float)value. This test case will therefore serve
            // only as a regression test in case I do something stupid.
            Point3d a        = new Point3d(0.001, 0.0001, 0.00001),
                    b        = new Point3d(473.6, 9.0, 1.1);
            Point3d actual   = a.Cross(b);
            Point3d expected = new Point3d(0.000020000000000000012, 0.0036360000000000003, -0.038360000000000005);

            Assert.That(actual.x, Is.EqualTo(expected.x).Within(1).Ulps);
            Assert.That(actual.y, Is.EqualTo(expected.y).Within(1).Ulps);
            Assert.That(actual.z, Is.EqualTo(expected.z).Within(1).Ulps);
        }
Пример #7
0
            public Plane(string map_line, bool flip_normal = false)
            {
                char[]   plane_delims  = { '(', ')', ' ' };
                string[] plane_strings = map_line.Split(plane_delims, StringSplitOptions.RemoveEmptyEntries);

                double a_x, a_y, a_z;

                Double.TryParse(plane_strings[0], out a_x);
                Double.TryParse(plane_strings[1], out a_y);
                Double.TryParse(plane_strings[2], out a_z);

                double b_x, b_y, b_z;

                Double.TryParse(plane_strings[3], out b_x);
                Double.TryParse(plane_strings[4], out b_y);
                Double.TryParse(plane_strings[5], out b_z);

                double c_x, c_y, c_z;

                Double.TryParse(plane_strings[6], out c_x);
                Double.TryParse(plane_strings[7], out c_y);
                Double.TryParse(plane_strings[8], out c_z);

                this.f_a = new Point3d(a_x, a_y, a_z);
                this.f_b = new Point3d(b_x, b_y, b_z);
                this.f_c = new Point3d(c_x, c_y, c_z);

                Point3d vec_a = new Point3d(this.b - this.a),
                        vec_b = new Point3d(this.c - this.a);

                this.f_normal = vec_a.Cross(vec_b).Normalize();
                if (flip_normal)
                {
                    this.f_normal = new Point3d(-this.f_normal.x, -this.f_normal.y, -this.f_normal.z);
                }
            }
        /// <summary>
        /// Sort the files based on the offset position from the first (sorted) file
        /// See http://nipy.org/nibabel/dicom/dicom_orientation.html
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        private DicomFile[] sort(DicomFile[] files)
        {
            if (files.Length == 1)
            {
                return(files);
            }

            if (files[0].Dataset.Get <double>(DicomTag.SliceLocation, double.MaxValue) != double.MaxValue)
            {
                return(sortBySliceLocation(files));
            }

            DicomFile[] sortedArray = new DicomFile[files.Length];
            //d should be an offset from the first (sorted) file.
            double[] d  = new double[files.Length];
            double   ds = 1;

            for (int j = 0; j < files.Length; j++)
            {
                double[] i  = files[j].Dataset.Get <double[]>(DicomTag.ImageOrientationPatient);
                double[] Tj = files[j].Dataset.Get <double[]>(DicomTag.ImagePositionPatient);

                Point3d F0 = new Point3d(i[3], i[4], i[5]);
                Point3d F1 = new Point3d(i[0], i[1], i[2]);
                var     n  = F0.Cross(F1) * ds;
                d[j] = Tj[0] * n.X + Tj[1] * n.Y + Tj[2] * n.Z;
            }

            List <DicomFile> sortedList  = new List <DicomFile>();
            List <double>    sortedListD = new List <double>();

            sortedList.Add(files[0]);
            sortedListD.Add(d[0]);

            for (int i = 1; i < files.Length; i++)
            {
                for (int j = 0; j < sortedList.Count; j++)
                {
                    if (sortedList.Count == 1)
                    {
                        if (sortedListD[j] > d[i])
                        {
                            sortedList.Add(files[i]);
                            sortedListD.Add(d[i]);
                            break;
                        }
                        else
                        {
                            sortedList.Insert(0, files[i]);
                            sortedListD.Insert(0, d[i]);
                            break;
                        }
                    }
                    else if (j < sortedList.Count - 1)
                    {
                        if (d[i] >= sortedListD[j] && d[i] <= sortedListD[j + 1])
                        {
                            sortedList.Insert(j + 1, files[i]);
                            sortedListD.Insert(j + 1, d[i]);
                            break;
                        }
                    }
                    else
                    {
                        sortedListD.Add(d[i]);
                        sortedList.Add(files[i]);
                        break;
                    }
                }
            }

            return(sortedList.ToArray());
        }
Пример #9
0
            public Plane(string map_line, bool flip_normal=false)
            {
                char[] plane_delims = { '(', ')', ' ' };
                string[] plane_strings = map_line.Split(plane_delims, StringSplitOptions.RemoveEmptyEntries);

                double a_x, a_y, a_z;
                Double.TryParse(plane_strings[0], out a_x);
                Double.TryParse(plane_strings[1], out a_y);
                Double.TryParse(plane_strings[2], out a_z);

                double b_x, b_y, b_z;
                Double.TryParse(plane_strings[3], out b_x);
                Double.TryParse(plane_strings[4], out b_y);
                Double.TryParse(plane_strings[5], out b_z);

                double c_x, c_y, c_z;
                Double.TryParse(plane_strings[6], out c_x);
                Double.TryParse(plane_strings[7], out c_y);
                Double.TryParse(plane_strings[8], out c_z);

                this.f_a = new Point3d(a_x, a_y, a_z);
                this.f_b = new Point3d(b_x, b_y, b_z);
                this.f_c = new Point3d(c_x, c_y, c_z);

                Point3d vec_a = new Point3d(this.b - this.a),
                        vec_b = new Point3d(this.c - this.a);
                this.f_normal = vec_a.Cross(vec_b).Normalize();
                if (flip_normal)
                    this.f_normal = new Point3d(-this.f_normal.x, -this.f_normal.y, -this.f_normal.z);
            }