% compute normals using cross product
normals(i, :) = cross(v1, v2, 2);
normals= normalizeVector3d(normals);% correction GTG 2014
Always better to normalize at the end otherwise the final normals are not normalized because of roundoff etc errors in the cross computation, this also has the advantage of eliminating 2 calls to normalizeVector3d, making faceNormal a tad faster.
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.
Fast, dedicated, code to compute Euclidean separation between sets of points in N-dimensional space.
Author: Ben Mitch
I believe that in 3D something like this is almost as fast for number of points >1000, ratio is approximately =2.
function [Dist] = Distance3DFast(M1,M2)
% M1 is a 3xN matrix
% M2 is a 3xM matrix
%Dist is a NxM matrix
% M1=rand(3, 10000); M2=rand(3,10000);
Dist = sqrt(bsxfun(@plus,dot(M1,M1)',dot(M2,M2))-2*M1'*M2);
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