Skip to content

Commit a8e8d9f

Browse files
author
monkstone
committed
refactor arcball
1 parent d4e1066 commit a8e8d9f

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

3DSketches/test_arcball.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
ArcBall class uses Quaternions class for efficient calculation of rotation, hold down x, y or z
88
keys to constrain rotation to that plane otherwise drag mouse for smooth rotation
99
"""
10+
X = 0
11+
Y= 1
12+
Z = 2
1013

1114
def setup():
1215
size(600, 600)
@@ -18,11 +21,17 @@ def draw():
1821
background(0xff66c0ff)
1922
translate(width/2.0, height/2.0, -height/4.0)
2023
defineLights()
21-
w, x, y, z = arcball.update()
22-
rotate(w, x, y, z)
24+
update()
2325
lights()
2426
stroke(0)
2527
cube(arcball.radius)
28+
29+
def update():
30+
"""
31+
wrap arcball update and rotation as a local function
32+
"""
33+
theta, x, y, z = arcball.update()
34+
rotate(theta, x, y, z)
2635

2736
def mousePressed():
2837
arcball.mousePressed(mouse.x, mouse.y)
@@ -46,13 +55,16 @@ def keyPressed():
4655
of rotation by holding down key corresponding to axis
4756
"""
4857
if (key.char == 'x'):
49-
arcball.selectAxis(0)
58+
arcball.selectAxis(X)
5059
if (key.char == 'y'):
51-
arcball.selectAxis(1)
60+
arcball.selectAxis(Y)
5261
if (key.char == 'z'):
53-
arcball.selectAxis(2)
62+
arcball.selectAxis(Z)
5463

5564
def keyReleased():
65+
"""
66+
Release axis constraint
67+
"""
5668
arcball.selectAxis(-1)
5769

5870
def cube(sz):

3DSketches/util/arcball.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ def __init__(self, cx, cy, radius):
2323

2424
def selectAxis(self, axis):
2525
"""
26-
call this from sketch (typically keyPressed() to constrain rotation to one axis)
26+
call this from sketch (typically in keyPressed() to constrain rotation to one axis)
27+
valid input 0, 1, 2 or -1
2728
"""
2829
self.axis = axis
2930

30-
def mouseToSphere(self, x, y):
31+
def __mouse2sphere(self, x, y):
3132
"""
32-
Map mouse to ArcBall (sphere)
33+
private map mouse to ArcBall (sphere)
3334
"""
3435
v = PVector()
3536
v.x = (x - self.center_x) / self.radius
@@ -39,24 +40,27 @@ def mouseToSphere(self, x, y):
3940
v.normalize()
4041
else:
4142
v.z = sqrt(1.0 - mag)
42-
return v if (self.axis == -1) else (self.constrainVector(v, self.axisSet[self.axis]))
43+
return v if (self.axis == -1) else (self.__constrain(v, self.axisSet[self.axis]))
4344

4445
def mousePressed(self, x, y):
4546
"""
4647
pass in mouse.x and mouse.y parameters from sketch
4748
"""
48-
self.v_down = self.mouseToSphere(x, y)
49+
self.v_down = self.__mouse2sphere(x, y)
4950
self.q_down.copy(self.q_now)
5051
self.q_drag.reset()
5152

5253
def mouseDragged(self, x, y):
5354
"""
5455
pass in mouse.x and mouse.y parameters from sketch
5556
"""
56-
self.v_drag = self.mouseToSphere(x, y)
57+
self.v_drag = self.__mouse2sphere(x, y)
5758
self.q_drag.set(PVector.dot(self.v_down, self.v_drag), self.v_down.cross(self.v_drag))
5859

59-
def constrainVector(self, vector, axis):
60+
def __constrain(self, vector, axis):
61+
"""
62+
private constrain (used to constrain axis)
63+
"""
6064
res = PVector.sub(vector, PVector.mult(axis, PVector.dot(axis, vector)))
6165
res.normalize()
6266
return res
@@ -66,11 +70,11 @@ def update(self):
6670
Call this function in the sketch draw loop to get rotation matrix as an array
6771
"""
6872
self.q_now = Quaternion.mult(self.q_drag, self.q_down)
69-
return self.quat2Matrix(self.q_now)
73+
return self.__quat2matrix(self.q_now)
7074

71-
def quat2Matrix(self, q) :
75+
def __quat2matrix(self, q) :
7276
"""
73-
Return matrix as array
77+
private return matrix as array
7478
"""
7579
rot = q.getValue()
7680
return rot

0 commit comments

Comments
 (0)