【VTKExamples::Visualization】第三期 Background设置

发布时间:2024年01月09日

很高兴在雪易的CSDN遇见你?

VTK技术爱好者 QQ:870202403


前言

本文分享几种背景设置的方式,希望对各位小伙伴有所帮助!

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的点赞就是我的动力(^U^)ノ~YO


目录

前言

1. 纯色背景

2. 渐变背景

3. 纹理背景

3.1 创建点集PolyData对象

3.2 通过顶点插值轮廓

完整示例代码

结论:


1. 纯色背景

  vtkSmartPointer<vtkRenderer> renderer = 
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(actor);
  renderer->SetBackground(1,.5,1); // Background color

2. 渐变背景

  vtkSmartPointer<vtkRenderer> renderer = 
    vtkSmartPointer<vtkRenderer>::New();
 
  // Add the actor to the scene
  renderer->AddActor(actor);
  
  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();

  // Setup the background gradient
  renderer->GradientBackgroundOn();
  renderer->SetBackground(colors->GetColor3d("Banana").GetData());
  renderer->SetBackground2(colors->GetColor3d("Tomato").GetData());

3. 纹理背景

??

3.1 创建点集PolyData对象

    // Create a set of vertices (polydata)
    vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(100.0, 0.0, 0.0);
    points->InsertNextPoint(300.0, 0.0, 0.0);

    // Setup colors
    unsigned char white[3] = { 255, 255, 255 };
    unsigned char black[3] = { 0, 0, 0 };

    vtkSmartPointer<vtkUnsignedCharArray> vertexColors =
        vtkSmartPointer<vtkUnsignedCharArray>::New();
    vertexColors->SetNumberOfComponents(3);
    vertexColors->SetName("Colors");
    vertexColors->InsertNextTuple3(black[0], black[1], black[2]);
    vertexColors->InsertNextTuple3(white[0], white[1], white[2]);

    // We must make two objects, because the ShepardMethod uses the ActiveScalars, as does the renderer!
    vtkSmartPointer<vtkPolyData> polydataToVisualize =
        vtkSmartPointer<vtkPolyData>::New();
    polydataToVisualize->SetPoints(points);
    polydataToVisualize->GetPointData()->SetScalars(vertexColors);

    vtkSmartPointer<vtkVertexGlyphFilter> vertexGlyphFilter =
        vtkSmartPointer<vtkVertexGlyphFilter>::New();
    vertexGlyphFilter->AddInputData(polydataToVisualize);
    vertexGlyphFilter->Update();

    //Create a mapper and actor
    vtkSmartPointer<vtkPolyDataMapper> vertsMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    vertsMapper->ScalarVisibilityOff();
    vertsMapper->SetInputConnection(vertexGlyphFilter->GetOutputPort());

    vtkSmartPointer<vtkActor> vertsActor =
        vtkSmartPointer<vtkActor>::New();
    vertsActor->SetMapper(vertsMapper);
    vertsActor->GetProperty()->SetColor(1, 0, 0);
    vertsActor->GetProperty()->SetPointSize(3);

//此时点的颜色为(1,0,0);
//vertsMapper->ScalarVisibilityOff();注释后,颜色为黑白。

3.2 通过顶点插值轮廓

// Create a shepard filter to interpolate the vertices over a regularized image grid
    vtkSmartPointer<vtkShepardMethod> shepard = vtkSmartPointer<vtkShepardMethod>::New();
    shepard->SetInputData(polydataToProcess);
    shepard->SetSampleDimensions(2, 2, 2);
    shepard->SetModelBounds(100, 300, -10, 10, -10, 10);
    shepard->SetMaximumDistance(1);

    // Contour the shepard generated image at 3 isovalues
    // The accuracy of the results are highly dependent on how the shepard filter is set up
    vtkSmartPointer<vtkContourFilter> contourFilter = vtkSmartPointer<vtkContourFilter>::New();
    /*contourFilter->SetNumberOfContours(3);
    contourFilter->SetValue(0, 0.25);
    contourFilter->SetValue(1, 0.50);
    contourFilter->SetValue(2, 0.75);*/
    contourFilter->SetNumberOfContours(4);
    contourFilter->SetValue(0, 0.2);
    contourFilter->SetValue(1, 0.4);
    contourFilter->SetValue(2, 0.6);
    contourFilter->SetValue(3, 0.8);
    contourFilter->SetInputConnection(shepard->GetOutputPort());
    contourFilter->Update();

