Skip to content

Instantly share code, notes, and snippets.

@ialhashim
Created May 12, 2015 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ialhashim/39f9e2a439effa13d979 to your computer and use it in GitHub Desktop.
Save ialhashim/39f9e2a439effa13d979 to your computer and use it in GitHub Desktop.
// https://github.com/libigl/libigl/blob/master/include/igl/project_to_line.cpp
auto projectionOnSegment = [&](const Vector3 & p, const Vector3 & s, const Vector3 & d){
Real px = p[0], py = p[1], pz = p[2];
Real sx = s[0], sy = s[1], sz = s[2];
Real dx = d[0], dy = d[1], dz = d[2];
Real dms[3], smp[3];
dms[0] = dx-sx; dms[1] = dy-sy; dms[2] = dz-sz;
Real v_sqrlen = dms[0]*dms[0] + dms[1]*dms[1] + dms[2]*dms[2]; assert(v_sqrlen != 0);
smp[0] = sx-px; smp[1] = sy-py; smp[2] = sz-pz;
t = -(dms[0]*smp[0]+dms[1]*smp[1]+dms[2]*smp[2])/v_sqrlen;
return t;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment