00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _LTI_DRAW_3D_H
00033 #define _LTI_DRAW_3D_H
00034
00035 #include "ltiDraw.h"
00036 #include "ltiIoObject.h"
00037
00038 namespace lti {
00039 template <class T>
00040 class rayTraceObject;
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 template<class T>
00108 class draw3D : public draw<T> {
00109 public:
00110
00111
00112
00113
00114
00115 static T getGray(const float& val);
00116
00117
00118
00119
00120
00121
00122 class hmatrix {
00123 public:
00124
00125
00126
00127 hmatrix() {
00128 clear();
00129 };
00130
00131
00132
00133
00134 hmatrix(const hmatrix& other) {
00135 copy(other);
00136 }
00137
00138
00139
00140
00141 ~hmatrix() {
00142 };
00143
00144
00145
00146
00147 void clear() {
00148 for (int i=0;i<3;i++)
00149 for (int j=0;j<4;j++)
00150 at(i,j) = 0.0;
00151 };
00152
00153
00154
00155
00156 dpoint3D operator*(const dpoint3D& p) const {
00157 dpoint3D temp;
00158
00159 const hmatrix &m = (*this);
00160
00161 temp.x = m.at(0,0)*p.x + m.at(0,1)*p.y + m.at(0,2)*p.z + m.at(0,3);
00162 temp.y = m.at(1,0)*p.x + m.at(1,1)*p.y + m.at(1,2)*p.z + m.at(1,3);
00163 temp.z = m.at(2,0)*p.x + m.at(2,1)*p.y + m.at(2,2)*p.z + m.at(2,3);
00164
00165 return temp;
00166 };
00167
00168
00169
00170
00171 double &at(const int m,const int n) {return mem[m][n];};
00172
00173
00174
00175
00176 const double &at(const int m,const int n) const {return mem[m][n];};
00177
00178
00179
00180
00181 hmatrix& copy(const hmatrix& other) {
00182 int j,i;
00183 for (j=0;j<3;++j) {
00184 for (i=0;i<4;++i) {
00185 mem[j][i] = other.mem[j][i];
00186 }
00187 }
00188 return *this;
00189 }
00190
00191
00192
00193
00194 hmatrix& operator=(const hmatrix& other) {
00195 return copy(other);
00196 }
00197
00198 protected:
00199 double mem[3][4];
00200 };
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 class parameters : public ioObject {
00217 public:
00218
00219
00220
00221 parameters()
00222 : zoom(1.0),perspective(0.0),center(0.5,0.5),
00223 camPos(0.0,0.0,1.0),elevation(0.0),azimuth(0.0),
00224 lightPosition(0,0,1000000),
00225 diffuseColor(draw3D<T>::getGray(223.0f/255.0f)),
00226 ambientColor(draw3D<T>::getGray(32.0f/255.0f)) {
00227 };
00228
00229
00230
00231
00232 parameters(const parameters& other)
00233 : ioObject() {
00234 copy(other);
00235 }
00236
00237
00238
00239
00240 virtual const char* getTypeName() const {
00241 return "draw3D::parameters";
00242 };
00243
00244
00245
00246
00247 parameters& copy(const parameters& other) {
00248 camPos = other.camPos;
00249 elevation = other.elevation;
00250 azimuth = other.azimuth;
00251 zoom = other.zoom;
00252 perspective = other.perspective;
00253 center = other.center;
00254 lightPosition = other.lightPosition;
00255 diffuseColor = other.diffuseColor;
00256 ambientColor = other.ambientColor;
00257 return *this;
00258 };
00259
00260
00261
00262
00263 parameters& operator=(const parameters& other) {
00264 return copy(other);
00265 }
00266
00267
00268
00269
00270 virtual parameters* clone() const {
00271 return new parameters(*this);
00272 };
00273
00274
00275
00276
00277
00278
00279
00280
00281 virtual bool write(ioHandler& handler,const bool complete=true) const {
00282 bool b=true;
00283
00284 if (complete) {
00285 b=handler.writeBegin();
00286 }
00287
00288 if (b) {
00289 lti::write(handler,"camPos",camPos);
00290 lti::write(handler,"elevation",elevation);
00291 lti::write(handler,"azimuth",azimuth);
00292 lti::write(handler,"zoom",zoom);
00293 lti::write(handler,"perspective",perspective);
00294 lti::write(handler,"center",center);
00295 lti::write(handler,"lightPosition",lightPosition);
00296 lti::write(handler,"diffuseColor",diffuseColor);
00297 lti::write(handler,"ambientColor",ambientColor);
00298 }
00299
00300 if (complete) {
00301 b=b && handler.writeEnd();
00302 }
00303
00304 return b;
00305 };
00306
00307
00308
00309
00310
00311
00312
00313
00314 virtual bool read(ioHandler& handler,const bool complete=true) {
00315 bool b=true;
00316
00317 if (complete) {
00318 b=handler.readBegin();
00319 }
00320
00321 if (b) {
00322 lti::read(handler,"camPos",camPos);
00323 lti::read(handler,"elevation",elevation);
00324 lti::read(handler,"azimuth",azimuth);
00325 lti::read(handler,"zoom",zoom);
00326 lti::read(handler,"perspective",perspective);
00327 lti::read(handler,"center",center);
00328 lti::read(handler,"lightPosition",lightPosition);
00329 lti::read(handler,"diffuseColor",diffuseColor);
00330 lti::read(handler,"ambientColor",ambientColor);
00331 }
00332
00333 if (complete) {
00334 b=b && handler.readEnd();
00335 }
00336
00337 return b;
00338 };
00339
00340
00341
00342
00343 void setCamera(const double& x1,const double& y1,const double& z1,
00344 const double& x2,const double& y2,const double& z2) {
00345
00346
00347 double dx,dy,dz;
00348 double dx2,dy2,dz2;
00349 double alpha,beta,cosalpha,cosbeta;
00350
00351 dx = x1-x2;
00352 dy = y1-y2;
00353 dz = z1-z2;
00354
00355 dx2 = dx*dx;
00356 dy2 = dy*dy;
00357 dz2 = dz*dz;
00358
00359 if ((dx2+dy2) != 0) {
00360 cosalpha = dx/(sqrt(dx2+dy2));
00361 alpha = abs(acos(cosalpha));
00362 if (dy<0) {
00363 alpha *= -1.0;
00364 }
00365 }
00366 else {
00367 cosalpha = 1.0;
00368 alpha = 0;
00369 }
00370
00371 if ((dx2+dy2+dz2) != 0) {
00372 cosbeta = sqrt((dx2+dy2)/(dx2+dy2+dz2));
00373 beta = abs(acos(cosbeta));
00374 if (dz<0) {
00375 beta *= -1.0;
00376 }
00377 }
00378 else {
00379 cosbeta = 1.0;
00380 beta = 0;
00381 }
00382
00383 setCamera(x2,y2,z2,(Pi/2.0) - beta,alpha);
00384 }
00385
00386
00387
00388
00389 void setCamera(const double& x1,const double& y1,const double& z1,
00390 const double& theElevation,const double& theAzimuth) {
00391 camPos.copy(dpoint3D(x1,y1,z1));
00392 elevation = theElevation;
00393 azimuth = theAzimuth;
00394 }
00395
00396
00397
00398
00399 void setCamera(const dpoint3D& x1,
00400 const dpoint3D& x2) {
00401 setCamera(x1.x,x1.y,x1.z,x2.x,x2.y,x2.z);
00402 }
00403
00404
00405
00406
00407 void setCamera(const dpoint3D& x1,
00408 const double& theElevation,const double& theAzimuth) {
00409 camPos.copy(x1);
00410 elevation = theElevation;
00411 azimuth = theAzimuth;
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421 double zoom;
00422
00423
00424
00425
00426
00427
00428 double perspective;
00429
00430
00431
00432
00433
00434 dpoint center;
00435
00436
00437
00438
00439 dpoint3D camPos;
00440
00441
00442
00443
00444
00445
00446
00447 double elevation;
00448
00449
00450
00451
00452
00453
00454
00455 double azimuth;
00456
00457
00458
00459
00460 dpoint3D lightPosition;
00461
00462
00463
00464
00465
00466
00467
00468
00469 T diffuseColor;
00470
00471
00472
00473
00474
00475
00476
00477
00478 T ambientColor;
00479 };
00480
00481
00482 public:
00483
00484
00485
00486 draw3D();
00487
00488
00489
00490 ~draw3D();
00491
00492
00493
00494
00495 virtual const char* getTypeName() const;
00496
00497
00498
00499
00500 void use(matrix<T>& img);
00501
00502
00503
00504
00505 bool setParameters(const parameters& opt);
00506
00507
00508
00509
00510 const parameters& getParameters() const {return param;};
00511
00512
00513
00514
00515 parameters& getParameters() {return param;};
00516
00517
00518
00519
00520 void setCamera(const double& x1,const double& y1,const double& z1,
00521 const double& x2,const double& y2,const double& z2);
00522
00523
00524
00525
00526 void setCamera(const double& x1,const double& y1,const double& z1,
00527 const double& elevation,const double& azimuth);
00528
00529
00530
00531
00532 void setCamera(const dpoint3D& x1,
00533 const dpoint3D& x2);
00534
00535
00536
00537
00538 void setCamera(const dpoint3D& x1,
00539 const double& elevation,const double& azimuth);
00540
00541
00542
00543
00544
00545
00546
00547 void setPerspective(const double& p);
00548
00549
00550
00551
00552
00553
00554
00555
00556 void setCenter(const double& x,const double& y);
00557
00558
00559
00560
00561
00562
00563
00564
00565 void setCenter(const dpoint& x);
00566
00567
00568
00569
00570 void setZoom(const double& z);
00571
00572
00573
00574
00575
00576 dpoint getOriginPosition() const;
00577
00578
00579
00580
00581 const hmatrix& getCameraMatrix() const;
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595 void resetDeepCtrl(const bool deep=true);
00596
00597
00598
00599
00600 void p3Dto2D(const double& x,const double& y,const double& z,
00601 int &x2,int &y2,double &z2) const;
00602
00603
00604
00605
00606 void p3Dto2D(const dpoint3D& p3,point& p2,double &z2) const;
00607
00608
00609
00610
00611 void set3D(const dpoint3D& p);
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623 void set2D(const int& x,const int& y,const double& z);
00624
00625
00626
00627
00628 void marker3D(const double& x,const double& y,const double& z);
00629
00630
00631
00632
00633 void marker3D(const double& x,
00634 const double& y,
00635 const double& z,
00636 const char* style);
00637
00638
00639
00640
00641 void set3D(const double& x,const double& y,const double& z);
00642
00643
00644
00645
00646 template<class U>
00647 void set3D(const hPoint3D<U>& p) {
00648 set3D(p.x/p.h,p.y/p.h,p.z/p.h);
00649 };
00650
00651
00652
00653
00654 void line3DTo(const double& x,const double& y,const double& z);
00655
00656
00657
00658
00659 void line3DTo(const dpoint3D& p);
00660
00661
00662
00663
00664 void line3D(const dpoint3D& a,const dpoint3D& b);
00665
00666
00667
00668
00669 void line3D(const double& x, const double& y, const double& z,
00670 const double& x2,const double& y2,const double& z2);
00671
00672
00673
00674
00675 void box(const dpoint3D& a,
00676 const dpoint3D& b, const bool filled=true);
00677
00678
00679
00680
00681
00682 void box(const dpoint3D& a,
00683 const dpoint3D& b,
00684 const T lineColor);
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701 void ellipsoid(const vector<double> &mean,
00702 const matrix<double> &sigma,
00703 const double &cst);
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734 void set3D(const channel& chnl,
00735 const float& height,
00736 const point& gridSize = point(5,5),
00737 const point& sampleRate = point(1,1),
00738 const bool onlyPoints = false,
00739 const bool useBoxes = false,
00740 const bool heightColor = false,
00741 const bool drawLines = false,
00742 const T& lineColor = T(),
00743 const bool drawContour = false,
00744 const T& contourColor = T());
00745
00746
00747
00748
00749
00750 void grid(const double size,const int nbSteps);
00751
00752
00753
00754
00755 void axis(const double size);
00756
00757
00758
00759
00760
00761 void axis(const double size,
00762 const T& colorX,
00763 const T& colorY,
00764 const T& colorZ);
00765
00766
00767 protected:
00768
00769
00770
00771
00772
00773
00774
00775
00776 class triangle {
00777 public:
00778
00779
00780
00781 triangle() {
00782 }
00783
00784
00785
00786
00787 dpoint3D& operator[](const int idx) {
00788 assert(idx < 3);
00789 return pts[idx];
00790 }
00791
00792
00793
00794
00795 const dpoint3D& operator[](const int idx) const {
00796 assert(idx < 3);
00797 return pts[idx];
00798 }
00799
00800
00801
00802
00803 dpoint3D getNormal() const {
00804 dpoint3D a,b,n;
00805 a.subtract(pts[0],pts[2]);
00806 b.subtract(pts[1],pts[2]);
00807 n.x = a.y*b.z - a.z*b.y;
00808 n.y = a.z*b.x - a.x*b.z;
00809 n.z = a.x*b.y - a.y*b.x;
00810
00811 return n;
00812 }
00813
00814
00815
00816
00817
00818 bool visible(const hmatrix& cam) const {
00819 dpoint3D n(getNormal());
00820 return ((cam.at(0,0)*n.x + cam.at(0,1)*n.y + cam.at(0,2)*n.z) >= 0);
00821 }
00822
00823 protected:
00824
00825
00826
00827 dpoint3D pts[3];
00828 };
00829
00830 void drawSymbol(double x, double y,double z);
00831
00832
00833
00834
00835 parameters param;
00836
00837
00838
00839
00840 bool deepCtrl;
00841
00842
00843
00844
00845 matrix<float> deepImg;
00846
00847
00848
00849
00850 dpoint3D act;
00851
00852
00853
00854
00855 hmatrix cam;
00856
00857
00858
00859
00860 dpoint3D camTarget;
00861
00862
00863
00864
00865 dpoint center;
00866
00867
00868
00869
00870 dpoint precenter;
00871
00872
00873
00874
00875 bool correctLine (int &x,int &y,double &z,
00876 const int& x2, const int& y2, const double& z2);
00877
00878
00879
00880
00881
00882
00883 T shadingColor(const dpoint3D& nrm,const T& color) const;
00884
00885
00886
00887
00888 void set3D(const triangle& t,const bool filled = true);
00889
00890
00891
00892
00893
00894
00895 void ioLine(const point& p1,const double& z1,
00896 const point& p2,const double& z2,
00897 ivector& in, ivector& out,
00898 dvector& zIn, dvector& zOut,
00899 const int firstY) const;
00900
00901
00902
00903
00904 inline double copysign(const double& a,const double& b);
00905
00906
00907
00908
00909 inline int sign(const double& a) {return (a >= 0.0) ? 1 : -1;};
00910
00911 private:
00912
00913
00914
00915
00916 const channel* mChannel;
00917 float mHeight;
00918 point mGridSize;
00919 point mSampleRate;
00920 bool mOnlyPoints;
00921 bool mUseBoxes;
00922 bool mHeightColor;
00923 bool mDrawLines;
00924 T mLineColor;
00925 bool mDrawContour;
00926 T mContourColor;
00927
00928
00929
00930
00931
00932 void drawCell(const int x,const int y);
00933
00934 };
00935
00936 template<class T>
00937 inline double draw3D<T>::copysign(const double& a,const double& b) {
00938 return (sign(a)*sign(b))*a;
00939 }
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954 template <class T>
00955 class rayTraceObject : public object {
00956 public:
00957
00958
00959
00960
00961 rayTraceObject(draw3D<T>& drawer);
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973 virtual bool rayTrace(const int x,const int y,
00974 double& z2,T& val) = 0;
00975
00976
00977
00978
00979
00980 virtual bool render() = 0;
00981
00982
00983
00984
00985 virtual const char* getTypeName() const;
00986
00987 protected:
00988
00989
00990
00991 draw3D<T>& drawer;
00992
00993
00994
00995
00996 const typename draw3D<T>::parameters& param;
00997
00998
00999
01000
01001 typename draw3D<T>::hmatrix cam;
01002
01003
01004
01005
01006 typename draw3D<T>::hmatrix camInv;
01007
01008
01009
01010
01011 dpoint center;
01012
01013
01014
01015
01016 T multiplyNormed(const T& a,const T& b) const;
01017
01018
01019
01020
01021 T addSaturated(const T& a,const T& b) const;
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034 void p2Dto3D(const int& x,
01035 const int& y,
01036 dpoint3D& imgp,
01037 dpoint3D& ray) const;
01038
01039
01040
01041
01042
01043
01044 void buildInverseCamera();
01045
01046 };
01047
01048
01049
01050
01051 template<class T>
01052 class rayTraceEllipsoid : public rayTraceObject<T> {
01053 public:
01054
01055
01056
01057
01058 rayTraceEllipsoid(draw3D<T>& drawer);
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073 rayTraceEllipsoid(draw3D<T>& drawer,
01074 const dpoint3D& theMean,
01075 const dmatrix& covar,
01076 const double& theMahDist = 1);
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089 void setEllipsoid(const dpoint3D& theMean,
01090 const dmatrix& covar,
01091 const double& theMahDist = 1);
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102 virtual bool rayTrace(const int x,const int y,double& z2,T& val);
01103
01104
01105
01106
01107
01108 virtual bool render();
01109
01110
01111
01112
01113 virtual const char* getTypeName() const;
01114
01115 protected:
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138 bool intersectRayEllipsoid(const dpoint3D &ip,
01139 const dpoint3D &ray,
01140 const dpoint3D &mu,
01141 const matrix<double> &sigma,
01142 const double &mah,
01143 double &lambda,
01144 dpoint3D &sp);
01145
01146
01147
01148
01149 void calcNormal(const dpoint3D &intersectionPoint,
01150 const dpoint3D &mu,
01151 const matrix<double> &sigma,
01152 dpoint3D &normal) const;
01153
01154
01155
01156
01157 dpoint3D mean;
01158
01159
01160
01161
01162 dmatrix sigma;
01163
01164
01165
01166
01167 dmatrix invMat;
01168
01169
01170
01171
01172
01173 double mahDist;
01174
01175
01176
01177
01178 T storedColor;
01179
01180 private:
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190 double t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t16,t17,t18,t21,
01191 t22,t23,t24,t26,t27,t29,t30,t31,t32,t35,t46,t47,t48,t49,t50,t52,t54,
01192 t56,t57,t58,t61,t63,t65,t66,t69,t71,t73,t74,t76,t77,t81,t83,t85,t88,
01193 t91,t93,t96,t98,t99,t102,t104,t106,t108,t110,t114,t117,t119,t125,t127,
01194 t130,t138,t139,t141,t142,t144,t146,t158,t161,t168,t169,t179,t183,t205,
01195 t226;
01196
01197
01198
01199
01200 dpoint3D ip;
01201
01202
01203
01204
01205 dpoint3D sp;
01206
01207
01208
01209
01210 dpoint3D ray;
01211
01212
01213
01214
01215 dpoint3D normal;
01216
01217
01218
01219
01220 dpoint3D lDir;
01221
01222
01223
01224
01225 double lambda,cosL;
01226
01227 };
01228
01229
01230
01231
01232
01233
01234
01235
01236 inline bool write(ioHandler& handler,const draw3D<rgbPixel>::parameters& p,
01237 const bool complete = true) {
01238 return p.write(handler,complete);
01239 }
01240
01241 inline bool write(ioHandler& handler,const draw3D<float>::parameters& p,
01242 const bool complete = true) {
01243 return p.write(handler,complete);
01244 }
01245
01246 inline bool write(ioHandler& handler,const draw3D<ubyte>::parameters& p,
01247 const bool complete = true) {
01248 return p.write(handler,complete);
01249 }
01250
01251 inline bool write(ioHandler& handler,const draw3D<double>::parameters& p,
01252 const bool complete = true) {
01253 return p.write(handler,complete);
01254 }
01255
01256
01257
01258
01259
01260
01261
01262 inline bool read(ioHandler& handler,draw3D<rgbPixel>::parameters& p,
01263 const bool complete = true) {
01264 return p.read(handler,complete);
01265 }
01266
01267 inline bool read(ioHandler& handler,draw3D<ubyte>::parameters& p,
01268 const bool complete = true) {
01269 return p.read(handler,complete);
01270 }
01271
01272 inline bool read(ioHandler& handler,draw3D<float>::parameters& p,
01273 const bool complete = true) {
01274 return p.read(handler,complete);
01275 }
01276
01277 inline bool read(ioHandler& handler,draw3D<double>::parameters& p,
01278 const bool complete = true) {
01279 return p.read(handler,complete);
01280 }
01281
01282
01283
01284 }
01285
01286 #endif