rays 0.1.12 → 0.1.17

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 (168) hide show
  1. checksums.yaml +5 -5
  2. data/.doc/ext/rays/bitmap.cpp +22 -76
  3. data/.doc/ext/rays/bounds.cpp +95 -125
  4. data/.doc/ext/rays/camera.cpp +171 -0
  5. data/.doc/ext/rays/color.cpp +223 -45
  6. data/.doc/ext/rays/color_space.cpp +146 -46
  7. data/.doc/ext/rays/defs.cpp +183 -0
  8. data/.doc/ext/rays/font.cpp +69 -21
  9. data/.doc/ext/rays/image.cpp +26 -37
  10. data/.doc/ext/rays/matrix.cpp +186 -29
  11. data/.doc/ext/rays/native.cpp +14 -8
  12. data/.doc/ext/rays/noise.cpp +53 -0
  13. data/.doc/ext/rays/painter.cpp +187 -292
  14. data/.doc/ext/rays/point.cpp +96 -77
  15. data/.doc/ext/rays/polygon.cpp +313 -0
  16. data/.doc/ext/rays/polygon_line.cpp +96 -0
  17. data/.doc/ext/rays/polyline.cpp +167 -0
  18. data/.doc/ext/rays/rays.cpp +103 -12
  19. data/.doc/ext/rays/shader.cpp +83 -9
  20. data/LICENSE +21 -0
  21. data/README.md +1 -1
  22. data/Rakefile +24 -9
  23. data/VERSION +1 -1
  24. data/ext/rays/bitmap.cpp +23 -81
  25. data/ext/rays/bounds.cpp +100 -128
  26. data/ext/rays/camera.cpp +186 -0
  27. data/ext/rays/color.cpp +231 -51
  28. data/ext/rays/color_space.cpp +149 -47
  29. data/ext/rays/defs.cpp +183 -0
  30. data/ext/rays/defs.h +26 -2
  31. data/ext/rays/extconf.rb +2 -3
  32. data/ext/rays/font.cpp +74 -24
  33. data/ext/rays/image.cpp +28 -40
  34. data/ext/rays/matrix.cpp +198 -30
  35. data/ext/rays/native.cpp +14 -8
  36. data/ext/rays/noise.cpp +55 -0
  37. data/ext/rays/painter.cpp +203 -298
  38. data/ext/rays/point.cpp +105 -81
  39. data/ext/rays/polygon.cpp +329 -0
  40. data/ext/rays/polygon_line.cpp +99 -0
  41. data/ext/rays/polyline.cpp +176 -0
  42. data/ext/rays/rays.cpp +103 -13
  43. data/ext/rays/shader.cpp +84 -9
  44. data/include/rays.h +10 -2
  45. data/include/rays/bitmap.h +14 -26
  46. data/include/rays/bounds.h +21 -4
  47. data/include/rays/camera.h +74 -0
  48. data/include/rays/color.h +25 -14
  49. data/include/rays/color_space.h +15 -10
  50. data/include/rays/coord.h +114 -0
  51. data/include/rays/debug.h +22 -0
  52. data/include/rays/defs.h +36 -0
  53. data/include/rays/exception.h +6 -2
  54. data/include/rays/font.h +4 -4
  55. data/include/rays/image.h +12 -18
  56. data/include/rays/matrix.h +50 -24
  57. data/include/rays/noise.h +42 -0
  58. data/include/rays/opengl.h +2 -50
  59. data/include/rays/painter.h +89 -93
  60. data/include/rays/point.h +44 -51
  61. data/include/rays/polygon.h +198 -0
  62. data/include/rays/polyline.h +71 -0
  63. data/include/rays/rays.h +3 -0
  64. data/include/rays/ruby.h +7 -1
  65. data/include/rays/ruby/bounds.h +1 -1
  66. data/include/rays/ruby/camera.h +41 -0
  67. data/include/rays/ruby/color.h +1 -1
  68. data/include/rays/ruby/color_space.h +1 -1
  69. data/include/rays/ruby/font.h +1 -1
  70. data/include/rays/ruby/matrix.h +1 -1
  71. data/include/rays/ruby/point.h +1 -1
  72. data/include/rays/ruby/polygon.h +52 -0
  73. data/include/rays/ruby/polyline.h +41 -0
  74. data/include/rays/ruby/rays.h +8 -0
  75. data/include/rays/ruby/shader.h +1 -1
  76. data/include/rays/shader.h +36 -8
  77. data/lib/rays.rb +7 -2
  78. data/lib/rays/bitmap.rb +0 -15
  79. data/lib/rays/bounds.rb +17 -23
  80. data/lib/rays/camera.rb +24 -0
  81. data/lib/rays/color.rb +20 -47
  82. data/lib/rays/color_space.rb +13 -13
  83. data/lib/rays/image.rb +3 -7
  84. data/lib/rays/matrix.rb +28 -0
  85. data/lib/rays/module.rb +4 -19
  86. data/lib/rays/painter.rb +78 -93
  87. data/lib/rays/point.rb +13 -21
  88. data/lib/rays/polygon.rb +58 -0
  89. data/lib/rays/polygon_line.rb +36 -0
  90. data/lib/rays/polyline.rb +32 -0
  91. data/lib/rays/shader.rb +20 -1
  92. data/rays.gemspec +5 -7
  93. data/src/bitmap.h +36 -0
  94. data/src/bounds.cpp +74 -11
  95. data/src/color.cpp +58 -23
  96. data/src/color_space.cpp +52 -34
  97. data/src/color_space.h +22 -0
  98. data/src/coord.cpp +170 -0
  99. data/src/coord.h +35 -0
  100. data/src/font.cpp +118 -0
  101. data/src/font.h +64 -0
  102. data/src/frame_buffer.cpp +37 -71
  103. data/src/frame_buffer.h +4 -4
  104. data/src/image.cpp +172 -98
  105. data/src/image.h +25 -0
  106. data/src/ios/bitmap.h +23 -0
  107. data/src/ios/bitmap.mm +133 -110
  108. data/src/ios/camera.mm +510 -0
  109. data/src/ios/font.mm +50 -62
  110. data/src/ios/helper.h +4 -4
  111. data/src/ios/opengl.mm +19 -4
  112. data/src/ios/rays.mm +3 -0
  113. data/src/matrix.cpp +111 -26
  114. data/src/matrix.h +30 -0
  115. data/src/noise.cpp +74 -0
  116. data/src/opengl.cpp +9 -27
  117. data/src/opengl.h +37 -0
  118. data/src/osx/bitmap.h +23 -0
  119. data/src/osx/bitmap.mm +133 -110
  120. data/src/osx/camera.mm +451 -0
  121. data/src/osx/font.mm +49 -62
  122. data/src/osx/helper.h +2 -2
  123. data/src/osx/opengl.mm +19 -83
  124. data/src/osx/rays.mm +3 -0
  125. data/src/painter.cpp +845 -671
  126. data/src/painter.h +24 -0
  127. data/src/point.cpp +140 -119
  128. data/src/polygon.cpp +1266 -0
  129. data/src/polygon.h +32 -0
  130. data/src/polyline.cpp +160 -0
  131. data/src/polyline.h +69 -0
  132. data/src/render_buffer.cpp +11 -4
  133. data/src/render_buffer.h +2 -2
  134. data/src/shader.cpp +163 -106
  135. data/src/shader.h +38 -0
  136. data/src/shader_program.cpp +533 -0
  137. data/src/{program.h → shader_program.h} +28 -16
  138. data/src/shader_source.cpp +140 -0
  139. data/src/shader_source.h +52 -0
  140. data/src/texture.cpp +136 -160
  141. data/src/texture.h +65 -0
  142. data/src/win32/bitmap.cpp +62 -52
  143. data/src/win32/font.cpp +11 -13
  144. data/src/win32/font.h +24 -0
  145. data/src/win32/gdi.h +6 -6
  146. data/test/helper.rb +0 -3
  147. data/test/test_bitmap.rb +31 -7
  148. data/test/test_bounds.rb +36 -0
  149. data/test/test_color.rb +59 -19
  150. data/test/test_color_space.rb +95 -0
  151. data/test/test_font.rb +5 -0
  152. data/test/test_image.rb +24 -20
  153. data/test/test_matrix.rb +106 -0
  154. data/test/test_painter.rb +157 -51
  155. data/test/test_painter_shape.rb +102 -0
  156. data/test/test_point.rb +29 -0
  157. data/test/test_polygon.rb +234 -0
  158. data/test/test_polygon_line.rb +167 -0
  159. data/test/test_polyline.rb +171 -0
  160. data/test/test_shader.rb +9 -9
  161. metadata +102 -70
  162. data/.doc/ext/rays/texture.cpp +0 -138
  163. data/ext/rays/texture.cpp +0 -149
  164. data/include/rays/ruby/texture.h +0 -41
  165. data/include/rays/texture.h +0 -71
  166. data/lib/rays/texture.rb +0 -24
  167. data/src/program.cpp +0 -648
  168. data/test/test_texture.rb +0 -27
