added backgroundimage transformation

This commit is contained in:
Djairo Hougee 2024-05-06 14:05:59 +02:00
parent 5f6b88b34c
commit 507d0065f0
5 changed files with 69 additions and 52 deletions

View File

@ -62,12 +62,11 @@ void CameraMoveCallback::zoom(const bool in) {
double scale = this->cam->GetParallelScale();
if (in) {
if (scale >= 0.2) {
scale -= 0.1;
}
if (scale >= 0.2)
scale -= 0.1;
} else {
if (scale <= 0.9)
scale += 0.1;
scale += 0.1;
}
this->cam->SetParallelScale(scale);

View File

@ -8,7 +8,7 @@ TimerCallbackCommand::TimerCallbackCommand() : dt(3600), maxTime(3600*24*365), t
TimerCallbackCommand* TimerCallbackCommand::New(Program *program) {
TimerCallbackCommand *cb = new TimerCallbackCommand();
cb->setProgram(program);
cb->setPaused(false);
cb->setPaused(true);
return cb;
}

View File

@ -1,5 +1,7 @@
#include "BackgroundImage.h"
#include <vtkImageDataGeometryFilter.h>
#include <vtkImageChangeInformation.h>
#include <vtkImageSliceMapper.h>
#include <vtkCamera.h>
#include <vtkImageActor.h>
#include <vtkImageData.h>
@ -7,6 +9,7 @@
#include <vtkImageReader2.h>
#include <vtkImageShiftScale.h>
#include <vtkMatrix4x4.h>
#include <vtkPolyDataMapper.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>
@ -19,56 +22,62 @@ BackgroundImage::BackgroundImage(string imagePath) : imagePath(imagePath) {
updateImage();
}
vtkSmartPointer<vtkMatrix4x4> BackgroundImage::getMatrix(const double x0, const double y0, const int xMax, const int yMax) {
double eyeTransform[] = {
2/(xMax-x0), 0, 0, -(xMax+x0)/(xMax-x0),
0, 2/(yMax-y0), 0, -(yMax+y0)/(yMax-y0),
0, 0, 1, 0,
0, 0, 0, 1
};
auto matrix = vtkSmartPointer<vtkMatrix4x4>::New();
matrix->DeepCopy(eyeTransform);
return matrix;
}
void BackgroundImage::updateImage() {
// read image data
vtkSmartPointer<vtkImageReader2> imageReader;
imageReader.TakeReference(this->readerFactory->CreateImageReader2(this->imagePath.c_str()));
imageReader->SetFileName(this->imagePath.c_str());
imageReader->Update();
// translate image such that the middle is at (0,0)
vtkNew<vtkImageChangeInformation> imageCenterer;
imageCenterer->SetInputConnection(imageReader->GetOutputPort());
imageCenterer->CenterImageOn();
imageCenterer->Update();
// get some info from the data we'll need in a second
vtkSmartPointer<vtkImageData> imageData = imageCenterer->GetOutput();
double origin[3];
int extent[6];
imageData->GetOrigin(origin);
imageData->GetExtent(extent);
// TODO: transform the image to the range [-1,1]
// This will allow the backgorundImage to share a camera with our other layers.
// Facilitating the cameraMovement callback.
vtkNew<vtkImageActor> imageActor;
imageActor->SetInputData(imageData);
// map the imageData to a vtkPolydata so we can use a vtkTransform
vtkNew<vtkImageDataGeometryFilter> imageDataGeometryFilter;
imageDataGeometryFilter->SetInputData(imageData);
imageDataGeometryFilter->Update();
this->ren->AddActor(imageActor);
// setup the vtkTransform - this is where use the data from imageData we got earlier
vtkNew<vtkTransform> transform;
transform->SetMatrix(getMatrix(origin[0], origin[1], extent[1]+origin[0], extent[3]+origin[1]));
vtkSmartPointer<vtkTransformFilter> transformFilter = vtkSmartPointer<vtkTransformFilter>::New();
transformFilter->SetTransform(transform);
transformFilter->SetInputConnection(imageDataGeometryFilter->GetOutputPort());
transformFilter->Update();
// Create a mapper and actor
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(transformFilter->GetOutputPort());
// // camera stuff
// // essentially sets the camera to the middle of the background, and points it at the background
// // TODO: extract this to its own function, separate from the background class.
// double origin[3], spacing[3];
// int extent[6];
// imageData->GetOrigin(origin);
// imageData->GetSpacing(spacing);
// imageData->GetExtent(extent);
//
// printf("%lf, %lf, %lf\n", origin[0], origin[1], origin[2]);
// printf("%lf, %lf, %lf\n", spacing[0], spacing[1], spacing[2]);
// printf("%d, %d, %d, ", extent[0], extent[1], extent[2]);
// printf("%d, %d, %d\n", extent[3], extent[4], extent[5]);
//
// vtkCamera *camera = this->ren->GetActiveCamera();
// camera->ParallelProjectionOn();
//
// double xc = origin[0] + 0.5 * (extent[0] + extent[1]) * spacing[0];
// double yc = origin[1] + 0.5 * (extent[2] + extent[3]) * spacing[1];
// double yd = (extent[3] - extent[2] + 1) * spacing[1];
//
// printf("%lf, %lf, %lf\n", xc, yc, yd);
//
// camera->SetParallelScale(0.5 * yd); // sets to 330; should be 1 -> transform
// camera->SetFocalPoint(xc, yc, 0.0); // sets to 0,0,0; isnice
// camera->SetPosition(xc, yc, 1000); // sets to 0,0,1000; isnice
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
this->ren->AddActor(actor);
}
@ -81,8 +90,3 @@ void BackgroundImage::setImagePath(string imagePath) {
updateImage();
}
void BackgroundImage::setCamera(vtkCamera *cam) {
this->getLayer()->SetActiveCamera(cam);
// TODO: fix the camera for this layer so this intentionally empty override can be removed.
}

View File

@ -16,6 +16,15 @@ private:
*/
void updateImage();
/** This function returns a 4x4matrix which maps the given square of [x0,w] x [y0,h] to the range [-1,1].
* @param x0 : x coordinate of leftmost edge of image
* @param y0 : y coordinate of bottommost edge of image
* @param xMax : x coordinate of rightmost edge of image
* @param yMax : y coordinate of topmost edge of image
* @return a 4x4 matrix which transforms the image from its original geometry to the range [-1,1]
*/
vtkSmartPointer<vtkMatrix4x4> getMatrix(const double x0, const double y0, const int xMax, const int yMax);
public:
/** Constructor.
@ -32,9 +41,6 @@ public:
* @param imagePath : String to the path of the new image to use.
*/
void setImagePath(std::string imagePath);
void setCamera(vtkCamera *cam) override;
};
#endif

View File

@ -5,6 +5,7 @@
#include <vtkGlyphSource2D.h>
#include <vtkNamedColors.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkProperty.h>
#include <vtkProperty2D.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkInteractorStyle.h>
@ -50,7 +51,7 @@ LGlyphLayer::LGlyphLayer() {
vtkNew<vtkGlyphSource2D> circleSource;
circleSource->SetGlyphTypeToCircle();
circleSource->SetScale(0.05);
circleSource->SetScale(0.01);
circleSource->Update();
vtkNew<vtkGlyph2D> glyph2D;
@ -65,6 +66,7 @@ LGlyphLayer::LGlyphLayer() {
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetOpacity(0.8);
this->ren->AddActor(actor);
}
@ -72,12 +74,18 @@ LGlyphLayer::LGlyphLayer() {
// creates a few points so we can test the updateData function
void LGlyphLayer::spoofPoints() {
this->points->InsertNextPoint(-4.125, 61.375 , 0);
this->points->InsertNextPoint(6.532949683882039, 53.24308582564463, 0); // Coordinates of Zernike
this->points->InsertNextPoint(5.315307819255385, 60.40001057122271, 0); // Coordinates of Bergen
this->points->InsertNextPoint( 6.646210231365825, 46.52346296009023, 0); // Coordinates of Lausanne
this->points->InsertNextPoint(-6.553894313570932, 62.39522131195857, 0); // Coordinates of the top of the Faroe islands
// this->points->InsertNextPoint(6.532949683882039, 53.24308582564463, 0); // Coordinates of Zernike
// this->points->InsertNextPoint(5.315307819255385, 60.40001057122271, 0); // Coordinates of Bergen
// this->points->InsertNextPoint( 6.646210231365825, 46.52346296009023, 0); // Coordinates of Lausanne
// this->points->InsertNextPoint(-6.553894313570932, 62.39522131195857, 0); // Coordinates of the top of the Faroe islands
this->points->Modified();
for (int i=0; i < 330; i+=5) {
for (int j=0; j < 330; j+=5) {
this->points->InsertNextPoint(-15.875+(12.875+15.875)/330*j, 46.125+(62.625-46.125)/330*i, 0);
}
}
this->points->Modified();
}
// returns new coords for a point; used to test the updateData function