rays 0.1.48 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/include/rays/image.h CHANGED
@@ -47,7 +47,7 @@ namespace Rays
47
47
 
48
48
  Painter painter ();
49
49
 
50
- Bitmap& bitmap ();
50
+ Bitmap& bitmap (bool modify = false);
51
51
 
52
52
  const Bitmap& bitmap () const;
53
53
 
@@ -57,6 +57,8 @@ namespace Rays
57
57
 
58
58
  This& reset (const coord* elements, size_t size);
59
59
 
60
+ This& transpose ();
61
+
60
62
  This& translate (coord x, coord y, coord z = 0);
61
63
 
62
64
  This& translate (const Coord3& translate);
@@ -92,6 +94,28 @@ namespace Rays
92
94
  };// Matrix
93
95
 
94
96
 
97
+ Matrix ortho (
98
+ coord left, coord right,
99
+ coord top, coord bottom);
100
+
101
+ Matrix ortho (
102
+ coord left, coord right,
103
+ coord top, coord bottom,
104
+ coord near, coord far);
105
+
106
+ Matrix perspective (float fov_y, float aspect_ratio, coord near, coord far);
107
+
108
+ Matrix look_at (
109
+ coord eye_x, coord eye_y, coord eye_z,
110
+ coord target_x, coord target_y, coord target_z,
111
+ coord up_x = 0, coord up_y = 1, coord up_z = 0);
112
+
113
+ Matrix look_at (
114
+ const Point& eye,
115
+ const Point& target,
116
+ const Point& up = Point(0, 1, 0));
117
+
118
+
95
119
  }// Rays
96
120
 
97
121
 
@@ -77,6 +77,12 @@ namespace Rays
77
77
  void polygon (
78
78
  const Polygon& polygon, const Bounds& bounds);
79
79
 
80
+ void point (coord x, coord y);
81
+
82
+ void point (const Point& point);
83
+
84
+ void points (const Point* points, size_t size);
85
+
80
86
  void line (coord x1, coord y1, coord x2, coord y2);
81
87
 
82
88
  void line (const Point& p1, const Point& p2);
@@ -235,6 +241,10 @@ namespace Rays
235
241
 
236
242
  uint nsegment () const;
237
243
 
244
+ void set_line_height (coord height);
245
+
246
+ coord line_height (bool raw = false) const;
247
+
238
248
  void set_blend_mode (BlendMode mode);
239
249
 
240
250
  BlendMode blend_mode () const;
@@ -191,29 +191,35 @@ namespace Rays
191
191
  Polygon create_curve (
192
192
  coord x1, coord y1, coord x2, coord y2,
193
193
  coord x3, coord y3, coord x4, coord y4,
194
- bool loop = false);
194
+ bool loop = false,
195
+ uint nsegment = 0);
195
196
 
196
197
  Polygon create_curve (
197
198
  const Point& p1, const Point& p2, const Point& p3, const Point& p4,
198
- bool loop = false);
199
+ bool loop = false,
200
+ uint nsegment = 0);
199
201
 
200
202
  Polygon create_curve (
201
203
  const Point* points, size_t size,
202
- bool loop = false);
204
+ bool loop = false,
205
+ uint nsegment = 0);
203
206
 
204
207
 
205
208
  Polygon create_bezier (
206
209
  coord x1, coord y1, coord x2, coord y2,
207
210
  coord x3, coord y3, coord x4, coord y4,
208
- bool loop = false);
211
+ bool loop = false,
212
+ uint nsegment = 0);
209
213
 
210
214
  Polygon create_bezier (
211
215
  const Point& p1, const Point& p2, const Point& p3, const Point& p4,
212
- bool loop = false);
216
+ bool loop = false,
217
+ uint nsegment = 0);
213
218
 
214
219
  Polygon create_bezier (
215
220
  const Point* points, size_t size,
216
- bool loop = false);
221
+ bool loop = false,
222
+ uint nsegment = 0);
217
223
 
218
224
 
219
225
  }// Rays
data/lib/rays/color.rb CHANGED
@@ -21,6 +21,8 @@ module Rays
21
21
  alias a= alpha=
22
22
  alias a alpha
23
23
 
24
+ alias to_hsb to_hsv
25
+
24
26
  def opaque?()
25
27
  alpha >= 1
26
28
  end
@@ -85,6 +87,10 @@ module Rays
85
87
  "#<#{self.class.name} #{to_a.join ' '}>"
86
88
  end
87
89
 
90
+ class << self
91
+ alias hsb hsv
92
+ end
93
+
88
94
  end# Color
