Skip to content

Commit cb7cba8

Browse files
authored
Merge pull request #91 from larskanis/fxmat
Add FXMat[34][fd]#to_a , #== and #inspect
2 parents b1e37e8 + 11a13b8 commit cb7cba8

3 files changed

Lines changed: 119 additions & 41 deletions

File tree

‎lib/fox16/core.rb‎

Lines changed: 95 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -227,69 +227,138 @@ def writeBoolEntry(section, key, value)
227227
end
228228
end
229229

230-
class FXVec2d
231-
# Convert to array
232-
def to_a
233-
[x, y]
234-
end
235-
230+
module FXVec_to_s
236231
# Convert to string
237232
def to_s
238233
to_a.to_s
239234
end
240235

241236
def inspect; to_a.inspect; end
242237
end
238+
private_constant :FXVec_to_s
239+
240+
class FXVec2d
241+
include FXVec_to_s
242+
# Convert to array
243+
def to_a
244+
[x, y]
245+
end
246+
end
243247

244248
class FXVec2f
249+
include FXVec_to_s
245250
# Convert to array
246251
def to_a; [x, y]; end
247-
248-
# Convert to string
249-
def to_s; to_a.to_s; end
250-
251-
def inspect; to_a.inspect; end
252252
end
253253

254254
class FXVec3d
255+
include FXVec_to_s
255256
# Convert to array
256257
def to_a; [x, y, z]; end
257-
258-
# Convert to string
259-
def to_s; to_a.to_s; end
260-
261-
def inspect; to_a.inspect; end
262258
end
263259

264260
class FXVec3f
261+
include FXVec_to_s
265262
# Convert to array
266263
def to_a; [x, y, z]; end
267-
268-
# Convert to string
269-
def to_s; to_a.to_s; end
270-
271-
def inspect; to_a.inspect; end
272264
end
273265

274266
class FXVec4d
267+
include FXVec_to_s
275268
# Convert to array
276269
def to_a; [x, y, z, w]; end
270+
end
277271

278-
# Convert to string
279-
def to_s; to_a.to_s; end
272+
class FXVec4f
273+
include FXVec_to_s
274+
# Convert to array
275+
def to_a; [x, y, z, w]; end
276+
end
280277

281-
def inspect; to_a.inspect; end
278+
class FXVec2d
279+
include FXVec_to_s
280+
# Convert to array
281+
def to_a
282+
[x, y]
283+
end
284+
end
285+
286+
class FXVec2f
287+
include FXVec_to_s
288+
# Convert to array
289+
def to_a; [x, y]; end
290+
end
291+
292+
class FXVec3d
293+
include FXVec_to_s
294+
# Convert to array
295+
def to_a; [x, y, z]; end
296+
end
297+
298+
class FXVec3f
299+
include FXVec_to_s
300+
# Convert to array
301+
def to_a; [x, y, z]; end
302+
end
303+
304+
class FXVec4d
305+
include FXVec_to_s
306+
# Convert to array
307+
def to_a; [x, y, z, w]; end
282308
end
283309

284310
class FXVec4f
311+
include FXVec_to_s
285312
# Convert to array
286313
def to_a; [x, y, z, w]; end
314+
end
287315

288-
# Convert to string
289-
def to_s; to_a.to_s; end
316+
module Equality_to_a
317+
def ==(other)
318+
other.respond_to?(:to_a) &&
319+
to_a == other.to_a
320+
end
290321

291322
def inspect; to_a.inspect; end
292323
end
324+
private_constant :Equality_to_a
325+
326+
module FXMat3_to_a
327+
include Equality_to_a
328+
def to_a
329+
[self[0].to_a,
330+
self[1].to_a,
331+
self[2].to_a]
332+
end
333+
end
334+
private_constant :FXMat3_to_a
335+
336+
module FXMat4_to_a
337+
include Equality_to_a
338+
def to_a
339+
[self[0].to_a,
340+
self[1].to_a,
341+
self[2].to_a,
342+
self[3].to_a]
343+
end
344+
end
345+
private_constant :FXMat4_to_a
346+
347+
class FXMat3d
348+
include FXMat3_to_a
349+
end
350+
351+
class FXMat3f
352+
include FXMat3_to_a
353+
end
354+
355+
class FXMat4d
356+
include FXMat4_to_a
357+
end
358+
359+
class FXMat4f
360+
include FXMat4_to_a
361+
end
293362

294363
class FXWindow
295364
#

‎test/TC_FXBMPImage.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_fileExt
1515

1616
def test_image_from_pixel_data
1717
img = FXBMPImage.new app
18-
img.setPixels +"rgbaRGBA", 0, 1, 2
18+
img.setPixels(+"rgbaRGBA", 0, 1, 2)
1919
bmp_data = FXMemoryStream.open(FXStreamSave, nil) do |outfile|
2020
img.savePixels(outfile)
2121
outfile.takeBuffer

‎test/TC_FXMat4f.rb‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class TC_FXMat4f < Test::Unit::TestCase
55
include Fox
66

