rays 0.1.47 → 0.1.49

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/rays/bitmap.cpp +287 -46
  3. data/.doc/ext/rays/camera.cpp +2 -2
  4. data/.doc/ext/rays/color.cpp +11 -0
  5. data/.doc/ext/rays/defs.cpp +32 -8
  6. data/.doc/ext/rays/font.cpp +50 -2
  7. data/.doc/ext/rays/image.cpp +3 -3
  8. data/.doc/ext/rays/matrix.cpp +65 -7
  9. data/.doc/ext/rays/native.cpp +2 -4
  10. data/.doc/ext/rays/painter.cpp +117 -9
  11. data/.doc/ext/rays/point.cpp +1 -11
  12. data/.doc/ext/rays/polygon.cpp +133 -97
  13. data/.doc/ext/rays/polyline.cpp +89 -10
  14. data/.doc/ext/rays/rays.cpp +80 -0
  15. data/.doc/ext/rays/{noise.cpp → util.cpp} +2 -2
  16. data/ChangeLog.md +46 -0
  17. data/VERSION +1 -1
  18. data/ext/rays/bitmap.cpp +288 -46
  19. data/ext/rays/camera.cpp +2 -2
  20. data/ext/rays/color.cpp +13 -1
  21. data/ext/rays/defs.cpp +32 -8
  22. data/ext/rays/defs.h +56 -3
  23. data/ext/rays/font.cpp +56 -4
  24. data/ext/rays/image.cpp +3 -3
  25. data/ext/rays/matrix.cpp +69 -7
  26. data/ext/rays/native.cpp +2 -4
  27. data/ext/rays/painter.cpp +132 -13
  28. data/ext/rays/point.cpp +1 -12
  29. data/ext/rays/polygon.cpp +136 -99
  30. data/ext/rays/polyline.cpp +95 -9
  31. data/ext/rays/rays.cpp +80 -0
  32. data/ext/rays/{noise.cpp → util.cpp} +2 -2
  33. data/include/rays/color.h +3 -1
  34. data/include/rays/defs.h +24 -26
  35. data/include/rays/font.h +17 -3
  36. data/include/rays/image.h +1 -1
  37. data/include/rays/matrix.h +24 -0
  38. data/include/rays/painter.h +24 -0
  39. data/include/rays/polygon.h +68 -43
  40. data/include/rays/polyline.h +17 -2
  41. data/include/rays/ruby/polygon.h +0 -11
  42. data/include/rays/ruby/rays.h +4 -0
  43. data/include/rays/{noise.h → util.h} +2 -2
  44. data/lib/rays/color.rb +7 -1
  45. data/lib/rays/font.rb +1 -1
  46. data/lib/rays/image.rb +11 -1
  47. data/lib/rays/matrix.rb +16 -0
  48. data/lib/rays/painter.rb +18 -7
  49. data/lib/rays/point.rb +5 -1
  50. data/lib/rays/polygon.rb +44 -35
  51. data/lib/rays/polyline.rb +54 -8
  52. data/lib/rays.rb +0 -1
  53. data/rays.gemspec +2 -2
  54. data/src/color.cpp +11 -2
  55. data/src/font.cpp +37 -18
  56. data/src/font.h +6 -5
  57. data/src/image.cpp +58 -14
  58. data/src/ios/font.mm +89 -32
  59. data/src/ios/helper.h +2 -2
  60. data/src/ios/helper.mm +2 -2
  61. data/src/matrix.cpp +45 -0
  62. data/src/osx/font.mm +93 -33
  63. data/src/osx/helper.h +2 -2
  64. data/src/osx/helper.mm +2 -2
  65. data/src/painter.cpp +246 -114
  66. data/src/painter.h +11 -3
  67. data/src/polygon.cpp +431 -332
  68. data/src/polyline.cpp +138 -27
  69. data/src/polyline.h +3 -5
  70. data/src/shader.cpp +36 -4
  71. data/src/shader.h +1 -1
  72. data/src/texture.cpp +23 -4
  73. data/src/texture.h +2 -0
  74. data/src/{noise.cpp → util.cpp} +1 -1
  75. data/src/win32/font.cpp +1 -1
  76. data/test/test_bitmap.rb +12 -5
  77. data/test/test_color.rb +25 -4
  78. data/test/test_font.rb +23 -2
  79. data/test/test_image.rb +44 -18
  80. data/test/test_matrix.rb +22 -0
  81. data/test/test_painter.rb +27 -0
  82. data/test/test_point.rb +1 -1
  83. data/test/test_polygon.rb +52 -45
  84. data/test/test_polyline.rb +191 -72
  85. metadata +12 -18
  86. data/.doc/ext/rays/polygon_line.cpp +0 -97
  87. data/ext/rays/polygon_line.cpp +0 -100
  88. data/lib/rays/polygon_line.rb +0 -33
  89. data/test/test_polygon_line.rb +0 -164
