1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include "VTKViewer_Actor.h"
23 #include <vtkCamera.h>
24 #include <vtkRenderer.h>
25 #include <vtkRenderWindow.h>
26 #include "VTKViewer_Utilities.h"
29 /*!@see vtkRenderer::ResetCamera(float bounds[6]) method*/
30 void ResetCamera(vtkRenderer* theRenderer, int theUsingZeroFocalPoint)
35 vtkCamera* aCamera = theRenderer->GetActiveCamera();
40 int aCount = ComputeVisiblePropBounds(theRenderer,aBounds);
42 if(theUsingZeroFocalPoint || aCount){
43 static float MIN_DISTANCE = 1.0 / VTK_LARGE_FLOAT;
45 float aLength = aBounds[1]-aBounds[0];
46 aLength = max((aBounds[3]-aBounds[2]),aLength);
47 aLength = max((aBounds[5]-aBounds[4]),aLength);
49 if(aLength < MIN_DISTANCE)
53 sqrt((aBounds[1]-aBounds[0])*(aBounds[1]-aBounds[0]) +
54 (aBounds[3]-aBounds[2])*(aBounds[3]-aBounds[2]) +
55 (aBounds[5]-aBounds[4])*(aBounds[5]-aBounds[4]));
57 if(aWidth < MIN_DISTANCE)
60 double aViewPlaneNormal[3];
61 aCamera->GetViewPlaneNormal(aViewPlaneNormal);
63 float aCenter[3] = {0.0, 0.0, 0.0};
64 if(!theUsingZeroFocalPoint){
65 aCenter[0] = (aBounds[0] + aBounds[1])/2.0;
66 aCenter[1] = (aBounds[2] + aBounds[3])/2.0;
67 aCenter[2] = (aBounds[4] + aBounds[5])/2.0;
69 aCamera->SetFocalPoint(aCenter[0],aCenter[1],aCenter[2]);
71 double aViewAngle = aCamera->GetViewAngle();
72 float aDistance = 2.0*aWidth/tan(aViewAngle*vtkMath::Pi()/360.0);
74 // check view-up vector against view plane normal
76 aCamera->GetViewUp(aViewUp);
77 if(fabs(vtkMath::Dot(aViewUp,aViewPlaneNormal)) > 0.999)
78 aCamera->SetViewUp(-aViewUp[2], aViewUp[0], aViewUp[1]);
81 aCamera->SetPosition(aCenter[0]+aDistance*aViewPlaneNormal[0],
82 aCenter[1]+aDistance*aViewPlaneNormal[1],
83 aCenter[2]+aDistance*aViewPlaneNormal[2]);
85 // find size of the window
86 int* aWinSize = theRenderer->GetSize();
87 if(aWinSize[0] < aWinSize[1])
88 aWidth *= float(aWinSize[1])/float(aWinSize[0]);
90 if(theUsingZeroFocalPoint)
93 aCamera->SetParallelScale(aWidth/2.0);
96 ResetCameraClippingRange(theRenderer);
99 /*! Compute the bounds of the visible props*/
100 int ComputeVisiblePropBounds(vtkRenderer* theRenderer, float theBounds[6])
105 theBounds[0] = theBounds[2] = theBounds[4] = VTK_LARGE_FLOAT;
106 theBounds[1] = theBounds[3] = theBounds[5] = -VTK_LARGE_FLOAT;
108 // loop through all props
109 vtkActorCollection* aCollection = theRenderer->GetActors();
110 aCollection->InitTraversal();
111 while (vtkActor* prop = aCollection->GetNextActor()) {
112 // if it's invisible, or has no geometry, we can skip the rest
113 if ( prop->GetVisibility() )
115 if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(prop))
116 if(anActor->IsInfinitive()) continue;
117 bounds = prop->GetBounds();
118 // make sure we haven't got bogus bounds
119 if ( bounds != NULL &&
120 bounds[0] > -VTK_LARGE_FLOAT && bounds[1] < VTK_LARGE_FLOAT &&
121 bounds[2] > -VTK_LARGE_FLOAT && bounds[3] < VTK_LARGE_FLOAT &&
122 bounds[4] > -VTK_LARGE_FLOAT && bounds[5] < VTK_LARGE_FLOAT )
126 if (bounds[0] < theBounds[0])
128 theBounds[0] = bounds[0];
130 if (bounds[1] > theBounds[1])
132 theBounds[1] = bounds[1];
134 if (bounds[2] < theBounds[2])
136 theBounds[2] = bounds[2];
138 if (bounds[3] > theBounds[3])
140 theBounds[3] = bounds[3];
142 if (bounds[4] < theBounds[4])
144 theBounds[4] = bounds[4];
146 if (bounds[5] > theBounds[5])
148 theBounds[5] = bounds[5];
156 /*!@see vtkRenderer::ResetCameraClippingRange(float bounds[6]) method*/
157 void ResetCameraClippingRange(vtkRenderer* theRenderer)
159 if(!theRenderer || !theRenderer->VisibleActorCount()) return;
161 vtkCamera* anActiveCamera = theRenderer->GetActiveCamera();
162 if( anActiveCamera == NULL ){
166 // Find the plane equation for the camera view plane
168 anActiveCamera->GetViewPlaneNormal(vn);
170 anActiveCamera->GetPosition(position);
173 theRenderer->ComputeVisiblePropBounds(bounds);
176 center[0] = (bounds[0] + bounds[1])/2.0;
177 center[1] = (bounds[2] + bounds[3])/2.0;
178 center[2] = (bounds[4] + bounds[5])/2.0;
180 double width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
181 (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
182 (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
184 double distance = sqrt((position[0]-center[0])*(position[0]-center[0]) +
185 (position[1]-center[1])*(position[1]-center[1]) +
186 (position[2]-center[2])*(position[2]-center[2]));
188 float range[2] = {distance - width/2.0, distance + width/2.0};
190 // Do not let the range behind the camera throw off the calculation.
191 if (range[0] < 0.0) range[0] = 0.0;
193 anActiveCamera->SetClippingRange( range );
196 /*!Compute trihedron size.*/
197 bool ComputeTrihedronSize( vtkRenderer* theRenderer,double& theNewSize,
198 const double theSize, const float theSizeInPercents )
200 // calculating diagonal of visible props of the renderer
202 if ( ComputeVisiblePropBounds( theRenderer, bnd ) == 0 )
204 bnd[ 1 ] = bnd[ 3 ] = bnd[ 5 ] = 100;
205 bnd[ 0 ] = bnd[ 2 ] = bnd[ 4 ] = 0;
209 aLength = bnd[ 1 ]-bnd[ 0 ];
210 aLength = max( ( bnd[ 3 ] - bnd[ 2 ] ),aLength );
211 aLength = max( ( bnd[ 5 ] - bnd[ 4 ] ),aLength );
213 static float EPS_SIZE = 5.0E-3;
214 theNewSize = aLength * theSizeInPercents / 100.0;
216 // if the new trihedron size have sufficient difference, then apply the value
217 return fabs( theNewSize - theSize) > theSize * EPS_SIZE ||
218 fabs( theNewSize-theSize ) > theNewSize * EPS_SIZE;