4

I'd like to interpolate a surface using c#. The situation is the following:

A set of x,y,z coordinates is given. Now I d like to interpolate between those points using a finer grid. Actually I d like to know the z coordinate at a certain point, e.g. x=2.2, y=1.6 z =??.

I was able to solve the interpolation using MatLab, but was not successful while using c#.. Furthermore, I was able to plot surfaces with ilnumerics, and tried to find some information on their homepage.

EDIT:

I think I need to clarify some things - sorry for the confusing way of asking my question

here you can see how I draw the surface out of some points:

using System;
using System.Drawing;
using System.Windows.Forms;
using ILNumerics;
using ILNumerics.Drawing;
using ILNumerics.Drawing.Plotting; 

namespace Surface
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void ilPanel1_Load(object sender, EventArgs e)
      {
         using (ILScope.Enter())
         {
            ILArray<float>  R = ILMath.linspace<float>(0, 5, 5);
            ILArray<float> R1 = ILMath.linspace<float>(0, 25, 5);
            ILArray<float>  y = 1;
            ILArray<float>  x = ILMath.meshgrid(R, R, y);
            ILArray<float> z = ILMath.meshgrid(R * R, R, y);
            ILArray<float> Z = ILMath.zeros<float>(x.S[0], x.S[1], 3);

            Z[":;:;1"] = x;
            Z[":;:;2"] = y;
            Z[":;:;0"] = z;

             ilPanel1.Scene.Add(new ILPlotCube(twoDMode: false) {
                new ILSurface(Z, colormap: Colormaps.Cool) {
                    Colors = 1.4f * x * x * x + 0.13f * y * y,
                    Childs = { new ILColorbar() }
                }
            });
         }
      }
   }
}

