[vtkusers] vtkCellArray Traversal

Jeremy Winston winston at cat.rpi.edu
Fri Sep 20 07:01:16 EDT 2002


Nicu Cornea wrote:
> 
> Hi everyone !
> 
> Can anyone give me an example how to traverse a vtkCellArray in TCL ?

Hi Nicu,
    Your question is timely, as I just had to do the same thing.
You cannot use the GetNextCell() method, since its args are pass-
by-reference, and (I think) such methods cannot be wrapped in Tcl.
    Hence, you have to do it "manually," using the GetComponent()
and SetComponent() methods to view/modify the underlying data elements
in each array.  I have a vtkPolyData instance that consists of several 
lines.  The following code snippet walks through its line array and its 
scalar array, changing the scalar values of the points that make up a 
line to 0 if it has fewer than $minpts points, or to $thresh otherwise.

  set c [vhdStrip GetOutput]                   ;# The vtkPolyData instance
  set sd [[$c GetPointData] GetScalars]        ;# Its scalar data array
  set ld [[$c GetLines] GetData]               ;# Its line data array
  set numLines [$c GetNumberOfLines]           ;# The number of lines in $ld
  set idx 0                                    ;# Index into the line array

  for {set i 0} {$i<$numLines} {incr i} {      ;# For each line...
    set nPts [$ld GetComponent $idx 0]         ;# # of points in this line
    set endPt [expr $idx + $nPts]              ;# Indx to last pt in this line
    if {$nPts < $minpts} {                     ;# If too few points in line...
      for {set j [expr $idx+1]} {$j<=$endPt} {incr j} {;# For @ pt in the line
        set ptNo [$ld GetComponent $j 0]       ;# Point # (indx into scalars)
        $sd SetComponent $ptNo 0 0             ;# Set pt's scalar to 0
      }
    } else {                                   ;# Don't skip this line
      for {set j [expr $idx+1]} {$j<=$endPt} {incr j} {;# For @ pt in the line
        set ptNo [$ld GetComponent $j 0]       ;# Point # (indx into scalars)
        $sd SetComponent $ptNo 0 $thresh       ;# Reset pt's scalar to orig val
      }
    }
    incr idx [expr $nPts + 1]                  ;# Move to next line
  }

Each of the arrays consists of a series of (n,e1,e2,e3,...,en) groups
where n is the # of elements in that group & e1 through en are the
elements.  E.g., the line array for two polylines (the first a 2-point line,
the second a closed triangle) might look like
   _______________________________
  | 2 | 0 | 1 | 4 | 2 | 3 | 4 | 2 |
   -------------------------------
    ^           ^
    |           '------ Four points in this line, w/point IDs 2, 3, 4 & 2.
    '-------------------Two points in this line, w/point IDs 0 & 1.

The point IDs are used to index into and the scalar array.  The scalar value
for the point with ID 0 is the first element in the scalar array; with ID 1,
the second, etc.

HTH,
-Jeremy



More information about the vtkusers mailing list