[vtkusers] vtkPolyData intersection

De Boer Ingo I.deBoer at polytec.de
Tue Apr 22 05:13:38 EDT 2003


Hi,

I did something in that direction. I think clipping or cutting would have the
same results. I clipped polygon data from a plan/texture. But the clipping is
bad on the edges :( I haven't found any better solution yet...

m_pcPoints holds the 2d positions of my polygon data.
m_pElementIndex holds the polygon information (number of points and the neighbours)

greets
  Ingo

/****************************************************************/
/**
 */
void CVtkData::CreateFromColorIndex(BYTE *pBuffer, int iWidth, int iHeight)
{
	ASSERT(m_pcPoints);
	ASSERT(m_pElementIndex);

	m_uiWidth = iWidth;
	m_uiHeight = iHeight;

	float fAspectRatio  = (float)m_uiWidth/(float)m_uiHeight;

	// Create image data
	vtkImageData *image = vtkImageData::New();
	image->SetExtent(0, iWidth, 0, iHeight, 0, 0);	
	image->SetUpdateExtent(0, iWidth, 0, iHeight, 0, 0);
	image->SetScalarTypeToUnsignedChar();

	for (int j = 0; j < iHeight; j++)
		for (int i = 0; i < iWidth; i++)
			*((BYTE*)image->GetScalarPointer(i,j,0)) = *pBuffer++;

	// Create texture
	if (m_pvtkTexture) m_pvtkTexture->Delete();
	m_pvtkTexture = vtkTexture::New();
	m_pvtkTexture->SetInput(image);
	m_pvtkTexture->InterpolateOn();

	// Create a plane source. The vtkPlaneSource generates texture coordinates.
	vtkPlaneSource *plane = vtkPlaneSource::New();
	plane->SetOrigin( 0.0, 0.0, 0.0);
	plane->SetPoint1( fAspectRatio*1.0, 0.0, 0.0);
	plane->SetPoint2( 0.0, 1.0, 0.0);
	plane->SetCenter( fAspectRatio*0.5, 0.5, 0.0);
	plane->SetXResolution(128);
	plane->SetYResolution(128);

	// create clipping region
	int i;

	// We'll create the building blocks of polydata including data attributes.
	vtkPolyData		*hole		= vtkPolyData::New();
	vtkPoints		*points		= vtkPoints::New();
	vtkCellArray	*polygons	= vtkCellArray::New();
	vtkFloatArray	*scalars	= vtkFloatArray::New();

	for (i = 0; i < m_lPoints; i++)
	{
		points->InsertPoint(i, m_pcPoints[i].m_tX, m_pcPoints[i].m_tY, 0.0);
	      scalars->InsertTuple1(i, 1);
	}

	for (i = 0; i < m_lElements; i++) 
	{
		int iElements = m_pElementIndex[i].m_iPoints;
		vtkIdType *polys = new vtkIdType[iElements];
		for (int j = 0; j < iElements; j++)
			polys[j] = m_pElementIndex[i].m_piIndices[j];
		polygons->InsertNextCell(iElements, polys);
		delete polys;
	}


	// We now assign the pieces to the vtkPolyData.
	hole->SetPoints(points);
	hole->SetPolys(polygons);
	hole->GetPointData()->SetScalars(scalars);

	// delete not needed stuff
	points->Delete();
	polygons->Delete();
	scalars->Delete();

	// create the implicit function from the dataset
	vtkImplicitDataSet *dataset = vtkImplicitDataSet::New();
	dataset->SetDataSet(hole);

	// create the clipper
	vtkClipPolyData *clipper = vtkClipPolyData::New();
	clipper->SetInput(plane->GetOutput());
	clipper->SetClipFunction(dataset);
	clipper->InsideOutOn();

	// Create a plane mapper
	vtkPolyDataMapper *planeMapper = vtkPolyDataMapper::New();

//#define _SHOW_HOLEOBJECT_
#ifdef _SHOW_HOLEOBJECT_
	planeMapper->SetInput(plane->GetOutput());
#else
	planeMapper->SetInput(clipper->GetOutput());
#endif
	// Remove old actor
	m_pvtkRenderer->RemoveActor(m_pvtkActorTexture);

	// Set the actor for the mapper and the texture
	m_pvtkActorTexture->SetMapper(planeMapper);
	m_pvtkActorTexture->SetTexture(m_pvtkTexture);

	// set transparency
	m_pvtkActorTexture->GetProperty()->SetOpacity(m_fTransparency);

	// Add actor to renderer
	m_pvtkRenderer->AddActor(m_pvtkActorTexture);

#ifdef _SHOW_HOLEOBJECT_
	// Create a hole mapper and show hole aswell
	vtkPolyDataMapper *holeMapper = vtkPolyDataMapper::New();
	holeMapper->SetInput(hole);
	vtkActor *holeActor = vtkActor::New();
	holeActor->SetMapper(holeMapper);
	m_pvtkRenderer->AddActor(holeActor);
	m_pvtkPropCollection->AddItem(holeActor);
#endif

	// update
	Update();
	Render();

	// cleanup
	planeMapper->Delete();
	plane->Delete();
	image->Delete();
}




More information about the vtkusers mailing list