vtkOBBTree Inside
This issue was created automatically from an original Mantis Issue. Further discussion may take place here.
http://public.kitware.com/pipermail/vtkusers/2003-May/067563.html import vtk.*;
// Program to test that vtkOBBTree.InserOrOutside has a bug, because when I create // some points and test if they are inside a surface, sometimes it says that it is // true, but if you draw the points then it seems not is true.
public class InsideOutsideTest {
static {
System.loadLibrary("vtkCommonJava");
System.loadLibrary("vtkIOJava");
System.loadLibrary("vtkGraphicsJava");
System.loadLibrary("vtkRenderingJava");
};
public static void main(String[] args) {
if (args.length > 0) {
int n = 100;
if (args.length > 1) {
try {
n = Integer.valueOf(args[1]).intValue();
} catch (NumberFormatException e) {}
}
// Read the surface
vtkPolyDataReader reader = new vtkPolyDataReader();
reader.SetFileName(args[0]);
reader.Update();
vtkPolyData surface = reader.GetOutput();
// vtkConeSource source = new vtkConeSource(); // vtkSphereSource source = new vtkSphereSource(); // vtkCubeSource source = new vtkCubeSource(); // vtkCylinderSource source = new vtkCylinderSource(); // source.Update();
// vtkPolyData surface = source.GetOutput();
// Build the OBBTree to test random points
vtkOBBTree obbTree = new vtkOBBTree();
obbTree.SetDataSet(surface);
obbTree.SetMaxLevel(10);
obbTree.BuildLocator();
// Create the points polydata
vtkPolyData points = new vtkPolyData();
points.SetPoints(new vtkPoints());
points.SetVerts(new vtkCellArray());
// Points are located inside surface bounds
double[] bb = surface.GetBounds();
for (int i = 0; i < n; i++) {
// Get a new random point
double[] point = new double[3];
point[0] = bb[0] + Math.random() * (bb[1] - bb[0]);
point[1] = bb[2] + Math.random() * (bb[3] - bb[2]);
point[2] = bb[4] + Math.random() * (bb[5] - bb[4]);
// Get new random points while they are not inside the surface
while (obbTree.InsideOrOutside(point) != -1) {
point[0] = bb[0] + Math.random() * (bb[1] - bb[0]);
point[1] = bb[2] + Math.random() * (bb[3] - bb[2]);
point[2] = bb[4] + Math.random() * (bb[5] - bb[4]);
}
// Add the point to the polydata
points.GetPoints().InsertNextPoint(point);
points.GetVerts().InsertNextCell(1);
points.GetVerts().InsertCellPoint(i);
}
// Create the surface actor
vtkPolyDataMapper surfaceMapper = new vtkPolyDataMapper();
surfaceMapper.SetInput(surface);
vtkLODActor surfaceActor = new vtkLODActor();
surfaceActor.SetMapper(surfaceMapper);
// Create the OBBTree actor
vtkPolyData obb = new vtkPolyData();
obbTree.GenerateRepresentation(5, obb);
vtkPolyDataMapper obbMapper = new vtkPolyDataMapper();
obbMapper.SetInput(obb);
vtkActor obbActor = new vtkActor();
obbActor.SetMapper(obbMapper);
obbActor.GetProperty().SetColor(1.0, 0.0, 0.0);
obbActor.GetProperty().SetRepresentationToWireframe();
// Create the points actor (using glyphs)
vtkSphereSource glyph = new vtkSphereSource();
glyph.SetRadius(0.1);
glyph.SetPhiResolution(3);
glyph.SetThetaResolution(3);
vtkGlyph3D glyphs = new vtkGlyph3D();
glyphs.SetInput(points);
glyphs.SetSource(glyph.GetOutput());
glyphs.Update();
vtkPolyDataMapper pointsMapper = new vtkPolyDataMapper();
pointsMapper.SetInput(points);
// pointsMapper.SetInput(glyphs.GetOutput()); vtkLODActor pointsActor = new vtkLODActor(); pointsActor.SetMapper(pointsMapper); pointsActor.GetProperty().SetColor(1.0, 1.0, 0.0);
// Create the renderer and add the actors
vtkRenderer renderer = new vtkRenderer();
renderer.AddActor(surfaceActor);
renderer.AddActor(obbActor);
renderer.AddActor(pointsActor);
// Create the window
vtkRenderWindow window = new vtkRenderWindow();
window.AddRenderer(renderer);
window.SetSize(500, 500);
// Create the interactor
vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
iren.SetRenderWindow(window);
iren.Initialize();
iren.Start();
} else {
System.out.println("Error: \"InsideOutsideTest <filename (byu files)> [npoints]\"");
}
}
}