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:

Hide image

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.

Hide image

We name the corners of the square P0, P1, P2 and P3:

Hide image

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.

with VertexBuffer do begin
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
for k := 0 to 359 do
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:

with VertexBuffer do begin
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:

function f(x,z : Double) : Double;
var
temp : Double;
begin
temp := x*x+z*z;
if temp < Epsilon then
temp := Epsilon; Result := -2000*Sin(temp/180*Pi)/temp;
end; procedure TForm1.GenerateMesh;
const
MaxX = 30;
MaxZ = 30;
var
u, v : Double;
P : array [0..3] of TPoint3D;
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);
for k := 0 to 359 do
BMP.Pixels[0,k] := CorrectColor(HSLtoRGB(k/360,0.75,0.5)); u := -MaxX;
while u < MaxX do begin
v := -MaxZ;
while v < MaxZ do begin
// 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); with Mesh1.Data do begin
// Set the points
with VertexBuffer do begin
Vertices[NP+0] := P[0];
Vertices[NP+1] := P[1];
Vertices[NP+2] := P[2];
Vertices[NP+3] := P[3];
end; // Map the colors
with VertexBuffer do begin
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:

Hide imageHide imageHide imageHide image

Contact

Please feel free to email me with feedback to aohlsson at embarcadero dot com

 
http://edn.embarcadero.com/article/42007

Visualizing mathematical functions by generating custom meshes using FireMonkey(很美)的更多相关文章

  1. Visualizing wave interference using FireMonkey(很美)

      Visualizing wave interference using FireMonkey By: Anders Ohlsson Abstract: This article discusses ...

  2. Part 14 Mathematical functions in sql server

    Part 29 Mathematical functions in sql server

  3. NVIDIA CG语言 函数之所有数学类函数(Mathematical Functions)

    数学类函数(Mathematical Functions) abs(x) 返回标量和向量x的绝对值 如果x是向量,则返回每一个成员的绝对值 acos(x) 返回标量和向量x的反余弦 x的范围是[-1, ...

  4. Custom Grid Columns - FireMonkey Guide

    原文 http://monkeystyler.com/guide/Custom-Grid-Columns ack to FireMonkey Topics As we saw in TGrid a F ...

  5. [翻译] Using Custom Functions in a Report 在报表中使用自己义函数

    Using Custom Functions in a Report  在报表中使用自己义函数   FastReport has a large number of built-in standard ...

  6. [HIve - LanguageManual] Hive Operators and User-Defined Functions (UDFs)

    Hive Operators and User-Defined Functions (UDFs) Hive Operators and User-Defined Functions (UDFs) Bu ...

  7. [Hive - Tutorial] Built In Operators and Functions 内置操作符与内置函数

    Built-in Operators Relational Operators The following operators compare the passed operands and gene ...

  8. Generating Complex Procedural Terrains Using GPU

    前言:感慨于居然不用tesselation也可以产生这么复杂的地形,当然致命的那个关于不能有洞的缺陷还是没有办法,但是这个赶脚生成的已经足够好了,再加上其它模型估 计效果还是比较震撼的.总之好文共分享 ...

  9. [中英双语] 数学缩写列表 (List of mathematical abbreviations)

    List of mathematical abbreviations From Wikipedia, the free encyclopedia 数学缩写列表 维基百科,自由的百科全书 This ar ...

随机推荐

  1. 点滴积累【C#】---C#实现上传照片到物理路径,并且将地址保存到数据库,

    效果: 思路: 首先,获取图片物理地址,然后进行判断将图片保存到文件夹下,再将图片的信息保存到数据库. 数据库: create table image1 ( ID ,) primary key, Im ...

  2. 调试JDK1.8源码的方法

    背景 在学习JDK源码的时候,免不了需要调试JDK的源码. 比如:想理解ConcurrentHashMap的put(K k, V v)方法,JDK自带的rt.jar文件是支持断点调试,但是却看不到变量 ...

  3. Atitit.Base64编码原理与实现设计

    Atitit.Base64编码原理与实现设计 1. Base64编码1 1.1. 为什么要用自己的base64编码方案1 2. Base64编码由来1 3. Base64编码原理1 3.1. 具体来说 ...

  4. Atitit。激活某个程序的api attilax总结

    Atitit.激活某个程序的api attilax总结 1. 设置当前某窗口为当前窗口,有几个步骤要做:1.得到窗口句柄FindWindow2.切换键盘输入焦点AttachThreadInput3.显 ...

  5. Python学习之warn()函数

    warn()函数位于warnings模块中,用来发出警告,或者忽略它或引发异常. def warn(message, category=None, stacklevel=, source=None) ...

  6. linux之grub的使用

    一.引言 昨天在家里在自己的电脑上安装CentOS7,一切都顺利,结果安装好重启,无法进入win7了,在grub的配置文件也添加了win7的条目,却提示 unkown command chainloa ...

  7. linux学习笔记29---命令watch

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...

  8. PHP标签

    1.PHP原标签 2.脚本标签 3.短标签 需要short_open_tag=on,默认情况下为on 4.asp标签 需要asp_tags为on,默认为off

  9. flume+kafka+storm打通过程

    0.有的地方我已经整理成脚本了,有的命令是脚本里面截取的 1.启动hadoop和yarn $HADOOP_HOME/sbin/start-dfs.sh;$HADOOP_HOME/sbin/start- ...

  10. 利用RPM和YUM安装软件

    安装软件事root的事,所以必须要以root身份登录! 假设我要安装一个文件名为rp-pppoe-3.5-32.1.i386.rpm的文件, 那么我们可以这样: rpm安装软件 rpm -ivh pa ...