2

I have two point clouds (XYZ coordinates) of different dimensions and would like to be able to calculate the difference between them (with the result as an XYZ array, with Z being the distance between them) and then plot both as surfaces together with the differences as a different color.

This question seems relevant, but is not quite what I am looking for: Subtract two trisurf plots from one another

Here is some example data and code. I can't figure out how to interpolate one dataset to another.

%Point Cloud 1
X1 = randn(100,1);
Y1 = randn(100,1);
Z1 =(exp(-X1.^2-Y1.^2));
% Point Cloud 2
X2 = randn(107,1);
Y2 = randn(107,1);
Z2 = (exp(-X2.^2-Y2.^2));

tri1 = delaunay(X1, Y1);
tri2 = delaunay(X2, Y2);

trisurf(tri1, X1, Y1, Z1, 1)
hold on
trisurf(tri2, X2, Y2, Z2, 100)
hold off

The question I mentioned above directs to here: How Do I Generate a 3-D Surface From Isolines? but I am new to interpolation of 3d data in Matlab and can't seem to figure it out. Any help would be appreciated. Thanks.

Community
  • 1
  • 1
Beau Walker
  • 23
  • 1
  • 6
  • What is the objective, why do you need the heights between them? And between what, delauny will give you triangles, you can find hiehgt between planes? But planes might overlap etc? – Fantastic Mr Fox May 14 '14 at 04:10
  • @Ben my point clouds are of a surface taken at two time intervals and I am trying to visualize areas where there has been deposition and erosion of sediment. – Beau Walker May 14 '14 at 15:15

1 Answers1

3

I think this will help, really it's just the interpolation method that you didn't have.

%Point Cloud 1
X1 = randn(100,1);
Y1 = randn(100,1);
Z1 =(exp(-X1.^2-Y1.^2));
% Point Cloud 2
X2 = randn(107,1);
Y2 = randn(107,1);
Z2 = (exp(-X2.^2-Y2.^2));

% Mesh for interpolation
x=linspace(min([X1;X2]),max([X1;X2]),40);
y=linspace(min([Y1;Y2]),max([Y1;Y2]),40);
[X,Y]=meshgrid(x,y);

% Calculate interpolants
V1=TriScatteredInterp(X1,Y1,Z1);
V2=TriScatteredInterp(X2,Y2,Z2);
F1=V1(X,Y);
F2=V2(X,Y);

% Plot to check results!
figure(1)
scatter3(X1,Y1,Z1)
hold on
mesh(X,Y,F1)
hold off
figure(2)
scatter3(X2,Y2,Z2)
hold on
mesh(X,Y,F2)
hold off

You can also use griddata:

% Calculate interpolants
F1=griddata(X1,Y1,Z1,X,Y);
F2=griddata(X2,Y2,Z2,X,Y);
David
  • 8,449
  • 1
  • 22
  • 32
  • Thanks so much for the reply. Code works great and makes the interpolation much simpler. Is there a way that I can subtract Point Cloud 2 from Point Cloud 1? I'd like to be able to visualize areas where there was a positive change and a negative change between the two point clouds. – Beau Walker May 14 '14 at 15:22
  • What I particularly am not able to do is get an array that represents F2 - F1 in the example above. F2 - F1 returns a 40 x 40 matrix, but I'd like to be able to use the result of F2 - F1 as the color value in a mesh, surf, or trisurf plot. – Beau Walker May 14 '14 at 17:15
  • 1
    `mesh(X,Y,F1-F2)` will give you a mesh plot, or `mesh(X,Y,zeros(size(X)),F1-F2)` should give you a flat surface coloured to reflect the difference between `F1` and `F2`. You can also use `contour(X,Y,F1-F2)` or `contourf(X,Y,F1-F2)`. – David May 14 '14 at 22:11
  • Thanks for your help @David. I combined your first two suggestions into `mesh(X,Y,F2,F2-F1)` and that gave me what I needed: a 3d plot of my data with the surface colored to reflect the difference between `F2` and `F1` – Beau Walker May 15 '14 at 01:00