rays 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. checksums.yaml +7 -0
  2. data/.doc/ext/rays/bitmap.cpp +70 -233
  3. data/.doc/ext/rays/bounds.cpp +339 -57
  4. data/.doc/ext/rays/color.cpp +58 -48
  5. data/.doc/ext/rays/color_space.cpp +174 -0
  6. data/.doc/ext/rays/font.cpp +31 -53
  7. data/.doc/ext/rays/image.cpp +64 -67
  8. data/.doc/ext/rays/matrix.cpp +22 -50
  9. data/.doc/ext/rays/native.cpp +9 -2
  10. data/.doc/ext/rays/painter.cpp +276 -259
  11. data/.doc/ext/rays/point.cpp +186 -52
  12. data/.doc/ext/rays/rays.cpp +25 -20
  13. data/.doc/ext/rays/shader.cpp +61 -0
  14. data/.doc/ext/rays/texture.cpp +47 -59
  15. data/{README → README.md} +0 -0
  16. data/Rakefile +6 -5
  17. data/VERSION +1 -1
  18. data/ext/rays/bitmap.cpp +88 -248
  19. data/ext/rays/bounds.cpp +437 -141
  20. data/ext/rays/color.cpp +79 -69
  21. data/ext/rays/color_space.cpp +185 -0
  22. data/ext/rays/extconf.rb +14 -63
  23. data/ext/rays/font.cpp +44 -65
  24. data/ext/rays/image.cpp +82 -81
  25. data/ext/rays/matrix.cpp +32 -60
  26. data/ext/rays/native.cpp +9 -2
  27. data/ext/rays/painter.cpp +345 -321
  28. data/ext/rays/point.cpp +212 -69
  29. data/ext/rays/rays.cpp +29 -23
  30. data/ext/rays/shader.cpp +63 -0
  31. data/ext/rays/texture.cpp +64 -74
  32. data/include/rays/bitmap.h +21 -12
  33. data/include/rays/bounds.h +67 -9
  34. data/include/rays/color.h +23 -7
  35. data/include/rays/{colorspace.h → color_space.h} +6 -3
  36. data/include/rays/exception.h +17 -11
  37. data/include/rays/font.h +4 -3
  38. data/include/rays/image.h +11 -6
  39. data/include/rays/matrix.h +15 -12
  40. data/include/rays/opengl.h +54 -1
  41. data/include/rays/painter.h +98 -108
  42. data/include/rays/point.h +45 -5
  43. data/include/rays/rays.h +2 -2
  44. data/include/rays/ruby/bitmap.h +2 -16
  45. data/include/rays/ruby/bounds.h +4 -16
  46. data/include/rays/ruby/color.h +3 -16
  47. data/include/rays/ruby/color_space.h +27 -0
  48. data/include/rays/ruby/font.h +2 -16
  49. data/include/rays/ruby/image.h +2 -16
  50. data/include/rays/ruby/matrix.h +2 -16
  51. data/include/rays/ruby/painter.h +2 -16
  52. data/include/rays/ruby/point.h +3 -16
  53. data/include/rays/ruby/shader.h +27 -0
  54. data/include/rays/ruby/texture.h +2 -16
  55. data/include/rays/ruby.h +1 -0
  56. data/include/rays/shader.h +48 -0
  57. data/include/rays/texture.h +13 -2
  58. data/include/rays.h +2 -1
  59. data/lib/rays/bitmap.rb +20 -11
  60. data/lib/rays/bounds.rb +29 -68
  61. data/lib/rays/color.rb +39 -0
  62. data/lib/rays/color_space.rb +33 -0
  63. data/lib/rays/font.rb +29 -0
  64. data/lib/rays/image.rb +22 -0
  65. data/lib/rays/module.rb +11 -7
  66. data/lib/rays/painter.rb +103 -40
  67. data/lib/rays/point.rb +19 -36
  68. data/lib/rays/shader.rb +13 -0
  69. data/lib/rays/texture.rb +9 -0
  70. data/lib/rays.rb +4 -0
  71. data/rays.gemspec +3 -4
  72. data/src/bounds.cpp +272 -63
  73. data/src/color.cpp +168 -21
  74. data/src/{colorspace.cpp → color_space.cpp} +38 -1
  75. data/src/exception.cpp +24 -15
  76. data/src/frame_buffer.cpp +275 -0
  77. data/src/frame_buffer.h +79 -0
  78. data/src/image.cpp +80 -36
  79. data/src/ios/bitmap.mm +340 -0
  80. data/src/ios/font.mm +206 -0
  81. data/src/{cocoa → ios}/helper.h +2 -2
  82. data/src/{cocoa → ios}/helper.mm +0 -0
  83. data/src/ios/opengl.mm +21 -0
  84. data/src/ios/program.cpp +122 -0
  85. data/src/{cocoa → ios}/rays.mm +8 -7
  86. data/src/matrix.cpp +10 -22
  87. data/src/opengl.cpp +64 -0
  88. data/src/{cocoa → osx}/bitmap.mm +121 -70
  89. data/src/{cocoa → osx}/font.mm +32 -24
  90. data/src/osx/helper.h +26 -0
  91. data/src/osx/helper.mm +25 -0
  92. data/src/osx/opengl.mm +103 -0
  93. data/src/osx/rays.mm +43 -0
  94. data/src/painter.cpp +596 -422
  95. data/src/point.cpp +154 -11
  96. data/src/program.cpp +513 -0
  97. data/src/program.h +73 -0
  98. data/src/render_buffer.cpp +120 -0
  99. data/src/render_buffer.h +47 -0
  100. data/src/shader.cpp +117 -0
  101. data/src/texture.cpp +104 -134
  102. data/test/helper.rb +10 -3
  103. data/test/test_bitmap.rb +18 -0
  104. data/test/test_bounds.rb +81 -35
  105. data/test/test_color.rb +29 -2
  106. data/test/test_image.rb +63 -0
  107. data/test/test_painter.rb +120 -0
  108. data/test/test_point.rb +30 -9
  109. data/test/test_shader.rb +37 -0
  110. data/test/test_texture.rb +18 -0
  111. metadata +75 -58
  112. data/.gitignore +0 -14
  113. data/ChangeLog +0 -8
