Esempio n. 1
0
  /**
   * Intersect the given Ray with this shape. Tests for every triangle of the shape. Speed depends
   * hence on the complexity of the shape.
   *
   * @param Ray ray The Ray to be intersected
   * @param Matrix4f transformation The transformation of the Shape into its actual position
   * @return a RayShapeIntersection with the coordinates of the HitPoint if any.
   */
  public RayShapeIntersection intersect(Ray ray, Matrix4f transformation) {
    RayShapeIntersection intersection = new RayShapeIntersection();
    List<RayShapeIntersection> hitTriangles = new ArrayList<RayShapeIntersection>();

    // get all triangles
    Iterator<Triangle> it = getTriangles().iterator();
    Triangle triangle;
    // calculate intersection of ray and triangle
    while (it.hasNext()) {
      triangle = it.next();
      triangle.transform(transformation);
      intersection = calculateIntersection(ray, triangle);
      if (intersection.hit) {
        hitTriangles.add(intersection);
      }
    }

    Vector3f distance = new Vector3f();
    float shortestDist = Float.MAX_VALUE;
    for (RayShapeIntersection in : hitTriangles) {
      distance.sub(ray.getOrigin(), in.hitPoint);
      if (distance.length() < shortestDist) {
        intersection = in;
        shortestDist = distance.length();
      }
    }

    // return the intersected point if any
    return intersection;
  }