Thursday, October 25, 2012

Initialize vector onto mesh face

Here is a little function i wrote some time ago. If you want to initialize your agents onto a specific face of your mesh , this is what you're looking for mate!.You provide,as argument, aWEFace and it returns a vec3D (toxiclibs library needed).

Vec3D initInTriangle3D(WEFace f) {
  Vec3D r = new Vec3D();
  float a1 = random(1.0);
  float a2 = random(1.0);
  Vec3D v0 = f.a.copy();
  Vec3D v1 = f.b.copy().sub(v0);
  Vec3D v2 = f.c.copy().sub(v0);
  r = v1.copy().scaleSelf(a1).addSelf(v2.copy().scaleSelf(a2).scaleSelf(1-a1));
  r.addSelf(v0);
  Vec3D r1 = new Vec3D(r.x, r.y, r.z);
  return r1;
}





















Grab the processing file here.
I have other functions and piece of code that i'll be posting (hope weekly).

2 comments:

  1. Also possible much shorter:

    Vec3D randomFacePoint(Face face) {
    float u = random(1);
    float v = random(1-u);
    return face.toTriangle().fromBarycentric(new Vec3D(u, v, 1-u-v));
    }

    ReplyDelete