@@ -1,16 +1,10 @@
1
1
  #include "rays/ruby/point.h"
2
2
 
3
3
 
4
- #include <rucy.h>
5
4
  #include "defs.h"
6
5
 
7
6
 
8
- using namespace Rucy;
9
-
10
- using Rays::coord;
11
-
12
-
13
- RUCY_DEFINE_VALUE_FROM_TO(Rays::Point)
7
+ RUCY_DEFINE_VALUE_OR_ARRAY_FROM_TO(Rays::Point)
14
8
 
15
9
  #define THIS to<Rays::Point*>(self)
16
10
 
@@ -29,21 +23,8 @@ VALUE initialize(VALUE self)
29
23
  CHECK;
30
24
  check_arg_count(__FILE__, __LINE__, "Point#initialize", argc, 0, 1, 2, 3);
31
25
 
32
- switch (argc)
33
- {
34
- case 1:
35
- *THIS = Rays::Point(to<coord>(argv[0]));
36
- break;
37
-
38
- case 2:
39
- *THIS = Rays::Point(to<coord>(argv[0]), to<coord>(argv[1]));
40
- break;
41
-
42
- case 3:
43
- *THIS = Rays::Point(
44
- to<coord>(argv[0]), to<coord>(argv[1]), to<coord>(argv[2]));
45
- break;
46
- }
26
+ if (argc >= 1)
27
+ *THIS = to<Rays::Point>(argc, argv);
47
28
 
