wo80 / Triangle.NET

C# / .NET version of Jonathan Shewchuk's Triangle mesh generator.
442 stars 81 forks source link

2D Interpolation #10

Closed alhaDHI closed 2 years ago

alhaDHI commented 5 years ago

I am generating a mesh from scatter data and then interpolating on to another set of data points.

private float[] GetInterpolatedData(double[] x, double[] y, float[] data, double[,] xy) { var points = new List(); for (var i = 0; i < data.Length; i++) { points.Add(new Vertex3(x[i], y[i], (double)data[i])); } var mesher = new GenericMesher(new Dwyer()); var mesh = (TriangleNet.Mesh)mesher.Triangulate(points); var triangleQuadTree = new TriangleQuadTree(mesh); var outData = new float[xy.GetLength(0)]; for (var i = 0; i < xy.GetLength(0); i++) { var tri = triangleQuadTree.Query(xy[i, 0], xy[i, 1]); if (tri != null) { var vert = new Vertex3(xy[i, 0], xy[i, 1], double.NaN); InterpolateAttributes(vert, tri); outData[i] = (float)vert.Z; } else { outData[i] = float.NaN; } }

        return outData;

}

The x and y coordinates of the mesh will remain constant but "data" will change. Can I change the data in the mesh without re-meshing and re-running TriangleQuadTree?

justingruenberg commented 5 years ago

I don't see a Vertex3 class in this project. Looking at the source for InterpolateAttributes, you probably want to create a Vertex object with one attribute as your data value.

dewell1 commented 4 years ago

Is there any working example on how to use the interpolation function of the library?

I don't really understand what to do with USE_Z

Geri-Borbas commented 4 years ago

I suggest looking around at the original repo at https://archive.codeplex.com/?p=triangle. You can find archived issues and discussions there.

wo80 commented 4 years ago

Or look at the commit history. You'll find https://github.com/eppz/Triangle.NET/commit/aed9de93e5281caf1f23f1fe156058b930f38e5a

Add conditional compilation symbols:
USE_ATTRIBS: use vertex attributes (automatic interpolation)
USE_Z: add z-property to Point class (no interpolation)

Btw: interpolation of the z value should actually work just fine, see QualityMesher.cs #L628 and #L797

dewell1 commented 4 years ago

I have around 500 datasets with 3 values each (x,y,z). I used the randompoints generator from the mesh explorer as basis for my own generator. I add x and y to each vertex that I add to the IPolygon the generator returns.

I now overloaded the Vertex constructor with (x, y, 0, 1) to add one data attribute and filled it with the z value.

Vertex newVertex;
for (int i = 0; i < numPoints; i++)
{
    newVertex = new Vertex(xValue, yValue , 0, 1);
    newVertex.Attributes[0] = zValue;
    input.Add(newVertex);
}

The generation of the mesh and the Delaunay triangulation works. But now I want to use this to interpolate x,y coordinates for their yet unknown z value.

wo80 commented 4 years ago
  1. If the triangle is given and you want to interpolate the attribute (e.g. z-value) at a point inside the triangle, use the InterpolateAttributes method of the Interpolation class.

  2. If a point inside the mesh is given, locate the triangle that contains the point (you can use the TriangleQuadTree) and then do 1.

dewell1 commented 4 years ago

Thanks a lot. In my case the point x,y will be given, so I locate the right triangle with the the TriangleQuadTree class. Then I use the Query(double x, double y) to get the right triangle from my triangulation. Then I feed the InterpolateAttributes function from Interpolation.cs with all those values and get the wanted dataattribute (in my case the value z) for the given vertex (=x,y point). I hope I understood it right.

wo80 commented 2 years ago

Closing this one with #16

See Triangle.Examples Example10.cs for an example doing interpolation without USE_Z or USE_ATTRIBS.