c++ - How to cut a plane in a user-designed object using vtk? -
for example, want design sphere radius 256 , origin (0.0,0.0,0.0). , want cut plane through origin , display cross-section. wiki of vtk of how generate sphere :http://www.vtk.org/wiki/vtk/examples/cxx/geometricobjects/sphere , how slice image after reading pics:http://www.vtk.org/gitweb?p=vtk.git;a=blob;f=examples/imageprocessing/cxx/imageslicing.cxx . want combine 2 pieces of codes accomplish this. fails.because says input of vtkimagereslice should object of class vtkimagedata vtkploydata.so how change vtkpolydata vtkimagedata?here code.
#include "vtksmartpointer.h" #include "vtkimagereader2.h" #include "vtkmatrix4x4.h" #include "vtkimagereslice.h" #include "vtklookuptable.h" #include "vtkimagemaptocolors.h" #include "vtkimageactor.h" #include "vtkrenderer.h" #include "vtkrenderwindow.h" #include "vtkrenderwindowinteractor.h" #include "vtkinteractorstyleimage.h" #include "vtkcommand.h" #include "vtkimagedata.h" #include "vtkimagemapper3d.h" #include "vtkstreamingdemanddrivenpipeline.h" #include "vtkinformation.h" #include "vtkspheresource.h" // mouse motion callback, turn "slicing" on , off class vtkimageinteractioncallback : public vtkcommand { public: static vtkimageinteractioncallback *new() { return new vtkimageinteractioncallback; }; vtkimageinteractioncallback() { this->slicing = 0; this->imagereslice = 0; this->interactor = 0; }; void setimagereslice(vtkimagereslice *reslice) { this->imagereslice = reslice; }; vtkimagereslice *getimagereslice() { return this->imagereslice; }; void setinteractor(vtkrenderwindowinteractor *interactor) { this->interactor = interactor; }; vtkrenderwindowinteractor *getinteractor() { return this->interactor; }; virtual void execute(vtkobject *, unsigned long event, void *) { vtkrenderwindowinteractor *interactor = this->getinteractor(); int lastpos[2]; interactor->getlasteventposition(lastpos); int currpos[2]; interactor->geteventposition(currpos); if (event == vtkcommand::leftbuttonpressevent) { this->slicing = 1; } else if (event == vtkcommand::leftbuttonreleaseevent) { this->slicing = 0; } else if (event == vtkcommand::mousemoveevent) { if (this->slicing) { vtkimagereslice *reslice = this->imagereslice; // increment slice position deltay of mouse int deltay = lastpos[1] - currpos[1]; reslice->update(); double slicespacing = reslice->getoutput()->getspacing()[2]; vtkmatrix4x4 *matrix = reslice->getresliceaxes(); // move center point slicing through double point[4]; double center[4]; point[0] = 0.0; point[1] = 0.0; point[2] = slicespacing * deltay; point[3] = 1.0; matrix->multiplypoint(point, center); matrix->setelement(0, 3, center[0]); matrix->setelement(1, 3, center[1]); matrix->setelement(2, 3, center[2]); interactor->render(); } else { vtkinteractorstyle *style = vtkinteractorstyle::safedowncast( interactor->getinteractorstyle()); if (style) { style->onmousemove(); } } } }; private: // actions (slicing only, now) int slicing; // pointer vtkimagereslice vtkimagereslice *imagereslice; // pointer interactor vtkrenderwindowinteractor *interactor; }; // program entry point int main(int argc, char *argv[]) { // generate sphere radius 256.0 , origin (0.0,0.0,0.0) vtksmartpointer<vtkspheresource> sphere = vtksmartpointer<vtkspheresource>::new(); sphere->setcenter(0.0, 0.0, 0.0); sphere->setradius(256.0); sphere->update(); static double sagittalelements[16] = { 0, 0, -1, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1 }; // set slice orientation vtksmartpointer<vtkmatrix4x4> resliceaxes = vtksmartpointer<vtkmatrix4x4>::new(); resliceaxes->deepcopy(sagittalelements); // set point through slice resliceaxes->setelement(0, 3, 0); resliceaxes->setelement(1, 3, 0); resliceaxes->setelement(2, 3, 0); // extract slice in desired orientation vtksmartpointer<vtkimagereslice> reslice = vtksmartpointer<vtkimagereslice>::new(); reslice->setinputconnection(sphere->getoutputport()); reslice->setoutputdimensionality(2); reslice->setresliceaxes(resliceaxes); reslice->setinterpolationmodetolinear(); // create greyscale lookup table vtksmartpointer<vtklookuptable> table = vtksmartpointer<vtklookuptable>::new(); table->setrange(0, 2000); // image intensity range table->setvaluerange(0.0, 1.0); // black white table->setsaturationrange(0.0, 0.0); // no color saturation table->setramptolinear(); table->build(); // map image through lookup table vtksmartpointer<vtkimagemaptocolors> color = vtksmartpointer<vtkimagemaptocolors>::new(); color->setlookuptable(table); color->setinputconnection(reslice->getoutputport()); // display image vtksmartpointer<vtkimageactor> actor = vtksmartpointer<vtkimageactor>::new(); actor->getmapper()->setinputconnection(color->getoutputport()); vtksmartpointer<vtkrenderer> renderer = vtksmartpointer<vtkrenderer>::new(); renderer->addactor(actor); vtksmartpointer<vtkrenderwindow> window = vtksmartpointer<vtkrenderwindow>::new(); window->addrenderer(renderer); // set interaction vtksmartpointer<vtkinteractorstyleimage> imagestyle = vtksmartpointer<vtkinteractorstyleimage>::new(); vtksmartpointer<vtkrenderwindowinteractor> interactor = vtksmartpointer<vtkrenderwindowinteractor>::new(); interactor->setinteractorstyle(imagestyle); window->setinteractor(interactor); window->render(); vtksmartpointer<vtkimageinteractioncallback> callback = vtksmartpointer<vtkimageinteractioncallback>::new(); callback->setimagereslice(reslice); callback->setinteractor(interactor); imagestyle->addobserver(vtkcommand::mousemoveevent, callback); imagestyle->addobserver(vtkcommand::leftbuttonpressevent, callback); imagestyle->addobserver(vtkcommand::leftbuttonreleaseevent, callback); // start interaction // start() method doesn't return until window closed user interactor->start(); return exit_success; }
thanks kun yang
no conversion needed. use vtkclippolydata poly -> poly filter.
Comments
Post a Comment