Class: Triangular::Solid

Inherits:
Object
  • Object
show all
Defined in:
lib/triangular/solid.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ Solid

Returns a new instance of Solid.



6
7
8
9
10
# File 'lib/triangular/solid.rb', line 6

def initialize(name, *args)
  @name = name
  @facets = args
  @units = units
end

Instance Attribute Details

#facetsObject

Returns the value of attribute facets.



4
5
6
# File 'lib/triangular/solid.rb', line 4

def facets
  @facets
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/triangular/solid.rb', line 4

def name
  @name
end

#unitsObject

Returns the value of attribute units.



4
5
6
# File 'lib/triangular/solid.rb', line 4

def units
  @units
end

Class Method Details

.parse(string) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/triangular/solid.rb', line 80

def self.parse(string)
  partial_pattern = /\s* solid\s+ (?<name> [a-zA-Z0-9\-\_\.]+)?/x
  match_data = string.match(partial_pattern)
  
  solid = self.new(match_data[:name])
  
  solid.facets = Facet.parse(string.gsub(partial_pattern, ""))
  
  solid
end

Instance Method Details

#align_to_origin!Object



52
53
54
55
# File 'lib/triangular/solid.rb', line 52

def align_to_origin!
  bounds = self.get_bounds
  self.translate!(-bounds[0].x, -bounds[0].y, -bounds[0].z)
end

#center!Object



57
58
59
60
61
62
63
64
65
# File 'lib/triangular/solid.rb', line 57

def center!
  bounds = self.get_bounds
  
  x_translation = ((bounds[1].x - bounds[0].x).abs / 2) + -bounds[1].x
  y_translation = ((bounds[1].y - bounds[0].y).abs / 2) + -bounds[1].y
  z_translation = ((bounds[1].z - bounds[0].z).abs / 2) + -bounds[1].z
  
  self.translate!(x_translation, y_translation, z_translation)
end

#get_boundsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/triangular/solid.rb', line 28

def get_bounds
  largest_x = @facets[0].vertices[0].x
  largest_y = @facets[0].vertices[0].y
  largest_z = @facets[0].vertices[0].z
  
  smallest_x = @facets[0].vertices[0].x
  smallest_y = @facets[0].vertices[0].y
  smallest_z = @facets[0].vertices[0].z
  
  @facets.each do |facet|
    facet.vertices.each do |vertex|
      largest_x = vertex.x if vertex.x > largest_x
      largest_y = vertex.y if vertex.y > largest_y
      largest_z = vertex.z if vertex.z > largest_z
      
      smallest_x = vertex.x if vertex.x < smallest_x
      smallest_y = vertex.y if vertex.y < smallest_y
      smallest_z = vertex.z if vertex.z < smallest_z
    end
  end
  
  [Point.new(smallest_x, smallest_y, smallest_z), Point.new(largest_x, largest_y, largest_z)]
end

#slice_at_z(z_plane) ⇒ Object



67
68
69
70
71
72
# File 'lib/triangular/solid.rb', line 67

def slice_at_z(z_plane)
  lines = @facets.map {|facet| facet.intersection_at_z(z_plane) }
  lines.compact!
  
  Polyline.new(lines)
end

#to_sObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/triangular/solid.rb', line 12

def to_s
  output = "solid #{@name || ""}\n"
  @facets.each do |facet|
    output << "facet normal #{facet.normal.x.to_f} #{facet.normal.y.to_f} #{facet.normal.z.to_f}\n"
    output << "outer loop\n"
    facet.vertices.each do |vertex|
      output <<"vertex #{vertex.x.to_f} #{vertex.y.to_f} #{vertex.z.to_f}\n"
    end
    output << "endloop\n"
    output << "endfacet\n"
  end
  output << "endsolid #{@name || ""}\n"
  
  output
end

#translate!(x, y, z) ⇒ Object



74
75
76
77
78
# File 'lib/triangular/solid.rb', line 74

def translate!(x, y, z)
  @facets.each do |facet|
    facet.translate!(x, y, z)
  end
end