48
29
  return self;
49
30
  }
@@ -95,6 +76,22 @@ VALUE move_by(VALUE self)
95
76
  return self;
96
77
  }
97
78
 
79
+ static
80
+ VALUE rotate(VALUE self, VALUE degree)
81
+ {
82
+ CHECK;
83
+ Rays::Point p = *THIS;
84
+ p.rotate(to<float>(degree));
85
+ return value(p);
86
+ }
87
+
88
+ static
89
+ VALUE rotate_self(VALUE self, VALUE degree)
90
+ {
91
+ CHECK;
92
+ THIS->rotate(to<float>(degree));
93
+ }
94
+
98
95
  static
99
96
  VALUE length(VALUE self)
100
97
  {
@@ -121,7 +118,8 @@ static
121
118
  VALUE set_x(VALUE self, VALUE x)
122
119
  {
123
120
  CHECK;
124
- return value(THIS->x = to<coord>(x));
121
+ THIS->x = to<coord>(x);
122
+ return x;
125
123
  }
126
124
 
127
125
  static
@@ -135,7 +133,8 @@ static
135
133
  VALUE set_y(VALUE self, VALUE y)
136
134
  {
137
135
  CHECK;
138
- return value(THIS->y = to<coord>(y));
136
+ THIS->y = to<coord>(y);
137
+ return y;
139
138
  }
140
139
 
141
140
  static
@@ -149,7 +148,8 @@ static
149
148
  VALUE set_z(VALUE self, VALUE z)
150
149
  {
151
150
  CHECK;
152
- return value(THIS->z = to<coord>(z));
151
+ THIS->z = to<coord>(z);
152
+ return z;
153
153
  }
154
154
 
155
155
  static
@@ -160,47 +160,54 @@ VALUE get_z(VALUE self)
160
160
  }