data/src/texture.cpp CHANGED
@@ -1,7 +1,10 @@
1
1
  #include "rays/texture.h"
2
2
 
3
3
 
4
- #include <rays/bitmap.h>
4
+ #include "rays/exception.h"
5
+ #include "rays/bitmap.h"
6
+ #include "rays/opengl.h"
7
+ #include "frame_buffer.h"
5
8
 
6
9
 
7
10
  namespace Rays
@@ -11,13 +14,16 @@ namespace Rays
11
14
  struct Texture::Data
12
15
  {
13
16
 
14
- Bitmap bitmap;
17
+ int id, width, height, width_pow2, height_pow2;
15
18
 
16
- int id, width, height;
19
+ ColorSpace color_space;
20
+
21
+ bool alpha_only, dirty;
17
22
 
18
23
  Data ()
19
- : id(-1), width(0), height(0)
24
+ : id(-1)
20
25
  {
26
+ clear();
21
27
  }
22
28
 
23
29
  ~Data ()
@@ -33,58 +39,40 @@ namespace Rays
33
39
  glDeleteTextures(1, &id_);
34
40
  }
35
41
 
36
- bitmap = Bitmap();
37
42
  id = -1;
38
- width = height = 0;
43
+ width = height = width_pow2 = height_pow2 = 0;
44
+ color_space = COLORSPACE_UNKNOWN;
45
+ alpha_only = dirty = false;
39
46
  }
40
47
 
41
48
  };// Texture::Data
42
49
 
43
50
 
44
- static bool
45
- get_component_type (GLenum* result, ColorSpace cs)
51
+ static void
52
+ create_texture (
53
+ Texture::Data* self, const Bitmap* bitmap, GLenum format, GLenum type)
46
54
  {
47
- if (!result || !cs) return false;
48
-
49
- if (cs.is_float())
50
- *result = GL_FLOAT;
51
- else
52
- {
53
- switch (cs.bpc())
54
- {
55
- case 8: *result = GL_UNSIGNED_BYTE; break;
56
- case 16: *result = GL_UNSIGNED_SHORT; break;
57
- case 32: *result = GL_UNSIGNED_INT; break;
58
- default: return false;
59
- }
60
- }
61
- return true;
62
- }
63
-
64
- static bool
65
- create_texture (Texture::Data* self, GLenum format)
66
- {
67
- if (!self || !self->bitmap) return false;
68
-
69
- GLenum type;
70
- if (!get_component_type(&type, self->bitmap.color_space()))
71
- return false;
55
+ if (!self || (bitmap && !*bitmap))
56
+ argument_error(__FILE__, __LINE__);
72
57
 
73
58
  GLuint id = 0;
74
59
  glGenTextures(1, &id);
75
60
  glBindTexture(GL_TEXTURE_2D, id);
76
61
  if (glIsTexture(id) == GL_FALSE)
77
- return false;
62
+ opengl_error(__FILE__, __LINE__, "failed to create texture.");
78
63
 
79
64
  self->id = id;
80
65
 
81
- glTexImage2D(
82
- GL_TEXTURE_2D, 0, format, self->bitmap.width(), self->bitmap.height(), 0,
83
- format, type, self->bitmap.data());
66
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
67
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
84
68
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);
85
69
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);
86
70
 
87
- return true;
71
+ glTexImage2D(
72
+ GL_TEXTURE_2D, 0, format, self->width_pow2, self->height_pow2, 0,
73
+ format, type, bitmap ? bitmap->pixels() : NULL);
74
+
75
+ check_error(__FILE__, __LINE__);
88
76
  }
89
77
 
90
78
  static int
@@ -95,10 +83,11 @@ namespace Rays
95
83
  return n;
96
84
  }
