1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #include "vvGlyph2D.h"
21 #include "vtkDataSet.h"
22 #include "vtkFloatArray.h"
23 #include "vtkIdList.h"
24 #include "vtkIdTypeArray.h"
25 #include "vtkInformation.h"
26 #include "vtkInformationVector.h"
28 #include "vtkObjectFactory.h"
29 #include "vtkPointData.h"
30 #include "vtkPolyData.h"
31 #include "vtkStreamingDemandDrivenPipeline.h"
32 #include "vtkTransform.h"
33 #include "vtkUnsignedCharArray.h"
35 vtkCxxRevisionMacro(vvGlyph2D, "DummyRevision");
36 vtkStandardNewMacro(vvGlyph2D);
38 vvGlyph2D::vvGlyph2D()
46 //----------------------------------------------------------------------------
47 int vvGlyph2D::RequestData(
48 vtkInformation *vtkNotUsed(request),
49 vtkInformationVector **inputVector,
50 vtkInformationVector *outputVector)
52 // get the info objects
53 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
54 vtkInformation *outInfo = outputVector->GetInformationObject(0);
56 // get the input and ouptut
57 vtkDataSet *input = vtkDataSet::SafeDownCast(
58 inInfo->Get(vtkDataObject::DATA_OBJECT()));
59 vtkPolyData *output = vtkPolyData::SafeDownCast(
60 outInfo->Get(vtkDataObject::DATA_OBJECT()));
63 vtkDataArray *inSScalars; // Scalars for Scaling
64 vtkDataArray *inCScalars; // Scalars for Coloring
65 vtkDataArray *inVectors;
66 int requestedGhostLevel;
67 unsigned char* inGhostLevels=0;
68 vtkDataArray *inNormals, *sourceNormals = NULL;
69 vtkDataArray *sourceTCoords = NULL;
70 vtkIdType numPts, numSourcePts, numSourceCells, inPtId, i;
72 vtkPoints *sourcePts = NULL;
74 vtkDataArray *newScalars=NULL;
75 vtkDataArray *newVectors=NULL;
76 vtkDataArray *newNormals=NULL;
77 vtkDataArray *newTCoords = NULL;
78 double x[3], v[3], vNew[3], s = 0.0, vMag = 0.0, value, tc[3];
79 vtkTransform *trans = vtkTransform::New();
84 vtkIdType ptIncr, cellId;
85 int haveVectors, haveNormals, haveTCoords = 0;
86 double scalex,scaley,scalez, den;
87 vtkPointData *outputPD = output->GetPointData();
88 int numberOfSources = this->GetNumberOfInputConnections(1);
89 vtkPolyData *defaultSource = NULL;
90 vtkIdTypeArray *pointIds=0;
91 vtkPolyData *source = 0;
93 vtkDebugMacro(<<"Generating glyphs");
95 pts = vtkIdList::New();
96 pts->Allocate(VTK_CELL_SIZE);
98 pd = input->GetPointData();
99 inSScalars = this->GetInputArrayToProcess(0,inputVector);
100 inVectors = this->GetInputArrayToProcess(1,inputVector);
101 inNormals = this->GetInputArrayToProcess(2,inputVector);
102 inCScalars = this->GetInputArrayToProcess(3,inputVector);
103 if (inCScalars == NULL)
105 inCScalars = inSScalars;
108 vtkDataArray* temp = 0;
111 temp = pd->GetArray("vtkGhostLevels");
113 if ( (!temp) || (temp->GetDataType() != VTK_UNSIGNED_CHAR)
114 || (temp->GetNumberOfComponents() != 1))
116 vtkDebugMacro("No appropriate ghost levels field available.");
120 inGhostLevels = ((vtkUnsignedCharArray*)temp)->GetPointer(0);
123 requestedGhostLevel =
124 outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS());
126 numPts = input->GetNumberOfPoints();
129 vtkDebugMacro(<<"No points to glyph!");
135 // Check input for consistency
137 if ( (den = this->Range[1] - this->Range[0]) == 0.0 )
141 if ( this->VectorMode != VTK_VECTOR_ROTATION_OFF &&
142 ((this->VectorMode == VTK_USE_VECTOR && inVectors != NULL) ||
143 (this->VectorMode == VTK_USE_NORMAL && inNormals != NULL)) )
152 if ( (this->IndexMode == VTK_INDEXING_BY_SCALAR && !inSScalars) ||
153 (this->IndexMode == VTK_INDEXING_BY_VECTOR &&
154 ((!inVectors && this->VectorMode == VTK_USE_VECTOR) ||
155 (!inNormals && this->VectorMode == VTK_USE_NORMAL))) )
157 if ( this->GetSource(0, inputVector[1]) == NULL )
159 vtkErrorMacro(<<"Indexing on but don't have data to index with");
166 vtkWarningMacro(<<"Turning indexing off: no data to index with");
167 this->IndexMode = VTK_INDEXING_OFF;
171 // Allocate storage for output PolyData
173 outputPD->CopyVectorsOff();
174 outputPD->CopyNormalsOff();
175 outputPD->CopyTCoordsOff();
177 if (!this->GetSource(0, inputVector[1]))
179 defaultSource = vtkPolyData::New();
180 defaultSource->Allocate();
181 vtkPoints *defaultPoints = vtkPoints::New();
182 defaultPoints->Allocate(6);
183 defaultPoints->InsertNextPoint(0, 0, 0);
184 defaultPoints->InsertNextPoint(1, 0, 0);
185 vtkIdType defaultPointIds[2];
186 defaultPointIds[0] = 0;
187 defaultPointIds[1] = 1;
188 defaultSource->SetPoints(defaultPoints);
189 defaultSource->InsertNextCell(VTK_LINE, 2, defaultPointIds);
190 defaultSource->SetUpdateExtent(0, 1, 0);
191 this->SetSource(defaultSource);
192 defaultSource->Delete();
193 defaultSource = NULL;
194 defaultPoints->Delete();
195 defaultPoints = NULL;
198 if ( this->IndexMode != VTK_INDEXING_OFF )
202 for (numSourcePts=numSourceCells=i=0; i < numberOfSources; i++)
204 source = this->GetSource(i, inputVector[1]);
205 if ( source != NULL )
207 if (source->GetNumberOfPoints() > numSourcePts)
209 numSourcePts = source->GetNumberOfPoints();
211 if (source->GetNumberOfCells() > numSourceCells)
213 numSourceCells = source->GetNumberOfCells();
215 if ( !(sourceNormals = source->GetPointData()->GetNormals()) )
224 source = this->GetSource(0, inputVector[1]);
225 sourcePts = source->GetPoints();
226 numSourcePts = sourcePts->GetNumberOfPoints();
227 numSourceCells = source->GetNumberOfCells();
229 sourceNormals = source->GetPointData()->GetNormals();
239 sourceTCoords = source->GetPointData()->GetTCoords();
249 // Prepare to copy output.
250 pd = input->GetPointData();
251 outputPD->CopyAllocate(pd,numPts*numSourcePts);
254 newPts = vtkPoints::New();
255 newPts->Allocate(numPts*numSourcePts);
256 if ( this->GeneratePointIds )
258 pointIds = vtkIdTypeArray::New();
259 pointIds->SetName(this->PointIdsName);
260 pointIds->Allocate(numPts*numSourcePts);
261 outputPD->AddArray(pointIds);
264 if ( this->ColorMode == VTK_COLOR_BY_SCALAR && inCScalars )
266 newScalars = inCScalars->NewInstance();
267 newScalars->SetNumberOfComponents(inCScalars->GetNumberOfComponents());
268 newScalars->Allocate(inCScalars->GetNumberOfComponents()*numPts*numSourcePts);
269 newScalars->SetName(inCScalars->GetName());
271 else if ( (this->ColorMode == VTK_COLOR_BY_SCALE) && inSScalars)
273 newScalars = vtkFloatArray::New();
274 newScalars->Allocate(numPts*numSourcePts);
275 newScalars->SetName("GlyphScale");
276 if (this->ScaleMode == VTK_SCALE_BY_SCALAR)
278 newScalars->SetName(inSScalars->GetName());
281 else if ( (this->ColorMode == VTK_COLOR_BY_VECTOR) && haveVectors)
283 newScalars = vtkFloatArray::New();
284 newScalars->Allocate(numPts*numSourcePts);
285 newScalars->SetName("VectorMagnitude");
289 newVectors = vtkFloatArray::New();
290 newVectors->SetNumberOfComponents(3);
291 newVectors->Allocate(3*numPts*numSourcePts);
292 newVectors->SetName("GlyphVector");
296 newNormals = vtkFloatArray::New();
297 newNormals->SetNumberOfComponents(3);
298 newNormals->Allocate(3*numPts*numSourcePts);
299 newNormals->SetName("Normals");
303 newTCoords = vtkFloatArray::New();
304 int numComps = sourceTCoords->GetNumberOfComponents();
305 newTCoords->SetNumberOfComponents(numComps);
306 newTCoords->Allocate(numComps*numPts*numSourcePts);
307 newTCoords->SetName("TCoords");
310 // Setting up for calls to PolyData::InsertNextCell()
311 if (this->IndexMode != VTK_INDEXING_OFF )
313 output->Allocate(3*numPts*numSourceCells,numPts*numSourceCells);
317 output->Allocate(this->GetSource(0, inputVector[1]),
318 3*numPts*numSourceCells, numPts*numSourceCells);
321 // Traverse all Input points, transforming Source points and copying
325 for (inPtId=0; inPtId < numPts; inPtId++)
327 scalex = scaley = scalez = 1.0;
328 if ( ! (inPtId % 10000) )
330 this->UpdateProgress ((double)inPtId/numPts);
331 if (this->GetAbortExecute())
337 // Get the scalar and vector data
340 s = inSScalars->GetComponent(inPtId, 0);
341 if ( this->ScaleMode == VTK_SCALE_BY_SCALAR ||
342 this->ScaleMode == VTK_DATA_SCALING_OFF )
344 scalex = scaley = scalez = s;
350 if ( this->VectorMode == VTK_USE_NORMAL )
352 inNormals->GetTuple(inPtId, v);
356 inVectors->GetTuple(inPtId, v);
359 vMag = vtkMath::Norm(v);
360 if ( this->ScaleMode == VTK_SCALE_BY_VECTORCOMPONENTS )
366 else if ( this->ScaleMode == VTK_SCALE_BY_VECTOR )
368 scalex = scaley = scalez = vMag;
372 // Clamp data scale if enabled
373 if ( this->Clamping )
375 scalex = (scalex < this->Range[0] ? this->Range[0] :
376 (scalex > this->Range[1] ? this->Range[1] : scalex));
377 scalex = (scalex - this->Range[0]) / den;
378 scaley = (scaley < this->Range[0] ? this->Range[0] :
379 (scaley > this->Range[1] ? this->Range[1] : scaley));
380 scaley = (scaley - this->Range[0]) / den;
381 scalez = (scalez < this->Range[0] ? this->Range[0] :
382 (scalez > this->Range[1] ? this->Range[1] : scalez));
383 scalez = (scalez - this->Range[0]) / den;
386 // Compute index into table of glyphs
387 if ( this->IndexMode == VTK_INDEXING_OFF )
393 if ( this->IndexMode == VTK_INDEXING_BY_SCALAR )
402 index = (int) ((double)(value - this->Range[0]) * numberOfSources / den);
403 index = (index < 0 ? 0 :
404 (index >= numberOfSources ? (numberOfSources-1) : index));
406 source = this->GetSource(index, inputVector[1]);
407 if ( source != NULL )
409 sourcePts = source->GetPoints();
410 sourceNormals = source->GetPointData()->GetNormals();
411 numSourcePts = sourcePts->GetNumberOfPoints();
412 numSourceCells = source->GetNumberOfCells();
416 // Make sure we're not indexing into empty glyph
417 if ( this->GetSource(index, inputVector[1]) == NULL )
422 // Check ghost points.
423 // If we are processing a piece, we do not want to duplicate
424 // glyphs on the borders. The corrct check here is:
425 // ghostLevel > 0. I am leaving this over glyphing here because
426 // it make a nice example (sphereGhost.tcl) to show the
427 // point ghost levels with the glyph filter. I am not certain
428 // of the usefullness of point ghost levels over 1, but I will have
429 // to think about it.
430 if (inGhostLevels && inGhostLevels[inPtId] > requestedGhostLevel)
435 if (!this->IsPointVisible(input, inPtId))
440 // Now begin copying/transforming glyph
443 // Copy all topology (transformation independent)
444 for (cellId=0; cellId < numSourceCells; cellId++)
446 cell = this->GetSource(index, inputVector[1])->GetCell(cellId);
447 cellPts = cell->GetPointIds();
448 npts = cellPts->GetNumberOfIds();
449 for (pts->Reset(), i=0; i < npts; i++)
451 pts->InsertId(i,cellPts->GetId(i) + ptIncr);
453 output->InsertNextCell(cell->GetCellType(),pts);
456 // translate Source to Input point
457 input->GetPoint(inPtId, x);
459 //projection on the plane orthogonale to the camera
460 trans->Scale(mOrientation[0],mOrientation[1],mOrientation[2]);
462 trans->Translate(x[0], x[1], x[2]);
467 for (i=0; i < numSourcePts; i++)
469 newVectors->InsertTuple(i+ptIncr, v);
471 if (this->Orient && (vMag > 0.0))
473 // if there is no y or z component
474 if ( v[1] == 0.0 && v[2] == 0.0 )
476 if (v[0] < 0) //just flip x if we need to
478 trans->RotateWXYZ(180.0,0,1,0);
483 vNew[0] = (v[0]+vMag) / 2.0;
484 vNew[1] = v[1] / 2.0;
485 vNew[2] = v[2] / 2.0;
486 trans->RotateWXYZ((double)180.0,vNew[0],vNew[1],vNew[2]);
493 for (i = 0; i < numSourcePts; i++)
495 sourceTCoords->GetTuple(i, tc);
496 newTCoords->InsertTuple(i+ptIncr, tc);
500 // determine scale factor from scalars if appropriate
502 if (inSScalars && (this->ColorMode == VTK_COLOR_BY_SCALE))
504 for (i=0; i < numSourcePts; i++)
506 newScalars->InsertTuple(i+ptIncr, &scalex); // = scaley = scalez
509 else if (inCScalars && (this->ColorMode == VTK_COLOR_BY_SCALAR))
511 for (i=0; i < numSourcePts; i++)
513 outputPD->CopyTuple(inCScalars, newScalars, inPtId, ptIncr+i);
516 if (haveVectors && this->ColorMode == VTK_COLOR_BY_VECTOR)
519 for (i=0; i < numSourcePts; i++)
521 newScalars->InsertTuple(i+ptIncr, &color);
525 // scale data if appropriate
528 if ( this->ScaleMode == VTK_DATA_SCALING_OFF )
530 scalex = scaley = scalez = this->ScaleFactor;
534 scalex *= this->ScaleFactor;
535 scaley *= this->ScaleFactor;
536 scalez *= this->ScaleFactor;
551 trans->Scale(scalex,scaley,scalez);
553 // multiply points and normals by resulting matrix
554 trans->TransformPoints(sourcePts,newPts);
558 trans->TransformNormals(sourceNormals,newNormals);
561 // Copy point data from source (if possible)
564 for (i=0; i < numSourcePts; i++)
566 outputPD->CopyData(pd,inPtId,ptIncr+i);
570 // If point ids are to be generated, do it here
571 if ( this->GeneratePointIds )
573 for (i=0; i < numSourcePts; i++)
575 pointIds->InsertNextValue(inPtId);
579 ptIncr += numSourcePts;
582 // Update ourselves and release memory
584 output->SetPoints(newPts);
589 int idx = outputPD->AddArray(newScalars);
590 outputPD->SetActiveAttribute(idx, vtkDataSetAttributes::SCALARS);
591 newScalars->Delete();
596 outputPD->SetVectors(newVectors);
597 newVectors->Delete();
602 outputPD->SetNormals(newNormals);
603 newNormals->Delete();
608 outputPD->SetTCoords(newTCoords);
609 newTCoords->Delete();
619 void vvGlyph2D::PrintSelf(ostream& os, vtkIndent indent)
621 this->Superclass::PrintSelf(os,indent);
624 void vvGlyph2D::SetOrientation(int x, int y, int z)
627 mOrientation[0] = 1.0e-10;
629 mOrientation[0] = 1.0;
631 mOrientation[1] = 1.0e-10;
633 mOrientation[1] = 1.0;
635 mOrientation[2] = 1.0e-10;
637 mOrientation[2] = 1.0;