Thursday, October 06, 2005

VTK Triangle Strips

One of the projects I am currently working on is to provide a better model for Diffusion MRI in fibreous white matter. The idea is to output some geometries and use another tool to do the heavy lifting, e. g. the simulation. After the white matter is modeled by some parametric curves it gets triangulated and displayed in VTK as a trinagle strip. The goal is to output this strip in some meaningful way. The code to do so is:

void PrintStrips ( FILE *f, vtkPolyData *mypoly ) {

vtkPoints *mypoints = mypoly->GetPoints ( ) ;
vtkCellArray *mystrips = mypoly->GetStrips ( ) ;

int *indices=mystrips->GetPointer ( ) ;
int numindices=mystrips->GetNumberOfConnectivityEntries ( ) ; //Whoa!
int idx=0;

while ( idx<numindices ) {
int striplen=indices[idx]; //Get length of next strip

for ( int jj=0; ( jj<striplen ) && ( idx<numindices ) ;jj++ ) {
float A[3], B[3], C[3]; //coordinates of triangle vertices
mypoints->GetPoint ( indices[idx], A ) ;
//mypoints->GetPoint ( indices[idx+1], B ) ;
//mypoints->GetPoint ( indices[idx+2], C ) ;
fprintf ( f, "[%f, %f, %f]\n", A[0], A[1], A[2] ) ;
//fprintf ( f, "[%f, %f, %f]\n", B[0], B[1], B[2] ) ;
//fprintf ( f, "[%f, %f, %f]\n", C[0], C[1], C[2] ) ;

The commented code allows you to output instead of triangle strips the data as each triangle. Some programs like Open Inventor like that better. (Also in the for loop change jj<striplen in jj<striplen-2).

Something about triangle strips:
Given the coordinates A, B, C, D, E,... the strip implicitly described by those coordinates give the following triangles: ABC, BCD, CDE, ...

Some software however can't automatically decode that and just deals with polygons. Then you have to be explicit and output ABC, BCD, CDE instead of the strip.


Post a Comment

<< Home