Skip to content

Instantly share code, notes, and snippets.

@lcrs
Created October 2, 2018 12:27
Show Gist options
  • Save lcrs/3b47d779e6b440fed8764fbaccd23330 to your computer and use it in GitHub Desktop.
Save lcrs/3b47d779e6b440fed8764fbaccd23330 to your computer and use it in GitHub Desktop.
// Create a basis from points of triangle.
matrix3 basis_from_triangle(vector p1, p2, p3)
{
// Axes vectors. May not be orthogonal.
vector X, Y, Z;
Z = p2 - p1;
X = p3 - p1;
Y = normalize(cross(Z, X)) * length(p2 - p3);
return set(X, Y, Z);
}
// Calculate rotation matrix rotating triangle ABC into triangle DEF.
matrix3 rotation_between_triangles(vector a, b, c, d, e, f)
{
matrix3 basisABC, basisDEF;
basisABC = basis_from_triangle(a, b, c);
basisDEF = basis_from_triangle(d, e, f);
return invert(basisABC) * basisDEF;
}
// Calculate transform matrix transforming triangle ABC into triangle DEF.
matrix transform_between_triangles(vector a, b, c, d, e, f)
{
matrix transform = ident();
matrix3 r = rotation_between_triangles(a, b, c, d, e, f);
transform *= r;
translate(transform, d - a * transform);
return transform;
}
4@transform = transform_between_triangles(point(1, "P", primpoint(1, @primnum, 0)),
point(1, "P", primpoint(1, @primnum, 1)),
point(1, "P", primpoint(1, @primnum, 2)),
point(0, "P", primpoint(0, @primnum, 0)),
point(0, "P", primpoint(0, @primnum, 1)),
point(0, "P", primpoint(0, @primnum, 2)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment