From 507d0065f0a6097ab836026e0f547ac1a94a5fe7 Mon Sep 17 00:00:00 2001 From: djairoh Date: Mon, 6 May 2024 14:05:59 +0200 Subject: [PATCH] added backgroundimage transformation --- vtk/src/commands/CameraMoveCallback.cpp | 7 +- vtk/src/commands/TimerCallbackCommand.cpp | 2 +- vtk/src/layers/BackgroundImage.cpp | 80 ++++++++++++----------- vtk/src/layers/BackgroundImage.h | 12 +++- vtk/src/layers/LGlyphLayer.cpp | 20 ++++-- 5 files changed, 69 insertions(+), 52 deletions(-) diff --git a/vtk/src/commands/CameraMoveCallback.cpp b/vtk/src/commands/CameraMoveCallback.cpp index a462376..29f6ea6 100644 --- a/vtk/src/commands/CameraMoveCallback.cpp +++ b/vtk/src/commands/CameraMoveCallback.cpp @@ -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); diff --git a/vtk/src/commands/TimerCallbackCommand.cpp b/vtk/src/commands/TimerCallbackCommand.cpp index 75e416b..81573d8 100644 --- a/vtk/src/commands/TimerCallbackCommand.cpp +++ b/vtk/src/commands/TimerCallbackCommand.cpp @@ -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; } diff --git a/vtk/src/layers/BackgroundImage.cpp b/vtk/src/layers/BackgroundImage.cpp index ae6d556..b20a14a 100644 --- a/vtk/src/layers/BackgroundImage.cpp +++ b/vtk/src/layers/BackgroundImage.cpp @@ -1,5 +1,7 @@ #include "BackgroundImage.h" +#include #include +#include #include #include #include @@ -7,6 +9,7 @@ #include #include #include +#include #include #include @@ -19,56 +22,62 @@ BackgroundImage::BackgroundImage(string imagePath) : imagePath(imagePath) { updateImage(); } + +vtkSmartPointer 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::New(); + matrix->DeepCopy(eyeTransform); + return matrix; +} + + void BackgroundImage::updateImage() { + // read image data vtkSmartPointer 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 imageCenterer; imageCenterer->SetInputConnection(imageReader->GetOutputPort()); imageCenterer->CenterImageOn(); imageCenterer->Update(); + // get some info from the data we'll need in a second vtkSmartPointer 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 imageActor; - imageActor->SetInputData(imageData); + // map the imageData to a vtkPolydata so we can use a vtkTransform + vtkNew 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 transform; + transform->SetMatrix(getMatrix(origin[0], origin[1], extent[1]+origin[0], extent[3]+origin[1])); + vtkSmartPointer transformFilter = vtkSmartPointer::New(); + transformFilter->SetTransform(transform); + transformFilter->SetInputConnection(imageDataGeometryFilter->GetOutputPort()); + transformFilter->Update(); + // Create a mapper and actor + vtkNew 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 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. -} diff --git a/vtk/src/layers/BackgroundImage.h b/vtk/src/layers/BackgroundImage.h index c837b87..71a0d1c 100644 --- a/vtk/src/layers/BackgroundImage.h +++ b/vtk/src/layers/BackgroundImage.h @@ -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 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 diff --git a/vtk/src/layers/LGlyphLayer.cpp b/vtk/src/layers/LGlyphLayer.cpp index a49ee7f..bbcc404 100644 --- a/vtk/src/layers/LGlyphLayer.cpp +++ b/vtk/src/layers/LGlyphLayer.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -50,7 +51,7 @@ LGlyphLayer::LGlyphLayer() { vtkNew circleSource; circleSource->SetGlyphTypeToCircle(); - circleSource->SetScale(0.05); + circleSource->SetScale(0.01); circleSource->Update(); vtkNew glyph2D; @@ -65,6 +66,7 @@ LGlyphLayer::LGlyphLayer() { vtkNew 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