Skip to content

Commit 6e46f6f

Browse files
committed
WIP: Sobel gradients
1 parent 62f9fea commit 6e46f6f

File tree

1 file changed

+100
-2
lines changed

1 file changed

+100
-2
lines changed

Assets/Scripts/VolumeData/VolumeDataset.cs

+100-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ public async Task<Texture3D> GetGradientTextureAsync(IProgressHandler progressHa
137137
{
138138
if (progressHandler == null)
139139
progressHandler = new NullProgressHandler();
140-
gradientTexture = await CreateGradientTextureInternalAsync(progressHandler != null ? progressHandler : NullProgressHandler.instance);
140+
try {
141+
gradientTexture = await CreateGradientTextureInternalAsync(progressHandler != null ? progressHandler : NullProgressHandler.instance);
142+
}
143+
catch(System.Exception exception)
144+
{
145+
Debug.LogException(exception);
146+
}
141147
}
142148
finally
143149
{
@@ -418,7 +424,94 @@ await Task.Run(() => {
418424
return texture;
419425

420426
}
421-
public Vector3 GetGrad(int x, int y, int z, float minValue, float maxRange)
427+
428+
private float Convolve(int x, int y, int z, float[,,] matrix)
429+
{
430+
float result = 0;
431+
for (int iz = 0; iz <=2; iz++)
432+
{
433+
for (int iy = 0; iy <=2; iy++)
434+
{
435+
for (int ix = 0; ix <=2; ix++)
436+
{
437+
float matrixValue = matrix[iz, iy, ix];
438+
float dataValue = GetData(x + ix - 1, y + iy - 1, z + iz - 1);
439+
result += matrixValue * dataValue;
440+
}
441+
}
442+
}
443+
return result;
444+
}
445+
446+
float[,,] kernelx = {
447+
{
448+
{-1, 0, 1},
449+
{-2, 0, 2},
450+
{-1, 0, 1}
451+
},
452+
{
453+
{-2, 0, 2},
454+
{-4, 0, 4},
455+
{-2, 0, 2}
456+
},
457+
{
458+
{-1, 0, 1},
459+
{-2, 0, 2},
460+
{-1, 0, 1}
461+
}};
462+
463+
float[,,] kernely = {
464+
{
465+
{-1, -2, -1},
466+
{0, 0, 0},
467+
{1, 2, 1}
468+
},
469+
{
470+
{-2, -4, -2},
471+
{0, 0, 0},
472+
{2, 4, 2}
473+
},
474+
{
475+
{-1, -2, -1},
476+
{0, 0, 0},
477+
{1, 2, 1}
478+
}};
479+
480+
float[,,] kernelz = {
481+
{
482+
{-1, -2, -1},
483+
{-2, -4, -2},
484+
{-1, -2, -1}
485+
},
486+
{
487+
{0, 0, 0},
488+
{0, 0, 0},
489+
{0, 0, 0}
490+
},
491+
{
492+
{1, 2, 1},
493+
{2, 4, 2},
494+
{1, 2, 1}
495+
}};
496+
497+
public Vector3 GetGradSobel(int x, int y, int z, float minValue, float maxRange)
498+
{
499+
// TODO
500+
if (x < 2 || y < 2 || z < 2 || x > dimX - 3 || y > dimY - 3 || z > dimZ - 3)
501+
{
502+
return Vector3.zero;
503+
}
504+
505+
float dx = Convolve(x, y, z, kernelx);
506+
float dy = Convolve(x, y, z, kernely);
507+
float dz = Convolve(x, y, z, kernelz);
508+
509+
Vector3 gradient = new Vector3(dx, dy, dz);
510+
511+
return new Vector3(gradient.x / maxRange, gradient.y / maxRange, gradient.z / maxRange);
512+
}
513+
514+
public Vector3 GetGradSimple(int x, int y, int z, float minValue, float maxRange)
422515
{
423516
float x1 = data[Math.Min(x + 1, dimX - 1) + y * dimX + z * (dimX * dimY)] - minValue;
424517
float x2 = data[Math.Max(x - 1, 0) + y * dimX + z * (dimX * dimY)] - minValue;
@@ -430,6 +523,11 @@ public Vector3 GetGrad(int x, int y, int z, float minValue, float maxRange)
430523
return new Vector3((x2 - x1) / maxRange, (y2 - y1) / maxRange, (z2 - z1) / maxRange);
431524
}
432525

526+
public Vector3 GetGrad(int x, int y, int z, float minValue, float maxRange)
527+
{
528+
return GetGradSobel(x, y, z, minValue, maxRange);
529+
}
530+
433531
public float GetAvgerageVoxelValues(int x, int y, int z)
434532
{
435533
// if a dimension length is not an even number

0 commit comments

Comments
 (0)