DGtal 0.3.0

testArithDSS.cpp

Go to the documentation of this file.
00001 
00037 #include <iostream>
00038 #include <iterator>
00039 #include <cstdio>
00040 #include <cmath>
00041 #include <fstream>
00042 #include <vector>
00043 
00044 #include "DGtal/base/Common.h"
00045 #include "DGtal/base/Exceptions.h"
00046 #include "DGtal/kernel/SpaceND.h"
00047 #include "DGtal/kernel/domains/HyperRectDomain.h"
00048 #include "DGtal/geometry/2d/ArithmeticalDSS.h"
00049 #include "DGtal/io/DGtalBoard.h"
00050 
00051 #ifdef WITH_GMP
00052 #include <gmpxx.h>
00053 #endif
00054 
00055 using namespace DGtal;
00056 using namespace std;
00057 using namespace LibBoard;
00058 
00060 // Functions for testing class ArithmeticalDSS.
00062 
00066 bool testDSS4drawing()
00067 {
00068 
00069         typedef PointVector<2,int> Point;
00070         typedef std::vector<Point>::iterator Iterator;
00071         typedef ArithmeticalDSS<Iterator,int,4> DSS4;  
00072 
00073         std::vector<Point> contour;
00074         contour.push_back(Point(0,0));
00075         contour.push_back(Point(1,0));
00076         contour.push_back(Point(1,1));
00077         contour.push_back(Point(2,1));
00078         contour.push_back(Point(3,1));
00079         contour.push_back(Point(3,2));
00080         contour.push_back(Point(4,2));
00081         contour.push_back(Point(5,2));
00082         contour.push_back(Point(6,2));
00083         contour.push_back(Point(6,3));
00084         contour.push_back(Point(6,4));
00085 
00086   
00087   // Adding step
00088   trace.beginBlock("Add points while it is possible and draw the result");
00089 
00090                 DSS4 theDSS4;   
00091                 Iterator i = contour.begin();   
00092                 theDSS4.init(i);
00093                 i++;
00094                 trace.info() << theDSS4 << " " << theDSS4.isValid() << std::endl;
00095 
00096                 while ( (i!=contour.end())
00097                                         &&(theDSS4.extend(i)) ) {
00098                         i++;
00099                 }
00100           trace.info() << theDSS4 << " " << theDSS4.isValid() << std::endl;
00101 
00102                 HyperRectDomain< SpaceND<2,int> > domain( Point(0,0), Point(10,10) );
00103 
00104                 DGtalBoard board;
00105                 board.setUnit(Board::UCentimeter);
00106                 
00107         board << SetMode(domain.styleName(), "Grid")
00108                                   << domain;            
00109     board << SetMode("PointVector", "Grid");
00110 
00111 //      board << SetMode(theDSS4.styleName(), "Both") 
00112 //                                      << theDSS4;
00113 //does not draw the default style
00114 
00115         board << SetMode(theDSS4.styleName(), "Points") 
00116                                         << theDSS4;
00117         board << SetMode(theDSS4.styleName(), "BoundingBox") 
00118                                         << theDSS4;
00119                 
00120                 board.saveSVG("DSS4.svg");
00121         
00122 
00123   trace.endBlock();
00124 
00125         return true;  
00126 }
00127 
00132 bool testDSS8drawing()
00133 {
00134 
00135         typedef PointVector<2,int> Point;
00136         typedef std::vector<Point>::iterator Iterator;
00137         typedef ArithmeticalDSS<Iterator,int,8> DSS8;  
00138 
00139         std::vector<Point> boundary;
00140         boundary.push_back(Point(0,0));
00141         boundary.push_back(Point(1,1));
00142         boundary.push_back(Point(2,1));
00143         boundary.push_back(Point(3,2));
00144         boundary.push_back(Point(4,2));
00145         boundary.push_back(Point(5,2));
00146         boundary.push_back(Point(6,3));
00147         boundary.push_back(Point(6,4));
00148 
00149   // Good Initialisation
00150   trace.beginBlock("Add points while it is possible and draw the result");
00151   DSS8 theDSS8;         
00152         Iterator i = boundary.begin();
00153         theDSS8.init(i);
00154         i++;
00155   trace.info() << theDSS8 << " " << theDSS8.isValid() << std::endl;
00156 
00157         {
00158 
00159                 while ( (i!=boundary.end())
00160                                         &&(theDSS8.extend(i)) ) {
00161                         i++;
00162                 }
00163           trace.info() << theDSS8 << " " << theDSS8.isValid() << std::endl;
00164 
00165 
00166                 HyperRectDomain< SpaceND<2,int> > domain( Point(0,0), Point(10,10) );
00167 
00168                 
00169                 DGtalBoard board;
00170                 board.setUnit(Board::UCentimeter);
00171                 
00172 
00173         board << SetMode(domain.styleName(), "Paving")
00174                                   << domain;            
00175     board << SetMode("PointVector", "Both");
00176 
00177 
00178 //      board << SetMode(theDSS8.styleName(), "Both") 
00179 //                                      << theDSS8;
00180 //does not work
00181 
00182 
00183         board << SetMode(theDSS8.styleName(), "Points") 
00184                                         << theDSS8;
00185         board << SetMode(theDSS8.styleName(), "BoundingBox") 
00186                                         << theDSS8;
00187                 
00188                 
00189                 board.saveSVG("DSS8.svg");
00190 
00191         }
00192 
00193   trace.endBlock();
00194 
00195         return true;  
00196 }
00197 
00202 bool testExtendRetract()
00203 {
00204 
00205 
00206         typedef PointVector<2,int> Point;
00207         typedef std::vector<Point>::iterator Iterator;
00208         typedef ArithmeticalDSS<Iterator,int,4> DSS4;  
00209 
00210 
00211         std::vector<Point> contour;
00212         contour.push_back(Point(0,0));
00213         contour.push_back(Point(1,0));
00214         contour.push_back(Point(1,1));
00215         contour.push_back(Point(2,1));
00216         contour.push_back(Point(3,1));
00217         contour.push_back(Point(3,2));
00218         contour.push_back(Point(4,2));
00219         contour.push_back(Point(5,2));
00220         contour.push_back(Point(6,2));
00221         contour.push_back(Point(6,3));
00222         contour.push_back(Point(6,4));
00223 
00224 
00225   trace.beginBlock("Checking consistency between adding and removing");
00226 
00227                 std::deque<DSS4 > v1,v2;
00228         DSS4 newDSS4;
00229                 Iterator i = contour.begin();
00230                 newDSS4.init(i);
00231           v1.push_back(newDSS4);         
00232                 i++;
00233 
00234                 //forward scan and store each DSS4
00235                 trace.info() << "forward scan" << std::endl;
00236 
00237                 while ( (i!=contour.end())
00238                                         &&(newDSS4.extend(i)) ) {
00239                 v1.push_back(newDSS4);
00240                         i++;
00241                 }
00242 
00243                 //backward scan
00244                 trace.info() << "backward scan" << std::endl;
00245 
00246                 i--; 
00247         DSS4 reverseDSS4;
00248                 reverseDSS4.init(i);
00249 
00250                 Iterator j = i; j--;
00251                 while ( (j!=contour.begin())&&(reverseDSS4.extendOppositeEnd(j)) ) {
00252                         j--;
00253                 }
00254                 reverseDSS4.extendOppositeEnd(j);
00255 
00256                 trace.info() << "removing" << std::endl;
00257 
00258                 //removing step, store each DSS4 for comparison
00259           v2.push_front(reverseDSS4);
00260                 while (reverseDSS4.retractOppositeEnd()) {
00261                 v2.push_front(reverseDSS4);
00262                 }               
00263                 
00264 
00265                 //comparison
00266                 trace.info() << "comparison" << std::endl;
00267                 trace.info() << v1.size() << " == " << v2.size() << std::endl;
00268                 ASSERT(v1.size() == v2.size());
00269 
00270                 bool isOk = true;
00271                 for (unsigned int k = 0; k < v1.size(); k++) {
00272                         if (v1.at(k) != v2.at(k)) isOk = false;
00273                         trace.info() << "DSS4 :" << k << std::endl;
00274 
00275                         trace.info() << v1.at(k) << v2.at(k) << std::endl;
00276                 }
00277 
00278 
00279                 if (isOk) trace.info() << "ok for the " << v1.size() << " DSS4" << std::endl;
00280                 else trace.info() << "failure" << std::endl;
00281 
00282   trace.endBlock();
00283 
00284         return isOk;
00285 }
00286 
00287 
00288 #ifdef WITH_GMP
00289 
00293 bool testGMP()
00294 {
00295         bool flag = false;
00296 
00297 
00298         typedef mpz_class Coordinate;
00299         typedef PointVector<2,Coordinate> Point;
00300         typedef std::vector<Point>::iterator Iterator;
00301         typedef ArithmeticalDSS<Iterator,Coordinate,4> DSS4;  
00302 
00303 
00304 
00305   trace.beginBlock("Add some points of big coordinates");
00306 
00307                 std::vector<Point> contour;
00308                 contour.push_back(Point(1000000000,1000000000));        
00309                 contour.push_back(Point(1000000001,1000000000));
00310                 contour.push_back(Point(1000000002,1000000000));
00311                 contour.push_back(Point(1000000003,1000000000));
00312                 contour.push_back(Point(1000000003,1000000001));
00313                 contour.push_back(Point(1000000004,1000000001));
00314                 contour.push_back(Point(1000000005,1000000001));
00315                 contour.push_back(Point(1000000005,1000000002));
00316 
00317                 DSS4 theDSS4;
00318                 Iterator i = contour.begin();
00319                 theDSS4.init(i);
00320                 i++;
00321                 while (i != contour.end()) {
00322                         theDSS4.extend(i);
00323                         i++;
00324                 }
00325           trace.info() << theDSS4 << " " << theDSS4.isValid() << std::endl;
00326 
00327                 Coordinate mu;
00328                 mu = "-3000000000";
00329                 if( (theDSS4.getA() == 2)
00330                         &&(theDSS4.getB() == 5)
00331                         &&(theDSS4.getMu() == mu)
00332                         &&(theDSS4.getOmega() == 7) ) {
00333                         flag = true;
00334                 } else {
00335                         flag = false;
00336                 }
00337 
00338   trace.endBlock();
00339 
00340         return flag;
00341 }
00342 
00343 #endif
00344 
00350 bool testCorner()
00351 {
00352 
00353         typedef PointVector<2,int> Point;
00354         typedef std::vector<Point>::iterator Iterator;
00355         typedef ArithmeticalDSS<Iterator,int,8> DSS8;  
00356 
00357         std::vector<Point> boundary;
00358         boundary.push_back(Point(10,10));
00359         boundary.push_back(Point(10,11));
00360         boundary.push_back(Point(11,11));
00361 
00362 
00363         DSS8 theDSS8;
00364         Iterator i = boundary.begin();
00365         theDSS8.init(i);
00366         i++;
00367         theDSS8.extend(i);
00368         i++;
00369         return ( !theDSS8.extend(i) );
00370 
00371 }
00372 
00373 int main(int argc, char **argv)
00374 {
00375 
00376   trace.beginBlock ( "Testing class ArithmeticalDSS" );
00377   trace.info() << "Args:";
00378   for ( int i = 0; i < argc; ++i )
00379     trace.info() << " " << argv[ i ];
00380   trace.info() << endl;
00381 
00382   bool res = testDSS4drawing() 
00383                                         && testDSS8drawing()
00384                                         && testExtendRetract()
00385                                         && testCorner()
00386 #ifdef WITH_GMP
00387                                         && testGMP()
00388 #endif
00389     ;
00390   trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
00391   trace.endBlock();
00392 
00393   return res ? 0 : 1;
00394 
00395 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines