private GridBasedVoxelDataStructure GetJacobianMatrix(GammaDistribution gammaDistribution) { var jacobian = CreateBlank(gammaDistribution.Gamma); var v = gammaDistribution.Vectors; foreach (Voxel voxel in jacobian) { jacobian.SetVoxelByCoords( (float)voxel.Position.X, (float)voxel.Position.Y, (float)voxel.Position.Z, (float)GetDeterminant(voxel.Position, v)); } return(jacobian); }
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); }