Skip to content

Commit e1ab130

Browse files
author
monkstone
committed
update to toxiclibs gem
1 parent 9c479f9 commit e1ab130

File tree

11 files changed

+263
-138
lines changed

11 files changed

+263
-138
lines changed

samples/external_library/java_processing/toxiclibs/gray_scott_tone_map.rb

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
### Using Toxiclibs jars in JRubyArt
2+
3+
Here we demonstrate how to use Karsten Schmidts (aka toxi, @postspectacular) toxiclibs jars in ruby-processing. Using the [toxiclibs gem][]...
4+
NB: need to use the --nojruby flag when running gray_scott_image.rb (ie use jruby-complete see wiki for why?)
5+
6+
### Web Links
7+
8+
[Post Spectacular Home][]
9+
10+
[Toxiclibs Clone][]
11+
12+
[Toxiclibs Documentation][]
13+
14+
### Licensing
15+
16+
I should be clear that the original toxiclibs is the work of Karsten Schmidt:-
17+
18+
Copyright (c) 2010 Karsten Schmidt
19+
20+
This demo & library is free software you can redistribute it and/or
21+
modify it under the terms of the GNU Lesser General Public
22+
License as published by the Free Software Foundation either
23+
version 2.1 of the License, or (at your option) any later version.
24+
25+
26+
[Post Spectacular Home]:http://postspectacular.com/
27+
[Toxiclibs Clone]:https://github.com/ruby-processing/toxiclibs
28+
[Toxiclibs Documentation]:http://toxiclibs.org/
29+
[toxiclibs gem]:https://github.com/ruby-processing/toxicgem
30+

samples/external_library/java_processing/toxiclibs/gray_scott_image.rb renamed to samples/external_library/ruby_gem/toxiclibs/gray_scott_image.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#
1+
require 'toxiclibs'
2+
23
# <p>GrayScottImage uses the seedImage() method to use a bitmap as simulation seed.
34
# In this demo the image is re-applied every frame and the user can adjust the
45
# F coefficient of the reaction diffusion to produce different patterns emerging
@@ -33,20 +34,12 @@
3334
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3435
#
3536

36-
load_libraries 'simutils','toxiclibscore','colorutils'
37-
38-
module Toxi
39-
include_package "toxi.sim.grayscott"
40-
include_package "toxi.math"
41-
include_package "toxi.color"
42-
end
43-
4437
attr_reader :gs, :tone_map, :img
4538

4639
def setup
47-
size 256, 256
48-
@gs = Toxi::GrayScott.new width, height, true
49-
@img = load_image "ti_yong.png"
40+
size(256, 256, P2D)
41+
@gs = Simulation::GrayScott.new width, height, true
42+
@img = load_image 'ti_yong.png'
5043
# create a duo-tone gradient map with 256 steps
5144
# NB: use '::' in place of '.' here for these java constants
5245
@tone_map = Toxi::ToneMap.new(0, 0.33, Toxi::NamedColor::CRIMSON, Toxi::NamedColor::WHITE, 256)
@@ -59,21 +52,21 @@ def draw
5952
10.times { @gs.update(1) }
6053
# read out the V result array
6154
# and use tone map to render colours
62-
gs.v.length.times do |i|
63-
pixels[i]=tone_map.getARGBToneFor(gs.v[i]) # NB: don't camel case convert here
55+
@gs.v.each_with_index do |v, i|
56+
pixels[i] = tone_map.getARGBToneFor(v) # NB: don't camel case convert here
6457
end
6558
update_pixels
6659
end
6760

6861
def key_pressed
62+
control_key = %w(1 2 3 4 5 6 7 8 9)
6963
case key
70-
when '1', '9', '3', '2', '4', '6', '5', '7', '8'
64+
when *control_key
7165
@gs.setF(0.02 + (key.ord - 48) * 0.001)
7266
when 's'
73-
save_frame "toxi.png"
67+
save_frame 'toxi.png'
7468
else
7569
@gs.reset()
7670
end
7771
end
7872

