1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * sortmarks.c -- Functions which sort the marks into a "readable sequence"
|
---|
4 | * Copyright (C) 1994 Stuart Inglis
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: sortmarks.c 16583 2008-07-29 10:20:36Z davidb $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include "sysfuncs.h"
|
---|
25 |
|
---|
26 | #include "marklist.h"
|
---|
27 | #include "sortmarks.h"
|
---|
28 |
|
---|
29 | #define GAP 4
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | static int
|
---|
34 | CmpOnY (const void *e1, const void *e2)
|
---|
35 | {
|
---|
36 | return (((marktype *) e1)->ycen * 0 + ((marktype *) e1)->ypos) -
|
---|
37 | (((marktype *) e2)->ycen * 0 + ((marktype *) e2)->ypos);
|
---|
38 | }
|
---|
39 |
|
---|
40 | static int
|
---|
41 | CmpOnX (const void *e1, const void *e2)
|
---|
42 | {
|
---|
43 | return (((marktype *) e1)->xcen * 0 + ((marktype *) e1)->xpos) -
|
---|
44 | (((marktype *) e2)->xcen * 0 + ((marktype *) e2)->xpos);
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | marklistptr
|
---|
55 | sortmarks (marklistptr listofmarks)
|
---|
56 | {
|
---|
57 | marktype *table;
|
---|
58 | int count = 0;
|
---|
59 | int i, s, e;
|
---|
60 |
|
---|
61 |
|
---|
62 | table = calloc ((unsigned int) marklist_length (listofmarks), sizeof (marklisttype));
|
---|
63 |
|
---|
64 | while (listofmarks)
|
---|
65 | {
|
---|
66 | table[count] = listofmarks->data;
|
---|
67 | marklist_removeat (&listofmarks, 0);
|
---|
68 | count++;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /* sort first on the y position of their centroids */
|
---|
72 | qsort ((void *) table, (unsigned int) (count), sizeof (*table), CmpOnY);
|
---|
73 |
|
---|
74 | for (s = 0; s < count;)
|
---|
75 | {
|
---|
76 | int c;
|
---|
77 |
|
---|
78 | for (e = s + 1, c = 0; (e < count) &&
|
---|
79 | ((table[e].ypos + table[e].ycen * 0) - (table[e - 1].ypos + table[e - 1].ycen * 0) <= GAP); c++, e++);
|
---|
80 |
|
---|
81 | if (e < count && c >= 1)
|
---|
82 | qsort (&table[s], (unsigned int) (e - s), sizeof (*table), CmpOnX);
|
---|
83 | s = e;
|
---|
84 | }
|
---|
85 |
|
---|
86 | for (i = 0; i < count; i++)
|
---|
87 | {
|
---|
88 | table[i].symnum = i;
|
---|
89 | marklist_add (&listofmarks, table[i]);
|
---|
90 | }
|
---|
91 |
|
---|
92 | return listofmarks;
|
---|
93 | }
|
---|