1 /**********************************************************************
2  *
3  * rttopo - topology library
4  * http://git.osgeo.org/gitea/rttopo/librttopo
5  *
6  * rttopo 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  * rttopo 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 rttopo.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright 2013 Nicklas Avén
22  * Copyright 2013 Nicklas Avén
23  *
24  **********************************************************************/
25 
26 
27 
28 /**********************************************************************
29  *
30  * rttopo - topology library
31  * http://git.osgeo.org/gitea/rttopo/librttopo
32  * Copyright 2013 Nicklas Avén
33  *
34  * This is free software; you can redistribute and/or modify it under
35  * the terms of the GNU General Public Licence. See the COPYING file.
36  *
37  **********************************************************************/
38 
39 #include "librttopo_geom_internal.h"
40 #include "rtgeom_log.h"
41 #include <limits.h>
42 #include "bytebuffer.h"
43 
44 /* Maximum number of geometry dimmensions that internal arrays can hold */
45 #define MAX_N_DIMS 4
46 
47 #define MAX_BBOX_SIZE 64
48 #define MAX_SIZE_SIZE 8
49 
50 
51 /**
52 * Header true/false flags
53 */
54 
55 #define FIRST_BYTE_SET_BBOXES(flag, bool)   ((flag) = ((bool) ? (flag) | 0x01 : (flag) & (~0x01)))
56 #define FIRST_BYTE_SET_SIZES(flag, bool)    ((flag) = ((bool) ? (flag) | 0x02 : (flag) & (~0x02)))
57 #define FIRST_BYTE_SET_IDLIST(flag, bool)   ((flag) = ((bool) ? (flag) | 0x04 : (flag) & (~0x04)))
58 #define FIRST_BYTE_SET_EXTENDED(flag, bool) ((flag) = ((bool) ? (flag) | 0x08 : (flag) & (~0x08)))
59 #define FIRST_BYTE_SET_EMPTY(flag, bool)    ((flag) = ((bool) ? (flag) | 0x10 : (flag) & (~0x10)))
60 
61 
62 /**
63 * Macros for manipulating the 'type_precision' int. An int8_t used as follows:
64 * Type 4 bits
65 * Precision 4 bits
66 */
67 
68 #define RTTYPE_PREC_SET_TYPE(flag, type) ((flag) = ((flag) & 0xF0) | (((type) & 0x0F)))
69 #define TYPE_PREC_SET_PREC(flag, prec) ((flag) = ((flag) & 0x0F) | (((prec) & 0x0F) << 4))
70 
71 #define HIGHER_DIM_SET_HASZ(flag, bool) ((flag) = ((bool) ? (flag) | 0x01 : (flag) & (~0x01)))
72 #define HIGHER_DIM_SET_HASM(flag, bool) ((flag) = ((bool) ? (flag) | 0x02 : (flag) & (~0x02)))
73 
74 #define HIGHER_DIM_SET_PRECZ(flag, prec) ((flag) = ((flag) & 0xE3) | (((prec) & 0x07) << 2))
75 #define HIGHER_DIM_SET_PRECM(flag, prec) ((flag) = ((flag) & 0x1F) | (((prec) & 0x07) << 5))
76 
77 typedef struct
78 {
79   /* Options defined at start */
80   uint8_t variant;
81   int8_t prec_xy;
82   int8_t prec_z;
83   int8_t prec_m;
84   float factor[4]; /*What factor to multiply the coordiinates with to get the requested precision*/
85 } TWKB_GLOBALS;
86 
87 typedef struct
88 {
89   uint8_t variant;  /*options that change at runtime*/
90   bytebuffer_t *header_buf;
91   bytebuffer_t *geom_buf;
92   int hasz;
93   int hasm;
94   const int64_t *idlist;
95   int64_t bbox_min[MAX_N_DIMS];
96   int64_t bbox_max[MAX_N_DIMS];
97   int64_t accum_rels[MAX_N_DIMS]; /*Holds the acculmulated relative values*/
98 } TWKB_STATE;
99 
100 static int rtgeom_to_twkb_buf(const RTCTX *ctx, const RTGEOM *geom, TWKB_GLOBALS *global_values, TWKB_STATE *ts);
101 
102 static int rtpoint_to_twkb_buf(const RTCTX *ctx, const RTPOINT *line, TWKB_GLOBALS *global_values, TWKB_STATE *ts);
103 static int rtline_to_twkb_buf(const RTCTX *ctx, const RTLINE *line, TWKB_GLOBALS *global_values, TWKB_STATE *ts);
104 static int rtpoly_to_twkb_buf(const RTCTX *ctx, const RTPOLY *poly, TWKB_GLOBALS *global_values, TWKB_STATE *ts);
105 static int rtcollection_to_twkb_buf(const RTCTX *ctx, const RTCOLLECTION *col, TWKB_GLOBALS *global_values, TWKB_STATE *ts);
106 static int rtgeom_write_to_buffer(const RTCTX *ctx, const RTGEOM *geom, TWKB_GLOBALS *global_values, TWKB_STATE *parent_state);
107 
108