首页 > 其他分享 >VTK—vtkImplicitFunction 隐函数

VTK—vtkImplicitFunction 隐函数

时间:2024-08-20 10:53:36浏览次数:8  
标签:SetValue 函数 VTK 0.5 textProperty slider vtkImplicitFunction include Create

通过这个例子可以直观理解隐函数是什么、在空间中怎么分布、怎么布尔运算。

1.完整代码

#include<vtkBox.h>
#include<vtkNew.h>
#include<vtkActor.h>
#include<vtkSphere.h>
#include<vtkAutoInit.h>
#include<vtkProperty.h>
#include<vtkRenderer.h>
#include<vtkImageData.h>
#include<vtkContourFilter.h>
#include<vtkImplicitBoolean.h>
#include<vtkDataSetMapper.h>
#include<vtkRenderWindow.h>
#include<vtkPolyDataMapper.h>
#include<vtkSampleFunction.h>
#include<vtkRenderWindowInteractor.h>

#include<vtkActor2D.h>
#include<vtkTextActor.h>
#include<vtkTextProperty.h>
#include<vtkCommand.h>
#include<vtkSliderWidget.h>
#include<vtkSliderRepresentation2D.h>

VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)


#include<QString>


class myCallback: public vtkCommand
{
public:
    static myCallback *New()   {
        return new myCallback;
    }

    void Execute(vtkObject *caller, unsigned long eventId, void *callData) override
    {
        vtkSliderWidget *widget = (vtkSliderWidget *)caller;
        vtkSliderRepresentation2D *slider = (vtkSliderRepresentation2D *)widget->GetRepresentation();
        m_contour1->SetValue(0, slider->GetValue());
        m_contour2->SetValue(0, slider->GetValue());
        m_contour3->SetValue(0, slider->GetValue());
    }

    vtkContourFilter *m_contour1;
    vtkContourFilter *m_contour2;
    vtkContourFilter *m_contour3;

private:
    myCallback() {
        m_contour1 = nullptr;
        m_contour2 = nullptr;
        m_contour3 = nullptr;
    }
    ~myCallback() {}
};


#define Create(type,name)\
    vtkNew<type> name;


