Rotate scaled 2d wave

I've written code to rotate a scaled 2d wave about the x and y axis, producing (x,y,z) data that is not scaled. I would like to use some bi-linear interpolation of the rotated data to reset the points into a scaled 2dwave. The initial images can be upto 1856 x1856 points, so this amounts to a great deal of points to interpolate, and the functions I've used seem too slow. I've begun writing custom code to speed things up since there is some relationship in the rotated scatter data points which may be taken advantage of, but it is does not seem to be a trivial task. I feel like this is not an uncommon problem and probably has an easier solution that I'm just not seeing. Has someone addressed this before and is there a useful function which I am not aware of. Is there even a completely different avenue I could take, eg. image rotate says it can be used for 3d images but it is not clear to me how.

Thanks for any help anybody can offer.

My best,
Richard
Hello Richard,

I am not sure I understand all the details of your application but here is how I see it: Data in 2D wave can be represented as a special case of 3D wave. Any rotation, translation or scaling on this data can be represented using a 3x3 transformation matrix. The same transformation that you apply to the data should also apply to any vector in the original frame and in particular to vectors representing the scaling. So, if you had a pair of X and Y vectors representing the wave scaling in the original frame you can transform them using your 3x3 matrix and then take a dot product with unit vectors in the new frame to deduce the new scaling.

HTH,

A.G.
WaveMetrics, Inc.
Thanks for your reply,

I'm sorry I wasn't clear enough. The data I begin with is an AFM image with height data (z) placed on an X/Y grid. This image may be as large as 1856 rows by 1856 columns, and not necessarily square. Rotating this data about the X axis, followed by the Y-axis causes the resulting data to be somewhat scattered and not equally spaced. This is because the transformation causes the rotated X and Y coordinates to be functions of not only the initial X and Y scaled data, but the initial Z coordinates as well. Since the Z values represent some real system they are scattered and so the resulting rotated X/Y coordinates are as well. I would like to place the X,Y,Z scatter data resulting from the rotation onto a grid, so that I can look at it as an image, rather than a Gizmo, and reduce the file size. I can find the appropriate scale easily enough by using the max X and Y values (the X,Y begins at the origin), the difficulty is interpolating the scatter data to find Z at the X/Y grid points. As I said I can have 1856x1856 points to deal with.

Thanks,
Richard



Igor wrote:
Hello Richard,

I am not sure I understand all the details of your application but here is how I see it: Data in 2D wave can be represented as a special case of 3D wave. Any rotation, translation or scaling on this data can be represented using a 3x3 transformation matrix. The same transformation that you apply to the data should also apply to any vector in the original frame and in particular to vectors representing the scaling. So, if you had a pair of X and Y vectors representing the wave scaling in the original frame you can transform them using your 3x3 matrix and then take a dot product with unit vectors in the new frame to deduce the new scaling.

HTH,

A.G.
WaveMetrics, Inc.

I have been doing some reading around the wavemetrics website and I found a macro (XYZTripletToMatrix) which looks like it will do the trick, but I suspect it will take a long time to run since it uses the imageinterpolate function which think is pretty computationally expensive. I'll see how it goes....
It does what I would like for small images, doesn't work for big ones. I don't think there is an easy solution for this.
Richard_G wrote:
It does what I would like for small images, doesn't work for big ones. I don't think there is an easy solution for this.


I wonder, would it be easier and just as "correct" if you interpolated the (scattered) Z values first to create a (uniformly scaled) (x,y,z) matrix and then rotated it?

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
Hello Richard,

I'm still not clear on what do you expect to get out of this but here are some thoughts:

Applying ImageInterpolate to the data is very wasteful: ImageInterpolate begins by computing the triangulation of your data which has O(N^2) operations. Since your data started off on a grid and went through a linear transformation, there is no justification for using such a computationally intensive method.

There are many ways that you can handle this computationally but here is one approach:

1. use the forward transformation to compute an inverse transform i.e., one that takes you from the rotated frame to the original data.
2. create a new 3D wave to contain your rotated data and initialize it to the exterior value (one for which data do not exist).
3. loop over all points in the new 3D wave.
3.1 for each x'y'z' apply the inverse transform to get xyz in original frame.
3.2 use x and y value in interp2d() with your original data to obtain an interpolated z0 value.
3.3 compare z0 and z and determine what value you want to put in the x'y'z' location.

The nature of the beast is that 3.2 can be performed using some kind of lookup table but even without it, this would be faster than any triangulation.

A.G.
WaveMetrics, Inc.
Thanks for all your suggestions. Briefly, in the end I :
1) Used wave stats to define the size and scale of the new scaled 2d wave that I would like to put the transformed scattered image data on. The new scaled 2d wave has more points than the original (untransformed image), but the surplus points are set to NAN.
2) Determined the matrix positions for the boundaries of the rotated data in the new scaled 2d wave
3) Performed a 2d linear interpolation for every point within the boundaries using their 4 nearest neighbors.

This seems to work well and only takes a few seconds.

I believe this is so quick since there is a relationship between points on the grid wave and the four nearest neighbors in the scatter data. You're right, no triangulation was necessary.

Thanks again


Igor wrote:
Hello Richard,

I'm still not clear on what do you expect to get out of this but here are some thoughts:

Applying ImageInterpolate to the data is very wasteful: ImageInterpolate begins by computing the triangulation of your data which has O(N^2) operations. Since your data started off on a grid and went through a linear transformation, there is no justification for using such a computationally intensive method.

There are many ways that you can handle this computationally but here is one approach:

1. use the forward transformation to compute an inverse transform i.e., one that takes you from the rotated frame to the original data.
2. create a new 3D wave to contain your rotated data and initialize it to the exterior value (one for which data do not exist).
3. loop over all points in the new 3D wave.
3.1 for each x'y'z' apply the inverse transform to get xyz in original frame.
3.2 use x and y value in interp2d() with your original data to obtain an interpolated z0 value.
3.3 compare z0 and z and determine what value you want to put in the x'y'z' location.

The nature of the beast is that 3.2 can be performed using some kind of lookup table but even without it, this would be faster than any triangulation.

A.G.
WaveMetrics, Inc.