source: git/src/netskel.c @ a4f1d96

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since a4f1d96 was 736f7df, checked in by Olly Betts <olly@…>, 10 years ago

lib/extract-msgs.pl,lib/survex.pot,src/: Insert "TRANSLATORS"
comments into source code.

  • Property mode set to 100644
File size: 30.5 KB
Line 
1/* netskel.c
2 * Survex network reduction - remove trailing traverses and concatenate
3 * traverses between junctions
4 * Copyright (C) 1991-2004,2005,2006,2010,2011,2012,2013,2014 Olly Betts
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 */
20
21/* #define BLUNDER_DETECTION */
22
23#if 0
24#define DEBUG_INVALID 1
25#define VALIDATE 1
26#define DUMP_NETWORK 1
27#endif
28
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#include "validate.h"
34#include "debug.h"
35#include "cavern.h"
36#include "filename.h"
37#include "message.h"
38#include "filelist.h"
39#include "img_hosted.h"
40#include "netartic.h"
41#include "netbits.h"
42#include "netskel.h"
43#include "network.h"
44#include "out.h"
45
46#define sqrdd(X) (sqrd((X)[0]) + sqrd((X)[1]) + sqrd((X)[2]))
47
48typedef struct Stack {
49   struct Link *join1, *join2;
50   struct Stack *next;
51} stack;
52
53typedef struct StackTr {
54   struct Link *join1;
55   struct StackTr *next;
56} stackTrail;
57
58/* string used between things in traverse printouts eg \1 - \2 - \3 -...*/
59static const char *szLink = " - ";
60static const char *szLinkEq = " = "; /* use this one for equates */
61
62#if 0
63#define fprint_prefix(FH, NAME) BLK((fprint_prefix)((FH), (NAME));\
64                                    fprintf((FH), " [%p]", (void*)(NAME)); )
65#endif
66
67static stack *ptr; /* Ptr to TRaverse linked list for in-between travs */
68static stackTrail *ptrTrail; /* Ptr to TRaverse linked list for trail travs*/
69
70static void remove_trailing_travs(void);
71static void remove_travs(void);
72static void replace_travs(void);
73static void replace_trailing_travs(void);
74static void write_passage_models(void);
75
76static void concatenate_trav(node *stn, int i);
77
78static void err_stat(int cLegsTrav, double lenTrav,
79                     double eTot, double eTotTheo,
80                     double hTot, double hTotTheo,
81                     double vTot, double vTotTheo);
82
83extern void
84solve_network(void /*node *stnlist*/)
85{
86   static int first_solve = 1;
87   node *stn;
88
89   if (stnlist == NULL) {
90      if (first_solve) fatalerror(/*No survey data*/43);
91      /* We've had a *solve followed by another *solve (or the implicit
92       * *solve at the end of the data.  Don't moan about that. */
93      return;
94   }
95   ptr = NULL;
96   ptrTrail = NULL;
97   dump_network();
98
99   /* If there are no fixed points, invent one.  Do this first to
100    * avoid problems with sub-nodes of the invented fix which have
101    * been removed.  It also means we can fix the "first" station,
102    * which makes more sense to the user. */
103   FOR_EACH_STN(stn, stnlist)
104      if (fixed(stn)) break;
105
106   if (!stn && first_solve) {
107     /* If we've had a *solve and all the new survey since then is hanging,
108      * we don't want to invent a fixed point.  We want to complain but
109      * the easiest way to is just to continue processing and let
110      * articulate() catch this condition as it will any hanging survey
111      * data. */
112      node *stnFirst = NULL;
113
114      /* New stations are pushed onto the head of the list, so the
115       * first station added is the last in the list. */
116      FOR_EACH_STN(stn, stnlist) {
117          /* Prefer a station with legs attached when choosing one to fix
118           * so that if there's a hanging station on a nosurvey leg we pick
119           * the main clump of survey data. */
120          if (stnFirst && !stnFirst->leg[0]) continue;
121          stnFirst = stn;
122      }
123
124      /* If we've got nosurvey legs, then the station we find to fix could have
125       * no real legs attached. */
126      SVX_ASSERT2(nosurveyhead || stnFirst->leg[0], "no fixed stns, but we've got a zero node!");
127      SVX_ASSERT2(stnFirst, "no stations left in net!");
128      stn = stnFirst;
129      printf(msg(/*Survey has no fixed points. Therefore I’ve fixed %s at (0,0,0)*/72),
130             sprint_prefix(stn->name));
131      putnl();
132      POS(stn,0) = (real)0.0;
133      POS(stn,1) = (real)0.0;
134      POS(stn,2) = (real)0.0;
135      fix(stn);
136   }
137
138   first_solve = 0;
139
140   remove_trailing_travs();
141   validate(); dump_network();
142   remove_travs();
143   validate(); dump_network();
144   remove_subnets();
145   validate(); dump_network();
146   articulate();
147   validate(); dump_network();
148   replace_subnets();
149   validate(); dump_network();
150   replace_travs();
151   validate(); dump_network();
152   replace_trailing_travs();
153   validate(); dump_network();
154
155   /* Now write out any passage models. */
156   write_passage_models();
157}
158
159static void
160remove_trailing_travs(void)
161{
162   node *stn;
163   /* TRANSLATORS: In French, Eric chose to use the terminology used by
164    * toporobot: "sequence" for the English "traverse", which makes sense
165    * (although toporobot actually uses this term to mean something more
166    * specific).  Feel free to follow this lead if you can't think of a better
167    * term - these messages mostly indicate how processing is progressing.
168    *
169    * A trailing traverse is a dead end back to a junction. */
170   out_current_action(msg(/*Removing trailing traverses*/125));
171   FOR_EACH_STN(stn, stnlist) {
172      if (!fixed(stn) && one_node(stn)) {
173         int i = 0;
174         int j;
175         node *stn2 = stn;
176         stackTrail *trav;
177
178#if PRINT_NETBITS
179         printf("Removed trailing trav ");
180#endif
181         do {
182            struct Link *leg;
183#if PRINT_NETBITS
184            print_prefix(stn2->name); printf("<%p>",stn2); fputs(szLink, stdout);
185#endif
186            remove_stn_from_list(&stnlist, stn2);
187            leg = stn2->leg[i];
188            j = reverse_leg_dirn(leg);
189            stn2 = leg->l.to;
190            i = j ^ 1; /* flip direction for other leg of 2 node */
191            /* stop if fixed or 3 or 1 node */
192         } while (two_node(stn2) && !fixed(stn2));
193
194         /* put traverse on stack */
195         trav = osnew(stackTrail);
196         trav->join1 = stn2->leg[j];
197         trav->next = ptrTrail;
198         ptrTrail = trav;
199
200         /* We want to keep all 2-nodes using legs 0 and 1 and all one nodes
201          * using leg 0 so we may need to swap leg j with leg 2 (for a 3 node)
202          * or leg 1 (for a fixed 2 node) */
203         if ((j == 0 && !one_node(stn2)) || (j == 1 && three_node(stn2))) {
204            /* i is the direction to swap with */
205            i = (three_node(stn2)) ? 2 : 1;
206            /* change the other direction of leg i to use leg j */
207            reverse_leg(stn2->leg[i])->l.reverse += j - i;
208            stn2->leg[j] = stn2->leg[i];
209            j = i;
210         }
211         stn2->leg[j] = NULL;
212
213#if PRINT_NETBITS
214         print_prefix(stn2->name); printf("<%p>",stn2); putnl();
215#endif
216      }
217   }
218}
219
220static void
221remove_travs(void)
222{
223   node *stn;
224   /* TRANSLATORS: In French, Eric chose to use the terminology used by
225    * toporobot: "sequence" for the English "traverse", which makes sense
226    * (although toporobot actually uses this term to mean something more
227    * specific).  Feel free to follow this lead if you can't think of a better
228    * term - these messages mostly indicate how processing is progressing. */
229   out_current_action(msg(/*Concatenating traverses*/126));
230   FOR_EACH_STN(stn, stnlist) {
231      if (fixed(stn) || three_node(stn)) {
232         int d;
233         for (d = 0; d <= 2; d++) {
234            linkfor *leg = stn->leg[d];
235            if (leg && !(leg->l.reverse & FLAG_REPLACEMENTLEG))
236               concatenate_trav(stn, d);
237         }
238      }
239   }
240}
241
242static void
243concatenate_trav(node *stn, int i)
244{
245   int j;
246   stack *trav;
247   node *stn2;
248   linkfor *newleg, *newleg2;
249
250   stn2 = stn->leg[i]->l.to;
251   /* Reject single legs as they may be already concatenated traverses */
252   if (fixed(stn2) || !two_node(stn2)) return;
253
254   trav = osnew(stack);
255   newleg2 = (linkfor*)osnew(linkrev);
256
257#if PRINT_NETBITS
258   printf("Concatenating trav "); print_prefix(stn->name); printf("<%p>",stn);
259#endif
260
261   newleg2->l.to = stn;
262   newleg2->l.reverse = i | FLAG_REPLACEMENTLEG;
263   trav->join1 = stn->leg[i];
264
265   j = reverse_leg_dirn(stn->leg[i]);
266   SVX_ASSERT(j == 0 || j == 1);
267
268   newleg = copy_link(stn->leg[i]);
269
270   while (1) {
271      stn = stn2;
272
273#if PRINT_NETBITS
274      fputs(szLink, stdout); print_prefix(stn->name); printf("<%p>",stn);
275#endif
276
277      /* stop if fixed or 3 or 1 node */
278      if (fixed(stn) || !two_node(stn)) break;
279
280      remove_stn_from_list(&stnlist, stn);
281
282      i = j ^ 1; /* flip direction for other leg of 2 node */
283
284      stn2 = stn->leg[i]->l.to;
285      j = reverse_leg_dirn(stn->leg[i]);
286
287      addto_link(newleg, stn->leg[i]);
288   }
289
290   trav->join2 = stn->leg[j];
291   trav->next = ptr;
292   ptr = trav;
293
294   newleg->l.to = stn;
295   newleg->l.reverse = j | FLAG_DATAHERE | FLAG_REPLACEMENTLEG;
296
297   newleg2->l.to->leg[reverse_leg_dirn(newleg2)] = newleg;
298   /* i.e. stn->leg[i] = newleg; with original stn and i */
299
300   stn->leg[j] = newleg2;
301
302#if PRINT_NETBITS
303   putchar(' ');
304   print_var(&(newleg->v));
305   printf("\nStacked ");
306   print_prefix(newleg2->l.to->name);
307   printf(",%d-", reverse_leg_dirn(newleg2));
308   print_prefix(stn->name);
309   printf(",%d\n", j);
310#endif
311}
312
313#ifdef BLUNDER_DETECTION
314/* expected_error is actually squared... */
315/* only called if fhErrStat != NULL */
316static void
317do_gross(delta e, delta v, node *stn1, node *stn2, double expected_error)
318{
319   double hsqrd, rsqrd, s, cx, cy, cz;
320   double tot;
321   int i;
322   int output = 0;
323   prefix *name1 = stn1->name, *name2 = stn2->name;
324
325#if 0
326printf( "e = ( %.2f, %.2f, %.2f )", e[0], e[1], e[2] );
327printf( " v = ( %.2f, %.2f, %.2f )\n", v[0], v[1], v[2] );
328#endif
329   hsqrd = sqrd(v[0]) + sqrd(v[1]);
330   rsqrd = hsqrd + sqrd(v[2]);
331   if (rsqrd == 0.0) return;
332
333   cx = v[0] + e[0];
334   cy = v[1] + e[1];
335   cz = v[2] + e[2];
336
337   s = (e[0] * v[0] + e[1] * v[1] + e[2] * v[2]) / rsqrd;
338   tot = 0;
339   for (i = 2; i >= 0; i--) tot += sqrd(e[i] - v[i] * s);
340
341   if (tot <= expected_error) {
342      if (!output) {
343         fprint_prefix(fhErrStat, name1);
344         fputs("->", fhErrStat);
345         fprint_prefix(fhErrStat, name2);
346      }
347      fprintf(fhErrStat, " L: %.2f", sqrt(tot));
348      /* checked - works */
349      fprintf(fhErrStat, " (%.2fm -> %.2fm)", sqrt(sqrdd(v)), sqrt(sqrdd(v)) * (1 - s));
350      output = 1;
351   }
352
353   s = sqrd(cx) + sqrd(cy);
354   if (s > 0.0) {
355      s = hsqrd / s;
356      SVX_ASSERT(s >= 0.0);
357      s = sqrt(s);
358      s = 1 - s;
359      tot = sqrd(cx * s) + sqrd(cy * s) + sqrd(e[2]);
360      if (tot <= expected_error) {
361         double newval, oldval;
362         if (!output) {
363            fprint_prefix(fhErrStat, name1);
364            fputs("->", fhErrStat);
365            fprint_prefix(fhErrStat, name2);
366         }
367         fprintf(fhErrStat, " B: %.2f", sqrt(tot));
368         /* checked - works */
369         newval = deg(atan2(cx, cy));
370         if (newval < 0) newval += 360;
371         oldval = deg(atan2(v[0], v[1]));
372         if (oldval < 0) oldval += 360;
373         fprintf(fhErrStat, " (%.2fdeg -> %.2fdeg)", oldval, newval);
374         output = 1;
375      }
376   }
377
378   if (hsqrd > 0.0) {
379      double nx, ny;
380      s = (e[0] * v[1] - e[1] * v[0]) / hsqrd;
381      nx = cx - s * v[1];
382      ny = cy + s * v[0];
383      s = sqrd(nx) + sqrd(ny) + sqrd(cz);
384      if (s > 0.0) {
385         s = rsqrd / s;
386         SVX_ASSERT(s >= 0);
387         s = sqrt(s);
388         tot = sqrd(cx - s * nx) + sqrd(cy - s * ny) + sqrd(cz - s * cz);
389         if (tot <= expected_error) {
390            if (!output) {
391               fprint_prefix(fhErrStat, name1);
392               fputs("->", fhErrStat);
393               fprint_prefix(fhErrStat, name2);
394            }
395            fprintf(fhErrStat, " G: %.2f", sqrt(tot));
396            /* checked - works */
397            fprintf(fhErrStat, " (%.2fdeg -> %.2fdeg)",
398                    deg(atan2(v[2], sqrt(v[0] * v[0] + v[1] * v[1]))),
399                    deg(atan2(cz, sqrt(nx * nx + ny * ny))));
400            output = 1;
401         }
402      }
403   }
404   if (output) fputnl(fhErrStat);
405}
406#endif
407
408static void
409replace_travs(void)
410{
411   stack *ptrOld;
412   node *stn1, *stn2, *stn3;
413   int i, j, k;
414   double eTot = 0, lenTrav = 0, lenTot;
415   double eTotTheo = 0;
416   double vTot = 0, vTotTheo = 0, hTot = 0, hTotTheo = 0;
417   delta e, sc;
418   bool fEquate; /* used to indicate equates in output */
419   int cLegsTrav = 0;
420   bool fArtic;
421
422    /* TRANSLATORS: In French, Eric chose to use the terminology used by
423     * toporobot: "sequence" for the English "traverse", which makes sense
424     * (although toporobot actually uses this term to mean something more
425     * specific).  Feel free to follow this lead if you can't think of a better
426     * term - these messages mostly indicate how processing is progressing. */
427   out_current_action(msg(/*Calculating traverses*/127));
428
429   if (!fhErrStat && !fSuppress)
430      fhErrStat = safe_fopen_with_ext(fnm_output_base, EXT_SVX_ERRS, "w");
431
432   if (!pimg) {
433      char *fnm = add_ext(fnm_output_base, EXT_SVX_3D);
434      filename_register_output(fnm);
435      pimg = img_open_write(fnm, survey_title, 0);
436      if (!pimg) fatalerror(img_error(), fnm);
437      osfree(fnm);
438   }
439
440   /* First do all the one leg traverses */
441   FOR_EACH_STN(stn1, stnlist) {
442#if PRINT_NETBITS
443      printf("One leg traverses from ");
444      print_prefix(stn1->name);
445      printf(" [%p]\n", stn1);
446#endif
447      for (i = 0; i <= 2; i++) {
448         linkfor *leg = stn1->leg[i];
449         if (leg && data_here(leg) &&
450             !(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) {
451            SVX_ASSERT(fixed(stn1));
452            SVX_ASSERT(!fZeros(&leg->v));
453
454            stn2 = leg->l.to;
455            if (TSTBIT(leg->l.flags, FLAGS_SURFACE)) {
456               stn1->name->sflags |= BIT(SFLAGS_SURFACE);
457               stn2->name->sflags |= BIT(SFLAGS_SURFACE);
458            } else {
459               stn1->name->sflags |= BIT(SFLAGS_UNDERGROUND);
460               stn2->name->sflags |= BIT(SFLAGS_UNDERGROUND);
461            }
462            img_write_item(pimg, img_MOVE, 0, NULL,
463                           POS(stn1, 0), POS(stn1, 1), POS(stn1, 2));
464            if (leg->meta) {
465                pimg->days1 = leg->meta->days1;
466                pimg->days2 = leg->meta->days2;
467            } else {
468                pimg->days1 = pimg->days2 = -1;
469            }
470            pimg->style = (leg->l.flags >> FLAGS_STYLE_BIT0) & 0x07;
471            img_write_item(pimg, img_LINE, leg->l.flags & FLAGS_MASK,
472                           sprint_prefix(stn1->name->up),
473                           POS(stn2, 0), POS(stn2, 1), POS(stn2, 2));
474            if (!(leg->l.reverse & FLAG_ARTICULATION)) {
475#ifdef BLUNDER_DETECTION
476               delta err;
477               int do_blunder;
478#else
479               if (fhErrStat) {
480                  fprint_prefix(fhErrStat, stn1->name);
481                  fputs(szLink, fhErrStat);
482                  fprint_prefix(fhErrStat, stn2->name);
483               }
484#endif
485               subdd(&e, &POSD(stn2), &POSD(stn1));
486               subdd(&e, &e, &leg->d);
487               if (fhErrStat) {
488                  eTot = sqrdd(e);
489                  hTot = sqrd(e[0]) + sqrd(e[1]);
490                  vTot = sqrd(e[2]);
491#ifndef NO_COVARIANCES
492                  /* FIXME: what about covariances? */
493                  hTotTheo = leg->v[0] + leg->v[1];
494                  vTotTheo = leg->v[2];
495                  eTotTheo = hTotTheo + vTotTheo;
496#else
497                  hTotTheo = leg->v[0] + leg->v[1];
498                  vTotTheo = leg->v[2];
499                  eTotTheo = hTotTheo + vTotTheo;
500#endif
501#ifdef BLUNDER_DETECTION
502                  memcpy(&err, &e, sizeof(delta));
503                  do_blunder = (eTot > eTotTheo);
504                  fputs("\ntraverse ", fhErrStat);
505                  fprint_prefix(fhErrStat, stn1->name);
506                  fputs("->", fhErrStat);
507                  fprint_prefix(fhErrStat, stn2->name);
508                  fprintf(fhErrStat, " e=(%.2f, %.2f, %.2f) mag=%.2f %s\n",
509                          e[0], e[1], e[2], sqrt(eTot),
510                          (do_blunder ? "suspect:" : "OK"));
511                  if (do_blunder)
512                     do_gross(err, leg->d, stn1, stn2, eTotTheo);
513#endif
514                  err_stat(1, sqrt(sqrdd(leg->d)), eTot, eTotTheo,
515                           hTot, hTotTheo, vTot, vTotTheo);
516               }
517            }
518         }
519      }
520   }
521
522   while (ptr != NULL) {
523      /* work out where traverse should be reconnected */
524      linkfor *leg = ptr->join1;
525      leg = reverse_leg(leg);
526      stn1 = leg->l.to;
527      i = reverse_leg_dirn(leg);
528
529      leg = ptr->join2;
530      leg = reverse_leg(leg);
531      stn2 = leg->l.to;
532      j = reverse_leg_dirn(leg);
533
534#if PRINT_NETBITS
535      printf(" Trav ");
536      print_prefix(stn1->name);
537      printf("<%p>[%d]%s...%s", stn1, i, szLink, szLink);
538      print_prefix(stn2->name);
539      printf("<%p>[%d]\n", stn2, j);
540#endif
541
542      SVX_ASSERT(fixed(stn1));
543      SVX_ASSERT(fixed(stn2));
544
545      /* calculate scaling factors for error distribution */
546      eTot = 0.0;
547      hTot = vTot = 0.0;
548      SVX_ASSERT(data_here(stn1->leg[i]));
549      if (fZeros(&stn1->leg[i]->v)) {
550         sc[0] = sc[1] = sc[2] = 0.0;
551      } else {
552         subdd(&e, &POSD(stn2), &POSD(stn1));
553         subdd(&e, &e, &stn1->leg[i]->d);
554         eTot = sqrdd(e);
555         hTot = sqrd(e[0]) + sqrd(e[1]);
556         vTot = sqrd(e[2]);
557         divds(&sc, &e, &stn1->leg[i]->v);
558      }
559#ifndef NO_COVARIANCES
560      /* FIXME: what about covariances? */
561      hTotTheo = stn1->leg[i]->v[0] + stn1->leg[i]->v[1];
562      vTotTheo = stn1->leg[i]->v[2];
563#else
564      hTotTheo = stn1->leg[i]->v[0] + stn1->leg[i]->v[1];
565      vTotTheo = stn1->leg[i]->v[2];
566#endif
567      eTotTheo = hTotTheo + vTotTheo;
568      cLegsTrav = 0;
569      lenTrav = 0.0;
570      img_write_item(pimg, img_MOVE, 0, NULL,
571                     POS(stn1, 0), POS(stn1, 1), POS(stn1, 2));
572
573      fArtic = stn1->leg[i]->l.reverse & FLAG_ARTICULATION;
574      osfree(stn1->leg[i]);
575      stn1->leg[i] = ptr->join1; /* put old link back in */
576
577      osfree(stn2->leg[j]);
578      stn2->leg[j] = ptr->join2; /* and the other end */
579
580#ifdef BLUNDER_DETECTION
581      delta err;
582      int do_blunder;
583      memcpy(&err, &e, sizeof(delta));
584      do_blunder = (eTot > eTotTheo);
585      if (fhErrStat && !fArtic) {
586         fputs("\ntraverse ", fhErrStat);
587         fprint_prefix(fhErrStat, stn1->name);
588         fputs("->", fhErrStat);
589         fprint_prefix(fhErrStat, stn2->name);
590         fprintf(fhErrStat, " e=(%.2f, %.2f, %.2f) mag=%.2f %s\n",
591                 e[0], e[1], e[2], sqrt(eTot),
592                 (do_blunder ? "suspect:" : "OK"));
593      }
594#endif
595      while (fTrue) {
596         int reached_end;
597         prefix *leg_pfx;
598
599         fEquate = fTrue;
600         /* get next node in traverse
601          * should have stn3->leg[k]->l.to == stn1 */
602         stn3 = stn1->leg[i]->l.to;
603         k = reverse_leg_dirn(stn1->leg[i]);
604         SVX_ASSERT2(stn3->leg[k]->l.to == stn1,
605                 "reverse leg doesn't reciprocate");
606
607         reached_end = (stn3 == stn2 && k == j);
608
609         if (data_here(stn1->leg[i])) {
610            leg_pfx = stn1->name->up;
611            leg = stn1->leg[i];
612#ifdef BLUNDER_DETECTION
613            if (do_blunder && fhErrStat)
614               do_gross(err, leg->d, stn1, stn3, eTotTheo);
615#endif
616            if (!reached_end)
617               adddd(&POSD(stn3), &POSD(stn1), &leg->d);
618         } else {
619            leg_pfx = stn3->name->up;
620            leg = stn3->leg[k];
621#ifdef BLUNDER_DETECTION
622            if (do_blunder && fhErrStat)
623               do_gross(err, leg->d, stn1, stn3, eTotTheo);
624#endif
625            if (!reached_end)
626               subdd(&POSD(stn3), &POSD(stn1), &leg->d);
627         }
628
629         lenTot = sqrdd(leg->d);
630
631         if (!fZeros(&leg->v)) fEquate = fFalse;
632         if (!reached_end) {
633            add_stn_to_list(&stnlist, stn3);
634            if (!fEquate) {
635               mulsd(&e, &leg->v, &sc);
636               adddd(&POSD(stn3), &POSD(stn3), &e);
637            }
638            fix(stn3);
639         }
640
641         if (!(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) {
642             if (TSTBIT(leg->l.flags, FLAGS_SURFACE)) {
643                stn1->name->sflags |= BIT(SFLAGS_SURFACE);
644                stn3->name->sflags |= BIT(SFLAGS_SURFACE);
645             } else {
646                stn1->name->sflags |= BIT(SFLAGS_UNDERGROUND);
647                stn3->name->sflags |= BIT(SFLAGS_UNDERGROUND);
648             }
649
650            SVX_ASSERT(!fEquate);
651            SVX_ASSERT(!fZeros(&leg->v));
652            if (leg->meta) {
653                pimg->days1 = leg->meta->days1;
654                pimg->days2 = leg->meta->days2;
655            } else {
656                pimg->days1 = pimg->days2 = -1;
657            }
658            pimg->style = (leg->l.flags >> FLAGS_STYLE_BIT0) & 0x07;
659            img_write_item(pimg, img_LINE, leg->l.flags & FLAGS_MASK,
660                           sprint_prefix(leg_pfx),
661                           POS(stn3, 0), POS(stn3, 1), POS(stn3, 2));
662         }
663
664         /* FIXME: equate at the start of a traverse treated specially
665          * - what about equates at end? */
666         if (stn1->name != stn3->name && !(fEquate && cLegsTrav == 0)) {
667            /* (node not part of same stn) &&
668             * (not equate at start of traverse) */
669#ifndef BLUNDER_DETECTION
670            if (fhErrStat && !fArtic) {
671               if (!stn1->name->ident) {
672                  /* FIXME: not ideal */
673                  fputs("<fixed point>", fhErrStat);
674               } else {
675                  fprint_prefix(fhErrStat, stn1->name);
676               }
677               fputs(fEquate ? szLinkEq : szLink, fhErrStat);
678               if (reached_end) {
679                  if (!stn3->name->ident) {
680                     /* FIXME: not ideal */
681                     fputs("<fixed point>", fhErrStat);
682                  } else {
683                     fprint_prefix(fhErrStat, stn3->name);
684                  }
685               }
686            }
687#endif
688            if (!fEquate) {
689               cLegsTrav++;
690               lenTrav += sqrt(lenTot);
691            }
692         } else {
693#if SHOW_INTERNAL_LEGS
694            if (fhErrStat && !fArtic) fprintf(fhErrStat, "+");
695#endif
696            if (lenTot > 0.0) {
697#if DEBUG_INVALID
698               fprintf(stderr, "lenTot = %8.4f ", lenTot);
699               fprint_prefix(stderr, stn1->name);
700               fprintf(stderr, " -> ");
701               fprint_prefix(stderr, stn3->name);
702#endif
703               BUG("during calculation of closure errors");
704            }
705         }
706         if (reached_end) break;
707
708         i = k ^ 1; /* flip direction for other leg of 2 node */
709
710         stn1 = stn3;
711      } /* endwhile */
712
713      if (cLegsTrav && !fArtic && fhErrStat)
714         err_stat(cLegsTrav, lenTrav, eTot, eTotTheo,
715                  hTot, hTotTheo, vTot, vTotTheo);
716
717      ptrOld = ptr;
718      ptr = ptr->next;
719      osfree(ptrOld);
720   }
721
722   /* Leave fhErrStat open in case we're asked to close loops again... */
723}
724
725static void
726err_stat(int cLegsTrav, double lenTrav,
727         double eTot, double eTotTheo,
728         double hTot, double hTotTheo,
729         double vTot, double vTotTheo)
730{
731   double E = sqrt(eTot / eTotTheo);
732   double H = sqrt(hTot / hTotTheo);
733   double V = sqrt(vTot / vTotTheo);
734   if (!fSuppress) {
735      double sqrt_eTot = sqrt(eTot);
736      fputnl(fhErrStat);
737      fprintf(fhErrStat, msg(/*Original length %6.2fm (%3d legs), moved %6.2fm (%5.2fm/leg). */145),
738              lenTrav, cLegsTrav, sqrt_eTot, sqrt_eTot / cLegsTrav);
739      if (lenTrav > 0.0) {
740         fprintf(fhErrStat, msg(/*Error %6.2f%%*/146), 100 * sqrt_eTot / lenTrav);
741      } else {
742         /* TRANSLATORS: Here N/A means "Not Applicable" -- it means the
743          * traverse has zero length, so error per metre is meaningless.
744          *
745          * There should be 4 spaces between "Error" and "N/A" so that it lines
746          * up with the numbers in the message above. */
747         fputs(msg(/*Error    N/A*/147), fhErrStat);
748      }
749      fputnl(fhErrStat);
750      fprintf(fhErrStat, "%f\n", E);
751      fprintf(fhErrStat, "H: %f V: %f\n", H, V);
752      fputnl(fhErrStat);
753   }
754   img_write_errors(pimg, cLegsTrav, lenTrav, E, H, V);
755}
756
757static void
758replace_trailing_travs(void)
759{
760   stackTrail *ptrOld;
761   node *stn1, *stn2;
762   linkfor *leg;
763   int i;
764
765   /* TRANSLATORS: In French, Eric chose to use the terminology used by
766    * toporobot: "sequence" for the English "traverse", which makes sense
767    * (although toporobot actually uses this term to mean something more
768    * specific).  Feel free to follow this lead if you can't think of a better
769    * term - these messages mostly indicate how processing is progressing.
770    *
771    * A trailing traverse is a dead end back to a junction. */
772   out_current_action(msg(/*Calculating trailing traverses*/128));
773
774   while (ptrTrail != NULL) {
775      leg = ptrTrail->join1;
776      leg = reverse_leg(leg);
777      stn1 = leg->l.to;
778      i = reverse_leg_dirn(leg);
779#if PRINT_NETBITS
780      printf(" Trailing trav ");
781      print_prefix(stn1->name);
782      printf("<%p>", stn1);
783      printf("%s...\n", szLink);
784      printf("    attachment stn is at (%f, %f, %f)\n",
785             POS(stn1, 0), POS(stn1, 1), POS(stn1, 2));
786#endif
787      /* We may have swapped the links round when we removed the leg.  If
788       * we did then stn1->leg[i] will be in use.  The link we swapped
789       * with is the first free leg */
790      if (stn1->leg[i]) {
791         /* j is the direction to swap with */
792         int j = (stn1->leg[1]) ? 2 : 1;
793         /* change the other direction of leg i to use leg j */
794         reverse_leg(stn1->leg[i])->l.reverse += j - i;
795         stn1->leg[j] = stn1->leg[i];
796      }
797      stn1->leg[i] = ptrTrail->join1;
798      SVX_ASSERT(fixed(stn1));
799      img_write_item(pimg, img_MOVE, 0, NULL,
800                     POS(stn1, 0), POS(stn1, 1), POS(stn1, 2));
801
802      while (1) {
803         prefix *leg_pfx;
804         int j;
805
806         leg = stn1->leg[i];
807         stn2 = leg->l.to;
808         j = reverse_leg_dirn(leg);
809         if (data_here(leg)) {
810            leg_pfx = stn1->name->up;
811            adddd(&POSD(stn2), &POSD(stn1), &leg->d);
812#if 0
813            printf("Adding leg (%f, %f, %f)\n", leg->d[0], leg->d[1], leg->d[2]);
814#endif
815         } else {
816            leg_pfx = stn2->name->up;
817            leg = stn2->leg[j];
818            subdd(&POSD(stn2), &POSD(stn1), &leg->d);
819#if 0
820            printf("Subtracting reverse leg (%f, %f, %f)\n", leg->d[0], leg->d[1], leg->d[2]);
821#endif
822         }
823
824         fix(stn2);
825         add_stn_to_list(&stnlist, stn2);
826         if (!(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) {
827             if (TSTBIT(leg->l.flags, FLAGS_SURFACE)) {
828                stn1->name->sflags |= BIT(SFLAGS_SURFACE);
829                stn2->name->sflags |= BIT(SFLAGS_SURFACE);
830             } else {
831                stn1->name->sflags |= BIT(SFLAGS_UNDERGROUND);
832                stn2->name->sflags |= BIT(SFLAGS_UNDERGROUND);
833             }
834         }
835         if (!(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) {
836            SVX_ASSERT(!fZeros(&leg->v));
837            if (leg->meta) {
838                pimg->days1 = leg->meta->days1;
839                pimg->days2 = leg->meta->days2;
840            } else {
841                pimg->days1 = pimg->days2 = -1;
842            }
843            pimg->style = (leg->l.flags >> FLAGS_STYLE_BIT0) & 0x07;
844            img_write_item(pimg, img_LINE, leg->l.flags & FLAGS_MASK,
845                           sprint_prefix(leg_pfx),
846                           POS(stn2, 0), POS(stn2, 1), POS(stn2, 2));
847         }
848
849         /* stop if not 2 node */
850         if (!two_node(stn2)) break;
851
852         stn1 = stn2;
853         i = j ^ 1; /* flip direction for other leg of 2 node */
854      }
855
856      ptrOld = ptrTrail;
857      ptrTrail = ptrTrail->next;
858      osfree(ptrOld);
859   }
860
861   /* write out connections with no survey data */
862   while (nosurveyhead) {
863      nosurveylink *p = nosurveyhead;
864      SVX_ASSERT(fixed(p->fr));
865      SVX_ASSERT(fixed(p->to));
866      if (TSTBIT(p->flags, FLAGS_SURFACE)) {
867         p->fr->name->sflags |= BIT(SFLAGS_SURFACE);
868         p->to->name->sflags |= BIT(SFLAGS_SURFACE);
869      } else {
870         p->fr->name->sflags |= BIT(SFLAGS_UNDERGROUND);
871         p->to->name->sflags |= BIT(SFLAGS_UNDERGROUND);
872      }
873      img_write_item(pimg, img_MOVE, 0, NULL,
874                     POS(p->fr, 0), POS(p->fr, 1), POS(p->fr, 2));
875      if (p->meta) {
876          pimg->days1 = p->meta->days1;
877          pimg->days2 = p->meta->days2;
878      } else {
879          pimg->days1 = pimg->days2 = -1;
880      }
881      pimg->style = img_STYLE_NOSURVEY;
882      img_write_item(pimg, img_LINE, (p->flags & FLAGS_MASK),
883                     sprint_prefix(p->fr->name->up),
884                     POS(p->to, 0), POS(p->to, 1), POS(p->to, 2));
885      nosurveyhead = p->next;
886      osfree(p);
887   }
888
889   /* write stations to .3d file and free legs and stations */
890   FOR_EACH_STN(stn1, stnlist) {
891      int d;
892      SVX_ASSERT(fixed(stn1));
893      if (stn1->name->stn == stn1) {
894         int sf = stn1->name->sflags;
895         /* take care of unused fixed points */
896         if (!TSTBIT(sf, SFLAGS_SOLVED)) {
897            const char * label = NULL;
898            if (TSTBIT(sf, SFLAGS_ANON)) {
899               label = "";
900            } else if (stn1->name->ident) {
901               label = sprint_prefix(stn1->name);
902            }
903            if (label) {
904               /* Set flag to stop station being rewritten after *solve. */
905               stn1->name->sflags = sf | BIT(SFLAGS_SOLVED);
906               sf &= SFLAGS_MASK;
907               if (stn1->name->max_export) sf |= BIT(SFLAGS_EXPORTED);
908               img_write_item(pimg, img_LABEL, sf, label,
909                              POS(stn1, 0), POS(stn1, 1), POS(stn1, 2));
910            }
911         }
912      }
913      /* update coords of bounding box, ignoring the base positions
914       * of points fixed with error estimates and only counting stations
915       * in underground surveys.
916       *
917       * NB We don't set SFLAGS_UNDERGROUND for the anchor station for
918       * a point fixed with error estimates, so this test will exclude
919       * those too, which is what we want.
920       */
921      if (TSTBIT(stn1->name->sflags, SFLAGS_UNDERGROUND)) {
922         for (d = 0; d < 3; d++) {
923            if (POS(stn1, d) < min[d]) {
924               min[d] = POS(stn1, d);
925               pfxLo[d] = stn1->name;
926            }
927            if (POS(stn1, d) > max[d]) {
928               max[d] = POS(stn1, d);
929               pfxHi[d] = stn1->name;
930            }
931         }
932      }
933
934      d = stn1->name->shape;
935      if (d <= 1 && !TSTBIT(stn1->name->sflags, SFLAGS_USED)) {
936         bool unused_fixed_point = fFalse;
937         if (d == 0) {
938            /* Unused fixed point without error estimates */
939            unused_fixed_point = fTrue;
940         } else if (stn1->leg[0]) {
941            prefix *pfx = stn1->leg[0]->l.to->name;
942            if (!pfx->ident && !TSTBIT(pfx->sflags, SFLAGS_ANON)) {
943               /* Unused fixed point with error estimates */
944               unused_fixed_point = fTrue;
945            }
946         }
947         if (unused_fixed_point) {
948            /* TRANSLATORS: fixed survey station that is not part of any survey
949             */
950            warning_in_file(stn1->name->filename, stn1->name->line,
951                    /*Unused fixed point “%s”*/73, sprint_prefix(stn1->name));
952         }
953      }
954
955      /* For stations fixed with error estimates, we need to ignore the leg to
956       * the "real" fixed point in the node stats.
957       */
958      if (stn1->leg[0] && !stn1->leg[0]->l.to->name->ident &&
959          !TSTBIT(stn1->leg[0]->l.to->name->sflags, SFLAGS_ANON))
960         stn1->name->shape--;
961
962      for (i = 0; i <= 2; i++) {
963         leg = stn1->leg[i];
964         /* only want to think about forwards legs */
965         if (leg && data_here(leg)) {
966            linkfor *legRev;
967            node *stnB;
968            int iB;
969            stnB = leg->l.to;
970            iB = reverse_leg_dirn(leg);
971            legRev = stnB->leg[iB];
972            SVX_ASSERT2(legRev->l.to == stn1, "leg doesn't reciprocate");
973            SVX_ASSERT(fixed(stn1));
974            if (!(leg->l.flags &
975                  (BIT(FLAGS_DUPLICATE)|BIT(FLAGS_SPLAY)|
976                   BIT(FLAGS_SURFACE)))) {
977               /* check not an equating leg, or one inside an sdfix point */
978               if (!(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) {
979                  totadj += sqrt(sqrd(POS(stnB, 0) - POS(stn1, 0)) +
980                                 sqrd(POS(stnB, 1) - POS(stn1, 1)) +
981                                 sqrd(POS(stnB, 2) - POS(stn1, 2)));
982                  total += sqrt(sqrdd(leg->d));
983                  totplan += hypot(leg->d[0], leg->d[1]);
984                  totvert += fabs(leg->d[2]);
985               }
986            }
987            osfree(leg);
988            osfree(legRev);
989            stn1->leg[i] = stnB->leg[iB] = NULL;
990         }
991      }
992   }
993
994   /* The station position is attached to the name, so we leave the names and
995    * positions in place - they can then be picked up if we have a *solve
996    * followed by more data */
997   for (stn1 = stnlist; stn1; stn1 = stn2) {
998      stn2 = stn1->next;
999      stn1->name->stn = NULL;
1000      osfree(stn1);
1001   }
1002   stnlist = NULL;
1003}
1004
1005static void
1006write_passage_models(void)
1007{
1008   lrudlist * psg = model;
1009   while (psg) {
1010      lrudlist * oldp;
1011      lrud * xsect = psg->tube;
1012      int xflags = 0;
1013      while (xsect) {
1014         lrud *oldx;
1015         prefix *pfx;
1016         const char *name;
1017         pimg->l = xsect->l;
1018         pimg->r = xsect->r;
1019         pimg->u = xsect->u;
1020         pimg->d = xsect->d;
1021         if (xsect->meta) {
1022             pimg->days1 = xsect->meta->days1;
1023             pimg->days2 = xsect->meta->days2;
1024         } else {
1025             pimg->days1 = pimg->days2 = -1;
1026         }
1027
1028         pfx = xsect->stn;
1029         name = sprint_prefix(pfx);
1030         oldx = xsect;
1031         xsect = xsect->next;
1032         osfree(oldx);
1033
1034         if (!pfx->pos) {
1035             /* TRANSLATORS: e.g. the user specifies a passage cross-section at
1036              * station "entrance.27", but there is no station "entrance.27" in
1037              * the centre-line. */
1038             error_in_file(pfx->filename, pfx->line,
1039                           /*Cross section specified at non-existent station “%s”*/83,
1040                           name);
1041         } else {
1042             if (xsect == NULL) xflags = img_XFLAG_END;
1043             img_write_item(pimg, img_XSECT, xflags, name, 0, 0, 0);
1044         }
1045      }
1046      oldp = psg;
1047      psg = psg->next;
1048      osfree(oldp);
1049   }
1050   model = NULL;
1051}
Note: See TracBrowser for help on using the repository browser.