Class: GMath3D::Box

Inherits:
Geom
  • Object
show all
Defined in:
lib/box.rb

Overview

Box represents an axitially aligned box on 3D space.

Instance Attribute Summary collapse

Attributes inherited from Geom

#tolerance

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Geom

default_tolerance

Constructor Details

#initialize(point1 = Vector3.new(0,0,0), point2 = Vector3.new(1,1,1)) ⇒ Box

[Input] point1 and point2 should be Vector3. Each points are opposing corners. [Output] return new instance of Box.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/box.rb', line 16

def initialize(point1 = Vector3.new(0,0,0), point2 = Vector3.new(1,1,1))
  Util3D.check_arg_type(Vector3, point1)
  Util3D.check_arg_type(Vector3, point2)
  super()
  @min_point = Vector3.new();
  @max_point = Vector3.new();
  @min_point.x = [ point1.x, point2.x ].min
  @min_point.y = [ point1.y, point2.y ].min
  @min_point.z = [ point1.z, point2.z ].min
  @max_point.x = [ point1.x, point2.x ].max
  @max_point.y = [ point1.y, point2.y ].max
  @max_point.z = [ point1.z, point2.z ].max
end

Instance Attribute Details

#max_pointObject (readonly)

Returns the value of attribute max_point.



9
10
11
# File 'lib/box.rb', line 9

def max_point
  @max_point
end

#min_pointObject (readonly)

Returns the value of attribute min_point.



8
9
10
# File 'lib/box.rb', line 8

def min_point
  @min_point
end

Class Method Details

.from_points(points) ⇒ Object

[Input] points should be Array of Vector3. Each points are opposing corners. [Output] return new instance of Box.



35
36
37
38
39
40
# File 'lib/box.rb', line 35

def self.from_points( points )
  return nil if (points == nil || points.size <=0)
  box = Box.new(points[0], points[0])
  box += points
  return box
end

Instance Method Details

#+(rhs) ⇒ Object

[Input] rhs shold be Vector3 or Box or Array of them. [Output] return added result as Box.



54
55
56
# File 'lib/box.rb', line 54

def +(rhs)
  add(rhs)
end

#==(rhs) ⇒ Object



46
47
48
# File 'lib/box.rb', line 46

def ==(rhs)
  equals_inner(rhs)
end

#centerObject

[Output] return cente point of Box as Vector3.



60
61
62
# File 'lib/box.rb', line 60

def center
  return (@min_point + @max_point) * 0.5
end

#lengthObject

[Output] return width, height, depth as [Numeric, Numeric, Numeric]



66
67
68
# File 'lib/box.rb', line 66

def length
  return max_point.x - min_point.x, max_point.y - min_point.y, max_point.z - min_point.z
end

#rotate(quat) ⇒ Object

[input] quat should be Quat. [Output] return rotated box as Box. since Box is AABB, returned box might be bigger than original one.



106
107
108
109
110
111
112
# File 'lib/box.rb', line 106

def rotate(quat)
  rot_matrix = Matrix.from_quat(quat)
  verts = self.vertices
  inv_mat = rot_matrix.inv
  verts = verts.collect {|item| inv_mat*item}
  return Box.from_points(verts)
end

#to_sObject



42
43
44
# File 'lib/box.rb', line 42

def to_s
  "Box[min#{min_point.to_element_s}, max#{max_point.to_element_s}]"
end

#translate(vec) ⇒ Object

[input] vec should be Vector3. [Output] return translated box as Box.



97
98
99
# File 'lib/box.rb', line 97

def translate(vec)
  return Box.new(min_point + vec, max_point + vec)
end

#verticesObject

[Output] return all vertices of Box.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/box.rb', line 79

def vertices
  verts = Array.new(8)
  length_ary = self.length
  verts[0] = @min_point.clone
  verts[1] = @min_point + Vector3.new(length_ary[0],             0,            0 )
  verts[2] = @min_point + Vector3.new(length_ary[0], length_ary[1],            0 )
  verts[3] = @min_point + Vector3.new(            0, length_ary[1],            0 )
  verts[4] = @min_point + Vector3.new(            0,             0, length_ary[2])
  verts[5] = @min_point + Vector3.new(length_ary[0],             0, length_ary[2])
  verts[6] = @min_point + Vector3.new(length_ary[0], length_ary[1], length_ary[2])
  verts[7] = @min_point + Vector3.new(            0, length_ary[1], length_ary[2])
  return verts
end

#volumeObject

[Output] return volume of Box as Numeric.



72
73
74
75
# File 'lib/box.rb', line 72

def volume
  width, height, depth = self.length
  return width*height*depth
end