% compute normals using cross product
normals(i, :) = cross(v1, v2, 2);
end
normals= normalizeVector3d(normals);% correction GTG 2014
end

---------------
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.

mex_sepsq
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
%example:
% M1=rand(3, 10000); M2=rand(3,10000);
% Dist=Distance3DFast(M1,M2);
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

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

Comment only