161
161
 
162
162
  static
163
- VALUE add(VALUE self, VALUE point)
163
+ VALUE negate(VALUE self)
164
164
  {
165
165
  CHECK;
166
-
167
- Rays::Point p = *THIS;
168
- p += to<Rays::Point&>(point);
169
- return value(p);
166
+ return value(-*THIS);
170
167
  }
171
168
 
172
169
  static
173
- VALUE sub(VALUE self, VALUE point)
170
+ VALUE add(VALUE self)
174
171
  {
175
172
  CHECK;
176
-
177
- Rays::Point p = *THIS;
178
- p -= to<Rays::Point&>(point);
179
- return value(p);
173
+ if (argc == 1 && argv->is_num())
174
+ return value(*THIS + to<coord>(*argv));
175
+ else
176
+ return value(*THIS + to<Rays::Point>(argc, argv));
180
177
  }
181
178
 
182
179
  static
183
- VALUE mult(VALUE self, VALUE point)
180
+ VALUE sub(VALUE self)
184
181
  {
185
182
  CHECK;
186
-
187
- Rays::Point p = *THIS;
188
- p *= to<Rays::Point&>(point);
189
- return value(p);
183
+ if (argc == 1 && argv->is_num())
184
+ return value(*THIS - to<coord>(*argv));
185
+ else
186
+ return value(*THIS - to<Rays::Point>(argc, argv));
190
187
  }
191
188
 
192
189
  static
193
- VALUE div(VALUE self, VALUE point)
190
+ VALUE mult(VALUE self)
194
191
  {
195
192
  CHECK;
193
+ if (argc == 1 && argv->is_num())
194
+ return value(*THIS * to<coord>(*argv));
195
+ else
196
+ return value(*THIS * to<Rays::Point>(argc, argv));
197
+ }
196
198
 
197
- Rays::Point p = *THIS;
198
- p /= to<Rays::Point&>(point);
199
- return value(p);
199
+ static
200
+ VALUE div(VALUE self)
201
+ {
202
+ CHECK;
203
+ if (argc == 1 && argv->is_num())
204
+ return value(*THIS / to<coord>(*argv));
205
+ else
206
+ return value(*THIS / to<Rays::Point>(argc, argv));
200
207
  }
201
208
 
202
209
  static
203
- VALUE array_get(VALUE self, VALUE index)
210
+ VALUE set_at(VALUE self, VALUE index, VALUE value)
204
211
  {
205
212
  CHECK;
206
213
 
@@ -208,11 +215,12 @@ VALUE array_get(VALUE self, VALUE index)
208
215
  if (i < 0 || 2 < i)
209
216
  index_error(__FILE__, __LINE__);
210
217
 
211
- return value((*THIS)[i]);
218
+ (*THIS)[i] = to<coord>(value);
219
+ return value;
212
220
  }
213
221
 
214
222
  static
215
- VALUE array_set(VALUE self, VALUE index, VALUE value)
223
+ VALUE get_at(VALUE self, VALUE index)
216
224
  {
217
225
  CHECK;
218
226
 
@@ -220,8 +228,7 @@ VALUE array_set(VALUE self, VALUE index, VALUE value)
220
228
  if (i < 0 || 2 < i)
221
229
  index_error(__FILE__, __LINE__);
222
230
 
223
- (*THIS)[i] = to<coord>(value);
224
- return value;
231
+ return value((*THIS)[i]);
225
232
  }
226
233
 
227
234
  static
@@ -231,6 +238,18 @@ VALUE inspect(VALUE self)
231
238
  return value(Xot::stringf("#<Rays::Point %s>", THIS->inspect().c_str()));
232
239
  }
233
240
 
241
+ static
242
+ VALUE dot(VALUE self, VALUE p1, VALUE p2)
243
+ {
244
+ return value(Rays::dot(to<Rays::Point>(p1), to<Rays::Point>(p2)));
245
+ }
246
+
247
+ static
248
+ VALUE cross(VALUE self, VALUE p1, VALUE p2)
249
+ {
250
+ return value(Rays::cross(to<Rays::Point>(p1), to<Rays::Point>(p2)));
251
+ }
252
+
234
253
 
