Search code examples
matlab3dtriangulationdelaunay

Erratic data cursor behavior for triangulated 3d surfaces in MATLAB R2011b


I'm seeing erratic behavior from the data cursor in MATLAB R2011b when applied to plots of triangulated 3d surfaces: Clicking on certain points selects completely different points instead. Example with a cylinder:

[r, phi, h] = meshgrid(1, 0:pi/10:2*pi, 0:0.05:1);
x = r.*cos(phi);
y = r.*sin(phi);
z = h;
xyz = [x(:) y(:) z(:)];
tri = delaunay(xyz);
trimesh(tri, xyz(:,1), xyz(:,2), xyz(:,3), ...
        'LineStyle', 'none', 'Marker', '.', 'MarkerSize', 30)
view(-37, 28)

Then enable data cursor mode and try to select the topmost dot of one of the columns in front. On my installation, MATLAB does not select the point under the cursor but a different one seemingly chosen at random.

Is this a bug or am I doing something wrong?


Solution

  • I found a solution to this problem in a File Exchange contribution by Jochen Rau. You can define which data is selectable with the data cursor via the 'HitTest' property. So for the example I provided, where I wanted only the markers to be selectable, the solution is to plot the mesh without markers and with 'HitTest' set to 'off' and then use 'scatter3' to plot the markers.

    [r, phi, h] = meshgrid(1, 0:pi/10:2*pi, 0:0.05:1);
    x = r.*cos(phi);
    y = r.*sin(phi);
    z = h;
    xyz = [x(:) y(:) z(:)];
    tri = delaunay(xyz);
    figure
    hold on
    trimesh(tri, xyz(:,1), xyz(:,2), xyz(:,3), ...
            'LineStyle', 'none', 'Marker', 'none', 'HitTest', 'off')
    scatter3(xyz(:,1), xyz(:,2), xyz(:,3))
    view(-37, 28)
    

    If you're wondering what the point of plotting the triangulation is: it's to aid in visualizing point clouds by obscuring points that are in the back. The 'trimesh' call accomplishes this because it still draws the faces in white.