Class: Zadt::FaceGraph

Inherits:
Graph
  • Object
show all
Defined in:
lib/zadt/AbstractDataTypes/Graph/face_graph.rb

Instance Attribute Summary collapse

Attributes inherited from Graph

#edges, #value, #vertices

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Graph

#add_vertex, #break_connection, #find_connection, #is_connected?, #make_connection, #remove_vertex

Constructor Details

#initializeFaceGraph

Contains (inherits) attr_accessor :value



13
14
15
16
17
# File 'lib/zadt/AbstractDataTypes/Graph/face_graph.rb', line 13

def initialize
  #@faces is ALL faces on the graph
  @faces = []
  super
end

Instance Attribute Details

#facesObject (readonly)

Made up of



7
8
9
# File 'lib/zadt/AbstractDataTypes/Graph/face_graph.rb', line 7

def faces
  @faces
end

Class Method Details

.helpObject



19
20
21
# File 'lib/zadt/AbstractDataTypes/Graph/face_graph.rb', line 19

def self.help
  FaceGraph.show_help_message
end

Instance Method Details

#add_attached_face(vertex_array, num_edges) ⇒ Object

This adds a face that will be attached to the given vertices

Make sure the vertices are connected, or it will raise an error

All new vertices and edges will be created This will automatically make_neighbors with any faces that share

a vertex with the new face


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zadt/AbstractDataTypes/Graph/face_graph.rb', line 62

def add_attached_face(vertex_array, num_edges)
  vertex_array_check(vertex_array)
  num_edges_check(num_edges)
  # Make the vertices into a line
  vertex_line = confirm_vertex_line(vertex_array)
  # This finds the "ends" of the vertex line
  end_vertices = [vertex_line.first, vertex_line.last]
  # Find the neighbors that will be added later
  new_neighbors = find_neighbors(vertex_array)
  # How many vertices and edges to be made
  vertices_to_make = num_edges - vertex_array.length
  edges_to_make = vertices_to_make + 1

  # Make new vertices
  vert_ref = Array.new(vertices_to_make) {Vertex.new}

  edge_ref = []
  # Connect new vertices in a line
  (edges_to_make - 2).times do |vert_id|
    # Connect each vertex to the one after it
    edge_ref << make_connection(vert_ref[vert_id], vert_ref[vert_id + 1])
  end
  # Connect "ends" of new vertices to "ends" of vertex line (making a circuit)
  # Connect "first" of new vertices to "last end" of old ones
  edge_ref << make_connection(vert_ref.first, end_vertices.last)
  # Connect "last" of new vertices to "first end" of old ones
  edge_ref << make_connection(vert_ref.last, end_vertices.first)

  # Add edges from vertex_line to edge_ref
  (vertex_line.length - 1).times do |vert_id|
    edge_ref << find_connection(vertex_line[vert_id], vertex_line[vert_id + 1])
  end

  face_border = edge_ref
  # Make a face out of the new circuit, and store it
  face = add_face(face_border)
  # Store the new vertices
  @vertices += vert_ref
  # Store the new edges
  @edges += edge_ref

  face
end

#add_face(edges_array) ⇒ Object



28
29
30
31
32
33
# File 'lib/zadt/AbstractDataTypes/Graph/face_graph.rb', line 28

def add_face(edges_array)
  edge_array_check(edges_array)
  face = Face.new(edges_array)
  @faces << face
  face
end

#find_face_neighbors(face) ⇒ Object

Neighbor is defined as sharing a vertex, not necessarily sharing an edge.



121
122
123
124
125
# File 'lib/zadt/AbstractDataTypes/Graph/face_graph.rb', line 121

def find_face_neighbors(face)
  raise "not a face" unless face.is_a?(Face)
  neighbors = find_neighbors(face.vertices)
  neighbors - [face]
end

#find_neighbors(vertex_array) ⇒ Object

Return all faces containing the given vertices



107
108
109
110
111
112
113
114
115
116
# File 'lib/zadt/AbstractDataTypes/Graph/face_graph.rb', line 107

def find_neighbors(vertex_array)
  vertex_array_check(vertex_array)
  neighbors = []
  vertex_array.each do |vertex|
    @faces.each do |face|
      neighbors << face if face.vertices.include?(vertex)
    end
  end
  neighbors.uniq
end

#helpObject



23
24
25
# File 'lib/zadt/AbstractDataTypes/Graph/face_graph.rb', line 23

def help
  FaceGraph.help
end

#make_original_face(num_edges) ⇒ Object

Makes a face with num_edges edges, which will be attached to nothing.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zadt/AbstractDataTypes/Graph/face_graph.rb', line 36

def make_original_face(num_edges)
  num_edges_check(num_edges)
  # Make the vertices
  vert_ref = Array.new(num_edges) {Vertex.new}
  edge_ref = []

  # Connect each vertex to the one before it (including the first one :)
  (num_edges).times do |vert_id|
    edge_ref << make_connection(vert_ref[vert_id - 1], vert_ref[vert_id])
  end

  # Make the face and store it
  face = add_face(edge_ref)
  # Store the new vertices
  @vertices += vert_ref
  # Store the new edges
  @edges += edge_ref

  face
end