WIP: Improved support for parallel projection. #168

Near plane negative to show everything
ZoomAlongRay now works for parallel projection (only implemented in
ceetron Plus)
This commit is contained in:
Jacob Støren 2015-12-13 20:55:37 +01:00 committed by Jacob Støren
parent 23bfb46c4c
commit 5cdf307d4e
5 changed files with 86 additions and 8 deletions

View File

@ -79,7 +79,7 @@ RimView::RimView(void)
cameraPosition.uiCapability()->setUiHidden(true);
CAF_PDM_InitField(&isPerspectiveView, "PerspectiveProjection", true, "Perspective Projection", "", "", "");
isPerspectiveView.uiCapability()->setUiHidden(true); // For now as this is experimental
//isPerspectiveView.uiCapability()->setUiHidden(true); // For now as this is experimental
double defaultScaleFactor = preferences->defaultScaleFactorZ;
CAF_PDM_InitField(&scaleZ, "GridZScale", defaultScaleFactor, "Z Scale", "", "Scales the scene in the Z direction", "");

View File

@ -189,7 +189,10 @@ bool caf::CeetronPlusNavigation::handleInputEvent(QInputEvent* inputEvent)
initializeRotationCenter();
if (m_isRotCenterInitialized)
{
QWheelEvent* we = static_cast<QWheelEvent*> ( inputEvent);
#if 0
int translatedMousePosX = we->x();
int translatedMousePosY = m_viewer->height() - we->y();
int delta = we->delta();
@ -199,8 +202,13 @@ bool caf::CeetronPlusNavigation::handleInputEvent(QInputEvent* inputEvent)
ray = m_viewer->mainCamera()->rayFromWindowCoordinates(translatedMousePosX, translatedMousePosY);
else
ray = m_viewer->mainCamera()->rayFromWindowCoordinates((int)(1.0*translatedMousePosX), (int)(1.0*translatedMousePosY));
#endif
zoomAlongRay(ray.p(), delta);
int cvfEvPosX, cvfEvPosY;
cvfEventPos(we->x(), we->y(), &cvfEvPosX, &cvfEvPosY);
cvf::ref<cvf::Ray> ray = createZoomRay(cvfEvPosX, cvfEvPosY);
zoomAlongRay(ray.p(), we->delta());
}
isEventHandled = true;

View File