data/src/polyline.cpp CHANGED
@@ -1,7 +1,8 @@
1
1
  #include "polyline.h"
2
2
 
3
3
 
4
- #include <assert.h>
4
+ #include <memory>
5
+ #include "rays/color.h"
5
6
  #include "rays/debug.h"
6
7
 
7
8
 
@@ -15,43 +16,122 @@ namespace Rays
15
16
  struct Polyline::Data
16
17
  {
17
18
 
19
+ typedef std::vector<Color> ColorList;
20
+
21
+ typedef std::vector<Coord3> TexCoordList;
22
+
18
23
  PointList points;
19
24
 
20
- bool loop = false, fill = false;
25
+ std::unique_ptr<ColorList> pcolors;
26
+
27
+ std::unique_ptr<TexCoordList> ptexcoords;
21
28
 
22
- template <typename I, typename FUN>
23
- void reset (I begin, I end, bool loop_, bool fill_, FUN to_point_fun)
29
+ bool loop = false, fill = false, hole = false;
30
+
31
+ void reset (
32
+ const auto* points_, const Color* colors_, const Coord3* texcoords_,
33
+ size_t size_, bool loop_, bool fill_, bool hole_,
34
+ auto to_point_fun)
24
35
  {
25
- if (begin > end)
26
- argument_error(__FILE__, __LINE__);
36
+ ColorList* colors = colors_ ? &this->colors() : NULL;
37
+ TexCoordList* texcoords = texcoords_ ? &this->texcoords() : NULL;
38
+ int size = (int) size_;
27
39
 
28
- points.clear();
29
40
  loop = loop_;
30
41
  fill = fill_;
42
+ hole = hole_;
43
+ if (!is_valid())
44
+ argument_error(__FILE__, __LINE__, "hole polyline must be looped");
31
45
 
32
- size_t size = end - begin;
33
- if (size <= 0) return;
34
-
46
+ points.clear();
35
47
  points.reserve(size);
36
- for (auto it = begin; it != end; ++it)
37
- points.emplace_back(to_point_fun(*it));
48
+ if (hole)
49
+ {
50
+ for (int i = size - 1; i >= 0; --i)
51
+ points.emplace_back(to_point_fun(points_[i]));
52
+ }
53
+ else
54
+ {
55
+ for (int i = 0; i < size; ++i)
56
+ points.emplace_back(to_point_fun(points_[i]));
57
+ }
58
+
59
+ if (colors)
60
+ {
61
+ colors->clear();
62
+ colors->reserve(size);
63
+ if (hole)
64
+ {
65
+ for (int i = size - 1; i >= 0; --i)
66
+ colors->emplace_back(colors_[i]);
67
+ }
68
+ else
69
+ {
70
+ for (int i = 0; i < size; ++i)
71
+ colors->emplace_back(colors_[i]);
72
+ }
73
+ }
74
+
75
+ if (texcoords)
76
+ {
77
+ texcoords->clear();
78
+ texcoords->reserve(size);
79
+ if (hole)
80
+ {
81
+ for (int i = size - 1; i >= 0; --i)
82
+ texcoords->emplace_back(texcoords_[i]);
83
+ }
84
+ else
85
+ {
86
+ for (int i = 0; i < size; ++i)
87
+ texcoords->emplace_back(texcoords_[i]);
88
+ }
89
+ }
38
90
  }
39
91
 
92
+ ColorList& colors ()
93
+ {
94
+ if (!pcolors) pcolors.reset(new ColorList());
95
+ return *pcolors;
96
+ }
97
+
98
+ TexCoordList& texcoords ()
99
+ {
100
+ if (!ptexcoords) ptexcoords.reset(new TexCoordList());
101
+ return *ptexcoords;
102
+ }
103
+
104
+ bool is_valid () const
105
+ {
106
+ return loop || !hole;
107
+ }
108
+
109
+ private:
110
+ #if 0
111
+ void reset_values (size_t size_, bool hole, auto fun)
112
+ {
113
+ int size = (int) size_;
114
+
115
+ if (hole)
116
+ for (int i = size - 1; i >= 0; --i) fun((size_t) i);
117
+ else
118
+ for (int i = 0; i < size; ++i) fun((size_t) i);
119
+ }
120
+ #endif
40
121
  };// Polyline::Data
41
122
 
42
123
 
43
- void
44
- Polyline_create (
45
- Polyline* polyline, const Path& path, bool loop, bool hole)
124
+ Polyline
125
+ Polyline_create (const Path& path, bool loop, bool hole)
46
126
  {
47
127
  Path cleaned;
48
128
  ClipperLib::CleanPolygon(path, cleaned);
49
129
 
50
- auto fun = [](const IntPoint& point) {return from_clipper(point);};
51
- if (hole)
52
- polyline->self->reset(cleaned.rbegin(), cleaned.rend(), loop, loop, fun);
53
- else
54
- polyline->self->reset(cleaned. begin(), cleaned. end(), loop, loop, fun);
130
+ Polyline pl;
131
+ pl.self->reset(
132
+ &cleaned[0], NULL, NULL, cleaned.size(), loop, loop, hole,
133
+ [](const IntPoint& point) {return from_clipper(point);});
134
+ return pl;
55
135
  }
56
136
 
57
137
  template <typename I>
@@ -66,8 +146,6 @@ namespace Rays
66
146
  void
67
147
  Polyline_get_path (Path* path, const Polyline& polyline, bool hole)
68
148
  {
69
- assert(path);
70
-
71
149
  const auto& points = polyline.self->points;
72
150
  if (hole)
73
151
  reset_path(path, points.rbegin(), points.rend());
@@ -80,17 +158,23 @@ namespace Rays
80
158
  {
81
159
  }
82
160
 
83
- Polyline::Polyline (const Point* points, size_t size, bool loop)
161
+ Polyline::Polyline (
162
+ const Point* points, size_t size, bool loop,
163
+ const Color* colors, const Coord3* texcoords,
164
+ bool hole)
84
165
  {
85
166
  self->reset(
86
- points, points + size, loop, loop,
167
+ points, colors, texcoords, size, loop, loop, hole,
87
168
  [](const Point& p) {return p;});
88
169
  }
89
170
 
90
- Polyline::Polyline (const Point* points, size_t size, bool loop, bool fill)
171
+ Polyline::Polyline (
172
+ const Point* points, size_t size, bool loop, bool fill,
173
+ const Color* colors, const Coord3* texcoords,
174
+ bool hole)
91
175
  {
92
176
  self->reset(
93
- points, points + size, loop, fill,
177
+ points, colors, texcoords, size, loop, fill, hole,
94
178
  [](const Point& p) {return p;});
95
179
  }
96
180
 
@@ -130,6 +214,33 @@ namespace Rays
130
214
  return self->fill;
131
215
  }
132
216
 
217
+ bool
218
+ Polyline::hole () const
219
+ {
220
+ return self->hole;
221
+ }
222
+
223
+ const Point*
224
+ Polyline::points () const
225
+ {
226
+ const auto& v = self->points;
227
+ return !v.empty() ? &v[0] : NULL;
228
+ }
229
+
230
+ const Color*
231
+ Polyline::colors () const
232
+ {
233
+ const auto& pv = self->pcolors;
234
+ return pv && !pv->empty() ? &(*pv)[0] : NULL;
235
+ }
236
+
237
+ const Coord3*
238
+ Polyline::texcoords () const
239
+ {
240
+ const auto& pv = self->ptexcoords;
241
+ return pv && !pv->empty() ? &(*pv)[0] : NULL;
242
+ }
243
+
133
244
  size_t
