Message Boards Message Boards

GROUPS:

Generate a interpolation function from a mesh with assigned value?

Posted 1 month ago
245 Views
|
5 Replies
|
4 Total Likes
|

Hi all, I was trying to use Mathematica for preprocessing to generate a Voronoi mesh with assigned values. Here is my code to generate the mesh:

noPoly = 20; boxSize = 400; pts = 
 RandomReal[{-boxSize, boxSize}, {noPoly, 2}]; mesh = 
 VoronoiMesh[pts, {{-boxSize, boxSize}, {-boxSize, boxSize}}]

You will get something like:

Veronoi mesh

Now this is what I want to do:

1) For each "grain" in the diagram, I want to assign a constant value for the whole grain. i.e., define a function f(x,y) on the mesh such that it is constant in the same grain but maybe different for different grains.

2) Then I want to generate a list of x,y,f(x,y) for x,y are uniform grid points, like (1,0), (2,0), (1,1), (1,2).... If I can do this, I know how to generate text file for another software to read and generate this function.

2) is easy if I could do 1), i.e. get the function f(x,y). I have check the Mathematica documentation but have no idea how to do this. It seems PropertyValue might help but how? How do I even get those value defined on a mesh?

Thanks in advance!

5 Replies
Posted 1 month ago

It seems no one have a solution yet.

If it is not possible to assign value to a mesh, is there other ways to generate a labelled Voronoi diagram? If I could label them, maybe I can substitute the labels with my values?

Can be done with Nearest. Here is that example.

SeedRandom[123456]
noPoly = 20; boxSize = 400; pts = 
 RandomReal[{-boxSize, boxSize}, {noPoly, 2}]; mesh = 
 VoronoiMesh[pts, {{-boxSize, boxSize}, {-boxSize, boxSize}}]

enter image description here

We create the function using a random value for each Voronoi point.

vals = RandomReal[1, noPoly];
nf = Nearest[pts -> "Index"];
fun[x_] := vals[[nf[x][[1]]]]

We'll check it by making a large array of these values and using ArrayPlot to illustrate in color. I do some rearrangement to account for the (in my opinion, counterintuitive) ordering used by ArrayPlot.

tt = Table[fun[{x, y}], {x, -400, 400}, {y, -400, 400}];
ArrayPlot[Transpose[Map[Reverse, tt]], ColorFunction -> "Rainbow"]

enter image description here

Posted 1 month ago

Hi Daniel,

Thanks for the workable solution.

I noticed the mesh is not being used in your solution. Does that mean there is no simple ways to extend this question to some arbitrary mesh or diagrams?

If given an arbitrary mesh, something similar can be done as follows.

(1) Extract polygons.

(2) Define function values based on polygon membership.

It won't be as fast though.

Posted 1 month ago

Thank you!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract