2017-02-13 160 views
0

我設置了一個3D數組圖像數據,渲染並顯示它,並向它添加一個vtkboxwidget。如何獲取vtkboxwidget座標

我想在用戶旋轉時獲取vtkboxwidget的每個角度的座標,縮放vtkboxwidget。我怎樣才能做到這一點。這是我的代碼。

#include "myrender.h" 
#include <vtkInteractorStyleTrackballCamera.h> 
// For vtkBoxWidget: 
#include "vtkPlanes.h" 
#include "vtkBoxWidget.h" 
#include "vtkTransform.h" 
#include "vtkCommand.h" 
#include "vtkProperty.h" 
#include "vtkPlane.h" 
#include "vtkImageData.h" 
#include "vtkExtractVOI.h" 
#include "vtkBorderRepresentation.h" 
#include "vtkProp3D.h" 

class vtkMyCallback : public vtkCommand{ 
public: 
    static vtkMyCallback *New() { 
     return new vtkMyCallback; 
    } 
    virtual void Execute(
     vtkObject *caller, unsigned long, void*) { 
    // Here we use the vtkBoxWidget to transform the underlying coneActor 
    // (by manipulating its transformation matrix). 
    vtkSmartPointer<vtkTransform> t = 
     vtkSmartPointer<vtkTransform>::New(); 
    vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller); 
    widget->GetTransform(t); 
    //widget->GetProp3D()->SetUserTransform(t); 
    double *pos = t->GetPosition(); 
    t->GetMatrix(); 
    std::cout<<"position: "<<pos[0]<<" "<<pos[1]<<" "<<pos[2]<<std::endl; 
}}; 
MyRender::MyRender() { 
    ROIdata = NULL; 
    ROI_sz0 = 0; 
    ROI_sz1 = 0; 
    ROI_sz2 = 0; 
} 

MyRender::~MyRender() { 

} 

void MyRender::setData(unsigned char *ROIdata, int ROI_sz0, int ROI_sz1,  int ROI_sz2) { 
    this->ROIdata = ROIdata; 
    this->ROI_sz0 = ROI_sz0; 
    this->ROI_sz1 = ROI_sz1; 
    this->ROI_sz2 = ROI_sz2; 
} 

void MyRender::render(vtkSmartPointer<vtkRenderWindow> renWin) { 
int width = ROI_sz0; 
int height = ROI_sz1; 
int depth = ROI_sz2; 
/** 
* RENDER WHOLE BRAIN DATA AND SHOW. 
*/ 
//Convert the c-style image to a vtkImageData 
vtkSmartPointer<vtkImageImport> imageImport = vtkSmartPointer<vtkImageImport>::New(); 
imageImport->SetImportVoidPointer(ROIdata); 
imageImport->SetDataScalarTypeToUnsignedChar(); 
imageImport->SetNumberOfScalarComponents(1); 
imageImport->SetDataSpacing(1.0, 1.0, 1.0); 
imageImport->SetDataOrigin(0, 0, 0); 
imageImport->SetDataExtent(0, width-1, 0, height-1, 0, depth-1); 
imageImport->SetWholeExtent(0, width-1, 0, height-1, 0, depth-1); 
imageImport->Update(); 
int *arr = imageImport->GetOutput()->GetDimensions(); 
std::cout<<"x size: "<<arr[0]<<" y size: "<<arr[1]<<" z size: "<<arr[2]<<std::endl; 
//Create the standard ren, render window, and interactor 
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New(); 
//Create transfer mapping scalar value to opacity 
vtkSmartPointer<vtkPiecewiseFunction> opacityFunc = vtkSmartPointer<vtkPiecewiseFunction>::New(); 
opacityFunc->AddPoint(0, 0.0); 
opacityFunc->AddPoint(512, 1.0); 
opacityFunc->ClampingOff(); 
//Create transfer mapping scalar value to color 
vtkSmartPointer<vtkColorTransferFunction> colorFunc 
     = vtkSmartPointer<vtkColorTransferFunction>::New(); 
colorFunc->AddRGBPoint(150, 1.0, 1.0, 1.0); 
//The property describes how the data will look 
vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New(); 
volumeProperty->SetColor(colorFunc); 
volumeProperty->SetScalarOpacity(opacityFunc); 
volumeProperty->ShadeOn(); 
volumeProperty->SetInterpolationTypeToLinear(); 
vtkSmartPointer<vtkSmartVolumeMapper> volumeMapper = vtkSmartPointer<vtkSmartVolumeMapper>::New(); 
volumeMapper->SetInputConnection(imageImport->GetOutputPort()); 
//The volume holds the mapper and the property and can be used to position/orient the volume 
vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New(); 
volume->SetMapper(volumeMapper); 
volume->SetProperty(volumeProperty); 
ren->AddVolume(volume); 
ren->SetBackground(0, 0, 0); 

vtkSmartPointer<vtkBoxWidget> box = vtkSmartPointer<vtkBoxWidget>::New(); 
box->SetInteractor(renWin->GetInteractor()); 
box->SetPlaceFactor(1); 
box->SetInputConnection(imageImport->GetOutputPort()); 
box->PlaceWidget(); 
box->InsideOutOn(); 
box->SetProp3D(volume); 
box->GetOutlineProperty()->SetRepresentationToSurface(); 
box->GetOutlineProperty()->SetOpacity(1.0); 
box->RotationEnabledOff(); 
vtkSmartPointer<vtkMyCallback> callback = vtkSmartPointer<vtkMyCallback>::New(); 
box->AddObserver(vtkCommand::InteractionEvent, callback); 
box->On(); 

renWin->AddRenderer(ren); 
renWin->Render(); 
} 

如何在vtkCommand類中實時獲取vtkboxwidget的coodinates。非常感謝。

回答

1

doc

無效vtkBoxWidget :: GetPolyData(vtkPolyData * PD)
抓住POLYDATA(包括點),定義框控件。 Polydata由6個四邊形面和15個點組成。前八個點定義了八個角點;接下來的六個定義了-x,+ x,-y,+ y,-z,+ z個麪點;最後一點(15點中的第15點)定義了六面體的中心。當InteractionEvent或EndInteractionEvent事件被調用時,這些點值保證是最新的。用戶提供了vtkPolyData,點和單元格被添加到它。

它看起來像是你在找什麼或不是?

+0

非常感謝你,那正是我想要的。在開始時設置輸入連接,並使用getpolydata獲取邊界。謝謝。 box-> SetInputConnection(imageImport-> GetOutputPort()); vtkSmartPointer pd = vtkSmartPointer :: New(); 012-box-> GetPolyData(pd); double * bound = pd-> GetBounds(); – MySuperPower

+0

很高興我能幫到你。如果這解決了你需要的一切,你能否將答案標記爲已接受?所以它從沒有回答的問題清單中消失... – tomj

+0

謝謝,我點擊了灰色的鉤子,它變成了綠色,這是否滿足你說的需要?我很抱歉,我是一個更新,不擅長英語的人。 – MySuperPower