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

Returns a new instance of Facet.



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

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

Instance Attribute Details

#normalObject

Returns the value of attribute normal.



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

def normal
  @normal
end

#verticesObject

Returns the value of attribute vertices.



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

def vertices
  @vertices
end

Class Method Details

.parse(string) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/triangular/facet.rb', line 53

def self.parse(string)
  facets = []
  
  string.scan(self.pattern) do |match_data|
    facet = self.new
    
    facet.vertices << Vertex.parse(match_data[4])
    facet.vertices << Vertex.parse(match_data[9])
    facet.vertices << Vertex.parse(match_data[14])
    
    facet.normal = Vector.parse(match_data[0])
    
    facets << facet
  end
  
  if facets.length == 1
    facets.first
  else
    facets
  end
end

.patternObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/triangular/facet.rb', line 75

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



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

def intersection_at_z(z_plane)
  return nil if @vertices.count{|vertex| vertex.z == z_plane} > 2
  
  intersection_points = []
  lines.each do |line|
    intersection_points << line.intersection_at_z(z_plane) unless line.start.z == z_plane && line.end.z == z_plane
  end
  
  intersection_points.compact!
  if intersection_points.empty?
    nil
  elsif intersection_points.count == 2       
    Line.new(intersection_points[0], intersection_points[1])
  end
end

#linesObject



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

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

#to_sObject



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

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

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



47
48
49
50
51
# File 'lib/triangular/facet.rb', line 47

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