1 /***************************************************************************
2  *
3  * Project:  OpenCPN
4  * Purpose:  S52 Presentation Library
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 #ifndef _S52PLIB_H_
27 #define _S52PLIB_H_
28 
29 #include <vector>
30 
31 #include "s52s57.h"                 //types
32 
33 class wxGLContext;
34 
35 #include "LLRegion.h"
36 #include "ocpn_types.h"
37 #include "DepthFont.h"
38 
39 #include <wx/dcgraph.h>         // supplemental, for Mac
40 
41 //    wxWindows Hash Map Declarations
42 #include <wx/hashmap.h>
43 class RuleHash;
44 
45 WX_DECLARE_HASH_MAP( wxString, Rule*, wxStringHash, wxStringEqual, RuleHash );
46 WX_DECLARE_HASH_MAP( int, wxString, wxIntegerHash, wxIntegerEqual, MyNatsurHash );
47 
48 WX_DEFINE_SORTED_ARRAY( LUPrec *, wxArrayOfLUPrec );
49 
50 WX_DECLARE_LIST( S52_TextC, TextObjList );
51 
52 struct CARC_Buffer {
53     unsigned char color[3][4];
54     float line_width[3];
55     int steps;
56 
57     int size;
58     float *data;
59 };
60 WX_DECLARE_STRING_HASH_MAP( CARC_Buffer, CARC_Hash );
61 WX_DECLARE_STRING_HASH_MAP( int, CARC_DL_Hash );
62 
63 class ViewPort;
64 class PixelCache;
65 
66 class RenderFromHPGL;
67 class TexFont;
68 
69 class noshow_element
70 {
71 public:
72     char obj[7];
73 };
74 
75 WX_DECLARE_OBJARRAY(noshow_element, ArrayOfNoshow);
76 
77 //-----------------------------------------------------------------------------
78 //      LUP Array container, and friends
79 //-----------------------------------------------------------------------------
80 typedef struct _LUPHashIndex {
81     int n_start;
82     int count;
83 } LUPHashIndex;
84 
85 WX_DECLARE_STRING_HASH_MAP( LUPHashIndex*, LUPArrayIndexHash );
86 
87 class LUPArrayContainer {
88 public:
89     LUPArrayContainer();
90     ~LUPArrayContainer();
91 
GetLUPArray(void)92     wxArrayOfLUPrec     *GetLUPArray(void){ return LUPArray; }
93     LUPHashIndex        *GetArrayIndexHelper( const char *objectName );
94 
95 private:
96     wxArrayOfLUPrec             *LUPArray;          // Sorted Array
97     LUPArrayIndexHash           IndexHash;
98 };
99 
100 
101 //-----------------------------------------------------------------------------
102 //    s52plib definition
103 //-----------------------------------------------------------------------------
104 
105 class s52plib {
106 public:
107      s52plib( const wxString& PLib, bool b_forceLegacy = false );
108     ~s52plib();
109 
110     void SetPPMM( float ppmm );
GetPPMM()111     float GetPPMM() { return canvas_pix_per_mm; }
112 
113     void SetOCPNVersion(int major, int minor, int patch);
114 
GetRVScaleFactor()115     double GetRVScaleFactor() { return m_rv_scale_factor; }
116 
117     LUPrec *S52_LUPLookup( LUPname LUP_name, const char * objectName,
118         S57Obj *pObj, bool bStrict = 0 );
119     int _LUP2rules( LUPrec *LUP, S57Obj *pObj );
120     S52color* getColor( const char *colorName );
121     wxColour getwxColour( const wxString &colorName );
122 
123     void UpdateMarinerParams( void );
124     void ClearCNSYLUPArray( void );
125 
126     void GenerateStateHash();
GetStateHash()127     long GetStateHash() { return m_state_hash;  }
128 
129     void SetPLIBColorScheme( wxString scheme );
130     void SetPLIBColorScheme( ColorScheme cs );
GetPLIBColorScheme(void)131     wxString GetPLIBColorScheme( void ) { return m_ColorScheme; }
132 
133     void SetGLRendererString(const wxString &renderer);
134     void SetGLOptions(bool b_useStencil,
135                       bool b_useStencilAP,
136                       bool b_useScissors,
137                       bool b_useFBO,
138                       bool b_useVBO,
139                       int  nTextureFormat);
140 
141     bool ObjectRenderCheck( ObjRazRules *rzRules, ViewPort *vp );
142     bool ObjectRenderCheckRules( ObjRazRules *rzRules, ViewPort *vp, bool check_noshow = false );
143     bool ObjectRenderCheckPos( ObjRazRules *rzRules, ViewPort *vp );
144     bool ObjectRenderCheckCat( ObjRazRules *rzRules, ViewPort *vp );
145     bool ObjectRenderCheckCS( ObjRazRules *rzRules, ViewPort *vp );
146 
147     static void DestroyLUP( LUPrec *pLUP );
148     static void ClearRulesCache( Rule *pR );
149     DisCat findLUPDisCat(const char *objectName, LUPname TNAM);
150 
151 //    Temporarily save/restore the current colortable index
152 //    Useful for Thumbnail rendering
SaveColorScheme(void)153     void SaveColorScheme( void ) { m_colortable_index_save = m_colortable_index;}
RestoreColorScheme(void)154     void RestoreColorScheme( void ) {}
155 
156 //    Rendering stuff
157     void PrepareForRender( ViewPort *vp );
158     void PrepareForRender( void );
159     void AdjustTextList( int dx, int dy, int screenw, int screenh );
160     void ClearTextList( void );
161     int SetLineFeaturePriority( ObjRazRules *rzRules, int npriority );
162     void FlushSymbolCaches();
163 
164     //    For DC's
165     int RenderObjectToDC( wxDC *pdc, ObjRazRules *rzRules, ViewPort *vp );
166     int RenderObjectToDCText( wxDC *pdc, ObjRazRules *rzRules, ViewPort *vp );
167     int RenderAreaToDC( wxDC *pdc, ObjRazRules *rzRules, ViewPort *vp, render_canvas_parms *pb_spec );
168 
169     // Accessors
GetShowSoundings()170     bool GetShowSoundings() { return m_bShowSoundg; }
SetShowSoundings(bool f)171     void SetShowSoundings( bool f ) { m_bShowSoundg = f; GenerateStateHash(); }
172 
GetShowS57Text()173     bool GetShowS57Text() { return m_bShowS57Text;  }
SetShowS57Text(bool f)174     void SetShowS57Text( bool f ) { m_bShowS57Text = f;  GenerateStateHash(); }
175 
GetShowS57ImportantTextOnly()176     bool GetShowS57ImportantTextOnly() { return m_bShowS57ImportantTextOnly; }
SetShowS57ImportantTextOnly(bool f)177     void SetShowS57ImportantTextOnly( bool f ) { m_bShowS57ImportantTextOnly = f; GenerateStateHash(); }
178 
SetLightsOff(bool val)179     void SetLightsOff(bool val){ m_lightsOff = val; }
GetLightsOff()180     bool GetLightsOff(){ return m_lightsOff; }
181 
182     void SetAnchorOn(bool val);
183     bool GetAnchorOn();
184 
185     void SetQualityOfData(bool val);
186     bool GetQualityOfData();
187 
GetMajorVersion(void)188     int GetMajorVersion( void ) { return m_VersionMajor; }
GetMinorVersion(void)189     int GetMinorVersion( void ) { return m_VersionMinor; }
190 
SetTextOverlapAvoid(bool f)191     void SetTextOverlapAvoid( bool f ) { m_bDeClutterText = f; }
SetShowNationalText(bool f)192     void SetShowNationalText( bool f ) { m_bShowNationalTexts = f; }
SetShowAtonText(bool f)193     void SetShowAtonText( bool f ) { m_bShowAtonText = f; }
SetShowLdisText(bool f)194     void SetShowLdisText( bool f ) { m_bShowLdisText = f; }
SetExtendLightSectors(bool f)195     void SetExtendLightSectors( bool f ) { m_bExtendLightSectors = f; }
196 
197     void SetDisplayCategory( enum _DisCat cat );
GetDisplayCategory()198     DisCat GetDisplayCategory(){ return m_nDisplayCategory; }
199 
SetGLPolygonSmoothing(bool bset)200     void SetGLPolygonSmoothing( bool bset ){ m_GLPolygonSmoothing = bset;}
GetGLPolygonSmoothing()201     bool GetGLPolygonSmoothing( ){ return m_GLPolygonSmoothing; }
SetGLLineSmoothing(bool bset)202     void SetGLLineSmoothing( bool bset ){ m_GLLineSmoothing = bset;}
GetGLLineSmoothing()203     bool GetGLLineSmoothing( ){ return m_GLLineSmoothing; }
204 
205     wxArrayOfLUPrec* SelectLUPARRAY( LUPname TNAM );
206     LUPArrayContainer *SelectLUPArrayContainer( LUPname TNAM );
207 
208     void DestroyPatternRuleNode( Rule *pR );
209     void DestroyRuleNode( Rule *pR );
210     static void DestroyRulesChain( Rules *top );
211 
212     //    For OpenGL
213     int RenderObjectToGL( const wxGLContext &glcc, ObjRazRules *rzRules, ViewPort *vp );
214     int RenderAreaToGL( const wxGLContext &glcc, ObjRazRules *rzRules, ViewPort *vp );
215     int RenderObjectToGLText( const wxGLContext &glcc, ObjRazRules *rzRules, ViewPort *vp );
216 
217     void RenderPolytessGL( ObjRazRules *rzRules, ViewPort *vp,double z_clip_geom, wxPoint *ptp );
218 
219     bool EnableGLLS(bool benable);
220 
221     bool IsObjNoshow( const char *objcl);
222     void AddObjNoshow( const char *objcl);
223     void RemoveObjNoshow( const char *objcl);
224     void ClearNoshow(void);
SaveObjNoshow()225     void SaveObjNoshow() { m_saved_noshow = m_noshow_array; };
RestoreObjNoshow()226     void RestoreObjNoshow() { m_noshow_array = m_saved_noshow; };
227 
228     //Todo accessors
229     LUPname m_nSymbolStyle;
230     LUPname m_nBoundaryStyle;
231     bool m_bOK;
232 
233     bool m_bShowSoundg;
234     bool m_bShowMeta;
235     bool m_bShowS57Text;
236     bool m_bUseSCAMIN;
237     bool m_bShowAtonText;
238     bool m_bShowLdisText;
239     bool m_bExtendLightSectors;
240     bool m_bShowS57ImportantTextOnly;
241     bool m_bDeClutterText;
242     bool m_bShowNationalTexts;
243     int m_nSoundingFactor;
244     double m_SoundingsScaleFactor;
245 
246     int m_VersionMajor;
247     int m_VersionMinor;
248 
249     int m_nDepthUnitDisplay;
250 
251     //    Library data
252     wxArrayPtrVoid *pAlloc;
253 
254     RuleHash *_line_sym; // line symbolisation rules
255     RuleHash *_patt_sym; // pattern symbolisation rules
256     RuleHash *_cond_sym; // conditional symbolisation rules
257     RuleHash *_symb_symR; // symbol symbolisation rules, Raster
258 
259     LUPArrayContainer   *line_LAC;
260     LUPArrayContainer   *areaPlain_LAC;
261     LUPArrayContainer   *areaSymbol_LAC;
262     LUPArrayContainer   *pointSimple_LAC;
263     LUPArrayContainer   *pointPaper_LAC;
264 
265     wxArrayOfLUPrec *condSymbolLUPArray; // Dynamic Conditional Symbology
266 
267     wxArrayPtrVoid *pOBJLArray; // Used for Display Filtering
268     std::vector<wxString> OBJLDescriptions;
269 
270     RuleHash *_symb_sym; // symbol symbolisation rules
271     MyNatsurHash m_natsur_hash;     // hash table for cacheing NATSUR string values from int attributes
272 
273     wxRect m_last_clip_rect;
274     int m_myConfig;
275 
276     double lastLightLat;
277     double lastLightLon;
278 
279 private:
280     int S52_load_Plib( const wxString& PLib, bool b_forceLegacy );
281     bool S52_flush_Plib();
282 
283     void PLIB_LoadS57Config();
284 
285     bool PreloadOBJLFromCSV(const wxString &csv_file);
286 
287     int DoRenderObject( wxDC *pdcin, ObjRazRules *rzRules, ViewPort *vp );
288     int DoRenderObjectTextOnly( wxDC *pdcin, ObjRazRules *rzRules, ViewPort *vp );
289 
290     //    Area Renderers
291     int RenderToBufferAC( ObjRazRules *rzRules, Rules *rules, ViewPort *vp,
292         render_canvas_parms *pb_spec );
293     int RenderToBufferAP( ObjRazRules *rzRules, Rules *rules, ViewPort *vp,
294 	render_canvas_parms *pb_spec );
295     int RenderToGLAC( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
296     int RenderToGLAP( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
297     int RenderToGLAP_GLSL( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
298 
299     //    Object Renderers
300     int RenderTX( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
301     int RenderTE( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
302     int RenderSY( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
303     int RenderLS( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
304     int RenderLC( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
305     int RenderMPS( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
306     int RenderCARC( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
307     char *RenderCS( ObjRazRules *rzRules, Rules *rules );
308     int RenderGLLS( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
309     int RenderGLLC( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
310 
311     int RenderCARC_VBO( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
312     int RenderCARC_GLSL( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
313 
314     void UpdateOBJLArray( S57Obj *obj );
315 
316     int reduceLOD( double LOD_meters, int nPoints, double *source, wxPoint2DDouble **dest, int *maskIn, int **maskOut);
317 
318     int RenderLSLegacy( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
319     int RenderLCLegacy( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
320     int RenderGLLSLegacy( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
321     int RenderGLLCLegacy( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
322     int RenderLSPlugIn( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
323     int RenderLCPlugIn( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
324 
325     int RenderLS_Dash_GLSL( ObjRazRules *rzRules, Rules *rules, ViewPort *vp );
326 
327     void DrawDashLine( wxPen &pen, wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, ViewPort *vp);
328 
329     render_canvas_parms* CreatePatternBufferSpec( ObjRazRules *rzRules,
330         Rules *rules, ViewPort *vp, bool b_revrgb, bool b_pot = false );
331 
332     void RenderToBufferFilledPolygon( ObjRazRules *rzRules, S57Obj *obj,
333         S52color *c, render_canvas_parms *pb_spec,
334         render_canvas_parms *patt_spec, ViewPort *vp );
335 
336     void draw_lc_poly( wxDC *pdc, wxColor &color, int width, wxPoint *ptp, int *mask,
337         int npt, float sym_len, float sym_factor, Rule *draw_rule,
338         ViewPort *vp );
339 
340     bool RenderHPGL( ObjRazRules *rzRules, Rule * rule_in, wxPoint &r,
341         ViewPort *vp, float rot_angle = 0. );
342     bool RenderRasterSymbol( ObjRazRules *rzRules, Rule *prule, wxPoint &r,
343         ViewPort *vp, float rot_angle = 0. );
344     bool RenderSoundingSymbol( ObjRazRules *rzRules, Rule *prule, wxPoint &r,
345         ViewPort *vp, wxColor symColor, float rot_angle = 0. );
346     wxImage RuleXBMToImage( Rule *prule );
347 
348     bool RenderText( wxDC *pdc, S52_TextC *ptext, int x, int y,
349         wxRect *pRectDrawn, S57Obj *pobj, bool bCheckOverlap, ViewPort *vp );
350 
351     bool CheckTextRectList( const wxRect &test_rect, S52_TextC *ptext );
352     int RenderT_All( ObjRazRules *rzRules, Rules *rules, ViewPort *vp,	bool bTX );
353 
354     int PrioritizeLineFeature( ObjRazRules *rzRules, int npriority );
355 
356     int dda_tri( wxPoint *ptp, S52color *c, render_canvas_parms *pb_spec,
357         render_canvas_parms *pPatt_spec );
358     int dda_trap( wxPoint *segs, int lseg, int rseg, int ytop, int ybot,
359         S52color *c, render_canvas_parms *pb_spec, render_canvas_parms *pPatt_spec );
360 
361     LUPrec *FindBestLUP( wxArrayOfLUPrec *LUPArray, unsigned int startIndex, unsigned int count,
362                               S57Obj *pObj, bool bStrict );
363 
364     void SetGLClipRect(const ViewPort &vp, const wxRect &rect);
365 
366     char *_getParamVal( ObjRazRules *rzRules, char *str, char *buf, int bsz );
367     S52_TextC *S52_PL_parseTX( ObjRazRules *rzRules, Rules *rules, char *cmd );
368     char *_parseTEXT( ObjRazRules *rzRules, S52_TextC *text, char *str0 );
369     S52_TextC *S52_PL_parseTE( ObjRazRules *rzRules, Rules *rules, char *cmd );
370 
371 
372     Rules *StringToRules( const wxString& str_in );
373     void GetAndAddCSRules( ObjRazRules *rzRules, Rules *rules );
374 
375     void DestroyPattRules( RuleHash *rh );
376     void DestroyRules( RuleHash *rh );
377     void DestroyLUPArray( wxArrayOfLUPrec *pLUPArray );
378 
379     bool TextRenderCheck( ObjRazRules *rzRules );
380     bool inter_tri_rect( wxPoint *ptp, render_canvas_parms *pb_spec );
381 
382     bool GetPointPixArray( ObjRazRules *rzRules, wxPoint2DDouble* pd, wxPoint *pp, int nv, ViewPort *vp );
383     bool GetPointPixSingle( ObjRazRules *rzRules, float north, float east, wxPoint *r, ViewPort *vp );
384     void GetPixPointSingle( int pixx, int pixy, double *plat, double *plon, ViewPort *vp );
385     void GetPixPointSingleNoRotate( int pixx, int pixy, double *plat, double *plon, ViewPort *vpt );
386 
387     wxString m_plib_file;
388 
389     float canvas_pix_per_mm; // Set by parent, used to scale symbols/lines/patterns
390     double m_rv_scale_factor;
391     float m_display_size_mm;
392 
393     S52color m_unused_color;
394     wxColor m_unused_wxColor;
395 
396     bool bUseRasterSym;
397     bool useLegacyRaster;
398 
399     wxDC *m_pdc; // The current DC
400 
401 //#ifdef ocpnUSE_GL
402     wxGLContext *m_glcc;
403 //#endif
404 
405     int *ledge;
406     int *redge;
407 
408     int m_colortable_index;
409     int m_colortable_index_save;
410 
411     TextObjList m_textObjList;
412 
413     wxString m_ColorScheme;
414 
415     bool m_lightsOff;
416     bool m_anchorOn;
417     bool m_qualityOfDataOn;
418 
419     long m_state_hash;
420 
421     bool m_txf_ready;
422     int m_txf_avg_char_width;
423     int m_txf_avg_char_height;
424     CARC_Hash m_CARC_hashmap;
425     CARC_DL_Hash m_CARC_DL_hashmap;
426     RenderFromHPGL* HPGL;
427 
428     TexFont *m_txf;
429     DepthFont m_texSoundings;
430     bool m_benableGLLS;
431     DisCat m_nDisplayCategory;
432     ArrayOfNoshow m_noshow_array;
433     ArrayOfNoshow m_saved_noshow;
434 
435     int m_coreVersionMajor;
436     int m_coreVersionMinor;
437     int m_coreVersionPatch;
438 
439     // GL Options, set by core depending on hardware capability
440     bool m_useStencil;
441     bool m_useStencilAP;
442     bool m_useScissors;
443     bool m_useFBO;
444     bool m_useVBO;
445     int  m_TextureFormat;
446     bool m_GLLineSmoothing;
447     bool m_GLPolygonSmoothing;
448     wxFont *m_soundFont;
449 };
450 
451 
452 #define HPGL_FILLED true
453 
454 class RenderFromHPGL {
455 public:
456     RenderFromHPGL( s52plib* plibarg );
457     ~RenderFromHPGL(  );
458 
459     void SetTargetDC( wxDC* pdc );
460     void SetTargetOpenGl();
461 #if wxUSE_GRAPHICS_CONTEXT
462     void SetTargetGCDC( wxGCDC* gdc );
463 #endif
SetVP(ViewPort * pVP)464     void SetVP( ViewPort *pVP ){ m_vp = pVP; }
465     bool Render(char *str, char *col, wxPoint &r, wxPoint &pivot, wxPoint origin, float scale, double rot_angle, bool bSymbol);
getBrush()466     wxBrush *getBrush(){ return brush; }
467 
468     GLUtesselator *m_tobj;
469     int          s_odc_tess_vertex_idx;
470     int          s_odc_tess_vertex_idx_this;
471     int          s_odc_tess_buf_len;
472     GLenum       s_odc_tess_mode;
473     int          s_odc_nvertex;
474     GLfloat     *s_odc_tess_work_buf;
475 
476 private:
477     const char* findColorNameInRef( char colorCode, char* col );
478     void RotatePoint( wxPoint& point, wxPoint origin, double angle );
479     wxPoint ParsePoint( wxString& argument );
480     void SetPen();
481     void Line( wxPoint from, wxPoint to );
482     void Circle( wxPoint center, int radius, bool filled = false );
483     void Polygon();
484 
485     void DrawPolygonTessellated( int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset );
486     void DrawPolygon( int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, float scale, float angle );
487 
488     s52plib* plib;
489     double scaleFactor;
490 
491     wxDC* targetDC;
492 #if wxUSE_GRAPHICS_CONTEXT
493     wxGCDC* targetGCDC;
494 #endif
495 
496     wxColor penColor;
497     wxPen* pen;
498     wxColor brushColor;
499     wxBrush* brush;
500     long penWidth;
501     int transparency;
502 
503     int noPoints;
504     wxPoint polygon[100];
505 
506     float m_currentColor[4];
507 
508     bool renderToDC;
509     bool renderToOpenGl;
510     bool renderToGCDC;
511     ViewPort *m_vp;
512 
513 
514     float *workBuf;
515     size_t workBufSize;
516     unsigned int workBufIndex;
517 
518 };
519 
520 #endif //_S52PLIB_H_
521