Page principale | Hiérarchie des classes | Liste des classes | Liste des fichiers | Membres de classe | Membres de fichier

Node.cpp

Aller à la documentation de ce fichier.
00001 /// \file 00002 /// Implémentation de la classe Node 00003 #include "Node.hpp" 00004 /////////////////////////////////////////////////////////////////////////////// 00005 /// Node 00006 /////////////////////////////////////////////////////////////////////////////// 00007 Node::Node(){ 00008 p = new Node_Leaf(); 00009 } 00010 Node::Node(Node_Leaf* node){ 00011 p = node; 00012 ++p->use; 00013 } 00014 Node Node::CreateLeaf(const string label){ 00015 Node_Leaf* ret = new Node_Leaf(); 00016 return ret; 00017 } 00018 Node Node::CreateGraph(const string label){ 00019 Node_Leaf* ret = new Node_Graph(); 00020 return ret; 00021 } 00022 ///. 00023 /// Crée un grid graphe à partir d'une image 00024 Node Node::CreateGridGraph(PNG image, string label){ 00025 Trace trace("CreateGridNode", channelGraph); 00026 // Le graphe 00027 Node ret = Node::CreateGraph(label); 00028 // Les points du graphe 00029 vector<Node> v; 00030 // Dimension de l'image 00031 int height = image.getHeight(); 00032 int width = image.getWidth(); 00033 // On crée un noeud par point de l'image 00034 for(int j = 0; j < height; j++){ 00035 for(int i = 0; i < width; i++){ 00036 ostringstream o; 00037 o << "LEAF[" << i << ":" << j << "]"; 00038 Node n = Node::CreateLeaf(o.str()); 00039 ret.addNode(n); 00040 n.setWeight(0); 00041 // On associe le pixel au noeud 00042 Pixel pix(i, j, image.getColor(i, j)); 00043 n.setPixel(pix); 00044 v.push_back(n); 00045 } 00046 } 00047 // On parcourt l'image 00048 for(int i = 0; i < width; i++){ 00049 for(int j = 0; j < height; j++){ 00050 // Noeud (i, j) 00051 Node n = v[i + j * width];//.getNode(i, j); 00052 // Couleur du noeud (i, j) 00053 Color nColor = n.getPixel().getColor(); 00054 if(i + 1 < width){ 00055 // Noeud à droite (i + 1, j) 00056 Node n1 = v[i + 1 + j * width]; 00057 Color n1Color = n1.getPixel().getColor(); 00058 float w = nColor.distance(n1Color); 00059 Edge e1(n, n1, w); 00060 e1.setCoord(n.getPixel().getX(), n.getPixel().getY(), 00061 n1.getPixel().getX(), n1.getPixel().getY()); 00062 ret.addEdge(e1); 00063 } 00064 if(i > 0 && j + 1 < height){ 00065 // Noeud en bas à gauche (i - 1, j + 1) 00066 Node n1 = v[i - 1 + (j + 1) * width]; 00067 Color n1Color = n1.getPixel().getColor(); 00068 float w = nColor.distance(n1Color); 00069 Edge e1(n, n1, w); 00070 e1.setCoord(n.getPixel().getX(), n.getPixel().getY(), 00071 n1.getPixel().getX(), n1.getPixel().getY()); 00072 ret.addEdge(e1); 00073 } 00074 if(i + 1 < width && j + 1 < height){ 00075 // Noeud en bas à droite (i + 1, j + 1) 00076 Node n1 = v[i + 1 + (j + 1) * width]; 00077 Color n1Color = n1.getPixel().getColor(); 00078 float w = nColor.distance(n1Color); 00079 Edge e1(n, n1, w); 00080 e1.setCoord(n.getPixel().getX(), n.getPixel().getY(), 00081 n1.getPixel().getX(), n1.getPixel().getY()); 00082 ret.addEdge(e1); 00083 } 00084 if(j + 1 < height){ 00085 // Noeud en bas (i, j + 1) 00086 Node n2 = v[i + (j + 1) * width]; 00087 Color n2Color = n2.getPixel().getColor(); 00088 float w = nColor.distance(n2Color); 00089 Edge e2(n, n2, w); 00090 e2.setCoord(n.getPixel().getX(), n.getPixel().getY(), 00091 n2.getPixel().getX(), n2.getPixel().getY()); 00092 00093 ret.addEdge(e2); 00094 } 00095 } 00096 } 00097 return ret; 00098 } 00099 Node::Node(const Node& node){ 00100 p = node.p; 00101 ++p->use; 00102 } 00103 Node& Node::operator=(const Node& rhs){ 00104 rhs.p->use++; 00105 if(--p->use == 0){ 00106 delete p; 00107 } 00108 p = rhs.p; 00109 return *this; 00110 } 00111 Node::~Node(){ 00112 if(--p->use == 0){ 00113 delete p; 00114 } 00115 } 00116 /* 00117 ENODE_TYPE Node::getType()const{ 00118 return p->getType(); 00119 } 00120 */ 00121 void Node::setRank(int i){ 00122 p->setRank(i); 00123 } 00124 int Node::getRank()const{ 00125 return p->getRank(); 00126 } 00127 void Node::setWeight(double weight){ 00128 p->setWeight(weight); 00129 } 00130 double Node::getWeight(){ 00131 return p->getWeight(); 00132 } 00133 void Node::setPixel(Pixel pixel){ 00134 p->setPixel(pixel); 00135 } 00136 Pixel Node::getPixel()const{ 00137 return p->getPixel(); 00138 } 00139 void Node::addNode(Node node){ 00140 p->addNode(node); 00141 } 00142 void Node::addEdge(Edge edge){ 00143 p->addEdge(edge); 00144 } 00145 string Node::toString(int level, bool child, bool edge)const{ 00146 return p->toString(level, child, edge); 00147 } 00148 ///. 00149 /// Produit une carte 00150 PixelMap* Node::makeMap(int level)const{ 00151 Trace trace("makeMap", channelGraph); 00152 int x1, y1, x2, y2; 00153 x1 = y1 = x2 = y2 = 0; 00154 p->_size(x1, y1, x2, y2); 00155 int width = (x2 - x1) + 1; 00156 int height = (y2 - y1) + 1; 00157 if((width > 0) && (height > 0)){ 00158 PixelMap* ret = new PixelMap(width, height); 00159 p->makeMap(ret, level, 0, 1); 00160 return ret; 00161 } 00162 return NULL; 00163 } 00164 00165 00166 ///. 00167 /// Produit un fichier PNG 00168 /// C'est ici qu'on démarre la récursion 00169 void Node::drawPNG(string fileName, int level)const{ 00170 Trace trace("Node_Graph::drawPNG()", channelGraph); 00171 int x1, y1, x2, y2; 00172 x1 = y1 = x2 = y2 = 0; 00173 p->_size(x1, y1, x2, y2); 00174 int width = (x2 - x1) + 1; 00175 int height = (y2 - y1) + 1; 00176 if((width > 0) && (height > 0)){ 00177 PNG output(width, height, PNG_GRAY, fileName); 00178 Color c; 00179 p->drawPNG(output, level, c, 1); 00180 output.write(); 00181 } 00182 } 00183 ///. 00184 /// Produit un fichier PNG des bords du graphe 00185 void Node::drawBorderPNG(string fileName, int level)const{ 00186 Trace trace("drawBorderPNG", channelGraph); 00187 PixelMap* map = makeMap(level); 00188 map->drawBorderPNG(fileName); 00189 delete map; 00190 } 00191 00192 00193 ///. 00194 /// Produit un fichier PS 00195 /// C'est ici qu'on démarre la récursion 00196 void Node::drawEPS(string fileName, int level)const{ 00197 Trace trace("Node_Graph::drawEPS()", channelGraph); 00198 int x1, y1, x2, y2; 00199 x1 = y1 = x2 = y2 = 0; 00200 p->_size(x1, y1, x2, y2); 00201 int width = (x2 - x1) + 1; 00202 int height = (y2 - y1) + 1; 00203 if((width > 0) && (height > 0)){ 00204 EPSWriter output(fileName, width, height); 00205 Color c; 00206 p->drawNodeEPS(output, level, c, 1, height); 00207 output.write(); 00208 } 00209 } 00210 void Node::sortEdge(){ 00211 p->sortEdge(); 00212 } 00213 void Node::sortEdge(float& min, float& max){ 00214 p->sortEdge(min, max); 00215 } 00216 int Node::getNodeSize(){ 00217 return p->getNodeSize(); 00218 } 00219 int Node::getLeafSize(){ 00220 return p->getLeafSize(); 00221 } 00222 int Node::getEdgeSize(){ 00223 return p->getEdgeSize(); 00224 } 00225 void Node::merge(Node node){ 00226 p->merge(node); 00227 } 00228 Node_Iterator Node::getNodeIterator(){ 00229 return p->getNodeIterator(); 00230 } 00231 Edge_Iterator Node::getEdgeIterator(){ 00232 return p->getEdgeIterator(); 00233 } 00234 /////////////////////////////////////////////////////////////////////////////// 00235 /// Node_Leaf 00236 /////////////////////////////////////////////////////////////////////////////// 00237 Node_Leaf::Node_Leaf(): 00238 use(0), 00239 rank(-1), 00240 weight(0){ 00241 } 00242 string Node_Leaf::toString(int l, bool, bool) const{ 00243 std::ostringstream o; 00244 for(int i = 0; i < l; i++){ 00245 o << " "; 00246 } 00247 o << "LEAF USE " << use << " WEIGHT " << weight << " PIXEL " << pixel.toString(); 00248 return o.str(); 00249 } 00250 void Node_Leaf::_size(int& x1, int& y1, int& x2, int& y2){ 00251 x1 = (getPixel().getX() < x1 ? getPixel().getX() : x1); 00252 y1 = (getPixel().getY() < y1 ? getPixel().getY() : y1); 00253 x2 = (getPixel().getX() > x2 ? getPixel().getX() : x2); 00254 y2 = (getPixel().getY() > y2 ? getPixel().getY() : y2); 00255 } 00256 void Node_Leaf::addNode(Node){} 00257 void Node_Leaf::addEdge(Edge){} 00258 void Node_Leaf::setRank(int i){ 00259 rank = i; 00260 } 00261 int Node_Leaf::getRank() const { 00262 return rank; 00263 } 00264 void Node_Leaf::setWeight(double _weight){ 00265 weight = _weight; 00266 } 00267 double Node_Leaf::getWeight()const{ 00268 return weight; 00269 } 00270 void Node_Leaf::setPixel(Pixel _pixel){ 00271 pixel = _pixel; 00272 } 00273 Pixel Node_Leaf::getPixel()const{ 00274 return pixel; 00275 } 00276 int Node_Leaf::getNodeSize()const { 00277 return 1; 00278 } 00279 int Node_Leaf::getLeafSize()const { 00280 return 1; 00281 } 00282 int Node_Leaf::getEdgeSize()const{ 00283 return 0; 00284 } 00285 // Produit une carte d'un noeud 00286 PixelMap* Node_Leaf::makeMap(PixelMap* map, int, int label, int)const{ 00287 map->addPixel(pixel, label); 00288 } 00289 00290 ///. 00291 /// Pour un noeud feuille on produit le pixel avec la couleur 00292 /// correspondante 00293 void Node_Leaf::drawPNG(PNG image, int level, Color c, int){ 00294 if(level == -1){ 00295 image.setColor(pixel.getX(), pixel.getY(), pixel.getColor()); 00296 } else { 00297 image.setColor(pixel.getX(), pixel.getY(), c); 00298 } 00299 } 00300 ///. 00301 /// Pour un noeud feuille on produit le pixel avec la couleur 00302 /// correspondante 00303 void Node_Leaf::drawNodeEPS(EPSWriter& writer, int level, Color c, int, int height){ 00304 if(level == -1){ 00305 writer.setColor(pixel.getColor()); 00306 } else { 00307 writer.setColor(c); 00308 } 00309 writer.drawRound(pixel.getX(), (height - pixel.getY())); 00310 } 00311 Node_Iterator Node_Leaf::getNodeIterator()const{ 00312 return Node_Iterator(this); 00313 } 00314 Edge_Iterator Node_Leaf::getEdgeIterator()const{ 00315 return Edge_Iterator(this); 00316 } 00317 /////////////////////////////////////////////////////////////////////////////// 00318 /// Node_Graph 00319 /////////////////////////////////////////////////////////////////////////////// 00320 Node_Graph::Node_Graph(){} 00321 ///. 00322 /// asdfjasdjféasd 00323 /// asdfljlasjdfléasd 00324 string Node_Graph::toString(int l, bool child, bool edge)const{ 00325 std::ostringstream o; 00326 for(int i = 0; i < l; i++){ 00327 o << " "; 00328 } 00329 o << " LEVEL " << l << " " << " USE " << use << " SIZE " << vNode.size() 00330 << " PIXEL " << pixel.toString(); 00331 if(child){ 00332 o << "\n"; 00333 for(Node_Iterator it = getNodeIterator(); it.hasNext(); ){ 00334 Node n = it.next(); 00335 o << n.toString(l + 1, child, edge) << "\n"; 00336 } 00337 } 00338 if(edge){ 00339 for(Edge_Iterator it = getEdgeIterator(); it.hasNext();){ 00340 Edge e = it.next(); 00341 o << e.toString() << endl; 00342 } 00343 } 00344 return o.str(); 00345 } 00346 void Node_Graph::_size(int& x1, int& y1, int& x2, int& y2){ 00347 for(Node_Iterator it = getNodeIterator(); it.hasNext();){ 00348 Node n = it.next(); 00349 n.p->_size(x1, y1, x2, y2); 00350 } 00351 } 00352 int Node_Graph::getNodeSize()const{ 00353 return vNode.size(); 00354 } 00355 int Node_Graph::getLeafSize()const{ 00356 int ret = 0; 00357 for(Node_Iterator it = getNodeIterator(); it.hasNext();){ 00358 Node n = it.next(); 00359 ret += n.getLeafSize(); 00360 } 00361 return ret; 00362 } 00363 int Node_Graph::getEdgeSize()const{ 00364 return vEdge.size(); 00365 } 00366 void Node_Graph::addNode(Node node){ 00367 vNode.push_back(node); 00368 } 00369 void Node_Graph::addEdge(Edge edge){ 00370 vEdge.push_back(edge); 00371 } 00372 void Node_Graph::merge(Node node){ 00373 Node_Graph* n = (Node_Graph*)node.p; 00374 vNode.insert(vNode.end(), n->vNode.begin(), n->vNode.end()); 00375 vEdge.insert(vEdge.end(), n->vEdge.begin(), n->vEdge.end()); 00376 } 00377 void Node_Graph::sortEdge(){ 00378 sort(vEdge.begin(), vEdge.end(), compareEdge); 00379 } 00380 void Node_Graph::sortEdge(float& min, float& max){ 00381 sort(vEdge.begin(), vEdge.end(), compareEdge); 00382 if(vEdge.size() > 0){ 00383 min = vEdge[0].getWeight(); 00384 max = vEdge[vEdge.size() - 1].getWeight(); 00385 } else { 00386 min = max = 0.0; 00387 } 00388 } 00389 Node_Iterator Node_Graph::getNodeIterator()const{ 00390 return Node_Iterator(this); 00391 } 00392 Edge_Iterator Node_Graph::getEdgeIterator()const{ 00393 return Edge_Iterator(this); 00394 } 00395 // Produit une carte d'un noeud 00396 PixelMap* Node_Graph::makeMap(PixelMap* map, int level, int label, int depth)const{ 00397 int i = 0; 00398 for(Node_Iterator it = getNodeIterator(); it.hasNext();){ 00399 Node node = it.next(); 00400 if(level == depth){ 00401 label = i; 00402 } 00403 node.p->makeMap(map, level, label, depth + 1); 00404 i++; 00405 } 00406 } 00407 00408 00409 ///. 00410 /// On poursuit la récursion et on détermine la couleur 00411 /// du cluster 00412 void Node_Graph::drawPNG(PNG image, int level, Color c, int depth){ 00413 int i = 0; 00414 for(Node_Iterator it = getNodeIterator(); it.hasNext();){ 00415 Node node = it.next(); 00416 if(level == 0 && depth == 1){ 00417 c = node.getPixel().getColor(); 00418 } else if(level == depth){ 00419 c = Color::getColor(i); 00420 i++; 00421 } 00422 node.p->drawPNG(image, level, c, depth + 1); 00423 } 00424 } 00425 ///. 00426 /// On poursuit la récursion et on détermine la couleur 00427 /// du cluster 00428 void Node_Graph::drawNodeEPS(EPSWriter& writer, int level, Color c, int depth, int height){ 00429 int i = 0; 00430 for(Node_Iterator it = getNodeIterator(); it.hasNext();){ 00431 Node node = it.next(); 00432 if(level == 0 && depth == 1){ 00433 c = node.getPixel().getColor(); 00434 } else if(level == depth){ 00435 c = Color::getColor(i); 00436 i++; 00437 } 00438 node.p->drawNodeEPS(writer, level, c, depth + 1, height); 00439 } 00440 if(level == depth){ 00441 drawEdgeEPS(writer, height); 00442 } 00443 00444 } 00445 ///. 00446 /// Production des arrêtes pour un fichier PS 00447 void Node_Graph::drawEdgeEPS(EPSWriter& writer, int height){ 00448 cout << getEdgeSize() << endl; 00449 for(Edge_Iterator it = getEdgeIterator(); it.hasNext();){ 00450 Edge edge = it.next(); 00451 Node n1 = edge.getSRC(); 00452 Node n2 = edge.getDST(); 00453 00454 int x0 = edge.getX0(); 00455 int y0 = edge.getY0(); 00456 int x1 = edge.getX1(); 00457 int y1 = edge.getY1(); 00458 00459 if((x0 == -1) || (y0 == -1) || (x1 == -1) || (y1 == -1)){ 00460 return; 00461 } 00462 y0 = height - y0; 00463 y1 = height - y1; 00464 00465 writer.drawLine(x0, y0, x1, y1); 00466 } 00467 }

Généré le Sun Jun 27 15:59:32 2004 pour segment par doxygen 1.3.7