1
0
Fork 0

[tg-construct] Reduce technical debt

This commit is contained in:
Scott Giese 2019-01-29 14:55:50 -06:00
parent 911f7f9d27
commit ca98d2ef5d
12 changed files with 108 additions and 91 deletions

View file

@ -35,9 +35,22 @@ int PrecisionFirstValue(const char * filename)
char line[80]; char line[80];
FILE *f = fopen(filename, "r"); FILE *f = fopen(filename, "r");
if (!f) return 0; if (!f) return 0;
if (fgets(line, 80, f) == 0) return 0; //skip poly count
if (fgets(line, 80, f) == 0) return 0; //skip length first polygon if (fgets(line, 80, f) == 0) {
if (fgets(line, 80, f) == 0) return 0; //get coords first vertex fclose(f);
return 0; //skip poly count
}
if (fgets(line, 80, f) == 0) {
fclose(f);
return 0; //skip length first polygon
}
if (fgets(line, 80, f) == 0) {
fclose(f);
return 0; //get coords first vertex
}
fclose(f); fclose(f);
int i = 0; int i = 0;
@ -66,7 +79,6 @@ bool LoadFromFile(Polygons &ppg, char * filename, double scale)
FILE *f = fopen(filename, "r"); FILE *f = fopen(filename, "r");
if (!f) return false; if (!f) return false;
int polyCnt, vertCnt; int polyCnt, vertCnt;
char junk [80];
double X, Y; double X, Y;
if (fscanf(f, "%d", &polyCnt) == 1 && polyCnt > 0) if (fscanf(f, "%d", &polyCnt) == 1 && polyCnt > 0)
@ -79,6 +91,8 @@ bool LoadFromFile(Polygons &ppg, char * filename, double scale)
if (fscanf(f, "%lf%*[, ]%lf", &X, &Y) != 2) break; if (fscanf(f, "%lf%*[, ]%lf", &X, &Y) != 2) break;
ppg[i][j].X = Round(X * scale); ppg[i][j].X = Round(X * scale);
ppg[i][j].Y = Round(Y * scale); ppg[i][j].Y = Round(Y * scale);
char junk [80];
fgets(junk, 80, f); fgets(junk, 80, f);
} }
} }

View file

@ -33,10 +33,14 @@
class TGAreaDefinition { class TGAreaDefinition {
public: public:
TGAreaDefinition( const std::string& n, const std::string& c, unsigned int p ) { TGAreaDefinition( const std::string& n, const std::string& c, unsigned int p ) :
name = n; name(n),
category = c; category(c)
{
priority = p; priority = p;
smooth_method = 0;
layered = false;
default_layer = 0;
}; };
std::string const& GetName() const { std::string const& GetName() const {
@ -68,7 +72,12 @@ typedef area_definition_list::const_iterator area_definition_iterator;
class TGAreaDefinitions { class TGAreaDefinitions {
public: public:
TGAreaDefinitions() {}; TGAreaDefinitions() :
sliver_area_name("<unnamed>")
{
sliver_area_priority = 0;
};
int init( const std::string& filename ); int init( const std::string& filename );
unsigned int size() const { unsigned int size() const {
return area_defs.size(); return area_defs.size();

View file

@ -40,11 +40,13 @@ TGConstruct::TGConstruct( const TGAreaDefinitions& areas, unsigned int s, SGLock
ignoreLandmass(false), ignoreLandmass(false),
debug_all(false), debug_all(false),
ds_id((void*)-1), ds_id((void*)-1),
l_id(nullptr),
isOcean(false) isOcean(false)
{ {
total_tiles = q.size(); total_tiles = q.size();
num_areas = areas.size(); num_areas = areas.size();
lock = l; lock = l;
nudge = 0.0;
} }
@ -55,9 +57,9 @@ TGConstruct::~TGConstruct() {
} }
// TGConstruct: Setup // TGConstruct: Setup
void TGConstruct::set_paths( const std::string work, const std::string share, void TGConstruct::set_paths( const std::string& work, const std::string& share,
const std::string match, const std::string output, const std::string& match, const std::string& output,
const std::vector<std::string> load ) { const std::vector<std::string>& load ) {
work_base = work; work_base = work;
share_base = share; share_base = share;
match_base = match; match_base = match;
@ -72,12 +74,10 @@ void TGConstruct::set_options( bool ignore_lm, double n ) {
void TGConstruct::run() void TGConstruct::run()
{ {
unsigned int tiles_complete;
// as long as we have feometry to parse, do so // as long as we have feometry to parse, do so
while ( !workQueue.empty() ) { while ( !workQueue.empty() ) {
bucket = workQueue.pop(); bucket = workQueue.pop();
tiles_complete = total_tiles - workQueue.size(); unsigned int tiles_complete = total_tiles - workQueue.size();
// assume non ocean tile until proven otherwise // assume non ocean tile until proven otherwise
isOcean = false; isOcean = false;

View file

@ -83,8 +83,8 @@ public:
#endif #endif
// paths // paths
void set_paths( const std::string work, const std::string share, const std::string match, void set_paths( const std::string& work, const std::string& share, const std::string& match,
const std::string output, const std::vector<std::string> load_dirs ); const std::string& output, const std::vector<std::string>& load_dirs );
void set_options( bool ignore_lm, double n ); void set_options( bool ignore_lm, double n );
// TODO : REMOVE // TODO : REMOVE

View file

@ -40,7 +40,7 @@ bool TGConstruct::ClipLandclassPolys( void ) {
tgPolygon safety_base; tgPolygon safety_base;
tgcontour_list slivers; tgcontour_list slivers;
SGGeod p; SGGeod p;
bool debug_area, debug_shape; bool debug_shape;
tgAccumulator accum(bucket.gen_index_str()); tgAccumulator accum(bucket.gen_index_str());
unsigned int accum_idx = 0; unsigned int accum_idx = 0;
@ -102,7 +102,7 @@ bool TGConstruct::ClipLandclassPolys( void ) {
// process polygons in priority order // process polygons in priority order
for ( unsigned int i = 0; i < area_defs.size(); i++ ) { for ( unsigned int i = 0; i < area_defs.size(); i++ ) {
debug_area = IsDebugArea( i ); bool debug_area = IsDebugArea( i );
for( unsigned int j = 0; j < polys_in.area_size(i); ++j ) { for( unsigned int j = 0; j < polys_in.area_size(i); ++j ) {
tgPolygon& current = polys_in.get_poly(i, j); tgPolygon& current = polys_in.get_poly(i, j);
debug_shape = IsDebugShape( polys_in.get_poly( i, j ).GetId() ); debug_shape = IsDebugShape( polys_in.get_poly( i, j ).GetId() );
@ -127,7 +127,7 @@ bool TGConstruct::ClipLandclassPolys( void ) {
char name[32]; char name[32];
sprintf(layer, "pre_clip_%d", polys_in.get_poly( i, j ).GetId() ); sprintf(layer, "pre_clip_%d", polys_in.get_poly( i, j ).GetId() );
sprintf(name, "shape %d,%d", i,j); sprintf(name, "shape %u,%u", i,j);
tgShapefile::FromPolygon( tmp, ds_name, layer, name ); tgShapefile::FromPolygon( tmp, ds_name, layer, name );
tgPolygon::ToClipperFile( tmp, ds_name, layer ); tgPolygon::ToClipperFile( tmp, ds_name, layer );
@ -159,7 +159,7 @@ bool TGConstruct::ClipLandclassPolys( void ) {
char name[32]; char name[32];
sprintf(layer, "post_clip_%d", polys_in.get_poly( i, j ).GetId() ); sprintf(layer, "post_clip_%d", polys_in.get_poly( i, j ).GetId() );
sprintf(name, "shape %d,%d", i,j); sprintf(name, "shape %u,%u", i,j);
tgShapefile::FromPolygon( clipped, ds_name, layer, name ); tgShapefile::FromPolygon( clipped, ds_name, layer, name );
} }
@ -175,7 +175,7 @@ bool TGConstruct::ClipLandclassPolys( void ) {
accum.Add( tmp ); accum.Add( tmp );
if ( debug_area || debug_shape ) { if ( debug_area || debug_shape ) {
char layer[32]; char layer[32];
sprintf(layer, "post_clip_accum_%d_%d", accum_idx++, polys_in.get_poly( i, j ).GetId() ); sprintf(layer, "post_clip_accum_%u_%u", accum_idx++, polys_in.get_poly( i, j ).GetId() );
accum.ToShapefiles( ds_name, layer, false ); accum.ToShapefiles( ds_name, layer, false );
accum.ToClipperfiles( ds_name, layer, false ); accum.ToClipperfiles( ds_name, layer, false );

View file

@ -68,8 +68,7 @@ SGVec3f TGConstruct::calc_normal( double area, const SGVec3d& p1, const SGVec3d&
void TGConstruct::calc_normals( std::vector<SGGeod>& geod_nodes, std::vector<SGVec3d>& wgs84_nodes, tgPolygon& poly ) { void TGConstruct::calc_normals( std::vector<SGGeod>& geod_nodes, std::vector<SGVec3d>& wgs84_nodes, tgPolygon& poly ) {
// for each face in the superpoly, calculate a face normal // for each face in the superpoly, calculate a face normal
SGVec3f normal; SGVec3f normal;
double area;
for (unsigned int tri = 0; tri < poly.Triangles(); tri++) { for (unsigned int tri = 0; tri < poly.Triangles(); tri++) {
SGGeod g1 = geod_nodes[ poly.GetTriIdx( tri, 0 ) ]; SGGeod g1 = geod_nodes[ poly.GetTriIdx( tri, 0 ) ];
@ -80,7 +79,7 @@ void TGConstruct::calc_normals( std::vector<SGGeod>& geod_nodes, std::vector<SGV
SGVec3d v2 = wgs84_nodes[ poly.GetTriIdx( tri, 1 ) ]; SGVec3d v2 = wgs84_nodes[ poly.GetTriIdx( tri, 1 ) ];
SGVec3d v3 = wgs84_nodes[ poly.GetTriIdx( tri, 2 ) ]; SGVec3d v3 = wgs84_nodes[ poly.GetTriIdx( tri, 2 ) ];
area = tgTriangle::area( g1, g2, g3 ); double area = tgTriangle::area( g1, g2, g3 );
normal = calc_normal( area, v1, v2, v3 ); normal = calc_normal( area, v1, v2, v3 );
poly.SetTriFaceArea( tri, area ); poly.SetTriFaceArea( tri, area );

View file

@ -87,7 +87,7 @@ void TGConstruct::AddCustomObjects( void ) {
in.getline(line, 2048); in.getline(line, 2048);
SG_LOG( SG_GENERAL, SG_DEBUG, "line = " << line ); SG_LOG( SG_GENERAL, SG_DEBUG, "line = " << line );
int result = sscanf( line, "%s %s", token, name ); int result = sscanf( line, "%255s %255s", token, name );
SG_LOG( SG_GENERAL, SG_DEBUG, "scanf scanned " << result << " tokens" ); SG_LOG( SG_GENERAL, SG_DEBUG, "scanf scanned " << result << " tokens" );
if ( result > 0 ) { if ( result > 0 ) {
@ -133,10 +133,10 @@ void TGConstruct::WriteBtgFile( void )
nodes.get_wgs84_nodes( wgs84_nodes ); nodes.get_wgs84_nodes( wgs84_nodes );
SGVec3d gbs_center = SGVec3d::fromGeod( bucket.get_center() ); SGVec3d gbs_center = SGVec3d::fromGeod( bucket.get_center() );
double dist_squared, radius_squared = 0; double radius_squared = 0;
for (int i = 0; i < (int)wgs84_nodes.size(); ++i) for (int i = 0; i < (int)wgs84_nodes.size(); ++i)
{ {
dist_squared = distSqr(gbs_center, wgs84_nodes[i]); double dist_squared = distSqr(gbs_center, wgs84_nodes[i]);
if ( dist_squared > radius_squared ) { if ( dist_squared > radius_squared ) {
radius_squared = dist_squared; radius_squared = dist_squared;
} }

View file

@ -72,7 +72,6 @@ int TGConstruct::LoadLandclassPolys( void ) {
{ {
// skipped! // skipped!
} else { } else {
int area;
std::string material; std::string material;
gzFile fp = gzopen( p.c_str(), "rb" ); gzFile fp = gzopen( p.c_str(), "rb" );
unsigned int count; unsigned int count;
@ -80,9 +79,9 @@ int TGConstruct::LoadLandclassPolys( void ) {
sgReadUInt( fp, &count ); sgReadUInt( fp, &count );
SG_LOG( SG_GENERAL, SG_DEBUG, " Load " << count << " polys from " << p.realpath() ); SG_LOG( SG_GENERAL, SG_DEBUG, " Load " << count << " polys from " << p.realpath() );
for ( unsigned int i=0; i<count; i++ ) { for ( unsigned int i = 0; i < count; ++i ) {
poly.LoadFromGzFile( fp ); poly.LoadFromGzFile( fp );
area = area_defs.get_area_priority( poly.GetFlag() ); int area = area_defs.get_area_priority( poly.GetFlag() );
material = area_defs.get_area_name( area ); material = area_defs.get_area_name( area );
bool isRoad = area_defs.is_road_area( area ); bool isRoad = area_defs.is_road_area( area );
@ -212,11 +211,10 @@ bool TGConstruct::CheckMatchingNode( SGGeod& node, bool road, bool fixed )
SGGeod TGConstruct::GetNearestNodeLongitude( const SGGeod& node, const std::vector<SGGeod>& selection ) SGGeod TGConstruct::GetNearestNodeLongitude( const SGGeod& node, const std::vector<SGGeod>& selection )
{ {
double min_dist = std::numeric_limits<double>::infinity(); double min_dist = std::numeric_limits<double>::infinity();
double cur_dist;
unsigned int min_idx = 0; unsigned int min_idx = 0;
for ( unsigned int i=0; i<selection.size(); i++ ) { for ( unsigned int i = 0; i < selection.size(); ++i ) {
cur_dist = fabs( node.getLongitudeDeg() - selection[i].getLongitudeDeg() ); double cur_dist = fabs( node.getLongitudeDeg() - selection[i].getLongitudeDeg() );
if ( cur_dist < min_dist ) { if ( cur_dist < min_dist ) {
min_dist = cur_dist; min_dist = cur_dist;
min_idx = i; min_idx = i;
@ -229,11 +227,10 @@ SGGeod TGConstruct::GetNearestNodeLongitude( const SGGeod& node, const std::vect
SGGeod TGConstruct::GetNearestNodeLatitude( const SGGeod& node, const std::vector<SGGeod>& selection ) SGGeod TGConstruct::GetNearestNodeLatitude( const SGGeod& node, const std::vector<SGGeod>& selection )
{ {
double min_dist = std::numeric_limits<double>::infinity(); double min_dist = std::numeric_limits<double>::infinity();
double cur_dist;
unsigned int min_idx = 0; unsigned int min_idx = 0;
for ( unsigned int i=0; i<selection.size(); i++ ) { for ( unsigned int i = 0; i < selection.size(); ++i ) {
cur_dist = fabs( node.getLatitudeDeg() - selection[i].getLatitudeDeg() ); double cur_dist = fabs( node.getLatitudeDeg() - selection[i].getLatitudeDeg() );
if ( cur_dist < min_dist ) { if ( cur_dist < min_dist ) {
min_dist = cur_dist; min_dist = cur_dist;
min_idx = i; min_idx = i;

View file

@ -43,7 +43,7 @@ void TGConstruct::TesselatePolys( void )
if ( IsDebugShape( poly.GetId() ) ) { if ( IsDebugShape( poly.GetId() ) ) {
char layer[32]; char layer[32];
sprintf(layer, "pretess_%d_%d", area, p ); sprintf(layer, "pretess_%u_%u", area, p );
tgShapefile::FromPolygon( poly, ds_name, layer, "poly" ); tgShapefile::FromPolygon( poly, ds_name, layer, "poly" );
} }

View file

@ -66,12 +66,11 @@ void TGLandclass::LoadFromGzFile(gzFile& fp)
std::ostream& operator<< ( std::ostream& out, const TGLandclass& lc ) std::ostream& operator<< ( std::ostream& out, const TGLandclass& lc )
{ {
unsigned int count;
tgPolygon poly; tgPolygon poly;
// Save all landclass shapes // Save all landclass shapes
for (unsigned int i=0; i<lc.polys.size(); i++) { for (unsigned int i=0; i<lc.polys.size(); i++) {
count = lc.polys[i].size(); unsigned int count = lc.polys[i].size();
out << count << "\n"; out << count << "\n";
for (unsigned int j=0; j<count; j++) { for (unsigned int j=0; j<count; j++) {
out << lc.polys[i][j] << " "; out << lc.polys[i][j] << " ";
@ -84,14 +83,14 @@ std::ostream& operator<< ( std::ostream& out, const TGLandclass& lc )
void TGLandclass::SaveToGzFile(gzFile& fp) void TGLandclass::SaveToGzFile(gzFile& fp)
{ {
int i, j, num_layers, num_polys; int i, j, num_layers;
tgPolygon shape; tgPolygon shape;
// Save all landclass shapes // Save all landclass shapes
num_layers = polys.size(); num_layers = polys.size();
sgWriteInt( fp, num_layers ); sgWriteInt( fp, num_layers );
for (i=0; i<num_layers; i++) { for (i=0; i<num_layers; i++) {
num_polys = polys[i].size(); int num_polys = polys[i].size();
sgWriteInt( fp, num_polys ); sgWriteInt( fp, num_polys );
for (j=0; j<num_polys; j++) { for (j=0; j<num_polys; j++) {

View file

@ -140,8 +140,7 @@ int make_socket (char *host, unsigned short int port) {
// connect to the server and get the next task // connect to the server and get the next task
long int get_next_task( const string& host, int port, long int last_tile ) { long int get_next_task( const string& host, int port, long int last_tile ) {
long int tile; int sock;
int sock, len;
fd_set ready; fd_set ready;
char message[MAXBUF]; char message[MAXBUF];
@ -172,17 +171,17 @@ long int get_next_task( const string& host, int port, long int last_tile ) {
cout << " received reply" << endl; cout << " received reply" << endl;
if ( FD_ISSET(sock, &ready) ) { if ( FD_ISSET(sock, &ready) ) {
/* input coming from socket */ /* input coming from socket */
if ( (len = read(sock, message, MAXBUF)) > 0 ) { if ( (int len = read(sock, message, MAXBUF)) > 0 ) {
message[len] = '\0'; message[len] = '\0';
tile = atoi(message); long int tile = atoi(message);
cout << " tile to construct = " << tile << endl; cout << " tile to construct = " << tile << endl;
close(sock); close(sock);
return tile; return tile;
} else { } else {
close(sock); close(sock);
return -1; return -1;
} }
} }
close(sock); close(sock);

View file

@ -171,56 +171,56 @@ long int get_next_tile() {
// first time this routine is called, init counters // first time this routine is called, init counters
if ( first_time ) { if ( first_time ) {
first_time = false; first_time = false;
start_seconds = seconds = time(NULL); start_seconds = seconds = time(NULL);
counter = global_counter = 0; counter = global_counter = 0;
} }
// cout << "lon = " << lon << " lat = " << lat << endl; // cout << "lon = " << lon << " lat = " << lat << endl;
// cout << "start_lat = " << start_lat << endl; // cout << "start_lat = " << start_lat << endl;
if ( lon > start_lon + area_width ) { if ( lon > start_lon + area_width ) {
// increment to next row // increment to next row
// skip every other row (to avoid two clients working on // skip every other row (to avoid two clients working on
// adjacent tiles) // adjacent tiles)
lat += 2.0 * dy; lat += 2.0 * dy;
SGBucket tmp( SGGeod::fromDeg(0.0, lat) ); SGBucket tmp( SGGeod::fromDeg(0.0, lat) );
double dx = tmp.get_width(); double dx = tmp.get_width();
lon = start_lon + (shift_over*dx) + (dx*0.5); lon = start_lon + (shift_over*dx) + (dx*0.5);
} }
if ( lat > start_lat + area_height ) { if ( lat > start_lat + area_height ) {
++pass; ++pass;
if ( pass == 1 ) { if ( pass == 1 ) {
shift_over = 0.0; shift_over = 0.0;
shift_up = 0.0; shift_up = 0.0;
} else if ( pass == 2 ) { } else if ( pass == 2 ) {
shift_over = 1.0; shift_over = 1.0;
shift_up = 0.0; shift_up = 0.0;
} else if ( pass == 3 ) { } else if ( pass == 3 ) {
shift_over = 0.0; shift_over = 0.0;
shift_up = 1.0; shift_up = 1.0;
} else if ( pass == 4 ) { } else if ( pass == 4 ) {
shift_over = 1.0; shift_over = 1.0;
shift_up = 1.0; shift_up = 1.0;
} else { } else {
return -1; return -1;
} }
// reset lat // reset lat
// lat = -89.0 + (shift_up*dy) - (dy*0.5); // lat = -89.0 + (shift_up*dy) - (dy*0.5);
// lat = 27.0 + (0*dy) + (dy*0.5); // lat = 27.0 + (0*dy) + (dy*0.5);
lat = start_lat + (shift_up*dy) + (dy*0.5); lat = start_lat + (shift_up*dy) + (dy*0.5);
// reset lon // reset lon
SGBucket tmp( SGGeod::fromDeg(0.0, lat) ); SGBucket tmp( SGGeod::fromDeg(0.0, lat) );
double dx = tmp.get_width(); double dx = tmp.get_width();
// lon = -82 + (shift_over*dx) + (dx*0.5); // lon = -82 + (shift_over*dx) + (dx*0.5);
lon = start_lon + (shift_over*dx) + (dx*0.5); lon = start_lon + (shift_over*dx) + (dx*0.5);
cout << "starting pass = " << pass cout << "starting pass = " << pass
<< " with lat = " << lat << " lon = " << lon << endl; << " with lat = " << lat << " lon = " << lon << endl;
} }
// if ( ! start_lon ) { // if ( ! start_lon ) {