rays 0.1.11 → 0.1.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.doc/ext/rays/bitmap.cpp +22 -76
- data/.doc/ext/rays/bounds.cpp +95 -125
- data/.doc/ext/rays/camera.cpp +88 -0
- data/.doc/ext/rays/color.cpp +223 -45
- data/.doc/ext/rays/color_space.cpp +146 -46
- data/.doc/ext/rays/defs.cpp +183 -0
- data/.doc/ext/rays/font.cpp +69 -21
- data/.doc/ext/rays/image.cpp +26 -37
- data/.doc/ext/rays/matrix.cpp +186 -29
- data/.doc/ext/rays/native.cpp +14 -8
- data/.doc/ext/rays/noise.cpp +53 -0
- data/.doc/ext/rays/painter.cpp +187 -292
- data/.doc/ext/rays/point.cpp +96 -77
- data/.doc/ext/rays/polygon.cpp +313 -0
- data/.doc/ext/rays/polygon_line.cpp +96 -0
- data/.doc/ext/rays/polyline.cpp +167 -0
- data/.doc/ext/rays/rays.cpp +103 -12
- data/.doc/ext/rays/shader.cpp +83 -9
- data/LICENSE +21 -0
- data/README.md +1 -1
- data/Rakefile +24 -9
- data/VERSION +1 -1
- data/ext/rays/bitmap.cpp +22 -80
- data/ext/rays/bounds.cpp +100 -128
- data/ext/rays/camera.cpp +94 -0
- data/ext/rays/color.cpp +231 -51
- data/ext/rays/color_space.cpp +149 -47
- data/ext/rays/defs.cpp +183 -0
- data/ext/rays/defs.h +26 -2
- data/ext/rays/extconf.rb +2 -3
- data/ext/rays/font.cpp +74 -24
- data/ext/rays/image.cpp +28 -40
- data/ext/rays/matrix.cpp +198 -30
- data/ext/rays/native.cpp +14 -8
- data/ext/rays/noise.cpp +55 -0
- data/ext/rays/painter.cpp +203 -298
- data/ext/rays/point.cpp +105 -81
- data/ext/rays/polygon.cpp +329 -0
- data/ext/rays/polygon_line.cpp +99 -0
- data/ext/rays/polyline.cpp +176 -0
- data/ext/rays/rays.cpp +103 -13
- data/ext/rays/shader.cpp +84 -9
- data/include/rays.h +10 -2
- data/include/rays/bitmap.h +14 -26
- data/include/rays/bounds.h +21 -4
- data/include/rays/camera.h +49 -0
- data/include/rays/color.h +25 -14
- data/include/rays/color_space.h +15 -10
- data/include/rays/coord.h +114 -0
- data/include/rays/debug.h +22 -0
- data/include/rays/defs.h +36 -0
- data/include/rays/exception.h +6 -2
- data/include/rays/font.h +4 -4
- data/include/rays/image.h +12 -18
- data/include/rays/matrix.h +50 -24
- data/include/rays/noise.h +42 -0
- data/include/rays/opengl.h +2 -50
- data/include/rays/painter.h +89 -93
- data/include/rays/point.h +44 -51
- data/include/rays/polygon.h +198 -0
- data/include/rays/polyline.h +71 -0
- data/include/rays/rays.h +3 -0
- data/include/rays/ruby.h +7 -1
- data/include/rays/ruby/bounds.h +1 -1
- data/include/rays/ruby/camera.h +41 -0
- data/include/rays/ruby/color.h +1 -1
- data/include/rays/ruby/color_space.h +1 -1
- data/include/rays/ruby/font.h +1 -1
- data/include/rays/ruby/matrix.h +1 -1
- data/include/rays/ruby/point.h +1 -1
- data/include/rays/ruby/polygon.h +52 -0
- data/include/rays/ruby/polyline.h +41 -0
- data/include/rays/ruby/rays.h +8 -0
- data/include/rays/ruby/shader.h +1 -1
- data/include/rays/shader.h +36 -8
- data/lib/rays.rb +7 -2
- data/lib/rays/bitmap.rb +0 -15
- data/lib/rays/bounds.rb +17 -23
- data/lib/rays/camera.rb +21 -0
- data/lib/rays/color.rb +20 -47
- data/lib/rays/color_space.rb +13 -13
- data/lib/rays/image.rb +3 -7
- data/lib/rays/matrix.rb +28 -0
- data/lib/rays/module.rb +4 -19
- data/lib/rays/painter.rb +78 -93
- data/lib/rays/point.rb +13 -21
- data/lib/rays/polygon.rb +58 -0
- data/lib/rays/polygon_line.rb +36 -0
- data/lib/rays/polyline.rb +32 -0
- data/lib/rays/shader.rb +20 -1
- data/rays.gemspec +5 -7
- data/src/bitmap.h +36 -0
- data/src/bounds.cpp +74 -11
- data/src/color.cpp +58 -23
- data/src/color_space.cpp +52 -34
- data/src/color_space.h +22 -0
- data/src/coord.cpp +170 -0
- data/src/coord.h +35 -0
- data/src/font.cpp +118 -0
- data/src/font.h +64 -0
- data/src/frame_buffer.cpp +37 -71
- data/src/frame_buffer.h +4 -4
- data/src/image.cpp +172 -98
- data/src/image.h +25 -0
- data/src/ios/bitmap.h +21 -0
- data/src/ios/bitmap.mm +129 -110
- data/src/ios/camera.mm +236 -0
- data/src/ios/font.mm +50 -62
- data/src/ios/helper.h +2 -2
- data/src/ios/opengl.mm +19 -4
- data/src/ios/rays.mm +3 -0
- data/src/matrix.cpp +111 -26
- data/src/matrix.h +30 -0
- data/src/noise.cpp +74 -0
- data/src/opengl.cpp +9 -27
- data/src/opengl.h +37 -0
- data/src/osx/bitmap.h +21 -0
- data/src/osx/bitmap.mm +129 -110
- data/src/osx/camera.mm +236 -0
- data/src/osx/font.mm +49 -62
- data/src/osx/helper.h +2 -2
- data/src/osx/opengl.mm +19 -83
- data/src/osx/rays.mm +3 -0
- data/src/painter.cpp +845 -671
- data/src/painter.h +24 -0
- data/src/point.cpp +140 -119
- data/src/polygon.cpp +1266 -0
- data/src/polygon.h +32 -0
- data/src/polyline.cpp +160 -0
- data/src/polyline.h +69 -0
- data/src/render_buffer.cpp +11 -4
- data/src/render_buffer.h +2 -2
- data/src/shader.cpp +163 -106
- data/src/shader.h +38 -0
- data/src/shader_program.cpp +533 -0
- data/src/{program.h → shader_program.h} +28 -16
- data/src/shader_source.cpp +140 -0
- data/src/shader_source.h +52 -0
- data/src/texture.cpp +136 -160
- data/src/texture.h +65 -0
- data/src/win32/bitmap.cpp +62 -52
- data/src/win32/font.cpp +11 -13
- data/src/win32/font.h +24 -0
- data/src/win32/gdi.h +6 -6
- data/test/helper.rb +0 -3
- data/test/test_bitmap.rb +31 -7
- data/test/test_bounds.rb +36 -0
- data/test/test_color.rb +59 -19
- data/test/test_color_space.rb +95 -0
- data/test/test_font.rb +5 -0
- data/test/test_image.rb +24 -20
- data/test/test_matrix.rb +106 -0
- data/test/test_painter.rb +157 -51
- data/test/test_painter_shape.rb +102 -0
- data/test/test_point.rb +29 -0
- data/test/test_polygon.rb +234 -0
- data/test/test_polygon_line.rb +167 -0
- data/test/test_polyline.rb +171 -0
- data/test/test_shader.rb +9 -9
- metadata +102 -70
- data/.doc/ext/rays/texture.cpp +0 -138
- data/ext/rays/texture.cpp +0 -149
- data/include/rays/ruby/texture.h +0 -41
- data/include/rays/texture.h +0 -71
- data/lib/rays/texture.rb +0 -24
- data/src/program.cpp +0 -648
- data/test/test_texture.rb +0 -27
@@ -0,0 +1,102 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
|
4
|
+
require_relative 'helper'
|
5
|
+
|
6
|
+
|
7
|
+
class TestPainterShape < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def color (*args)
|
10
|
+
Rays::Color.new *args
|
11
|
+
end
|
12
|
+
|
13
|
+
def image (fill = 1, stroke = 0, pixel_density = 1, &block)
|
14
|
+
Rays::Image.new(100, 100, Rays::RGBA, pixel_density).paint {|p|
|
15
|
+
p.fill fill > 0 ? color(fill) : nil
|
16
|
+
p.stroke stroke > 0 ? color(stroke) : nil
|
17
|
+
p.instance_eval &block if block
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_line ()
|
22
|
+
img = image(0, 1) {line 1, 1, 98, 98}
|
23
|
+
assert_equal 0, img[ 0, 0].a
|
24
|
+
assert_equal 1, img[ 1, 1].a
|
25
|
+
assert_equal 1, img[97, 97].a #img[98, 98].a
|
26
|
+
assert_equal 0, img[99, 99].a
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_curve ()
|
30
|
+
=begin
|
31
|
+
img = image(0, 1) {curve 1, 1, 98, 1, 98, 98, 1, 98}
|
32
|
+
assert_equal 0, img[ 0, 0].a
|
33
|
+
assert_equal 1, img[ 1, 1].a
|
34
|
+
assert_equal 0, img[98, 1].a
|
35
|
+
assert_equal 0, img[98, 50].a
|
36
|
+
assert_equal 0, img[98, 98].a
|
37
|
+
assert_equal 1, img[ 1, 97].a #img[ 1, 98].a
|
38
|
+
assert_equal 0, img[99, 99].a
|
39
|
+
=end
|
40
|
+
assert_raise(ArgumentError) {image {curve}}
|
41
|
+
assert_raise(ArgumentError) {image {curve 0}}
|
42
|
+
assert_raise(ArgumentError) {image {curve 0, 1}}
|
43
|
+
assert_raise(ArgumentError) {image {curve 0, 1, 2}}
|
44
|
+
assert_raise(ArgumentError) {image {curve 0, 1, 2, 3}}
|
45
|
+
assert_raise(ArgumentError) {image {curve 0, 1, 2, 3, 4}}
|
46
|
+
assert_raise(ArgumentError) {image {curve 0, 1, 2, 3, 4, 5}}
|
47
|
+
assert_raise(ArgumentError) {image {curve 0, 1, 2, 3, 4, 5, 6}}
|
48
|
+
assert_nothing_raised {image {curve 0, 1, 2, 3, 4, 5, 6, 7}}
|
49
|
+
assert_raise(ArgumentError) {image {curve 0, 1, 2, 3, 4, 5, 6, 7, 8}}
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_bezier ()
|
53
|
+
=begin
|
54
|
+
img = image(0, 1) {bezier 1, 1, 98, 1, 98, 98, 1, 98}
|
55
|
+
assert_equal 0, img[ 0, 0].a
|
56
|
+
assert_equal 1, img[ 1, 1].a
|
57
|
+
assert_equal 0, img[98, 1].a
|
58
|
+
assert_equal 0, img[98, 50].a
|
59
|
+
assert_equal 0, img[98, 98].a
|
60
|
+
assert_equal 1, img[ 1, 97].a #img[ 1, 98].a
|
61
|
+
assert_equal 0, img[99, 99].a
|
62
|
+
=end
|
63
|
+
assert_raise(ArgumentError) {image {bezier}}
|
64
|
+
assert_raise(ArgumentError) {image {bezier 0}}
|
65
|
+
assert_raise(ArgumentError) {image {bezier 0, 1}}
|
66
|
+
assert_raise(ArgumentError) {image {bezier 0, 1, 2}}
|
67
|
+
assert_raise(ArgumentError) {image {bezier 0, 1, 2, 3}}
|
68
|
+
assert_raise(ArgumentError) {image {bezier 0, 1, 2, 3, 4}}
|
69
|
+
assert_raise(ArgumentError) {image {bezier 0, 1, 2, 3, 4, 5}}
|
70
|
+
assert_raise(ArgumentError) {image {bezier 0, 1, 2, 3, 4, 5, 6}}
|
71
|
+
assert_nothing_raised {image {bezier 0, 1, 2, 3, 4, 5, 6, 7}}
|
72
|
+
assert_raise(ArgumentError) {image {bezier 0, 1, 2, 3, 4, 5, 6, 7, 8}}
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_rect ()
|
76
|
+
img = image {rect 1, 1, 98, 98}
|
77
|
+
assert_equal 0, img[ 0, 0].a
|
78
|
+
assert_equal 1, img[ 1, 1].a
|
79
|
+
assert_equal 1, img[98, 98].a
|
80
|
+
assert_equal 0, img[99, 99].a
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_rect_rounded ()
|
84
|
+
img = image {rect 1, 1, 98, 98, 10, 10, 10, 10}
|
85
|
+
assert_equal 0, img[ 0, 0].a
|
86
|
+
assert_equal 0, img[ 1, 1].a
|
87
|
+
assert_equal 1, img[10, 10].a
|
88
|
+
assert_equal 1, img[89, 89].a
|
89
|
+
assert_equal 0, img[98, 98].a
|
90
|
+
assert_equal 0, img[99, 99].a
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_ellipse ()
|
94
|
+
img = image {ellipse 1, 1, 98, 98}
|
95
|
+
assert_equal 0, img[ 0, 0].a
|
96
|
+
assert_equal 1, img[50, 1].a
|
97
|
+
assert_equal 1, img[50, 50].a
|
98
|
+
assert_equal 1, img[ 1, 50].a
|
99
|
+
assert_equal 0, img[99, 99].a
|
100
|
+
end
|
101
|
+
|
102
|
+
end# TestPainter
|
data/test/test_point.rb
CHANGED
@@ -18,6 +18,18 @@ class TestPoint < Test::Unit::TestCase
|
|
18
18
|
assert_raise(ArgumentError) {point(1, 2, 3, 4)}
|
19
19
|
end
|
20
20
|
|
21
|
+
def test_dup ()
|
22
|
+
o = point
|
23
|
+
assert_equal point(0, 0, 0), o
|
24
|
+
o.x = 1
|
25
|
+
assert_equal point(1, 0, 0), o
|
26
|
+
x = o.dup
|
27
|
+
assert_equal point(1, 0, 0), x
|
28
|
+
x.x = 2
|
29
|
+
assert_equal point(2, 0, 0), x
|
30
|
+
assert_equal point(1, 0, 0), o
|
31
|
+
end
|
32
|
+
|
21
33
|
def test_get_xyz ()
|
22
34
|
o = point 1, 2, 3
|
23
35
|
assert_equal 1, o.x
|
@@ -95,6 +107,7 @@ class TestPoint < Test::Unit::TestCase
|
|
95
107
|
|
96
108
|
def test_normal ()
|
97
109
|
assert_equal point(1, 2, 3).normalize, point(1, 2, 3).normal
|
110
|
+
assert_raise(Rucy::NativeError) {point(0).normal}
|
98
111
|
end
|
99
112
|
|
100
113
|
def test_to_a ()
|
@@ -143,6 +156,10 @@ class TestPoint < Test::Unit::TestCase
|
|
143
156
|
assert o > point(1, 2, 2)
|
144
157
|
end
|
145
158
|
|
159
|
+
def test_negate ()
|
160
|
+
assert_equal point(-1, 2, -3), -point(1, -2, 3)
|
161
|
+
end
|
162
|
+
|
146
163
|
def test_arithmetic_operations ()
|
147
164
|
assert_equal point(11, 22, 33), point(10, 20, 30) + point(1, 2, 3)
|
148
165
|
assert_equal point( 9, 18, 27), point(10, 20, 30) - point(1, 2, 3)
|
@@ -158,4 +175,16 @@ class TestPoint < Test::Unit::TestCase
|
|
158
175
|
assert_equal point( 5, 10, 15), point(10, 20, 30) / 2
|
159
176
|
end
|
160
177
|
|
178
|
+
def test_inspect ()
|
179
|
+
assert_equal "#<Rays::Point 1.0, 2.0, 3.0>", point(1, 2, 3).inspect
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_dot ()
|
183
|
+
assert_equal 1*4 + 2*5 + 3*6, Rays::Point::dot(point(1, 2, 3), point(4, 5, 6))
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_cross ()
|
187
|
+
assert_equal point(0, 0, 1), Rays::Point::cross(point(1, 0, 0), point(0, 1, 0))
|
188
|
+
end
|
189
|
+
|
161
190
|
end# TestPoint
|
@@ -0,0 +1,234 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
|
4
|
+
require_relative 'helper'
|
5
|
+
|
6
|
+
|
7
|
+
class TestPolygon < Test::Unit::TestCase
|
8
|
+
|
9
|
+
class Rays::Polygon
|
10
|
+
def dump ()
|
11
|
+
map {|l| l.map &:to_a}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def polygon (*args)
|
16
|
+
Rays::Polygon.new *args
|
17
|
+
end
|
18
|
+
|
19
|
+
def line (*args)
|
20
|
+
Rays::Polygon::Line.new *args
|
21
|
+
end
|
22
|
+
|
23
|
+
def polyline (*args)
|
24
|
+
Rays::Polyline.new *args
|
25
|
+
end
|
26
|
+
|
27
|
+
def point (*args)
|
28
|
+
Rays::Point.new *args
|
29
|
+
end
|
30
|
+
|
31
|
+
def bounds (*args)
|
32
|
+
Rays::Bounds.new *args
|
33
|
+
end
|
34
|
+
|
35
|
+
def rect (*args)
|
36
|
+
Rays::Polygon.rect *args
|
37
|
+
end
|
38
|
+
|
39
|
+
def assert_equal_polygon (poly1, poly2)
|
40
|
+
assert_equal *[poly1, poly2].map {|poly|
|
41
|
+
poly.dump.map {|l| l.map {|p| p.map &:round}}.map(&:sort).sort.flatten
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_initialize ()
|
46
|
+
assert_equal [[[1, 2], [3, 4]]], polygon( 1, 2, 3, 4, loop: false).dump
|
47
|
+
assert_equal [[[5, 6], [7, 8]]], polygon( [5, 6], [7, 8], loop: false).dump
|
48
|
+
assert_equal [[[1, 1], [2, 2]]], polygon( [1], [2], loop: false).dump
|
49
|
+
assert_equal [[[3, 3], [4, 4]]], polygon(point(3), point(4), loop: false).dump
|
50
|
+
assert_nothing_raised {polygon( loop: true)}
|
51
|
+
assert_nothing_raised {polygon( loop: false)}
|
52
|
+
assert_raise(ArgumentError) {polygon(1, loop: true)}
|
53
|
+
assert_raise(ArgumentError) {polygon(1, loop: false)}
|
54
|
+
assert_raise(ArgumentError) {polygon(1, 2, loop: true)}
|
55
|
+
assert_nothing_raised {polygon(1, 2, loop: false)}
|
56
|
+
assert_raise(ArgumentError) {polygon(1, 2, 3, loop: true)}
|
57
|
+
assert_raise(ArgumentError) {polygon(1, 2, 3, loop: false)}
|
58
|
+
assert_raise(ArgumentError) {polygon(1, 2, 3, 4, loop: true)}
|
59
|
+
assert_nothing_raised {polygon(1, 2, 3, 4, loop: false)}
|
60
|
+
assert_raise(ArgumentError) {polygon(1, 2, 3, 4, 5, loop: true)}
|
61
|
+
assert_raise(ArgumentError) {polygon(1, 2, 3, 4, 5, loop: false)}
|
62
|
+
assert_nothing_raised {polygon(1, 2, 3, 4, 5, 6, loop: true)}
|
63
|
+
assert_nothing_raised {polygon(1, 2, 3, 4, 5, 6, loop: false)}
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_expand ()
|
67
|
+
polygon([10,10], [20,10], [30,20], loop: false).expand(1).tap {|o|
|
68
|
+
assert_equal 1, o .size
|
69
|
+
assert_equal 6, o[0].size
|
70
|
+
}
|
71
|
+
polygon([10,10], [20,10], [20,20], [10,20], loop: true) .expand(1).tap {|o|
|
72
|
+
assert_equal 1, o .size
|
73
|
+
assert_equal 4, o[0].size
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_transform_with_matrix ()
|
78
|
+
m = Rays::Matrix.translate 10, 10
|
79
|
+
assert_equal_polygon rect(20, 20, 50, 50), rect(10, 10, 50, 50).transform(m)
|
80
|
+
|
81
|
+
m = Rays::Matrix.scale 2
|
82
|
+
assert_equal_polygon rect(20, 20, 40, 40), rect(10, 10, 20, 20).transform(m)
|
83
|
+
|
84
|
+
m = Rays::Matrix.rotate 90
|
85
|
+
assert_equal_polygon rect(-10, 0, 10, 10), rect(0, 0, 10, 10).transform(m)
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_transform_block ()
|
89
|
+
o = rect(0, 0, 100, 100) - rect(10, 10, 50, 50)
|
90
|
+
assert_equal 2, o.size
|
91
|
+
|
92
|
+
o.transform {|lines|
|
93
|
+
lines.map {|l| l.transform Rays::Matrix.translate 10, 10}
|
94
|
+
}.tap {|x|
|
95
|
+
assert_equal_polygon (rect(10, 10, 100, 100) - rect(20, 20, 50, 50)), x
|
96
|
+
}
|
97
|
+
|
98
|
+
o.transform {|lines|
|
99
|
+
lines.reject {|l| l.to_a.include? point(10, 10)}
|
100
|
+
}.tap {|x|
|
101
|
+
assert_equal 1, x.size
|
102
|
+
assert_equal 2, o.size
|
103
|
+
}
|
104
|
+
|
105
|
+
o.transform {|lines|
|
106
|
+
lines.reject {|l| l.to_a.include? point(10, 10)}
|
107
|
+
}.tap {|x|
|
108
|
+
assert_equal 1, x.size
|
109
|
+
assert_equal 2, o.size
|
110
|
+
}
|
111
|
+
|
112
|
+
o.transform {|lines|
|
113
|
+
lines + [line(1, 2, 3, 4, 5, 6)]
|
114
|
+
}.tap {|x|
|
115
|
+
assert_equal 3, x.size
|
116
|
+
assert_equal 2, o.size
|
117
|
+
}
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_intersects ()
|
121
|
+
assert rect(10, 10, 20, 20).intersects(rect 20, 20, 20, 20)
|
122
|
+
assert_not rect(10, 10, 20, 20).intersects(rect 40, 40, 20, 20)
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_bounds ()
|
126
|
+
assert_equal bounds(10, 20, 0, 20, 10, 0), polygon(10, 20, 30, 20, 20, 30).bounds
|
127
|
+
|
128
|
+
assert polygon(10, 20, 30, 20, 20, 30).bounds.valid?
|
129
|
+
assert_not polygon() .bounds.valid?
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_loop ()
|
133
|
+
assert_equal true, polygon(1, 2, 3, 4, 5, 6 ).first.loop?
|
134
|
+
assert_equal true, polygon(1, 2, 3, 4, 5, 6, loop: true ).first.loop?
|
135
|
+
assert_equal false, polygon(1, 2, 3, 4, 5, 6, loop: false).first.loop?
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_size ()
|
139
|
+
assert_equal 0, polygon( ).size
|
140
|
+
assert_equal 1, polygon(1, 2, 3, 4, loop: false).size
|
141
|
+
assert_equal 1, polygon(1, 2, 3, 4, 5, 6, loop: false).size
|
142
|
+
assert_equal 2, (rect(0, 0, 5, 5) | rect(10, 0, 5, 5)).size
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_empty? ()
|
146
|
+
assert_equal true, polygon( ).empty?
|
147
|
+
assert_equal false, polygon(1, 2, 3, 4, 5, 6).empty?
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_index ()
|
151
|
+
o1 = rect 0, 0, 10, 10
|
152
|
+
o2 = rect 20, 0, 10, 10
|
153
|
+
o3 = rect 40, 0, 10, 10
|
154
|
+
o = o1 | o2 | o3
|
155
|
+
assert_not_nil o[ 0]
|
156
|
+
assert_not_nil o[ 1]
|
157
|
+
assert_not_nil o[-1]
|
158
|
+
assert_raise(IndexError) {o[ 3]}
|
159
|
+
assert_raise(IndexError) {o[-4]}
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_sub ()
|
163
|
+
rect10 = rect 0, 0, 10, 10
|
164
|
+
|
165
|
+
o = rect10 - rect(5, 0, 10, 10)
|
166
|
+
assert_equal_polygon rect(0, 0, 5, 10), o
|
167
|
+
|
168
|
+
o = rect10 - rect(5, 0, 10, 10) - rect(0, 0, 2, 10)
|
169
|
+
assert_equal_polygon rect(2, 0, 3, 10), o
|
170
|
+
|
171
|
+
o = rect10 - [rect(5, 0, 10, 10), rect(0, 0, 3, 10)]
|
172
|
+
assert_equal_polygon rect(3, 0, 2, 10), o
|
173
|
+
|
174
|
+
assert_equal_polygon rect10, rect10 - polygon()
|
175
|
+
assert_equal_polygon polygon(), polygon() - rect10
|
176
|
+
assert_equal_polygon rect10, rect10 - []
|
177
|
+
assert_equal_polygon polygon(), rect10 - rect10
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_and ()
|
181
|
+
rect10 = rect 0, 0, 10, 10
|
182
|
+
|
183
|
+
o = rect10 & rect(5, 0, 10, 10)
|
184
|
+
assert_equal_polygon rect(5, 0, 5, 10), o
|
185
|
+
|
186
|
+
o = rect10 & rect(5, 0, 10, 10) & rect(0, 5, 10, 10)
|
187
|
+
assert_equal_polygon rect(5, 5, 5, 5), o
|
188
|
+
|
189
|
+
o = rect10 & [rect(5, 0, 10, 10), rect(0, 6, 10, 10)]
|
190
|
+
assert_equal_polygon rect(5, 6, 5, 4), o
|
191
|
+
|
192
|
+
assert_equal_polygon polygon(), rect10 & polygon()
|
193
|
+
assert_equal_polygon polygon(), polygon() & rect10
|
194
|
+
assert_equal_polygon rect10, rect10 & []
|
195
|
+
assert_equal_polygon rect10, rect10 & rect10
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_or ()
|
199
|
+
rect10 = rect 0, 0, 10, 10
|
200
|
+
|
201
|
+
o = rect10 | rect(5, 0, 10, 10)
|
202
|
+
assert_equal_polygon rect(0, 0, 15, 10), o
|
203
|
+
|
204
|
+
o = rect10 | rect(5, 0, 10, 10) | rect(-5, 0, 10, 10)
|
205
|
+
assert_equal_polygon rect(-5, 0, 20, 10), o
|
206
|
+
|
207
|
+
o = rect10 | [rect(5, 0, 10, 10), rect(-6, 0, 10, 10)]
|
208
|
+
assert_equal_polygon rect(-6, 0, 21, 10), o
|
209
|
+
|
210
|
+
assert_equal_polygon rect10, rect10 | polygon()
|
211
|
+
assert_equal_polygon rect10, polygon() | rect10
|
212
|
+
assert_equal_polygon rect10, rect10 | []
|
213
|
+
assert_equal_polygon rect10, rect10 | rect10
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_xor ()
|
217
|
+
rect10 = rect 0, 0, 10, 10
|
218
|
+
|
219
|
+
o = rect10 ^ rect(5, 0, 10, 10)
|
220
|
+
assert_equal_polygon rect(0, 0, 5, 10) | rect(10, 0, 5, 10), o
|
221
|
+
|
222
|
+
o = rect10 ^ rect(5, 0, 10, 10) ^ rect(0, 0, 15, 5)
|
223
|
+
assert_equal_polygon rect(0, 5, 5, 5) | rect(5, 0, 5, 5) | rect(10, 5, 5, 5), o
|
224
|
+
|
225
|
+
o = rect10 ^ [rect(5, 0, 10, 10), rect(0, 0, 15, 6)]
|
226
|
+
assert_equal_polygon rect(0, 6, 5, 4) | rect(5, 0, 5, 6) | rect(10, 6, 5, 4), o
|
227
|
+
|
228
|
+
assert_equal_polygon rect10, rect10 ^ polygon()
|
229
|
+
assert_equal_polygon rect10, polygon() ^ rect10
|
230
|
+
assert_equal_polygon rect10, rect10 ^ []
|
231
|
+
assert_equal_polygon polygon(), rect10 ^ rect10
|
232
|
+
end
|
233
|
+
|
234
|
+
end# TestPolygon
|
@@ -0,0 +1,167 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
|
4
|
+
require_relative 'helper'
|
5
|
+
|
6
|
+
|
7
|
+
class TestPolygonLine < Test::Unit::TestCase
|
8
|
+
|
9
|
+
class Rays::Polygon::Line
|
10
|
+
def dump ()
|
11
|
+
map &:to_a
|
12
|
+
end
|
13
|
+
|
14
|
+
def loop_hole ()
|
15
|
+
[loop?, hole?]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def line (*args)
|
20
|
+
Rays::Polygon::Line.new *args
|
21
|
+
end
|
22
|
+
|
23
|
+
def point (*args)
|
24
|
+
Rays::Point.new *args
|
25
|
+
end
|
26
|
+
|
27
|
+
def rect (*args)
|
28
|
+
Rays::Polygon.rect *args
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_initialize ()
|
32
|
+
assert_equal [[1, 2], [3, 4], [5, 6]], line( 1, 2, 3, 4 , 5, 6 ).dump
|
33
|
+
assert_equal [[1, 2], [3, 4], [5, 6]], line( [1, 2], [3, 4], [5, 6]).dump
|
34
|
+
assert_equal [[1, 1], [2, 2], [3, 3]], line( [1], [2], [3]).dump
|
35
|
+
assert_equal [[1, 1], [2, 2], [3, 3]], line(point(1), point(2), point(3)).dump
|
36
|
+
|
37
|
+
assert_equal [true, false], line(1, 2, 3, 4, 5, 6 ).loop_hole
|
38
|
+
assert_equal [true, false], line(1, 2, 3, 4, 5, 6, loop: true ).loop_hole
|
39
|
+
assert_equal [false, false], line(1, 2, 3, 4, 5, 6, loop: false ).loop_hole
|
40
|
+
assert_equal [true, true ], line(1, 2, 3, 4, 5, 6, hole: true ).loop_hole
|
41
|
+
assert_equal [true, false], line(1, 2, 3, 4, 5, 6, hole: false).loop_hole
|
42
|
+
assert_equal [true, true ], line(1, 2, 3, 4, 5, 6, loop: true, hole: true ).loop_hole
|
43
|
+
assert_equal [true, false], line(1, 2, 3, 4, 5, 6, loop: true, hole: false).loop_hole
|
44
|
+
assert_raise(ArgumentError) {line(1, 2, 3, 4, 5, 6, loop: false, hole: true ).loop_hole}
|
45
|
+
assert_equal [false, false], line(1, 2, 3, 4, 5, 6, loop: false, hole: false).loop_hole
|
46
|
+
|
47
|
+
assert_raise(ArgumentError) {line( loop: true, hole: true )}
|
48
|
+
assert_nothing_raised {line( loop: true, hole: false)}
|
49
|
+
assert_raise(ArgumentError) {line( loop: false, hole: true )}
|
50
|
+
assert_nothing_raised {line( loop: false, hole: false)}
|
51
|
+
assert_raise(ArgumentError) {line(1, loop: true, hole: true )}
|
52
|
+
assert_raise(ArgumentError) {line(1, loop: true, hole: false)}
|
53
|
+
assert_raise(ArgumentError) {line(1, loop: false, hole: true )}
|
54
|
+
assert_raise(ArgumentError) {line(1, loop: false, hole: false)}
|
55
|
+
assert_raise(ArgumentError) {line(1, 2, loop: true, hole: true )}
|
56
|
+
assert_raise(ArgumentError) {line(1, 2, loop: true, hole: false)}
|
57
|
+
assert_raise(ArgumentError) {line(1, 2, loop: false, hole: true )}
|
58
|
+
assert_nothing_raised {line(1, 2, loop: false, hole: false)}
|
59
|
+
assert_raise(ArgumentError) {line(1, 2, 3, loop: true, hole: true )}
|
60
|
+
assert_raise(ArgumentError) {line(1, 2, 3, loop: true, hole: false)}
|
61
|
+
assert_raise(ArgumentError) {line(1, 2, 3, loop: false, hole: true )}
|
62
|
+
assert_raise(ArgumentError) {line(1, 2, 3, loop: false, hole: false)}
|
63
|
+
assert_raise(ArgumentError) {line(1, 2, 3, 4, loop: true, hole: true )}
|
64
|
+
assert_raise(ArgumentError) {line(1, 2, 3, 4, loop: true, hole: false)}
|
65
|
+
assert_raise(ArgumentError) {line(1, 2, 3, 4, loop: false, hole: true )}
|
66
|
+
assert_nothing_raised {line(1, 2, 3, 4, loop: false, hole: false)}
|
67
|
+
assert_raise(ArgumentError) {line(1, 2, 3, 4, 5, loop: true, hole: true )}
|
68
|
+
assert_raise(ArgumentError) {line(1, 2, 3, 4, 5, loop: true, hole: false)}
|
69
|
+
assert_raise(ArgumentError) {line(1, 2, 3, 4, 5, loop: false, hole: true )}
|
70
|
+
assert_raise(ArgumentError) {line(1, 2, 3, 4, 5, loop: false, hole: false)}
|
71
|
+
assert_nothing_raised {line(1, 2, 3, 4, 5, 6, loop: true, hole: true )}
|
72
|
+
assert_nothing_raised {line(1, 2, 3, 4, 5, 6, loop: true, hole: false)}
|
73
|
+
assert_raise(ArgumentError) {line(1, 2, 3, 4, 5, 6, loop: false, hole: true )}
|
74
|
+
assert_nothing_raised {line(1, 2, 3, 4, 5, 6, loop: false, hole: false)}
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_transform_with_materix ()
|
78
|
+
m = Rays::Matrix.translate 100, 200
|
79
|
+
line([10,10], [20,20], loop: false).transform(m).tap {|o|
|
80
|
+
assert_equal [[110,210], [120,220]], o.dump
|
81
|
+
}
|
82
|
+
|
83
|
+
m = Rays::Matrix.scale 2
|
84
|
+
line([10,10], [20,20], loop: false).transform(m).tap {|o|
|
85
|
+
assert_equal [[20,20], [40,40]], o.dump
|
86
|
+
}
|
87
|
+
|
88
|
+
m = Rays::Matrix.rotate 90
|
89
|
+
line([10,10], [20,20], loop: false).transform(m).tap {|o|
|
90
|
+
assert_equal [[-10,10], [-20,20]], o.dump
|
91
|
+
}
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_transform_with_block ()
|
95
|
+
line([10,10], [20,20], loop: false).transform {|points|
|
96
|
+
points.map {|p| p + [10, 20]}
|
97
|
+
}.tap {|o|
|
98
|
+
assert_equal [[20,30], [30,40]], o.dump
|
99
|
+
}
|
100
|
+
|
101
|
+
line([10,10], [20,20], [30,30], loop: false).transform {|points|
|
102
|
+
points.reject {|p| p == point(20, 20)}
|
103
|
+
}.tap {|o|
|
104
|
+
assert_equal [[10,10], [30,30]], o.dump
|
105
|
+
}
|
106
|
+
|
107
|
+
line([10,10], [20,20], loop: false).transform {|points|
|
108
|
+
points + [[30, 30]]
|
109
|
+
}.tap {|o|
|
110
|
+
assert_equal [[10,10], [20,20], [30,30]], o.dump
|
111
|
+
}
|
112
|
+
|
113
|
+
points = [1, 2, 3, 4, 5, 6]
|
114
|
+
line(*points, loop: false, hole: false).transform(loop: true ).tap {|o|
|
115
|
+
assert_equal points, o.dump.flatten
|
116
|
+
assert_equal [true, false], o.loop_hole
|
117
|
+
}
|
118
|
+
assert_raise(ArgumentError) {
|
119
|
+
line(*points, loop: false, hole: false).transform( hole: true )
|
120
|
+
}
|
121
|
+
assert_raise(ArgumentError) {
|
122
|
+
line(*points, loop: false, hole: false).transform(loop: false, hole: true )
|
123
|
+
}
|
124
|
+
line(*points, loop: false, hole: false).transform(loop: true, hole: true ).tap {|o|
|
125
|
+
assert_equal points, o.dump.flatten
|
126
|
+
assert_equal [true, true ], o.loop_hole
|
127
|
+
}
|
128
|
+
line(*points, loop: true, hole: false).transform(loop: false ).tap {|o|
|
129
|
+
assert_equal points, o.dump.flatten
|
130
|
+
assert_equal [false, false], o.loop_hole
|
131
|
+
}
|
132
|
+
line(*points, loop: true, hole: false).transform( hole: true ).tap {|o|
|
133
|
+
assert_equal points, o.dump.flatten
|
134
|
+
assert_equal [true, true ], o.loop_hole
|
135
|
+
}
|
136
|
+
assert_raise(ArgumentError) {
|
137
|
+
line(*points, loop: true, hole: false).transform(loop: false, hole: true )
|
138
|
+
}
|
139
|
+
assert_raise(ArgumentError) {
|
140
|
+
line(*points, loop: true, hole: true ).transform(loop: false )
|
141
|
+
}
|
142
|
+
line(*points, loop: true, hole: true ).transform( hole: false).tap {|o|
|
143
|
+
assert_equal points, o.dump.flatten
|
144
|
+
assert_equal [true, false], o.loop_hole
|
145
|
+
}
|
146
|
+
line(*points, loop: true, hole: true ).transform(loop: false, hole: false).tap {|o|
|
147
|
+
assert_equal points, o.dump.flatten
|
148
|
+
assert_equal [false, false], o.loop_hole
|
149
|
+
}
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_hole ()
|
153
|
+
(rect(0, 0, 10, 10) - rect(1, 1, 2, 2)).tap {|o|
|
154
|
+
assert_equal 1, o.select {|line| line.hole?}.size
|
155
|
+
}
|
156
|
+
(rect(0, 0, 10, 10) - rect(100, 1, 2, 2)).tap {|o|
|
157
|
+
assert_equal 0, o.select {|line| line.hole?}.size
|
158
|
+
}
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_inspect ()
|
162
|
+
assert_equal(
|
163
|
+
"#<Rays::Polygon::Line [1.0, 2.0], [3.0, 4.0], [5.0, 6.0], loop: true, hole: false>",
|
164
|
+
line(1, 2, 3, 4, 5, 6).inspect)
|
165
|
+
end
|
166
|
+
|
167
|
+
end# TestPolygonLine
|