From fb5b853650c4290ab2b4061da3a5c302da3f90b5 Mon Sep 17 00:00:00 2001 From: nds Date: Mon, 13 Apr 2009 11:13:50 +0000 Subject: [PATCH] It corrects ResetCamera() method to improve fit all functionality for long thin objects. --- src/VTKViewer/VTKViewer_Utilities.cxx | 47 ++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/VTKViewer/VTKViewer_Utilities.cxx b/src/VTKViewer/VTKViewer_Utilities.cxx index 09b764236..51f0c0343 100755 --- a/src/VTKViewer/VTKViewer_Utilities.cxx +++ b/src/VTKViewer/VTKViewer_Utilities.cxx @@ -54,12 +54,49 @@ ResetCamera(vtkRenderer* theRenderer, if(aLength < MIN_DISTANCE) return; - - vtkFloatingPointType aWidth = - sqrt((aBounds[1]-aBounds[0])*(aBounds[1]-aBounds[0]) + - (aBounds[3]-aBounds[2])*(aBounds[3]-aBounds[2]) + - (aBounds[5]-aBounds[4])*(aBounds[5]-aBounds[4])); + double aX, anY, aZ; + vtkFloatingPointType aNewBounds[6] = {VTK_LARGE_FLOAT, -VTK_LARGE_FLOAT, + VTK_LARGE_FLOAT, -VTK_LARGE_FLOAT, + VTK_LARGE_FLOAT, -VTK_LARGE_FLOAT }; + + // We calculate a new bounding box that is aligned with the view CS axes. + // The new box should fully include the original box in the view XOY plane. + // Then we calculate the distance from the camera so that the new box is + // not clipped, preserving the current view angle. + // This gives us the required zooming. + // In order to simplify calculation of the distance + // we set the camera's scale to 1 before transforming the box coordinates + // (actually we set it to 0.9 to have some empty space around the 3D model + // after this operation). Otherwise we would have to take the current scale + // into account when calculating the new camera position. + aCamera->SetParallelScale( 0.9 ); + + for ( int i = 0; i < 2; i++ ) { + for ( int j = 2; j < 4; j++ ) { + for ( int k = 4; k < 6; k++ ) { + aX = aBounds[i]; + anY = aBounds[j]; + aZ = aBounds[k]; + + // Transform the original 3D point in the world CS + // into a point in the view CS without any scaling + // (i.e. we only rotate and translate the CS according + // to the current camera orientation). + theRenderer->WorldToView(aX, anY, aZ); + + aNewBounds[i] = i%2 == 0 ? min(aNewBounds[i], aX) + : max(aNewBounds[i], aX); + aNewBounds[j] = j%2 == 0 ? min(aNewBounds[j], anY) + : max(aNewBounds[j], anY); + aNewBounds[k] = k%2 == 0 ? min(aNewBounds[k], aZ) + : max(aNewBounds[k], aZ); + } + } + } + vtkFloatingPointType aWidth = max(aNewBounds[1] - aNewBounds[0], + aNewBounds[3] - aNewBounds[2]); + if(aWidth < MIN_DISTANCE) return; -- 2.39.2