1

我正在尝试为 PolyData 创建一个颜色图,其点构成了核反应堆的结构。似乎每次我改变颜色时,单元格的大小都会根据我分配给它们的标量值而改变。我只想要一个颜色图,它显示我的结构的单元格甚至点之间的插值颜色。这是导致我的颜色图更改单元格大小的代码。

#include <vtkFloatArray.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkLookupTable.h>
#include <vtkPointData.h>
#include <vtkCubeSource.h>
#include <vtkPolyData.h>
#include <vtkGlyph3DMapper.h>


int main(){
    vtkSmartPointer<vtkPoints> points=//points used to set deminsions of grid
        vtkSmartPointer<vtkPoints>::New();
    // cube source 
     vtkSmartPointer<vtkCubeSource> cube = 
        vtkSmartPointer<vtkCubeSource>::New();
     cube->SetZLength(2);
     //-----------------------------------------------
    vtkSmartPointer<vtkPolyData> polydata =
        vtkSmartPointer<vtkPolyData>::New();
     points->Allocate(3136);
     for(int i=0;i<27;++i){
         if(i!=0){i+=1;}
        for(int j=4;j<12;++j){  
            points->InsertNextPoint(j, 0, (i)); 
        }
        for(int k=2;k<14;++k){
            points->InsertNextPoint(k, 1, i);
            }           
        for(int m=2;m<4;++m)                            
            for(int l=1;l<15;++l){
                points->InsertNextPoint(l, m, i);
                }
        for(int n=4;n<12;++n)
            for(int p=0;p<16;++p){
                points->InsertNextPoint(p, n, i);
                }
        for(int q=12;q<14;++q)  
            for(int r=1;r<15;++r){
                points->InsertNextPoint(r, q, i);
                }
        for(int s=2;s<14;++s){
            points->InsertNextPoint(s, 14, i);
            }
        for(int t=4;t<12;++t){
            points->InsertNextPoint(t, 15, i);
            }
        }
    //--------------------build scalor array--------------------------
        vtkSmartPointer<vtkFloatArray> sarray=
    vtkSmartPointer<vtkFloatArray>::New();
            sarray->SetNumberOfValues(3136);
            for(int i=0;i<3136;++i){
            if(i<2000)
                sarray->InsertValue(i,1);
            else
                sarray->InsertValue(i,10);
            }
    vtkSmartPointer<vtkLookupTable>cTable = 
           vtkSmartPointer<vtkLookupTable>::New();
    int tableSize =  10;// not sure if I need this
    cTable->SetNumberOfTableValues(tableSize);// not sure if I need this
    cTable->Build();
     //--------------------------------------------------------------------------------
    polydata->SetPoints(points);
    polydata->BuildCells();
    polydata->GetPointData()->SetScalars(sarray);
     //---------------------------------------------

      vtkSmartPointer<vtkGlyph3DMapper> mapper = 
        vtkSmartPointer<vtkGlyph3DMapper>::New();
     mapper->SetSourceConnection(cube->GetOutputPort());
    #if VTK_MAJOR_VERSION <= 5
      mapper->SetInputConnection(polydata->GetProducerPort());
    #else
      mapper->SetInputData(polydata);
    #endif
    mapper->Update();
    mapper->SetScalarRange(1, 10);
    mapper->SetLookupTable(cTable);
    //-----------------------------------------------------------------
    vtkSmartPointer<vtkActor> actor = 
     vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetPointSize(3);

    vtkSmartPointer<vtkRenderer> renderer = 
    vtkSmartPointer<vtkRenderer>::New();

    vtkSmartPointer<vtkRenderWindow> renderWindow = 
     vtkSmartPointer<vtkRenderWindow>::New();

      renderWindow->AddRenderer(renderer);

      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
        vtkSmartPointer<vtkRenderWindowInteractor>::New();

      renderWindowInteractor->SetRenderWindow(renderWindow);

      renderer->AddActor(actor);
      renderer->SetBackground(.2, .3, .4);
      renderer->ResetCamera();
      renderWindow->Render();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }
4

0 回答 0