2015-12-23 23:05:09 +00:00
|
|
|
local current = (...):gsub('%.[^%.]+$', '');
|
|
|
|
local Node = require(current .. '.Node');
|
|
|
|
local Edge = require(current .. '.Edge');
|
2015-12-20 11:09:20 +00:00
|
|
|
|
|
|
|
local Graph = {};
|
|
|
|
|
|
|
|
function Graph.new()
|
|
|
|
local self = {};
|
|
|
|
|
|
|
|
local nodes = {};
|
|
|
|
local edges = {};
|
|
|
|
local edgeIDs = 0;
|
|
|
|
|
2015-12-23 23:39:15 +00:00
|
|
|
local minX, maxX, minY, maxY;
|
|
|
|
|
|
|
|
-- ------------------------------------------------
|
|
|
|
-- Local Functions
|
|
|
|
-- ------------------------------------------------
|
|
|
|
|
|
|
|
---
|
|
|
|
-- Sets the graph's boundaries to nil.
|
|
|
|
--
|
|
|
|
local function resetBoundaries()
|
|
|
|
minX, maxX, minY, maxY = nil, nil, nil, nil;
|
|
|
|
end
|
|
|
|
|
|
|
|
---
|
|
|
|
-- @param minX - The current minimum x position.
|
|
|
|
-- @param maxX - The current maximum y position.
|
|
|
|
-- @param minY - The current minimum x position.
|
|
|
|
-- @param maxY - The current maximum y position.
|
|
|
|
-- @param nx - The new x position to check.
|
|
|
|
-- @param ny - The new y position to check.
|
|
|
|
--
|
|
|
|
local function updateBoundaries( minX, maxX, minY, maxY, nx, ny )
|
|
|
|
return math.min( minX or nx, nx ), math.max( maxX or nx, nx ), math.min( minY or ny, ny ), math.max( maxY or ny, ny );
|
|
|
|
end
|
|
|
|
|
2016-01-09 02:41:31 +00:00
|
|
|
---
|
|
|
|
-- Adds a new edge between two nodes.
|
|
|
|
-- @param origin - The node from which the edge originates.
|
|
|
|
-- @param target - The node to which the edge is pointing to.
|
|
|
|
--
|
|
|
|
local function addEdge( origin, target )
|
|
|
|
for _, edge in pairs( edges ) do
|
|
|
|
if edge.origin == origin and edge.target == target then
|
|
|
|
error "Trying to connect nodes which are already connected.";
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert( origin ~= target, "Tried to connect a node with itself." );
|
|
|
|
edges[edgeIDs] = Edge.new( edgeIDs, origin, target );
|
|
|
|
edgeIDs = edgeIDs + 1;
|
|
|
|
end
|
|
|
|
|
2015-12-23 23:39:15 +00:00
|
|
|
-- ------------------------------------------------
|
|
|
|
-- Public Functions
|
|
|
|
-- ------------------------------------------------
|
|
|
|
|
2015-12-23 08:03:08 +00:00
|
|
|
---
|
|
|
|
-- Add a node to the graph.
|
2016-01-03 01:43:33 +00:00
|
|
|
-- @param id - The ID will be used to reference the Node inside of the graph.
|
|
|
|
-- @param x - The x coordinate the Node should be spawned at (optional).
|
|
|
|
-- @param y - The y coordinate the Node should be spawned at (optional).
|
|
|
|
-- @param anchor - Wether the node should be locked in place or not (optional).
|
2015-12-23 08:03:08 +00:00
|
|
|
--
|
2016-01-03 01:31:57 +00:00
|
|
|
function self:addNode( id, x, y, anchor )
|
2015-12-23 08:14:51 +00:00
|
|
|
assert( not nodes[id], "Node IDs must be unique." );
|
2016-01-03 01:31:57 +00:00
|
|
|
nodes[id] = Node.new( id, x, y, anchor );
|
2016-01-02 14:27:02 +00:00
|
|
|
return nodes[id];
|
2015-12-20 11:09:20 +00:00
|
|
|
end
|
|
|
|
|
2015-12-23 08:03:08 +00:00
|
|
|
---
|
2016-01-03 01:43:33 +00:00
|
|
|
-- Remove a node from the graph.
|
|
|
|
-- This will also remove all edges pointing to or originating from this node.
|
2015-12-23 08:03:08 +00:00
|
|
|
-- @param node - The node to remove from the graph.
|
|
|
|
--
|
2015-12-21 11:42:45 +00:00
|
|
|
function self:removeNode( node )
|
|
|
|
nodes[node:getID()] = nil;
|
|
|
|
|
|
|
|
self:removeEdges( node );
|
|
|
|
end
|
|
|
|
|
2015-12-23 08:03:08 +00:00
|
|
|
---
|
|
|
|
-- Adds a new edge between two nodes.
|
|
|
|
-- @param origin - The node from which the edge originates.
|
|
|
|
-- @param target - The node to which the edge is pointing to.
|
|
|
|
--
|
2016-01-09 02:35:51 +00:00
|
|
|
function self:connectNodes( origin, target )
|
2016-01-09 02:41:31 +00:00
|
|
|
addEdge( origin, target );
|
|
|
|
end
|
2015-12-21 11:36:38 +00:00
|
|
|
|
2016-01-09 02:41:31 +00:00
|
|
|
---
|
|
|
|
-- Adds a new edge between two nodes referenced by their IDs.
|
|
|
|
-- @param origin - The node id from which the edge originates.
|
|
|
|
-- @param target - The node id to which the edge is pointing to.
|
|
|
|
--
|
|
|
|
function self:connectIDs( originID, targetID )
|
|
|
|
addEdge( nodes[originID], nodes[targetID] );
|
2015-12-20 11:09:20 +00:00
|
|
|
end
|
|
|
|
|
2015-12-23 08:03:08 +00:00
|
|
|
---
|
|
|
|
-- Removes all edges leading to or originating from a node.
|
|
|
|
-- @param node - The node to remove all edges from.
|
|
|
|
--
|
2015-12-21 11:42:45 +00:00
|
|
|
function self:removeEdges( node )
|
|
|
|
for id, edge in pairs( edges ) do
|
|
|
|
if edge.origin == node or edge.target == node then
|
|
|
|
edges[id] = nil;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-23 08:03:08 +00:00
|
|
|
---
|
|
|
|
-- Updates the graph.
|
|
|
|
-- @param dt - The delta time between frames.
|
|
|
|
--
|
|
|
|
function self:update( dt )
|
2015-12-20 11:09:20 +00:00
|
|
|
for _, edge in pairs( edges ) do
|
|
|
|
edge.origin:attractTo( edge.target );
|
|
|
|
edge.target:attractTo( edge.origin );
|
|
|
|
end
|
|
|
|
|
2015-12-23 23:39:15 +00:00
|
|
|
resetBoundaries();
|
|
|
|
|
2015-12-20 11:09:20 +00:00
|
|
|
for _, nodeA in pairs( nodes ) do
|
2016-01-02 14:17:20 +00:00
|
|
|
if not nodeA:isAnchor() then
|
|
|
|
for _, nodeB in pairs( nodes ) do
|
|
|
|
if nodeA ~= nodeB then
|
|
|
|
nodeA:repelFrom( nodeB );
|
|
|
|
end
|
2015-12-20 11:09:20 +00:00
|
|
|
end
|
2016-01-02 14:17:20 +00:00
|
|
|
nodeA:move( dt );
|
2015-12-20 11:09:20 +00:00
|
|
|
end
|
2015-12-23 23:39:15 +00:00
|
|
|
|
|
|
|
minX, maxX, minY, maxY = updateBoundaries( minX, maxX, minY, maxY, nodeA:getPosition() );
|
2015-12-20 11:09:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-24 02:24:40 +00:00
|
|
|
---
|
|
|
|
-- Checks if the id points to an existing node.
|
|
|
|
-- @param id - The id to check for.
|
|
|
|
--
|
|
|
|
function self:exists( id )
|
|
|
|
return nodes[id] ~= nil;
|
|
|
|
end
|
|
|
|
|
2015-12-23 08:03:08 +00:00
|
|
|
---
|
|
|
|
-- This function receives a single parameter of type function to which it
|
|
|
|
-- will pass the edges and nodes tables. This means the user has to provide
|
|
|
|
-- his own drawing function.
|
|
|
|
-- @param func - The function to pass the tables to.
|
|
|
|
--
|
2015-12-23 07:38:42 +00:00
|
|
|
function self:draw( func )
|
|
|
|
func( edges, nodes );
|
2015-12-20 11:09:20 +00:00
|
|
|
end
|
|
|
|
|
2016-01-09 01:02:44 +00:00
|
|
|
---
|
|
|
|
-- Checks if a certain Node ID already exists.
|
|
|
|
-- @param id - The id to check for.
|
|
|
|
--
|
|
|
|
function self:hasNode( id )
|
|
|
|
return nodes[id] ~= nil;
|
|
|
|
end
|
|
|
|
|
2015-12-24 02:24:57 +00:00
|
|
|
---
|
|
|
|
-- Returns the node the id is pointing to.
|
2016-01-03 01:43:33 +00:00
|
|
|
-- @param id - The id to check for.
|
|
|
|
--
|
2015-12-24 02:24:57 +00:00
|
|
|
function self:getNode( id )
|
|
|
|
return nodes[id];
|
|
|
|
end
|
|
|
|
|
2015-12-23 08:03:08 +00:00
|
|
|
---
|
|
|
|
-- Gets a node at a certain point in the graph.
|
|
|
|
-- @param x - The x coordinate to check.
|
|
|
|
-- @param y - The y coordinate to check.
|
|
|
|
-- @param range - The range in which to check around the given coordinates.
|
|
|
|
--
|
2015-12-20 11:09:20 +00:00
|
|
|
function self:getNodeAt(x, y, range)
|
|
|
|
for _, node in pairs( nodes ) do
|
|
|
|
local nx, ny = node:getPosition();
|
|
|
|
if x < nx + range and x > nx - range and y < ny + range and y > ny - range then
|
|
|
|
return node;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-23 23:39:15 +00:00
|
|
|
---
|
|
|
|
-- Returns the graph's minimum and maxmimum x and y values.
|
|
|
|
--
|
|
|
|
function self:getBoundaries()
|
|
|
|
return minX, maxX, minY, maxY;
|
|
|
|
end
|
|
|
|
|
2015-12-23 23:40:50 +00:00
|
|
|
---
|
|
|
|
-- Returns the x and y coordinates of the graph's center.
|
|
|
|
--
|
|
|
|
function self:getCenter()
|
|
|
|
return ( ( maxX - minX ) * 0.5 ) + minX, ( ( maxY - minY ) * 0.5 ) + minY;
|
|
|
|
end
|
|
|
|
|
2016-01-03 01:43:33 +00:00
|
|
|
---
|
|
|
|
-- Turn a node into an anchor.
|
|
|
|
-- Anchored nodes have fixed positions and can't be moved by the physical
|
|
|
|
-- forces.
|
|
|
|
-- @param id - The node's id.
|
|
|
|
-- @param x - The x coordinate to anchor the node to.
|
|
|
|
-- @param y - The x coordinate to anchor the node to.
|
|
|
|
--
|
2016-01-02 14:17:20 +00:00
|
|
|
function self:setAnchor( id, x, y )
|
|
|
|
nodes[id]:setPosition( x, y );
|
|
|
|
nodes[id]:setAnchor( true );
|
|
|
|
end
|
|
|
|
|
2015-12-20 11:09:20 +00:00
|
|
|
return self;
|
|
|
|
end
|
|
|
|
|
2016-01-09 04:06:54 +00:00
|
|
|
---
|
|
|
|
-- Replaces the default Edge class with a custom one.
|
|
|
|
-- @param class - The custom Edge class to use.
|
|
|
|
--
|
|
|
|
function Graph.setEdgeClass( class )
|
|
|
|
Edge = class;
|
|
|
|
end
|
|
|
|
|
|
|
|
---
|
|
|
|
-- Replaces the default Node class with a custom one.
|
|
|
|
-- @param class - The custom Node class to use.
|
|
|
|
--
|
|
|
|
function Graph.setNodeClass( class )
|
|
|
|
Node = class;
|
|
|
|
end
|
|
|
|
|
2015-12-20 11:09:20 +00:00
|
|
|
return Graph;
|