1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestContourWidget2.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 // Test functionality to initialize a contour widget from user supplied
16 // polydata. Here we will create closed circle and initialize it from that.
17 #include "vtkSmartPointer.h"
18 
19 #include "vtkCamera.h"
20 #include "vtkCellArray.h"
21 #include "vtkCommand.h"
22 #include "vtkContourWidget.h"
23 #include "vtkMath.h"
24 #include "vtkOrientedGlyphContourRepresentation.h"
25 #include "vtkPlane.h"
26 #include "vtkPoints.h"
27 #include "vtkPolyData.h"
28 #include "vtkRenderWindow.h"
29 #include "vtkRenderWindowInteractor.h"
30 #include "vtkRenderer.h"
31 #include "vtkTestUtilities.h"
32 #include "vtkWidgetEvent.h"
33 #include "vtkWidgetEventTranslator.h"
34 
TestContourWidget2(int argc,char * argv[])35 int TestContourWidget2(int argc, char* argv[])
36 {
37   // Create the RenderWindow, Renderer and both Actors
38   //
39   vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
40   vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
41   renWin->AddRenderer(ren1);
42 
43   vtkSmartPointer<vtkRenderWindowInteractor> iren =
44     vtkSmartPointer<vtkRenderWindowInteractor>::New();
45   iren->SetRenderWindow(renWin);
46 
47   ren1->SetBackground(0.1, 0.2, 0.4);
48   renWin->SetSize(600, 600);
49 
50   vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
51     vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
52   vtkSmartPointer<vtkContourWidget> contourWidget = vtkSmartPointer<vtkContourWidget>::New();
53   contourWidget->SetInteractor(iren);
54   contourWidget->SetRepresentation(contourRep);
55   contourWidget->On();
56 
57   for (int i = 0; i < argc; i++)
58   {
59     if (strcmp("-Shift", argv[i]) == 0)
60     {
61       contourWidget->GetEventTranslator()->RemoveTranslation(vtkCommand::LeftButtonPressEvent);
62       contourWidget->GetEventTranslator()->SetTranslation(
63         vtkCommand::LeftButtonPressEvent, vtkWidgetEvent::Translate);
64     }
65     else if (strcmp("-Scale", argv[i]) == 0)
66     {
67       contourWidget->GetEventTranslator()->RemoveTranslation(vtkCommand::LeftButtonPressEvent);
68       contourWidget->GetEventTranslator()->SetTranslation(
69         vtkCommand::LeftButtonPressEvent, vtkWidgetEvent::Scale);
70     }
71   }
72 
73   vtkSmartPointer<vtkPolyData> pd = vtkSmartPointer<vtkPolyData>::New();
74 
75   vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
76   vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
77   vtkIdType* lineIndices = new vtkIdType[21];
78   for (int i = 0; i < 20; i++)
79   {
80     const double angle = 2.0 * vtkMath::Pi() * i / 20.0;
81     points->InsertPoint(static_cast<vtkIdType>(i), 0.1 * cos(angle), 0.1 * sin(angle), 0.0);
82     lineIndices[i] = static_cast<vtkIdType>(i);
83   }
84 
85   lineIndices[20] = 0;
86   lines->InsertNextCell(21, lineIndices);
87   delete[] lineIndices;
88   pd->SetPoints(points);
89   pd->SetLines(lines);
90 
91   contourWidget->Initialize(pd);
92   contourWidget->Render();
93   ren1->ResetCamera();
94   renWin->Render();
95 
96   iren->Initialize();
97   iren->Start();
98 
99   return EXIT_SUCCESS;
100 }
101