79-
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'toxiclibs'
2+
3+
#
4+
# <p>GrayScottToneMap shows how to use the ColorGradient & ToneMap classes of the
5+
# colorutils package to create a tone map for rendering the results of
6+
# the Gray-Scott reaction-diffusion.</p>
7+
#
8+
# <p><strong>Usage:</strong><ul>
9+
# <li>click + drag mouse to draw dots used as simulation seed</li>
10+
# <li>press any key to reset</li>
11+
# </ul></p>
12+
#
13+
# Copyright (c) 2010 Karsten Schmidt, ruby-processing Version (c) 2015 Martin Prout
14+
15+
# This demo & library is free software you can redistribute it and/or
16+
# modify it under the terms of the GNU Lesser General Public
17+
# License as published by the Free Software Foundation either
18+
# version 2.1 of the License, or (at your option) any later version.
19+
#
20+
# http://creativecommons.org/licenses/LGPL/2.1/
21+
#
22+
# This library is distributed in the hope that it will be useful,
23+
# but WITHOUT ANY WARRANTY without even the implied warranty of
24+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25+
# Lesser General Public License for more details.
26+
#
27+
# You should have received a copy of the GNU Lesser General Public
28+
# License along with this library if not, write to the Free Software
29+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30+
#
31+
32+
NUM_ITERATIONS = 10
33+
attr_reader :gs, :tone_map
34+
35+
def setup
36+
size(256,256, P2D)
37+
@gs = Simulation::GrayScott.new width, height, false
38+
@gs.set_coefficients 0.021, 0.076, 0.12, 0.06
39+
# create a color gradient for 256 values
40+
grad = Toxi::ColorGradient.new
41+
# NamedColors are preset colors, but any TColor can be added
42+
# see javadocs for list of names:
43+
# http://toxiclibs.org/docs/colorutils/toxi/color/NamedColor::html
44+
# NB: use '::' in place of '.' here for these java constants
45+
grad.add_color_at(0, Toxi::NamedColor::BLACK)
46+
grad.add_color_at(128, Toxi::NamedColor::RED)
47+
grad.add_color_at(192, Toxi::NamedColor::YELLOW)
48+
grad.add_color_at(255, Toxi::NamedColor::WHITE)
49+
# this gradient is used to map simulation values to colors
50+
# the first 2 parameters define the min/max values of the
51+
# input range (Gray-Scott produces values in the interval of 0.0 - 0.5)
52+
# setting the max = 0.33 increases the contrast
53+
@tone_map = Toxi::ToneMap.new 0, 0.33, grad
54+
end
55+
56+
def draw
57+
@gs.set_rect(mouse_x, mouse_y, 20, 20) if mouse_pressed?
58+
load_pixels
59+
# update the simulation a few time steps
60+
NUM_ITERATIONS.times { @gs.update(1) }
61+
# read out the V result array
62+
# and use tone map to render colours
63+
gs.v.each_with_index do |v, i|
64+
pixels[i] = tone_map.getARGBToneFor(v) # NB: don't camel case convert here
65+
end
66+
update_pixels
67+
end
68+
69+
def key_pressed
70+
@gs.reset
71+
end

samples/external_library/java_processing/toxiclibs/implicit.rb renamed to samples/external_library/ruby_gem/toxiclibs/implicit.rb

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
#
3-
# This example implements a custom VolumetricSpace using an implicit function
3+
# This example implements a custom VolumetricSpace uMath.sing an implicit function
44
# to calculate each voxel. This is slower than the default array or HashMap
55
# based implementations, but also has much less memory requirements and so might
66
# be an interesting and more viable approach for very highres voxel spaces
@@ -9,15 +9,15 @@
99
# acting as lower threshold when computing the iso surface)
1010
#
1111
# Usage:
12-
# move mouse to rotate camera
13-
# -/=: zoom in/out
12+
# drag mouse to rotate camera
13+
# mouse wheel zoom in/out
1414
# l: apply laplacian mesh smooth
1515
#
1616
#
1717

1818
#
19-
# Copyright (c) 2010 Karsten Schmidt & ruby-processing version Martin Prout 2013
20-
# This sketch relies on a custom ruby-processing mesh_to_vbo library
19+
# Copyright (c) 2010 Karsten Schmidt & ruby-procesMath.sing version Martin Prout 2013
20+
# This sketch relies on a custom ruby-procesMath.sing mesh_to_vbo library
2121
#
2222
# This library is free software you can redistribute it and/or
2323
# modify it under the terms of the GNU Lesser General Public
@@ -36,14 +36,9 @@
3636
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3737
#
3838

