[ff6cfe1] | 1 | /* netbits.c |
---|
[d1b1380] | 2 | * Miscellaneous primitive network routines for Survex |
---|
[fdffa7d] | 3 | * Copyright (C) 1992-2024 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
[846746e] | 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
[ecbc6c18] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
[4c83f84] | 20 | #include <config.h> |
---|
[d1b1380] | 21 | |
---|
[f98f8a5] | 22 | #if 0 |
---|
[b84a343] | 23 | # define DEBUG_INVALID 1 |
---|
[f98f8a5] | 24 | #endif |
---|
| 25 | |
---|
[7a05e07] | 26 | #include "debug.h" |
---|
[a420b49] | 27 | #include "cavern.h" |
---|
[7a05e07] | 28 | #include "filename.h" |
---|
| 29 | #include "message.h" |
---|
[d1b1380] | 30 | #include "netbits.h" |
---|
[a420b49] | 31 | #include "datain.h" /* for compile_error */ |
---|
[a838042] | 32 | #include "validate.h" /* for compile_error */ |
---|
[20e064b] | 33 | #include <math.h> |
---|
[d1b1380] | 34 | |
---|
[dd368da] | 35 | #define THRESHOLD (REAL_EPSILON * 1000) /* 100 was too small */ |
---|
[daf88e1] | 36 | |
---|
[564f471] | 37 | node *stn_iter = NULL; /* for FOR_EACH_STN */ |
---|
| 38 | |
---|
[dda0ca7] | 39 | static struct { |
---|
| 40 | prefix * to_name; |
---|
| 41 | prefix * fr_name; |
---|
| 42 | linkfor * leg; |
---|
| 43 | int n; |
---|
| 44 | } last_leg = { NULL, NULL, NULL, 0 }; |
---|
| 45 | |
---|
| 46 | void clear_last_leg(void) { |
---|
| 47 | last_leg.to_name = NULL; |
---|
| 48 | } |
---|
| 49 | |
---|
[78d2394] | 50 | static char freeleg(node **stnptr); |
---|
| 51 | |
---|
[f98f8a5] | 52 | #ifdef NO_COVARIANCES |
---|
[298e2f3] | 53 | static void check_var(const var *v) { |
---|
[647407d] | 54 | int bad = 0; |
---|
| 55 | int i; |
---|
| 56 | |
---|
| 57 | for (i = 0; i < 3; i++) { |
---|
[20e064b] | 58 | if (isnan(v[i]) |
---|
[dac18d8] | 59 | printf("*** NaN!!!\n"), bad = 1; |
---|
[647407d] | 60 | } |
---|
| 61 | if (bad) print_var(v); |
---|
| 62 | return; |
---|
[f98f8a5] | 63 | } |
---|
| 64 | #else |
---|
[a862ee8] | 65 | #define V(A,B) ((*v)[A][B]) |
---|
[298e2f3] | 66 | static void check_var(const var *v) { |
---|
[a862ee8] | 67 | int bad = 0; |
---|
| 68 | int ok = 0; |
---|
| 69 | int i, j; |
---|
[407084d] | 70 | #if DEBUG_INVALID |
---|
[a862ee8] | 71 | real det = 0.0; |
---|
[407084d] | 72 | #endif |
---|
[a862ee8] | 73 | |
---|
| 74 | for (i = 0; i < 3; i++) { |
---|
| 75 | for (j = 0; j < 3; j++) { |
---|
[20e064b] | 76 | if (isnan(V(i, j))) |
---|
[dac18d8] | 77 | printf("*** NaN!!!\n"), bad = 1, ok = 1; |
---|
[a5da11c] | 78 | if (V(i, j) != 0.0) ok = 1; |
---|
[a862ee8] | 79 | } |
---|
| 80 | } |
---|
| 81 | if (!ok) return; /* ignore all-zero matrices */ |
---|
| 82 | |
---|
[40fb2e4] | 83 | #if DEBUG_INVALID |
---|
[a862ee8] | 84 | for (i = 0; i < 3; i++) { |
---|
[a5da11c] | 85 | det += V(i, 0) * (V((i + 1) % 3, 1) * V((i + 2) % 3, 2) - |
---|
| 86 | V((i + 1) % 3, 2) * V((i + 2) % 3, 1)); |
---|
[a862ee8] | 87 | } |
---|
| 88 | |
---|
[daf88e1] | 89 | if (fabs(det) < THRESHOLD) |
---|
[a862ee8] | 90 | printf("*** Singular!!!\n"), bad = 1; |
---|
[40fb2e4] | 91 | #endif |
---|
[a862ee8] | 92 | |
---|
| 93 | #if 0 |
---|
[840e496] | 94 | /* don't check this - it isn't always the case! */ |
---|
[daf88e1] | 95 | if (fabs(V(0,1) - V(1,0)) > THRESHOLD || |
---|
| 96 | fabs(V(0,2) - V(2,0)) > THRESHOLD || |
---|
| 97 | fabs(V(1,2) - V(2,1)) > THRESHOLD) |
---|
[a862ee8] | 98 | printf("*** Not symmetric!!!\n"), bad = 1; |
---|
| 99 | if (V(0,0) <= 0.0 || V(1,1) <= 0.0 || V(2,2) <= 0.0) |
---|
| 100 | printf("*** Not positive definite (diag <= 0)!!!\n"), bad = 1; |
---|
| 101 | if (sqrd(V(0,1)) >= V(0,0)*V(1,1) || sqrd(V(0,2)) >= V(0,0)*V(2,2) || |
---|
| 102 | sqrd(V(1,0)) >= V(0,0)*V(1,1) || sqrd(V(2,0)) >= V(0,0)*V(2,2) || |
---|
| 103 | sqrd(V(1,2)) >= V(2,2)*V(1,1) || sqrd(V(2,1)) >= V(2,2)*V(1,1)) |
---|
| 104 | printf("*** Not positive definite (off diag^2 >= diag product)!!!\n"), bad = 1; |
---|
| 105 | #endif |
---|
| 106 | if (bad) print_var(*v); |
---|
| 107 | } |
---|
[59f2dbb] | 108 | |
---|
| 109 | #define SN(V,A,B) ((*(V))[(A)==(B)?(A):2+(A)+(B)]) |
---|
| 110 | #define S(A,B) SN(v,A,B) |
---|
| 111 | |
---|
[298e2f3] | 112 | static void check_svar(const svar *v) { |
---|
[59f2dbb] | 113 | int bad = 0; |
---|
| 114 | int ok = 0; |
---|
[dac18d8] | 115 | int i; |
---|
[59f2dbb] | 116 | #if DEBUG_INVALID |
---|
| 117 | real det = 0.0; |
---|
| 118 | #endif |
---|
| 119 | |
---|
[dac18d8] | 120 | for (i = 0; i < 6; i++) { |
---|
[20e064b] | 121 | if (isnan((*v)[i])) |
---|
[dac18d8] | 122 | printf("*** NaN!!!\n"), bad = 1, ok = 1; |
---|
| 123 | if ((*v)[i] != 0.0) ok = 1; |
---|
[59f2dbb] | 124 | } |
---|
| 125 | if (!ok) return; /* ignore all-zero matrices */ |
---|
| 126 | |
---|
| 127 | #if DEBUG_INVALID |
---|
| 128 | for (i = 0; i < 3; i++) { |
---|
| 129 | det += S(i, 0) * (S((i + 1) % 3, 1) * S((i + 2) % 3, 2) - |
---|
| 130 | S((i + 1) % 3, 2) * S((i + 2) % 3, 1)); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | if (fabs(det) < THRESHOLD) |
---|
| 134 | printf("*** Singular!!!\n"), bad = 1; |
---|
| 135 | #endif |
---|
| 136 | |
---|
[840e496] | 137 | #if 0 |
---|
| 138 | /* don't check this - it isn't always the case! */ |
---|
[dac18d8] | 139 | if ((*v)[0] <= 0.0 || (*v)[1] <= 0.0 || (*v)[2] <= 0.0) |
---|
[59f2dbb] | 140 | printf("*** Not positive definite (diag <= 0)!!!\n"), bad = 1; |
---|
[dac18d8] | 141 | if (sqrd((*v)[3]) >= (*v)[0]*(*v)[1] || |
---|
| 142 | sqrd((*v)[4]) >= (*v)[0]*(*v)[2] || |
---|
| 143 | sqrd((*v)[5]) >= (*v)[1]*(*v)[2]) |
---|
[59f2dbb] | 144 | printf("*** Not positive definite (off diag^2 >= diag product)!!!\n"), bad = 1; |
---|
[840e496] | 145 | #endif |
---|
[59f2dbb] | 146 | if (bad) print_svar(*v); |
---|
| 147 | } |
---|
[f98f8a5] | 148 | #endif |
---|
[a862ee8] | 149 | |
---|
[298e2f3] | 150 | static void check_d(const delta *d) { |
---|
[5d26a94] | 151 | int bad = 0; |
---|
| 152 | int i; |
---|
| 153 | |
---|
| 154 | for (i = 0; i < 3; i++) { |
---|
[20e064b] | 155 | if (isnan((*d)[i])) |
---|
[dac18d8] | 156 | printf("*** NaN!!!\n"), bad = 1; |
---|
[5d26a94] | 157 | } |
---|
| 158 | |
---|
| 159 | if (bad) printf("(%4.2f,%4.2f,%4.2f)\n", (*d)[0], (*d)[1], (*d)[2]); |
---|
| 160 | } |
---|
| 161 | |
---|
[564f471] | 162 | /* insert at head of double-linked list */ |
---|
| 163 | void |
---|
| 164 | add_stn_to_list(node **list, node *stn) { |
---|
[4c07c51] | 165 | SVX_ASSERT(list); |
---|
| 166 | SVX_ASSERT(stn); |
---|
| 167 | SVX_ASSERT(stn_iter != stn); /* if it does, we're still on a list... */ |
---|
[85fe934] | 168 | #if 0 |
---|
| 169 | printf("add_stn_to_list(%p, [%p] ", list, stn); |
---|
| 170 | if (stn->name) print_prefix(stn->name); |
---|
| 171 | printf(")\n"); |
---|
| 172 | #endif |
---|
[564f471] | 173 | stn->next = *list; |
---|
| 174 | stn->prev = NULL; |
---|
| 175 | if (*list) (*list)->prev = stn; |
---|
| 176 | *list = stn; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /* remove from double-linked list */ |
---|
| 180 | void |
---|
| 181 | remove_stn_from_list(node **list, node *stn) { |
---|
[4c07c51] | 182 | SVX_ASSERT(list); |
---|
| 183 | SVX_ASSERT(stn); |
---|
[85fe934] | 184 | #if 0 |
---|
| 185 | printf("remove_stn_from_list(%p, [%p] ", list, stn); |
---|
| 186 | if (stn->name) print_prefix(stn->name); |
---|
| 187 | printf(")\n"); |
---|
| 188 | #endif |
---|
[b84a343] | 189 | #if DEBUG_INVALID |
---|
[85fe934] | 190 | { |
---|
| 191 | /* check station is actually in this list */ |
---|
| 192 | node *stn_to_remove_is_in_list = *list; |
---|
| 193 | validate(); |
---|
| 194 | while (stn_to_remove_is_in_list != stn) { |
---|
[4c07c51] | 195 | SVX_ASSERT(stn_to_remove_is_in_list); |
---|
[85fe934] | 196 | stn_to_remove_is_in_list = stn_to_remove_is_in_list->next; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | #endif |
---|
[564f471] | 200 | /* adjust the iterator if it points to the element we're deleting */ |
---|
| 201 | if (stn_iter == stn) stn_iter = stn_iter->next; |
---|
| 202 | /* need a special case if we're removing the list head */ |
---|
| 203 | if (stn->prev == NULL) { |
---|
| 204 | *list = stn->next; |
---|
| 205 | if (*list) (*list)->prev = NULL; |
---|
| 206 | } else { |
---|
| 207 | stn->prev->next = stn->next; |
---|
[28ddfe1] | 208 | if (stn->next) stn->next->prev = stn->prev; |
---|
[564f471] | 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
[d1b1380] | 212 | /* Create (uses osmalloc) a forward leg containing the data in leg, or |
---|
| 213 | * the reversed data in the reverse of leg, if leg doesn't hold data |
---|
| 214 | */ |
---|
[2140502] | 215 | linkfor * |
---|
[a420b49] | 216 | copy_link(linkfor *leg) |
---|
| 217 | { |
---|
[d1b1380] | 218 | linkfor *legOut; |
---|
| 219 | int d; |
---|
[a420b49] | 220 | legOut = osnew(linkfor); |
---|
[d1b1380] | 221 | if (data_here(leg)) { |
---|
[a420b49] | 222 | for (d = 2; d >= 0; d--) legOut->d[d] = leg->d[d]; |
---|
[d1b1380] | 223 | } else { |
---|
[a420b49] | 224 | leg = reverse_leg(leg); |
---|
[4c07c51] | 225 | SVX_ASSERT(data_here(leg)); |
---|
[a420b49] | 226 | for (d = 2; d >= 0; d--) legOut->d[d] = -leg->d[d]; |
---|
[d1b1380] | 227 | } |
---|
| 228 | #if 1 |
---|
[7a05e07] | 229 | # ifndef NO_COVARIANCES |
---|
[59f2dbb] | 230 | check_svar(&(leg->v)); |
---|
[cb3d1e2] | 231 | { |
---|
[59f2dbb] | 232 | int i; |
---|
| 233 | for (i = 0; i < 6; i++) legOut->v[i] = leg->v[i]; |
---|
[d1b1380] | 234 | } |
---|
[7a05e07] | 235 | # else |
---|
[a420b49] | 236 | for (d = 2; d >= 0; d--) legOut->v[d] = leg->v[d]; |
---|
[7a05e07] | 237 | # endif |
---|
[d1b1380] | 238 | #else |
---|
[59f2dbb] | 239 | memcpy(legOut->v, leg->v, sizeof(svar)); |
---|
[d1b1380] | 240 | #endif |
---|
[b5a3219] | 241 | legOut->meta = pcs->meta; |
---|
| 242 | if (pcs->meta) ++pcs->meta->ref_count; |
---|
[d1b1380] | 243 | return legOut; |
---|
| 244 | } |
---|
| 245 | |
---|
[0804fbe] | 246 | /* Adds to the forward leg “leg”, the data in leg2, or the reversed data |
---|
[d1b1380] | 247 | * in the reverse of leg2, if leg2 doesn't hold data |
---|
| 248 | */ |
---|
[2140502] | 249 | linkfor * |
---|
[a5da11c] | 250 | addto_link(linkfor *leg, const linkfor *leg2) |
---|
[a420b49] | 251 | { |
---|
[d1b1380] | 252 | if (data_here(leg2)) { |
---|
[407084d] | 253 | adddd(&leg->d, &leg->d, &((linkfor *)leg2)->d); |
---|
[d1b1380] | 254 | } else { |
---|
[a420b49] | 255 | leg2 = reverse_leg(leg2); |
---|
[4c07c51] | 256 | SVX_ASSERT(data_here(leg2)); |
---|
[407084d] | 257 | subdd(&leg->d, &leg->d, &((linkfor *)leg2)->d); |
---|
[d1b1380] | 258 | } |
---|
[59f2dbb] | 259 | addss(&leg->v, &leg->v, &((linkfor *)leg2)->v); |
---|
[d1b1380] | 260 | return leg; |
---|
| 261 | } |
---|
| 262 | |
---|
[dda0ca7] | 263 | static linkfor * |
---|
[693388e] | 264 | addleg_(node *fr, node *to, |
---|
| 265 | real dx, real dy, real dz, |
---|
| 266 | real vx, real vy, real vz, |
---|
| 267 | #ifndef NO_COVARIANCES |
---|
| 268 | real cyz, real czx, real cxy, |
---|
| 269 | #endif |
---|
| 270 | int leg_flags) |
---|
| 271 | { |
---|
| 272 | int i, j; |
---|
| 273 | linkfor *leg, *leg2; |
---|
| 274 | /* we have been asked to add a leg with the same node at both ends |
---|
| 275 | * - this should be trapped by the caller */ |
---|
[4c07c51] | 276 | SVX_ASSERT(fr->name != to->name); |
---|
[693388e] | 277 | |
---|
| 278 | leg = osnew(linkfor); |
---|
| 279 | leg2 = (linkfor*)osnew(linkrev); |
---|
| 280 | |
---|
| 281 | i = freeleg(&fr); |
---|
| 282 | j = freeleg(&to); |
---|
| 283 | |
---|
| 284 | leg->l.to = to; |
---|
| 285 | leg2->l.to = fr; |
---|
| 286 | leg->d[0] = dx; |
---|
| 287 | leg->d[1] = dy; |
---|
| 288 | leg->d[2] = dz; |
---|
| 289 | #ifndef NO_COVARIANCES |
---|
| 290 | leg->v[0] = vx; |
---|
| 291 | leg->v[1] = vy; |
---|
| 292 | leg->v[2] = vz; |
---|
| 293 | leg->v[3] = cxy; |
---|
| 294 | leg->v[4] = czx; |
---|
| 295 | leg->v[5] = cyz; |
---|
| 296 | check_svar(&(leg->v)); |
---|
| 297 | #else |
---|
| 298 | leg->v[0] = vx; |
---|
| 299 | leg->v[1] = vy; |
---|
| 300 | leg->v[2] = vz; |
---|
| 301 | #endif |
---|
| 302 | leg2->l.reverse = i; |
---|
| 303 | leg->l.reverse = j | FLAG_DATAHERE | leg_flags; |
---|
| 304 | |
---|
[fdffa7d] | 305 | leg->l.flags = pcs->flags | (pcs->recorded_style << FLAGS_STYLE_BIT0); |
---|
[b5a3219] | 306 | leg->meta = pcs->meta; |
---|
| 307 | if (pcs->meta) ++pcs->meta->ref_count; |
---|
[693388e] | 308 | |
---|
| 309 | fr->leg[i] = leg; |
---|
| 310 | to->leg[j] = leg2; |
---|
| 311 | |
---|
[f15cde77] | 312 | ++fr->name->shape; |
---|
| 313 | ++to->name->shape; |
---|
[dda0ca7] | 314 | |
---|
| 315 | return leg; |
---|
[693388e] | 316 | } |
---|
| 317 | |
---|
[be374fc] | 318 | /* Add a leg between names *fr_name and *to_name |
---|
| 319 | * If either is a three node, then it is split into two |
---|
| 320 | * and the data structure adjusted as necessary. |
---|
| 321 | */ |
---|
[2140502] | 322 | void |
---|
| 323 | addlegbyname(prefix *fr_name, prefix *to_name, bool fToFirst, |
---|
| 324 | real dx, real dy, real dz, |
---|
| 325 | real vx, real vy, real vz |
---|
| 326 | #ifndef NO_COVARIANCES |
---|
| 327 | , real cyz, real czx, real cxy |
---|
| 328 | #endif |
---|
| 329 | ) |
---|
| 330 | { |
---|
| 331 | node *to, *fr; |
---|
| 332 | if (to_name == fr_name) { |
---|
[52f46ed] | 333 | int type = pcs->from_equals_to_is_only_a_warning ? DIAG_WARN : DIAG_ERR; |
---|
[736f7df] | 334 | /* TRANSLATORS: Here a "survey leg" is a set of measurements between two |
---|
| 335 | * "survey stations". |
---|
[b49ac56] | 336 | * |
---|
[736f7df] | 337 | * %s is replaced by the name of the station. */ |
---|
[52f46ed] | 338 | compile_diagnostic(type, /*Survey leg with same station (“%s”) at both ends - typing error?*/50, |
---|
[cab0f26] | 339 | sprint_prefix(to_name)); |
---|
[2140502] | 340 | return; |
---|
[421b7d2] | 341 | } |
---|
[2140502] | 342 | if (fToFirst) { |
---|
| 343 | to = StnFromPfx(to_name); |
---|
| 344 | fr = StnFromPfx(fr_name); |
---|
| 345 | } else { |
---|
| 346 | fr = StnFromPfx(fr_name); |
---|
| 347 | to = StnFromPfx(to_name); |
---|
| 348 | } |
---|
[dda0ca7] | 349 | if (last_leg.to_name) { |
---|
| 350 | if (last_leg.to_name == to_name && last_leg.fr_name == fr_name) { |
---|
| 351 | /* FIXME: Not the right way to average... */ |
---|
| 352 | linkfor * leg = last_leg.leg; |
---|
| 353 | int n = last_leg.n++; |
---|
| 354 | leg->d[0] = (leg->d[0] * n + dx) / (n + 1); |
---|
| 355 | leg->d[1] = (leg->d[1] * n + dy) / (n + 1); |
---|
| 356 | leg->d[2] = (leg->d[2] * n + dz) / (n + 1); |
---|
| 357 | #ifndef NO_COVARIANCES |
---|
| 358 | leg->v[0] = (leg->v[0] * n + vx) / (n + 1); |
---|
| 359 | leg->v[1] = (leg->v[1] * n + vy) / (n + 1); |
---|
| 360 | leg->v[2] = (leg->v[2] * n + vz) / (n + 1); |
---|
| 361 | leg->v[3] = (leg->v[3] * n + cxy) / (n + 1); |
---|
| 362 | leg->v[4] = (leg->v[4] * n + czx) / (n + 1); |
---|
| 363 | leg->v[5] = (leg->v[5] * n + cyz) / (n + 1); |
---|
| 364 | check_svar(&(leg->v)); |
---|
| 365 | #else |
---|
| 366 | leg->v[0] = (leg->v[0] * n + vx) / (n + 1); |
---|
| 367 | leg->v[1] = (leg->v[1] * n + vy) / (n + 1); |
---|
| 368 | leg->v[2] = (leg->v[2] * n + vz) / (n + 1); |
---|
| 369 | #endif |
---|
| 370 | return; |
---|
| 371 | } |
---|
| 372 | } |
---|
| 373 | cLegs++; |
---|
| 374 | |
---|
| 375 | last_leg.to_name = to_name; |
---|
| 376 | last_leg.fr_name = fr_name; |
---|
| 377 | last_leg.n = 1; |
---|
| 378 | last_leg.leg = addleg_(fr, to, dx, dy, dz, vx, vy, vz, |
---|
[2140502] | 379 | #ifndef NO_COVARIANCES |
---|
[dda0ca7] | 380 | cyz, czx, cxy, |
---|
[2140502] | 381 | #endif |
---|
[dda0ca7] | 382 | 0); |
---|
[2140502] | 383 | } |
---|
| 384 | |
---|
[be374fc] | 385 | /* helper function for replace_pfx */ |
---|
| 386 | static void |
---|
[aa198cd2] | 387 | replace_pfx_(node *stn, node *from, pos *pos_with) |
---|
[be374fc] | 388 | { |
---|
| 389 | int d; |
---|
| 390 | stn->name->pos = pos_with; |
---|
| 391 | for (d = 0; d < 3; d++) { |
---|
| 392 | linkfor *leg = stn->leg[d]; |
---|
| 393 | node *to; |
---|
| 394 | if (!leg) break; |
---|
| 395 | to = leg->l.to; |
---|
| 396 | if (to == from) continue; |
---|
| 397 | |
---|
| 398 | if (fZeros(data_here(leg) ? &leg->v : &reverse_leg(leg)->v)) |
---|
[aa198cd2] | 399 | replace_pfx_(to, stn, pos_with); |
---|
[be374fc] | 400 | } |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | /* We used to iterate over the whole station list (inefficient) - now we |
---|
| 404 | * just look at any neighbouring nodes to see if they are equated */ |
---|
| 405 | static void |
---|
| 406 | replace_pfx(const prefix *pfx_replace, const prefix *pfx_with) |
---|
| 407 | { |
---|
| 408 | pos *pos_replace; |
---|
[4c07c51] | 409 | SVX_ASSERT(pfx_replace); |
---|
| 410 | SVX_ASSERT(pfx_with); |
---|
[be374fc] | 411 | pos_replace = pfx_replace->pos; |
---|
[4c07c51] | 412 | SVX_ASSERT(pos_replace != pfx_with->pos); |
---|
[be374fc] | 413 | |
---|
[aa198cd2] | 414 | replace_pfx_(pfx_replace->stn, NULL, pfx_with->pos); |
---|
[be374fc] | 415 | |
---|
| 416 | #if DEBUG_INVALID |
---|
| 417 | { |
---|
| 418 | node *stn; |
---|
| 419 | FOR_EACH_STN(stn, stnlist) { |
---|
[4c07c51] | 420 | SVX_ASSERT(stn->name->pos != pos_replace); |
---|
[be374fc] | 421 | } |
---|
| 422 | } |
---|
| 423 | #endif |
---|
| 424 | |
---|
| 425 | /* free the (now-unused) old pos */ |
---|
| 426 | osfree(pos_replace); |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | /* Add an equating leg between existing stations *fr and *to (whose names are |
---|
| 430 | * name1 and name2). |
---|
[d1b1380] | 431 | */ |
---|
[a420b49] | 432 | void |
---|
[be374fc] | 433 | process_equate(prefix *name1, prefix *name2) |
---|
[a420b49] | 434 | { |
---|
[925fa451] | 435 | node *stn1, *stn2; |
---|
[dda0ca7] | 436 | clear_last_leg(); |
---|
[be374fc] | 437 | if (name1 == name2) { |
---|
| 438 | /* catch something like *equate "fred fred" */ |
---|
[736f7df] | 439 | /* TRANSLATORS: Here "station" is a survey station, not a train station. |
---|
| 440 | */ |
---|
[cab0f26] | 441 | compile_diagnostic(DIAG_WARN, /*Station “%s” equated to itself*/13, |
---|
| 442 | sprint_prefix(name1)); |
---|
[be374fc] | 443 | return; |
---|
| 444 | } |
---|
[925fa451] | 445 | stn1 = StnFromPfx(name1); |
---|
| 446 | stn2 = StnFromPfx(name2); |
---|
[be374fc] | 447 | /* equate nodes if not already equated */ |
---|
[925fa451] | 448 | if (name1->pos != name2->pos) { |
---|
[be374fc] | 449 | if (pfx_fixed(name1)) { |
---|
| 450 | if (pfx_fixed(name2)) { |
---|
| 451 | /* both are fixed, but let them off iff their coordinates match */ |
---|
| 452 | char *s = osstrdup(sprint_prefix(name1)); |
---|
| 453 | int d; |
---|
| 454 | for (d = 2; d >= 0; d--) { |
---|
| 455 | if (name1->pos->p[d] != name2->pos->p[d]) { |
---|
[cab0f26] | 456 | compile_diagnostic(DIAG_ERR, /*Tried to equate two non-equal fixed stations: “%s” and “%s”*/52, |
---|
| 457 | s, sprint_prefix(name2)); |
---|
[be374fc] | 458 | osfree(s); |
---|
| 459 | return; |
---|
| 460 | } |
---|
| 461 | } |
---|
[736f7df] | 462 | /* TRANSLATORS: "equal" as in: |
---|
| 463 | * |
---|
| 464 | * *fix a 1 2 3 |
---|
| 465 | * *fix b 1 2 3 |
---|
| 466 | * *equate a b */ |
---|
[cab0f26] | 467 | compile_diagnostic(DIAG_WARN, /*Equating two equal fixed points: “%s” and “%s”*/53, |
---|
| 468 | s, sprint_prefix(name2)); |
---|
[be374fc] | 469 | osfree(s); |
---|
| 470 | } |
---|
[925fa451] | 471 | |
---|
[be374fc] | 472 | /* name1 is fixed, so replace all refs to name2's pos with name1's */ |
---|
| 473 | replace_pfx(name2, name1); |
---|
| 474 | } else { |
---|
| 475 | /* name1 isn't fixed, so replace all refs to its pos with name2's */ |
---|
| 476 | replace_pfx(name1, name2); |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | /* count equates as legs for now... */ |
---|
| 480 | cLegs++; |
---|
| 481 | addleg_(stn1, stn2, |
---|
| 482 | (real)0.0, (real)0.0, (real)0.0, |
---|
| 483 | (real)0.0, (real)0.0, (real)0.0, |
---|
[7a05e07] | 484 | #ifndef NO_COVARIANCES |
---|
[be374fc] | 485 | (real)0.0, (real)0.0, (real)0.0, |
---|
[7a05e07] | 486 | #endif |
---|
[be374fc] | 487 | FLAG_FAKE); |
---|
| 488 | } |
---|
[d1b1380] | 489 | } |
---|
| 490 | |
---|
| 491 | /* Add a 'fake' leg (not counted) between existing stations *fr and *to |
---|
[2140502] | 492 | * (which *must* be different) |
---|
[d1b1380] | 493 | * If either node is a three node, then it is split into two |
---|
| 494 | * and the data structure adjusted as necessary |
---|
| 495 | */ |
---|
[a420b49] | 496 | void |
---|
| 497 | addfakeleg(node *fr, node *to, |
---|
| 498 | real dx, real dy, real dz, |
---|
| 499 | real vx, real vy, real vz |
---|
[7a05e07] | 500 | #ifndef NO_COVARIANCES |
---|
[a420b49] | 501 | , real cyz, real czx, real cxy |
---|
[7a05e07] | 502 | #endif |
---|
[a420b49] | 503 | ) |
---|
| 504 | { |
---|
[dda0ca7] | 505 | clear_last_leg(); |
---|
[693388e] | 506 | addleg_(fr, to, dx, dy, dz, vx, vy, vz, |
---|
[8cf2e04] | 507 | #ifndef NO_COVARIANCES |
---|
[693388e] | 508 | cyz, czx, cxy, |
---|
[8cf2e04] | 509 | #endif |
---|
[693388e] | 510 | FLAG_FAKE); |
---|
[d1b1380] | 511 | } |
---|
| 512 | |
---|
[78d2394] | 513 | static char |
---|
[a420b49] | 514 | freeleg(node **stnptr) |
---|
| 515 | { |
---|
[d1b1380] | 516 | node *stn, *oldstn; |
---|
| 517 | linkfor *leg, *leg2; |
---|
[94f6ef3] | 518 | #ifndef NO_COVARIANCES |
---|
[59f2dbb] | 519 | int i; |
---|
[94f6ef3] | 520 | #endif |
---|
[7a05e07] | 521 | |
---|
[a420b49] | 522 | stn = *stnptr; |
---|
[7a05e07] | 523 | |
---|
[a420b49] | 524 | if (stn->leg[0] == NULL) return 0; /* leg[0] unused */ |
---|
| 525 | if (stn->leg[1] == NULL) return 1; /* leg[1] unused */ |
---|
| 526 | if (stn->leg[2] == NULL) return 2; /* leg[2] unused */ |
---|
[d1b1380] | 527 | |
---|
| 528 | /* All legs used, so split node in two */ |
---|
[a420b49] | 529 | oldstn = stn; |
---|
| 530 | stn = osnew(node); |
---|
| 531 | leg = osnew(linkfor); |
---|
| 532 | leg2 = (linkfor*)osnew(linkrev); |
---|
[d1b1380] | 533 | |
---|
[a420b49] | 534 | *stnptr = stn; |
---|
[d1b1380] | 535 | |
---|
[564f471] | 536 | add_stn_to_list(&stnlist, stn); |
---|
[a420b49] | 537 | stn->name = oldstn->name; |
---|
[d1b1380] | 538 | |
---|
[a420b49] | 539 | leg->l.to = stn; |
---|
| 540 | leg->d[0] = leg->d[1] = leg->d[2] = (real)0.0; |
---|
[7a05e07] | 541 | |
---|
[8cf2e04] | 542 | #ifndef NO_COVARIANCES |
---|
[59f2dbb] | 543 | for (i = 0; i < 6; i++) leg->v[i] = (real)0.0; |
---|
[8cf2e04] | 544 | #else |
---|
[a420b49] | 545 | leg->v[0] = leg->v[1] = leg->v[2] = (real)0.0; |
---|
[8cf2e04] | 546 | #endif |
---|
[693388e] | 547 | leg->l.reverse = 1 | FLAG_DATAHERE | FLAG_FAKE; |
---|
[fdffa7d] | 548 | leg->l.flags = pcs->flags | (pcs->recorded_style << FLAGS_STYLE_BIT0); |
---|
[d1b1380] | 549 | |
---|
[a420b49] | 550 | leg2->l.to = oldstn; |
---|
| 551 | leg2->l.reverse = 0; |
---|
[d1b1380] | 552 | |
---|
[eb04711] | 553 | /* NB this preserves pos->stn->leg[0] to point to the "real" fixed point |
---|
| 554 | * for stations fixed with error estimates |
---|
| 555 | */ |
---|
[a420b49] | 556 | stn->leg[0] = oldstn->leg[0]; |
---|
[d1b1380] | 557 | /* correct reverse leg */ |
---|
[a420b49] | 558 | reverse_leg(stn->leg[0])->l.to = stn; |
---|
| 559 | stn->leg[1] = leg2; |
---|
[d1b1380] | 560 | |
---|
[a420b49] | 561 | oldstn->leg[0] = leg; |
---|
[d1b1380] | 562 | |
---|
[a420b49] | 563 | stn->leg[2] = NULL; /* needed as stn->leg[dirn]==NULL indicates unused */ |
---|
[d1b1380] | 564 | |
---|
| 565 | return(2); /* leg[2] unused */ |
---|
| 566 | } |
---|
| 567 | |
---|
[a420b49] | 568 | node * |
---|
| 569 | StnFromPfx(prefix *name) |
---|
| 570 | { |
---|
[d1b1380] | 571 | node *stn; |
---|
[a420b49] | 572 | if (name->stn != NULL) return (name->stn); |
---|
| 573 | stn = osnew(node); |
---|
| 574 | stn->name = name; |
---|
| 575 | if (name->pos == NULL) { |
---|
| 576 | name->pos = osnew(pos); |
---|
[d1b1380] | 577 | unfix(stn); |
---|
| 578 | } |
---|
[a420b49] | 579 | stn->leg[0] = stn->leg[1] = stn->leg[2] = NULL; |
---|
[564f471] | 580 | add_stn_to_list(&stnlist, stn); |
---|
[a420b49] | 581 | name->stn = stn; |
---|
[d1b1380] | 582 | cStns++; |
---|
| 583 | return stn; |
---|
| 584 | } |
---|
| 585 | |
---|
[a420b49] | 586 | extern void |
---|
[a862ee8] | 587 | fprint_prefix(FILE *fh, const prefix *ptr) |
---|
[a420b49] | 588 | { |
---|
[4c07c51] | 589 | SVX_ASSERT(ptr); |
---|
[a2c33ae] | 590 | if (TSTBIT(ptr->sflags, SFLAGS_ANON)) { |
---|
| 591 | /* We release the stations, so ptr->stn is NULL late on, so we can't |
---|
| 592 | * use that to print "anonymous station surveyed from somesurvey.12" |
---|
| 593 | * here. FIXME */ |
---|
| 594 | fputs("anonymous station", fh); |
---|
[55a0527] | 595 | /* FIXME: if ident is set, show it? */ |
---|
[a2c33ae] | 596 | return; |
---|
| 597 | } |
---|
[a420b49] | 598 | if (ptr->up != NULL) { |
---|
| 599 | fprint_prefix(fh, ptr->up); |
---|
[5d59477] | 600 | if (ptr->up->up != NULL) fputc(output_separator, fh); |
---|
[ba84079] | 601 | SVX_ASSERT(prefix_ident(ptr)); |
---|
| 602 | fputs(prefix_ident(ptr), fh); |
---|
[d1b1380] | 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
[ee2e333] | 606 | static char *buffer = NULL; |
---|
[95d4862] | 607 | static OSSIZE_T buffer_len = 256; |
---|
[ee2e333] | 608 | |
---|
[95d4862] | 609 | static OSSIZE_T |
---|
[ee2e333] | 610 | sprint_prefix_(const prefix *ptr) |
---|
[a420b49] | 611 | { |
---|
[95d4862] | 612 | OSSIZE_T len = 1; |
---|
[8cf2e04] | 613 | if (ptr->up != NULL) { |
---|
[ba84079] | 614 | const char *ident = prefix_ident(ptr); |
---|
| 615 | SVX_ASSERT(ident); |
---|
[5d59477] | 616 | len = sprint_prefix_(ptr->up); |
---|
[fcf1954] | 617 | OSSIZE_T end = len - 1; |
---|
[ee2e333] | 618 | if (ptr->up->up != NULL) len++; |
---|
[ba84079] | 619 | len += strlen(ident); |
---|
[ee2e333] | 620 | if (len > buffer_len) { |
---|
| 621 | buffer = osrealloc(buffer, len); |
---|
| 622 | buffer_len = len; |
---|
[a420b49] | 623 | } |
---|
[fcf1954] | 624 | char *p = buffer + end; |
---|
[5d59477] | 625 | if (ptr->up->up != NULL) *p++ = output_separator; |
---|
[ba84079] | 626 | strcpy(p, ident); |
---|
[d1b1380] | 627 | } |
---|
[ee2e333] | 628 | return len; |
---|
[d1b1380] | 629 | } |
---|
| 630 | |
---|
[a420b49] | 631 | extern char * |
---|
[a862ee8] | 632 | sprint_prefix(const prefix *ptr) |
---|
[a420b49] | 633 | { |
---|
[4c07c51] | 634 | SVX_ASSERT(ptr); |
---|
[ee2e333] | 635 | if (!buffer) buffer = osmalloc(buffer_len); |
---|
[a2c33ae] | 636 | if (TSTBIT(ptr->sflags, SFLAGS_ANON)) { |
---|
| 637 | /* We release the stations, so ptr->stn is NULL late on, so we can't |
---|
| 638 | * use that to print "anonymous station surveyed from somesurvey.12" |
---|
| 639 | * here. FIXME */ |
---|
[657fcee] | 640 | strcpy(buffer, "anonymous station"); |
---|
[55a0527] | 641 | /* FIXME: if ident is set, show it? */ |
---|
[a2c33ae] | 642 | return buffer; |
---|
| 643 | } |
---|
[ee2e333] | 644 | *buffer = '\0'; |
---|
| 645 | sprint_prefix_(ptr); |
---|
[a420b49] | 646 | return buffer; |
---|
[d1b1380] | 647 | } |
---|
| 648 | |
---|
[59f2dbb] | 649 | /* r = ab ; r,a,b are variance matrices */ |
---|
| 650 | void |
---|
[298e2f3] | 651 | mulss(var *r, const svar *a, const svar *b) |
---|
[59f2dbb] | 652 | { |
---|
| 653 | #ifdef NO_COVARIANCES |
---|
| 654 | /* variance-only version */ |
---|
| 655 | (*r)[0] = (*a)[0] * (*b)[0]; |
---|
| 656 | (*r)[1] = (*a)[1] * (*b)[1]; |
---|
| 657 | (*r)[2] = (*a)[2] * (*b)[2]; |
---|
| 658 | #else |
---|
| 659 | int i, j, k; |
---|
| 660 | real tot; |
---|
| 661 | |
---|
| 662 | #if 0 |
---|
[298e2f3] | 663 | SVX_ASSERT((const var *)r != a); |
---|
| 664 | SVX_ASSERT((const var *)r != b); |
---|
[59f2dbb] | 665 | #endif |
---|
| 666 | |
---|
| 667 | check_svar(a); |
---|
| 668 | check_svar(b); |
---|
| 669 | |
---|
| 670 | for (i = 0; i < 3; i++) { |
---|
| 671 | for (j = 0; j < 3; j++) { |
---|
| 672 | tot = 0; |
---|
| 673 | for (k = 0; k < 3; k++) { |
---|
| 674 | tot += SN(a,i,k) * SN(b,k,j); |
---|
| 675 | } |
---|
| 676 | (*r)[i][j] = tot; |
---|
| 677 | } |
---|
| 678 | } |
---|
| 679 | check_var(r); |
---|
| 680 | #endif |
---|
| 681 | } |
---|
| 682 | |
---|
[78d2394] | 683 | #ifndef NO_COVARIANCES |
---|
[59f2dbb] | 684 | /* r = ab ; r,a,b are variance matrices */ |
---|
| 685 | void |
---|
[298e2f3] | 686 | smulvs(svar *r, const var *a, const svar *b) |
---|
[59f2dbb] | 687 | { |
---|
| 688 | int i, j, k; |
---|
| 689 | real tot; |
---|
| 690 | |
---|
| 691 | #if 0 |
---|
[298e2f3] | 692 | SVX_ASSERT((const var *)r != a); |
---|
[59f2dbb] | 693 | #endif |
---|
[298e2f3] | 694 | SVX_ASSERT((const svar *)r != b); |
---|
[59f2dbb] | 695 | |
---|
| 696 | check_var(a); |
---|
| 697 | check_svar(b); |
---|
| 698 | |
---|
| 699 | (*r)[3]=(*r)[4]=(*r)[5]=-999; |
---|
| 700 | for (i = 0; i < 3; i++) { |
---|
| 701 | for (j = 0; j < 3; j++) { |
---|
| 702 | tot = 0; |
---|
| 703 | for (k = 0; k < 3; k++) { |
---|
| 704 | tot += (*a)[i][k] * SN(b,k,j); |
---|
| 705 | } |
---|
| 706 | if (i <= j) |
---|
| 707 | SN(r,i,j) = tot; |
---|
| 708 | else if (fabs(SN(r,j,i) - tot) > THRESHOLD) { |
---|
| 709 | printf("not sym - %d,%d = %f, %d,%d was %f\n", |
---|
| 710 | i,j,tot,j,i,SN(r,j,i)); |
---|
| 711 | BUG("smulvs didn't produce a sym mx\n"); |
---|
| 712 | } |
---|
| 713 | } |
---|
| 714 | } |
---|
| 715 | check_svar(r); |
---|
| 716 | } |
---|
[d1b1380] | 717 | #endif |
---|
| 718 | |
---|
[59f2dbb] | 719 | /* r = vb ; r,b delta vectors; a variance matrix */ |
---|
| 720 | void |
---|
[298e2f3] | 721 | mulsd(delta *r, const svar *v, const delta *b) |
---|
[59f2dbb] | 722 | { |
---|
| 723 | #ifdef NO_COVARIANCES |
---|
| 724 | /* variance-only version */ |
---|
| 725 | (*r)[0] = (*v)[0] * (*b)[0]; |
---|
| 726 | (*r)[1] = (*v)[1] * (*b)[1]; |
---|
| 727 | (*r)[2] = (*v)[2] * (*b)[2]; |
---|
| 728 | #else |
---|
| 729 | int i, j; |
---|
| 730 | real tot; |
---|
| 731 | |
---|
[298e2f3] | 732 | SVX_ASSERT((const delta*)r != b); |
---|
[59f2dbb] | 733 | check_svar(v); |
---|
| 734 | check_d(b); |
---|
| 735 | |
---|
| 736 | for (i = 0; i < 3; i++) { |
---|
| 737 | tot = 0; |
---|
| 738 | for (j = 0; j < 3; j++) tot += S(i,j) * (*b)[j]; |
---|
| 739 | (*r)[i] = tot; |
---|
| 740 | } |
---|
| 741 | check_d(r); |
---|
| 742 | #endif |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | /* r = ca ; r,a variance matrices; c real scaling factor */ |
---|
| 746 | void |
---|
[298e2f3] | 747 | mulsc(svar *r, const svar *a, real c) |
---|
[59f2dbb] | 748 | { |
---|
| 749 | #ifdef NO_COVARIANCES |
---|
| 750 | /* variance-only version */ |
---|
| 751 | (*r)[0] = (*a)[0] * c; |
---|
| 752 | (*r)[1] = (*a)[1] * c; |
---|
| 753 | (*r)[2] = (*a)[2] * c; |
---|
| 754 | #else |
---|
| 755 | int i; |
---|
| 756 | |
---|
| 757 | check_svar(a); |
---|
| 758 | for (i = 0; i < 6; i++) (*r)[i] = (*a)[i] * c; |
---|
| 759 | check_svar(r); |
---|
| 760 | #endif |
---|
| 761 | } |
---|
| 762 | |
---|
[d1b1380] | 763 | /* r = a + b ; r,a,b delta vectors */ |
---|
[a420b49] | 764 | void |
---|
[298e2f3] | 765 | adddd(delta *r, const delta *a, const delta *b) |
---|
[a420b49] | 766 | { |
---|
[5d26a94] | 767 | check_d(a); |
---|
| 768 | check_d(b); |
---|
[d1b1380] | 769 | (*r)[0] = (*a)[0] + (*b)[0]; |
---|
| 770 | (*r)[1] = (*a)[1] + (*b)[1]; |
---|
| 771 | (*r)[2] = (*a)[2] + (*b)[2]; |
---|
[5d26a94] | 772 | check_d(r); |
---|
[d1b1380] | 773 | } |
---|
| 774 | |
---|
| 775 | /* r = a - b ; r,a,b delta vectors */ |
---|
[a420b49] | 776 | void |
---|
[298e2f3] | 777 | subdd(delta *r, const delta *a, const delta *b) { |
---|
[5d26a94] | 778 | check_d(a); |
---|
| 779 | check_d(b); |
---|
[d1b1380] | 780 | (*r)[0] = (*a)[0] - (*b)[0]; |
---|
| 781 | (*r)[1] = (*a)[1] - (*b)[1]; |
---|
| 782 | (*r)[2] = (*a)[2] - (*b)[2]; |
---|
[5d26a94] | 783 | check_d(r); |
---|
[d1b1380] | 784 | } |
---|
| 785 | |
---|
[59f2dbb] | 786 | /* r = a + b ; r,a,b variance matrices */ |
---|
| 787 | void |
---|
[298e2f3] | 788 | addss(svar *r, const svar *a, const svar *b) |
---|
[59f2dbb] | 789 | { |
---|
| 790 | #ifdef NO_COVARIANCES |
---|
| 791 | /* variance-only version */ |
---|
| 792 | (*r)[0] = (*a)[0] + (*b)[0]; |
---|
| 793 | (*r)[1] = (*a)[1] + (*b)[1]; |
---|
| 794 | (*r)[2] = (*a)[2] + (*b)[2]; |
---|
| 795 | #else |
---|
| 796 | int i; |
---|
| 797 | |
---|
| 798 | check_svar(a); |
---|
| 799 | check_svar(b); |
---|
| 800 | for (i = 0; i < 6; i++) (*r)[i] = (*a)[i] + (*b)[i]; |
---|
| 801 | check_svar(r); |
---|
| 802 | #endif |
---|
| 803 | } |
---|
| 804 | |
---|
| 805 | /* r = a - b ; r,a,b variance matrices */ |
---|
| 806 | void |
---|
[298e2f3] | 807 | subss(svar *r, const svar *a, const svar *b) |
---|
[59f2dbb] | 808 | { |
---|
| 809 | #ifdef NO_COVARIANCES |
---|
| 810 | /* variance-only version */ |
---|
| 811 | (*r)[0] = (*a)[0] - (*b)[0]; |
---|
| 812 | (*r)[1] = (*a)[1] - (*b)[1]; |
---|
| 813 | (*r)[2] = (*a)[2] - (*b)[2]; |
---|
| 814 | #else |
---|
| 815 | int i; |
---|
| 816 | |
---|
| 817 | check_svar(a); |
---|
| 818 | check_svar(b); |
---|
| 819 | for (i = 0; i < 6; i++) (*r)[i] = (*a)[i] - (*b)[i]; |
---|
| 820 | check_svar(r); |
---|
| 821 | #endif |
---|
| 822 | } |
---|
| 823 | |
---|
[7a05e07] | 824 | /* inv = v^-1 ; inv,v variance matrices */ |
---|
[f98f8a5] | 825 | extern int |
---|
[298e2f3] | 826 | invert_svar(svar *inv, const svar *v) |
---|
[f98f8a5] | 827 | { |
---|
[78d2394] | 828 | #ifdef NO_COVARIANCES |
---|
[f98f8a5] | 829 | int i; |
---|
| 830 | for (i = 0; i < 3; i++) { |
---|
[42d23c5] | 831 | if ((*v)[i] == 0.0) return 0; /* matrix is singular */ |
---|
[f98f8a5] | 832 | (*inv)[i] = 1.0 / (*v)[i]; |
---|
| 833 | } |
---|
| 834 | #else |
---|
[dac18d8] | 835 | real det, a, b, c, d, e, f, bcff, efcd, dfbe; |
---|
[59f2dbb] | 836 | |
---|
| 837 | #if 0 |
---|
[298e2f3] | 838 | SVX_ASSERT((const var *)inv != v); |
---|
[59f2dbb] | 839 | #endif |
---|
| 840 | |
---|
| 841 | check_svar(v); |
---|
[dac18d8] | 842 | /* a d e |
---|
| 843 | * d b f |
---|
| 844 | * e f c |
---|
| 845 | */ |
---|
| 846 | a = (*v)[0], b = (*v)[1], c = (*v)[2]; |
---|
| 847 | d = (*v)[3], e = (*v)[4], f = (*v)[5]; |
---|
| 848 | bcff = b * c - f * f; |
---|
| 849 | efcd = e * f - c * d; |
---|
| 850 | dfbe = d * f - b * e; |
---|
| 851 | det = a * bcff + d * efcd + e * dfbe; |
---|
[59f2dbb] | 852 | |
---|
[42d23c5] | 853 | if (det == 0.0) { |
---|
[59f2dbb] | 854 | /* printf("det=%.20f\n", det); */ |
---|
| 855 | return 0; /* matrix is singular */ |
---|
| 856 | } |
---|
| 857 | |
---|
| 858 | det = 1 / det; |
---|
| 859 | |
---|
[dac18d8] | 860 | (*inv)[0] = det * bcff; |
---|
| 861 | (*inv)[1] = det * (c * a - e * e); |
---|
| 862 | (*inv)[2] = det * (a * b - d * d); |
---|
| 863 | (*inv)[3] = det * efcd; |
---|
| 864 | (*inv)[4] = det * dfbe; |
---|
| 865 | (*inv)[5] = det * (e * d - a * f); |
---|
[59f2dbb] | 866 | |
---|
[38193a5] | 867 | #if 0 |
---|
| 868 | /* This test fires very occasionally, and there's not much point in |
---|
| 869 | * it anyhow - the matrix inversion algorithm is simple enough that |
---|
| 870 | * we can be confident it's correctly implemented, so we might as |
---|
| 871 | * well save the cycles and not perform this check. |
---|
| 872 | */ |
---|
[59f2dbb] | 873 | { /* check that original * inverse = identity matrix */ |
---|
[b4fe9fb] | 874 | int i; |
---|
[59f2dbb] | 875 | var p; |
---|
[dac18d8] | 876 | real D = 0; |
---|
| 877 | mulss(&p, v, inv); |
---|
[59f2dbb] | 878 | for (i = 0; i < 3; i++) { |
---|
[dac18d8] | 879 | int j; |
---|
| 880 | for (j = 0; j < 3; j++) D += fabs(p[i][j] - (real)(i==j)); |
---|
[59f2dbb] | 881 | } |
---|
[dac18d8] | 882 | if (D > THRESHOLD) { |
---|
[59f2dbb] | 883 | printf("original * inverse=\n"); |
---|
[dac18d8] | 884 | print_svar(*v); |
---|
[59f2dbb] | 885 | printf("*\n"); |
---|
[dac18d8] | 886 | print_svar(*inv); |
---|
[59f2dbb] | 887 | printf("=\n"); |
---|
| 888 | print_var(p); |
---|
| 889 | BUG("matrix didn't invert"); |
---|
| 890 | } |
---|
[dac18d8] | 891 | check_svar(inv); |
---|
[59f2dbb] | 892 | } |
---|
[78d2394] | 893 | #endif |
---|
[38193a5] | 894 | #endif |
---|
[59f2dbb] | 895 | return 1; |
---|
| 896 | } |
---|
| 897 | |
---|
[d1b1380] | 898 | /* r = (b^-1)a ; r,a delta vectors; b variance matrix */ |
---|
[78d2394] | 899 | #ifndef NO_COVARIANCES |
---|
[a420b49] | 900 | void |
---|
[298e2f3] | 901 | divds(delta *r, const delta *a, const svar *b) |
---|
[a420b49] | 902 | { |
---|
[d1b1380] | 903 | #ifdef NO_COVARIANCES |
---|
| 904 | /* variance-only version */ |
---|
| 905 | (*r)[0] = (*a)[0] / (*b)[0]; |
---|
| 906 | (*r)[1] = (*a)[1] / (*b)[1]; |
---|
| 907 | (*r)[2] = (*a)[2] / (*b)[2]; |
---|
| 908 | #else |
---|
[59f2dbb] | 909 | svar b_inv; |
---|
[dac18d8] | 910 | if (!invert_svar(&b_inv, b)) { |
---|
[59f2dbb] | 911 | print_svar(*b); |
---|
| 912 | BUG("covariance matrix is singular"); |
---|
| 913 | } |
---|
| 914 | mulsd(r, &b_inv, a); |
---|
[d1b1380] | 915 | #endif |
---|
| 916 | } |
---|
[78d2394] | 917 | #endif |
---|
[d1b1380] | 918 | |
---|
[a420b49] | 919 | bool |
---|
[298e2f3] | 920 | fZeros(const svar *v) { |
---|
[d1b1380] | 921 | #ifdef NO_COVARIANCES |
---|
| 922 | /* variance-only version */ |
---|
[a420b49] | 923 | return ((*v)[0] == 0.0 && (*v)[1] == 0.0 && (*v)[2] == 0.0); |
---|
[d1b1380] | 924 | #else |
---|
[59f2dbb] | 925 | int i; |
---|
| 926 | |
---|
| 927 | check_svar(v); |
---|
[63d4f07] | 928 | for (i = 0; i < 6; i++) if ((*v)[i] != 0.0) return false; |
---|
[59f2dbb] | 929 | |
---|
[63d4f07] | 930 | return true; |
---|
[59f2dbb] | 931 | #endif |
---|
[dac18d8] | 932 | } |
---|