1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    PointLocator.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkActor.h"
16 #include "vtkPointLocator.h"
17 #include "vtkPolyData.h"
18 #include "vtkPolyDataMapper.h"
19 #include "vtkProperty.h"
20 #include "vtkRenderWindow.h"
21 #include "vtkRenderWindowInteractor.h"
22 #include "vtkRenderer.h"
23 #include "vtkRenderer.h"
24 #include "vtkSphereSource.h"
25 
26 #include "vtkRegressionTestImage.h"
27 #include "vtkDebugLeaks.h"
28 
PointLocator(int argc,char * argv[])29 int PointLocator( int argc, char *argv[] )
30 {
31   vtkRenderer *renderer = vtkRenderer::New();
32   vtkRenderWindow *renWin = vtkRenderWindow::New();
33     renWin->AddRenderer(renderer);
34   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
35     iren->SetRenderWindow(renWin);
36 
37   vtkSphereSource *sphere = vtkSphereSource::New();
38     sphere->SetThetaResolution(8); sphere->SetPhiResolution(8);
39     sphere->SetRadius(1.0);
40     sphere->Update();
41   vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
42     sphereMapper->SetInputConnection(sphere->GetOutputPort());
43   vtkActor *sphereActor = vtkActor::New();
44     sphereActor->SetMapper(sphereMapper);
45 
46   vtkSphereSource *spot = vtkSphereSource::New();
47     spot->SetPhiResolution(6);
48     spot->SetThetaResolution(6);
49     spot->SetRadius(0.1);
50 
51   vtkPolyDataMapper *spotMapper = vtkPolyDataMapper::New();
52     spotMapper->SetInputConnection(spot->GetOutputPort());
53 
54   // Build a locator
55   vtkPointLocator *pointLocator = vtkPointLocator::New();
56   pointLocator->SetDataSet(sphere->GetOutput());
57   pointLocator->BuildLocator();
58 
59   //
60   double p1[] = {2.0, 1.0, 3.0};
61 
62   // Find closest point
63   vtkIdType ptId;
64   double dist;
65   p1[0] = 0.1; p1[1] = -0.2; p1[2] = 0.2;
66   ptId = pointLocator->FindClosestPoint(p1);
67   vtkActor *closestPointActor = vtkActor::New();
68     closestPointActor->SetMapper(spotMapper);
69     // TODO cleanupo
70     closestPointActor->SetPosition(
71       sphere->GetOutput()->GetPoints()->GetPoint(ptId)[0],
72       sphere->GetOutput()->GetPoints()->GetPoint(ptId)[1],
73       sphere->GetOutput()->GetPoints()->GetPoint(ptId)[2]);
74     closestPointActor->GetProperty()->SetColor(0.0, 1.0, 0.0);
75 
76   // Find closest point within radius
77   float radius = 5.0;
78   p1[0] = .2; p1[1] = 1.0; p1[2] = 1.0;
79   ptId = pointLocator->FindClosestPointWithinRadius(radius, p1, dist);
80   vtkActor *closestPointActor2 = vtkActor::New();
81     closestPointActor2->SetMapper(spotMapper);
82     // TODO cleanup
83     closestPointActor2->SetPosition(
84       sphere->GetOutput()->GetPoints()->GetPoint(ptId)[0],
85       sphere->GetOutput()->GetPoints()->GetPoint(ptId)[1],
86       sphere->GetOutput()->GetPoints()->GetPoint(ptId)[2]);
87     closestPointActor2->GetProperty()->SetColor(0.0, 1.0, 0.0);
88 
89   renderer->AddActor(sphereActor);
90   renderer->AddActor(closestPointActor);
91   renderer->AddActor(closestPointActor2);
92   renderer->SetBackground(1,1,1);
93   renWin->SetSize(300,300);
94 
95   // interact with data
96   renWin->Render();
97 
98   int retVal = vtkRegressionTestImage( renWin );
99   if ( retVal == vtkRegressionTester::DO_INTERACTOR)
100   {
101     iren->Start();
102   }
103 
104   // Clean up
105   renderer->Delete();
106   renWin->Delete();
107   iren->Delete();
108   sphere->Delete();
109   sphereMapper->Delete();
110   sphereActor->Delete();
111   spot->Delete();
112   spotMapper->Delete();
113   closestPointActor->Delete();
114   closestPointActor2->Delete();
115   pointLocator->FreeSearchStructure();
116   pointLocator->Delete();
117 
118   return !retVal;
119 }
120