[vtkusers] Help with Vtk when rendering millions of points
David Doria
daviddoria at gmail.com
Tue Aug 7 07:02:26 EDT 2012
On Tue, Aug 7, 2012 at 5:19 AM, ikatz <ikatzbilb at hotmail.com> wrote:
> I can't provide a self-contained example because the code is quite complex
> and uses another libraries for other purposes, it would be impossible to
> compile everything. Anyway, I will try to explain as best as possible.
> We are using what we call gfxPointClouds, an structure that holds all the
> poins of the point cloud, and after getting the number of points we use
> vtkPolyData structures to manage all the points. This point cloud element
> is
> treated as other elements like spheres or lines in our vtkRenderWindow, the
> problem is that when we want to "refresh" the whole Viewport (working with
> >10 million point clouds) it takes a lot of time (sometimes 1 minute) or
> does not even load the point cloud and the program fails. I don't know if
> there is another way of managing this point clouds structures in VTK other
> than vtkPolyData or if it's normal to last so match...
>
> Here is the code:
>
> const gfxPointCloud3* cop = dynamic_cast<const
> gfxPointCloud3*>(pointCloud);
> unsigned int iNumPoints = cop->GetNumElements();
>
> //Reset to an empty state and free any memory.
> m_PolyData->Initialize();
>
> //No points, no work.
> if( iNumPoints > 0 )
> {
> vtkPoints* pointSet = vtkPoints::New();
> pointSet->Allocate(iNumPoints);
> vtkCellArray* cellSet = vtkCellArray::New();
> vtkBitArray* selectSet = vtkBitArray::New();
> vtkFloatArray* scalarSet = vtkFloatArray::New();
>
> pointSet->SetNumberOfPoints( iNumPoints );
> selectSet->SetNumberOfValues( iNumPoints );
>
> switch( cop->GetAttributeSize() )
> {
> case 1: //char (8 bits)
> case 2: //short (16 bits)
> case 4: //int (32 bits)
> case 8: //double (64 bits)
> m_AttributeSize = cop->GetAttributeSize();
> scalarSet->SetNumberOfValues( iNumPoints );
> bAttribute = true;
> break;
> default: //invalid size: disable the color map.
> m_AttributeSize = 0;
> m_Map->ScalarVisibilityOff();
> }
>
> gfxPoint3 point;
> for( unsigned int i=0; i<iNumPoints; i++ )
> {
> //Add the point to the point list.
> point = cop->GetElement(i);
> pointSet->SetPoint( i, point.x(), point.y(),
> point.z() );
>
> //Create a new cell and add the new point to it.
> cellSet->InsertNextCell( 1 );
> cellSet->InsertCellPoint( i );
>
> //Mark the new point as not selected.
> selectSet->SetValue( i, 0 );
>
> //IF{} just to speed-up when there is no attribute
> in the point cloud.
> if( bAttribute )
> {
> switch( m_AttributeSize )
> {
> //All types are converted to float.
> case 1: //char (8 bits)
> scalarSet->SetValue( i,
> *((char*)(cop->GetAttribute(i))) );
> break;
> case 2: //short (16 bits)
> scalarSet->SetValue( i,
> *((short*)(cop->GetAttribute(i))) );
> break;
> case 4: //int (32 bits)
> scalarSet->SetValue( i,
> *((int*)(cop->GetAttribute(i))) );
> break;
> case 8: //double (64 bits)
> scalarSet->SetValue( i,
> *((double*)(cop->GetAttribute(i))) );
> break;
> }
> }
> }
>
> m_PolyData->SetPoints( pointSet );
> m_PolyData->SetVerts( cellSet );
>
> //Associate the selection scalar with the cells.
> m_PolyData->GetCellData()->SetScalars( selectSet );
>
> //Associate the attribute scalar with the points.
> if( bAttribute )
> m_PolyData->GetPointData()->SetScalars( scalarSet
> );
>
> m_PolyData->Modified();
>
> m_Map->SetInput( m_PolyData );
> m_SelectionMap->SetInput( m_PolyData );
>
> scalarSet->Delete();
> selectSet->Delete();
> cellSet->Delete();
> pointSet->Delete();
> }
>
You should be able to turn that into a working demo without much trouble.
It looks like you can just replace your custom data type with a
vtkPointSource (
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PointSource ) to produce
some random data. Of course you'll also have to make the member variables
defined in this demo scope, etc.
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120807/7de64d5a/attachment.htm>
More information about the vtkusers
mailing list