summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/deprecated/Node.lua
blob: 84d3d3ee11f319e9f6e37ffb020023110cae85b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
-- Copyright 2010 by Renée Ahrens, Olof Frahm, Jens Kluttig, Matthias Schulz, Stephan Schuster
-- Copyright 2011 by Jannis Pohlmann
-- Copyright 2012 by Till Tantau
--
-- This file may be distributed an/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License
--
-- See the file doc/generic/pgf/licenses/LICENSE for more information

-- @release $Header$



--- The Node class
--
--

local Node = {}
Node.__index = Node


-- Namespace

local lib = require "pgf.gd.lib"


-- Imports

local Vector = require "pgf.gd.deprecated.Vector"



--- Creates a new node.
--
-- @param values  Values to override default node settings.
--                The following parameters can be set:\par
--                |name|: The name of the node. It is obligatory to define this.\par
--                |tex|:  Information about the corresponding \TeX\ node.\par
--                |edges|: Edges adjacent to the node.\par
--                |pos|: Initial position of the node.\par
--                |options|: A table of node options passed over from \tikzname.
--
-- @return A newly allocated node.
--
function Node.new(values)
  local new = {
    class = Node,
    name = nil,
    tex = { 
      -- texNode = nil,
      -- maxX = nil,
      -- minX = nil,
      -- maxY = nil,
      -- minY = nil 
    },
    edges = {},
    -- pos = nil,
    options = {},
    -- growth_direction = nil,
    -- index = nil,
    -- event_index = nil,
    kind = "node",
  }
  setmetatable(new, Node)
  if values then
    for k,v in pairs(values) do
      new [k] = v
    end
  end
  if not new.pos then 
    new.pos = Vector.new(2) 
  end
  return new
end



--- Sets the node option \meta{name} to \meta{value}.
--
-- @param name Name of the node option to be changed.
-- @param value New value for the node option \meta{name}.
--
function Node:setOption(name, value)
  self.options[name] = value
end



--- Returns the value of the node option \meta{name}.
--
-- @param name Name of the node option.
-- @param graph If this optional argument is given, 
--        in case the option is not set as a node parameter, 
--        we try to look it up as a graph parameter.
--
-- @return The value of the node option \meta{name} or |nil|.
--
function Node:getOption(name, graph)
  return lib.lookup_option(name, self, graph)
end



--- Computes the width of the node.
--
-- @return Width of the node.
--
function Node:getTexWidth()
  return math.abs(self.tex.maxX - self.tex.minX)
end



--- Computes the heigth of the node.
--
-- @return Height of the node.
--
function Node:getTexHeight()
  return math.abs(self.tex.maxY - self.tex.minY)
end



--- Adds new edge to the node.
--
-- @param edge The edge to be added.
--
function Node:addEdge(edge)
  table.insert(self.edges, edge)
end



--- Removes an edge from the node.
--
-- @param edge The edge to remove.
--
function Node:removeEdge(edge)
  self.edges = lib.imap (self.edges, function(other) if other ~= edge then return other end end)
end



--- Counts the adjacent edges of the node.
--
-- @return The number of adjacent edges of the node.
--
function Node:getDegree()
  return #self.edges
end



--- Returns all edges of the node.
--
-- Instead of calling |node:getEdges()| the edges can alternatively be 
-- accessed directly with |node.edges|.
--
-- @return All edges of the node.
--
function Node:getEdges()
  return self.edges
end



--- Returns the incoming edges of the node. Undefined result for hyperedges.
--
-- @param ignore_reversed Optional parameter to consider reversed edges not 
--                        reversed for this method call. Defaults to |false|.
--
-- @return Incoming edges of the node. This includes undirected edges
--         and directed edges pointing to the node.
--
function Node:getIncomingEdges(ignore_reversed)
  return lib.imap(self.edges,
		  function (edge) 
		    if edge:isHead(self, ignore_reversed) then return edge end
		  end)
end



--- Returns the outgoing edges of the node. Undefined result for hyperedges.
--
-- @param ignore_reversed Optional parameter to consider reversed edges not 
--                        reversed for this method call. Defaults to |false|.
--
-- @return Outgoing edges of the node. This includes undirected edges
--         and directed edges leaving the node.
--
function Node:getOutgoingEdges(ignore_reversed)
  return lib.imap(self.edges,
		  function (edge) 
		    if edge:isTail(self, ignore_reversed) then return edge end
		  end)
end



--- Returns the number of incoming edges of the node.
--
-- @see Node:getIncomingEdges(reversed)
--
-- @param ignore_reversed Optional parameter to consider reversed edges not 
--                        reversed for this method call. Defaults to |false|.
--
-- @return The number of incoming edges of the node.
--
function Node:getInDegree(ignore_reversed)
  return #self:getIncomingEdges(ignore_reversed)
end



--- Returns the number of edges starting at the node.
--
-- @see Node:getOutgoingEdges()
--
-- @param ignore_reversed Optional parameter to consider reversed edges not 
--                        reversed for this method call. Defaults to |false|.
--
-- @return The number of outgoing edges of the node.
--
function Node:getOutDegree(ignore_reversed)
  return #self:getOutgoingEdges(ignore_reversed)
end



--- Creates a shallow copy of the node. 
--
-- Most notably, the edges adjacent are not preserved in the copy.
--
-- @return Copy of the node.
--
function Node:copy()
  local result = lib.copy(self, Node.new())
  result.edges = {}
  return result
end



--- Compares two nodes by their name.
--
-- @ignore This should not appear in the documentation.
--
-- @param other Another node to compare with.
--
-- @return |true| if both nodes have the same name. |false| otherwise.
--
function Node:__eq(object)
  return self.name == object.name
end



--- Returns a formated string representation of the node.
--
-- @ignore This should not appear in the documentation.
--
-- @return String represenation of the node.
--
function Node:__tostring()
  local tmp = Node.__tostring
  Node.__tostring = nil
  local result = "Node<" .. tostring(self) .. ">(" .. self.name .. ")"
  Node.__tostring = tmp
  return result
end




-- Done

return Node