|
Introduction
This article describes a simple method of rendering 3D surfaces on a 2D plane. It doesn't use OpenGL or DirectX or stuff like that. It just utilizes the power of the CPU. Simplicity has, of course, its price - the rendering process isn't as fast as when using hardware acceleration.
Its application varies from simply admiring beautiful 3D surfaces to data visualization purposes, to stuff whatever one needs. I've used it in programs written for courses of Optimization Methods, Identification, Calculus (Mathematical Analysis), and Dimensional Analysis.
Properties
Density - Size of the spanning mesh.
PenColor - Color of the drawing pen. Used to draw meshes.
StartPoint - Plotting start point.
EndPoint - Plotting end point.
Function - Function used to calculate surface vertices.
ColorSchema - Color schema assigned to mesh.
Methods
ReCalculateTransformationsCoeficients - Recalculates transformations' coefficients on the basis of new parameters.
Project - Performs projection. Calculates screen coordinates for the 3D point.
RenderSurface - Main method. Render the surface on given graphics.
Points of Interest
Additional classes ColorSchema and CompiledFunction are interesting parts of the code. The first one encapsulates an array of Color entries. It has some predefined color palettes. The other one compiles a given formula to a function delegate, providing simple parser services.
Usage
Surface3DRenderer sr = new Surface3DRenderer(70, 35, 40,
0, 0, ClientRectangle.Width,
ClientRectangle.Height, 0.5, 0, 0);
sr.ColorSchema = new ColorSchema(120);
sr.SetFunction("sin(x1)*cos(x2)/(sqrt(sqrt(x1*x1+x2*x2))+1)*10");
Use this to actually render something:
sr.RenderSurface(e.Graphics);
And this, whenever plotting area size changes:
sr.ReCalculateTransformationsCoeficients(70, 35, 40, 0, 0,
ClientRectangle.Width, ClientRectangle.Height, 0.5, 0, 0);
Copyright
History
- 22.02.2007 - first version.
- 23.02.2007 - sample images added.
Sample (application)
Below, I'm publishing screenshots from an application that was written for my academic course of Optimization Methods (finding the minimum/maximum of functions). Labels are in Polish, but I hope that they are self-explaining.

Entering the formula:

Finding functions' minimum using the Greatest Slope method:

| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh) | FirstPrevNext |
|
 |
|
|
Dear Michal,
After I read your passage, I think you can give me some inspirations. Would you please give me some hints?
I wanna display a path within a graph, which shows x,y,z axis clearly, but I don't want to do this task by matlab.
The most important thing is how to show the path form the first point to the second point until the last point which resembles animation.
In addition, the x, y, z coordinates of various points are stored in three txt files where each of the txt file contains the coordinates of one axis only.
Thank you very much!
Best regards, John
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Please can you share how you generated the static color schemes (especially, the lines, pink, flag, colorcube)? I could not get copper and some working. Do you know how to generate the bone color scheme in matlab[^]?
Best regards, Paul.
Jesus Christ is LOVE! Please tell somebody.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
This aspect is full presented in code attached to this article. About Matlab, try the following line: [x, y] =meshgrid(-2:.2:2); surf(x,y,sin(x)+cos(y)) colormap bone
Regards
Michał Bryłka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks for the reply.
Sorry, I could not explain it well. You had some list of color schemes, such as the ColorSchema.Jet, ColorSchema.Lines etc.
Did you create this by programming or by hand? If by programming, I wish you could share that part of the code with me.
The link I provide was to give you a picture of the matlab bone colormap. I wish to generate that color schema by codes too, but could not.
Best regards, Paul.
Jesus Christ is LOVE! Please tell somebody.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
Everything about my color schemata can be found in enclosed "ColorSchema.cs" file.
As far as Matlab is concerned, bone color map should only be used in medical imaging, because it doesn't look good in other applications. However one can use it everywhere, like in code I've provided in my last previous reply. Did it solve your problem?
Regards
Michał Bryłka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Michal Brylka wrote: Everything about my color schemata can be found in enclosed "ColorSchema.cs" file.
Sorry for the inconvenience, I still could not found the method to generate the list of colors, for example listed in the ColorSchema.Colorcube of the "ColorSchema.cs", and still do not understand how you did it.
Best regards, Paul.
Jesus Christ is LOVE! Please tell somebody.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
ok, look at this:
public static readonly ColorSchema Autumn = new ColorSchema(...
this invokes the following class constructor:
protected ColorSchema(byte[,] schema, string schemaName) { this.schema = new Color[schema.GetLength(0)]; for (int i = 0; i < this.schema.Length; i++) { this.schema[i] = Color.FromArgb(schema[i, 0], schema[i, 1], schema[i, 2]); } this.schemaName = schemaName; }
thus filling static field with values. Problem solved ?
Regards
Michał Bryłka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Again sorry for the inconvenience, I mean how did you generate that "byte[,] schema" for the Autumn, etc?
Best regards, Paul.
Jesus Christ is LOVE! Please tell somebody.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
aha, now I see what you mean. Few years ago, I've been using Matlab on my classes of Optimization Methods. I was amazed too how those color schema were generated. It turned out that it's simply 3xN matrix. Try this one in Matlab:
a = jet
I believe that this solved your problem fully.
Regards
Michał Bryłka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
Actually, I've written this class because libraries like ZedGraph or nPlot were lacking them. So the idea of merging it with ZedGraph is tempting and I'll try to contact the its author.
Since you're a site builder, could you please answer why I'm getting the following errors in many parts of the screen: Error: Invalid procedure call or argument Script: /useritems/surfacePloter.asp Source: Microsoft VBScript runtime error Line: 0 Col: 0 Maybe there is a bug somewhere in scripts. It all started happening like a week ago. Sometimes I can't even access my profile. Thanks in advance
Michał Bryłka
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
Wow, great to hear! Thanks for the info, Michał!
"Site builder" means that I contributed scripts long time ago (e.g. the forum we are using here).
Unfortunately, I have no direct access to any part of the internals. BTW I'm getting this error, too. In the past things like this happened from time to time. I'm sure Chris is aware of this and tries to fix it!
Cheers Uwe
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hello, I've looked closely at architecture of ZedGraph and I think there is no point in artificially putting 3D support there. It wasn't designed to be so.
Michał Bryłka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It can be useful for me... How can I plot custom surface from file that contain xyz or just z parameter.
Good work.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
The code was designed to render images calculated by certain functions. However you can construct instance of Surface3DPloter and use its "Project" method to calculate 2D coordinates of triple <x,y,z>. I would add such method if I had time.
Michał Bryłka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Yes it really a nice work. Do plan to add support for rotations?
Best regards, Paul.
Jesus Christ is LOVE! Please tell somebody.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
This is already done. Finding it may be tricky though. First 3 parameters of constructor or ReCalculateTransformationsCoeficients method are coordinates of virtual observator. Try manipulating them.
Michał Bryłka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
May be without the axes it might not look nice, any hint on drawing the x,y,z axes?
Best regards, Paul.
Jesus Christ is LOVE! Please tell somebody.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Michal Brylka wrote: Sure, my previous article describes how to draw arrows:
Thanks for the information. The .NET AdjustableArrowCap class should be enough.
Best regards, Paul.
Jesus Christ is LOVE! Please tell somebody.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
And I don't mean this in a bad way, but it might be a good idea to give some example scenarios in the introduction for inspiration.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Sure, none offense taken . I've enclosed sample application of this class at the end of the article.
Michał Bryłka
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|