vtkCenterOfMass查找点集的质心


开发环境:

  1. Windows 11 家庭中文版
  2. Microsoft Visual Studio Community 2019
  3. VTK-9.3.0.rc0
  4. vtk-example

demo解决问题:查找点集的质心。

Center of mass is 0.5 0.5 0

关键点:

  1. vtkCenterOfMass 會找出 vtkPointSet(vtkPolyData 或 vtkUnstructuredGrid)的「質量中心」。用户可选择指定在计算中使用标量作为权重。如果 “UseScalarsAsWeights”(使用标量作为权重)选项处于关闭状态,则每个点在计算中的贡献相同。
  vtkNew<vtkCenterOfMass> centerOfMassFilter;
  centerOfMassFilter->SetInputData(polydata);
  centerOfMassFilter->SetUseScalarsAsWeights(false);
  centerOfMassFilter->Update();

prj name: CenterOfMass

#include <vtkCenterOfMass.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>

#include <cmath>
#include <limits>

int main(int, char*[])
{
  // Create a point set of a square.
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0, 0, 0);
  points->InsertNextPoint(1, 0, 0);
  points->InsertNextPoint(0, 1, 0);
  points->InsertNextPoint(1, 1, 0);

  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);

  // Compute the center of mass.
  vtkNew<vtkCenterOfMass> centerOfMassFilter;
  centerOfMassFilter->SetInputData(polydata);
  centerOfMassFilter->SetUseScalarsAsWeights(false);
  centerOfMassFilter->Update();

  double center[3];
  centerOfMassFilter->GetCenter(center);

  std::cout << "Center of mass is " << center[0] << " " << center[1] << " "
            << center[2] << std::endl;

  return EXIT_SUCCESS;
}