CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TEveEllipsoidGL.cc
Go to the documentation of this file.
2 
3 #include "TEveProjections.h" // AMT missing getter for projection center / beam-spot
4 
7 #include "TEveProjectionManager.h"
8 
9 #include "TMath.h"
10 
11 #include "TGLRnrCtx.h"
12 
13 #include "TMatrixDEigen.h"
14 #include "TMatrixDSym.h"
15 
16 #include "TDecompSVD.h"
17 #include "TGLIncludes.h"
18 
19 //==============================================================================
20 // TEveEllipsoidGL
21 //==============================================================================
22 
23 //______________________________________________________________________________
24 // OpenGL renderer class for TEveEllipsoid.
25 //
26 
27 
28 
29 //______________________________________________________________________________
31  TGLObject(), fE(0)
32 {
33  // Constructor.
34 
35  // fDLCache = kFALSE; // Disable display list.
36 }
37 
38 //______________________________________________________________________________
39 Bool_t TEveEllipsoidGL::SetModel(TObject* obj, const Option_t* /*opt*/)
40 {
41  // Set model object.
42 
43  fE = SetModelDynCast<TEveEllipsoid>(obj);
44  return kTRUE;
45 }
46 
47 //______________________________________________________________________________
49 {
50  // Set bounding box.
51  ( (TEveEllipsoid*)fExternalObj)->ComputeBBox();
52  SetAxisAlignedBBox(((TEveEllipsoid*)fExternalObj)->AssertBBox());
53 }
54 
55 namespace {
56 GLUquadric* quad = 0; // !!!! AMT check why TGLQuadric crashes on mac
57 }
58 
59 //______________________________________________________________________________
60 void TEveEllipsoidGL::DirectDraw(TGLRnrCtx& /*rnrCtx*/) const
61 {
62  // Render with OpenGL.
63 
64  // printf("TEveEllipsoidGL::DirectDraw LOD %s\n", fE->GetName());
65 
66  glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT | GL_LIGHTING_BIT);
67  glEnable(GL_NORMALIZE );
68  if(!quad)
69  quad = gluNewQuadric();
70 
71  glPushMatrix();
72 
73  TMatrixDSym xxx(3);
74  for(int i=0;i<3;i++)
75  for(int j=0;j<3;j++)
76  {
77  xxx(i,j) = fE->RefEMtx()(i+1,j+1);
78  }
79  TMatrixDEigen eig(xxx);
80 
81 
82  // rewrite for multmatrix ....
83  TEveTrans x;
84  for(int i=0;i<3;i++)
85  for(int j=0;j<3;j++)
86  {
87  x(i+1, j+1) = eig.GetEigenVectors()(i,j);
88  }
89 
90  TVector3 a = x.GetBaseVec(1);
91  TVector3 c = a.Cross(x.GetBaseVec(2));
92  x.SetBaseVec(3, c);
93 
94  glTranslatef(fE->RefPos()[0], fE->RefPos()[1], fE->RefPos()[2]);
95  glMultMatrixd(x.Array());
96  glScalef(fE->RefExtent3D()[0] , fE->RefExtent3D()[1], fE->RefExtent3D()[2]);
97  gluSphere(quad,1. , 30, 30);
98 
99  glPopMatrix();
100  glPopAttrib();
101 
102 
103  // gluDeleteQuadric(quad);
104 }
105 
106 
107 //==============================================================================
108 // TEveEllipsoidProjectedGL
109 //==============================================================================
110 
111 //______________________________________________________________________________
112 // OpenGL renderer class for TEveEllipsoidProjected.
113 //
114 
115 
116 
117 //______________________________________________________________________________
119  fM(0)
120 {
121  // Constructor.
122 
123  // fDLCache = kFALSE; // Disable display list.
124  fMultiColor = kTRUE;
125 }
126 
127 //______________________________________________________________________________
128 Bool_t TEveEllipsoidProjectedGL::SetModel(TObject* obj, const Option_t* /*opt*/)
129 {
130  // Set model object.
131 
132  fM = SetModelDynCast<TEveEllipsoidProjected>(obj);
133  fE = dynamic_cast<TEveEllipsoid*>(fM->GetProjectable());
134  return fE != 0;
135 }
136 
137 //______________________________________________________________________________
139 {
140  // Set bounding box.
141 
142  SetAxisAlignedBBox(((TEveEllipsoidProjected*)fExternalObj)->AssertBBox());
143 }
144 
145 //______________________________________________________________________________
146 void TEveEllipsoidProjectedGL::DirectDraw(TGLRnrCtx& rnrCtx) const
147 {
148  // Render with OpenGL.
149 
150  TEveProjection *proj = fM->GetManager()->GetProjection();
151 
152 
153  glPushAttrib(GL_ENABLE_BIT| GL_POLYGON_BIT | GL_LINE_BIT | GL_POINT_BIT);
154  glDisable(GL_LIGHTING);
155  glDisable(GL_CULL_FACE);
156 
157  glPushMatrix();
158  if ( proj->GetType() == TEveProjection::kPT_RPhi)
159  DrawRhoPhi();
160  else
161  DrawRhoZ();
162 
163  glPopMatrix();
164  glPopAttrib();
165 }
166 
167 //______________________________________________________________________________
168 void TEveEllipsoidProjectedGL::drawArch(float phiStart, float phiEnd, float phiStep, TEveVector& v0, TEveVector& v1, TEveVector& v2) const
169 {
170  TEveProjection *proj = fM->GetManager()->GetProjection();
171  float phi = phiStart;
172  while (phi < phiEnd ) {
173  TEveVector v = v0 + v1*((float)cos(phi)) + v2*((float)sin(phi));
174  proj->ProjectVector(v, fM->fDepth);
175  glVertex3fv(v.Arr());
176 
177  phi += phiStep;
178  }
179  TEveVector v = v0 + v1*((float)cos(phiEnd)) + v2*((float)sin(phiEnd));
180  proj->ProjectVector(v, fM->fDepth);
181  glVertex3fv(v.Arr());
182 }
183 
184  //-------------------------------------------------------------------------------
186 {
187  // printf("TEveEllipsoidProjectedGL::DirectDraw [%s ]\n", fE->GetName() );
188 
189  TMatrixDSym xxx(3);
190  for(int i=0;i<2;i++)
191  for(int j=0;j<2;j++)
192  {
193  xxx(i,j) = fE->RefEMtx()(i+1,j+1);
194  }
195 
196  TMatrixDEigen eig(xxx);
197  TVectorD xxxEig ( eig.GetEigenValuesRe());
198 
199 
200  // Projection supports only floats :(
201  TEveVector v0(fE->RefPos()[0], fE->RefPos()[1], 0);
202  TEveVector v1(eig.GetEigenVectors()(0, 0) , eig.GetEigenVectors()(0, 1), 0 );
203  v1 *= fE->fEScale * sqrt(TMath::Abs(xxxEig[0]));
204  TEveVector v2(eig.GetEigenVectors()(1, 0) , eig.GetEigenVectors()(1, 1), 0 );
205  v2 *= fE->fEScale * sqrt(TMath::Abs(xxxEig[1]));
206 
207  TEveProjection *proj = fM->GetManager()->GetProjection();
208 
209  // fill
210  glBegin(GL_POLYGON);
211  drawArch(0, TMath::TwoPi(), TMath::TwoPi()/20, v0, v1, v2);
212  glEnd();
213 
214  // frame
215  TGLUtil::LineWidth(fE->fLineWidth);
216  TGLUtil::Color(fE->fLineColor);
217 
218  glBegin(GL_LINE_LOOP);
219  drawArch(0, TMath::TwoPi(), TMath::TwoPi()/20, v0, v1, v2);
220  glEnd();
221 
222  glBegin(GL_LINES);
223  {
224  // glColor3f(1, 0, 0);
225  TEveVector p1 = v0 - v1;
226  TEveVector p2 = v0 + v1;
227  proj->ProjectVector(p1, fM->fDepth);
228  proj->ProjectVector(p2, fM->fDepth);
229  glVertex3fv(p1.Arr());
230  glVertex3fv(p2.Arr());
231  }
232  {
233  // glColor3f(0, 1, 0);
234  TEveVector p1 = v0 - v2;
235  TEveVector p2 = v0 + v2;
236  proj->ProjectVector(p1, fM->fDepth);
237  proj->ProjectVector(p2, fM->fDepth);
238  glVertex3fv(p1.Arr());
239  glVertex3fv(p2.Arr());
240  }
241  glEnd();
242 
243 
244 
245 }
246 
247 //--------------------------------------------------------------------
249 {
250  // printf("TEveEllipsoidProjectedGL::DirectDraw [%s ]\n", fE->GetTitle() );
251 
252  TEveVector v0(fE->RefPos()[0], fE->RefPos()[1], fE->RefPos()[2]);
253 
254  TMatrixDSym xxx(3);
255  xxx(0, 0) = 1;
256  for(int i=1;i<3;i++)
257  for(int j=1;j<3;j++)
258  {
259  xxx(i,j) = fE->RefEMtx()(i+1,j+1);
260  }
261  TMatrixDEigen eig(xxx);
262  TVectorD xxxEig ( eig.GetEigenValuesRe());
263 
264 
265  TEveVector v1(0, eig.GetEigenVectors()(1, 2), eig.GetEigenVectors()(2, 2));
266  v1 *= fE->fEScale * sqrt(TMath::Abs(xxxEig[2]));
267 
268  TEveVector v2(0, eig.GetEigenVectors()(1, 1), eig.GetEigenVectors()(2, 1));
269  v2 *= fE->fEScale * sqrt(TMath::Abs(xxxEig[1]));
270  if (v1[1]*v2[2] > v1[2]*v2[1])
271  v2 *= -1;
272 
273 
274  TEveProjection *proj = fM->GetManager()->GetProjection();
275 
276  // ellipse intersection with projection center
277  bool splitted = false;
278  int N = 20;
279  double phiStep = TMath::TwoPi()/N;
280 
281  // projection center can be moved in beam-spot
282  float bs = 0;
283  if (proj->GetDisplaceOrigin())
284  bs = proj->RefCenter()[1];
285 
286  float da = v2[1]*v2[1] + v1[1]*v1[1];
287  float db = 2 * v1[1] * (v0[1]-bs);
288  float dc = (v0[1]-bs)*(v0[1]-bs) - v2[1]*v2[1];
289  float disc = (db*db -4*da*dc);
290 
291  if ( disc > 0) {
292  disc = sqrt(disc);
293  float cosS1 = ( -db + disc)/(2 * da);
294  float cosS2 = ( -db - disc)/(2 * da);
295  if (TMath::Abs(cosS1) < 1) {
296  splitted = true;
297  // printf("splitted \n");
298 
299  double phi1 = acos(cosS1);
300  double phi2 = acos(cosS2);
301  TEveVector ps1 = v0 + v1*((float)cos(phi1)) + v2*((float)sin(phi1));
302  TEveVector ps2 = v0 + v1*((float)cos(phi2)) + v2*((float)sin(phi2));
303 
304 
305 
306  // acos has values [0, Pi] , check the symetry over x axis (mirroring)
307  if (TMath::Abs(ps1[1] - bs) > 1e-5)
308  phi1 = TMath::TwoPi() -phi1;
309 
310  if (TMath::Abs(ps2[1] - bs) > 1e-5)
311  phi2 = TMath::TwoPi() -phi2;
312 
313  int N = 20;
314  double phiStep = TMath::TwoPi()/N;
315  double phiOffset = phiStep*0.1;
316  double phiMin = TMath::Min(phi1, phi2);
317  double phiMax = TMath::Max(phi1, phi2);
318  // printf(" %f %f \n",phi1*TMath::RadToDeg(), phi2*TMath::RadToDeg() );
319 
320  // fill
321  // upper clothing
322  glBegin(GL_POLYGON);
323  drawArch(phiMin + phiOffset, phiMax - phiOffset, phiStep, v0, v1, v2);
324  glEnd();
325  // bottom clothing
326  glBegin(GL_POLYGON);
327  drawArch(phiMax + phiOffset, phiMax + TMath::TwoPi() - (phiMax -phiMin) - phiOffset, phiStep, v0, v1, v2);
328  glEnd();
329 
330  // frame
331  TGLUtil::LineWidth(fE->fLineWidth);
332  TGLUtil::Color(fE->fLineColor);
333  // upper clothing
334  glBegin(GL_LINE_LOOP);
335  drawArch(phiMin + phiOffset, phiMax - phiOffset, phiStep, v0, v1, v2);
336  glEnd();
337  // bottom clothing
338  glBegin(GL_LINE_LOOP);
339  drawArch(phiMax + phiOffset, phiMax + TMath::TwoPi() - (phiMax -phiMin) - phiOffset, phiStep, v0, v1, v2);
340  glEnd();
341 
342  }
343  }
344 
345 
346  if (!splitted) {
347  glBegin(GL_POLYGON);
348  drawArch(0, TMath::TwoPi(), phiStep, v0, v1, v2);
349  glEnd();
350  TGLUtil::LineWidth(fE->fLineWidth);
351  TGLUtil::Color(fE->fLineColor);
352  glBegin(GL_LINE_LOOP);
353  drawArch(0, TMath::TwoPi(), phiStep, v0, v1, v2);
354  glEnd();
355  }
356 
357  drawRhoZAxis(v0, v2);
358  drawRhoZAxis(v0, v1);
359 }
360 
361 //______________________________________________________________________________
362 void TEveEllipsoidProjectedGL::drawRhoZAxis(TEveVector& v0, TEveVector& v2) const
363 {
364  glBegin(GL_LINES);
365  TEveProjection* proj = fM->GetManager()->GetProjection();
366 
367  float bs = 0;
368  if (proj->GetDisplaceOrigin())
369  bs = proj->RefCenter()[1];
370 
371  float off = (v2[1] > v0[1] ) ? 0.01 : -0.01;
372  TEveVector alu = v0 + v2;
373  proj->ProjectVector(alu, fM->fDepth);
374  glVertex3fv(alu.Arr());
375 
376  if (TMath::Abs(v0[1]/v2[1]) < 1 )
377  {
378  alu = v0 - ((float) ((1-off) *(v0[1]-bs)/v2[1])) * v2;
379  proj->ProjectVector(alu, fM->fDepth);
380  glVertex3fv(alu.Arr());
381 
382  //============================
383 
384  alu = v0 - ((float) ((1+off) * (v0[1]-bs)/v2[1])) * v2;
385  proj->ProjectVector(alu, fM->fDepth);
386  glVertex3fv(alu.Arr());
387  }
388 
389  alu = v0 - v2;
390  proj->ProjectVector(alu, fM->fDepth);
391  glVertex3fv(alu.Arr());
392 
393  glEnd();
394 }
const double TwoPi
int i
Definition: DBlmapReader.cc:9
TEveVector & RefPos()
Definition: TEveEllipsoid.h:38
void drawArch(float pStart, float pEnd, float phiStep, TEveVector &v0, TEveVector &v1, TEveVector &v2) const
virtual void DirectDraw(TGLRnrCtx &rnrCtx) const
virtual Bool_t SetModel(TObject *obj, const Option_t *opt=0)
TEveEllipsoid * fE
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
tuple db
Definition: EcalCondDB.py:151
virtual Bool_t SetModel(TObject *obj, const Option_t *opt=0)
T Min(T a, T b)
Definition: MathUtil.h:39
void drawRhoZAxis(TEveVector &v, TEveVector &) const
T sqrt(T t)
Definition: SSEVec.h:18
TEveTrans & RefEMtx()
Definition: TEveEllipsoid.h:40
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
T Abs(T a)
Definition: MathUtil.h:49
int j
Definition: DBlmapReader.cc:9
virtual void SetBBox()
double p2[4]
Definition: TauolaWrapper.h:90
T Max(T a, T b)
Definition: MathUtil.h:44
virtual void DirectDraw(TGLRnrCtx &rnrCtx) const
TEveVector & RefExtent3D()
Definition: TEveEllipsoid.h:39
#define N
Definition: blowfish.cc:9
TEveEllipsoidProjected * fM
double p1[4]
Definition: TauolaWrapper.h:89
double a
Definition: hdecay.h:121