97
85
 
98
- static bool
86
+ static void
99
87
  colorspace_for_alphabitmap (ColorSpace* result, ColorSpace cs)
100
88
  {
101
- if (!result || !cs) return false;
89
+ if (!result || !cs)
90
+ argument_error(__FILE__, __LINE__);
102
91
 
103
92
  *result = COLORSPACE_UNKNOWN;
104
93
 
@@ -112,11 +101,9 @@ namespace Rays
112
101
  case 16: *result = GRAY_16; break;
113
102
  case 24: *result = GRAY_24; break;
114
103
  case 32: *result = GRAY_32; break;
115
- default: return false;
104
+ default: rays_error(__FILE__, __LINE__, "invalid bpc.");
116
105
  }
117
106
  }
118
-
119
- return true;
120
107
  }
121
108
 
122
109
  template <int BytesPerPixel>
@@ -146,8 +133,8 @@ namespace Rays
146
133
  inline void
147
134
  copy_pixel<4> (uchar* dest, const uchar* src)
148
135
  {
149
- assert(sizeof(ulong) == 4);
150
- *(ulong*) dest = *(ulong*) src;
136
+ assert(sizeof(uint) == 4);
137
+ *(uint*) dest = *(uint*) src;
151
138
  }
152
139
 
153
140
  template <int BytesPerPixel>
@@ -157,7 +144,7 @@ namespace Rays
157
144
  uchar* dest, size_t dest_offset, const uchar* src, size_t src_offset)
158
145
  {
159
146
  if (!dest || !src || dest_offset <= 0 || src_offset <= 0)
160
- return;
147
+ argument_error(__FILE__, __LINE__);
161
148
 
162
149
  while (width--)
163
150
  {
@@ -181,18 +168,18 @@ namespace Rays
181
168
  }
182
169
  }
183
170
 
184
- static bool
171
+ static void
185
172
  copy_bitmap (Bitmap* dest, const Bitmap& src, int src_offset = 0)
186
173
  {
187
174
  if (!dest || !src || src_offset < 0)
188
- return false;
175
+ argument_error(__FILE__, __LINE__);
189
176
 
190
177
  int src_Bpp = src.color_space().Bpp();
191
178
  int src_Bpc = src.color_space().Bpc();
192
179
  int dest_Bpp = dest->color_space().Bpp();
193
180
 
194
181
  if (src_offset >= (src_Bpp / src_Bpc))
195
- return false;
182
+ rays_error(__FILE__, __LINE__, "invalid src_offset.");
196
183
 
197
184
  int width = std::min(src.width(), dest->width());
198
185
  int height = std::min(src.height(), dest->height());
@@ -203,108 +190,73 @@ namespace Rays
203
190
  uchar* d = dest->at<uchar>(0, y);
204
191
  copy_pixels(src_Bpp, width, d, dest_Bpp, s, src_Bpp);
205
192
  }
206
-
207
- return true;
208
193
  }
209
194
 
