00001 #include "TridSketches.h"
00002
00003 #include "MessageService/MsgService.h"
00004
00005 #include "TROOT.h"
00006 #include "TGX11.h"
00007 #include "TVirtualX.h"
00008 #include "TMath.h"
00009 #include "KeySymbols.h"
00010
00011 #include <GL/gl.h>
00012 #include <GL/glx.h>
00013 #include <GL/glu.h>
00014 #include "glf.h"
00015 #include <iostream>
00016 using namespace std;
00017
00018 CVSID("$Id: TridSketches.cxx,v 1.22 2007/09/14 14:12:19 tagg Exp $");
00019
00020 ClassImp(TridSketch)
00021
00022 unsigned int TridSketch::fgSketchIdCounter = 1;
00023 TVector3 TridSketch::fgDefaultColor = TVector3( 1,1,1 );
00024 float TridSketch::fgDefaultTrans = 1.0;
00025 Bool_t TridSketch::fgScenery = false;
00026
00027 TridSketch::TridSketch( int type, TVector3 center ) :
00028 fType( type ),
00029 fHighlighted( false ),
00030 fToggle( true ),
00031 fScenery( fgScenery ),
00032 fTime( -999. ),
00033 fCallList( 0 ),
00034 fCenter( center )
00035 {
00036
00037
00038 fId = fgSketchIdCounter;
00039 fgSketchIdCounter++;
00040
00041 fColor = fgDefaultColor;
00042 fTransparency = fgDefaultTrans;
00043 }
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 TridSketch::~TridSketch( void )
00063 {
00064 glDeleteLists(fCallList,1);
00065 }
00066
00067 void
00068 TridSketch::SetDirty( void )
00069 {
00070 glDeleteLists(fCallList,1);
00071 fCallList = 0;
00072 }
00073
00074 Bool_t
00075 TridSketch::Draw( double timestart, double timeend )
00076 {
00077
00078
00079
00080
00081 if(fToggle) {
00082 if( (fScenery) ||
00083 ( ( fTime >= timestart ) && ( fTime < timeend ) )
00084 ) {
00085
00086
00087 glColor4f(fColor.x(),fColor.y(),fColor.z(),fTransparency);
00088
00089 if(fCallList == 0) {
00090
00091 fCallList = glGenLists(1);
00092 if(fCallList) {
00093 glNewList(fCallList,GL_COMPILE_AND_EXECUTE);
00094 glLoadName(fId);
00095 this->DrawSelf();
00096 glEndList();
00097 } else {
00098 glLoadName(fId);
00099 this->DrawSelf();
00100
00101 MSG("TriD",Msg::kWarning) << "OpenGL problem: Could not create a new display list." << endl;
00102 }
00103 } else {
00104 glCallList(fCallList);
00105 }
00106
00107 return true;
00108
00109 }
00110 }
00111 return false;
00112 }
00113
00114
00115 void TridSketch::GlVertexTVector( TVector3& vect )
00116 {
00117
00118
00119
00120 glVertex3d(vect.x(), vect.y(), vect.z() );
00121 }
00122
00123 double TridSketch::ComputeApparentZ(double* modelViewMatrix)
00124 {
00125 fApparentZ =
00126 modelViewMatrix[2] *fCenter.x() +
00127 modelViewMatrix[6] *fCenter.y() +
00128 modelViewMatrix[10]*fCenter.z()*10000.;
00129 return fApparentZ;
00130 }
00131
00132
00135
00136 ClassImp(TridSketchLine)
00137
00138 TridSketchLine::TridSketchLine( float x1, float y1, float z1, float x2, float y2, float z2 )
00139 : TridSketch(kLine, TVector3((x1+x2)*0.5,(y1+y2)*0.5,(z1+z2)*0.5) )
00140 , fWidth(1.0), fStipple(-1)
00141 {
00142
00143
00144
00145 p[0].SetXYZ(x1,y1,z1);
00146 p[1].SetXYZ(x2,y2,z2);
00147 }
00148
00149 TridSketchLine::TridSketchLine( const TVector3& inp1, const TVector3& inp2 )
00150 : TridSketch( kLine, (inp1 + inp2)*0.5 )
00151 , fWidth(1.0), fStipple(-1)
00152 {
00153
00154
00155
00156 p[0] = inp1;
00157 p[1] = inp2;
00158 }
00159
00160 TridSketchLine::TridSketchLine( const TVector3 &x0, const TVector3 &v1, float half_length )
00161 : TridSketch( kLine, x0 )
00162 , fWidth(1.0), fStipple(-1)
00163 {
00164
00165
00166
00167
00168 p[0] = x0+(v1*half_length);
00169 p[1] = x0-(v1*half_length);
00170 }
00171
00172 void TridSketchLine::DrawSelf( void )
00173 {
00174
00175
00176 glLineWidth(fWidth);
00177 if(fStipple>0) {
00178 glLineStipple(fStipple, 0xAAAA);
00179 glEnable(GL_LINE_STIPPLE);
00180 }
00181 glBegin(GL_LINES);
00182 GlVertexTVector( p[0] );
00183 GlVertexTVector( p[1] );
00184 glEnd();
00185 glLineWidth(1.0);
00186 if(fStipple>0) {
00187 glDisable(GL_LINE_STIPPLE);
00188 }
00189
00190 int error;
00191 while ((error = glGetError()) != GL_NO_ERROR)
00192 MSG("TriD",Msg::kError) << "TridSketchLine::DrawSelf. GL error: "
00193 << "(" << error << ")"
00194 << gluErrorString(error)
00195 << endl;
00196
00197 }
00198
00199
00202
00203 ClassImp(TridSketchPlane)
00204
00205 TridSketchPlane::TridSketchPlane( void )
00206 : TridSketch(kPlane, TVector3())
00207 {
00208 }
00209
00210 TridSketchPlane::TridSketchPlane( const TVector3& p1, const TVector3& p2, const TVector3& p3, const TVector3& p4 )
00211 : TridSketch( kPlane, (p1 + p2 + p3 + p4)*0.25 )
00212 {
00213
00214
00215
00216
00217 p[0] = p1;
00218 p[1] = p2;
00219 p[2] = p3;
00220 p[3] = p4;
00221 }
00222
00223 TridSketchPlane::TridSketchPlane( const TVector3& x0,
00224 const TVector3& v1, float half_length,
00225 const TVector3& v2, float half_width )
00226 : TridSketch( kPlane, x0 )
00227 {
00228
00229
00230
00231 p[0] = x0 + v1*half_length + v2*half_width;
00232 p[1] = x0 - v1*half_length + v2*half_width;
00233 p[2] = x0 - v1*half_length - v2*half_width;
00234 p[3] = x0 + v1*half_length - v2*half_width;
00235 fCenter = x0;
00236 }
00237
00238 void TridSketchPlane::DrawSelf( void )
00239 {
00240
00241
00242 glBegin(GL_QUADS);
00243 GlVertexTVector(p[0]);
00244 GlVertexTVector(p[1]);
00245 GlVertexTVector(p[2]);
00246 GlVertexTVector(p[3]);
00247 glEnd();
00248
00249 }
00250
00253
00254 ClassImp(TridSketchBox)
00255
00256 TridSketchBox::TridSketchBox( void )
00257 : TridSketch( kBox, TVector3() )
00258 {
00259
00260
00261
00262 cout << "TridSketchBox:: Wrong constructor!" << endl;
00263 for(int i=0;i<8;i++) {
00264 cout << "Box " << fId << "\t" << i << "\t" << p[i].x() << "\t" << p[i].y() << "\t" << p[i].z() << endl;
00265 }
00266 }
00267
00268 TridSketchBox::TridSketchBox( const TVector3& p0, const TVector3& p1, const TVector3& p2, const TVector3& p3,
00269 const TVector3& p4, const TVector3& p5, const TVector3& p6, const TVector3& p7 )
00270 : TridSketch( kBox, (p1+p2+p3+p4+p5+p6+p7)*(1.0/8.0) )
00271 {
00272
00273
00274
00275
00276
00277 p[0] = p0;
00278 p[1] = p1;
00279 p[2] = p2;
00280 p[3] = p3;
00281 p[4] = p4;
00282 p[5] = p5;
00283 p[6] = p6;
00284 p[7] = p7;
00285 }
00286
00287 TridSketchBox::TridSketchBox( const TVector3& x0, const TVector3& v1, float half_length,
00288 const TVector3& v2, float half_width,
00289 const TVector3& v3, float half_depth )
00290 : TridSketch( kBox, x0 )
00291 {
00292
00293
00294
00295 p[0] = x0 + v1*half_length + v2*half_width + v3*half_depth;
00296 p[1] = x0 + v1*half_length - v2*half_width + v3*half_depth;
00297 p[2] = x0 + v1*half_length - v2*half_width - v3*half_depth;
00298 p[3] = x0 + v1*half_length + v2*half_width - v3*half_depth;
00299 p[4] = x0 - v1*half_length + v2*half_width + v3*half_depth;
00300 p[5] = x0 - v1*half_length - v2*half_width + v3*half_depth;
00301 p[6] = x0 - v1*half_length - v2*half_width - v3*half_depth;
00302 p[7] = x0 - v1*half_length + v2*half_width - v3*half_depth;
00303 }
00304
00305
00306 void TridSketchBox::DrawSelf( void )
00307 {
00308
00309
00310
00311 glBegin(GL_QUADS);
00312
00313 GlVertexTVector(p[0]);
00314 GlVertexTVector(p[1]);
00315 GlVertexTVector(p[2]);
00316 GlVertexTVector(p[3]);
00317
00318 GlVertexTVector(p[4]);
00319 GlVertexTVector(p[5]);
00320 GlVertexTVector(p[6]);
00321 GlVertexTVector(p[7]);
00322
00323 glEnd();
00324
00325 glBegin(GL_QUAD_STRIP);
00326
00327 GlVertexTVector(p[0]);
00328 GlVertexTVector(p[4]);
00329 GlVertexTVector(p[1]);
00330 GlVertexTVector(p[5]);
00331
00332 GlVertexTVector(p[2]);
00333 GlVertexTVector(p[6]);
00334
00335 GlVertexTVector(p[3]);
00336 GlVertexTVector(p[7]);
00337
00338 GlVertexTVector(p[0]);
00339 GlVertexTVector(p[4]);
00340
00341 glEnd();
00342
00343 int error;
00344 while ((error = glGetError()) != GL_NO_ERROR)
00345 MSG("TriD",Msg::kError) << "TridSketchBox::DrawSelf. GL error: "
00346 << "(" << error << ")"
00347 << gluErrorString(error)
00348 << endl;
00349 }
00350
00351
00354
00355 ClassImp(TridSketch3Prism)
00356
00357 TridSketch3Prism::TridSketch3Prism( void )
00358 : TridSketch( k3Prism, TVector3() )
00359 {
00360
00361
00362
00363 cout << "TridSketch3Prism:: Wrong constructor!" << endl;
00364 for(int i=0;i<8;i++) {
00365 cout << "3Prism " << fId << "\t" << i << "\t" << p[i].x() << "\t" << p[i].y() << "\t" << p[i].z() << endl;
00366 }
00367 }
00368
00369 TridSketch3Prism::TridSketch3Prism( const TVector3& p0, const TVector3& p1, const TVector3& p2, const TVector3& p3,
00370 const TVector3& p4, const TVector3& p5 )
00371 : TridSketch( k3Prism, (p0+p1+p2+p3+p4+p5)*(1.0/6.0) )
00372 {
00373
00374
00375
00376
00377
00378 p[0] = p0;
00379 p[1] = p1;
00380 p[2] = p2;
00381 p[3] = p3;
00382 p[4] = p4;
00383 p[5] = p5;
00384 }
00385
00386 TridSketch3Prism::TridSketch3Prism( const TVector3& x0,
00387 const TVector3& v1, float half_length,
00388 const TVector3& v2, float half_width,
00389 const TVector3& v3, float half_depth,
00390 bool up, bool right)
00391 : TridSketch( k3Prism, x0 )
00392 {
00393
00394
00395
00396
00397 if(up) half_length = -half_length;
00398 if(right) half_width = -half_width;
00399
00400 p[0] = x0 - v1*half_length - v2*half_width - v3*half_depth;
00401 p[1] = x0 + v1*half_length - v2*half_width - v3*half_depth;
00402 p[2] = x0 + v1*half_length + v2*half_width - v3*half_depth;
00403
00404 p[3] = x0 - v1*half_length - v2*half_width + v3*half_depth;
00405 p[4] = x0 + v1*half_length - v2*half_width + v3*half_depth;
00406 p[5] = x0 + v1*half_length + v2*half_width + v3*half_depth;
00407 }
00408
00409
00410 void TridSketch3Prism::DrawSelf( void )
00411 {
00412
00413
00414
00415 glBegin(GL_TRIANGLES);
00416
00417 GlVertexTVector(p[0]);
00418 GlVertexTVector(p[1]);
00419 GlVertexTVector(p[2]);
00420
00421 GlVertexTVector(p[3]);
00422 GlVertexTVector(p[4]);
00423 GlVertexTVector(p[5]);
00424
00425 glEnd();
00426
00427 glBegin(GL_QUAD_STRIP);
00428
00429 GlVertexTVector(p[0]);
00430 GlVertexTVector(p[3]);
00431 GlVertexTVector(p[1]);
00432 GlVertexTVector(p[4]);
00433
00434 GlVertexTVector(p[2]);
00435 GlVertexTVector(p[5]);
00436
00437 GlVertexTVector(p[0]);
00438 GlVertexTVector(p[3]);
00439
00440 glEnd();
00441
00442 int error;
00443 while ((error = glGetError()) != GL_NO_ERROR)
00444 MSG("TriD",Msg::kError) << "TridSketch3Prism::DrawSelf. GL error: "
00445 << "(" << error << ")"
00446 << gluErrorString(error)
00447 << endl;
00448 }
00449
00451
00453
00454 ClassImp(TridSketchTube)
00455
00456 TridSketchTube::TridSketchTube( void )
00457 : TridSketch(kTube, TVector3() )
00458 {
00459
00460
00461
00462 p = new TVector3[6];
00463 cout << "TridSketchTube:: Wrong constructor!" << endl;
00464 for(int i=0;i<8;i++) {
00465 cout << "Tube " << fId << "\t" << i << "\t" << p[i].x() << "\t" << p[i].y() << "\t" << p[i].z() << endl;
00466 }
00467 }
00468
00469
00470 TridSketchTube::TridSketchTube( const TVector3& end1, const TVector3& end2,
00471 Double_t radius, Int_t nfaces, Bool_t closed )
00472 : TridSketch(kTube, (end1+end2)*0.5 )
00473 {
00474
00475
00476
00477 fNfaces = nfaces;
00478 fClosed = closed;
00479 p = new TVector3[nfaces*2];
00480
00481
00482 TVector3 v = end2-end1;
00483 TVector3 n1=v.Orthogonal();
00484 TVector3 n2=v.Cross(n1);
00485
00486 n1.SetMag(1.0);
00487 n2.SetMag(1.0);
00488
00489
00490 for(int i=0;i<fNfaces;i++) {
00491 double angle = (double)i/(double)fNfaces * 2.0*TMath::Pi();
00492 p[i] = end1 + n1*cos(angle)*radius + n2*sin(angle)*radius;
00493 p[i+fNfaces] = end2 + n1*cos(angle)*radius + n2*sin(angle)*radius;
00494 }
00495 }
00496
00497
00498 void TridSketchTube::DrawSelf( void )
00499 {
00500
00501
00502
00503 if(fClosed) {
00504
00505 glBegin(GL_POLYGON);
00506 for(int i=0;i<fNfaces;i++)
00507 GlVertexTVector(p[i]);
00508 glEnd();
00509
00510
00511 glBegin(GL_POLYGON);
00512 for(int i=fNfaces;i<fNfaces*2;i++)
00513 GlVertexTVector(p[i]);
00514 glEnd();
00515 }
00516
00517
00518 glBegin(GL_QUAD_STRIP);
00519 for(int i=0;i<fNfaces+1;i++) {
00520
00521 int iv1 = i%fNfaces;
00522 int iv2 = i%fNfaces + fNfaces;
00523 if((i%2)==0) {
00524 GlVertexTVector(p[iv1]);
00525 GlVertexTVector(p[iv2]);
00526 } else {
00527 GlVertexTVector(p[iv1]);
00528 GlVertexTVector(p[iv2]);
00529 }
00530 }
00531 glEnd();
00532
00533 int error;
00534 while ((error = glGetError()) != GL_NO_ERROR)
00535 MSG("TriD",Msg::kError) << "TridSketchTube::DrawSelf. GL error: "
00536 << "(" << error << ")"
00537 << gluErrorString(error)
00538 << endl;
00539 }
00540
00542
00544
00545 ClassImp(TridSketchEllipse)
00546
00547 TridSketchEllipse::TridSketchEllipse( void )
00548 : TridSketch( kEllipse, TVector3() )
00549 {
00550
00551
00552
00553 p = new TVector3[6];
00554 cout << "TridSketchEllipse:: Wrong constructor!" << endl;
00555 for(int i=0;i<8;i++) {
00556 cout << "Ellipse " << fId << "\t" << i << "\t" << p[i].x() << "\t" << p[i].y() << "\t" << p[i].z() << endl;
00557 }
00558 }
00559
00560
00561 TridSketchEllipse::TridSketchEllipse( const TVector3& center,
00562 const TVector3& axis1,
00563 const TVector3& axis2,
00564 Int_t nvertex )
00565 : TridSketch( kEllipse, center )
00566 {
00567
00568
00569
00570 fNv = nvertex;
00571 p = new TVector3[nvertex];
00572
00573
00574 for(int i=0;i<fNv;i++) {
00575 double angle = (double)i/(double)fNv * 2.0*TMath::Pi();
00576 p[i] = center + axis1*cos(angle) + axis2*sin(angle);
00577 }
00578 }
00579
00580
00581 void TridSketchEllipse::DrawSelf( void )
00582 {
00583
00584
00585
00586 glBegin(GL_POLYGON);
00587 for(int i=0;i<fNv;i++)
00588 GlVertexTVector(p[i]);
00589 glEnd();
00590
00591 int error;
00592 while ((error = glGetError()) != GL_NO_ERROR)
00593 MSG("TriD",Msg::kError) << "TridSketchEllipse::DrawSelf. GL error: "
00594 << "(" << error << ")"
00595 << gluErrorString(error)
00596 << endl;
00597 }
00598
00599
00603
00604 ClassImp(TridSketchText)
00605
00606 TridSketchText::TridSketchText(
00607 const TVector3& pos, const TVector3& up, const TVector3& norm,
00608 const char* text )
00609 : TridSketch( kText, pos ),
00610 fPosition(pos),
00611 fUp(up.Unit()),
00612 fNormal(norm.Unit()),
00613 fScaleHoriz(1),
00614 fScaleVert(1),
00615 fThickness(0.1),
00616 fText(text),
00617 fFont(-1),
00618 fJustify(kCenter)
00619 {
00620 }
00621
00622 void TridSketchText::DrawSelf( void )
00623 {
00624
00625
00626
00627 glPushMatrix();
00628
00629
00630 glTranslated(fPosition.x(),fPosition.y(),fPosition.z());
00631
00632
00633
00634 TVector3 stdnorm(0,0,1);
00635 TVector3 stdup (0,1,0);
00636
00637
00638
00639 TVector3 rot;
00640 Double_t ang1;
00641 if(fNormal == -kv_z) {
00642 rot = TVector3(0,1,0);
00643 ang1 = 180.;
00644 } else {
00645
00646
00647 rot = stdnorm.Cross(fNormal);
00648 ang1= asin(rot.Mag())*57.295779513;
00649 if(ang1!=0) rot.SetMag(1.0);
00650 }
00651
00652 if(ang1!=0)
00653 stdup.Rotate(ang1*0.01745329252,rot);
00654
00655 double ang2 = stdup.Angle(fUp)*57.295779513;
00656
00657
00658 if(ang2!=0)
00659 glRotated(ang2, fNormal.x(), fNormal.y(), fNormal.z() );
00660
00661
00662 if(ang1!=0)
00663 glRotated(ang1, rot.x(), rot.y(), rot.z() );
00664
00665
00666 glScaled(fScaleHoriz, fScaleVert, 1.0);
00667
00668
00669 glTranslated(0.,0.,-1.);
00670
00671
00672 int oldFont = glfGetCurrentFont();
00673 if(fFont>0) glfSetCurrentFont(fFont);
00674
00675 glfSetSymbolDepth(fThickness);
00676 glfStringCentering(true);
00677
00678 float minx, miny, maxx, maxy;
00679 glfGetStringBounds(const_cast<char*>(fText.Data()), &minx, &miny, &maxx, &maxy);
00680
00681
00682 minx -= 1.0;
00683 maxx -= 1.0;
00684
00685 float halfwidth = (minx+maxx)*0.5;
00686 float halfheight = (-miny+maxy)*0.5;
00687
00688
00689 float movex = 0;
00690 float movey = 0;
00691
00692 if(fJustify & kJustifyLeft) movex = -halfwidth + minx;
00693 if(fJustify & kJustifyRight) movex = halfwidth - minx;
00694 if(fJustify & kJustifyTop) movey += halfheight;
00695 if(fJustify & kJustifyBottom) movey -= halfheight;
00696
00697 glTranslatef(-movex,-movey,0.0f);
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713 glfDraw3DSolidString(const_cast<char*>(fText.Data()));
00714
00715 glfSetCurrentFont(oldFont);
00716
00717
00718 glPopMatrix();
00719
00720 int error;
00721 while ((error = glGetError()) != GL_NO_ERROR)
00722 MSG("TriD",Msg::kError) << "TridSketchText::DrawSelf. GL error: "
00723 << "(" << error << ")"
00724 << gluErrorString(error)
00725 << endl;
00726
00727
00728 }
00729