Skip to content

Commit

Permalink
total refactor!
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Jan 17, 2008
1 parent b5bc004 commit 204cd72
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 135 deletions.
40 changes: 40 additions & 0 deletions bin/glowstick 100644 → 100755
@@ -0,0 +1,40 @@
#!/usr/bin/env ruby

$:.unshift(File.join(File.dirname(__FILE__), *%w[.. lib]))

require 'glowstick'

class SystemProcess
def initialize(pid)
@pid = pid.to_i
end

# Memory usage in kilobytes (resident set size)
def memory
ps_int('rss')
end

private

def ps_int(keyword)
`ps -o #{keyword}= -p #{@pid}`.to_i
end
end

class Memo
def initialize(pid)
@process = SystemProcess.new(pid)
@graph = Glowstick::Graph.new
end

def idle
# sleep 0.1
mem = @process.memory
@graph.add(mem)
true
end
end

memo = Memo.new(4871)

Glowstick.new(memo).start
20 changes: 20 additions & 0 deletions extra.rb
@@ -0,0 +1,20 @@
def print(text, size, x, y)
view_size = (@window.r - @window.l).to_f
window_size = @w.to_f
xscale = 8 / (window_size / view_size) * (size / 100.0)

GL.Translate(@window.l, @window.b + 100, 0)
GL.Scale(xscale, 2, 1)
text.each_byte { |x| glutStrokeCharacter(GLUT_STROKE_ROMAN, x) }
end

def read
@data = File.read(ARGV[0]).split("\n").map { |x| x.to_i }
puts @data.size

# temp = []
# @data.each_with_index do |x, i|
# temp << x if i % 15 == 0
# end
# @data = temp
end
144 changes: 10 additions & 134 deletions lib/glowstick.rb
@@ -1,142 +1,18 @@
# gems
require 'rubygems' require 'rubygems'
require 'opengl' require 'opengl'
# require 'mathn'
# core
require 'enumerator' require 'enumerator'

include Gl, Glu, Glut include Gl, Glu, Glut


class Box # internal
attr_accessor :l, :r, :b, :t require 'glowstick/glowstick'

require 'glowstick/box'
def initialize(l, r, b, t) require 'glowstick/graph'
@l = l
@r = r
@b = b
@t = t
end

def inset_percent(p)
@l -= (r - l) * (p / 100.0)
@r += (r - l) * (p / 100.0)
@b -= (t - b) * (p / 100.0)
@t += (t - b) * (p / 100.0)
end
end


module Glowstick
class Glowstick
VERSION = '1.0.0' VERSION = '1.0.0'

def self.start
@window = Box.new(0, 500, 0, 20000)

@w = 800
@h = 400

read
calculate_domain_and_range
main
end

def self.init
glClearColor(0.0, 0.0, 0.0, 0.0)

glEnable(GL_LINE_SMOOTH)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE)

glShadeModel(GL_FLAT)
end

def self.read
@data = File.read(ARGV[0]).split("\n").map { |x| x.to_i }
puts @data.size

temp = []
@data.each_with_index do |x, i|
temp << x if i % 15 == 0
end
@data = temp
end

def self.calculate_domain_and_range
a = @data.sort

@window.b = a.first
@window.t = a.last
@window.l = 0
@window.r = a.size

@window.inset_percent(10)
end

def self.plot
i = 0

@data.each_cons(2) do |a|
line(i, a[0], i + 1, a[1])
i += 1
end
end

def self.display
glClear(GL_COLOR_BUFFER_BIT)

plot

print("Hello Stroke", 2, 0, 10)

glutSwapBuffers()
end

def self.print(text, size, x, y)
view_size = (@window.r - @window.l).to_f
window_size = @w.to_f
xscale = 8 / (window_size / view_size) * (size / 100.0)

GL.Translate(@window.l, @window.b + 100, 0)
GL.Scale(xscale, 2, 1)
text.each_byte { |x| glutStrokeCharacter(GLUT_STROKE_ROMAN, x) }
end

def self.reshape(w = @w, h = @h)
@w = w
@h = h

glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
GLU.Ortho2D(@window.l, @window.r, @window.b, @window.t)
end

def self.keyboard(key, x, y)
case (key)
when ?l
nil
when ?r
glutPostRedisplay()
end
end

def self.main
glutInit
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowSize(@w, @h)
glutInitWindowPosition(100, 100)
glutCreateWindow("Glowstick")
init()
glutDisplayFunc(self.method(:display).to_proc)
glutReshapeFunc(self.method(:reshape).to_proc)
glutKeyboardFunc(self.method(:keyboard).to_proc)
glutMainLoop()
end

def self.line(x1, y1, x2, y2)
glBegin(GL_LINES)
glVertex((x1),(y1))
glVertex((x2),(y2))
glEnd()
end
end end

ARGV[0] = "/Users/tom/memlog2.log"
Glowstick.start
35 changes: 35 additions & 0 deletions lib/glowstick/box.rb
@@ -0,0 +1,35 @@
class Glowstick

class Box
attr_accessor :l, :r, :b, :t

def initialize(l, r, b, t)
@l = l
@r = r
@b = b
@t = t
end

def inset_percent(p)
@l -= (r - l) * (p / 100.0)
@r += (r - l) * (p / 100.0)
@b -= (t - b) * (p / 100.0)
@t += (t - b) * (p / 100.0)
self
end

# Calculate the smallest Box that can contain all of the given boxes
# +args+ is the list or Array of Boxes
#
# Returns Glowstick::Box
def self.container(*args)
boxes = Array(args).flatten
l = boxes.map { |box| box.l }.min
r = boxes.map { |box| box.r }.max
b = boxes.map { |box| box.b }.min
t = boxes.map { |box| box.t }.max
Box.new(l, r, b, t)
end
end # Box

end # Glowstick
94 changes: 94 additions & 0 deletions lib/glowstick/glowstick.rb
@@ -0,0 +1,94 @@
class Glowstick

###########################################################################
# Class

class << self
attr_accessor :graphs
end

self.graphs = []

def self.register(graph)
self.graphs << graph
end

###########################################################################
# Instance

def initialize(handler)
@window = Box.new(0, 1, 0, 1)
@handler = handler
@w = 800
@h = 400
end

def display
# clear the buffer
glClear(GL_COLOR_BUFFER_BIT)

# # calculate and apply the current view size
boxes = Glowstick.graphs.map { |g| g.bounding_box }
if boxes.empty?
@window = Box.new(0, 1, 0, 1)
else
@window = Box.container(boxes).inset_percent(10)
end

reshape

# draw each graph
Glowstick.graphs.each do |graph|
graph.draw
end

# swap buffer
glutSwapBuffers()
end

def reshape(w = @w, h = @h)
@w = w
@h = h

glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
GLU.Ortho2D(@window.l, @window.r, @window.b, @window.t)
end

def keyboard(key, x, y)
case (key)
when ?r
glutPostRedisplay()
end
end

def idle
should_draw = @handler.idle
if should_draw
glutPostRedisplay
end
end

def start
glutInit
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowSize(@w, @h)
glutInitWindowPosition(100, 100)
glutCreateWindow("Glowstick")

glClearColor(0.0, 0.0, 0.0, 0.0)
glEnable(GL_LINE_SMOOTH)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE)
glShadeModel(GL_FLAT)

glutDisplayFunc(self.method(:display).to_proc)
glutReshapeFunc(self.method(:reshape).to_proc)
glutKeyboardFunc(self.method(:keyboard).to_proc)
glutIdleFunc(self.method(:idle).to_proc)

glutMainLoop()
end
end
40 changes: 39 additions & 1 deletion lib/glowstick/graph.rb
@@ -1,7 +1,45 @@
module Glowstick class Glowstick


class Graph class Graph
attr_accessor :data


def initialize
@data = []
@box = Box.new(0, 1, 0, 1)
Glowstick.register(self)
end

def add(val)
@data << val
end

def bounding_box
@box.b = @data.min || 0
@box.t = @data.max || 1
@box.l = 0
@box.r = @data.size

@box.t = @box.b + 1 if @box.b == @box.t
@box.r = 1 if @box.r == 0

@box
end

def draw
i = 0

@data.each_cons(2) do |a|
line(i, a[0], i + 1, a[1])
i += 1
end
end

def line(x1, y1, x2, y2)
glBegin(GL_LINES)
glVertex((x1),(y1))
glVertex((x2),(y2))
glEnd()
end
end # Graph end # Graph


end # Glowstick end # Glowstick

0 comments on commit 204cd72

Please sign in to comment.