### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Surfaces  (Read 702 times)

#### ranny

• Jr. Member
• Posts: 53
##### Surfaces
« on: August 06, 2020, 11:03:56 am »
Hi,

Does anyone have a routine for producing a surface based on a set of data points that are, for example, elevations (Y) from a plane (X,Z)?  I have the bones of a graphical routine to plot it but I am struggling to come up with a bit of code to generate the data between point so as to achieve a smooth looking surface plot.

I can handle a bit of maths but not so good at understanding surfaces.  Any help greatly appreciated.

Thanks

#### marcov

• Global Moderator
• Hero Member
• Posts: 8719
• FPC developer.
##### Re: Surfaces
« Reply #1 on: August 06, 2020, 12:12:54 pm »
Comes with FPC !

In the FPC demoes, there is a voxel demo that generates a plasma and then implements a modex 2.5D view of it.

It is very oldschool dos, so it won't run under windows, but the generation of a plasma is fairly universal

See https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/demo/modex/voxel.pp?view=markup&revision=3&root=fpcbuild in case you have a lazarus installer that distributes an incomplete fpc

#### ranny

• Jr. Member
• Posts: 53
##### Re: Surfaces
« Reply #2 on: August 06, 2020, 12:41:22 pm »
Thanks for the info, but not sure I can use that, so thought I better explain a bit more what I am looking for....

I want to draw a surface where the surface will pass through every point and interpolate between the points to give a smooth curve/surface.  For 2D I use cubic splines which work very well.  I have a 3D polynomial least squares fit routine but it is not good at any points outside the dataset and also at higher powers the surface gets "wobbly".  What I intended to do was run a series of cubic splines along the X and Z planes which should work but would be cumbersome, so I was hoping that someone may already have a routine for taking the dataset and produce a set of equations to generate the qaudrilaterals for my OpenGL routine for plotting.  This plotting routine works well for the 3D polynomial data so I only need the routine to generate the points.  See the attached picture for the current plotting routine.

Hope that makes sense, would appreciate any help, thanks in advance.

#### winni

• Hero Member
• Posts: 1762
##### Re: Surfaces
« Reply #3 on: August 06, 2020, 01:39:58 pm »
Hi!

Not for the creating of equations but perhaps for "a series of cubic splines along the X and Z planes which should work but would be cumbersome":

The BGRABitmapPack contains the two spline functions

Code: Pascal  [Select][+][-]
1. function ComputeClosedSpline
2. function ComputeOpenedSpline
3.

Both functions can be used with the parameters

Code: Pascal  [Select][+][-]
1. TSplineStyle = (
2.     {** The curve is drawn inside the polygonal envelope without reaching the starting and ending points }
3.     ssInside,
4.     {** The curve is drawn inside the polygonal envelope and the starting and ending points are reached }
5.     ssInsideWithEnds,
6.     {** The curve crosses the polygonal envelope without reaching the starting and ending points }
7.     ssCrossing,
8.     {** The curve crosses the polygonal envelope and the starting and ending points are reached }
9.     ssCrossingWithEnds,
10.     {** The curve is outside the polygonal envelope (starting and ending points are reached) }
11.     ssOutside,
12.     {** The curve expands outside the polygonal envelope (starting and ending points are reached) }
13.     ssRoundOutside,
14.     {** The curve is outside the polygonal envelope and there is a tangeant at vertices (starting and ending points are reached) }
15.     ssVertexToSide,
16.     {** The curve is rounded using Bezier curves when the angle is less than or equal to 45° }
17.     ssEasyBezier);
18.

The BGRABitmapPack is available through the Online Package Manager.

Winni

#### ranny

• Jr. Member
• Posts: 53
##### Re: Surfaces
« Reply #4 on: August 07, 2020, 02:06:58 pm »
Thanks for the info.

I am getting the feeling though that these suggestions are going to display surfaces however by having the calculation routines will allow me to pick a point on the X.Z plane and get a calculated Y value, I don't know if these plotting routines would provide this.

I will maybe explore the maths a bit more and see if I can come up with a solution and let you know.

#### wp

• Hero Member
• Posts: 7521
##### Re: Surfaces
« Reply #5 on: August 07, 2020, 02:43:54 pm »
Are your data points in a regular x,y array? Or are they at random locations?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### Alexandr R

• New Member
• Posts: 20
##### Re: Surfaces
« Reply #6 on: August 08, 2020, 09:31:17 pm »
Hi. Look at the link (here in Russian).
http://www.inf.tsu.ru/library/DiplomaWorks/CompScience/2002/Holkin/diplom.pdf
Try to search for "mathematical algorithms for building a digital elevation model"

Привет. Посмотри по ссылке (здесь по русски) . Попробуй поискать "математические алгоритмы построения цифровой модели рельефа"

#### ranny

• Jr. Member
• Posts: 53
##### Re: Surfaces
« Reply #7 on: August 10, 2020, 08:40:08 am »
Are your data points in a regular x,y array? Or are they at random locations?

WP - All data points are in a regular array of strips of coordinates in the X.Z plane.  My plan was to construct a spline for the strips of data in the X direction for each value of Z and then for the Z direction for each value of X.  Then I have equations for all X and Z directions for which I can calculate any value.  At least that is the plan, and should work in theory but was hoping there was an existing solution.  The goal is not only to generate a surface but to be able to calculate any point on that surface.

Thanks.

#### wp

• Hero Member
• Posts: 7521
##### Re: Surfaces
« Reply #8 on: August 10, 2020, 09:54:16 am »
Are your data points in a regular x,y array? Or are they at random locations?
My plan was to construct a spline for the strips of data in the X direction for each value of Z and then for the Z direction for each value of X.  Then I have equations for all X and Z directions for which I can calculate any value.
I think you cannot separate the X from the Z direction if you want to get a smooth surface in both directions. I think that the wiki article https://en.wikipedia.org/wiki/Bicubic_interpolation contains all the math.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### avra

• Hero Member
• Posts: 1998
##### Re: Surfaces
« Reply #9 on: August 10, 2020, 10:25:50 am »
If you can live with colored 2D to represent a 3D dataset, then you can take a look at this:
https://forum.lazarus.freepascal.org/index.php/topic,46718.0.html
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

#### ranny

• Jr. Member
• Posts: 53
##### Re: Surfaces
« Reply #10 on: August 10, 2020, 04:30:54 pm »
WP - I have seen this article and others on bicubic interpolation however from what I can see this is a solution for image resizing and the articles show how, since it is a cubic, there are 4 coefficients in the X and Z direction giving 16 coefficients in total for an individual square plane.  In a 2D cubic spline when working with lots of points in a single curve, there are several cubic equations along the curve each with their own coefficients.  This gives slope and curvature continuity between each curve segment, but requires lots of groups of coefficients.   The benefit is a very tight fitting curve where there are big changes in Y relative to X.  This is a good explanation of piecewise splines.

https://people.cs.clemson.edu/~dhouse/courses/405/notes/splines.pdf

My original plan of having lots of piecewise splines in X direction and then in Z direction means that for any given X and Z will give a result that lies between original points.  I am sure I have read somewhere that this is a known method.

No matter what, its very intersting!