#include <vtkCommand.h>
#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkDistanceWidget.h>
#include <vtkDistanceRepresentation.h>
#include <vtkAngleWidget.h>
#include <vtkProperty2D.h>
#include <vtkLeaderActor2D.h>
#include <vtkAngleRepresentation2D.h>
#include <vtkBiDimensionalWidget.h>
#include <vtkBiDimensionalRepresentation2D.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)
class vtkBiDimensionalCallback : public vtkCommand
{
public:
static vtkBiDimensionalCallback* New()
{
return new vtkBiDimensionalCallback;
}
virtual void Execute(vtkObject* caller, unsigned long, void*)
{
vtkBiDimensionalWidget* biDimensionalWidget =
reinterpret_cast<vtkBiDimensionalWidget*> (caller);
vtkBiDimensionalRepresentation2D* representation =
static_cast<vtkBiDimensionalRepresentation2D*> (biDimensionalWidget->GetRepresentation());
double p1[3];
representation->GetPoint1DisplayPosition(p1);
double p2[3];
representation->GetPoint1DisplayPosition(p2);
double p3[3];
representation->GetPoint1DisplayPosition(p3);
double p4[3];
representation->GetPoint1DisplayPosition(p4);
//显示其中一个点的屏幕坐标(px)
std::cout << "P1: " << p1[0] << " " << p1[1] << " " << p1[2] << std::endl;
}
vtkBiDimensionalCallback() { }
};
int main()
{
int WidgetType;
std::cout << "Please select the Measurement Distance WidgetType: " << std::endl;
std::cout << "0 vtkDistanceWidget \n";
std::cout << "1 vtkAngleWidget \n";
std::cout << "2 vtkBiDimensionalWidget \n";
std::cin >> WidgetType;
vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("0.jpg");
reader->Update();
vtkSmartPointer<vtkImageActor> imgActor = vtkSmartPointer<vtkImageActor>::New();
imgActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkRenderer> render = vtkSmartPointer<vtkRenderer>::New();
render->AddActor(imgActor);
render->SetBackground(0, 0, 0);
render->ResetCamera();
vtkSmartPointer<vtkRenderWindow> rw = vtkSmartPointer<vtkRenderWindow>::New();
rw->AddRenderer(render);
rw->SetWindowName("MeasurementDistanceApp");
rw->SetSize(320, 320);
rw->Render();
vtkSmartPointer<vtkRenderWindowInteractor> rwi =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
rwi->SetRenderWindow(rw);
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
rwi->SetInteractorStyle(style);
/****************************************************************/
//vtkDistanceWidget
if (WidgetType == 0)
{
//实例化Widget
vtkSmartPointer<vtkDistanceWidget> distanceWidget =
vtkSmartPointer<vtkDistanceWidget>::New();
//指定渲染窗口交互器,来监听用户事件
distanceWidget->SetInteractor(rwi);
//必要时使用观察者/命令模式创建回调函数(此处没用)
//创建几何表达实体。用SetRepresentation()把事件与Widget关联起来
//或者使用Widget默认的几何表达实体
distanceWidget->CreateDefaultRepresentation();
static_cast<vtkDistanceRepresentation*> (distanceWidget->GetRepresentation())
->SetLabelFormat("%-#6.3g px");
//激活Widget
distanceWidget->On();
rw->Render();
rwi->Initialize();
rwi->Start();
}
//vtkAngleWidget
if (WidgetType == 1)
{
vtkSmartPointer<vtkAngleWidget> angleWiget = vtkSmartPointer<vtkAngleWidget>::New();
angleWiget->SetInteractor(rwi);
//创建个性化的实体图标
vtkSmartPointer<vtkAngleRepresentation2D> angleRep =
vtkSmartPointer<vtkAngleRepresentation2D>::New();
angleRep->GetRay1()->GetProperty()->SetColor(0, 1, 0);
angleRep->GetRay1()->GetProperty()->SetLineWidth(3);
angleRep->GetRay2()->GetProperty()->SetColor(0, 1, 0);
angleRep->GetRay1()->GetProperty()->SetLineWidth(3);
angleRep->GetArc()->GetProperty()->SetColor(0, 1, 0);
angleRep->GetArc()->GetProperty()->SetLineWidth(3);
angleWiget->SetRepresentation(angleRep);
angleWiget->On();
rw->Render();
rwi->Initialize();
rwi->Start();
}
//vtkBiDimensionalWidget
if (WidgetType == 2)
{
vtkSmartPointer<vtkBiDimensionalWidget> bidimensionalWidget =
vtkSmartPointer<vtkBiDimensionalWidget>::New();
bidimensionalWidget->SetInteractor(rwi);
//采用默认的图标
bidimensionalWidget->CreateDefaultRepresentation();
//添加“观察者-命令模式(命令子类方案)”
vtkSmartPointer<vtkBiDimensionalCallback> bidiCallback =
vtkSmartPointer<vtkBiDimensionalCallback>::New();
bidimensionalWidget->AddObserver(vtkCommand::InteractionEvent, bidiCallback);
bidimensionalWidget->On();
rw->Render();
rwi->Initialize();
rwi->Start();
}
return 0;
}
效果: