1
0
Fork 0

Numerous small fixes following code review:

(1) Whitespace use matched better to surrounding code
(2) While(1) in TGArray::rectify_heights replaced with do/while loop
(3) Incorrect interpolation for 2 points fixed in altitude_from_grid
(4) exit(1) replaced with return EXIT_FAILURE in cliff-decode.cxx
This commit is contained in:
James.Hester 2019-01-13 09:59:00 +11:00
parent dfe81ce9fa
commit 3421ce2018
5 changed files with 404 additions and 352 deletions

View file

@ -67,14 +67,14 @@ bool TGArray::open( const string& file_base ) {
array_in = gzopen( array_name.c_str(), "rb" ); array_in = gzopen( array_name.c_str(), "rb" );
if (array_in == NULL) { if (array_in == NULL) {
// try unrectified // try unrectified
array_name = file_base + ".arr.gz"; array_name = file_base + ".arr.gz";
array_in = gzopen(array_name.c_str(), "rb"); array_in = gzopen(array_name.c_str(), "rb");
if (array_in == NULL) { if (array_in == NULL) {
return false; return false;
} else { } else {
rectified = false; rectified = false;
} }
} }
SG_LOG(SG_GENERAL,SG_DEBUG,"Loaded height array " << array_name); SG_LOG(SG_GENERAL,SG_DEBUG,"Loaded height array " << array_name);
@ -121,34 +121,34 @@ TGArray::close() {
void TGArray::load_cliffs(const string & height_base) void TGArray::load_cliffs(const string & height_base)
{ {
//Get the directory so we can list the children //Get the directory so we can list the children
tgPolygon poly; //actually a contour but whatever... tgPolygon poly; //actually a contour but whatever...
int total_contours_read = 0; int total_contours_read = 0;
SGPath b(height_base); SGPath b(height_base);
simgear::Dir d(b.dir()); simgear::Dir d(b.dir());
simgear::PathList files = d.children(simgear::Dir::TYPE_FILE); simgear::PathList files = d.children(simgear::Dir::TYPE_FILE);
BOOST_FOREACH(const SGPath& p, files) { for (const SGPath& p: files) {
if (p.file_base() != b.file_base()) { if (p.file_base() != b.file_base()) {
continue; continue;
} }
string lext = p.lower_extension(); string lext = p.lower_extension();
if (lext == "cliffs") { if (lext == "cliffs") {
gzFile fp = gzopen( p.c_str(), "rb" ); gzFile fp = gzopen( p.c_str(), "rb" );
unsigned int count; unsigned int count;
sgReadUInt( fp, &count ); sgReadUInt( fp, &count );
SG_LOG( SG_GENERAL, SG_DEBUG, " Load " << count << " contours from " << p.realpath() ); SG_LOG( SG_GENERAL, SG_DEBUG, " Load " << count << " contours 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 );
if ( poly.Contours()==1 ) { //should always have one contour if ( poly.Contours()==1 ) { //should always have one contour
cliffs_list.push_back(poly.GetContour(0)); cliffs_list.push_back(poly.GetContour(0));
} else { } else {
SG_LOG( SG_GENERAL, SG_WARN, " Found " << poly.Contours() << " contours in " << p.realpath() ); SG_LOG( SG_GENERAL, SG_WARN, " Found " << poly.Contours() << " contours in " << p.realpath() );
}
}
} }
}
} }
}
} }
@ -408,7 +408,7 @@ double TGArray::closest_nonvoid_elev( double lon, double lat ) const {
for ( int row = 0; row < rows; row++ ) { for ( int row = 0; row < rows; row++ ) {
for ( int col = 0; col < cols; col++ ) { for ( int col = 0; col < cols; col++ ) {
SGGeod p1 = SGGeod::fromDeg( (originx + col * col_step)/3600.0, (originy + row * row_step)/3600.0 ); SGGeod p1 = SGGeod::fromDeg( (originx + col * col_step)/3600.0, (originy + row * row_step)/3600.0 );
double dist = SGGeodesy::distanceM( p0, p1 ); double dist = SGGeodesy::distanceM( p0, p1 );
double elev = get_array_elev(col, row); double elev = get_array_elev(col, row);
if ( dist < mindist && elev > -9000 ) { if ( dist < mindist && elev > -9000 ) {
@ -425,61 +425,68 @@ double TGArray::closest_nonvoid_elev( double lon, double lat ) const {
} }
} }
//Find and remember all points that are bad because they are
//too close to a cliff
std::vector<int> TGArray::collect_bad_points(const double bad_zone) { std::vector<int> TGArray::collect_bad_points(const double bad_zone) {
//Find and remember all points that are bad because they are
//too close to a cliff std::vector<int> bad_points; //local to avoid multi-thread issues
std::vector<int> bad_points; //local to avoid multi-thread issues
for(int horiz=0;horiz<cols;horiz++) { for( int horiz=0;horiz<cols;horiz++ ) {
double lon = (originx + col_step*horiz)/3600; double lon = (originx + col_step*horiz)/3600;
for(int vert=0;vert<rows;vert++) { for( int vert=0;vert<rows;vert++ ) {
double lat = (originy + row_step*vert)/3600; double lat = (originy + row_step*vert)/3600;
if(is_near_cliff(lon,lat,bad_zone)) { if( is_near_cliff(lon,lat,bad_zone) ) {
bad_points.push_back(horiz+vert*cols); bad_points.push_back(horiz+vert*cols);
}
} }
} }
}
return bad_points; return bad_points;
} }
// Check to see if the specified grid point is bad // Check to see if the specified grid point is bad
bool TGArray::is_bad_point(const int xgrid, const int ygrid, const std::vector<int> bad_points) const { bool TGArray::is_bad_point(const int xgrid, const int ygrid, const std::vector<int> bad_points) const {
int grididx; int grididx;
grididx = xgrid+ygrid*cols; grididx = xgrid+ygrid*cols;
auto result = std::find(std::begin(bad_points),std::end(bad_points),grididx); auto result = std::find( std::begin(bad_points),std::end(bad_points),grididx );
if (result != std::end(bad_points)) return true; if ( result != std::end(bad_points) ) return true;
return false; return false;
} }
//This may collide with other threads, but as they will both be writing //This may collide with other threads, but as they will both be writing
//the correct height, this is harmless. //the correct height, this is harmless.
void TGArray::rectify_heights(const double bad_zone) { void TGArray::rectify_heights( const double bad_zone ) {
double new_ht; double new_ht;
std::vector<int> rectified,bad_points; std::vector<int> rectified,bad_points;
bad_points = collect_bad_points(bad_zone); int total_rectified;
while(1) { bad_points = collect_bad_points( bad_zone );
for (auto pt : bad_points) {
int ygrid = pt/cols; do {
int xgrid = pt - ygrid*cols; for ( auto pt : bad_points ) {
new_ht = rectify_point(xgrid,ygrid,bad_points); int ygrid = pt/cols;
if (new_ht > -9999) { int xgrid = pt - ygrid*cols;
rectified.push_back(pt); new_ht = rectify_point( xgrid,ygrid,bad_points );
set_array_elev(xgrid,ygrid,(int) new_ht); if (new_ht > -9999) {
} rectified.push_back(pt);
} set_array_elev( xgrid,ygrid,(int) new_ht );
SG_LOG(SG_GENERAL, SG_DEBUG, "Rectified " << rectified.size() << " points "); }
if(rectified.size()>0) { }
for(auto r : rectified) { total_rectified = rectified.size();
bad_points.erase(std::remove(std::begin(bad_points),std::end(bad_points),r)); SG_LOG(SG_GENERAL, SG_DEBUG, "Rectified " << total_rectified << " points ");
}
rectified.clear(); if( total_rectified > 0 ) {
} else { for( auto r : rectified ) {
if(bad_points.size() > 0) { bad_points.erase( std::remove( std::begin(bad_points), std::end(bad_points),r) );
}
rectified.clear();
}
} while ( total_rectified > 0 );
if( bad_points.size() > 0 ) {
SG_LOG(SG_GENERAL, SG_DEBUG, "Failed to rectify " << bad_points.size() << " points"); SG_LOG(SG_GENERAL, SG_DEBUG, "Failed to rectify " << bad_points.size() << " points");
} }
break; // Cant do any more
}
}
} }
/* If we have cliffs, it is possible that a grid point will be too close /* If we have cliffs, it is possible that a grid point will be too close
@ -496,67 +503,82 @@ through the three known points.
* * * * * *
TODO: Handle points on the boundaries. */ TODO: Handle points on the boundaries. */
double TGArray::rectify_point(const int xgrid, const int ygrid, const std::vector<int> bad_points) const { double TGArray::rectify_point(const int xgrid, const int ygrid, const std::vector<int> bad_points) const {
//xgrid: grid units horizontally //xgrid: grid units horizontally
//ygrid: grid units vertically //ygrid: grid units vertically
//Loop over corner points, if no points available, give up //Loop over corner points, if no points available, give up
int corners[4][2]; //possible corners int corners[4][2]; //possible corners
int final_pts[3][2]; // rectangle corners int final_pts[3][2]; // rectangle corners
int pt_cnt = 0; int pt_cnt = 0;
double centre_long, centre_lat; double centre_long, centre_lat;
double cliff_error = col_step; //Assume row step, col step the same double cliff_error = col_step; //Assume row step, col step the same
int original_height = get_array_elev(xgrid,ygrid); int original_height = get_array_elev(xgrid,ygrid);
centre_long = (originx + col_step*xgrid)/3600; centre_long = (originx + col_step*xgrid)/3600;
centre_lat = (originy + row_step*ygrid)/3600; centre_lat = (originy + row_step*ygrid)/3600;
for (int horiz = -1; horiz <= 1; horiz+=2) {
if (xgrid + horiz >= cols || xgrid + horiz < 0) continue; //edge of bucket for ( int horiz = -1; horiz <= 1; horiz+=2 ) {
double test_long = centre_long + (col_step*horiz)/3600; if (xgrid + horiz >= cols || xgrid + horiz < 0) continue; //edge of bucket
for (int vert = -1; vert <= 1; vert+=2) {
if (ygrid + vert >= rows || ygrid + vert < 0) continue; //edge of bucket double test_long = centre_long + (col_step*horiz)/3600;
double test_lat = centre_lat + (row_step*vert)/3600; for ( int vert = -1; vert <= 1; vert+=2 ) {
if (!is_bad_point(xgrid+horiz,ygrid+vert,bad_points) && //can trust height if (ygrid + vert >= rows || ygrid + vert < 0) continue; //edge of bucket
check_points(test_long,test_lat,centre_long,centre_lat)) { //same side
corners[pt_cnt][0] = horiz; double test_lat = centre_lat + (row_step*vert)/3600;
corners[pt_cnt][1] = vert; if ( !is_bad_point( xgrid+horiz,ygrid+vert,bad_points ) && //can trust height
pt_cnt++; check_points( test_long,test_lat,centre_long,centre_lat ) ) { //same side
corners[pt_cnt][0] = horiz;
corners[pt_cnt][1] = vert;
pt_cnt++;
}
} }
} } // end of search for corners
} // end of search for corners
if (pt_cnt == 0) return -9999; // no corners found if (pt_cnt == 0) return -9999; // no corners found
// Find two points that form a rectangle with a corner
int pt; // Find two points that form a rectangle with a corner
double height = 0; int pt;
for (pt = 0; pt < pt_cnt; pt++) { double height = 0;
if (!is_bad_point(xgrid+corners[pt][0],ygrid,bad_points) && for ( pt = 0; pt < pt_cnt; pt++ ) {
!is_bad_point(xgrid, ygrid+corners[pt][1],bad_points)) {
double test_horiz = centre_long + corners[pt][0]*col_step/3600; if ( !is_bad_point( xgrid+corners[pt][0],ygrid,bad_points ) &&
double test_vert = centre_lat + corners[pt][1]*row_step/3600; !is_bad_point( xgrid, ygrid+corners[pt][1],bad_points ) ) {
if (check_points(test_horiz,centre_lat,centre_long,centre_lat) &&
check_points(centre_long,test_vert,centre_long,centre_lat)) break; double test_horiz = centre_long + corners[pt][0]*col_step/3600;
double test_vert = centre_lat + corners[pt][1]*row_step/3600;
if ( check_points( test_horiz,centre_lat,centre_long,centre_lat ) &&
check_points( centre_long,test_vert,centre_long,centre_lat ) ) break;
} }
} }
if (pt == pt_cnt) { // perhaps we have a concave cliff, just take the if (pt == pt_cnt) {
// average of the known points // perhaps we have a concave cliff, just take the
double totht = 0; // average of the known points
for(int pti = 0; pti <pt_cnt; pti++) { double totht = 0;
totht = totht + get_array_elev(xgrid+corners[pti][0],ygrid+corners[pti][1]); for( int pti = 0; pti <pt_cnt; pti++ ) {
} totht = totht + get_array_elev( xgrid+corners[pti][0],ygrid+corners[pti][1] );
height = totht/pt_cnt; }
} else {
height = totht/pt_cnt;
} else {
// We have three points, calculate the height // We have three points, calculate the height
// Set anything very negative to zero // Set anything very negative to zero
double corner = get_array_elev(xgrid+corners[pt][0],ygrid+corners[pt][1]); double corner = get_array_elev( xgrid+corners[pt][0],ygrid+corners[pt][1] );
double horiz = get_array_elev(xgrid,ygrid+corners[pt][1]); double horiz = get_array_elev( xgrid,ygrid+corners[pt][1] );
double vert = get_array_elev(xgrid+corners[pt][0],ygrid); double vert = get_array_elev( xgrid+corners[pt][0],ygrid );
if (corner < -9000) corner = 0; if ( corner < -9000 ) corner = 0;
if (horiz < -9000) horiz = 0; if ( horiz < -9000 ) horiz = 0;
if (vert < -9000) vert = 0; if ( vert < -9000 ) vert = 0;
height = horiz + (vert - corner); height = horiz + ( vert - corner );
} }
SG_LOG(SG_GENERAL, SG_DEBUG, xgrid << "," << ygrid << ": was " << original_height << " , now " << height);
return height; SG_LOG(SG_GENERAL, SG_DEBUG, xgrid << "," << ygrid << ": was " << original_height << " , now " << height);
return height;
} }
// return the current altitude based on grid data. // return the current altitude based on grid data.
@ -578,7 +600,7 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
------ ------
then calculate our end points then calculate our end points
*/ */
// Store in degrees for later // Store in degrees for later
double londeg = lon/3600; double londeg = lon/3600;
@ -589,8 +611,6 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
xindex = (int)(xlocal); xindex = (int)(xlocal);
yindex = (int)(ylocal); yindex = (int)(ylocal);
// printf("xindex = %d yindex = %d\n", xindex, yindex);
if ( xindex + 1 == cols ) { if ( xindex + 1 == cols ) {
xindex--; xindex--;
} }
@ -601,7 +621,9 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
if ( (xindex < 0) || (xindex + 1 >= cols) || if ( (xindex < 0) || (xindex + 1 >= cols) ||
(yindex < 0) || (yindex + 1 >= rows) ) { (yindex < 0) || (yindex + 1 >= rows) ) {
SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING: Attempt to interpolate value outside of array!!!" ); SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING: Attempt to interpolate value outside of array!!!" );
return -9999; return -9999;
} }
@ -613,95 +635,101 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
int corners[4][2]; int corners[4][2];
int ccnt = 0; int ccnt = 0;
int missing = -1; //the missing point when 3 available int missing = -1; //the missing point when 3 available
double lon1 = (originx+(xindex*col_step))/3600; double lon1 = (originx+(xindex*col_step))/3600;
double lat1 = (originy+(yindex*row_step))/3600; double lat1 = (originy+(yindex*row_step))/3600;
double lon2 = lon1 + col_step/3600; double lon2 = lon1 + col_step/3600;
double lat2 = lat1 + row_step/3600; double lat2 = lat1 + row_step/3600;
if (check_points(lon1,lat1,londeg,latdeg)) {
corners[ccnt][0] = xindex;
corners[ccnt][1] = yindex;
ccnt++;
} else missing = 0;
if (check_points(lon1,lat2,londeg,latdeg)) {
corners[ccnt][0] = xindex;
corners[ccnt][1] = yindex+1;
ccnt++;
} else missing = 1;
if (check_points(lon2,lat2,londeg,latdeg)) {
corners[ccnt][0] = xindex+1;
corners[ccnt][1] = yindex+1;
ccnt++;
} else missing = 2;
if (check_points(lon2,lat1,londeg,latdeg)) {
corners[ccnt][0] = xindex+1;
corners[ccnt][1] = yindex;
ccnt++;
} else missing = 3;
switch (ccnt) { if ( check_points(lon1,lat1,londeg,latdeg) ) {
case 3: //3 points are corners of a rectangle corners[ccnt][0] = xindex;
corners[ccnt][1] = yindex;
ccnt++;
} else missing = 0;
if ( check_points(lon1,lat2,londeg,latdeg) ) {
corners[ccnt][0] = xindex;
corners[ccnt][1] = yindex+1;
ccnt++;
} else missing = 1;
if ( check_points(lon2,lat2,londeg,latdeg) ) {
corners[ccnt][0] = xindex+1;
corners[ccnt][1] = yindex+1;
ccnt++;
} else missing = 2;
if ( check_points(lon2,lat1,londeg,latdeg) ) {
corners[ccnt][0] = xindex+1;
corners[ccnt][1] = yindex;
ccnt++;
} else missing = 3;
switch (ccnt) {
case 3: //3 points are corners of a rectangle
// choose the points so that x2 is the right angle // choose the points so that x2 is the right angle
// and x1-x2 is the x arm of the triangle // and x1-x2 is the x arm of the triangle
// dx,dy are the (positive) distances from the x1 corner // dx,dy are the (positive) distances from the x1 corner
SG_LOG(SG_GENERAL, SG_DEBUG, "3 points, missing #" << missing); SG_LOG(SG_GENERAL, SG_DEBUG, "3 points, missing #" << missing);
dx = xlocal -xindex; dx = xlocal -xindex;
dy = ylocal -yindex; dy = ylocal -yindex;
switch (missing) {
switch ( missing ) {
case 0: //SW corner missing case 0: //SW corner missing
x1 = corners[0][0]; x1 = corners[0][0];
y1 = corners[0][1]; y1 = corners[0][1];
x2 = corners[1][0]; x2 = corners[1][0];
y2 = corners[1][1]; y2 = corners[1][1];
x3 = corners[2][0]; x3 = corners[2][0];
y3 = corners[2][1]; y3 = corners[2][1];
dy = 1 - dy; dy = 1 - dy;
break; break;
case 1: //NW corner missing case 1: //NW corner missing
x1 = corners[0][0]; x1 = corners[0][0];
y1 = corners[0][1]; y1 = corners[0][1];
x2 = corners[2][0]; x2 = corners[2][0];
y2 = corners[2][1]; y2 = corners[2][1];
x3 = corners[1][0]; x3 = corners[1][0];
y3 = corners[1][1]; y3 = corners[1][1];
break; break;
case 2: //NE corner missing case 2: //NE corner missing
x1 = corners[2][0]; x1 = corners[2][0];
y1 = corners[2][1]; y1 = corners[2][1];
x2 = corners[0][0]; x2 = corners[0][0];
y2 = corners[0][1]; y2 = corners[0][1];
x3 = corners[1][0]; x3 = corners[1][0];
y3 = corners[1][1]; y3 = corners[1][1];
dx = 1 - dx; //x1 is SE corner dx = 1 - dx; //x1 is SE corner
break; break;
case 3: //SE corner missing case 3: //SE corner missing
x1 = corners[2][0]; x1 = corners[2][0];
y1 = corners[2][1]; y1 = corners[2][1];
x2 = corners[1][0]; x2 = corners[1][0];
y2 = corners[1][1]; y2 = corners[1][1];
x3 = corners[0][0]; x3 = corners[0][0];
y3 = corners[0][1]; y3 = corners[0][1];
dx = 1 - dx; //x1 is NE corner dx = 1 - dx; //x1 is NE corner
dy = 1 - dy; dy = 1 - dy;
break; break;
} }
// Now do the calcs on the triangle // Now do the calcs on the triangle
// We interpolate on height along x1-x2 and // We interpolate on height along x1-x2 and
// x1 - x3. Then interpolate between these // x1 - x3. Then interpolate between these
// two points along y. // two points along y.
z1 = get_array_elev(x1,y1); z1 = get_array_elev(x1,y1);
z2 = get_array_elev(x2,y2); z2 = get_array_elev(x2,y2);
z3 = get_array_elev(x3,y3); z3 = get_array_elev(x3,y3);
@ -709,13 +737,14 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
zB = dx * (z3 - z1) + z1; zB = dx * (z3 - z1) + z1;
if ( dx > SG_EPSILON ) { if ( dx > SG_EPSILON ) {
elev = dy * (zB - zA) / dx + zA; elev = dy * (zB - zA) / dx + zA;
} else { } else {
elev = zA; elev = zA;
} }
break; break;
case 2: //project onto line connecting two points
case 2: //project onto line connecting two points
x1 = corners[0][0]; x1 = corners[0][0];
y1 = corners[0][1]; y1 = corners[0][1];
z1 = get_array_elev(x1,y1); z1 = get_array_elev(x1,y1);
@ -729,119 +758,135 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
dx = xlocal - x1; dx = xlocal - x1;
dy = ylocal - y1; dy = ylocal - y1;
if (x1==x2) { if (x1==x2) {
elev = z1+dy*(z2-z1); elev = z1+dy*(z2-z1)/(y2-y1);
} }
else if (y1==y2) { else if (y1==y2) {
elev = z1+dx*(z2-z1); elev = z1+dx*(z2-z1)/(x2-x1);
} }
else { //diagonal: project onto 45 degree line else { //diagonal: project onto 45 degree line
int comp1 = x2-x1; int comp1 = x2-x1;
int comp2 = y2-y1; int comp2 = y2-y1;
double dotprod = (dx*comp1 + dy*comp2)/sqrt(2); double dotprod = (dx*comp1 + dy*comp2)/sqrt(2);
double projlen = sqrt(dx*dx+dy*dy)*dotprod; double projlen = sqrt(dx*dx+dy*dy)*dotprod;
elev = (z2-z1)*projlen/sqrt(2); elev = (z2-z1)*projlen/sqrt(2);
} }
break;
case 1: //only one point found
elev = get_array_elev(corners[0][0],corners[0][1]);
break; break;
case 0: // all points on wrong side, fall through to normal calc
case 1: //only one point found
elev = get_array_elev( corners[0][0],corners[0][1] );
break;
case 0: // all points on wrong side, fall through to normal calc
SG_LOG(SG_GENERAL, SG_WARN, "All elevation grid points on wrong side of cliff for " << std::setprecision(10) << londeg << "," << latdeg ); SG_LOG(SG_GENERAL, SG_WARN, "All elevation grid points on wrong side of cliff for " << std::setprecision(10) << londeg << "," << latdeg );
SG_LOG(SG_GENERAL, SG_WARN, "Grid points ("<< std::setprecision(9) << lon1 << "," << lat1 << "),("<<lon2<<","<<lat2<<")"); SG_LOG(SG_GENERAL, SG_WARN, "Grid points ("<< std::setprecision(9) << lon1 << "," << lat1 << "),("<<lon2<<","<<lat2<<")");
default: // all corners
default: // all corners
dx = xlocal - xindex; dx = xlocal - xindex;
dy = ylocal - yindex; dy = ylocal - yindex;
if ( dx > dy ) { if ( dx > dy ) {
// lower triangle // lower triangle
x1 = xindex; x1 = xindex;
y1 = yindex; y1 = yindex;
z1 = get_array_elev(x1, y1); z1 = get_array_elev(x1, y1);
x2 = xindex + 1; x2 = xindex + 1;
y2 = yindex; y2 = yindex;
z2 = get_array_elev(x2, y2); z2 = get_array_elev(x2, y2);
x3 = xindex + 1; x3 = xindex + 1;
y3 = yindex + 1; y3 = yindex + 1;
z3 = get_array_elev(x3, y3); z3 = get_array_elev(x3, y3);
if ( z1 < -9000 || z2 < -9000 || z3 < -9000 ) { if ( z1 < -9000 || z2 < -9000 || z3 < -9000 ) {
// don't interpolate off a void // don't interpolate off a void
return closest_nonvoid_elev( lon, lat ); return closest_nonvoid_elev( lon, lat );
} }
zA = dx * (z2 - z1) + z1; zA = dx * (z2 - z1) + z1;
zB = dx * (z3 - z1) + z1; zB = dx * (z3 - z1) + z1;
if ( dx > SG_EPSILON ) { if ( dx > SG_EPSILON ) {
elev = dy * (zB - zA) / dx + zA; elev = dy * (zB - zA) / dx + zA;
} else { } else {
elev = zA; elev = zA;
} }
} else { } else {
// upper triangle // upper triangle
x1 = xindex; x1 = xindex;
y1 = yindex; y1 = yindex;
z1 = get_array_elev(x1, y1); z1 = get_array_elev(x1, y1);
x2 = xindex; x2 = xindex;
y2 = yindex + 1; y2 = yindex + 1;
z2 = get_array_elev(x2, y2); z2 = get_array_elev(x2, y2);
x3 = xindex + 1; x3 = xindex + 1;
y3 = yindex + 1; y3 = yindex + 1;
z3 = get_array_elev(x3, y3); z3 = get_array_elev(x3, y3);
if ( z1 < -9000 || z2 < -9000 || z3 < -9000 ) { if ( z1 < -9000 || z2 < -9000 || z3 < -9000 ) {
// don't interpolate off a void // don't interpolate off a void
return closest_nonvoid_elev( lon, lat ); return closest_nonvoid_elev( lon, lat );
} }
zA = dy * (z2 - z1) + z1; zA = dy * (z2 - z1) + z1;
zB = dy * (z3 - z1) + z1; zB = dy * (z3 - z1) + z1;
if ( dy > SG_EPSILON ) { if ( dy > SG_EPSILON ) {
elev = dx * (zB - zA) / dy + zA; elev = dx * (zB - zA) / dy + zA;
} else { } else {
elev = zA; elev = zA;
} }
} }
} }
return elev; return elev;
} }
// Check that two points are on the same side of all cliff contours // Check that two points are on the same side of all cliff contours
// Could speed up by checking bounding box first // Could speed up by checking bounding box first
bool TGArray::check_points (const double lon1, const double lat1, const double lon2, const double lat2) const { bool TGArray::check_points( const double lon1, const double lat1, const double lon2, const double lat2 ) const {
if (cliffs_list.size()==0) return true;
if (fabs(lon1-lon2)<SG_EPSILON && fabs(lat1-lat2)<SG_EPSILON) return true; if ( cliffs_list.size()==0 ) return true;
SGGeod pt1 = SGGeod::fromDeg(lon1,lat1);
SGGeod pt2 = SGGeod::fromDeg(lon2,lat2); if ( fabs(lon1-lon2)<SG_EPSILON && fabs(lat1-lat2)<SG_EPSILON ) return true;
bool same_side = true;
for (int i=0;i<cliffs_list.size();i++) { SGGeod pt1 = SGGeod::fromDeg( lon1,lat1 );
bool check_result = cliffs_list[i].AreSameSide(pt1,pt2); SGGeod pt2 = SGGeod::fromDeg( lon2,lat2 );
if(!check_result) { bool same_side = true;
SG_LOG(SG_GENERAL, SG_DEBUG, "Cliff " << i <<":" <<pt1 << " and " << pt2 << " on opposite sides");
same_side = false; for ( int i=0;i<cliffs_list.size();i++ ) {
break; bool check_result = cliffs_list[i].AreSameSide( pt1,pt2 );
if(!check_result) {
SG_LOG(SG_GENERAL, SG_DEBUG, "Cliff " << i <<":" <<pt1 << " and " << pt2 << " on opposite sides");
same_side = false;
break;
}
} }
}
return same_side; return same_side;
} }
//Check that a point is more than given distance from any cliff //Check that a point is more than given distance from any cliff
//Could speed up by checking bounding box //Could speed up by checking bounding box
bool TGArray::is_near_cliff(const double lon1, const double lat1, const double bad_zone) const { bool TGArray::is_near_cliff( const double lon1, const double lat1, const double bad_zone ) const {
if (cliffs_list.size()==0) return false;
SGGeod pt1 = SGGeod::fromDeg(lon1,lat1); if (cliffs_list.size()==0) return false;
for (int i=0;i<cliffs_list.size();i++) {
double dist = cliffs_list[i].MinDist(pt1); SGGeod pt1 = SGGeod::fromDeg(lon1,lat1);
if (dist < bad_zone) return true;
} for ( int i=0;i<cliffs_list.size();i++ ) {
return false; double dist = cliffs_list[i].MinDist(pt1);
if (dist < bad_zone) return true;
}
return false;
} }
TGArray::~TGArray( void ) TGArray::~TGArray( void )
@ -875,10 +920,10 @@ void TGArray::set_array_elev( int col, int row, int val )
bool TGArray::is_open() const bool TGArray::is_open() const
{ {
if ( array_in != NULL ) { if ( array_in != NULL ) {
return true; return true;
} else { } else {
return false; return false;
} }
} }

