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 "vtkContourWidget.h"
20 #include "vtkOrientedGlyphContourRepresentation.h"
21 #include "vtkRenderer.h"
22 #include "vtkRenderWindow.h"
23 #include "vtkRenderWindowInteractor.h"
24 #include "vtkCommand.h"
25 #include "vtkTestUtilities.h"
26 #include "vtkCamera.h"
27 #include "vtkPlane.h"
28 #include "vtkPolyData.h"
29 #include "vtkCellArray.h"
30 #include "vtkPoints.h"
31 #include "vtkMath.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 =
40 vtkSmartPointer<vtkRenderer>::New();
41 vtkSmartPointer<vtkRenderWindow> renWin =
42 vtkSmartPointer<vtkRenderWindow>::New();
43 renWin->AddRenderer(ren1);
44
45 vtkSmartPointer<vtkRenderWindowInteractor> iren =
46 vtkSmartPointer<vtkRenderWindowInteractor>::New();
47 iren->SetRenderWindow(renWin);
48
49 ren1->SetBackground(0.1, 0.2, 0.4);
50 renWin->SetSize(600, 600);
51
52 vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
53 vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
54 vtkSmartPointer<vtkContourWidget> contourWidget =
55 vtkSmartPointer<vtkContourWidget>::New();
56 contourWidget->SetInteractor(iren);
57 contourWidget->SetRepresentation(contourRep);
58 contourWidget->On();
59
60 for (int i = 0; i < argc; i++)
61 {
62 if (strcmp("-Shift", argv[i]) == 0)
63 {
64 contourWidget->GetEventTranslator()->RemoveTranslation(
65 vtkCommand::LeftButtonPressEvent );
66 contourWidget->GetEventTranslator()->SetTranslation(
67 vtkCommand::LeftButtonPressEvent,
68 vtkWidgetEvent::Translate );
69 }
70 else if (strcmp("-Scale", argv[i]) == 0)
71 {
72 contourWidget->GetEventTranslator()->RemoveTranslation(
73 vtkCommand::LeftButtonPressEvent );
74 contourWidget->GetEventTranslator()->SetTranslation(
75 vtkCommand::LeftButtonPressEvent,
76 vtkWidgetEvent::Scale );
77 }
78 }
79
80
81 vtkSmartPointer<vtkPolyData> pd =
82 vtkSmartPointer<vtkPolyData>::New();
83
84 vtkSmartPointer<vtkPoints> points =
85 vtkSmartPointer<vtkPoints>::New();
86 vtkSmartPointer<vtkCellArray> lines =
87 vtkSmartPointer<vtkCellArray>::New();
88 vtkIdType *lineIndices = new vtkIdType[21];
89 for (int i = 0; i< 20; i++)
90 {
91 const double angle = 2.0*vtkMath::Pi()*i/20.0;
92 points->InsertPoint(static_cast<vtkIdType>(i), 0.1*cos(angle),
93 0.1*sin(angle), 0.0 );
94 lineIndices[i] = static_cast<vtkIdType>(i);
95 }
96
97 lineIndices[20] = 0;
98 lines->InsertNextCell(21,lineIndices);
99 delete [] lineIndices;
100 pd->SetPoints(points);
101 pd->SetLines(lines);
102
103 contourWidget->Initialize(pd);
104 contourWidget->Render();
105 ren1->ResetCamera();
106 renWin->Render();
107
108 iren->Initialize();
109 iren->Start();
110
111 return EXIT_SUCCESS;
112 }
113
114
115