89
95
 
90
96
 
data/lib/rays/image.rb CHANGED
@@ -9,7 +9,9 @@ module Rays
9
9
 
10
10
  extend Forwardable
11
11
 
12
- def_delegators :bitmap, :pixels=, :pixels, :[]=, :[]
12
+ def_delegators :bitmap, :pixels, :[]
13
+
14
+ def_delegators :bitmap_for_write, :pixels=, :[]=
13
15
 
14
16
  def paint(&block)
15
17
  painter.paint self, &block
@@ -24,6 +26,14 @@ module Rays
24
26
  Bounds.new 0, 0, width, height
25
27
  end
26
28
 
29
+ def bitmap(modify = false)
30
+ get_bitmap modify
31
+ end
32
+
33
+ private def bitmap_for_write()
34
+ get_bitmap true
35
+ end
36
+
27
37
  end# Image
28
38
 
29
39
 
data/lib/rays/matrix.rb CHANGED
@@ -9,6 +9,22 @@ module Rays
9
9
  include Comparable
10
10
  include Enumerable
11
11
 
12
+ def transpose()
13
+ dup.transpose!
14
+ end
15
+
16
+ def translate(*args)
17
+ dup.translate!(*args)
18
+ end
19
+
20
+ def scale(*args)
21
+ dup.scale!(*args)
22
+ end
23
+
24
+ def rotate(*args)
25
+ dup.rotate!(*args)
26
+ end
27
+
12
28
  def each(&block)
13
29
  to_a.each(&block)
14
30
  end
data/lib/rays/painter.rb CHANGED
@@ -57,26 +57,26 @@ module Rays
57
57
 
58
58
  def line(*args, loop: false)
59
59
  if args.first.kind_of?(Polyline)
60
- draw_polyline args.first
60
+ polyline! args.first
61
61
  else
62
- draw_line args, loop
62
+ line! args, loop
63
63
  end
64
64
  end
65
65
 
66
66
  def rect(*args, round: nil, lt: nil, rt: nil, lb: nil, rb: nil)
67
- draw_rect args, round, lt, rt, lb, rb
67
+ rect! args, round, lt, rt, lb, rb
68
68
  end
69
69
 
70
70
  def ellipse(*args, center: nil, radius: nil, hole: nil, from: nil, to: nil)
71
- draw_ellipse args, center, radius, hole, from, to
71
+ ellipse! args, center, radius, hole, from, to
72
72
  end
73
73
 
74
74
  def curve(*args, loop: false)
75
- draw_curve args, loop
75
+ curve! args, loop
76
76
  end
77
77
 
78
78
  def bezier(*args, loop: false)
79
- draw_bezier args, loop
79
+ bezier! args, loop
80
80
  end
81
81
 
82
82
  def color=(fill, stroke = nil)
@@ -130,8 +130,8 @@ module Rays
130
130
 
131
131
  universal_accessor :background, :fill, :stroke, :color,
132
132
  :stroke_width, :stroke_outset, :stroke_cap, :stroke_join, :miter_limit,
133
- :nsegment, :blend_mode, :texture, :texcoord_mode, :texcoord_wrap,
134
- :shader, :clip, :font
133
+ :nsegment, :line_height, :blend_mode, :clip, :font,
134
+ :texture, :texcoord_mode, :texcoord_wrap, :shader
135
135
 
136
136
  private
137
137
 
data/lib/rays/point.rb CHANGED
@@ -17,6 +17,10 @@ module Rays
17
17
  dup.move_by!(*args)
18
18
  end
19
19
 
20
+ def rotate(degree)
21
+ dup.rotate!(degree)
22
+ end
23
+
20
24
  def zero?()
21
25
  length == 0
22
26
  end
data/lib/rays/polygon.rb CHANGED
@@ -79,12 +79,12 @@ module Rays
79
79
  ellipse! args, center, radius, hole, from, to, nsegment
80
80
  end
81
81
 
82
- def self.curve(*points, loop: false)
83
- curve! points, loop
82
+ def self.curve(*points, loop: false, nsegment: nil)
83
+ curve! points, loop, nsegment
84
84
  end
85
85
 
86
- def self.bezier(*points, loop: false)
87
- bezier! points, loop
86
+ def self.bezier(*points, loop: false, nsegment: nil)
87
+ bezier! points, loop, nsegment
88
88
  end
89
89
 
90
90
  end# Polygon
data/rays.gemspec CHANGED
@@ -25,8 +25,8 @@ Gem::Specification.new do |s|
25
25
  s.platform = Gem::Platform::RUBY
