[vtk-developers] Rendering Image and Picking Points
Luis Vieira
luis.vieira at vektore.com
Fri Jun 10 12:45:15 EDT 2016
Hello Guys!!
First of all, I have to say that I appreciate any help!
I am struggling to rendering an Image and set up the camera focal point to
placed that image within the center of the camera.
1- I want this because the point is when I am rotating the object and
picking point in its surface I got the X, Y and Z world coordinates that
doesn't match the real coordinates of the camera;
2- When I am trying to use those coordinates once I had picked to calculate
the center of mass between them , the results are not accurately rights. And
everything that I have to derivate/build also are not, as well as when I
make a Plane and the its origin goes away into the camera out of my
cylinder(image).
Following my code. Thank you very much!! Luis
MainWindow.cpp
void MainWindow::ProcessImage(QString _model, QString _image)
{
double bounds[6] = { +1, -1, +1, -1, +1, -1 };
qvtkWidget->setVisible(true);
std::string iname = _model.toStdString();//model is a *.obj
Cylinder of Solid rock with h1024xw1024";
std::string imagename = _image.toStdString() ;//is the texture
*.bmp file that wraps up the cylinder";
vtkRenderer* renderer = vtkRenderer::New();
vtkRenderWindow* renderWindow = vtkRenderWindow::New();
renderWindow->StereoCapableWindowOn();
// Activate 3DConnexion device only on the left render window.
qvtkWidget->SetUseTDx(true);
qvtkWidget->SetRenderWindow(renderWindow);
//renderWindow->Delete();
const double angleSensitivity = 0.02;
const double translationSensitivity = 0.001;
QVTKInteractor *iren = qvtkWidget->GetInteractor();
vtkInteractorStyle *s =
static_cast<vtkInteractorStyle
*>(iren->GetInteractorStyle());
vtkTDxInteractorStyleCamera *t =
static_cast<vtkTDxInteractorStyleCamera
*>(s->GetTDxStyle());
t->GetSettings()->SetAngleSensitivity(angleSensitivity);
t->GetSettings()->SetTranslationXSensitivity(translationSensitivity);
t->GetSettings()->SetTranslationYSensitivity(translationSensitivity);
t->GetSettings()->SetTranslationZSensitivity(translationSensitivity);
// Read the image which will be the texture
QMenu* popup1 = new QMenu(qvtkWidget);
popup1->addAction("Draw Line");
popup1->addAction("Draw Plane");
popup1->addAction("Draw Clip");
popup1->addAction("Background White");
popup1->addAction("Background Black");
popup1->addAction("Stereo Rendering");
connect(popup1, SIGNAL(triggered(QAction*)), this,
SLOT(ProcessUserViewPortInteraction(QAction*)));
std::cout << "Reading image " << imagename << "..." <<
std::endl;
vtkBMPReader* bmpReader = vtkBMPReader::New();
bmpReader->SetFileName(imagename.c_str());
bmpReader->Update();
std::cout << "Done" << std::endl;
// Creating the texture
std::cout << "Making a texture out of the image... " <<
std::endl;
vtkTexture* texture = vtkTexture::New();
texture->SetInputConnection(bmpReader->GetOutputPort());
std::cout << "Done" << std::endl;
// Import geometry from a VRML file
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(bmpReader->GetOutputPort());
//imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->SetSize(600, 600);
vtkDataSet *pDataset;
// Import geometry from an OBJ file
std::cout << "Reading OBJ file " << iname << "..." <<
std::endl;
vtkOBJReader* reader = vtkOBJReader::New();
reader->SetFileName(iname.c_str());
reader->Update();
vtkPolyData *polyData2 = reader->GetOutput();
std::cout << "Obj reader = " << polyData2->GetNumberOfPoints()
<< std::endl;
std::cout << "Obj point data = " <<
polyData2->GetPointData()->GetNumberOfArrays() << std::endl;
std::cout << "Obj point data tuples = " <<
polyData2->GetPointData()->GetArray(0)->GetNumberOfTuples() << std::endl;
std::cout << "Obj point data compon = " <<
polyData2->GetPointData()->GetArray(0)->GetNumberOfComponents() <<
std::endl;
// //// Renderer
vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
mapper->SetInputData(polyData2);
vtkActor* texturedQuad = vtkActor::New();
texturedQuad->SetMapper(mapper);
texturedQuad->SetTexture(texture);
//texturedQuad->SetPosition(0, 0, 0);
// Create Normal Vectors to enhance smoothness & illumination
vtkSmartPointer<vtkPolyDataNormals> normals =
vtkSmartPointer<vtkPolyDataNormals>::New();
normals->SetInputData(polyData2);
normals->SetFeatureAngle(60.0);
// Mapper
vtkPolyDataMapper *SolidMapper = vtkPolyDataMapper::New();
SolidMapper->SetInputData(normals->GetOutput());
SolidMapper->ScalarVisibilityOff();
// Actor
vtkActor *SolidActor = vtkActor::New();
SolidActor->SetMapper(SolidMapper);
vtkImageData* image = imageViewer->GetInput();
int dim[3]; image->GetDimensions(dim);
double spacing[3];
image->GetSpacing(spacing);
double origin[3];
image->GetOrigin(origin);
renderer->ComputeVisiblePropBounds(bounds);
Renderer->AddActor(SolidActor);
Renderer->AddActor(texturedQuad);
float Cx = (dim[0] * spacing[0]) / 2. + origin[0];
float Cy = (dim[1] * spacing[1]) / 2. + origin[1];
vtkCamera* m_Camera = vtkCamera::New();
m_Camera->SetPosition(Cx, Cy, 1);
m_Camera->SetFocalPoint(Cx, Cy, 0);
m_Camera->SetViewUp(0, 1, 0);
m_Camera->GetDirectionOfProjection();
m_Camera->ParallelProjectionOn();
Renderer->SetActiveCamera(m_Camera);
Renderer->SetBackground(0, 0, 0); // Background color white
Renderer->ResetCamera();
qvtkWidget->GetRenderWindow()->AddRenderer(Renderer);
qvtkWidget->GetRenderWindow()->Render();
//renderer->ResetCamera();
vtkIPWCallback* myCallback =
vtkIPWCallback::New();
myCallback->renderer = Renderer;
myCallback->renderWindowInteractor =
qvtkWidget->GetRenderWindow()->GetInteractor();
myCallback->readerClip = vtkOBJReader::New();
myCallback->readerClip = reader;
this->styleCallback = myCallback;
//renderer->ResetCamera();
this->stylePicker->normalsKore =
vtkSmartPointer<vtkPolyDataNormals>::New();
this->stylePicker->normalsKore = normals;
this->stylePicker->renderer =
vtkSmartPointer<vtkRenderer>::New();
this->stylePicker->actor = vtkActor::New();
this->stylePicker->mapper = vtkPolyDataMapper::New();
this->stylePicker->points = vtkSmartPointer<vtkPoints>::New();
this->stylePicker->vertices =
vtkSmartPointer<vtkCellArray>::New();
this->stylePicker->renderer = Renderer;
this->stylePicker->renderWindowInteractor =
qvtkWidget->GetRenderWindow()->GetInteractor();
vtkSmartPointer<vtkPointPicker> pointPicker =
vtkSmartPointer<vtkPointPicker>::New();
qvtkWidget->GetRenderWindow()->GetInteractor()->SetPicker(pointPicker);
qvtkWidget->GetRenderWindow()->GetInteractor()->SetInteractorStyle(this->sty
lePicker);
viewPortAxActor = vtkSmartPointer<vtkAxesActor>::New();
viewPortAxActor->GetXAxisCaptionActor2D()->GetCaptionTextProperty()->SetColo
r(0, 0, 0);
viewPortAxActor->GetXAxisCaptionActor2D()->SetWidth(0.05);
viewPortAxActor->GetXAxisCaptionActor2D()->SetHeight(0.05);
viewPortAxActor->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->SetColo
r(0, 0, 0);
viewPortAxActor->GetYAxisCaptionActor2D()->SetWidth(0.05);
viewPortAxActor->GetYAxisCaptionActor2D()->SetHeight(0.05);
viewPortAxActor->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->SetColo
r(0, 0, 0);
viewPortAxActor->GetZAxisCaptionActor2D()->SetWidth(0.05);
viewPortAxActor->GetZAxisCaptionActor2D()->SetHeight(0.05);
viewPortOMWidget =
vtkSmartPointer<vtkOrientationMarkerWidget>::New();
viewPortOMWidget->SetOrientationMarker(viewPortAxActor);
viewPortOMWidget->SetViewport(0.0, 0.0, 0.25, 0.25);
Connections = vtkEventQtSlotConnect::New();
Connections->Connect(qvtkWidget->GetRenderWindow()->GetInteractor(),
vtkCommand::KeyPressEvent,
this,
SLOT(popup(vtkObject*, unsigned long, void*, void*,
vtkCommand*)),
popup1, 1.0);
}
MainWindow.h
//Point PickFunction
class MouseInteractorStyle2 : public
vtkInteractorStyleTrackballCamera
{
public:
std::vector<double*> pointList;
std::vector<double*> pointListPlane;
vtkSmartPointer<vtkMatrix4x4> matrix;
vtkSmartPointer<vtkPolyDataNormals> normalsKore =
vtkSmartPointer<vtkPolyDataNormals>::New();
vtkSmartPointer<vtkSphereSource> spherePickSource;
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> vertices =
vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkPolyData> polydata;
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindowInteractor>
renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkActor* actor;
vtkPolyDataMapper* mapper;
static MouseInteractorStyle2* New()
{
return new MouseInteractorStyle2;
}
//vtkTypeMacro(MouseInteractorStyle2,
vtkInteractorStyleTrackballCamera);
virtual void OnRightButtonDown()
{
//std::cout << "Picking pixel: " <<
this->Interactor->GetEventPosition()[0] << " " <<
this->Interactor->GetEventPosition()[1] << std::endl;
//this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0
],
// this->Interactor->GetEventPosition()[1],
// 0, // always zero.
//
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
//double picked[3];
//this->Interactor->GetPicker()->GetPickPosition(picked);
//std::cout << "Picked value: " << picked[0] << " " <<
picked[1] << " " << picked[2] << std::endl;
int* clickPos =
this->renderWindowInteractor->GetEventPosition();
// Pick from this location.
vtkPropPicker* picker =
vtkPropPicker::New();
picker->Pick(clickPos[0], clickPos[1], 0,
this->renderer);
//vtkSmartPointer<vtkCellPicker> picker =
// vtkSmartPointer<vtkCellPicker>::New();
////picker->SetTolerance(0.005);
picker->Pick(clickPos[0], clickPos[1], 0,
this->renderer);
double* pos = picker->GetPickPosition();
std::cout << "Pick position (world coordinates) is: "
<< pos[0] << " " << pos[1]
<< " " << pos[2] << std::endl;
std::cout << "Picked actor: " <<
picker->GetActor() << std::endl;
// Add label array.
polydata = vtkSmartPointer<vtkPolyData>::New();
spherePickSource =
vtkSmartPointer<vtkSphereSource>::New();
spherePickSource->SetRadius(0.030);
spherePickSource->SetCenter(pos[0], pos[1],
pos[2]);
if (pointList.size() < 2)
{
//pointList.clear();
pointList.push_back(picked);
}
if (pointListPlane.size() < 30)
{
pointListPlane.push_back(picked);
}
//Create a mapper and actor
mapper =
vtkPolyDataMapper::New();
mapper->SetInputConnection(spherePickSource->GetOutputPort());
actor =
vtkActor::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetPointSize(5);
actor->GetProperty()->SetColor(1, 1, 1);
this->renderer->AddActor(actor);
vtkInteractorStyleTrackballCamera::OnRightButtonDown();
}
private:
};
Luis Vieira, B.SC.
Consultant, Software Engineer
Vektore Exploration Consulting Corporation
Av. Nove de Julho 1953 suite 1401, Sao Paulo
CEP 01313001 - Brazil
ca.linkedin.com/in/joaoluisvieira
<mailto:luis.vieira at vektore.com> luis.vieira at vektore.com
<http://www.vektore.com/> www.vektore.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20160610/8be9dcdd/attachment-0001.html>
More information about the vtk-developers
mailing list