forked from dgobbi/ToolCursor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkGeometricCursorShapes.cxx
381 lines (319 loc) · 9.79 KB
/
vtkGeometricCursorShapes.cxx
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*=========================================================================
Program: ToolCursor
Module: vtkGeometricCursorShapes.cxx
Copyright (c) 2010 David Gobbi
All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkGeometricCursorShapes.h"
#include "vtkObjectFactory.h"
#include "vtkToolCursor.h"
#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkPointData.h"
#include "vtkMath.h"
vtkStandardNewMacro(vtkGeometricCursorShapes);
//----------------------------------------------------------------------------
vtkGeometricCursorShapes::vtkGeometricCursorShapes()
{
this->MakeShapes();
}
//----------------------------------------------------------------------------
vtkGeometricCursorShapes::~vtkGeometricCursorShapes()
{
}
//----------------------------------------------------------------------------
void vtkGeometricCursorShapes::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//----------------------------------------------------------------------------
void vtkGeometricCursorShapes::MakeShapes()
{
vtkDataSet *data;
data = this->MakeCrossShape(0);
this->AddShape("Cross", data, 0);
data->Delete();
data = this->MakeCrossShape(1);
this->AddShape("SplitCross", data, 0);
data->Delete();
data = this->MakeSphereShape(0);
this->AddShape("Sphere", data, 0);
data->Delete();
data = this->MakeSphereShape(1);
this->AddShape("SplitSphere", data, 0);
data->Delete();
data = this->MakeConeShape(0);
this->AddShape("Cone", data, 0);
data->Delete();
data = this->MakeConeShape(1);
this->AddShape("DualCone", data, 0);
data->Delete();
}
//----------------------------------------------------------------------------
vtkDataSet *vtkGeometricCursorShapes::MakeCrossShape(int dual)
{
double radius = 10.0;
double inner = 3.5;
double thickness = 2.0;
double xmin = inner;
double xmax = radius;
double ymin = -thickness*0.5;
double ymax = +thickness*0.5;
double zmin = 0;
double zmax = thickness*0.5;
vtkIntArray *scalars = vtkIntArray::New();
vtkPoints *points = vtkPoints::New();
vtkCellArray *strips = vtkCellArray::New();
vtkIdType nPoints = 0;
int colorIndex = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
double z = zmax;
for (int k = 0; k < 2; k++)
{
points->InsertNextPoint(xmin, ymin, z);
points->InsertNextPoint(xmin, ymax, z);
points->InsertNextPoint(xmax, ymax, z);
points->InsertNextPoint(xmax, ymin, z);
scalars->InsertNextTupleValue(&colorIndex);
scalars->InsertNextTupleValue(&colorIndex);
scalars->InsertNextTupleValue(&colorIndex);
scalars->InsertNextTupleValue(&colorIndex);
z = zmin;
}
static vtkIdType rectIds[5][4] = { { 1, 0, 2, 3 },
{ 4, 0, 5, 1 },
{ 5, 1, 6, 2 },
{ 6, 2, 7, 3 },
{ 7, 3, 4, 0 } };
vtkIdType pointIds[4];
for (int ii = 0; ii < 5; ii++)
{
for (int jj = 0; jj < 4; jj++)
{
pointIds[jj] = rectIds[ii][jj]+nPoints;
}
strips->InsertNextCell(4, pointIds);
}
nPoints += 8;
// do a rotation of 90 degrees for next piece
double tmp1 = ymin;
double tmp2 = ymax;
ymin = -xmax;
ymax = -xmin;
xmin = tmp1;
xmax = tmp2;
}
// do the other side
zmin = -zmin;
zmax = -zmax;
xmin = -xmin;
xmax = -xmax;
if (dual)
{
colorIndex = 1;
}
}
vtkPolyData *data = vtkPolyData::New();
data->SetPoints(points);
points->Delete();
data->SetStrips(strips);
strips->Delete();
data->GetPointData()->SetScalars(scalars);
scalars->Delete();
return data;
}
//----------------------------------------------------------------------------
vtkDataSet *vtkGeometricCursorShapes::MakeSphereShape(int dual)
{
double pi = vtkMath::Pi();
double radius = 5.0;
int resolution = 9;
vtkIdType *pointIds = new vtkIdType[4*(resolution+1)];
vtkIntArray *scalars = vtkIntArray::New();
vtkDoubleArray *normals = vtkDoubleArray::New();
normals->SetNumberOfComponents(3);
vtkPoints *points = vtkPoints::New();
vtkCellArray *strips = vtkCellArray::New();
vtkIdType nPoints = 0;
int colorIndex = 0;
for (int i = 0; i < 2; i++)
{
// The sign (i.e. for top or bottom) is stored in s
double s = 1 - 2*i;
// The unit position vector of the point is stored in v
double v[3];
v[0] = 0;
v[1] = 0;
v[2] = s;
points->InsertNextPoint(radius*v[0], radius*v[1], radius*v[2]);
normals->InsertNextTupleValue(v);
scalars->InsertNextTupleValue(&colorIndex);
int n = (resolution + 1)/2;
int m = 2*resolution;
for (int j = 1; j <= n; j++)
{
double phi = pi*j/resolution;
double r = sin(phi);
v[2] = cos(phi)*s;
if (2*j >= resolution)
{
v[2] = 0;
}
for (int k = 0; k < m; k++)
{
double theta = pi*k/resolution;
v[0] = r*cos(theta);
v[1] = r*sin(theta)*s;
points->InsertNextPoint(radius*v[0], radius*v[1], radius*v[2]);
normals->InsertNextTupleValue(v);
scalars->InsertNextTupleValue(&colorIndex);
}
}
// Make the fan for the top
pointIds[0] = nPoints++;
for (int ii = 0; ii < (m-1); ii++)
{
pointIds[1] = nPoints + ii;
pointIds[2] = nPoints + ii + 1;
strips->InsertNextCell(3, pointIds);
}
pointIds[1] = nPoints + m - 1;
pointIds[2] = nPoints;
strips->InsertNextCell(3, pointIds);
// Make the strips for the rest
for (int jj = 1; jj < n; jj++)
{
for (int kk = 0; kk < m; kk++)
{
pointIds[2*kk] = nPoints + kk;
pointIds[2*kk+1] = nPoints + kk + m;
}
pointIds[2*m] = nPoints;
pointIds[2*m+1] = nPoints + m;
strips->InsertNextCell(2*(m+1), pointIds);
nPoints += m;
}
nPoints += m;
if (dual)
{
colorIndex = 1;
}
}
delete [] pointIds;
vtkPolyData *data = vtkPolyData::New();
data->SetPoints(points);
points->Delete();
data->SetStrips(strips);
strips->Delete();
data->GetPointData()->SetScalars(scalars);
scalars->Delete();
data->GetPointData()->SetNormals(normals);
normals->Delete();
return data;
}
//----------------------------------------------------------------------------
vtkDataSet *vtkGeometricCursorShapes::MakeConeShape(int dual)
{
double pi = vtkMath::Pi();
double radius = 8.0;
double height = 15.0;
int resolution = 20;
vtkIdType *pointIds = new vtkIdType[2*(resolution+1)];
vtkIntArray *scalars = vtkIntArray::New();
vtkDoubleArray *normals = vtkDoubleArray::New();
normals->SetNumberOfComponents(3);
vtkPoints *points = vtkPoints::New();
vtkCellArray *strips = vtkCellArray::New();
vtkIdType nPoints = 0;
int sides = (dual ? 2 : 1);
for (int colorIndex = 0; colorIndex < sides; colorIndex++)
{
// The sign (i.e. for top or bottom) is stored in s
double s = 1 - 2*colorIndex;
// The length of the side of the cone
double l = sqrt(radius*radius + height*height);
double f1 = radius/l;
double f2 = height/l;
// The unit normal vector
double v[3];
// The point of the cone
for (int i = 0; i < 2; i++)
{
double r = radius*i;
double z = height*i;
double offset = 0.5*(1 - i);
for (int j = 0; j < resolution; j++)
{
double theta = 2*pi*(j + offset)/resolution;
double ct = cos(theta);
double st = sin(theta);
v[0] = f2*ct;
v[1] = f2*st*s;
v[2] = -f1*s;
points->InsertNextPoint(r*ct, r*st*s, z*s);
normals->InsertNextTupleValue(v);
scalars->InsertNextTupleValue(&colorIndex);
}
}
// The base of the cone
v[0] = 0;
v[1] = 0;
v[2] = s;
points->InsertNextPoint(0, 0, height*s);
normals->InsertNextTupleValue(v);
scalars->InsertNextTupleValue(&colorIndex);
for (int k = 0; k < resolution; k++)
{
double theta = 2*pi*k/resolution;
points->InsertNextPoint(radius*cos(theta), radius*sin(theta)*s, height*s);
normals->InsertNextTupleValue(v);
scalars->InsertNextTupleValue(&colorIndex);
}
// Make the fan for the top
for (int ii = 0; ii < (resolution-1); ii++)
{
pointIds[0] = nPoints + ii;
pointIds[1] = nPoints + ii + resolution + 1;
pointIds[2] = nPoints + ii + resolution;
strips->InsertNextCell(3, pointIds);
}
pointIds[0] = nPoints + 2*resolution - 1;
pointIds[1] = nPoints + resolution - 1;
pointIds[2] = nPoints + resolution;
strips->InsertNextCell(3, pointIds);
nPoints += 2*resolution;
// Make the fan for the base
pointIds[0] = nPoints++;
for (int jj = 0; jj < (resolution-1); jj++)
{
pointIds[1] = nPoints + jj;
pointIds[2] = nPoints + jj + 1;
strips->InsertNextCell(3, pointIds);
}
pointIds[1] = nPoints + resolution - 1;
pointIds[2] = nPoints;
strips->InsertNextCell(3, pointIds);
nPoints += resolution;
}
delete [] pointIds;
vtkPolyData *data = vtkPolyData::New();
data->SetPoints(points);
points->Delete();
data->SetStrips(strips);
strips->Delete();
data->GetPointData()->SetScalars(scalars);
scalars->Delete();
data->GetPointData()->SetNormals(normals);
normals->Delete();
return data;
}