00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 #include "gis.h"
00244
00245
00246
00247 #define MIN(a,b) (a < b ? a : b)
00248 #define MAX(a,b) (a > b ? a : b)
00249
00250 #define NO_DEFAULT_RULE (! r->defaultDRuleSet)
00251 #define NO_LEFT_INFINITE_RULE (! r->infiniteLeftSet)
00252 #define NO_RIGHT_INFINITE_RULE (! r->infiniteRightSet)
00253 #define NO_FINITE_RULE (r->nofRules <= 0)
00254 #define NO_EXPLICIT_RULE (NO_FINITE_RULE && \
00255 NO_LEFT_INFINITE_RULE && NO_RIGHT_INFINITE_RULE)
00256
00257 #define DEFAULT_MIN ((DCELL) 1)
00258 #define DEFAULT_MAX ((DCELL) 255)
00259
00260
00261
00262 void
00263 G_fpreclass_clear (r)
00264
00265 struct FPReclass *r;
00266
00267 {
00268 r->nofRules = 0;
00269 r->defaultDRuleSet = 0;
00270 r->defaultRRuleSet = 0;
00271 r->infiniteRightSet = r->infiniteLeftSet = 0;
00272 }
00273
00274
00275
00276 void
00277 G_fpreclass_reset (r)
00278
00279 struct FPReclass *r;
00280
00281 {
00282 G_fpreclass_clear (r);
00283
00284 if (r->maxNofRules > 0) G_free (r->table);
00285
00286 r->maxNofRules = 0;
00287 }
00288
00289
00290
00291 int G_fpreclass_init (r)
00292
00293 struct FPReclass *r;
00294
00295 {
00296 r->maxNofRules = 0;
00297 G_fpreclass_reset (r);
00298
00299 return 1;
00300 }
00301
00302
00303
00304 void
00305 G_fpreclass_set_domain (r, dLow, dHigh)
00306
00307 struct FPReclass *r;
00308 DCELL dLow, dHigh;
00309
00310 {
00311 r->defaultDMin = dLow;
00312 r->defaultDMax = dHigh;
00313 r->defaultDRuleSet = 1;
00314 }
00315
00316
00317
00318 void
00319 G_fpreclass_set_range (r, low, high)
00320
00321 struct FPReclass *r;
00322 DCELL low, high;
00323
00324 {
00325 r->defaultRMin = low;
00326 r->defaultRMax = high;
00327 r->defaultRRuleSet = 1;
00328 }
00329
00330
00331
00332 static void
00333 fpreclass_set_limits (r, dLow, dHigh, rLow, rHigh)
00334
00335 struct FPReclass *r;
00336 DCELL dLow, dHigh;
00337 DCELL rLow, rHigh;
00338
00339 {
00340 r->dMin = dLow;
00341 r->dMax = dHigh;
00342 r->rMin = rLow;
00343 r->rMax = rHigh;
00344 }
00345
00346
00347
00348 static void
00349 fpreclass_update_limits (r, dLow, dHigh, rLow, rHigh)
00350
00351 struct FPReclass *r;
00352 DCELL dLow, dHigh;
00353 DCELL rLow, rHigh;
00354
00355 {
00356 if (NO_EXPLICIT_RULE) {
00357 fpreclass_set_limits (r, dLow, dHigh, rLow, rHigh);
00358 return;
00359 }
00360
00361 r->dMin = MIN (r->dMin, MIN (dLow, dHigh));
00362 r->dMax = MAX (r->dMax, MAX (dLow, dHigh));
00363 r->rMin = MIN (r->rMin, MIN (rLow, rHigh));
00364 r->rMax = MAX (r->rMax, MAX (rLow, rHigh));
00365 }
00366
00367
00368
00369 int
00370 G_fpreclass_get_limits (r, dMin, dMax, rMin, rMax)
00371
00372 struct FPReclass *r;
00373 DCELL *dMin, *dMax;
00374 DCELL *rMin, *rMax;
00375
00376 {
00377 if (NO_EXPLICIT_RULE) {
00378 if (NO_DEFAULT_RULE) return -1;
00379
00380 *dMin = r->defaultDMin; *dMax = r->defaultDMax;
00381
00382 if (r->defaultRRuleSet) {
00383 *rMin = r->defaultRMin; *rMax = r->defaultRMax;
00384 } else {
00385 *rMin = DEFAULT_MIN; *rMax = DEFAULT_MAX;
00386 }
00387
00388 return 0;
00389 }
00390
00391 *dMin = r->dMin; *dMax = r->dMax;
00392 *rMin = r->rMin; *rMax = r->rMax;
00393
00394 return 1;
00395 }
00396
00397
00398
00399 int
00400 G_fpreclass_nof_rules (r)
00401
00402 struct FPReclass *r;
00403
00404 {
00405 return r->nofRules;
00406 }
00407
00408
00409
00410 void
00411 G_fpreclass_get_ith_rule (r, i, dLow, dHigh, rLow, rHigh)
00412
00413 struct FPReclass *r;
00414 int i;
00415 DCELL *dLow, *dHigh;
00416 DCELL *rLow, *rHigh;
00417
00418 {
00419 *dLow = r->table[i].dLow;
00420 *dHigh = r->table[i].dHigh;
00421 *rLow = r->table[i].rLow;
00422 *rHigh = r->table[i].rHigh;
00423 }
00424
00425
00426
00427 static void
00428 fpreclass_table_increase (r)
00429
00430 struct FPReclass *r;
00431
00432 {
00433 if (r->nofRules < r->maxNofRules) return;
00434
00435 if (r->maxNofRules == 0) {
00436 r->maxNofRules = 50;
00437 r->table = (struct FPReclass_table *)
00438 G_malloc (r->maxNofRules * sizeof (struct FPReclass_table));
00439 } else {
00440 r->maxNofRules += 50;
00441 r->table = (struct FPReclass_table *)
00442 G_realloc ((char *) r->table, r->maxNofRules * sizeof (struct FPReclass_table));
00443 }
00444 }
00445
00446
00447
00448 void
00449 G_fpreclass_set_neg_infinite_rule (r, dLeft, c)
00450
00451 struct FPReclass *r;
00452 DCELL dLeft;
00453 DCELL c;
00454
00455 {
00456 r->infiniteDLeft = dLeft;
00457 r->infiniteRLeft = c;
00458 fpreclass_update_limits (r, dLeft, dLeft, c, c);
00459 r->infiniteLeftSet = 1;
00460 }
00461
00462
00463
00464 int
00465 G_fpreclass_get_neg_infinite_rule (r, dLeft, c)
00466
00467 struct FPReclass *r;
00468 DCELL *dLeft;
00469 DCELL *c;
00470
00471 {
00472 if (r->infiniteLeftSet == 0) return 0;
00473
00474 *dLeft = r->infiniteDLeft;
00475 *c = r->infiniteRLeft;
00476
00477 return 1;
00478 }
00479
00480
00481
00482 void
00483 G_fpreclass_set_pos_infinite_rule (r, dRight, c)
00484
00485 struct FPReclass *r;
00486 DCELL dRight;
00487 DCELL c;
00488
00489 {
00490 r->infiniteDRight = dRight;
00491 r->infiniteRRight = c;
00492 fpreclass_update_limits (r, dRight, dRight, c, c);
00493 r->infiniteRightSet = 1;
00494 }
00495
00496
00497
00498 int
00499 G_fpreclass_get_pos_infinite_rule (r, dRight, c)
00500
00501 struct FPReclass *r;
00502 DCELL *dRight;
00503 DCELL *c;
00504
00505 {
00506 if (r->infiniteRightSet == 0) return 0;
00507
00508 *dRight = r->infiniteDRight;
00509 *c = r->infiniteRRight;
00510
00511 return 1;
00512 }
00513
00514
00515
00516 void
00517 G_fpreclass_add_rule (r, dLow, dHigh, rLow, rHigh)
00518
00519 struct FPReclass *r;
00520 DCELL dLow, dHigh;
00521 DCELL rLow, rHigh;
00522
00523 {
00524 int i;
00525 struct FPReclass_table *p;
00526
00527 fpreclass_table_increase (r);
00528
00529 i = r->nofRules;
00530
00531 p = &(r->table[i]);
00532 if (dHigh >= dLow) {
00533 p->dLow = dLow; p->dHigh = dHigh; p->rLow = rLow; p->rHigh = rHigh;
00534 } else {
00535 p->dLow = dHigh; p->dHigh = dLow; p->rLow = rHigh; p->rHigh = rLow;
00536 }
00537
00538 fpreclass_update_limits (r, dLow, dHigh, rLow, rHigh);
00539
00540 r->nofRules++;
00541 }
00542
00543
00544
00545 void
00546 G_fpreclass_reverse_rule_order (r)
00547
00548 struct FPReclass *r;
00549
00550 {
00551 struct FPReclass_table tmp;
00552 struct FPReclass_table *pLeft, *pRight;
00553
00554 pLeft = r->table;
00555 pRight = &(r->table [r->nofRules - 1]);
00556
00557 while (pLeft < pRight) {
00558 tmp.dLow = pLeft->dLow; tmp.dHigh = pLeft->dHigh;
00559 tmp.rLow = pLeft->rLow; tmp.rHigh = pLeft->rHigh;
00560
00561 pLeft->dLow = pRight->dLow; pLeft->dHigh = pRight->dHigh;
00562 pLeft->rLow = pRight->rLow; pLeft->rHigh = pRight->rHigh;
00563
00564 pRight->dLow = tmp.dLow; pRight->dHigh = tmp.dHigh;
00565 pRight->rLow = tmp.rLow; pRight->rHigh = tmp.rHigh;
00566
00567 pLeft++; pRight--;
00568 }
00569 }
00570
00571
00572
00573 static DCELL
00574 fpreclass_interpolate (dLow, dHigh, rLow, rHigh, dValue)
00575
00576 DCELL dLow, dHigh;
00577 DCELL rLow, rHigh;
00578 DCELL dValue;
00579
00580 {
00581 if (rLow == rHigh) return rLow;
00582 if (dLow == dHigh) return rLow;
00583
00584 return ((dValue - dLow) / (dHigh - dLow) * (rHigh - rLow) + rLow);
00585 }
00586
00587
00588
00589 static DCELL
00590 fpreclass_get_default_cell_value (r, cellVal)
00591
00592 struct FPReclass *r;
00593 DCELL cellVal;
00594
00595 {
00596 DCELL tmp;
00597 G_set_d_null_value(&tmp, 1);
00598
00599 if ((cellVal < MIN (r->defaultDMin, r->defaultDMax)) ||
00600 (cellVal > MAX (r->defaultDMin, r->defaultDMax))) return tmp;
00601
00602 if (r->defaultRRuleSet)
00603 return fpreclass_interpolate (r->defaultDMin, r->defaultDMax,
00604 r->defaultRMin, r->defaultRMax, cellVal);
00605 else
00606 return fpreclass_interpolate (r->defaultDMin, r->defaultDMax,
00607 DEFAULT_MIN, DEFAULT_MAX, cellVal);
00608 }
00609
00610
00611
00612 DCELL
00613 G_fpreclass_get_cell_value (r, cellVal)
00614
00615 struct FPReclass *r;
00616 DCELL cellVal;
00617
00618 {
00619 DCELL tmp;
00620 struct FPReclass_table *p;
00621
00622 G_set_d_null_value(&tmp, 1);
00623 if (NO_EXPLICIT_RULE) {
00624
00625 if (NO_DEFAULT_RULE) return tmp;
00626 return fpreclass_get_default_cell_value (r, cellVal);
00627 }
00628
00629 if (! NO_FINITE_RULE)
00630 for (p = &(r->table [r->nofRules - 1]); p >= r->table; p--)
00631 if ((cellVal >= p->dLow) && (cellVal <= p->dHigh))
00632 return fpreclass_interpolate (p->dLow, p->dHigh, p->rLow, p->rHigh,
00633 cellVal);
00634
00635 if ((! NO_LEFT_INFINITE_RULE) && (cellVal <= r->infiniteDLeft))
00636 return r->infiniteRLeft;
00637
00638 if ((NO_RIGHT_INFINITE_RULE) || (cellVal < r->infiniteDRight))
00639 return tmp;
00640
00641 return r->infiniteRRight;
00642 }
00643
00644
00645
00646 void
00647 G_fpreclass_perform_di (r, dcell, cell, n)
00648
00649 struct FPReclass *r;
00650 DCELL *dcell;
00651 CELL *cell;
00652 int n;
00653
00654 {
00655 int i;
00656
00657 for (i = 0; i < n; i++, dcell++)
00658 if (! G_is_d_null_value (dcell))
00659 *cell++ = G_fpreclass_get_cell_value (r, *dcell);
00660 else
00661 G_set_c_null_value (cell++, 1);
00662 }
00663
00664
00665
00666 void
00667 G_fpreclass_perform_df (r, dcell, cell, n)
00668
00669 struct FPReclass *r;
00670 DCELL *dcell;
00671 FCELL *cell;
00672 int n;
00673
00674 {
00675 int i;
00676
00677 for (i = 0; i < n; i++, dcell++)
00678 if (! G_is_d_null_value (dcell))
00679 *cell++ = G_fpreclass_get_cell_value (r, *dcell);
00680 else
00681 G_set_f_null_value (cell++, 1);
00682 }
00683
00684
00685
00686 void
00687 G_fpreclass_perform_dd (r, dcell, cell, n)
00688
00689 struct FPReclass *r;
00690 DCELL *dcell;
00691 DCELL *cell;
00692 int n;
00693
00694 {
00695 int i;
00696
00697 for (i = 0; i < n; i++, dcell++)
00698 if (! G_is_d_null_value (dcell))
00699 *cell++ = G_fpreclass_get_cell_value (r, *dcell);
00700 else
00701 G_set_d_null_value (cell++, 1);
00702 }
00703
00704
00705
00706 void
00707 G_fpreclass_perform_fi (r, fcell, cell, n)
00708
00709 struct FPReclass *r;
00710 FCELL *fcell;
00711 CELL *cell;
00712 int n;
00713
00714 {
00715 int i;
00716
00717 for (i = 0; i < n; i++, fcell++)
00718 if (! G_is_f_null_value (fcell))
00719 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *fcell);
00720 else
00721 G_set_c_null_value (cell++, 1);
00722 }
00723
00724
00725
00726 void
00727 G_fpreclass_perform_ff (r, fcell, cell, n)
00728
00729 struct FPReclass *r;
00730 FCELL *fcell;
00731 FCELL *cell;
00732 int n;
00733
00734 {
00735 int i;
00736
00737 for (i = 0; i < n; i++, fcell++)
00738 if (! G_is_f_null_value (fcell))
00739 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *fcell);
00740 else
00741 G_set_f_null_value (cell++, 1);
00742 }
00743
00744
00745
00746 void
00747 G_fpreclass_perform_fd (r, fcell, cell, n)
00748
00749 struct FPReclass *r;
00750 FCELL *fcell;
00751 DCELL *cell;
00752 int n;
00753
00754 {
00755 int i;
00756
00757 for (i = 0; i < n; i++, fcell++)
00758 if (! G_is_f_null_value (fcell))
00759 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *fcell);
00760 else
00761 G_set_d_null_value (cell++, 1);
00762 }
00763
00764
00765
00766 void
00767 G_fpreclass_perform_ii (r, icell, cell, n)
00768
00769 struct FPReclass *r;
00770 CELL *icell;
00771 CELL *cell;
00772 int n;
00773
00774 {
00775 int i;
00776
00777 for (i = 0; i < n; i++, icell++)
00778 if (! G_is_c_null_value (icell))
00779 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *icell);
00780 else
00781 G_set_c_null_value (cell++, 1);
00782 }
00783
00784
00785
00786 void
00787 G_fpreclass_perform_if (r, icell, cell, n)
00788
00789 struct FPReclass *r;
00790 CELL *icell;
00791 FCELL *cell;
00792 int n;
00793
00794 {
00795 int i;
00796
00797 for (i = 0; i < n; i++, icell++)
00798 if (! G_is_c_null_value (icell))
00799 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *icell);
00800 else
00801 G_set_f_null_value (cell++, 1);
00802 }
00803
00804
00805
00806 void
00807 G_fpreclass_perform_id (r, icell, cell, n)
00808
00809 struct FPReclass *r;
00810 CELL *icell;
00811 DCELL *cell;
00812 int n;
00813
00814 {
00815 int i;
00816
00817 for (i = 0; i < n; i++, icell++)
00818 if (! G_is_c_null_value (icell))
00819 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *icell);
00820 else
00821 G_set_d_null_value (cell++, 1);
00822 }
00823
00824
00825
00826
00827