Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TArcBall.cxx
Go to the documentation of this file.
1// @(#)root/gl:$Id$
2// Author: Timur Pocheptsov 03/08/2004
3
4/*************************************************************************
5 * Copyright (C) 1995-2004, 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 "TArcBall.h"
13#include "TPoint.h"
14#include "TMath.h"
15
16const Double_t Epsilon = 1.0e-5;
17
18/** \class TArcBall
19\ingroup opengl
20Implements the arc-ball rotation manipulator. Used by plot-painters.
21
22Arcball sphere constants:
23 - Diameter is 2.0f
24 - Radius is 1.0f
25*/
26
27
28
29
30////////////////////////////////////////////////////////////////////////////////
31
32inline void Vector3dCross(Double_t *NewObj, const Double_t * v1, const Double_t *v2)
33{
34 NewObj[0] = v1[1] * v2[2] - v1[2] * v2[1];
35 NewObj[1] = v1[2] * v2[0] - v1[0] * v2[2];
36 NewObj[2] = v1[0] * v2[1] - v1[1] * v2[0];
37}
38
39////////////////////////////////////////////////////////////////////////////////
40
42{
43 return NewObj[0] * v1[0] + NewObj[1] * v1[1] + NewObj[2] * v1[2];
44}
45
46////////////////////////////////////////////////////////////////////////////////
47
49{
50 return NewObj[0] * NewObj[0] + NewObj[1] * NewObj[1] + NewObj[2] * NewObj[2];
51}
52
53////////////////////////////////////////////////////////////////////////////////
54
59
60////////////////////////////////////////////////////////////////////////////////
61
63{
64 for (Int_t i = 0; i < 9; ++i)
65 NewObj[i] = 0.;
66}
67
68////////////////////////////////////////////////////////////////////////////////
69
71{
73 //then set diagonal as 1
74 NewObj[0] = NewObj[4] = NewObj[8] = 1.;
75}
76
77////////////////////////////////////////////////////////////////////////////////
78
80{
81 Double_t n = (q1[0] * q1[0]) + (q1[1] * q1[1]) + (q1[2] * q1[2]) + (q1[3] * q1[3]);
82 Double_t s = (n > 0.0f) ? (2.0f / n) : 0.0f;
83 Double_t xs = q1[0] * s, ys = q1[1] * s, zs = q1[2] * s;
84 Double_t wx = q1[3] * xs, wy = q1[3] * ys, wz = q1[3] * zs;
85 Double_t xx = q1[0] * xs, xy = q1[0] * ys, xz = q1[0] * zs;
86 Double_t yy = q1[1] * ys, yz = q1[1] * zs, zz = q1[2] * zs;
87
88 NewObj[0] = 1.0f - (yy + zz); NewObj[3] = xy - wz; NewObj[6] = xz + wy;
89 NewObj[1] = xy + wz; NewObj[4] = 1.0f - (xx + zz); NewObj[7] = yz - wx;
90 NewObj[2] = xz - wy; NewObj[5] = yz + wx; NewObj[8] = 1.0f - (xx + yy);
91}
92
93////////////////////////////////////////////////////////////////////////////////
94
96{
98
99 result[0] = (NewObj[0] * m1[0]) + (NewObj[3] * m1[1]) + (NewObj[6] * m1[2]);
100 result[3] = (NewObj[0] * m1[3]) + (NewObj[3] * m1[4]) + (NewObj[6] * m1[5]);
101 result[6] = (NewObj[0] * m1[6]) + (NewObj[3] * m1[7]) + (NewObj[6] * m1[8]);
102
103 result[1] = (NewObj[1] * m1[0]) + (NewObj[4] * m1[1]) + (NewObj[7] * m1[2]);
104 result[4] = (NewObj[1] * m1[3]) + (NewObj[4] * m1[4]) + (NewObj[7] * m1[5]);
105 result[7] = (NewObj[1] * m1[6]) + (NewObj[4] * m1[7]) + (NewObj[7] * m1[8]);
106
107 result[2] = (NewObj[2] * m1[0]) + (NewObj[5] * m1[1]) + (NewObj[8] * m1[2]);
108 result[5] = (NewObj[2] * m1[3]) + (NewObj[5] * m1[4]) + (NewObj[8] * m1[5]);
109 result[8] = (NewObj[2] * m1[6]) + (NewObj[5] * m1[7]) + (NewObj[8] * m1[8]);
110
111 for (Int_t i = 0; i < 9; ++i)
112 NewObj[i] = result[i];
113}
114
115////////////////////////////////////////////////////////////////////////////////
116
118{
119 NewObj[0] = m1[0]; NewObj[4] = m1[4]; NewObj[8] = m1[8];
120 NewObj[1] = m1[1]; NewObj[5] = m1[5]; NewObj[9] = m1[9];
121 NewObj[2] = m1[2]; NewObj[6] = m1[6]; NewObj[10] = m1[10];
122}
123
124////////////////////////////////////////////////////////////////////////////////
125
127{
129 ( (NewObj[0] * NewObj[0]) + (NewObj[1] * NewObj[1]) + (NewObj[2] * NewObj[2]) +
130 (NewObj[4] * NewObj[4]) + (NewObj[5] * NewObj[5]) + (NewObj[6] * NewObj[6]) +
131 (NewObj[8] * NewObj[8]) + (NewObj[9] * NewObj[9]) + (NewObj[10] * NewObj[10]) ) / 3.0f );
132
133 if (rot3) {
134 rot3[0] = NewObj[0]; rot3[1] = NewObj[1]; rot3[2] = NewObj[2];
135 rot3[3] = NewObj[4]; rot3[4] = NewObj[5]; rot3[5] = NewObj[6];
136 rot3[6] = NewObj[8]; rot3[7] = NewObj[9]; rot3[8] = NewObj[10];
137
138 Double_t n = 1. / TMath::Sqrt(NewObj[0] * NewObj[0] + NewObj[1] * NewObj[1] + NewObj[2] * NewObj[2] + 0.0001);
139
140 rot3[0] *= n;
141 rot3[1] *= n;
142 rot3[2] *= n;
143
144 n = 1. / TMath::Sqrt(NewObj[4] * NewObj[4] + NewObj[5] * NewObj[5] + NewObj[6] * NewObj[6] + 0.0001);
145 rot3[3] *= n;
146 rot3[4] *= n;
147 rot3[5] *= n;
148
149 n = 1.0f / TMath::Sqrt(NewObj[8] * NewObj[8] + NewObj[9] * NewObj[9] + NewObj[10] * NewObj[10] + 0.0001);
150 rot3[6] *= n;
151 rot3[7] *= n;
152 rot3[8] *= n;
153 }
154
155 if (rot4) {
156 if (rot4 != NewObj)
158
159 Double_t n = 1. / TMath::Sqrt(NewObj[0] * NewObj[0] + NewObj[1] * NewObj[1] + NewObj[2] * NewObj[2] + 0.0001);
160
161 rot4[0] *= n;
162 rot4[1] *= n;
163 rot4[2] *= n;
164
165 n = 1. / TMath::Sqrt(NewObj[4] * NewObj[4] + NewObj[5] * NewObj[5] + NewObj[6] * NewObj[6] + 0.0001);
166 rot4[4] *= n;
167 rot4[5] *= n;
168 rot4[6] *= n;
169
170 n = 1. / TMath::Sqrt(NewObj[8] * NewObj[8] + NewObj[9] * NewObj[9] + NewObj[10] * NewObj[10] + 0.0001);
171 rot4[8] *= n;
172 rot4[9] *= n;
173 rot4[10] *= n;
174 }
175
176 return s;
177}
178
179////////////////////////////////////////////////////////////////////////////////
180
182{
183 NewObj[0] = m1[0]; NewObj[4] = m1[3]; NewObj[8] = m1[6];
184 NewObj[1] = m1[1]; NewObj[5] = m1[4]; NewObj[9] = m1[7];
185 NewObj[2] = m1[2]; NewObj[6] = m1[5]; NewObj[10] = m1[8];
186}
187
188////////////////////////////////////////////////////////////////////////////////
189
191{
192 NewObj[0] *= scale; NewObj[4] *= scale; NewObj[8] *= scale;
193 NewObj[1] *= scale; NewObj[5] *= scale; NewObj[9] *= scale;
194 NewObj[2] *= scale; NewObj[6] *= scale; NewObj[10] *= scale;
195}
196
197////////////////////////////////////////////////////////////////////////////////
198
205
206////////////////////////////////////////////////////////////////////////////////
207///map to sphere
208
210{
211 Double_t tempPt[] = { static_cast<Double_t>(NewPt.fX), static_cast<Double_t>(NewPt.fY)};
212 //Adjust point coords and scale down to range of [-1 ... 1]
213 tempPt[0] = tempPt[0] * fAdjustWidth - 1.;
214 tempPt[1] = 1. - tempPt[1] * fAdjustHeight;
215 //Compute the square of the length of the vector to the point from the center
216 Double_t length = tempPt[0] * tempPt[0] + tempPt[1] * tempPt[1];
217 //If the point is mapped outside of the sphere... (length > radius squared)
218 if (length > 1.) {
220 //Return the "normalized" vector, a point on the sphere
221 NewVec[0] = tempPt[0] * norm;
222 NewVec[1] = tempPt[1] * norm;
223 NewVec[2] = 0.;
224 } else { //Else it's on the inside
225 //Return a vector to a point mapped inside the sphere sqrt(radius squared - length)
226 NewVec[0] = tempPt[0];
227 NewVec[1] = tempPt[1];
228 NewVec[2] = TMath::Sqrt(1. - length);
229 }
230}
231
232////////////////////////////////////////////////////////////////////////////////
233/// constructor
234
236 :fThisRot(), fLastRot(),
237 fTransform(), fStVec(),
238 fEnVec(), fAdjustWidth(0.),
239 fAdjustHeight(0.)
240{
241 SetBounds(Width, Height);
243}
244
245////////////////////////////////////////////////////////////////////////////////
246///Mouse down
247
249{
251
252 for (Int_t i = 0; i < 9; ++i)
253 fLastRot[i] = fThisRot[i];
254}
255
256////////////////////////////////////////////////////////////////////////////////
257///Mouse drag, calculate rotation
258
260{
262 //Return the quaternion equivalent to the rotation
263 Double_t newRot[4] = {0.};
264 Double_t perp[3] = {0.};
265
267 //Compute the length of the perpendicular vector
268 if (Vector3dLength(perp) > Epsilon) {
269 //We're ok, so return the perpendicular vector as the transform after all
270 newRot[0] = perp[0];
271 newRot[1] = perp[1];
272 newRot[2] = perp[2];
273 //In the quaternion values, w is cosine (theta / 2), where theta is rotation angle
275 } else //if it's zero
276 newRot[0] = newRot[1] = newRot[2] = newRot[3] = 0.;
277
281}
282
283////////////////////////////////////////////////////////////////////////////////
284///Set rotation matrix as union
285
287{
288 fTransform[0] = 1.f, fTransform[1] = fTransform[2] = fTransform[3] =
289 fTransform[4] = 0.f, fTransform[5] = 1.f, fTransform[6] = fTransform[7] =
290 fTransform[8] = fTransform[9] = 0.f, fTransform[10] = 1.f, fTransform[11] =
291 fTransform[12] = fTransform[13] = fTransform[14] = 0.f, fTransform[15] = 1.f;
294}
295
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
Double_t Matrix4fSVD(const Double_t *NewObj, Double_t *rot3, Double_t *rot4)
Definition TArcBall.cxx:126
void Matrix3dSetIdentity(Double_t *NewObj)
Definition TArcBall.cxx:70
void Matrix4dSetRotationScaleFromMatrix4d(Double_t *NewObj, const Double_t *m1)
Definition TArcBall.cxx:117
void Matrix4dMulRotationScale(Double_t *NewObj, Double_t scale)
Definition TArcBall.cxx:190
Double_t Vector3dDot(const Double_t *NewObj, const Double_t *v1)
Definition TArcBall.cxx:41
void Matrix3dSetZero(Double_t *NewObj)
Definition TArcBall.cxx:62
void Matrix3dMulMatrix3d(Double_t *NewObj, const Double_t *m1)
Definition TArcBall.cxx:95
void Matrix4dSetRotationFromMatrix3d(Double_t *NewObj, const Double_t *m1)
Definition TArcBall.cxx:199
void Matrix4dSetRotationScaleFromMatrix3d(Double_t *NewObj, const Double_t *m1)
Definition TArcBall.cxx:181
void Matrix3dSetRotationFromQuat4d(Double_t *NewObj, const Double_t *q1)
Definition TArcBall.cxx:79
Double_t Vector3dLengthSquared(const Double_t *NewObj)
Definition TArcBall.cxx:48
const Double_t Epsilon
Definition TArcBall.cxx:16
void Vector3dCross(Double_t *NewObj, const Double_t *v1, const Double_t *v2)
Definition TArcBall.cxx:32
Double_t Vector3dLength(const Double_t *NewObj)
Definition TArcBall.cxx:55
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Option_t Option_t TPoint xy
Double_t fStVec[3]
Definition TArcBall.h:24
Double_t fTransform[16]
Definition TArcBall.h:23
void ResetMatrices()
Set rotation matrix as union.
Definition TArcBall.cxx:286
void MapToSphere(const TPoint &NewPt, Double_t *NewVec) const
map to sphere
Definition TArcBall.cxx:209
void Drag(const TPoint &NewPt)
Mouse drag, calculate rotation.
Definition TArcBall.cxx:259
Double_t fEnVec[3]
Definition TArcBall.h:25
Double_t fThisRot[9]
Definition TArcBall.h:21
Double_t fLastRot[9]
Definition TArcBall.h:22
Double_t fAdjustWidth
Definition TArcBall.h:26
void SetBounds(UInt_t NewWidth, UInt_t NewHeight)
Definition TArcBall.h:38
void Click(const TPoint &NewPt)
Mouse down.
Definition TArcBall.cxx:248
Double_t fAdjustHeight
Definition TArcBall.h:27
TArcBall(const TArcBall &)
const Int_t n
Definition legend1.C:16
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:673