-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformUnitBall.cxx
More file actions
366 lines (304 loc) · 12.7 KB
/
TransformUnitBall.cxx
File metadata and controls
366 lines (304 loc) · 12.7 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*=========================================================================
TransformUnitBall -- this calls vtkSphereSource to create & plot
a unit ball (sphere) u. It then creates a 3x3 random matrix and
multiplies it into the unit ball, A*u, and plots the resulting
ellipse.
This code started from TestSphereSource.cxx, which is part of the
VTK distribution. I ripped out stuff I don't need and added
my own suff.... lots of it.
SDB -- March 2021.
=========================================================================*/
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkAxesActor.h>
#include <vtkRenderWindow.h>
#include <vtkNamedColors.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include <vtkMath.h>
#include <vtkPoints.h>
#include <vtkTextActor.h>
#include <vtkProperty2D.h>
#include <vtkTextProperty.h>
#include <vtkMatrix3x3.h>
#include <vtkCamera.h>
#include <iostream>
#include <ostream>
#include <string>
#include <sstream>
#include <time.h>
#include <vector>
//------------------------------------------------------------------
class KeyEventHandler {
public:
bool RPressed;
KeyEventHandler() {
RPressed = false;
}
void KeypressCallbackFunction(vtkObject* caller,
long unsigned int vtkNotUsed(eventId),
void* vtkNotUsed(callData)) {
vtkRenderWindowInteractor *iren =
static_cast<vtkRenderWindowInteractor*>(caller);
//std::cout << "Caught event in KeypressCallbackFunction" << std::endl;
std::string key;
if (iren->GetKeySym()) {
key = iren->GetKeySym();
//cout << "Key pressed: " << key << endl;
}
if (key == "r"){
RPressed = true;
iren->ExitCallback (); // Exit the loop since we caught an "r"
} else {
RPressed = false;
}
}
bool RKeyPressed(void) {
// I need to reset the state after every query, otherwise
// the RPressed persists and I can't capture other events.
bool temp = RPressed;
RPressed = false;
return temp;
}
};
//=========================================================================
int main(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
cout << "Hit \'r\' to refresh (get a new ellipsoid)" << endl;
cout << "Hit \'q\' to quit" << endl;
// Seed RNG with time
vtkMath::RandomSeed(time(NULL));
//--------------------------------------------------------------------
// Unit ball
vtkSmartPointer<vtkSphereSource> unitBall = vtkSmartPointer<vtkSphereSource>::New();
unitBall->SetThetaResolution(32);
unitBall->SetPhiResolution(32);
unitBall->SetStartTheta(0.0);
unitBall->SetEndTheta(360.0);
unitBall->SetStartPhi(0.0);
unitBall->SetEndPhi(180.0);
unitBall->LatLongTessellationOff(); // Generate triangles when off.
unitBall->SetOutputPointsPrecision(vtkAlgorithm::DOUBLE_PRECISION);
double center[3] = {0.0f, 0.0f, 0.0f};
unitBall->SetCenter(center);
double radius = 1.0f;
unitBall->SetRadius(radius);
unitBall->Update();
//--------------------------------------------------------------------
// Visualization stuff for unit ball
// Define plot colors
vtkNew<vtkNamedColors> colors;
vtkColor3d actorColorRed = colors->GetColor3d("Red");
vtkColor3d actorColorBlue = colors->GetColor3d("Blue");
vtkColor3d EdgeColour = colors->GetColor3d("slate_grey_dark");
vtkColor3d BackgroundColour = colors->GetColor3d("Silver");
vtkColor3d textColor = colors->GetColor3d("Green");
// Create mapper for unit ball
vtkNew<vtkPolyDataMapper> unitBallMapper;
unitBallMapper->SetInputConnection(unitBall->GetOutputPort());
// Create actor for unit ball
vtkNew<vtkActor> unitBallActor;
unitBallActor->SetMapper(unitBallMapper);
unitBallActor->GetProperty()->EdgeVisibilityOn();
unitBallActor->GetProperty()->SetColor(actorColorBlue.GetData());
unitBallActor->GetProperty()->SetEdgeColor(EdgeColour.GetData());
unitBallActor->GetProperty()->SetOpacity(.3);
//-------------------------------------------------------------
// General visualization stuff -- mostly boilerplate code.
// Create renderer and render window
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(BackgroundColour.GetData());
// Store my initial camera position so I can get it back.
double initPos[3] = {1.0, 0.0, 0.0};
double initFP[3];
double initViewUp[3] = {0.0, 0.0, 1.0}; // I want z axis up
renderer->GetActiveCamera()->SetPosition(initPos);
renderer->GetActiveCamera()->GetFocalPoint(initFP);
renderer->GetActiveCamera()->SetViewUp(initViewUp);
// cout << "Camera position = " << initPos[0] << " " << initPos[1] << " " << initPos[2] << " " << endl;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Transforming a unit ball -- y = A*x");
renderWindow->SetSize(800, 600); // This sets size of window on computer screen.
// Create Interactor
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkNew<vtkInteractorStyleTrackballCamera> style;
renderWindowInteractor->SetInteractorStyle(style);
// Key event -- attach to renderWindowInteractor
KeyEventHandler KeyEvent;
renderWindowInteractor->AddObserver(vtkCommand::KeyPressEvent,
&KeyEvent,
&KeyEventHandler::KeypressCallbackFunction);
// Add axes to the render window interactor.
// I do this new with every loop so the axes start in the
// correct orientation.
vtkNew<vtkAxesActor> axes;
vtkNew<vtkOrientationMarkerWidget> widget;
widget->SetOutlineColor( 0.9300, 0.5700, 0.1300 );
widget->SetOrientationMarker( axes );
widget->SetInteractor( renderWindowInteractor );
widget->SetViewport( 0.0, 0.0, 0.2, 0.2 );
widget->SetEnabled( 1 );
//-----------------------------------------------------------------
// Now run a loop. Inside the loop create a new random matrix
// and transform the unit ball and make a plot of the resulting
// ellipsoid.
while(1) {
cout << "----------------------------------------" << endl;
//--------------------------------------------------------------------
// Random matrix A. Rather than pfaffing around with classes
// provided by VTK I just used a standard C (not C++) 3x3 matrix
// since it's easy.
// Create random matrix.
double val;
double A[3][3];
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
val = vtkMath::Gaussian(0.0,1.0);
A[i][j] = val;
}
}
cout << "A = " << endl;
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
cout << A[i][j] << " ";
}
cout << A[i][2] << endl;
}
//-----------------------------------------------------------------
// Compute SVD of A
double U[3][3];
double VT[3][3];
double sigv[3];
vtkMath::SingularValueDecomposition3x3 (A, U, sigv, VT);
// Sort singular values in decreasing order.
// Note that VTK uses a funky method to compute the SVD and
// the singular values are not necesarily positive. Therefore,
// take the abs first.
std::vector<double> sig(3);
for (int i=0; i<3; i++) sig[i] = std::abs(sigv[i]);
std::sort(sig.begin(), sig.end(), std::greater<double>());
//cout << "sig = " << sig[0] << sig[1] << sig[2] << endl;
std::stringstream out1;
out1 << std::setprecision(3) << "sig1 = " << sig[0];
std::stringstream out2;
out2 << std::setprecision(3) << ", sig2 = " << sig[1];
std::stringstream out3;
out3 << std::setprecision(3) << ", sig3 = " << sig[2];
//std::string txt1 = "sig1 = " + std::to_string(sig[0]);
//std::string txt2 = "sig2 = " + std::to_string(sig[1]);
//std::string txt3 = "sig3 = " + std::to_string(sig[2]);
//std::string txt = txt1 + ", " + txt2 + ", " +txt3;
std::string txt = out1.str()+out2.str()+out3.str();
cout << txt << endl;
//--------------------------------------------------------------------
// Create ellipsoid by multiplying unit ball by A. I call
// the result "ellipse".
// Get points in unit ball, multiply by A and put result into new sphere object.
vtkSmartPointer<vtkPoints> sphPts = vtkSmartPointer<vtkPoints>::New();
sphPts = unitBall->GetOutput()->GetPoints();
int N = sphPts->GetNumberOfPoints();
//cout << "Number of points in unit ball N = " << N << endl;
// Input and output vectors for transform.
double x[3]; // Input vector to transformation
double y[3]; // Output vector from transformation
// Geometric pieces of the new ellipsoid.
vtkSmartPointer<vtkPoints> ellipsePts = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> ellipseVerts = vtkSmartPointer<vtkCellArray>::New();
// The topology (mesh) of the ellipsoid is the same as the unit ball. Use
// the polygons in the unit ball as the polygons in the new ellipsoid.
vtkSmartPointer<vtkCellArray> sphPolys = vtkSmartPointer<vtkCellArray>::New();
sphPolys = unitBall->GetOutput()->GetPolys();
// Do the multiplication y = A*x
double s;
double ymax = 0; // I use this to scale the actors when rendering.
for (int i=0; i<N; i++) {
sphPts->GetPoint(i, x);
//cout << x[0] << ", " << x[1] << ", " << x[2] << endl;
// Naive gemv
for (int j=0; j<3; j++) {
s = 0;
for (int k=0; k<3; k++) {
s += A[j][k]*x[k];
}
y[j] = s;
if (s > ymax) {
ymax = s;
}
}
//cout << y[0] << ", " << y[1] << ", " << y[2] << endl;
//cout << "------------------" << endl;
// Now put y into the new VTK object
vtkIdType pid;
pid = ellipsePts->InsertNextPoint(y[0], y[1], y[2]);
ellipseVerts->InsertNextCell( 1, &pid );
}
// Create a polydata object and add to it: points, verts, and polys.
// If I don't add the polys then the plot only shows vertices, no
// surface.
vtkSmartPointer<vtkPolyData> ellipseData =
vtkSmartPointer<vtkPolyData>::New();
ellipseData->SetPoints(ellipsePts);
ellipseData->SetVerts (ellipseVerts);
ellipseData->SetPolys(sphPolys); // Reuse unit ball's polygon topology
//-------------------------------------------------------------
// Visualization stuff for ellipse
// Create mapper for ellipse
vtkSmartPointer<vtkPolyDataMapper> ellipseMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
ellipseMapper->SetInputData(ellipseData);
ellipseMapper->ScalarVisibilityOn();
// Create actor for ellipse
vtkNew<vtkActor> ellipseActor;
ellipseActor->SetMapper(ellipseMapper);
ellipseActor->GetProperty()->EdgeVisibilityOn();
ellipseActor->GetProperty()->SetColor(actorColorRed.GetData());
ellipseActor->GetProperty()->SetEdgeColor(EdgeColour.GetData());
ellipseActor->GetProperty()->SetOpacity(0.8);
ellipseActor->GetProperty()->SetPointSize(1);
//-------------------------------------------------------------
// Text annotation
vtkSmartPointer<vtkTextActor> textActor =
vtkSmartPointer<vtkTextActor>::New();
// txt is generated above -- the three sing. values.
std::string legend = "Action of matrix on unit ball. "+txt;
textActor->SetInput(legend.c_str());
textActor->GetProperty()->SetColor(textColor.GetData());
textActor->SetPosition2(50, 50);
textActor->GetTextProperty()->SetFontSize(24);
// add the unit ball actor to the renderer
double mag = 1.0/(2.2*(ymax+1.0));
unitBallActor->SetScale(mag, mag, mag);
renderer->AddActor(unitBallActor);
// add the generated ellipse actor to the renderer
ellipseActor->SetScale(mag, mag, mag);
renderer->AddActor(ellipseActor);
// Add the text to the renderer
renderer->AddActor2D(textActor);
// Start the event loop by telling the render window to render
// and the interactor to start. First reset view position.
renderer->GetActiveCamera()->SetPosition(initPos);
renderer->GetActiveCamera()->SetFocalPoint(initFP);
renderer->GetActiveCamera()->SetViewUp(initViewUp);
renderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
// If we get here, it's because an event happened which caused
// the interactor to stop. Check if it's an 'r' key for refresh
// or if some other even happened, in case we should quit.
if(KeyEvent.RKeyPressed()) {
//cout << "R key noticed in main" << endl;
renderer->RemoveActor(ellipseActor);
renderer->RemoveActor2D(textActor);
} else {
//cout << "Back in main, no R key ... must be time to quit." << endl;
return EXIT_SUCCESS;
}
} // while(1)
}