210
- static bool
211
- create_alpha_texture (Texture::Data* self)
195
+ static void
196
+ setup_texture (
197
+ Texture::Data* self, int width, int height, const ColorSpace& cs,
198
+ const Bitmap* bitmap, bool alpha_only)
212
199
  {
213
- if (!self || !self->bitmap) return false;
214
-
215
- ColorSpace cs = self->bitmap.color_space();
216
- int width_pow2 = min_pow2(self->width);
217
- int height_pow2 = min_pow2(self->height);
200
+ if (!self || width <= 0 || height <= 0 || !cs || (bitmap && !*bitmap))
201
+ argument_error(__FILE__, __LINE__);
218
202
 
219
- if (
220
- width_pow2 != self->width ||
221
- height_pow2 != self->height ||
222
- cs.is_rgb() ||
223
- cs.is_bgr())
224
- {
225
- ColorSpace newcs;
226
- if (!colorspace_for_alphabitmap(&newcs, cs))
227
- return false;
203
+ GLenum format, type;
204
+ cs.get_gl_enums(&format, &type, alpha_only);
228
205
 
229
- Bitmap bmp(width_pow2, height_pow2, newcs);
230
- if (!bmp) return false;
206
+ self->clear();
231
207
 
232
- if (!copy_bitmap(&bmp, self->bitmap, cs.alpha_pos()))
233
- return false;
208
+ self->width = width;
209
+ self->height = height;
210
+ self->width_pow2 = min_pow2(width);
211
+ self->height_pow2 = min_pow2(height);
212
+ self->color_space = cs;
213
+ self->alpha_only = alpha_only;
214
+ self->dirty = true;
234
215
 
235
- self->bitmap = bmp;
236
- cs = self->bitmap.color_space();
216
+ if (alpha_only)
217
+ {
218
+ colorspace_for_alphabitmap(&self->color_space, cs);
219
+ if (!self->color_space.is_gray())
220
+ rays_error(__FILE__, __LINE__, "alpha_only takes only gray color-space.");
237
221
  }
238
222
 
239
- if (!self->bitmap.color_space().is_gray())
240
- return false;
241
-
242
- return create_texture(self, GL_ALPHA);
243
- }
244
-
245
- static bool
246
- create_color_texture (Texture::Data* self)
247
- {
248
- if (!self || !self->bitmap) return false;
249
-
250
- int width_pow2 = min_pow2(self->width);
251
- int height_pow2 = min_pow2(self->height);
252
-
253
- if (
254
- width_pow2 != self->width ||
255
- height_pow2 != self->height)
223
+ Bitmap bmp;
224
+ if (bitmap)
256
225
  {
257
- Bitmap bmp(
258
- width_pow2, height_pow2, self->bitmap.color_space());
259
- if (!bmp) return false;
260
-
261
- if (!copy_bitmap(&bmp, self->bitmap))
262
- return false;
226
+ if (
227
+ self->color_space != cs ||
228
+ self->width_pow2 != self->width ||
229
+ self->height_pow2 != self->height)
230
+ {
231
+ bmp = Bitmap(self->width_pow2, self->height_pow2, self->color_space);
232
+ if (!bmp)
233
+ rays_error(__FILE__, __LINE__, "creating bitmap failed.");
263
234
 
264
- self->bitmap = bmp;
235
+ copy_bitmap(&bmp, *bitmap, alpha_only ? self->color_space.alpha_pos() : 0);
236
+ bitmap = &bmp;
237
+ }
265
238
  }
266
239
 
267
- ColorSpace cs = self->bitmap.color_space();
268
-
269
- bool alpha = cs.has_alpha();
270
- if (cs.is_rgb())
271
- return create_texture(self, alpha ? GL_RGBA : GL_RGB);
272
- else if (cs.is_bgr())
273
- return create_texture(self, alpha ? GL_BGRA : GL_BGR);
274
- else if (cs.is_gray())
275
- return create_texture(self, GL_LUMINANCE);
276
- else
277
- return false;
240
+ create_texture(self, bitmap, format, type);
278
241
  }
279
242
 
280
- static bool
281
- setup_texture (
282
- Texture::Data* self, const Bitmap& bitmap, bool alphaonly = false)
283
- {
284
- if (!self || !bitmap) return false;
285
-
286
- self->clear();
287
-
288
- self->bitmap = bitmap;
289
- if (!self->bitmap) return false;
290
243
 
291
- self->width = self->bitmap.width();
292
- self->height = self->bitmap.height();
293
-
294
- if (alphaonly)
295
- return create_alpha_texture(self);
296
- else
297
- return create_color_texture(self);
244
+ Texture::Texture ()
245
+ {
298
246
  }
299
247
 
300
-
301
- Texture::Texture ()
248
+ Texture::Texture (int width, int height, const ColorSpace& cs, bool alpha_only)
302
249
  {
250
+ setup_texture(
251
+ self.get(), width, height, cs,
252
+ NULL, alpha_only);
303
253
  }
304
254
 
305
- Texture::Texture (const Bitmap& bitmap, bool alphaonly)
255
+ Texture::Texture (const Bitmap& bitmap, bool alpha_only)
306
256
  {
307
- setup_texture(self.get(), bitmap, alphaonly);
257
+ setup_texture(
258
+ self.get(), bitmap.width(), bitmap.height(), bitmap.color_space(),
259
+ &bitmap, alpha_only);
308
260
  }
309
261
 
310
262
  Texture::~Texture ()
@@ -329,16 +281,28 @@ namespace Rays
329
281
  return self->height;
330
282
  }
331
283
 
284
+ const ColorSpace&
285
+ Texture::color_space () const
286
+ {
287
+ return self->color_space;
288
+ }
289
+
290
+ bool
291
+ Texture::alpha_only () const
292
+ {
293
+ return self->alpha_only;
294
+ }
295
+
332
296
  float
333
297
  Texture::s (float x) const
334
298
  {
335
- return x / (float) self->bitmap.width();
299
+ return x / (float) self->width_pow2;
336
300
  }
337
301
 
338
302
  float
339
303
  Texture::t (float y) const
340
304
  {
341
- return y / (float) self->bitmap.height();
305
+ return y / (float) self->height_pow2;
342
306
  }
343
307
 
344
308
  float
@@ -353,10 +317,16 @@ namespace Rays
353
317
  return t(self->height);
354
318
  }
355
319
 
356
- const Bitmap&
357
- Texture::bitmap () const
320
+ bool
321
+ Texture::dirty () const
322
+ {
323
+ return self->dirty;
324
+ }
325
+
326
+ void
327
+ Texture::set_dirty (bool b)
358
328
  {
359
- return self->bitmap;
329
+ self->dirty = b;
360
330
  }
361
331
 
362
332
  Texture::operator bool () const
data/test/helper.rb CHANGED
@@ -1,13 +1,20 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
 