The x and y coordinates are linearly distributed from 0 to 5 and the z coordinate has an quadratic shape. I d like to now the value of the z coordinate at a certain x,y coordinate, e.g. x=2.2, y=1.6 z =?? - which is definitely not a know point on my surface. So I thought it would be a good idea to interpolate the surface with an "finer" grid, that I m able to read out the value of the z coordinate...

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
gabs
  • 53
  • 1
  • 6
  • If you've had trouble with code, post the code. Maybe along with your working MatLab code. – weston Sep 17 '13 at 11:42
  • Hi, first of all thanks for your replay.. I problem someone solved with MatLab is not exactly the same, but you can check that link: http://stackoverflow.com/questions/8195404/interpolation-curve-to-surface There is a relation for the z coordinates known, not in my case... – gabs Sep 17 '13 at 11:54
  • I suggest you read the [help](http://stackoverflow.com/help) regarding asking a question. You way it is, it's impossible to tell the problem you are having and so impossible to help without basically giving you the full working code. – weston Sep 17 '13 at 12:28
  • Thanks for the advice ^^ but I did not ask for full working code! I was asking if anybody has an idea how to solve an interpolation of a 3d surface while using c#, maybe it also depends how the responding guy reads the question. – gabs Sep 17 '13 at 12:31
  • The only thing you do ask is "Has anybody experience solving this problem with c#?". How would a "yes" answer help you? You're having a problem, but you don't give enough details about it for anyone to help. – weston Sep 17 '13 at 12:33
  • "but was not successful while using c#" Why not? What is the problem you want help with? Do you see what I mean? – weston Sep 17 '13 at 12:34
  • Lets say I can imagine what you mean.. So if you have experience in in interpolating surfaces, how did you do it? did you used ilnumerics? because i was not able to find any interpolation method on their homepage, thats why I was not able to solve the problem with c#. If I would have been allowed to post a picture you would be able to see the plotted surface... Its like I said, I m able to calculate/draw a surface from a given set of points, but I would need a "finer" grid, for getting the z-coordinate at a certain x,y coordinate.. – gabs Sep 17 '13 at 12:48
  • Assuming you mean the z coordinate of the underlying function and not the one of the rendered estimation: see my answer below. – Haymo Kutschbach Sep 19 '13 at 20:24

2 Answers2

1

There are different interpolation techniques to choose from. I would suggest to start with Bilinear interpolation:

http://en.wikipedia.org/wiki/Bilinear_interpolation

or divide every quad into two triangles and use barycentric interpolation:

http://en.wikipedia.org/wiki/Barycentric_coordinate_system_(mathematics)

Enigma
  • 1,699
  • 10
  • 14
  • Hey, thank for you answer, but I dont need any interpolation method. As I said before, I was able to solve the problem in MatLab and now I m asking if somebody has an idea about, how to solve it with c#... – gabs Sep 17 '13 at 11:53
  • "I dont need any interpolation method"? You said, it worked in Matlab. Why not go the same way in C# as in Matlab? Your problem is not clear to me yet either. – Haymo Kutschbach Sep 17 '13 at 12:43
  • 1
    If understanding c# is the real problem, I would suggest you to read some c# tutorials. This should be a good one: http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx. Do not forget to look into arrays: http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx – Enigma Sep 17 '13 at 12:46
  • Ya, in MatLab its a little easier, cause I can use function like interp2 for the interpolation stuff... I m looking for an algorithm which interpolates a surface out of a given set of points – gabs Sep 17 '13 at 12:57
  • Biliniar interpolation and barycentric interpolations are just that. Algorithms to 2d interpolate stuff. Please have another look at: http://supercomputingblog.com/graphics/coding-bilinear-interpolation/ and imagine that you are interpolating height instead of colours. In this algorithm, Q11, Q12, Q21 and Q22 should be replaced with the height at the given corners and P is the height at location (x,y) – Enigma Sep 17 '13 at 13:01
  • The code at page 2 is C++ code, but should be possible to port to C# – Enigma Sep 17 '13 at 13:08
  • Thank you.. I checked the piece of code, but believe thats for two dimensional calculations. – gabs Sep 17 '13 at 13:16
  • Yes, you are interpolating the height (z) over the two other axis (x,y) – Enigma Sep 18 '13 at 06:21
  • Your function interp2 in Matlab does also interpolate in two dimensions. http://www.mathworks.nl/help/matlab/ref/interp2.html – Enigma Sep 18 '13 at 06:23
  • Yes u are right, for the first try i assumed to have a relation for the z coordinate like z=f(x,y). But actually it isnt like this.. I ll give the thing you recommended an other try. thanks... – gabs Sep 18 '13 at 07:19
1

The z coordinate of your function is computed in this line:

 ILArray<float> z = ILMath.meshgrid(R * R, R, y);

Since meshgrid is actually used to create the X and Y coordinates for 2 dimensional function evaluation, only the R * R result goes into z. After that line, x,y and z look as follows:

x
<Single> [5,5]
[0]: 0,00000 1,25000 2,50000 3,75000 5,00000 
[1]: 0,00000 1,25000 2,50000 3,75000 5,00000 
[2]: 0,00000 1,25000 2,50000 3,75000 5,00000 
[3]: 0,00000 1,25000 2,50000 3,75000 5,00000 
[4]: 0,00000 1,25000 2,50000 3,75000 5,00000 

y
<Single> [5,5]
[0]: 0,00000 0,00000 0,00000 0,00000 0,00000 
[1]: 1,25000 1,25000 1,25000 1,25000 1,25000 
[2]: 2,50000 2,50000 2,50000 2,50000 2,50000 
[3]: 3,75000 3,75000 3,75000 3,75000 3,75000 
[4]: 5,00000 5,00000 5,00000 5,00000 5,00000 

z
<Single> [5,5]
[0]: 0,00000 1,56250 6,25000 14,06250 25,00000 
[1]: 0,00000 1,56250 6,25000 14,06250 25,00000 
[2]: 0,00000 1,56250 6,25000 14,06250 25,00000 
[3]: 0,00000 1,56250 6,25000 14,06250 25,00000 
[4]: 0,00000 1,56250 6,25000 14,06250 25,00000 

Obviously, z does only depend on x, which gets clear by the resulting surface:

Surface

So, the value of z would be: x * x. Or for your specific example:

x=2.2, y=1.6 z =4.84

Edit: In case the underlying function is not known, you could either

  • try to learn that function (using ridge_regression() or pinv()), or
  • interpolate from the data of the neighboring grid points.

There is currently no corresponding function (like 'interp2') in ILNumerics. However, in your case - where only one single point needs to be interpolated (?), one can find the neighboring grid points and use one of the common interpolation methods.

Edit: With the release of the interpolation toolbox things became significantly easier. You can now interpolate in any dimension in high speed and with a single line.

Haymo Kutschbach
  • 3,322
  • 1
  • 17
  • 25
  • Thanks for the explanation.. In my example I assumed to know the relation for the z coordinate. But actually I "only" have a set of points represented in x,y,z coordinates. This points should be interpolated with a surface, which should provide the z coordinate at any point on the surface... – gabs Sep 23 '13 at 08:25
  • Hey Haymo, I have set of points - something like 25 points. Out of these points I d like to calculate a surface, where all points have to be on the surface. So with the interpolated surface in hand, it should be possible to calculate the z coordinate between those "control points". Is there any function or method, which determines a surface out of some given points?? – gabs Sep 24 '13 at 09:46
  • 1
    You need a function which generates a mesh from the given points. A common mesh generator is delaunay triangulation: http://en.wikipedia.org/wiki/Delaunay_triangulation There is currently no such function provided by the ILNumerics core package. – Haymo Kutschbach Sep 25 '13 at 15:23
  • Thank you, everything is clear now. I was able to generate a mesh while calculating a bezier surface. Furthermore, I mentioned that I need the height in at every x,y coordinate. So i just calculate triangle mashes within every square and determine the intersection with an ray.. – gabs Sep 30 '13 at 12:57