1 /*=========================================================================
2
3 Program: Visualization Toolkit
4
5 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
6 All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13 =========================================================================*/
14
15 #include "vtkTestUtilities.h"
16 #include "vtkRegressionTestImage.h"
17
18 #include "vtkActor.h"
19 #include "vtkCamera.h"
20 #include "vtkLookupTable.h"
21 #include "vtkHardwareSelector.h"
22 #include "vtkIdTypeArray.h"
23 #include "vtkInformation.h"
24 #include "vtkNew.h"
25 #include "vtkPointGaussianMapper.h"
26 #include "vtkPointSource.h"
27 #include "vtkProp3DCollection.h"
28 #include "vtkRandomAttributeGenerator.h"
29 #include "vtkRenderedAreaPicker.h"
30 #include "vtkRenderer.h"
31 #include "vtkRenderWindowInteractor.h"
32 #include "vtkRenderWindow.h"
33 #include "vtkSelection.h"
34 #include "vtkSelectionNode.h"
35
TestPointGaussianSelection(int argc,char * argv[])36 int TestPointGaussianSelection(int argc, char *argv[])
37 {
38 int desiredPoints = 1.0e3;
39
40 vtkNew<vtkPointSource> points;
41 points->SetNumberOfPoints(desiredPoints);
42 points->SetRadius(pow(desiredPoints,0.33)*20.0);
43 points->Update();
44
45 vtkNew<vtkRandomAttributeGenerator> randomAttr;
46 randomAttr->SetInputConnection(points->GetOutputPort());
47
48 vtkNew<vtkPointGaussianMapper> mapper;
49
50 vtkNew<vtkRenderer> renderer;
51 renderer->SetBackground(0.0, 0.0, 0.0);
52 vtkNew<vtkRenderWindow> renderWindow;
53 renderWindow->SetSize(300, 300);
54 renderWindow->SetMultiSamples(0);
55 renderWindow->AddRenderer(renderer);
56 vtkNew<vtkRenderWindowInteractor> iren;
57 iren->SetRenderWindow(renderWindow);
58
59 vtkNew<vtkActor> actor;
60 actor->SetMapper(mapper);
61 renderer->AddActor(actor);
62
63 #ifdef TestPoints
64 randomAttr->SetDataTypeToUnsignedChar();
65 randomAttr->GeneratePointVectorsOn();
66 randomAttr->SetMinimumComponentValue(0);
67 randomAttr->SetMaximumComponentValue(255);
68 randomAttr->Update();
69 mapper->SetInputConnection(randomAttr->GetOutputPort());
70 mapper->SelectColorArray("RandomPointVectors");
71 mapper->SetScalarModeToUsePointFieldData();
72 mapper->SetScaleFactor(0.0);
73 mapper->EmissiveOff();
74 #else
75 randomAttr->SetDataTypeToFloat();
76 randomAttr->GeneratePointScalarsOn();
77 randomAttr->GeneratePointVectorsOn();
78 randomAttr->Update();
79
80 mapper->SetInputConnection(randomAttr->GetOutputPort());
81 mapper->SetColorModeToMapScalars();
82 mapper->SetScalarModeToUsePointFieldData();
83 mapper->SelectColorArray("RandomPointVectors");
84 mapper->SetInterpolateScalarsBeforeMapping(0);
85 mapper->SetScaleArray("RandomPointVectors");
86 mapper->SetScaleArrayComponent(3);
87
88 // Note that LookupTable is 4x faster than
89 // ColorTransferFunction. So if you have a choice
90 // Usa a lut instead.
91 //
92 vtkNew<vtkLookupTable> lut;
93 lut->SetHueRange(0.1,0.2);
94 lut->SetSaturationRange(1.0,0.5);
95 lut->SetValueRange(0.8,1.0);
96 mapper->SetLookupTable(lut);
97 #endif
98
99 renderWindow->Render();
100 renderer->GetActiveCamera()->Zoom(3.5);
101 renderWindow->Render();
102
103 vtkNew<vtkHardwareSelector> selector;
104 selector->SetFieldAssociation(vtkDataObject::FIELD_ASSOCIATION_POINTS);
105 selector->SetRenderer(renderer);
106 selector->SetArea(10,10,50,50);
107 vtkSelection *result = selector->Select();
108
109 bool goodPick = false;
110
111 if (result->GetNumberOfNodes() == 1)
112 {
113 vtkSelectionNode *node = result->GetNode(0);
114 vtkIdTypeArray *selIds = vtkArrayDownCast<vtkIdTypeArray>(
115 node->GetSelectionList());
116
117 if (selIds)
118 {
119 vtkIdType numIds = selIds->GetNumberOfTuples();
120 for (vtkIdType i = 0; i < numIds; ++i)
121 {
122 vtkIdType curId = selIds->GetValue(i);
123 cerr << curId << "\n";
124 }
125 }
126
127 if (node->GetProperties()->Has(vtkSelectionNode::PROP_ID())
128 && node->GetProperties()->Get(vtkSelectionNode::PROP()) == actor.Get()
129 && node->GetProperties()->Get(vtkSelectionNode::COMPOSITE_INDEX()) == 1
130 && selIds
131 && selIds->GetNumberOfTuples() == 14
132 && selIds->GetValue(4) == 227
133 )
134 {
135 goodPick = true;
136 }
137 }
138 result->Delete();
139
140 if (!goodPick)
141 {
142 cerr << "Incorrect splats picked!\n";
143 return EXIT_FAILURE;
144 }
145
146 // Interact if desired
147 int retVal = vtkRegressionTestImage(renderWindow);
148 if ( retVal == vtkRegressionTester::DO_INTERACTOR)
149 {
150 iren->Start();
151 }
152
153 return !retVal;
154 }
155