import NotFoundException from '../../NotFoundException' ; import ResultPoint from '../../ResultPoint' ; import BitMatrix from '../../common/BitMatrix' ; import { int } from '../../../customTypings' ; author Guenther Grau
  export default class BoundingBox { private image : BitMatrix ; private topLeft : ResultPoint ; private bottomLeft : ResultPoint ; private topRight : ResultPoint ; private bottomRight : ResultPoint ; private minX : int ; private maxX : int ; private minY : int ; private maxY : int ; constructor ( image , topLeft ? , bottomLeft ? , topRight ? , bottomRight ? ) { if ( image instanceof BoundingBox ) { this . constructor_2 ( image ) ; } else { this . constructor_1 ( image , topLeft , bottomLeft , topRight , bottomRight ) ; } } param image param topLeft param bottomLeft param topRight param bottomRight throws private constructor_1 ( image , topLeft , bottomLeft , topRight , bottomRight ) { const leftUnspecified = topLeft == null || bottomLeft == null ; const rightUnspecified = topRight == null || bottomRight == null ; if ( leftUnspecified && rightUnspecified ) { throw new NotFoundException ( ) ; } if ( leftUnspecified ) { topLeft = new ResultPoint ( 0 , topRight . getY ( ) ) ; bottomLeft = new ResultPoint ( 0 , bottomRight . getY ( ) ) ; } else if ( rightUnspecified ) { topRight = new ResultPoint ( image . getWidth ( ) - 1 , topLeft . getY ( ) ) ; bottomRight = new ResultPoint ( image . getWidth ( ) - 1 , bottomLeft . getY ( ) ) ; } this . image = image ; this . topLeft = topLeft ; this . bottomLeft = bottomLeft ; this . topRight = topRight ; this . bottomRight = bottomRight ; this . minX = < int > Math . trunc ( Math . min ( topLeft . getX ( ) , bottomLeft . getX ( ) ) ) ; this . maxX = < int > Math . trunc ( Math . max ( topRight . getX ( ) , bottomRight . getX ( ) ) ) ; this . minY = < int > Math . trunc ( Math . min ( topLeft . getY ( ) , topRight . getY ( ) ) ) ; this . maxY = < int > Math . trunc ( Math . max ( bottomLeft . getY ( ) , bottomRight . getY ( ) ) ) ; } private constructor_2 ( boundingBox ) { this . image = boundingBox . image ; this . topLeft = boundingBox . getTopLeft ( ) ; this . bottomLeft = boundingBox . getBottomLeft ( ) ; this . topRight = boundingBox . getTopRight ( ) ; this . bottomRight = boundingBox . getBottomRight ( ) ; this . minX = boundingBox . getMinX ( ) ; this . maxX = boundingBox . getMaxX ( ) ; this . minY = boundingBox . getMinY ( ) ; this . maxY = boundingBox . getMaxY ( ) ; } throws static merge ( leftBox , rightBox ) { if ( leftBox == null ) { return rightBox ; } if ( rightBox == null ) { return leftBox ; } return new BoundingBox ( leftBox . image , leftBox . topLeft , leftBox . bottomLeft , rightBox . topRight , rightBox . bottomRight ) ; } throws addMissingRows ( missingStartRows , missingEndRows , isLeft ) { let newTopLeft = this . topLeft ; let newBottomLeft = this . bottomLeft ; let newTopRight = this . topRight ; let newBottomRight = this . bottomRight ; if ( missingStartRows > 0 ) { let top = isLeft ? this . topLeft : this . topRight ; let newMinY = < int > Math . trunc ( top . getY ( ) - missingStartRows ) ; if ( newMinY < 0 ) { newMinY = 0 ; } let newTop = new ResultPoint ( top . getX ( ) , newMinY ) ; if ( isLeft ) { newTopLeft = newTop ; } else { newTopRight = newTop ; } } if ( missingEndRows > 0 ) { let bottom = isLeft ? this . bottomLeft : this . bottomRight ; let newMaxY = < int > Math . trunc ( bottom . getY ( ) + missingEndRows ) ; if ( newMaxY >= this . image . getHeight ( ) ) { newMaxY = this . image . getHeight ( ) - 1 ; } let newBottom = new ResultPoint ( bottom . getX ( ) , newMaxY ) ; if ( isLeft ) { newBottomLeft = newBottom ; } else { newBottomRight = newBottom ; } } return new BoundingBox ( this . image , newTopLeft , newBottomLeft , newTopRight , newBottomRight ) ; } getMinX ( ) { return this . minX ; } getMaxX ( ) { return this . maxX ; } getMinY ( ) { return this . minY ; } getMaxY ( ) { return this . maxY ; } getTopLeft ( ) { return this . topLeft ; } getTopRight ( ) { return this . topRight ; } getBottomLeft ( ) { return this . bottomLeft ; } getBottomRight ( ) { return this . bottomRight ; } }