Wednesday, November 14, 2012

PROJECT ON PLANE

Another little simple function i've been using in some codes (thanks wolfram mathworld),especially to project agents, running 2D ,on a mesh face.You can use it to project vectors on any planes too,giving at least three points.You will need toxiclibs for this(i know there is a way to do the same thing with toxi rays).


    Vec3D projectOnTriangle3D(Vec3D r, Vec3D a,Vec3D b,Vec3D c) {
    float aa, bb, cc, dd;
    float x1 = a.x;
    float y1 = a.y;
    float z1 = a.z;
    float x2 = b.x;
    float y2 = b.y;
    float z2 = b.z;
    float x3 = c.x;
    float y3 = c.y;
    float z3 = c.z;
    aa = y1 * (z2 - z3) + y2 * (z3 - z1) + y3 * (z1 - z2);
    bb = z1 * (x2 - x3) + z2  *(x3 - x1) + z3  *(x1 - x2);
    cc = x1  *(y2 - y3) + x2 * (y3 - y1) + x3  *(y1 - y2);
    dd = -(x1  *(y2  *z3 - y3  *z2) + x2  *(y3  *z1 - y1 * z3) + x3 * (y1 * z2 - y2  *z1));
    float z = (-dd - aa * r.x - bb * r.y)/cc;
    Vec3D projVec = new Vec3D(r.x, r.y, z);
    return projVec;
  }

No comments:

Post a Comment