//--------------------------------------------------------------------------- #pragma hdrstop #include "Landscape.h" #if CODEGEAR string itos(int i) { // convert int to string stringstream s; s << i; return s.str(); } #endif int Landscape::LandCounter = 0; //--------------------------------------------------------------------------- #pragma package(smart_init) //------------------------------------------------------------------------------ //FUNCTION: Square::Square //ARGUMENTS: none //RETURNS: none //NOTES: Square class default Constructor //------------------------------------------------------------------------------ Square::Square(int x, int y) { sq.x = x; sq.y = y; visits = 0; cost = -99; target = -99; #if READSOURCE source = -99; #endif for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { effcost.cell[i][j] = -1.0; // initialise retained effective costs } } } //------------------------------------------------------------------------------ //FUNCTION: Square::various //ARGUMENTS: none //RETURNS: various //NOTES: Square functions to return individual attributes //------------------------------------------------------------------------------ locn Square::get_locn(void) { return sq; } int Square::get_cost(void) { return cost; } #if READSOURCE int Square::get_source(void) { return source; } #endif int Square::get_target(void) { return target; } int Square::get_visits(void) { if (visits < 0) visits = -visits; return visits; } array3x3d Square::get_effcosts(void) { array3x3d effcosts; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { effcosts.cell[i][j] = effcost.cell[i][j]; } } return effcosts; } //------------------------------------------------------------------------------ //FUNCTION: Square::set_attributes //ARGUMENTS: //RETURNS: none //NOTES: Square functions to set landscape attributes //------------------------------------------------------------------------------ void Square::set_cost(int c) { cost = c; } void Square::set_attributes(int c, int s, int t) { cost = c; #if READSOURCE source = s; #endif target = t; } void Square::inc_visits(void) { if (visits > -1) { visits++; visits = -visits; } } void Square::reset_visits(int option) { if (option == 0) visits = 0; else { // reset to positive if (visits < 0) { visits = -visits; } } } void Square::set_effcosts(array3x3d a) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { effcost.cell[i][j] = a.cell[i][j]; } } } //------------------------------------------------------------------------------ //FUNCTION: Landscape::Landscape //ARGUMENTS: None //RETURNS: None //NOTES: Landscape class constructor //------------------------------------------------------------------------------ Landscape::Landscape() { id = LandCounter; LandCounter++; //Unique identifier for each landscape } //------------------------------------------------------------------------------ //FUNCTION: Landscape::~Landscape //ARGUMENTS: None //RETURNS: None //NOTES: Landscape class destructor //------------------------------------------------------------------------------ Landscape::~Landscape() { #if !VCL #if BATCH cout<>txt>>fcost; cost = (int)fcost; intarget>>txt>>ftarget; target = (int)ftarget; if (i < 5 && target != cost) { max = -990; i = 99; } if (read_source) { insource>>txt>>fsource; source = (int)fsource; if (i < 5 && source != cost) { max = -990; i = 99; } } else source = 0; if (i == 0) xmax = cost; if (i == 1) ymax = cost; if (i == 2) Xorigin = cost; if (i == 3) Yorigin = cost; if (i == 4) cellsize = cost; #if !VCL #if BATCH cout< -1; y--) { for (x = 0; x < xmax; x++) { incost>>cost; intarget>>target; if (read_source) insource>>source; else source = 0; if (cost < 0) { // NODATA square - record a zero pointer Squares.push_back(0); } else { // square with data - set up square in heap memory and store costs Squares.push_back(new Square(x,y)); Squares.back()->set_attributes(cost,source,target); // add cost value to colour table if not already present i = 0; while (i < costs.size()) { if (cost == costs[i]) i = 999999; // cost is already present else i++; } if (i != 999999) { // add cost to table costs.push_back(cost); } } if (cost>max) max = cost; } } // sort costs in colour table into ascending order and assign colour scores // in the range 0-255, approx. equally spaced sort (costs.begin(), costs.end()); for (i = 0; i < costs.size(); i++) { if (costs.size() > 1) { colours.push_back(255 * i / (costs.size()-1)); } } #if !VCL #if BATCH cout<<"Finished reading landscape attributes, xmax = "< -1; y--) { for (x = 0; x < xmax; x++) { if (Squares[i] != 0) { if (ftype == 1) landscp<get_cost()<<" "; if (ftype == 2) { #if READSOURCE landscp<get_source()<<" "; #else landscp<<"-99 "; #endif } if (ftype == 3) landscp<get_cost()<<" "; if (ftype == 4) landscp<get_visits()<<" "; } else // write NODATA value landscp<<"-99 "; i++; } landscp< -1; y--) { for (int x = 0; x < xmax; x++) { if (Squares[i] != 0) { Squares[i]->set_effcosts(null); } i++; } } } //------------------------------------------------------------------------------ //FUNCTION: Landscape::ResetVisits //ARGUMENTS: none //RETURNS: none //NOTES: resets visits of all squares to positive if negative //------------------------------------------------------------------------------ void Landscape::ResetVisits(int option) { int i; i = 0; for (int y = ymax-1; y > -1; y--) { for (int x = 0; x < xmax; x++) { if (Squares[i] != 0) { Squares[i]->reset_visits(option); } i++; } } } int Landscape::get_costcolour(int s) { // returns a colour code (0-255) in which to draw the square int c,i,cost; if (s < 0 || s >= Squares.size()) { // error - return zero return 0; } else { c = i = 0; cost = Squares[s]->get_cost(); while (i < costs.size()) { if (cost == costs[i]) c = colours[i]; i++; } } return c; } //------------------------------------------------------------------------------