00001
00002
00003
00004
00005 #include <TROOT.h>
00006 #include <TCanvas.h>
00007 #include <TPad.h>
00008 #include <TPaveLabel.h>
00009 #include <iostream>
00010 #include "math.h"
00011 #include "TMath.h"
00012
00013 #include "BesVisLib/BesView.h"
00014 #include "BesVisLib/BesGeometry.h"
00015 #include "BesVisLib/BesEvent.h"
00016 #include "BesVisLib/BesCursor.h"
00017
00018 using namespace std;
00019
00020 #ifndef __CINT__
00021 ClassImp(BesView)
00022 #endif
00023
00024
00025
00026
00027
00028 BesView::BesView() : BesTView(1) {
00029
00030
00031 if ( gDebug ) cout << "BesView normal ctor called" << endl;
00032
00033
00034 fLabel = new TPaveLabel(0.02,0.17,0.02,0.09,"3D View");
00035 fLabel->SetFillColor(42);
00036
00037
00038 fStatus3D = new BesStatus();
00039 fStatus3D->Default(k3DView);
00040
00041
00042 fStatusXY = new BesStatus();
00043 fStatusXY->Default(kXYView);
00044
00045
00046 fStatusZR = new BesStatus();
00047 fStatusZR->Default(kZRView);
00048
00049
00050 fStatusCurrent = new BesStatus();
00051
00052
00053 fViewType = k3DView;
00054 fStatusCurrent->Transfer(fStatus3D,1);
00055 if ( !gBesGeometry ) {
00056 cout << "BesGeometry does not exist! " << endl;
00057 return;
00058 }
00059
00060 m_BesR = gBesGeometry->GetBesR();
00061 m_BesZ = gBesGeometry->GetBesZ();
00062 DefaultRange();
00063 fExtentDefault = GetExtent();
00064
00065 fZoomFactor = 1.2;
00066 fMarkerSize = 0.1;
00067 }
00068
00069
00070
00071 BesView::~BesView() {
00072
00073
00074 if ( gDebug ) cout << "BesView default dtor called" << endl;
00075
00076 delete fLabel;
00077
00078 delete fStatus3D;
00079 delete fStatusXY;
00080 delete fStatusZR;
00081 delete fStatusCurrent;
00082
00083 }
00084
00085
00086
00087 void BesView::DefaultRange()
00088 {
00089 SetRange(-1.0*m_BesR, -1.0*m_BesR, -0.5*m_BesZ, m_BesR, m_BesR, 0.5*m_BesZ);
00090 }
00091
00092
00093
00094 void BesView::SetViewType(EBESViewType ViewType, Bool_t resetview) {
00095
00096
00097
00098
00099 if ( fViewType & k3DView ){
00100
00101 fStatus3D->Transfer(fStatusCurrent,0);
00102
00103 } else if ( fViewType & kXYView ) {
00104
00105 fStatusXY->Transfer(fStatusCurrent,0);
00106
00107 } else if ( fViewType & kZRView ) {
00108
00109 fStatusZR->Transfer(fStatusCurrent,0);
00110
00111 }
00112
00113 fViewType = ViewType;
00114 ClearPad();
00115
00116
00117 Int_t iret;
00118 if ( fViewType & k3DView ) {
00119
00120
00121
00122
00123
00124 Int_t iPerspective = 0;
00125
00126 if (IsPerspective()) iPerspective = 1;
00127
00128
00129 fStatusCurrent->Transfer(fStatus3D,1);
00130
00131
00132 if ( !gBesGeometry ) return;
00133
00134
00135 if ( resetview ) {
00136
00137 this->Front();
00138 this->Center();
00139
00140 DefaultRange();
00141 for (int i = 0; i < 2; i++) this->ZoomView(gPad, fZoomFactor);
00142
00143
00144 fMarkerSize = 0.1;
00145 }
00146
00147
00148
00149 cout << "is there any problem after update from root's version 4.03 to 4.04" << endl;
00150 gBesGeometry->Draw("3D,SAME");
00151
00152 this->SetParallel();
00153
00154
00155 if (iPerspective == 1) this->SetPerspective();
00156
00157
00158 if ( gEvent ) gEvent->DrawTracks("");
00159
00160
00161 } else if ( fViewType & kXYView ) {
00162
00163
00164
00165
00166
00167
00168 fStatusCurrent->Transfer(fStatusXY,1);
00169
00170
00171 if ( !gBesGeometry ) return;
00172
00173
00174 if ( resetview ) {
00175 SetView(0, 0, 270, iret);
00176 this->Center();
00177
00178 DefaultRange();
00179 DefaultRange();
00180 for (int i = 0; i < 2; i++) this->ZoomView(gPad, fZoomFactor);
00181
00182
00183 fMarkerSize = 0.1;
00184
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194 gBesGeometry->Draw("XY,SAME");
00195
00196
00197 if ( gEvent ) {
00198 gEvent->DrawHits("");
00199 gEvent->DrawTracks("");
00200 }
00201
00202
00203 } else if ( fViewType & kZRView ) {
00204
00205
00206
00207
00208
00209
00210 fStatusCurrent->Transfer(fStatusZR,1);
00211
00212
00213 if ( !gBesGeometry ) return;
00214
00215
00216 if ( resetview ) {
00217 SetView(180,90,90,iret);
00218 this->Center();
00219
00220 DefaultRange();
00221 DefaultRange();
00222 for (int i = 0; i < 2; i++) this->ZoomView(gPad, fZoomFactor);
00223
00224
00225 fMarkerSize = 0.1;
00226
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236 gBesGeometry->Draw("ZR,SAME");
00237
00238
00239 if ( gEvent ) {
00240 gEvent->DrawHits("");
00241 gEvent->DrawTracks("");
00242 }
00243
00244
00245 }
00246
00247
00248 DrawLabel();
00249 }
00250
00251 void BesView::SetViewTypeBare(EBESViewType ViewType, Bool_t resetview) {
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 }
00350
00351
00352
00353 void BesView::ClearPad() {
00354
00355
00356
00357
00358
00359 if ( gDebug ) cout << "BesView::ClearPad called" << endl;
00360 if ( gPad->GetListOfPrimitives() ) gPad->GetListOfPrimitives()->Clear();
00361 }
00362
00363
00364
00365 void BesView::ExecuteEvent(Int_t event, Int_t px, Int_t py) {
00366
00367
00368 if ( gDebug ) cout << "BesView::ExecuteEvent called" << endl;
00369
00370
00371 if (gROOT->GetEditorMode()) {
00372 gPad->ExecuteEvent(event,px,py);
00373 return;
00374 }
00375
00376 if (gBesCursor->GetType() == kBesHand) {
00377
00378 gPad->SetCursor(kMove);
00379 static Double_t xmin, ymin, xmax, ymax, x0, y0;
00380 Double_t dx, dy, x, y;
00381 switch (event) {
00382
00383 case kButton1Down:
00384
00385 xmin = gPad->GetX1();
00386 ymin = gPad->GetY1();
00387 xmax = gPad->GetX2();
00388 ymax = gPad->GetY2();
00389 x0 = gPad->AbsPixeltoX(px);
00390 y0 = gPad->AbsPixeltoY(py);
00391 break;
00392
00393 case kButton1Motion:
00394 gPad->SetCursor(kMove);
00395
00396 case kButton1Up:
00397
00398 x = gPad->AbsPixeltoX(px);
00399 y = gPad->AbsPixeltoY(py);
00400 dx = -(x - x0);
00401 dy = -(y - y0);
00402
00403 gPad->Range(xmin+dx, ymin+dy, xmax+dx, ymax+dy);
00404
00405 gPad->Modified();
00406 break;
00407
00408 default:
00409 gPad->SetCursor(kMove);
00410 }
00411 }
00412 else if (gBesCursor->GetType() == kBesPick){
00413
00414 gPad->SetCursor(kPointer);
00415
00416 if ( fViewType & k3DView ) {
00417 ExecuteRotateView(event, px, py);
00418 }
00419 }
00420 }
00421
00422
00423
00424 void BesView::Move(Int_t px, Int_t py) {
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448 Double_t x1, y1, x2, y2, dx, dy, x, y, x0, y0;
00449 x1 = gPad->GetX1();
00450 y1 = gPad->GetY1();
00451 x2 = gPad->GetX2();
00452 y2 = gPad->GetY2();
00453 x0 = gPad->PixeltoX(0);
00454 y0 = gPad->PixeltoY(0);
00455 x = gPad->PixeltoX(px);
00456 y = gPad->PixeltoY(py);
00457 dx = -(x - x0);
00458 dy = -(y - y0);
00459
00460 gPad->Range(x1+dx, y1+dy, x2+dx, y2+dy);
00461 gPad->Modified();
00462 }
00463
00464
00465
00466 void BesView::Center() {
00467
00468
00469
00470
00471
00472 Double_t xrange, yrange;
00473 xrange = gPad->GetX2() - gPad->GetX1();
00474 yrange = gPad->GetY2() - gPad->GetY1();
00475 gPad->Range(-xrange/2, -yrange/2, xrange/2, yrange/2);
00476 gPad->Modified();
00477
00478 }
00479
00480
00481
00482 void BesView::SetZoomRatio(Double_t ratio)
00483 {
00484 DefaultRange();
00485 ZoomView(gPad, ratio);
00486
00487
00488 fStatusCurrent->fZoom = ratio;
00489 fStatusXY->fZoom = ratio;
00490 fStatusZR->fZoom = ratio;
00491 }
00492
00493
00494
00495 void BesView::ZoomIn() {
00496
00497
00498 fMarkerSize = fMarkerSize * fZoomFactor;
00499 ZoomView(gPad, fZoomFactor);
00500
00501
00502 fStatusCurrent->fZoom = this->GetZoomRatio();
00503 fStatusXY->fZoom = this->GetZoomRatio();
00504 fStatusZR->fZoom = this->GetZoomRatio();
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528 }
00529
00530
00531
00532 void BesView::ZoomOut() {
00533
00534
00535 fMarkerSize = fMarkerSize / fZoomFactor;
00536 UnzoomView(gPad, fZoomFactor);
00537
00538 fStatusCurrent->fZoom = this->GetZoomRatio();
00539 fStatusXY->fZoom = this->GetZoomRatio();
00540 fStatusZR->fZoom = this->GetZoomRatio();
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558 }
00559
00560
00561
00562 Int_t BesView::GetRSign(Float_t Phi) {
00563
00564
00565
00566 Int_t Sign;
00567
00568
00569 Float_t Phi2 = (Phi - 2*TMath::Pi()*Int_t(Phi/(2*TMath::Pi())))
00570 * 180./TMath::Pi();
00571 if ( Phi2 < 0 ) Phi2 += 360;
00572 if ( Phi2 >= fStatusCurrent->fPhiRangeMin && Phi2 < fStatusCurrent->fPhiRangeMax ) {
00573 Sign = 1;
00574 } else {
00575 Sign = -1;
00576 }
00577 return Sign;
00578 }
00579
00580
00581
00582 void BesView::SetPhiRange(Float_t PhiMin, Float_t PhiMax) {
00583
00584
00585 if ( gDebug ) cout << "BesView::SetPhiRange called" << endl;
00586
00587 fStatusCurrent->fPhiRangeMin = PhiMin;
00588 fStatusCurrent->fPhiRangeMax = PhiMax;
00589
00590
00591 if ( !gBesGeometry ) return;
00592
00593 UpdateView(0);
00594 }
00595
00596
00597
00598 void BesView::UpdateView(Bool_t resetview) {
00599
00600
00601 if ( !gBesGeometry ) return;
00602
00603
00604 SetViewType(fViewType,resetview);
00605 }
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618 void BesView::DrawLabel() {
00619
00620
00621
00622 fLabel->SetX1(-0.9);
00623 fLabel->SetX2(-0.6);
00624 fLabel->SetY1(-0.9);
00625 fLabel->SetY2(-0.78);
00626
00627 switch (fViewType) {
00628 case k3DView:
00629 fLabel->SetLabel("3D View");
00630 break;
00631 case kXYView:
00632 fLabel->SetLabel("XY View");
00633 break;
00634 case kZRView:
00635 fLabel->SetLabel("ZR View");
00636 break;
00637 }
00638 fLabel->Draw("BR,NDC,SAME");
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655 }
00656
00657
00658
00659 void BesView::WCtoNDC(const Float_t *pw, Float_t *pn) {
00660
00661
00662
00663
00664
00665 BesTView::WCtoNDC(pw, pn);
00666
00667
00668 if ( fStatusCurrent->fFishEye == kTRUE ) {
00669 Float_t r, s;
00670 if ( fViewType & kZRView ) {
00671 Float_t pvWC[3], pvNDC[3];
00672 pvWC[0] = +4;
00673 pvWC[1] = 0;
00674 pvWC[2] = 0;
00675 BesTView::WCtoNDC(pvWC, pvNDC);
00676 pn[0] = TMath::Sign(Double_t(TMath::Sqrt(TMath::Abs(pn[0] - pvNDC[0]))),
00677 Double_t(pn[0] - pvNDC[0])) + pvNDC[0];
00678 pn[1] = TMath::Sign(Double_t(TMath::Sqrt(TMath::Abs(pn[1] - pvNDC[1]))),
00679 Double_t(pn[1] - pvNDC[1])) + pvNDC[1];
00680 } else {
00681 r = TMath::Sqrt(TMath::Power(pn[0],2)
00682 +TMath::Power(pn[1],2));
00683 s = TMath::Sqrt(r)/r;
00684 pn[0] *= s;
00685 pn[1] *= s;
00686 }
00687 }
00688 }
00689
00690
00691
00692 void BesView::WCtoNDC(const Double_t *pw, Double_t *pn) {
00693
00694
00695
00696
00697
00698 BesTView::WCtoNDC(pw, pn);
00699
00700 if ( fStatusCurrent->fFishEye == kTRUE ) {
00701 Double_t r, s;
00702 if ( fViewType & kZRView ) {
00703 Double_t pvWC[3], pvNDC[3];
00704 pvWC[0] = +4;
00705 pvWC[1] = 0;
00706 pvWC[2] = 0;
00707 BesTView::WCtoNDC(pvWC, pvNDC);
00708 pn[0] = TMath::Sign(TMath::Sqrt(TMath::Abs(pn[0] - pvNDC[0])),
00709 pn[0] - pvNDC[0]) + pvNDC[0];
00710 pn[1] = TMath::Sign(TMath::Sqrt(TMath::Abs(pn[1] - pvNDC[1])),
00711 pn[1] - pvNDC[1]) + pvNDC[1];
00712 } else {
00713
00714 r = TMath::Sqrt(TMath::Power(pn[0],2)
00715 +TMath::Power(pn[1],2));
00716 s = TMath::Sqrt(r)/r;
00717 pn[0] *= s;
00718 pn[1] *= s;
00719 }
00720 }
00721 }
00722
00723
00724
00725 void BesView::NDCtoWC(const Float_t* pn, Float_t* pw) {
00726
00727
00728 Float_t p[3];
00729 p[0] = pn[0];
00730 p[1] = pn[1];
00731 p[2] = pn[2];
00732
00733 if ( fStatusCurrent->fFishEye == kTRUE ) {
00734 Float_t r;
00735 if ( fViewType & kZRView ) {
00736 Float_t pvWC[3], pvNDC[3];
00737 pvWC[0] = +4;
00738 pvWC[1] = 0;
00739 pvWC[2] = 0;
00740 BesTView::WCtoNDC(pvWC, pvNDC);
00741 p[0] = TMath::Sign((pn[0] - pvNDC[0])*(pn[0] - pvNDC[0]),
00742 pn[0] - pvNDC[0]) + pvNDC[0];
00743 p[1] = TMath::Sign((pn[1] - pvNDC[1])*(pn[1] - pvNDC[1]),
00744 pn[1] - pvNDC[1]) + pvNDC[1];
00745 } else {
00746 r = TMath::Sqrt(TMath::Power(p[0],2)
00747 +TMath::Power(p[1],2));
00748 p[0] *= r;
00749 p[1] *= r;
00750 }
00751 }
00752 BesTView::NDCtoWC(p, pw);
00753 }
00754
00755
00756
00757 void BesView::NDCtoWC(const Double_t* pn, Double_t* pw) {
00758
00759
00760 Double_t p[3];
00761 p[0] = pn[0];
00762 p[1] = pn[1];
00763 p[2] = pn[2];
00764
00765 if ( fStatusCurrent->fFishEye == kTRUE ) {
00766 Double_t r;
00767 if ( fViewType & kZRView ) {
00768 Double_t pvWC[3], pvNDC[3];
00769 pvWC[0] = +4;
00770 pvWC[1] = 0;
00771 pvWC[2] = 0;
00772 BesTView::WCtoNDC(pvWC, pvNDC);
00773 p[0] = TMath::Sign((pn[0] - pvNDC[0])*(pn[0] - pvNDC[0]),
00774 pn[0] - pvNDC[0]) + pvNDC[0];
00775 p[1] = TMath::Sign((pn[1] - pvNDC[1])*(pn[1] - pvNDC[1]),
00776 pn[1] - pvNDC[1]) + pvNDC[1];
00777 } else {
00778 r = TMath::Sqrt(TMath::Power(p[0],2)
00779 +TMath::Power(p[1],2));
00780 p[0] *= r;
00781 p[1] *= r;
00782 }
00783 }
00784 BesTView::NDCtoWC(p, pw);
00785 }
00786
00787
00788
00789 void BesView::Side3D() {
00790
00791
00792
00793 this->SetViewType(k3DView,1);
00794 Int_t iret;
00795 this->SetView(0,90,270,iret);
00796 }
00797
00798
00799
00800 char *BesView::GetObjectInfo(Int_t px, Int_t py) const {
00801
00802
00803 Double_t pw[3];
00804 Double_t Pndc[3] = { gPad->AbsPixeltoX(px), gPad->AbsPixeltoY(py), 0};
00805 ((BesView*)gPad->GetView())->NDCtoWC(Pndc, pw);
00806
00807
00808
00809
00810
00811
00812 static char info[64];
00813 if ( fViewType & kZRView ) {
00814 sprintf(info, "z=%-.3fmm, r=%-.3fmm", pw[2], pw[1]);
00815 return info;
00816 } else {
00817 sprintf(info, "x=%-.3fmm, y=%-.3fmm", pw[0], pw[1]);
00818 return info;
00819 }
00820 }
00821
00822
00823
00824 void BesView::Reset() {
00825
00826
00827
00828
00829 fStatus3D->Default(k3DView);
00830 fStatusXY->Default(kXYView);
00831 fStatusZR->Default(kZRView);
00832 fZoomFactor = 1.2;
00833
00834
00835 fStatusCurrent->Default(fViewType);
00836
00837
00838
00839 this->UpdateView(1);
00840 gPad->Update();
00841 }