1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkCameraActor.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 
16 #include "vtkCameraActor.h"
17 
18 #include "vtkActor.h"
19 #include "vtkPolyDataMapper.h"
20 #include "vtkFrustumSource.h"
21 #include "vtkCamera.h"
22 #include "vtkPlanes.h"
23 #include "vtkProperty.h"
24 #include "vtkObjectFactory.h"
25 #include "vtkMath.h"
26 
27 vtkStandardNewMacro(vtkCameraActor);
28 vtkCxxSetObjectMacro(vtkCameraActor, Camera, vtkCamera);
29 
30 // ----------------------------------------------------------------------------
vtkCameraActor()31 vtkCameraActor::vtkCameraActor()
32 {
33   this->Camera=nullptr;
34   this->WidthByHeightRatio=1.0;
35   this->FrustumSource=nullptr;
36   this->FrustumMapper=nullptr;
37   this->FrustumActor=nullptr;
38 }
39 
40 // ----------------------------------------------------------------------------
~vtkCameraActor()41 vtkCameraActor::~vtkCameraActor()
42 {
43   this->SetCamera(nullptr);
44 
45   if(this->FrustumActor!=nullptr)
46   {
47     this->FrustumActor->Delete();
48   }
49 
50    if(this->FrustumMapper!=nullptr)
51    {
52     this->FrustumMapper->Delete();
53    }
54   if(this->FrustumSource!=nullptr)
55   {
56     this->FrustumSource->Delete();
57   }
58 }
59 
60 // ----------------------------------------------------------------------------
61 // Description:
62 // Support the standard render methods.
RenderOpaqueGeometry(vtkViewport * viewport)63 int vtkCameraActor::RenderOpaqueGeometry(vtkViewport *viewport)
64 {
65   this->UpdateViewProps();
66 
67   int result=0;
68   if(this->FrustumActor!=nullptr && this->FrustumActor->GetMapper()!=nullptr)
69   {
70     result=this->FrustumActor->RenderOpaqueGeometry(viewport);
71   }
72   return result;
73 }
74 
75 // ----------------------------------------------------------------------------
76 // Description:
77 // Does this prop have some translucent polygonal geometry? No.
HasTranslucentPolygonalGeometry()78 vtkTypeBool vtkCameraActor::HasTranslucentPolygonalGeometry()
79 {
80   return false;
81 }
82 
83 
84 //-----------------------------------------------------------------------------
ReleaseGraphicsResources(vtkWindow * window)85 void vtkCameraActor::ReleaseGraphicsResources(vtkWindow *window)
86 {
87   if(this->FrustumActor!=nullptr)
88   {
89     this->FrustumActor->ReleaseGraphicsResources(window);
90   }
91 }
92 
93 //-------------------------------------------------------------------------
94 // Get the bounds for this Actor as (Xmin,Xmax,Ymin,Ymax,Zmin,Zmax).
GetBounds()95 double *vtkCameraActor::GetBounds()
96 {
97   // we cannot initialize the Bounds the same way vtkBoundingBox does because
98   // vtkProp3D::GetLength() does not check if the Bounds are initialized or
99   // not and makes a call to sqrt(). This call to sqrt with invalid values
100   // would raise a floating-point overflow exception (notably on BCC).
101   // As vtkMath::UninitializeBounds initialized finite unvalid bounds, it
102   // passes silently and GetLength() returns 0.
103   vtkMath::UninitializeBounds(this->Bounds);
104 
105   this->UpdateViewProps();
106   if(this->FrustumActor!=nullptr && this->FrustumActor->GetUseBounds())
107   {
108     this->FrustumActor->GetBounds(this->Bounds);
109   }
110   return this->Bounds;
111 }
112 
113 //-------------------------------------------------------------------------
GetMTime()114 vtkMTimeType vtkCameraActor::GetMTime()
115 {
116   vtkMTimeType mTime=this->Superclass::GetMTime();
117   if(this->Camera!=nullptr)
118   {
119     vtkMTimeType time;
120     time = this->Camera->GetMTime();
121     if(time>mTime)
122     {
123       mTime=time;
124     }
125   }
126   return mTime;
127 }
128 
129 // ----------------------------------------------------------------------------
130 // Description:
131 // Get property of the internal actor.
GetProperty()132 vtkProperty *vtkCameraActor::GetProperty()
133 {
134   if(this->FrustumActor==nullptr)
135   {
136     this->FrustumActor=vtkActor::New();
137   }
138 
139   return this->FrustumActor->GetProperty();
140 }
141 
142 // ----------------------------------------------------------------------------
143 // Description:
144 // Set property of the internal actor.
SetProperty(vtkProperty * p)145 void vtkCameraActor::SetProperty(vtkProperty *p)
146 {
147   if(this->FrustumActor==nullptr)
148   {
149     this->FrustumActor=vtkActor::New();
150   }
151 
152   this->FrustumActor->SetProperty(p);
153 }
154 
155 // ----------------------------------------------------------------------------
UpdateViewProps()156 void vtkCameraActor::UpdateViewProps()
157 {
158   if(this->Camera==nullptr)
159   {
160     vtkDebugMacro(<< "no camera to represent.");
161     return;
162   }
163 
164   vtkPlanes *planes=nullptr;
165   if(this->FrustumSource==nullptr)
166   {
167     this->FrustumSource=vtkFrustumSource::New();
168     planes=vtkPlanes::New();
169     this->FrustumSource->SetPlanes(planes);
170     planes->Delete();
171   }
172   else
173   {
174     planes=this->FrustumSource->GetPlanes();
175   }
176 
177   double coefs[24];
178   this->Camera->GetFrustumPlanes(this->WidthByHeightRatio,coefs);
179   planes->SetFrustumPlanes(coefs);
180 
181   this->FrustumSource->SetShowLines(false);
182 
183   if(this->FrustumMapper==nullptr)
184   {
185     this->FrustumMapper=vtkPolyDataMapper::New();
186   }
187 
188   this->FrustumMapper->SetInputConnection(
189     this->FrustumSource->GetOutputPort());
190 
191   if(this->FrustumActor==nullptr)
192   {
193     this->FrustumActor=vtkActor::New();
194   }
195 
196   this->FrustumActor->SetMapper(this->FrustumMapper);
197 
198   vtkProperty *p=this->FrustumActor->GetProperty();
199   p->SetRepresentationToWireframe();
200   this->FrustumActor->SetVisibility(1);
201 }
202 
203 //-------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)204 void vtkCameraActor::PrintSelf(ostream& os, vtkIndent indent)
205 {
206   this->Superclass::PrintSelf(os,indent);
207 
208   os << indent << "Camera: ";
209   if(this->Camera==nullptr)
210   {
211     os << "(none)" << endl;
212   }
213   else
214   {
215     this->Camera->PrintSelf(os,indent);
216   }
217 
218 
219   os << indent << "WidthByHeightRatio: " << this->WidthByHeightRatio <<  endl;
220 }
221