-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis.cpp
177 lines (132 loc) · 4.54 KB
/
vis.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include "unistd.h"
#include "data.hpp"
#include "model.hpp"
#include <math.h>
#include <gsl/gsl_multimin.h>
#include "vis.hpp"
#define PI 3.141592654
using namespace std;
/********************************************************************************
* Visuals/main thread
********************************************************************************/
class TimerCallback : public vtkCommand {
public:
static TimerCallback* New() {
return new TimerCallback;
}
virtual void Execute(vtkObject *vtkNotUsed(caller), unsigned long eventId,void *vtkNotUsed(callData)) {
if(vtkCommand::TimerEvent == eventId) {
mParent->onTimeout();
}
}
FittingWindow* mParent;
};
FittingWindow::FittingWindow()
: mDataChanged(true) {
mAngle_x = 0;
mAngle_y = 0;
mAngle_z = 0;
// create sphere geometry
mSphere = vtkSphereSource::New();
mSphere->SetRadius(1.0);
mSphere->SetCenter(0,0,0);
mSphere->SetThetaResolution(5);
mSphere->SetPhiResolution(5);
mArrow = vtkArrowSource::New();
//Creater the mappers
mSphereMapper = vtkPolyDataMapper::New();
mSphereMapper->SetInput(mSphere->GetOutput());
mArrowMapper = vtkPolyDataMapper::New();
mArrowMapper->SetInput(mArrow->GetOutput());
mCalcRenderer = vtkRenderer::New();
mRenderWin = vtkRenderWindow::New();
mRenderWin->AddRenderer(mCalcRenderer);
mWindowInteractor = vtkRenderWindowInteractor::New();
mRenderWin->SetInteractor(mWindowInteractor);
mRenderWin->SetSize(800, 600);
//We start by visualising iteraction 0
visualisedIteraction = 0;
mArrowActor = vtkActor::New();
mArrowActor->SetMapper(mArrowMapper);
mArrowActor->SetScale(4);
mArrowActor->GetProperty()->SetColor(1,1,0);
}
FittingWindow::~FittingWindow() {
}
void FittingWindow::onTimeout() {
if(mDataChanged) {
//There is new data avaiable. We should change the visualisation
mDataChanged = false;
update();
}
}
void FittingWindow::setNuclei(const Nuclei& nuclei) {
mNuclei = nuclei;
updateNuclei(mCalcSpheres,true);
updateNuclei(mExpSpheres ,false);
}
void FittingWindow::setCalcVals(const Vals& calcVals,const Vector3& metal,
double angle_x,double angle_y,double angle_z) {
mCalcVals = calcVals;
mMetal = metal;
mDataChanged = true;
mAngle_x = angle_x;
mAngle_y = angle_y;
mAngle_z = angle_z;
}
void FittingWindow::setExpVals(const Vals& expVals) {
mExpVals = expVals;
mDataChanged = true;
}
void FittingWindow::start() {
mWindowInteractor->Start();
}
void FittingWindow::mainLoop() {
mWindowInteractor->Initialize();
vtkSmartPointer<TimerCallback> timerCallback = TimerCallback::New();
timerCallback->mParent = this;
mWindowInteractor->AddObserver(vtkCommand::TimerEvent,timerCallback);
mWindowInteractor->CreateRepeatingTimer(500);
mWindowInteractor->Start();
}
void FittingWindow::update() {
updateVals(mCalcRenderer,mCalcVals,mCalcSpheres,false);
updateVals(mCalcRenderer,mExpVals ,mExpSpheres ,true);
//Move the arrow
mArrowActor->SetOrientation(mAngle_x /2/PI * 360,
mAngle_y /2/PI * 360,
mAngle_z /2/PI * 360);
mArrowActor->SetPosition(mMetal.x,mMetal.y,mMetal.z);
mCalcRenderer->AddActor(mArrowActor);
mRenderWin->Render();
}
void FittingWindow::updateNuclei(std::vector<vtkSmartPointer<vtkActor> >& actors,
bool wireframe) {
for(unsigned long i = 0; i<mNuclei.size();i++) {
vtkSmartPointer<vtkActor> sphereActor = vtkActor::New();
sphereActor->SetMapper(mSphereMapper);
sphereActor->GetProperty()->SetRepresentationToWireframe();
sphereActor->SetPosition(mNuclei[i].x,mNuclei[i].y,mNuclei[i].z);
mCalcRenderer->AddActor(sphereActor);
actors.push_back(sphereActor);
}
mCalcRenderer->ResetCameraClippingRange();
}
void FittingWindow::updateVals(vtkRenderer* renderer,
const Vals& vals,
std::vector<vtkSmartPointer<vtkActor> >& spheres,
bool experimental) {
double maxAbs = abs(abs(mExpVals.max) > abs(mExpVals.min) ? mExpVals.max : mExpVals.min);
for(unsigned long i = 0;i<vals.size();i++) {
vtkSmartPointer<vtkActor> actor = spheres[i];
double c = vals[i]/(maxAbs*2)+0.5;
actor->SetScale(0.1+sqrt(abs(vals[i])/maxAbs));
if(experimental) {
actor->GetProperty()->SetColor(1,1,1);
} else {
actor->GetProperty()->SetColor(0,c,1-c);
}
actor->GetProperty()->SetRepresentationToWireframe();
}
}