createBoxFrom<R extends Box> static method

R createBoxFrom<R extends Box>(
  1. Iterable<Position> positions,
  2. CreateBox<R> factory
)

A minimum bounding box created by factory, calculated from positions.

Throws FormatException if cannot create (ie. positions is empty).

Implementation

static R createBoxFrom<R extends Box>(
  Iterable<Position> positions,
  CreateBox<R> factory,
) {
  // calculate mininum and maximum coordinates
  num? minX, minY, minZ, minM;
  num? maxX, maxY, maxZ, maxM;
  var isFirst = true;
  for (final cp in positions) {
    if (isFirst) {
      minX = cp.x;
      minY = cp.y;
      minZ = cp.optZ;
      minM = cp.optM;
      maxX = cp.x;
      maxY = cp.y;
      maxZ = cp.optZ;
      maxM = cp.optM;
    } else {
      minX = math.min(minX!, cp.x);
      minY = math.min(minY!, cp.y);
      minZ = cp.is3D && minZ != null ? math.min(minZ, cp.z) : null;
      minM = cp.isMeasured && minM != null ? math.min(minM, cp.m) : null;
      maxX = math.max(maxX!, cp.x);
      maxY = math.max(maxY!, cp.y);
      maxZ = cp.is3D && maxZ != null ? math.max(maxZ, cp.z) : null;
      maxM = cp.isMeasured && maxM != null ? math.max(maxM, cp.m) : null;
    }
    isFirst = false;
  }

  if (isFirst) {
    throw const FormatException('Positions should not be empty.');
  }

  // create a new bounding box
  return factory.call(
    minX: minX!,
    minY: minY!,
    minZ: minZ,
    minM: minM,
    maxX: maxX!,
    maxY: maxY!,
    maxZ: maxZ,
    maxM: maxM,
  );
}