ruby-processing 2.4.2 → 2.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,323 +1,37 @@
1
- EPSILON = 9.999999747378752e-05 # a value used by processing.org
2
-
3
- class Vec
4
- attr_accessor :x, :y, :z
5
-
6
- def initialize(x = 0 ,y = 0, z = 0)
7
- @x, @y, @z = x, y, z
8
- post_initialize
9
- end
10
-
11
- def post_initialize
12
- nil
13
- end
14
-
15
- def ==(vec)
16
- (x - vec.x).abs < EPSILON && (y - vec.y).abs < EPSILON && (z - vec.z).abs < EPSILON
17
- end
18
-
19
- def to_a
20
- [x, y, z]
21
- end
22
-
23
- def self.dist_squared(vec_a, vec_b)
24
- (vec_a.x - vec_b.x)**2 + (vec_a.y - vec_b.y)**2 + (vec_a.z - vec_b.z)**2
25
- end
26
-
27
- def self.dist(vec_a, vec_b)
28
- Math.sqrt((vec_a.x - vec_b.x)**2 + (vec_a.y - vec_b.y)**2 + (vec_a.z - vec_b.z)**2)
29
- end
30
-
31
- def to_s
32
- "#{self.class}(Use Vec2D, Vec3D or make your own subclass)"
33
- end
34
-
35
- def inspect
36
- self.to_s
37
- end
38
- end
39
-
40
-
41
- class Vec2D < Vec
42
-
43
- def to_a
44
- [x, y]
45
- end
46
-
47
- def self.from_angle scalar
48
- Vec2D.new(Math.cos(scalar), Math.sin(scalar))
49
- end
50
-
51
- def modulus
52
- Math.hypot(x, y)
53
- end
54
-
55
- def mag_squared
56
- x**2 + y**2
57
- end
58
-
59
- # vanilla processing PVector returns a Vector, rather than Scalar (defaults to 3D result with z = 0)
60
- def cross(vec)
61
- x * vec.y - y * vec.x
62
- end
63
-
64
- # Scalar product, also known as inner product or dot product
65
- def dot(vec)
66
- x * vec.x + y * vec.y
67
- end
68
-
69
- def collinear_with?(vec)
70
- cross(vec).abs < EPSILON
71
- end
72
-
73
- def +(vec)
74
- Vec2D.new(x + vec.x, y + vec.y)
75
- end
76
-
77
- def -(vec)
78
- Vec2D.new(x - vec.x, y - vec.y)
79
- end
80
-
81
- def *(scalar)
82
- Vec2D.new(x * scalar, y * scalar)
83
- end
84
-
85
- def / (scalar)
86
- Vec2D.new(x / scalar, y / scalar) unless scalar == 0
87
- end
88
-
89
- # @param [Vec2D] vec
90
- # The target vector
91
- # @param [Float] scalar
92
- # @return [Vec2D]
93
- # A new Vec2D on the way to the target (all the way if scalar not in 0 .. 1.0)
94
-
95
- def lerp(vec, scalar)
96
- if (0 .. 1.0).include? scalar
97
- x0, y0 = x * scalar + vec.x * (1 - scalar), y * scalar + vec.y * (1 - scalar)
98
- else
99
- x0, y0 = vec.x, vec.y # you will get there rather quicker than you hoped
100
- end
101
- Vec2D.new(x0, y0)
102
- end
103
-
104
- # Change the values of current Vec2D toward a target
105
- # @param [Vec2D] vec
106
- # The target vector
107
- # @param [Float] scalar
108
- # @return [Vec2D]
109
- # self Vec2D on the way to the target (unchanged if scalar not in 0 .. 1.0)
110
-
111
-
112
- def lerp!(vec, scalar)
113
- @x, @y = x * scalar + vec.x * (1 - scalar), y * scalar + vec.y * (1 - scalar) if (0 .. 1.0).include? scalar
114
- return self
115
- end
116
-
117
- def heading
118
- Math.atan2(-y, x) * -1.0
119
- end
120
-
121
- def normalize!
122
- magnitude = Math.hypot(x, y)
123
- @x, @y = x / magnitude, y / magnitude
124
- return self
125
- end
126
-
127
- def set_mag(scalar)
128
- magnitude = Math.hypot(x, y)
129
- @x, @y = (x * scalar) / magnitude, (y * scalar) / magnitude
130
- return self
131
- end
132
-
133
- def to_s
134
- "#{self.class}(x=#{x}, y=#{y})"
135
- end
136
-
137
- alias :mag :modulus
138
-
139
- end
140
-
141
- class Vec3D < Vec
142
-
143
- def modulus
144
- Math.sqrt(x**2 + y**2 + z**2)
145
- end
146
-
147
- def cross(vec)
148
- xc = y * vec.z - z * vec.y
149
- yc = z * vec.x - x * vec.z
150
- zc = x * vec.y - y * vec.x
151
- Vec3D.new(xc, yc, zc)
152
- end
153
-
154
- # Scalar product, also known as inner product or dot product
155
- def dot(vec)
156
- x * vec.x + y * vec.y + z * vec.z
157
- end
158
-
159
- def collinear_with?(vec)
160
- cross(vec) == Vec3D.new
161
- end
162
-
163
- def to_a
164
- [x, y, z]
165
- end
166
-
167
- def +(vec)
168
- Vec3D.new(x + vec.x, y + vec.y, z + vec.z)
169
- end
170
-
171
- def -(vec)
172
- Vec3D.new(x - vec.x, y - vec.y, z - vec.z)
173
- end
174
-
175
- def * (scalar)
176
- Vec3D.new(x * scalar, y * scalar, z * scalar)
177
- end
178
-
179
- def / (scalar)
180
- Vec3D.new(x / scalar, y / scalar, z / scalar) unless scalar.abs < EPSILON
181
- end
182
-
183
- def normalize!
184
- magnitude = Math.sqrt(x**2 + y**2 + z**2)
185
- @x, @y, @z = x / magnitude, y / magnitude, z / magnitude
186
- return self
187
- end
188
-
189
- def set_mag(scalar)
190
- magnitude = Math.sqrt(x**2 + y**2 + z**2)
191
- @x, @y, @z = (x * scalar) / magnitude, (y * scalar) / magnitude, (z * scalar) / magnitude
192
- return self
193
- end
194
-
195
- def to_s
196
- "#{self.class}(x=#{x}, y=#{y}, z=#{z})"
197
- end
198
-
199
- alias :mag :modulus
200
-
201
- end
202
-
203
-
204
- class Quaternion
205
-
206
- attr_reader :w, :x, :y, :z
207
-
208
- def initialize(w = 1.0, x = 0, y = 0, z = 0)
209
- @w, @x, @y, @z = w, x, y, z
210
- end
211
-
212
- def ==(quat)
213
- (w - quat.w).abs < EPSILON && (x - quat.x).abs < EPSILON && (y - quat.y).abs < EPSILON && (z - quat.z).abs < EPSILON
214
- end
215
-
216
-
217
- def reset
218
- @w = 1.0
219
- @x = 0.0
220
- @y = 0.0
221
- @z = 0.0
222
- end
223
-
224
- def set(w, v)
225
- @w, @x, @y, @z = w, v.x, v.y, v.z
226
- end
227
-
228
- def copy(q)
229
- @w, @x, @y, @z = q.w, q.x, q.y, q.z
230
- end
231
-
232
- def self.mult(q1, q2) # class method
233
- x0 = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y
234
- y0 = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z
235
- z0 = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x
236
- w0 = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z
237
- Quaternion.new(w0, x0, y0, z0)
238
- end
239
-
240
- def * (q1) # instance method
241
- x0 = w * q1.x + x * q1.w + y * q1.z - z * q1.y
242
- y0 = w * q1.y + y * q1.w + z * q1.x - x * q1.z
243
- z0 = w * q1.z + z * q1.w + x * q1.y - y * q1.x
244
- w0 = w * q1.w - x * q1.x - y * q1.y - z * q1.z
245
- Quaternion.new(w0, x0, y0, z0)
246
- end
247
-
248
- def get_value
249
- sa = Math.sqrt(1.0 - w * w)
250
- sa = 1.0 unless (sa >= EPSILON)
251
- [Math.acos(w) * 2, x / sa, y / sa, z / sa]
252
- end
253
-
254
- def to_s
255
- "#{self.class}(w=#{w}, x=#{x}, y=#{y}, z=#{z})"
256
- end
257
-
258
- def inspect
259
- self.to_s
260
- end
261
- end
262
-
263
- class ArcBall
264
- attr_reader :center_x, :center_y, :v_down, :v_drag, :q_now, :q_drag, :q_down, :axis, :axis_set, :radius
265
-
266
- def initialize(cx, cy, radius)
267
- @center_x = cx
268
- @center_y = cy
269
- @radius = radius
270
- @v_down = Vec3D.new
271
- @v_drag = Vec3D.new
272
- @q_now = Quaternion.new
273
- @q_down = Quaternion.new
274
- @q_drag = Quaternion.new
275
- @axis_set = [Vec3D.new(1.0, 0.0, 0.0), Vec3D.new(0.0, 1.0, 0.0), Vec3D.new(0.0, 0.0, 1.0)]
276
- @axis = -1
277
- end
278
-
279
- def select_axis(axis)
280
- @axis = axis
281
- end
282
-
283
- def mouse2sphere(x, y)
284
- v = Vec3D.new((x - center_x) / radius, (y - center_y) / radius, 0)
285
- mag = v.mag
286
- if (mag > 1.0)
287
- v.normalize!
288
- else
289
- v.z = Math.sqrt(1.0 - mag)
290
- end
291
- v = constrain(v, axis_set[axis]) unless (axis == -1)
292
- return v
293
- end
294
-
295
- def mouse_pressed(x, y)
296
- @v_down = mouse2sphere(x, y)
297
- @q_down.copy(q_now)
298
- @q_drag.reset
299
- end
300
-
301
- def mouse_dragged(x, y)
302
- @v_drag = mouse2sphere(x, y)
303
- @q_drag.set(v_down.dot(v_drag), v_down.cross(v_drag))
304
- end
305
-
306
-
307
- def constrain(vector, axis)
308
- res = vector - (axis * axis.dot(vector))
309
- res.normalize!
310
- end
311
-
312
- def update
313
- @q_now = q_drag * q_down
314
- quat2matrix(q_now)
315
- end
316
-
317
- def quat2matrix(q)
318
- q.get_value
319
- end
320
- end
321
-
322
-
323
-
1
+ # vecmath.rb
2
+ # The vecmath library provides Vec2D and Vec3D classes which can be
3
+ # use in place of processing PVector, and provide a more rubylike interface.
4
+ # Also included in the vecmath library is the ArcBall utility after:-
5
+ # Ken Shoemake. Computer Graphics Laboratory. University of Pennsylvania. Philadelphia, PA
6
+ # ==== Example Arcball usage see vecmath library in samples
7
+ # def setup
8
+ # .....
9
+ # camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), 0, 0, 0, 0, 1, 0)
10
+ # @arcball = ArcBall.new(0, 0, min(width - 20, height - 20) * 0.8)
11
+ # .....
12
+ # end
13
+ #
14
+ # def draw
15
+ # .....
16
+ # update
17
+ # some_render_code
18
+ # .....
19
+ # end
20
+ #
21
+ # def update
22
+ # theta, x, y, z = arcball.update
23
+ # rotate(theta, x, y, z)
24
+ # end
25
+ #
26
+ # def mouse_pressed
27
+ # arcball.mouse_pressed(mouse_x, mouse_y)
28
+ # end
29
+ #
30
+ # def mouse_dragged
31
+ # arcball.mouse_dragged(mouse_x, mouse_y)
32
+ # end
33
+
34
+
35
+ require_relative 'lib/vec'
36
+ require_relative 'lib/quaternion'
37
+ require_relative 'lib/arcball'
data/samples/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ desc 'run contributed samples'
4
+ task :default => [:contributed]
5
+
6
+ desc 'run contributed samples'
7
+ task :contributed do
8
+ sh "cd contributed && rake"
9
+ end
10
+
11
+ desc 'shaders'
12
+ task :shaders do
13
+ sh "cd processing_app/topics/shaders && rake"
14
+ end
15
+
16
+ desc 'vecmath'
17
+ task :vecmath do
18
+ sh "cd processing_app/library/vecmath && rake"
19
+ end
@@ -0,0 +1,30 @@
1
+ # Simple demo Rakefile to autorun samples in current directory
2
+ # adjust path to rp5 executable, and or opts as required
3
+
4
+ SAMPLES_DIR="./"
5
+
6
+ desc 'run demo'
7
+ task :default => [:demo]
8
+
9
+ desc 'demo'
10
+ task :demo do
11
+ samples_list.shuffle.each{|sample| run_sample sample}
12
+ end
13
+
14
+ def samples_list
15
+ files = []
16
+ Dir.chdir(SAMPLES_DIR)
17
+ Dir.glob("*.rb").each do |file|
18
+ files << File.join(SAMPLES_DIR, file)
19
+ end
20
+ return files
21
+ end
22
+
23
+ def run_sample(sample_name)
24
+ puts "Running #{sample_name}...quit to run next sample"
25
+ open("|rp5 run #{sample_name}", "r") do |io|
26
+ while l = io.gets
27
+ puts(l.chop)
28
+ end
29
+ end
30
+ end
@@ -3,12 +3,13 @@
3
3
  # You can print out the parametric equations for t = 0..1
