Class: Geom::TriangleMesh

Inherits:
Object
  • Object
show all
Defined in:
lib/geom/triangle_mesh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vertices = nil, faces = nil, user_data = nil) ⇒ TriangleMesh

Returns a new instance of TriangleMesh.



6
7
8
9
10
11
12
13
# File 'lib/geom/triangle_mesh.rb', line 6

def initialize( vertices=nil, faces=nil, user_data=nil )
  @data     = user_data || Hash.new
  @faces    = faces     || Array.new
  @vertices = vertices  || Array.new
  @meshes   = Array.new

  @tess     = EarTrim
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/geom/triangle_mesh.rb', line 4

def data
  @data
end

#facesObject

Returns the value of attribute faces.



4
5
6
# File 'lib/geom/triangle_mesh.rb', line 4

def faces
  @faces
end

#meshesObject

Returns the value of attribute meshes.



4
5
6
# File 'lib/geom/triangle_mesh.rb', line 4

def meshes
  @meshes
end

#tessObject

Returns the value of attribute tess.



4
5
6
# File 'lib/geom/triangle_mesh.rb', line 4

def tess
  @tess
end

#texcoordObject

Returns the value of attribute texcoord.



4
5
6
# File 'lib/geom/triangle_mesh.rb', line 4

def texcoord
  @texcoord
end

#verticesObject

Returns the value of attribute vertices.



4
5
6
# File 'lib/geom/triangle_mesh.rb', line 4

def vertices
  @vertices
end

Instance Method Details

#<<(mesh) ⇒ Object



15
16
17
# File 'lib/geom/triangle_mesh.rb', line 15

def << (mesh)
  @meshes << mesh
end

#bounding_boxObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/geom/triangle_mesh.rb', line 73

def bounding_box
  min = Geom::Number3D.new( 1000, 1000, 1000)
  max = Geom::Number3D.new(-1000,-1000,-1000)

  vertices.each do |v|
    min.x = v.x if v.x < min.x
    min.y = v.y if v.y < min.y
    min.z = v.z if v.z < min.z

    max.x = v.x if v.x > max.x
    max.y = v.y if v.y > max.y
    max.z = v.z if v.z > max.z
  end

  { :max => max , :min => min }
end

#merge(other) ⇒ Object



90
91
92
# File 'lib/geom/triangle_mesh.rb', line 90

def merge(other)
  @vertices.concat other.vertices
end

#reverseObject



38
39
40
# File 'lib/geom/triangle_mesh.rb', line 38

def reverse
  faces.each {|f| f.vertices.reverse! }
end

#transform_vertices(transformation) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/geom/triangle_mesh.rb', line 42

def transform_vertices(transformation)
  ta = transformation.to_a
  m11 = ta[0][0]
  m12 = ta[0][1]
  m13 = ta[0][2]
  m21 = ta[1][0]
  m22 = ta[1][1]
  m23 = ta[1][2]
  m31 = ta[2][0]
  m32 = ta[2][1]
  m33 = ta[2][2]

  m14 = ta[0][3]
  m24 = ta[1][3]
  m34 = ta[2][3]

  @vertices.each do |v|
    vx = v.x
    vy = v.y
    vz = v.z

    tx = vx * m11 + vy * m12 + vz * m13 + m14
    ty = vx * m21 + vy * m22 + vz * m23 + m24
    tz = vx * m31 + vy * m32 + vz * m33 + m34

    v.x = tx
    v.y = ty
    v.z = tz
  end
end

#updateObject



34
35
36
# File 'lib/geom/triangle_mesh.rb', line 34

def update
  @meshes.each {|m| m.update}
end