1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkCameraRepresentation.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 "vtkCameraRepresentation.h"
16 #include "vtkCameraInterpolator.h"
17 #include "vtkCallbackCommand.h"
18 #include "vtkObjectFactory.h"
19 #include "vtkRenderer.h"
20 #include "vtkRenderWindow.h"
21 #include "vtkRenderWindowInteractor.h"
22 #include "vtkCamera.h"
23 #include "vtkPoints.h"
24 #include "vtkCellArray.h"
25 #include "vtkPolyData.h"
26 #include "vtkPolyDataMapper2D.h"
27 #include "vtkProperty2D.h"
28 #include "vtkActor2D.h"
29 #include "vtkTransform.h"
30 #include "vtkTransformPolyDataFilter.h"
31 
32 
33 vtkStandardNewMacro(vtkCameraRepresentation);
34 
35 vtkCxxSetObjectMacro(vtkCameraRepresentation, Camera, vtkCamera);
36 vtkCxxSetObjectMacro(vtkCameraRepresentation, Interpolator, vtkCameraInterpolator);
37 
38 //-------------------------------------------------------------------------
vtkCameraRepresentation()39 vtkCameraRepresentation::vtkCameraRepresentation()
40 {
41   this->Camera = nullptr;
42   this->Interpolator = vtkCameraInterpolator::New();
43   this->NumberOfFrames = 24;
44 
45   // Set up the
46   double size[2];
47   this->GetSize(size);
48   this->Position2Coordinate->SetValue(0.04*size[0], 0.04*size[1]);
49   this->ProportionalResize = 1;
50   this->Moving = 1;
51   this->SetShowBorder(vtkBorderRepresentation::BORDER_ON);
52 
53   // Create the geometry in canonical coordinates
54   this->Points = vtkPoints::New();
55   this->Points->SetDataTypeToDouble();
56   this->Points->SetNumberOfPoints(25);
57   this->Points->SetPoint(0, 0.0, 0.0, 0.0);
58   this->Points->SetPoint(1, 6.0, 0.0, 0.0);
59   this->Points->SetPoint(2, 6.0, 2.0, 0.0);
60   this->Points->SetPoint(3, 0.0, 2.0, 0.0);
61   this->Points->SetPoint(4, 0.375, 0.25, 0.0);
62   this->Points->SetPoint(5, 1.0, 0.25, 0.0);
63   this->Points->SetPoint(6, 1.0, 1.75, 0.0);
64   this->Points->SetPoint(7, 0.375, 1.75, 0.0);
65   this->Points->SetPoint(8, 1.0, 0.875, 0.0);
66   this->Points->SetPoint(9, 1.25, 0.75, 0.0);
67   this->Points->SetPoint(10, 1.5, 0.75, 0.0);
68   this->Points->SetPoint(11, 1.5, 1.25, 0.0);
69   this->Points->SetPoint(12, 1.25, 1.25, 0.0);
70   this->Points->SetPoint(13, 1.0, 1.125, 0.0);
71   this->Points->SetPoint(14, 2.5, 0.5, 0.0);
72   this->Points->SetPoint(15, 3.5, 1.0, 0.0);
73   this->Points->SetPoint(16, 2.5, 1.5, 0.0);
74   this->Points->SetPoint(17, 4.625, 0.375, 0.0);
75   this->Points->SetPoint(18, 5.625, 0.375, 0.0);
76   this->Points->SetPoint(19, 5.75, 0.5, 0.0);
77   this->Points->SetPoint(20, 5.75, 1.5, 0.0);
78   this->Points->SetPoint(21, 5.625, 1.625, 0.0);
79   this->Points->SetPoint(22, 4.625, 1.625, 0.0);
80   this->Points->SetPoint(23, 4.5, 1.5, 0.0);
81   this->Points->SetPoint(24, 4.5, 0.5, 0.0);
82 
83   vtkCellArray *cells = vtkCellArray::New();
84   cells->InsertNextCell(4); //camera body
85   cells->InsertCellPoint(4);
86   cells->InsertCellPoint(5);
87   cells->InsertCellPoint(6);
88   cells->InsertCellPoint(7);
89   cells->InsertNextCell(6); //camera lens
90   cells->InsertCellPoint(8);
91   cells->InsertCellPoint(9);
92   cells->InsertCellPoint(10);
93   cells->InsertCellPoint(11);
94   cells->InsertCellPoint(12);
95   cells->InsertCellPoint(13);
96   cells->InsertNextCell(3); //play button
97   cells->InsertCellPoint(14);
98   cells->InsertCellPoint(15);
99   cells->InsertCellPoint(16);
100   cells->InsertNextCell(4); //part of delete button
101   cells->InsertCellPoint(17);
102   cells->InsertCellPoint(20);
103   cells->InsertCellPoint(21);
104   cells->InsertCellPoint(24);
105   cells->InsertNextCell(4); //part of delete button
106   cells->InsertCellPoint(18);
107   cells->InsertCellPoint(19);
108   cells->InsertCellPoint(22);
109   cells->InsertCellPoint(23);
110   this->PolyData = vtkPolyData::New();
111   this->PolyData->SetPoints(this->Points);
112   this->PolyData->SetPolys(cells);
113   cells->Delete();
114 
115   this->TransformFilter = vtkTransformPolyDataFilter::New();
116   this->TransformFilter->SetTransform(this->BWTransform);
117   this->TransformFilter->SetInputData(this->PolyData);
118 
119   this->Mapper = vtkPolyDataMapper2D::New();
120   this->Mapper->SetInputConnection(
121     this->TransformFilter->GetOutputPort());
122   this->Property = vtkProperty2D::New();
123   this->Actor = vtkActor2D::New();
124   this->Actor->SetMapper(this->Mapper);
125   this->Actor->SetProperty(this->Property);
126 }
127 
128 //-------------------------------------------------------------------------
~vtkCameraRepresentation()129 vtkCameraRepresentation::~vtkCameraRepresentation()
130 {
131   this->SetCamera(nullptr);
132   this->SetInterpolator(nullptr);
133 
134   this->Points->Delete();
135   this->TransformFilter->Delete();
136   this->PolyData->Delete();
137   this->Mapper->Delete();
138   this->Property->Delete();
139   this->Actor->Delete();
140 }
141 
142 //-------------------------------------------------------------------------
BuildRepresentation()143 void vtkCameraRepresentation::BuildRepresentation()
144 {
145   // Note that the transform is updated by the superclass
146   this->Superclass::BuildRepresentation();
147 }
148 
149 //-------------------------------------------------------------------------
AddCameraToPath()150 void vtkCameraRepresentation::AddCameraToPath()
151 {
152   if ( ! this->Camera )
153   {
154     return;
155   }
156   if ( ! this->Interpolator )
157   {
158     this->Interpolator = vtkCameraInterpolator::New();
159   }
160   this->CurrentTime = static_cast<double>(
161     this->Interpolator->GetNumberOfCameras());
162   this->Interpolator->AddCamera(this->CurrentTime,this->Camera);
163 }
164 
165 
166 //-------------------------------------------------------------------------
AnimatePath(vtkRenderWindowInteractor * rwi)167 void vtkCameraRepresentation::AnimatePath(vtkRenderWindowInteractor *rwi)
168 {
169   vtkCameraInterpolator *camInt = this->Interpolator;
170 
171   if ( ! camInt || ! rwi )
172   {
173     return;
174   }
175 
176   int numCameras = camInt->GetNumberOfCameras();
177   if ( numCameras <= 0 )
178   {
179     return;
180   }
181   double delT = static_cast<double>(numCameras - 1) / this->NumberOfFrames;
182 
183   double t=0.0;
184   for (int i=0; i < this->NumberOfFrames; i++, t+=delT)
185   {
186     camInt->InterpolateCamera(t,this->Camera);
187     rwi->Render();
188   }
189 }
190 
191 //-------------------------------------------------------------------------
InitializePath()192 void vtkCameraRepresentation::InitializePath()
193 {
194   if ( ! this->Interpolator )
195   {
196     return;
197   }
198   this->Interpolator->Initialize();
199   this->CurrentTime = 0.0;
200 }
201 
202 //-------------------------------------------------------------------------
GetActors2D(vtkPropCollection * pc)203 void vtkCameraRepresentation::GetActors2D(vtkPropCollection *pc)
204 {
205   pc->AddItem(this->Actor);
206   this->Superclass::GetActors2D(pc);
207 }
208 
209 //-------------------------------------------------------------------------
ReleaseGraphicsResources(vtkWindow * w)210 void vtkCameraRepresentation::ReleaseGraphicsResources(vtkWindow *w)
211 {
212   this->Actor->ReleaseGraphicsResources(w);
213   this->Superclass::ReleaseGraphicsResources(w);
214 }
215 
216 //-------------------------------------------------------------------------
RenderOverlay(vtkViewport * w)217 int vtkCameraRepresentation::RenderOverlay(vtkViewport *w)
218 {
219   int count = this->Superclass::RenderOverlay(w);
220   count += this->Actor->RenderOverlay(w);
221   return count;
222 }
223 
224 //-------------------------------------------------------------------------
RenderOpaqueGeometry(vtkViewport * w)225 int vtkCameraRepresentation::RenderOpaqueGeometry(vtkViewport *w)
226 {
227   int count = this->Superclass::RenderOpaqueGeometry(w);
228   count += this->Actor->RenderOpaqueGeometry(w);
229   return count;
230 }
231 
232 //-------------------------------------------------------------------------
RenderTranslucentPolygonalGeometry(vtkViewport * w)233 int vtkCameraRepresentation::RenderTranslucentPolygonalGeometry(vtkViewport *w)
234 {
235   int count = this->Superclass::RenderTranslucentPolygonalGeometry(w);
236   count += this->Actor->RenderTranslucentPolygonalGeometry(w);
237   return count;
238 }
239 
240 //-----------------------------------------------------------------------------
241 // Description:
242 // Does this prop have some translucent polygonal geometry?
HasTranslucentPolygonalGeometry()243 vtkTypeBool vtkCameraRepresentation::HasTranslucentPolygonalGeometry()
244 {
245   int result = this->Superclass::HasTranslucentPolygonalGeometry();
246   result |= this->Actor->HasTranslucentPolygonalGeometry();
247   return result;
248 }
249 
250 //-------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)251 void vtkCameraRepresentation::PrintSelf(ostream& os, vtkIndent indent)
252 {
253   this->Superclass::PrintSelf(os,indent);
254 
255   if ( this->Property )
256   {
257     os << indent << "Property:\n";
258     this->Property->PrintSelf(os,indent.GetNextIndent());
259   }
260   else
261   {
262     os << indent << "Property: (none)\n";
263   }
264 
265   os << indent << "Camera Interpolator: " << this->Interpolator << "\n";
266   os << indent << "Camera: " << this->Camera << "\n";
267   os << indent << "Number of Frames: " << this->NumberOfFrames << "\n";
268 
269 }
270