完整示例代码

    // Create a set of vertices (polydata)
    vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(100.0, 0.0, 0.0);
    points->InsertNextPoint(300.0, 0.0, 0.0);

    // Setup colors
    unsigned char white[3] = { 255, 255, 255 };
    unsigned char black[3] = { 0, 0, 0 };

    vtkSmartPointer<vtkUnsignedCharArray> vertexColors =
        vtkSmartPointer<vtkUnsignedCharArray>::New();
    vertexColors->SetNumberOfComponents(3);
    vertexColors->SetName("Colors");
    vertexColors->InsertNextTuple3(black[0], black[1], black[2]);
    vertexColors->InsertNextTuple3(white[0], white[1], white[2]);

    // Create a scalar array for the pointdata, each value represents the distance
    // of the vertices from the first vertex
    vtkSmartPointer<vtkFloatArray> values =
        vtkSmartPointer<vtkFloatArray>::New();
    values->SetNumberOfComponents(1);
    values->SetName("Values");
    values->InsertNextValue(0.0);
    values->InsertNextValue(1.0);

    // We must make two objects, because the ShepardMethod uses the ActiveScalars, as does the renderer!
    vtkSmartPointer<vtkPolyData> polydataToProcess =
        vtkSmartPointer<vtkPolyData>::New();
    polydataToProcess->SetPoints(points);
    polydataToProcess->GetPointData()->SetScalars(values);

    vtkSmartPointer<vtkPolyData> polydataToVisualize =
        vtkSmartPointer<vtkPolyData>::New();
    polydataToVisualize->SetPoints(points);
    polydataToVisualize->GetPointData()->SetScalars(vertexColors);

    vtkSmartPointer<vtkVertexGlyphFilter> vertexGlyphFilter =
        vtkSmartPointer<vtkVertexGlyphFilter>::New();
    vertexGlyphFilter->AddInputData(polydataToVisualize);
    vertexGlyphFilter->Update();

    //Create a mapper and actor
    vtkSmartPointer<vtkPolyDataMapper> vertsMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    vertsMapper->ScalarVisibilityOff();
    vertsMapper->SetInputConnection(vertexGlyphFilter->GetOutputPort());

    vtkSmartPointer<vtkActor> vertsActor =
        vtkSmartPointer<vtkActor>::New();
    vertsActor->SetMapper(vertsMapper);
    vertsActor->GetProperty()->SetColor(1, 0, 0);
    vertsActor->GetProperty()->SetPointSize(3);


    // Create a shepard filter to interpolate the vertices over a regularized image grid
    vtkSmartPointer<vtkShepardMethod> shepard = vtkSmartPointer<vtkShepardMethod>::New();
    shepard->SetInputData(polydataToProcess);
    shepard->SetSampleDimensions(2, 2, 2);
    shepard->SetModelBounds(100, 300, -10, 10, -10, 10);
    shepard->SetMaximumDistance(1);

    // Contour the shepard generated image at 3 isovalues
    // The accuracy of the results are highly dependent on how the shepard filter is set up
    vtkSmartPointer<vtkContourFilter> contourFilter = vtkSmartPointer<vtkContourFilter>::New();
    /*contourFilter->SetNumberOfContours(3);
    contourFilter->SetValue(0, 0.25);
    contourFilter->SetValue(1, 0.50);
    contourFilter->SetValue(2, 0.75);*/
    contourFilter->SetNumberOfContours(4);
    contourFilter->SetValue(0, 0.2);
    contourFilter->SetValue(1, 0.4);
    contourFilter->SetValue(2, 0.6);
    contourFilter->SetValue(3, 0.8);
    contourFilter->SetInputConnection(shepard->GetOutputPort());
    contourFilter->Update();

    //Create a mapper and actor for the resulting isosurfaces
    vtkSmartPointer<vtkPolyDataMapper> contourMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    contourMapper->SetInputConnection(contourFilter->GetOutputPort());
    contourMapper->ScalarVisibilityOn();
    contourMapper->SetColorModeToMapScalars();

    vtkSmartPointer<vtkActor> contourActor =
        vtkSmartPointer<vtkActor>::New();
    contourActor->SetMapper(contourMapper);
    contourActor->GetProperty()->SetAmbient(1);
    contourActor->GetProperty()->SetSpecular(0);
    contourActor->GetProperty()->SetDiffuse(0);

    // Report the results of the interpolation
    double* range = contourFilter->GetOutput()->GetScalarRange();

    std::cout << "Shepard interpolation:" << std::endl;
    std::cout << "contour output scalar range: " << range[0] << ", " << range[1] << std::endl;

    vtkIdType nCells = contourFilter->GetOutput()->GetNumberOfCells();
    double bounds[6];
    for (vtkIdType i = 0; i < nCells; ++i)
    {
        if (i % 2) // each isosurface value only has 2 cells to report on the odd ones
        {
            contourFilter->GetOutput()->GetCellBounds(i, bounds);
            std::cout << "cell " << i << ", x position: " << bounds[0] << std::endl;
        }
    }

    // Create a transfer function to color the isosurfaces
    vtkSmartPointer<vtkColorTransferFunction> lut =
        vtkSmartPointer<vtkColorTransferFunction>::New();
    lut->SetColorSpaceToRGB();
    lut->AddRGBPoint(range[0], 0, 0, 0);//black
    lut->AddRGBPoint(range[1], 1, 1, 1);//white
    lut->SetScaleToLinear();

    contourMapper->SetLookupTable(lut);

    // Create a renderer, render window and interactor
    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    renderer->GradientBackgroundOn();
    renderer->SetBackground(0, 0, 1);
    renderer->SetBackground2(1, 0, 1);

    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    renderer->AddActor(contourActor);
    renderer->AddActor(vertsActor);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // Position the camera so that the image produced is viewable
    vtkCamera* camera = renderer->GetActiveCamera();
    camera->SetPosition(450, 100, 100);
    camera->SetFocalPoint(200, 0, 0);
    camera->SetViewUp(0, 0, 1);

    renderWindow->Render();
    renderWindowInteractor->Start();

结论:

? ? ? ? 主要介绍背景创建的几种方法

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的赞赏是我的最最最最大的动力(^U^)ノ~YO

文章来源:https://blog.csdn.net/qq_40041064/article/details/135461924
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。