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