createFromObject<R extends Box> static method

R createFromObject<R extends Box>(
  1. Object box, {
  2. required CreateBox<R> to,
  3. Coords? type,
})

Creates a bounding box of R from box (of R or Iterable<num>).

If box is R and with compatible coordinate type already, then it's returned. Other Box instances are copied as R.

If box is Iterable<num>, then a bounding box instance is created using the factory function to. Supported coordinate value combinations by coordinate type:

Type Expected values
xy minX, minY, maxX, maxY
xyz minX, minY, minZ, maxX, maxY, maxZ
xym minX, minY, minM, maxX, maxY, maxM
xyzm minX, minY, minZ, minM, maxX, maxY, maxZ, maxM

Use an optional type to explicitely set the coordinate type. If not provided and an iterable has 6 items, then xyz coordinates are assumed.

Otherwise throws FormatException.

Implementation

static R createFromObject<R extends Box>(
  Object box, {
  required CreateBox<R> to,
  Coords? type,
}) {
  if (box is Box) {
    if (box is R && (type == null || type == box.type)) {
      // box is of R and with compatiable coord type
      return box;
    } else {
      if (type == null) {
        // create a copy with same coordinate values
        return to.call(
          minX: box.minX,
          minY: box.minY,
          minZ: box.minZ,
          minM: box.minM,
          maxX: box.maxX,
          maxY: box.maxY,
          maxZ: box.maxZ,
          maxM: box.maxM,
        );
      } else {
        // create a copy with z and m selected if coord type suggests so
        return to.call(
          minX: box.minX,
          minY: box.minY,
          minZ: type.is3D ? box.minZ ?? 0 : null,
          minM: type.isMeasured ? box.minM ?? 0 : null,
          maxX: box.maxX,
          maxY: box.maxY,
          maxZ: type.is3D ? box.maxZ ?? 0 : null,
          maxM: type.isMeasured ? box.maxM ?? 0 : null,
        );
      }
    }
  } else if (box is Iterable<num>) {
    return buildBox(box, to: to, type: type);
  }
  throw invalidCoordinates;
}