DGtal  0.9.3
Functions
testPointVector.cpp File Reference
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
#include <vector>
#include "DGtal/base/Common.h"
#include "DGtal/kernel/PointVector.h"
Include dependency graph for testPointVector.cpp:

Go to the source code of this file.

Functions

bool testComparison ()
 
bool testMaxMin ()
 
bool testSimplePoint ()
 
bool testNorms ()
 
bool testSimpleVector ()
 
bool testIterator ()
 
bool testOperators ()
 
bool testIntegerNorms ()
 
int main ()
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr )
Date
2010/03/03

This file is part of the DGtal library

Definition in file testPointVector.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 288 of file testPointVector.cpp.

References testComparison(), testIntegerNorms(), testIterator(), testMaxMin(), testNorms(), testOperators(), testSimplePoint(), and testSimpleVector().

289 {
290  bool res;
291  res = testSimplePoint()
292  && testSimpleVector()
293  && testNorms()
294  && testIterator()
295  && testComparison()
296  && testOperators()
297  && testIntegerNorms()
298  && testMaxMin();
299  if (res)
300  return 0;
301  else
302  return 1;
303 }
bool testSimpleVector()
bool testMaxMin()
bool testSimplePoint()
bool testIterator()
bool testIntegerNorms()
bool testOperators()
bool testComparison()
bool testNorms()

◆ testComparison()

bool testComparison ( )

A PointVector may represent either a point or a vector depending on the context. For performance reasons, these two types are just aliases. The user should take care how to use it depending on the context. For instance, adding two points has no meaning, but will be authorized by the compiler.

The default less than operator is the one of the lexicographic ordering, starting from dimension 0 to N-1.

PointVector also realizes the concept CLattice with an infimum (meet, greatest lower bound) and a supremum (join, least upper bound) operation.

Usage example:

...
typedef PointVector<5,double> VectorD5;
VectorD5 p, q, r;
p.at(1) = 2.0; // p = {0.0, 2.0, 0.0, 0.0, 0.0}
q.at(3) = -5.5 // q = {0.0, 0.0, 0.0, -5.5, 0.0}
r = p + q ; // r = {0.0, 2.0, 0.0, -5.5, 0.0}
d = r.norm( DGtal::PointVector::L_infty ); // d = 5.5
...

Definition at line 76 of file testPointVector.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

