Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: chrome/browser/android/vr_shell/vr_controller.cc

Issue 2795793002: Implementation of elbow model for the controller position and rotation. (Closed)
Patch Set: Rebasing. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/vr_shell/vr_controller.h" 5 #include "chrome/browser/android/vr_shell/vr_controller.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ptr_util.h"
12 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "chrome/browser/android/vr_shell/elbow_model.h"
13 #include "device/vr/vr_math.h" 15 #include "device/vr/vr_math.h"
14 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr.h" 16 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr.h"
15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr_controller.h" 17 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr_controller.h"
16 18
17 namespace vr_shell { 19 namespace vr_shell {
18 20
19 namespace { 21 namespace {
20 22
21 constexpr float kDisplacementScaleFactor = 300.0f; 23 constexpr float kDisplacementScaleFactor = 300.0f;
22 24
(...skipping 11 matching lines...) Expand all
34 // not equal. Also, minimum time distance needed to call two timestamps 36 // not equal. Also, minimum time distance needed to call two timestamps
35 // not equal. 37 // not equal.
36 constexpr float kDelta = 1.0e-7f; 38 constexpr float kDelta = 1.0e-7f;
37 39
38 constexpr float kCutoffHz = 10.0f; 40 constexpr float kCutoffHz = 10.0f;
39 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz)); 41 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz));
40 constexpr float kNanoSecondsPerSecond = 1.0e9f; 42 constexpr float kNanoSecondsPerSecond = 1.0e9f;
41 43
42 constexpr int kMaxNumOfExtrapolations = 2; 44 constexpr int kMaxNumOfExtrapolations = 2;
43 45
44 static constexpr gfx::Point3F kControllerPosition = {0.2f, -0.5f, -0.15f}; 46 // Distance from the center of the controller to start rendering the laser.
47 constexpr float kLaserStartDisplacement = 0.045;
45 48
46 void ClampTouchpadPosition(gfx::Vector2dF* position) { 49 void ClampTouchpadPosition(gfx::Vector2dF* position) {
47 position->set_x(std::min(std::max(0.0f, position->x()), 1.0f)); 50 position->set_x(vr::Clampf(position->x(), 0.0f, 1.0f));
48 position->set_y(std::min(std::max(0.0f, position->y()), 1.0f)); 51 position->set_y(vr::Clampf(position->y(), 0.0f, 1.0f));
49 } 52 }
50 53
51 } // namespace 54 } // namespace
52 55
53 VrController::VrController(gvr_context* vr_context) { 56 VrController::VrController(gvr_context* vr_context) {
54 DVLOG(1) << __FUNCTION__ << "=" << this; 57 DVLOG(1) << __FUNCTION__ << "=" << this;
55 Initialize(vr_context); 58 Initialize(vr_context);
59 elbow_model_ = base::MakeUnique<ElbowModel>(handedness_);
56 Reset(); 60 Reset();
61 last_timestamp_nanos_ =
62 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos;
57 } 63 }
58 64
59 VrController::~VrController() { 65 VrController::~VrController() {
60 DVLOG(1) << __FUNCTION__ << "=" << this; 66 DVLOG(1) << __FUNCTION__ << "=" << this;
61 } 67 }
62 68
63 void VrController::OnResume() { 69 void VrController::OnResume() {
64 if (controller_api_) 70 if (controller_api_)
65 controller_api_->Resume(); 71 controller_api_->Resume();
66 } 72 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 float VrController::TouchPosY() { 113 float VrController::TouchPosY() {
108 return controller_state_->GetTouchPos().y; 114 return controller_state_->GetTouchPos().y;
109 } 115 }
110 116
111 vr::Quatf VrController::Orientation() const { 117 vr::Quatf VrController::Orientation() const {
112 const gvr::Quatf& orientation = controller_state_->GetOrientation(); 118 const gvr::Quatf& orientation = controller_state_->GetOrientation();
113 return *reinterpret_cast<vr::Quatf*>(const_cast<gvr::Quatf*>(&orientation)); 119 return *reinterpret_cast<vr::Quatf*>(const_cast<gvr::Quatf*>(&orientation));
114 } 120 }
115 121
116 void VrController::GetTransform(vr::Mat4f* out) const { 122 void VrController::GetTransform(vr::Mat4f* out) const {
117 // TODO(acondor): Position and orientation needs to be obtained 123 QuatToMatrix(elbow_model_->GetControllerRotation(), out);
118 // from an elbow model. 124 auto position = elbow_model_->GetControllerPosition();
119 // Placing the controller in a fixed position for now. 125 vr::TranslateM(*out, vr::ToVector(position), out);
120 vr::SetIdentityM(out); 126 }
121 // Changing rotation point. 127
122 vr::TranslateM(*out, gfx::Vector3dF(0, 0, 0.05), out); 128 float VrController::GetOpacity() const {
123 vr::Mat4f quat_to_matrix; 129 return elbow_model_->GetAlphaValue();
124 vr::QuatToMatrix(Orientation(), &quat_to_matrix); 130 }
125 vr::MatrixMul(quat_to_matrix, *out, out); 131
126 gfx::Vector3dF translation(kControllerPosition.x(), kControllerPosition.y(), 132 gfx::Point3F VrController::GetPointerStart() const {
127 kControllerPosition.z() - 0.05); 133 auto controller_position = elbow_model_->GetControllerPosition();
128 vr::TranslateM(*out, translation, out); 134 gfx::Vector3dF pointer_direction{0.0f, -sin(kErgoAngleOffset),
135 -cos(kErgoAngleOffset)};
136 vr::Mat4f rotation_mat;
137 vr::QuatToMatrix(Orientation(), &rotation_mat);
138 pointer_direction = vr::MatrixVectorRotate(rotation_mat, pointer_direction);
139 return controller_position +
140 gfx::ScaleVector3d(pointer_direction, kLaserStartDisplacement);
129 } 141 }
130 142
131 VrControllerModel::State VrController::GetModelState() const { 143 VrControllerModel::State VrController::GetModelState() const {
132 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) 144 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK))
133 return VrControllerModel::TOUCHPAD; 145 return VrControllerModel::TOUCHPAD;
134 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) 146 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP))
135 return VrControllerModel::APP; 147 return VrControllerModel::APP;
136 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME)) 148 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME))
137 return VrControllerModel::SYSTEM; 149 return VrControllerModel::SYSTEM;
138 return VrControllerModel::IDLE; 150 return VrControllerModel::IDLE;
(...skipping 16 matching lines...) Expand all
155 } 167 }
156 168
157 bool VrController::ButtonState(gvr::ControllerButton button) const { 169 bool VrController::ButtonState(gvr::ControllerButton button) const {
158 return controller_state_->GetButtonState(button); 170 return controller_state_->GetButtonState(button);
159 } 171 }
160 172
161 bool VrController::IsConnected() { 173 bool VrController::IsConnected() {
162 return controller_state_->GetConnectionState() == gvr::kControllerConnected; 174 return controller_state_->GetConnectionState() == gvr::kControllerConnected;
163 } 175 }
164 176
165 void VrController::UpdateState() { 177 void VrController::UpdateState(const gfx::Vector3dF& head_direction) {
166 const int32_t old_status = controller_state_->GetApiStatus(); 178 const int32_t old_status = controller_state_->GetApiStatus();
167 const int32_t old_connection_state = controller_state_->GetConnectionState(); 179 const int32_t old_connection_state = controller_state_->GetConnectionState();
168 // Read current controller state. 180 // Read current controller state.
169 controller_state_->Update(*controller_api_); 181 controller_state_->Update(*controller_api_);
170 // Print new API status and connection state, if they changed. 182 // Print new API status and connection state, if they changed.
171 if (controller_state_->GetApiStatus() != old_status || 183 if (controller_state_->GetApiStatus() != old_status ||
172 controller_state_->GetConnectionState() != old_connection_state) { 184 controller_state_->GetConnectionState() != old_connection_state) {
173 VLOG(1) << "Controller Connection status: " 185 VLOG(1) << "Controller Connection status: "
174 << gvr_controller_connection_state_to_string( 186 << gvr_controller_connection_state_to_string(
175 controller_state_->GetConnectionState()); 187 controller_state_->GetConnectionState());
176 } 188 }
189
190 const gvr::Vec3f& gvr_gyro = controller_state_->GetGyro();
191 elbow_model_->Update({IsConnected(), Orientation(),
192 gfx::Vector3dF(gvr_gyro.x, gvr_gyro.y, gvr_gyro.z),
193 head_direction, DeltaTimeSeconds()});
177 } 194 }
178 195
179 void VrController::UpdateTouchInfo() { 196 void VrController::UpdateTouchInfo() {
180 CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly."; 197 CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly.";
181 if (IsTouching() && state_ == SCROLLING && 198 if (IsTouching() && state_ == SCROLLING &&
182 (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ || 199 (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ ||
183 (cur_touch_point_->position == prev_touch_point_->position && 200 (cur_touch_point_->position == prev_touch_point_->position &&
184 extrapolated_touch_ < kMaxNumOfExtrapolations))) { 201 extrapolated_touch_ < kMaxNumOfExtrapolations))) {
185 extrapolated_touch_++; 202 extrapolated_touch_++;
186 touch_position_changed_ = true; 203 touch_position_changed_ = true;
187 // Fill the touch_info 204 // Fill the touch_info
188 float duration = 205 float duration = DeltaTimeSeconds();
189 (gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos -
190 last_timestamp_nanos_) /
191 kNanoSecondsPerSecond;
192 touch_info_->touch_point.position.set_x(cur_touch_point_->position.x() + 206 touch_info_->touch_point.position.set_x(cur_touch_point_->position.x() +
193 overall_velocity_.x() * duration); 207 overall_velocity_.x() * duration);
194 touch_info_->touch_point.position.set_y(cur_touch_point_->position.y() + 208 touch_info_->touch_point.position.set_y(cur_touch_point_->position.y() +
195 overall_velocity_.y() * duration); 209 overall_velocity_.y() * duration);
196 } else { 210 } else {
197 if (extrapolated_touch_ == kMaxNumOfExtrapolations) { 211 if (extrapolated_touch_ == kMaxNumOfExtrapolations) {
198 overall_velocity_ = {0, 0}; 212 overall_velocity_ = {0, 0};
199 } 213 }
200 extrapolated_touch_ = 0; 214 extrapolated_touch_ = 0;
201 } 215 }
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 touch_info_->touch_point.position - prev_touch_point_->position; 433 touch_info_->touch_point.position - prev_touch_point_->position;
420 434
421 const gfx::Vector2dF& velocity = ScaleVector2d(displacement, (1 / duration)); 435 const gfx::Vector2dF& velocity = ScaleVector2d(displacement, (1 / duration));
422 436
423 float weight = duration / (kRC + duration); 437 float weight = duration / (kRC + duration);
424 438
425 overall_velocity_ = ScaleVector2d(overall_velocity_, (1 - weight)) + 439 overall_velocity_ = ScaleVector2d(overall_velocity_, (1 - weight)) +
426 ScaleVector2d(velocity, weight); 440 ScaleVector2d(velocity, weight);
427 } 441 }
428 442
443 float VrController::DeltaTimeSeconds() {
mthiesse 2017/04/26 17:46:48 nit: Take last_timestamp_nanos_ in as a parameter
acondor_ 2017/04/26 18:42:26 Done.
444 return (gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos -
445 last_timestamp_nanos_) /
446 kNanoSecondsPerSecond;
447 }
448
429 } // namespace vr_shell 449 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698