| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <math.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
|
|
| #include "wcserr.h" |
| #include "wcsmath.h" |
| #include "wcsprintf.h" |
| #include "wcsutil.h" |
| #include "tab.h" |
|
|
| const int TABSET = 137; |
|
|
| |
| const char *tab_errmsg[] = { |
| "Success", |
| "Null tabprm pointer passed", |
| "Memory allocation failed", |
| "Invalid tabular parameters", |
| "One or more of the x coordinates were invalid", |
| "One or more of the world coordinates were invalid"}; |
|
|
| |
| #define TAB_ERRMSG(status) WCSERR_SET(status), tab_errmsg[status] |
|
|
| |
|
|
| int tabini(int alloc, int M, const int K[], struct tabprm *tab) |
|
|
| { |
| static const char *function = "tabini"; |
|
|
| int k, m, N; |
| double *dp; |
| struct wcserr **err; |
|
|
| if (tab == 0x0) return TABERR_NULL_POINTER; |
|
|
| |
| err = &(tab->err); |
| if (tab->flag != -1) { |
| if (tab->err) free(tab->err); |
| } |
| tab->err = 0x0; |
|
|
|
|
| if (M <= 0) { |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "M must be positive, got %d", M); |
| } |
|
|
| |
| if (K) { |
| N = M; |
|
|
| for (m = 0; m < M; m++) { |
| if (K[m] < 0) { |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "Invalid tabular parameters: Each element of K must be " |
| "non-negative, got %d", K[m]); |
| } |
|
|
| N *= K[m]; |
| } |
|
|
| } else { |
| |
| N = 0; |
| } |
|
|
|
|
| |
| if (tab->flag == -1 || tab->m_flag != TABSET) { |
| if (tab->flag == -1) { |
| tab->sense = 0x0; |
| tab->p0 = 0x0; |
| tab->delta = 0x0; |
| tab->extrema = 0x0; |
| tab->set_M = 0; |
| } |
|
|
| tab->m_flag = 0; |
| tab->m_M = 0; |
| tab->m_N = 0; |
| tab->m_K = 0x0; |
| tab->m_map = 0x0; |
| tab->m_crval = 0x0; |
| tab->m_index = 0x0; |
| tab->m_indxs = 0x0; |
| tab->m_coord = 0x0; |
|
|
| } else { |
| |
| for (m = 0; m < tab->m_M; m++) { |
| if (tab->m_indxs[m] == (double *)0x1) tab->m_indxs[m] = 0x0; |
| } |
|
|
| if (tab->m_coord == (double *)0x1) tab->m_coord = 0x0; |
| } |
|
|
|
|
| |
| if (alloc || |
| tab->K == 0x0 || |
| tab->map == 0x0 || |
| tab->crval == 0x0 || |
| tab->index == 0x0 || |
| tab->coord == 0x0) { |
|
|
| |
| if (tab->m_flag == TABSET && (tab->m_M < M || tab->m_N < N)) { |
| |
| tabfree(tab); |
| } |
|
|
| if (alloc || tab->K == 0x0) { |
| if (tab->m_K) { |
| |
| tab->K = tab->m_K; |
|
|
| } else { |
| if (!(tab->K = calloc(M, sizeof(int)))) { |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| tab->m_flag = TABSET; |
| tab->m_M = M; |
| tab->m_K = tab->K; |
| } |
| } |
|
|
| if (alloc || tab->map == 0x0) { |
| if (tab->m_map) { |
| |
| tab->map = tab->m_map; |
|
|
| } else { |
| if (!(tab->map = calloc(M, sizeof(int)))) { |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| tab->m_flag = TABSET; |
| tab->m_M = M; |
| tab->m_map = tab->map; |
| } |
| } |
|
|
| if (alloc || tab->crval == 0x0) { |
| if (tab->m_crval) { |
| |
| tab->crval = tab->m_crval; |
|
|
| } else { |
| if (!(tab->crval = calloc(M, sizeof(double)))) { |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| tab->m_flag = TABSET; |
| tab->m_M = M; |
| tab->m_crval = tab->crval; |
| } |
| } |
|
|
| if (alloc || tab->index == 0x0) { |
| if (tab->m_index) { |
| |
| tab->index = tab->m_index; |
|
|
| } else { |
| if (!(tab->index = calloc(M, sizeof(double *)))) { |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| tab->m_flag = TABSET; |
| tab->m_M = M; |
| tab->m_N = N; |
| tab->m_index = tab->index; |
|
|
| if (!(tab->m_indxs = calloc(M, sizeof(double *)))) { |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| |
| if (K) { |
| for (m = 0; m < M; m++) { |
| if (K[m]) { |
| if (!(tab->index[m] = calloc(K[m], sizeof(double)))) { |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| tab->m_indxs[m] = tab->index[m]; |
| } |
| } |
| } |
| } |
| } |
|
|
| if (alloc || tab->coord == 0x0) { |
| if (tab->m_coord) { |
| |
| tab->coord = tab->m_coord; |
|
|
| } else if (N) { |
| if (!(tab->coord = calloc(N, sizeof(double)))) { |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| tab->m_flag = TABSET; |
| tab->m_M = M; |
| tab->m_N = N; |
| tab->m_coord = tab->coord; |
| } |
| } |
| } |
|
|
| tab->flag = 0; |
| tab->M = M; |
|
|
| |
| for (m = 0; m < M; m++) { |
| tab->map[m] = -1; |
| tab->crval[m] = 0.0; |
|
|
| if (K) { |
| tab->K[m] = K[m]; |
| if ((dp = tab->index[m])) { |
| for (k = 0; k < K[m]; k++) { |
| *(dp++) = k; |
| } |
| } |
| } else { |
| tab->K[m] = 0; |
| } |
| } |
|
|
| |
| for (dp = tab->coord; dp < tab->coord + N; dp++) { |
| *dp = UNDEFINED; |
| } |
|
|
| return 0; |
| } |
|
|
| |
|
|
| int tabmem(struct tabprm *tab) |
|
|
| { |
| static const char *function = "tabmem"; |
|
|
| int m, M, N; |
| struct wcserr **err; |
|
|
| if (tab == 0x0) return TABERR_NULL_POINTER; |
| err = &(tab->err); |
|
|
| if (tab->M == 0 || tab->K == 0x0) { |
| |
| return wcserr_set(WCSERR_SET(TABERR_MEMORY), |
| "Null pointers in tabprm struct"); |
| } |
|
|
|
|
| N = M = tab->M; |
| for (m = 0; m < M; m++) { |
| if (tab->K[m] < 0) { |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "Invalid tabular parameters: Each element of K must be " |
| "non-negative, got %d", M); |
| } |
|
|
| N *= tab->K[m]; |
| } |
|
|
|
|
| if (tab->m_M == 0) { |
| tab->m_M = M; |
| } else if (tab->m_M < M) { |
| |
| return wcserr_set(WCSERR_SET(TABERR_MEMORY), |
| "tabprm struct inconsistent"); |
| } |
|
|
| if (tab->m_N == 0) { |
| tab->m_N = N; |
| } else if (tab->m_N < N) { |
| |
| return wcserr_set(WCSERR_SET(TABERR_MEMORY), |
| "tabprm struct inconsistent"); |
| } |
|
|
| if (tab->m_K == 0x0) { |
| if ((tab->m_K = tab->K)) { |
| tab->m_flag = TABSET; |
| } |
| } |
|
|
| if (tab->m_map == 0x0) { |
| if ((tab->m_map = tab->map)) { |
| tab->m_flag = TABSET; |
| } |
| } |
|
|
| if (tab->m_crval == 0x0) { |
| if ((tab->m_crval = tab->crval)) { |
| tab->m_flag = TABSET; |
| } |
| } |
|
|
| if (tab->m_index == 0x0) { |
| if ((tab->m_index = tab->index)) { |
| tab->m_flag = TABSET; |
| } |
| } |
|
|
| for (m = 0; m < tab->m_M; m++) { |
| if (tab->m_indxs[m] == 0x0 || tab->m_indxs[m] == (double *)0x1) { |
| if ((tab->m_indxs[m] = tab->index[m])) { |
| tab->m_flag = TABSET; |
| } |
| } |
| } |
|
|
| if (tab->m_coord == 0x0 || tab->m_coord == (double *)0x1) { |
| if ((tab->m_coord = tab->coord)) { |
| tab->m_flag = TABSET; |
| } |
| } |
|
|
| tab->flag = 0; |
|
|
| return 0; |
| } |
|
|
| |
|
|
| int tabcpy(int alloc, const struct tabprm *tabsrc, struct tabprm *tabdst) |
|
|
| { |
| static const char *function = "tabcpy"; |
|
|
| int k, m, M, n, N, status; |
| double *dstp, *srcp; |
| struct wcserr **err; |
|
|
| if (tabsrc == 0x0) return TABERR_NULL_POINTER; |
| if (tabdst == 0x0) return TABERR_NULL_POINTER; |
| err = &(tabdst->err); |
|
|
| M = tabsrc->M; |
| if (M <= 0) { |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "M must be positive, got %d", M); |
| } |
|
|
| if ((status = tabini(alloc, M, tabsrc->K, tabdst))) { |
| return status; |
| } |
|
|
| N = M; |
| for (m = 0; m < M; m++) { |
| tabdst->map[m] = tabsrc->map[m]; |
| tabdst->crval[m] = tabsrc->crval[m]; |
| N *= tabsrc->K[m]; |
| } |
|
|
| for (m = 0; m < M; m++) { |
| if ((srcp = tabsrc->index[m])) { |
| dstp = tabdst->index[m]; |
| for (k = 0; k < tabsrc->K[m]; k++) { |
| *(dstp++) = *(srcp++); |
| } |
| } |
| } |
|
|
| srcp = tabsrc->coord; |
| dstp = tabdst->coord; |
| for (n = 0; n < N; n++) { |
| *(dstp++) = *(srcp++); |
| } |
|
|
| return 0; |
| } |
|
|
| |
|
|
| int tabcmp(int dummy, |
| double tol, |
| const struct tabprm *tab1, |
| const struct tabprm *tab2, |
| int *equal) |
|
|
| { |
| int m, M, N; |
|
|
| |
| (void)dummy; |
|
|
| if (tab1 == 0x0) return TABERR_NULL_POINTER; |
| if (tab2 == 0x0) return TABERR_NULL_POINTER; |
| if (equal == 0x0) return TABERR_NULL_POINTER; |
|
|
| *equal = 0; |
|
|
| if (tab1->M != tab2->M) { |
| return 0; |
| } |
|
|
| M = tab1->M; |
|
|
| if (!wcsutil_intEq(M, tab1->K, tab2->K) || |
| !wcsutil_intEq(M, tab1->map, tab2->map) || |
| !wcsutil_Eq(M, tol, tab1->crval, tab2->crval)) { |
| return 0; |
| } |
|
|
| N = M; |
| for (m = 0; m < M; m++) { |
| if (!wcsutil_Eq(tab1->K[m], tol, tab1->index[m], tab2->index[m])) { |
| return 0; |
| } |
|
|
| N *= tab1->K[m]; |
| } |
|
|
| if (!wcsutil_Eq(N, tol, tab1->coord, tab2->coord)) { |
| return 0; |
| } |
|
|
| *equal = 1; |
|
|
| return 0; |
| } |
|
|
|
|
| |
|
|
| int tabfree(struct tabprm *tab) |
|
|
| { |
| int m; |
|
|
| if (tab == 0x0) return TABERR_NULL_POINTER; |
|
|
| if (tab->flag != -1) { |
| |
| for (m = 0; m < tab->m_M; m++) { |
| if (tab->m_indxs[m] == (double *)0x1) tab->m_indxs[m] = 0x0; |
| } |
|
|
| if (tab->m_coord == (double *)0x1) tab->m_coord = 0x0; |
|
|
| |
| if (tab->m_flag == TABSET) { |
| if (tab->K == tab->m_K) tab->K = 0x0; |
| if (tab->map == tab->m_map) tab->map = 0x0; |
| if (tab->crval == tab->m_crval) tab->crval = 0x0; |
| if (tab->index == tab->m_index) tab->index = 0x0; |
| if (tab->coord == tab->m_coord) tab->coord = 0x0; |
|
|
| if (tab->m_K) free(tab->m_K); |
| if (tab->m_map) free(tab->m_map); |
| if (tab->m_crval) free(tab->m_crval); |
|
|
| if (tab->m_index) { |
| for (m = 0; m < tab->m_M; m++) { |
| if (tab->m_indxs[m]) free(tab->m_indxs[m]); |
| } |
| free(tab->m_index); |
| free(tab->m_indxs); |
| } |
|
|
| if (tab->m_coord) free(tab->m_coord); |
| } |
|
|
| |
| if (tab->sense) free(tab->sense); |
| if (tab->p0) free(tab->p0); |
| if (tab->delta) free(tab->delta); |
| if (tab->extrema) free(tab->extrema); |
| } |
|
|
| tab->m_flag = 0; |
| tab->m_M = 0; |
| tab->m_N = 0; |
| tab->m_K = 0x0; |
| tab->m_map = 0x0; |
| tab->m_crval = 0x0; |
| tab->m_index = 0x0; |
| tab->m_indxs = 0x0; |
| tab->m_coord = 0x0; |
|
|
| tab->sense = 0x0; |
| tab->p0 = 0x0; |
| tab->delta = 0x0; |
| tab->extrema = 0x0; |
| tab->set_M = 0; |
|
|
| if (tab->err) { |
| free(tab->err); |
| tab->err = 0x0; |
| } |
|
|
| tab->flag = 0; |
|
|
| return 0; |
| } |
|
|
| |
|
|
| int tabprt(const struct tabprm *tab) |
|
|
| { |
| char *cp, text[128]; |
| int j, k, m, n, nd; |
| double *dp; |
|
|
| if (tab == 0x0) return TABERR_NULL_POINTER; |
|
|
| if (tab->flag != TABSET) { |
| wcsprintf("The tabprm struct is UNINITIALIZED.\n"); |
| return 0; |
| } |
|
|
| wcsprintf(" flag: %d\n", tab->flag); |
| wcsprintf(" M: %d\n", tab->M); |
|
|
| |
| WCSPRINTF_PTR(" K: ", tab->K, "\n"); |
| wcsprintf(" "); |
| for (m = 0; m < tab->M; m++) { |
| wcsprintf("%6d", tab->K[m]); |
| } |
| wcsprintf("\n"); |
|
|
| |
| WCSPRINTF_PTR(" map: ", tab->map, "\n"); |
| wcsprintf(" "); |
| for (m = 0; m < tab->M; m++) { |
| wcsprintf("%6d", tab->map[m]); |
| } |
| wcsprintf("\n"); |
|
|
| |
| WCSPRINTF_PTR(" crval: ", tab->crval, "\n"); |
| wcsprintf(" "); |
| for (m = 0; m < tab->M; m++) { |
| wcsprintf(" %#- 11.5g", tab->crval[m]); |
| } |
| wcsprintf("\n"); |
|
|
| |
| WCSPRINTF_PTR(" index: ", tab->index, "\n"); |
| for (m = 0; m < tab->M; m++) { |
| wcsprintf(" index[%d]: ", m); |
| WCSPRINTF_PTR("", tab->index[m], ""); |
| if (tab->index[m]) { |
| for (k = 0; k < tab->K[m]; k++) { |
| if (k%5 == 0) { |
| wcsprintf("\n "); |
| } |
| wcsprintf(" %#- 11.5g", tab->index[m][k]); |
| } |
| wcsprintf("\n"); |
| } |
| } |
|
|
| |
| WCSPRINTF_PTR(" coord: ", tab->coord, "\n"); |
| dp = tab->coord; |
| for (n = 0; n < tab->nc; n++) { |
| |
| j = n; |
| cp = text; |
| for (m = 0; m < tab->M; m++) { |
| nd = (tab->K[m] < 10) ? 1 : 2; |
| sprintf(cp, ",%*d", nd, j % tab->K[m] + 1); |
| j /= tab->K[m]; |
| cp += strlen(cp); |
| } |
|
|
| wcsprintf(" (*%s)", text); |
| for (m = 0; m < tab->M; m++) { |
| wcsprintf(" %#- 11.5g", *(dp++)); |
| } |
| wcsprintf("\n"); |
| } |
|
|
| wcsprintf(" nc: %d\n", tab->nc); |
|
|
| WCSPRINTF_PTR(" sense: ", tab->sense, "\n"); |
| if (tab->sense) { |
| wcsprintf(" "); |
| for (m = 0; m < tab->M; m++) { |
| wcsprintf("%6d", tab->sense[m]); |
| } |
| wcsprintf("\n"); |
| } |
|
|
| WCSPRINTF_PTR(" p0: ", tab->p0, "\n"); |
| if (tab->p0) { |
| wcsprintf(" "); |
| for (m = 0; m < tab->M; m++) { |
| wcsprintf("%6d", tab->p0[m]); |
| } |
| wcsprintf("\n"); |
| } |
|
|
| WCSPRINTF_PTR(" delta: ", tab->delta, "\n"); |
| if (tab->delta) { |
| wcsprintf(" "); |
| for (m = 0; m < tab->M; m++) { |
| wcsprintf(" %#- 11.5g", tab->delta[m]); |
| } |
| wcsprintf("\n"); |
| } |
|
|
| WCSPRINTF_PTR(" extrema: ", tab->extrema, "\n"); |
| dp = tab->extrema; |
| for (n = 0; n < tab->nc/tab->K[0]; n++) { |
| |
| j = n; |
| cp = text; |
| *cp = '\0'; |
| for (m = 1; m < tab->M; m++) { |
| nd = (tab->K[m] < 10) ? 1 : 2; |
| sprintf(cp, ",%*d", nd, j % tab->K[m] + 1); |
| j /= tab->K[m]; |
| cp += strlen(cp); |
| } |
|
|
| wcsprintf(" (*,*%s)", text); |
| for (m = 0; m < 2*tab->M; m++) { |
| if (m == tab->M) wcsprintf("-> "); |
| wcsprintf(" %#- 11.5g", *(dp++)); |
| } |
| wcsprintf("\n"); |
| } |
|
|
| WCSPRINTF_PTR(" err: ", tab->err, "\n"); |
| if (tab->err) { |
| wcserr_prt(tab->err, " "); |
| } |
|
|
| |
| wcsprintf(" m_flag: %d\n", tab->m_flag); |
| wcsprintf(" m_M: %d\n", tab->m_M); |
| wcsprintf(" m_N: %d\n", tab->m_N); |
|
|
| WCSPRINTF_PTR(" m_K: ", tab->m_K, ""); |
| if (tab->m_K == tab->K) wcsprintf(" (= K)"); |
| wcsprintf("\n"); |
|
|
| WCSPRINTF_PTR(" m_map: ", tab->m_map, ""); |
| if (tab->m_map == tab->map) wcsprintf(" (= map)"); |
| wcsprintf("\n"); |
|
|
| WCSPRINTF_PTR(" m_crval: ", tab->m_crval, ""); |
| if (tab->m_crval == tab->crval) wcsprintf(" (= crval)"); |
| wcsprintf("\n"); |
|
|
| WCSPRINTF_PTR(" m_index: ", tab->m_index, ""); |
| if (tab->m_index == tab->index) wcsprintf(" (= index)"); |
| wcsprintf("\n"); |
| for (m = 0; m < tab->M; m++) { |
| wcsprintf(" m_indxs[%d]: ", m); |
| WCSPRINTF_PTR("", tab->m_indxs[m], ""); |
| if (tab->m_indxs[m] == tab->index[m]) wcsprintf(" (= index[%d])", m); |
| wcsprintf("\n"); |
| } |
|
|
| WCSPRINTF_PTR(" m_coord: ", tab->m_coord, ""); |
| if (tab->m_coord == tab->coord) wcsprintf(" (= coord)"); |
| wcsprintf("\n"); |
|
|
| return 0; |
| } |
|
|
| |
|
|
| int tabperr(const struct tabprm *tab, const char *prefix) |
|
|
| { |
| if (tab == 0x0) return TABERR_NULL_POINTER; |
|
|
| if (tab->err) { |
| wcserr_prt(tab->err, prefix); |
| } |
|
|
| return 0; |
| } |
|
|
| |
|
|
| int tabset(struct tabprm *tab) |
|
|
| { |
| static const char *function = "tabset"; |
|
|
| int i, ic, k, *Km, m, M, ne; |
| double *dcrd, *dmax, *dmin, dPsi, dval, *Psi; |
| struct wcserr **err; |
|
|
| if (tab == 0x0) return TABERR_NULL_POINTER; |
| err = &(tab->err); |
|
|
| |
| if ((M = tab->M) < 1) { |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "Invalid tabular parameters: M must be positive, got %d", M); |
| } |
|
|
| |
| if (!tab->K) { |
| return wcserr_set(WCSERR_SET(TABERR_MEMORY), |
| "Null pointers in tabprm struct"); |
| } |
|
|
| tab->nc = 1; |
| for (m = 0; m < M; m++) { |
| if (tab->K[m] < 1) { |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "Invalid tabular parameters: Each element of K must be positive, " |
| "got %d", tab->K[m]); |
| } |
|
|
| |
| tab->nc *= tab->K[m]; |
| } |
|
|
| |
| if (!tab->map) { |
| return wcserr_set(WCSERR_SET(TABERR_MEMORY), |
| "Null pointers in tabprm struct"); |
| } |
|
|
| for (m = 0; m < M; m++) { |
| i = tab->map[m]; |
| if (i < 0) { |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "Invalid tabular parameters: Each element of map must be " |
| "non-negative, got %d", i); |
| } |
| } |
|
|
| |
| if (!tab->crval || !tab->index || !tab->coord) { |
| return wcserr_set(WCSERR_SET(TABERR_MEMORY), |
| "Null pointers in tabprm struct"); |
| } |
|
|
| |
| for (m = 0; m < tab->m_M; m++) { |
| if (tab->m_indxs[m] == (double *)0x1 && |
| (tab->m_indxs[m] = tab->index[m])) { |
| tab->m_flag = TABSET; |
| } |
| } |
|
|
| if (tab->m_coord == (double *)0x1 && |
| (tab->m_coord = tab->coord)) { |
| tab->m_flag = TABSET; |
| } |
|
|
|
|
| |
| if (tab->flag != TABSET || tab->set_M < M) { |
| |
| if (tab->sense) free(tab->sense); |
| if (tab->p0) free(tab->p0); |
| if (tab->delta) free(tab->delta); |
| if (tab->extrema) free(tab->extrema); |
|
|
| |
| if (!(tab->sense = calloc(M, sizeof(int)))) { |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| if (!(tab->p0 = calloc(M, sizeof(int)))) { |
| free(tab->sense); |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| if (!(tab->delta = calloc(M, sizeof(double)))) { |
| free(tab->sense); |
| free(tab->p0); |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| ne = M * tab->nc * 2 / tab->K[0]; |
| if (!(tab->extrema = calloc(ne, sizeof(double)))) { |
| free(tab->sense); |
| free(tab->p0); |
| free(tab->delta); |
| return wcserr_set(TAB_ERRMSG(TABERR_MEMORY)); |
| } |
|
|
| tab->set_M = M; |
| } |
|
|
| |
| Km = tab->K; |
| for (m = 0; m < M; m++, Km++) { |
| tab->sense[m] = 0; |
|
|
| if (*Km > 1) { |
| if ((Psi = tab->index[m]) == 0x0) { |
| |
| tab->sense[m] = 1; |
|
|
| } else { |
| for (k = 0; k < *Km-1; k++) { |
| switch (tab->sense[m]) { |
| case 0: |
| if (Psi[k] < Psi[k+1]) { |
| |
| tab->sense[m] = 1; |
| } else if (Psi[k] > Psi[k+1]) { |
| |
| tab->sense[m] = -1; |
| } |
| break; |
|
|
| case 1: |
| if (Psi[k] > Psi[k+1]) { |
| |
| free(tab->sense); |
| free(tab->p0); |
| free(tab->delta); |
| free(tab->extrema); |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "Invalid tabular parameters: Index vectors are not " |
| "monotonically increasing"); |
| } |
| break; |
|
|
| case -1: |
| if (Psi[k] < Psi[k+1]) { |
| |
| free(tab->sense); |
| free(tab->p0); |
| free(tab->delta); |
| free(tab->extrema); |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "Invalid tabular parameters: Index vectors are not " |
| "monotonically decreasing"); |
| } |
| break; |
| } |
| } |
| } |
|
|
| if (tab->sense[m] == 0) { |
| free(tab->sense); |
| free(tab->p0); |
| free(tab->delta); |
| free(tab->extrema); |
| return wcserr_set(WCSERR_SET(TABERR_BAD_PARAMS), |
| "Invalid tabular parameters: Index vectors are not monotonic"); |
| } |
| } |
| } |
|
|
| |
| dcrd = tab->coord; |
| dmin = tab->extrema; |
| dmax = tab->extrema + M; |
| for (ic = 0; ic < tab->nc; ic += tab->K[0]) { |
| for (m = 0; m < M; m++, dcrd++) { |
| if (tab->K[0] > 1) { |
| |
| Psi = tab->index[0]; |
| if (Psi == 0x0) { |
| dPsi = 1.0; |
| } else { |
| dPsi = Psi[1] - Psi[0]; |
| } |
|
|
| dval = *dcrd; |
| if (dPsi != 0.0) { |
| dval -= 0.5 * (*(dcrd+M) - *dcrd)/dPsi; |
| } |
|
|
| *(dmax+m) = *(dmin+m) = dval; |
| } else { |
| *(dmax+m) = *(dmin+m) = *dcrd; |
| } |
| } |
|
|
| dcrd -= M; |
| for (i = 0; i < tab->K[0]; i++) { |
| for (m = 0; m < M; m++, dcrd++) { |
| if (*(dmax+m) < *dcrd) *(dmax+m) = *dcrd; |
| if (*(dmin+m) > *dcrd) *(dmin+m) = *dcrd; |
|
|
| if (tab->K[0] > 1 && i == tab->K[0]-1) { |
| |
| Psi = tab->index[0]; |
| if (Psi == 0x0) { |
| dPsi = 1.0; |
| } else { |
| dPsi = Psi[i] - Psi[i-1]; |
| } |
|
|
| dval = *dcrd; |
| if (dPsi != 0.0) { |
| dval += 0.5 * (*dcrd - *(dcrd-M))/dPsi; |
| } |
|
|
| if (*(dmax+m) < dval) *(dmax+m) = dval; |
| if (*(dmin+m) > dval) *(dmin+m) = dval; |
| } |
| } |
| } |
|
|
| dmin += 2*M; |
| dmax += 2*M; |
| } |
|
|
| tab->flag = TABSET; |
|
|
| return 0; |
| } |
|
|
| |
|
|
| int tabx2s( |
| struct tabprm *tab, |
| int ncoord, |
| int nelem, |
| const double x[], |
| double world[], |
| int stat[]) |
|
|
| { |
| static const char *function = "tabx2s"; |
|
|
| int i, iv, k, *Km, m, M, n, nv, offset, p1, status; |
| double *coord, *Psi, psi_m, upsilon, wgt; |
| register int *statp; |
| register const double *xp; |
| register double *wp; |
| struct wcserr **err; |
|
|
| if (tab == 0x0) return TABERR_NULL_POINTER; |
| err = &(tab->err); |
|
|
| |
| if (tab->flag != TABSET) { |
| if ((status = tabset(tab))) return status; |
| } |
|
|
| |
| M = tab->M; |
|
|
| status = 0; |
| xp = x; |
| wp = world; |
| statp = stat; |
| for (n = 0; n < ncoord; n++) { |
| |
| Km = tab->K; |
| for (m = 0; m < M; m++, Km++) { |
| |
| i = tab->map[m]; |
| psi_m = *(xp+i) + tab->crval[m]; |
|
|
| Psi = tab->index[m]; |
| if (Psi == 0x0) { |
| |
| upsilon = psi_m; |
|
|
| } else { |
| |
| |
| Psi--; |
|
|
| if (*Km == 1) { |
| |
| if (Psi[1]-0.5 <= psi_m && psi_m <= Psi[1]+0.5) { |
| upsilon = psi_m; |
| } else { |
| *statp = 1; |
| status = wcserr_set(TAB_ERRMSG(TABERR_BAD_X)); |
| goto next; |
| } |
|
|
| } else { |
| |
| if (tab->sense[m] == 1) { |
| |
| if (psi_m < Psi[1]) { |
| if (Psi[1] - 0.5*(Psi[2]-Psi[1]) <= psi_m) { |
| |
| k = 1; |
|
|
| } else { |
| |
| *statp = 1; |
| status = wcserr_set(TAB_ERRMSG(TABERR_BAD_X)); |
| goto next; |
| } |
|
|
| } else if (Psi[*Km] < psi_m) { |
| if (psi_m <= Psi[*Km] + 0.5*(Psi[*Km]-Psi[*Km-1])) { |
| |
| k = *Km - 1; |
|
|
| } else { |
| |
| *statp = 1; |
| status = wcserr_set(TAB_ERRMSG(TABERR_BAD_X)); |
| goto next; |
| } |
|
|
| } else { |
| for (k = 1; k < *Km; k++) { |
| if (psi_m < Psi[k]) { |
| continue; |
| } |
| if (Psi[k] == psi_m && psi_m < Psi[k+1]) { |
| break; |
| } |
| if (Psi[k] < psi_m && psi_m <= Psi[k+1]) { |
| break; |
| } |
| } |
| } |
|
|
| } else { |
| |
| if (psi_m > Psi[1]) { |
| if (Psi[1] + 0.5*(Psi[1]-Psi[2]) >= psi_m) { |
| |
| k = 1; |
|
|
| } else { |
| |
| *statp = 1; |
| status = wcserr_set(TAB_ERRMSG(TABERR_BAD_X)); |
| goto next; |
| } |
|
|
| } else if (psi_m < Psi[*Km]) { |
| if (Psi[*Km] - 0.5*(Psi[*Km-1]-Psi[*Km]) <= psi_m) { |
| |
| k = *Km - 1; |
|
|
| } else { |
| |
| *statp = 1; |
| status = wcserr_set(TAB_ERRMSG(TABERR_BAD_X)); |
| goto next; |
| } |
|
|
| } else { |
| for (k = 1; k < *Km; k++) { |
| if (psi_m > Psi[k]) { |
| continue; |
| } |
| if (Psi[k] == psi_m && psi_m > Psi[k+1]) { |
| break; |
| } |
| if (Psi[k] > psi_m && psi_m >= Psi[k+1]) { |
| break; |
| } |
| } |
| } |
| } |
|
|
| upsilon = k + (psi_m - Psi[k]) / (Psi[k+1] - Psi[k]); |
| } |
| } |
|
|
| if (upsilon < 0.5 || upsilon > *Km + 0.5) { |
| |
| *statp = 1; |
| status = wcserr_set(TAB_ERRMSG(TABERR_BAD_X)); |
| goto next; |
| } |
|
|
| |
| |
| p1 = (int)floor(upsilon); |
| tab->p0[m] = p1 - 1; |
| tab->delta[m] = upsilon - p1; |
|
|
| if (p1 == 0) { |
| tab->p0[m] += 1; |
| tab->delta[m] -= 1.0; |
| } else if (p1 == *Km && *Km > 1) { |
| tab->p0[m] -= 1; |
| tab->delta[m] += 1.0; |
| } |
| } |
|
|
|
|
| |
| |
| for (m = 0; m < M; m++) { |
| i = tab->map[m]; |
| *(wp+i) = 0.0; |
| } |
|
|
| |
| nv = 1 << M; |
| for (iv = 0; iv < nv; iv++) { |
| |
| offset = 0; |
| wgt = 1.0; |
| for (m = M-1; m >= 0; m--) { |
| offset *= tab->K[m]; |
| offset += tab->p0[m]; |
| if (iv & (1 << m)) { |
| if (tab->K[m] > 1) offset++; |
| wgt *= tab->delta[m]; |
| } else { |
| wgt *= 1.0 - tab->delta[m]; |
| } |
| } |
|
|
| if (wgt == 0.0) continue; |
|
|
| |
| coord = tab->coord + offset*M; |
| for (m = 0; m < M; m++) { |
| i = tab->map[m]; |
| *(wp+i) += *(coord++) * wgt; |
| } |
|
|
| if (wgt == 1.0) break; |
| } |
|
|
| *statp = 0; |
|
|
| next: |
| xp += nelem; |
| wp += nelem; |
| statp++; |
| } |
|
|
| return status; |
| } |
|
|
| |
|
|
| int tabs2x( |
| struct tabprm* tab, |
| int ncoord, |
| int nelem, |
| const double world[], |
| double x[], |
| int stat[]) |
|
|
| { |
| static const char *function = "tabs2x"; |
|
|
| int tabedge(struct tabprm *); |
| int tabrow(struct tabprm *, const double *); |
| int tabvox(struct tabprm *, const double *, int, double **, unsigned int *); |
|
|
| int edge, i, ic, iv, k, *Km, M, m, n, nv, offset, status; |
| double *dcrd, delta, *Psi, psi_m, **tabcoord, upsilon; |
| register int *statp; |
| register const double *wp; |
| register double *xp; |
| struct wcserr **err; |
|
|
| if (tab == 0x0) return TABERR_NULL_POINTER; |
| err = &(tab->err); |
|
|
| |
| if (tab->flag != TABSET) { |
| if ((status = tabset(tab))) return status; |
| } |
|
|
| |
| M = tab->M; |
|
|
| tabcoord = 0x0; |
| nv = 0; |
| if (M > 1) { |
| nv = 1 << M; |
| tabcoord = calloc(nv, sizeof(double *)); |
| } |
|
|
|
|
| status = 0; |
| wp = world; |
| xp = x; |
| statp = stat; |
| for (n = 0; n < ncoord; n++) { |
| |
| edge = 0; |
| for (m = 0; m < M; m++) { |
| tab->p0[m] = 0; |
| } |
|
|
| for (ic = 0; ic < tab->nc; ic++) { |
| if (tab->p0[0] == 0) { |
| |
| if (edge || tabrow(tab, wp)) { |
| |
| ic += tab->K[0]; |
| tab->p0[1]++; |
| edge = tabedge(tab); |
|
|
| |
| ic--; |
| continue; |
| } |
| } |
|
|
| if (M == 1) { |
| |
| if (*wp == tab->coord[0]) { |
| tab->p0[0] = 0; |
| tab->delta[0] = 0.0; |
| break; |
|
|
| } else if (ic < tab->nc - 1) { |
| if (((tab->coord[ic] <= *wp && *wp <= tab->coord[ic+1]) || |
| (tab->coord[ic] >= *wp && *wp >= tab->coord[ic+1])) && |
| (tab->index[0] == 0x0 || |
| tab->index[0][ic] != tab->index[0][ic+1])) { |
| tab->p0[0] = ic; |
| tab->delta[0] = (*wp - tab->coord[ic]) / |
| (tab->coord[ic+1] - tab->coord[ic]); |
| break; |
| } |
| } |
|
|
| } else { |
| |
| if (!edge) { |
| |
| for (iv = 0; iv < nv; iv++) { |
| offset = 0; |
| for (m = M-1; m >= 0; m--) { |
| offset *= tab->K[m]; |
| offset += tab->p0[m]; |
| if ((iv & (1 << m)) && (tab->K[m] > 1)) offset++; |
| } |
| tabcoord[iv] = tab->coord + offset*M; |
| } |
|
|
| if (tabvox(tab, wp, 0, tabcoord, 0x0) == 0) { |
| |
| break; |
| } |
| } |
|
|
| |
| tab->p0[0]++; |
| edge = tabedge(tab); |
| } |
| } |
|
|
|
|
| if (ic == tab->nc) { |
| |
| if (M == 1) { |
| |
| if (tab->extrema[0] <= *wp && *wp <= tab->extrema[1]) { |
| dcrd = tab->coord; |
| for (i = 0; i < 2; i++) { |
| if (i) dcrd += tab->K[0] - 2; |
|
|
| delta = (*wp - *dcrd) / (*(dcrd+1) - *dcrd); |
|
|
| if (i == 0) { |
| if (-0.5 <= delta && delta <= 0.0) { |
| tab->p0[0] = 0; |
| tab->delta[0] = delta; |
| ic = 0; |
| break; |
| } |
| } else { |
| if (1.0 <= delta && delta <= 1.5) { |
| tab->p0[0] = tab->K[0] - 1; |
| tab->delta[0] = delta - 1.0; |
| ic = 0; |
| } |
| } |
| } |
| } |
|
|
| } else { |
| |
| |
| } |
| } |
|
|
|
|
| if (ic == tab->nc) { |
| |
| *statp = 1; |
| status = wcserr_set(TAB_ERRMSG(TABERR_BAD_WORLD)); |
|
|
| } else { |
| |
| Km = tab->K; |
| for (m = 0; m < M; m++, Km++) { |
| |
| upsilon = (tab->p0[m] + 1) + tab->delta[m]; |
|
|
| if (upsilon < 0.5 || upsilon > *Km + 0.5) { |
| |
| *statp = 1; |
| status = wcserr_set(TAB_ERRMSG(TABERR_BAD_WORLD)); |
|
|
| } else { |
| |
| Psi = tab->index[m]; |
| if (Psi == 0x0) { |
| |
| psi_m = upsilon; |
|
|
| } else { |
| |
| |
| Psi--; |
|
|
| if (*Km == 1) { |
| |
| psi_m = Psi[1]; |
| } else { |
| k = (int)(upsilon); |
| psi_m = Psi[k]; |
| if (k < *Km) { |
| psi_m += (upsilon - k) * (Psi[k+1] - Psi[k]); |
| } |
| } |
| } |
|
|
| i = tab->map[m]; |
| xp[i] = psi_m - tab->crval[m]; |
| } |
| } |
| *statp = 0; |
| } |
|
|
| wp += nelem; |
| xp += nelem; |
| statp++; |
| } |
|
|
| if (tabcoord) free(tabcoord); |
|
|
| return status; |
| } |
|
|
| |
| |
| |
|
|
| int tabedge(struct tabprm* tab) |
|
|
| { |
| int edge, *Km, m; |
|
|
| edge = 0; |
| Km = tab->K; |
| for (m = 0; m < tab->M; m++, Km++) { |
| if (tab->p0[m] == *Km) { |
| |
| |
| tab->p0[m] = 0; |
| tab->p0[m+1]++; |
| } else if (tab->p0[m] == *Km - 1 && *Km > 1) { |
| |
| edge = 1; |
| } |
| } |
|
|
| return edge; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| int tabrow(struct tabprm* tab, const double *wp) |
|
|
| { |
| int M, m, offset; |
| unsigned int eq, gt, iv, lt, nv; |
| const double tol = 1e-10; |
| double *cp, w; |
|
|
| M = tab->M; |
|
|
| |
| |
| |
| nv = 1 << M; |
|
|
| eq = 0; |
| lt = 0; |
| gt = 0; |
| for (iv = 0; iv < nv; iv++) { |
| |
| offset = 0; |
| for (m = M-1; m > 0; m--) { |
| offset *= tab->K[m]; |
| offset += tab->p0[m]; |
|
|
| |
| if (iv & (1 << m)) { |
| if (tab->K[m] > 1) offset++; |
| } |
| } |
|
|
| |
| offset *= 2; |
|
|
| |
| if (iv & 1) offset++; |
|
|
| |
| offset *= M; |
|
|
| |
| cp = tab->extrema + offset; |
|
|
| |
| |
| |
| |
| for (m = 0; m < M; m++, cp++) { |
| |
| w = wp[tab->map[m]]; |
|
|
| |
| if (fabs(*cp - w) < tol) { |
| eq |= (1 << m); |
| } else if (*cp < w) { |
| lt |= (1 << m); |
| } else if (*cp > w) { |
| gt |= (1 << m); |
| } |
| } |
|
|
| |
| if ((lt | eq) == nv-1 && (gt | eq) == nv-1) { |
| |
| return 0; |
| } |
| } |
|
|
| |
| return 1; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| int tabvox( |
| struct tabprm* tab, |
| const double *wp, |
| int level, |
| double **tabcoord, |
| unsigned int *vox) |
|
|
| { |
| int i, M, m; |
| unsigned int eq, et, gt, iv, jv, lt, nv, vox2[16]; |
| const double tol = 1e-10; |
| double coord[16], *cp, dv, w, wgt; |
|
|
| M = tab->M; |
|
|
| |
| nv = 1 << M; |
|
|
| dv = 1.0; |
| for (i = 0; i < level; i++) { |
| dv /= 2.0; |
| } |
|
|
| |
| |
| |
| lt = 0; |
| gt = 0; |
| eq = 0; |
| for (iv = 0; iv < nv; iv++) { |
| |
| for (m = 0; m < M; m++) { |
| coord[m] = 0.0; |
| tab->delta[m] = level ? dv*vox[m] : 0.0; |
|
|
| if (iv & (1 << m)) { |
| tab->delta[m] += dv; |
| } |
| } |
|
|
| |
| |
| |
| for (jv = 0; jv < nv; jv++) { |
| |
| wgt = 1.0; |
| for (m = 0; m < M; m++) { |
| if (jv & (1 << m)) { |
| wgt *= tab->delta[m]; |
| } else { |
| wgt *= 1.0 - tab->delta[m]; |
| } |
| } |
|
|
| if (wgt == 0.0) continue; |
|
|
| |
| cp = tabcoord[jv]; |
| for (m = 0; m < M; m++) { |
| coord[m] += *(cp++) * wgt; |
| } |
|
|
| if (wgt == 1.0) break; |
| } |
|
|
| |
| et = 0; |
| for (m = 0; m < M; m++) { |
| |
| w = wp[tab->map[m]]; |
|
|
| |
| if (fabs(coord[m] - w) < tol) { |
| et |= (1 << m); |
| } else if (coord[m] < w) { |
| lt |= (1 << m); |
| } else if (coord[m] > w) { |
| gt |= (1 << m); |
| } |
| } |
|
|
| if (et == nv-1) { |
| |
| return 0; |
| } |
|
|
| eq |= et; |
| } |
|
|
| |
| if ((lt | eq) == nv-1 && (gt | eq) == nv-1) { |
| |
|
|
| |
| if (level == 31) { |
| |
| dv /= 2.0; |
| for (m = 0; m < M; m++) { |
| tab->delta[m] = dv * (2.0*vox[m] + 1.0); |
| } |
|
|
| return 0; |
| } |
|
|
| |
| for (iv = 0; iv < nv; iv++) { |
| |
| for (m = 0; m < M; m++) { |
| vox2[m] = level ? 2*vox[m] : 0; |
| if (iv & (1 << m)) { |
| vox2[m]++; |
| } |
| } |
|
|
| |
| if (tabvox(tab, wp, level+1, tabcoord, vox2) == 0) { |
| return 0; |
| } |
| } |
| } |
|
|
| |
| return 1; |
| } |
|
|