Cameron Lowell Palmer
Cameron Lowell Palmer

Reputation: 22245

VTK Position Camera to Fill Viewport with Object

I have an image plane that receives streaming video from the camera. I'm trying to place that image in front of the camera such that it fills the screen. However, I'm having trouble calculating the distance the camera needs to be from the image.

auto camera = this->renderer()->camera();
double *origin = imageData->GetOrigin();
int *extent = imageData->GetExtent();
double *spacing = imageData->GetSpacing();
double viewportHeight = this->renderer()->camera()->viewport()->height();

this->Internal->cameraImagePlaneRepresentation()->setImageData(imageData);

double imageCenterX = origin[0] + 0.5f * (extent[0] + extent[1]) * spacing[0];
double imageCenterY = origin[1] + 0.5f * (extent[2] + extent[3]) * spacing[1];
double imageWidth = (extent[1] - extent[0] + 1.f) * spacing[0];
double imageHeight = (extent[3] - extent[2] + 1.f) * spacing[1];

double viewAngleRadians = degreesToRadians(camera->viewAngle());
double hypotenuse = imageHeight / viewAngleRadians;
double distance = tan(viewAngleRadians * 0.5f) * hypotenuse;

camera->setFocalPoint(vesVector3f(imageCenterX, imageCenterY, 0.f));
camera->setPosition(vesVector3f(imageCenterX, imageCenterY, distance));

I would think the distance would be correct, but it is off by an order of magnitude.

Upvotes: 2

Views: 2438

Answers (1)

Cameron Lowell Palmer
Cameron Lowell Palmer

Reputation: 22245

It greatly helps if you get the formula right for calculating distance.

double viewAngleRadians = degreesToRadians(camera->viewAngle());
double distance = imageHeight / viewAngleRadians;

Upvotes: 2

Related Questions