-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrotating_sphere.cpp
79 lines (65 loc) · 2.11 KB
/
rotating_sphere.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "vtkActor.h"
#include "vtkCommand.h"
#include "vtkNamedColors.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkSphereSource.h"
class TimerCallback final : public vtkCommand {
public:
TimerCallback(vtkSmartPointer<vtkActor> actor) : actor_{std::move(actor)} {};
TimerCallback() = default;
~TimerCallback() final = default;
static auto New() {
return new TimerCallback();
}
void
Execute(vtkObject* caller, unsigned long eventId, void* callData) override {
auto interactor = dynamic_cast<vtkRenderWindowInteractor*>(caller);
switch (eventId) {
case vtkCommand::TimerEvent:
++timer_cnt_;
if (timer_cnt_ < 36) {
actor_->RotateZ(5);
interactor->GetRenderWindow()->Render();
} else {
interactor->DestroyTimer();
}
}
}
private:
std::size_t timer_cnt_ = 0;
vtkSmartPointer<vtkActor> actor_;
};
auto main(int argc, char** argv) -> int {
vtkNew<vtkSphereSource> sphere_source;
sphere_source->SetCenter(0.0, 0.0, 0.0);
sphere_source->SetRadius(1.0);
sphere_source->SetThetaResolution(30);
sphere_source->SetPhiResolution(30);
// sphere_source->Update();
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(sphere_source->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->RotateX(90);
actor->GetProperty()->SetRepresentationToWireframe();
vtkNew<vtkRenderer> render;
render->AddActor(actor);
vtkNew<vtkRenderWindow> window;
window->AddRenderer(render);
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(window);
interactor->Initialize();
vtkNew<vtkNamedColors> colors;
actor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
render->SetBackground(colors->GetColor4d("Seashell").GetData());
auto callback = new TimerCallback(actor);
interactor->AddObserver(vtkCommand::TimerEvent, callback);
interactor->CreateRepeatingTimer(100);
window->Render();
interactor->Start();
}