-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vision3D.cpp
490 lines (313 loc) · 13.2 KB
/
Vision3D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Vision3D Class
// Damien MATTEI
#include "Vision3D.hpp"
// implementations
template <class T> Vision3D<T>::Vision3D() : c(), s() {
#ifdef DISPLAY_CONSTRUCTOR
displayConstructor();
#endif
}
// c position of eye or camera ,s position of center of screen
template <class T> Vision3D<T>::Vision3D(Point3D<T> c,Point3D<T> s) : c(c), s(s) {
#ifdef DISPLAY_CONSTRUCTOR
displayConstructor();
#endif
// compute vector w
// create SC vector
Vector3D<T> sc(c,s);
d = sc.norm(); // compute norm of SC
// vectors defining screen frame
// vector u
Vector3D<T> u;
// vector v
//Vector3D<T> v;
// vector w
//Vector3D<T> w;
// Vector3D<T> * ptr_w = sc | d; // normalize w
// w = *ptr_w;
//w = sc / d;
Vector3D<T> w = Vector3D<T>(sc / d);
/* compute vector u
* knowing u.w = 0
* and |u| = 1
*/
u.z = 0;
u.y = - ( w.x / sqrt(w.x * w.x + w.y * w.y)); // arbitrary taking negative solution for orientation
u.x = - w.y * u.y / w.x;
// // vector u
// this definition create a possible problem:
// Vision3D.cpp:50:19: warning: variable 'u' is uninitialized when used within its own initialization
// [-Wuninitialized]
// - w.y * u.y / w.x
// ^
// Vector3D<T> u = Vector3D<T>( 0 ,
// - ( w.x / sqrt(w.x * w.x + w.y * w.y)) , // arbitrary taking negative solution for orientation
// - w.y * u.y / w.x
// ) ;
/* compute vector v
* knowing u ^ v = w
* i.e: v = w ^ u
* ( ^ : cross product)
*/
// vector v
Vector3D<T> v = Vector3D<T>( w ^ u );
// change of basis matrix
// T da1,da2,da3;
// da1 = v.y * w.z - v.z * w.y;
// da2 = v.z * w.x - v.x * w.z;
// da3 = v.x * w.y - v.y * w.x;
Vector3D<T> da = v ^ w;
// T db1,db2,db3;
// db1 = w.y * u.z - w.z * u.y;
// db2 = w.z * u.x - w.x * u.z;
// db3 = w.x * u.y - w.y * u.x;
Vector3D<T> db = w ^ u;
// T dc1,dc2,dc3;
// dc1 = u.y * v.z - u.z * v.y;
// dc2 = u.z * v.x - u.x * v.z;
// dc3 = u.x * v.y - u.y * v.x;
Vector3D<T> dc = u ^ v;
T dau,dbv,dcw;
// dau = u.x * da1 + u.y * da2 + u.z * da3;
// dbv = v.x * db1 + v.y * db2 + v.z * db3;
// dcw = w.x * dc1 + w.y * dc2 + w.z * dc3;
// dot products - produits scalaires
dau = u * da;
dbv = v * db;
dcw = w * dc;
// m[0][0] = da1 / dau; m[0][1] = da2 / dau; m[0][2] = da3 / dau;
// m[1][0] = db1 / dbv; m[1][1] = db2 / dbv; m[1][2] = db3 / dbv;
// m[2][0] = dc1 / dcw; m[2][1] = dc2 / dcw; m[2][2] = dc3 / dcw;
// Vector3D<T> * daquPtr = da | dau;
// Vector3D<T> * dbqvPtr = db | dbv;
// Vector3D<T> * dcqwPtr = dc | dcw;
// // q mean quotient i.e: daqu -> da Quotient dau
// Vector3D<T> & daqu = *daquPtr;
// Vector3D<T> & dbqv = *dbqvPtr;
// Vector3D<T> & dcqw = *dcqwPtr;
Vector3D<T> daqu = da / dau;
Vector3D<T> dbqv = db / dbv;
Vector3D<T> dcqw = dc / dcw;
// m[0][0] = da.x / dau; m[0][1] = da.y / dau; m[0][2] = da.z / dau;
// m[1][0] = db.x / dbv; m[1][1] = db.y / dbv; m[1][2] = db.z / dbv;
// m[2][0] = dc.x / dcw; m[2][1] = dc.y / dcw; m[2][2] = dc.z / dcw;
// this would be more simple with a Matrix3X3 class
// m[0][0] = daqu.x; m[0][1] = daqu.y; m[0][2] = daqu.z;
// m[1][0] = dbqv.x; m[1][1] = dbqv.y; m[1][2] = dbqv.z;
// m[2][0] = dcqw.x; m[2][1] = dcqw.y; m[2][2] = dcqw.z;
DEBUG(cerr << endl << endl << "Vision3D.cpp : Vision3D<T>::Vision3D(Point3D<T> c,Point3D<T> s) : m3x3" << endl;)
m3x3 = Matrix3x3<T>(daqu,dbqv,dcqw);
}
template <class T> string Vision3D<T>::display(void) {
std::stringstream stream;
stream << "Vision3D @" << " 0x" << std::hex << (long)this << endl << *this;
return stream.str();
}
template <class T> Vision3D<T>::~Vision3D() {
#ifdef DISPLAY_CONSTRUCTOR
displayDestructor();
#endif
}
template <class T> void Vision3D<T>::displayConstructor() {
cout << "# Vision3D constructor " << this->display() << " #" << endl;
}
template <class T> void Vision3D<T>::displayDestructor() {
cout << "# Vision3D destructor " << this->display() << " #" << endl;
}
template <class T> ostream& operator<< (ostream &out, Vision3D<T> &vis3d) {
out << "observer : " << vis3d.c
<< ", center of screen : " << vis3d.s
// << ", u vector: " << vis3d.u
// << ", v vector: " << vis3d.v
// << ", w vector: " << vis3d.w
;
return out;
}
// une version avec reference en argument et resultat
template<class T> Point2D<T> * Vision3D<T>::projectionRef(Point3D<T> & p) {
// Point3D<T> pPrim( m[0][0] * p.x + m[0][1] * p.y + m[0][2] * p.z,
// m[1][0] * p.x + m[1][1] * p.y + m[1][2] * p.z,
// m[2][0] * p.x + m[2][1] * p.y + m[2][2] * p.z );
Point3D<T> pPrim = m3x3 * p;
T r = d / (d + pPrim.z);
// screen point
Point2D<T> * ps = new Point2D<T>( pPrim.y * r,
- pPrim.x * r );
#ifdef DEBUG
std::cout << " Vision3D<T>::projection : ps : " << ps << std::endl;
#endif
return ps; // warning: ps should be deleted by the calling function
}
template<class T> Point2D<T> Vision3D<T>::projection(Point3D<T> p) {
// Point3D<T> pPrim( m[0][0] * p.x + m[0][1] * p.y + m[0][2] * p.z,
// m[1][0] * p.x + m[1][1] * p.y + m[1][2] * p.z,
// m[2][0] * p.x + m[2][1] * p.y + m[2][2] * p.z );
Point3D<T> pPrim = m3x3 * p;
T r = d / (d + pPrim.z);
// screen point
Point2D<T> ps( pPrim.y * r,
- pPrim.x * r );
#ifdef DEBUG
std::cout << " Vision3D<T>::projection : ps : " << ps << std::endl;
#endif
// todo: tester les recopie (si elles se font bien) lors du retour du parametre ps
return ps;
}
template<class T> Point2D<int> Vision3D<T>::convert2Pixel(Point2D<T> p) {
Point2D<int> pix( (int) (p.x * pixelInUnit),
(int) (p.y * pixelInUnit) );
return pix;
}
template<class T> Point2D<int> * Vision3D<T>::convert2PixelRef(Point2D<T> & p) {
Point2D<int> * pix = new Point2D<int>( (int) (p.x * pixelInUnit),
(int) (p.y * pixelInUnit) );
return pix; // warning: pix should be deleted by the calling function
}
template<class T> Point2D<int> Vision3D<T>::convert2AbsPixel(Point2D<int> p) {
Point2D<int> pix( p.x + winHalfSizeX,
p.y + winHalfSizeY );
return pix;
}
template<class T> Point2D<int> * Vision3D<T>::convert2AbsPixelRef(Point2D<int> & p) {
Point2D<int> * pix = new Point2D<int>( p.x + winHalfSizeX,
p.y + winHalfSizeY );
return pix;
}
template<class T> Point2D<int> Vision3D<T>::convert2ScreenCoord(Point2D<int> p) {
// verifier ce calcul
Point2D<int> pix( p.x ,
2 * winHalfSizeY - p.y );
return pix;
}
template<class T> Point2D<int> * Vision3D<T>::convert2ScreenCoordRef(Point2D<int> & p) {
Point2D<int> * pix = new Point2D<int>( p.x ,
2 * winHalfSizeY - p.y );
return pix; // warning: pix should be deleted by the calling function
}
// functional programming style :-)
// TODO: tester les recopies que celà crée lors du return à la fin de la fonction
template<class T> Point2D<int> Vision3D<T>::projectPoint3DtoPixel(Point3D<T> p) {
return convert2ScreenCoord(
convert2AbsPixel(
convert2Pixel(
projection(p))));
}
// une version avec pointeurs, liberer les variables intermédiaires, donc decomposee en etapes d'affectations aussi
// less functional programming :-( but a little more fast
template<class T> Point2D<int> * Vision3D<T>::projectPoint3DtoPixelRef(Point3D<T> & p) {
// screen point
Point2D<T> * ps = projectionRef(p);
Point2D<T> & refPntT = *ps;
Point2D<int> * pix = convert2PixelRef(refPntT);
delete ps;
Point2D<int> & refPntInt = *pix;
Point2D<int> * pixAbs = convert2AbsPixelRef(refPntInt);
delete pix;
refPntInt = *pixAbs;
Point2D<int> * pixScreenCoord = convert2ScreenCoordRef(refPntInt);
delete pixAbs;
return pixScreenCoord;
}
// // compute the 3D -> 2D for the object
// // so we got the projected 2D points as result
// // nota: this is intermediate calculus that could be used for debugging
// template<class T> void Vision3D<T>::computePoints3DtoPoints2D(void) {
// // finding the vertex list
// list < Point3D<T> > vertexList = univ.vertexList;
// // iterate on the list to compute 3D to 2D projection
// // note : i put typename hint because as it is a template definition
// // compiler can not know the type it is until the compiler knows T
// typename list< Point3D<T> >::iterator iterP3D;
// for ( iterP3D = vertexList.begin();
// iterP3D != vertexList.end();
// ++iterP3D )
// points2d.push_back(projection(*iterP3D));
// }
// // compute the pixels, starting from 2D points
// // nota: again this is a whole part calculus splitted in two for debugging
// template<class T> void Vision3D<T>::computePoints2DtoPixels(void) {
// // iterate on the list 2D point list to compute pixels
// // note : i put typename hint because as it is a template definition
// // compiler can not know the type it is until the compiler knows T
// typename list< Point2D<T> >::iterator iterP2D;
// for (iterP2D = points2d.begin(); iterP2D != points2d.end(); ++iterP2D)
// pixels.push_back(convert2ScreenCoord(convert2AbsPixel(convert2Pixel(*iterP2D))));
// }
// associate Point3D and Pixels in unordered map (Point3D <-> Pixel)
// DEPRECATED new version use pointers instead of copying objects
// template<class T> void Vision3D<T>::associatePt3Pix2InMap(void) {
// Point2D<int> pt2;
// // finding the vertex list
// list < Point3D<T> > vertexList = univ.vertexList;
// // iterate on the list to compute 3D to 2D projection and Pixels calculus
// // note : i put typename hint because as it is a template definition
// // compiler can not know the type it is until the compiler knows T
// typename list< Point3D<T> >::iterator iterP3D;
// DEBUG(std::cout << "getViewField() : " << getViewField() << std::endl;
// std::cout << "getHalfScreenSizeX() : " << getHalfScreenSizeX() << std::endl;
// std::cout << "getPixelInUnit() : " << getPixelInUnit() << std::endl;)
// for (iterP3D = vertexList.begin(); iterP3D != vertexList.end(); ++iterP3D) {
// DEBUG(std::cout << " *iterP3D : " << *iterP3D << std::endl;)
// Point2D<float> ptproj = projection(*iterP3D);
// DEBUG(std::cout << " ptproj : " << ptproj << std::endl;)
// Point2D<int> ptc2p = convert2Pixel(ptproj);
// DEBUG(std::cout << " ptc2p : " << ptc2p << std::endl;)
// pt2 = projectPoint3DtoPixel(*iterP3D);
// DEBUG(std::cout << " pt2 : " << pt2 << std::endl;)
// htPointPixel[*iterP3D] = pt2;
// }
// }
// associate Point3D and Pixels in unordered map (Point3D <-> Pixel)
// pointers and reference version
template<class T> void Vision3D<T>::associatePt3Pix2PointersInMap(void) {
Point2D<int> * ptr_pt2;
// finding the vertex list
list < Point3D<T> *> vertexPtrList = univ.containerPoint3DptrlistC;
// iterate on the list to compute 3D to 2D projection and Pixels calculus
// note : i put typename hint because as it is a template definition
// compiler can not know the type it is until the compiler knows T
typename list< Point3D<T> *>::iterator iterPtrP3D;
DEBUG(std::cout << "getViewField() : " << getViewField() << std::endl;
std::cout << "getHalfScreenSizeX() : " << getHalfScreenSizeX() << std::endl;
std::cout << "getPixelInUnit() : " << getPixelInUnit() << std::endl;)
for (iterPtrP3D = vertexPtrList.begin(); iterPtrP3D != vertexPtrList.end(); ++iterPtrP3D) {
Point3D<T> * ptrP3D = *iterPtrP3D;
Point3D<T> & refP3D = *ptrP3D;
DEBUG(std::cout << " **iterPtrP3D : " << **iterPtrP3D << std::endl;)
// seems to be for debug only
DEBUG(Point2D<float> ptproj = projection(**iterPtrP3D);)
DEBUG(std::cout << " ptproj : " << ptproj << std::endl;)
DEBUG(Point2D<int> ptc2p = convert2Pixel(ptproj);)
DEBUG(std::cout << " ptc2p : " << ptc2p << std::endl;)
//pt2 = projectPoint3DtoPixel(**iterPtrP3D);
//pt2 = projectPoint3DtoPixel(*ptrP3D);
// a pointer returned from projection & co
ptr_pt2 = projectPoint3DtoPixelRef(refP3D);
DEBUG(Point2D<int> & ref_pt2 = *ptr_pt2;)
DEBUG(std::cout << " ref_pt2 : " << ref_pt2 << std::endl;)
//htPointersPointPixel[*iterPtrP3D] = &pt2;
//htPointersPointPixel[*ptrP3D] = &pt2;
//htPointersPointPixel[refP3D] = &pt2;
htPointersPointPixel[ptrP3D] = ptr_pt2;
}
}
// for test coding , not used , a functional approach with lambda
template<class T> void Vision3D<T>::associatePt3Pix2PointersInMapWithTransform(void) {
// finding the vertex list
list < Point3D<T> *> vertexPtrList = univ.containerPoint3DptrlistC;
list < Point2D<int> *> containerPoint2DptrList;
std::transform (vertexPtrList.begin(), vertexPtrList.end(), containerPoint2DptrList.begin(),
//projectPoint3DtoPixelRef);
//std::function< Point2D<int> * (Point3D<T> &) > (&Vision3D<T>::projectPoint3DtoPixelRef));
//std::function< Point2D<int> * (Point3D<T> &) > (&Vision3D<T>::projectPoint3DtoPixelRef));
//std::mem_fun(&Vision3D<T>::projectPoint3DtoPixelRef));
[this](Point3D<T> * ptrP3D){ // lambda
Point2D<int> * ptr_pt2;
Point3D<T> & refP3D = *ptrP3D;
ptr_pt2 = projectPoint3DtoPixelRef(refP3D);
htPointersPointPixel[ptrP3D] = ptr_pt2;
return ptr_pt2;
});
}
template class Vision3D<float>;
template ostream& operator<< (ostream &out, Vision3D<float> &vis3d);