39-
load_libraries 'toxiclibscore', 'vbo', 'volumeutils'
39+
require 'toxiclibs'
4040

41-
module Toxi
42-
include_package 'toxi.geom'
43-
include_package 'toxi.geom.mesh'
44-
include_package 'toxi.volume'
45-
include_package 'toxi.processing'
46-
end
41+
load_library :mesh_to_vbo
4742

4843
RES = 64
4944
ISO = 0.2
@@ -53,10 +48,11 @@ module Toxi
5348

5449
def setup
5550
size(720,720, P3D)
51+
Processing::ArcBall.init(self)
5652
@vbo = MeshToVBO.new(self)
5753
@curr_zoom = 1
58-
vol = EvaluatingVolume.new(Toxi::Vec3D.new(400,400,400), RES, RES, RES, MAX_ISO)
59-
surface = Toxi::HashIsoSurface.new(vol)
54+
vol = EvaluatingVolume.new(TVec3D.new(400,400,400), RES, RES, RES, MAX_ISO)
55+
surface = Volume::HashIsoSurface.new(vol)
6056
@mesh = Toxi::WETriangleMesh.new
6157
surface.compute_surface_mesh(mesh, ISO)
6258
@is_wire_frame = false
@@ -72,19 +68,11 @@ def draw
7268
background(0)
7369
lights
7470
define_lights
75-
translate(width / 2.0, height / 2.0, 0)
76-
rotate_x(mouse_y * -0.01)
77-
rotate_y(mouse_x * -0.01)
78-
scale(curr_zoom)
7971
shape(implicit)
8072
end
8173

8274
def key_pressed
8375
case key
84-
when '-'
85-
@curr_zoom -= 0.1
86-
when '='
87-
@curr_zoom += 0.1
8876
when 'l', 'L'
8977
Toxi::LaplacianSmooth.new.filter(mesh, 1)
9078
@implicit = vbo.meshToVBO(mesh, true)
@@ -105,17 +93,14 @@ def define_lights
10593
spot_light(30, 30, 30, 0, 40, 200, 0, -0.5, -0.5, PI / 2, 2)
10694
end
10795

108-
class EvaluatingVolume < Toxi::VolumetricSpace
109-
include Processing::Proxy
110-
java_import 'toxi.math.SinCosLUT'
96+
class EvaluatingVolume < Volume::VolumetricSpace
11197

11298
attr_reader :upper_bound, :lut
113-
FREQ = PI * 3.8
99+
FREQ = Math::PI * 3.8
114100

115101
def initialize(scal_vec, resX, resY, resZ, upper_limit)
116102
super(scal_vec, resX, resY, resZ)
117103
@upper_bound = upper_limit
118-
@lut=SinCosLUT.new
119104
end
120105

121106
def clear
@@ -132,8 +117,8 @@ def getVoxel(x, y, z) # can't overload so we renamed
132117
xx = x * 1.0 / resX - 0.5 # NB: careful about integer division !!!
133118
yy = y * 1.0 / resY - 0.5
134119
zz = z * 1.0 / resZ - 0.5
135-
#val = lut.sin(xx * FREQ) + lut.cos(yy * FREQ) + lut.sin(zz * FREQ)
136-
val = lut.cos(xx * FREQ) * lut.sin(yy* FREQ) + lut.cos(yy* FREQ) * lut.sin(zz* FREQ) + lut.cos(zz* FREQ)* lut.sin(xx* FREQ)
120+
#val = Math.sin(xx * FREQ) + Math.cos(yy * FREQ) + Math.sin(zz * FREQ)
121+
val = Math.cos(xx * FREQ) * Math.sin(yy* FREQ) + Math.cos(yy* FREQ) * Math.sin(zz* FREQ) + Math.cos(zz* FREQ)* Math.sin(xx* FREQ)
137122
if (val > upper_bound)
138123
val = 0
139124
end

0 commit comments

Comments
 (0)