-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manipulator.cpp
244 lines (185 loc) · 6.49 KB
/
Manipulator.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
#include "Manipulator.h"
Manipulator::Manipulator(Camera& camera)
:_distance(1.0f),
_trackball_size(0.8f),
_min_distance(0.05),
_acc_totalTime(150.0f) {
/*osg::Vec3d eye(camera._eyex, camera._eyey, camera._eyez);
osg::Vec3d center(camera._centerx, camera._centery, camera._centerz);
osg::Vec3d up(camera._upx, camera._upy, camera._upz);
_distance = (eye - center).length();
osg::Matrixd matrix;
matrix.makeLookAt(eye, center, up);
matrix = osg::Matrixd::inverse(matrix);
_center = osg::Vec3d(0.0, 0.0, -_distance) * matrix;
_rotation = matrix.getRotate();*/
setByLookAt(camera);
}
void Manipulator::setByLookAt(Camera c)
{
Camera camera=c;
osg::Vec3d eye(camera._eyex, camera._eyey, camera._eyez);
osg::Vec3d center(camera._centerx, camera._centery, camera._centerz);
osg::Vec3d up(camera._upx, camera._upy, camera._upz);
_distance = (eye - center).length();
osg::Matrixd matrix;
matrix.makeLookAt(eye, center, up);
matrix = osg::Matrixd::inverse(matrix);
_center = osg::Vec3d(0.0, 0.0, -_distance) * matrix;
_rotation = matrix.getRotate();
}
osg::Matrixd Manipulator::getViewMatrix() {
return osg::Matrixd::translate(0.0, 0.0, _distance) *
osg::Matrixd::rotate(_rotation) *
osg::Matrixd::translate(_center);
}
osg::Matrixd Manipulator::getInverseViewMatrix() {
return osg::Matrixd::translate(-_center)*
osg::Matrixd::rotate(_rotation.inverse()) *
osg::Matrixd::translate(0.0, 0.0, -_distance);
//return osg::Matrixd::inverse(getViewMatrix());
}
osg::Matrixd Manipulator:: getPredInverseViewMatrix(double delta_time) {
/*
double acc_angle = 0.0;
osg::Vec3d axis;
_acc_rotation.getRotate(acc_angle, axis);
// delta_t/t
double ratio = delta_time / _acc_totalTime;
//double ratio=0;
// 预测旋转
osg::Quat delta_rotation(ratio * acc_angle , axis);
osg::Quat pred_rotation = _rotation *delta_rotation;
// 预测平移
osg::Vec3d pred_center = _center + _acc_translation * ratio;
double pred_distance = _distance + _acc_distance * ratio;
pred_distance = pred_distance < _min_distance ? _min_distance : pred_distance;
return osg::Matrixd::translate(-pred_center)*
osg::Matrixd::rotate(pred_rotation.inverse()) *
osg::Matrixd::translate(0.0, 0.0, -pred_distance);*/
double epsilon = 0.00000001;
double angle = 0.0;
osg::Vec3d axis;
_acc_rotation.getRotate(angle, axis);
cout<<"角度:"<<angle<<" "<<endl;
float scale = 0.0;
if(_acc_totalTime > epsilon) scale = delta_time / _acc_totalTime;
osg::Quat rotation(scale * angle , axis);
rotation = _rotation *rotation;
osg::Vec3d center = _center + _acc_translation * scale;
//2014/4/28
double distance = _distance;
//double distance = _distance+_acc_distance*scale;
if(distance < _min_distance ) distance = _min_distance;
return osg::Matrixd::translate(-center)*
osg::Matrixd::rotate(rotation.inverse()) *
osg::Matrixd::translate(0.0, 0.0, -distance);
}
osg::Matrixd Manipulator:: getPredViewMatrix(double delta_time) {
return osg::Matrixd::inverse(getPredInverseViewMatrix(delta_time));
}
void Manipulator::update(TouchEvent &touch_event) {
unsigned char type = touch_event._event_type;
if((type & (1 << 6)) && (type & (1<<7))) {
if((type & 1) && (type & 2)){
// do nothing
}else if(type & 1){
rotateTrackball(touch_event._x1, touch_event._y1,_last_cord1._v[0], _last_cord1._v[1]);
}else if(type & 2){
rotateTrackball(touch_event._x2, touch_event._y2,_last_cord2._v[0], _last_cord2._v[1]);
}
}
updateLastPoint(touch_event);
}
/*bool timeForPrediction() {
osg::Timer_t curr_time = osg::Timer::instance()->tick();
double duration = osg::Timer::instance()->delta_m(_last_pred_time, curr_time);
if(duration >= _pred_period){
_last_pred_time = curr_time;
if(isViewpointChanged()){
return true;
}
}
return false;
}*/
void Manipulator::resetPrediction() {
_acc_translation.set(0.0, 0.0, 0.0);
_acc_rotation.set(0.0, 0.0, 0.0, 1.0);
_acc_distance = 1.0;
}
void Manipulator::setAccTotalTime(double time) {
_acc_totalTime = time;
}
bool Manipulator::isViewpointChanged() {
if(1 - abs(_acc_rotation.w()) > 0.00016 || _acc_translation.length() >0.005) {
return true;
}
return false;
//return true;
}
void Manipulator::rotateTrackball(float px0, float py0, float px1, float py1) {
osg::Vec3d axis;
float angle;
calcRotationArgs(axis, angle, px1, py1, px0, py0);
osg::Quat new_rotate;
new_rotate.makeRotate(angle, axis);
_rotation = _rotation * new_rotate;
_acc_rotation = _acc_rotation *new_rotate;
}
void Manipulator::calcRotationArgs(osg::Vec3d& axis, float& angle,
float p1x, float p1y,float p2x, float p2y) {
osg::Matrixd rotation_matrix(_rotation);
osg::Vec3d uv = osg::Vec3d(0.0f,1.0f,0.0f)*rotation_matrix;
osg::Vec3d sv = osg::Vec3d(1.0f,0.0f,0.0f)*rotation_matrix;
osg::Vec3d lv = osg::Vec3d(0.0f,0.0f,-1.0f)*rotation_matrix;
osg::Vec3d p1 = sv * p1x + uv * p1y - lv * tb_project_to_sphere(_trackball_size, p1x, p1y);
osg::Vec3d p2 = sv * p2x + uv * p2y - lv * tb_project_to_sphere(_trackball_size, p2x, p2y);
axis = p2^p1;
axis.normalize();
float t = (p2 - p1).length() / (2.0 * _trackball_size);
if (t > 1.0) t = 1.0;
if (t < -1.0) t = -1.0;
angle = osg::inRadians(asin(t));
//2014/4/28
//2014/10/26,0.1
angle = 0.02 * angle;
}
float Manipulator::tb_project_to_sphere(float r, float x, float y)
{
float d, t, z;
d = sqrt(x*x + y*y);
if (d < r * 0.70710678118654752440) {
z = sqrt(r*r - d*d);
}
else {
t = r / 1.41421356237309504880;
z = t*t / d;
}
return z;
}
void Manipulator::translate(float dx, float dy, float dz) {
osg::Matrixd rotation_matrix;
rotation_matrix.makeRotate(_rotation);
osg::Vec3d dv(dx, dy, dz);
dv = dv *rotation_matrix;
_center += dv;
_acc_translation += dv;
}
void Manipulator::updateLastPoint(TouchEvent &touch_event) {
_last_cord1._v[0] = touch_event._x1;
_last_cord1._v[1] = touch_event._y1;
_last_cord2._v[0] = touch_event._x2;
_last_cord2._v[1] = touch_event._y2;
}
void Manipulator::handleUpdate(TouchEvent &touch_event)
{
update(touch_event);//封装一层
}
osg::Matrixd Manipulator::getPredInverseMatrix(double delta_time)
{
return getPredInverseViewMatrix(delta_time);
}
Manipulator::~Manipulator()
{
cout<<"执行Manipulator析构"<<endl;
}