-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorizer.php
More file actions
executable file
·190 lines (155 loc) · 5.65 KB
/
Copy pathvectorizer.php
File metadata and controls
executable file
·190 lines (155 loc) · 5.65 KB
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
<?php
require("vendor/autoload.php");
require("Svg/SvgBuilder.php");
require("Svg/NodeArity.php");
require("Svg/NodeArityCollection.php");
require("Svg/NodeColor.php");
require("Svg/NodeColorCollection.php");
require("Svg/SvgNode.php");
require("Svg/SvgPolygon.php");
$client = new Everyman\Neo4j\Client();
$queryString = "
MATCH (n1)-[b:BORDER]-(n2)
WHERE b.weight > {threshold}
AND (n1.x < n2.x OR n1.y < n2.y)
RETURN n1, n2
ORDER BY n1.x, n2.y
";
$query = new Everyman\Neo4j\Cypher\Query(
$client,
$queryString,
array(
'threshold' => 0.1
)
);
$result = $query->getResultSet();
$nodes = array();
$arities = new NodeArityCollection();
$index = array();
$colors = new NodeColorCollection();
foreach($result as $row) {
$node1 = new SvgNode($row['n1']);
$node2 = new SvgNode($row['n2']);
$node1Id = $node1->getDbNode()->getId();
$node2Id = $node2->getDbNode()->getId();
if ($arities->issetNodeArity($node1Id)) {
$arities->getNodeArity($node1Id)->addNeighbour($node2Id);
} else {
$arities->setNodeArity($node1Id, new NodeArity(array($node2Id)));
$nodes[] = $node1Id;
$index[$node1Id] = $node1;
$colors->initNodeColor($node1Id);
}
if ($arities->issetNodeArity($node2Id)) {
$arities->getNodeArity($node2Id)->addNeighbour($node1Id);
} else {
$arities->setNodeArity($node2Id, new NodeArity(array($node1Id)));
$nodes[] = $node2Id;
$index[$node2Id] = $node2;
$colors->initNodeColor($node2Id);
}
}
do {
$previousCount = count($nodes);
$nodes = purgeUselessNodes($nodes, $arities, $index);
$currentCount = count($nodes);
} while($previousCount !== $currentCount);
$polygons = searchPolygons($nodes, $arities, $index, $colors);
//var_dump(count($polygons));
$svgBuilder = new SvgBuilder();
file_put_contents(__DIR__ . '/test.svg', $svgBuilder->traceSvg($polygons));
function searchPolygons($nodes, NodeArityCollection $arities, $index, NodeColorCollection $colors) {
$polygons = array();
$colorId = 0;
foreach($nodes as $nodeId) {
$colorId++;
$node = $index[$nodeId];
$arity = $arities->getNodeArity($node->getDbNode()->getId())->count();
$colorsCount = $colors->getNodeColor($node->getDbNode()->getId())->count();
$isBorder = $node->isBorder();
//echo $node->getProperty('x') . ' - ' . $node->getProperty('y') . " : $arity - $colorsCount\n";
if ((! $isBorder && $arity > $colorsCount) || ($isBorder && $arity - 1 > $colorsCount))
{
//echo "$nodeId - " . $node->getDbNode()->getProperty('x') . ' : ' . $node->getDbNode()->getProperty('y') . "\n";
$polygon = new SvgPolygon();
$polygon->setColor($colorId);
try {
searchPolygon($polygon, $node, $arities, $index, $colors);
//var_dump(count($polygon));
$polygons[] = $polygon;
} catch( Exception $e) {
echo $e->getMessage() . "\n";
}
}
}
return $polygons;
}
function purgeUselessNodes($nodes, NodeArityCollection $arities, $index)
{
$result = array();
$removed = array();
foreach($nodes as $nodeId) {
$node = $index[$nodeId];
$arity = $arities->getNodeArity($node->getDbNode()->getId());
if ($arity->count() > 1) {
$result[] = $nodeId;
$removed[$node->getDbNode()->getId()] = false;
} else {
$removed[$node->getDbNode()->getId()] = true;
}
}
foreach($arities->getAllNodeArity() as $id => $arity) {
if ($removed[$id]) {
$arities->removeNodeArity($id);
continue;
}
foreach($arity->getNeighbours() as $n => $neighbour) {
if ($removed[$neighbour]) {
$arity->removeNeighbour($n);
}
}
}
return $result;
}
function searchPolygon(SvgPolygon $currentPolygon, SvgNode $currentNode, NodeArityCollection $arities, $index, NodeColorCollection $colors) {
//echo "## - " . $currentNode->getDbNode()->getProperty('x') . ' : ' . $currentNode->getDbNode()->getProperty('y') . "\n";
if($currentPolygon->isEmpty()) {
// polygon beginning
$lastNode = $currentNode;
$nextNode = getNextNode($currentNode, $lastNode, $arities, $index, $colors);
$currentPolygon->addPoint($currentNode);
$res = searchPolygon($currentPolygon, $nextNode, $arities, $index, $colors);
$colors->getNodeColor($currentNode->getDbNode()->getId())->addColor($currentPolygon->getColor());
return $res;
}
if($currentPolygon->isClosed($currentNode)) {
// the polygon is closed
return $currentPolygon;
}
$lastNode = $currentPolygon->getLastPoint();
$currentPolygon->addPoint($currentNode);
$colors->getNodeColor($currentNode->getDbNode()->getId())->addColor($currentPolygon->getColor());
if($colors->getNodeColor($currentNode->getDbNode()->getId())->count() > $arities->getNodeArity($currentNode->getDbNode()->getId())->count()) {
echo "erreur";
return $currentPolygon;
}
$nextNode = getNextNode($currentNode, $lastNode, $arities, $index, $colors);
return searchPolygon($currentPolygon, $nextNode, $arities, $index, $colors);
}
function getNextNode(SvgNode $currentNode, SvgNode $lastNode, NodeArityCollection $arities, $index, NodeColorCollection $colors) {
$arity = $arities->getNodeArity($currentNode->getDbNode()->getId());
$neighbours = $arity->getPriorizedNeighbours($currentNode, $lastNode, $index);
foreach(array(-1, 0, 1) as $priority) {
if (isset($neighbours[$priority])) {
$isBorder = $neighbours[$priority]->isBorder();
$arity = $arities->getNodeArity($neighbours[$priority]->getDbNode()->getId())->count();
$colorsCount = $colors->getNodeColor($neighbours[$priority]->getDbNode()->getId())->count();
//echo $node->getProperty('x') . ' - ' . $node->getProperty('y') . " : $arity - $colorsCount\n";
if ((! $isBorder && $arity > $colorsCount) || ($isBorder && $arity - 1 > $colorsCount))
{
return $neighbours[$priority];
}
}
}
throw new Exception('No neighbour ??? for ' . $currentNode->getDbNode()->getId() . " - " . count($neighbours));
}