26
26
  s.required_ruby_version = '>= 3.0.0'
27
27
 
28
- s.add_runtime_dependency 'xot', '~> 0.1.41'
29
- s.add_runtime_dependency 'rucy', '~> 0.1.43'
28
+ s.add_runtime_dependency 'xot', '~> 0.2'
29
+ s.add_runtime_dependency 'rucy', '~> 0.2'
30
30
 
31
31
  s.files = `git ls-files`.split $/
32
32
  s.executables = s.files.grep(%r{^bin/}) {|f| File.basename f}
data/src/color.cpp CHANGED
@@ -47,8 +47,17 @@ namespace Rays
47
47
  hue = fmod(hue, 1.f);
48
48
  if (hue < 0) hue += 1.f;
49
49
 
50
- auto c = glm::rgbColor(Vec3(hue * 360.f, saturation, value));
51
- return Color(c[0], c[1], c[2], alpha);
50
+ auto rgb = glm::rgbColor(Vec3(hue * 360.f, saturation, value));
51
+ return Color(rgb[0], rgb[1], rgb[2], alpha);
52
+ }
53
+
54
+ void
55
+ get_hsv (float* hue, float* saturation, float* value, const Color& color)
56
+ {
57
+ auto hsv = glm::hsvColor(Vec3(color.r, color.g, color.b));
58
+ if (hue) *hue = hsv[0] / 360.f;
59
+ if (saturation) *saturation = hsv[1];
60
+ if (value) *value = hsv[2];
52
61
  }
53
62
 
54
63
 
data/src/font.cpp CHANGED
@@ -58,21 +58,6 @@ namespace Rays
58
58
  return font.self->get_raw(pixel_density);
59
59
  }
60
60
 
61
- coord
62
- Font_get_width (const Font& font, float pixel_density, const char* str)
63
- {
64
- return Font_get_raw(font, pixel_density).get_width(str);
65
- }
66
-
67
- coord
68
- Font_get_height (
69
- const Font& font, float pixel_density,
70
- coord* ascent, coord* descent, coord* leading)
71
- {
72
- return Font_get_raw(font, pixel_density)
73
- .get_height(ascent, descent, leading);
74
- }
75
-
76
61
 
77
62
  Font::Font ()
