예제 #1
0
파일: GridMath.cs 프로젝트: yt876/rt-dtools
        public IVoxelDataStructure Subtract(IVoxelDataStructure grid1, IVoxelDataStructure grid2)
        {
            var   newGrid = CreateBlank(grid1, grid2);
            int   lenX    = newGrid.XCoords.Length;
            int   lenY    = newGrid.YCoords.Length;
            int   lenZ    = newGrid.ZCoords.Length;
            Voxel v1      = new Voxel();
            Voxel v2      = new Voxel();

            for (int i = 0; i < lenX; i++)
            {
                for (int j = 0; j < lenY; j++)
                {
                    for (int k = 0; k < lenZ; k++)
                    {
                        double x = newGrid.XCoords[i];
                        double y = newGrid.YCoords[j];
                        double z = newGrid.ZCoords[k];
                        grid1.Interpolate(x, y, z, v1);
                        v1.Value *= grid1.Scaling;
                        grid2.Interpolate(x, y, z, v2);
                        v2.Value *= grid2.Scaling;

                        /*newGrid.Data[i, j, k] = 100 * (v1.Value - v2.Value) / (grid1.MaxVoxel.Value*grid1.Scaling);
                         * if (newGrid.Data[i, j, k] > newGrid.MaxVoxel.Value)
                         *  newGrid.MaxVoxel.Value = newGrid.Data[i, j, k];
                         * if (newGrid.Data[i, j, k] < newGrid.MinVoxel.Value)
                         *  newGrid.MinVoxel.Value = newGrid.Data[i, j, k];*/
                    }
                }
            }
            return(newGrid);
        }
예제 #2
0
파일: GridMath.cs 프로젝트: yt876/rt-dtools
        public GammaDistribution Gamma(IVoxelDataStructure reference, IVoxelDataStructure evaluated, IProgress <int> progress, float distTol, float doseTol, float threshold)
        {
            var         gammaDistribution = new GammaDistribution();
            VectorField gammaVectorField  = new VectorField();
            var         xVectors          = CreateBlank(reference);
            var         yVectors          = CreateBlank(reference);
            var         zVectors          = CreateBlank(reference);

            float thresholdDose = (threshold / 100) * reference.MaxVoxel.Value * reference.Scaling;

            doseTol = (doseTol / 100) * (reference.MaxVoxel.Value * reference.Scaling); //global gamma

            var newGrid = CreateBlank(reference);
            //Create a sorted list of offsets with a certain diameter and step size
            var offsets = CreateOffsets(distTol * 3, distTol / 10.0, newGrid.GridSpacing);

            Point3d posn  = new Point3d();
            Point3d posn2 = new Point3d();

            double dx = 0, dy = 0, dz = 0; // Keep track of where we are pointing.

            int voxelNum    = 0;
            int totalVoxels = newGrid.NumberOfVoxels;

            foreach (Voxel voxel in newGrid)
            {
                voxelNum++;
                if (voxelNum % (totalVoxels / 20) == 0)
                {
                    progress.Report((int)(100 * ((double)voxelNum / (double)totalVoxels)));
                }

                posn = voxel.Position;

                var refDose  = reference.Interpolate(posn).Value *reference.Scaling;
                var evalDose = evaluated.Interpolate(posn).Value *evaluated.Scaling;
                if (refDose < thresholdDose && evalDose < thresholdDose)
                {
                    newGrid.SetVoxelByCoords((float)posn.X, (float)posn.Y, (float)posn.Z, -1);
                    continue;
                }

                //Set minGamma squared using a point with no offset (dose difference only).
                var dd = refDose - evalDose;

                float minGammaSquared = GammaSquared(dd * dd, 0, doseTol * doseTol, distTol * distTol);
                dx = dy = dz = 0;

                //Store the last distance we evaluated
                float lastDistSq = 0;

                //loop throught the sorted list of offsets
                for (int o = 1; o < offsets.Count; o++)
                {
                    Offset offset = offsets[o];
                    float  distSq = (float)offset.DistanceSquared;

                    //set posn2 to to the actual physical location in the grid we are interested in
                    posn.Add(offset.Displacement, posn2);

                    if (minGammaSquared < distSq / (distTol * distTol) && distSq > lastDistSq)
                    {
                        break; //there is no way gamma can get smaller since distance is increasing in each offset and future gamma will be dominated by the DTA portion.
                    }

                    dx = offset.Displacement.X;
                    dy = offset.Displacement.Y;
                    dz = offset.Displacement.Z;

                    //compute dose difference squared and then gamma squared
                    refDose  = reference.Interpolate(posn).Value *reference.Scaling;
                    evalDose = evaluated.Interpolate(posn2).Value *evaluated.Scaling;

                    float doseDiff = (refDose - evalDose);

                    float gammaSquared = GammaSquared(doseDiff * doseDiff, distSq, doseTol * doseTol, distTol * distTol);

                    if (gammaSquared < minGammaSquared)
                    {
                        minGammaSquared = gammaSquared;
                    }

                    lastDistSq = distSq;
                }

                float gamma = (float)Math.Sqrt((double)minGammaSquared);

                if (gamma > 0 || gamma < 0 || float.IsNaN(gamma) || float.IsInfinity(gamma) || float.IsNegativeInfinity(gamma))
                {
                }

                newGrid.SetVoxelByCoords((float)posn.X, (float)posn.Y, (float)posn.Z, gamma);
                xVectors.SetVoxelByCoords((float)posn.X, (float)posn.Y, (float)posn.Z, (float)dx);
                yVectors.SetVoxelByCoords((float)posn.X, (float)posn.Y, (float)posn.Z, (float)dy);
                zVectors.SetVoxelByCoords((float)posn.X, (float)posn.Y, (float)posn.Z, (float)dz);

                TestAndSetMinAndMax(newGrid, gamma);
            }

            gammaDistribution.Gamma     = newGrid;
            gammaDistribution.Vectors.X = xVectors;
            gammaDistribution.Vectors.Y = yVectors;
            gammaDistribution.Vectors.Z = zVectors;
            //gammaDistribution.Jacobian = GetJacobianMatrix(gammaDistribution);

            return(gammaDistribution);
        }