CMS 3D CMS Logo

CmsAnnotation.cc
Go to the documentation of this file.
1 #include <sstream>
2 
3 #include "TGLIncludes.h"
4 #include "TGLCamera.h"
5 #include "TGLRnrCtx.h"
6 #include "TGLSelectRecord.h"
7 #include "TGLViewerBase.h"
8 #include "TGLViewer.h"
9 #include "TImage.h"
10 #include "TEveManager.h"
11 
15 
16 CmsAnnotation::CmsAnnotation(TGLViewerBase *parent, Float_t posx, Float_t posy) :
17  TGLOverlayElement(TGLOverlayElement::kUser),
18  fPosX(posx), fPosY(posy),
19  fMouseX(0), fMouseY(0),
20  fDrag(kNone),
21  fParent(nullptr),
22  fSize(0.2),
23  fSizeDrag(0.0),
24  fActive(false),
25  fAllowDestroy(false)
26 {
27  // Constructor.
28  // Create annotation as plain text
29 
30  parent->AddOverlayElement(this);
31  fParent = (TGLViewer*)parent;
32 }
33 
34 
36 {
37  // Destructor.
38 
39  fParent->RemoveOverlayElement(this);
40 }
41 
42 
43 void
44 CmsAnnotation::Render(TGLRnrCtx& rnrCtx)
45 {
46  if (rnrCtx.GetCamera()->RefViewport().Width() == 0 ||
47  rnrCtx.GetCamera()->RefViewport().Height() == 0 ) return;
48 
49  static UInt_t ttid_black = 0;
50  static UInt_t ttid_white = 0;
51 
52  bool whiteBg = rnrCtx.ColorSet().Background().GetColorIndex() == kWhite;
53  UInt_t& ttid = whiteBg ? ttid_white : ttid_black;
54 
55  if ( (whiteBg == false && ttid == 0) || (whiteBg && ttid == 0))
56  {
57  TImage* imgs[3];
58  TString base = whiteBg ? "White" : "Black";
59  imgs[0] = TImage::Open(FWCheckBoxIcon::coreIcondir()+"CMSLogo" + base + "Bg.png");
60  imgs[1] = TImage::Open(FWCheckBoxIcon::coreIcondir()+"CMSLogo" + base + "BgM.png");
61  imgs[2] = TImage::Open(FWCheckBoxIcon::coreIcondir()+"CMSLogo" + base + "BgS.png");
62 
63  glGenTextures(1, &ttid);
64  glBindTexture(GL_TEXTURE_2D, ttid);
65 
66  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
67  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
68  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
69  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
70  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL,0);
71  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
72 
73 
74  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
75  glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
76 
77  for (int i=0; i < 3; i++)
78  glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA, imgs[i]->GetWidth(), imgs[i]->GetHeight(), 0,
79  GL_BGRA, GL_UNSIGNED_BYTE, imgs[i]->GetArgbArray());
80 
81  glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
82 
83  for (int i=0; i < 3; i++)
84  delete imgs[i];
85  }
86 
87  glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POLYGON_BIT );
88  TGLCapabilitySwitch lights_off(GL_LIGHTING, kFALSE);
89 
90 
91  // reset matrix
92  glMatrixMode(GL_PROJECTION);
93  glPushMatrix();
94  glLoadIdentity();
95 
96  if (rnrCtx.Selection())
97  {
98  TGLRect rect(*rnrCtx.GetPickRectangle());
99  rnrCtx.GetCamera()->WindowToViewport(rect);
100  gluPickMatrix(rect.X(), rect.Y(), rect.Width(), rect.Height(),
101  (Int_t*) rnrCtx.GetCamera()->RefViewport().CArr());
102  }
103  const TGLRect& vp = rnrCtx.RefCamera().RefViewport();
104  glOrtho(vp.X(), vp.Width(), vp.Y(), vp.Height(), 0, 1);
105  glMatrixMode(GL_MODELVIEW);
106  glPushMatrix();
107  glLoadIdentity();
108 
109  // move to pos
110  Float_t posX = vp.Width() * fPosX;
111  Float_t posY = vp.Height() * fPosY;
112  glTranslatef(posX, posY, -0.99);
113 
114  glDisable(GL_DEPTH_TEST);
115  glEnable(GL_BLEND);
116  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
117 
118  glEnable(GL_TEXTURE_2D);
119  glBindTexture(GL_TEXTURE_2D, ttid);
120 
121  glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
122 
123  // logo
124  glPushName(kMove);
125  TGLUtil::Color(rnrCtx.ColorSet().Background().GetColorIndex());
126  glBegin(GL_QUADS);
127  Float_t z = 0.9;
128  Float_t a = fSize * vp.Height();
129  glTexCoord2f(0, 1); glVertex3f( 0, -a, z);
130  glTexCoord2f(1, 1); glVertex3f( a, -a, z);
131  glTexCoord2f(1, 0); glVertex3f( a, 0, z);
132  glTexCoord2f(0, 0); glVertex3f( 0, 0, z);
133  glEnd();
134  glPopName();
135 
136 
137  glDisable(GL_TEXTURE_2D);
138 
139  if (fActive)
140  {
141  // resize button
142  glPushMatrix();
143  glBegin(GL_QUADS);
144  Float_t a = fSize * vp.Height();
145  TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 95);
146  glTexCoord2f(0, 1); glVertex3f( 0, -a, z);
147  glTexCoord2f(1, 1); glVertex3f( a, -a, z);
148  glTexCoord2f(1, 0); glVertex3f( a, 0, z);
149  glTexCoord2f(0, 0); glVertex3f( 0, 0, z);
150  glEnd();
151 
152  glTranslatef(a, -a, 0);
153  a *= 0.2;
154  z = 0.95;
155  glPushName(kResize);
156  TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 100);
157  glBegin(GL_QUADS);
158  glVertex3f( 0, 0, z);
159  glVertex3f( 0, a, z);
160  glVertex3f(-a, a, z);
161  glVertex3f(-a, 0, z);
162  glEnd();
163  {
164  glTranslatef(-a/3, a/3, 0);
165  glBegin(GL_LINES);
166  TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 40);
167  Float_t s = a / 3;
168  glVertex3f( 0, 0, z); glVertex3f( 0, s, z);
169  glVertex3f( 0, 0, z); glVertex3f( -s, 0, z);
170  glEnd();
171  }
172  glPopName();
173  glPopMatrix();
174 
175  // delete
176  if (fAllowDestroy)
177  {
178  glPushName(7);
179  TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 100);
180  glTranslatef(0, -a, 0);
181  glBegin(GL_QUADS);
182  glVertex3f( 0, 0, z);
183  glVertex3f( a, 0, z);
184  glVertex3f( a, a, z);
185  glVertex3f( 0, a, z);
186  glEnd();
187  {
188  glBegin(GL_LINES);
189  TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 40);
190  Float_t s = a/3;
191  glVertex3f( s, s, z);
192  glVertex3f( a-s, a-s, z);
193  glVertex3f( s, a-s, z);
194  glVertex3f( a-s, s, z);
195  glEnd();
196  }
197  glPopName();
198  }
199  }
200 
201  glEnable(GL_DEPTH_TEST);
202  glMatrixMode(GL_PROJECTION);
203  glPopMatrix();
204  glMatrixMode(GL_MODELVIEW);
205  glPopMatrix();
206 
207  glPopAttrib();
208 }
209 
210 
211 Bool_t CmsAnnotation::Handle(TGLRnrCtx& rnrCtx,
212  TGLOvlSelectRecord& selRec,
213  Event_t* event)
214 {
215  // Handle overlay event.
216  // Return TRUE if event was handled.
217 
218  if (selRec.GetN() < 2) return kFALSE;
219  int recID = selRec.GetItem(1);
220 
221  switch (event->fType)
222  {
223  case kButtonPress:
224  {
225  fMouseX = event->fX;
226  fMouseY = event->fY;
227  fDrag = (recID == kResize) ? kResize : kMove;
228  fSizeDrag = fSize;
229  return kTRUE;
230  }
231  case kButtonRelease:
232  {
233  fDrag = kNone;
234  if (recID == 7)
235  {
236  fParent->RequestDraw(rnrCtx.ViewerLOD());
237  delete this;
238  return kTRUE;
239  }
240  break;
241  }
242  case kMotionNotify:
243  {
244  const TGLRect& vp = rnrCtx.RefCamera().RefViewport();
245  if (vp.Width() == 0 || vp.Height() == 0) return false;
246  if (fDrag != kNone)
247  {
248  if (fDrag == kMove)
249  {
250  fPosX += (Float_t)(event->fX - fMouseX) / vp.Width();
251  fPosY -= (Float_t)(event->fY - fMouseY) / vp.Height();
252  fMouseX = event->fX;
253  fMouseY = event->fY;
254 
255  Float_t h = fSize;
256  Float_t w = fSize/vp.Aspect();
257 
258  // Make sure we don't go offscreen (use fDraw variables set in draw)
259  if (fPosX < 0)
260  fPosX = 0;
261  else if (fPosX +w > 1.0f)
262  fPosX = 1.0f - w;
263  if (fPosY < h)
264  fPosY = h;
265  else if (fPosY > 1.0f)
266  fPosY = 1.0f;
267  }
268  else
269  {
270  using namespace TMath;
271  Float_t oovpw = 1.0f / vp.Width(), oovph = 1.0f / vp.Height();
272 
273  Float_t xw = oovpw * Min(Max(0, event->fX), vp.Width());
274  Float_t yw = oovph * Min(Max(0, vp.Height() - event->fY), vp.Height());
275 
276  Float_t rx = Max((xw - fPosX) / (oovpw * fMouseX - fPosX), 0.0f);
277  Float_t ry = Max((yw - fPosY) / (oovph*(vp.Height() - fMouseY) - fPosY), 0.0f);
278 
279  fSize = Max(fSizeDrag * Min(rx, ry), 0.01f);
280  }
281  }
282  return kTRUE;
283  }
284  default:
285  {
286  return kFALSE;
287  }
288  }
289 
290  return false;
291 }
292 
293 //______________________________________________________________________________
294 Bool_t CmsAnnotation::MouseEnter(TGLOvlSelectRecord& /*rec*/)
295 {
296  // Mouse has entered overlay area.
297 
298  fActive = kTRUE;
299  return kTRUE;
300 }
301 
302 //______________________________________________________________________
304 {
305  // Mouse has left overlay area.
306 
307  fActive = kFALSE;
308 }
309 
310 
311 //______________________________________________________________________
313 {
314  return GetState() == TGLOverlayElement::kActive;
315 }
316 
317 //______________________________________________________________________
319 {
320  SetState(x ? (TGLOverlayElement::kActive) : (TGLOverlayElement::kInvisible));
321  fParent->Changed();
322  gEve->Redraw3D();
323 }
324 
325 //______________________________________________________________________________
326 
327 void
329 {
330  std::stringstream s;
331  s<<fSize;
332  iTo.addKeyValue("LogoSize",FWConfiguration(s.str()));
333 
334  std::stringstream x;
335  x<<fPosX;
336  iTo.addKeyValue("LogoPosX",FWConfiguration(x.str()));
337 
338  std::stringstream y;
339  y<<fPosY;
340  iTo.addKeyValue("LogoPosY",FWConfiguration(y.str()));
341 }
342 
343 void
345 {
346  const FWConfiguration* value;
347  value = iFrom.valueForKey("LogoSize");
348  if (value) fSize = atof(value->value().c_str());
349 
350  value = iFrom.valueForKey("LogoPosX");
351  if (value) fPosX = atof(value->value().c_str());
352 
353  value = iFrom.valueForKey("LogoPosY");
354  if (value) fPosY = atof(value->value().c_str());
355 
356 }
static const TString & coreIcondir()
const double w
Definition: UKUtility.cc:23
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
bool getVisible() const
virtual void setFrom(const FWConfiguration &)
T Min(T a, T b)
Definition: MathUtil.h:39
#define nullptr
Bool_t MouseEnter(TGLOvlSelectRecord &selRec) override
void Render(TGLRnrCtx &rnrCtx) override
double f[11][100]
TGLViewer * fParent
Definition: CmsAnnotation.h:49
~CmsAnnotation() override
base
Make Sure CMSSW is Setup ##.
virtual void addTo(FWConfiguration &) const
CmsAnnotation(TGLViewerBase *parent, Float_t posx, Float_t posy)
EDrag fDrag
last mouse position
Definition: CmsAnnotation.h:47
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
T Max(T a, T b)
Definition: MathUtil.h:44
const std::string & value(unsigned int iIndex=0) const
Bool_t Handle(TGLRnrCtx &rnrCtx, TGLOvlSelectRecord &selRec, Event_t *event) override
void MouseLeave() override
double a
Definition: hdecay.h:121
const FWConfiguration * valueForKey(const std::string &iKey) const
void setVisible(bool x)
Definition: event.py:1
Float_t fSizeDrag
relative size to viewport width
Definition: CmsAnnotation.h:52