134
245
  Polyline::size () const
135
246
  {
@@ -162,7 +273,7 @@ namespace Rays
162
273
 
163
274
  Polyline::operator bool () const
164
275
  {
165
- return true;
276
+ return self->is_valid();
166
277
  }
167
278
 
168
279
  bool
data/src/polyline.h CHANGED
@@ -50,13 +50,11 @@ namespace Rays
50
50
  }
51
51
 
52
52
 
53
- void Polyline_create (
54
- Polyline* polyline, const ClipperLib::Path& path, bool loop,
55
- bool hole = false);
53
+ Polyline Polyline_create (
54
+ const ClipperLib::Path& path, bool loop, bool hole = false);
56
55
 
57
56
  void Polyline_get_path (
58
- ClipperLib::Path* path, const Polyline& polyline,
59
- bool hole = false);
57
+ ClipperLib::Path* path, const Polyline& polyline, bool hole = false);
60
58
 
61
59
  bool Polyline_expand (
62
60
  Polygon* result, const Polyline& polyline,
data/src/shader.cpp CHANGED
@@ -97,7 +97,30 @@ namespace Rays
97
97
  }
98
98
 
99
99
  static Shader
100
- make_default_shader_for_texture ()
100
+ make_default_shader_for_texture_clamp ()
101
+ {
102
+ const ShaderBuiltinVariableNames& names =
103
+ ShaderEnv_get_builtin_variable_names(DEFAULT_ENV);
104
+ return Shader(
105
+ "varying vec4 " + V_TEXCOORD + ";\n"
106
+ "varying vec4 " + V_COLOR + ";\n"
107
+ "uniform vec3 " + U_TEXCOORD_MIN + ";\n"
108
+ "uniform vec3 " + U_TEXCOORD_MAX + ";\n"
109
+ "uniform vec3 " + U_TEXCOORD_OFFSET + ";\n"
110
+ "uniform sampler2D " + U_TEXTURE + ";\n"
111
+ "void main ()\n"
112
+ "{\n"
113
+ " vec2 texcoord__ = clamp(" +
114
+ V_TEXCOORD + ".xy, " +
115
+ U_TEXCOORD_MIN + ".xy, " +
116
+ U_TEXCOORD_MAX + ".xy - " + U_TEXCOORD_OFFSET + ".xy);\n"
117
+ " vec4 color__ = texture2D(" + U_TEXTURE + ", texcoord__);\n"
118
+ " gl_FragColor = " + V_COLOR + " * color__;\n"
119
+ "}\n");
120
+ }
121
+
122
+ static Shader
123
+ make_default_shader_for_texture_repeat ()
101
124
  {
102
125
  const ShaderBuiltinVariableNames& names =
103
126
  ShaderEnv_get_builtin_variable_names(DEFAULT_ENV);
@@ -160,10 +183,19 @@ namespace Rays
160
183
  }
161
184
 
162
185
  const Shader&
163
- Shader_get_default_shader_for_texture ()
186
+ Shader_get_default_shader_for_texture (TexCoordWrap wrap)
164
187
  {
165
- static const Shader SHADER = make_default_shader_for_texture();
166
- return SHADER;
188
+ switch (wrap)
189
+ {
190
+ case TEXCOORD_REPEAT:
191
+ static const Shader REPEAT = make_default_shader_for_texture_repeat();
192
+ return REPEAT;
193
+
194
+ case TEXCOORD_CLAMP:
195
+ default:
196
+ static const Shader CLAMP = make_default_shader_for_texture_clamp();
197
+ return CLAMP;
198
+ }
167
199
  }
168
200
 
169
201
  const Shader&
data/src/shader.h CHANGED
@@ -57,7 +57,7 @@ namespace Rays
57
57
 
58
58
  const Shader& Shader_get_default_shader_for_shape ();
59
59
 
60
- const Shader& Shader_get_default_shader_for_texture ();
60
+ const Shader& Shader_get_default_shader_for_texture (TexCoordWrap wrap);
61
61
 
