File Exchange

image thumbnail

Ray casting for deformable triangular 3D meshes

version 1.2 (257 KB) by

Ray casting or collision detection for deformable triangular 3D meshes.

20 Downloads

Updated

View License

This is a Matlab wrapper for OPCODE, which is a collision detection
or ray casting library for triangular 3D meshes.
OPCODE uses a couple of different aabb trees to store the mesh and this is
a pretty simple wrapper for one of the trees.

Nice thing about opcode is that it allows for deformable meshes,
meaning that you can update the mesh while it is stored in the tree,
which is much faster than what it takes to rebuild the aabb tree.

Input and output:

To make the tree:
tree = opcodemesh(v,f);
where
vertices v : 3 x nv
faces f : 3 x nf

To intersect:
[hit,d,trix,bary,Q] = tree.intersect(orig,dir);
where
starting points orig : 3 x nc
direction dir : 3 x nc
whether hit or not hit : nc x 1 logical
distance from orig to intersection point d : nc x 1
index into f of the intersection triangle trix : nc x 1
barycentric coordinates of the triangle that the rays intersected bary : 2 x nc
actual 3D coordinates of the intersection poitns Q : 3 x nc
If a ray misses, then you have 0's for trix and nan's for d,bary,Q at that index.

To get the actual 3D coords, you can also do
Qhit = repmat(1-B(2,:)-B(1,:),3,1).*v(:,f(1,T)) + ...
    repmat(B(1,:),3,1).*v(:,f(2,T)) + ...
    repmat(B(2,:),3,1).*v(:,f(3,T));
where B = bary(:,hit), T = trix(hit),
which gives you the coordinates at the intersected points.

To update the mesh with a new set of vertices (as in deform the mesh):
tree.update(vnew);
vnew : 3 x nv
vnew must be the same size and the original set of vertices.
The topology of the mesh cannot change.

To delete the tree (which is actually done automatically by Matlab when the variable is cleared):
tree.delete();