77
def setup
8-
@hmat = FXMat4f.new
8+
@hmat = FXMat4f.new 59650271608832.0, 1.52688322114599e+38, 5.170800853465662e-13, 2.0711191302720796e-42, 1.0, 2.2, 2.586273209481682e-13, 1.0355595651360398e-42, 0.0, 0.0, 0.0, 0.0, 0.0, 3.673819216381981e-39, 7.006492321624085e-45, 3.7991997507591117e-35
9+
@samples = [-5.412440155462294e+24, 9.504638999570352e+35, 7.158709246972819e+30, 1.0, 2.2, 3.3, 4.4, 1.0819492946742955e+37, 66.6, 777.7, -4.0472602345783137e+31, -33.3, 5.332417885281655e+37, -999.9, 4.4, -0.11]
910
end
1011

1112
def test_initialize
@@ -25,9 +26,9 @@ def test_from_elements
2526
a20, a21, a22, a23 = 0.0, 0.0, 0.0, 0.0
2627
a30, a31, a32, a33 = 0.0, 0.0, 0.0, 0.0
2728
h = FXMat4f.new(a00, a01, a02, a03,
28-
a10, a11, a12, a13,
29-
a20, a21, a22, a23,
30-
a30, a31, a32, a33)
29+
a10, a11, a12, a13,
30+
a20, a21, a22, a23,
31+
a30, a31, a32, a33)
3132
assert_instance_of(FXMat4f, h)
3233
end
3334

@@ -44,16 +45,16 @@ def test_copy
4445
anotherHMat = FXMat4f.new(@hmat)
4546
assert_instance_of(FXMat4f, anotherHMat)
4647
assert_not_same(@hmat, anotherHMat)
47-
# assert_equal(@hmat, anotherHMat)
48+
assert_equal(@hmat, anotherHMat)
4849
end
4950

5051
def test_add
51-
anotherMat = FXMat4f.new
52+
anotherMat = FXMat4f.new *@samples
5253
sum1 = anotherMat + @hmat
5354
sum2 = @hmat + anotherMat
5455
assert_instance_of(FXMat4f, sum1)
5556
assert_instance_of(FXMat4f, sum2)
56-
# assert_equal(sum1, sum2)
57+
assert_equal(sum1, sum2)
5758
end
5859

5960
def test_neg
@@ -62,31 +63,31 @@ def test_neg
6263
end
6364

6465
def test_sub
65-
anotherMat = FXMat4f.new
66+
anotherMat = FXMat4f.new *@samples
6667
diff1 = anotherMat - @hmat
6768
diff2 = @hmat - anotherMat
6869
assert_instance_of(FXMat4f, diff1)
6970
assert_instance_of(FXMat4f, diff2)
70-
# assert_equal(diff1, -diff2)
71+
assert_equal(diff1, -diff2)
7172
end
7273

7374
def test_mul_matrices
74-
a = FXMat4f.new
75-
b = FXMat4f.new
75+
a = FXMat4f.new *@samples
76+
b = FXMat4f.new *@samples
7677
product = a*b
7778
assert_instance_of(FXMat4f, product)
78-
# assert_equal(product, b*a)
79+
assert_equal(product, b*a)
7980
end
8081

8182
def test_mul_by_scalar
8283
p = FXMat4f.new.eye
8384
q = FXMat4f.new(4.0, 0.0, 0.0, 0.0,
8485
0.0, 4.0, 0.0, 0.0,
8586
0.0, 0.0, 4.0, 0.0,
86-
0.0, 0.0, 0.0, 4.0)
87+
0.0, 0.0, 0.0, 1.0)
8788
r = p*4.0
8889
assert_instance_of(FXMat4f, r)
89-
# assert_equal(q, r)
90+
assert_equal(q, r)
9091
end
9192

9293
def test_div
@@ -225,4 +226,12 @@ def test_scale_vec
225226
scaled = @hmat.scale(v)
226227
assert_same(@hmat, scaled)
227228
end
229+
230+
def test_to_a
231+
assert_equal [[59650271608832.0, 1.52688322114599e+38, 5.170800853465662e-13, 2.0711191302720796e-42], [1.0, 2.200000047683716, 2.586273209481682e-13, 1.0355595651360398e-42], [0.0, 0.0, 0.0, 0.0], [0.0, 3.673819216381981e-39, 7.006492321624085e-45, 3.7991997507591117e-35]], @hmat.to_a
232+
end
233+
234+
def test_inspect
235+
assert_equal "[[59650271608832.0, 1.52688322114599e+38, 5.170800853465662e-13, 2.0711191302720796e-42], [1.0, 2.200000047683716, 2.586273209481682e-13, 1.0355595651360398e-42], [0.0, 0.0, 0.0, 0.0], [0.0, 3.673819216381981e-39, 7.006492321624085e-45, 3.7991997507591117e-35]]", @hmat.inspect
236+
end
228237
end

0 commit comments

Comments
 (0)