OpenCPN Partial API docs
cm93.h
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: CM93 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 __CM93CHART_H__
27 #define __CM93CHART_H__
28 
29 #include <wx/listctrl.h> // Somehow missing from wx build
30 
31 #include "s57chart.h"
32 #include "model/cutil.h" // for types
33 #include "poly_math.h"
34 
35 // Some constants
36 #define INDEX_m_sor 217 // cm93 dictionary index for object type _m_sor
37 
38 #define CM93_ZOOM_FACTOR_MAX_RANGE 5
39 
40 // Static functions
41 int Get_CM93_CellIndex(double lat, double lon, int scale);
42 void Get_CM93_Cell_Origin(int cellindex, int scale, double *lat, double *lon);
43 
44 // Fwd definitions
45 class covr_set;
46 class wxSpinCtrl;
47 class ChartCanvas;
48 
49 class M_COVR_Desc {
50 public:
51  M_COVR_Desc();
52  ~M_COVR_Desc();
53 
54  int GetWKBSize();
55  bool WriteWKB(void *p);
56  int ReadWKB(wxFFileInputStream &ifs);
57  void Update(M_COVR_Desc *pmcd);
58  OCPNRegion GetRegion(const ViewPort &vp, wxPoint *pwp);
59 
60  int m_cell_index;
61  int m_object_id;
62  int m_subcell;
63 
64  int m_nvertices;
65  float_2Dpt *pvertices;
66 
67  int m_ngl_vertices;
68  float_2Dpt *gl_screen_vertices;
69  int gl_screen_projection_type;
70 
71  int m_npub_year;
72  double transform_WGS84_offset_x;
73  double transform_WGS84_offset_y;
74  double m_covr_lat_min;
75  double m_covr_lat_max;
76  double m_covr_lon_min;
77  double m_covr_lon_max;
78  double user_xoff;
79  double user_yoff;
80  double m_centerlat_cos;
81 
82  LLBBox m_covr_bbox;
83  bool m_buser_offsets;
84 };
85 
86 WX_DECLARE_OBJARRAY(M_COVR_Desc, Array_Of_M_COVR_Desc);
87 WX_DEFINE_ARRAY_PTR(M_COVR_Desc *, Array_Of_M_COVR_Desc_Ptr);
88 
89 WX_DECLARE_LIST(M_COVR_Desc, List_Of_M_COVR_Desc);
90 
91 // Georeferencing constants
92 
93 // This constant was developed empirically by looking at a
94 // representative cell, comparing the cm93 point transform coefficients
95 // to the stated lat/lon bounding box.
96 // This value corresponds to the semi-major axis for the "International 1924"
97 // geo-standard For WGS84, it should be 6378137.0......
98 
99 static const double CM93_semimajor_axis_meters =
100  6378388.0; // CM93 semimajor axis
101 
102 // CM93 Data structures
103 
104 class Extended_Geometry;
105 
106 //#pragma pack(push,1)
107 
108 typedef struct {
109  unsigned short x;
110  unsigned short y;
111 } cm93_point;
112 
113 typedef struct {
114  unsigned short x;
115  unsigned short y;
116  unsigned short z;
117 } cm93_point_3d;
118 
119 //#pragma pack(pop)
120 
121 typedef struct {
122  double lon_min;
123  double lat_min;
124  double lon_max;
125  double lat_max;
126  // Bounding Box, in Mercator transformed co-ordinates
127  double easting_min;
128  double northing_min;
129  double easting_max;
130  double northing_max;
131 
132  unsigned short usn_vector_records; // number of spacial(vector) records
133  int n_vector_record_points; // number of cm93 points in vector record block
134  int m_46;
135  int m_4a;
136  unsigned short usn_point3d_records;
137  int m_50;
138  int m_54;
139  unsigned short usn_point2d_records; // m_58;
140  unsigned short m_5a;
141  unsigned short m_5c;
142  unsigned short usn_feature_records; // m_5e, number of feature records
143 
144  int m_60;
145  int m_64;
146  unsigned short m_68;
147  unsigned short m_6a;
148  unsigned short m_6c;
149  int m_nrelated_object_pointers;
150 
151  int m_72;
152  unsigned short m_76;
153 
154  int m_78;
155  int m_7c;
156 
157 } header_struct;
158 
159 typedef struct {
160  unsigned short n_points;
161  unsigned short x_min;
162  unsigned short y_min;
163  unsigned short x_max;
164  unsigned short y_max;
165  int index;
166  cm93_point *p_points;
168 
169 typedef struct {
170  geometry_descriptor *pGeom_Description;
171  unsigned char segment_usage;
173 
174 typedef struct {
175  unsigned char otype;
176  unsigned char geotype;
177  unsigned short n_geom_elements;
178  void *pGeometry; // may be a (cm93_point*) or other geom;
179  unsigned char n_related_objects;
180  void *p_related_object_pointer_array;
181  unsigned char n_attributes; // number of attributes
182  unsigned char *attributes_block; // encoded attributes
183 
184 } Object;
185 
186 typedef struct {
187  // Georeferencing transform coefficients
188  double transform_x_rate;
189  double transform_y_rate;
190  double transform_x_origin;
191  double transform_y_origin;
192 
193  cm93_point *p2dpoint_array;
194  Object **pprelated_object_block;
195  unsigned char *attribute_block_top; // attributes block
197  *edge_vector_descriptor_block; // edge vector descriptor block
198  geometry_descriptor *point3d_descriptor_block;
199  cm93_point *pvector_record_block_top;
200  cm93_point_3d *p3dpoint_array;
201 
202  int m_nvector_records;
203  int m_nfeature_records;
204  int m_n_point3d_records;
205  int m_n_point2d_records;
206 
207  List_Of_M_COVR_Desc m_cell_mcovr_list;
208  bool b_have_offsets;
209  bool b_have_user_offsets;
210 
211  double user_xoff;
212  double user_yoff;
213 
214  double min_lat, min_lon;
215 
216  // Allocated working blocks
217  vector_record_descriptor *object_vector_record_descriptor_block;
218  Object *pobject_block;
219 
221 
222 //----------------------------------------------------------------------------
223 // cm93_dictionary class
224 // Encapsulating the conversion between binary cm_93 object class,
225 // attributes, etc to standard S57 text conventions
226 //----------------------------------------------------------------------------
227 
229 public:
230  cm93_dictionary();
231  ~cm93_dictionary();
232 
233  bool LoadDictionary(const wxString &dictionary_dir);
234  bool IsOk(void) { return m_ok; }
235  wxString GetDictDir(void) { return m_dict_dir; }
236 
237  wxString GetClassName(int iclass);
238  wxString GetAttrName(int iattr);
239  char GetAttrType(int iattr);
240 
241 private:
242  int m_max_class;
243  int m_max_attr;
244  wxArrayString *m_S57ClassArray;
245  wxArrayString *m_AttrArray;
246  int *m_GeomTypeArray;
247  char *m_ValTypeArray;
248  bool m_ok;
249  wxString m_dict_dir;
250 };
251 
253 public:
254  cm93_attr_block(void *block, cm93_dictionary *pdict);
255  unsigned char *GetNextAttr();
256 
257  int m_cptr;
258  unsigned char *m_block;
259 
260  cm93_dictionary *m_pDict;
261 };
262 
263 //----------------------------------------------------------------------------
264 // cm93 Chart Manager class
265 //----------------------------------------------------------------------------
266 class cm93manager {
267 public:
268  cm93manager();
269  ~cm93manager();
270  bool Loadcm93Dictionary(const wxString &name);
271  cm93_dictionary *FindAndLoadDict(const wxString &file);
272 
273  cm93_dictionary *m_pcm93Dict;
274 
275  // Member variables used to record the calling of
276  // cm93chart::CreateHeaderDataFromCM93Cell() for each available scale value.
277  // This allows that routine to return quickly with no error for all cells
278  // other than the first, at each scale....
279 
280  bool m_bfoundA;
281  bool m_bfoundB;
282  bool m_bfoundC;
283  bool m_bfoundD;
284  bool m_bfoundE;
285  bool m_bfoundF;
286  bool m_bfoundG;
287  bool m_bfoundZ;
288 };
289 
290 //----------------------------------------------------------------------------
291 // cm93 Chart object class
292 //----------------------------------------------------------------------------
293 class cm93chart : public s57chart {
294 public:
295  cm93chart();
296  ~cm93chart();
297 
298  cm93chart(int scale_index);
299  InitReturn Init(const wxString &name, ChartInitFlag flags);
300 
301  void ResetSubcellKey() { m_loadcell_key = '0'; }
302 
303  double GetNormalScaleMin(double canvas_scale_factor, bool b_allow_overzoom);
304  double GetNormalScaleMax(double canvas_scale_factor, int canvas_width);
305 
306  bool AdjustVP(ViewPort &vp_last, ViewPort &vp_proposed);
307  void SetVPParms(const ViewPort &vpt);
308  void GetPointPix(ObjRazRules *rzRules, float northing, float easting,
309  wxPoint *r);
310  void GetPointPix(ObjRazRules *rzRules, wxPoint2DDouble *en, wxPoint *r,
311  int nPoints);
312  void GetPixPoint(int pixx, int pixy, double *plat, double *plon,
313  ViewPort *vpt);
314 
315  void SetCM93Dict(cm93_dictionary *pDict) { m_pDict = pDict; }
316  void SetCM93Prefix(const wxString &prefix) { m_prefix = prefix; }
317  void SetCM93Manager(cm93manager *pManager) { m_pManager = pManager; }
318 
319  bool UpdateCovrSet(ViewPort *vpt);
320  bool IsPointInLoadedM_COVR(double xc, double yc);
321  covr_set *GetCoverSet() { return m_pcovr_set; }
322  LLRegion GetValidRegion();
323 
324  const wxString &GetLastFileName(void) const { return m_LastFileName; }
325 
326  std::vector<int> GetVPCellArray(const ViewPort &vpt);
327 
328  Array_Of_M_COVR_Desc_Ptr m_pcovr_array_loaded;
329 
330  void SetUserOffsets(int cell_index, int object_id, int subcell, int xoff,
331  int yoff);
332  wxString GetScaleChar() { return m_scalechar; }
333 
334  wxPoint *GetDrawBuffer(int nSize);
335 
336  OCPNRegion m_render_region;
337 
338 private:
339  InitReturn CreateHeaderDataFromCM93Cell(void);
340  int read_header_and_populate_cib(header_struct *ph, Cell_Info_Block *pCIB);
341  Extended_Geometry *BuildGeom(Object *pobject, wxFileOutputStream *postream,
342  int iobject);
343 
344  S57Obj *CreateS57Obj(int cell_index, int iobject, int subcell,
345  Object *pobject, cm93_dictionary *pDict,
346  Extended_Geometry *xgeom, double ref_lat, double ref_lon,
347  double scale, double view_scale_ppm);
348 
349  void ProcessMCOVRObjects(int cell_index, char subcell);
350 
351  void translate_colmar(const wxString &sclass, S57attVal *pattValTmp);
352 
353  int CreateObjChain(int cell_index, int subcell, double view_scale_ppm);
354 
355  void Unload_CM93_Cell(void);
356 
357  // cm93 point manipulation methods
358  void Transform(cm93_point *s, double trans_x, double trans_y, double *lat,
359  double *lon);
360 
361  int loadcell_in_sequence(int, char);
362  int loadsubcell(int, wxChar);
363  void ProcessVectorEdges(void);
364 
365  wxPoint2DDouble FindM_COVROffset(double lat, double lon);
366  M_COVR_Desc *FindM_COVR_InWorkingSet(double lat, double lon);
367 
368  Cell_Info_Block m_CIB;
369 
370  cm93_dictionary *m_pDict;
371  cm93manager *m_pManager;
372 
373  wxString m_prefix;
374 
375  double m_sfactor;
376 
377  wxString m_scalechar;
378  std::vector<int> m_cells_loaded_array;
379 
380  int m_current_cell_vearray_offset;
381  int *m_pcontour_array;
382  int m_ncontour_alloc;
383  ViewPort m_vp_current;
384  wxChar m_loadcell_key;
385  double m_dval;
386 
387  covr_set *m_pcovr_set;
388 
389  wxPoint *m_pDrawBuffer; // shared outline drawing buffer
390  int m_nDrawBufferSize;
391 
392  wxString m_LastFileName;
393 
394  LLRegion m_region;
395  wxArrayString m_noFindArray;
396 };
397 
398 //----------------------------------------------------------------------------
399 // cm93 Composite Chart object class
400 //----------------------------------------------------------------------------
401 class CM93OffsetDialog;
402 
403 class cm93compchart : public s57chart {
404 public:
405  cm93compchart();
406  ~cm93compchart();
407 
408  InitReturn Init(const wxString &name, ChartInitFlag flags);
409 
410  void Activate(void);
411  void Deactivate(void);
412 
413  double GetNormalScaleMin(double canvas_scale_factor, bool b_allow_overzoom);
414  double GetNormalScaleMax(double canvas_scale_factor, int canvas_width);
415  int GetNativeScale(void);
416 
417  wxString GetPubDate();
418 
419  void SetVPParms(const ViewPort &vpt);
420  void GetValidCanvasRegion(const ViewPort &VPoint, OCPNRegion *pValidRegion);
421  LLRegion GetValidRegion();
422 
423  ThumbData *GetThumbData(int tnx, int tny, float lat, float lon);
424  ThumbData *GetThumbData() { return (ThumbData *)NULL; }
425 
426  bool AdjustVP(ViewPort &vp_last, ViewPort &vp_proposed);
427 
428  bool RenderRegionViewOnDC(wxMemoryDC &dc, const ViewPort &VPoint,
429  const OCPNRegion &Region);
430 
431  virtual bool RenderRegionViewOnGL(const wxGLContext &glc,
432  const ViewPort &VPoint,
433  const OCPNRegion &RectRegion,
434  const LLRegion &Region);
435  void SetColorScheme(ColorScheme cs, bool bApplyImmediate);
436 
437  bool RenderNextSmallerCellOutlines(ocpnDC &dc, ViewPort &vp, ChartCanvas *cc);
438 
439  void GetPointPix(ObjRazRules *rzRules, float rlat, float rlon, wxPoint *r);
440  void GetPixPoint(int pixx, int pixy, double *plat, double *plon,
441  ViewPort *vpt);
442  void GetPointPix(ObjRazRules *rzRules, wxPoint2DDouble *en, wxPoint *r,
443  int nPoints);
444 
445  ListOfObjRazRules *GetObjRuleListAtLatLon(float lat, float lon,
446  float select_radius,
447  ViewPort *VPoint,
448  int selection_mask = MASK_ALL);
449  S57ObjectDesc *CreateObjDescription(const ObjRazRules *obj);
450 
451  std::unordered_map<unsigned, VE_Element *> &Get_ve_hash(void);
452  std::unordered_map<unsigned, VC_Element *> &Get_vc_hash(void);
453 
454  void UpdateLUPs(s57chart *pOwner);
455  void ForceEdgePriorityEvaluate(void);
456  std::list<S57Obj*> *GetAssociatedObjects(S57Obj *obj);
457  cm93chart *GetCurrentSingleScaleChart() { return m_pcm93chart_current; }
458 
459  void SetSpecialOutlineCellIndex(int cell_index, int object_id, int subcell) {
460  m_cell_index_special_outline = cell_index;
461  m_object_id_special_outline = object_id;
462  m_subcell_special_outline = subcell;
463  }
464 
465  void SetSpecialCellIndexOffset(int cell_index, int object_id, int subcell,
466  int xoff, int yoff);
467  void CloseandReopenCurrentSubchart(void);
468 
469  void InvalidateCache();
470 
471 private:
472  void UpdateRenderRegions(const ViewPort &VPoint);
473  OCPNRegion GetValidScreenCanvasRegion(const ViewPort &VPoint,
474  const OCPNRegion &ScreenRegion);
475 
476  bool RenderViewOnDC(wxMemoryDC &dc, const ViewPort &VPoint);
477 
478  InitReturn CreateHeaderData();
479  cm93_dictionary *FindAndLoadDictFromDir(const wxString &dir);
480  void FillScaleArray(double lat, double lon);
481  int PrepareChartScale(const ViewPort &vpt, int cmscale,
482  bool bOZ_protect = true);
483  int GetCMScaleFromVP(const ViewPort &vpt);
484  bool DoRenderRegionViewOnDC(wxMemoryDC &dc, const ViewPort &VPoint,
485  const OCPNRegion &Region);
486 
487  bool DoRenderRegionViewOnGL(const wxGLContext &glc, const ViewPort &VPoint,
488  const OCPNRegion &RectRegion,
489  const LLRegion &Region);
490 
491  bool RenderCellOutlines(ocpnDC &dc, ViewPort &vp, wxPoint *pwp,
492  M_COVR_Desc *mcd);
493 
494  // Data members
495 
496  cm93_dictionary *m_pDictComposite;
497  cm93manager *m_pcm93mgr;
498 
499  cm93chart *m_pcm93chart_array[8];
500  bool m_bScale_Array[8];
501  cm93chart *m_pcm93chart_current;
502  int m_cmscale;
503 
504  wxString m_prefixComposite;
505 
506  int m_current_cell_pub_date; // the (integer) publish date of the cell at the
507  // current VP
508 
509  wxBitmap *m_pDummyBM;
510  int m_cell_index_special_outline;
511  int m_object_id_special_outline;
512  int m_subcell_special_outline;
513  int m_special_offset_x;
514  int m_special_offset_y;
515  ViewPort m_vpt;
516 
517  cm93chart *m_last_cell_adjustvp;
518 };
519 
520 class OCPNOffsetListCtrl;
521 //----------------------------------------------------------------------------------------------------------
522 // CM93OffsetDialog Specification
523 //----------------------------------------------------------------------------------------------------------
524 class CM93OffsetDialog : public wxDialog {
525  DECLARE_CLASS(CM93OffsetDialog)
526  DECLARE_EVENT_TABLE()
527 
528 public:
529  CM93OffsetDialog(wxWindow *parent);
530  ~CM93OffsetDialog();
531 
532  void OnClose(wxCloseEvent &event);
533  void OnOK(wxCommandEvent &event);
534 
535  void SetCM93Chart(cm93compchart *pchart);
536  void SetColorScheme();
537  void UpdateMCOVRList(const ViewPort &vpt); // Rebuild MCOVR list
538 
539  OCPNOffsetListCtrl *m_pListCtrlMCOVRs;
540  Array_Of_M_COVR_Desc_Ptr m_pcovr_array;
541 
542  wxString m_selected_chart_scale_char;
543 
544 private:
545  void OnCellSelected(wxListEvent &event);
546  void OnOffSetSet(wxCommandEvent &event);
547 
548  void UpdateOffsets(void);
549 
550  wxSpinCtrl *m_pSpinCtrlXoff;
551  wxSpinCtrl *m_pSpinCtrlYoff;
552  wxButton *m_OKButton;
553 
554  wxWindow *m_pparent;
555  cm93compchart *m_pcompchart;
556 
557  int m_xoff;
558  int m_yoff;
559  int m_selected_cell_index;
560  int m_selected_object_id;
561  int m_selected_subcell;
562  int m_selected_list_index;
563  double m_centerlat_cos;
564 };
565 
566 #endif
Definition: ocpndc.h:58
Definition: cm93.h:174
Runtime representation of a plugin block.
Definition: Quilt.cpp:867