/
ManipulatorComponents.cpp
262 lines (199 loc) · 9.48 KB
/
ManipulatorComponents.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "ManipulatorComponents.h"
#include "igrid.h"
#include "math/FloatTools.h"
#include "math/Ray.h"
#include "pivot.h"
namespace selection
{
void transform_local2object(Matrix4& object, const Matrix4& local, const Matrix4& local2object)
{
object = local2object.getMultipliedBy(local).getMultipliedBy(local2object.getFullInverse());
}
void translation_local2object(Vector3& object, const Vector3& local, const Matrix4& local2object)
{
object = local2object.getTranslatedBy(local).getMultipliedBy(local2object.getFullInverse()).translation();
}
Vector3 ManipulatorComponentBase::getPlaneProjectedPoint(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
Matrix4 device2pivot = constructDevice2Pivot(pivot2world, view);
// greebo: We need to know the z-distance (or depth) of the pivot plane in device coordinates.
// x and y are defined by the mouse clicks, but we need the third depth component to pass into the
// device2pivot matrix. Luckily, this value can be extracted from the pivot2device matrix itself,
// because the distance of that plane in device space is stored in the tz matrix component.
// The trick is to invert the device2pivot matrix to get the tz value, to get a complete 4D point
// to transform back into pivot space.
Matrix4 pivot2device = constructPivot2Device(pivot2world, view);
// This is now the complete 4D point to transform back into pivot space
Vector4 point(devicePoint.x(), devicePoint.y(), pivot2device.tz(), 1);
// Running the point through the device2pivot matrix will give us the mouse coordinates relative to pivot origin
return device2pivot.transform(point).getProjected();
}
Vector3 ManipulatorComponentBase::getSphereIntersection(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
// Construct a ray to intersect with a defined sphere
// The device coords x,y generated by the mouse click already defines a ray in 3D space,
// we just need to pick a z coordinate for start and end. For simplicity we use z=-1 and z=+1
// which resolves to the near and far plane in device space.
Vector4 deviceOrigin(devicePoint.x(), devicePoint.y(), -1, 1); // point on near plane
Vector4 deviceEnd(devicePoint.x(), devicePoint.y(), +1, 1); // point on far plane
// Run these points through the device2pivot matrix and construct the ray
Matrix4 device2pivot = constructDevice2Pivot(pivot2world, view);
Vector3 rayOrigin = device2pivot.transform(deviceOrigin).getProjected();
Vector3 rayDirection = (device2pivot.transform(deviceEnd).getProjected() - rayOrigin).getNormalised();
// Construct the ray and return the intersection or the Ray's nearest point to the sphere
Ray ray(rayOrigin, rayDirection);
Vector3 intersectionPoint;
ray.intersectSphere(Vector3(0, 0, 0), 64.0, intersectionPoint);
return intersectionPoint;
}
Vector3 ManipulatorComponentBase::getAxisConstrained(const Vector3& direction, const Vector3& axis)
{
// Subtract anything that points along the axis from the direction vector
// after this step, the direction vector is perpendicular to axis.
return (direction - axis*direction.dot(axis)).getNormalised();
}
Vector3::ElementType ManipulatorComponentBase::getAngleForAxis(const Vector3& a, const Vector3& b, const Vector3& axis)
{
if (axis.dot(a.crossProduct(b)) > 0.0)
{
return a.angle(b);
}
else
{
return -a.angle(b);
}
}
// ===============================================================================================
void RotateFree::beginTransformation(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
_start = getSphereIntersection(pivot2world, view, devicePoint);
_start.normalise();
}
void RotateFree::transform(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
Vector3 current = getSphereIntersection(pivot2world, view, devicePoint);
current.normalise();
// call the Rotatable with its transform method
Quaternion rotation = Quaternion::createForUnitVectors(_start, current);
_rotatable.rotate(rotation);
}
// ===============================================================================================
void RotateAxis::beginTransformation(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
_start = getSphereIntersection(pivot2world, view, devicePoint);
// Constrain the start vector to an axis
_start = getAxisConstrained(_start, _axis);
}
/// \brief Converts current position to a normalised vector orthogonal to axis.
void RotateAxis::transform(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
Vector3 current = getSphereIntersection(pivot2world, view, devicePoint);
// Constrain the start vector to an axis
current = getAxisConstrained(current, _axis);
Vector3::ElementType angle = getAngleForAxis(_start, current, _axis);
_rotatable.rotate(Quaternion::createForAxisAngle(_axis, angle));
}
// ===============================================================================================
void TranslateAxis::beginTransformation(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
_start = getPlaneProjectedPoint(pivot2world, view, devicePoint);
}
void TranslateAxis::transform(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
// Get the regular difference between the starting point and the current mouse point
Vector3 current = getPlaneProjectedPoint(pivot2world, view, devicePoint);
Vector3 diff = current - _start;
// Project this diff vector to our constraining axis
Vector3 axisProjected = _axis * diff.dot(_axis);
// Snap and apply translation
axisProjected.snap(GlobalGrid().getGridSize());
_translatable.translate(axisProjected);
}
// ===============================================================================================
void TranslateFree::beginTransformation(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
// Transform the device coordinates to a point in pivot space
// The point is part of the plane going through pivot space origin, orthogonal to the view direction
_start = getPlaneProjectedPoint(pivot2world, view, devicePoint);
}
void TranslateFree::transform(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
Vector3 current = getPlaneProjectedPoint(pivot2world, view, devicePoint);
Vector3 diff = current - _start;
diff.snap(GlobalGrid().getGridSize());
_translatable.translate(diff);
}
// ===============================================================================================
void ScaleAxis::beginTransformation(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
// Transform the device coordinates to a point in pivot space
// The point is part of the plane going through pivot space origin, orthogonal to the view direction
_start = getPlaneProjectedPoint(pivot2world, view, devicePoint);
}
void ScaleAxis::transform(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
// Get the regular difference between the starting point and the current mouse point
Vector3 current = getPlaneProjectedPoint(pivot2world, view, devicePoint);
Vector3 diff = current - _start;
// Project this diff vector to our constraining axis
Vector3 axisProjected = _axis * diff.dot(_axis);
axisProjected.snap(GlobalGrid().getGridSize());
Vector3 start(_start.getSnapped(GlobalGrid().getGridSize()));
Vector3 scale(
start[0] == 0 ? 1 : 1 + axisProjected[0] / start[0],
start[1] == 0 ? 1 : 1 + axisProjected[1] / start[1],
start[2] == 0 ? 1 : 1 + axisProjected[2] / start[2]
);
_scalable.scale(scale);
}
// ===============================================================================================
void ScaleFree::beginTransformation(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
// Transform the device coordinates to a point in pivot space
// The point is part of the plane going through pivot space origin, orthogonal to the view direction
_start = getPlaneProjectedPoint(pivot2world, view, devicePoint);
}
void ScaleFree::transform(const Matrix4& pivot2world, const VolumeTest& view, const Vector2& devicePoint)
{
Vector3 current = getPlaneProjectedPoint(pivot2world, view, devicePoint);
Vector3 diff = current - _start;
diff.snap(GlobalGrid().getGridSize());
Vector3 start(_start.getSnapped(GlobalGrid().getGridSize()));
Vector3 scale(
start[0] == 0 ? 1 : 1 + diff[0] / start[0],
start[1] == 0 ? 1 : 1 + diff[1] / start[1],
start[2] == 0 ? 1 : 1 + diff[2] / start[2]
);
_scalable.scale(scale);
}
SelectionTranslator::SelectionTranslator(const TranslationCallback& onTranslation) :
_onTranslation(onTranslation)
{}
void SelectionTranslator::translate(const Vector3& translation)
{
if (GlobalSelectionSystem().Mode() == SelectionSystem::eComponent)
{
GlobalSelectionSystem().foreachSelectedComponent(TranslateComponentSelected(translation));
}
else
{
// Cycle through the selected items and apply the translation
GlobalSelectionSystem().foreachSelected(TranslateSelected(translation));
}
// Invoke the feedback function
if (_onTranslation)
{
_onTranslation(translation);
}
}
TranslatablePivot::TranslatablePivot(ManipulationPivot& pivot) :
_pivot(pivot)
{}
void TranslatablePivot::translate(const Vector3& translation)
{
Vector3 translationSnapped = translation.getSnapped(GlobalGrid().getGridSize());
_pivot.applyTranslation(translationSnapped);
// User is placing the pivot manually, so let's keep it that way
_pivot.setUserLocked(true);
}
}