namespace Unity.UIWidgets.painting {
public static class MatrixUtils {
public static Matrix3 makeRotate ( float degree )
{
var m = Matrix3 . I ( ) ;
m . setRotate ( degree ) ;
return m ;
}
public static Matrix3 makeTrans ( Vector2 vector )
{
var m = Matrix3 . I ( ) ;
m . setTranslate ( vector . x , vector . y ) ;
return m ;
}
public static Matrix3 makeTrans ( float dx , float dy )
{
var m = Matrix3 . I ( ) ;
m . setTranslate ( dx , dy ) ;
return m ;
}
public static Matrix3 Value ( this Matrix3 matrix3 )
{
var m = Matrix3 . I ( ) ;
m . copyFrom ( matrix3 ) ;
return m ;
}
public static Matrix3 inverse ( this Matrix3 matrix3 )
{
var m = Matrix3 . I ( ) ;
var invertable = matrix3 . invert ( m ) ;
D . assert ( invertable ) ;
return m ;
}
public static Rect inverseTransformRect ( this Matrix3 matrix3 , Rect rect )
{
D . assert ( rect ! = null ) ;
D . assert ( matrix3 . determinant ! = 0.0 ) ;
if ( matrix3 . isIdentity ( ) ) {
return rect ;
}
var inverse = matrix3 . inverse ( ) ;
return inverse . transformRect ( rect ) ;
}
public static Offset transformPoint ( this Matrix3 matrix3 , Offset point )
{
return matrix3 . mapXY ( ( float ) point . dx , ( float ) point . dy ) ;
}
public static Rect transformRect ( this Matrix3 matrix3 , Rect rect )
{
return matrix3 . mapRect ( rect ) ;
}
if ( matrix3 . isTranslate ( ) ) return matrix3 . mapXY ( 0 , 0 ) ;
return null ;
}
public static Offset transformPoint ( this Matrix4x4 transform , Offset point ) {
var position3 = new Vector3 ( ( float ) point . dx , ( float ) point . dy , 0 ) ;
var transformed3 = transform . MultiplyPoint ( position3 ) ;
return new Offset ( transformed3 . x , transformed3 . y ) ;
}
public static Rect transformRect ( this Matrix4x4 transform , Offset [ ] points , out bool isRect ) {
D . assert ( points ! = null & & points . Length = = 4 , "expected 4 points" ) ;
var topLeft = transform . transformPoint ( points [ 0 ] ) ;
var topRight = transform . transformPoint ( points [ 1 ] ) ;
var bottomLeft = transform . transformPoint ( points [ 2 ] ) ;
var bottomRight = transform . transformPoint ( points [ 3 ] ) ;
isRect = topLeft . dy = = topRight . dy
& & topRight . dx = = bottomRight . dx
& & bottomRight . dy = = bottomLeft . dy
& & bottomLeft . dx = = topLeft . dx ;
var left = Math . Min ( Math . Min ( Math . Min ( topLeft . dx , topRight . dx ) , bottomLeft . dx ) , bottomRight . dx ) ;
var right = Math . Max ( Math . Max ( Math . Max ( topLeft . dx , topRight . dx ) , bottomLeft . dx ) , bottomRight . dx ) ;
var top = Math . Min ( Math . Min ( Math . Min ( topLeft . dy , topRight . dy ) , bottomLeft . dy ) , bottomRight . dy ) ;
var bottom = Math . Max ( Math . Max ( Math . Max ( topLeft . dy , topRight . dy ) , bottomLeft . dy ) , bottomRight . dy ) ;
return Rect . fromLTRB ( left , top , right , bottom ) ;
}
public static Rect transformRect ( this Matrix4x4 transform , Rect rect , out bool isRect ) {
return transform . transformRect (
new [ ] { rect . topLeft , rect . topRight , rect . bottomLeft , rect . bottomRight } ,
out isRect ) ;
}
public static Rect transformRect ( this Matrix4x4 transform , Offset [ ] points ) {
bool isRect ;
return transform . transformRect ( points , out isRect ) ;
}
public static Rect transformRect ( this Matrix4x4 transform , Rect rect ) {
bool isRect ;
return transform . transformRect ( rect , out isRect ) ;
}
public static Rect inverseTransformRect ( this Matrix4x4 transform , Rect rect ) {
D . assert ( rect ! = null ) ;
D . assert ( transform . determinant ! = 0.0 ) ;
if ( transform . isIdentity ) {
return rect ;
}
var inverse = transform . inverse ;
return inverse . transformRect ( rect ) ;
}
public static Offset getAsTranslation ( this Matrix4x4 transform )
{
if ( transform . m00 = = 1.0 & &
transform . m10 = = 0.0 & &
transform . m20 = = 0.0 & &
transform . m30 = = 0.0 & &
transform . m01 = = 0.0 & &
transform . m11 = = 1.0 & &
transform . m21 = = 0.0 & &
transform . m31 = = 0.0 & &
transform . m02 = = 0.0 & &
transform . m12 = = 0.0 & &
transform . m22 = = 1.0 & &
transform . m32 = = 0.0 & &
transform . m23 = = 0.0 & &
transform . m33 = = 1.0 ) {
return new Offset ( transform . m03 , transform . m13 ) ;
}
return null ;
}
public static bool isPerspective ( this Matrix4x4 transform ) {
return transform [ 3 , 0 ] ! = 0 | | transform [ 3 , 1 ] ! = 0 | | transform [ 3 , 2 ] ! = 0 | | transform [ 3 , 3 ] ! = 1 ;
return matrix3 . isTranslate ( ) ? new Offset ( matrix3 [ Matrix3 . kMTransX ] , matrix3 [ Matrix3 . kMTransY ] ) : null ;
}
}
}