[vtk-developers] Proposed CenterOfMass function for vtkPoints

David Doria daviddoria+vtk at gmail.com
Wed Mar 31 17:30:09 EDT 2010


Can we add a CenterOfMass(double[3]) function to vtkPoints?

void vtkPoints::CenterOfMass(double center[3])
{
  center[0] = 0.0;
  center[1] = 0.0;
  center[2] = 0.0;

  for(vtkIdType i = 0; i < this->GetNumberOfPoints(); i++)
    {
    double point[3];
    this->GetPoint(i, point);

    center[0] += point[0];
    center[1] += point[1];
    center[2] += point[2];
    }

  double numberOfPoints = static_cast<double>(this->GetNumberOfPoints());
  center[0] /= numberOfPoints;
  center[1] /= numberOfPoints;
  center[2] /= numberOfPoints;
}

It would be used like this:

  vtkSmartPointer<vtkPoints> points =
      vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(0,0,0);
  points->InsertNextPoint(1,0,0);
  points->InsertNextPoint(0,1,0);
  points->InsertNextPoint(1,1,0);

  double center[3];
  points->CenterOfMass(center);
  cout << "Center: " << center[0] << " " << center[1] << " " <<
center[2] << endl;

or like this:

  vtkSmartPointer<vtkPoints> points =
      vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(0,0,0);
  points->InsertNextPoint(1,0,0);
  points->InsertNextPoint(0,1,0);
  points->InsertNextPoint(1,1,0);

  vtkSmartPointer<vtkPolyData> pd =
      vtkSmartPointer<vtkPolyData>::New();
  pd->SetPoints(points);

  double center[3];
  pd->GetPoints()->CenterOfMass(center);
  cout << "Center: " << center[0] << " " << center[1] << " " <<
center[2] << endl;

The modified files and a demo are attached if anyone wants to try them out.

Thanks,

David
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vtkPoints.cxx
Type: text/x-c++src
Size: 6982 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20100331/981f3a07/attachment-0001.cxx>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vtkPoints.h
Type: text/x-chdr
Size: 7636 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20100331/981f3a07/attachment-0001.h>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Test.cpp
Type: text/x-c++src
Size: 1062 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20100331/981f3a07/attachment-0001.cpp>


More information about the vtk-developers mailing list