Skip to content

Commit 591d72a

Browse files
committed
sampling_poisson anonymous class instance
1 parent 1de5b8b commit 591d72a

3 files changed

Lines changed: 231 additions & 63 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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("|k9 -r #{sample_name}", 'r') do |io|
26+
while l = io.gets
27+
puts(l.chop)
28+
end
29+
end
30+
end
Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,74 @@
1-
21
#
32
# PixelFlow | Copyright (C) 2016 Thomas Diewald - http://thomasdiewald.com
4-
#
3+
# Translated to RubyArt by MArtin Prout
54
# A Processing/Java library for high performance GPU-Computing (GLSL).
65
# MIT License: https://opensource.org/licenses/MIT
76
#
87

8+
load_library :PixelFlow
9+
java_import 'com.thomasdiewald.pixelflow.java.sampling.PoissonSample'
10+
java_import 'com.thomasdiewald.pixelflow.java.sampling.PoissonDiscSamping2D'
11+
attr_reader :samples, :display_radius
912

13+
def settings
14+
size(1280, 720, P2D)
15+
smooth(16)
16+
end
1017

11-
load_library :PixelFlow
12-
java_import 'com.thomasdiewald.pixelflow.java.sampling.PoissonSample'
13-
java_import 'com.thomasdiewald.pixelflow.java.sampling.PoissonDiscSamping2D'
14-
attr_reader :samples, :display_radius
18+
def setup
19+
sketch_title 'Poisson Sampling 2D'
20+
@display_radius = true
21+
generate_poisson_sampling2d
22+
end
1523

16-
def settings
17-
size(1280, 720, P2D)
18-
smooth(16)
19-
end
20-
21-
def setup
22-
sketch_title 'Poisson Sampling 2D'
23-
@display_radius = true
24-
generate_poisson_sampling2d
25-
end
26-
27-
28-
def generate_poisson_sampling2d
29-
pds = RealDiscSamping2D.new
30-
bounds = [0, 0, 0, width, height, 0]
31-
rmin = 2
32-
rmax = 25
33-
roff = 0.5
34-
new_points = 100
35-
start = Time.now
36-
pds.setRandomSeed(rand(0..10_0000))
37-
pds.generatePoissonSampling2D(bounds, rmin, rmax, roff, new_points)
38-
time = Time.now - start
39-
puts("poisson samples 2D generated")
40-
puts(" time: #{(time * 1000).floor}ms")
41-
puts(" count: #{pds.samples.size}")
42-
@samples = pds.samples
43-
end
44-
45-
def draw
46-
background(64)
47-
samples.each do |sample|
48-
px = sample.x
49-
py = sample.y
50-
pr = sample.rad
51-
if display_radius
52-
stroke(255)
53-
stroke_weight(0.5)
54-
fill(255)
55-
no_stroke
56-
ellipse(px, py, pr * 2, pr * 2)
57-
else
58-
stroke(255)
59-
stroke_weight(2)
60-
point(px, py)
61-
end
24+
def generate_poisson_sampling2d
25+
# create an anonymous class instance in JRuby that implements the java
26+
# abstract class PoissonDiscSamping2D
27+
pds = Class.new(PoissonDiscSamping2D) do
28+
def newInstance(x, y, r, rcollision)
29+
PoissonSample.new(x, y, r, rcollision)
6230
end
63-
end
31+
end.new
32+
bounds = [0, 0, 0, width, height, 0]
33+
rmin = 2
34+
rmax = 25
35+
roff = 0.5
36+
new_points = 100
37+
start = Time.now
38+
pds.setRandomSeed(rand(0..10_0000))
39+
pds.generatePoissonSampling2D(bounds, rmin, rmax, roff, new_points)
40+
@samples = pds.samples
41+
time = Time.now - start
42+
puts("poisson samples 2D generated")
43+
puts(" time: #{(time * 1000).floor}ms")
44+
puts(" count: #{samples.size}")
45+
end
6446

65-
def key_released
66-
case key
67-
when ' '
68-
@display_radius = !display_radius
69-
when 'r'
70-
generate_poisson_sampling2d
47+
def draw
48+
background(64)
49+
samples.each do |sample|
50+
px = sample.x
51+
py = sample.y
52+
pr = sample.rad
53+
if display_radius
54+
stroke(255)
55+
stroke_weight(0.5)
56+
fill(255)
57+
no_stroke
58+
ellipse(px, py, pr * 2, pr * 2)
59+
else
60+
stroke(255)
61+
stroke_weight(2)
62+
point(px, py)
7163
end
7264
end
65+
end
7366

