ROOT  6.06/09
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:36
void SetX(SCoord_t x)
Definition: TPoint.h:51
const UChar_t * ColorFor(UInt_t widget) const
Returns color to be used for given widget.
Definition: TGLManip.cxx:97
virtual Bool_t HandleButton(const Event_t &event, const TGLCamera &camera)
Handle mouse button event over manipulator - returns kTRUE if redraw required kFALSE otherwise...
Abstract base camera class - concrete classes for orthographic and perspective cameras derive from it...
Definition: TGLCamera.h:43
void LimitScale(Double_t &factor) const
initial scaling factors
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 'line', with head at end shape 'head' - box/arrow/none, (head) size 'size', color 'rgba'.
Definition: TGLUtil.cxx:2333
const TGLBoundingBox & BoundingBox() const
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
Int_t fY
Definition: GuiTypes.h:179
Double_t Mag() const
Definition: TGLUtil.h:303
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
TGLVertex3 Center() const
static const UChar_t fgGrey[4]
Definition: TGLUtil.h:1056
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
UInt_t fSelectedWidget
manipulated shape
Definition: TGLManip.h:40
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:52
Scale manipulator - attaches to physical shape and draws local axes widgets with box heads...
Definition: TGLScaleManip.h:30
Concrete physical shape - a GL drawable.
TGLVector3 fStartScale
Definition: TGLScaleManip.h:33
static void DrawSphere(const TGLVertex3 &position, Double_t radius, const UChar_t rgba[4])
Draw sphere, centered on vertex 'position', with radius 'radius', color 'rgba'.
Definition: TGLUtil.cxx:2318
3 component (x/y/z) vector class.
Definition: TGLUtil.h:250
TPoint fLastMouse
first (start) mouse position (in WINDOW coords)
Definition: TGLManip.h:45
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
SCoord_t GetY() const
Definition: TPoint.h:50
Bool_t fActive
active width (axis) component
Definition: TGLManip.h:41
TGLVector3 GetScale() const
EGEventType fType
Definition: GuiTypes.h:176
static const UChar_t fgWhite[4]
Definition: TGLUtil.h:1055
EManip GetManip() const
TGLPhysicalShape * fShape
Definition: TGLManip.h:39
unsigned int UInt_t
Definition: RtypesCore.h:42
void Scale(const TGLVector3 &scale)
Double_t Dot(const TGLVector3 &v1, const TGLVector3 &v2)
Definition: TGLUtil.h:322
double Double_t
Definition: RtypesCore.h:55
virtual ~TGLScaleManip()
Destroy the scale manipulator.
const TGLVector3 & Axis(UInt_t i, Bool_t normalised=kTRUE) const
SCoord_t GetX() const
Definition: TPoint.h:49
Concrete class describing an orientated (free) or axis aligned box of 8 vertices. ...
virtual void Draw(const TGLCamera &camera) const
Draw scale manipulator - tubes with box heads, in local axes of attached shape, in red(X)...
TPoint fFirstMouse
manipulator is active?
Definition: TGLManip.h:44
const Bool_t kTRUE
Definition: Rtypes.h:91
ClassImp(TGLScaleManip) TGLScaleManip
Construct scale manipulator not bound to any physical shape.
Int_t fX
Definition: GuiTypes.h:179