Skip to content

Commit

Permalink
Avoid using Array2D since it's deprecated in Pharo 12
Browse files Browse the repository at this point in the history
* asMatrixCollection was only used from printOn:. I rewrote it to print directly.
* The real use was as internal state of BlMatrixDecomposition2D, to store (sx, sy, shx, shy) values in some maths for interpolation. I rewrote the code to directly reference those 4 values instead of the Array2D.

See #643
  • Loading branch information
tinchodias committed Dec 3, 2024
1 parent 8ad0367 commit 7764807
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 169 deletions.
10 changes: 1 addition & 9 deletions src/Bloc/BlMatrix.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ BlMatrix >> = anObject [
self subclassResponsibility
]

{ #category : #converting }
BlMatrix >> asMatrixCollection [
<return: #Array2D>

^ self subclassResponsibility
]

{ #category : #'matrix - mathematical functions' }
BlMatrix >> decomposition [
"Return a decomposition of the matrix"
Expand All @@ -45,8 +38,7 @@ BlMatrix >> hash [

{ #category : #'matrix - mathematical functions' }
BlMatrix >> interpolate: aFactor to: anotherMatrix [
"Perform a matrix interpolation with a given factor"
<return: #BlMatrix>
"Answer a `BlMatrix` that interpolates self with another matrix, with a given factor."

^ (self decomposition
interpolate: aFactor
Expand Down
110 changes: 54 additions & 56 deletions src/Bloc/BlMatrix2D.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -84,79 +84,63 @@ BlMatrix2D >> = anObject [
and: [ self y = anObject y ] ] ] ] ]
]

{ #category : #converting }
BlMatrix2D >> asMatrixCollection [
^ Array2D rows: 3 columns: 3 contents: {
sx . shx . x.
shy . sy . y.
0 . 0. 1
}
]

{ #category : #'matrix - mathematical functions' }
BlMatrix2D >> decomposition [
"Decompose this 2D Matrix into components and return a resulting decomposition.
Based on https://drafts.csswg.org/css-transforms/#decomposing-a-2d-matrix"
<return: #BlMatrixDecomposition2D>

| row0x row0y row1x row1y scaleX scaleY translate determinant angle matrix |

row0x := self sx.
row0y := self shy.
row1x := self shx.
row1y := self sy.
"Answer my decomposition, in a`BlMatrixDecomposition2D`.
Based on: https://drafts.csswg.org/css-transforms/#decomposing-a-2d-matrix"

translate := BlVector x: self x y: self y.
| row0x row0y row1x row1y scaleX scaleY determinant angle |
row0x := sx.
row0y := shy.
row1x := shx.
row1y := sy.

scaleX := ((row0x * row0x) + (row0y * row0y)) sqrt.
scaleY := ((row1x * row1x) + (row1y * row1y)) sqrt.

determinant := (row0x * row1y) - (row0y * row1x).
"If determinant is negative, one axis was flipped."
determinant < 0
ifTrue: [
row0x < row1y
ifTrue: [ scaleX := scaleX negated ]
ifFalse: [ scaleY := scaleY negated ] ].
determinant := (row0x * row1y) - (row0y * row1x).
determinant < 0 ifTrue: [
row0x < row1y
ifTrue: [ scaleX := scaleX negated ]
ifFalse: [ scaleY := scaleY negated ] ].

"Renormalize matrix to remove scale."
scaleX isZero
ifFalse: [
row0x := row0x * (1.0 / scaleX).
row0y := row0y * (1.0 / scaleX) ].
scaleY isZero
ifFalse: [
row1x := row1x * (1.0 / scaleY).
row1y := row1y * (1.0 / scaleY) ].
scaleX isZero ifFalse: [
row0x := row0x * (1.0 / scaleX).
row0y := row0y * (1.0 / scaleX) ].
scaleY isZero ifFalse: [
row1x := row1x * (1.0 / scaleY).
row1y := row1y * (1.0 / scaleY) ].

"Compute rotation and renormalize matrix."
angle := row0y arcTan: row0x.
angle isZero
ifFalse: [
| sn cs m11 m12 m21 m22 |
sn := row0y negated.
cs := row0x.
m11 := row0x.
m12 := row0y.
m21 := row1x.
m22 := row1y.
row0x := (cs * m11) + (sn * m21).
row0y := (cs * m12) + (sn * m22).
row1x := (sn negated * m11) + (cs * m21).
row1y := (sn negated * m12) + (cs * m22) ].

matrix := Array2D
rows: 2
columns: 2
contents: { row0x . row0y . row1x . row1y }.

angle isZero ifFalse: [
| sn cs m11 m12 m21 m22 |
sn := row0y negated.
cs := row0x.
m11 := row0x.
m12 := row0y.
m21 := row1x.
m22 := row1y.
row0x := (cs * m11) + (sn * m21).
row0y := (cs * m12) + (sn * m22).
row1x := (sn negated * m11) + (cs * m21).
row1y := (sn negated * m12) + (cs * m22) ].

"Convert into degrees because our rotation functions expect it."
angle := angle radiansToDegrees.

^ BlMatrixDecomposition2D new
translation: translate;
translation: (BlVector x: x y: y);
scale: (BlVector x: scaleX y: scaleY);
angle: angle;
matrix: matrix
sx: row0x;
sy: row1y;
shx: row1x;
shy: row0y;
yourself
]

{ #category : #'matrix - mathematical functions' }
Expand Down Expand Up @@ -281,7 +265,21 @@ BlMatrix2D >> perspective: aDepth [

{ #category : #printing }
BlMatrix2D >> printOn: aStream [
aStream print: self asMatrixCollection

super printOn: aStream.

aStream nextPut: $(.

#(x y sx sy shx shy)
do: [ :each |
aStream
nextPutAll: each;
nextPut: $:;
space.
((self perform: each) printOn: aStream) ]
separatedBy: [ aStream space ].

aStream nextPut: $)
]

{ #category : #'matrix - transformations' }
Expand Down
32 changes: 19 additions & 13 deletions src/Bloc/BlMatrix3D.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,6 @@ BlMatrix3D >> = anObject [
^true
]

{ #category : #converting }
BlMatrix3D >> asMatrixCollection [
^ Array2D
rows: 4
columns: 4
contents: {
sx . shx0 . shx1 . x .
shy0 . sy . shy1 . y .
shz0 . shz1 . sz . z .
wx . wy . wz . w
}
]

{ #category : #initialization }
BlMatrix3D >> initialize [
super initialize.
Expand Down Expand Up @@ -296,6 +283,25 @@ BlMatrix3D >> preTranslateX: aX Y: aY Z: aZ [
w := w + (aX * wx) + (aY * wy) + (aZ * wz)
]

{ #category : #printing }
BlMatrix3D >> printOn: aStream [

super printOn: aStream.

aStream nextPut: $(.

#(#x #y #z #w #sx #sy #sz #shx0 #shy0 #shz0 #shx1 #shy1 #shz1 #wx #wy #wz)
do: [ :each |
aStream
nextPutAll: each;
nextPut: $:;
space.
((self perform: each) printOn: aStream) ]
separatedBy: [ aStream space ].

aStream nextPut: $)
]

{ #category : #initialization }
BlMatrix3D >> quaternion: aQuaternion [
"Set me to be a rotation matrix defined by a given quaternion"
Expand Down
12 changes: 2 additions & 10 deletions src/Bloc/BlMatrixDecomposition.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,16 @@ Class {
#category : #'Bloc-Basic-Math'
}

{ #category : #converting }
BlMatrixDecomposition >> asDictionary [
<return: #Dictionary>

^ self subclassResponsibility
]

{ #category : #'matrix - mathematical functions' }
BlMatrixDecomposition >> composition [
"Compose and return a matrix for this decomposition"
<return: #BlMatrix>
"Compose and return a `BlMatrix` for this decomposition"

^ self subclassResponsibility
]

{ #category : #'matrix - mathematical functions' }
BlMatrixDecomposition >> interpolate: aFactor to: anotherDecomposition [
<return: #BlMatrixDecomposition>
"Answer a `BlMatrixDecomposition` that interpolates self with anotherDecomposition."

^ self subclassResponsibility
]
Loading

0 comments on commit 7764807

Please sign in to comment.