-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfind_grid.cc
1445 lines (1243 loc) · 59.1 KB
/
find_grid.cc
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <sys/stat.h>
#include <stdio.h>
#include <vector>
#include <map>
#include <set>
#include <climits>
#include <boost/polygon/voronoi.hpp>
#include <assert.h>
#include "point.hh"
#include "mrgingham.hh"
#include "mrgingham-internal.h"
using namespace mrgingham;
// From the boost voronoi-diagram tutorial. Don't ask.
using boost::polygon::voronoi_diagram;
namespace boost { namespace polygon {
template <>
struct geometry_concept<PointInt> {
typedef point_concept type;
};
template <>
struct point_traits<PointInt> {
typedef int coordinate_type;
static inline coordinate_type get(const PointInt& point, orientation_2d orient) {
return (orient == HORIZONTAL) ? point.x : point.y;
}
};
}}
typedef voronoi_diagram<double> VORONOI;
/*
Voronoi library terminology. Each "cell" c has an edge = c->incident_edge(). The
edge e points FROM the cell e->cell(). The edges are a linked list. e->next() is
the next edge from c, moving clockwise. e->prev() moves counterclockwise(). An
edge e A->B has an inverse e->twin() B->A.
For any voronoi vertex ("cell" in the terminology of this voronoi library) I
want look at all the neighboring cells. The simplest thing to do is to look at
all the vertices directly connected to this vertex with an edge. This works most
of the time. But for very skewed views of a chessboard this sometimes isn't
enough, and I look at the in-between vertices too. From vertex A below I would
previously consider E and B and C as neighbors, but I now also want to consider
D
E--B-----D
| / \ /
|/ \ /
A-----C
How do I find D? Let e be A->B. Then D =
e->twin()->prev()->prev()->twin()->cell()
There're a few topological wrinkles that this scheme creates. First off, at the
boundary of the graph, there may not exist an "inbetween" vertex. In the example
above there's no vertex between C and E from A (looking clockwise). I check by
making sure that e->twin()->prev()->twin()->cell() == e->next()->twin()->cell().
In the "good" example of D being between B and C, both should point to C.
I can also have more complex topologies, which would create duplicate neighbors.
For instance if I have this:
---F
-/ /|
/ / |
A---H |
\ \ |
-\ \|
---G
Looking from A, vertex G lies between F,H. But G is directly connected to A, so
I would already use this as a neighbor. There's a whole class of such issues,
and I avoid them by only selecting vertices that are monotonic in angle, moving
around A. So in the above example the midpoint MUST lie to the right of F and to
the left of H. But G lies to the right of H, so I do not report an in-between
vertex between F and H.
This is all a bit heuristic, but is sufficient for the purposes of chessboard
grid finding.
*/
#define FOR_ALL_ADJACENT_CELLS(c) do { \
const PointInt* pt = &points[c->source_index()]; \
const VORONOI::edge_type* const __e0 = c->incident_edge(); \
bool __first = true; \
for(const VORONOI::edge_type* __e = __e0; \
__e0 != NULL && (__e != __e0 || __first); \
__e = __e->next(), __first=false) \
{ \
/* Look for two neighbors using this edge. The edge itself, */ \
/* and the vertex between this edge and the next edge */ \
for(int __i=0; __i<2; __i++) \
{ \
const VORONOI::cell_type* c_adjacent = __e->twin()->cell(); \
const PointInt* pt_adjacent = &points[c_adjacent->source_index()]; \
if(__i == 0) \
; /* use c_adjacent, pt_adjacent above */ \
else \
{ \
const VORONOI::cell_type* c0 = c_adjacent; \
const PointInt* pt0 = pt_adjacent; \
const VORONOI::cell_type* c1 = __e->next()->twin()->cell(); \
const PointInt* pt1 = &points[c1->source_index()]; \
PointInt v0( pt0->x - pt->x, pt0->y - pt->y ); \
PointInt v1( pt1->x - pt->x, pt1->y - pt->y ); \
\
/* check for out-of-bounds midpoints in two ways */ \
/* just one method is probably enough, but I want to make sure */ \
\
/* if this edge and the next edge don't form an acute angle, */ \
/* then we're at a graph boundary, and there's no midpoint */ \
if((long)v1.x*(long)v0.y > (long)v0.x*(long)v1.y) \
continue; \
\
/* if this and the next edge don't form a triangle, */ \
/* then we're at a graph boundary, and there's no midpoint */ \
if(__e->twin()->prev()->twin()->cell() != __e->next()->twin()->cell() ) \
continue; \
\
/* get the midpoint vertex. It must lie angularly between its two neighbors */ \
c_adjacent = __e->twin()->prev()->prev()->twin()->cell(); \
const PointInt* ptmid = &points[c_adjacent->source_index()]; \
PointInt vmid( ptmid->x - pt->x, ptmid->y - pt->y ); \
if((long)v1 .x*(long)vmid.y > (long)vmid.x*(long)v1 .y) \
continue; \
if((long)vmid.x*(long)v0 .y > (long)v0 .x*(long)vmid.y) \
continue; \
pt_adjacent = &points[c_adjacent->source_index()]; \
} \
\
PointInt delta( pt_adjacent->x - pt->x, \
pt_adjacent->y - pt->y );
#define FOR_ALL_ADJACENT_CELLS_END() }}} while(0)
// for integers
#define ABS(x) ((x) > 0 ? (x) : -(x))
struct CandidateSequence
{
// First two cells and the last cell. The rest of the sequence is
// constructed by following the best path from these two cells. The rules
// that define this "best" path are consistent, so we don't store the path
// itself, but recompute it each time it is needed
const VORONOI::cell_type* c0;
const VORONOI::cell_type* c1;
const VORONOI::cell_type* clast;
PointDouble delta_mean;
double spacing_angle;
double spacing_length;
};
struct HypothesisStatistics
{
PointInt delta_last;
double length_ratio_sum;
int length_ratio_N;
};
static void fill_initial_hypothesis_statistics(// out
HypothesisStatistics* stats,
// in
const PointInt* delta0)
{
stats->delta_last = *delta0;
stats->length_ratio_sum = 0.0;
stats->length_ratio_N = 0;
}
// need delta, N_remaining, c, points
#define FOR_MATCHING_ADJACENT_CELLS(debug_sequence_pointscale) do { \
HypothesisStatistics stats; \
fill_initial_hypothesis_statistics(&stats, delta); \
for(int i=0; i<N_remaining; i++) \
{ \
const VORONOI::cell_type* c_adjacent = get_adjacent_cell_along_sequence(&stats, c, points, debug_sequence_pointscale);
#define FOR_MATCHING_ADJACENT_CELLS_END() \
c = c_adjacent; }} while(0)
// tight bound on angle error, loose bound on length error. This is because
// perspective distortion can vary the lengths, but NOT the orientations
#define THRESHOLD_SPACING_COS 0.984 /* 10 degrees */
#define THRESHOLD_SPACING_LENGTH_RATIO_MIN 0.7
#define THRESHOLD_SPACING_LENGTH_RATIO_MAX 1.4
#define THRESHOLD_SPACING_LENGTH_RATIO_DEVIATION 0.35
static const VORONOI::cell_type*
get_adjacent_cell_along_sequence( // out,in.
HypothesisStatistics* stats,
// in
const VORONOI::cell_type* c,
const std::vector<PointInt>& points,
int debug_sequence_pointscale /* <=0 means "no debugging" */ )
{
// We're given a voronoi cell, and some properties that a potential next
// cell in the sequence should match. I look through all the voronoi
// neighbors of THIS cell, and return the first one that matches all my
// requirements. Multiple neighboring cells COULD match, but I'm assuming
// clean data, so this possibility is ignored.
//
// A matching next cell should have the following properties, all
// established by the previous cells in the sequence. The next cell should
// match all of these:
//
// - located along an expected direction (tight bound on angle)
//
// - should be an expected distance away (loose bound on absolute distance)
//
// - the ratio of successive distances in a sequence should be constant-ish
//
// The points in a sequence come from projections of equidistantly-spaced
// points in a straight line, so the projected DIRECTIONS should match very
// well. The distances may not, if the observed object is tilted and is
// relatively close to the camera, but each successive distance will vary ~
// geometrically due to perspective effects, or it the distances will all be
// roughly constant, which is still geometric, technically
const PointInt& delta_last = stats->delta_last;
double delta_last_length = hypot((double)delta_last.x, (double)delta_last.y);
FOR_ALL_ADJACENT_CELLS(c)
{
if(debug_sequence_pointscale > 0)
fprintf(stderr, "Considering connection in sequence from (%d,%d) -> (%d,%d); delta (%d,%d) ..... \n",
pt->x / debug_sequence_pointscale,
pt->y / debug_sequence_pointscale,
pt_adjacent->x / debug_sequence_pointscale,
pt_adjacent->y / debug_sequence_pointscale,
delta.x / debug_sequence_pointscale,
delta.y / debug_sequence_pointscale);
double delta_length = hypot( (double)delta.x, (double)delta.y );
double cos_err =
((double)delta_last.x * (double)delta.x +
(double)delta_last.y * (double)delta.y) /
(delta_last_length * delta_length);
if( cos_err < THRESHOLD_SPACING_COS )
{
if(debug_sequence_pointscale > 0)
fprintf(stderr, "..... rejecting. Angle is wrong. I wanted cos_err>=threshold, but saw %f<%f\n",
cos_err, THRESHOLD_SPACING_COS);
continue;
}
double length_ratio = delta_length / delta_last_length;
if( length_ratio < THRESHOLD_SPACING_LENGTH_RATIO_MIN ||
length_ratio > THRESHOLD_SPACING_LENGTH_RATIO_MAX )
{
if(debug_sequence_pointscale > 0)
fprintf(stderr, "..... rejecting. Lengths are wrong. I wanted abs(length_ratio)<=threshold, but saw %f<%f or %f>%f\n",
length_ratio, THRESHOLD_SPACING_LENGTH_RATIO_MIN,
length_ratio, THRESHOLD_SPACING_LENGTH_RATIO_MAX);
continue;
}
// I compute the mean and look at the deviation from the CURRENT mean. I
// ignore the first few points, since the mean is unstable then. This is
// OK, however, since I'm going to find and analyze the same sequence in
// the reverse order, and this will cover the other end
if( stats->length_ratio_N > 2 )
{
double length_ratio_mean = stats->length_ratio_sum / (double)stats->length_ratio_N;
double length_ratio_deviation = length_ratio - length_ratio_mean;
if( length_ratio_deviation < -THRESHOLD_SPACING_LENGTH_RATIO_DEVIATION ||
length_ratio_deviation > THRESHOLD_SPACING_LENGTH_RATIO_DEVIATION )
{
if(debug_sequence_pointscale > 0)
fprintf(stderr, "..... rejecting. Lengths are wrong. I wanted abs(length_ratio_deviation)<=threshold, but saw %f>%f\n",
fabs(length_ratio_deviation), THRESHOLD_SPACING_LENGTH_RATIO_DEVIATION);
continue;
}
}
stats->length_ratio_sum += length_ratio;
stats->length_ratio_N++;
stats->delta_last = delta;
if(debug_sequence_pointscale > 0)
fprintf(stderr, "..... accepting!\n\n");
return c_adjacent;
} FOR_ALL_ADJACENT_CELLS_END();
return NULL;
}
static
const VORONOI::cell_type* search_along_sequence( // out
PointDouble* delta_mean,
// in
const PointInt* delta,
const VORONOI::cell_type* c,
int N_remaining,
const std::vector<PointInt>& points,
int debug_sequence_pointscale )
{
delta_mean->x = (double)delta->x;
delta_mean->y = (double)delta->y;
const VORONOI::cell_type* clast = NULL;
FOR_MATCHING_ADJACENT_CELLS(debug_sequence_pointscale)
{
if( c_adjacent == NULL )
return NULL;
delta_mean->x += (double)stats.delta_last.x;
delta_mean->y += (double)stats.delta_last.y;
if(i == N_remaining-1)
clast = c_adjacent;
}
FOR_MATCHING_ADJACENT_CELLS_END();
delta_mean->x /= (double)(N_remaining+1);
delta_mean->y /= (double)(N_remaining+1);
return clast;
}
static void output_point( std::vector<PointDouble>& points_out,
const VORONOI::cell_type* c,
const std::vector<PointInt>& points )
{
const PointInt* pt = &points[c->source_index()];
points_out.push_back( PointDouble( (double)pt->x / (double)FIND_GRID_SCALE,
(double)pt->y / (double)FIND_GRID_SCALE) );
}
static void output_points_along_sequence( std::vector<PointDouble>& points_out,
const PointInt* delta,
const VORONOI::cell_type* c,
int N_remaining,
const std::vector<PointInt>& points)
{
FOR_MATCHING_ADJACENT_CELLS(-1)
{
output_point(points_out, c_adjacent, points);
} FOR_MATCHING_ADJACENT_CELLS_END();
}
static void output_row( std::vector<PointDouble>& points_out,
const CandidateSequence& row,
const std::vector<PointInt>& points,
const int gridn)
{
output_point(points_out, row.c0, points);
output_point(points_out, row.c1, points);
const PointInt* pt0 = &points[row.c0->source_index()];
const PointInt* pt1 = &points[row.c1->source_index()];
PointInt delta({ pt1->x - pt0->x,
pt1->y - pt0->y});
output_points_along_sequence( points_out, &delta, row.c1, gridn-2, points);
}
// dumps the voronoi diagram to a self-plotting vnlog
#define DUMP_FILENAME_VORONOI "/tmp/mrgingham-2-voronoi.vnl"
static void dump_voronoi( const VORONOI* voronoi,
const std::vector<PointInt>& points )
{
FILE* fp = fopen(DUMP_FILENAME_VORONOI, "w");
assert(fp);
// the kernel limits the #! line to 127 characters, so I abbreviate
fprintf(fp, "#!/usr/bin/feedgnuplot --domain --dataid --with 'lines linecolor 0' --square --maxcurves 100000 --set 'yrange [:] rev'\n");
fprintf(fp, "# x id_edge y\n");
int i_edge = 0;
for (auto it = voronoi->cells().begin(); it != voronoi->cells().end(); it++ )
{
const VORONOI::cell_type* c = &(*it);
const PointInt* pt0 = &points[c->source_index()];
const VORONOI::edge_type* const e0 = c->incident_edge();
bool first = true;
for(const VORONOI::edge_type* e = e0;
e0 != NULL && (e != e0 || first);
e = e->next(), first=false)
{
const VORONOI::cell_type* c_adjacent = e->twin()->cell();
const PointInt* pt1 = &points[c_adjacent->source_index()];
fprintf(fp, "%f %d %f\n", pt0->x/(double)FIND_GRID_SCALE, i_edge, pt0->y/(double)FIND_GRID_SCALE);
fprintf(fp, "%f %d %f\n", pt1->x/(double)FIND_GRID_SCALE, i_edge, pt1->y/(double)FIND_GRID_SCALE);
i_edge++;
}
}
fclose(fp);
chmod(DUMP_FILENAME_VORONOI,
S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP |
S_IXUSR | S_IXGRP | S_IXOTH);
fprintf(stderr, "Wrote self-plotting voronoi diagram to " DUMP_FILENAME_VORONOI "\n");
}
static void dump_interval( FILE* fp,
const int i_candidate,
const int i_pt,
const VORONOI::cell_type* c0,
const VORONOI::cell_type* c1,
const std::vector<PointInt>& points )
{
const PointInt* pt0 = &points[c0->source_index()];
if( c1 == NULL )
{
fprintf(fp,
"%d %d %f %f - - - - - -\n",
i_candidate, i_pt,
(double)pt0->x / (double)FIND_GRID_SCALE, (double)pt0->y / (double)FIND_GRID_SCALE);
return;
}
const PointInt* pt1 = &points[c1->source_index()];
double dx = (double)(pt1->x - pt0->x) / (double)FIND_GRID_SCALE;
double dy = (double)(pt1->y - pt0->y) / (double)FIND_GRID_SCALE;
double length = hypot(dx,dy);
double angle = atan2(dy,dx) * 180.0 / M_PI;
fprintf(fp,
"%d %d %f %f %f %f %f %f %f %f\n",
i_candidate, i_pt,
(double)pt0->x / (double)FIND_GRID_SCALE, (double)pt0->y / (double)FIND_GRID_SCALE,
(double)pt1->x / (double)FIND_GRID_SCALE, (double)pt1->y / (double)FIND_GRID_SCALE,
dx, dy, length, angle);
}
static void dump_intervals_along_sequence( FILE* fp,
const CandidateSequence* cs,
int i_candidate,
const std::vector<PointInt>& points,
const int gridn)
{
int N_remaining = gridn-1;
dump_interval(fp, i_candidate, 0, cs->c0, cs->c1, points);
const PointInt* pt0 = &points[cs->c0->source_index()];
const PointInt* pt1 = &points[cs->c1->source_index()];
PointInt _delta({ pt1->x - pt0->x,
pt1->y - pt0->y});
const PointInt* delta = &_delta;
const VORONOI::cell_type* c = cs->c1;
FOR_MATCHING_ADJACENT_CELLS(-1)
{
dump_interval(fp, i_candidate, i+1, c,
i+1 == gridn-1 ? NULL : c_adjacent,
points);
} FOR_MATCHING_ADJACENT_CELLS_END();
}
static
double get_spacing_angle( double y, double x )
{
double angle = 180.0/M_PI * atan2(y,x);
if( angle < 0 ) angle += 180.0;
return angle;
}
typedef std::vector<CandidateSequence> v_CS;
typedef std::map<unsigned int, std::vector<int> > SequenceIndicesFromPoint;
struct outer_cycle
{
short e[4];
};
static void get_sequence_candidates( // out
v_CS* sequence_candidates,
// in
const VORONOI* voronoi,
const std::vector<PointInt>& points,
// for debugging
const debug_sequence_t& debug_sequence,
const int gridn)
{
const VORONOI::cell_type* tracing_c = NULL;
int debug_sequence_pointscale = -1;
if(debug_sequence.dodebug)
{
// we're tracing some point. I find the nearest voronoi vertex, and
// debug_sequence that
unsigned long d2 = (unsigned long)(-1L); // max at first
debug_sequence_pointscale = FIND_GRID_SCALE;
for (auto it = voronoi->cells().begin(); it != voronoi->cells().end(); it++ )
{
const VORONOI::cell_type* c = &(*it);
const PointInt* pt = &points[c->source_index()];
long dx = (long)(pt->x - debug_sequence_pointscale*debug_sequence.pt.x);
long dy = (long)(pt->y - debug_sequence_pointscale*debug_sequence.pt.y);
unsigned long d2_here = (unsigned long)(dx*dx + dy*dy);
if(d2_here < d2)
{
d2 = d2_here;
tracing_c = c;
}
}
const PointInt* pt = &points[tracing_c->source_index()];
fprintf(stderr, "============== Looking at sequences from (%d,%d)\n",
pt->x / debug_sequence_pointscale,
pt->y / debug_sequence_pointscale);
}
for (auto it = voronoi->cells().begin(); it != voronoi->cells().end(); it++ )
{
const VORONOI::cell_type* c = &(*it);
bool debug_sequence = ( c == tracing_c );
FOR_ALL_ADJACENT_CELLS(c)
{
if(c == tracing_c)
fprintf(stderr, "\n\n====== Looking at adjacent point (%d,%d)\n",
pt_adjacent->x / debug_sequence_pointscale,
pt_adjacent->y / debug_sequence_pointscale);
PointDouble delta_mean;
const VORONOI::cell_type* clast =
search_along_sequence( &delta_mean,
&delta, c_adjacent, gridn-2, points,
(c == tracing_c) ? debug_sequence_pointscale : -1 );
if( clast )
{
double spacing_angle = get_spacing_angle(delta_mean.y, delta_mean.x);
double spacing_length = hypot(delta_mean.x, delta_mean.y);
sequence_candidates->push_back( CandidateSequence({c, c_adjacent, clast, delta_mean,
spacing_angle, spacing_length}) );
}
} FOR_ALL_ADJACENT_CELLS_END();
}
}
static void get_candidate_point( unsigned int* cs_point,
const VORONOI::cell_type* c )
{
*cs_point = c->source_index();
}
static void get_candidate_points_along_sequence( unsigned int* cs_points,
const PointInt* delta,
const VORONOI::cell_type* c,
int N_remaining,
const std::vector<PointInt>& points)
{
FOR_MATCHING_ADJACENT_CELLS(-1)
{
get_candidate_point(cs_points, c_adjacent);
cs_points++;
} FOR_MATCHING_ADJACENT_CELLS_END();
}
static void get_candidate_points( unsigned int* cs_points,
const CandidateSequence* cs,
const std::vector<PointInt>& points,
const int gridn)
{
get_candidate_point( &cs_points[0], cs->c0 );
get_candidate_point( &cs_points[1], cs->c1 );
const PointInt* pt0 = &points[cs->c0->source_index()];
const PointInt* pt1 = &points[cs->c1->source_index()];
PointInt delta({ pt1->x - pt0->x,
pt1->y - pt0->y});
get_candidate_points_along_sequence(&cs_points[2], &delta, cs->c1, gridn-2, points);
}
// dumps a terse self-plotting vnlog visualization of sequence candidates, and a
// more detailed vnlog containing more data
#define DUMP_BASENAME_ALL_SEQUENCE_CANDIDATES "/tmp/mrgingham-3-candidates"
#define DUMP_BASENAME_OUTER_EDGES "/tmp/mrgingham-4-outer-edges"
#define DUMP_BASENAME_OUTER_EDGE_CYCLES "/tmp/mrgingham-5-outer-edge-cycles"
#define DUMP_BASENAME_IDENTIFIED_OUTER_EDGE_CYCLE "/tmp/mrgingham-6-identified-outer-edge-cycle"
#define dump_candidates(basename, sequence_candidates, outer_edges, points, gridn) \
_dump_candidates( basename".vnl", basename"-detailed.vnl", \
sequence_candidates, outer_edges, points, gridn )
static void _dump_candidates(const char* filename_sparse,
const char* filename_dense,
const v_CS* sequence_candidates,
const std::vector<int>* outer_edges,
const std::vector<PointInt>& points,
const int gridn)
{
FILE* fp = fopen(filename_sparse, "w");
assert(fp);
// the kernel limits the #! line to 127 characters, so I abbreviate
fprintf(fp, "#!/usr/bin/feedgnuplot --dom --aut --square --rangesizea 3 --w 'vec size screen 0.01,20 fixed fill' --set 'yr [:] rev'\n");
fprintf(fp, "# fromx fromy deltax deltay\n");
if(outer_edges == NULL)
for( auto it = sequence_candidates->begin(); it != sequence_candidates->end(); it++ )
{
const CandidateSequence* cs = &(*it);
const PointInt* pt = &points[cs->c0->source_index()];
fprintf(fp,
"%f %f %f %f\n",
(double)(pt->x) / (double)FIND_GRID_SCALE,
(double)(pt->y) / (double)FIND_GRID_SCALE,
cs->delta_mean.x / (double)FIND_GRID_SCALE,
cs->delta_mean.y / (double)FIND_GRID_SCALE);
}
else
for( auto it = outer_edges->begin(); it != outer_edges->end(); it++ )
{
const CandidateSequence* cs = &((*sequence_candidates)[*it]);
const PointInt* pt = &points[cs->c0->source_index()];
fprintf(fp,
"%f %f %f %f\n",
(double)(pt->x) / (double)FIND_GRID_SCALE,
(double)(pt->y) / (double)FIND_GRID_SCALE,
cs->delta_mean.x / (double)FIND_GRID_SCALE,
cs->delta_mean.y / (double)FIND_GRID_SCALE);
}
fclose(fp);
chmod(filename_sparse,
S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP |
S_IXUSR | S_IXGRP | S_IXOTH);
fprintf(stderr, "Wrote self-plotting sequence-candidate dump to %s\n",
filename_sparse);
// detailed
fp = fopen(filename_dense, "w");
assert(fp);
fprintf(fp, "# candidateid pointid fromx fromy tox toy deltax deltay len angle\n");
if(outer_edges == NULL)
{
int N = sequence_candidates->size();
for( int i=0; i<N; i++ )
dump_intervals_along_sequence( fp,
&(*sequence_candidates)[i],
i, points,
gridn);
}
else
{
int N = outer_edges->size();
for( int i=0; i<N; i++ )
dump_intervals_along_sequence( fp,
&(*sequence_candidates)[(*outer_edges)[i]],
i, points,
gridn);
}
fclose(fp);
fprintf(stderr, "Wrote detailed sequence-candidate dump to %s\n",
filename_dense);
}
static void dump_outer_edge_cycles(const std::vector<outer_cycle>& outer_cycles,
const std::vector<int>& outer_edges,
const v_CS& sequence_candidates,
const std::vector<PointInt>& points)
{
FILE* fp = fopen(DUMP_BASENAME_OUTER_EDGE_CYCLES, "w");
assert(fp);
// the kernel limits the #! line to 127 characters, so I abbreviate
fprintf(fp, "#!/usr/bin/feedgnuplot --datai --dom --aut --square --rangesizea 3 --w 'vec size screen 0.01,20 fixed fill' --set 'yr [:] rev'\n");
fprintf(fp, "# fromx type fromy deltax deltay\n");
for( int i_cycle=0; i_cycle<(int)outer_cycles.size(); i_cycle++ )
{
for(int i_edge = 0; i_edge<4; i_edge++ )
{
const CandidateSequence* cs = &sequence_candidates[outer_edges[ outer_cycles[i_cycle].e[i_edge] ]];
const PointInt* pt = &points[cs->c0->source_index()];
fprintf(fp,
"%f %d %f %f %f\n",
(double)(pt->x) / (double)FIND_GRID_SCALE,
i_cycle,
(double)(pt->y) / (double)FIND_GRID_SCALE,
cs->delta_mean.x / (double)FIND_GRID_SCALE,
cs->delta_mean.y / (double)FIND_GRID_SCALE);
}
}
fclose(fp);
chmod(DUMP_BASENAME_OUTER_EDGE_CYCLES,
S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP |
S_IXUSR | S_IXGRP | S_IXOTH);
fprintf(stderr, "Wrote outer edge cycle dump to %s\n",
DUMP_BASENAME_OUTER_EDGE_CYCLES);
}
static void dump_outer_edge_cycles_identified(const std::vector<outer_cycle>& outer_cycles,
const std::vector<int>& outer_edges,
const v_CS& sequence_candidates,
const std::vector<PointInt>& points,
const int* outer_cycle_pair, int iclockwise,
const int* iedge_top)
{
FILE* fp = fopen(DUMP_BASENAME_IDENTIFIED_OUTER_EDGE_CYCLE, "w");
assert(fp);
// the kernel limits the #! line to 127 characters, so I abbreviate
fprintf(fp, "#!/usr/bin/feedgnuplot --datai --dom --aut --square --rangesizea 3 --w 'vec size screen 0.01,20 fixed fill' --set 'yr [:] rev'\n");
fprintf(fp, "# fromx type fromy deltax deltay\n");
for( int i_cycle=0; i_cycle<2; i_cycle++ )
{
for(int i_edge = 0; i_edge<4; i_edge++ )
{
const CandidateSequence* cs = &sequence_candidates[outer_edges[ outer_cycles[outer_cycle_pair[i_cycle]].e[i_edge] ]];
const PointInt* pt = &points[cs->c0->source_index()];
char what[128];
sprintf(what, "%s%s",
(i_cycle == iclockwise) ? "clockwise" : "counterclockwise",
iedge_top[i_cycle] == i_edge ? "-top" : "");
fprintf(fp,
"%f %s %f %f %f\n",
(double)(pt->x) / (double)FIND_GRID_SCALE,
what,
(double)(pt->y) / (double)FIND_GRID_SCALE,
cs->delta_mean.x / (double)FIND_GRID_SCALE,
cs->delta_mean.y / (double)FIND_GRID_SCALE);
}
}
fclose(fp);
chmod(DUMP_BASENAME_IDENTIFIED_OUTER_EDGE_CYCLE,
S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP |
S_IXUSR | S_IXGRP | S_IXOTH);
fprintf(stderr, "Wrote outer edge cycle dump to %s\n",
DUMP_BASENAME_IDENTIFIED_OUTER_EDGE_CYCLE);
}
static bool is_crossing( unsigned int line0_pt0, unsigned int line0_pt1,
unsigned int line1_pt0, unsigned int line1_pt1,
const std::vector<PointInt>& points)
{
// First I translate everything so that line0_pt0 is at the origin
float l0pt1[2] =
{ (float)(points[line0_pt1].x - points[line0_pt0].x),
(float)(points[line0_pt1].y - points[line0_pt0].y) };
float l1pt0[2] =
{ (float)(points[line1_pt0].x - points[line0_pt0].x),
(float)(points[line1_pt0].y - points[line0_pt0].y) };
float l1pt1[2] =
{ (float)(points[line1_pt1].x - points[line0_pt0].x),
(float)(points[line1_pt1].y - points[line0_pt0].y) };
// Now I rotate the 3 points such that l0pt1 ends up aligned on the x axis.
// I don't bother to divide by the hypotenuse, so l0pt1 is at (d^2, 0)
float d2 = l0pt1[0]*l0pt1[0] + l0pt1[1]*l0pt1[1];
float l1pt0_rotated[2] =
{ l1pt0[0]*l0pt1[0] + l1pt0[1]*l0pt1[1],
-l1pt0[0]*l0pt1[1] + l1pt0[1]*l0pt1[0] };
float l1pt1_rotated[2] =
{ l1pt1[0]*l0pt1[0] + l1pt1[1]*l0pt1[1],
-l1pt1[0]*l0pt1[1] + l1pt1[1]*l0pt1[0] };
// In this rotated space, the two points must have opposite-sign y (lie on
// both sides of the line).
if( l1pt0_rotated[1]*l1pt1_rotated[1] > 0 )
return false;
// To cross the line, both x coords can't be off the edge
if( (l1pt0_rotated[0] < 0 && l1pt1_rotated[0] < 0) ||
(l1pt0_rotated[0] > d2 && l1pt1_rotated[0] > d2) )
return false;
// OK, maybe they do cross. I actually compute the intersection
// a + k(b-a) = [..., 0] -> k = a1/(a1-b1)
float k = l1pt0_rotated[1] / (l1pt0_rotated[1] - l1pt1_rotated[1]);
float x = l1pt0_rotated[0] + k * (l1pt1_rotated[0] - l1pt0_rotated[0]);
return x >= 0.0f && x <= d2;
}
// recursively search for 4-cycle sequences of outer edges. On success, returns
// true, with the cycle reported in edges
static bool next_outer_edge(// this iteration
outer_cycle* edges, // edge sequence being
// evaluated. Output
// returned here
int edge_count, // how many edges we've got,
// including this one
// context
unsigned int point_initial,
const std::vector<int>& outer_edges,
const v_CS& sequence_candidates,
const SequenceIndicesFromPoint& outer_edges_from_point,
const std::vector<PointInt>& points,
bool debug)
{
bool found_cycle = false;
outer_cycle outer_cycle_found = {};
int i_edge = (int)edges->e[edge_count-1];
unsigned int first_point_this_edge = sequence_candidates[outer_edges[i_edge]].c0 ->source_index();
unsigned int last_point_this_edge = sequence_candidates[outer_edges[i_edge]].clast->source_index();
const std::vector<int>* next_edges;
try
{
next_edges = &outer_edges_from_point.at(last_point_this_edge);
}
catch(...)
{
if(debug)
fprintf(stderr, "No opposing outer edge\n");
return false;
}
int Nedges_from_here = next_edges->size();
for(int i=0; i<Nedges_from_here; i++)
{
// I make sure to not follow edges that are inverses of the immediately
// previous edge, and I make sure that the 4th edge forms a loop AND
// that a non-4th edge does NOT go back to the start. This is enough,
// and I don't need any more already-visited logic:
//
// - 1st edge is given
// - 2nd edge can go anywhere except directly backwards
// - 3rd edge can go anywhere except the edges 1 (the start) and 2 (the
// previous point)
// - 4th edge can go only to the start point
unsigned int last_point_next_edge = sequence_candidates[outer_edges[ (*next_edges)[i] ]].clast->source_index();
if( last_point_next_edge == first_point_this_edge )
// This next edge is an inverse of this edge. It's not a part of my
// 4-cycle
continue;
// I need to ignore X structures. In the below, ACBDA is valid,
// but ABCDA is NOT valid.
//
// A C
// |\/|
// |/\|
// D B
//
// Thus I make sure that
// edge 3 does not cross edge 1 and that
// edge 4 does not cross edge 2
if(edge_count != 3)
{
// This is not the last edge. It may not go to the start
if( last_point_next_edge == point_initial )
continue;
if(edge_count == 2)
{
if( is_crossing(sequence_candidates[outer_edges[ edges->e[0] ]].c0 ->source_index(),
sequence_candidates[outer_edges[ edges->e[0] ]].clast->source_index(),
sequence_candidates[outer_edges[ (*next_edges)[i] ]].c0 ->source_index(),
sequence_candidates[outer_edges[ (*next_edges)[i] ]].clast->source_index(),
points ))
continue;
}
edges->e[edge_count] = (short)(*next_edges)[i];
if(!next_outer_edge( edges, edge_count+1,
point_initial,
outer_edges,
sequence_candidates,
outer_edges_from_point,
points,
debug))
continue;
// Got a successful result. It must be unique
if(found_cycle)
{
if(debug)
fprintf(stderr, "Found non-unique 4-cycle\n");
return false;
}
found_cycle = true;
outer_cycle_found = *edges;
}
else
{
// Last edge. May only go back to the start
if( last_point_next_edge != point_initial )
continue;
if( is_crossing(sequence_candidates[outer_edges[ edges->e[1] ]].c0 ->source_index(),
sequence_candidates[outer_edges[ edges->e[1] ]].clast->source_index(),
sequence_candidates[outer_edges[ (*next_edges)[i] ]].c0 ->source_index(),
sequence_candidates[outer_edges[ (*next_edges)[i] ]].clast->source_index(),
points ))
{
// I already found the last edge, but it's crossing itself. I
// know there aren't any more solutions here, so I exit
return false;
}
edges->e[3] = (short)(*next_edges)[i];
return true;
}
}
if(!found_cycle) return false;
*edges = outer_cycle_found;
return true;
}
static bool is_equalAndOpposite_cycle(const outer_cycle& cycle0,
const outer_cycle& cycle1,
// context
const std::vector<int>& outer_edges,
const v_CS& sequence_candidates,
bool debug)
{
// Pick an arbitrary starting point: initial point of the first edge of the
// first cycle
int iedge0 = 0;
unsigned int ipt0 = sequence_candidates[outer_edges[cycle0.e[iedge0]]].c0->source_index();
// find the edge in the potentially-opposite cycle that ends at this point
int iedge1 = -1;
for(int _iedge1 = 0; _iedge1 < 4; _iedge1++)
if(ipt0 == sequence_candidates[outer_edges[ cycle1.e[_iedge1] ]].clast->source_index())
{
iedge1 = _iedge1;
break;
}
if( iedge1 < 0)
{
if(debug)
fprintf(stderr, "Given outer cycles are NOT equal and opposite: couldn't find a corresponding point in the two cycles\n");
return false;
}
// Now I traverse the two cycles, and compare. I traverse the second cycle
// backwards, and compare the back/front and front/back
for(int i=0; i<4; i++)
{
unsigned int cycle0_points[2] =
{ (unsigned int)sequence_candidates[outer_edges[cycle0.e[iedge0]]].c0 ->source_index(),
(unsigned int)sequence_candidates[outer_edges[cycle0.e[iedge0]]].clast->source_index()};
unsigned int cycle1_points[2] =
{ (unsigned int)sequence_candidates[outer_edges[cycle1.e[iedge1]]].c0 ->source_index(),
(unsigned int)sequence_candidates[outer_edges[cycle1.e[iedge1]]].clast->source_index() };
if(cycle0_points[0] != cycle1_points[1] ||
cycle0_points[1] != cycle1_points[0] )
{
if(debug)
fprintf(stderr, "Given outer cycles are NOT equal and opposite\n");
return false;
}
iedge0 = (iedge0+1) % 4;
iedge1 = (iedge1+3) % 4;