| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <assert.h> |
| 22 |
|
| 23 |
#include "mallocvar.h" |
| 24 |
|
| 25 |
#include "logreport.h" |
| 26 |
#include "curve.h" |
| 27 |
|
| 28 |
|
| 29 |
static Point |
| 30 |
realCoordFromInt(pm_pixelcoord const int_coord) { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
Point real_coord; |
| 35 |
|
| 36 |
real_coord.x = int_coord.col; |
| 37 |
real_coord.y = int_coord.row; |
| 38 |
real_coord.z = 0.0; |
| 39 |
|
| 40 |
return real_coord; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
Curve * |
| 46 |
curve_new(void) { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
Curve * curveP; |
| 51 |
|
| 52 |
MALLOCVAR_NOFAIL(curveP); |
| 53 |
|
| 54 |
curveP->pointList = NULL; |
| 55 |
CURVE_LENGTH(curveP) = 0; |
| 56 |
CURVE_CYCLIC(curveP) = false; |
| 57 |
PREVIOUS_CURVE(curveP) = NULL; |
| 58 |
NEXT_CURVE(curveP) = NULL; |
| 59 |
|
| 60 |
return curveP; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
Curve * |
| 66 |
curve_copyMost(Curve * const oldCurveP) { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
Curve * curveP = curve_new(); |
| 73 |
|
| 74 |
CURVE_CYCLIC(curveP) = CURVE_CYCLIC(oldCurveP); |
| 75 |
PREVIOUS_CURVE(curveP) = PREVIOUS_CURVE(oldCurveP); |
| 76 |
NEXT_CURVE(curveP) = NEXT_CURVE(oldCurveP); |
| 77 |
|
| 78 |
return curveP; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
void |
| 84 |
curve_move(Curve * const dstP, |
| 85 |
Curve * const srcP) { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if (CURVE_LENGTH(dstP) > 0) |
| 91 |
free(dstP->pointList); |
| 92 |
|
| 93 |
*dstP = *srcP; |
| 94 |
|
| 95 |
free(srcP); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void |
| 101 |
curve_free(Curve * const curveP) { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if (CURVE_LENGTH(curveP) > 0) |
| 109 |
free(curveP->pointList); |
| 110 |
|
| 111 |
free(curveP); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
void |
| 117 |
curve_appendPoint(Curve * const curveP, |
| 118 |
Point const coord) { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
CURVE_LENGTH(curveP)++; |
| 123 |
REALLOCARRAY_NOFAIL(curveP->pointList, CURVE_LENGTH(curveP)); |
| 124 |
LAST_CURVE_POINT(curveP) = coord; |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
void |
| 131 |
curve_appendPixel(Curve * const curveP, |
| 132 |
pm_pixelcoord const coord) { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
curve_appendPoint(curveP, realCoordFromInt(coord)); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
void |
| 142 |
curve_setDistance(Curve * const curveP) { |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
unsigned int p; |
| 160 |
|
| 161 |
LOG("\nAssigning initial t values:\n "); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
CURVE_DIST(curveP, 0) = 0.0; |
| 175 |
|
| 176 |
for (p = 1; p < CURVE_LENGTH(curveP); ++p) { |
| 177 |
Point const point = CURVE_POINT(curveP, p); |
| 178 |
Point const previous_p = CURVE_POINT(curveP, p - 1); |
| 179 |
float const d = point_distance(point, previous_p); |
| 180 |
CURVE_DIST(curveP, p) = CURVE_DIST(curveP, p - 1) + d; |
| 181 |
} |
| 182 |
|
| 183 |
assert(LAST_CURVE_DIST(curveP) != 0.0); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
for (p = 1; p < CURVE_LENGTH(curveP); ++p) |
| 188 |
CURVE_DIST(curveP, p) = |
| 189 |
CURVE_DIST(curveP, p) / LAST_CURVE_DIST(curveP); |
| 190 |
|
| 191 |
curve_logEntire(curveP); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
#define NUM_TO_PRINT 3 |
| 197 |
|
| 198 |
#define LOG_CURVE_POINT(c, p, printDistance) \ |
| 199 |
do \ |
| 200 |
{ \ |
| 201 |
LOG2 ("(%.3f,%.3f)", CURVE_POINT (c, p).x, CURVE_POINT (c, p).y); \ |
| 202 |
if (printDistance) \ |
| 203 |
LOG1 ("/%.2f", CURVE_DIST(c, p)); \ |
| 204 |
} \ |
| 205 |
while (0) |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
void |
| 210 |
curve_log(Curve * const curveP, |
| 211 |
bool const printDistance) { |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if (!log_file) |
| 219 |
return; |
| 220 |
|
| 221 |
LOG1("curve id = %lx:\n", (unsigned long) curveP); |
| 222 |
LOG1(" length = %u.\n", CURVE_LENGTH(curveP)); |
| 223 |
if (CURVE_CYCLIC(curveP)) |
| 224 |
LOG(" cyclic.\n"); |
| 225 |
|
| 226 |
LOG(" "); |
| 227 |
|
| 228 |
|
| 229 |
if (CURVE_LENGTH(curveP) <= NUM_TO_PRINT * 2) { |
| 230 |
unsigned int thisPoint; |
| 231 |
|
| 232 |
for (thisPoint = 0; thisPoint < CURVE_LENGTH(curveP); ++thisPoint) { |
| 233 |
LOG_CURVE_POINT(curveP, thisPoint, printDistance); |
| 234 |
LOG(" "); |
| 235 |
|
| 236 |
if (thisPoint != CURVE_LENGTH(curveP) - 1 |
| 237 |
&& (thisPoint + 1) % NUM_TO_PRINT == 0) |
| 238 |
LOG("\n "); |
| 239 |
} |
| 240 |
} else { |
| 241 |
unsigned int thisPoint; |
| 242 |
for (thisPoint = 0; |
| 243 |
thisPoint < NUM_TO_PRINT && thisPoint < CURVE_LENGTH(curveP); |
| 244 |
++thisPoint) { |
| 245 |
LOG_CURVE_POINT(curveP, thisPoint, printDistance); |
| 246 |
LOG(" "); |
| 247 |
} |
| 248 |
|
| 249 |
LOG("...\n ..."); |
| 250 |
|
| 251 |
for (thisPoint = CURVE_LENGTH(curveP) - NUM_TO_PRINT; |
| 252 |
thisPoint < CURVE_LENGTH(curveP); |
| 253 |
++thisPoint) { |
| 254 |
LOG(" "); |
| 255 |
LOG_CURVE_POINT(curveP, thisPoint, printDistance); |
| 256 |
} |
| 257 |
} |
| 258 |
LOG(".\n"); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
void |
| 264 |
curve_logEntire(Curve * const curveP) { |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
unsigned int thisPoint; |
| 269 |
|
| 270 |
if (!log_file) |
| 271 |
return; |
| 272 |
|
| 273 |
LOG1("curve id = %lx:\n", (unsigned long) curveP); |
| 274 |
LOG1(" length = %u.\n", CURVE_LENGTH(curveP)); |
| 275 |
if (CURVE_CYCLIC(curveP)) |
| 276 |
LOG(" cyclic.\n"); |
| 277 |
|
| 278 |
LOG(" "); |
| 279 |
|
| 280 |
for (thisPoint = 0; thisPoint < CURVE_LENGTH(curveP); ++thisPoint) { |
| 281 |
LOG(" "); |
| 282 |
LOG_CURVE_POINT(curveP, thisPoint, true); |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
LOG(".\n"); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
CurveList |
| 292 |
curve_newList(void) { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
CurveList curveList; |
| 297 |
|
| 298 |
curveList.length = 0; |
| 299 |
curveList.data = NULL; |
| 300 |
|
| 301 |
return curveList; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
void |
| 307 |
curve_freeList(CurveList * const curveListP) { |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
unsigned int thisCurve; |
| 312 |
|
| 313 |
for (thisCurve = 0; thisCurve < curveListP->length; ++thisCurve) |
| 314 |
curve_free(curveListP->data[thisCurve]); |
| 315 |
|
| 316 |
|
| 317 |
if (curveListP->data != NULL) |
| 318 |
free(curveListP->data); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
void |
| 324 |
curve_appendList(CurveList * const curveListP, |
| 325 |
Curve * const curveP) { |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
++curveListP->length; |
| 330 |
REALLOCARRAY_NOFAIL(curveListP->data, curveListP->length); |
| 331 |
curveListP->data[curveListP->length - 1] = curveP; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
CurveListArray |
| 337 |
curve_newListArray(void) { |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
CurveListArray curveListArray; |
| 342 |
|
| 343 |
CURVE_LIST_ARRAY_LENGTH(curveListArray) = 0; |
| 344 |
curveListArray.data = NULL; |
| 345 |
|
| 346 |
return curveListArray; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
void |
| 352 |
curve_freeListArray(const CurveListArray * const curveListArrayP, |
| 353 |
at_progress_func notify_progress, |
| 354 |
void * const clientData) { |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
unsigned int thisList; |
| 359 |
|
| 360 |
for (thisList = 0; |
| 361 |
thisList < CURVE_LIST_ARRAY_LENGTH(*curveListArrayP); |
| 362 |
++thisList) { |
| 363 |
|
| 364 |
if (notify_progress) |
| 365 |
notify_progress(((float)thisList)/ |
| 366 |
(CURVE_LIST_ARRAY_LENGTH(*curveListArrayP) * |
| 367 |
(float)3.0)+(float)0.666 , |
| 368 |
clientData); |
| 369 |
curve_freeList(&CURVE_LIST_ARRAY_ELT(*curveListArrayP, thisList)); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
if (curveListArrayP->data) |
| 374 |
free(curveListArrayP->data); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
void |
| 380 |
curve_appendArray(CurveListArray * const curveListArrayP, |
| 381 |
CurveList const curveList) { |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
CURVE_LIST_ARRAY_LENGTH(*curveListArrayP)++; |
| 386 |
REALLOCARRAY_NOFAIL(curveListArrayP->data, |
| 387 |
CURVE_LIST_ARRAY_LENGTH(*curveListArrayP)); |
| 388 |
LAST_CURVE_LIST_ARRAY_ELT(*curveListArrayP) = curveList; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|