To compile, go to ./src_matlab
and run mexall.m
which compiles the mex code and copies it to ./matlab
(I compile it against everything in opcode, so it's a bit slow.)

To run the demo, go to ./matlab
and run opcodemeshdemo.m

OPCODE is in
http://www.lia.ufc.br/~gilvan/cd/
(which is a more portable version)
and
http://www.codercorner.com/Opcode.htm
(the original site)

Contains much appreciated code from
http://www.mathworks.com/matlabcentral/fileexchange/38964
for nicely wrapping C++ functions in a Matlab class.

Fixes from the previous version:
Fixed compile issues for Win64 by removing all of the assembly code.
You don't need to normalize the direction vectors anymore.
It also returns the actual points now instead of just the barycentric points.

Comments and Ratings (19)

Ngo Nguyen

Hi Vipin, I got the error:

C:\Program Files (x86)\Microsoft Visual Studio
14.0\VC\INCLUDE\xkeycheck.h(250): fatal error C1189: #error:
The C++ Standard Library forbids macroizing keywords. Enable
warning C4005 to find the forbidden macro.

Can you tell me how to fix it?
Thank you.

Johannes Korsawe

Really good.

One small thing to improve (could be much effort, though):

Also return multiple intersections, not only the first.

TCH

TCH (view profile)

Here I put a test code but I cannot get the hit result that obviously shown in the figure(1). I am totally confused with the result, is there a bug in the original opcode?

@@@ code start

clc
clear
faces = [ ...
 10 , 6 , 4 ; ...
 11 , 6 , 10 ; ...
 6 , 7 , 4 ; ...
 10 , 4 , 13 ; ...
 11 , 8 , 6 ; ...
 16 , 11 , 10 ; ...
 6 , 3 , 7 ; ...
 4 , 7 , 2 ; ...
 13 , 4 , 5 ; ...
 14 , 10 , 13 ; ...
 12 , 8 , 11 ; ...
 8 , 3 , 6 ; ...
 17 , 11 , 16 ; ...
 16 , 10 , 14 ; ...
 3 , 1 , 7 ; ...
 7 , 0 , 2 ; ...
 4 , 2 , 5 ; ...
 13 , 5 , 9 ; ...
 14 , 13 , 15 ...
] + 1;

vertices = [ ...
    4.231401060000e-001 , 1.289017940000e-001 , 8.522257000000e-003 ; ...
    4.234843750000e-001 , 1.295909270000e-001 , 8.482499000000e-003 ; ...
    4.203841550000e-001 , 1.286728970000e-001 , 9.441268000000e-003 ; ...
    4.212358700000e-001 , 1.298959500000e-001 , 9.275906000000e-003 ; ...
    4.181593630000e-001 , 1.289744720000e-001 , 1.030612100000e-002 ; ...
    4.176608580000e-001 , 1.284392850000e-001 , 1.045896700000e-002 ; ...
    4.186631770000e-001 , 1.295075680000e-001 , 1.016991900000e-002 ; ...
    4.208076170000e-001 , 1.292857210000e-001 , 9.348370000000e-003 ; ...
    4.191720280000e-001 , 1.300382540000e-001 , 1.004999700000e-002 ; ...
    4.151989140000e-001 , 1.282018740000e-001 , 1.154331100000e-002 ; ...
    4.163471070000e-001 , 1.291179810000e-001 , 1.115785700000e-002 ; ...
    4.169212040000e-001 , 1.295760350000e-001 , 1.096513000000e-002 ; ...
    4.174953000000e-001 , 1.300340880000e-001 , 1.077240300000e-002 ; ...
    4.157730100000e-001 , 1.286599270000e-001 , 1.135058400000e-002 ; ...
    4.162002870000e-001 , 1.305507970000e-001 , 1.134597100000e-002 ; ...
    4.156481020000e-001 , 1.302543030000e-001 , 1.150225100000e-002 ; ...
    4.167558590000e-001 , 1.308455510000e-001 , 1.119693700000e-002 ; ...
    4.173129270000e-001 , 1.311391140000e-001 , 1.105040500000e-002 ...
];

x = vertices(:,1);
y = vertices(:,2);
z = vertices(:,3);

orig0 = [0.41749369461334546, 0.12947887360062926, 0.010681338615420484]; % ray's origin
dir0 = [-0.40977066483612395, 0.15771530547732460, -0.89845082484126815]; % ray's direction

orig = orig0 - dir0 * 0.01;
orig0 = orig;

orig = [orig;orig];
dir = [dir0;-dir0];

endp = orig + dir * 0.1;
linex = [orig(:,1), endp(:,1)];
liney = [orig(:,2), endp(:,2)];
linez = [orig(:,3), endp(:,3)];

t = opcodemesh(vertices',faces');

tic;
[hit,d,trix,bary,Q] = t.intersect(orig',dir');
% intersect = TriangleRayIntersection(orig, dir, vert1, vert2, vert3);
fprintf('Number of: faces=%i, points=%i, intresections=%i; time=%f sec\n', ...
  size(faces,1), size(vertices,1), sum(hit), toc);

%%
% *Display the results: Surface in blue, line in light read and intersected
% triangles in dark red*
figure(1); clf;
hold on;
trisurf(faces,x,y,z,'FaceAlpha', 0.9)
hold on;
plot3(linex,liney,linez,'-k', 'linewidth', 2);
hold on
plot3(orig0(1),orig0(2),orig0(3), '-bo');
% set(gca, 'CameraPosition', [106.2478 -35.9079 136.4875])
%set(gco,'EdgeColor','none');
xlabel('x');
ylabel('y');
zlabel('z');
axis tight

@@@ code end

Vipin Vijayan

I doubt it. I've never used the mesh to mesh collision function and I'm not really using this code much anymore.

Andre

Andre (view profile)

Any chance to get the mesh to mesh collision function wrapped?

Eric

Eric (view profile)

Great submission. Very useful.

Thank you Germano Gomes for mex64 advice.

Vipin Vijayan

Yeah, sorry about the confusion with norm(to-from). You're right, normc should be used for the input.

Note(ray casting): the correct value of 'd' is obtained by normalizing each column of the (to-from) 3xN matrix. I use Matlab normc for this:

[hit,d,trix,bary] = t.intersect(from,normc(to-from));

do not use:

 [hit,d,trix,bary] = t.intersect(from,(to-from)./norm(to-from));

if you have more than one vector/ray

Very useful Matlab wrapper for fast collision detection and ray tracing.

 For those using Matlab win64: look at the error information and comment out (//) the machine code (the __asm keyword invokes the inline assembler).

 Example solution: error fix in IceFPU.h line 47:

//! Fast square root for floating-point values.
inline_ float FastSqrt(float square)
{
// #ifdef _MSC_VER
// float retval;
//
// __asm {
// mov eax, square
// sub eax, 0x3F800000
// sar eax, 1
// add eax, 0x3F800000
// mov [retval], eax
// }
// return retval;
// #else
return sqrt(square);
// #endif

In all cases just leave the code below #else (this is in vanilla C++) that does the same work without any incompatibility with win64.

Finally, unfortunately the returned d (matrix of distances) returns wrong values that seem to be scaled or shifted by the number of rays (vectors from-to), even if this vector is normalized as suggested by Vipin.
Vipin can you check and fix this please.

For it to be perfect I suggest returning the correct 'd' matrix, not needing the normalization step in Matlab and returning the intersection points(Q matrix) from the Mex function. That would be nice.

Thank you Vipin

Vipin Vijayan

I don't have an Matlab win64 install now. I tried it on win 32, os x 64, and linux 64. Maybe you can tell me what the error looks like and I will see what I can do.

omar

omar (view profile)

Thanks Vipin..now i've the same problem with K G...i get errors when compiling it under win 64 !

K G

K G (view profile)

how can I use it in Matlab(win64), it can not be mex in a 64bit system

Vipin Vijayan

The compiled file is not in your path or you didn't compile it.

omar

omar (view profile)

Hi...i get an error when i try to run the opcodemeshdemo.m in Matlab(2012b-64).

//
Undefined function 'opcodemeshmex' for input
arguments of type 'char'.

Error in opcodemesh (line 8)
            this.objectHandle =
            opcodemeshmex('create',
            varargin{:});

Error in opcodemeshdemo (line 19)
t = opcodemesh(v',f');

//

any help ?

Thanks,
omar

Vipin Vijayan

Made a mistake in the previous comment. Calculation of Q should actually be like the following example.

f = [1 2 3]'; v = [-1 0 0.1 ; 1 1 0.1 ; 1 -1 0.1]';
t = opcodemesh(v,f);
from = [0.17 0.17 0.5]';
to = [0.17 0.17 -1]';
[hit,d,trix,bary] = t.intersect(from,(to-from)./norm(to-from));
B = bary(:,hit); T = trix(hit);
Q = repmat(1-B(2,:)-B(1,:),3,1).*v(:,f(1,T)) + repmat(B(1,:),3,1).*v(:,f(2,T)) + repmat(B(2,:),3,1).*v(:,f(3,T));

Vipin Vijayan

You had to normalize the direction (to - from) and I had gotten the calculation for the points Q wrong. I will upload a version eventually so you don't have to normalize the direction. Btw, here is an example that works:

f = [1 2 3]'; v = [-1 0 0.1 ; 1 1 0.1 ; 1 -1 0.1]';
t = opcodemesh(v,f);
from = [0 0 0.5]';
to = [0 0 -1]';
[hit,d,trix,bary] = t.intersect(from,(to-from)./norm(to-from));
B = bary(:,hit); T = trix(hit);
Q = repmat(B(1,:),3,1).*v(:,f(3,T)) + repmat(B(2,:),3,1).*v(:,f(2,T)) + repmat(1-B(2,:)-B(1,:),3,1).*v(:,f(1,T));

Zhou

Zhou (view profile)

I used the code following the descriptions.
i have one questions:
(1) why d is not equal to norm(orig-Q)?

(2) The following simple example can not obtian right result (d=0.5, Q=[0 0 0]). (i get a wrong result: d =0.3333, Q = [0.5 -0.25 0]
==================
v = [-1 0 0 ; 1 1 0 ; 1 -1 0];
f = [1 2 3];
t = opcodemesh(v',f');

from = [0 0 0.5]';
to = [0 0 -1]';
[hit,d,trix,bary] = t.intersect(from,to-from);

Q = repmat(bary(1,:),3,1).*v(f(trix,1),:)' + ...
    repmat(bary(2,:),3,1).*v(f(trix,2),:)' + ...
    repmat(1-bary(1,:)-bary(2,:),3,1).*v(f(trix,3),:)'
===========

I do not know why?

Thanks!

Vipin Vijayan

PS. The getting the points part should actually look like

Q = repmat(B(1,:),3,1).*v(:,f(1,T)) + ...
    repmat(B(2,:),3,1).*v(:,f(2,T)) + ...
    repmat(1-B(2,:)-B(1,:),3,1).*v(:,f(3,T));
where B = bary(:,hit), T = trix(hit).

Updates

1.2

Fixed compile issues for Win64 by removing all of the assembly code.
You don't need to normalize the direction vectors anymore.
It also returns the actual points now instead of just the barycentric points.

1.1

Fixed compile issues for Win64 by removing all of the assembly code.
You don't need to normalize the direction vectors anymore.
It also returns the actual points now instead of just the barycentric points.

MATLAB Release
MATLAB 8.1 (R2013a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

opcodemesh/matlab/

opcodemesh/src_matlab/