float pointToLineSegment3(vec3 p1, vec3 p2, vec3 p)
{
if ( p1 == p2 )
return distance(p, p1);
float a = distance(p1, p2);
float b = distance(p1, p);
float c = distance(p2, p);
if ( c + b <= a)
return 0.0;
else if ( c * c >= a * a + b * b )
return b;
else if ( b * b >= a * a + c * c )
return c;
float d = ( a + b + c ) * 0.5;
float k = d * (d - a) * (d - b) * (d - c);
float s = sqrt(k);
return 2.0 * s / a;
}