[vtkusers] vtkPoints + InsertPoint

David Gobbi david.gobbi at gmail.com
Fri Mar 12 10:35:04 EST 2010


This thread is full of nonsense.  The reason for the error is in the
details of how InsertTuple works. Let's dissect what happens when you
do this:

points->InsertPoint(60983688, x, y, z);

First, vtkDataArrayTemplate.txx (which is the actual container) will
check to see if it has enough memory to hold 60983688 tuples, and if
not, it will allocate a new array that is the sum of the old size and
the new size.  Look at the code for
vtkDataArrayTemplate::ResizeAndExtend(vtkIdType sz).  Whenever
InsertPoint requires that the array is resized, the new size is always
more than double the previous size.

The reason VTK does this is that resizing by a factor of two is known
to be the most computationally efficient way to grow an array when the
final size is unknown.  It is definitely not a memory-efficient way of
doing things, but the VTK designers have, quite correctly, chosen
computational efficiency over memory efficiency.

So, when you are using InsertPoint or InsertNextPoint (or InsertTuple
or InsertNextTuple), the array will allocate up to twice as much
memory as you actually need.  And if you call InsertTuple repeatedly,
then there will be instances where VTK will temporarily require up to
three times as much memory as you actually need, while it is copying
from the old array to the newly allocated array as part of the resize
operation.

You can avoid this by using SetNumberOfPoints() to allocate exactly as
much space as you need before you add any points.

   David


On Fri, Mar 12, 2010 at 8:06 AM, Bill Chivas <noo134 at googlemail.com> wrote:
> Thanks David.
> I run it without crashing, too.
> So, i think the problem is somewhere else ...
>
> Thanks,
> Bill
>
> 2010/3/12 David Doria <daviddoria+vtk at gmail.com>
>>
>> On Fri, Mar 12, 2010 at 9:36 AM, Bill Chivas <noo134 at googlemail.com>
>> wrote:
>>>
>>> To David:
>>>
>>> If it works with only two points, try to put some more (e.g. 10).
>>> In mine, it crashes in 3 or 4 points.
>>>
>>
>> This runs without crashing for me:
>> #include <vtkSmartPointer.h>
>> #include <vtkPoints.h>
>> #include <vtkMath.h>
>> int main(int argc, char *argv[])
>> {
>>   vtkSmartPointer<vtkPoints> points =
>>       vtkSmartPointer<vtkPoints>::New();
>>
>>   points->SetNumberOfPoints(61000000);
>>
>>   int start = 60983588;
>>   for(unsigned int i = 0; i < 100; i++)
>>     {
>>     points->InsertPoint(start + i, vtkMath::Random(0.0,1.0),
>> vtkMath::Random(0.0,1.0), vtkMath::Random(0.0,1.0));
>>     }
>>
>>   return EXIT_SUCCESS;
>> }
>> Thanks,
>>
>> David
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>



More information about the vtkusers mailing list