1 /***************************************************************************
2  *
3  * Project:  OpenCPN
4  * Purpose:  S57 Chart Object
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 __S57CHART_H__
27 #define __S57CHART_H__
28 
29 #include <wx/wx.h>
30 #include <wx/progdlg.h>
31 #include "bbox.h"
32 #include "chartbase.h"
33 #include "wx/dir.h"
34 #include "wx/filename.h"
35 #include "wx/file.h"
36 #include "wx/stream.h"
37 #include "wx/wfstream.h"
38 #include "gdal/ogrsf_frmts.h"
39 
40 #include "iso8211.h"
41 
42 #include "gdal/gdal.h"
43 #include "s57RegistrarMgr.h"
44 #include "S57ClassRegistrar.h"
45 #include "S57Light.h"
46 #include "S57Sector.h"
47 #include "s52s57.h"                 //types
48 #include "OCPNRegion.h"
49 #include "ocpndc.h"
50 #include "viewport.h"
51 #include "SencManager.h"
52 #include <memory>
53 
54 class ChartCanvas;
55 // ----------------------------------------------------------------------------
56 // Useful Prototypes
57 // ----------------------------------------------------------------------------
58 
59 
60 // ----------------------------------------------------------------------------
61 // S57 Utility Prototypes
62 // ----------------------------------------------------------------------------
63 extern "C" bool s57_GetChartExtent(const wxString& FullPath, Extent *pext);
64 
65 void s57_DrawExtendedLightSectors( ocpnDC& temp_dc, ViewPort& VPoint, std::vector<s57Sector_t>& sectorlegs );
66 bool s57_CheckExtendedLightSectors( ChartCanvas *cc, int mx, int my, ViewPort& VPoint, std::vector<s57Sector_t>& sectorlegs );
67 
68 //----------------------------------------------------------------------------
69 // Constants
70 //----------------------------------------------------------------------------
71 
72 enum
73 {
74       BUILD_SENC_OK,
75       BUILD_SENC_NOK_RETRY,
76       BUILD_SENC_NOK_PERMANENT,
77       BUILD_SENC_PENDING
78 };
79 
80 //----------------------------------------------------------------------------
81 // Fwd Defns
82 //----------------------------------------------------------------------------
83 
84 class ChartBase;
85 class ViewPort;
86 class ocpnBitmap;
87 class PixelCache;
88 class S57ObjectDesc;
89 class S57Reader;
90 class OGRS57DataSource;
91 class S57ClassRegistrar;
92 class S57Obj;
93 class VE_Element;
94 class VC_Element;
95 class connector_segment;
96 
97 #include <wx/dynarray.h>
98 
99 // Declare the Array of S57Obj
100 WX_DECLARE_OBJARRAY(S57Obj, ArrayOfS57Obj);
101 
102 // And also a list
103 WX_DECLARE_LIST(S57Obj, ListOfS57Obj);
104 
105 
106 WX_DECLARE_LIST(ObjRazRules, ListOfObjRazRules);
107 
108 //----------------------------------------------------------------------------
109 // s57 Chart object class
110 //----------------------------------------------------------------------------
111 class s57chart : public ChartBase
112 {
113 public:
114       s57chart();
115       ~s57chart();
116 
117       virtual InitReturn Init( const wxString& name, ChartInitFlag flags );
118 
119 //    Accessors
120 
121       virtual ThumbData *GetThumbData(int tnx, int tny, float lat, float lon);
GetThumbData()122       virtual ThumbData *GetThumbData() {return pThumbData;}
123       bool UpdateThumbData(double lat, double lon);
124 
GetNativeScale()125       virtual int GetNativeScale(){return m_Chart_Scale;}
126       virtual double GetNormalScaleMin(double canvas_scale_factor, bool b_allow_overzoom);
127       virtual double GetNormalScaleMax(double canvas_scale_factor, int canvas_width);
128 
SetNativeScale(int s)129       void SetNativeScale(int s){m_Chart_Scale = s;}
130 
131       virtual bool RenderRegionViewOnDC(wxMemoryDC& dc, const ViewPort& VPoint, const OCPNRegion &Region);
132       virtual bool RenderOverlayRegionViewOnDC(wxMemoryDC& dc, const ViewPort& VPoint, const OCPNRegion &Region);
133 
134       virtual bool RenderRegionViewOnDCNoText(wxMemoryDC& dc, const ViewPort& VPoint, const OCPNRegion &Region);
135       virtual bool RenderRegionViewOnDCTextOnly(wxMemoryDC& dc, const ViewPort& VPoint, const OCPNRegion &Region);
136 
137       virtual void GetValidCanvasRegion(const ViewPort& VPoint, OCPNRegion *pValidRegion);
138       virtual LLRegion GetValidRegion();
139 
140       virtual void GetPointPix(ObjRazRules *rzRules, float rlat, float rlon, wxPoint *r);
141       virtual void GetPointPix(ObjRazRules *rzRules, wxPoint2DDouble *en, wxPoint *r, int nPoints);
142       virtual void GetPixPoint(int pixx, int pixy, double *plat, double *plon, ViewPort *vpt);
143 
144       virtual void SetVPParms(const ViewPort &vpt);
145 
146       virtual bool AdjustVP(ViewPort &vp_last, ViewPort &vp_proposed);
147 //      virtual bool IsRenderDelta(ViewPort &vp_last, ViewPort &vp_proposed);
148 
GetNearestPreferredScalePPM(double target_scale_ppm)149       virtual double GetNearestPreferredScalePPM(double target_scale_ppm){ return target_scale_ppm; }
150 
151       void SetFullExtent(Extent& ext);
152       bool GetChartExtent(Extent *pext);
153 
154       void SetColorScheme(ColorScheme cs, bool bApplyImmediate = true);
155       virtual void UpdateLUPs(s57chart *pOwner);
156 
157       int _insertRules(S57Obj *obj, LUPrec *LUP, s57chart *pOwner);
158 
159       virtual ListOfObjRazRules *GetObjRuleListAtLatLon(float lat, float lon, float select_radius,
160                                                         ViewPort *VPoint, int selection_mask = MASK_ALL);
161       bool DoesLatLonSelectObject(float lat, float lon, float select_radius, S57Obj *obj);
162       bool IsPointInObjArea(float lat, float lon, float select_radius, S57Obj *obj);
163       wxString GetObjectAttributeValueAsString( S57Obj *obj, int iatt, wxString curAttrName );
164       static wxString GetAttributeValueAsString( S57attVal *pAttrVal, wxString AttrName );
165       static bool CompareLights( const S57Light* l1, const S57Light* l2 );
166       wxString CreateObjDescriptions( ListOfObjRazRules* rule);
167       static wxString GetAttributeDecode(wxString& att, int ival);
168 
169       int BuildRAZFromSENCFile(const wxString& SENCPath);
170       static void GetChartNameFromTXT(const wxString& FullPath, wxString &Name);
171       wxString buildSENCName( const wxString& name);
172 
173       //    DEPCNT VALDCO array access
174       bool GetNearestSafeContour(double safe_cnt, double &next_safe_cnt);
175 
176       virtual ListOfS57Obj *GetAssociatedObjects(S57Obj *obj);
177 
Get_ve_hash(void)178       virtual VE_Hash&  Get_ve_hash(void){ return m_ve_hash; }
Get_vc_hash(void)179       virtual VC_Hash&  Get_vc_hash(void){ return m_vc_hash; }
180 
181       virtual void ForceEdgePriorityEvaluate(void);
182 
GetLineVertexBuffer(void)183       float *GetLineVertexBuffer( void ){ return m_line_vertex_buffer; }
184 
185       void ClearRenderedTextCache();
186 
GetCalculatedSafetyContour(void)187       double GetCalculatedSafetyContour(void){ return m_next_safe_cnt; }
188 
189       virtual bool RenderRegionViewOnGL(const wxGLContext &glc, const ViewPort& VPoint,
190                                         const OCPNRegion &RectRegion, const LLRegion &Region);
191       virtual bool RenderOverlayRegionViewOnGL(const wxGLContext &glc, const ViewPort& VPoint,
192                                                const OCPNRegion &RectRegion, const LLRegion &Region);
193       virtual bool RenderRegionViewOnGLNoText(const wxGLContext &glc, const ViewPort& VPoint,
194                                         const OCPNRegion &RectRegion, const LLRegion &Region);
195       virtual bool RenderViewOnGLTextOnly(const wxGLContext &glc, const ViewPort& VPoint);
196 
197 // Public data
198 //Todo Accessors here
199       //  Object arrays used by S52PLIB TOPMAR rendering logic
200       wxArrayPtrVoid *pFloatingATONArray;
201       wxArrayPtrVoid *pRigidATONArray;
202 
203       double        ref_lat, ref_lon;             // Common reference point, derived from FullExtent
204       double        m_LOD_meters;
205       Extent        m_FullExtent;
206       bool          m_bExtentSet;
207       bool          m_bLinePrioritySet;
208 
209       //  SM Projection parms, stored as convenience to expedite pixel conversions
210       double    m_easting_vp_center, m_northing_vp_center;
211       double    m_pixx_vp_center, m_pixy_vp_center;
212       double    m_view_scale_ppm;
213 
214       //    Last ViewPort succesfully rendered, stored as an aid to calculating pixel cache address offsets and regions
215       ViewPort    m_last_vp;
216       OCPNRegion    m_last_Region;
217 
IsCacheValid()218       virtual bool IsCacheValid(){ return (pDIB != nullptr); }
219       virtual void InvalidateCache();
220       virtual bool RenderViewOnDC(wxMemoryDC& dc, const ViewPort& VPoint);
221 
222       virtual void ClearDepthContourArray(void);
223       virtual void BuildDepthContourArray(void);
224       int ValidateAndCountUpdates( const wxFileName file000, const wxString CopyDir,
225                                    wxString &LastUpdateDate, bool b_copyfiles);
226       static int GetUpdateFileArray(const wxFileName file000, wxArrayString *UpFiles,
227                                     wxDateTime date000, wxString edtn000 );
228       wxString GetISDT(void);
229       InitReturn PostInit( ChartInitFlag flags, ColorScheme cs );
230 
GetUsageChar(void)231       char GetUsageChar(void){ return m_usage_char; }
232       static bool IsCellOverlayType(const wxString &pFullPath);
233 
234       bool        m_b2pointLUPS;
235       bool        m_b2lineLUPS;
236       bool        m_RAZBuilt;
237 
238       struct _chart_context     *m_this_chart_context;
239 
240       int FindOrCreateSenc( const wxString& name, bool b_progress = true );
DisableBackgroundSENC()241       void DisableBackgroundSENC(){ m_disableBackgroundSENC = true; }
EnableBackgroundSENC()242       void EnableBackgroundSENC(){ m_disableBackgroundSENC = false; }
243 
244       SENCThreadStatus m_SENCthreadStatus;
245 protected:
246       void AssembleLineGeometry( void );
247 
248       ObjRazRules *razRules[PRIO_NUM][LUPNAME_NUM];
249 
250 private:
251       int GetLineFeaturePointArray(S57Obj *obj, void **ret_array);
252       void SetSafetyContour(void);
253 
254       bool DoRenderViewOnDC(wxMemoryDC& dc, const ViewPort& VPoint, RenderTypeEnum option, bool force_new_view);
255 
256       bool DoRenderRegionViewOnDC(wxMemoryDC& dc, const ViewPort& VPoint, const OCPNRegion &Region, bool b_overlay);
257 
258       int DCRenderRect(wxMemoryDC& dcinput, const ViewPort& vp, wxRect *rect);
259       bool DCRenderLPB(wxMemoryDC& dcinput, const ViewPort& vp, wxRect* rect);
260       bool DCRenderText(wxMemoryDC& dcinput, const ViewPort& vp);
261 
262 
263       int BuildSENCFile(const wxString& FullPath000, const wxString& SENCFileName, bool b_progress = true);
264 
265       void SetLinePriorities(void);
266 
267       bool BuildThumbnail(const wxString &bmpname);
268       bool CreateHeaderDataFromENC(void);
269       bool CreateHeaderDataFromSENC(void);
270       bool CreateHeaderDataFromoSENC(void);
271       bool GetBaseFileAttr( const wxString& file000 );
272 
273       void ResetPointBBoxes(const ViewPort &vp_last, const ViewPort &vp_this);
274 
275            //    Access to raw ENC DataSet
276       bool InitENCMinimal( const wxString& FullPath );
277       int GetENCScale();
278       OGRFeature *GetChartFirstM_COVR(int &catcov);
279       OGRFeature *GetChartNextM_COVR(int &catcov);
280 
281       void FreeObjectsAndRules();
282       const char *getName(OGRFeature *feature);
283 
284       bool DoRenderOnGL(const wxGLContext &glc, const ViewPort& VPoint);
285       bool DoRenderOnGLText(const wxGLContext &glc, const ViewPort& VPoint);
286       bool DoRenderRegionViewOnGL(const wxGLContext &glc, const ViewPort& VPoint,
287                                   const OCPNRegion &RectRegion, const LLRegion &Region, bool b_overlay);
288 
289       void BuildLineVBO( void );
290 
291       void ChangeThumbColor(ColorScheme cs);
292       void LoadThumb();
293 
294  // Private Data
295       char        *hdr_buf;
296       char        *mybuf_ptr;
297       int         hdr_len;
298       wxString    m_SENCFileName;
299 
300 
301       wxArrayString *m_tmpup_array;
302       PixelCache   *pDIB;
303 
304       wxBitmap     *m_pCloneBM;
305       wxMask       *m_pMask;
306 
307       bool         bGLUWarningSent;
308 
309       wxBitmap    *m_pDIBThumbDay;
310       wxBitmap    *m_pDIBThumbDim;
311       wxBitmap    *m_pDIBThumbOrphan;
312       bool        m_bneed_new_thumbnail;
313 
314       bool        m_bbase_file_attr_known;
315       wxDateTime  m_date000;                    // extracted from DSID:ISDT
316       wxString    m_edtn000;                    // extracted from DSID:EDTN
317       int         m_nGeoRecords;                // extracted from DSSI:NOGR
318       int         m_native_scale;               // extracted from DSPM:CSCL
319 
320 
321 //  Raw ENC DataSet members
322       OGRS57DataSource *m_pENCDS;
323 
324 //  DEPCNT VALDCO array members
325       int         m_nvaldco;
326       int         m_nvaldco_alloc;
327       double       *m_pvaldco_array;
328 
329 
330       float      *m_line_vertex_buffer;
331       size_t      m_vbo_byte_length;
332 
333       bool        m_blastS57TextRender;
334       wxString    m_lastColorScheme;
335       wxRect      m_last_vprect;
336       long        m_plib_state_hash;
337       bool        m_btex_mem;
338       char        m_usage_char;
339 
340       double      m_next_safe_cnt;
341 
342       int         m_LineVBO_name;
343 
344       VE_Hash     m_ve_hash;
345       VC_Hash     m_vc_hash;
346       std::vector<connector_segment *> m_pcs_vector;
347       std::vector<VE_Element *> m_pve_vector;
348 
349       wxString    m_TempFilePath;
350       bool        m_disableBackgroundSENC;
351 protected:
352       sm_parms    vp_transform;
353 
354 };
355 
356 
357 #endif
358