00001 #include "PixelArray.h" 00002 #include <math.h> 00003 #include <iostream> 00004 #include <fstream> 00005 00006 PixelArray::PixelArray(){ 00007 iter = pixels.begin();//set up the iterator 00008 } 00009 00010 PixelArray::~PixelArray(){;} 00011 00012 void PixelArray::clearAll(){ 00013 pixels.clear();//clear the vectore 00014 } 00015 00016 int PixelArray::size(){ 00017 return pixels.size(); 00018 } 00019 00020 void PixelArray::add(int x, int y){ 00021 //create and add new PixelProfile to end 00022 pixels.push_back(*(new PixelProfile(x, y))); 00023 iter = pixels.begin();//update iterator 00024 } 00025 00026 /* I'm assuming that most of the time pixels will be accessed in 00027 * the same order they were put into the array. So, we keep an 00028 * iterator around between calls so we can easily grab the next 00029 * pixel. If the next pixel is not the one we need, we run through 00030 * the list to find it. Return NULL if it's not found. 00031 */ 00032 PixelProfile* PixelArray::getPixel(int x, int y){ 00033 int thex = x; 00034 int they = y; 00035 00036 iter++;//increment 00037 00038 if(iter != pixels.end() && iter >= pixels.begin() && (*iter).getX() == thex && (*iter).getY() == they){ 00039 return &(*iter) ;//return if it matches 00040 } 00041 00042 int loopCount = 1 ; 00043 iter = pixels.begin(); 00044 00045 while (iter != pixels.end() && iter >= pixels.begin() && (int)((pixels.size()) / (pow(2.0, loopCount))) != 0 && 00046 (((*iter).getX() != thex) || ((*iter).getY() != they))) { 00047 00048 if ((*iter).getY() > they) { 00049 iter -= (int)((pixels.size()) / (pow(2.0, loopCount))) ; 00050 loopCount++ ; 00051 } 00052 else if ((*iter).getY() < they) { 00053 iter += (int)((pixels.size()) / (pow(2.0, loopCount))) ; 00054 loopCount++ ; 00055 } 00056 else if ((*iter).getY() == they) { 00057 if ((*iter).getX() < thex ) { 00058 iter++ ; 00059 } 00060 else if ((*iter).getX() > thex ) { 00061 iter-- ; 00062 } 00063 00064 } 00065 } 00066 00067 00068 // if value's been found, return it 00069 if ((*iter).getX() == thex && (*iter).getY() == they) { 00070 return &(*iter); 00071 } 00072 00073 // totally give up on quick and fancy and just search from beginning 00074 else{ 00075 iter = pixels.begin(); 00076 while (iter!= pixels.end() && iter >= pixels.begin()){ 00077 if((*iter).getX()== thex && (*iter).getY()== they) { 00078 return &(*iter); 00079 } 00080 iter++; 00081 } 00082 } 00083 00084 // the value is not in the array, return null 00085 return NULL; 00086 } 00087 00088 void PixelArray::addToProfile(int x, int y, double val){ 00089 PixelProfile* pix = getPixel(x, y); 00090 if(pix != NULL)//make sure we found it 00091 pix->add(val); 00092 } 00093 00094 std::string PixelArray::getAsString(){ 00095 std::string str = ""; 00096 std::stringstream ss; 00097 00098 00099 for(std::vector<PixelProfile>::size_type i=0; i<pixels.size(); i++){ 00100 ss << pixels[i].getAsString() << "\n"; 00101 } 00102 str = ss.str(); 00103 00104 return str; 00105 } 00106 00107 00111 std::string PixelArray::getAsString(std::string delim){ 00112 std::string str = ""; 00113 std::stringstream ss; 00114 00115 for(std::vector<PixelProfile>::size_type i=0; i<pixels.size(); i++){ 00116 ss << pixels[i].getAsString(delim) << "\n"; 00117 } 00118 str = ss.str(); 00119 00120 return str; 00121 } 00122 00123 00127 std::string PixelArray::getAsString(std::string delim, std::ofstream &f){ 00128 for(std::vector<PixelProfile>::size_type i=0; i<pixels.size(); i++){ 00129 f << pixels[i].getAsString(delim) << "\n"; 00130 } 00131 return ""; 00132 } 00133 00134 00135 std::string PixelArray::runDriverTest(){ 00136 std::string str = "Starting Driver Test\n"; 00137 00138 //create some PixelProfiles 00139 this->add(1, 2); 00140 this->add(3, 4); 00141 this->add(5, 6); 00142 this->add(7, 8); 00143 this->add(9, 10); 00144 00145 00146 //create some data for each profile 00147 int i = 0 ; 00148 for(i=0; i<5; i++){ 00149 (this->getPixel((2*i)+1, (2*i)+2))->add(i); 00150 } 00151 00152 for(i=0; i<5; i++){ 00153 (this->getPixel((2*i)+1, (2*i)+2))->add(i*4); 00154 } 00155 for(i=0; i<5; i++){ 00156 (this->getPixel((2*i)+1, (2*i)+2))->add(i+9); 00157 } 00158 for(i=0; i<5; i++){ 00159 (this->getPixel((2*i)+1, (2*i)+2))->add(3); 00160 } 00161 00162 //test messager 00163 str.append("pixels created\n"); 00164 //get the string of this PixelArray 00165 str.append(this->getAsString()); 00166 00167 return str; 00168 } 00169
Home |
Search |
Disclaimers & Privacy |
Contact Us GLIMSView Maintainer: dsoltesz@usgs.gov |