1380
|
1 |
"{ Package: 'stx:libview2' }"
|
|
2 |
|
|
3 |
DisplayTransform variableFloatSubclass:#MatrixTransform2x3
|
1250
|
4 |
instanceVariableNames:''
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Graphics-Transformations'
|
|
8 |
!
|
|
9 |
|
3425
|
10 |
!MatrixTransform2x3 class methodsFor:'documentation'!
|
1250
|
11 |
|
3425
|
12 |
documentation
|
|
13 |
"
|
|
14 |
This class represents a transformation for points, that is a combination of scale, offset, and rotation.
|
|
15 |
It is implemented as a 2x3 matrix containing the transformation from the local coordinate system in the global coordinate system.
|
|
16 |
Thus, transforming points from local to global coordinates is fast and cheap,
|
|
17 |
whereas transformations from global to local coordinate systems are relatively expensive.
|
|
18 |
|
|
19 |
Implementation Note: It is assumed that the transformation deals with Integer points.
|
|
20 |
All transformations will return Integer coordinates (even though float points may be passed in here).
|
|
21 |
"
|
3111
|
22 |
!
|
1250
|
23 |
|
3425
|
24 |
examples
|
|
25 |
"
|
|
26 |
example :
|
|
27 |
[exBegin]
|
|
28 |
|v|
|
|
29 |
|
|
30 |
v := View new openAndWait.
|
|
31 |
v transformation:(MatrixTransform2x3 withAngle:5).
|
|
32 |
v displayLineFrom:50@50 to:100@100
|
|
33 |
[exEnd]
|
|
34 |
|
|
35 |
[exBegin]
|
|
36 |
|v|
|
|
37 |
|
|
38 |
v := View new openAndWait.
|
|
39 |
v transformation:(MatrixTransform2x3 withAngle:45).
|
|
40 |
v fillRectangleX:10 y:10 width:100 height:100.
|
|
41 |
[exEnd]
|
|
42 |
"
|
|
43 |
! !
|
1250
|
44 |
|
|
45 |
!MatrixTransform2x3 class methodsFor:'instance creation'!
|
|
46 |
|
|
47 |
identity
|
3435
|
48 |
^self new setScale: 1.0
|
3111
|
49 |
!
|
1250
|
50 |
|
|
51 |
new
|
3111
|
52 |
^self new: 6
|
|
53 |
!
|
1250
|
54 |
|
|
55 |
transformFromLocal: localBounds toGlobal: globalBounds
|
|
56 |
^((self withOffset: (globalBounds center)) composedWithLocal:
|
|
57 |
(self withScale: (globalBounds extent / localBounds extent) asFloatPoint))
|
|
58 |
composedWithLocal: (self withOffset: localBounds center negated)
|
|
59 |
"
|
|
60 |
^(self identity)
|
|
61 |
setScale: (globalBounds extent / localBounds extent) asFloatPoint;
|
|
62 |
setOffset: localBounds center negated asFloatPoint;
|
|
63 |
composedWithGlobal:(self withOffset: globalBounds center asFloatPoint)
|
3111
|
64 |
"
|
|
65 |
!
|
1250
|
66 |
|
|
67 |
withAngle: angle
|
3425
|
68 |
"return a new transformation which rotates by angle (in degrees)"
|
|
69 |
|
|
70 |
^self new setAngle: angle
|
3111
|
71 |
!
|
1250
|
72 |
|
|
73 |
withOffset: aPoint
|
3435
|
74 |
"return a new transformation which translates by aPoint"
|
3425
|
75 |
|
3435
|
76 |
^self identity setOffset: aPoint
|
3111
|
77 |
!
|
1250
|
78 |
|
|
79 |
withRotation: angle
|
3435
|
80 |
"return a new transformation which rotates by angle (in degrees)"
|
3425
|
81 |
|
3435
|
82 |
^self new setAngle: angle
|
3111
|
83 |
!
|
1250
|
84 |
|
|
85 |
withScale: aPoint
|
3435
|
86 |
"return a new transformation which scales by aPoint's x/y values in x/y directions"
|
3425
|
87 |
|
3435
|
88 |
^self new setScale: aPoint
|
3425
|
89 |
! !
|
|
90 |
|
|
91 |
|
1250
|
92 |
|
|
93 |
!MatrixTransform2x3 methodsFor:'accessing'!
|
|
94 |
|
|
95 |
at: index
|
1380
|
96 |
"/ <primitive: 'primitiveFloatArrayAt'>
|
|
97 |
"/ ^Float fromIEEE32Bit: (self basicAt: index)
|
|
98 |
^ self basicAt: index
|
|
99 |
!
|
1250
|
100 |
|
|
101 |
at: index put: value
|
1380
|
102 |
"/ <primitive: 'primitiveFloatArrayAtPut'>
|
|
103 |
"/ value isFloat
|
|
104 |
"/ ifTrue:[self basicAt: index put: value asIEEE32BitWord]
|
|
105 |
"/ ifFalse:[self at: index put: value asFloat].
|
|
106 |
"/ ^value
|
|
107 |
^ self basicAt: index put:value
|
|
108 |
!
|
1250
|
109 |
|
|
110 |
inverseTransformation
|
|
111 |
"Return the inverse transformation of the receiver.
|
|
112 |
The inverse transformation is computed by first calculating
|
|
113 |
the inverse offset and then computing transformations
|
|
114 |
for the two identity vectors (1@0) and (0@1)"
|
|
115 |
| r1 r2 r3 m |
|
|
116 |
r3 _ self invertPoint: 0@0.
|
|
117 |
r1 _ (self invertPoint: 1@0) - r3.
|
|
118 |
r2 _ (self invertPoint: 0@1) - r3.
|
|
119 |
m _ self species new.
|
|
120 |
m
|
|
121 |
a11: r1 x; a12: r2 x; a13: r3 x;
|
|
122 |
a21: r1 y; a22: r2 y; a23: r3 y.
|
3111
|
123 |
^m
|
|
124 |
!
|
1250
|
125 |
|
|
126 |
offset
|
3111
|
127 |
^self a13 @ self a23
|
|
128 |
!
|
1250
|
129 |
|
|
130 |
offset: aPoint
|
|
131 |
self a13: aPoint x asFloat.
|
3111
|
132 |
self a23: aPoint y asFloat.
|
|
133 |
! !
|
1250
|
134 |
|
|
135 |
!MatrixTransform2x3 methodsFor:'comparing'!
|
|
136 |
|
|
137 |
= MatrixTransform2x3
|
1380
|
138 |
| length |
|
|
139 |
"/ <primitive:'primitiveFloatArrayEqual'>
|
|
140 |
self class = MatrixTransform2x3 class ifFalse:[^false].
|
|
141 |
length _ self size.
|
|
142 |
(length = MatrixTransform2x3 size) ifFalse:[^false].
|
|
143 |
1 to: self size do:[:i| (self at: i) = (MatrixTransform2x3 at: i) ifFalse:[^false]].
|
|
144 |
^true
|
|
145 |
!
|
1250
|
146 |
|
|
147 |
hash
|
1380
|
148 |
| result |
|
|
149 |
"/ <primitive:'primitiveFloatArrayHash'>
|
|
150 |
"/ result _ 0.
|
|
151 |
"/ 1 to: self size do:[:i| result _ result + (self basicAt: i) ].
|
|
152 |
"/ ^result bitAnd: 16r1FFFFFFF
|
|
153 |
result _ 0.
|
|
154 |
1 to: self size do:[:i| result _ result + (self basicAt: i) hash ].
|
|
155 |
^result bitAnd: 16r1FFFFFFF
|
|
156 |
! !
|
1250
|
157 |
|
|
158 |
!MatrixTransform2x3 methodsFor:'composing'!
|
|
159 |
|
|
160 |
composedWithLocal: aTransformation
|
|
161 |
"Return the composition of the receiver and the local transformation passed in"
|
|
162 |
aTransformation isMatrixTransform2x3 ifFalse:[^super composedWith: aTransformation].
|
3111
|
163 |
^self composedWithLocal: aTransformation asMatrixTransform2x3 into: self class new
|
|
164 |
!
|
1250
|
165 |
|
|
166 |
composedWithLocal: aTransformation into: result
|
1380
|
167 |
"Return the composition of the receiver and the local transformation passed in.
|
|
168 |
Store the composed matrix into result."
|
|
169 |
| a11 a12 a13 a21 a22 a23 b11 b12 b13 b21 b22 b23 matrix |
|
|
170 |
"/ <primitive: 'm23PrimitiveComposeMatrix'>
|
|
171 |
matrix _ aTransformation asMatrixTransform2x3.
|
|
172 |
a11 _ self a11. b11 _ matrix a11.
|
|
173 |
a12 _ self a12. b12 _ matrix a12.
|
|
174 |
a13 _ self a13. b13 _ matrix a13.
|
|
175 |
a21 _ self a21. b21 _ matrix a21.
|
|
176 |
a22 _ self a22. b22 _ matrix a22.
|
|
177 |
a23 _ self a23. b23 _ matrix a23.
|
|
178 |
result a11: (a11 * b11) + (a12 * b21).
|
|
179 |
result a12: (a11 * b12) + (a12 * b22).
|
|
180 |
result a13: a13 + (a11 * b13) + (a12 * b23).
|
|
181 |
result a21: (a21 * b11) + (a22 * b21).
|
|
182 |
result a22: (a21 * b12) + (a22 * b22).
|
|
183 |
result a23: a23 + (a21 * b13) + (a22 * b23).
|
|
184 |
^result
|
|
185 |
! !
|
1250
|
186 |
|
|
187 |
!MatrixTransform2x3 methodsFor:'converting'!
|
|
188 |
|
|
189 |
asMatrixTransform2x3
|
3111
|
190 |
^self
|
|
191 |
! !
|
1250
|
192 |
|
|
193 |
!MatrixTransform2x3 methodsFor:'element access'!
|
|
194 |
|
|
195 |
a11
|
3111
|
196 |
^self at: 1
|
|
197 |
!
|
1250
|
198 |
|
|
199 |
a11: value
|
3111
|
200 |
self at: 1 put: value
|
|
201 |
!
|
1250
|
202 |
|
|
203 |
a12
|
3111
|
204 |
^self at: 2
|
|
205 |
!
|
1250
|
206 |
|
|
207 |
a12: value
|
3111
|
208 |
self at: 2 put: value
|
|
209 |
!
|
1250
|
210 |
|
|
211 |
a13
|
3111
|
212 |
^self at: 3
|
|
213 |
!
|
1250
|
214 |
|
|
215 |
a13: value
|
3111
|
216 |
self at: 3 put: value
|
|
217 |
!
|
1250
|
218 |
|
|
219 |
a21
|
3111
|
220 |
^self at: 4
|
|
221 |
!
|
1250
|
222 |
|
|
223 |
a21: value
|
3111
|
224 |
self at: 4 put: value
|
|
225 |
!
|
1250
|
226 |
|
|
227 |
a22
|
3111
|
228 |
^self at: 5
|
|
229 |
!
|
1250
|
230 |
|
|
231 |
a22: value
|
3111
|
232 |
self at: 5 put: value
|
|
233 |
!
|
1250
|
234 |
|
|
235 |
a23
|
3111
|
236 |
^self at: 6
|
|
237 |
!
|
1250
|
238 |
|
|
239 |
a23: value
|
3111
|
240 |
self at: 6 put: value
|
|
241 |
! !
|
1250
|
242 |
|
|
243 |
!MatrixTransform2x3 methodsFor:'initialize'!
|
|
244 |
|
|
245 |
setIdentiy
|
|
246 |
"Initialize the receiver to the identity transformation (e.g., not affecting points)"
|
|
247 |
self
|
|
248 |
a11: 1.0; a12: 0.0; a13: 0.0;
|
3111
|
249 |
a21: 0.0; a22: 1.0; a23: 0.0.
|
|
250 |
! !
|
1250
|
251 |
|
|
252 |
!MatrixTransform2x3 methodsFor:'printing'!
|
|
253 |
|
|
254 |
printOn: aStream
|
|
255 |
aStream
|
|
256 |
nextPutAll: self class name;
|
|
257 |
nextPut: $(;
|
|
258 |
cr; print: self a11; tab; print: self a12; tab; print: self a13;
|
|
259 |
cr; print: self a21; tab; print: self a22; tab; print: self a23;
|
3111
|
260 |
cr; nextPut:$).
|
|
261 |
! !
|
1250
|
262 |
|
|
263 |
!MatrixTransform2x3 methodsFor:'private'!
|
|
264 |
|
|
265 |
setAngle: angle
|
3425
|
266 |
"Set the raw rotation angle (in degrees) in the receiver"
|
|
267 |
|
|
268 |
| rad s c |
|
|
269 |
rad := angle degreesToRadians.
|
|
270 |
s := rad sin.
|
|
271 |
c := rad cos.
|
|
272 |
self a11: c.
|
|
273 |
self a12: s negated.
|
|
274 |
self a21: s.
|
|
275 |
self a22: c.
|
3111
|
276 |
!
|
1250
|
277 |
|
|
278 |
setOffset: aPoint
|
3111
|
279 |
"Set the raw offset in the receiver"
|
|
280 |
| pt |
|
|
281 |
pt := aPoint asPoint.
|
|
282 |
self a13: pt x asFloat.
|
|
283 |
self a23: pt y asFloat.
|
|
284 |
!
|
1250
|
285 |
|
|
286 |
setScale: aPoint
|
3111
|
287 |
"Set the raw scale in the receiver"
|
|
288 |
| pt |
|
|
289 |
pt := aPoint asPoint.
|
|
290 |
self a11: pt x asFloat.
|
|
291 |
self a22: pt y asFloat.
|
|
292 |
! !
|
1250
|
293 |
|
|
294 |
!MatrixTransform2x3 methodsFor:'testing'!
|
|
295 |
|
|
296 |
isIdentity
|
1380
|
297 |
"Return true if the receiver is the identity transform; that is, if applying to a point returns the point itself."
|
|
298 |
"/ <primitive: 'm23PrimitiveIsIdentity'>
|
|
299 |
^self isPureTranslation and:[self a13 = 0.0 and:[self a23 = 0.0]]
|
|
300 |
!
|
1250
|
301 |
|
|
302 |
isMatrixTransform2x3
|
|
303 |
"Return true if the receiver is 2x3 matrix transformation"
|
3111
|
304 |
^true
|
|
305 |
!
|
1250
|
306 |
|
3425
|
307 |
isNoScale
|
|
308 |
"Return true if the receiver has an identity scale; i.e. the scale factor is 1.
|
|
309 |
Return false otherwise."
|
|
310 |
|
|
311 |
^ self a11 = 1 and:[ self a22 = 1 ]
|
|
312 |
!
|
|
313 |
|
1250
|
314 |
isPureTranslation
|
1380
|
315 |
"Return true if the receiver specifies no rotation or scaling."
|
|
316 |
"/ <primitive: 'm23PrimitiveIsPureTranslation'>
|
|
317 |
^self a11 = 1.0 and:[self a12 = 0.0 and:[self a22 = 0.0 and:[self a21 = 1.0]]]
|
|
318 |
! !
|
1250
|
319 |
|
|
320 |
!MatrixTransform2x3 methodsFor:'transforming points'!
|
|
321 |
|
|
322 |
globalPointToLocal: aPoint
|
1380
|
323 |
"Transform aPoint from global coordinates into local coordinates"
|
|
324 |
"/ <primitive: 'm23PrimitiveInvertPoint'>
|
|
325 |
^(self invertPoint: aPoint) rounded
|
|
326 |
!
|
1250
|
327 |
|
|
328 |
invertPoint: aPoint
|
|
329 |
"Transform aPoint from global coordinates into local coordinates"
|
|
330 |
| x y det a11 a12 a21 a22 detX detY |
|
|
331 |
x _ aPoint x asFloat - (self a13).
|
|
332 |
y _ aPoint y asFloat - (self a23).
|
|
333 |
a11 _ self a11. a12 _ self a12.
|
|
334 |
a21 _ self a21. a22 _ self a22.
|
|
335 |
det _ (a11 * a22) - (a12 * a21).
|
|
336 |
det = 0.0 ifTrue:[^0@0]. "So we have at least a valid result"
|
|
337 |
det _ 1.0 / det.
|
|
338 |
detX _ (x * a22) - (a12 * y).
|
|
339 |
detY _ (a11 * y) - (x * a21).
|
3111
|
340 |
^(detX * det) @ (detY * det)
|
|
341 |
!
|
1250
|
342 |
|
|
343 |
localPointToGlobal: aPoint
|
1380
|
344 |
"Transform aPoint from local coordinates into global coordinates"
|
|
345 |
"/ <primitive: 'm23PrimitiveTransformPoint'>
|
|
346 |
^(self transformPoint: aPoint) rounded
|
|
347 |
!
|
1250
|
348 |
|
3111
|
349 |
transformDirection: aPoint
|
|
350 |
"Transform aPoint from local coordinates into global coordinates;
|
|
351 |
Ignores the offset."
|
|
352 |
| x y |
|
|
353 |
x := (aPoint x * self a11) + (aPoint y * self a12).
|
|
354 |
y := (aPoint x * self a21) + (aPoint y * self a22).
|
|
355 |
^x @ y
|
|
356 |
!
|
|
357 |
|
1250
|
358 |
transformPoint: aPoint
|
3111
|
359 |
"Transform aPoint from local coordinates into global coordinates"
|
|
360 |
| x y |
|
|
361 |
x := (aPoint x * self a11) + (aPoint y * self a12) + self a13.
|
|
362 |
y := (aPoint x * self a21) + (aPoint y * self a22) + self a23.
|
|
363 |
^x @ y
|
|
364 |
! !
|
1250
|
365 |
|
|
366 |
!MatrixTransform2x3 methodsFor:'transforming rects'!
|
|
367 |
|
|
368 |
globalBounds: srcRect toLocal: dstRect
|
1380
|
369 |
"Transform aRectangle from global coordinates into local coordinates"
|
|
370 |
"/ <primitive:'m23PrimitiveInvertRectInto'>
|
|
371 |
^super globalBoundsToLocal: srcRect
|
|
372 |
!
|
1250
|
373 |
|
|
374 |
globalBoundsToLocal: aRectangle
|
|
375 |
"Transform aRectangle from global coordinates into local coordinates"
|
3111
|
376 |
^self globalBounds: aRectangle toLocal: Rectangle new
|
|
377 |
!
|
1250
|
378 |
|
|
379 |
localBounds: srcRect toGlobal: dstRect
|
1380
|
380 |
"Transform aRectangle from local coordinates into global coordinates"
|
|
381 |
"/ <primitive:'m23PrimitiveTransformRectInto'>
|
|
382 |
^super localBoundsToGlobal: srcRect
|
|
383 |
!
|
1250
|
384 |
|
|
385 |
localBoundsToGlobal: aRectangle
|
|
386 |
"Transform aRectangle from local coordinates into global coordinates"
|
3111
|
387 |
^self localBounds: aRectangle toGlobal: Rectangle new
|
|
388 |
! !
|
1250
|
389 |
|
|
390 |
!MatrixTransform2x3 class methodsFor:'documentation'!
|
|
391 |
|
|
392 |
version
|
3435
|
393 |
^ '$Header: /cvs/stx/stx/libview2/MatrixTransform2x3.st,v 1.6 2014-12-21 23:26:37 cg Exp $'
|
3426
|
394 |
!
|
|
395 |
|
|
396 |
version_CVS
|
3435
|
397 |
^ '$Header: /cvs/stx/stx/libview2/MatrixTransform2x3.st,v 1.6 2014-12-21 23:26:37 cg Exp $'
|
1250
|
398 |
! !
|
3111
|
399 |
|