[vtkusers] Help Needed: Clipping Data in VTK!!

Rahul Goela rahulgoela at yahoo.com
Wed Feb 18 09:35:53 EST 2004


Dear VTK users:

I have been facing problems with clipping data using
VTK for quite some time. I would appreciate if you
could advise me on what I have done wrong.

I am displaying 30 points in a 3D space. I also have a
transluscent sphere on the screen. This sphere enloses
say 4 of these 30 points. I need to using a clipping
function and determine which points are being
enclosed. If there is any way I visualize the pts
present only in the sphere or display them in a
different colour than the rest, It would solve my
problem.

I have used two methodologies:

1. Using vtkClipDataSet

My code is provided below. after excuting this code,
the output is only one point on the screen. Also this
point lies very far outside the sphere and therefore
clipping is not exactly correct.   


---------CODE--------------------------------------

vtkPoints *thisPts = vtkPoints::New();
numPts=30;
for(i=0; i<numPts; i++)
{ 
  //I have defined 30 pts in xcon,ycon and zcon.  
          xyz[0] = xcon[i];	
	  xyz[1] = ycon[i]; 
	  xyz[2] = zcon[i]; 
	  thisPts->InsertPoint(i, xyz); 
  }
aVertex = vtkVertex::New();
for( int m = 0; m < NumPoints; m++ )
{
 aVertex->GetPointIds()->InsertNextId(m);
}

dataSet = vtkUnstructuredGrid::New();
dataSet->InsertNextCell(aVertex->GetCellType(),aVertex->GetPointIds());
dataSet->SetPoints(thisPts);
 
 //CREATING THE SPHERE
  vtkSphere *s12 = vtkSphere::New();
    s12->SetCenter( 6.5, 6.0, 6.0 );
    s12->SetRadius( 1.0 );
  vtkSampleFunction *sf12 = vtkSampleFunction::New();
    sf12->SetImplicitFunction( s12 );
    sf12->SetSampleDimensions(60,60,60); 
    sf12->SetModelBounds(-7.5,7.5,-7,7,-7, 7);
  vtkContourFilter *cf12 = vtkContourFilter::New();
    cf12->SetInput(sf12->GetOutput());
    cf2->SetValue(0,0.0);
  vtkPolyDataMapper *sM = vtkPolyDataMapper::New();
    sM->SetInput(cf12->GetOutput());
  vtkActor *spActor = vtkActor::New();
    spActor->SetMapper(sM);
    spActor->GetProperty()->SetColor(1,1,0);//red
    spActor->GetProperty()->SetOpacity(0.05);
  
//CLIP THE DATA
vtkClipDataSet *cpds1 = vtkClipDataSet::New();
 cpds1->SetInput(dataSet);
 cpds1->SetClipFunction(s12);
 cpds1->GenerateClipScalarsOn();
 cpds1->SetValue( 0 );
 cpds1->GenerateClippedOutputOn();
 
//GLYPH THE OUTPUT POINTS
vtkSphereSource *ss1 = vtkSphereSource::New();
  ss1->SetRadius(0.5);

vtkGlyph3D *g3d2 = vtkGlyph3D::New();
  g3d2->SetSource( ss1->GetOutput() );
  g3d2->SetInput( cpds1->GetClippedOutput() );
  g3d2->SetScaleFactor( 0.1 );
 
vtkDataSetMapper *dsm1 = vtkDataSetMapper::New();
  dsm1->SetInput( g3d2->GetOutput() );
  dsm1->SetScalarRange( -40.0, 40.0 );

vtkActor *a2 = vtkActor::New();
  a2->SetMapper( dsm1 );
  a2->GetProperty()->SetColor( 1, 1, 0 );

---------------------------------------------------


2. Using ClipPolyData

The code is provided below. This method displays no
output when i display clipped points. 

However instead of using the points as input, if i
randomly create some points using vtkPointSource, then
in the output i see some green and black spheres.
Black spheres lie inside the sphere and green ones
outside. 

But if instead of points source, I use the points as
input as vtkPolyData, only sphere is displayed on the
screen.

---------CODE--------------------------------------

vtkPoints *thisPts = vtkPoints::New();
numPts=30;
for(i=0; i<numPts; i++)
{ 
  //I have defined 30 pts in xcon,ycon and zcon.  
          xyz[0] = xcon[i];	
	  xyz[1] = ycon[i]; 
	  xyz[2] = zcon[i]; 
	  thisPts->InsertPoint(i, xyz); 
  }
aVertex = vtkVertex::New();
for( int m = 0; m < NumPoints; m++ )
{
 aVertex->GetPointIds()->InsertNextId(m);
}

 vtkPolyData *dataSetPD = vtkPolyData::New();
 vtkCellArray *polys = vtkCellArray::New();
 polys->InsertNextCell(aVertex->GetPointIds());
 dataSetPD->SetPoints(thisPts); 
 dataSetPD->SetPolys(polys); 
 
 //CREATING THE SPHERE
  vtkSphere *s12 = vtkSphere::New();
    s12->SetCenter( 6.5, 6.0, 6.0 );
    s12->SetRadius( 1.0 );
  vtkSampleFunction *sf12 = vtkSampleFunction::New();
    sf12->SetImplicitFunction( s12 );
    sf12->SetSampleDimensions(60,60,60); 
    sf12->SetModelBounds(-7.5,7.5,-7,7,-7, 7);
  vtkContourFilter *cf12 = vtkContourFilter::New();
    cf12->SetInput(sf12->GetOutput());
    cf2->SetValue(0,0.0);
  vtkPolyDataMapper *sM = vtkPolyDataMapper::New();
    sM->SetInput(cf12->GetOutput());
  vtkActor *spActor = vtkActor::New();
    spActor->SetMapper(sM);
    spActor->GetProperty()->SetColor(1,1,0);//red
    spActor->GetProperty()->SetOpacity(0.05);
  
//CLIP THE DATA
vtkClipPolyData *cpd1 = vtkClipPolyData::New();
 cpd1->SetInput(dataSetPD);
 cpd1->SetClipFunction( s12 );
 cpd1->GenerateClipScalarsOn();
 cpd1->SetValue( 0 );
 cpd1->GenerateClippedOutputOn(); 

//GLYPH THE OUTPUT POINTS
vtkSphereSource *ss1 = vtkSphereSource::New();
  ss1->SetRadius(0.5);

vtkGlyph3D *g3d2 = vtkGlyph3D::New();
  g3d2->SetSource( ss1->GetOutput() );
  g3d2->SetInput( cpd1->GetClippedOutput() );
  g3d2->SetScaleFactor( 0.1 );

vtkPolyDataMapper *pdm2 = vtkPolyDataMapper::New();
 pdm2->SetInput( g3d2->GetOutput() );
 pdm2->SetScalarRange( -40.0, 40.0 ); 

vtkActor *a2 = vtkActor::New();
  a2->SetMapper( pdm1 );
  a2->GetProperty()->SetColor( 1, 1, 0 );

---------------------------------------------------

I would be extremely grateful if any one could throw
some light on how to achieve the clipping.

Thanks alot 

best regards

Rahul Goela



________________________________________________________________________
Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more. 
Go to: http://in.insurance.yahoo.com/licspecial/index.html



More information about the vtkusers mailing list