Skip to content

Commit

Permalink
Normalize weights; take into account dimension reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
wschroed committed Aug 11, 2017
1 parent 6614873 commit f851d15
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
30 changes: 21 additions & 9 deletions Common/DataModel/Testing/Python/TestStaticPointLocator2.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
error = 1

# Test the generation of the binning divisions when degenerate conditions occur
print("\nTesting degenerate manual division specification")
print("\nTesting degenerate points specification")
locator.AutomaticOn()
points.SetNumberOfPoints(10)
points.SetPoint(0, 0,0,0)
Expand All @@ -73,19 +73,31 @@
locator.BuildLocator()
print("Divisions: {0}".format( locator.GetDivisions() ))
ndivs = locator.GetDivisions()
if ndivs[0] != 1 or ndivs[1] != 1 or ndivs[2] != 5:
if ndivs[0] != 1 or ndivs[1] != 1 or ndivs[2] != 2:
error = 1

# Test the generation of the binning divisions when degenerate conditions occur
print("\nTesting manual divisions, degenerate points specification")
locator.AutomaticOff()
locator.SetDivisions(0,2,10)
locator.Modified()
locator.BuildLocator()
print("Divisions: {0}".format( locator.GetDivisions() ))
ndivs = locator.GetDivisions()
if ndivs[0] != 1 or ndivs[1] != 2 or ndivs[2] != 10:
error = 1

# Test the vtkBoundingBox code
print("\nTesting vtkBoundingBox")
targetBins = 2500
print("\nTesting vtkBoundingBox w/ {} target bins".format(targetBins))
divs = [1,1,1]
bounds = [0,0,0,0,0,0]
bds = [0,0,0,0,0,0]
bbox = vtk.vtkBoundingBox()

# Degenerate
bbox.SetBounds(0,0,1,1,2,2)
bbox.ComputeDivisions(2500,bounds,divs)
bbox.ComputeDivisions(targetBins,bounds,divs)
bbox.GetBounds(bds)
print("BBox bounds: ({},{}, {},{}, {},{})".format(bds[0],bds[1],bds[2],bds[3],bds[4],bds[5]))
print(" Adjusted bounds: ({},{}, {},{}, {},{})".format(bounds[0],bounds[1],bounds[2],bounds[3],bounds[4],bounds[5]))
Expand All @@ -95,27 +107,27 @@

# Line
bbox.SetBounds(0,0,1,1,5,10)
bbox.ComputeDivisions(2500,bounds,divs)
bbox.ComputeDivisions(targetBins,bounds,divs)
bbox.GetBounds(bds)
print("BBox bounds: ({},{}, {},{}, {},{})".format(bds[0],bds[1],bds[2],bds[3],bds[4],bds[5]))
print(" Adjusted bounds: ({},{}, {},{}, {},{})".format(bounds[0],bounds[1],bounds[2],bounds[3],bounds[4],bounds[5]))
print(" Divisions: ({},{},{})".format(divs[0],divs[1],divs[2]))
if divs[0] != 1 or divs[1] != 1 or divs[2] != 39:
if divs[0] != 1 or divs[1] != 1 or divs[2] != targetBins:
error = 1

# Plane
bbox.SetBounds(-1,4,1,1,5,10)
bbox.ComputeDivisions(2500,bounds,divs)
bbox.ComputeDivisions(targetBins,bounds,divs)
bbox.GetBounds(bds)
print("BBox bounds: ({},{}, {},{}, {},{})".format(bds[0],bds[1],bds[2],bds[3],bds[4],bds[5]))
print(" Adjusted bounds: ({},{}, {},{}, {},{})".format(bounds[0],bounds[1],bounds[2],bounds[3],bounds[4],bounds[5]))
print(" Divisions: ({},{},{})".format(divs[0],divs[1],divs[2]))
if divs[0] != 23 or divs[1] != 1 or divs[2] != 23:
if divs[0] != targetBins**(1/2.0) or divs[1] != 1 or divs[2] != targetBins**(1/2.0):
error = 1

