Visualizing mathematical functions by generating custom meshes using FireMonkey(很美)
Abstract: This article discusses how you can generate your own 3-dimensional mesh for visualizing mathematical functions using Delphi XE2 and FireMonkey.
Prerequisites!
This article assumes that you are familiar with the basics of 3D graphics, including meshes and textures.
The goal!
The goal is to graph a function like sin(x*x+z*z)/(x*x+z*z) in three dimensions using brilliant colors, as the image below shows:
Generating the mesh
The easiest way to generate a mesh is to use the Data.Points and Data.TriangleIndices of the TMesh object. However, these two properties are strings, and they get parsed in order to generate the mesh at runtime (and design time if populated at design time). This parsing is pretty time consuming, in fact, in this particular case about 65 times as slow as using the internal buffers. Therefore we will instead be using the non-published properties Data.VertexBuffer and Data.IndexBuffer.
In our example we will iterate along the X-axis from -30 to +30, and the same for the Z-axis. The function we're graphing gives us the value for Y for each point.
Step 1: Generating the wire frame
The image below shows a sparse wire frame representing the surface f = exp(sin x + cos z). Shown in red is one of the squares. Each square gets split into two triangles in order to generate the mesh. The mesh is simply built up from all of the triangles that we get when we iterate over the XZ plane.
We name the corners of the square P0, P1, P2 and P3:
The two triangles now become (P1,P2,P3) and (P3,P0,P1).
Given that u is somewhere on the X-axis, v is somewhere on the Z-axis, and that d is our delta step, the code to set up these four points in the XZ-plane becomes:
P[0].x := u;
P[0].z := v; P[1].x := u+d;
P[1].z := v; P[2].x := u+d;
P[2].z := v+d; P[3].x := u;
P[3].z := v+d;
Now we calculate the corresponding function values for the Y component of each point. f is our function f(x,z).
P[0].y := f(P[0].x,P[0].z);
P[1].y := f(P[1].x,P[1].z);
P[2].y := f(P[2].x,P[2].z);
P[3].y := f(P[3].x,P[3].z);
The points are now fully defined in all three dimensions. Next, we plug them into the mesh.
withVertexBufferdobegin
Vertices[0] := P[0];
Vertices[1] := P[1];
Vertices[2] := P[2];
Vertices[3] := P[3];
end;
That part was easy. Now we need to tell the mesh which points make up which triangles. We do that like so:
// First triangle is (P1,P2,P3)
IndexBuffer[0] := 1;
IndexBuffer[1] := 2;
IndexBuffer[2] := 3;// Second triangle is (P3,P0,P1)
IndexBuffer[3] := 3;
IndexBuffer[4] := 0;
IndexBuffer[5] := 1;
Step 2: Generating the texture
In order to give the mesh some color, we create a texture bitmap that looks like this:

