DGtal 1.3.0
Loading...
Searching...
No Matches
testGridCurve.cpp
1
33#include <iostream>
34#include <fstream>
35#include <sstream>
36#include <exception>
37
38#include "DGtal/base/Common.h"
39
40#include "DGtal/kernel/SpaceND.h"
41#include "DGtal/kernel/domains/HyperRectDomain.h"
42#include "DGtal/topology/KhalimskySpaceND.h"
43#include "DGtal/geometry/curves/GridCurve.h"
44
45#include "DGtal/io/boards/Board2D.h"
46
47#include "DGtal/io/boards/CDrawableWithBoard2D.h"
48
49#include "ConfigTest.h"
50
52
53using namespace std;
54using namespace DGtal;
55
57// Functions for testing class FreemanChain.
59
64template <typename KSpace>
65bool testIOGridCurve(const string& filename)
66{
67
68 unsigned int d = KSpace::Point::dimension;
69 GridCurve<KSpace> c; //grid curve
70
72 trace.info() << endl;
73 trace.info() << "Reading GridCurve d=" << d << " ";
74
75 ifstream instream; // input stream
76 instream.open (filename.c_str(), ifstream::in);
77
78 c.initFromVectorStream(instream);
79
80 trace.info() << "(" << c.size() << ") elts" << std::endl;
81 trace.info() << c << endl;
82
84 std::stringstream s;
85 s << "gridcurve" << d << ".dat";
86
87 trace.info() << "Writing GridCurve d=" << d << " in " << s.str() << endl;
88
89 ofstream outstream(s.str().c_str()); //output stream
90 if (!outstream.is_open()) return false;
91 else {
92 c.writeVectorToStream(outstream);
93 }
94 outstream.close();
95
96 return true;
97}
98
99
104bool testIsOpen(const string &filename, const bool& aFlag)
105{
106
107 trace.info() << endl;
108 trace.info() << "Open/Closed test" << endl;
109
110 GridCurve<KhalimskySpaceND<2> > c; //grid curve
111
112 ifstream instream; // input stream
113 instream.open (filename.c_str(), ifstream::in);
114 c.initFromVectorStream(instream);
115
116 trace.info() << c.isOpen() << " == " << aFlag << endl;
117
118 return (c.isOpen() == aFlag);
119}
120
125bool testExceptions(const string &filename)
126{
127
128 GridCurve<KhalimskySpaceND<2> > c; //grid curve
129
130 trace.info() << endl;
131 trace.info() << "Trying to read bad file: " << filename << endl;
132
133 ifstream instream; // input stream
134 instream.open (filename.c_str(), ifstream::in);
135
136 try {
137 c.initFromVectorStream(instream);
138 trace.info() << "no exception catched!?" << endl;
139 return false;
140 } catch (DGtal::ConnectivityException& e) {
141 trace.info() << e.what() << endl;
142 return true;
143 } catch (DGtal::InputException& e) {
144 trace.info() << e.what() << endl;
145 return true;
146 } catch (exception& e) {
147 trace.info() << e.what() << endl;
148 return true;
149 }
150}
151
156bool testDrawGridCurve(const string &filename)
157{
158
159 GridCurve<KhalimskySpaceND<2> > c; //grid curve
160
161 trace.info() << endl;
162 trace.info() << "Displaying GridCurve " << endl;
163
164 //reading grid curve
165 fstream inputStream;
166 inputStream.open (filename.c_str(), ios::in);
167 c.initFromVectorStream(inputStream);
168 inputStream.close();
169
170 //displaying it
171 Board2D aBoard;
172 aBoard.setUnit(Board2D::UCentimeter);
173 aBoard << c;
174 aBoard.saveEPS( "GridCurve.eps", Board2D::BoundingBox, 5000 );
175#ifdef WITH_CAIRO
176 aBoard.saveCairo("GridCurve-cairo.pdf", Board2D::CairoPDF, Board2D::BoundingBox, 5000);
177#endif
178
179 return true;
180}
181
182
187template <typename Range>
188bool testRange(const Range &aRange)
189{
190
191 trace.info() << endl;
192 trace.info() << "Testing Range" << endl;
193
195 std::vector<Value> v1,v2,v3,v4;
196
197{
198 trace.info() << "Forward" << endl;
199 typename Range::ConstIterator i = aRange.begin();
200 typename Range::ConstIterator end = aRange.end();
201 for ( ; i != end; ++i) {
202 //cout << *i << endl;
203 v1.push_back(*i);
204 }
205}
206{
207 trace.info() << "Backward" << endl;
208 typename Range::ConstReverseIterator i = aRange.rbegin();
209 typename Range::ConstReverseIterator end = aRange.rend();
210 for ( ; i != end; ++i) {
211 //cout << *i << endl;
212 v2.push_back(*i);
213 }
214}
215{
216 trace.info() << "Circulator" << endl;
217 typename Range::ConstCirculator c = aRange.c();
218 typename Range::ConstCirculator cend = aRange.c();
219 if (isNotEmpty(c,cend))
220 {
221 do
222 {
223 //cout << *c << endl;
224 v3.push_back(*c);
225 c++;
226 } while (c!=cend);
227 }
228}
229
230{
231 trace.info() << "Reverse Circulator" << endl;
232 typename Range::ConstReverseCirculator c = aRange.rc();
233 ++c;
234 typename Range::ConstReverseCirculator cend = c;
235 if (isNotEmpty(c,cend))
236 {
237 do
238 {
239 //cout << *c << endl;
240 v4.push_back(*c);
241 c++;
242 } while (c!=cend);
243 }
244}
245
246 return ( std::equal(v1.begin(),v1.end(),v3.begin())
247 && std::equal(v2.begin(),v2.end(),v4.begin())
248 && std::equal(v1.begin(),v1.end(),v2.rbegin())
249 && std::equal(v3.begin(),v3.end(),v4.rbegin()) );
250}
251
252template <typename Range>
253bool testPairsRange(const Range &aRange)
254{
255
256 trace.info() << endl;
257 trace.info() << "Testing Range" << endl;
258
259{
260 trace.info() << "Forward" << endl;
261 typename Range::ConstIterator i = aRange.begin();
262 typename Range::ConstIterator end = aRange.end();
263 for ( ; i != end; ++i) {
264 cout << (*i).first << " " << (*i).second << endl;
265 }
266}
267{
268 trace.info() << "Backward" << endl;
269 typename Range::ConstReverseIterator i = aRange.rbegin();
270 typename Range::ConstReverseIterator end = aRange.rend();
271 for ( ; i != end; ++i) {
272 cout << i->first << " " << i->second << endl;
273 }
274}
275
276 return true;
277}
278
279template <typename Range>
280bool testDisplayRange(const Range &aRange)
281{
282
283 trace.info() << endl;
284 trace.info() << "Displaying Range" << endl;
285 trace.info() << aRange << endl;
286
287 return true;
288}
289
290template <typename Range>
291bool testDrawRange(const Range &aRange, const string &aName, const string& aDomainMode)
292{
293
294 std::stringstream s;
295 s << aName << "Range.eps";
296
297 trace.info() << endl;
298 trace.info() << "Drawing " << s.str() << " " << endl;
299
300 //board
301 Board2D aBoard;
302 aBoard.setUnit(Board2D::UCentimeter);
303 //displaying domain
304 PointVector<2,int> low(-1,-1);
305 PointVector<2,int> up(3,3);
306 if (aDomainMode == "Paving") up = PointVector<2,int>(4,4);
307 HyperRectDomain< SpaceND<2,int> > aDomain( low,up );
308 aBoard << SetMode(aDomain.className(), aDomainMode) << aDomain;
309 //displaying range
310 aBoard << aRange;
311 //save
312 aBoard.saveEPS( s.str().c_str(), Board2D::BoundingBox, 5000 );
313
314 return true;
315}
316
317template <typename Range>
318void testRangeConceptChecking()
319{
320 BOOST_CONCEPT_ASSERT(( concepts::CDrawableWithBoard2D<Range> ));
321 BOOST_CONCEPT_ASSERT(( concepts::CConstBidirectionalRange<Range> ));
322}
323
325// Standard services - public :
326
327int main( int argc, char** argv )
328{
329 trace.beginBlock ( "Testing class GridCurve" );
330 trace.info() << "Args:";
331 for ( int i = 0; i < argc; ++i )
332 trace.info() << " " << argv[ i ];
333 trace.info() << endl;
334
335
336 std::string sinus2D4 = testPath + "samples/sinus2D4.dat";
337 std::string polyg2D = testPath + "samples/polyg2D.dat";
338 std::string sinus3D = testPath + "samples/sinus3D.dat";
339 std::string emptyFile = testPath + "samples/emptyFile.dat";
340 std::string square = testPath + "samples/smallSquare.dat";
341
342 typedef KhalimskySpaceND<2> K2;
343 typedef KhalimskySpaceND<3> K3;
344
346 typedef GridCurve<K2> GridCurve;
347
348testRangeConceptChecking<GridCurve::SCellsRange>();
349testRangeConceptChecking<GridCurve::PointsRange>();
350testRangeConceptChecking<GridCurve::MidPointsRange>();
351testRangeConceptChecking<GridCurve::ArrowsRange>();
352testRangeConceptChecking<GridCurve::InnerPointsRange>();
353testRangeConceptChecking<GridCurve::OuterPointsRange>();
354testRangeConceptChecking<GridCurve::IncidentPointsRange>();
355
357 bool res = testIOGridCurve<K2>(sinus2D4)
358 && testIOGridCurve<K3>(sinus3D)
359 && testExceptions(sinus3D)
360 && testExceptions(polyg2D)
361 && testExceptions(emptyFile)
362 && testDrawGridCurve(sinus2D4)
363 && testIsOpen(sinus2D4,true)
364 && testIsOpen(square,false);
365
366
367 //reading grid curve
368 GridCurve c;
369 fstream inputStream;
370 inputStream.open (square.c_str(), ios::in);
371 c.initFromVectorStream(inputStream);
372 inputStream.close();
373
374 res = res
375 && testRange<GridCurve::SCellsRange>(c.getSCellsRange())
376 && testRange<GridCurve::PointsRange>(c.getPointsRange())
377 && testRange<GridCurve::MidPointsRange>(c.getMidPointsRange())
378 && testPairsRange<GridCurve::ArrowsRange>(c.getArrowsRange())
379 && testRange<GridCurve::InnerPointsRange>(c.getInnerPointsRange())
380 && testRange<GridCurve::OuterPointsRange>(c.getOuterPointsRange())
381 && testPairsRange<GridCurve::IncidentPointsRange>(c.getIncidentPointsRange())
382 && testRange<GridCurve::CodesRange>(c.getCodesRange())
383;
384
385 res = res
386 && testDisplayRange<GridCurve::SCellsRange>(c.getSCellsRange())
387 && testDisplayRange<GridCurve::PointsRange>(c.getPointsRange())
388 && testDisplayRange<GridCurve::MidPointsRange>(c.getMidPointsRange())
389 && testDisplayRange<GridCurve::ArrowsRange>(c.getArrowsRange())
390 && testDisplayRange<GridCurve::InnerPointsRange>(c.getInnerPointsRange())
391 && testDisplayRange<GridCurve::OuterPointsRange>(c.getOuterPointsRange())
392 && testDisplayRange<GridCurve::IncidentPointsRange>(c.getIncidentPointsRange())
393 && testDisplayRange<GridCurve::CodesRange>(c.getCodesRange())
394;
395
396 res = res
397 && testDrawRange<GridCurve::SCellsRange>(c.getSCellsRange(),"1cells","Grid")
398 && testDrawRange<GridCurve::PointsRange>(c.getPointsRange(),"Points","Paving")
399 && testDrawRange<GridCurve::MidPointsRange>(c.getMidPointsRange(),"MidPoints","Paving")
400 && testDrawRange<GridCurve::ArrowsRange>(c.getArrowsRange(),"Arrows","Paving")
401 && testDrawRange<GridCurve::InnerPointsRange>(c.getInnerPointsRange(),"InnerPoints","Grid")
402 && testDrawRange<GridCurve::OuterPointsRange>(c.getOuterPointsRange(),"OuterPoints","Grid")
403 && testDrawRange<GridCurve::IncidentPointsRange>(c.getIncidentPointsRange(),"IncidentPoints","Grid")
404;
405
407
408 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
409 trace.endBlock();
410
411 return res ? 0 : 1;
412}
413// //
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:86
virtual const char * what() const noexcept
Definition: Exceptions.h:78
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
Definition: GridCurve.h:173
bool initFromVectorStream(std::istream &in)
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
virtual const char * what() const noexcept
Definition: Exceptions.h:66
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
static const constexpr Dimension dimension
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
std::reverse_iterator< ConstCirculator > ConstReverseCirculator
std::reverse_iterator< ConstIterator > ConstReverseIterator
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
void setUnit(Unit unit)
Definition: Board.cpp:240
void saveCairo(const char *filename, CairoType type=CairoPNG, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1139
KSpace K2
Definition: StdDefs.h:78
KSpace K3
Definition: StdDefs.h:147
DGtal is the top-level namespace which contains all DGtal functions and types.
bool isNotEmpty(const IC &itb, const IC &ite)
Trace trace
Definition: Common.h:154
STL namespace.
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
Aim: Defines the concept describing a bidirectional const range.
Aim: The concept CDrawableWithBoard2D specifies what are the classes that admit an export with Board2...
int main()
Definition: testBits.cpp:56