62
62
  const Shader& Shader_get_shader_for_text ();
63
63
 
data/src/texture.cpp CHANGED
@@ -145,7 +145,7 @@ namespace Rays
145
145
  }
146
146
 
147
147
  static void
148
- copy_bitmap (Bitmap* dest, const Bitmap& src)
148
+ copy_pixels (Bitmap* dest, const Bitmap& src)
149
149
  {
150
150
  assert(dest);
151
151
 
@@ -174,7 +174,7 @@ namespace Rays
174
174
  if (!*bmp)
175
175
  rays_error(__FILE__, __LINE__);
176
176
 
177
- copy_bitmap(bmp.get(), bitmap);
177
+ copy_pixels(bmp.get(), bitmap);
178
178
  return bmp;
179
179
  }
180
180
 
@@ -197,8 +197,8 @@ namespace Rays
197
197
  if (glIsTexture(self->id) == GL_FALSE)
198
198
  opengl_error(__FILE__, __LINE__, "failed to create texture.");
199
199
 
200
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
201
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
200
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
201
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
202
202
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
203
203
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);
204
204
 
@@ -258,6 +258,25 @@ namespace Rays
258
258
  &bitmap);
259
259
  }
260
260
 
261
+ Texture&
262
+ Texture::operator = (const Bitmap& bitmap)
263
+ {
264
+ if (!bitmap)
265
+ argument_error(__FILE__, __LINE__);
266
+
267
+ int w = bitmap.width(), h = bitmap.height();
268
+ if (w != width() || h != height())
269
+ argument_error(__FILE__, __LINE__, "the size of bitmap does not match");
270
+
271
+ GLenum format, type;
272
+ ColorSpace_get_gl_format_and_type(&format, &type, bitmap.color_space());
273
+
274
+ glBindTexture(GL_TEXTURE_2D, self->id);
275
+ glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, format, type, bitmap.pixels());
276
+
277
+ return *this;
278
+ }
279
+
261
280
  Texture::~Texture ()
262
281
  {
263
282
  }
data/src/texture.h CHANGED
@@ -28,6 +28,8 @@ namespace Rays
28
28
 
29
29
  Texture (const Bitmap& bitmap);
30
30
 
31
+ Texture& operator = (const Bitmap& bitmap);
32
+
31
33
  ~Texture ();
32
34
 
33
35
  int width () const;
@@ -1,4 +1,4 @@
1
- #include "rays/noise.h"
1
+ #include "rays/util.h"
2
2
 
3
3
 
4
4
  #include <glm/gtc/noise.hpp>
data/src/win32/font.cpp CHANGED
@@ -57,7 +57,7 @@ namespace Rays
57
57
 
58
58
 
59
59
  const Font&
