00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "neato.h"
00018 #include <stdio.h>
00019 #include "mem.h"
00020 #include "info.h"
00021
00022
00023 Info_t *nodeInfo;
00024 static Freelist pfl;
00025
00026 void infoinit()
00027 {
00028 freeinit(&pfl, sizeof(PtItem));
00029 }
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 static int compare(Point * o, PtItem * p, PtItem * q)
00041 {
00042 double x0;
00043 double y0;
00044 double x1;
00045 double y1;
00046 double a, b;
00047
00048 if (q == NULL)
00049 return -1;
00050 if ((p->p.x == q->p.x) && (p->p.y == q->p.y))
00051 return 0;
00052
00053 x0 = ((double) (p->p.x)) - ((double) (o->x));
00054 y0 = ((double) (p->p.y)) - ((double) (o->y));
00055 x1 = ((double) (q->p.x)) - ((double) (o->x));
00056 y1 = ((double) (q->p.y)) - ((double) (o->y));
00057 if (x0 >= 0.0) {
00058 if (x1 < 0.0)
00059 return -1;
00060 else if (x0 > 0.0) {
00061 if (x1 > 0.0) {
00062 a = y1 / x1;
00063 b = y0 / x0;
00064 if (b < a)
00065 return -1;
00066 else if (b > a)
00067 return 1;
00068 else if (x0 < x1)
00069 return -1;
00070 else
00071 return 1;
00072 } else {
00073 if (y1 > 0.0)
00074 return -1;
00075 else
00076 return 1;
00077 }
00078 } else {
00079 if (x1 > 0.0) {
00080 if (y0 <= 0.0)
00081 return -1;
00082 else
00083 return 1;
00084 } else {
00085 if (y0 < y1) {
00086 if (y1 <= 0.0)
00087 return 1;
00088 else
00089 return -1;
00090 } else {
00091 if (y0 <= 0.0)
00092 return -1;
00093 else
00094 return 1;
00095 }
00096 }
00097 }
00098 } else {
00099 if (x1 >= 0.0)
00100 return 1;
00101 else {
00102 a = y1 / x1;
00103 b = y0 / x0;
00104 if (b < a)
00105 return -1;
00106 else if (b > a)
00107 return 1;
00108 else if (x0 > x1)
00109 return -1;
00110 else
00111 return 1;
00112 }
00113 }
00114 }
00115
00116 #if 0
00117 static void printV(PtItem * vp)
00118 {
00119 if (vp == NULL) {
00120 fprintf(stderr, "<empty>\n");
00121 return;
00122 }
00123
00124 while (vp != NULL) {
00125 fprintf(stderr, "(%.16f,%.16f)\n", vp->p.x, vp->p.y);
00126 vp = vp->next;
00127 }
00128 }
00129
00130 static void error(Info_t * ip, Site * s, double x, double y)
00131 {
00132 fprintf(stderr,
00133 "Unsorted vertex list for site %d (%.16f,%.16f), pt (%f,%f)\n",
00134 s->sitenbr, s->coord.x, s->coord.y, x, y);
00135 printV(ip->verts);
00136 }
00137 #endif
00138
00139 #if 0
00140 static int sorted(Point * origin, PtItem * vp)
00141 {
00142 PtItem *next;
00143
00144 if (vp == NULL)
00145 return 1;
00146 next = vp->next;
00147
00148 while (next != NULL) {
00149 if (compare(origin, vp, next) <= 0) {
00150 vp = next;
00151 next = next->next;
00152 } else {
00153 fprintf(stderr, "(%.16f,%.16f) > (%.16f,%.16f)\n",
00154 vp->p.x, vp->p.y, next->p.x, next->p.y);
00155 return 0;
00156 }
00157 }
00158
00159 return 1;
00160
00161 }
00162 #endif
00163
00164 void addVertex(Site * s, double x, double y)
00165 {
00166 Info_t *ip;
00167 PtItem *p;
00168 PtItem *curr;
00169 PtItem *prev;
00170 Point *origin = &(s->coord);
00171 PtItem tmp;
00172 int cmp;
00173
00174 ip = nodeInfo + (s->sitenbr);
00175 curr = ip->verts;
00176
00177 tmp.p.x = x;
00178 tmp.p.y = y;
00179
00180 cmp = compare(origin, &tmp, curr);
00181 if (cmp == 0)
00182 return;
00183 else if (cmp < 0) {
00184 p = (PtItem *) getfree(&pfl);
00185 p->p.x = x;
00186 p->p.y = y;
00187 p->next = curr;
00188 ip->verts = p;
00189 return;
00190 }
00191
00192 prev = curr;
00193 curr = curr->next;
00194 while ((cmp = compare(origin, &tmp, curr)) > 0) {
00195 prev = curr;
00196 curr = curr->next;
00197 }
00198 if (cmp == 0)
00199 return;
00200 p = (PtItem *) getfree(&pfl);
00201 p->p.x = x;
00202 p->p.y = y;
00203 prev->next = p;
00204 p->next = curr;
00205
00206
00207
00208
00209
00210 }