Skip to content

Commit 69e8a63

Browse files
committed
mlavik1#69: Improved Generate2DHistogramTexture. This should now work for datasets that do not have a value range starting at zero, and should hopefully fix some of the reported IndexOutOfRangeExceptions.
1 parent 88dd9c4 commit 69e8a63

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

Assets/Scripts/Utils/HistogramTextureGenerator.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,20 @@ public static Texture2D GenerateHistogramTextureOnGPU(VolumeDataset dataset)
110110
/// <returns></returns>
111111
public static Texture2D Generate2DHistogramTexture(VolumeDataset dataset)
112112
{
113-
int numSamples = dataset.GetMaxDataValue() + 1;
113+
int minValue = dataset.GetMinDataValue();
114+
int maxValue = dataset.GetMaxDataValue();
115+
116+
// Value range of the density values.
117+
int densityValRange = maxValue - minValue + 1;
118+
float densityRangeRecip = 1.0f / (maxValue - minValue); // reciprocal
119+
// Clamp density value samples.
120+
int numDensitySamples = System.Math.Min(densityValRange, 512);
114121
int numGradientSamples = 256;
115-
Color[] cols = new Color[numSamples * numGradientSamples];
116-
Texture2D texture = new Texture2D(numSamples, numGradientSamples, TextureFormat.RGBAFloat, false);
117122

123+
Color[] cols = new Color[numDensitySamples * numGradientSamples];
124+
Texture2D texture = new Texture2D(numDensitySamples, numGradientSamples, TextureFormat.RGBAFloat, false);
125+
126+
// Zero-initialise colours.
118127
for (int iCol = 0; iCol < cols.Length; iCol++)
119128
cols[iCol] = new Color(0.0f, 0.0f, 0.0f, 0.0f);
120129

@@ -137,8 +146,16 @@ public static Texture2D Generate2DHistogramTexture(VolumeDataset dataset)
137146
int z1 = dataset.data[x + y * dataset.dimX + (z + 1) * (dataset.dimX * dataset.dimY)];
138147
int z2 = dataset.data[x + y * dataset.dimX + (z - 1) * (dataset.dimX * dataset.dimY)];
139148

149+
// Calculate gradient
140150
Vector3 grad = new Vector3((x2 - x1) / (float)maxRange, (y2 - y1) / (float)maxRange, (z2 - z1) / (float)maxRange);
141-
cols[density + (int)(grad.magnitude * numGradientSamples / maxNormalisedMagnitude) * numSamples] = Color.white;
151+
152+
// Calculate density and gradient value indices (in flattened 2D array)
153+
float tDensity = (density - minValue) * densityRangeRecip;
154+
int iDensity = Mathf.RoundToInt((numDensitySamples - 1) * tDensity);
155+
int iGrad = (int)(grad.magnitude * numGradientSamples / maxNormalisedMagnitude);
156+
157+
// Assign a white colour to all samples (in a histogram where x = density and y = gradient magnitude).
158+
cols[iDensity + iGrad * numDensitySamples] = Color.white;
142159
}
143160
}
144161
}

0 commit comments

Comments
 (0)