78
63
  {
@@ -116,7 +101,19 @@ namespace Rays
116
101
  coord
117
102
  Font::get_width (const char* str) const
118
103
  {
119
- return self->rawfont.get_width(str);
104
+ if (!strchr(str, '\n'))
105
+ return self->rawfont.get_width(str);
106
+
107
+ Xot::StringList lines;
108
+ split(&lines, str);
109
+
110
+ coord width = 0;
111
+ for (const auto& line : lines)
112
+ {
113
+ coord w = self->rawfont.get_width(line.c_str());
114
+ if (w > width) width = w;
115
+ }
116
+ return width;
120
117
  }
121
118
 
122
119
  coord
data/src/font.h CHANGED
@@ -55,12 +55,6 @@ namespace Rays
55
55
 
56
56
  const RawFont& Font_get_raw (const Font& font, float pixel_density);
57
57
 
58
- coord Font_get_width (const Font& font, float pixel_density, const char* str);
59
-
60
- coord Font_get_height (
61
- const Font& font, float pixel_density,
62
- coord* ascent = NULL, coord* descent = NULL, coord* leading = NULL);
63
-
64
58
 
65
59
  RawFont RawFont_load (const char* path, coord size);
66
60
 
data/src/image.cpp CHANGED
@@ -10,6 +10,13 @@
10
10
  #include "texture.h"
11
11
 
12
12
 
13
+ #if 0
14
+ #define PRINT_MODIFIED_FLAGS(message) self->print_modified_flags(message)
15
+ #else
16
+ #define PRINT_MODIFIED_FLAGS(message)
17
+ #endif
18
+
19
+
13
20
  namespace Rays
14
21
  {
15
22
 
@@ -27,6 +34,16 @@ namespace Rays
27
34
 
28
35
  mutable Texture texture;
29
36
 
37
+ void print_modified_flags (const char* message)
38
+ {
39
+ printf("%s: %d %d %d %d \n",
40
+ message,
41
+ bitmap ? 1 : 0,
42
+ Bitmap_get_modified(bitmap) ? 1 : 0,
43
+ texture ? 1 : 0,
44
+ texture.modified() ? 1 : 0);
45
+ }
46
+
30
47
  };// Image::Data
31
48
 
32
49
 
@@ -82,18 +99,30 @@ namespace Rays
82
99
  if (!self->bitmap)
83
100
  {
84
101
  if (self->texture)
102
+ {
103
+ PRINT_MODIFIED_FLAGS("new bitmap from texture");
85
104
  self->bitmap = Bitmap_from(self->texture);
105
+ }
86
106
  else
107
+ {
108
+ PRINT_MODIFIED_FLAGS("new bitmap");
87
109
  self->bitmap = Bitmap(self->width, self->height, self->color_space);
110
+ }
88
111
  clear_modified_flags(image);
89
112
  }
90
- else if (
91
- self->texture &&
92
- self->texture.modified() &&
93
- !Bitmap_get_modified(self->bitmap))
113
+ else if (self->texture && self->texture.modified())
94
114
  {
95
- self->bitmap = Bitmap_from(self->texture);
96
- clear_modified_flags(image);
115
+ if (Bitmap_get_modified(self->bitmap))
116
+ {
117
+ invalid_state_error(
118
+ __FILE__, __LINE__, "bitmap and texture modifications conflicted");
119
+ }
120
+ else
121
+ {
122
+ PRINT_MODIFIED_FLAGS("bitmap from texture");
123
+ self->bitmap = Bitmap_from(self->texture);
124
+ clear_modified_flags(image);
125
+ }
97
126
  }
98
127
 
99
128
  return self->bitmap;
@@ -115,9 +144,13 @@ namespace Rays
115
144
  if (!self->texture)
116
145
  {
117
146
  if (self->bitmap)
147
+ {
148
+ PRINT_MODIFIED_FLAGS("new texture from bitmap");
118
149
  self->texture = Texture(self->bitmap);
150
+ }
119
151
  else
120
152
  {
153
+ PRINT_MODIFIED_FLAGS("new texture");
121
154
  self->texture = Texture(self->width, self->height, self->color_space);
122
155
 
123
156
  Painter p = image.painter();
@@ -127,13 +160,19 @@ namespace Rays
127
160
  }
128
161
  clear_modified_flags(&image);
129
162
  }
130
- else if (
131
- self->bitmap &&
132
- Bitmap_get_modified(self->bitmap) &&
133
- !self->texture.modified())
163
+ else if (self->bitmap && Bitmap_get_modified(self->bitmap))
134
164
  {
135
- self->texture = Texture(self->bitmap);
136
- clear_modified_flags(&image);
165
+ if (self->texture.modified())
166
+ {
167
+ invalid_state_error(
168
+ __FILE__, __LINE__, "texture and bitmap modifications conflicted");
169
+ }
170
+ else
171
+ {
172
+ PRINT_MODIFIED_FLAGS("texture from bitmap");
173
+ self->texture = self->bitmap;
174
+ clear_modified_flags(&image);
175
+ }
137
176
  }
138
177
 
139
178
  return self->texture;
@@ -232,15 +271,20 @@ namespace Rays
232
271
  }
233
272
 
234
273
  Bitmap&
235
- Image::bitmap ()
274
+ Image::bitmap (bool modify)
236
275
  {
276
+ if (modify)
277
+ {
278
+ if (!self->bitmap) get_bitmap(this);
279
+ Bitmap_set_modified(&self->bitmap);
280
+ }
237
281
  return get_bitmap(this);
238
282
  }
239
283
 
240
284
  const Bitmap&
241
285
  Image::bitmap () const
242
286
  {
243
- return get_bitmap(const_cast<Image*>(this));
287
+ return const_cast<Image*>(this)->bitmap();
244
288
  }
245
289
 
246
290
  Image::operator bool () const
data/src/ios/font.mm CHANGED
@@ -78,14 +78,12 @@ namespace Rays
78
78
  get_font_families ()
