1 /******************************************************************************
2  *
3  * Project:  OpenCP
4  * Purpose:  S52 PLIB and S57 Chart data types
5  * Author:   David Register
6  *
7  ***************************************************************************
8  *   Copyright (C) 2010 by David S. Register   *
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program; if not, write to the                         *
22  *   Free Software Foundation, Inc.,                                       *
23  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.         *
24  ***************************************************************************
25  *
26  */
27 
28 
29 #ifndef _S52S57_H_
30 #define _S52S57_H_
31 
32 #include "bbox.h"
33 #include "ocpn_types.h"
34 
35 #include <vector>
36 
37 #define CURRENT_SENC_FORMAT_VERSION  201
38 
39 #define OBJL_NAME_LEN  6
40 
41 //    Fwd Defns
42 class wxArrayOfS57attVal;
43 class OGREnvelope;
44 class OGRGeometry;
45 
46 // name of the addressed look up table set (fifth letter)
47 typedef enum _LUPname{
48     SIMPLIFIED                             = 'L', // points
49     PAPER_CHART                            = 'R', // points
50     LINES                                  = 'S', // lines
51     PLAIN_BOUNDARIES                       = 'N', // areas
52     SYMBOLIZED_BOUNDARIES                  = 'O', // areas
53     LUPNAME_NUM                            = 5
54 }LUPname;
55 
56 
57 
58 
59 // Addressed Object Type
60 typedef enum _Object_t{
61    POINT_T  = 'P',
62    LINES_T  = 'L',
63    AREAS_T  = 'A',
64    OBJ_NUM  = 3         // number of object type
65 }Object_t;
66 
67 
68 // Display Priority
69 typedef enum _DisPrio{
70    PRIO_NODATA          = '0',                  // no data fill area pattern
71    PRIO_GROUP1          = '1',                  // S57 group 1 filled areas
72    PRIO_AREA_1          = '2',                  // superimposed areas
73    PRIO_AREA_2          = '3',                  // superimposed areas also water features
74    PRIO_SYMB_POINT      = '4',                  // point symbol also land features
75    PRIO_SYMB_LINE       = '5',                  // line symbol also restricted areas
76    PRIO_SYMB_AREA       = '6',                  // area symbol also traffic areas
77    PRIO_ROUTEING        = '7',                  // routeing lines
78    PRIO_HAZARDS         = '8',                  // hazards
79    PRIO_MARINERS        = '9',                  // VRM, EBL, own ship
80    PRIO_NUM             = 10                    // number of priority levels
81 
82 }DisPrio;
83 
84 // RADAR Priority
85 typedef enum _RadPrio{
86    RAD_OVER = 'O',            // presentation on top of RADAR
87    RAD_SUPP = 'S',            // presentation suppressed by RADAR
88    RAD_NUM  = 2
89 }RadPrio;
90 
91 //    PixelPtr type flags
92 enum{
93       ID_EMPTY          = 0,
94       ID_wxBitmap,
95       ID_RGBA,
96       ID_GL_PATT_SPEC,
97       ID_RGB_PATT_SPEC,
98       ID_GLIST
99 
100 };
101 
102 // display category type
103 typedef enum _DisCat{
104    DISPLAYBASE          = 'D',            //
105    STANDARD             = 'S',            //
106    OTHER                = 'O',            // O for OTHER
107    MARINERS_STANDARD    = 'M',            // Mariner specified
108    MARINERS_OTHER,                        // value not defined
109    DISP_CAT_NUM,                          // value not defined
110 }DisCat;
111 
112 
113 #define MASK_POINT      1
114 #define MASK_LINE       2
115 #define MASK_AREA       4
116 #define MASK_MPS        8
117 #define MASK_ALL        MASK_POINT + MASK_LINE + MASK_AREA + MASK_MPS
118 
119 
120 
121 typedef enum _Rules_t{
122    RUL_NONE,                        // no rule type (init)
123    RUL_TXT_TX,                      // TX
124    RUL_TXT_TE,                      // TE
125    RUL_SYM_PT,                      // SY
126    RUL_SIM_LN,                      // LS
127    RUL_COM_LN,                      // LC
128    RUL_ARE_CO,                      // AC
129    RUL_ARE_PA,                      // AP
130    RUL_CND_SY,                      // CS
131    RUL_MUL_SG,                      // Multipoint Sounding
132    RUL_ARC_2C                       // Circular Arc, used for sector lights, opencpn private
133 }Rules_t;
134 
135 //-- SYMBOLISATION MODULE STRUCTURE -----------------------------
136 // position parameter:  LINE,       PATTERN, SYMBOL
137 typedef struct _position{
138    union          {int              dummy1,     PAMI,       dummy2;     } minDist;
139    union          {int              dummy1,     PAMA,       dummy2;     } maxDist;
140    union          {int              LICL,       PACL,       SYCL;       } pivot_x;
141    union          {int              LIRW,       PARW,       SYRW;       } pivot_y;
142    union          {int              LIHL,       PAHL,       SYHL;       } bnbox_w;
143    union          {int              LIVL,       PAVL,       SYVL;       } bnbox_h;
144    union          {int              LBXC,       PBXC,       SBXC;       } bnbox_x; // UpLft crnr
145    union          {int              LBXR,       PBXR,       SBXR;       } bnbox_y; // UpLft crnr
146 }position;
147 
148 // rule parameter for : LINE,       PATTERN,    SYMBOL
149 typedef struct _Rule{
150    int            RCID;
151    union          {char             LINM[8],    PANM[8], SYNM[8];       } name;
152    union          {char             dummy,      PADF,       SYDF;       } definition;
153    union          {char             dummy1,     PATP,       dummy2;     } fillType;
154    union          {char             dummy1,     PASP,       dummy2;     } spacing;
155    union          {position   line,       patt,       symb;       } pos;
156    union          {wxString   *LXPO,      *PXPO,      *SXPO;      } exposition;
157    union          {wxString   *dummy,     *PBTM,      *SBTM;      } bitmap;
158    union          {char       *LCRF,      *PCRF,      *SCRF;      } colRef;
159    union          {char       *LVCT,      *PVCT,      *SVCT;      } vector;
160 
161    // Private data
162    int     parm0;                      // always indicates form of pixelPtr
163    int     parm1;                      // integer parameters
164    int     parm2;
165    int     parm3;
166    int     parm4;
167    int     parm5;
168    int     parm6;
169    int     parm7;
170    void    *pixelPtr;                  // opaque pointer
171 
172    }Rule;
173 
174 typedef struct _Rules{
175    Rules_t  ruleType;
176    char    *INSTstr;          // Symbology Instruction string
177    Rule    *razRule;          // rule
178    char    *INST0;            // Head of the entire object Instruction string
179    int     n_sequence;        // sequence number in list, used to identify a particular rule
180    bool    b_private_razRule; // marker indicating that razRule should be free'd on Rules destroy
181    struct _Rules *next;
182 }Rules;
183 
184 
185 // LOOKUP MODULE CLASS
186 
187 class LUPrec{
188 public:
189    int            RCID;             // record identifier
190    char           OBCL[7];          // Name (6 char) '\0' terminated
191    Object_t       FTYP;             // 'A' Area, 'L' Line, 'P' Point
192    DisPrio        DPRI;             // Display Priority
193    RadPrio        RPRI;             // 'O' or 'S', Radar Priority
194    LUPname        TNAM;             // FTYP:  areas, points, lines
195    std::vector<char *> ATTArray;    // Array of LUP Attributes
196    wxString       *INST;            // Instruction Field (rules)
197    DisCat         DISC;             // Display Categorie: D/S/O, DisplayBase, Standard, Other
198    int            LUCM;             // Look-Up Comment (PLib3.x put 'groupes' here,
199                                     // hence 'int', but its a string in the specs)
200    int            nSequence;        // A sequence number, indicating order of encounter in
201                                     //  the PLIB file
202    Rules          *ruleList;        // rasterization rule list
203 };
204 
205 // Conditional Symbology
206 typedef struct _Cond{
207    const char *name;
208    void *(*condInst)(void *param);
209 }Cond;
210 
211 
212 
213 
214 class S52_TextC
215 {
216 public:
217       S52_TextC();
218       ~S52_TextC();
219 
220     wxString   frmtd;       // formated text string
221     char       hjust;
222     char       vjust;
223     char       space;
224     char       style;       // CHARS
225     char       weight;      // CHARS
226     char       width;       // CHARS
227     int        bsize;       // CHARS -body size
228     int        xoffs;       // text offsets, in units of bsize
229     int        yoffs;       //
230     S52color   *pcol;       // pointer to S52colour
231     int        dis;         // display
232     wxFont     *pFont;
233     int        rul_seq_creator;  // sequence number of the Rule creating this object
234     int           RGBA_width;
235     int           RGBA_height;
236     int           rendered_char_height;
237     wxRect      rText;          // rectangle of the text as currently rendered, used for declutter
238     bool        bnat;           // frmtd is National text, UTF-8 encoded
239     bool        bspecial_char;  // frmtd has special ASCII characters, i.e. > 127
240     int         avgCharWidth;
241     int         texobj;
242     int         text_width;
243     int         text_height;
244 
245 };
246 
247 
248 
249 
250 //
251 // WARNING: must be in sync OGRatt_t
252 
253 typedef enum _OGRatt_t{
254    OGR_INT,
255    OGR_INT_LST,
256    OGR_REAL,
257    OGR_REAL_LST,
258    OGR_STR,
259 }OGRatt_t;
260 
261 typedef struct _S57attVal {
262     void * value;
263     OGRatt_t valType;
264 } S57attVal;
265 
266 WX_DEFINE_ARRAY( S57attVal *, wxArrayOfS57attVal );
267 
268 typedef struct _OBJLElement {
269     char OBJLName[OBJL_NAME_LEN];
270     int nViz;
271 } OBJLElement;
272 
273 // OGR primitive type
274 typedef enum _geoPrim_t{
275    GEO_POINT,
276    GEO_LINE,
277    GEO_AREA,
278    GEO_META,
279    GEO_PRIM,            // number of primitive
280 }GeoPrim_t;
281 
282 
283 typedef struct _pt{
284    double x;
285    double y;
286 }pt;
287 
288 
289 //      Fwd References
290 class s57chart;
291 class S57Obj;
292 class OGRFeature;
293 class PolyTessGeo;
294 class line_segment_element;
295 class PI_line_segment_element;
296 
297 typedef struct _chart_context{
298     void                    *m_pvc_hash;
299     void                    *m_pve_hash;
300     double                  ref_lat;
301     double                  ref_lon;
302     wxArrayPtrVoid          *pFloatingATONArray;
303     wxArrayPtrVoid          *pRigidATONArray;
304     s57chart                *chart;
305     double                  safety_contour;
306     float                   *vertex_buffer;
307 
308 }chart_context;
309 
310 
311 
312 class LineGeometryDescriptor{
313 public:
314     double          extent_s_lat;
315     double          extent_n_lat;
316     double          extent_w_lon;
317     double          extent_e_lon;
318     int             indexCount;
319     int *           indexTable;
320 };
321 
322 
323 typedef struct _MultipointGeometryDescriptor{
324     double          extent_s_lat;
325     double          extent_n_lat;
326     double          extent_w_lon;
327     double          extent_e_lon;
328     int             pointCount;
329     void *          pointTable;
330 }MultipointGeometryDescriptor;
331 
332 
333 class S57Obj
334 {
335 public:
336 
337       //  Public Methods
338       S57Obj();
339       ~S57Obj();
340 
341       S57Obj( const char* featureName );
342 
343       wxString GetAttrValueAsString ( const char *attr );
344       int GetAttributeIndex( const char *AttrSeek );
345 
346       bool AddIntegerAttribute( const char *acronym, int val );
347       bool AddIntegerListAttribute( const char *acronym, int *pval, int nValue );
348       bool AddDoubleAttribute( const char *acronym, double val );
349       bool AddDoubleListAttribute( const char *acronym, double *pval, int nValue );
350       bool AddStringAttribute( const char *acronym, char *val );
351 
352       bool SetPointGeometry( double lat, double lon, double ref_lat, double ref_lon);
353       bool SetLineGeometry( LineGeometryDescriptor *pGeo, GeoPrim_t geoType, double ref_lat, double ref_lon);
354       bool SetAreaGeometry( PolyTessGeo *ppg, double ref_lat, double ref_lon);
355       bool SetMultipointGeometry( MultipointGeometryDescriptor *pGeo, double ref_lat, double ref_lon);
356 
357 
358 
359       // Private Methods
360 private:
361       void Init();
362 
363 public:
364       // Instance Data
365       char                    FeatureName[8];
366       GeoPrim_t               Primitive_type;
367 
368       char                    *att_array;
369       wxArrayOfS57attVal      *attVal;
370       int                     n_attr;
371 
372       int                     iOBJL;
373       int                     Index;
374 
375       double                  x;                      // for POINT
376       double                  y;
377       double                  z;
378       int                     npt;                    // number of points as needed by arrays
379 
380       pt                      *geoPt;                 // used for cm93 line feature select check
381 
382       double                  *geoPtz;                // an array[3] for MultiPoint, SM with Z, i.e. depth
383       double                  *geoPtMulti;            // an array[2] for MultiPoint, lat/lon to make bbox
384                                                       // of decomposed points
385       PolyTessGeo             *pPolyTessGeo;
386 
387       LLBBox                  BBObj;                  // lat/lon BBox of the rendered object
388       double                  m_lat;                  // The lat/lon of the object's "reference" point
389       double                  m_lon;
390 
391       Rules                   *CSrules;               // per object conditional symbology
392       int                     bCS_Added;
393 
394       S52_TextC               *FText;
395       int                     bFText_Added;
396       wxRect                  rText;
397 
398       int                     Scamin;                 // SCAMIN attribute decoded during load
399       bool                    bIsClone;
400       int                     nRef;                   // Reference counter, to signal OK for deletion
401       bool                    bIsAton;                // This object is an aid-to-navigation
402       bool                    bIsAssociable;          // This object is DRGARE or DEPARE
403 
404       int                     m_n_lsindex;
405       int                     *m_lsindex_array;
406       int                     m_n_edge_max_points;
407       line_segment_element    *m_ls_list;
408       PI_line_segment_element *m_ls_list_legacy;
409 
410       DisCat                  m_DisplayCat;
411       int                     m_DPRI;                 // display priority, assigned from initial LUP
412                                                       // May be adjusted by CS
413       bool                    m_bcategory_mutable;    //  CS procedure may move this object to a higher category.
414                                                       //  Used as a hint to rendering filter logic
415 
416                                                       // This transform converts from object geometry
417                                                       // to SM coordinates.
418       double                  x_rate;                 // These auxiliary transform coefficients are
419       double                  y_rate;                 // to be used in GetPointPix() and friends
420       double                  x_origin;               // on a per-object basis if necessary
421       double                  y_origin;
422 
423       chart_context           *m_chart_context;       // per-chart constants, carried in each object for convenience
424       int auxParm0;                                   // some per-object auxiliary parameters, used for OpenGL
425       int auxParm1;
426       int auxParm2;
427       int auxParm3;
428 
429       bool                    bBBObj_valid;
430 };
431 
432 typedef std::vector<S57Obj *> S57ObjVector;
433 
434 typedef struct _sm_parms{
435     double easting_vp_center;
436     double northing_vp_center;
437 }sm_parms;
438 
439 
440 WX_DEFINE_ARRAY_PTR(Rules*, ArrayOfRules);
441 
442 typedef struct _mps_container{
443     ArrayOfRules *cs_rules;
444 }mps_container;
445 
446 // object rasterization rules
447 typedef struct _ObjRazRules{
448    LUPrec          *LUP;
449    S57Obj          *obj;
450    sm_parms        *sm_transform_parms;
451    struct _ObjRazRules *child;            // child list, used only for MultiPoint Soundings
452    struct _ObjRazRules *next;
453    struct _mps_container *mps;
454 }ObjRazRules;
455 
456 
457 
458 
459 
460 
461 //----------------------------------------------------------------------------------
462 //          Used for s52 Fast Polygon Renderer
463 //----------------------------------------------------------------------------------
464 class render_canvas_parms
465 {
466 public:
467       render_canvas_parms(void);
468       ~render_canvas_parms(void);
469 
470       unsigned char           *pix_buff;
471       int                     lclip;
472       int                     rclip;
473       int                     pb_pitch;
474       int                     x;
475       int                     y;
476       int                     width;
477       int                     height;
478       int                     w_pot;
479       int                     h_pot;
480       int                     depth;
481       bool                    b_stagger;
482       int                     OGL_tex_name;
483       bool                    b_revrgb;
484 };
485 
486 //----------------------------------------------------------------------------------
487 //          Classes used to create arrays of geometry elements
488 //----------------------------------------------------------------------------------
489 
490 class VE_Element
491 {
492 public:
493       unsigned int index;
494       unsigned int nCount;
495       float      *pPoints;
496       int         max_priority;
497       size_t      vbo_offset;
498       LLBBox      edgeBBox;
499 
500 };
501 
502 class VC_Element
503 {
504 public:
505       unsigned int index;
506       float      *pPoint;
507 };
508 
509 typedef std::vector<VE_Element *> VE_ElementVector;
510 typedef std::vector<VC_Element *> VC_ElementVector;
511 
512 typedef enum
513 {
514     TYPE_CE = 0,
515     TYPE_CC,
516     TYPE_EC,
517     TYPE_EE,
518     TYPE_EE_REV
519 } SegmentType;
520 
521 class connector_segment
522 {
523 public:
524     int vbo_offset;
525     int max_priority_cs;
526     float               cs_lat_avg;                // segment centroid
527     float               cs_lon_avg;
528 
529 };
530 
531 class line_segment_element
532 {
533 public:
534     int                 priority;
535     union{              connector_segment   *pcs;
536     VE_Element          *pedge;
537     };
538     SegmentType         ls_type;
539 
540     line_segment_element *next;
541 };
542 
543 #if 0 //TODO
544 class line_segment_element_legacy
545 {
546 public:
547     size_t              vbo_offset;
548     size_t              n_points;
549     int                 priority;
550     float               lat_max;                // segment bounding box
551     float               lat_min;
552     float               lon_max;
553     float               lon_min;
554     int                 type;
555     void                *private0;
556 
557     line_segment_element *next;
558 };
559 
560 
561 class connector_segment
562 {
563 public:
564     void *start;
565     void *end;
566     SegmentType type;
567     int vbo_offset;
568     int max_priority;
569 };
570 
571 #endif
572 
573 
574 WX_DECLARE_HASH_MAP( unsigned int, VE_Element *, wxIntegerHash, wxIntegerEqual, VE_Hash );
575 WX_DECLARE_HASH_MAP( unsigned int, VC_Element *, wxIntegerHash, wxIntegerEqual, VC_Hash );
576 
577 class connector_key
578 {
579 public:
connector_key()580     connector_key()
581     {
582       memset(k, 0 , sizeof k);
583     }
584 
connector_key(SegmentType t,int a,int b)585     connector_key(SegmentType t, int a, int b)
586     {
587       set(t,a,b);
588     }
589 
set(SegmentType t,int a,int b)590     void set(SegmentType t, int a, int b)
591     {
592       memcpy(k, &a, sizeof a);
593       memcpy(&k[sizeof a], &b, sizeof b);
594       k[sizeof (a) + sizeof (b)] = (unsigned char)t;
595     }
596 
597     unsigned long hash() const;
598 
599     unsigned char k[sizeof(int) + sizeof(int) + sizeof(char)];
600 };
601 
602 class connHash
603 {
604 public:
connHash()605   connHash() { }
operator()606   unsigned long operator()( const connector_key& k ) const
607   { return k.hash(); }
608 
609   connHash& operator=(const connHash&) { return *this; }
610 };
611 
612 // comparison operator
613 class connEqual
614 {
615 public:
connEqual()616 connEqual() { }
operator()617 bool operator()( const connector_key& a, const connector_key& b ) const
618 {
619   return memcmp(a.k, b.k, sizeof b.k) == 0;
620 }
621 
622 connEqual& operator=(const connEqual&) { return *this; }
623 };
624 
625 WX_DECLARE_HASH_MAP( connector_key, connector_segment *, connHash, connEqual, connected_segment_hash );
626 
627 #endif
628