source: git/src/cavern.h

main
Last change on this file was 8048d405, checked in by Olly Betts <olly@…>, 5 weeks ago

Separate linkcommon reverse and flag bits

We had at least 2 spare bytes of padding, so we can store the reverse
direction separately which seems cleaner (and slightly more efficient
but the difference is so small it's only measureable using cachegrind).

  • Property mode set to 100644
File size: 16.2 KB
Line 
1/* cavern.h
2 * SURVEX Cave surveying software - header file
3 * Copyright (C) 1991-2025 Olly Betts
4 * Copyright (C) 2004 Simeon Warner
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, see
18 * <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef CAVERN_H
22#define CAVERN_H
23
24/* Using covariances increases the memory required somewhat - may be
25 * desirable to disable this for small memory machines */
26
27/* #define NO_COVARIANCES 1 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <ctype.h>
33#include <math.h>
34#include <float.h>
35
36#include <proj.h>
37
38#include "img_for_survex.h"
39#include "str.h"
40#include "useful.h"
41
42typedef double real; /* so we can change the precision used easily */
43#define HUGE_REAL HUGE_VAL
44#define REAL_EPSILON DBL_EPSILON
45
46#define WGS84_DATUM_STRING "EPSG:4326"
47
48#define SPECIAL_EOL             0x0001
49#define SPECIAL_BLANK           0x0002
50#define SPECIAL_KEYWORD         0x0004
51#define SPECIAL_COMMENT         0x0008
52#define SPECIAL_OMIT            0x0010
53#ifndef NO_DEPRECATED
54#define SPECIAL_ROOT            0x0020
55#endif
56#define SPECIAL_SEPARATOR       0x0040
57#define SPECIAL_NAMES           0x0080
58#define SPECIAL_DECIMAL         0x0100
59#define SPECIAL_MINUS           0x0200
60#define SPECIAL_PLUS            0x0400
61#define SPECIAL_OPEN            0x0800
62#define SPECIAL_CLOSE           0x1000
63
64extern char *fnm_output_base;
65extern bool fnm_output_base_is_dir;
66
67extern bool fExportUsed;
68
69extern int current_days_since_1900;
70extern unsigned current_year;
71
72/* Types */
73
74typedef enum {
75   Q_NULL = -1, Q_DEFAULT, Q_POS, Q_PLUMB, Q_LEVEL,
76   Q_GRADIENT, Q_BACKGRADIENT, Q_BEARING, Q_BACKBEARING,
77   Q_LENGTH, Q_BACKLENGTH, Q_DEPTH, Q_DX, Q_DY, Q_DZ, Q_COUNT, Q_DECLINATION,
78   Q_LEFT, Q_RIGHT, Q_UP, Q_DOWN,
79   Q_MAC
80} q_quantity;
81
82typedef enum {
83   INFER_NULL = -1,
84   INFER_EQUATES,
85   INFER_EXPORTS,
86   INFER_PLUMBS,
87   INFER_SUBSURVEYS,
88   /* In Compass DAT files a dummy zero-length leg from a station to itself is
89    * used to provide a place to specify LRUD for the start or end of a
90    * traverse (depending if dimensions are measured at the from or to
91    * station), so we shouldn't warn about equating a station to itself.
92    * This should be set *as well as* INFER_EQUATES.
93    */
94   INFER_EQUATES_SELF_OK
95} infer_what;
96
97/* unsigned long to cope with 16-bit int-s */
98#define BIT(N) (1UL << (N))
99#define BITA(N) (1UL << ((N) - 'a'))
100
101#define TSTBIT(W, N) (((W)>>(N))&1)
102
103/* masks for quantities which are length and angles respectively */
104#define LEN_QMASK (BIT(Q_LENGTH) | BIT(Q_BACKLENGTH) | BIT(Q_DEPTH) |\
105   BIT(Q_DX) | BIT(Q_DY) | BIT(Q_DZ) | BIT(Q_POS) | BIT(Q_COUNT) |\
106   BIT(Q_LEFT) | BIT(Q_RIGHT) | BIT(Q_UP) | BIT(Q_DOWN))
107#define ANG_QMASK (BIT(Q_BEARING) | BIT(Q_BACKBEARING) |\
108   BIT(Q_GRADIENT) | BIT(Q_BACKGRADIENT) | BIT(Q_PLUMB) | BIT(Q_LEVEL) |\
109   BIT(Q_DECLINATION))
110
111/* if you add/change the order, check factor_tab in commands.c */
112typedef enum {
113   UNITS_NULL = -1, UNITS_METRES, UNITS_FEET, UNITS_YARDS,
114   UNITS_DEGS, UNITS_QUADRANTS, UNITS_GRADS, UNITS_PERCENT, UNITS_MINUTES,
115   UNITS_MAC, UNITS_DEPRECATED_ALIAS_FOR_GRADS
116} u_units;
117
118/* don't reorder these values!  They need to match with img.h too */
119typedef enum {
120   FLAGS_NOT = -2, FLAGS_UNKNOWN = -1, FLAGS_SURFACE, FLAGS_DUPLICATE,
121   FLAGS_SPLAY,
122#if 0
123   /* underground, but through rock (e.g. radiolocation).  Want to hide from
124    * plots by default (so not cave) but don't want to include in surface
125    * triangulation nets (so not surface) */
126   FLAGS_SKELETAL, /* FIXME */
127#endif
128   /* Don't need to match img.h: */
129   FLAGS_ANON_ONE_END,
130   FLAGS_IMPLICIT_SPLAY,
131   FLAGS_STYLE_BIT0, FLAGS_STYLE_BIT1, FLAGS_STYLE_BIT2
132} flags;
133
134/* flags are currently stored in an unsigned char */
135static_assert(FLAGS_STYLE_BIT2 <= 7, "FLAGS_* don't fit in a byte");
136
137/* Mask to AND with to get bits to pass to img library. */
138#define FLAGS_MASK \
139    (BIT(FLAGS_SURFACE) | BIT(FLAGS_DUPLICATE) | BIT(FLAGS_SPLAY))
140
141static_assert(BIT(FLAGS_SURFACE) == img_FLAG_SURFACE, "*_SURFACE differ");
142static_assert(BIT(FLAGS_DUPLICATE) == img_FLAG_DUPLICATE, "*_DUPLICATE differ");
143static_assert(BIT(FLAGS_SPLAY) == img_FLAG_SPLAY, "*_SPLAY differ");
144
145typedef enum {
146   /* Don't reorder these values!  They need to match with img.h too. */
147   SFLAGS_SURFACE = 0, SFLAGS_UNDERGROUND, SFLAGS_ENTRANCE, SFLAGS_EXPORTED,
148   SFLAGS_FIXED, SFLAGS_ANON, SFLAGS_WALL,
149   /* These values don't need to match img.h, but mustn't clash. */
150   SFLAGS_HANGING = 9,
151   SFLAGS_USED = 10, // Warn unused fixed point if unset but SFLAGS_FIXED set.
152   SFLAGS_SOLVED = 11,
153   SFLAGS_SUSPECTTYPO = 12,
154   SFLAGS_SURVEY = 13,
155   SFLAGS_PREFIX_ENTERED = 14,
156   // If set, use ident.i; if unset, use ident.p
157   SFLAGS_IDENT_INLINE = 15
158} sflags;
159
160/* Mask to AND with to get bits to pass to img library. */
161#define SFLAGS_MASK (BIT(SFLAGS_SURFACE) | BIT(SFLAGS_UNDERGROUND) |\
162        BIT(SFLAGS_ENTRANCE) | BIT(SFLAGS_EXPORTED) | BIT(SFLAGS_FIXED) |\
163        BIT(SFLAGS_ANON) | BIT(SFLAGS_WALL))
164
165static_assert(BIT(SFLAGS_SURFACE) == img_SFLAG_SURFACE, "*_SURFACE differ");
166static_assert(BIT(SFLAGS_UNDERGROUND) == img_SFLAG_UNDERGROUND, "*_UNDERGROUND differ");
167static_assert(BIT(SFLAGS_ENTRANCE) == img_SFLAG_ENTRANCE, "*_ENTRANCE differ");
168static_assert(BIT(SFLAGS_EXPORTED) == img_SFLAG_EXPORTED, "*_EXPORTED differ");
169static_assert(BIT(SFLAGS_FIXED) == img_SFLAG_FIXED, "*_FIXED differ");
170static_assert(BIT(SFLAGS_ANON) == img_SFLAG_ANON, "*_ANON differ");
171static_assert(BIT(SFLAGS_WALL) == img_SFLAG_WALL, "*_WALL differ");
172
173/* enumeration of field types */
174typedef enum {
175   End = 0, Tape, Comp, Clino, BackTape, BackComp, BackClino,
176   Left, Right, Up, Down,
177   FrDepth, ToDepth, Dx, Dy, Dz, FrCount, ToCount,
178   /* Up to here are readings are allowed multiple values
179    * and have slot in the value[] array in datain.c.
180    * (Depth, DepthChange, and Count can have multiple
181    * readings, but are actually handled using tokens
182    * above rather than as themselves).
183    *
184    * Fr must be the first reading after this comment!
185    */
186   Fr, To, Station, Depth, DepthChange, Count, Dir,
187   Newline, IgnoreAllAndNewLine, Ignore, IgnoreAll,
188   /* IgnoreAll must be the last reading before this comment!
189    *
190    * Readings after this comment are only used in datain.c
191    * so can have enum values >= 32 because we only use a
192    * bitmask for those readings used in commands.c.
193    */
194   CompassDATFr, CompassDATTo,
195   CompassDATComp, CompassDATClino, CompassDATBackComp, CompassDATBackClino,
196   CompassDATLeft, CompassDATRight, CompassDATUp, CompassDATDown,
197   CompassDATFlags,
198
199   WallsSRVFr, WallsSRVTo, WallsSRVTape, WallsSRVComp, WallsSRVClino,
200   WallsSRVFrDepth, WallsSRVToDepth,
201   // Optional pair of readings giving heights above stations on CT surveys.
202   WallsSRVHeights,
203   // Optional delimited LRUD and variance overrides.
204   WallsSRVExtras
205} reading;
206
207static_assert(IgnoreAll < 32, "IgnoreAll doesn't fit in 32-bit mask");
208
209/* position or length vector */
210typedef real delta[3];
211
212/* variance */
213#ifdef NO_COVARIANCES
214typedef real var[3];
215typedef var svar;
216#else
217typedef real var[3][3];
218typedef real svar[6];
219#endif
220
221/* station name */
222typedef struct Prefix {
223   struct Prefix *up, *down, *right;
224   struct Node *stn;
225   struct Pos *pos;
226   union {
227       const char *p;
228       char i[sizeof(const char*)];
229   } ident;
230   // A filename:line:column where this name was used.  If it's a station used
231   // in *fix then this will be the location of such a *fix, otherwise if it's
232   // a station used in *equate then it's the location of such a *equate.
233   // Otherwise it's the first place it was used.  (For a station invented to
234   // do a delta-star transform, this will be NULL.)
235   const char *filename;
236   unsigned int line;
237   unsigned short column;
238   /* stn flags - e.g. surface, underground, entrance
239    * also suspecttypo and survey */
240   unsigned short sflags;
241   /* If (min_export == 0) then max_export is max # levels above is this
242    * prefix is used (and so needs to be exported) (0 == parent only).
243    * If (min_export > 0) then max_export is max # levels above this
244    * prefix has been exported, and min_export is how far down the exports
245    * have got (if min_export > 1 after a run, this prefix hasn't been
246    * exported from below enough).
247    * If INFER_EXPORTS is active when a station is encountered, we
248    * set min_export = USHRT_MAX and max_export gets set as usual.  Then at
249    * the end of the run, we also mark stations with min_export == USHRT_MAX
250    * and max_export > 0 as exported. */
251   unsigned short max_export, min_export;
252} prefix;
253
254static inline const char *prefix_ident(const prefix *p) {
255    return TSTBIT(p->sflags, SFLAGS_IDENT_INLINE) ? p->ident.i : p->ident.p;
256}
257
258/* survey metadata */
259typedef struct Meta_data {
260    size_t ref_count;
261    /* Days since 1900 for start and end date of survey, or -1 if undated. */
262    int days1, days2;
263} meta_data;
264
265/* stuff stored for both forward & reverse legs */
266typedef struct {
267   struct Node *to;
268   // Reverse leg number (0, 1 or 2)
269   unsigned char reverse;
270   // These are "internal" flag bits:
271   // bit 4: FLAG_FAKE (an equate or leg inside an sdfix
272   // bit 5: FLAG_ARTICULATION (i.e. carries no error)
273   // bit 6: FLAG_REPLACEMENTLEG (by reduction rules)
274   // bit 7: FLAG_DATAHERE (i.e. this is a forward leg)
275   unsigned char bits;
276   /* flags - e.g. FLAGS_SURFACE, FLAGS_DUPLICATE.
277    * only used if (FLAG_DATAHERE & !(FLAG_REPLACEMENTLEG|FLAG_FAKE))
278    * This could be only in linkfor, but this is actually more space
279    * efficient.
280    */
281   unsigned char flags;
282} linkcommon;
283
284#define FLAG_DATAHERE 0x80
285#define FLAG_REPLACEMENTLEG 0x40
286#define FLAG_ARTICULATION 0x20
287#define FLAG_FAKE 0x10 /* an equate or leg inside an sdfix */
288#define MASK_REVERSEDIRN 0x03
289
290/* forward leg - deltas & vars stored here */
291typedef struct Link {
292   linkcommon l;
293   delta d; /* Delta */
294   svar v; /* Variances */
295   meta_data *meta;
296} linkfor;
297
298/* node - like a station, except several nodes are used to represent a
299 * station with more than 3 legs connected to it
300 */
301typedef struct Node {
302   struct Prefix *name;
303   struct Link *leg[3];
304   struct Node *prev, *next;
305   // Used in netartic.c to identify unconnected components and articulation
306   // points within components.
307   //
308   // Used in matrix.c to record the matrix row corresponding to this node
309   // or -1 for nodes already fixed (more than one node may map to the same
310   // row).
311   long colour;
312} node;
313
314/* station position */
315typedef struct Pos {
316   // Easting, Northing, Altitude.
317   real p[3];
318} pos;
319
320/*
321typedef struct Inst {
322   real zero, scale, units;
323} inst;
324*/
325
326/* Survey data styles */
327#define STYLE_NORMAL     0
328#define STYLE_DIVING     1
329#define STYLE_CARTESIAN  2
330#define STYLE_CYLPOLAR   3
331#define STYLE_NOSURVEY   4
332#define STYLE_PASSAGE    5
333#define STYLE_IGNORE     6
334
335static_assert(STYLE_NORMAL == img_STYLE_NORMAL, "*_NORMAL differ");
336static_assert(STYLE_DIVING == img_STYLE_DIVING, "*_DIVING differ");
337static_assert(STYLE_CARTESIAN == img_STYLE_CARTESIAN, "*_CARTESIAN differ");
338static_assert(STYLE_CYLPOLAR == img_STYLE_CYLPOLAR, "*_CYLPOLAR differ");
339static_assert(STYLE_NOSURVEY == img_STYLE_NOSURVEY, "*_NOSURVEY differ");
340static_assert(STYLE_NORMAL == img_STYLE_NORMAL, "*_NORMAL differ");
341
342/* various settings preserved by *BEGIN and *END */
343typedef struct Settings {
344   struct Settings *next;
345   unsigned int Truncate;
346   bool f_clino_percent;
347   bool f_backclino_percent;
348   bool f_bearing_quadrants;
349   bool f_backbearing_quadrants;
350   bool dash_for_anon_wall_station;
351   bool from_equals_to_is_only_a_warning;
352   unsigned char infer;
353   enum {OFF, LOWER, UPPER} Case;
354   /* STYLE_xxx value to process data as. */
355   int style;
356   /* STYLE_xxx value to put in 3d file (different for Compass DAT diving
357    * data, as the data in the DAT file is always presented in the format
358    * tape,compass,clino even if that isn't how it was really measured).
359    */
360   int recorded_style;
361   prefix *Prefix;
362   prefix *begin_survey; /* used to check BEGIN and END match */
363   short *Translate; /* if short is >= 16 bits, which ANSI requires */
364   real Var[Q_MAC];
365   real z[Q_MAC];
366   real sc[Q_MAC];
367   real units[Q_MAC];
368   const reading *ordering;
369   long begin_lpos; /* File offset for start of BEGIN line */
370   int begin_lineno; /* 0 means no block started in this file */
371   int begin_col; /* Column of prefix in BEGIN line (or 0 if none) */
372   int flags;
373   char* proj_str;
374   /* Location at which we calculate the declination if
375    * z[Q_DECLINATION] == HUGE_REAL.
376    *
377    * Latitude and longitude are in radians; altitude is in metres above the
378    * ellipsoid.
379    */
380   real dec_lat, dec_lon, dec_alt;
381   /* Cached auto-declination in radians, or HUGE_REAL for no cached value.
382    * Only meaningful if days1 != -1.
383    */
384   real declination;
385   double min_declination, max_declination;
386   int min_declination_days, max_declination_days;
387   const char* dec_filename;
388   int dec_line;
389   /* Copy of the text of the `*declination auto ...` line (malloced). */
390   char* dec_context;
391   /* Grid convergence in radians. */
392   real convergence;
393   /* Input grid convergence in radians. */
394   real input_convergence;
395   /* Rotation from North for `*data cartesian`. */
396   real cartesian_rotation;
397   /* Which North to use for `*data cartesian`. */
398   enum { TRUE_NORTH, GRID_NORTH, MAGNETIC_NORTH } cartesian_north;
399   meta_data * meta;
400} settings;
401
402/* global variables */
403extern settings *pcs;
404extern prefix *root;
405extern prefix *anon_list;
406extern node *fixedlist;
407extern node *stnlist;
408extern unsigned long optimize;
409extern char * proj_str_out;
410extern PJ * pj_cached;
411
412extern string survey_title;
413
414extern bool fExplicitTitle;
415extern long cLegs, cStns, cComponents, cSolves;
416extern FILE *fhErrStat;
417extern img *pimg;
418extern real totadj, total, totplan, totvert;
419extern real min[9], max[9];
420extern prefix *pfxHi[9], *pfxLo[9];
421extern int quiet; // 1 to turn off progress messages; >=2 turns off summary too.
422extern bool fSuppress; /* only output 3d file */
423
424/* macros */
425
426#define POS(S, D) ((S)->name->pos->p[(D)])
427#define POSD(S) ((S)->name->pos->p)
428
429#define data_here(L) ((L)->l.bits & FLAG_DATAHERE)
430#define reverse_leg_dirn(L) ((L)->l.reverse)
431#define reverse_leg(L) ((L)->l.to->leg[reverse_leg_dirn(L)])
432
433/* if p[0]==UNFIXED_VAL, station is unfixed */
434#define UNFIXED_VAL HUGE_VAL
435#define pfx_fixed(N) ((N)->pos->p[0] != UNFIXED_VAL)
436#define pos_fixed(P) ((P)->p[0] != UNFIXED_VAL)
437#define unfix(S) POS((S), 0) = UNFIXED_VAL
438#define fixed(S) pfx_fixed((S)->name)
439
440/* macros for special chars */
441
442#define isEol(c)    (pcs->Translate[(c)] & SPECIAL_EOL)
443#define isBlank(c)  (pcs->Translate[(c)] & SPECIAL_BLANK)
444#define isKeywd(c)  (pcs->Translate[(c)] & SPECIAL_KEYWORD)
445#define isComm(c)   (pcs->Translate[(c)] & SPECIAL_COMMENT)
446#define isOmit(c)   (pcs->Translate[(c)] & SPECIAL_OMIT)
447#ifndef NO_DEPRECATED
448#define isRoot(c)   (pcs->Translate[(c)] & SPECIAL_ROOT)
449#endif
450#define isSep(c)    (pcs->Translate[(c)] & SPECIAL_SEPARATOR)
451#define isNames(c)  (pcs->Translate[(c)] & SPECIAL_NAMES)
452#define isDecimal(c) (pcs->Translate[(c)] & SPECIAL_DECIMAL)
453#define isMinus(c)  (pcs->Translate[(c)] & SPECIAL_MINUS)
454#define isPlus(c)   (pcs->Translate[(c)] & SPECIAL_PLUS)
455#define isOpen(c)   (pcs->Translate[(c)] & SPECIAL_OPEN)
456#define isClose(c)  (pcs->Translate[(c)] & SPECIAL_CLOSE)
457
458#define isSign(c)   (pcs->Translate[(c)] & (SPECIAL_PLUS | SPECIAL_MINUS))
459#define isData(c)   (pcs->Translate[(c)] & (SPECIAL_OMIT | SPECIAL_ROOT|\
460   SPECIAL_SEPARATOR | SPECIAL_NAMES | SPECIAL_DECIMAL | SPECIAL_PLUS |\
461   SPECIAL_MINUS))
462
463typedef struct nosurveylink {
464   node *fr, *to;
465   int flags;
466   meta_data *meta;
467   struct nosurveylink *next;
468} nosurveylink;
469
470extern nosurveylink *nosurveyhead;
471
472typedef struct lrud {
473    struct lrud * next;
474    prefix *stn;
475    meta_data *meta;
476    real l, r, u, d;
477} lrud;
478
479typedef struct lrudlist {
480    lrud * tube;
481    struct lrudlist * next;
482} lrudlist;
483
484extern lrudlist * model;
485
486extern lrud ** next_lrud;
487
488extern char output_separator;
489
490#endif /* CAVERN_H */
Note: See TracBrowser for help on using the repository browser.