当前位置:网站首页>【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
2022-07-01 16:29:00 【beidou111】
原理:splishsplash中的MVC模式与GenericParameter
splishsplash中采用了MVC设计模式。该设计模式最大的特点就是将用户界面与后端数据计算分开,也就是俗称的“前端”和“后端”分开。实际上,只要是涉及到GUI或者图形学的程序,都几乎或多或少要有MVC设计模式。
MVC中的三个字母代表的是Model View Controller
Model负责数据的计算,View负责用户IO(也就是显示界面和接收用户输入),Controller负责两者的通讯。通过Controller,我们彻底截断了后端和前端。实现了系统的解耦。
splishsplash中的Model不必多说,就是计算的主体,可以认为是FluidModel等实例的计算数据。
View代表的则是GUI,目前默认采用的是IMGUI。
而Controller,则是GenericParamerter 这个第三方库。这个库是splishsplash项目组自己开发的另一个项目。在splishsplash中作为第三方库来使用。它的用处就是管理参数。只要你想要将Model中的参数与用户界面的参数相互通讯,就可以将GenericParameter作为你当前类的父类。GenericParameter就会负责和IMGUI相互沟通。那么,对我们代码开发者来说,只需要会用GenericParameter的API就可以实现与GUI之间的通讯了。
你可能会问为何要中间加一个中介,而不是直接去联通IMGUI呢?原因是1)GUI库有很多种选择,IMGUI,TweakBar(曾经的默认GUI)等等。假如我变更了GUI,那岂不是算法中的所有代码都要变?所以为了统一API,增加一个中介是有好处的。2)假如没有中介的Controller,两者沟通的代码,要么写在GUI的源文件里,要么写在算法的源文件里,两者都会造成很令人头疼的耦合问题。3)简化了使用。IMGUI有很多功能,但我们这里只需要管理参数就够了,多余的对我们来说反而是负担。
因此,下面我们只关注这个中介Controller(也就是GenericParameters)的API。
使用方式讲解
我们看一个例子,代码来自SurfaceTensionBase.cpp
SURFACE_TENSION = createNumericParameter("surfaceTension", "Surface tension coefficient", &m_surfaceTension);
setGroup(SURFACE_TENSION, "Surface tension");
setDescription(SURFACE_TENSION, "Coefficient for the surface tension computation");
RealParameter* rparam = static_cast<RealParameter*>(getParameter(SURFACE_TENSION));
rparam->setMinValue(0.0);
该代码的作用是:绑定参数m_surfaceTension到GUI上去,GUI中显示的参数叫做surfaceTension。
我们一行行分析:
第一行
SURFACE_TENSION = createNumericParameter("surfaceTension", "Surface tension coefficient", &m_surfaceTension);
createNumericParameter这个函数就是创建一个数值型的参数。这个参数的名字叫做surfaceTension(这个名字同样适用于json文件)。描述为Surface tension coefficient,所绑定的参数为m_surfaceTension(注意传递的是指针)。至于返回的那个整数型变量,是GUI的组号。必然是一个static的(因为要求静态生存期)
第二行
setGroup(SURFACE_TENSION, "Surface tension");
为了方便管理,参数通常要分组。这在GUI上面的效果就是分页。每个组就是单独的一页来显示。
第三行
setDescription(SURFACE_TENSION, "Coefficient for the surface tension computation");
鼠标悬浮的时候显示的描述。
第四行
RealParameter* rparam = static_cast<RealParameter*>(getParameter(SURFACE_TENSION));
用来设定最大最小值的。这是为了防止用户错误输入。
第五行
rparam->setMinValue(0.0);
设定最小值
我们来修改一下试验效果
刚才讲完了原理和用法,那么我们就尝试改动一些东西,看看是不是理论符合实践。
测试改名字和描述和初始值
第一行改为
SURFACE_TENSION = createNumericParameter("thisIsName", "This is Discription", &m_surfaceTension);
编译运行
我们发现确实如此
那么是否能通过json改变它的参数呢?目前默认是0.05
我们在一个json中更改
"Materials": [
{
"id": "Fluid",
"thisIsName": 1.11
}
],
运行
发现确实读入了参数
测试分组
第二行更改为
setGroup(SURFACE_TENSION, "A new group");
编译运行
当你选择任何SurfaceTension算法的时候就会出现新分组

并且我们之前的那个参数也被挪到新分页内了。
请注意:决定你的参数是否在新分页的,是你的参数所在的组(也就是组号SURFACE_TENSION所决定的)
例如原有的Boundary surface tension coeff就没有分到新租“A new group”
测试组描述
与之前那个不同,这个是针对组内每个变量的描述
setDescription(SURFACE_TENSION, "This is the group discription");

测试最大最小值
第五行更改为
rparam->setMinValue(1.0);
rparam->setMaxValue(5.0);
假如我们键入0.9,会自动弹回1.0
最大值同理
边栏推荐
- 数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
- How to use F1 to F12 correctly on laptop keyboard
- 数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
- SQL question brushing 584 Looking for user references
- Why is the pkg/errors tripartite library more recommended for go language error handling?
- sql刷题1050. 合作过至少三次的演员和导演
- Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
- Redis 分布式鎖
- UML tourism management system "suggestions collection"
- 独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
猜你喜欢

数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)

数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)

How to cancel automatic search and install device drivers for laptops

How to maintain the laptop battery

Introduction to software engineering - Chapter 6 - detailed design

What is the digital transformation of manufacturing industry

Template Engine Velocity Foundation
![[live broadcast appointment] database obcp certification comprehensive upgrade open class](/img/50/83a533f4e8a60f90e03b991385c08d.jpg)
[live broadcast appointment] database obcp certification comprehensive upgrade open class

你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费

Stegano in the world of attack and defense
随机推荐
Redis 分布式鎖
SystemVerilog structure (II)
Redis6.0 新功能
Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting
The difference between the lazy mode of singleton mode and the evil mode
What is the effect of choosing game shield safely in the game industry?
China sorbitol Market Forecast and investment strategy report (2022 Edition)
Comprehensively view the value of enterprise digital transformation
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
How wild are hackers' ways of making money? CTF reverse entry Guide
【flask入门系列】Cookie与Session
Buuctf gold III
为国产数据库添砖加瓦,StoneDB 一体化实时 HTAP 数据库正式开源!
【C语言基础】12 字符串
Hi Fun Summer, play SQL planner with starrocks!
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
C language input / output stream and file operation
数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)