首页 > 解决方案 > 如何在 VTK 中定义 C++ 函数

问题描述

在此处输入图像描述我是 C++ 和 VTK 的新手。我正在尝试将单元格 ID 放入一个rectilinearGrid基本示例中。我正在使用这段代码,但编译器说我在评论中写的错误是错误的

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkFloatArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRectilinearGrid.h>
#include <vtkRectilinearGridGeometryFilter.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <array>

int main()
{
  vtkNew<vtkNamedColors> colors;

  std::array<int, 16> x = {
      {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}};
  std::array<int, 16> y = {
      {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}};
  std::array<int, 16> z = {
      {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}};

  // Create a rectilinear grid by defining three arrays specifying the
  // coordinates in the x-y-z directions.
  vtkNew<vtkFloatArray> xCoords;
  for (auto&& i : x)
  {
    xCoords->InsertNextValue(i);
  }
  vtkNew<vtkFloatArray> yCoords;
  for (auto&& i : y)
  {
    yCoords->InsertNextValue(i);
  }
  vtkNew<vtkFloatArray> zCoords;
  for (auto&& i : z)
  {
    zCoords->InsertNextValue(i);
  }

  // The coordinates are assigned to the rectilinear grid. Make sure that
  // the number of values in each of the XCoordinates, YCoordinates,
  // and ZCoordinates is equal to what is defined in SetDimensions().
  //
  vtkNew<vtkRectilinearGrid> rgrid;
  rgrid->SetDimensions(int(x.size()), int(y.size()), int(z.size()));
  rgrid->SetXCoordinates(xCoords);
  rgrid->SetYCoordinates(yCoords);
  rgrid->SetZCoordinates(zCoords);

  vtkCell* GetCell(vtkRectilinearGrid * rgrid, int i, int j, int k) //I SHOULD INSERT IN HERE ";" FOR 
  {                                                                 //CLOSING THE STATEMENT. BUT IN                  
      int dims[3];                                                    //THIS WAY THE FUNCTION PARAMETER  
      rgrid->GetDimensions(dims);                                     // BEHIND WOULDN'T BE CONNECTED.

      if (i < 0 || i > dims[0] - 1 ||
          j < 0 || j > dims[1] - 1 ||
          k < 0 || k > dims[2] - 1)
      {
          return NULL; // out of bounds!
      }

      int pos[3];
      pos[0] = i;
      pos[1] = j;
      pos[2] = k;

      vtkIdType id;
      id = vtkStructuredData::ComputeCellId(dims, pos);

      return rgrid->GetCell(id);
  };




  // Extract a plane from the grid to see what we've got.
  vtkNew<vtkRectilinearGridGeometryFilter> plane;
  plane->SetInputData(rgrid);
  plane->SetExtent(0, 46, 16, 16, 0, 43);

  vtkNew<vtkPolyDataMapper> rgridMapper;
  rgridMapper->SetInputConnection(plane->GetOutputPort());

  vtkNew<vtkActor> wireActor;
  wireActor->SetMapper(rgridMapper);
  wireActor->GetProperty()->SetRepresentationToWireframe();
  wireActor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());

  // Create the usual rendering stuff.
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  renderer->AddActor(wireActor);
  renderer->SetBackground(1, 1, 1);
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Elevation(30.0);
  renderer->GetActiveCamera()->Azimuth(15.0);
  renderer->GetActiveCamera()->Zoom(1.0);
  renderer->SetBackground(colors->GetColor3d("Beige").GetData());

  renWin->SetSize(600, 600);






  // interact with data
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

怎么可能修好?

更新 1:我插入了编译错误的图像。应插入“;” 用于在 {} 之前关闭语句

更新2:确切的错误是

Errore (attivo) E0065 预期为 ';' RGrid C:\vtk\VTK-8.2.0\Examples\DataManipulation\Cxx\RGrid.cxx 73

我正在使用 Visual Studio。我试图删除最后一个“;” 但没有任何改变

更新3:我已经上传了所有代码

标签: c++functionvtk

解决方案


您已经在GetCell函数体内定义了main函数,这在 C++ 中是不允许的。主体内只允许声明,因此编译器期望函数头后有一个分号。

将整个GetCell功能块移到功能外main。如果这导致您无法解决的问题,请询问有关它们的另一个问题。


推荐阅读