-
Notifications
You must be signed in to change notification settings - Fork 24
/
TrackballDecorator.cs
176 lines (140 loc) · 5.85 KB
/
TrackballDecorator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Input;
// IAddChild, ContentPropertyAttribute
namespace Microsoft._3DTools
{
public class TrackballDecorator : Viewport3DDecorator
{
public TrackballDecorator()
{
// the transform that will be applied to the viewport 3d's camera
_transform = new Transform3DGroup();
_transform.Children.Add(_scale);
_transform.Children.Add(new RotateTransform3D(_rotation));
// used so that we always get events while activity occurs within
// the viewport3D
_eventSource = new Border();
_eventSource.Background = Brushes.Transparent;
PreViewportChildren.Add(_eventSource);
}
/// <summary>
/// A transform to move the camera or scene to the trackball's
/// current orientation and scale.
/// </summary>
public Transform3D Transform
{
get { return _transform; }
}
#region Event Handling
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
_previousPosition2D = e.GetPosition(this);
_previousPosition3D = ProjectToTrackball(ActualWidth,
ActualHeight,
_previousPosition2D);
if (Mouse.Captured == null)
{
Mouse.Capture(this, CaptureMode.Element);
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
if (IsMouseCaptured)
{
Mouse.Capture(this, CaptureMode.None);
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (IsMouseCaptured)
{
Point currentPosition = e.GetPosition(this);
// avoid any zero axis conditions
if (currentPosition == _previousPosition2D) return;
// Prefer tracking to zooming if both buttons are pressed.
if (e.LeftButton == MouseButtonState.Pressed)
{
Track(currentPosition);
}
else if (e.RightButton == MouseButtonState.Pressed)
{
Zoom(currentPosition);
}
_previousPosition2D = currentPosition;
Viewport3D viewport3D = this.Viewport3D;
if (viewport3D != null)
{
if (viewport3D.Camera != null)
{
if (viewport3D.Camera.IsFrozen)
{
viewport3D.Camera = viewport3D.Camera.Clone();
}
if (viewport3D.Camera.Transform != _transform)
{
viewport3D.Camera.Transform = _transform;
}
}
}
}
}
#endregion Event Handling
private void Track(Point currentPosition)
{
Vector3D currentPosition3D = ProjectToTrackball(
ActualWidth, ActualHeight, currentPosition);
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D);
double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D);
// quaterion will throw if this happens - sometimes we can get 3D positions that
// are very similar, so we avoid the throw by doing this check and just ignoring
// the event
if (axis.Length == 0) return;
Quaternion delta = new Quaternion(axis, -angle);
// Get the current orientantion from the RotateTransform3D
AxisAngleRotation3D r = _rotation;
Quaternion q = new Quaternion(_rotation.Axis, _rotation.Angle);
// Compose the delta with the previous orientation
q *= delta;
// Write the new orientation back to the Rotation3D
_rotation.Axis = q.Axis;
_rotation.Angle = q.Angle;
_previousPosition3D = currentPosition3D;
}
private Vector3D ProjectToTrackball(double width, double height, Point point)
{
double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2]
double y = point.Y / (height / 2);
x = x - 1; // Translate 0,0 to the center
y = 1 - y; // Flip so +Y is up instead of down
double z2 = 1 - x * x - y * y; // z^2 = 1 - x^2 - y^2
double z = z2 > 0 ? Math.Sqrt(z2) : 0;
return new Vector3D(x, y, z);
}
private void Zoom(Point currentPosition)
{
double yDelta = currentPosition.Y - _previousPosition2D.Y;
double scale = Math.Exp(yDelta / 100); // e^(yDelta/100) is fairly arbitrary.
_scale.ScaleX *= scale;
_scale.ScaleY *= scale;
_scale.ScaleZ *= scale;
}
//--------------------------------------------------------------------
//
// Private data
//
//--------------------------------------------------------------------
private Point _previousPosition2D;
private Vector3D _previousPosition3D = new Vector3D(0, 0, 1);
private Transform3DGroup _transform;
private ScaleTransform3D _scale = new ScaleTransform3D();
private AxisAngleRotation3D _rotation = new AxisAngleRotation3D();
private Border _eventSource;
}
}