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.



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

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

Instance Attribute Details

#facetsObject

Returns the value of attribute facets.



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

def facets
  @facets
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#unitsObject

Returns the value of attribute units.



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

def units
  @units
end

Class Method Details

.parse(string) ⇒ Object



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

def self.parse(string)
  partial_pattern = /\s* solid\s+ (?<name> [a-zA-Z0-9\-_.]+)?/x
  match_data = string.match(partial_pattern)

  solid = new(match_data[:name])

  solid.facets = Facet.parse(string.gsub(partial_pattern, ''))

  solid
end

Instance Method Details

#align_to_origin!Object



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

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

#boundsObject



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

def 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

#center!Object



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

def center!
  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

  translate!(x_translation, y_translation, z_translation)
end

#slice_at_z(z_plane) ⇒ Object



65
66
67
68
69
70
# File 'lib/triangular/solid.rb', line 65

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

  Polyline.new(lines)
end

#to_sObject



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

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



72
73
74
75
76
# File 'lib/triangular/solid.rb', line 72

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