DGtal  0.9.3
testPointVector.cpp
Go to the documentation of this file.
1 
33 #include <cstdio>
34 #include <cmath>
35 #include <iostream>
36 #include <fstream>
37 #include <vector>
38 #include "DGtal/base/Common.h"
39 #include "DGtal/kernel/PointVector.h"
40 
41 using namespace DGtal;
42 using namespace std;
43 
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 }
104 
105 
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 }
121 
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 }
155 
156 bool testNorms()
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 }
179 
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 }
202 
203 
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 }
225 
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 }
260 
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 }
287 
288 int main()
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 }
304 
void beginBlock(const std::string &keyword="")
const Point aPoint(3, 4)
UnsignedComponent normInfinity() const
Self sup(const Self &apoint) const
static const Dimension dimension
Copy of the static dimension of the Point/Vector.
Definition: PointVector.h:167
Trace trace
Definition: Common.h:137
Component max() const
Component dot(const Self &v) const
bool testSimpleVector()
Component min() const
bool testMaxMin()
STL namespace.
double endBlock()
bool testSimplePoint()
UnsignedComponent norm1() const
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
Self inf(const Self &apoint) const
bool testIterator()
bool testIntegerNorms()
Container::const_iterator ConstIterator
Definition: PointVector.h:182
DGtal is the top-level namespace which contains all DGtal functions and types.
Iterator maxElement()
bool testOperators()
int main()
std::ostream & info()
Iterator minElement()
bool testComparison()
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
bool testNorms()