4
4
  module Olap
5
5
  def self.overlaps(x, y, point_x, point_y)
6
- Math.sqrt((x-point_x)**2 + (y-point_y)**2) < RADIUS
6
+ (x-point_x)**2 + (y-point_y)**2 < RADIUS * RADIUS
7
7
  end
8
8
  end
9
9
 
10
10
  class Curve
11
11
  include Olap
12
+ include Processing::Proxy
12
13
  attr_accessor :x1, :y1, :c1x, :c1y, :c2x, :c2y, :x2, :y2
13
14
 
14
15
  def initialize
@@ -38,9 +39,9 @@ class Curve
38
39
 
39
40
 
40
41
  def draw
41
- $app.bezier *all_points
42
- $app.oval @x1, @y1, 3, 3
43
- $app.oval @x2, @y2, 3, 3
42
+ bezier *all_points
43
+ oval @x1, @y1, 3, 3
44
+ oval @x2, @y2, 3, 3
44
45
  end
45
46
 
46
47
 
@@ -66,9 +67,12 @@ RADIUS = 7
66
67
  load_library :control_panel
67
68
  include Olap
68
69
 
70
+ attr_reader :hide
71
+
69
72
  def setup
70
73
  size 300, 300
71
- @curves = []
74
+ @curves = []
75
+ @hide = false
72
76
  control_panel do |c|
73
77
  c.look_feel "Nimbus"
74
78
  c.button :new_curve
@@ -146,6 +150,7 @@ end
146
150
 
147
151
  def mouse_released
148
152
  @control, @end_point = nil, nil
153
+ @hide = false
149
154
  end
150
155
 
151
156
 
@@ -225,7 +230,10 @@ end
225
230
 
226
231
 
227
232
  def draw
228
- panel.visible = self.visible
233
+ if (!hide)
234
+ panel.visible = self.visible
235
+ @hide = true
236
+ end
229
237
  background 50
230
238
  draw_control_tangent_lines
231
239
  draw_curves