60
- default_font ()
60
+ get_default_font ()
61
61
  {
62
62
  static const Font FONT(NULL);
63
63
  return FONT;
data/test/test_bitmap.rb CHANGED
@@ -6,8 +6,8 @@ class TestBitmap < Test::Unit::TestCase
6
6
  W = 32
7
7
  H = 16
8
8
 
9
- def bitmap(w = W, h = H)
10
- Rays::Bitmap.new w, h
9
+ def bitmap(w = W, h = H, *args)
10
+ Rays::Bitmap.new w, h, *args
11
11
  end
12
12
 
13
13
  def color(*args)
@@ -32,10 +32,17 @@ class TestBitmap < Test::Unit::TestCase
32
32
  end
33
33
 
34
34
  def test_pixels()
35
- colors = %w[#f00 #0f0 #00f #ff0].map {|s| color s}
36
- bmp = bitmap 2, 2
37
- bmp[0, 0], bmp[1, 0], bmp[0, 1], bmp[1, 1] = colors
35
+ bmp = bitmap 2, 2, Rays::RGBA
36
+ assert_equal [0] * 4, bmp.pixels
37
+
38
+ bmp.pixels = [0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffff00]
38
39
  assert_equal [0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffff00], bmp.pixels
40
+
41
+ bmp = bitmap 2, 2, Rays::RGBA_float
42
+ assert_equal [0,0,0,0] * 4, bmp.pixels
43
+
44
+ bmp.pixels = [1,0,0,1, 0,1,0,1, 0,0,1,1, 1,1,0,1]
45
+ assert_equal [1,0,0,1, 0,1,0,1, 0,0,1,1, 1,1,0,1], bmp.pixels
39
46
  end
40
47
 
41
48
  def test_at()
data/test/test_color.rb CHANGED
@@ -66,19 +66,18 @@ class TestColor < Test::Unit::TestCase
66
66
  def test_set_rgb()
67
67
  o = color
68
68
  assert_equal [0, 0, 0, 1], o.to_a
69
- o.red = 1
69
+ o.red = 1
70
70
  assert_equal [1, 0, 0, 1], o.to_a
71
71
  o.green = 2
72
72
  assert_equal [1, 2, 0, 1], o.to_a
73
- o.blue = 3
73
+ o.blue = 3
74
74
  assert_equal [1, 2, 3, 1], o.to_a
75
75
  o.alpha = 4
76
76
  assert_equal [1, 2, 3, 4], o.to_a
77
77
  end
78
78
 
79
79
  def test_to_a()
80
- o = color 1, 2, 3, 4
81
- assert_equal [1, 2, 3, 4], o.to_a
80
+ assert_equal [1, 2, 3, 4], color(1, 2, 3, 4).to_a
82
81
  end
83
82
 
84
83
  def test_index()
@@ -121,6 +120,14 @@ class TestColor < Test::Unit::TestCase
121
120
  assert o > color(1, 2, 3, 3)
122
121
  end
123
122
 
123
+ def test_to_hsv()
124
+ h, s, v, a = hsv(0.1, 0.2, 0.3, 0.4).to_hsv
125
+ assert_in_delta 0.1, h
126
+ assert_in_delta 0.2, s
127
+ assert_in_delta 0.3, v
128
+ assert_in_delta 0.4, a
129
+ end
130
+
124
131
  def test_hsv_hue()
125
132
  assert_equal_color color(0.5, 0, 1), hsv(-0.25, 1, 1)
126
133
  assert_equal_color color(1, 0, 0), hsv( 0, 1, 1)
@@ -149,4 +156,18 @@ class TestColor < Test::Unit::TestCase
149
156
  assert_equal_color color(1, 0, 0, 1), hsv(1, 1, 1, 1)
150
157
  end
151
158
 
159
+ def test_hsb()
160
+ assert_equal(
161
+ Rays::Color.hsv(0.1, 0.2, 0.3, 0.4),
162
+ Rays::Color.hsb(0.1, 0.2, 0.3, 0.4))
163
+
164
+ assert_equal(
165
+ color(0.1, 0.2, 0.3, 0.4).to_hsv,
166
+ color(0.1, 0.2, 0.3, 0.4).to_hsb)
167
+ end
168
+
169
+ def test_inspect()
170
+ assert_equal "#<Rays::Color 1.0 2.0 3.0 1.0>", color(1, 2, 3).inspect
171
+ end
172
+
152
173
  end# TestColor
data/test/test_font.rb CHANGED
@@ -3,8 +3,10 @@ require_relative 'helper'
3
3
 
4
4
  class TestFont < Test::Unit::TestCase
5
5
 
6
+ R = Rays
7
+
6
8
  def font(*args)
7
- Rays::Font.new(*args)
9
+ R::Font.new(*args)
8
10
  end
9
11
 
10
12
  def test_name()
@@ -13,13 +15,28 @@ class TestFont < Test::Unit::TestCase
13
15
 
14
16
  def test_size()
15
17
  assert_kind_of Numeric, font.size
16
- assert_equal 32, font(nil, 32).size
18
+
19
+ f = font('Arial', 10)
20
+ assert_equal 10, f.size
21
+
22
+ name = f.name
23
+ f.size = 11
24
+ assert_equal 11, f.size
25
+ assert_equal name, f.name
26
+
27
+ f11 = f.dup
28
+ f.size = 12
29
+ assert_equal 12, f .size
30
+ assert_equal 11, f11.size
17
31
  end
18
32
 
19
33
  def test_width()
20
34
  assert_equal 0, font.width('')
21
35
  w = font.width 'X'
22
36
  assert_equal w * 2, font.width('XX')
37
+ assert_equal w * 2, font.width("XX\nX")
38
+ assert_equal w * 2, font.width("XX\nXX")
39
+ assert_equal w * 3, font.width("XX\nXXX")
23
40
  end
24
41
 
25
42
  def test_height()
@@ -27,4 +44,8 @@ class TestFont < Test::Unit::TestCase
27
44
  assert_equal f.height, f.ascent + f.descent + f.leading
28
45
  end
29
46
 
47
+ def test_families()
48
+ assert_not R::Font.families.empty?
49
+ end
50
+
30
51
  end# TestFont
data/test/test_image.rb CHANGED
@@ -11,7 +11,7 @@ class TestImage < Test::Unit::TestCase
11
11
  Rays::Image.load path
12
12
  end
13
13
 
14
- def color(r = 0, g = 0, b = 0, a = 0)
14
+ def color(r, g, b, a)
15
15
  Rays::Color.new r, g, b, a
16
16
  end
17
17
 
@@ -19,6 +19,10 @@ class TestImage < Test::Unit::TestCase
19
19
  Rays::Bounds.new(*args)
20
20
  end
21
21
 
22
+ def update_texture(img)
23
+ image(1, 1).paint {image img}
24
+ end
25
+
22
26
  def test_initialize()
23
27
  assert_equal 10, image(10, 20).width
24
28
  assert_equal 20, image(10, 20).height
@@ -42,6 +46,28 @@ class TestImage < Test::Unit::TestCase
42
46
  assert_equal 10, image(20, 10).bitmap.height
43
47
  end
44
48
 
49
+ def test_bitmap_with_modify_flag()
50
+ img1 = image 1, 1
51
+ update_texture img1
52
+ img1.bitmap(false).tap {|bmp| bmp[0, 0] = color 1, 0, 0, 1}
53
+
54
+ img2 = image 1, 1
55
+ update_texture img2
56
+ img2.bitmap(true) .tap {|bmp| bmp[0, 0] = color 0, 1, 0, 1}
57
+
58
+ assert_equal [0x00000000], image(1, 1).paint {image img1}.pixels
59
+ assert_equal [0xff00ff00], image(1, 1).paint {image img2}.pixels
60
+ end
61
+
62
+ def test_pixels()
63
+ img = image 2, 1
64
+ assert_equal [0x00000000, 0x00000000], img.pixels
65
+
66
+ img.pixels = [0xffff0000, 0xff00ff00]
67
+ assert_equal [0xffff0000, 0xff00ff00], img.pixels
68
+ assert_equal [0xffff0000, 0xff00ff00], image(2, 1).paint {image img}.pixels
69
+ end
70
+
45
71
  def test_painter()
46
72
  pa = image(10, 10).painter
47
73
  assert_equal color(0, 0, 0, 0), pa.background
@@ -52,24 +78,24 @@ class TestImage < Test::Unit::TestCase
52
78
  end
53
79
 
54
80
  def test_paint()
55
- def paint(&block)
81
+ paint = -> &block {
56
82
  Rays::Image.new(10, 10).paint(&block)
57
- end
58
- def fill(&block)
59
- paint {|p| p.fill 1, 0, 0; p.stroke nil; block.call p}
60
- end
61
- def stroke(&block)
62
- paint {|p| p.fill nil; p.stroke 1, 0, 0; block.call p}
63
- end
64
- def drawn(&block)
65
- fill(&block).bitmap.to_a.reject {|o| o.transparent?}.uniq.size > 0
66
- end
67
-
68
- assert_equal color(0, 0, 0, 0), fill {|p| p.rect 1, 1, 8, 8}[0, 0]
69
- assert_equal color(1, 0, 0, 1), fill {|p| p.rect 1, 1, 8, 8}[1, 1]
70
- assert_equal color(1, 0, 0, 1), stroke {|p| p.line 0, 0, 1, 1}[0, 0]
71
-
72
- assert drawn {|p| p.text "a"}
83
+ }
84
+ fill = -> &block {
85
+ paint.call {|p| p.fill 1, 0, 0; p.stroke nil; block.call p}
86
+ }
87
+ stroke = -> &block {
88
+ paint.call {|p| p.fill nil; p.stroke 1, 0, 0; block.call p}
89
+ }
90
+ drawn = -> &block {
91
+ fill[&block].bitmap.to_a.reject {|o| o.transparent?}.uniq.size > 0
92
+ }
93
+
94
+ assert_equal color(0, 0, 0, 0), fill.call {|p| p.rect 1, 1, 8, 8}[0, 0]
95
+ assert_equal color(1, 0, 0, 1), fill.call {|p| p.rect 1, 1, 8, 8}[1, 1]
96
+ assert_equal color(1, 0, 0, 1), stroke.call {|p| p.line 0, 0, 1, 1}[0, 0]
97
+
98
+ assert drawn.call {|p| p.text "a"}
73
99
  end
74
100
 
75
101
  def test_save_load()
data/test/test_matrix.rb CHANGED
@@ -87,6 +87,10 @@ class TestMatrix < Test::Unit::TestCase
87
87
  assert o > matrix(0)
88
88
  end
89
89
 
90
+ def test_transpose()
91
+ assert_equal mat_str('1594 2615 3726 4837'), mat_str('1234 5678 9123 4567').transpose
92
+ end
93
+
90
94
  def test_transform()
91
95
  assert_equal mat_str('1001 0102 0013 0001'), translate(1, 2, 3)
92
96
  assert_equal mat_str('2000 0300 0040 0001'), scale(2, 3, 4)
@@ -100,4 +104,22 @@ class TestMatrix < Test::Unit::TestCase
100
104
  assert (rotate(90, 1, 0, 0) * point(0, 1, 0)).z > 0.99
101
105
  end
102
106
 
107
+ def test_ortho()
108
+ m = Rays::Matrix
109
+ assert_equal m, m.ortho(1, 2, 3, 4) .class
110
+ assert_equal m, m.ortho(1, 2, 3, 4, 5, 6).class
111
+ end
112
+
113
+ def test_projection()
114
+ m = Rays::Matrix
115
+ assert_equal m, m.ortho(1, 2, 3, 4).class
116
+ end
117
+
118
+ def test_look_at()
119
+ m = Rays::Matrix
120
+ assert_equal m, m.look_at(1, 2, 3, 4, 5, 6) .class
121
+ assert_equal m, m.look_at(1, 2, 3, 4, 5, 6, 7, 8, 9) .class
122
+ assert_equal m, m.look_at(point(1, 2, 3), point(4, 5, 6), point(7, 8, 9)).class
123
+ end
124
+
103
125
  end# TestMatrix
data/test/test_painter.rb CHANGED
@@ -152,6 +152,33 @@ class TestPainter < Test::Unit::TestCase
152
152
  assert_equal 4, pa.miter_limit
153
153
  end
154
154
 
155
+ def test_line_height_accessor()
156
+ pa = painter
157
+
158
+ h = pa.line_height
159
+ assert h > 0
160
+ assert_not_equal 1, h
161
+
162
+ assert_equal [h, -1], [pa.line_height, pa.line_height!]
163
+ pa.line_height = 1
164
+ assert_equal [1, 1], [pa.line_height, pa.line_height!]
165
+ pa.line_height 2
166
+ assert_equal [2, 2], [pa.line_height, pa.line_height!]
167
+ pa.push line_height: 9 do |_|
168
+ assert_equal [9, 9], [pa.line_height, pa.line_height!]
169
+ end
170
+ assert_equal [2, 2], [pa.line_height, pa.line_height!]
171
+ pa.line_height = -1
172
+ assert_equal [h, -1], [pa.line_height, pa.line_height!]
173
+ pa.line_height 0
174
+ assert_equal [0, 0], [pa.line_height, pa.line_height!]
175
+ pa.line_height = nil
176
+ assert_equal [h, -1], [pa.line_height, pa.line_height!]
177
+
178
+ pa.font "Menlo", 100
179
+ assert_equal pa.font.height, pa.line_height
180
+ end
181
+
155
182
  def test_blend_mode_accessor()
156
183
  pa = painter
157
184
  assert_equal :normal, pa.blend_mode