Skip to content

Matrix

Extract orientation from primitive based on three point data

Very often, you’ll encounter tasks that require extracting the orientation of a primitive in 3D space, moving it back to the origin, or replacing it with a point that already has an @orient value. You can then copy this orientation onto the appropriate object.

To solve this problem, we can find the matrix orientation from three points that represent a triangle in 3D space. This is an importatn step! Once you have the matrix, you can convert it into a quaternion

Extract matrix

primitive.wrangle
int ppts[] = primpoints(0, @primnum);
if (len(ppts) < 3) {
return;
}
vector p0 = point(0, "P", ppts[1]);
vector p1 = point(0, "P", ppts[2]);
vector p2 = point(0, "P", ppts[3]);
vector edge1 = normalize(p1 - p0);
vector edge2 = normalize(p2 - p0);
vector normal = normalize(cross(edge1, edge2));
vector tangent = edge1;
vector bitangent = normalize(cross(normal, tangent));
matrix3 rotation_matrix = set(tangent, normal, bitangent);
vector4 orient = quaternion(rotation_matrix);
setpointattrib(0, "orient", @ptnum, orient, "set");
setpointattrib(0, "rotation_matrix", @ptnum, rotation_matrix, "set");