cut method

bool cut(
  1. Box one,
  2. Box two
)

Implementation

bool cut(Box one, Box two) {
  final wholeR = volume(one, momentsR);
  final wholeG = volume(one, momentsG);
  final wholeB = volume(one, momentsB);
  final wholeW = volume(one, weights);

  final maxRResult = maximize(
      one, Direction.red, one.r0 + 1, one.r1, wholeR, wholeG, wholeB, wholeW);
  final maxGResult = maximize(one, Direction.green, one.g0 + 1, one.g1,
      wholeR, wholeG, wholeB, wholeW);
  final maxBResult = maximize(one, Direction.blue, one.b0 + 1, one.b1, wholeR,
      wholeG, wholeB, wholeW);

  Direction cutDirection;
  final maxR = maxRResult.maximum;
  final maxG = maxGResult.maximum;
  final maxB = maxBResult.maximum;
  if (maxR >= maxG && maxR >= maxB) {
    cutDirection = Direction.red;
    if (maxRResult.cutLocation < 0) {
      return false;
    }
  } else if (maxG >= maxR && maxG >= maxB) {
    cutDirection = Direction.green;
  } else {
    cutDirection = Direction.blue;
  }

  two.r1 = one.r1;
  two.g1 = one.g1;
  two.b1 = one.b1;

  switch (cutDirection) {
    case Direction.red:
      one.r1 = maxRResult.cutLocation;
      two.r0 = one.r1;
      two.g0 = one.g0;
      two.b0 = one.b0;
      break;
    case Direction.green:
      one.g1 = maxGResult.cutLocation;
      two.r0 = one.r0;
      two.g0 = one.g1;
      two.b0 = one.b0;
      break;
    case Direction.blue:
      one.b1 = maxBResult.cutLocation;
      two.r0 = one.r0;
      two.g0 = one.g0;
      two.b0 = one.b1;
      break;
    default:
      throw 'unexpected direction $cutDirection';
  }

  one.vol = (one.r1 - one.r0) * (one.g1 - one.g0) * (one.b1 - one.b0);
  two.vol = (two.r1 - two.r0) * (two.g1 - two.g0) * (two.b1 - two.b0);
  return true;
}