74-
class RealDiscSamping2D < PoissonDiscSamping2D
75-
def newInstance(x, y, r, rcollision)
76-
PoissonSample.new(x, y, r, rcollision)
77-
end
67+
def key_released
68+
case key
69+
when ' '
70+
@display_radius = !display_radius
71+
when 'r'
72+
generate_poisson_sampling2d
7873
end
74+
end
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#
2+
# PixelFlow | Copyright (C) 2016 Thomas Diewald - http:#thomasdiewald.com
3+
#
4+
# A Processing/Java library for high performance GPU-Computing (GLSL).
5+
# MIT License: https:#opensource.org/licenses/MIT
6+
#
7+
load_library :PixelFlow, :peasycam
8+
9+
module Poisson
10+
java_import 'com.thomasdiewald.pixelflow.java.geometry.DwCube'
11+
java_import 'com.thomasdiewald.pixelflow.java.geometry.DwIcosahedron'
12+
java_import 'com.thomasdiewald.pixelflow.java.geometry.DwIndexedFaceSetAble'
13+
java_import 'com.thomasdiewald.pixelflow.java.geometry.DwMeshUtils'
14+
java_import 'com.thomasdiewald.pixelflow.java.sampling.PoissonDiscSamping3D'
15+
java_import 'com.thomasdiewald.pixelflow.java.sampling.PoissonSample'
16+
java_import 'peasy.PeasyCam'
17+
java_import 'processing.core.PShape'
18+
end
19+
20+
include Poisson
21+
22+
attr_reader :cam, :shp_samples_spheres, :shp_samples_points, :shp_gizmo
23+
attr_reader :display_radius, :generate_spheres, :ifs, :verts_per_face, :samples
24+
25+
def settings
26+
size(1280, 720, P3D)
27+
smooth(8)
28+
end
29+
30+
def setup
31+
sketch_title 'Sampling Poisson 3D'
32+
@cam = PeasyCam.new(self, 0, 0, 0, 1000)
33+
@display_radius = true
34+
@generate_spheres = true
35+
@ifs = nil
36+
@shp_gizmo = nil
37+
@verts_per_face = 0
38+
generate_poisson_sampling
39+
frame_rate(1_000)
40+
end
41+
42+
def generate_poisson_sampling
43+
# create an anonymous class instance in JRuby that implements the java
44+
# abstract class PoissonDiscSamping3D
45+
pds = Class.new(PoissonDiscSamping3D) do
46+
# java_signature 'PoissonSample newInstance(float, float, float, float, float)'
47+
def newInstance(x, y, z, r, rcollision)
48+
PoissonSample.new(x, y, z, r, rcollision)
49+
end
50+
end.new
51+
bounds = [-200, -200, 0, 200, 200, 400]
52+
rmin = 10
53+
rmax = 50
54+
roff = 1
55+
new_points = 50
56+
start = Time.now
57+
pds.generatePoissonSampling(bounds, rmin, rmax, roff, new_points)
58+
@shp_samples_spheres = create_shape(GROUP)
59+
@shp_samples_points = create_shape(GROUP)
60+
@samples = pds.samples
61+
samples.each do |sample|
62+
add_shape(sample)
63+
end
64+
time = Time.now - start
65+
start = Time.now
66+
puts("poisson samples 3D generated")
67+
puts(" time: #{(time * 1_000).floor}ms")
68+
puts(" count: #{pds.samples.size}")
69+
70+
time = Time.now - start
71+
puts("PShapes created")
72+
puts(" time: #{(time * 1_000).floor}ms")
73+
puts(" count: #{samples.size}")
74+
end
75+
76+
def draw
77+
lights
78+
point_light(128, 96, 64, -500, -500, -1_000)
79+
# directionalLight(128, 96, 64, -500, -500, +1_000)
80+
background(64)
81+
display_gizmo(500)
82+
if display_radius
83+
shape(shp_samples_spheres)
84+
else
85+
shape(shp_samples_points)
86+
end
87+
end
88+
89+
def add_shape(sample)
90+
shp_point = create_shape(POINT, sample.x, sample.y, sample.z)
91+
shp_point.set_stroke(color(255))
92+
shp_point.set_stroke_weight(3)
93+
shp_samples_points.add_child(shp_point)
94+
if ifs.nil?
95+
@ifs = DwIcosahedron.new(2)
96+
@verts_per_face = 3
97+
# @ifs = DwCube.new(2)
98+
# @verts_per_face = 4
99+
end
100+
shp_sphere = create_shape(PShape::GEOMETRY)
101+
shp_sphere.set_stroke(false)
102+
shp_sphere.set_fill(color(255))
103+
shp_sphere.reset_matrix
104+
shp_sphere.translate(sample.x, sample.y, sample.z)
105+
DwMeshUtils::createPolyhedronShape(shp_sphere, ifs, sample.rad, verts_per_face, true)
106+
shp_samples_spheres.add_child(shp_sphere)
107+
# @shp_sphere_normals = createShape(PShape::GEOMETRY)
108+
# shp_sphere_normals.setStroke(false)
109+
# shp_sphere_normals.setFill(color(255))
110+
# shp_sphere_normals.resetMatrix
111+
# shp_sphere_normals.translate(sample.x, sample.y, sample.z)
112+
# DwMeshUtils::createPolyhedronShapeNormals(shp_sphere_normals, ifs, sample.rad, 10)
113+
# shp_samples_spheres.addChild(shp_sphere_normals)
114+
end
115+
116+
def display_gizmo(s)
117+
if shp_gizmo.nil?
118+
stroke_weight(1)
119+
@shp_gizmo = create_shape
120+
shp_gizmo.begin_shape(LINES)
121+
shp_gizmo.stroke(255, 0, 0)
122+
shp_gizmo.vertex(0, 0, 0)
123+
shp_gizmo.vertex(s, 0, 0)
124+
shp_gizmo.stroke(0, 255, 0)
125+
shp_gizmo.vertex(0, 0, 0)
126+
shp_gizmo.vertex(0, s, 0)
127+
shp_gizmo.stroke(0, 0, 255)
128+
shp_gizmo.vertex(0, 0, 0)
129+
shp_gizmo.vertex(0, 0, s)
130+
shp_gizmo.end_shape
131+
end
132+
shape shp_gizmo
133+
end
134+
135+
def key_released
136+
case key
137+
when ' '
138+
@display_radius = !display_radius
139+
when 'r'
140+
generate_poisson_sampling
141+
end
142+
end

0 commit comments

Comments
 (0)