Search code examples
matlab3dshapesmatlab-cvstpoint-clouds

Transform 3D to PointCloud


I'm working with Persistent Homology and I need cloud points of common 3D shapes to be able to test my methods.

The problem is that I'm a Java programmer and Java doesn't offer such tools, but I'm pretty sure Matlab does... I tried reading about this here:

http://www.mathworks.com/help/vision/ref/pcfitsphere.html
http://www.mathworks.com/help/matlab/ref/sphere.html
http://www.mathworks.com/help/vision/ref/pcshow.html#inputarg_ptCloud

Those links provide information on Spheres and PointClouds, but I've never programmed on matlab so I can't even propose code.

Is there a way to take a 3d shape, get it's point cloud and print the point cloud on the console? Like:

x0, y0, z0

x1, y1, z1

x2, y2, z2

... What I was doing was creating a Java class that printed random points based on a function, so for example I'd give my program the function of a sphere... But it gets super complicated when I'm trying to create functions of pyramids or three-torus.


Solution

  • Here is a MATLAB example of points inside a sphere:

    % random points in spherical coordinates
    N = 1000;
    theta = 2*pi*rand(N,1);
    phi = asin(2*rand(N,1)-1);
    radii = 3*(rand(N,1).^(1/3));
    
    % convert to cartesian
    [x,y,z] = sph2cart(theta, phi, radii);
    
    % plot
    scatter3(x, y, z, 10, 'filled')
    axis vis3d equal, grid on, box on
    xlabel X, ylabel Y, zlabel Z
    

    sphere-cloud

    See this for reference.


    EDIT

    Here is another example for generating points inside a pyramid.

    This time I'm taking a brute-force approach by simply generating lots of random 3d points in the [0,1] cube, and then filtering them by testing which points are inside the pyramid convex polyhedron (using Delaunay triangulation).

    % random points
    N = 3000;
    XYZ = rand(N,3);
    
    % unit pyramid in [0,1]
    V = [0   0   0 ;
         1   0   0 ;
         1   1   0 ;
         0   1   0 ;
         0.5 0.5 0 ;
         0.5 0.5 sqrt(2)/2];
    
    % delaunay triangulation
    DT = delaunayn(V);
    
    % determine points within
    in = ~isnan(tsearchn(V, DT, XYZ));
    
    % plot
    scatter3(XYZ(in,1), XYZ(in,2), XYZ(in,3), 8, 'filled')
    view(3), axis vis3d equal, grid on, box on
    axis([0 1 0 1 0 1])
    xlabel X, ylabel Y, zlabel Z
    
    % overlay pyramid
    hold on
    h = tetramesh(DT, V);
    set(h, 'FaceAlpha',0.1, 'EdgeColor','m', 'FaceColor','m')
    hold off
    

    pyramid-cloud