4
- require 'xot/load_path'
5
- Xot::LoadPath.unshift File.expand_path('../../lib', __FILE__)
4
+ %w[../xot ../rucy .]
5
+ .map {|s| File.expand_path "../../#{s}/lib", __FILE__}
6
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
6
7
 
7
8
  require 'test/unit'
9
+ require 'xot/test'
8
10
  require 'rays'
9
11
 
12
+ include Xot::Test
13
+
10
14
 
11
15
  unless $RAYS_NOAUTOINIT
12
- def Rays.fin! (); end
16
+ def Rays.fin! () end
13
17
  end
18
+
19
+
20
+ Rays.init_offscreen_context
data/test/test_bitmap.rb CHANGED
@@ -6,4 +6,22 @@ require_relative 'helper'
6
6
 
7
7
  class TestBitmap < Test::Unit::TestCase
8
8
 
9
+ W = 32
10
+ H = 16
11
+
12
+ def bmp (w = W, h = H)
13
+ Rays::Bitmap.new w, h
14
+ end
15
+
16
+ def test_initialize ()
17
+ assert_equal W, bmp.width
18
+ assert_equal H, bmp.height
19
+ end
20
+
21
+ def test_to_texture ()
22
+ assert_equal Rays::Texture, bmp.to_texture.class
23
+ assert_equal W, bmp.to_texture.width
24
+ assert_equal H, bmp.to_texture.height
25
+ end
26
+
9
27
  end# TestBitmap
data/test/test_bounds.rb CHANGED
@@ -25,6 +25,16 @@ class TestBounds < Test::Unit::TestCase
25
25
  assert_raise(ArgumentError) {bounds(1, 2, 3, 4, 5, 6, 7)}
26
26
  end
27
27
 
28
+ def test_intersect? ()
29
+ assert bounds(10, 20, 30, 100, 100, 100).intersect?(bounds 50, 60, 70, 100, 100, 100)
30
+ assert_not bounds(10, 20, 30, 10, 10, 10).intersect?(bounds 50, 60, 70, 100, 100, 100)
31
+ end
32
+
33
+ def test_include? ()
34
+ assert bounds(10, 20, 30, 100, 100, 100).include?(point 50, 60)
35
+ assert_not bounds(10, 20, 30, 10, 10, 10).include?(point 50, 60)
36
+ end
37
+
28
38
  def test_get_xyzwhd ()
29
39
  o = bounds 1, 2, 3, 4, 5, 6
30
40
  assert_equal 1, o.x
@@ -68,22 +78,36 @@ class TestBounds < Test::Unit::TestCase
68
78
  assert_equal 4, o.right
69
79
  assert_equal 6, o.bottom
70
80
  assert_equal 8, o.front
81
+ assert_equal [1, 2], o.lt.to_a
82
+ assert_equal [4, 2], o.rt.to_a
83
+ assert_equal [1, 6], o.lb.to_a
84
+ assert_equal [4, 6], o.rb.to_a
71
85
  end
72
86
 
73
87
  def test_set_ltbrbf ()
74
- o = bounds
75
- o.left = -1
76
- assert_equal [-1, 0, 0, 1, 0, 0], o.to_a(3)
77
- o.top = -2
78
- assert_equal [-1, -2, 0, 1, 2, 0], o.to_a(3)
79
- o.back = -3
80
- assert_equal [-1, -2, -3, 1, 2, 3], o.to_a(3)
81
- o.right = 11
82
- assert_equal [-1, -2, -3, 13, 2, 3], o.to_a(3)
83
- o.bottom = 22
84
- assert_equal [-1, -2, -3, 13, 25, 3], o.to_a(3)
85
- o.front = 33
86
- assert_equal [-1, -2, -3, 13, 25, 37], o.to_a(3)
88
+ assert_equal [1, 0, 0, -1, 0, 0], bounds.tap {|o| o.left = 1}.to_a(3)
89
+ assert_equal [0, 2, 0, 0, -2, 0], bounds.tap {|o| o.top = 2}.to_a(3)
90
+ assert_equal [0, 0, 3, 0, 0, -3], bounds.tap {|o| o.back = 3}.to_a(3)
91
+ assert_equal [0, 0, 0, 5, 0, 0], bounds.tap {|o| o.right = 4}.to_a(3)
92
+ assert_equal [0, 0, 0, 0, 6, 0], bounds.tap {|o| o.bottom = 5}.to_a(3)
93
+ assert_equal [0, 0, 0, 0, 0, 7], bounds.tap {|o| o.front = 6}.to_a(3)
94
+
95
+ assert_equal [-1, 0, 0, 1, 0, 0], bounds.tap {|o| o.left = -1}.to_a(3)
96
+ assert_equal [ 0, -2, 0, 0, 2, 0], bounds.tap {|o| o.top = -2}.to_a(3)
97
+ assert_equal [ 0, 0, -3, 0, 0, 3], bounds.tap {|o| o.back = -3}.to_a(3)
98
+ assert_equal [ 0, 0, 0, -5, 0, 0], bounds.tap {|o| o.right = -4}.to_a(3)
99
+ assert_equal [ 0, 0, 0, 0, -6, 0], bounds.tap {|o| o.bottom = -5}.to_a(3)
100
+ assert_equal [ 0, 0, 0, 0, 0, -7], bounds.tap {|o| o.front = -6}.to_a(3)
101
+
102
+ assert_equal [20, 30, 0, 20, 30, 0], bounds(10, 20, 30, 40).tap {|o| o.lt = [20, 30]}.to_a(3)
103
+ assert_equal [10, 30, 0, 11, 30, 0], bounds(10, 20, 30, 40).tap {|o| o.rt = [20, 30]}.to_a(3)
104
+ assert_equal [20, 20, 0, 20, 11, 0], bounds(10, 20, 30, 40).tap {|o| o.lb = [20, 30]}.to_a(3)
105
+ assert_equal [10, 20, 0, 11, 11, 0], bounds(10, 20, 30, 40).tap {|o| o.rb = [20, 30]}.to_a(3)
106
+
107
+ assert_equal [20, 30, 0, 20, 30, 0], bounds(10, 20, 30, 40).tap {|o| o.lt += 10}.to_a(3)
108
+ assert_equal [10, 30, 0, 40, 30, 0], bounds(10, 20, 30, 40).tap {|o| o.rt += 10}.to_a(3)
109
+ assert_equal [20, 20, 0, 20, 50, 0], bounds(10, 20, 30, 40).tap {|o| o.lb += 10}.to_a(3)
110
+ assert_equal [10, 20, 0, 40, 50, 0], bounds(10, 20, 30, 40).tap {|o| o.rb += 10}.to_a(3)
87
111
  end
