import Matrix3x3 from "./matrix" ; import Orientation from "./orientation" ; import { ISO_NONE } from "../constants" ; export default class Transform { private _rotationDegree : { normal : { x : number ; y : number ; z : number ; } ; side : { x : number ; y : number ; z : number ; } ; } ; private _matrix : Matrix3x3 ; private _float : number ; constructor ( orientation , parentOrientation ) { this . _rotationDegree = this . _calcRotationDegree ( orientation , parentOrientation ) ; this . _matrix = Matrix3x3 . ONE ; this . _float = 0 ; } get changed ( ) { return ! ( this . _matrix . match ( Matrix3x3 . ONE ) && this . _float === 0 ) ; } public rotateTo ( props ) { const normal = props . normal ; const side = props . side ; const degreeZ = normal * this . _rotationDegree . normal . z + side * this . _rotationDegree . side . z ; const degreeY = normal * this . _rotationDegree . normal . y + side * this . _rotationDegree . side . y ; const degreeX = normal * this . _rotationDegree . normal . x + side * this . _rotationDegree . side . x ; this . _matrix . rotateZ ( degreeZ ) ; this . _matrix . rotateY ( degreeY ) ; this . _matrix . rotateX ( degreeX ) ; } public toString ( ) { if ( ! this . changed ) { return "none" ; } else { return `matrix3d(${ this . _matrix . toString4x4 ( ) }) translateZ(${ this . _float . toFixed ( 4 ) }px)` ; } } public setScale ( val ) { this . _matrix . scale ( { x : val , y : val , z : val } ) ; } public setFloat ( val ) { this . _float = val ; } public reset ( ) { this . _matrix = Matrix3x3 . ONE ; } private _calcRotationDegree ( orientation , parentOrientation ) { const rotations = { normal : { x : 0 , y : 0 , z : 0 } , side : { x : 0 , y : 0 , z : 0 } , } ; if ( orientation . match ( new Orientation ( ISO_NONE ) ) ) return rotations ; if ( parentOrientation . match ( new Orientation ( ISO_NONE ) ) ) { rotations . normal . x = orientation . isHorizontal ? 55 : - 35 ; if ( orientation . isVertical ) { rotations . side . y = orientation . isLeft ? - 45 : 45 ; } else { rotations . side . z = orientation . isLeft ? 45 : - 45 ; } } else if ( parentOrientation . isHorizontal ) { if ( orientation . isHorizontal ) { rotations . side . z = parentOrientation . isRight ? 90 : - 90 ; } else { rotations . normal . x = - 90 ; if ( ! parentOrientation . side . match ( orientation . side ) ) { rotations . side . y = parentOrientation . isRight ? - 90 : 90 ; } } } else if ( parentOrientation . isVertical ) { if ( orientation . isVertical ) { rotations . side . y = parentOrientation . isRight ? - 90 : 90 ; } else { rotations . normal . x = 90 ; if ( ! parentOrientation . side . match ( orientation . side ) ) { rotations . side . z = parentOrientation . isRight ? 90 : - 90 ; } } } return rotations ; } }