77 {
78  const double t[ ] = { 3.5, 4.1, 2.2, 3.2 };
79  PointVector<4,double> v ( t );
80  PointVector<4,double> v2 ( t );
81  PointVector<4,double> v3 ( { 3.5, 4.2, 2.2, 3.2 } );
82 
83  trace.beginBlock("Comparison of Points");
84  if (v == v2)
85  trace.info()<< "v == v2 (true)"<<std::endl;
86  else
87  trace.info()<< "v == v2 (false)"<<std::endl;
88 
89  if (v == v3)
90  trace.info()<< "v == v3 (true)"<<std::endl;
91  else
92  trace.info()<< "v == v3 (false)"<<std::endl;
93 
94  if (v < v2)
95  trace.info()<< "v < v2 (true)"<<std::endl;
96  else
97  trace.info()<< "v < v2 (false)"<<std::endl;
98 
99 
100  trace.endBlock();
101 
102  return ((v == v2) && !(v != v2));
103 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
std::ostream & info()

◆ testIntegerNorms()

bool testIntegerNorms ( )

Definition at line 261 of file testPointVector.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::norm1(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::normInfinity(), and DGtal::trace.

Referenced by main().

262 {
263  unsigned int nbok = 0;
264  unsigned int nb = 0;
265 
266  DGtal::int32_t t[]= {2,1,3,4};
268  DGtal::int32_t t2[]= {4,5,3,2};
270  PointVector<4,DGtal::int32_t> p = p2 - p1;
271 
272  trace.beginBlock ( "Checking Integer norm1" );
273  trace.info() << "p1: "<<p1 <<", "<<"p2: "<<p2 <<std::endl;
274  nbok += p.norm1() == 8 ? 1 : 0;
275  nb++;
276  trace.info() << "(" << nbok << "/" << nb << ") "
277  << "L1(p2-p1): "<< p.norm1() << "( == 8 ?)" << std::endl;
278  nbok += p.normInfinity() == 4 ? 1 : 0;
279  nb++;
280  trace.info() << "(" << nbok << "/" << nb << ") "
281  << "Linfty(p2-p1): "<< p.normInfinity() << "( == 4 ?)"
282  << std::endl;
283  trace.endBlock();
284 
285  return nbok == nb;
286 }
void beginBlock(const std::string &keyword="")
UnsignedComponent normInfinity() const
Trace trace
Definition: Common.h:137
double endBlock()
UnsignedComponent norm1() const
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
std::ostream & info()
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72

◆ testIterator()

bool testIterator ( )

Definition at line 204 of file testPointVector.cpp.

References aPoint(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::begin(), DGtal::Trace::beginBlock(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::end(), DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

205 {
207  PointVector<4, int> avector;
208 
209  trace.beginBlock("Point Iterator Test");
210 
211  for (unsigned int i=0;i<25;++i)
212  aPoint[i] = i;
213  trace.info() << "aPoint="<<aPoint<< std::endl;
214 
215  trace.info() << "With iterator: ";
216  for (PointVector<25,int>::ConstIterator it = aPoint.begin() ; it != aPoint.end(); ++it)
217  trace.info() << (*it) <<" " ;
218 
219  trace.info() << std::endl;
220 
221  trace.endBlock();
222 
223  return true;
224 }
void beginBlock(const std::string &keyword="")
const Point aPoint(3, 4)
Trace trace
Definition: Common.h:137
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
Container::const_iterator ConstIterator
Definition: PointVector.h:182
std::ostream & info()

◆ testMaxMin()

bool testMaxMin ( )

Definition at line 106 of file testPointVector.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::max(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::maxElement(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::min(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::minElement(), and DGtal::trace.

Referenced by main().

107 {
108 
109  const double t[ ] = { 3.5, 4.1, 2.2, 3.2 };
110  PointVector<4,double> v ( t );
111 
112  trace.beginBlock("Testing max/min of a vector");
113  trace.info() << " Vector: "<< v<<std::endl;
114  trace.info() << "max val = "<< v.max() <<std::endl;
115  trace.info()<< "min val = "<<v.min() << std::endl;
116  trace.info() << "maxElement val = "<< *v.maxElement() <<std::endl;
117  trace.info()<< "minElement val = "<<*v.minElement() << std::endl;
118  trace.endBlock();
119  return ((v.max() == 4.1) && (v.min()==2.2));
120 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
std::ostream & info()

◆ testNorms()

bool testNorms ( )

Definition at line 156 of file testPointVector.cpp.

References aPoint(), DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

157 {
158  typedef PointVector<3, int> PointType;
159  PointType aPoint;
160 
161  aPoint[ 2 ] = 2;
162  aPoint[ 1 ] = -1;
163  aPoint[ 0 ] = 3;
164 
165  trace.beginBlock ( "Test of Norms" );
166  trace.info() << "aPoint l_2 norm="<<aPoint.norm() <<endl;
167  trace.info() << "aPoint l_1 norm="<<aPoint.norm ( PointType::L_1 ) <<endl;
168  trace.info() << "aPoint l_infty norm="<<aPoint.norm ( PointType::L_infty ) <<endl;
169 
170  trace.info() << "Normalization="<<aPoint.getNormalized () <<endl;
171 
172  trace.endBlock();
173 
174 
175  return ( ( aPoint.norm ( PointType::L_1 ) == 6 ) &&
176  ( aPoint.norm ( PointType::L_infty ) == 3 ) );
177 
178 }
void beginBlock(const std::string &keyword="")
const Point aPoint(3, 4)
Trace trace
Definition: Common.h:137
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
std::ostream & info()

◆ testOperators()

bool testOperators ( )

Definition at line 226 of file testPointVector.cpp.

References DGtal::Trace::beginBlock(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::dot(), DGtal::Trace::endBlock(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::inf(), DGtal::Trace::info(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::sup(), and DGtal::trace.

Referenced by main().

227 {
228  unsigned int nb = 0;
229  unsigned int nbok = 0;
230  trace.beginBlock("Point Operators Test");
231 
232  DGtal::int32_t t1[] = {1,2,3,4};
234  DGtal::int32_t t2[]= {5,4,3,2};
236 
237  trace.info() << "p1: "<<p1 <<", "<<"p2: "<<p2 <<std::endl;
238  trace.info() << "p1+p2: "<<p1+p2 <<std::endl;
239  trace.info() << "p1*2+p2: "<<p1*2+p2 <<std::endl;
240  trace.info() << "p1-p2: "<<p1-p2 <<std::endl;
241  trace.info() << "-p2: "<< -p2 <<std::endl;
242  trace.info() << "inf(p1,p2): "<<p1.inf(p2) <<std::endl;
243  trace.info() << "sup(p1,p2): "<<p1.sup(p2) <<std::endl;
244  trace.info() << "p1 dot p2: "<<p1.dot(p2) <<std::endl;
245 
246  trace.endBlock();
247 
248  trace.beginBlock("Vector Operators Test");
251  ++nb, nbok += ( p4 == p1 ) ? 1 : 0;
252  p4 = 2*p1 + p3;
253  trace.info() << "2*p1+p3: "<< p4 << " (==p1)" << std::endl;
254  ++nb, nbok += ( p4 == p1 ) ? 1 : 0;
255  trace.info() << "2*p1+3*p2: "<< 2*p1+3*p2 << std::endl;
256  trace.endBlock();
257 
258  return nb == nbok;
259 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
std::ostream & info()
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72

◆ testSimplePoint()

bool testSimplePoint ( )

Test instanciation of Points

Definition at line 126 of file testPointVector.cpp.

References aPoint(), DGtal::Trace::beginBlock(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::dimension, DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

127 {
128  PointVector<3, int> aPVInt3;
129 
130  int t[]={-3 ,4 ,4 ,0};
132  PointVector<4,int> aFPoint;
133 
134  aPoint *= 5;
135 
136  cout << "aPoint=" << aPoint << endl;
137 
138  trace.beginBlock ( "Test point dimension" );
139  trace.info() << "aPoint dimension="<<aPoint.dimension <<endl;
140  trace.endBlock();
141 
142  if ( aPoint.dimension != 4 )
143  return false;
144 
145  int tt[] = { 3, 4, 2, 2 };
146  PointVector<4,int> v (tt);
147  aPoint = aFPoint + v;
148  trace.beginBlock ( "Test point addition with vector" );
149  trace.info() << "aPoint = "<< aFPoint << " + " << v << endl;
150  trace.info() << "aPoint = "<< aPoint << endl;
151  trace.endBlock();
152 
153  return true;
154 }
void beginBlock(const std::string &keyword="")
const Point aPoint(3, 4)
Trace trace
Definition: Common.h:137
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
std::ostream & info()

◆ testSimpleVector()

bool testSimpleVector ( )

Test instancition of Vectors

Definition at line 184 of file testPointVector.cpp.

References DGtal::Trace::beginBlock(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::dimension, DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

185 {
186  PointVector<3, int> aPVInt3;
187  PointVector<4, int> aVector;
188  PointVector<4, int> aFVector;
189 
190  trace.beginBlock ( "Test of Vector Dimension" );
191  trace.info() << "aVector dimension="<< aVector.dimension <<endl;
192  trace.info() << "aVector = "<< aVector <<endl;
193  trace.endBlock();
194 
195  if ( aVector.dimension != 4 )
196  return false;
197 
198  aVector += aFVector;
199 
200  return true;
201 }
void beginBlock(const std::string &keyword="")
static const Dimension dimension
Copy of the static dimension of the Point/Vector.
Definition: PointVector.h:167
Trace trace
Definition: Common.h:137
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
std::ostream & info()