[vtk-developers] Proposed CenterOfMass function for vtkPoints

Will Schroeder will.schroeder at kitware.com
Thu Apr 1 06:18:01 EDT 2010


I see what you are doing but of course it is not really the center of
mass, also it is not consistent with the names of the methods
GetBounds(), GetCenter() that exist in vtkDataSet, so I think the
method is misnamed. However, I am not sure what to call it...maybe
GetWeightedCenter(), GetComputedCenter(), GetPointsCenter()? Also, you
may want to cache the computation like GetBounds() does (it first does
a mtime check against the compute time).



On Wed, Mar 31, 2010 at 5:30 PM, David Doria <daviddoria+vtk at gmail.com> wrote:
> 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
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtk-developers
>
>
>



-- 
William J. Schroeder, PhD
Kitware, Inc.
28 Corporate Drive
Clifton Park, NY 12065
will.schroeder at kitware.com
http://www.kitware.com
(518) 881-4902



More information about the vtk-developers mailing list