This is simply a HSL color map where the hue goes from 0 to 359 degrees. The saturation and value are fixed.
The code to generate this texture looks like this:
BMP := TBitmap.Create(1,360);// This is actually just a line
fork := 0to359do
BMP.Pixels[0,k] := HSLtoRGB(k/360,0.75,0.5);
Step 3: Mapping the texture onto the wire frame
Finally, we need to map the texture onto the mesh. This is done using the TexCoord0 array. Each item in the TexCoord0 array is a point in a square (0,0)-(1,1) coordinate system. Since we're mapping to a texture that is just a line, our x-coordinate is always 0. The y-coordinate is mapped into (0,1), and the code becomes:
withVertexBufferdobegin
TexCoord0[0] := PointF(0,(P[0].y+35)/45);
TexCoord0[1] := PointF(0,(P[1].y+35)/45);
TexCoord0[2] := PointF(0,(P[2].y+35)/45);
TexCoord0[3] := PointF(0,(P[3].y+35)/45);
end;
Putting it all together
The full code to generate the entire mesh is listed below:
functionf(x,z : Double) : Double;
var
temp : Double;
begin
temp := x*x+z*z;
iftemp < Epsilonthen
temp := Epsilon; Result := -2000*Sin(temp/180*Pi)/temp;
end;procedureTForm1.GenerateMesh;
const
MaxX = 30;
MaxZ = 30;
var
u, v : Double;
P :array[0..3]ofTPoint3D;
d : Double;
NP, NI : Integer;
BMP : TBitmap;
k : Integer;
begin
Mesh1.Data.Clear; d := 0.5; NP := 0;
NI := 0; Mesh1.Data.VertexBuffer.Length := Round(2*MaxX*2*MaxZ/d/d)*4;
Mesh1.Data.IndexBuffer.Length := Round(2*MaxX*2*MaxZ/d/d)*6; BMP := TBitmap.Create(1,360);
fork := 0to359do
BMP.Pixels[0,k] := CorrectColor(HSLtoRGB(k/360,0.75,0.5)); u := -MaxX;
whileu < MaxXdobegin
v := -MaxZ;
whilev < MaxZdobegin
// Set up the points in the XZ plane
P[0].x := u;
P[0].z := v;
P[1].x := u+d;
P[1].z := v;
P[2].x := u+d;
P[2].z := v+d;
P[3].x := u;
P[3].z := v+d;// Calculate the corresponding function values for Y = f(X,Z)
P[0].y := f(Func,P[0].x,P[0].z);
P[1].y := f(Func,P[1].x,P[1].z);
P[2].y := f(Func,P[2].x,P[2].z);
P[3].y := f(Func,P[3].x,P[3].z);withMesh1.Datadobegin
// Set the points
withVertexBufferdobegin
Vertices[NP+0] := P[0];
Vertices[NP+1] := P[1];
Vertices[NP+2] := P[2];
Vertices[NP+3] := P[3];
end;// Map the colors
withVertexBufferdobegin
TexCoord0[NP+0] := PointF(0,(P[0].y+35)/45);
TexCoord0[NP+1] := PointF(0,(P[1].y+35)/45);
TexCoord0[NP+2] := PointF(0,(P[2].y+35)/45);
TexCoord0[NP+3] := PointF(0,(P[3].y+35)/45);
end;// Map the triangles
IndexBuffer[NI+0] := NP+1;
IndexBuffer[NI+1] := NP+2;
IndexBuffer[NI+2] := NP+3;
IndexBuffer[NI+3] := NP+3;
IndexBuffer[NI+4] := NP+0;
IndexBuffer[NI+5] := NP+1;
end; NP := NP+4;
NI := NI+6; v := v+d;
end;
u := u+d;
end; Mesh1.Material.Texture := BMP;
end;
Demo application
You can find my demo application that graphs 5 different mathematical functions in CodeCentral. Here are a few screen shots from the application:
Contact
Please feel free to email me with feedback to aohlsson at embarcadero dot com
Visualizing mathematical functions by generating custom meshes using FireMonkey(很美)的更多相关文章
- Visualizing wave interference using FireMonkey(很美)
Visualizing wave interference using FireMonkey By: Anders Ohlsson Abstract: This article discusses ...
- Part 14 Mathematical functions in sql server
Part 29 Mathematical functions in sql server
- NVIDIA CG语言 函数之所有数学类函数(Mathematical Functions)
数学类函数(Mathematical Functions) abs(x) 返回标量和向量x的绝对值 如果x是向量,则返回每一个成员的绝对值 acos(x) 返回标量和向量x的反余弦 x的范围是[-1, ...
- Custom Grid Columns - FireMonkey Guide
原文 http://monkeystyler.com/guide/Custom-Grid-Columns ack to FireMonkey Topics As we saw in TGrid a F ...
- [翻译] Using Custom Functions in a Report 在报表中使用自己义函数
Using Custom Functions in a Report 在报表中使用自己义函数 FastReport has a large number of built-in standard ...
- [HIve - LanguageManual] Hive Operators and User-Defined Functions (UDFs)
Hive Operators and User-Defined Functions (UDFs) Hive Operators and User-Defined Functions (UDFs) Bu ...
- [Hive - Tutorial] Built In Operators and Functions 内置操作符与内置函数
Built-in Operators Relational Operators The following operators compare the passed operands and gene ...
- Generating Complex Procedural Terrains Using GPU
前言:感慨于居然不用tesselation也可以产生这么复杂的地形,当然致命的那个关于不能有洞的缺陷还是没有办法,但是这个赶脚生成的已经足够好了,再加上其它模型估 计效果还是比较震撼的.总之好文共分享 ...
- [中英双语] 数学缩写列表 (List of mathematical abbreviations)
List of mathematical abbreviations From Wikipedia, the free encyclopedia 数学缩写列表 维基百科,自由的百科全书 This ar ...
随机推荐
- 何为优秀的机器学习特征 zz
提供好的特征是机器学习任务中最重要的工作,那么何为优秀的机器学习特征?以及如何高效地组合这些特征? 以二分类问题为例,好的特征具有很好的区分性.例如学习任务是区分两种不同类型的狗:灰猎犬(Greyho ...
- 分页技术框架(Pager-taglib)学习三(pager-taglib中传递参数时中文乱码问题)
一.问题描述 问题: 使用<pg:param name="key" />标签传递中文参数时,会有乱码. 原因: 因为它默认是用gb2312来对添加的参数进行编码,如果你 ...
- 新标准C++程序设计读书笔记_类和对象
面向对象的程序设计方法 抽象:将某类客观事物共同特点(属性)归纳出来,形成一个数据结构(可以用多个变量描述事物的属性):将这类事物所能进行的行为也归纳出来,形成一个个函数,这些函数可以用来操作数据结构 ...
- 13个实用的Apache Rewrite重写规则
1.去掉域名中的www标记 复制代码 代码如下: RewriteCond %{HTTP_HOST} !^jb51\.net$ [NC]RewriteRule .? http://jb51.net%{R ...
- hdu 1018 Big Number 数学结论
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- dbutils使用---QueryRunner实现in批量查询
sql.append("AND a.").append(MchStore.STORE_PROVINCE_COL).append(" IN ("); for (i ...
- java全局变量使用
1.在多线程的作用下,全局变量可能被多个程序使用,如果有人修改,全局变量就被修改了,导致别人使用的时候,出现问题 2.解决方法: 全局变量改为私有变量. 或者把全局变量改为final类型,只能读取,不 ...
- Netty4.x中文教程系列(六) 从头开始Bootstrap
Netty4.x中文教程系列(六) 从头开始Bootstrap 其实自从中文教程系列(五)一直不知道自己到底想些什么.加上忙着工作上出现了一些问题.本来想就这么放弃维护了.没想到有朋友和我说百度搜索推 ...
- CRM认识的误区
众所周知,CRM,就是平时说的“客户关系管理”,指用CRM来管理 企业与客户之间的关系.纵观整个IT圈子,做CRM的厂商比比皆是,每个厂商都有自己的产品宣言,令人眼花缭乱.但是领很多老板们不解的是,我 ...
- 换个角度剖析iptables防火墙
这篇文章会尽量以通俗易懂的方式描述iptables的相关概念,请耐心的读完它. 防火墙相关概念 此处先描述一些相关概念. 从逻辑上讲.防火墙可以大体分为主机防火墙和网络防火墙. 主机防火墙:针对于单个 ...

