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

Side by Side Diff: third_party/WebKit/Source/modules/sensor/OrientationSensor.cpp

Issue 2785493002: [Sensors] OrientationSensor.populateMatrix() works also with Float64Array and DOMMatrix types (Closed)
Patch Set: 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 2017 The Chromium Authors. All rights reserved. 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 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 "modules/sensor/OrientationSensor.h" 5 #include "modules/sensor/OrientationSensor.h"
6 6
7 #include "bindings/core/v8/ExceptionState.h" 7 #include "bindings/core/v8/ExceptionState.h"
8 #include "core/geometry/DOMMatrix.h"
8 9
9 using device::mojom::blink::SensorType; 10 using device::mojom::blink::SensorType;
10 11
11 namespace blink { 12 namespace blink {
12 13
13 Vector<double> OrientationSensor::quaternion(bool& isNull) { 14 Vector<double> OrientationSensor::quaternion(bool& isNull) {
14 m_readingDirty = false; 15 m_readingDirty = false;
15 isNull = !canReturnReadings(); 16 isNull = !canReturnReadings();
16 return isNull ? Vector<double>() 17 return isNull ? Vector<double>()
17 : Vector<double>({readingValueUnchecked(3), // W 18 : Vector<double>({readingValueUnchecked(3), // W
18 readingValueUnchecked(0), // Vx 19 readingValueUnchecked(0), // Vx
19 readingValueUnchecked(1), // Vy 20 readingValueUnchecked(1), // Vy
20 readingValueUnchecked(2)}); // Vz 21 readingValueUnchecked(2)}); // Vz
21 } 22 }
22 23
23 void OrientationSensor::populateMatrix(DOMFloat32Array* buffer, 24 template <typename T>
24 ExceptionState& exceptionState) { 25 void doPopulateMatrix(T* targetMatrix, double x, double y, double z, double w) {
25 if (buffer->length() < 16) { 26 auto out = targetMatrix->data();
27 out[0] = 1.0 - 2 * (y * y + z * z);
28 out[1] = 2 * (x * y - z * w);
29 out[2] = 2 * (x * z + y * w);
30 out[3] = 0.0;
31 out[4] = 2 * (x * y + z * w);
32 out[5] = 1.0 - 2 * (x * x + z * z);
33 out[6] = 2 * (y * z - x * w);
34 out[7] = 0.0;
35 out[8] = 2 * (x * z - y * w);
36 out[9] = 2 * (y * z + x * w);
37 out[10] = 1.0 - 2 * (x * x + y * y);
38 out[11] = 0.0;
39 out[12] = 0.0;
40 out[13] = 0.0;
41 out[14] = 0.0;
42 out[15] = 1.0;
43 }
44
45 template <>
46 void doPopulateMatrix(DOMMatrix* targetMatrix,
47 double x,
48 double y,
49 double z,
50 double w) {
51 targetMatrix->setM11(1.0 - 2 * (y * y + z * z));
Reilly Grant (use Gerrit) 2017/03/28 18:23:03 It would be nice if DOMMatrix provided a way to ca
52 targetMatrix->setM12(2 * (x * y - z * w));
53 targetMatrix->setM13(2 * (x * z + y * w));
54 targetMatrix->setM14(0.0);
55 targetMatrix->setM21(2 * (x * y + z * w));
56 targetMatrix->setM22(1.0 - 2 * (x * x + z * z));
57 targetMatrix->setM23(2 * y * z - 2 * x * w);
58 targetMatrix->setM24(0.0);
59 targetMatrix->setM31(2 * (x * z - y * w));
60 targetMatrix->setM32(2 * (y * z + x * w));
61 targetMatrix->setM33(1.0 - 2 * (x * x + y * y));
62 targetMatrix->setM34(0.0);
63 targetMatrix->setM41(0.0);
64 targetMatrix->setM42(0.0);
65 targetMatrix->setM43(0.0);
66 targetMatrix->setM44(1.0);
67 }
68
69 template <typename T>
70 bool checkBufferLength(T* buffer) {
71 return buffer->length() >= 16;
72 }
73
74 template <>
75 bool checkBufferLength(DOMMatrix*) {
76 return true;
77 }
78
79 template <typename Matrix>
80 void OrientationSensor::populateMatrixInternal(Matrix* targetMatrix,
Reilly Grant (use Gerrit) 2017/03/28 18:23:03 I would merge this function into populateMatrix be
Mikhail 2017/03/29 08:27:58 Merging looks a bit problematic as there are two t
Reilly Grant (use Gerrit) 2017/03/29 15:31:22 I would write populateMatrix like this, which only
81 ExceptionState& exceptionState) {
82 if (!checkBufferLength(targetMatrix)) {
26 exceptionState.throwTypeError( 83 exceptionState.throwTypeError(
27 "Target buffer must have at least 16 elements."); 84 "Target buffer must have at least 16 elements.");
28 return; 85 return;
29 } 86 }
30 if (!isActivated()) { 87 if (!isActivated()) {
31 exceptionState.throwDOMException( 88 exceptionState.throwDOMException(
32 InvalidStateError, "The sensor must be in 'connected' state."); 89 InvalidStateError, "The sensor must be in 'connected' state.");
33 return; 90 return;
34 } 91 }
35 if (!canReturnReadings()) 92 if (!canReturnReadings())
36 return; 93 return;
37 94
38 float x = readingValueUnchecked(0); 95 double x = readingValueUnchecked(0);
39 float y = readingValueUnchecked(1); 96 double y = readingValueUnchecked(1);
40 float z = readingValueUnchecked(2); 97 double z = readingValueUnchecked(2);
41 float w = readingValueUnchecked(3); 98 double w = readingValueUnchecked(3);
42 99
43 float* out = buffer->data(); 100 doPopulateMatrix(targetMatrix, x, y, z, w);
44 out[0] = 1.0 - 2 * (y * y - z * z); 101 }
45 out[1] = 2 * (x * y - z * w); 102
46 out[2] = 2 * (x * z + y * w); 103 void OrientationSensor::populateMatrix(
47 out[4] = 2 * (x * y + z * w); 104 Float32ArrayOrFloat64ArrayOrDOMMatrix& matrix,
48 out[5] = 1.0 - 2 * (x * x - z * z); 105 ExceptionState& exceptionState) {
49 out[6] = 2 * (y * z - x * w); 106 if (matrix.isFloat32Array())
50 out[8] = 2 * (x * z - y * w); 107 populateMatrixInternal(matrix.getAsFloat32Array(), exceptionState);
51 out[9] = 2 * (y * z + x * w); 108 else if (matrix.isFloat64Array())
52 out[10] = 1.0 - 2 * (x * x - y * y); 109 populateMatrixInternal(matrix.getAsFloat64Array(), exceptionState);
53 out[15] = 1.0; 110 else if (matrix.isDOMMatrix())
111 populateMatrixInternal(matrix.getAsDOMMatrix(), exceptionState);
112 else
113 NOTREACHED() << "Unexpected rotation matrix type.";
54 } 114 }
55 115
56 bool OrientationSensor::isReadingDirty() const { 116 bool OrientationSensor::isReadingDirty() const {
57 return m_readingDirty || !canReturnReadings(); 117 return m_readingDirty || !canReturnReadings();
58 } 118 }
59 119
60 OrientationSensor::OrientationSensor(ExecutionContext* executionContext, 120 OrientationSensor::OrientationSensor(ExecutionContext* executionContext,
61 const SensorOptions& options, 121 const SensorOptions& options,
62 ExceptionState& exceptionState, 122 ExceptionState& exceptionState,
63 device::mojom::blink::SensorType type) 123 device::mojom::blink::SensorType type)
64 : Sensor(executionContext, options, exceptionState, type), 124 : Sensor(executionContext, options, exceptionState, type),
65 m_readingDirty(true) {} 125 m_readingDirty(true) {}
66 126
67 void OrientationSensor::onSensorReadingChanged() { 127 void OrientationSensor::onSensorReadingChanged() {
68 m_readingDirty = true; 128 m_readingDirty = true;
69 } 129 }
70 130
71 DEFINE_TRACE(OrientationSensor) { 131 DEFINE_TRACE(OrientationSensor) {
72 Sensor::trace(visitor); 132 Sensor::trace(visitor);
73 } 133 }
74 134
75 } // namespace blink 135 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698