@@ -110,11 +110,20 @@ public static Texture2D GenerateHistogramTextureOnGPU(VolumeDataset dataset)
110
110
/// <returns></returns>
111
111
public static Texture2D Generate2DHistogramTexture ( VolumeDataset dataset )
112
112
{
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 ) ;
114
121
int numGradientSamples = 256 ;
115
- Color [ ] cols = new Color [ numSamples * numGradientSamples ] ;
116
- Texture2D texture = new Texture2D ( numSamples , numGradientSamples , TextureFormat . RGBAFloat , false ) ;
117
122
123
+ Color [ ] cols = new Color [ numDensitySamples * numGradientSamples ] ;
124
+ Texture2D texture = new Texture2D ( numDensitySamples , numGradientSamples , TextureFormat . RGBAFloat , false ) ;
125
+
126
+ // Zero-initialise colours.
118
127
for ( int iCol = 0 ; iCol < cols . Length ; iCol ++ )
119
128
cols [ iCol ] = new Color ( 0.0f , 0.0f , 0.0f , 0.0f ) ;
120
129
@@ -137,8 +146,16 @@ public static Texture2D Generate2DHistogramTexture(VolumeDataset dataset)
137
146
int z1 = dataset . data [ x + y * dataset . dimX + ( z + 1 ) * ( dataset . dimX * dataset . dimY ) ] ;
138
147
int z2 = dataset . data [ x + y * dataset . dimX + ( z - 1 ) * ( dataset . dimX * dataset . dimY ) ] ;
139
148
149
+ // Calculate gradient
140
150
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 ;
142
159
}
143
160
}
144
161
}
0 commit comments