Search code examples
matlabinterpolationpoint-clouds

Calculating and plotting differences between two point clouds


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.


Solution

  • 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);