1 #include "VTKViewer_Actor.h"
6 #include <vtkRenderer.h>
7 #include <vtkRenderWindow.h>
8 #include "VTKViewer_Utilities.h"
11 //****************************************************************
12 //see vtkRenderer::ResetCamera(float bounds[6]) method
13 void ResetCamera(vtkRenderer* theRenderer, int theUsingZeroFocalPoint)
15 if(!theRenderer) return;
17 int aCount = ComputeVisiblePropBounds(theRenderer,bounds);
18 if(theUsingZeroFocalPoint || aCount){
19 float aLength = bounds[1]-bounds[0];
20 aLength = max((bounds[3]-bounds[2]),aLength);
21 aLength = max((bounds[5]-bounds[4]),aLength);
24 if ( theRenderer->GetActiveCamera() != NULL )
25 theRenderer->GetActiveCamera()->GetViewPlaneNormal(vn);
30 float center[3] = {0.0, 0.0, 0.0};
31 if(!theUsingZeroFocalPoint){
32 center[0] = (bounds[0] + bounds[1])/2.0;
33 center[1] = (bounds[2] + bounds[3])/2.0;
34 center[2] = (bounds[4] + bounds[5])/2.0;
36 theRenderer->GetActiveCamera()->SetFocalPoint(center[0],center[1],center[2]);
38 float width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
39 (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
40 (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
42 double ang = theRenderer->GetActiveCamera()->GetViewAngle();
43 float distance = 2.0*width/tan(ang*vtkMath::Pi()/360.0);
45 // check view-up vector against view plane normal
46 double *vup = theRenderer->GetActiveCamera()->GetViewUp();
47 if ( fabs(vtkMath::Dot(vup,vn)) > 0.999 ){
48 theRenderer->GetActiveCamera()->SetViewUp(-vup[2], vup[0], vup[1]);
52 theRenderer->GetActiveCamera()->SetPosition(center[0]+distance*vn[0],
53 center[1]+distance*vn[1],
54 center[2]+distance*vn[2]);
55 // find size of the window
56 int* winsize = theRenderer->GetSize();
57 if(winsize[0] < winsize[1]) width *= float(winsize[1])/float(winsize[0]);
59 if(theUsingZeroFocalPoint) width *= sqrt(2.0);
61 theRenderer->GetActiveCamera()->SetParallelScale(width/2.0);
64 //theRenderer->ResetCameraClippingRange(bounds);
65 ResetCameraClippingRange(theRenderer);
68 //****************************************************************
69 // Compute the bounds of the visible props
70 int ComputeVisiblePropBounds(vtkRenderer* theRenderer, float theBounds[6])
75 theBounds[0] = theBounds[2] = theBounds[4] = VTK_LARGE_FLOAT;
76 theBounds[1] = theBounds[3] = theBounds[5] = -VTK_LARGE_FLOAT;
78 // loop through all props
79 vtkActorCollection* aCollection = theRenderer->GetActors();
80 aCollection->InitTraversal();
81 while (vtkActor* prop = aCollection->GetNextActor()) {
82 // if it's invisible, or has no geometry, we can skip the rest
83 if ( prop->GetVisibility() )
85 if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(prop))
86 if(anActor->IsInfinitive()) continue;
87 bounds = prop->GetBounds();
88 // make sure we haven't got bogus bounds
89 if ( bounds != NULL &&
90 bounds[0] > -VTK_LARGE_FLOAT && bounds[1] < VTK_LARGE_FLOAT &&
91 bounds[2] > -VTK_LARGE_FLOAT && bounds[3] < VTK_LARGE_FLOAT &&
92 bounds[4] > -VTK_LARGE_FLOAT && bounds[5] < VTK_LARGE_FLOAT )
96 if (bounds[0] < theBounds[0])
98 theBounds[0] = bounds[0];
100 if (bounds[1] > theBounds[1])
102 theBounds[1] = bounds[1];
104 if (bounds[2] < theBounds[2])
106 theBounds[2] = bounds[2];
108 if (bounds[3] > theBounds[3])
110 theBounds[3] = bounds[3];
112 if (bounds[4] < theBounds[4])
114 theBounds[4] = bounds[4];
116 if (bounds[5] > theBounds[5])
118 theBounds[5] = bounds[5];
126 //****************************************************************
127 //see vtkRenderer::ResetCameraClippingRange(float bounds[6]) method
128 void ResetCameraClippingRange(vtkRenderer* theRenderer)
130 if(!theRenderer || !theRenderer->VisibleActorCount()) return;
132 vtkCamera* anActiveCamera = theRenderer->GetActiveCamera();
133 if( anActiveCamera == NULL ){
137 // Find the plane equation for the camera view plane
139 anActiveCamera->GetViewPlaneNormal(vn);
141 anActiveCamera->GetPosition(position);
144 theRenderer->ComputeVisiblePropBounds(bounds);
147 center[0] = (bounds[0] + bounds[1])/2.0;
148 center[1] = (bounds[2] + bounds[3])/2.0;
149 center[2] = (bounds[4] + bounds[5])/2.0;
151 double width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
152 (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
153 (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
155 double distance = sqrt((position[0]-center[0])*(position[0]-center[0]) +
156 (position[1]-center[1])*(position[1]-center[1]) +
157 (position[2]-center[2])*(position[2]-center[2]));
159 float range[2] = {distance - width/2.0, distance + width/2.0};
161 // Do not let the range behind the camera throw off the calculation.
162 if (range[0] < 0.0) range[0] = 0.0;
164 anActiveCamera->SetClippingRange( range );