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 <grass/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 (struct FPReclass *r)
00264
00265 {
00266 r->nofRules = 0;
00267 r->defaultDRuleSet = 0;
00268 r->defaultRRuleSet = 0;
00269 r->infiniteRightSet = r->infiniteLeftSet = 0;
00270 }
00271
00272
00273
00274 void
00275 G_fpreclass_reset (struct FPReclass *r)
00276
00277 {
00278 G_fpreclass_clear (r);
00279
00280 if (r->maxNofRules > 0) G_free (r->table);
00281
00282 r->maxNofRules = 0;
00283 }
00284
00285
00286
00287 int G_fpreclass_init (struct FPReclass *r)
00288
00289 {
00290 r->maxNofRules = 0;
00291 G_fpreclass_reset (r);
00292
00293 return 1;
00294 }
00295
00296
00297
00298 void
00299 G_fpreclass_set_domain (struct FPReclass *r, DCELL dLow, DCELL dHigh)
00300
00301 {
00302 r->defaultDMin = dLow;
00303 r->defaultDMax = dHigh;
00304 r->defaultDRuleSet = 1;
00305 }
00306
00307
00308
00309 void
00310 G_fpreclass_set_range (struct FPReclass *r, DCELL low, DCELL high)
00311
00312 {
00313 r->defaultRMin = low;
00314 r->defaultRMax = high;
00315 r->defaultRRuleSet = 1;
00316 }
00317
00318
00319
00320 static void
00321 fpreclass_set_limits (struct FPReclass *r, DCELL dLow, DCELL dHigh, DCELL rLow, DCELL rHigh)
00322
00323 {
00324 r->dMin = dLow;
00325 r->dMax = dHigh;
00326 r->rMin = rLow;
00327 r->rMax = rHigh;
00328 }
00329
00330
00331
00332 static void
00333 fpreclass_update_limits (struct FPReclass *r, DCELL dLow, DCELL dHigh, DCELL rLow, DCELL rHigh)
00334
00335 {
00336 if (NO_EXPLICIT_RULE) {
00337 fpreclass_set_limits (r, dLow, dHigh, rLow, rHigh);
00338 return;
00339 }
00340
00341 r->dMin = MIN (r->dMin, MIN (dLow, dHigh));
00342 r->dMax = MAX (r->dMax, MAX (dLow, dHigh));
00343 r->rMin = MIN (r->rMin, MIN (rLow, rHigh));
00344 r->rMax = MAX (r->rMax, MAX (rLow, rHigh));
00345 }
00346
00347
00348
00349 int
00350 G_fpreclass_get_limits (struct FPReclass *r, DCELL *dMin, DCELL *dMax, DCELL *rMin, DCELL *rMax)
00351
00352 {
00353 if (NO_EXPLICIT_RULE) {
00354 if (NO_DEFAULT_RULE) return -1;
00355
00356 *dMin = r->defaultDMin; *dMax = r->defaultDMax;
00357
00358 if (r->defaultRRuleSet) {
00359 *rMin = r->defaultRMin; *rMax = r->defaultRMax;
00360 } else {
00361 *rMin = DEFAULT_MIN; *rMax = DEFAULT_MAX;
00362 }
00363
00364 return 0;
00365 }
00366
00367 *dMin = r->dMin; *dMax = r->dMax;
00368 *rMin = r->rMin; *rMax = r->rMax;
00369
00370 return 1;
00371 }
00372
00373
00374
00375 int
00376 G_fpreclass_nof_rules (struct FPReclass *r)
00377
00378 {
00379 return r->nofRules;
00380 }
00381
00382
00383
00384 void
00385 G_fpreclass_get_ith_rule (struct FPReclass *r, int i, DCELL *dLow, DCELL *dHigh, DCELL *rLow, DCELL *rHigh)
00386
00387 {
00388 *dLow = r->table[i].dLow;
00389 *dHigh = r->table[i].dHigh;
00390 *rLow = r->table[i].rLow;
00391 *rHigh = r->table[i].rHigh;
00392 }
00393
00394
00395
00396 static void
00397 fpreclass_table_increase (struct FPReclass *r)
00398
00399 {
00400 if (r->nofRules < r->maxNofRules) return;
00401
00402 if (r->maxNofRules == 0) {
00403 r->maxNofRules = 50;
00404 r->table = (struct FPReclass_table *)
00405 G_malloc (r->maxNofRules * sizeof (struct FPReclass_table));
00406 } else {
00407 r->maxNofRules += 50;
00408 r->table = (struct FPReclass_table *)
00409 G_realloc ((char *) r->table, r->maxNofRules * sizeof (struct FPReclass_table));
00410 }
00411 }
00412
00413
00414
00415 void
00416 G_fpreclass_set_neg_infinite_rule (struct FPReclass *r, DCELL dLeft, DCELL c)
00417
00418 {
00419 r->infiniteDLeft = dLeft;
00420 r->infiniteRLeft = c;
00421 fpreclass_update_limits (r, dLeft, dLeft, c, c);
00422 r->infiniteLeftSet = 1;
00423 }
00424
00425
00426
00427 int
00428 G_fpreclass_get_neg_infinite_rule (struct FPReclass *r, DCELL *dLeft, DCELL *c)
00429
00430 {
00431 if (r->infiniteLeftSet == 0) return 0;
00432
00433 *dLeft = r->infiniteDLeft;
00434 *c = r->infiniteRLeft;
00435
00436 return 1;
00437 }
00438
00439
00440
00441 void
00442 G_fpreclass_set_pos_infinite_rule (struct FPReclass *r, DCELL dRight, DCELL c)
00443
00444 {
00445 r->infiniteDRight = dRight;
00446 r->infiniteRRight = c;
00447 fpreclass_update_limits (r, dRight, dRight, c, c);
00448 r->infiniteRightSet = 1;
00449 }
00450
00451
00452
00453 int
00454 G_fpreclass_get_pos_infinite_rule (struct FPReclass *r, DCELL *dRight, DCELL *c)
00455
00456 {
00457 if (r->infiniteRightSet == 0) return 0;
00458
00459 *dRight = r->infiniteDRight;
00460 *c = r->infiniteRRight;
00461
00462 return 1;
00463 }
00464
00465
00466
00467 void
00468 G_fpreclass_add_rule (struct FPReclass *r, DCELL dLow, DCELL dHigh, DCELL rLow, DCELL rHigh)
00469
00470 {
00471 int i;
00472 struct FPReclass_table *p;
00473
00474 fpreclass_table_increase (r);
00475
00476 i = r->nofRules;
00477
00478 p = &(r->table[i]);
00479 if (dHigh >= dLow) {
00480 p->dLow = dLow; p->dHigh = dHigh; p->rLow = rLow; p->rHigh = rHigh;
00481 } else {
00482 p->dLow = dHigh; p->dHigh = dLow; p->rLow = rHigh; p->rHigh = rLow;
00483 }
00484
00485 fpreclass_update_limits (r, dLow, dHigh, rLow, rHigh);
00486
00487 r->nofRules++;
00488 }
00489
00490
00491
00492 void
00493 G_fpreclass_reverse_rule_order (struct FPReclass *r)
00494
00495 {
00496 struct FPReclass_table tmp;
00497 struct FPReclass_table *pLeft, *pRight;
00498
00499 pLeft = r->table;
00500 pRight = &(r->table [r->nofRules - 1]);
00501
00502 while (pLeft < pRight) {
00503 tmp.dLow = pLeft->dLow; tmp.dHigh = pLeft->dHigh;
00504 tmp.rLow = pLeft->rLow; tmp.rHigh = pLeft->rHigh;
00505
00506 pLeft->dLow = pRight->dLow; pLeft->dHigh = pRight->dHigh;
00507 pLeft->rLow = pRight->rLow; pLeft->rHigh = pRight->rHigh;
00508
00509 pRight->dLow = tmp.dLow; pRight->dHigh = tmp.dHigh;
00510 pRight->rLow = tmp.rLow; pRight->rHigh = tmp.rHigh;
00511
00512 pLeft++; pRight--;
00513 }
00514 }
00515
00516
00517
00518 static DCELL
00519 fpreclass_interpolate (DCELL dLow, DCELL dHigh, DCELL rLow, DCELL rHigh, DCELL dValue)
00520
00521 {
00522 if (rLow == rHigh) return rLow;
00523 if (dLow == dHigh) return rLow;
00524
00525 return ((dValue - dLow) / (dHigh - dLow) * (rHigh - rLow) + rLow);
00526 }
00527
00528
00529
00530 static DCELL
00531 fpreclass_get_default_cell_value (struct FPReclass *r, DCELL cellVal)
00532
00533 {
00534 DCELL tmp;
00535 G_set_d_null_value(&tmp, 1);
00536
00537 if ((cellVal < MIN (r->defaultDMin, r->defaultDMax)) ||
00538 (cellVal > MAX (r->defaultDMin, r->defaultDMax))) return tmp;
00539
00540 if (r->defaultRRuleSet)
00541 return fpreclass_interpolate (r->defaultDMin, r->defaultDMax,
00542 r->defaultRMin, r->defaultRMax, cellVal);
00543 else
00544 return fpreclass_interpolate (r->defaultDMin, r->defaultDMax,
00545 DEFAULT_MIN, DEFAULT_MAX, cellVal);
00546 }
00547
00548
00549
00550 DCELL
00551 G_fpreclass_get_cell_value (struct FPReclass *r, DCELL cellVal)
00552
00553 {
00554 DCELL tmp;
00555 struct FPReclass_table *p;
00556
00557 G_set_d_null_value(&tmp, 1);
00558 if (NO_EXPLICIT_RULE) {
00559
00560 if (NO_DEFAULT_RULE) return tmp;
00561 return fpreclass_get_default_cell_value (r, cellVal);
00562 }
00563
00564 if (! NO_FINITE_RULE)
00565 for (p = &(r->table [r->nofRules - 1]); p >= r->table; p--)
00566 if ((cellVal >= p->dLow) && (cellVal <= p->dHigh))
00567 return fpreclass_interpolate (p->dLow, p->dHigh, p->rLow, p->rHigh,
00568 cellVal);
00569
00570 if ((! NO_LEFT_INFINITE_RULE) && (cellVal <= r->infiniteDLeft))
00571 return r->infiniteRLeft;
00572
00573 if ((NO_RIGHT_INFINITE_RULE) || (cellVal < r->infiniteDRight))
00574 return tmp;
00575
00576 return r->infiniteRRight;
00577 }
00578
00579
00580
00581 void
00582 G_fpreclass_perform_di (struct FPReclass *r, DCELL *dcell, CELL *cell, int n)
00583
00584 {
00585 int i;
00586
00587 for (i = 0; i < n; i++, dcell++)
00588 if (! G_is_d_null_value (dcell))
00589 *cell++ = G_fpreclass_get_cell_value (r, *dcell);
00590 else
00591 G_set_c_null_value (cell++, 1);
00592 }
00593
00594
00595
00596 void
00597 G_fpreclass_perform_df (struct FPReclass *r, DCELL *dcell, FCELL *cell, int n)
00598
00599 {
00600 int i;
00601
00602 for (i = 0; i < n; i++, dcell++)
00603 if (! G_is_d_null_value (dcell))
00604 *cell++ = G_fpreclass_get_cell_value (r, *dcell);
00605 else
00606 G_set_f_null_value (cell++, 1);
00607 }
00608
00609
00610
00611 void
00612 G_fpreclass_perform_dd (struct FPReclass *r, DCELL *dcell, DCELL *cell, int n)
00613
00614 {
00615 int i;
00616
00617 for (i = 0; i < n; i++, dcell++)
00618 if (! G_is_d_null_value (dcell))
00619 *cell++ = G_fpreclass_get_cell_value (r, *dcell);
00620 else
00621 G_set_d_null_value (cell++, 1);
00622 }
00623
00624
00625
00626 void
00627 G_fpreclass_perform_fi (struct FPReclass *r, FCELL *fcell, CELL *cell, int n)
00628
00629 {
00630 int i;
00631
00632 for (i = 0; i < n; i++, fcell++)
00633 if (! G_is_f_null_value (fcell))
00634 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *fcell);
00635 else
00636 G_set_c_null_value (cell++, 1);
00637 }
00638
00639
00640
00641 void
00642 G_fpreclass_perform_ff (struct FPReclass *r, FCELL *fcell, FCELL *cell, int n)
00643
00644 {
00645 int i;
00646
00647 for (i = 0; i < n; i++, fcell++)
00648 if (! G_is_f_null_value (fcell))
00649 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *fcell);
00650 else
00651 G_set_f_null_value (cell++, 1);
00652 }
00653
00654
00655
00656 void
00657 G_fpreclass_perform_fd (struct FPReclass *r, FCELL *fcell, DCELL *cell, int n)
00658
00659 {
00660 int i;
00661
00662 for (i = 0; i < n; i++, fcell++)
00663 if (! G_is_f_null_value (fcell))
00664 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *fcell);
00665 else
00666 G_set_d_null_value (cell++, 1);
00667 }
00668
00669
00670
00671 void
00672 G_fpreclass_perform_ii (struct FPReclass *r, CELL *icell, CELL *cell, int n)
00673
00674 {
00675 int i;
00676
00677 for (i = 0; i < n; i++, icell++)
00678 if (! G_is_c_null_value (icell))
00679 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *icell);
00680 else
00681 G_set_c_null_value (cell++, 1);
00682 }
00683
00684
00685
00686 void
00687 G_fpreclass_perform_if (struct FPReclass *r, CELL *icell, FCELL *cell, int n)
00688
00689 {
00690 int i;
00691
00692 for (i = 0; i < n; i++, icell++)
00693 if (! G_is_c_null_value (icell))
00694 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *icell);
00695 else
00696 G_set_f_null_value (cell++, 1);
00697 }
00698
00699
00700
00701 void
00702 G_fpreclass_perform_id (struct FPReclass *r, CELL *icell, DCELL *cell, int n)
00703
00704 {
00705 int i;
00706
00707 for (i = 0; i < n; i++, icell++)
00708 if (! G_is_c_null_value (icell))
00709 *cell++ = G_fpreclass_get_cell_value (r, (DCELL) *icell);
00710 else
00711 G_set_d_null_value (cell++, 1);
00712 }
00713
00714
00715
00716
00717