小影的旅途 2015-08-02 11:11 采纳率: 0%
浏览 2178

求助:vtk callback并没有完成预期的效果

我按照官网的例子自己写了一个callback函数,设定为初始的renderer背景颜色为黑色,点击鼠标右键后renderer的背景颜色设置为(1.0,0.0,0.0)
但是这段代码跑起来 renderer的背景颜色就是红色(1.0,0.0,0.0)。

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

// The callback does the work.
// The callback keeps a pointer to the sphere whose resolution is
// controlled. After constructing the callback, the program sets the
// SphereSource of the callback to
// the object to be controlled.
class vtkSliderCallback : public vtkCommand
{
public:
static vtkSliderCallback New()
{
return new vtkSliderCallback;
}
virtual void Execute(vtkObject *caller, unsigned long, void
)
{
vtkSliderWidget sliderWidget =
reinterpret_cast<vtkSliderWidget
>(caller);
int value = static_cast(static_cast(sliderWidget->GetRepresentation())->GetValue());
this->SphereSource->SetPhiResolution(value/2);
this->SphereSource->SetThetaResolution(value);
}
vtkSliderCallback():SphereSource(0) {}
vtkSphereSource *SphereSource;
};

class vtkMouseCallback: public vtkCommand
{
public:
int slicing;
static vtkMouseCallback* New()
{
return new vtkMouseCallback;
}

virtual void Execute(vtkObject *caller, unsigned long event, void *)
{
slicing = 0;
vtkRenderer *ren1 = vtkRenderer::SafeDownCast(caller);
if(vtkCommand::RightButtonPressEvent){
slicing = 1;
}
if(slicing){
ren1->SetBackground(1.0,0.0,0.0);
}
}

};

int main()
{
// A sphere
vtkSmartPointer sphereSource = vtkSmartPointer::New();
sphereSource->SetCenter(0.0, 0.0, 0.0);
sphereSource->SetRadius(4.0);
sphereSource->SetPhiResolution(2);
sphereSource->SetThetaResolution(1);

vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInput(sphereSource->GetOutput());

vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetInterpolationToFlat();

// A renderer and render window
vtkSmartPointer renderer = vtkSmartPointer::New();
vtkSmartPointer renderWindow = vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);

// An interactor
vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

// Add the actors to the scene
renderer->AddActor(actor);

// Render an image (lights and cameras are created automatically)
renderWindow->Render();

// Here we describe the representation of the widget.
vtkSmartPointer sliderRep = vtkSmartPointer::New();
sliderRep->SetMinimumValue(3.0);
sliderRep->SetMaximumValue(1000.0);
sliderRep->SetValue(sphereSource->GetThetaResolution());
sliderRep->SetTitleText("Sphere Resolution");

// Here we use normalized display coordinates (0,1) so that the
// slider will stay in the same proportionate location if the window
// is resized.
sliderRep->GetPoint1Coordinate()->SetCoordinateSystemToNormalizedDisplay();
sliderRep->GetPoint1Coordinate()->SetValue(.1 ,.1);
sliderRep->GetPoint2Coordinate()->SetCoordinateSystemToNormalizedDisplay();
sliderRep->GetPoint2Coordinate()->SetValue(.3, .1);

// Create the callback and pass it the sphereSource to be controlled
vtkSmartPointer callback = vtkSmartPointer::New();
callback->SphereSource = sphereSource;

vtkSmartPointer call = vtkMouseCallback::New();
renderWindowInteractor->AddObserver(vtkCommand::InteractionEvent,call);
// The widget is the controller for the interction.
vtkSmartPointer sliderWidget = vtkSmartPointer::New();
sliderWidget->SetInteractor(renderWindowInteractor);
sliderWidget->SetRepresentation(sliderRep);
sliderWidget->SetAnimationModeToAnimate();
sliderWidget->EnabledOn();
//renderWindowInteractor->Initialize();
renderWindow->Render();
// Observe the interaction events of the widget. If the computation
// in the callback is time consuming, observe the
// EndInteractionEvent instead.
sliderWidget->AddObserver(vtkCommand::InteractionEvent,callback);

renderWindowInteractor->Start();

return 0;
}

  • 写回答

1条回答 默认 最新

  • devmiao 2015-08-02 22:24
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3