disp3D 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +73 -1
- data/VERSION +1 -1
- data/disp3D.gemspec +97 -0
- data/example/stl_viewer/app_model.rb +8 -0
- data/example/stl_viewer/document.rb +15 -0
- data/example/stl_viewer/document_ctrl.rb +62 -0
- data/example/stl_viewer/gl_ctrl.rb +15 -0
- data/example/stl_viewer/main.rb +120 -0
- data/example/stl_viewer/mesh_info.rb +12 -0
- data/example/stl_viewer/stl_viewer.rb +9 -0
- data/lib/camera.rb +49 -35
- data/lib/disp3D.rb +19 -3
- data/lib/dsl.rb +18 -0
- data/lib/gl_view.rb +80 -0
- data/lib/glut_window.rb +60 -0
- data/lib/light.rb +49 -0
- data/lib/manipulator.rb +92 -21
- data/lib/node.rb +15 -9
- data/lib/node_arrows.rb +68 -0
- data/lib/node_collection.rb +14 -3
- data/lib/node_leaf.rb +80 -0
- data/lib/node_lines.rb +4 -7
- data/lib/node_points.rb +9 -11
- data/lib/node_polylines.rb +34 -0
- data/lib/node_tea_pod.rb +9 -2
- data/lib/node_text.rb +17 -0
- data/lib/node_tris.rb +24 -0
- data/lib/picked_result.rb +15 -0
- data/lib/picker.rb +56 -0
- data/lib/qt_widget_gl.rb +80 -0
- data/lib/scene_graph.rb +5 -0
- data/lib/stl.rb +92 -0
- data/lib/util.rb +18 -0
- data/test/test_data/binary_test.stl +0 -0
- data/test/test_data/cube-ascii.stl +86 -0
- data/test/test_dsl.rb +8 -0
- data/test/test_glut_window.rb +113 -0
- data/test/test_qtgl.rb +26 -0
- data/test/test_stl.rb +12 -0
- data/test/test_tea_pod.rb +13 -2
- metadata +41 -19
- data/lib/helloworld.rb +0 -112
- data/lib/view.rb +0 -47
- data/test/test_line.rb +0 -8
- data/test/test_lines.rb +0 -17
- data/test/test_point.rb +0 -25
- data/test/test_points.rb +0 -27
data/lib/node_collection.rb
CHANGED
@@ -2,12 +2,16 @@ require 'disp3D'
|
|
2
2
|
|
3
3
|
module Disp3D
|
4
4
|
class NodeCollection < Node
|
5
|
+
|
6
|
+
def initialize()
|
7
|
+
@children = []
|
8
|
+
end
|
9
|
+
|
5
10
|
def add(node)
|
6
11
|
# TODO if ancestor Node is added then cancel...
|
7
12
|
# add parents and check
|
8
13
|
if(node.kind_of?(Array))
|
9
14
|
node.each do |item|
|
10
|
-
|
11
15
|
@children.push(item)
|
12
16
|
end
|
13
17
|
else
|
@@ -16,13 +20,20 @@ module Disp3D
|
|
16
20
|
end
|
17
21
|
|
18
22
|
def draw
|
23
|
+
pre_draw()
|
19
24
|
@children.each do |child|
|
20
25
|
child.draw
|
21
26
|
end
|
27
|
+
post_draw()
|
22
28
|
end
|
23
29
|
|
24
|
-
def
|
25
|
-
@children
|
30
|
+
def box
|
31
|
+
return nil if @children == nil || @children.size == 0
|
32
|
+
box = @children[0].box
|
33
|
+
@children.each do |child|
|
34
|
+
box += child.box
|
35
|
+
end
|
36
|
+
return box
|
26
37
|
end
|
27
38
|
end
|
28
39
|
end
|
data/lib/node_leaf.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# -*- coding: cp932 -*-
|
2
|
+
require 'disp3D'
|
3
|
+
|
4
|
+
module Disp3D
|
5
|
+
class NodeLeaf < Node
|
6
|
+
attr_accessor :material_color
|
7
|
+
attr_accessor :shininess
|
8
|
+
|
9
|
+
def initialize(geometry = nil)
|
10
|
+
@geom = geometry
|
11
|
+
|
12
|
+
@material_color = [1.0, 1.0, 1.0, 1.0]
|
13
|
+
@shininess = nil
|
14
|
+
@shininess_default = 32.0
|
15
|
+
|
16
|
+
@node_id = new_id()
|
17
|
+
@list_created = false
|
18
|
+
end
|
19
|
+
|
20
|
+
def draw
|
21
|
+
@@named_nodes[@node_id] = self
|
22
|
+
GL.LoadName(@node_id)
|
23
|
+
draw_inner(self.method(:draw_element))
|
24
|
+
end
|
25
|
+
|
26
|
+
def box
|
27
|
+
return nil if @geom == nil
|
28
|
+
if(@geom.kind_of?(Array))
|
29
|
+
return nil if @geom.size == 0
|
30
|
+
box = @geom[0].box
|
31
|
+
@geom.each do |element|
|
32
|
+
box += element.box
|
33
|
+
end
|
34
|
+
return box
|
35
|
+
else
|
36
|
+
return @geom.box
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
def draw_inner(draw_element)
|
42
|
+
diffuse = @material_color
|
43
|
+
ambient = [@material_color[0]*0.5, @material_color[1]*0.5, @material_color[2]*0.5, 1]
|
44
|
+
specular = [1,1,1,0.5]
|
45
|
+
|
46
|
+
shineness = [@shiness]
|
47
|
+
shineness = [@shininess_default] if( !@shiness )
|
48
|
+
|
49
|
+
GL.Materialfv(GL::GL_FRONT, GL::GL_DIFFUSE, diffuse)
|
50
|
+
GL.Materialfv(GL::GL_FRONT, GL::GL_AMBIENT, ambient)
|
51
|
+
GL.Materialfv(GL::GL_FRONT, GL::GL_SPECULAR, specular)
|
52
|
+
GL.Materialfv(GL::GL_FRONT, GL::GL_SHININESS, shineness)
|
53
|
+
|
54
|
+
if(@list_created == false)
|
55
|
+
@list_created = true
|
56
|
+
GL.NewList(@node_id, GL::GL_COMPILE_AND_EXECUTE)
|
57
|
+
pre_draw # matrix manipulation
|
58
|
+
draw_element.call
|
59
|
+
post_draw # matrix manipulation
|
60
|
+
GL.EndList()
|
61
|
+
else
|
62
|
+
GL.CallList(@node_id)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def draw_element
|
67
|
+
# you cannot call this directory. use child class one.
|
68
|
+
raise
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
@@id_list = Array.new()
|
73
|
+
def new_id()
|
74
|
+
# p "constructed id list #{@@id_list}"
|
75
|
+
id_adding = GL.GenLists(1)
|
76
|
+
@@id_list.push(id_adding)
|
77
|
+
return id_adding
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/lib/node_lines.rb
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
require 'disp3D'
|
2
2
|
|
3
3
|
module Disp3D
|
4
|
-
class NodeLines <
|
4
|
+
class NodeLines < NodeLeaf
|
5
5
|
attr_accessor :width
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
protected
|
8
|
+
def draw_element
|
10
9
|
if(@geom)
|
10
|
+
GL.ShadeModel(GL::FLAT)
|
11
11
|
GL.LineWidth(@width) if(@width)
|
12
|
-
|
13
12
|
GL.Begin(GL::LINES)
|
14
13
|
if(@geom.kind_of?(GMath3D::FiniteLine))
|
15
14
|
GL.Vertex( @geom.start_point.x, @geom.start_point.y, @geom.start_point.z )
|
@@ -21,8 +20,6 @@ module Disp3D
|
|
21
20
|
end
|
22
21
|
end
|
23
22
|
GL.End()
|
24
|
-
|
25
|
-
post_draw()
|
26
23
|
end
|
27
24
|
end
|
28
25
|
|
data/lib/node_points.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
require 'disp3D'
|
2
2
|
|
3
3
|
module Disp3D
|
4
|
-
class NodePoints <
|
4
|
+
class NodePoints < NodeLeaf
|
5
5
|
attr_accessor :size
|
6
6
|
|
7
|
-
def
|
8
|
-
|
7
|
+
def initialize(geom)
|
8
|
+
super(geom)
|
9
|
+
@size = 3.0;
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
def draw_element
|
9
14
|
if(@geom)
|
15
|
+
GL.ShadeModel(GL::FLAT)
|
10
16
|
GL.PointSize(@size)
|
11
|
-
|
12
17
|
GL.Begin(GL::POINTS)
|
13
|
-
|
14
18
|
if(@geom.kind_of?(GMath3D::Vector3))
|
15
19
|
GL.Vertex( @geom.x, @geom.y, @geom.z )
|
16
20
|
elsif(@geom.kind_of?(Array))
|
@@ -23,12 +27,6 @@ module Disp3D
|
|
23
27
|
|
24
28
|
GL.End()
|
25
29
|
end
|
26
|
-
post_draw()
|
27
|
-
end
|
28
|
-
|
29
|
-
def initialize(geom)
|
30
|
-
super(geom)
|
31
|
-
@size = 3.0;
|
32
30
|
end
|
33
31
|
end
|
34
32
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'disp3D'
|
2
|
+
|
3
|
+
module Disp3D
|
4
|
+
class NodePolylines < NodeLeaf
|
5
|
+
attr_accessor :width
|
6
|
+
|
7
|
+
protected
|
8
|
+
def draw_element
|
9
|
+
if(@geom)
|
10
|
+
GL.ShadeModel(GL::FLAT)
|
11
|
+
GL.LineWidth(@width) if(@width)
|
12
|
+
if(@geom.kind_of?(GMath3D::Polyline))
|
13
|
+
draw_each(@geom)
|
14
|
+
elsif(@geom.kind_of?(Array))
|
15
|
+
@geom.each do |polyline|
|
16
|
+
draw_each(polyline)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def draw_each(polyline)
|
23
|
+
GL.Begin(GL::LINE_STRIP)
|
24
|
+
polyline.vertices.each do | vertex |
|
25
|
+
GL.Vertex( vertex.x, vertex.y, vertex.z )
|
26
|
+
end
|
27
|
+
GL.Vertex( polyline.vertices[0].x, polyline.vertices[0].y, polyline.vertices[0].z ) if( !polyline.is_open )
|
28
|
+
GL.End()
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
data/lib/node_tea_pod.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
require 'disp3D'
|
2
2
|
|
3
3
|
module Disp3D
|
4
|
-
class NodeTeaPod
|
5
|
-
|
4
|
+
class NodeTeaPod < NodeLeaf
|
5
|
+
|
6
|
+
def box
|
7
|
+
return Box.new(Vector3.new(-0.5, -0.5, -0.5),Vector3.new(0.5, 0.5, 0.5))
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
def draw_element
|
12
|
+
GL.ShadeModel(GL::SMOOTH)
|
6
13
|
GLUT.SolidTeapot(0.5)
|
7
14
|
end
|
8
15
|
end
|
data/lib/node_text.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'disp3D'
|
2
|
+
|
3
|
+
module Disp3D
|
4
|
+
class NodeText < NodeLeaf
|
5
|
+
attr_accessor :text
|
6
|
+
attr_accessor :position
|
7
|
+
protected
|
8
|
+
def draw_element
|
9
|
+
if(@text and @position)
|
10
|
+
GL.RasterPos(@position.x, @position.y, @position.z)
|
11
|
+
@text.bytes.to_a.each do |ascii|
|
12
|
+
GLUT.BitmapCharacter(GLUT::BITMAP_HELVETICA_18, ascii)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/node_tris.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'disp3D'
|
2
|
+
|
3
|
+
module Disp3D
|
4
|
+
class NodeTris < NodeLeaf
|
5
|
+
|
6
|
+
protected
|
7
|
+
def draw_element
|
8
|
+
if(@geom)
|
9
|
+
GL.ShadeModel(GL::SMOOTH)
|
10
|
+
GL.Begin(GL::TRIANGLES)
|
11
|
+
if(@geom.kind_of?(GMath3D::TriMesh))
|
12
|
+
@geom.triangles.each do |tri_geom|
|
13
|
+
GL.Normal(tri_geom.normal.x, tri_geom.normal.y, tri_geom.normal.z)
|
14
|
+
tri_geom.vertices.each do |vertex|
|
15
|
+
GL.Vertex( vertex.x, vertex.y, vertex.z )
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
GL.End()
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
data/lib/picker.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'disp3D'
|
2
|
+
|
3
|
+
module Disp3D
|
4
|
+
class Picker
|
5
|
+
def initialize(view)
|
6
|
+
@view = view
|
7
|
+
@max_select_count = 100
|
8
|
+
end
|
9
|
+
|
10
|
+
def hit_test(x,y)
|
11
|
+
selection = GL.SelectBuffer(@max_select_count)
|
12
|
+
GL.RenderMode(GL::SELECT)
|
13
|
+
|
14
|
+
GL.InitNames()
|
15
|
+
GL.PushName(-1)
|
16
|
+
GL.MatrixMode(GL::PROJECTION)
|
17
|
+
GL.PushMatrix()
|
18
|
+
GL.LoadIdentity()
|
19
|
+
vp = GL.GetIntegerv(GL::VIEWPORT)
|
20
|
+
|
21
|
+
GLU.PickMatrix(x, vp[3] - y - 1, 6, 6, vp)
|
22
|
+
@view.camera.set_screen(vp[2], vp[3])
|
23
|
+
|
24
|
+
GL.MatrixMode(GL::MODELVIEW)
|
25
|
+
|
26
|
+
@view.camera.display()
|
27
|
+
@view.world_scene_graph.display()
|
28
|
+
|
29
|
+
GL.MatrixMode(GL::PROJECTION)
|
30
|
+
|
31
|
+
GL.PopMatrix()
|
32
|
+
GL.MatrixMode(GL::MODELVIEW)
|
33
|
+
hit = GL.RenderMode(GL::RENDER)
|
34
|
+
|
35
|
+
return nil if (hit < 0 || hit > 100000) # invalid hit count
|
36
|
+
|
37
|
+
data = selection.unpack("I*")
|
38
|
+
picked_result = Array.new()
|
39
|
+
hit.times.each do | i |
|
40
|
+
count = data[4*i]
|
41
|
+
near = data[4*i+1].to_f / (0x7fffffff).to_f
|
42
|
+
far = data[4*i+2].to_f / (0x7fffffff).to_f
|
43
|
+
return nil if ( count > 100000)# invalid hit count
|
44
|
+
|
45
|
+
# TODO! get world cod
|
46
|
+
nodes = Array.new()
|
47
|
+
count.times do | j |
|
48
|
+
picked_node = Node.from_id(data[4*i+3 + j])
|
49
|
+
nodes.push(picked_node) if (picked_node != nil)
|
50
|
+
end
|
51
|
+
picked_result.push(PickedResult.new(near, far, nodes))
|
52
|
+
end
|
53
|
+
return picked_result
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/qt_widget_gl.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'Qt'
|
2
|
+
require 'disp3D'
|
3
|
+
|
4
|
+
include Disp3D
|
5
|
+
class QtWidgetGL < Qt::GLWidget
|
6
|
+
attr_accessor :width
|
7
|
+
attr_accessor :height
|
8
|
+
|
9
|
+
def initialize(parent)
|
10
|
+
super(parent)
|
11
|
+
@width = 400
|
12
|
+
@height = 400
|
13
|
+
|
14
|
+
@min_width = 50
|
15
|
+
@min_height = 50
|
16
|
+
end
|
17
|
+
|
18
|
+
def dispose()
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
def camera
|
23
|
+
return @view.camera
|
24
|
+
end
|
25
|
+
|
26
|
+
def world_scene_graph
|
27
|
+
return @view.world_scene_graph
|
28
|
+
end
|
29
|
+
|
30
|
+
def fit
|
31
|
+
@view.fit
|
32
|
+
end
|
33
|
+
|
34
|
+
def initializeGL()
|
35
|
+
@view = Disp3D::GLView.new(@width, @height)
|
36
|
+
end
|
37
|
+
|
38
|
+
def minimumSizeHint()
|
39
|
+
return Qt::Size.new(@min_width, @min_height)
|
40
|
+
end
|
41
|
+
|
42
|
+
def sizeHint()
|
43
|
+
return Qt::Size.new(@width, @height)
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_GLUT_button(event)
|
47
|
+
return GLUT::GLUT_RIGHT_BUTTON if( event.button == Qt::RightButton)
|
48
|
+
return GLUT::GLUT_LEFT_BUTTON if( event.button == Qt::LeftButton)
|
49
|
+
return nil
|
50
|
+
end
|
51
|
+
|
52
|
+
def mouseReleaseEvent(event)
|
53
|
+
@view.mouse_release_proc.call(self, get_GLUT_button(event), event.pos.x, event.pos.y) if( @view.mouse_release_proc != nil)
|
54
|
+
@view.manipulator.mouse(get_GLUT_button(event), GLUT::GLUT_UP, event.pos.x,event.pos.y)
|
55
|
+
end
|
56
|
+
|
57
|
+
def mousePressEvent(event)
|
58
|
+
@view.mouse_press_proc.call(self, get_GLUT_button(event), event.pos.x, event.pos.y) if( @view.mouse_press_proc != nil)
|
59
|
+
@view.manipulator.mouse(get_GLUT_button(event), GLUT::GLUT_DOWN, event.pos.x,event.pos.y)
|
60
|
+
end
|
61
|
+
|
62
|
+
def mouseMoveEvent(event)
|
63
|
+
@view.mouse_move_proc.call(self, x,y) if( @view.mouse_move_proc != nil)
|
64
|
+
|
65
|
+
need_update = @view.manipulator.motion(event.pos.x,event.pos.y)
|
66
|
+
if(need_update)
|
67
|
+
updateGL()
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def paintGL
|
72
|
+
@view.display
|
73
|
+
end
|
74
|
+
|
75
|
+
def resizeGL(width, height)
|
76
|
+
@view.camera.reshape(width, height)
|
77
|
+
@view.manipulator.reset_size(width, height)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|