📄 chart.cpp
字号:
/* GHelm - Nautical Navigation Software * Copyright (C) 2004 Jon Michaelchuck * * This application is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this software; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */#include <iostream>#include <math.h>#include "chart.h"#include "s57.h"/** * Constructor */Chart::Chart() : slat(0), wlon(0), nlat(0), elon(0), absolute_zoom_factor(0), zoom_exponent(.45), dsid_loaded(false), dspm_loaded(false){}/** * Destructor */Chart::~Chart(){ std::map<int, GLuint>::iterator it; std::map<int, GLuint>::iterator end = line_lists.end(); for (it = line_lists.begin(); it != end; ++it) glDeleteLists(it->second, 1); end = area_lists.end(); for (it = area_lists.begin(); it != end; ++it) glDeleteLists(it->second, 1);}/** * Load chart data * @param fpath The filepath * @param configuration configuration to use * @return 0 on success, -1 on failure. */int Chart::Load(boost::filesystem::path &fpath, Configuration &configuration){ if (S57Load(fpath) < 0) { std::cerr << "Chart::Load: Error loading chart " << fpath.string() << std::endl; return -1; } filepath = fpath; GenerateLineLists(configuration); GenerateAreaLists(configuration); GenerateCoastlines(); GeneratePointFeatures(configuration); return 0;}/** * Draw the chart */void Chart::Draw(double zoom, Configuration &configuration){ std::map<int, GLuint>::iterator it; std::map<int, GLuint>::iterator end = area_lists.end(); for (it = area_lists.begin(); it != end; ++it) { if (configuration.areas_drawn[it->first]) glCallList(it->second); } end = line_lists.end(); for (it = line_lists.begin(); it != end; ++it) { if (configuration.lines_drawn[it->first]) glCallList(it->second); } float z = pow(absolute_zoom_factor * zoom, zoom_exponent); std::map<int, std::vector<PointFeature> >::iterator pfm; std::map<int, std::vector<PointFeature> >::iterator pfmend = point_features_map.end(); for (pfm = point_features_map.begin(); pfm != pfmend; ++pfm) { if (pfm->first != SOUNDG) { std::vector<PointFeature>::iterator pit; std::vector<PointFeature>::iterator pend = pfm->second.end(); if (configuration.points_drawn[pfm->first]) { for (pit = pfm->second.begin(); pit != pend; ++pit) { pit->Draw(z); } } } }}/** * Setup structures for drawing from the loaded point feature data. */void Chart::GeneratePointFeatures(Configuration &configuration){ std::map<int, std::vector<PointFeature> >::iterator pfm; std::map<int, std::vector<PointFeature> >::iterator end(point_features_map.end()); for (pfm = point_features_map.begin(); pfm != end; ++pfm) { if (pfm->first != SOUNDG) { std::vector<PointFeature>::iterator pit; std::vector<PointFeature>::iterator end = pfm->second.end(); for (pit = pfm->second.begin(); pit != end; ++pit) { pit->SetLongLat(isolated_node_vectors_map); pit->cvg = configuration.point_cvgs[pfm->first]; } } } }/** * Generate line lists */void Chart::GenerateLineLists(Configuration &configuration){ std::map<int, std::vector<LineFeature> >::iterator lfm; std::map<int, std::vector<LineFeature> >::iterator end; end = line_features_map.end(); for (lfm = line_features_map.begin(); lfm != end; ++lfm) { signal_progress.emit(); GLuint list = glGenLists(1); glNewList(list, GL_COMPILE); glColor3fv(configuration.line_drawprops[lfm->first].rgb); glLineWidth(configuration.line_drawprops[lfm->first].size); std::vector<LineFeature>::iterator lit; std::vector<LineFeature>::iterator lend = lfm->second.end(); for (lit = lfm->second.begin(); lit != lend; ++lit) lit->Draw(edge_vectors_map); glEndList(); line_lists[lfm->first] = list; }}/*** Generate area lists*/void Chart::GenerateAreaLists(Configuration &configuration){ std::map<int, std::vector<AreaFeature> >::iterator afm; std::map<int, std::vector<AreaFeature> >::iterator end; end = area_features_map.end(); for (afm = area_features_map.begin(); afm != end; ++afm) { signal_progress.emit(); GLuint list = glGenLists(1); glNewList(list, GL_COMPILE); glColor3fv(configuration.area_drawprops[afm->first].rgb); std::vector<AreaFeature>::iterator ait; std::vector<AreaFeature>::iterator aend = afm->second.end(); for (ait = afm->second.begin(); ait != aend; ++ait) ait->Draw(edge_vectors_map); glEndList(); area_lists[afm->first] = list; } return;} /** * Generate coastlines for drawing land */void Chart::GenerateCoastlines(){ std::vector<LineFeature> coastline_features; // FIXME verify check coastline_features = line_features_map[COALNE]; std::pair<int, int> beg_end; std::pair<int, int> tmp_beg_end; std::vector<LineFeature>::iterator it; std::vector<LineFeature>::iterator end = coastline_features.end(); for (it = coastline_features.begin(); it != end; ++it) { signal_progress.emit(); beg_end = it->GetBegEndNodes(edge_vectors_map); std::vector<LineFeature>::iterator lit; for (lit = coastline_features.begin(); lit != end; ++lit) { tmp_beg_end = lit->GetBegEndNodes(edge_vectors_map); if (tmp_beg_end.first == beg_end.second) { } } }}//*****************************************************************************//// S57 loading////*****************************************************************************/** * Load an s57 chart * @param fpath Filepath of chart to load * @return 0 on success, -1 on failure */int Chart::S57Load(boost::filesystem::path &fpath){ const char *filename = fpath.string().c_str(); DDFModule ddf; if (ddf.Open(filename) == 0) { // DDFModule returns 0 on fail std::cerr << "Chart::S57Load(): Failed loading file " << filename << std::endl; ddf.Close(); return -1; } DDFRecord *record; while ((record = ddf.ReadRecord()) != NULL) { signal_progress.emit(); DDFField *field = record->GetField(1); if (!field) { std::cerr << "Chart::S57Load(): Error getting field" << std::endl; ddf.Close(); return -1; } DDFFieldDefn *field_defn = field->GetFieldDefn(); const char *field_name = field_defn->GetName(); if (strncmp(field_name, "DSID", 4) == 0) { if (S57ReadDSIDRecord(record) < 0) { std::cerr << "Chart::S57Load():" << " error loading dsid record" << std::endl; ddf.Close(); return -1; } } else if (strncmp(field_name, "DSPM", 4) == 0) { if (S57ReadDSPMRecord(record) < 0) { std::cerr << "Chart::S57Load():" << " error loading dspm record" << std::endl; ddf.Close(); return -1; } } else if (strncmp(field_name, "CATD", 4) == 0) { } else if (strncmp(field_name, "DDDF", 4) == 0) { } else if (strncmp(field_name, "DDSI", 4) == 0) { } else if (strncmp(field_name, "FRID", 4) == 0) { if (S57ReadFeatureRecord(record) < 0) { std::cerr << "Chart::S57Load():" << " error loading feature record" << std::endl; ddf.Close(); return -1; } } else if (strncmp(field_name, "VRID", 4) == 0) { if (S57ReadVectorRecord(record) < 0) { std::cerr << "Chart::S57Load():" << " error loading vector record" << std::endl; ddf.Close(); return -1; } } else { std::cerr << "Chart::S57Load(): Unknown leading record name " << field_name << std::endl; ddf.Close(); return -1; } } ddf.Close(); // we've read the file. if (!dsid_loaded) { std::cerr << "Chart::S57Load(): DSID not loaded" << std::endl; return -1; } if (!dspm_loaded) { std::cerr << "Chart::S57Load(): DSPM not loaded" << std::endl; return -1; } if (S57SetupEdgeVectors() < 0) { std::cerr << "Chart::S57Load(): Error setting up edge records" << std::endl; return -1; } if (S57SetupAreaFeatures() < 0) { std::cerr << "Chart::S57Load(): Error setting up area features" << std::endl; return -1; } // This MUST be after SetupEdgeVectors and SetupAreaFeatures otherwise // our connected nodes won't be multiplied. S57NormalizeCoordinates(); return 0;}/** * Read dsid data from a record * @param record The record to read * @return 0 on success, -1 on failure. */int Chart::S57ReadDSIDRecord(DDFRecord *record){ if (dsid_loaded) { std::cerr << "Chart::S57ReadDSIDRecord(): DSID already loaded" << std::endl; return -1; } if (dsid.Load(record) < 0) { std::cerr << "Chart::S57ReadDSIDRecord(): Error loading dsid record" << std::endl; return -1; } dsid_loaded = true; return 0;}/** * Read dspm data from a record * @param record The record to read * @return 0 on success, -1 on failure. */int Chart::S57ReadDSPMRecord(DDFRecord *record){ if (dspm_loaded) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -