Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
vtkTensorGlyphColor.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkTensorGlyphColor.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
15#include "vtkTensorGlyphColor.h"
16
17#include "vtkStreamingDemandDrivenPipeline.h"
18#include "vtkCell.h"
19#include "vtkCellArray.h"
20#include "vtkDataSet.h"
21#include "vtkExecutive.h"
22#include "vtkFloatArray.h"
23#include "vtkDoubleArray.h"
24#include "vtkMath.h"
25#include "vtkInformation.h"
26#include "vtkInformationVector.h"
27#include "vtkObjectFactory.h"
28#include "vtkPointData.h"
29#include "vtkPolyData.h"
30#include "vtkTransform.h"
31
33
34// Construct object with scaling on and scale factor 1.0. Eigenvalues are
35// extracted, glyphs are colored with input scalar data, and logarithmic
36// scaling is turned off.
38{
39 this->Scaling = 1;
40 this->ScaleFactor = 1.0;
41 this->ExtractEigenvalues = 1;
42 this->ColorGlyphs = 1;
44 this->ClampScaling = 0;
45 this->MaxScaleFactor = 100;
46 this->ThreeGlyphs = 0;
47 this->Symmetric = 0;
48 this->Length = 1.0;
49
50 this->SetNumberOfInputPorts(2);
51
52 // by default, process active point tensors
53 this->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS,
54 vtkDataSetAttributes::TENSORS);
55
56 // by default, process active point scalars
57 this->SetInputArrayToProcess(1, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS,
58 vtkDataSetAttributes::SCALARS);
59}
60
61//----------------------------------------------------------------------------
63
64//----------------------------------------------------------------------------
66 vtkInformation *vtkNotUsed(request),
67 vtkInformationVector **inputVector,
68 vtkInformationVector *outputVector)
69{
70 // get the info objects
71 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
72 vtkInformation *sourceInfo = inputVector[1]->GetInformationObject(0);
73 vtkInformation *outInfo = outputVector->GetInformationObject(0);
74
75 if (sourceInfo)
76 {
77 sourceInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER(),
78 0);
79 sourceInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES(),
80 1);
81 sourceInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS(),
82 0);
83 }
84
85 inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER(),
86 outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()));
87 inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES(),
88 outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES()));
89 inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS(),
90 outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS()));
91 inInfo->Set(vtkStreamingDemandDrivenPipeline::EXACT_EXTENT(), 1);
92
93 return 1;
94}
95
96//----------------------------------------------------------------------------
98 vtkInformation *vtkNotUsed(request),
99 vtkInformationVector **inputVector,
100 vtkInformationVector *outputVector)
101{
102 // get the info objects
103 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
104 vtkInformation *sourceInfo = inputVector[1]->GetInformationObject(0);
105 vtkInformation *outInfo = outputVector->GetInformationObject(0);
106
107 // get the input and output
108 vtkDataSet *input = vtkDataSet::SafeDownCast(
109 inInfo->Get(vtkDataObject::DATA_OBJECT()));
110 vtkPolyData *source = vtkPolyData::SafeDownCast(
111 sourceInfo->Get(vtkDataObject::DATA_OBJECT()));
112 vtkPolyData *output = vtkPolyData::SafeDownCast(
113 outInfo->Get(vtkDataObject::DATA_OBJECT()));
114
115 vtkDataArray *inTensors;
116 double tensor[9];
117 vtkDataArray *inScalars;
118 vtkIdType numPts, numSourcePts, numSourceCells, inPtId, i;
119 int j;
120 vtkPoints *sourcePts;
121 vtkDataArray *sourceNormals;
122 vtkCellArray *sourceCells, *cells;
123 vtkPoints *newPts;
124 vtkFloatArray *newScalars=nullptr;
125 vtkFloatArray *newNormals=nullptr;
126 double x[3], s;
127 vtkTransform *trans;
128 vtkCell *cell;
129 vtkIdList *cellPts;
130 int npts;
131 vtkIdType *pts;
132 vtkIdType ptIncr, cellId;
133 vtkIdType subIncr;
134 int numDirs, dir, eigen_dir, symmetric_dir;
135 vtkMatrix4x4 *matrix;
136 double *m[3], w[3], *v[3];
137 double m0[3], m1[3], m2[3];
138 double v0[3], v1[3], v2[3];
139 double xv[3], yv[3], zv[3];
140 double maxScale;
141
142 numDirs = (this->ThreeGlyphs?3:1)*(this->Symmetric+1);
143
144 // set up working matrices
145 m[0] = m0; m[1] = m1; m[2] = m2;
146 v[0] = v0; v[1] = v1; v[2] = v2;
147
148 vtkDebugMacro(<<"Generating tensor glyphs");
149
150 vtkPointData *outPD = output->GetPointData();
151 inTensors = this->GetInputArrayToProcess(0, inputVector);
152 inScalars = this->GetInputArrayToProcess(1, inputVector);
153 numPts = input->GetNumberOfPoints();
154
155 if ( !inTensors || numPts < 1 )
156 {
157 vtkErrorMacro(<<"No data to glyph!");
158 return 1;
159 }
160
161 pts = new vtkIdType[source->GetMaxCellSize()];
162 trans = vtkTransform::New();
163 matrix = vtkMatrix4x4::New();
164
165 //
166 // Allocate storage for output PolyData
167 //
168 sourcePts = source->GetPoints();
169 numSourcePts = sourcePts->GetNumberOfPoints();
170 numSourceCells = source->GetNumberOfCells();
171
172 newPts = vtkPoints::New();
173 newPts->Allocate(numDirs*numPts*numSourcePts);
174
175 // Setting up for calls to PolyData::InsertNextCell()
176 if ( (sourceCells=source->GetVerts())->GetNumberOfCells() > 0 )
177 {
178 cells = vtkCellArray::New();
179 cells->Allocate(numDirs*numPts*sourceCells->GetSize());
180 output->SetVerts(cells);
181 cells->Delete();
182 }
183 if ( (sourceCells=this->GetSource()->GetLines())->GetNumberOfCells() > 0 )
184 {
185 cells = vtkCellArray::New();
186 cells->Allocate(numDirs*numPts*sourceCells->GetSize());
187 output->SetLines(cells);
188 cells->Delete();
189 }
190 if ( (sourceCells=this->GetSource()->GetPolys())->GetNumberOfCells() > 0 )
191 {
192 cells = vtkCellArray::New();
193 cells->Allocate(numDirs*numPts*sourceCells->GetSize());
194 output->SetPolys(cells);
195 cells->Delete();
196 }
197 if ( (sourceCells=this->GetSource()->GetStrips())->GetNumberOfCells() > 0 )
198 {
199 cells = vtkCellArray::New();
200 cells->Allocate(numDirs*numPts*sourceCells->GetSize());
201 output->SetStrips(cells);
202 cells->Delete();
203 }
204
205 // only copy scalar data through
206 vtkPointData *pd = this->GetSource()->GetPointData();
207 // generate scalars if eigenvalues are chosen or if scalars exist.
208 if (this->ColorGlyphs &&
209 ((this->ColorMode == COLOR_BY_EIGENVALUES) ||
210 (inScalars && (this->ColorMode == COLOR_BY_SCALARS)) ) )
211 {
212 newScalars = vtkFloatArray::New();
213 newScalars->SetNumberOfComponents(4);
214 newScalars->Allocate(numDirs*numPts*numSourcePts);
215 if (this->ColorMode == COLOR_BY_EIGENVALUES)
216 {
217 newScalars->SetName("MaxEigenvalue");
218 }
219 else
220 {
221 newScalars->SetName(inScalars->GetName());
222 }
223 }
224 else
225 {
226 outPD->CopyAllOff();
227 outPD->CopyScalarsOn();
228 outPD->CopyAllocate(pd,numDirs*numPts*numSourcePts);
229 }
230 if ( (sourceNormals = pd->GetNormals()) )
231 {
232 newNormals = vtkFloatArray::New();
233 newNormals->SetNumberOfComponents(3);
234 newNormals->SetName("Normals");
235 newNormals->Allocate(numDirs*3*numPts*numSourcePts);
236 }
237 //
238 // First copy all topology (transformation independent)
239 //
240 for (inPtId=0; inPtId < numPts; inPtId++)
241 {
242 ptIncr = numDirs * inPtId * numSourcePts;
243 for (cellId=0; cellId < numSourceCells; cellId++)
244 {
245 cell = this->GetSource()->GetCell(cellId);
246 cellPts = cell->GetPointIds();
247 npts = (int)cellPts->GetNumberOfIds();
248 for (dir=0; dir < numDirs; dir++)
249 {
250 // This variable may be removed, but that
251 // will not improve readability
252 subIncr = ptIncr + dir*numSourcePts;
253 for (i=0; i < npts; i++)
254 {
255 pts[i] = cellPts->GetId(i) + subIncr;
256 }
257 output->InsertNextCell(cell->GetCellType(),npts,pts);
258 }
259 }
260 }
261 //
262 // Traverse all Input points, transforming glyph at Source points
263 //
264 trans->PreMultiply();
265
266 for (inPtId=0; inPtId < numPts; inPtId++)
267 {
268 ptIncr = numDirs * inPtId * numSourcePts;
269
270 // Translation is postponed
271 // Symmetric tensor support
272 inTensors->GetTuple(inPtId, tensor);
273 if (inTensors->GetNumberOfComponents() == 6)
274 {
275 vtkMath::TensorFromSymmetricTensor(tensor);
276 }
277
278 // compute orientation vectors and scale factors from tensor
279 if ( this->ExtractEigenvalues ) // extract appropriate eigenfunctions
280 {
281 // We are interested in the symmetrical part of the tensor only, since
282 // eigenvalues are real if and only if the matrice of reals is symmetrical
283 for (j=0; j<3; j++)
284 {
285 for (i=0; i<3; i++)
286 {
287 m[i][j] = 0.5 * (tensor[i + 3 * j] + tensor[j + 3 * i]);
288 }
289 }
290 vtkMath::Jacobi(m, w, v);
291
292 //copy eigenvectors
293 xv[0] = v[0][0]; xv[1] = v[1][0]; xv[2] = v[2][0];
294 yv[0] = v[0][1]; yv[1] = v[1][1]; yv[2] = v[2][1];
295 zv[0] = v[0][2]; zv[1] = v[1][2]; zv[2] = v[2][2];
296 }
297 else //use tensor columns as eigenvectors
298 {
299 for (i=0; i<3; i++)
300 {
301 xv[i] = tensor[i];
302 yv[i] = tensor[i+3];
303 zv[i] = tensor[i+6];
304 }
305 w[0] = vtkMath::Normalize(xv);
306 w[1] = vtkMath::Normalize(yv);
307 w[2] = vtkMath::Normalize(zv);
308 }
309
310 // compute scale factors
311 w[0] *= this->ScaleFactor;
312 w[1] *= this->ScaleFactor;
313 w[2] *= this->ScaleFactor;
314
315 if ( this->ClampScaling )
316 {
317 for (maxScale=0.0, i=0; i<3; i++)
318 {
319 if ( maxScale < fabs(w[i]) )
320 {
321 maxScale = fabs(w[i]);
322 }
323 }
324 if ( maxScale > this->MaxScaleFactor )
325 {
326 maxScale = this->MaxScaleFactor / maxScale;
327 for (i=0; i<3; i++)
328 {
329 w[i] *= maxScale; //preserve overall shape of glyph
330 }
331 }
332 }
333
334 // normalization is postponed
335
336 // make sure scale is okay (non-zero) and scale data
337 for (maxScale=0.0, i=0; i<3; i++)
338 {
339 if ( w[i] > maxScale )
340 {
341 maxScale = w[i];
342 }
343 }
344 if ( maxScale == 0.0 )
345 {
346 maxScale = 1.0;
347 }
348 for (i=0; i<3; i++)
349 {
350 if ( w[i] == 0.0 )
351 {
352 w[i] = maxScale * 1.0e-06;
353 }
354 }
355
356 // Now do the real work for each "direction"
357
358 for (dir=0; dir < numDirs; dir++)
359 {
360 eigen_dir = dir%(this->ThreeGlyphs?3:1);
361 symmetric_dir = dir/(this->ThreeGlyphs?3:1);
362
363 // Remove previous scales ...
364 trans->Identity();
365
366 // translate Source to Input point
367 input->GetPoint(inPtId, x);
368 trans->Translate(x[0], x[1], x[2]);
369
370 // normalized eigenvectors rotate object for eigen direction 0
371 matrix->Element[0][0] = xv[0];
372 matrix->Element[0][1] = yv[0];
373 matrix->Element[0][2] = zv[0];
374 matrix->Element[1][0] = xv[1];
375 matrix->Element[1][1] = yv[1];
376 matrix->Element[1][2] = zv[1];
377 matrix->Element[2][0] = xv[2];
378 matrix->Element[2][1] = yv[2];
379 matrix->Element[2][2] = zv[2];
380 trans->Concatenate(matrix);
381
382 if (eigen_dir == 1)
383 {
384 trans->RotateZ(90.0);
385 }
386
387 if (eigen_dir == 2)
388 {
389 trans->RotateY(-90.0);
390 }
391
392 if (this->ThreeGlyphs)
393 {
394 trans->Scale(w[eigen_dir], this->ScaleFactor, this->ScaleFactor);
395 }
396 else
397 {
398 trans->Scale(w[0], w[1], w[2]);
399 }
400
401 // Mirror second set to the symmetric position
402 if (symmetric_dir == 1)
403 {
404 trans->Scale(-1.,1.,1.);
405 }
406
407 // if the eigenvalue is negative, shift to reverse direction.
408 // The && is there to ensure that we do not change the
409 // old behaviour of vtkTensorGlyphColors (which only used one dir),
410 // in case there is an oriented glyph, e.g. an arrow.
411 if (w[eigen_dir] < 0 && numDirs > 1)
412 {
413 trans->Translate(-this->Length, 0., 0.);
414 }
415
416 // multiply points (and normals if available) by resulting
417 // matrix
418 trans->TransformPoints(sourcePts,newPts);
419
420 // Apply the transformation to a series of points,
421 // and append the results to outPts.
422 if ( newNormals )
423 {
424 // a negative determinant means the transform turns the
425 // glyph surface inside out, and its surface normals all
426 // point inward. The following scale corrects the surface
427 // normals to point outward.
428 if (trans->GetMatrix()->Determinant() < 0)
429 {
430 trans->Scale(-1.0,-1.0,-1.0);
431 }
432 trans->TransformNormals(sourceNormals,newNormals);
433 }
434
435 // Copy point data from source
436 if ( this->ColorGlyphs && inScalars &&
437 (this->ColorMode == COLOR_BY_SCALARS) )
438 {
439 for (i=0; i < numSourcePts; i++)
440 {
441 auto st = inScalars->GetTuple(inPtId);
442 newScalars->InsertTuple4(ptIncr+i,st[0],st[1],st[2],st[3]);
443 }
444 }
445 else if (this->ColorGlyphs &&
447 {
448 // If ThreeGlyphs is false we use the first (largest)
449 // eigenvalue as scalar.
450 s = w[eigen_dir];
451 for (i=0; i < numSourcePts; i++)
452 {
453 newScalars->InsertTuple(ptIncr+i, &s);
454 }
455 }
456 else
457 {
458 for (i=0; i < numSourcePts; i++)
459 {
460 outPD->CopyData(pd,i,ptIncr+i);
461 }
462 }
463 ptIncr += numSourcePts;
464 }
465 }
466 vtkDebugMacro(<<"Generated " << numPts <<" tensor glyphs");
467 //
468 // Update output and release memory
469 //
470 delete [] pts;
471
472 output->SetPoints(newPts);
473 newPts->Delete();
474
475 if ( newScalars )
476 {
477 int idx = outPD->AddArray(newScalars);
478 outPD->SetActiveAttribute(idx, vtkDataSetAttributes::SCALARS);
479 newScalars->Delete();
480 }
481
482 if ( newNormals )
483 {
484 outPD->SetNormals(newNormals);
485 newNormals->Delete();
486 }
487
488 output->Squeeze();
489 trans->Delete();
490 matrix->Delete();
491
492 return 1;
493}
494
495//----------------------------------------------------------------------------
496void vtkTensorGlyphColor::SetSourceConnection(int id, vtkAlgorithmOutput* algOutput)
497{
498 if (id < 0)
499 {
500 vtkErrorMacro("Bad index " << id << " for source.");
501 return;
502 }
503
504 int numConnections = this->GetNumberOfInputConnections(1);
505 if (id < numConnections)
506 {
507 this->SetNthInputConnection(1, id, algOutput);
508 }
509 else if (id == numConnections && algOutput)
510 {
511 this->AddInputConnection(1, algOutput);
512 }
513 else if (algOutput)
514 {
515 vtkWarningMacro("The source id provided is larger than the maximum "
516 "source id, using " << numConnections << " instead.");
517 this->AddInputConnection(1, algOutput);
518 }
519}
520
521//----------------------------------------------------------------------------
522void vtkTensorGlyphColor::SetSourceData(vtkPolyData *source)
523{
524 this->SetInputData(1, source);
525}
526
527//----------------------------------------------------------------------------
529{
530 if (this->GetNumberOfInputConnections(1) < 1)
531 {
532 return nullptr;
533 }
534 return vtkPolyData::SafeDownCast(this->GetExecutive()->GetInputData(1, 0));
535}
536
537//----------------------------------------------------------------------------
538int vtkTensorGlyphColor::FillInputPortInformation(int port, vtkInformation *info)
539{
540 if (port == 1)
541 {
542 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
543 return 1;
544 }
545 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
546 return 1;
547}
548
549//----------------------------------------------------------------------------
550void vtkTensorGlyphColor::PrintSelf(ostream& os, vtkIndent indent)
551{
552 this->Superclass::PrintSelf(os,indent);
553
554 os << indent << "Source: " << this->GetSource() << "\n";
555 os << indent << "Scaling: " << (this->Scaling ? "On\n" : "Off\n");
556 os << indent << "Scale Factor: " << this->ScaleFactor << "\n";
557 os << indent << "Extract Eigenvalues: " << (this->ExtractEigenvalues ? "On\n" : "Off\n");
558 os << indent << "Color Glyphs: " << (this->ColorGlyphs ? "On\n" : "Off\n");
559 os << indent << "Color Mode: " << this->ColorMode << endl;
560 os << indent << "Clamp Scaling: " << (this->ClampScaling ? "On\n" : "Off\n");
561 os << indent << "Max Scale Factor: " << this->MaxScaleFactor << "\n";
562 os << indent << "Three Glyphs: " << (this->ThreeGlyphs ? "On\n" : "Off\n");
563 os << indent << "Symmetric: " << (this->Symmetric ? "On\n" : "Off\n");
564 os << indent << "Length: " << this->Length << "\n";
565}
scale and orient glyph(s) according to eigenvalues and eigenvectors of symmetrical part of tensor
int FillInputPortInformation(int port, vtkInformation *info) override
~vtkTensorGlyphColor() override
void SetSourceData(vtkPolyData *source)
int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
int RequestUpdateExtent(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void SetSourceConnection(int id, vtkAlgorithmOutput *algOutput)
vtkStandardNewMacro(vtkTensorGlyphColor) vtkTensorGlyphColor