DGtal  1.5.beta
testPointListReader.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/io/readers/PointListReader.h"
34 #include "DGtal/helpers/StdDefs.h"
35 #include "DGtal/geometry/curves/FreemanChain.h"
36 
37 #include "ConfigTest.h"
38 
40 
41 using namespace std;
42 using namespace DGtal;
43 
44 
46 // Functions for testing class PointListReader.
48 
53 {
54  unsigned int nbok = 0;
55  unsigned int nb = 0;
56 
57  trace.beginBlock ( "Testing reading point list ..." );
58  std::string filename = testPath + "samples/pointList1.pl";
59  std::vector<unsigned int> vectPos;
60  vectPos.push_back(1);
61  vectPos.push_back(2);
62  vector<Z2i::Point> vectPoints = PointListReader<Z2i::Point>::getPointsFromFile(filename,
63  vectPos);
64  for(unsigned int k=0;k < vectPoints.size(); k++){
65  trace.info() << " pt: "<< vectPoints.at(k)<< endl;
66  }
67  nbok += (vectPoints.size()==4) ? 1 : 0;
68  nb++;
69  trace.info() << "(" << nbok << "/" << nb << ") "<< std::endl;
70  trace.endBlock();
71 
72  trace.beginBlock ( "Testing reading freemanchain ..." );
73  std::string filenameFC = testPath + "samples/freemanChainSample.fc";
74  std::vector< FreemanChain< int > > vectFC = PointListReader< Z2i::Point>:: getFreemanChainsFromFile<int> (filenameFC);
75  for(unsigned int i=0; i< vectFC.size(); i++){
76  FreemanChain<int> fc = vectFC.at(i);
77  trace.info() << "Freeman chain " << i << ": " << fc.x0 << " " << fc.y0 << " " << fc.chain << endl;
78  }
79  nbok += (vectFC.size()==5) ? 1 : 0;
80  nb++;
81  trace.endBlock();
82 
83  trace.beginBlock ( "Testing reading 3D polygons (one each line)..." );
84  std::string filenamePoly = testPath + "samples/polygons.dat";
85  std::vector< std::vector< Z3i::Point > > vectPolygons = PointListReader< Z3i::Point >::getPolygonsFromFile(filenamePoly);
86  for(unsigned int i=0; i< vectPolygons.size(); i++){
87  std::vector< Z3i::Point > aPolygon = vectPolygons.at(i);
88  trace.info() << "Polygon " << i << ": " ;
89  for(unsigned int j =0; j <aPolygon.size(); j++){
90  trace.info()<< "Point :" << aPolygon.at(j);
91  }
92  trace.info()<< std::endl;
93  }
94  trace.beginBlock ( "Testing reading special 1 dimension elements (polygon of 1 dimension point)..." );
95 
96  typedef PointVector<1, int> Point1D;
97  std::vector< std::vector< Point1D > > vectPt1D = PointListReader< Point1D >::getPolygonsFromFile(filenamePoly);
98  for(unsigned int i=0; i< vectPt1D.size(); i++){
99  std::vector< Point1D > aPolygon = vectPt1D.at(i);
100  trace.info() << "Polygon " << i << ": " ;
101  for(unsigned int j =0; j <aPolygon.size(); j++){
102  trace.info()<< "Point :" << aPolygon.at(j);
103  }
104  trace.info()<< std::endl;
105  }
106  Point1D p1, p2, p3, p4, p5, p6; p1[0]=1; p2[0]=2; p3[0]=3; p4[0]=4; p5[0]=5; p6[0]=6;
107  Point1D p7, p8, p9, p10, p11, p12; p7[0]=0; p8[0]=0; p9[0]=10; p10[0]=0; p11[0]=5; p12[0]=5;
108 
109  nbok += (vectPt1D.at(0).size()==6 && vectPt1D.at(1).size()==6 && vectPt1D.at(0).at(0) == p1 &&
110  vectPt1D.at(0).at(1)==p2 && vectPt1D.at(0).at(2) == p3 && vectPt1D.at(0).at(3) == p4
111  && vectPt1D.at(0).at(4)==p5 && vectPt1D.at(0).at(5)==p6
112  && vectPt1D.at(1).at(0)==p7
113  && vectPt1D.at(1).at(1)==p8 && vectPt1D.at(1).at(2)==p9 && vectPt1D.at(1).at(3)==p10
114  && vectPt1D.at(1).at(4)==p11 && vectPt1D.at(1).at(5)==p12);
115 
116 
117  nb++;
118  trace.endBlock();
119 
120  trace.beginBlock ( "Testing reading 2D polygons (one each line)..." );
121 
122  std::vector< std::vector< Z2i::Point > > vectPolygons2D = PointListReader< Z2i::Point >::getPolygonsFromFile(filenamePoly);
123  for(unsigned int i=0; i< vectPolygons2D.size(); i++){
124  std::vector< Z2i::Point > aPolygon2D = vectPolygons2D.at(i);
125  trace.info() << "Polygon " << i << ": " ;
126  for(unsigned int j =0; j <aPolygon2D.size(); j++){
127  trace.info()<< "Point :" << aPolygon2D.at(j);
128  }
129  trace.info()<< std::endl;
130  }
131  nbok += (vectPolygons2D.at(0).size()==3 && vectPolygons2D.at(1).size()==3 && vectPolygons2D.at(0).at(0)==Z2i::Point(1,2)
132  && vectPolygons2D.at(0).at(1)==Z2i::Point(3,4) && vectPolygons2D.at(0).at(2)==Z2i::Point(5,6)
133  && vectPolygons2D.at(1).at(0)==Z2i::Point(0,0) && vectPolygons2D.at(1).at(1)==Z2i::Point(10, 0)
134  && vectPolygons2D.at(1).at(2)==Z2i::Point(5,5)) ? 1 : 0;
135 
136  nb++;
137  trace.endBlock();
138 
139  return nbok == nb;
140 }
141 
143 // Standard services - public :
144 
145 int main( int argc, char** argv )
146 {
147  trace.beginBlock ( "Testing class PointListReader" );
148  trace.info() << "Args:";
149  for ( int i = 0; i < argc; ++i )
150  trace.info() << " " << argv[ i ];
151  trace.info() << endl;
152 
153 
154  bool res = testPointListReader(); // && ... other tests
155  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
156  trace.endBlock();
157  return res ? 0 : 1;
158 }
159 // //
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Aim: Implements method to read a set of points represented in each line of a file.
MyPointD Point
Definition: testClone2.cpp:383
int main(int argc, char **argv)
bool testPointListReader()