View file

@ -11,7 +11,7 @@
#include "tg_misc.hxx" #include "tg_misc.hxx"
tgPolygon tgChopper::Clip( const tgPolygon& subject, tgPolygon tgChopper::Clip( const tgPolygon& subject,
const std::string& type, SGBucket& b) const std::string& type, SGBucket& b )
{ {
tgPolygon base, result; tgPolygon base, result;
@ -21,8 +21,7 @@ tgPolygon tgChopper::Clip( const tgPolygon& subject,
base.AddNode( 0, b.get_corner( SG_BUCKET_NE ) ); base.AddNode( 0, b.get_corner( SG_BUCKET_NE ) );
base.AddNode( 0, b.get_corner( SG_BUCKET_NW ) ); base.AddNode( 0, b.get_corner( SG_BUCKET_NW ) );
result = tgPolygon::Intersect( subject, base); result = tgPolygon::Intersect( subject, base );
// Debug: See if numbers of nodes have changed
if ( result.Contours() > 0 ) { if ( result.Contours() > 0 ) {
if ( subject.GetPreserve3D() ) { if ( subject.GetPreserve3D() ) {
@ -68,7 +67,7 @@ void tgChopper::ClipRow( const tgPolygon& subject, const double& center_lat, con
} }
void tgChopper::Add( const tgPolygon& subject, const std::string& type) void tgChopper::Add( const tgPolygon& subject, const std::string& type )
{ {
// bail out immediately if polygon is empty // bail out immediately if polygon is empty
if ( subject.Contours() == 0 ) if ( subject.Contours() == 0 )
@ -95,7 +94,7 @@ void tgChopper::Add( const tgPolygon& subject, const std::string& type)
// We just have a single row - no need to intersect first // We just have a single row - no need to intersect first
SG_LOG( SG_GENERAL, SG_DEBUG, " UN_CLIPPED row - center lat is " << b_min.get_center_lat() ); SG_LOG( SG_GENERAL, SG_DEBUG, " UN_CLIPPED row - center lat is " << b_min.get_center_lat() );
ClipRow( subject, b_min.get_center_lat(), type); ClipRow( subject, b_min.get_center_lat(), type );
} }
else else
{ {
@ -122,7 +121,7 @@ void tgChopper::Add( const tgPolygon& subject, const std::string& type)
clip_row.AddNode( 0, SGGeod::fromDeg( 180.0, clip_top) ); clip_row.AddNode( 0, SGGeod::fromDeg( 180.0, clip_top) );
clip_row.AddNode( 0, SGGeod::fromDeg(-180.0, clip_top) ); clip_row.AddNode( 0, SGGeod::fromDeg(-180.0, clip_top) );
clipped = tgPolygon::Intersect( subject, clip_row); clipped = tgPolygon::Intersect( subject, clip_row );
if ( clipped.TotalNodes() > 0 ) { if ( clipped.TotalNodes() > 0 ) {
if ( subject.GetPreserve3D() ) { if ( subject.GetPreserve3D() ) {

View file

@ -61,7 +61,7 @@ double tgContour::GetArea( void ) const
SGVec2d a, b; SGVec2d a, b;
unsigned int i, j; unsigned int i, j;
if (node_list.size() ) { if ( node_list.size() ) {
j = node_list.size() - 1; j = node_list.size() - 1;
for (i=0; i<node_list.size(); i++) { for (i=0; i<node_list.size(); i++) {
a = SGGeod_ToSGVec2d( node_list[i] ); a = SGGeod_ToSGVec2d( node_list[i] );
@ -77,80 +77,88 @@ double tgContour::GetArea( void ) const
// Check that the two supplied points are on the same side of the contour // Check that the two supplied points are on the same side of the contour
bool tgContour::AreSameSide( const SGGeod& firstpt, const SGGeod& secondpt) const bool tgContour::AreSameSide( const SGGeod& firstpt, const SGGeod& secondpt) const
{ {
//Find equation of line segment joining the points //Find equation of line segment joining the points
double x1 = firstpt.getLatitudeDeg(); double x1 = firstpt.getLatitudeDeg();
double x2 = secondpt.getLatitudeDeg(); double x2 = secondpt.getLatitudeDeg();
double y1 = firstpt.getLongitudeDeg(); double y1 = firstpt.getLongitudeDeg();
double y2 = secondpt.getLongitudeDeg(); double y2 = secondpt.getLongitudeDeg();
//Store differences for later
double xdif = x2-x1; //Store differences for later
double ydif = y2-y1; double xdif = x2-x1;
/*We describe a line parametrically: double ydif = y2-y1;
/*We describe a line parametrically:
x1 (x2-x1) x1 (x2-x1)
L = + t L = + t
y1 (y2-y1) y1 (y2-y1)
with u the parametric coefficient for the second line. with u the parametric coefficient for the second line.
Then the line segments intersect if 0 <= t,u <= 1. Then the line segments intersect if 0 <= t,u <= 1.
To determine t and u we use the approach of Goldman ("Graphics To determine t and u we use the approach of Goldman ("Graphics
Gems" as described in Stack Overflow question 563198). Gems" as described in Stack Overflow question 563198).
if r x s = r_x * s_y - r_y * s_x, then if r x s = r_x * s_y - r_y * s_x, then
t = (q - p) x s / (r x s) t = (q - p) x s / (r x s)
and and
u = (q - p) x r / (r x s) u = (q - p) x r / (r x s)
for line 1 = p + t r, line 2 = q + u s for line 1 = p + t r, line 2 = q + u s
*/ */
//Now cycle over all nodes and count how many times we intersect
int intersect_ct = 0; //Now cycle over all nodes and count how many times we intersect
if (node_list.size()) { int intersect_ct = 0;
int j = node_list.size() - 1; if (node_list.size()) {
for (int i=0;i<node_list.size()-1;i++) { int j = node_list.size() - 1;
double nx1 = node_list[i].getLatitudeDeg(); for (int i=0;i<node_list.size()-1;i++) {
double ny1 = node_list[i].getLongitudeDeg(); double nx1 = node_list[i].getLatitudeDeg();
double nx2 = node_list[i+1].getLatitudeDeg(); double ny1 = node_list[i].getLongitudeDeg();
double ny2 = node_list[i+1].getLongitudeDeg(); double nx2 = node_list[i+1].getLatitudeDeg();
double nydif = ny2-ny1; double ny2 = node_list[i+1].getLongitudeDeg();
double nxdif = nx2-nx1; double nydif = ny2-ny1;
double denom = xdif*nydif - ydif*nxdif; double nxdif = nx2-nx1;
if (denom != 0) { //Not parallel double denom = xdif*nydif - ydif*nxdif;
double crossx = nx1-x1; double crossy = ny1-y1;
double t = (crossx*nydif - crossy*nxdif)/denom; if (denom != 0) { //Not parallel
double u = -1*(xdif*crossy - ydif*crossx)/denom; double crossx = nx1-x1; double crossy = ny1-y1;
// We consider that an intersection at the edge of the line has double t = (crossx*nydif - crossy*nxdif)/denom;
// crossed double u = -1*(xdif*crossy - ydif*crossx)/denom;
// over, that is, they lie on opposite sides. This way we capture // We consider that an intersection at the edge of the line has
// places where the chopper has clipped a cliff on the tile edge // crossed
if (t > -0.0001 && t < 1.0001 && u > -0.0001 && u < 1.0001) intersect_ct++; // over, that is, they lie on opposite sides. This way we capture
} // places where the chopper has clipped a cliff on the tile edge
if (t > -0.0001 && t < 1.0001 && u > -0.0001 && u < 1.0001) intersect_ct++;
}
}
} }
}
bool isinter = (intersect_ct%2 == 0); bool isinter = (intersect_ct%2 == 0);
return isinter; return isinter;
} }
double tgContour::MinDist(const SGGeod& probe) const double tgContour::MinDist(const SGGeod& probe) const {
{ SGVec3d probexyz;
SGVec3d probexyz; SGGeodesy::SGGeodToCart( probe,probexyz );
SGGeodesy::SGGeodToCart(probe,probexyz); double mindist = 100000.0;
double mindist = 100000.0; double dist;
double dist;
if (node_list.size()) { if ( node_list.size() ) {
int j = node_list.size() - 1;
for (int i=0;i<j;i++) { int j = node_list.size() - 1;
SGVec3d start,end;
SGGeodesy::SGGeodToCart(node_list[i],start); for (int i=0;i<j;i++) {
SGGeodesy::SGGeodToCart(node_list[i+1],end); SGVec3d start,end;
SGLineSegment<double> piece = SGLineSegment<double>(start,end); SGGeodesy::SGGeodToCart( node_list[i],start );
dist = distSqr(piece,probexyz); SGGeodesy::SGGeodToCart( node_list[i+1],end );
if (dist < mindist) mindist = dist; SGLineSegment<double> piece = SGLineSegment<double>(start,end);
dist = distSqr( piece,probexyz );
if (dist < mindist) mindist = dist;
}
} }
}
return sqrt(mindist); return sqrt(mindist);
} }
bool tgContour::IsInside( const tgContour& inside, const tgContour& outside ) bool tgContour::IsInside( const tgContour& inside, const tgContour& outside )

View file

@ -137,7 +137,7 @@ tgPolygon tgPolygon::Diff( const tgPolygon& subject, tgPolygon& clip )
} }
//Intersect will keep open paths open //Intersect will keep open paths open
tgPolygon tgPolygon::Intersect( const tgPolygon& subject, const tgPolygon& clip) tgPolygon tgPolygon::Intersect( const tgPolygon& subject, const tgPolygon& clip )
{ {
tgPolygon result; tgPolygon result;
UniqueSGGeodSet all_nodes; UniqueSGGeodSet all_nodes;
@ -162,14 +162,14 @@ tgPolygon tgPolygon::Intersect( const tgPolygon& subject, const tgPolygon& clip)
ClipperLib::Clipper c; ClipperLib::Clipper c;
c.Clear(); c.Clear();
c.AddPaths(clipper_subject, ClipperLib::PolyType::Subject, subject.IsClosed()); c.AddPaths(clipper_subject, ClipperLib::PolyType::Subject, subject.IsClosed() );
c.AddPaths(clipper_clip, ClipperLib::PolyType::Clip, true); c.AddPaths(clipper_clip, ClipperLib::PolyType::Clip, true);
if(subject.IsClosed()) { if(subject.IsClosed()) {
c.Execute(ClipperLib::ClipType::Intersection, clipper_result, ClipperLib::PolyFillType::EvenOdd, ClipperLib::PolyFillType::EvenOdd); c.Execute(ClipperLib::ClipType::Intersection, clipper_result, ClipperLib::PolyFillType::EvenOdd, ClipperLib::PolyFillType::EvenOdd );
result = tgPolygon::FromClipper( clipper_result ); result = tgPolygon::FromClipper( clipper_result );
} }
else { else {
c.Execute(ClipperLib::ClipType::Intersection, clipper_tree_result, ClipperLib::PolyFillType::EvenOdd, ClipperLib::PolyFillType::EvenOdd); c.Execute(ClipperLib::ClipType::Intersection, clipper_tree_result, ClipperLib::PolyFillType::EvenOdd, ClipperLib::PolyFillType::EvenOdd );
result = tgPolygon::FromClipper( clipper_tree_result ); result = tgPolygon::FromClipper( clipper_tree_result );
} }
result = tgPolygon::AddColinearNodes( result, all_nodes ); result = tgPolygon::AddColinearNodes( result, all_nodes );

View file

@ -391,7 +391,7 @@ int main( int argc, char **argv ) {
if( poDS == NULL ) if( poDS == NULL )
{ {
SG_LOG( SG_GENERAL, SG_ALERT, "Failed opening datasource " << datasource ); SG_LOG( SG_GENERAL, SG_ALERT, "Failed opening datasource " << datasource );
exit( 1 ); return EXIT_FAILURE;
} }
SG_LOG( SG_GENERAL, SG_ALERT, "Processing datasource " << datasource ); SG_LOG( SG_GENERAL, SG_ALERT, "Processing datasource " << datasource );
@ -404,7 +404,7 @@ int main( int argc, char **argv ) {
if (poLayer == NULL ) if (poLayer == NULL )
{ {
SG_LOG( SG_GENERAL, SG_ALERT, "Failed opening layer " << argv[i] << " from datasource " << datasource ); SG_LOG( SG_GENERAL, SG_ALERT, "Failed opening layer " << argv[i] << " from datasource " << datasource );
exit( 1 ); return EXIT_FAILURE;
} }
processLayer(poLayer, results ); processLayer(poLayer, results );
} }