1
0
Fork 0

TGArray: Speed up the construction and destruction

This commit is contained in:
James Turner 2012-11-23 18:30:37 +01:00 committed by Christian Schmitt
parent 7ed3965933
commit 1b53799d51
2 changed files with 89 additions and 101 deletions

View file

@ -17,25 +17,19 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id: array.cxx,v 1.24 2005-11-10 16:26:59 curt Exp $
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include <config.h> # include <config.h>
#endif #endif
#include <cstring>
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <iostream>
#include <string>
#include <simgear/constants.h>
#include <simgear/misc/sgstream.hxx> #include <simgear/misc/sgstream.hxx>
#include <simgear/debug/logstream.hxx> #include <simgear/debug/logstream.hxx>
#include <simgear/misc/strutils.hxx> #include <simgear/misc/strutils.hxx>
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_path.hxx>
#include <simgear/math/leastsqs.hxx>
#include "array.hxx" #include "array.hxx"
@ -47,10 +41,7 @@ TGArray::TGArray( void ):
fitted_in(NULL) fitted_in(NULL)
{ {
SG_LOG(SG_GENERAL, SG_DEBUG, "class TGArray CONstructor called." ); SG_LOG(SG_GENERAL, SG_DEBUG, "class TGArray CONstructor called." );
in_data = new int*[ARRAY_SIZE_1]; in_data = new int[ARRAY_SIZE_1 * ARRAY_SIZE_1];
for (int i = 0; i < ARRAY_SIZE_1; i++) {
in_data[i] = new int[ARRAY_SIZE_1];
}
} }
@ -59,9 +50,7 @@ TGArray::TGArray( const string &file ):
fitted_in(NULL) fitted_in(NULL)
{ {
SG_LOG(SG_GENERAL, SG_DEBUG, "class TGArray CONstructor called." ); SG_LOG(SG_GENERAL, SG_DEBUG, "class TGArray CONstructor called." );
in_data = new int* [ARRAY_SIZE_1]; in_data = new int[ARRAY_SIZE_1 * ARRAY_SIZE_1];
for (int i = 0; i < ARRAY_SIZE_1; i++)
in_data[i] = new int[ARRAY_SIZE_1];
SG_LOG(SG_GENERAL, SG_ALERT, "ps TGArray CONstructor called." ); SG_LOG(SG_GENERAL, SG_ALERT, "ps TGArray CONstructor called." );
@ -137,7 +126,7 @@ TGArray::parse( SGBucket& b ) {
for ( int i = 0; i < cols; i++ ) { for ( int i = 0; i < cols; i++ ) {
for ( int j = 0; j < rows; j++ ) { for ( int j = 0; j < rows; j++ ) {
*array_in >> in_data[i][j]; *array_in >> in_data[(i * ARRAY_SIZE_1) + j];
} }
} }
@ -160,12 +149,7 @@ TGArray::parse( SGBucket& b ) {
SG_LOG(SG_GENERAL, SG_DEBUG, " cols = " << cols << " rows = " << rows ); SG_LOG(SG_GENERAL, SG_DEBUG, " cols = " << cols << " rows = " << rows );
SG_LOG(SG_GENERAL, SG_DEBUG, " col_step = " << col_step << " row_step = " << row_step ); SG_LOG(SG_GENERAL, SG_DEBUG, " col_step = " << col_step << " row_step = " << row_step );
for ( int i = 0; i < cols; i++ ) { memset(in_data, 0, sizeof(int) * ARRAY_SIZE_1 * ARRAY_SIZE_1);
for ( int j = 0; j < rows; j++ ) {
in_data[i][j] = 0;
}
}
SG_LOG(SG_GENERAL, SG_DEBUG, " File not open, so using zero'd data" ); SG_LOG(SG_GENERAL, SG_DEBUG, " File not open, so using zero'd data" );
} }
@ -209,7 +193,7 @@ bool TGArray::write( const string root_dir, SGBucket& b ) {
gzprintf( fp, "%d %d %d %d\n", cols, (int)col_step, rows, (int)row_step ); gzprintf( fp, "%d %d %d %d\n", cols, (int)col_step, rows, (int)row_step );
for ( int i = 0; i < cols; ++i ) { for ( int i = 0; i < cols; ++i ) {
for ( int j = 0; j < rows; ++j ) { for ( int j = 0; j < rows; ++j ) {
gzprintf( fp, "%d ", (int)in_data[i][j] ); gzprintf( fp, "%d ", get_array_elev(i, j) );
} }
gzprintf( fp, "\n" ); gzprintf( fp, "\n" );
} }
@ -225,7 +209,7 @@ void TGArray::remove_voids( ) {
// array is a void.) // array is a void.)
bool have_void = true; bool have_void = true;
int last_elev = -32768; int last_elev = -32768;
for ( int pass = 0; pass < 2 && have_void; ++pass ) { for ( int pass = 0; pass < 2 && have_void; ++pass ) {
// attempt to fill in any void data horizontally // attempt to fill in any void data horizontally
for ( int i = 0; i < cols; i++ ) { for ( int i = 0; i < cols; i++ ) {
int j; int j;
@ -234,10 +218,10 @@ void TGArray::remove_voids( ) {
last_elev = -32768; last_elev = -32768;
have_void = false; have_void = false;
for ( j = 0; j < rows; j++ ) { for ( j = 0; j < rows; j++ ) {
if ( in_data[i][j] > -9000 ) { if ( get_array_elev(i,j) > -9000 ) {
last_elev = in_data[i][j]; last_elev = get_array_elev(i,j) ;
} else if ( last_elev > -9000 ) { } else if ( last_elev > -9000 ) {
in_data[i][j] = last_elev; set_array_elev(i, j, last_elev);
} else { } else {
have_void = true; have_void = true;
} }
@ -246,10 +230,10 @@ void TGArray::remove_voids( ) {
last_elev = -32768; last_elev = -32768;
have_void = false; have_void = false;
for ( j = rows - 1; j >= 0; j-- ) { for ( j = rows - 1; j >= 0; j-- ) {
if ( in_data[i][j] > -9000 ) { if ( get_array_elev(i,j) > -9000 ) {
last_elev = in_data[i][j]; last_elev = get_array_elev(i,j);
} else if ( last_elev > -9000 ) { } else if ( last_elev > -9000 ) {
in_data[i][j] = last_elev; set_array_elev(i, j, last_elev);
} else { } else {
have_void = true; have_void = true;
} }
@ -264,10 +248,10 @@ void TGArray::remove_voids( ) {
last_elev = -32768; last_elev = -32768;
have_void = false; have_void = false;
for ( i = 0; i < cols; i++ ) { for ( i = 0; i < cols; i++ ) {
if ( in_data[i][j] > -9999 ) { if ( get_array_elev(i,j) > -9999 ) {
last_elev = in_data[i][j]; last_elev = get_array_elev(i,j);
} else if ( last_elev > -9999 ) { } else if ( last_elev > -9999 ) {
in_data[i][j] = last_elev; set_array_elev(i, j, last_elev);
} else { } else {
have_void = true; have_void = true;
} }
@ -277,10 +261,10 @@ void TGArray::remove_voids( ) {
last_elev = -32768; last_elev = -32768;
have_void = false; have_void = false;
for ( i = cols - 1; i >= 0; i-- ) { for ( i = cols - 1; i >= 0; i-- ) {
if ( in_data[i][j] > -9999 ) { if ( get_array_elev(i,j) > -9999 ) {
last_elev = in_data[i][j]; last_elev = get_array_elev(i,j);
} else if ( last_elev > -9999 ) { } else if ( last_elev > -9999 ) {
in_data[i][j] = last_elev; set_array_elev(i, j, last_elev);
} else { } else {
have_void = true; have_void = true;
} }
@ -294,8 +278,8 @@ void TGArray::remove_voids( ) {
// as a panic fall back. // as a panic fall back.
for ( int i = 0; i < cols; i++ ) { for ( int i = 0; i < cols; i++ ) {
for ( int j = 0; j < rows; j++ ) { for ( int j = 0; j < rows; j++ ) {
if ( in_data[i][j] <= -9999 ) { if ( get_array_elev(i,j) <= -9999 ) {
in_data[i][j] = 0; set_array_elev(i, j, 0);
} }
} }
} }
@ -305,7 +289,7 @@ void TGArray::remove_voids( ) {
// add a node to the output corner node list // add a node to the output corner node list
void TGArray::add_corner_node( int i, int j, double val ) { void TGArray::add_corner_node( int i, int j, double val ) {
double x = (originx + i * col_step) / 3600.0; double x = (originx + i * col_step) / 3600.0;
double y = (originy + j * row_step) / 3600.0; double y = (originy + j * row_step) / 3600.0;
SG_LOG(SG_GENERAL, SG_DEBUG, "originx = " << originx << " originy = " << originy ); SG_LOG(SG_GENERAL, SG_DEBUG, "originx = " << originx << " originy = " << originy );
@ -355,9 +339,9 @@ int TGArray::fit( double error ) {
colmax = cols; colmax = cols;
rowmin = 0; rowmin = 0;
rowmax = rows; rowmax = rows;
cout << " Fitting region = " << colmin << "," << rowmin << " to " cout << " Fitting region = " << colmin << "," << rowmin << " to "
<< colmax << "," << rowmax << endl;; << colmax << "," << rowmax << endl;;
// generate corners list // generate corners list
add_corner_node( colmin, rowmin, in_data[colmin][rowmin] ); add_corner_node( colmin, rowmin, in_data[colmin][rowmin] );
add_corner_node( colmin, rowmax-1, in_data[colmin][rowmax] ); add_corner_node( colmin, rowmax-1, in_data[colmin][rowmax] );
@ -402,16 +386,16 @@ int TGArray::fit( double error ) {
max_error = least_squares_max_error(x, y, n, m, b); max_error = least_squares_max_error(x, y, n, m, b);
/* /*
printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n", printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n",
start, end, ave_error, max_error, m, b); start, end, ave_error, max_error, m, b);
f = fopen("gnuplot.dat", "w"); f = fopen("gnuplot.dat", "w");
for ( j = 0; j <= end; j++) { for ( j = 0; j <= end; j++) {
fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ), fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ),
in_data[row][j]); in_data[row][j]);
} }
for ( j = start; j <= end; j++) { for ( j = start; j <= end; j++) {
fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ), fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ),
in_data[row][j]); in_data[row][j]);
} }
fclose(f); fclose(f);
@ -422,7 +406,7 @@ int TGArray::fit( double error ) {
if ( max_error > error_sq ) { if ( max_error > error_sq ) {
good_fit = false; good_fit = false;
} }
end++; end++;
} }
@ -436,14 +420,14 @@ int TGArray::fit( double error ) {
// the data set // the data set
end--; end--;
} }
least_squares(x, y, n, &m, &b); least_squares(x, y, n, &m, &b);
// ave_error = least_squares_error(x, y, n, m, b); // ave_error = least_squares_error(x, y, n, m, b);
max_error = least_squares_max_error(x, y, n, m, b); max_error = least_squares_max_error(x, y, n, m, b);
/* /*
printf("\n"); printf("\n");
printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n", printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n",
start, end, ave_error, max_error, m, b); start, end, ave_error, max_error, m, b);
printf("\n"); printf("\n");
@ -468,9 +452,9 @@ int TGArray::fit( double error ) {
dem = fopen("gnuplot.dat", "w"); dem = fopen("gnuplot.dat", "w");
for ( j = 0; j < ARRAY_SIZE_1; j++) { for ( j = 0; j < ARRAY_SIZE_1; j++) {
fprintf(dem, "%.2f %.2f\n", 0.0 + ( j * col_step ), fprintf(dem, "%.2f %.2f\n", 0.0 + ( j * col_step ),
in_data[j][row]); in_data[j][row]);
} }
fclose(dem); fclose(dem);
*/ */
@ -499,7 +483,7 @@ double TGArray::closest_nonvoid_elev( double lon, double lat ) const {
for ( int col = 0; col < cols; col++ ) { for ( int col = 0; col < cols; col++ ) {
Point3D p1(originx + col * col_step, originy + row * row_step, 0.0); Point3D p1(originx + col * col_step, originy + row * row_step, 0.0);
double dist = p0.distance3D( p1 ); double dist = p0.distance3D( p1 );
double elev = in_data[col][row]; double elev = get_array_elev(col, row);
if ( dist < mindist && elev > -9000 ) { if ( dist < mindist && elev > -9000 ) {
mindist = dist; mindist = dist;
minelev = elev; minelev = elev;
@ -526,8 +510,8 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
int x1, x2, x3, y1, y2, y3; int x1, x2, x3, y1, y2, y3;
float z1, z2, z3; float z1, z2, z3;
int xindex, yindex; int xindex, yindex;
/* determine if we are in the lower triangle or the upper triangle /* determine if we are in the lower triangle or the upper triangle
______ ______
| /| | /|
| / | | / |
@ -566,17 +550,17 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
if ( dx > dy ) { if ( dx > dy ) {
// lower triangle // lower triangle
x1 = xindex; x1 = xindex;
y1 = yindex; y1 = yindex;
z1 = in_data[x1][y1]; z1 = get_array_elev(x1, y1);
x2 = xindex + 1; x2 = xindex + 1;
y2 = yindex; y2 = yindex;
z2 = in_data[x2][y2]; z2 = get_array_elev(x2, y2);
x3 = xindex + 1; x3 = xindex + 1;
y3 = yindex + 1; y3 = yindex + 1;
z3 = in_data[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
@ -585,7 +569,7 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
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 {
@ -594,18 +578,18 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
} else { } else {
// upper triangle // upper triangle
x1 = xindex; x1 = xindex;
y1 = yindex; y1 = yindex;
z1 = in_data[x1][y1]; z1 = get_array_elev(x1, y1);
x2 = xindex;
y2 = yindex + 1;
z2 = get_array_elev(x2, y2);
x3 = xindex + 1;
y3 = yindex + 1;
z3 = get_array_elev(x3, y3);
x2 = xindex;
y2 = yindex + 1;
z2 = in_data[x2][y2];
x3 = xindex + 1;
y3 = yindex + 1;
z3 = in_data[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 );
@ -613,7 +597,7 @@ double TGArray::altitude_from_grid( double lon, double lat ) const {
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 {
@ -657,7 +641,7 @@ void TGArray::outputmesh_output_nodes( const string& fg_root, SGBucket& p )
cout << " fg_root = " << fg_root << " Base Path = " << base_path << endl; cout << " fg_root = " << fg_root << " Base Path = " << base_path << endl;
dir = fg_root + "/" + base_path; dir = fg_root + "/" + base_path;
cout << " Dir = " << dir << endl; cout << " Dir = " << dir << endl;
// stat() directory and create if needed // stat() directory and create if needed
errno = 0; errno = 0;
result = stat(dir.c_str(), &stat_buf); result = stat(dir.c_str(), &stat_buf);
@ -695,9 +679,9 @@ void TGArray::outputmesh_output_nodes( const string& fg_root, SGBucket& p )
} }
for ( i = 1; i <= excount; i++ ) { for ( i = 1; i <= excount; i++ ) {
fscanf(fd, "%d %lf %lf %lf\n", &junki, fscanf(fd, "%d %lf %lf %lf\n", &junki,
&exnodes[i][0], &exnodes[i][1], &exnodes[i][2]); &exnodes[i][0], &exnodes[i][1], &exnodes[i][2]);
printf("(extra) %d %.2f %.2f %.2f\n", printf("(extra) %d %.2f %.2f %.2f\n",
i, exnodes[i][0], exnodes[i][1], exnodes[i][2]); i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
} }
fclose(fd); fclose(fd);
@ -720,7 +704,7 @@ void TGArray::outputmesh_output_nodes( const string& fg_root, SGBucket& p )
// now write out extra node data // now write out extra node data
for ( i = 1; i <= excount; i++ ) { for ( i = 1; i <= excount; i++ ) {
fprintf(fd, "%d %.2f %.2f %.2f\n", fprintf(fd, "%d %.2f %.2f %.2f\n",
i, exnodes[i][0], exnodes[i][1], exnodes[i][2]); i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
} }
@ -729,9 +713,9 @@ void TGArray::outputmesh_output_nodes( const string& fg_root, SGBucket& p )
for ( j = rowmin; j <= rowmax; j++ ) { for ( j = rowmin; j <= rowmax; j++ ) {
for ( i = colmin; i <= colmax; i++ ) { for ( i = colmin; i <= colmax; i++ ) {
if ( out_data[i][j] > -9000.0 ) { if ( out_data[i][j] > -9000.0 ) {
fprintf(fd, "%d %.2f %.2f %.2f\n", fprintf(fd, "%d %.2f %.2f %.2f\n",
count++, count++,
originx + (double)i * col_step, originx + (double)i * col_step,
originy + (double)j * row_step, originy + (double)j * row_step,
out_data[i][j]); out_data[i][j]);
} }
@ -744,10 +728,17 @@ void TGArray::outputmesh_output_nodes( const string& fg_root, SGBucket& p )
#endif #endif
TGArray::~TGArray( void ) { TGArray::~TGArray( void )
for (int i = 0; i < ARRAY_SIZE_1; i++) {
delete [] in_data[i]; delete in_data;
delete [] in_data;
} }
int TGArray::get_array_elev( int col, int row ) const
{
return in_data[(col * ARRAY_SIZE_1) + row];
}
void TGArray::set_array_elev( int col, int row, int val )
{
in_data[(col * ARRAY_SIZE_1) + row] = val;
}

View file

@ -18,16 +18,15 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
// //
// $Id: array.hxx,v 1.18 2005-11-10 16:26:59 curt Exp $
#ifndef _ARRAY_HXX #ifndef _ARRAY_HXX
#define _ARRAY_HXX #define _ARRAY_HXX
#ifndef __cplusplus #ifndef __cplusplus
# error This library requires C++ # error This library requires C++
#endif #endif
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include <config.h> # include <config.h>
@ -55,15 +54,15 @@ private:
// coordinates (in arc seconds) of south west corner // coordinates (in arc seconds) of south west corner
double originx, originy; double originx, originy;
// number of columns and rows // number of columns and rows
int cols, rows; int cols, rows;
// Distance between column and row data points (in arc seconds) // Distance between column and row data points (in arc seconds)
double col_step, row_step; double col_step, row_step;
// pointers to the actual grid data allocated here // pointers to the actual grid data allocated here
int **in_data; int *in_data;
// float (*out_data)[ARRAY_SIZE_1]; // float (*out_data)[ARRAY_SIZE_1];
// output nodes // output nodes
@ -83,7 +82,7 @@ public:
bool open ( const std::string& file_base ); bool open ( const std::string& file_base );
// return if array was successfully opened or not // return if array was successfully opened or not
inline bool is_open() { inline bool is_open() {
if ( array_in != NULL ) { if ( array_in != NULL ) {
return array_in->is_open(); return array_in->is_open();
} else { } else {
@ -129,12 +128,10 @@ public:
inline point_list get_corner_list() const { return corner_list; } inline point_list get_corner_list() const { return corner_list; }
inline point_list get_fitted_list() const { return fitted_list; } inline point_list get_fitted_list() const { return fitted_list; }
inline int get_array_elev( int col, int row ) { int get_array_elev( int col, int row ) const;
return in_data[col][row]; void set_array_elev( int col, int row, int val );
}
inline void set_array_elev( int col, int row, int val ) {
in_data[col][row] = val;
}
inline Point3D get_fitted_pt( int i ) { inline Point3D get_fitted_pt( int i ) {
return fitted_list[i]; return fitted_list[i];
} }