88
112
 
89
113
  def test_position ()
@@ -105,9 +129,12 @@ class TestBounds < Test::Unit::TestCase
105
129
 
106
130
  def test_move_to ()
107
131
  o = bounds 1, 2, 3, 4, 5, 6
108
- assert_equal bounds(7, 7, 3, 4, 5, 6), o.move_to(7)
109
- assert_equal bounds(7, 8, 3, 4, 5, 6), o.move_to(7, 8)
110
- assert_equal bounds(7, 8, 9, 4, 5, 6), o.move_to(7, 8, 9)
132
+ assert_equal bounds(7, 2, 3, 4, 5, 6), o.move_to( 7)
133
+ assert_equal bounds(7, 8, 3, 4, 5, 6), o.move_to( 7, 8)
134
+ assert_equal bounds(1, 8, 3, 4, 5, 6), o.move_to(nil, 8)
135
+ assert_equal bounds(7, 8, 9, 4, 5, 6), o.move_to( 7, 8, 9)
136
+ assert_equal bounds(1, 8, 9, 4, 5, 6), o.move_to(nil, 8, 9)
137
+ assert_equal bounds(1, 2, 9, 4, 5, 6), o.move_to(nil, nil, 9)
111
138
  assert_raise(ArgumentError) {o.move_to()}
112
139
  assert_raise(ArgumentError) {o.move_to(7, 8, 9, 10)}
113
140
 
@@ -122,10 +149,13 @@ class TestBounds < Test::Unit::TestCase
122
149
 
123
150
  def test_move_by ()
124
151
  o = bounds 1, 2, 3, 4, 5, 6
125
- assert_equal bounds( 8, 9, 3, 4, 5, 6), o.move_by(7)
126
- assert_equal bounds( 8, 10, 3, 4, 5, 6), o.move_by(7, 8)
127
- assert_equal bounds( 8, 10, 12, 4, 5, 6), o.move_by(7, 8, 9)
128
- assert_equal bounds(-6, -5, 3, 4, 5, 6), o.move_by(-7)
152
+ assert_equal bounds( 8, 2, 3, 4, 5, 6), o.move_by( 7)
153
+ assert_equal bounds( 8, 10, 3, 4, 5, 6), o.move_by( 7, 8)
154
+ assert_equal bounds( 1, 10, 3, 4, 5, 6), o.move_by(nil, 8)
155
+ assert_equal bounds( 8, 10, 12, 4, 5, 6), o.move_by( 7, 8, 9)
156
+ assert_equal bounds( 1, 10, 12, 4, 5, 6), o.move_by(nil, 8, 9)
157
+ assert_equal bounds( 1, 2, 12, 4, 5, 6), o.move_by(nil, nil, 9)
158
+ assert_equal bounds(-6, 2, 3, 4, 5, 6), o.move_by(-7)
129
159
  assert_equal bounds(-6, -6, 3, 4, 5, 6), o.move_by(-7, -8)
130
160
  assert_equal bounds(-6, -6, -6, 4, 5, 6), o.move_by(-7, -8, -9)
131
161
  assert_raise(ArgumentError) {o.move_by()}
@@ -142,9 +172,12 @@ class TestBounds < Test::Unit::TestCase
142
172
 
143
173
  def test_resize_to ()
144
174
  o = bounds 1, 2, 3, 4, 5, 6
