Darkobra
Darkobra

Reputation: 85

Change Matlab's trimesh vertices after plotting

I have a problem with Matlab's trimesh function.

What I need to do is change a shape plotted through the trimesh function overtime. I actually manage to do that by plotting the transformed face all over again, that gives me the expected shape. This is how I plot my shape:

hc = trimesh(triangles,candide3(:,1),candide3(:,2),candide3(:,3));

Here candide3 is a 113x3 double matrix of vertices, while triangles is a 184x3 double matrix of indexes, where every row is a triangle. This plots what I want, exactly. Then I need to transform the face - while triangles remains always the same matrix, a few entry of candide3 need to change overtime, in real time (approx. 25 times per second). If I do this:

candidet = transform(candide3, ...);
trimesh(triangles,candide3(:,1),candide3(:,2),candide3(:,3));

I get exactly the transformation I am looking for. And that's fine, but not enough. I'd like to avoid calling trimesh 25 times per second and set the new vertices instead. So I tried the set function, like this:

candidet = transform(candide3, ...);
set(hc,'XData',candidet(:,1),'YData',candidet(:,2),'ZData',candidet(:,3));

But this does not give the expected result: it plots a mess of triangles and surfaces that doesn't even go near to what I'm looking for, it feels weird and random. So looking through the documentation I tried this also:

candidet = transform(candide3, ...);
hc.XData = candidet(:,1);
hc.YData = candidet(:,2);
hc.ZData = candidet(:,3);

And this give the same nonsense as the above try.

I am using Matlab R2016b on Max OS X El Capitan. Thanks to whoever will try to help me.

Upvotes: 0

Views: 288

Answers (1)

Florian
Florian

Reputation: 1824

If you check how trimesh works internally, you will notice that it takes the arguments triangles, x, y, z into a patch command like this:

h = patch('faces',triangles,'vertices',[x(:) y(:) z(:)],'facevertexcdata',c(:),...);

Therefore, I recommend trying the following:

hc = trimesh(triangles,candide3(:,1),candide3(:,2),candide3(:,3));
candidet = transform(candide3, ...);
set(hc,'vertices',candidet);

Upvotes: 1

Related Questions