1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "VTKViewer_Utilities.h"
24 #include "VTKViewer_Actor.h"
25 #include "VTKViewer_Algorithm.h"
31 #include <vtkCamera.h>
32 #include <vtkRenderer.h>
33 #include <vtkRenderWindow.h>
35 /*!@see vtkRenderer::ResetCamera(double bounds[6]) method*/
37 ResetCamera(vtkRenderer* theRenderer,
38 int theUsingZeroFocalPoint)
43 vtkCamera* aCamera = theRenderer->GetActiveCamera();
48 int aCount = ComputeVisiblePropBounds(theRenderer,aBounds);
50 if(theUsingZeroFocalPoint || aCount){
51 static double MIN_DISTANCE = 1.0 / VTK_LARGE_FLOAT;
53 double aLength = aBounds[1]-aBounds[0];
54 aLength = std::max((aBounds[3]-aBounds[2]),aLength);
55 aLength = std::max((aBounds[5]-aBounds[4]),aLength);
57 if(aLength < MIN_DISTANCE)
61 sqrt((aBounds[1]-aBounds[0])*(aBounds[1]-aBounds[0]) +
62 (aBounds[3]-aBounds[2])*(aBounds[3]-aBounds[2]) +
63 (aBounds[5]-aBounds[4])*(aBounds[5]-aBounds[4]));
65 if(aWidth < MIN_DISTANCE)
68 double aViewPlaneNormal[3];
69 aCamera->GetViewPlaneNormal(aViewPlaneNormal);
71 double aCenter[3] = {0.0, 0.0, 0.0};
72 if(!theUsingZeroFocalPoint){
73 aCenter[0] = (aBounds[0] + aBounds[1])/2.0;
74 aCenter[1] = (aBounds[2] + aBounds[3])/2.0;
75 aCenter[2] = (aBounds[4] + aBounds[5])/2.0;
77 aCamera->SetFocalPoint(aCenter[0],aCenter[1],aCenter[2]);
79 double aViewAngle = aCamera->GetViewAngle();
80 double aDistance = 2.0*aWidth/tan(aViewAngle*vtkMath::Pi()/360.0);
82 // check view-up vector against view plane normal
84 aCamera->GetViewUp(aViewUp);
85 if(fabs(vtkMath::Dot(aViewUp,aViewPlaneNormal)) > 0.999)
86 aCamera->SetViewUp(-aViewUp[2], aViewUp[0], aViewUp[1]);
89 aCamera->SetPosition(aCenter[0]+aDistance*aViewPlaneNormal[0],
90 aCenter[1]+aDistance*aViewPlaneNormal[1],
91 aCenter[2]+aDistance*aViewPlaneNormal[2]);
93 // find size of the window
94 int* aWinSize = theRenderer->GetSize();
95 if(aWinSize[0] < aWinSize[1])
96 aWidth *= double(aWinSize[1])/double(aWinSize[0]);
98 if(theUsingZeroFocalPoint)
101 aCamera->SetParallelScale(aWidth/2.0);
104 ResetCameraClippingRange(theRenderer);
107 /*! Compute the bounds of the visible props*/
109 ComputeVisiblePropBounds(vtkRenderer* theRenderer,
114 theBounds[0] = theBounds[2] = theBounds[4] = VTK_LARGE_FLOAT;
115 theBounds[1] = theBounds[3] = theBounds[5] = -VTK_LARGE_FLOAT;
117 // loop through all props
118 VTK::ActorCollectionCopy aCopy(theRenderer->GetActors());
119 vtkActorCollection* aCollection = aCopy.GetActors();
120 aCollection->InitTraversal();
121 while (vtkActor* aProp = aCollection->GetNextActor()) {
122 // if it's invisible, or has no geometry, we can skip the rest
123 if(aProp->GetVisibility() && aProp->GetMapper() && vtkMath::AreBoundsInitialized(aProp->GetBounds())){
124 if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(aProp))
125 if(anActor->IsInfinitive())
127 double *aBounds = aProp->GetBounds();
128 static double MIN_DISTANCE = 1./VTK_LARGE_FLOAT;
129 static double MAX_DISTANCE = 0.9*VTK_LARGE_FLOAT;
131 if(abs(aBounds[1] - aBounds[0]) < MIN_DISTANCE) {
136 if(abs(aBounds[3] - aBounds[2]) < MIN_DISTANCE) {
141 if(abs(aBounds[5] - aBounds[4]) < MIN_DISTANCE) {
146 // make sure we haven't got bogus bounds
147 if ( aBounds != NULL &&
148 aBounds[0] > -MAX_DISTANCE && aBounds[1] < MAX_DISTANCE &&
149 aBounds[2] > -MAX_DISTANCE && aBounds[3] < MAX_DISTANCE &&
150 aBounds[4] > -MAX_DISTANCE && aBounds[5] < MAX_DISTANCE)
154 theBounds[0] = std::min(aBounds[0],theBounds[0]);
155 theBounds[2] = std::min(aBounds[2],theBounds[2]);
156 theBounds[4] = std::min(aBounds[4],theBounds[4]);
158 theBounds[1] = std::max(aBounds[1],theBounds[1]);
159 theBounds[3] = std::max(aBounds[3],theBounds[3]);
160 theBounds[5] = std::max(aBounds[5],theBounds[5]);
168 /*!@see vtkRenderer::ResetCameraClippingRange(double bounds[6]) method*/
170 ResetCameraClippingRange(vtkRenderer* theRenderer)
172 if(!theRenderer || !theRenderer->VisibleActorCount()) return;
174 vtkCamera* anActiveCamera = theRenderer->GetActiveCamera();
175 if( anActiveCamera == NULL ){
179 // Find the plane equation for the camera view plane
181 anActiveCamera->GetViewPlaneNormal(vn);
183 anActiveCamera->GetPosition(position);
186 //theRenderer->ComputeVisiblePropBounds(bounds);
187 ComputeVisiblePropBounds(theRenderer, bounds);
190 center[0] = (bounds[0] + bounds[1])/2.0;
191 center[1] = (bounds[2] + bounds[3])/2.0;
192 center[2] = (bounds[4] + bounds[5])/2.0;
194 double width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
195 (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
196 (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
198 double distance = sqrt((position[0]-center[0])*(position[0]-center[0]) +
199 (position[1]-center[1])*(position[1]-center[1]) +
200 (position[2]-center[2])*(position[2]-center[2]));
202 double range[2] = {distance - width/2.0, distance + width/2.0};
204 // Do not let the range behind the camera throw off the calculation.
205 if (range[0] < 0.0) range[0] = 0.0;
207 anActiveCamera->SetClippingRange( range );
210 /*!Compute trihedron size.*/
212 ComputeTrihedronSize( vtkRenderer* theRenderer,
214 const double theSize,
215 const double theSizeInPercents )
217 // calculating diagonal of visible props of the renderer
219 if ( ComputeVisiblePropBounds( theRenderer, bnd ) == 0 )
221 bnd[ 1 ] = bnd[ 3 ] = bnd[ 5 ] = 100;
222 bnd[ 0 ] = bnd[ 2 ] = bnd[ 4 ] = 0;
226 aLength = bnd[ 1 ]-bnd[ 0 ];
227 aLength = std::max( ( bnd[ 3 ] - bnd[ 2 ] ),aLength );
228 aLength = std::max( ( bnd[ 5 ] - bnd[ 4 ] ),aLength );
230 static double EPS_SIZE = 5.0E-3;
231 theNewSize = aLength * theSizeInPercents / 100.0;
233 // if the new trihedron size have sufficient difference, then apply the value
234 return fabs( theNewSize - theSize) > theSize * EPS_SIZE ||
235 fabs( theNewSize-theSize ) > theNewSize * EPS_SIZE;
238 bool IsBBEmpty(vtkRenderer* theRenderer)
243 double aNewBndBox[6];
244 aNewBndBox[ 0 ] = aNewBndBox[ 2 ] = aNewBndBox[ 4 ] = VTK_LARGE_FLOAT;
245 aNewBndBox[ 1 ] = aNewBndBox[ 3 ] = aNewBndBox[ 5 ] = -VTK_LARGE_FLOAT;
247 // iterate through displayed objects and set size if necessary
248 VTK::ActorCollectionCopy aCopy(theRenderer->GetActors());
249 vtkActorCollection* anActors = aCopy.GetActors();
250 anActors->InitTraversal();
252 while(vtkActor* anAct = anActors->GetNextActor())
253 //if(SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(anAct))
254 if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(anAct))
255 if(anActor->GetVisibility() && !anActor->IsInfinitive())
257 double *aBounds = anActor->GetBounds();
258 if(aBounds[0] > -VTK_LARGE_FLOAT && aBounds[1] < VTK_LARGE_FLOAT &&
259 aBounds[2] > -VTK_LARGE_FLOAT && aBounds[3] < VTK_LARGE_FLOAT &&
260 aBounds[4] > -VTK_LARGE_FLOAT && aBounds[5] < VTK_LARGE_FLOAT)
268 Check that the given bounding box is valid, i.e each min bound < each max bound
271 bool isBoundValid(double* theBounds) {
272 if(theBounds[0] > theBounds[1] ||
273 theBounds[2] > theBounds[3] ||
274 theBounds[4] > theBounds[5])
280 bool ComputeBBCenter(vtkRenderer* theRenderer, double theCenter[3])
282 theCenter[0] = theCenter[1] = theCenter[2] = 0.0;
287 double aNewBndBox[6];
288 aNewBndBox[ 0 ] = aNewBndBox[ 2 ] = aNewBndBox[ 4 ] = VTK_LARGE_FLOAT;
289 aNewBndBox[ 1 ] = aNewBndBox[ 3 ] = aNewBndBox[ 5 ] = -VTK_LARGE_FLOAT;
291 // iterate through displayed objects and set size if necessary
292 VTK::ActorCollectionCopy aCopy(theRenderer->GetActors());
293 vtkActorCollection* anActors = aCopy.GetActors();
294 anActors->InitTraversal();
296 while(vtkActor* anAct = anActors->GetNextActor())
298 //if(SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(anAct))
299 if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(anAct))
301 if(anActor->GetVisibility() && !anActor->IsInfinitive())
303 double *aBounds = anActor->GetBounds();
305 //Ignore invalid bounds
306 if(!isBoundValid(aBounds)) continue;
308 if(aBounds[0] > -VTK_LARGE_FLOAT && aBounds[1] < VTK_LARGE_FLOAT &&
309 aBounds[2] > -VTK_LARGE_FLOAT && aBounds[3] < VTK_LARGE_FLOAT &&
310 aBounds[4] > -VTK_LARGE_FLOAT && aBounds[5] < VTK_LARGE_FLOAT)
312 for(int i = 0; i < 5; i = i + 2){
313 if(aBounds[i] < aNewBndBox[i])
314 aNewBndBox[i] = aBounds[i];
315 if(aBounds[i+1] > aNewBndBox[i+1])
316 aNewBndBox[i+1] = aBounds[i+1];
326 // null bounding box => the center is (0,0,0)
330 if(aNewBndBox[0] > -VTK_LARGE_FLOAT && aNewBndBox[1] < VTK_LARGE_FLOAT &&
331 aNewBndBox[2] > -VTK_LARGE_FLOAT && aNewBndBox[3] < VTK_LARGE_FLOAT &&
332 aNewBndBox[4] > -VTK_LARGE_FLOAT && aNewBndBox[5] < VTK_LARGE_FLOAT)
334 static double MIN_DISTANCE = 1.0 / VTK_LARGE_FLOAT;
336 double aLength = aNewBndBox[1]-aNewBndBox[0];
337 aLength = std::max((aNewBndBox[3]-aNewBndBox[2]),aLength);
338 aLength = std::max((aNewBndBox[5]-aNewBndBox[4]),aLength);
340 if(aLength < MIN_DISTANCE)
344 sqrt((aNewBndBox[1]-aNewBndBox[0])*(aNewBndBox[1]-aNewBndBox[0]) +
345 (aNewBndBox[3]-aNewBndBox[2])*(aNewBndBox[3]-aNewBndBox[2]) +
346 (aNewBndBox[5]-aNewBndBox[4])*(aNewBndBox[5]-aNewBndBox[4]));
348 if(aWidth < MIN_DISTANCE)
351 theCenter[0] = (aNewBndBox[0] + aNewBndBox[1])/2.0;
352 theCenter[1] = (aNewBndBox[2] + aNewBndBox[3])/2.0;
353 theCenter[2] = (aNewBndBox[4] + aNewBndBox[5])/2.0;
361 int aCount = ComputeVisiblePropBounds(theRenderer,aBounds);
362 printf("aNewBndBox[0] = %f, aNewBndBox[1] = %f,\naNewBndBox[2] = %f, aNewBndBox[3] = %f,\naNewBndBox[4] = %f, aNewBndBox[5] = %f\n",
363 aBounds[0],aBounds[1],aBounds[2],aBounds[3],aBounds[4],aBounds[5]);
364 printf("aCount = %d\n",aCount);
367 static double MIN_DISTANCE = 1.0 / VTK_LARGE_FLOAT;
369 double aLength = aBounds[1]-aBounds[0];
370 aLength = max((aBounds[3]-aBounds[2]),aLength);
371 aLength = max((aBounds[5]-aBounds[4]),aLength);
373 if(aLength < MIN_DISTANCE)
377 sqrt((aBounds[1]-aBounds[0])*(aBounds[1]-aBounds[0]) +
378 (aBounds[3]-aBounds[2])*(aBounds[3]-aBounds[2]) +
379 (aBounds[5]-aBounds[4])*(aBounds[5]-aBounds[4]));
381 if(aWidth < MIN_DISTANCE)
384 theCenter[0] = (aBounds[0] + aBounds[1])/2.0;
385 theCenter[1] = (aBounds[2] + aBounds[3])/2.0;
386 theCenter[2] = (aBounds[4] + aBounds[5])/2.0;