int main()
{
    //两个隐函数,方盒子和球
    Create(vtkBox, box);
    box->SetBounds(-1, 1, -1, 1, -1, 1);
    Create(vtkSphere, sphere);
    sphere->SetRadius(1);
    sphere->SetCenter(1, 0, 0);

    //隐函数布尔加
    Create(vtkImplicitBoolean, add);
    add->SetOperationTypeToUnion();
    add->AddFunction(box);
    add->AddFunction(sphere);
    Create(vtkSampleFunction, addSample);
    addSample->SetModelBounds(-5, 5, -5, 5, -5, 5);
    addSample->SetSampleDimensions(50, 50, 50);
    addSample->SetImplicitFunction(add);
    Create(vtkContourFilter, contour1);
    contour1->SetInputConnection(addSample->GetOutputPort());
    contour1->SetValue(0, 0);
    Create(vtkDataSetMapper, mapper1);
    mapper1->SetInputConnection(contour1->GetOutputPort());
    Create(vtkActor, actor1);
    actor1->SetMapper(mapper1);
    actor1->GetProperty()->SetEdgeVisibility(true);


    //隐函数布尔异
    Create(vtkImplicitBoolean, diff);
    diff->SetOperationTypeToDifference();
    diff->AddFunction(box);
    diff->AddFunction(sphere);
    Create(vtkSampleFunction, diffSample);
    diffSample->SetModelBounds(-5, 5, -5, 5, -5, 5);
    diffSample->SetSampleDimensions(50, 50, 50);
    diffSample->SetImplicitFunction(diff);
    Create(vtkContourFilter, contour2);
    contour2->SetInputConnection(diffSample->GetOutputPort());
    contour2->SetValue(0, 0);
    Create(vtkDataSetMapper, mapper2);
    mapper2->SetInputConnection(contour2->GetOutputPort());
    Create(vtkActor, actor2);
    actor2->SetMapper(mapper2);
    actor2->GetProperty()->SetEdgeVisibility(true);

    //隐函数布尔交
    Create(vtkImplicitBoolean, section);
    section->SetOperationTypeToIntersection();
    section->AddFunction(box);
    section->AddFunction(sphere);
    Create(vtkSampleFunction, secSample);
    secSample->SetModelBounds(-5, 5, -5, 5, -5, 5);
    secSample->SetSampleDimensions(50, 50, 50);
    secSample->SetImplicitFunction(section);
    Create(vtkContourFilter, contour3);
    contour3->SetInputConnection(secSample->GetOutputPort());
    contour3->SetValue(0, 0);
    Create(vtkDataSetMapper, mapper3);
    mapper3->SetInputConnection(contour3->GetOutputPort());
    Create(vtkActor, actor3);
    actor3->SetMapper(mapper3);
    actor3->GetProperty()->SetEdgeVisibility(true);

    //左上角窗口
    Create(vtkRenderer, render1);
    render1->AddActor(actor1);
    render1->SetViewport(0, 0, 0.5, 1);
    //右上角窗口
    Create(vtkRenderer, render2);
    render2->AddActor(actor2);
    render2->SetViewport(0.5, 0.5, 1, 1);
    //右下角窗口
    Create(vtkRenderer, render3);
    render3->AddActor(actor3);
    render3->SetViewport(0.5, 0, 1, 0.5);
    //左上角窗口
    Create(vtkRenderer, render4);
    render4->SetViewport(0, 0, 0.5, 0.5);

    Create(vtkRenderWindow, window);
    window->AddRenderer(render1);
    window->AddRenderer(render2);
    window->AddRenderer(render3);
    window->AddRenderer(render4);
    Create(vtkRenderWindowInteractor, interactor);
    interactor->SetRenderWindow(window);

    //显示汉字的方式
    Create(vtkTextProperty, textProperty);
    textProperty->SetJustificationToCentered();
    textProperty->SetVerticalJustificationToCentered();
    textProperty->SetFontFamily(VTK_FONT_FILE);
    textProperty->SetFontFile("C:/WINDOWS/FONTS/MSYHL.TTC");

    //这里多renderer的坐标系统很迷惑
    Create(vtkTextActor, topLeft);
    topLeft->SetTextScaleModeToNone();
    topLeft->SetTextProperty(textProperty);
    topLeft->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
    topLeft->GetPositionCoordinate()->SetValue(0.2, 0.9);
    topLeft->SetInput("布尔加");
    render1->AddActor(topLeft);

    Create(vtkTextActor, topRight);
    topRight->SetTextScaleModeToNone();
    topRight->SetTextProperty(textProperty);
    topRight->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
    topRight->GetPositionCoordinate()->SetValue(0.2, 0.8);
    topRight->SetInput("布尔减");
    render2->AddActor(topRight);

    Create(vtkTextActor, bottemRight);
    bottemRight->SetTextScaleModeToNone();
    bottemRight->SetTextProperty(textProperty);
    bottemRight->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
    bottemRight->GetPositionCoordinate()->SetValue(0.2, 0.8);
    bottemRight->SetInput("布尔交");
    render3->AddActor(bottemRight);

    //滑动条
    Create(vtkSliderWidget, widget);
    Create(vtkSliderRepresentation2D, slider);
    slider->SetMinimumValue(-1);
    slider->SetMaximumValue(5);
    slider->SetValue(0);
    widget->SetRepresentation(slider);
    widget->SetAnimationModeToOff();
    widget->SetInteractor(interactor);
    widget->SetDefaultRenderer(render4);
    //滑动条在右侧窗口不显示,有点疑惑
    slider->GetPoint1Coordinate()->SetCoordinateSystemToNormalizedViewport();
    slider->GetPoint1Coordinate()->SetValue(0.2, 0.5);
    slider->GetPoint2Coordinate()->SetCoordinateSystemToNormalizedViewport();
    slider->GetPoint2Coordinate()->SetValue(0.8, 0.5);
    widget->EnabledOn();

    //回调
    Create(myCallback, callback);
    callback->m_contour1 = contour1;
    callback->m_contour2 = contour2;
    callback->m_contour3 = contour3;
    widget->AddObserver(vtkCommand::InteractionEvent, callback);


    interactor->Start();
    return 0;
}

1.隐函数的布尔运算

void SetOperationTypeToUnion();             //并集
void SetOperationTypeToIntersection();      //交集
void SetOperationTypeToDifference();        //差集
void SetOperationTypeToUnionOfMagnitudes(); //尚未理解

2.对隐函数空间采样

Create(vtkSampleFunction, diffSample);
diffSample->SetModelBounds(-5, 5, -5, 5, -5, 5);  //采样空间范围
diffSample->SetSampleDimensions(50, 50, 50);      //XYZ方向点数
diffSample->SetImplicitFunction(diff);            
//输入隐函数,对空间中的点坐标按照隐函数规则进行计算

3.提取等值面

Create(vtkContourFilter, contour2);
contour2->SetInputConnection(diffSample->GetOutputPort());
contour2->SetValue(0, 0);

初始值是0,就是隐函数对应的默认特征。后面通过滑动条改变这个值,可以看到几何特征变大变小。其他值代表了对默认特征的偏离程度,负值向内,正值向外。

4.四个视口显示

//左上角窗口
    Create(vtkRenderer, render1);
    render1->AddActor(actor1);
    render1->SetViewport(0, 0, 0.5, 1);
    //右上角窗口
    Create(vtkRenderer, render2);
    render2->AddActor(actor2);
    render2->SetViewport(0.5, 0.5, 1, 1);
    //右下角窗口
    Create(vtkRenderer, render3);
    render3->AddActor(actor3);
    render3->SetViewport(0.5, 0, 1, 0.5);
    //左上角窗口
    Create(vtkRenderer, render4);
    render4->SetViewport(0, 0, 0.5, 0.5);