235
254
  static Class cPoint;
236
255
 
@@ -245,6 +264,8 @@ Init_point ()
245
264
  rb_define_private_method(cPoint, "initialize_copy", RUBY_METHOD_FUNC(initialize_copy), 1);
246
265
  cPoint.define_method("move_to!", move_to);
247
266
  cPoint.define_method("move_by!", move_by);
267
+ rb_define_method(cPoint, "rotate", RUBY_METHOD_FUNC(rotate), 1);
268
+ cPoint.define_method("rotate!", rotate_self);
248
269
  rb_define_method(cPoint, "length", RUBY_METHOD_FUNC(length), 0);
249
270
  rb_define_method(cPoint, "normalize", RUBY_METHOD_FUNC(normalize), 0);
250
271
  rb_define_method(cPoint, "normal", RUBY_METHOD_FUNC(normal), 0);
@@ -254,13 +275,16 @@ Init_point ()
254
275
  rb_define_method(cPoint, "y", RUBY_METHOD_FUNC(get_y), 0);
255
276
  rb_define_method(cPoint, "z=", RUBY_METHOD_FUNC(set_z), 1);
256
277
  rb_define_method(cPoint, "z", RUBY_METHOD_FUNC(get_z), 0);
257
- rb_define_method(cPoint, "op_add", RUBY_METHOD_FUNC(add), 1);
258
- rb_define_method(cPoint, "op_sub", RUBY_METHOD_FUNC(sub), 1);
259
- rb_define_method(cPoint, "op_mult", RUBY_METHOD_FUNC(mult), 1);
260
- rb_define_method(cPoint, "op_div", RUBY_METHOD_FUNC(div), 1);
261
- cPoint.define_method("[]", array_get);
262
- cPoint.define_method("[]=", array_set);
278
+ cPoint.define_method("-@", negate);
279
+ cPoint.define_method("+", add);
280
+ cPoint.define_method("-", sub);
281
+ cPoint.define_method("*", mult);
282
+ cPoint.define_method("/", div);
283
+ cPoint.define_method("[]=", set_at);
284
+ cPoint.define_method("[]", get_at);
263
285
  rb_define_method(cPoint, "inspect", RUBY_METHOD_FUNC(inspect), 0);
286
+ rb_define_module_function(cPoint, "dot", RUBY_METHOD_FUNC(dot), 2);
287
+ rb_define_module_function(cPoint, "cross", RUBY_METHOD_FUNC(cross), 2);
264
288
  }
265
289
 
266
290
 
@@ -269,43 +293,38 @@ namespace Rucy
269
293
 
270
294
 
271
295
  template <> Rays::Point
272
- value_to<Rays::Point> (Value value, bool convert)
296
+ value_to<Rays::Point> (int argc, const Value* argv, bool convert)
273
297
  {
274
- if (convert)
298
+ if (argc == 1 && argv->is_array())
275
299
  {
276
- size_t argc = 0;
277
- Value* argv = NULL;
278
- if (value.is_array())
279
- {
280
- argc = value.size();
281
- argv = value.as_array();
282
- }
283
- else
284
- {
285
- argc = 1;
286
- argv = &value;
287
- }
300
+ argc = argv->size();
301
+ argv = argv->as_array();
302
+ }
288
303
 
289
- if (argc < 1)
290
- Rucy::argument_error(__FILE__, __LINE__);
304
+ assert(argc == 0 || (argc > 0 && argv));
291
305
 
292
- if (argv[0].is_kind_of(Rays::point_class()))
293
- value = argv[0];
294
- else if (argv[0].is_i() || argv[0].is_f())
306
+ if (convert)
307
+ {
308
+ if (argc == 0)
309
+ return Rays::Point();
310
+ else if (argv->is_num())
295
311
  {
296
312
  switch (argc)
297
313
  {
298
- #define V(i) argv[i].as_f(true)
314
+ #define V(i) to<coord>(argv[i])
299
315
  case 1: return Rays::Point(V(0));
300
316
  case 2: return Rays::Point(V(0), V(1));
301
317
  case 3: return Rays::Point(V(0), V(1), V(2));
302
318
  #undef V
303
- default: Rucy::argument_error(__FILE__, __LINE__);
319
+ default: argument_error(__FILE__, __LINE__);
304
320
  }
305
321
  }
306
322
  }
307
323
 
308
- return value_to<Rays::Point&>(value, convert);
324
+ if (argc != 1)
325
+ argument_error(__FILE__, __LINE__);
326
+
327
+ return value_to<Rays::Point&>(*argv, convert);
309
328
  }
