| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/sensor/OrientationSensor.h" |
| 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 |
| 9 using device::mojom::blink::SensorType; |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 Vector<double> OrientationSensor::quaternion(bool& isNull) { |
| 14 m_readingDirty = false; |
| 15 isNull = !canReturnReadings(); |
| 16 return isNull ? Vector<double>() |
| 17 : Vector<double>({readingValueUnchecked(3), // W |
| 18 readingValueUnchecked(0), // Vx |
| 19 readingValueUnchecked(1), // Vy |
| 20 readingValueUnchecked(2)}); // Vz |
| 21 } |
| 22 |
| 23 void OrientationSensor::populateMatrix(DOMFloat32Array* buffer, |
| 24 ExceptionState& exceptionState) { |
| 25 if (buffer->length() < 16) { |
| 26 exceptionState.throwTypeError( |
| 27 "Target buffer must have at least 16 elements."); |
| 28 return; |
| 29 } |
| 30 if (!isActivated()) { |
| 31 exceptionState.throwDOMException( |
| 32 InvalidStateError, "The sensor must be in 'connected' state."); |
| 33 return; |
| 34 } |
| 35 if (!canReturnReadings()) |
| 36 return; |
| 37 |
| 38 float x = readingValueUnchecked(0); |
| 39 float y = readingValueUnchecked(1); |
| 40 float z = readingValueUnchecked(2); |
| 41 float w = readingValueUnchecked(3); |
| 42 |
| 43 float* out = buffer->data(); |
| 44 out[0] = 1.0 - 2 * (y * y - z * z); |
| 45 out[1] = 2 * (x * y - z * w); |
| 46 out[2] = 2 * (x * z + y * w); |
| 47 out[4] = 2 * (x * y + z * w); |
| 48 out[5] = 1.0 - 2 * (x * x - z * z); |
| 49 out[6] = 2 * (y * z - x * w); |
| 50 out[8] = 2 * (x * z - y * w); |
| 51 out[9] = 2 * (y * z + x * w); |
| 52 out[10] = 1.0 - 2 * (x * x - y * y); |
| 53 out[15] = 1.0; |
| 54 } |
| 55 |
| 56 bool OrientationSensor::isReadingDirty() const { |
| 57 return m_readingDirty || !canReturnReadings(); |
| 58 } |
| 59 |
| 60 OrientationSensor::OrientationSensor(ExecutionContext* executionContext, |
| 61 const SensorOptions& options, |
| 62 ExceptionState& exceptionState, |
| 63 device::mojom::blink::SensorType type) |
| 64 : Sensor(executionContext, options, exceptionState, type), |
| 65 m_readingDirty(true) {} |
| 66 |
| 67 void OrientationSensor::onSensorReadingChanged() { |
| 68 m_readingDirty = true; |
| 69 } |
| 70 |
| 71 DEFINE_TRACE(OrientationSensor) { |
| 72 Sensor::trace(visitor); |
| 73 } |
| 74 |
| 75 } // namespace blink |
| OLD | NEW |