79
79
  {
80
80
  static const FontFamilyMap MAP = []() {
81
- NSFontManager* fm = NSFontManager.sharedFontManager;
82
-
83
81
  FontFamilyMap map;
84
- for (NSString* family in fm.availableFontFamilies)
82
+ for (NSString* family in UIFont.familyNames)
85
83
  {
86
84
  FontFamilyMap::mapped_type array;
87
- for (NSArray<NSString*>* members in [fm availableMembersOfFontFamily: family])
88
- array.emplace_back(members[0].UTF8String);
85
+ for (NSString* name in [UIFont fontNamesForFamilyName: family])
86
+ array.emplace_back(name.UTF8String);
89
87
  map[family.UTF8String] = array;
90
88
  }
91
89
  return map;
@@ -163,11 +161,9 @@ namespace Rays
163
161
  if (!line)
164
162
  rays_error(__FILE__, __LINE__, "creating CTLineRef failed.");
165
163
 
166
- coord width = 0, height = 0, ascent = 0;
167
- width = get_width(str);
168
- height = get_height(&ascent);
169
-
170
- height = ceil(height);
164
+ coord width, height, ascent = 0;
165
+ width = ceil(get_width(str));
166
+ height = ceil(get_height(&ascent));
171
167
  ascent = floor(ascent);
172
168
 
173
169
  CGRect rect = CGRectMake(x, context_height - height - y, width, height);
data/src/ios/helper.h CHANGED
@@ -15,9 +15,9 @@ namespace Rays
15
15
  void safe_cfrelease (CFTypeRef ref);
16
16
 
17
17
 
18
- typedef std::shared_ptr<const __CFString> CFString;
18
+ typedef std::shared_ptr<const __CFString> CFStringPtr;
19
19
 
20
- CFString cfstring (const char* str);
20
+ CFStringPtr cfstring (const char* str);
21
21
 
22
22
 
23
23
  }// Rays
data/src/ios/helper.mm CHANGED
@@ -13,12 +13,12 @@ namespace Rays
13
13
  }
14
14
 
15
15
 
16
- CFString
16
+ CFStringPtr
17
17
  cfstring (const char* str)
18
18
  {
19
19
  CFStringRef ref = CFStringCreateWithCString(
20
20
  kCFAllocatorDefault, str, kCFStringEncodingUTF8);
21
- return CFString(ref, safe_cfrelease);
21
+ return CFStringPtr(ref, safe_cfrelease);
22
22
  }
23
23
 
24
24
 
data/src/matrix.cpp CHANGED
@@ -13,6 +13,44 @@ namespace Rays
13
13
  {
14
14
 
15
15
 
16
+ Matrix
17
+ ortho (coord left, coord right, coord top, coord bottom)
18
+ {
19
+ return to_rays(glm::ortho(left, right, bottom, top));
20
+ }
21
+
22
+ Matrix
23
+ ortho (coord left, coord right, coord top, coord bottom, coord near, coord far)
24
+ {
25
+ return to_rays(glm::ortho(left, right, bottom, top, near, far));
26
+ }
27
+
28
+ Matrix
29
+ perspective (float fov_y, float aspect_ratio, coord near, coord far)
30
+ {
31
+ return to_rays(glm::perspective(
32
+ (float) Xot::deg2rad(fov_y), aspect_ratio, near, far));
33
+ }
34
+
35
+ Matrix
36
+ look_at (
37
+ coord eye_x, coord eye_y, coord eye_z,
38
+ coord target_x, coord target_y, coord target_z,
39
+ coord up_x, coord up_y, coord up_z)
40
+ {
41
+ return to_rays(glm::lookAt(
42
+ Vec3( eye_x, eye_y, eye_z),
43
+ Vec3(target_x, target_y, target_z),
44
+ Vec3( up_x, up_y, up_z)));
45
+ }
46
+
47
+ Matrix
48
+ look_at (const Point& eye, const Point& target, const Point& up)
49
+ {
50
+ return to_rays(glm::lookAt(to_glm(eye), to_glm(target), to_glm(up)));
51
+ }
52
+
53
+
16
54
  Matrix::Matrix (coord value)
17
55
  {
18
56
  reset(value);
@@ -84,6 +122,13 @@ namespace Rays
84
122
  return *this;
85
123
  }
86
124
 
125
+ Matrix&
126
+ Matrix::transpose ()
127
+ {
128
+ to_glm(*this) = glm::transpose(to_glm(*this));
129
+ return *this;
130
+ }
131
+
87
132
  Matrix&
88
133
  Matrix::translate (coord x, coord y, coord z)
89
134
  {
data/src/osx/font.mm CHANGED
@@ -163,11 +163,9 @@ namespace Rays
163
163
  if (!line)
164
164
  rays_error(__FILE__, __LINE__, "creating CTLineRef failed.");
165
165
 
166
- coord width = 0, height = 0, ascent = 0;
167
- width = get_width(str);
168
- height = get_height(&ascent);
169
-
170
- height = ceil(height);
166
+ coord width, height, ascent = 0;
167
+ width = ceil(get_width(str));
168
+ height = ceil(get_height(&ascent));
171
169
  ascent = floor(ascent);
172
170
 
173
171
  CGRect rect = CGRectMake(x, context_height - height - y, width, height);