310
329
 
311
330
 
@@ -0,0 +1,313 @@
1
+ #include "rays/ruby/polygon.h"
2
+
3
+
4
+ #include <vector>
5
+ #include <functional>
6
+ #include "rays/ruby/bounds.h"
7
+ #include "rays/ruby/polyline.h"
8
+ #include "defs.h"
9
+
10
+
11
+ RUCY_DEFINE_VALUE_OR_ARRAY_FROM_TO(Rays::Polygon)
12
+
13
+ #define THIS to<Rays::Polygon*>(self)
14
+
15
+ #define CHECK RUCY_CHECK_OBJECT(Rays::Polygon, self)
16
+
17
+
18
+ static
19
+ VALUE alloc(VALUE klass)
20
+ {
21
+ return new_type<Rays::Polygon>(klass);
22
+ }
23
+
24
+ static
25
+ VALUE setup(VALUE self, VALUE args, VALUE loop)
26
+ {
27
+ CHECK;
28
+
29
+ if (args[0].is_kind_of(Rays::polyline_class()))
30
+ *THIS = to<Rays::Polygon>(args.size(), args.as_array());
31
+ else
32
+ {
33
+ std::vector<Rays::Point> points;
34
+ get_line_args(&points, args.size(), args.as_array());
35
+ *THIS = Rays::Polygon(&points[0], points.size(), loop);
36
+ }
37
+ }
38
+
39
+ static
40
+ VALUE expand(VALUE self)
41
+ {
42
+ CHECK;
43
+ check_arg_count(__FILE__, __LINE__, "Polygon#expand", argc, 1, 2, 3, 4);
44
+
45
+ coord width = to<coord> (argv[0]);
46
+ Rays::CapType cap = argc >= 2 ? to<Rays::CapType> (argv[1]) : Rays::CAP_DEFAULT;
47
+ Rays::JoinType join = argc >= 3 ? to<Rays::JoinType>(argv[2]) : Rays::JOIN_DEFAULT;
48
+ coord ml = argc >= 4 ? to<coord> (argv[3]) : Rays::JOIN_DEFAULT_MITER_LIMIT;
49
+
50
+ Rays::Polygon polygon;
51
+ THIS->expand(&polygon, width, cap, join, ml);
52
+ return value(polygon);
53
+ }
54
+
55
+ static
56
+ VALUE bounds(VALUE self)
57
+ {
58
+ CHECK;
59
+ return value(THIS->bounds());
60
+ }
61
+
62
+ static
63
+ VALUE size(VALUE self)
64
+ {
65
+ CHECK;
66
+ return value(THIS->size());
67
+ }
68
+
69
+ static
70
+ VALUE empty(VALUE self)
71
+ {
72
+ CHECK;
73
+ return value(THIS->empty());
74
+ }
75
+
76
+ static
77
+ VALUE get_at(VALUE self, VALUE index)
78
+ {
79
+ CHECK;
80
+
81
+ int size = (int) THIS->size();
82
+ int i = to<int>(index);
83
+ if (i < 0) i += size;
84
+
85
+ if (i < 0 || size <= i)
86
+ index_error(__FILE__, __LINE__);
87
+
88
+ return value((*THIS)[i]);
89
+ }
90
+
91
+ static
92
+ VALUE each(VALUE self)
93
+ {
94
+ CHECK;
95
+
96
+ Value ret = Qnil;
97
+ for (const auto& line : *THIS)
98
+ ret = rb_yield(value(line));
99
+ return ret;
100
+ }
101
+
102
+ static void
103
+ each_polygon (const Value& value, std::function<void(const Rays::Polygon&)> fun)
104
+ {
105
+ int size = value.size();
106
+ const Value* array = value.as_array();
107
+
108
+ for (int i = 0; i < size; ++i)
109
+ fun(to<Rays::Polygon&>(array[i]));
110
+ }
111
+
112
+ static
113
+ VALUE op_sub(VALUE self, VALUE obj)
114
+ {
115
+ CHECK;
116
+
117
+ if (obj.is_array())
118
+ {
119
+ Rays::Polygon result = *THIS;
120
+ each_polygon(obj, [&](const Rays::Polygon& polygon)
121
+ {
122
+ result = result - polygon;
123
+ });
124
+ return value(result);
125
+ }
126
+ else
127
+ return value(*THIS - to<Rays::Polygon&>(obj));
128
+ }
129
+
130
+ static
131
+ VALUE op_and(VALUE self, VALUE obj)
132
+ {
133
+ CHECK;
134
+
135
+ if (obj.is_array())
136
+ {
137
+ Rays::Polygon result = *THIS;
138
+ each_polygon(obj, [&](const Rays::Polygon& polygon)
139
+ {
140
+ result = result & polygon;
141
+ });
142
+ return value(result);
143
+ }
144
+ else
145
+ return value(*THIS & to<Rays::Polygon&>(obj));
146
+ }
147
+
148
+ static
149
+ VALUE op_or(VALUE self, VALUE obj)
150
+ {
151
+ CHECK;
152
+
153
+ if (obj.is_array())
154
+ {
155
+ Rays::Polygon result = *THIS;
156
+ each_polygon(obj, [&](const Rays::Polygon& polygon)
157
+ {
158
+ result = result | polygon;
159
+ });
160
+ return value(result);
161
+ }
162
+ else
163
+ return value(*THIS | to<Rays::Polygon&>(obj));
164
+ }
165
+
166
+ static
167
+ VALUE op_xor(VALUE self, VALUE obj)
168
+ {
169
+ CHECK;
170
+
171
+ if (obj.is_array())
172
+ {
173
+ Rays::Polygon result = *THIS;
174
+ each_polygon(obj, [&](const Rays::Polygon& polygon)
175
+ {
176
+ result = result ^ polygon;
177
+ });
178
+ return value(result);
179
+ }
180
+ else
181
+ return value(*THIS ^ to<Rays::Polygon&>(obj));
182
+ }
183
+
184
+ static
185
+ VALUE create_rect(VALUE self, VALUE
186
+ args, VALUE round, VALUE lefttop, VALUE righttop, VALUE leftbottom, VALUE rightbottom, VALUE nsegment)
187
+ {
188
+ coord x, y, w, h, lt, rt, lb, rb;
189
+ uint nseg;
190
+ get_rect_args(
191
+ &x, &y, &w, &h, &lt, &rt, &lb, &rb, &nseg,
192
+ args.size(), args.as_array(),
193
+ round, lefttop, righttop, leftbottom, rightbottom, nsegment);
194
+
195
+ return value(Rays::create_rect(x, y, w, h, lt, rt, lb, rb, nseg));
196
+ }
197
+
198
+ static
199
+ VALUE create_ellipse(VALUE self, VALUE
200
+ args, VALUE center, VALUE radius, VALUE hole, VALUE angle_from, VALUE angle_to, VALUE nsegment)
201
+ {
202
+ coord x, y, w, h;
203
+ Rays::Point hole_size;
204
+ float from, to_;
205
+ uint nseg;
206
+ get_ellipse_args(
207
+ &x, &y, &w, &h, &hole_size, &from, &to_, &nseg,
208
+ args.size(), args.as_array(),
209
+ center, radius, hole, angle_from, angle_to, nsegment);
210
+
211
+ return value(Rays::create_ellipse(x, y, w, h, hole_size, from, to_, nseg));
212
+ }
213
+
214
+ static
215
+ VALUE create_curve(VALUE self, VALUE args, VALUE loop)
216
+ {
217
+ std::vector<Rays::Point> points;
218
+ get_line_args(&points, args.size(), args.as_array());
219
+
220
+ return value(Rays::create_curve(&points[0], points.size(), loop));
221
+ }
222
+
223
+ static
224
+ VALUE create_bezier(VALUE self, VALUE args, VALUE loop)
225
+ {
226
+ std::vector<Rays::Point> points;
227
+ get_line_args(&points, args.size(), args.as_array());
228
+
229
+ return value(Rays::create_bezier(&points[0], points.size(), loop));
230
+ }
231
+
232
+
233
+ static Class cPolygon;
234
+
235
+ void
236
+ Init_polygon ()
237
+ {
238
+ Module mRays = rb_define_module("Rays");
239
+
240
+ cPolygon = rb_define_class_under(mRays, "Polygon", rb_cObject);
241
+ rb_define_alloc_func(cPolygon, alloc);
242
+ rb_define_private_method(cPolygon, "setup", RUBY_METHOD_FUNC(setup), 2);
243
+ rb_define_method(cPolygon, "expand", RUBY_METHOD_FUNC(expand), -1);
244
+ rb_define_method(cPolygon, "bounds", RUBY_METHOD_FUNC(bounds), 0);
245
+ rb_define_method(cPolygon, "size", RUBY_METHOD_FUNC(size), 0);
246
+ cPolygon.define_method("empty?", empty);
247
+ cPolygon.define_method("[]", get_at);
248
+ rb_define_method(cPolygon, "each", RUBY_METHOD_FUNC(each), 0);
249
+ cPolygon.define_method("+", op_or);
250
+ cPolygon.define_method("-", op_sub);
251
+ cPolygon.define_method("&", op_and);
252
+ cPolygon.define_method("|", op_or);
253
+ cPolygon.define_method("^", op_xor);
254
+ rb_define_singleton_method(cPolygon, "create_rect", RUBY_METHOD_FUNC(create_rect), 7);
255
+ rb_define_singleton_method(cPolygon, "create_ellipse", RUBY_METHOD_FUNC(create_ellipse), 7);
256
+ rb_define_singleton_method(cPolygon, "create_curve", RUBY_METHOD_FUNC(create_curve), 2);
257
+ rb_define_singleton_method(cPolygon, "create_bezier", RUBY_METHOD_FUNC(create_bezier), 2);
258
+ }
259
+
260
+
261
+ namespace Rucy
262
+ {
263
+
264
+
265
+ template <> Rays::Polygon
266
+ value_to<Rays::Polygon> (int argc, const Value* argv, bool convert)
267
+ {
268
+ assert(argc == 0 || (argc > 0 && argv));
269
+
270
+ if (convert)
271
+ {
272
+ if (argc <= 0)
273
+ return Rays::Polygon();
274
+ else if (argv->is_kind_of(Rays::polygon_line_class()))
275
+ {
276
+ std::vector<Rays::Polygon::Line> lines;
277
+ lines.reserve(argc);
278
+ for (int i = 0; i < argc; ++i)
279
+ lines.emplace_back(to<Rays::Polygon::Line&>(argv[i]));
280
+ return Rays::Polygon(&lines[0], lines.size());
281
+ }
282
+ else if (argv->is_kind_of(Rays::polyline_class()))
283
+ return Rays::Polygon(to<Rays::Polyline&>(*argv));
284
+ else if (argv->is_num() || argv->is_array())
285
+ {
286
+ std::vector<Rays::Point> points;
287
+ get_line_args(&points, argc, argv);
288
+ return Rays::Polygon(&points[0], points.size());
289
+ }
290
+ }
291
+
292
+ if (argc != 1)
293
+ argument_error(__FILE__, __LINE__);
294
+
295
+ return value_to<Rays::Polygon&>(*argv, convert);
296
+ }
297
+
298
+
299
+ }// Rucy
300
+
301
+
302
+ namespace Rays
303
+ {
304
+
305
+
306
+ Class
307
+ polygon_class ()
308
+ {
309
+ return cPolygon;
310
+ }
311
+
312
+
313
+ }// Rays