Logo ROOT   6.12/07
Reference Guide
TGLScaleManip.cxx
Go to the documentation of this file.
1 // @(#)root/gl:$Id$
2 // Author: Richard Maunder 16/09/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TGLScaleManip.h"
13 #include "TGLPhysicalShape.h"
14 #include "TGLCamera.h"
15 #include "TGLIncludes.h"
16 
17 /** \class TGLScaleManip
18 \ingroup opengl
19 Scale manipulator - attaches to physical shape and draws local axes
20 widgets with box heads. User can mouse over (turns yellow) and L
21 click/drag to scale along this axis.
22 */
23 
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Construct scale manipulator not bound to any physical shape.
28 
30 {
31 }
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// Construct scale manipulator bound to TGLPhysicalShape 'shape'.
35 
37  TGLManip(shape)
38 {
39 }
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Destroy the scale manipulator
43 
45 {
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Draw scale manipulator - tubes with box heads, in local axes of
50 /// attached shape, in red(X), green(Y) and blue(Z), with white center sphere.
51 /// If selected widget (mouse over) this is drawn in active colour (yellow).
52 
53 void TGLScaleManip::Draw(const TGLCamera & camera) const
54 {
55  if (!fShape) {
56  return;
57  }
58 
59  // Get draw scales
60  const TGLBoundingBox & box = fShape->BoundingBox();
61  Double_t baseScale;
62  TGLVector3 axisScale[3];
63  CalcDrawScale(box, camera, baseScale, axisScale);
64 
65  // Get permitted manipulations on shape
67 
68  glEnable(GL_BLEND);
69  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
70  glDisable(GL_CULL_FACE);
71 
72  // Draw three axis widgets out of bounding box where permitted
73  // Not drawing will prevent interaction
74  // GL name loading for hit testing - 0 reserved for no selection
75  if (manip & TGLPhysicalShape::kScaleX) {
76  glPushName(1);
77  TGLUtil::DrawLine(box.Center(), axisScale[0], TGLUtil::kLineHeadBox,
78  baseScale, ColorFor(1));
79  glPopName();
80  } else {
81  TGLUtil::DrawLine(box.Center(), axisScale[0], TGLUtil::kLineHeadBox,
82  baseScale, TGLUtil::fgGrey);
83  }
84  if (manip & TGLPhysicalShape::kScaleY) {
85  glPushName(2);
86  TGLUtil::DrawLine(box.Center(), axisScale[1], TGLUtil::kLineHeadBox,
87  baseScale, ColorFor(2));
88  glPopName();
89  } else {
90  TGLUtil::DrawLine(box.Center(), axisScale[1], TGLUtil::kLineHeadBox,
91  baseScale, TGLUtil::fgGrey);
92  }
93  if (manip & TGLPhysicalShape::kScaleZ) {
94  glPushName(3);
95  TGLUtil::DrawLine(box.Center(), axisScale[2], TGLUtil::kLineHeadBox,
96  baseScale, ColorFor(3));
97  glPopName();
98  } else {
99  TGLUtil::DrawLine(box.Center(), axisScale[2], TGLUtil::kLineHeadBox,
100  baseScale, TGLUtil::fgGrey);
101  }
102  // Draw white center sphere
103  TGLUtil::DrawSphere(box.Center(), baseScale/2.0, TGLUtil::fgWhite);
104 
105  glEnable(GL_CULL_FACE);
106  glDisable(GL_BLEND);
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Handle mouse button event over manipulator - returns kTRUE if
111 /// redraw required kFALSE otherwise.
112 
114  const TGLCamera & camera)
115 {
116  if (event.fType == kButtonPress && fSelectedWidget != 0) {
118  }
119 
120  return TGLManip::HandleButton(event, camera);
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Handle mouse motion over manipulator - if active (selected
125 /// widget) scale physical along selected widget (axis) of the
126 /// manipulator, so it tracks mouse action. Returns kTRUE if redraw
127 /// required kFALSE otherwise.
128 
130  const TGLCamera & camera)
131 {
132  if (fActive) {
133  // Find mouse delta projected into world at attached object center
135  event.fX - fFirstMouse.GetX(),
136  -event.fY + fFirstMouse.GetY()); // Y inverted
137 
138  UInt_t axisIndex = fSelectedWidget - 1; // Ugg sort out axis / widget id mapping
139  TGLVector3 widgetAxis = fShape->BoundingBox().Axis(axisIndex, kTRUE);
140 
141  // Scale by projected screen factor
142  TGLVector3 screenScale = camera.ViewportDeltaToWorld(fShape->BoundingBox().Center(), 500, 500);
143  Double_t factor = -5.0*Dot(shift, widgetAxis) / screenScale.Mag();
144 
145  TGLVector3 newScale = fStartScale;
146  newScale[axisIndex] += factor;
147  LimitScale(newScale[axisIndex]);
148  fShape->Scale(newScale);
149 
150  fLastMouse.SetX(event.fX);
151  fLastMouse.SetY(event.fY);
152 
153  return kTRUE;
154  }
155  return kFALSE;
156 }
157 
158 ////////////////////////////////////////////////////////////////////////////////
159 /// Clamp scale to sizable values: 1000 - 1/1000
160 /// Guards against div by zero problems.
161 
163 {
164  if (factor < 1e-4) {
165  factor = 1e-4;
166  }
167  if (factor > 1e+4) {
168  factor = 1e+4;
169  }
170 }
Abstract base class for viewer manipulators, which allow direct in viewer manipulation of a (TGlPhysi...
Definition: TGLManip.h:28
void SetX(SCoord_t x)
Definition: TPoint.h:49
TGLVertex3 Center() const
virtual Bool_t HandleButton(const Event_t &event, const TGLCamera &camera)
Handle mouse button event over manipulator - returns kTRUE if redraw required kFALSE otherwise...
TGLScaleManip()
Construct scale manipulator not bound to any physical shape.
Abstract base camera class - concrete classes for orthographic and perspective cameras derive from it...
Definition: TGLCamera.h:43
virtual Bool_t HandleMotion(const Event_t &event, const TGLCamera &camera)
Handle mouse motion over manipulator - if active (selected widget) scale physical along selected widg...
static void DrawLine(const TGLLine3 &line, ELineHeadShape head, Double_t size, const UChar_t rgba[4])
Draw thick line (tube) defined by &#39;line&#39;, with head at end shape &#39;head&#39; - box/arrow/none, (head) size &#39;size&#39;, color &#39;rgba&#39;.
Definition: TGLUtil.cxx:2333
Int_t fY
Definition: GuiTypes.h:177
virtual Bool_t HandleButton(const Event_t &event, const TGLCamera &camera)
Handle a mouse button event - return kTRUE if processed, kFALSE otherwise.
Definition: TGLManip.cxx:118
static const UChar_t fgGrey[4]
Definition: TGLUtil.h:1052
bool Bool_t
Definition: RtypesCore.h:59
SCoord_t GetY() const
Definition: TPoint.h:48
SCoord_t GetX() const
Definition: TPoint.h:47
UInt_t fSelectedWidget
manipulated shape
Definition: TGLManip.h:32
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
void SetY(SCoord_t y)
Definition: TPoint.h:50
virtual void Draw(const TGLCamera &camera) const
Draw scale manipulator - tubes with box heads, in local axes of attached shape, in red(X)...
Scale manipulator - attaches to physical shape and draws local axes widgets with box heads...
Definition: TGLScaleManip.h:28
Concrete physical shape - a GL drawable.
TGLVector3 fStartScale
Definition: TGLScaleManip.h:31
EManip GetManip() const
static void DrawSphere(const TGLVertex3 &position, Double_t radius, const UChar_t rgba[4])
Draw sphere, centered on vertex &#39;position&#39;, with radius &#39;radius&#39;, color &#39;rgba&#39;.
Definition: TGLUtil.cxx:2318
const TGLBoundingBox & BoundingBox() const
TGLVector3 GetScale() const
3 component (x/y/z) vector class.
Definition: TGLUtil.h:246
TPoint fLastMouse
first (start) mouse position (in WINDOW coords)
Definition: TGLManip.h:37
const UChar_t * ColorFor(UInt_t widget) const
Returns color to be used for given widget.
Definition: TGLManip.cxx:97
void LimitScale(Double_t &factor) const
initial scaling factors
TGLVector3 ViewportDeltaToWorld(const TGLVertex3 &worldRef, Double_t viewportXDelta, Double_t viewportYDelta, TGLMatrix *modviewMat=0) const
Apply a 2D viewport delta (shift) to the projection of worldRef onto viewport, returning the resultan...
Definition: TGLCamera.cxx:546
Bool_t fActive
active width (axis) component
Definition: TGLManip.h:33
Double_t Mag() const
Definition: TGLUtil.h:299
EGEventType fType
Definition: GuiTypes.h:174
static const UChar_t fgWhite[4]
Definition: TGLUtil.h:1051
TGLPhysicalShape * fShape
Definition: TGLManip.h:31
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
void Scale(const TGLVector3 &scale)
Double_t Dot(const TGLVector3 &v1, const TGLVector3 &v2)
Definition: TGLUtil.h:318
#define ClassImp(name)
Definition: Rtypes.h:359
double Double_t
Definition: RtypesCore.h:55
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
virtual ~TGLScaleManip()
Destroy the scale manipulator.
Concrete class describing an orientated (free) or axis aligned box of 8 vertices. ...
void CalcDrawScale(const TGLBoundingBox &box, const TGLCamera &camera, Double_t &base, TGLVector3 axis[3]) const
Calculates base and axis scale factor (in world units) for drawing manipulators with reasonable size ...
Definition: TGLManip.cxx:156
const TGLVector3 & Axis(UInt_t i, Bool_t normalised=kTRUE) const
TPoint fFirstMouse
manipulator is active?
Definition: TGLManip.h:36
const Bool_t kTRUE
Definition: RtypesCore.h:87
Int_t fX
Definition: GuiTypes.h:177