在VTK TCL脚本教程中,经常看到这样的系数设置:
quadric->SetCoefficients( .5, 1, .2, 0, .1, 0, 0, .2, 0, 0 );

我们需要知道,三元二次方程代表了空间中的曲面:




可以看出,这里有10个系数,他们对应着Coefficients。



#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkContourFilter.h>
#include <vtkNamedColors.h>
#include <vtkOutlineFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkQuadric.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSampleFunction.h>

int main( int, char *[] )
{
    vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

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

    vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer(aren);

    vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);

    //
    // Create surfaces F(x,y,z) = constant
    //
    // Sample quadric function
    vtkSmartPointer<vtkQuadric> quadric =
        vtkSmartPointer<vtkQuadric>::New();
    // 0.5 * x^2 + 1 * y^2 + 0.2 * z^2 + 0 * xy + 0.1 * yz + 0 * zx + 0 * x + 0.2 * y + 0 * z + 0 = 0
    quadric->SetCoefficients( .5, 1, .2, 0, .1, 0, 0, .2, 0, 0 );
    vtkSmartPointer<vtkSampleFunction> sample =
        vtkSmartPointer<vtkSampleFunction>::New();
    sample->SetSampleDimensions( 50, 50, 50 );
    sample->SetImplicitFunction( quadric );

    vtkSmartPointer<vtkContourFilter> contour =
        vtkSmartPointer<vtkContourFilter>::New();
    contour->SetInputConnection( sample->GetOutputPort() );
    contour->GenerateValues( 6, 0, 2 );

    vtkSmartPointer<vtkPolyDataMapper> contourMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
    contourMapper->SetInputConnection(contour->GetOutputPort());
    contourMapper->SetScalarRange( 0, 2 );

    vtkSmartPointer<vtkActor> contourActor =
        vtkSmartPointer<vtkActor>::New();
    contourActor->SetMapper(contourMapper);

    // Create outline
// vtkSmartPointer<vtkOutlineFilter> outline =
// vtkSmartPointer<vtkOutlineFilter>::New();
// outline->SetInputConnection( sample->GetOutputPort() );

// vtkSmartPointer<vtkPolyDataMapper> outlineMapper =
// vtkSmartPointer<vtkPolyDataMapper>::New();
// outlineMapper->SetInputConnection(outline->GetOutputPort());

// vtkSmartPointer<vtkActor> outlineActor =
// vtkSmartPointer<vtkActor>::New();
// outlineActor->SetMapper( outlineMapper );
// outlineActor->GetProperty()->SetColor(colors->GetColor3d("Brown").GetData());
// outlineActor->GetProperty()->SetLineWidth(3.0);

    //
    // Rendering stuff
    //
    aren->SetBackground(colors->GetColor3d("SlateGray").GetData());
    aren->AddActor(contourActor);
    //aren->AddActor(outlineActor);

    aren->ResetCamera();
    aren->GetActiveCamera()->Azimuth(30);
    aren->GetActiveCamera()->Elevation(30);

    renWin->SetSize(640, 512);
    renWin->Render();

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

    return EXIT_SUCCESS;
} 

上面例子中的系数对应的方程是:
ContourPlot3D[0.5 * x^2 + 1 * y^2 + 0.2 * z^2 + 0 * x*y + 0.1 * y*z + 0 * z*x + 0 * x + 0.2 * y + 0 * z + 0 == 0, {x, 0, 50}, {y, 0, 50}, {z, 0, 50}]
但为什么是空呢?
(在mathematics上绘图而成)


使用采样的方法研究这个三元二次函数,得到的图像是这样的:




Here is code:

package require vtk
package require vtkinteraction

vtkQuadric quadric
    quadric SetCoefficients .5 1 .2 0 .1 0 0 .2 0 0
vtkSampleFunction sample
    sample SetSampleDimensions 50 50 50
    sample SetImplicitFunction quadric
    sample ComputeNormalsOff
vtkDataSetMapper dataMapper
    dataMapper SetInputConnection [sample GetOutputPort]
vtkActor dataActor
    dataActor SetMapper dataMapper

# The outline gives context to the original data.
vtkOutlineFilter outline
    outline SetInputConnection [sample GetOutputPort]
vtkPolyDataMapper outlineMapper
    outlineMapper SetInputConnection [outline GetOutputPort]
vtkActor outlineActor
    outlineActor SetMapper outlineMapper
set outlineProp [outlineActor GetProperty]
eval $outlineProp SetColor 0 0 0

vtkRenderer ren1
vtkRenderWindow renWin
    renWin AddRenderer ren1
vtkRenderWindowInteractor iren
    iren SetRenderWindow renWin


#ren1 AddActor outlineActor
ren1 AddActor dataActor
ren1 SetBackground 1 1 1
renWin SetSize 500 500
ren1 ResetCamera
[ren1 GetActiveCamera] Zoom 1.5
iren Initialize

iren AddObserver UserEvent {wm deiconify .vtkInteract}

# prevent the tk window from showing up then start the event loop
wm withdraw .
iren Start

分类: C plus plusTCL

4 条评论

Sannyjex · 2018年12月7日 下午3:56

Make a more new posts please 🙂
___
Sanny

    theArcticOcean · 2018年12月7日 下午10:57

    Thank you for your comments, I will do it.

【VTK】vtkExtractGeometry与vtkImplicitBoolean的简单使用 | theArcticOcean · 2018年11月17日 下午5:43

[…] 【vtk】vtk教程中的三元二次方程 […]

【VTK】Subsampling Image Data | theArcticOcean · 2018年12月2日 上午11:59

[…] 关于例子的三元方程,之前的文章也有介绍: vtk教程中的三元二次方程 […]

发表评论

电子邮件地址不会被公开。 必填项已用*标注