@ -162,3 +162,38 @@ void caf::TrackBallBasedNavigation::zoomAlongRay(cvf::Ray* ray, int delta)
m_viewer->navigationPolicyUpdate();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::TrackBallBasedNavigation::cvfEventPos(int qtX, int qtY, int* cvfX, int* cvfY)
{
*cvfX = qtX;
*cvfY = m_viewer->height() - qtY;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::ref<cvf::Ray> caf::TrackBallBasedNavigation::createZoomRay(int cvfXPos, int cvfYPos)
{
cvf::ref<cvf::Ray> ray;
cvf::Camera* cam = m_viewer->mainCamera();
ray = cam->rayFromWindowCoordinates(cvfXPos, cvfYPos);
if (cam->projection() == cvf::Camera::ORTHO)
{
cvf::Vec3d camDir = cam->direction();
cvf::Plane focusPlane;
focusPlane.setFromPointAndNormal(m_pointOfInterest, -camDir);
cvf::Vec3d intersectionPoint;
ray->planeIntersect(focusPlane, &intersectionPoint);
cvf::ref<cvf::Ray> orthoZoomRay = new cvf::Ray();
orthoZoomRay->setOrigin(cam->position());
orthoZoomRay->setDirection((intersectionPoint - cam->position()).getNormalized());
ray = orthoZoomRay;
}
return ray;
}

View File

@ -77,7 +77,10 @@ protected:
bool m_isNavigating;
bool m_hasMovedMouseDuringNavigation;
void cvfEventPos(int qtX, int qtY, int* x, int* y);
// Zooming towards cursor
cvf::ref<cvf::Ray> createZoomRay(int cvfXPos, int cvfYPos);
void zoomAlongRay( cvf::Ray* ray, int delta );
bool m_isZooming;
cvf::ref<cvf::Ray> m_zoomRay;

View File

@ -265,16 +265,45 @@ void caf::Viewer::optimizeClippingPlanes()
cvf::BoundingBox bb = m_renderingSequence->boundingBox();
if (!bb.isValid()) return;
cvf::Vec3d eye, vrp, up;
m_mainCamera->toLookAt(&eye, &vrp, &up);
cvf::Vec3d eye = m_mainCamera->position();
cvf::Vec3d viewdir = m_mainCamera->direction();
cvf::Vec3d viewdir = (vrp - eye).getNormalized();
cvf::Vec3d bboxCorners[8];
bb.cornerVertices(bboxCorners);
double maxDistEyeToCornerAlongViewDir = -HUGE_VAL;
double minDistEyeToCornerAlongViewDir = HUGE_VAL;
for (int bcIdx = 0; bcIdx < 8; ++bcIdx )
{
double distEyeBoxCornerAlongViewDir = (bboxCorners[bcIdx] - eye)*viewdir;
if (distEyeBoxCornerAlongViewDir > maxDistEyeToCornerAlongViewDir)
maxDistEyeToCornerAlongViewDir = distEyeBoxCornerAlongViewDir;
if (distEyeBoxCornerAlongViewDir < minDistEyeToCornerAlongViewDir)
minDistEyeToCornerAlongViewDir = distEyeBoxCornerAlongViewDir;
}
double farPlaneDist = CVF_MIN(maxDistEyeToCornerAlongViewDir, m_maxFarPlaneDistance);
double nearPlaneDist = minDistEyeToCornerAlongViewDir;
bool isOrthoNearPlaneFollowingCamera = false;
if (m_mainCamera->projection() == cvf::Camera::PERSPECTIVE || isOrthoNearPlaneFollowingCamera)
{
if (nearPlaneDist < m_minNearPlaneDistance) nearPlaneDist = m_minNearPlaneDistance;
if (m_navigationPolicy.notNull() && m_navigationPolicyEnabled)
{
double pointOfInterestDist = (eye - m_navigationPolicy->pointOfInterest()).length();
nearPlaneDist = CVF_MIN(nearPlaneDist, pointOfInterestDist*0.2);
}
}
#if 0
double distEyeBoxCenterAlongViewDir = (bb.center() - eye)*viewdir;
double farPlaneDist = distEyeBoxCenterAlongViewDir + bb.radius() * 1.2;
farPlaneDist = CVF_MIN(farPlaneDist, m_maxFarPlaneDistance);
double nearPlaneDist = distEyeBoxCenterAlongViewDir - bb.radius();
if (nearPlaneDist < m_minNearPlaneDistance) nearPlaneDist = m_minNearPlaneDistance;
if (m_navigationPolicy.notNull() && m_navigationPolicyEnabled)
@ -282,6 +311,7 @@ void caf::Viewer::optimizeClippingPlanes()
double pointOfInterestDist = (eye - m_navigationPolicy->pointOfInterest()).length();
nearPlaneDist = CVF_MIN(nearPlaneDist, pointOfInterestDist*0.2);
}
#endif
if (farPlaneDist <= nearPlaneDist) farPlaneDist = nearPlaneDist + 1.0;
@ -954,7 +984,9 @@ void caf::Viewer::enableParallelProjection(bool enableOrtho)
// so we do not need to update the camera position based on orthoHeight and fieldOfView.
// We assume the camera is in a sensible position.
m_mainCamera->setProjectionAsPerspective(m_cameraFieldOfViewYDeg, m_mainCamera->nearPlane(), m_mainCamera->farPlane());
// Set a dummy near plane to be > 0 and < farPlane. These wll be updated by the optimize clipping planes
double dummyNearPlane = m_mainCamera->farPlane() *0.1;
m_mainCamera->setProjectionAsPerspective(m_cameraFieldOfViewYDeg, dummyNearPlane, m_mainCamera->farPlane());
this->update();
}
}
@ -995,7 +1027,7 @@ double distToPlaneOfInterest(const cvf::Camera* camera, const cvf::Vec3d& pointO
Vec3d eyeToFocus = pointOfInterest - eye;
double distToFocusPlane = eyeToFocus*camDir;
return distToFocusPlane;
return cvf::Math::abs(distToFocusPlane);
}
//--------------------------------------------------------------------------------------------------