5.显示汉字需要特殊处理

 //显示汉字的方式
    Create(vtkTextProperty, textProperty);
    textProperty->SetJustificationToCentered();
    textProperty->SetVerticalJustificationToCentered();
    textProperty->SetFontFamily(VTK_FONT_FILE);
    textProperty->SetFontFile("C:/WINDOWS/FONTS/MSYHL.TTC");

5.1将字体家族设置为VTK_FONT_FILE,表示从文件获取

5.2获取电脑中的字体文件,此处是微软雅黑,C:/WINDOWS/FONTS/MSYHL.TTC

5.3这样处理之后就可以愉快的输入汉字了

6.多个Renderer中的定位很迷惑

topLeft->GetPositionCoordinate()->SetValue(0.2, 0.9);
topRight->GetPositionCoordinate()->SetValue(0.2, 0.8);
bottemRight->GetPositionCoordinate()->SetValue(0.2, 0.8);
slider->GetPoint1Coordinate()->SetValue(0.2, 0.5);

与单个renderer中理解的不一致,还没搞明白。 

 

 

 

 

 

标签:SetValue,函数,VTK,0.5,textProperty,slider,vtkImplicitFunction,include,Create
From: https://blog.csdn.net/weixin_69505365/article/details/141351733

相关文章

  • C语言 函数
    数组概念C语言中得函数就是一个完成某项特定的任务的一小段代码。这段代码是有特殊的写法和调试方式的。C语言的程序是由无数个小的函数组成的C语言中我们会用到两种函数:库函数自定义函数库函数1.标准库和头文件C语言中是不会提供任何函数的,C语言的国际标准(ANSIC)规定了......
  • 关系代数、函数依赖、Armstrong公理及软考试题解析
    注:本文面向于软考高级—系统架构设计师,具体来说是数据库部分,知识点偏零碎化。想要系统学习数据库原理的,可考虑去看《数据库原理》,清华大学出版社或其他出版社皆可。概述概念关系,就是二维表。特性:列不可分性:关系中每一列都是不可再分的属性,即不能出现如下复合属性行列无序性:......
  • 详解Python 66 个内置函数!附代码
    Python有许多内置函数,共有66个。以下是这些内置函数的详细解释和示例代码:1.abs(x): 返回一个数的绝对值。x = -10print(abs(x))  # 输出:102.all(iterable): 如果可迭代对象中所有元素都为真,则返回True;否则返回False。iterable = [True, True, False]print(al......
  • 字节显示转换操作类 - C#小函数类推荐
          此文记录的是一个将字节大小的值转换为KB、MB等显示的小函数。/***字节显示转换操作类AustinLiu刘恒辉ProjectManagerandSoftwareDesignerE-Mail:[email protected]:http://lzhdim.cnblogs.comDate:2024-01-1515:......
  • 【数据结构与算法第一章】编程基础:变量与数据类型、指针、结构体、数组与链表、程序结
    目录【数据结构与算法第一章】编程基础1.1变量与数据类型1.2指针1.3结构体1.4数组和链表1.5程序结构1.6函数中参数的传递1.7C语言中运算符的含义【数据结构与算法第一章】编程基础1.1变量与数据类型变量:    ①在C语言中,所有变量必须先声明后使用......
  • Oracle数据库必学!超实用的9个字符串处理函数
    Oracle查询语句中的单行函数,特别是一些常用的字符串处理函数。1.ConCAT函数作用:将两个字符串连接在一起,生成一个新的字符串。使用方法:concat(字符串1,字符串2)示例:SELECTconcat('Hello,','World')FROMDUAL;结果为:“Hello,World”。2.CHR与ASCII函数CHR函数......
  • 信号与槽函数
    1.信号与槽函数的作用(通信机制)用来在组件和组件,组件和窗口之间通信温故知新:Linux中进程间通信signal(你要捕捉的信,函数指针)信号:QT中给每个组件都定义了信号,每个信号都有它特定的触发条件QT中所有的信号,本质上都是个函数,但是有点特殊,信号只有声明,没有源代码比如:以......
  • C语言程序设计-[24] 程序结构与函数定义
    1、C程序基本结构。2、函数的分类。3、函数的定义。无参函数的代码示例如下:注:调用的函数都是无返回值的,所以使用了void。有参函数的代码示例如下:注1:调用的函数是有参数的,main()函数的a,b,c值分别传给了average()函数的x,y,z;注2:调用的函数是有返回值的,且返......
  • 函数式编程
    函数式编程-Stream流1.概述1.1为什么学?能够看懂公司里的代码大数量下处理集合效率高代码可读性高消灭嵌套地狱下面是没有使用函数式编程的代码://查询未成年作家的评分在70以上的书籍由于洋流影响所以作家和书籍可能出现重复,需要进行去重List<Book>bookList=newA......
  • day03JS-函数
    1.什么是函数函数具有某种特定功能的代码块。函数其实本质也是一种数据,属于对象数据类型。2.为什么要有函数解决代码的冗余问题,形成代码复用。可以把整个代码项目,通过函数模块化。封装代码,让函数内部的代码对外部不可见。3.函数的组成函数的声明:function函数名(参数1,......