-
Notifications
You must be signed in to change notification settings - Fork 0
/
scc_box.c
290 lines (254 loc) · 6.83 KB
/
scc_box.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
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
281
282
283
284
285
286
287
288
289
290
/* ScummC
* Copyright (C) 2004-2007 Alban Bedel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/**
* @file scc_box.c
* @brief Common box stuff
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "scc_box.h"
void scc_box_list_free(scc_box_t* box) {
scc_box_t* n;
while(box) {
n = box->next;
free(box);
box = n;
}
}
int scc_box_add_pts(scc_box_t* box,int x,int y) {
int i;
if(box->npts >= 4) return 0;
// look if don't alredy have this point
for(i = 0 ; i < box->npts ; i++)
if(box->pts[i].x == x && box->pts[i].y == y) return 1;
box->pts[i].x = x;
box->pts[i].y = y;
box->npts++;
return 1;
}
int scc_box_are_neighbors(scc_box_t* box,int n1,int n2) {
scc_box_t *a = NULL,*b = NULL,*c;
int n,m;
int i,j;
int f,f2,l,l2;
// box 0 is connected to nothing
if((!n1) || (!n2)) return 0;
for(n = 1, c = box ; c ; n++,c = c->next) {
if(n == n1) a = c;
if(n == n2) b = c;
}
if(!(a && b)) {
printf("Failed to find boxes %d and/or %d.\n",n1,n2);
return 0;
}
if((a->flags & SCC_BOX_INVISIBLE) || (b->flags & SCC_BOX_INVISIBLE))
return 0;
for(n = 0 ; n < a->npts ; n++) {
m = (n+1)%a->npts;
// is it an vertical side
if(a->pts[n].x == a->pts[m].x) {
// look at tho other box
for(i = 0 ; i < b->npts ; i++) {
j = (i+1)%b->npts;
// ignore side which are not on the same vertical
if((b->pts[i].x != a->pts[n].x) ||
(b->pts[j].x != a->pts[n].x)) continue;
if(a->pts[n].y < a->pts[m].y)
f = n, l = m;
else
f = m, l = n;
if(b->pts[i].y < b->pts[j].y)
f2 = i, l2 = j;
else
f2 = j, l2 = i;
if((a->pts[l].y < b->pts[f2].y) ||
(a->pts[f].y > b->pts[l2].y)) continue;
return 1;
}
}
// is it an horizontal line
if(a->pts[n].y == a->pts[m].y) {
// look at the other box
for(i = 0 ; i < b->npts ; i++) {
j = (i+1)%b->npts;
if((b->pts[i].y != a->pts[n].y) ||
(b->pts[j].y != a->pts[n].y)) continue;
if(a->pts[n].x < a->pts[m].x)
f = n, l = m;
else
f = m, l = n;
if(b->pts[i].x < b->pts[j].x)
f2 = i, l2 = j;
else
f2 = j, l2 = i;
if((a->pts[l].x < b->pts[f2].x) ||
(a->pts[f].x > b->pts[l2].x)) continue;
return 1;
}
}
}
return 0;
}
// code pumped from scummvm
// changed to make it work with any matrix size
int scc_box_get_matrix(scc_box_t* box,uint8_t** ret) {
scc_box_t* b;
int i,j,k,num = 0;
uint8_t *adjacentMatrix, *itineraryMatrix;
// count the boxes
for(b = box ; b ; b = b->next) num++;
// add the box 0
num++;
// Allocate the adjacent & itinerary matrices
adjacentMatrix = malloc(num * num);
itineraryMatrix = malloc(num * num);
// Initialise the adjacent matrix: each box has distance 0 to itself,
// and distance 1 to its direct neighbors. Initially, it has distance
// 255 (= infinity) to all other boxes.
for (i = 0; i < num; i++) {
for (j = 0; j < num; j++) {
if (i == j) {
adjacentMatrix[i*num+j] = 0;
itineraryMatrix[i*num+j] = j;
} else if (scc_box_are_neighbors(box,i,j)) {
adjacentMatrix[i*num+j] = 1;
itineraryMatrix[i*num+j] = j;
} else {
adjacentMatrix[i*num+j] = 255;
itineraryMatrix[i*num+j] = 255;
}
}
}
// Compute the shortest routes between boxes via Kleene's algorithm.
for (k = 0; k < num; k++) {
for (i = 0; i < num; i++) {
for (j = 0; j < num; j++) {
uint8_t distIK,distKJ;
if (i == j)
continue;
distIK = adjacentMatrix[num*i+k];
distKJ = adjacentMatrix[num*k+j];
if (adjacentMatrix[num*i+j] > distIK + distKJ) {
adjacentMatrix[num*i+j] = distIK + distKJ;
itineraryMatrix[num*i+j] = itineraryMatrix[num*i+k];
}
}
}
}
free(adjacentMatrix);
ret[0] = itineraryMatrix;
return num;
}
long long scc_box_adjust_point(scc_box_t* b,int x, int y,
int* dst_x, int* dst_y) {
long long dst_dist = -1;
scc_box_pts_t dst = { .x = x, .y = y };
int i,inside = 0;
// Project the point on each side and count
// how often it is on the inner side.
for(i = 0 ; i < b->npts ; i++) {
scc_box_pts_t pts;
int j = (i+1)%b->npts;
long long dist;
int dx = b->pts[j].x - b->pts[i].x;
int dy = b->pts[j].y - b->pts[i].y;
if(abs(dx) >= abs(dy)) { // Vertical projection
int left,right;
if(b->pts[i].x <= b->pts[j].x)
left = i, right = j;
else
left = j, right = i;
if(x < b->pts[left].x)
pts = b->pts[left];
else if(x > b->pts[right].x)
pts = b->pts[right];
else {
pts.x = x;
pts.y = b->pts[i].y;
if(dx) pts.y += (x-b->pts[i].x)*dy/dx;
}
if((dx >= 0 && pts.y <= y) ||
(dx < 0 && pts.y >= y))
inside++;
} else { // Horizontal projection
int top,bottom;
if(b->pts[i].y <= b->pts[j].y)
top = i, bottom = j;
else
top = j, bottom = i;
if(y < b->pts[top].y)
pts = b->pts[top];
else if(y > b->pts[bottom].y)
pts = b->pts[bottom];
else {
pts.y = y;
pts.x = b->pts[i].x;
if(dy) pts.x += (y-b->pts[i].y)*dx/dy;
}
if((dy >= 0 && pts.x >= x) ||
(dy < 0 && pts.x <= x))
inside++;
}
dx = pts.x-x;
dy = pts.y-y;
dist = (long long)dx*dx + (long long)dy*dy;
if(dst_dist < 0 || dist < dst_dist) {
dst = pts;
dst_dist = dist;
}
}
// Inside a box, no need to move the point
if(inside >= b->npts) {
*dst_x = x;
*dst_y = y;
return -1;
}
*dst_x = dst.x;
*dst_y = dst.y;
return dst_dist;
}
scc_box_t* scc_boxes_adjust_point(scc_box_t* box,int x, int y,
int* dst_x, int* dst_y) {
scc_box_t* b;
scc_box_t* dst_box = NULL;
long long dst_dist = 0;
scc_box_pts_t dst = { .x = x, .y = y };
for(b = box ; b ; b = b->next) {
scc_box_pts_t pts;
long long dist;
if((dist = scc_box_adjust_point(b,x,y,&pts.x,&pts.y)) < 0) {
dst_box = b;
dst = pts;
break;
}
if(!dst_box || dist < dst_dist) {
dst_box = b;
dst = pts;
dst_dist = dist;
}
}
*dst_x = dst.x;
*dst_y = dst.y;
return dst_box;
}