Class: Triangular::Facet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(normal = nil, *args) ⇒ Facet



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

def initialize(normal = nil, *args)
  @normal = normal
  @vertices = args
end

Instance Attribute Details

#normalObject

Returns the value of attribute normal.



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

def normal
  @normal
end

#verticesObject

Returns the value of attribute vertices.



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

def vertices
  @vertices
end

Class Method Details

.parse(string) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/triangular/facet.rb', line 50

def self.parse(string)
  facets = []

  string.scan(pattern) do |match_data|
    facets << Facet.new(
      Vector.parse(match_data[0]), # Normal
      Vertex.parse(match_data[4]), # Vertex 1
      Vertex.parse(match_data[9]), # Vertex 2
      Vertex.parse(match_data[14]) # Vertex 3
    )
  end

  facets.length == 1 ? facets.first : facets
end

.patternObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/triangular/facet.rb', line 65

def self.pattern
  /
  \s* facet\snormal\s (?<normal> #{Point.pattern})\s
  \s* outer\sloop\s
  \s* (?<vertex1> #{Vertex.pattern})
  \s* (?<vertex2> #{Vertex.pattern})
  \s* (?<vertex3> #{Vertex.pattern})
  \s* endloop\s
  \s* endfacet\s
  /x
end

Instance Method Details

#intersection_at_z(z_plane) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/triangular/facet.rb', line 32

def intersection_at_z(z_plane)
  return nil if @vertices.count { |vertex| vertex.z == z_plane } > 2

  intersection_points = lines.map do |line|
    line.intersection_at_z(z_plane)
  end.compact

  return Line.new(intersection_points[0], intersection_points[1]) if intersection_points.count == 2

  nil
end

#linesObject



24
25
26
27
28
29
30
# File 'lib/triangular/facet.rb', line 24

def lines
  [
    Line.new(@vertices[0], @vertices[1]),
    Line.new(@vertices[1], @vertices[2]),
    Line.new(@vertices[2], @vertices[0])
  ]
end

#to_sObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/triangular/facet.rb', line 12

def to_s
  output = "facet normal #{@normal}\n"
  output += "outer loop\n"
  @vertices.each do |vertex|
    output += "#{vertex}\n"
  end
  output += "endloop\n"
  output += "endfacet\n"

  output
end

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



44
45
46
47
48
# File 'lib/triangular/facet.rb', line 44

def translate!(x, y, z)
  @vertices.each do |vertex|
    vertex.translate!(x, y, z)
  end
end