145
- assert_equal bounds(1, 2, 3, 7, 7, 6), o.resize_to(7)
146
- assert_equal bounds(1, 2, 3, 7, 8, 6), o.resize_to(7, 8)
147
- assert_equal bounds(1, 2, 3, 7, 8, 9), o.resize_to(7, 8, 9)
175
+ assert_equal bounds(1, 2, 3, 7, 5, 6), o.resize_to( 7)
176
+ assert_equal bounds(1, 2, 3, 7, 8, 6), o.resize_to( 7, 8)
177
+ assert_equal bounds(1, 2, 3, 4, 8, 6), o.resize_to(nil, 8)
178
+ assert_equal bounds(1, 2, 3, 7, 8, 9), o.resize_to( 7, 8, 9)
179
+ assert_equal bounds(1, 2, 3, 4, 8, 9), o.resize_to(nil, 8, 9)
180
+ assert_equal bounds(1, 2, 3, 4, 5, 9), o.resize_to(nil, nil, 9)
148
181
  assert_raise(ArgumentError) {o.resize_to()}
149
182
  assert_raise(ArgumentError) {o.resize_to(7, 8, 9, 10)}
150
183
 
@@ -159,10 +192,13 @@ class TestBounds < Test::Unit::TestCase
159
192
 
160
193
  def test_resize_by ()
161
194
  o = bounds 1, 2, 3, 4, 5, 6
162
- assert_equal bounds(1, 2, 3, 11, 12, 6), o.resize_by(7)
163
- assert_equal bounds(1, 2, 3, 11, 13, 6), o.resize_by(7, 8)
164
- assert_equal bounds(1, 2, 3, 11, 13, 15), o.resize_by(7, 8, 9)
165
- assert_equal bounds(1, 2, 3, -3, -2, 6), o.resize_by(-7)
195
+ assert_equal bounds(1, 2, 3, 11, 5, 6), o.resize_by( 7)
196
+ assert_equal bounds(1, 2, 3, 11, 13, 6), o.resize_by( 7, 8)
197
+ assert_equal bounds(1, 2, 3, 4, 13, 6), o.resize_by(nil, 8)
198
+ assert_equal bounds(1, 2, 3, 11, 13, 15), o.resize_by( 7, 8, 9)
199
+ assert_equal bounds(1, 2, 3, 4, 13, 15), o.resize_by(nil, 8, 9)
200
+ assert_equal bounds(1, 2, 3, 4, 5, 15), o.resize_by(nil, nil, 9)
201
+ assert_equal bounds(1, 2, 3, -3, 5, 6), o.resize_by(-7)
166
202
  assert_equal bounds(1, 2, 3, -3, -3, 6), o.resize_by(-7, -8)
167
203
  assert_equal bounds(1, 2, 3, -3, -3, -3), o.resize_by(-7, -8, -9)
168
204
  assert_raise(ArgumentError) {o.resize_by()}
@@ -179,9 +215,12 @@ class TestBounds < Test::Unit::TestCase
179
215
 
180
216
  def test_inset_by ()
181
217
  o = bounds 1, 2, 3, 20, 30, 40
182
- assert_equal bounds(8, 9, 3, 6, 16, 40), o.inset_by(7)
183
- assert_equal bounds(8, 10, 3, 6, 14, 40), o.inset_by(7, 8)
184
- assert_equal bounds(8, 10, 12, 6, 14, 22), o.inset_by(7, 8, 9)
218
+ assert_equal bounds(8, 2, 3, 6, 30, 40), o.inset_by( 7)
219
+ assert_equal bounds(8, 10, 3, 6, 14, 40), o.inset_by( 7, 8)
220
+ assert_equal bounds(1, 10, 3, 20, 14, 40), o.inset_by(nil, 8)
221
+ assert_equal bounds(8, 10, 12, 6, 14, 22), o.inset_by( 7, 8, 9)
222
+ assert_equal bounds(1, 10, 12, 20, 14, 22), o.inset_by(nil, 8, 9)
223
+ assert_equal bounds(1, 2, 12, 20, 30, 22), o.inset_by(nil, nil, 9)
185
224
  assert_raise(ArgumentError) {o.inset_by()}
186
225
  assert_raise(ArgumentError) {o.inset_by(7, 8, 9, 10)}
187
226
 
@@ -208,7 +247,7 @@ class TestBounds < Test::Unit::TestCase
208
247
  def test_index ()
209
248
  o = bounds 1, 2, 3, 4, 5, 6
210
249
  assert_equal point(1, 2, 3), o[0]
211
- assert_equal point(4, 6, 8), o[1]
250
+ assert_equal point(4, 5, 6), o[1]
212
251
  assert_raise(IndexError) {o[-1]}
213
252
  assert_raise(IndexError) {o[2]}
214
253
  end
@@ -216,17 +255,17 @@ class TestBounds < Test::Unit::TestCase
216
255
  def test_index_assign ()
217
256
  o = bounds 1, 2, 3, 4, 5, 6
