-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_assemble.c
148 lines (124 loc) · 3.47 KB
/
main_assemble.c
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
#include <graphviz/cgraph.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "layout.h"
#include "utils.h"
void usage(char *command_name) {
fprintf(stderr, "usage: %s\n", command_name);
fprintf(stderr, "Reads coordinates from stdin and adds edges between adjacent\n");
fprintf(stderr, "rectangular coordinates into a shaped grid, and writes\n");
fprintf(stderr, "a dot graph to stdout.\n");
fprintf(stderr, "\n");
fprintf(stderr, "Input should be lines of three numbers, separated by commas, like:\n");
fprintf(stderr, "1,2,3\n");
}
int main(int argc, char** argv) {
char *command_name = argv[0];
char label[256];
char attrvalue[12];
Agraph_t *maze = agopen("maze", Agstrictundirected, NULL);
if (NULL == maze) {
ERROR_EXIT("Cannot allocate new maze");
}
int max_x = 0;
int max_y = 0;
int max_z = 0;
while (!feof(stdin)) {
int x, y, z;
int ret = fscanf(stdin, " %d , %d , %d ", &x, &y, &z);
if (3 != ret) {
fprintf(stderr, "Stdin should be a set of comma separated number triples\n");
usage(command_name);
return 1;
}
if (x < 0 || y < 0 || z < 0) {
fprintf(stderr, "All coordinates must be greater than or equal to zero\n");
usage(command_name);
return 1;
}
if (INT_MAX == x || INT_MAX == y || INT_MAX == z) {
fprintf(stderr, "Coordinates are too large\n");
usage(command_name);
return 1;
}
if (max_x < x) {
max_x = x;
}
if (max_y < y) {
max_y = y;
}
if (max_z < z) {
max_z = z;
}
checked_snprintf(label, sizeof(label), "node_%ld_%ld_%ld", x, y, z);
Agnode_t *node = agnode(maze, label, true);
if (NULL == node) {
ERROR_EXIT("Can't allocate a graph node");
}
checked_snprintf(attrvalue, sizeof(attrvalue), "%ld", x);
agsafeset(node, "_0", attrvalue, "");
checked_snprintf(attrvalue, sizeof(attrvalue), "%ld", y);
agsafeset(node, "_1", attrvalue, "");
checked_snprintf(attrvalue, sizeof(attrvalue), "%ld", z);
agsafeset(node, "_2", attrvalue, "");
}
max_x++;
max_y++;
max_z++;
struct maze_grid grid = maze_read_grid(maze);
for (size_t i = 0; i < grid.nodes_count; i++) {
Agnode_t *node = grid.nodes[i];
struct maze_pt3 node_pos;
if (0 != maze_read_location(maze, node, &node_pos)) {
ERROR_EXIT("gridded nodes should always have locations");
}
struct maze_pt3 other_pos;
Agnode_t *other;
other_pos = node_pos;
if (other_pos.x > 0) {
other_pos.x--;
if (NULL != (other = maze_find_in_grid_at_pt(maze,grid, other_pos))) {
agedge(maze, node, other, NULL, 1);
}
}
other_pos = node_pos;
if (other_pos.y > 0) {
other_pos.y--;
if (NULL != (other = maze_find_in_grid_at_pt(maze,grid, other_pos))) {
agedge(maze, node, other, NULL, 1);
}
}
other_pos = node_pos;
if (other_pos.z > 0) {
other_pos.z--;
if (NULL != (other = maze_find_in_grid_at_pt(maze,grid, other_pos))) {
agedge(maze, node, other, NULL, 1);
}
}
other_pos = node_pos;
if (other_pos.x < max_x - 1) {
other_pos.x++;
if (NULL != (other = maze_find_in_grid_at_pt(maze,grid, other_pos))) {
agedge(maze, node, other, NULL, 1);
}
}
other_pos = node_pos;
if (other_pos.y < max_y - 1) {
other_pos.y++;
if (NULL != (other = maze_find_in_grid_at_pt(maze,grid, other_pos))) {
agedge(maze, node, other, NULL, 1);
}
}
other_pos = node_pos;
if (other_pos.z < max_z - 1) {
other_pos.z++;
if (NULL != (other = maze_find_in_grid_at_pt(maze,grid, other_pos))) {
agedge(maze, node, other, NULL, 1);
}
}
}
agwrite(maze, stdout);
agclose(maze);
}