# Volume
bbox.SetBounds(-6,4,1,2,5,10)
bbox.ComputeDivisions(2500,bounds,divs)
bbox.ComputeDivisions(targetBins,bounds,divs)
bbox.GetBounds(bds)
print("BBox bounds: ({},{}, {},{}, {},{})".format(bds[0],bds[1],bds[2],bds[3],bds[4],bds[5]))
print(" Adjusted bounds: ({},{}, {},{}, {},{})".format(bounds[0],bounds[1],bounds[2],bounds[3],bounds[4],bounds[5]))
Expand Down
36 changes: 23 additions & 13 deletions Common/DataModel/vtkBoundingBox.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,15 @@ void vtkBoundingBox::Scale(double s[3])
}

// ---------------------------------------------------------------------------
// Compute the number of divisions given the current bounding box and a target
// number of divisions.
// Compute the number of divisions given the current bounding box and a
// target number of buckets/bins. Note that degenerate bounding boxes (i.e.,
// one or more of the edges are zero length) are handled properly.
vtkIdType vtkBoundingBox::
ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const
{
// First determine the maximum length of the side of the bounds. Keep track
// of zero width sides of the bounding box.
int nonZero[3], maxIdx=(-1);
int numNonZero=0, nonZero[3], maxIdx=(-1);
double max=0.0, lengths[3];
this->GetLengths(lengths);

Expand All @@ -437,11 +438,19 @@ ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const
{
maxIdx = i;
}
nonZero[i] = (lengths[i] > 0.0 ? 1 : 0);
if ( lengths[i] > 0.0 )
{
nonZero[i] = 1;
numNonZero++;
}
else
{
nonZero[i] = 0;
}
}

// If the bounding box is degenerate, then one bin of arbitrary size
if ( maxIdx < 0 || totalBins <= 1 )
if ( numNonZero < 1 )
{
divs[0] = divs[1] = divs[2] = 1;
bounds[0] = this->MinPnt[0] - 0.5;
Expand All @@ -456,15 +465,16 @@ ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const
// Okay we need to compute the divisions roughly in proportion to the
// bounding box edge lengths. The idea is to make the bins as close to a
// cube as possible.
double totLen = lengths[0] + lengths[1] + lengths[2];
double f = static_cast<double>(totalBins);
f /= (nonZero[0] ? lengths[0] : 1.0);
f /= (nonZero[1] ? lengths[1] : 1.0);
f /= (nonZero[2] ? lengths[2] : 1.0);
f = pow (f,0.333333);

divs[0] = (nonZero[0] ? vtkMath::Floor(f*lengths[0]) : 1);
divs[1] = (nonZero[1] ? vtkMath::Floor(f*lengths[1]) : 1);
divs[2] = (nonZero[2] ? vtkMath::Floor(f*lengths[2]) : 1);
f /= (nonZero[0] ? (lengths[0]/totLen) : 1.0);
f /= (nonZero[1] ? (lengths[1]/totLen) : 1.0);
f /= (nonZero[2] ? (lengths[2]/totLen) : 1.0);
f = pow (f,(1.0/static_cast<double>(numNonZero)));

divs[0] = (nonZero[0] ? vtkMath::Floor(f*lengths[0]/totLen) : 1);
divs[1] = (nonZero[1] ? vtkMath::Floor(f*lengths[1]/totLen) : 1);
divs[2] = (nonZero[2] ? vtkMath::Floor(f*lengths[2]/totLen) : 1);

// Now compute the final bounds, making sure it is a non-zero volume.
double delta = 0.5 * lengths[maxIdx] / static_cast<double>(divs[maxIdx]);
Expand Down
4 changes: 1 addition & 3 deletions Filters/Points/Testing/Python/TestSPHInterpolator2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(VTK_DATA_ROOT + "/Data/SPH_Points2D.vtu")
reader.Update()
import time
time.sleep(50)
output = reader.GetOutput()
scalarRange = output.GetPointData().GetArray("Rho").GetRange()

print("Scalar range: {}".format(scalarRange))

# Something to sample with
center = output.GetCenter()
Expand Down

0 comments on commit f851d15

Please sign in to comment.