DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testPointVector.cpp
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 
76 bool testComparison()
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 #ifdef CPP11_INITIALIZER_LIST
82  PointVector<4,double> v3 ( { 3.5, 4.2, 2.2, 3.2 } );
83 #endif
84 
85  trace.beginBlock("Comparison of Points");
86  if (v == v2)
87  trace.info()<< "v == v2 (true)"<<std::endl;
88  else
89  trace.info()<< "v == v2 (false)"<<std::endl;
90 
91 #ifdef CPP11_INITIALIZER_LIST
92  if (v == v3)
93  trace.info()<< "v == v3 (true)"<<std::endl;
94  else
95  trace.info()<< "v == v3 (false)"<<std::endl;
96 #endif
97 
98  if (v < v2)
99  trace.info()<< "v < v2 (true)"<<std::endl;
100  else
101  trace.info()<< "v < v2 (false)"<<std::endl;
102 
103 
104  trace.endBlock();
105 
106  return ((v == v2) && !(v != v2));
107 }
108 
109 
110 bool testMaxMin()
111 {
112 
113  const double t[ ] = { 3.5, 4.1, 2.2, 3.2 };
114  PointVector<4,double> v ( t );
115 
116  trace.beginBlock("Testing max/min of a vector");
117  trace.info() << " Vector: "<< v<<std::endl;
118  trace.info() << "max val = "<< v.max() <<std::endl;
119  trace.info()<< "min val = "<<v.min() << std::endl;
120  trace.info() << "maxElement val = "<< *v.maxElement() <<std::endl;
121  trace.info()<< "minElement val = "<<*v.minElement() << std::endl;
122  trace.endBlock();
123  return ((v.max() == 4.1) && (v.min()==2.2));
124 }
125 
130 bool testSimplePoint()
131 {
132  PointVector<3, int> aPVInt3;
133 
134  int t[]={-3 ,4 ,4 ,0};
135  PointVector<4,int> aPoint(t);
136  PointVector<4,int> aFPoint;
137 
138  aPoint *= 5;
139 
140  cout << "aPoint=" << aPoint << endl;
141 
142  trace.beginBlock ( "Test point dimension" );
143  trace.info() << "aPoint dimension="<<aPoint.dimension <<endl;
144  trace.endBlock();
145 
146  if ( aPoint.dimension != 4 )
147  return false;
148 
149  int tt[] = { 3, 4, 2, 2 };
150  PointVector<4,int> v (tt);
151  aPoint = aFPoint + v;
152  trace.beginBlock ( "Test point addition with vector" );
153  trace.info() << "aPoint = "<< aFPoint << " + " << v << endl;
154  trace.info() << "aPoint = "<< aPoint << endl;
155  trace.endBlock();
156 
157  return true;
158 }
159 
160 bool testNorms()
161 {
162  typedef PointVector<3, int> PointType;
163  PointType aPoint;
164 
165  aPoint.at ( 2 ) = 2;
166  aPoint.at ( 1 ) = -1;
167  aPoint.at ( 0 ) = 3;
168 
169  trace.beginBlock ( "Test of Norms" );
170  trace.info() << "aPoint l_2 norm="<<aPoint.norm() <<endl;
171  trace.info() << "aPoint l_1 norm="<<aPoint.norm ( PointType::L_1 ) <<endl;
172  trace.info() << "aPoint l_infty norm="<<aPoint.norm ( PointType::L_infty ) <<endl;
173 
174  trace.info() << "Normalization="<<aPoint.getNormalized () <<endl;
175 
176  trace.endBlock();
177 
178 
179  return ( ( aPoint.norm ( PointType::L_1 ) == 6 ) &&
180  ( aPoint.norm ( PointType::L_infty ) == 3 ) );
181 
182 }
183 
188 bool testSimpleVector()
189 {
190  PointVector<3, int> aPVInt3;
191  PointVector<4, int> aVector;
192  PointVector<4, int> aFVector;
193 
194  trace.beginBlock ( "Test of Vector Dimension" );
195  trace.info() << "aVector dimension="<< aVector.dimension <<endl;
196  trace.info() << "aVector = "<< aVector <<endl;
197  trace.endBlock();
198 
199  if ( aVector.dimension != 4 )
200  return false;
201 
202  aVector += aFVector;
203 
204  return true;
205 }
206 
207 
208 bool testIterator()
209 {
210  PointVector<25,int> aPoint;
211  PointVector<4, int> avector;
212 
213  trace.beginBlock("Point Iterator Test");
214 
215  for (unsigned int i=0;i<25;++i)
216  aPoint.at(i) = i;
217  trace.info() << "aPoint="<<aPoint<< std::endl;
218 
219  trace.info() << "With iterator: ";
220  for (PointVector<25,int>::Iterator it = aPoint.begin() ; it != aPoint.end(); ++it)
221  trace.info() << (*it) <<" " ;
222 
223  trace.info() << std::endl;
224 
225  trace.endBlock();
226 
227  return true;
228 }
229 
230 bool testOperators()
231 {
232  trace.beginBlock("Point Operators Test");
233 
234  DGtal::int32_t t1[] = {1,2,3,4};
236  DGtal::int32_t t2[]= {5,4,3,2};
238 
239  trace.info() << "p1: "<<p1 <<", "<<"p2: "<<p2 <<std::endl;
240  trace.info() << "p1+p2: "<<p1+p2 <<std::endl;
241  trace.info() << "p1*2+p2: "<<p1*2+p2 <<std::endl;
242  trace.info() << "p1-p2: "<<p1-p2 <<std::endl;
243  trace.info() << "inf(p1,p2): "<<p1.inf(p2) <<std::endl;
244  trace.info() << "sup(p1,p2): "<<p1.sup(p2) <<std::endl;
245  trace.info() << "p1 dot p2: "<<p1.dot(p2) <<std::endl;
246 
247  trace.endBlock();
248 
249  return true;
250 }
251 
252 bool testIntegerNorms()
253 {
254  unsigned int nbok = 0;
255  unsigned int nb = 0;
256 
257  DGtal::int32_t t[]= {2,1,3,4};
259  DGtal::int32_t t2[]= {4,5,3,2};
261  PointVector<4,DGtal::int32_t> p = p2 - p1;
262 
263  trace.beginBlock ( "Checking Integer norm1" );
264  trace.info() << "p1: "<<p1 <<", "<<"p2: "<<p2 <<std::endl;
265  nbok += p.norm1() == 8 ? 1 : 0;
266  nb++;
267  trace.info() << "(" << nbok << "/" << nb << ") "
268  << "L1(p2-p1): "<< p.norm1() << "( == 8 ?)" << std::endl;
269  nbok += p.normInfinity() == 4 ? 1 : 0;
270  nb++;
271  trace.info() << "(" << nbok << "/" << nb << ") "
272  << "Linfty(p2-p1): "<< p.normInfinity() << "( == 4 ?)"
273  << std::endl;
274  trace.endBlock();
275 
276  return nbok == nb;
277 }
278 
279 int main()
280 {
281  bool res;
282  res = testSimplePoint()
283  && testSimpleVector()
284  && testNorms()
285  && testIterator()
286  && testComparison()
287  && testOperators()
288  && testIntegerNorms()
289  && testMaxMin();
290  if (res)
291  return 0;
292  else
293  return 1;
294 }
295