|
| 1 | +#!/usr/bin/env jruby -v -W2 |
| 2 | +# frozen_string_literal: true |
| 3 | +require 'propane' |
| 4 | +require 'arcball' |
| 5 | + |
| 6 | +# TwinIso class |
| 7 | +class TwinIso < Propane::App |
| 8 | + load_libraries :hemesh |
| 9 | + java_import 'wblut.processing.WB_Render' |
| 10 | + java_import 'wblut.hemesh.HEC_IsoSurface' |
| 11 | + java_import 'wblut.hemesh.HE_Mesh' |
| 12 | + java_import 'wblut.hemesh.HEM_Smooth' |
| 13 | + |
| 14 | + RES ||= 20 |
| 15 | + |
| 16 | + attr_reader :mesh, :inv_mesh, :render |
| 17 | + |
| 18 | + def setup |
| 19 | + sketch_title 'Twin Iso' |
| 20 | + Processing::ArcBall.init(self) |
| 21 | + values = [] # build a multi-dimensional array in ruby |
| 22 | + (0..RES).each do |i| # the inclusive range is intentional here |
| 23 | + valu = [] |
| 24 | + (0..RES).each do |j| |
| 25 | + val = [] |
| 26 | + (0..RES).each do |k| |
| 27 | + val << 2.1 * noise(0.35 * i, 0.35 * j, 0.35 * k) |
| 28 | + end |
| 29 | + valu << val |
| 30 | + end |
| 31 | + values << valu |
| 32 | + end |
| 33 | + |
| 34 | + creator = HEC_IsoSurface.new |
| 35 | + creator.set_resolution(RES, RES, RES) # number of cells in x,y,z direction |
| 36 | + creator.set_size(400.0 / RES, 400.0 / RES, 400.0 / RES) # cell size |
| 37 | + # JRuby requires a bit of help to determine correct 'java args', particulary with |
| 38 | + # overloaded arrays args as seen below. Note we are saying we have an 'array' of |
| 39 | + # 'float array' here, where the values can also be double[][][]. |
| 40 | + java_values = values.to_java(Java::float[][]) # pre-coerce values to java |
| 41 | + creator.set_values(java_values) # the grid points |
| 42 | + creator.set_isolevel(1) # isolevel to mesh |
| 43 | + creator.set_invert(false) # invert mesh |
| 44 | + creator.set_boundary(100) # value of isoFunction outside grid |
| 45 | + # use creator.clear_boundary to set boundary values to "no value". |
| 46 | + # A boundary value of "no value" results in an open mesh |
| 47 | + @mesh = HE_Mesh.new(creator) |
| 48 | + # mesh.modify(HEM_Smooth.new.set_iterations(10).setAutoRescale(true)) |
| 49 | + creator.set_invert(true) |
| 50 | + @inv_mesh = HE_Mesh.new(creator) |
| 51 | + inv_mesh.modify(HEM_Smooth.new.set_iterations(10).set_auto_rescale(true)) |
| 52 | + @render = WB_Render.new(self) |
| 53 | + end |
| 54 | + |
| 55 | + def settings |
| 56 | + size 800, 800, P3D |
| 57 | + smooth(8) |
| 58 | + end |
| 59 | + |
| 60 | + def draw |
| 61 | + background(200) |
| 62 | + lights |
| 63 | + define_lights |
| 64 | + no_stroke |
| 65 | + fill(255, 0, 0) |
| 66 | + render.draw_faces(inv_mesh) |
| 67 | + stroke(0) |
| 68 | + render.draw_edges(mesh) |
| 69 | + stroke(255, 0, 0, 80) |
| 70 | + render.draw_edges(inv_mesh) |
| 71 | + end |
| 72 | + |
| 73 | + def define_lights |
| 74 | + ambient(80, 80, 80) |
| 75 | + ambient_light(80, 80, 80) |
| 76 | + point_light(30, 30, 30, 0, 0, 1) |
| 77 | + directional_light(40, 40, 50, 0, 0, 1) |
| 78 | + spot_light(30, 30, 30, 0, 40, 200, 0, -0.5, 0.5, PI / 2, 2) |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +TwinIso.new |
0 commit comments