computeAABB method

  1. @override
AABB computeAABB()
override

Implementation

@override
AABB computeAABB() {
  AABB? aabb;
  for (final List<ClipShape> clips in clipShapes) {
    for (final ClipShape clipShape in clips) {
      AABB bounds = clipShape.shape.computeAABB();
      if (aabb == null) {
        aabb = bounds;
      } else {
        if (bounds[0] < aabb[0]) {
          aabb[0] = bounds[0];
        }
        if (bounds[1] < aabb[1]) {
          aabb[1] = bounds[1];
        }
        if (bounds[2] > aabb[2]) {
          aabb[2] = bounds[2];
        }
        if (bounds[3] > aabb[3]) {
          aabb[3] = bounds[3];
        }
      }
    }
  }
  if (aabb != null) {
    return aabb;
  }

  for (final ActorComponent component in children!) {
    if (component is! ActorBasePath) {
      continue;
    }
    ActorBasePath path = component as ActorBasePath;
    // This is the axis aligned bounding box in the space of the
    // parent (this case our shape).
    AABB pathAABB = path.getPathAABB();

    if (aabb == null) {
      aabb = pathAABB;
    } else {
      // Combine.
      aabb[0] = min(aabb[0], pathAABB[0]);
      aabb[1] = min(aabb[1], pathAABB[1]);

      aabb[2] = max(aabb[2], pathAABB[2]);
      aabb[3] = max(aabb[3], pathAABB[3]);
    }
  }

  double minX = double.maxFinite;
  double minY = double.maxFinite;
  double maxX = -double.maxFinite;
  double maxY = -double.maxFinite;

  if (aabb == null) {
    return AABB.fromValues(minX, minY, maxX, maxY);
  }
  Mat2D world = worldTransform;

  double maxStroke = 0.0;
  for (final ActorStroke stroke in _strokes) {
    if (stroke.width > maxStroke) {
      maxStroke = stroke.width;
    }
  }
  double padStroke = maxStroke / 2.0;
  aabb[0] -= padStroke;
  aabb[2] += padStroke;
  aabb[1] -= padStroke;
  aabb[3] += padStroke;

  List<Vec2D> points = [
    Vec2D.fromValues(aabb[0], aabb[1]),
    Vec2D.fromValues(aabb[2], aabb[1]),
    Vec2D.fromValues(aabb[2], aabb[3]),
    Vec2D.fromValues(aabb[0], aabb[3])
  ];
  for (var i = 0; i < points.length; i++) {
    Vec2D pt = points[i];
    Vec2D wp = Vec2D.transformMat2D(pt, pt, world);
    if (wp[0] < minX) {
      minX = wp[0];
    }
    if (wp[1] < minY) {
      minY = wp[1];
    }

    if (wp[0] > maxX) {
      maxX = wp[0];
    }
    if (wp[1] > maxY) {
      maxY = wp[1];
    }
  }
  return AABB.fromValues(minX, minY, maxX, maxY);
}