218
257
  o[0] = point 7, 8, 9
219
- assert_equal bounds(7, 8, 9, -2, -1, 0), o
258
+ assert_equal bounds(7, 8, 9, 4, 5, 6), o
220
259
  o[1] = point 10, 11, 12
221
- assert_equal bounds(7, 8, 9, 4, 4, 4), o
260
+ assert_equal bounds(7, 8, 9, 10, 11, 12), o
222
261
  assert_raise(IndexError) {o[-1]}
223
262
  assert_raise(IndexError) {o[2]}
224
263
  end
225
264
 
226
265
  def test_compare ()
227
266
  o = bounds 1, 2, 3, 4, 5, 6
228
- assert o == bounds(1, 2, 3, 4, 5, 6)
229
- assert !(o != bounds(1, 2, 3, 4, 5, 6))
267
+ assert o == bounds(1, 2, 3, 4, 5, 6)
268
+ assert_not o != bounds(1, 2, 3, 4, 5, 6)
230
269
 
231
270
  assert o < bounds(2, 2, 3, 4, 5, 6)
232
271
  assert o < bounds(1, 3, 3, 4, 5, 6)
@@ -243,4 +282,11 @@ class TestBounds < Test::Unit::TestCase
243
282
  assert o > bounds(1, 2, 3, 4, 5, 5)
244
283
  end
245
284
 
285
+ def test_operators ()
286
+ assert_equal bounds(50, 60, 70, 60, 60, 60), bounds(10, 20, 30, 100, 100, 100) & bounds(50, 60, 70, 100, 100, 100)
287
+ assert_equal bounds(10, 20, 30, 140, 140, 140), bounds(10, 20, 30, 100, 100, 100) | bounds(50, 60, 70, 100, 100, 100)
288
+
289
+ assert_equal point(0), (bounds(10, 20, 30, 10, 10, 10) & bounds(50, 60, 70, 10, 10, 10)).size
290
+ end
291
+
246
292
  end# TestBounds
data/test/test_color.rb CHANGED
@@ -10,15 +10,42 @@ class TestColor < Test::Unit::TestCase
10
10
  Rays::Color.new *args
11
11
  end
12
12
 
13
+ def color8 (r, g, b, a, div = 255)
14
+ color *[r, g, b, a].map {|n| n / div.to_f}
15
+ end
16
+
17
+ def parse (*args)
18
+ Rays::Color.parse *args
19
+ end
20
+
13
21
  def test_initialize ()
14
22
  assert_equal color(0, 0, 0, 1), color()
15
23
  assert_equal color(1, 1, 1, 1), color(1)
16
24
  assert_equal color(1, 1, 1, 2), color(1, 2)
17
25
  assert_equal color(1, 2, 3, 1), color(1, 2, 3)
18
26
  assert_equal color(1, 2, 3, 4), color(1, 2, 3, 4)
27
+ assert_equal color8(1, 2, 3, 4), color('#01020304')
19
28
  assert_raise(ArgumentError) {color(1, 2, 3, 4, 5)}
20
29
  end
21
30
 
31
+ def test_parse ()
32
+ assert_equal color(0, 0, 0, 1), parse('#000')
33
+ assert_equal color(0, 0, 0, 1), parse('#000000')
34
+ assert_equal color(0, 0, 0, 0), parse('#0000')
35
+ assert_equal color(0, 0, 0, 0), parse('#00000000')
36
+ assert_equal color8(0x01, 0x23, 0x45, 0x67), parse('#01234567')
37
+ assert_equal color8(0x89, 0xab, 0xcd, 0xef), parse('#89abcdef')
38
+ assert_equal color8(0x0, 0x2, 0x4, 0x6, 15), parse('#0246')
39
+ assert_equal color8(0x9, 0xb, 0xd, 0xf, 15), parse('#9bdf')
40
+ assert_equal color(0, 0, 0, 1), parse(' #000 ')
41
+ assert_raise(ArgumentError) {parse '#'}
42
+ assert_raise(ArgumentError) {parse '000'}
43
+ assert_raise(ArgumentError) {parse '#0'}
44
+ assert_raise(ArgumentError) {parse '#00'}
45
+ assert_raise(ArgumentError) {parse '#00000'}
46
+ assert_raise(ArgumentError) {parse '#0000000'}
47
+ end
48
+
22
49
  def test_get_rgb ()
23
50
  o = color 1, 2, 3, 4
24
51
  assert_equal 1, o.red
@@ -71,8 +98,8 @@ class TestColor < Test::Unit::TestCase
71
98
 
72
99
  def test_compare ()
73
100
  o = color 1, 2, 3, 4
74
- assert o == color(1, 2, 3, 4)
75
- assert !(o != color(1, 2, 3, 4))
101
+ assert o == color(1, 2, 3, 4)
102
+ assert_not o != color(1, 2, 3, 4)
76
103
 
77
104
  assert o < color(2, 2, 3, 4)
78
105
  assert o < color(1, 3, 3, 4)