From c7dcf1172b5d56a2ff6e7ce233da7fd020fa5365 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 17 Apr 2023 11:53:10 +0000 Subject: [PATCH] =?UTF-8?q?float=E6=94=B9double?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: Ia39eee650b65a147bf7eb609a75fe283ee2cd987 --- interfaces/native/include/geomagnetic_field.h | 22 +-- interfaces/native/include/sensor_algorithm.h | 32 ++-- interfaces/native/src/geomagnetic_field.cpp | 146 +++++++++--------- interfaces/native/src/sensor_algorithm.cpp | 94 +++++------ .../test/unittest/sensor_algorithm_test.cpp | 144 ++++++++--------- interfaces/plugin/include/sensor_napi_utils.h | 1 + interfaces/plugin/src/sensor_js.cpp | 62 ++++---- interfaces/plugin/src/sensor_napi_utils.cpp | 16 ++ 8 files changed, 267 insertions(+), 250 deletions(-) diff --git a/interfaces/native/include/geomagnetic_field.h b/interfaces/native/include/geomagnetic_field.h index 1c3dd4c1..c60fd899 100644 --- a/interfaces/native/include/geomagnetic_field.h +++ b/interfaces/native/include/geomagnetic_field.h @@ -21,23 +21,23 @@ class GeomagneticField { public: - GeomagneticField(float latitude, float longitude, float altitude, int64_t timeMillis); + GeomagneticField(double latitude, double longitude, double altitude, int64_t timeMillis); ~GeomagneticField() = default; - float ObtainX(); - float ObtainY(); - float ObtainZ(); - float ObtainGeomagneticDip(); - float ObtainDeflectionAngle(); - float ObtainLevelIntensity(); - float ObtainTotalIntensity(); + double ObtainX(); + double ObtainY(); + double ObtainZ(); + double ObtainGeomagneticDip(); + double ObtainDeflectionAngle(); + double ObtainLevelIntensity(); + double ObtainTotalIntensity(); private: - std::vector> GetSchmidtQuasiNormalFactors(int32_t expansionDegree); + std::vector> GetSchmidtQuasiNormalFactors(int32_t expansionDegree); void CalculateGeomagneticComponent(double latDiffRad, int64_t timeMillis); void GetLongitudeTrigonometric(); void GetRelativeRadiusPower(); - void CalibrateGeocentricCoordinates(float latitude, float longitude, float altitude); - void InitLegendreTable(int32_t expansionDegree, float thetaRad); + void CalibrateGeocentricCoordinates(double latitude, double longitude, double altitude); + void InitLegendreTable(int32_t expansionDegree, double thetaRad); double ToDegrees(double angrad); double ToRadians(double angdeg); }; diff --git a/interfaces/native/include/sensor_algorithm.h b/interfaces/native/include/sensor_algorithm.h index 83bee7e2..43f8e2c5 100644 --- a/interfaces/native/include/sensor_algorithm.h +++ b/interfaces/native/include/sensor_algorithm.h @@ -23,27 +23,27 @@ class SensorAlgorithm { public: SensorAlgorithm() = default; ~SensorAlgorithm() = default; - int32_t CreateQuaternion(std::vector rotationVector, std::vector &quaternion); - int32_t TransformCoordinateSystem(std::vector inRotationMatrix, int32_t axisX, - int32_t axisY, std::vector &outRotationMatrix); - int32_t GetAltitude(float seaPressure, float currentPressure, float *altitude); - int32_t GetGeomagneticDip(std::vector inclinationMatrix, float *geomagneticDip); - int32_t GetAngleModify(std::vector currotationMatrix, std::vector prerotationMatrix, - std::vector &angleChange); - int32_t GetDirection(std::vector rotationMatrix, std::vector &rotationAngle); - int32_t CreateRotationMatrix(std::vector rotationVector, std::vector &rotationMatrix); - int32_t CreateRotationAndInclination(std::vector gravity, std::vector geomagnetic, - std::vector &rotationMatrix, std::vector &inclinationMatrix); + int32_t CreateQuaternion(std::vector rotationVector, std::vector &quaternion); + int32_t TransformCoordinateSystem(std::vector inRotationMatrix, int32_t axisX, + int32_t axisY, std::vector &outRotationMatrix); + int32_t GetAltitude(double seaPressure, double currentPressure, double *altitude); + int32_t GetGeomagneticDip(std::vector inclinationMatrix, double *geomagneticDip); + int32_t GetAngleModify(std::vector currotationMatrix, std::vector prerotationMatrix, + std::vector &angleChange); + int32_t GetDirection(std::vector rotationMatrix, std::vector &rotationAngle); + int32_t CreateRotationMatrix(std::vector rotationVector, std::vector &rotationMatrix); + int32_t CreateRotationAndInclination(std::vector gravity, std::vector geomagnetic, + std::vector &rotationMatrix, std::vector &inclinationMatrix); private: - int32_t TransformCoordinateSystemImpl(std::vector inRotationMatrix, int32_t axisX, - int32_t axisY, std::vector &outRotationMatrix); + int32_t TransformCoordinateSystemImpl(std::vector inRotationMatrix, int32_t axisX, + int32_t axisY, std::vector &outRotationMatrix); static constexpr int32_t QUATERNION_LENGTH = 4; static constexpr int32_t ROTATION_VECTOR_LENGTH = 3; static constexpr int32_t THREE_DIMENSIONAL_MATRIX_LENGTH = 9; static constexpr int32_t FOUR_DIMENSIONAL_MATRIX_LENGTH = 16; - static constexpr float GRAVITATIONAL_ACCELERATION = 9.81f; - static constexpr float RECIPROCAL_COEFFICIENT = 5.255f; - static constexpr float ZERO_PRESSURE_ALTITUDE = 44330.0f; + static constexpr double GRAVITATIONAL_ACCELERATION = 9.81f; + static constexpr double RECIPROCAL_COEFFICIENT = 5.255f; + static constexpr double ZERO_PRESSURE_ALTITUDE = 44330.0f; }; #endif // SENSOR_ALGORITHM_H diff --git a/interfaces/native/src/geomagnetic_field.cpp b/interfaces/native/src/geomagnetic_field.cpp index 31929718..7b7eac64 100644 --- a/interfaces/native/src/geomagnetic_field.cpp +++ b/interfaces/native/src/geomagnetic_field.cpp @@ -20,18 +20,18 @@ using namespace std; namespace { -constexpr float EARTH_MAJOR_AXIS_RADIUS = 6378.137f; -constexpr float EARTH_MINOR_AXIS_RADIUS = 6356.7523142f; -constexpr float EARTH_REFERENCE_RADIUS = 6371.2f; -constexpr float PRECISION = 1e-5f; -constexpr float LATITUDE_MAX = 90.0f; -constexpr float LATITUDE_MIN = -90.0f; -constexpr float CONVERSION_FACTOR = 1000.0f; -constexpr float DERIVATIVE_FACTOR = 1.0f; +constexpr double EARTH_MAJOR_AXIS_RADIUS = 6378.137f; +constexpr double EARTH_MINOR_AXIS_RADIUS = 6356.7523142f; +constexpr double EARTH_REFERENCE_RADIUS = 6371.2f; +constexpr double PRECISION = 1e-5f; +constexpr double LATITUDE_MAX = 90.0f; +constexpr double LATITUDE_MIN = -90.0f; +constexpr double CONVERSION_FACTOR = 1000.0f; +constexpr double DERIVATIVE_FACTOR = 1.0f; // the time from 1970-01-01 to 2020-01-01 as UTC milliseconds from the epoch constexpr int64_t WMM_BASE_TIME = 1580486400000; // The following Gaussian coefficients are derived from the US/ United Kingdom World Magnetic Model 2020-2025. -constexpr float GAUSS_COEFFICIENT_G[13][13] = { +constexpr double GAUSS_COEFFICIENT_G[13][13] = { {0.0f}, {-29404.5f, -1450.7f}, {-2500.0f, 2982.0f, 1676.8f}, @@ -46,7 +46,7 @@ constexpr float GAUSS_COEFFICIENT_G[13][13] = { {3.0f, -1.4f, -2.5f, 2.4f, -0.9f, 0.3f, -0.7f, -0.1f, 1.4f, -0.6f, 0.2f, 3.1f}, {-2.0f, -0.1f, 0.5f, 1.3f, -1.2f, 0.7f, 0.3f, 0.5f, -0.2f, -0.5f, 0.1f, -1.1f, -0.3f} }; -constexpr float GAUSS_COEFFICIENT_H[13][13] = { +constexpr double GAUSS_COEFFICIENT_H[13][13] = { {0.0f}, {0.0f, 4652.9f}, {0.0f, -2991.6f, -734.8f}, @@ -61,7 +61,7 @@ constexpr float GAUSS_COEFFICIENT_H[13][13] = { {0.0f, 0.0f, 2.6f, -0.5f, -0.4f, 0.6f, -0.2f, -1.7f, -1.6f, -3.0f, -2.0f, -2.6f}, {0.0f, -1.2f, 0.5f, 1.3f, -1.8f, 0.1f, 0.7f, -0.1f, 0.6f, 0.2f, -0.9f, 0.0f, 0.5f} }; -constexpr float DELTA_GAUSS_COEFFICIENT_G[13][13] = { +constexpr double DELTA_GAUSS_COEFFICIENT_G[13][13] = { {0.0f}, {6.7f, 7.7f}, {-11.5f, -7.1f, -2.2f}, @@ -76,7 +76,7 @@ constexpr float DELTA_GAUSS_COEFFICIENT_G[13][13] = { {0.0f, -0.1f, 0.0f, 0.0f, 0.0f, -0.1f, 0.0f, 0.0f, -0.1f, -0.1f, -0.1f, -0.1f}, {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.1f} }; -constexpr float DELTA_GAUSS_COEFFICIENT_H[13][13] = { +constexpr double DELTA_GAUSS_COEFFICIENT_H[13][13] = { {0.0f}, {0.0f, -25.1f}, {0.0f, -30.2f, -23.9f}, @@ -94,46 +94,46 @@ constexpr float DELTA_GAUSS_COEFFICIENT_H[13][13] = { constexpr int32_t GAUSSIAN_COEFFICIENT_DIMENSION = 13; std::mutex mutex_; -float northComponent; -float eastComponent; -float downComponent; -float geocentricLatitude; -float geocentricLongitude; -float geocentricRadius; +double northComponent; +double eastComponent; +double downComponent; +double geocentricLatitude; +double geocentricLongitude; +double geocentricRadius; -std::vector> schmidtQuasiNormalFactors; -std::vector> polynomials(GAUSSIAN_COEFFICIENT_DIMENSION); -std::vector> polynomialsDerivative(GAUSSIAN_COEFFICIENT_DIMENSION); -std::vector relativeRadiusPower(GAUSSIAN_COEFFICIENT_DIMENSION + 2); -std::vector sinMLongitude(GAUSSIAN_COEFFICIENT_DIMENSION); -std::vector cosMLongitude(GAUSSIAN_COEFFICIENT_DIMENSION); +std::vector> schmidtQuasiNormalFactors; +std::vector> polynomials(GAUSSIAN_COEFFICIENT_DIMENSION); +std::vector> polynomialsDerivative(GAUSSIAN_COEFFICIENT_DIMENSION); +std::vector relativeRadiusPower(GAUSSIAN_COEFFICIENT_DIMENSION + 2); +std::vector sinMLongitude(GAUSSIAN_COEFFICIENT_DIMENSION); +std::vector cosMLongitude(GAUSSIAN_COEFFICIENT_DIMENSION); } -GeomagneticField::GeomagneticField(float latitude, float longitude, float altitude, int64_t timeMillis) +GeomagneticField::GeomagneticField(double latitude, double longitude, double altitude, int64_t timeMillis) { std::lock_guard geomagneticLock(mutex_); schmidtQuasiNormalFactors = GetSchmidtQuasiNormalFactors(GAUSSIAN_COEFFICIENT_DIMENSION); - float gcLatitude = fmax(LATITUDE_MIN + PRECISION, fmin(LATITUDE_MAX - PRECISION, latitude)); + double gcLatitude = fmax(LATITUDE_MIN + PRECISION, fmin(LATITUDE_MAX - PRECISION, latitude)); CalibrateGeocentricCoordinates(gcLatitude, longitude, altitude); - InitLegendreTable(GAUSSIAN_COEFFICIENT_DIMENSION - 1, static_cast(M_PI / 2.0 - geocentricLatitude)); + InitLegendreTable(GAUSSIAN_COEFFICIENT_DIMENSION - 1, static_cast(M_PI / 2.0 - geocentricLatitude)); GetRelativeRadiusPower(); double latDiffRad = ToRadians(gcLatitude) - geocentricLatitude; CalculateGeomagneticComponent(latDiffRad, timeMillis); } -std::vector> GeomagneticField::GetSchmidtQuasiNormalFactors(int32_t expansionDegree) +std::vector> GeomagneticField::GetSchmidtQuasiNormalFactors(int32_t expansionDegree) { - std::vector> schmidtQuasiNormFactors(expansionDegree + 1); + std::vector> schmidtQuasiNormFactors(expansionDegree + 1); schmidtQuasiNormFactors[0].resize(1); schmidtQuasiNormFactors[0][0] = 1.0f; for (int32_t row = 1; row <= expansionDegree; row++) { schmidtQuasiNormFactors[row].resize(row + 1); schmidtQuasiNormFactors[row][0] = - schmidtQuasiNormFactors[row - 1][0] * (2 * row - 1) / static_cast(row); + schmidtQuasiNormFactors[row - 1][0] * (2 * row - 1) / static_cast(row); for (int32_t column = 1; column <= row; column++) { schmidtQuasiNormFactors[row][column] = schmidtQuasiNormFactors[row][column - 1] - * static_cast(sqrt((row - column + 1) * ((column == 1) ? 2 : 1) - / static_cast(row + column))); + * static_cast(sqrt((row - column + 1) * ((column == 1) ? 2 : 1) + / static_cast(row + column))); } } return schmidtQuasiNormFactors; @@ -141,17 +141,17 @@ std::vector> GeomagneticField::GetSchmidtQuasiNormalFactors(i void GeomagneticField::CalculateGeomagneticComponent(double latDiffRad, int64_t timeMillis) { - float yearsSinceBase = (timeMillis - WMM_BASE_TIME) / (365.0f * 24.0f * 60.0f * 60.0f * 1000.0f); - float inverseCosLatitude = DERIVATIVE_FACTOR / static_cast(cos(geocentricLatitude)); + double yearsSinceBase = (timeMillis - WMM_BASE_TIME) / (365.0f * 24.0f * 60.0f * 60.0f * 1000.0f); + double inverseCosLatitude = DERIVATIVE_FACTOR / static_cast(cos(geocentricLatitude)); GetLongitudeTrigonometric(); - float gcX = 0.0f; - float gcY = 0.0f; - float gcZ = 0.0f; + double gcX = 0.0f; + double gcY = 0.0f; + double gcZ = 0.0f; for (int32_t row = 1; row < GAUSSIAN_COEFFICIENT_DIMENSION; row++) { for (int32_t column = 0; column <= row; column++) { - float g = GAUSS_COEFFICIENT_G[row][column] + yearsSinceBase + double g = GAUSS_COEFFICIENT_G[row][column] + yearsSinceBase * DELTA_GAUSS_COEFFICIENT_G[row][column]; - float h = GAUSS_COEFFICIENT_H[row][column] + yearsSinceBase + double h = GAUSS_COEFFICIENT_H[row][column] + yearsSinceBase * DELTA_GAUSS_COEFFICIENT_H[row][column]; gcX += relativeRadiusPower[row + 2] * (g * cosMLongitude[column] + h * sinMLongitude[column]) @@ -167,9 +167,9 @@ void GeomagneticField::CalculateGeomagneticComponent(double latDiffRad, int64_t * polynomials[row][column] * schmidtQuasiNormalFactors[row][column]; } - northComponent = static_cast(gcX * cos(latDiffRad) + gcZ * sin(latDiffRad)); + northComponent = static_cast(gcX * cos(latDiffRad) + gcZ * sin(latDiffRad)); eastComponent = gcY; - downComponent = static_cast(-gcX * sin(latDiffRad) + gcZ * cos(latDiffRad)); + downComponent = static_cast(-gcX * sin(latDiffRad) + gcZ * cos(latDiffRad)); } } @@ -177,8 +177,8 @@ void GeomagneticField::GetLongitudeTrigonometric() { sinMLongitude[0] = 0.0f; cosMLongitude[0] = 1.0f; - sinMLongitude[1] = static_cast(sin(geocentricLongitude)); - cosMLongitude[1] = static_cast(cos(geocentricLongitude)); + sinMLongitude[1] = static_cast(sin(geocentricLongitude)); + cosMLongitude[1] = static_cast(cos(geocentricLongitude)); for (uint32_t index = 2; index < GAUSSIAN_COEFFICIENT_DIMENSION; ++index) { uint32_t x = index >> 1; sinMLongitude[index] = (sinMLongitude[index - x] * cosMLongitude[x] @@ -197,33 +197,33 @@ void GeomagneticField::GetRelativeRadiusPower() } } -void GeomagneticField::CalibrateGeocentricCoordinates(float latitude, float longitude, float altitude) +void GeomagneticField::CalibrateGeocentricCoordinates(double latitude, double longitude, double altitude) { - float altitudeKm = altitude / CONVERSION_FACTOR; - float a2 = EARTH_MAJOR_AXIS_RADIUS * EARTH_MAJOR_AXIS_RADIUS; - float b2 = EARTH_MINOR_AXIS_RADIUS * EARTH_MINOR_AXIS_RADIUS; + double altitudeKm = altitude / CONVERSION_FACTOR; + double a2 = EARTH_MAJOR_AXIS_RADIUS * EARTH_MAJOR_AXIS_RADIUS; + double b2 = EARTH_MINOR_AXIS_RADIUS * EARTH_MINOR_AXIS_RADIUS; double gdLatRad = ToRadians(latitude); - float clat = static_cast(cos(gdLatRad)); - float slat = static_cast(sin(gdLatRad)); - float tlat = slat / clat; - float latRad = static_cast(sqrt(a2 * clat * clat + b2 * slat * slat)); - geocentricLatitude = static_cast(atan(tlat * (latRad * altitudeKm + b2) + double clat = static_cast(cos(gdLatRad)); + double slat = static_cast(sin(gdLatRad)); + double tlat = slat / clat; + double latRad = static_cast(sqrt(a2 * clat * clat + b2 * slat * slat)); + geocentricLatitude = static_cast(atan(tlat * (latRad * altitudeKm + b2) / (latRad * altitudeKm + a2))); - geocentricLongitude = static_cast(ToRadians(longitude)); - float radSq = altitudeKm * altitudeKm + 2 * altitudeKm + geocentricLongitude = static_cast(ToRadians(longitude)); + double radSq = altitudeKm * altitudeKm + 2 * altitudeKm * latRad + (a2 * a2 * clat * clat + b2 * b2 * slat * slat) / (a2 * clat * clat + b2 * slat * slat); - geocentricRadius = static_cast(sqrt(radSq)); + geocentricRadius = static_cast(sqrt(radSq)); } -void GeomagneticField::InitLegendreTable(int32_t expansionDegree, float thetaRad) +void GeomagneticField::InitLegendreTable(int32_t expansionDegree, double thetaRad) { polynomials[0].resize(1); polynomials[0][0] = 1.0f; polynomialsDerivative[0].resize(1); polynomialsDerivative[0][0] = 0.0f; - float cosValue = static_cast(cos(thetaRad)); - float sinValue = static_cast(sin(thetaRad)); + double cosValue = static_cast(cos(thetaRad)); + double sinValue = static_cast(sin(thetaRad)); for (int32_t row = 1; row <= expansionDegree; row++) { polynomials[row].resize(row + 1); polynomialsDerivative[row].resize(row + 1); @@ -237,8 +237,8 @@ void GeomagneticField::InitLegendreTable(int32_t expansionDegree, float thetaRad polynomialsDerivative[row][column] = -sinValue * polynomials[row - 1][column] + cosValue * polynomialsDerivative[row - 1][column]; } else { - float k = ((row - 1) * (row - 1) - column * column) - / static_cast((2 * row - 1) * (2 * row - 3)); + double k = ((row - 1) * (row - 1) - column * column) + / static_cast((2 * row - 1) * (2 * row - 3)); polynomials[row][column] = cosValue * polynomials[row - 1][column] - k * polynomials[row - 2][column]; polynomialsDerivative[row][column] = -sinValue * polynomials[row - 1][column] @@ -249,29 +249,29 @@ void GeomagneticField::InitLegendreTable(int32_t expansionDegree, float thetaRad } } -float GeomagneticField::ObtainX() +double GeomagneticField::ObtainX() { std::lock_guard geomagneticLock(mutex_); return northComponent; } -float GeomagneticField::ObtainY() +double GeomagneticField::ObtainY() { std::lock_guard geomagneticLock(mutex_); return eastComponent; } -float GeomagneticField::ObtainZ() +double GeomagneticField::ObtainZ() { std::lock_guard geomagneticLock(mutex_); return downComponent; } -float GeomagneticField::ObtainGeomagneticDip() +double GeomagneticField::ObtainGeomagneticDip() { std::lock_guard geomagneticLock(mutex_); - float horizontalIntensity = hypot(northComponent, eastComponent); - return static_cast(ToDegrees(atan2(downComponent, horizontalIntensity))); + double horizontalIntensity = hypot(northComponent, eastComponent); + return static_cast(ToDegrees(atan2(downComponent, horizontalIntensity))); } double GeomagneticField::ToDegrees(double angrad) @@ -284,25 +284,25 @@ double GeomagneticField::ToRadians(double angdeg) return angdeg / 180.0 * M_PI; } -float GeomagneticField::ObtainDeflectionAngle() +double GeomagneticField::ObtainDeflectionAngle() { std::lock_guard geomagneticLock(mutex_); - return static_cast(ToDegrees(atan2(eastComponent, northComponent))); + return static_cast(ToDegrees(atan2(eastComponent, northComponent))); } -float GeomagneticField::ObtainLevelIntensity() +double GeomagneticField::ObtainLevelIntensity() { std::lock_guard geomagneticLock(mutex_); - float horizontalIntensity = hypot(northComponent, eastComponent); + double horizontalIntensity = hypot(northComponent, eastComponent); return horizontalIntensity; } -float GeomagneticField::ObtainTotalIntensity() +double GeomagneticField::ObtainTotalIntensity() { std::lock_guard geomagneticLock(mutex_); - float sumOfSquares = northComponent * northComponent + eastComponent * eastComponent + double sumOfSquares = northComponent * northComponent + eastComponent * eastComponent + downComponent * downComponent; - float totalIntensity = static_cast(sqrt(sumOfSquares)); + double totalIntensity = static_cast(sqrt(sumOfSquares)); return totalIntensity; } diff --git a/interfaces/native/src/sensor_algorithm.cpp b/interfaces/native/src/sensor_algorithm.cpp index 30fbc963..98208d3d 100644 --- a/interfaces/native/src/sensor_algorithm.cpp +++ b/interfaces/native/src/sensor_algorithm.cpp @@ -24,7 +24,7 @@ using OHOS::HiviewDFX::HiLogLabel; static constexpr HiLogLabel LABEL = {LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorAlgorithmAPI"}; -int32_t SensorAlgorithm::CreateQuaternion(std::vector rotationVector, std::vector &quaternion) +int32_t SensorAlgorithm::CreateQuaternion(std::vector rotationVector, std::vector &quaternion) { if (static_cast(rotationVector.size()) < ROTATION_VECTOR_LENGTH || static_cast(rotationVector.size()) > QUATERNION_LENGTH) { @@ -36,9 +36,9 @@ int32_t SensorAlgorithm::CreateQuaternion(std::vector rotationVector, std return OHOS::Sensors::PARAMETER_ERROR; } if (static_cast(rotationVector.size()) == ROTATION_VECTOR_LENGTH) { - quaternion[0] = 1 - static_cast((pow(rotationVector[0], 2) + pow(rotationVector[1], 2) + quaternion[0] = 1 - static_cast((pow(rotationVector[0], 2) + pow(rotationVector[1], 2) + pow(rotationVector[2], 2))); - quaternion[0] = (quaternion[0] > 0) ? static_cast(std::sqrt(quaternion[0])) : 0; + quaternion[0] = (quaternion[0] > 0) ? static_cast(std::sqrt(quaternion[0])) : 0; } else { quaternion[0] = rotationVector[3]; } @@ -48,8 +48,8 @@ int32_t SensorAlgorithm::CreateQuaternion(std::vector rotationVector, std return OHOS::Sensors::SUCCESS; } -int32_t SensorAlgorithm::TransformCoordinateSystemImpl(std::vector inRotationMatrix, int32_t axisX, - int32_t axisY, std::vector &outRotationMatrix) +int32_t SensorAlgorithm::TransformCoordinateSystemImpl(std::vector inRotationMatrix, int32_t axisX, + int32_t axisY, std::vector &outRotationMatrix) { if ((axisX & 0x7C) != 0 || (axisX & 0x3) == 0) { SEN_HILOGE("axisX is invalid parameter"); @@ -91,8 +91,8 @@ int32_t SensorAlgorithm::TransformCoordinateSystemImpl(std::vector inRota return OHOS::Sensors::SUCCESS; } -int32_t SensorAlgorithm::TransformCoordinateSystem(std::vector inRotationMatrix, int32_t axisX, int32_t axisY, - std::vector &outRotationMatrix) +int32_t SensorAlgorithm::TransformCoordinateSystem(std::vector inRotationMatrix, int32_t axisX, int32_t axisY, + std::vector &outRotationMatrix) { int32_t inRotationMatrixLength = static_cast(inRotationMatrix.size()); if (((inRotationMatrixLength != THREE_DIMENSIONAL_MATRIX_LENGTH) && (inRotationMatrixLength != FOUR_DIMENSIONAL_MATRIX_LENGTH)) @@ -101,7 +101,7 @@ int32_t SensorAlgorithm::TransformCoordinateSystem(std::vector inRotation return OHOS::Sensors::PARAMETER_ERROR; } if (inRotationMatrix == outRotationMatrix) { - std::vector tempRotationMatrix(inRotationMatrixLength); + std::vector tempRotationMatrix(inRotationMatrixLength); if (TransformCoordinateSystemImpl(inRotationMatrix, axisX, axisY, tempRotationMatrix) != OHOS::Sensors::SUCCESS) { SEN_HILOGE("TransformCoordinateSystemImpl failed"); return OHOS::Sensors::PARAMETER_ERROR; @@ -114,20 +114,20 @@ int32_t SensorAlgorithm::TransformCoordinateSystem(std::vector inRotation return TransformCoordinateSystemImpl(inRotationMatrix, axisX, axisY, outRotationMatrix); } -int32_t SensorAlgorithm::GetAltitude(float seaPressure, float currentPressure, float *altitude) +int32_t SensorAlgorithm::GetAltitude(double seaPressure, double currentPressure, double *altitude) { if (altitude == nullptr) { SEN_HILOGE("invalid parameter"); return OHOS::Sensors::PARAMETER_ERROR; } - float coef = 1.0f / RECIPROCAL_COEFFICIENT; - float rationOfStandardPressure = currentPressure / seaPressure; - float difference = pow(rationOfStandardPressure, coef); + double coef = 1.0f / RECIPROCAL_COEFFICIENT; + double rationOfStandardPressure = currentPressure / seaPressure; + double difference = pow(rationOfStandardPressure, coef); *altitude = ZERO_PRESSURE_ALTITUDE * (1.0f - difference); return OHOS::Sensors::SUCCESS; } -int32_t SensorAlgorithm::GetGeomagneticDip(std::vector inclinationMatrix, float *geomagneticDip) +int32_t SensorAlgorithm::GetGeomagneticDip(std::vector inclinationMatrix, double *geomagneticDip) { if (geomagneticDip == nullptr) { SEN_HILOGE("invalid parameter"); @@ -146,8 +146,8 @@ int32_t SensorAlgorithm::GetGeomagneticDip(std::vector inclinationMatrix, return OHOS::Sensors::SUCCESS; } -int32_t SensorAlgorithm::GetAngleModify(std::vector curRotationMatrix, std::vector preRotationMatrix, - std::vector &angleChange) +int32_t SensorAlgorithm::GetAngleModify(std::vector curRotationMatrix, std::vector preRotationMatrix, + std::vector &angleChange) { if (static_cast(angleChange.size()) < ROTATION_VECTOR_LENGTH) { SEN_HILOGE("invalid parameter"); @@ -165,8 +165,8 @@ int32_t SensorAlgorithm::GetAngleModify(std::vector curRotationMatrix, st SEN_HILOGE("Invalid input currotationMatrix parameter"); return OHOS::Sensors::PARAMETER_ERROR; } - float curMatrix[THREE_DIMENSIONAL_MATRIX_LENGTH] = {0}; - float preMatrix[THREE_DIMENSIONAL_MATRIX_LENGTH] = {0}; + double curMatrix[THREE_DIMENSIONAL_MATRIX_LENGTH] = {0}; + double preMatrix[THREE_DIMENSIONAL_MATRIX_LENGTH] = {0}; int32_t curmatrixDimension = ((curRotationMatrixLength == FOUR_DIMENSIONAL_MATRIX_LENGTH) ? QUATERNION_LENGTH : ROTATION_VECTOR_LENGTH); int32_t prematrixDimension = ((preRotationMatrixLength == FOUR_DIMENSIONAL_MATRIX_LENGTH) @@ -177,19 +177,19 @@ int32_t SensorAlgorithm::GetAngleModify(std::vector curRotationMatrix, st int32_t preMatrixIndex = i % ROTATION_VECTOR_LENGTH + (i / ROTATION_VECTOR_LENGTH) * prematrixDimension; preMatrix[i] = preRotationMatrix[preMatrixIndex]; } - float radian[THREE_DIMENSIONAL_MATRIX_LENGTH] = {0}; + double radian[THREE_DIMENSIONAL_MATRIX_LENGTH] = {0}; radian[1] = preMatrix[0] * curMatrix[1] + preMatrix[3] * curMatrix[4] + preMatrix[6] * curMatrix[7]; radian[4] = preMatrix[1] * curMatrix[1] + preMatrix[4] * curMatrix[4] + preMatrix[7] * curMatrix[7]; radian[6] = preMatrix[2] * curMatrix[0] + preMatrix[5] * curMatrix[3] + preMatrix[8] * curMatrix[6]; radian[7] = preMatrix[2] * curMatrix[1] + preMatrix[5] * curMatrix[4] + preMatrix[8] * curMatrix[7]; radian[8] = preMatrix[2] * curMatrix[2] + preMatrix[5] * curMatrix[5] + preMatrix[8] * curMatrix[8]; - angleChange[0] = static_cast(std::atan2(radian[1], radian[4])); - angleChange[1] = static_cast(std::asin(-radian[7])); - angleChange[2] = static_cast(std::atan2(-radian[6], radian[8])); + angleChange[0] = static_cast(std::atan2(radian[1], radian[4])); + angleChange[1] = static_cast(std::asin(-radian[7])); + angleChange[2] = static_cast(std::atan2(-radian[6], radian[8])); return OHOS::Sensors::SUCCESS; } -int32_t SensorAlgorithm::GetDirection(std::vector rotationMatrix, std::vector &rotationAngle) +int32_t SensorAlgorithm::GetDirection(std::vector rotationMatrix, std::vector &rotationAngle) { if (static_cast(rotationAngle.size()) < ROTATION_VECTOR_LENGTH) { SEN_HILOGE("invalid parameter"); @@ -203,16 +203,16 @@ int32_t SensorAlgorithm::GetDirection(std::vector rotationMatrix, std::ve } int32_t dimension = ((rotationMatrixLength == FOUR_DIMENSIONAL_MATRIX_LENGTH) ? QUATERNION_LENGTH : ROTATION_VECTOR_LENGTH); - rotationAngle[0] = static_cast(std::atan2(rotationMatrix[1], + rotationAngle[0] = static_cast(std::atan2(rotationMatrix[1], rotationMatrix[dimension * 1 + 1])); - rotationAngle[1] = static_cast(std::atan2(-rotationMatrix[2 * dimension + 1], + rotationAngle[1] = static_cast(std::atan2(-rotationMatrix[2 * dimension + 1], std::sqrt(pow(rotationMatrix[1], 2) + pow(rotationMatrix[dimension + 1], 2)))); - rotationAngle[2] = static_cast(std::atan2(-rotationMatrix[2 * dimension], + rotationAngle[2] = static_cast(std::atan2(-rotationMatrix[2 * dimension], rotationMatrix[2 * dimension + 2])); return OHOS::Sensors::SUCCESS; } -int32_t SensorAlgorithm::CreateRotationMatrix(std::vector rotationVector, std::vector &rotationMatrix) +int32_t SensorAlgorithm::CreateRotationMatrix(std::vector rotationVector, std::vector &rotationMatrix) { int32_t rotationMatrixLength = static_cast(rotationMatrix.size()); if ((static_cast(rotationVector.size()) < ROTATION_VECTOR_LENGTH) @@ -221,21 +221,21 @@ int32_t SensorAlgorithm::CreateRotationMatrix(std::vector rotationVector, SEN_HILOGE("Invalid input rotationMatrix parameter"); return OHOS::Sensors::PARAMETER_ERROR; } - std::vector quaternion(4); + std::vector quaternion(4); int32_t ret = CreateQuaternion(rotationVector, quaternion); if (ret != OHOS::Sensors::SUCCESS) { SEN_HILOGE("create quaternion failed"); return ret; } - float squareOfX = 2 * static_cast(pow(quaternion[1], 2)); - float squareOfY = 2 * static_cast(pow(quaternion[2], 2)); - float squareOfZ = 2 * static_cast(pow(quaternion[3], 2)); - float productOfWZ = 2 * quaternion[0] * quaternion[3]; - float productOfXY = 2 * quaternion[1] * quaternion[2]; - float productOfWY = 2 * quaternion[0] * quaternion[2]; - float productOfXZ = 2 * quaternion[1] * quaternion[3]; - float productOfWX = 2 * quaternion[0] * quaternion[1]; - float productOfYZ = 2 * quaternion[2] * quaternion[3]; + double squareOfX = 2 * static_cast(pow(quaternion[1], 2)); + double squareOfY = 2 * static_cast(pow(quaternion[2], 2)); + double squareOfZ = 2 * static_cast(pow(quaternion[3], 2)); + double productOfWZ = 2 * quaternion[0] * quaternion[3]; + double productOfXY = 2 * quaternion[1] * quaternion[2]; + double productOfWY = 2 * quaternion[0] * quaternion[2]; + double productOfXZ = 2 * quaternion[1] * quaternion[3]; + double productOfWX = 2 * quaternion[0] * quaternion[1]; + double productOfYZ = 2 * quaternion[2] * quaternion[3]; int32_t rotationMatrixDimension = ((rotationMatrixLength == FOUR_DIMENSIONAL_MATRIX_LENGTH) ? QUATERNION_LENGTH : ROTATION_VECTOR_LENGTH); rotationMatrix[0] = 1 - squareOfY - squareOfZ; @@ -261,39 +261,39 @@ int32_t SensorAlgorithm::CreateRotationMatrix(std::vector rotationVector, return OHOS::Sensors::SUCCESS; } -int32_t SensorAlgorithm::CreateRotationAndInclination(std::vector gravity, std::vector geomagnetic, - std::vector &rotationMatrix, std::vector &inclinationMatrix) +int32_t SensorAlgorithm::CreateRotationAndInclination(std::vector gravity, std::vector geomagnetic, + std::vector &rotationMatrix, std::vector &inclinationMatrix) { if (static_cast(gravity.size()) < ROTATION_VECTOR_LENGTH || static_cast(geomagnetic.size()) < ROTATION_VECTOR_LENGTH) { SEN_HILOGE("Invalid input parameter"); return OHOS::Sensors::PARAMETER_ERROR; } - float totalGravity = pow(gravity[0], 2) + pow(gravity[1], 2) + pow(gravity[2], 2); + double totalGravity = pow(gravity[0], 2) + pow(gravity[1], 2) + pow(gravity[2], 2); if (totalGravity < (0.01f * pow(GRAVITATIONAL_ACCELERATION, 2))) { SEN_HILOGE("Invalid input gravity parameter"); return OHOS::Sensors::PARAMETER_ERROR; } - std::vector componentH(3); + std::vector componentH(3); componentH[0] = geomagnetic[1] * gravity[2] - geomagnetic[2] * gravity[1]; componentH[1] = geomagnetic[2] * gravity[0] - geomagnetic[0] * gravity[2]; componentH[2] = geomagnetic[0] * gravity[1] - geomagnetic[1] * gravity[0]; - float totalH = static_cast(std::sqrt(pow(componentH[0], 2) + pow(componentH[1], 2) + double totalH = static_cast(std::sqrt(pow(componentH[0], 2) + pow(componentH[1], 2) + pow(componentH[2], 2))); if (totalH < 0.1f) { SEN_HILOGE("The total strength of H is less than 0.1"); return OHOS::Sensors::PARAMETER_ERROR; } - float reciprocalH = 1.0f / totalH; + double reciprocalH = 1.0f / totalH; componentH[0] *= reciprocalH; componentH[1] *= reciprocalH; componentH[2] *= reciprocalH; - float reciprocalA = 1.0f / static_cast(std::sqrt(totalGravity)); + double reciprocalA = 1.0f / static_cast(std::sqrt(totalGravity)); gravity[0] *= reciprocalA; gravity[1] *= reciprocalA; gravity[2] *= reciprocalA; - std::vector measuredValue(3); + std::vector measuredValue(3); measuredValue[0] = gravity[1] * componentH[2] - gravity[2] * componentH[1]; measuredValue[1] = gravity[2] * componentH[0] - gravity[0] * componentH[2]; measuredValue[2] = gravity[0] * componentH[1] - gravity[1] * componentH[0]; @@ -304,11 +304,11 @@ int32_t SensorAlgorithm::CreateRotationAndInclination(std::vector gravity SEN_HILOGE("Invalid input parameter"); return OHOS::Sensors::PARAMETER_ERROR; } - float reciprocalE = 1.0f / static_cast(std::sqrt(pow(geomagnetic[0], 2) + pow(geomagnetic[1], 2) + double reciprocalE = 1.0f / static_cast(std::sqrt(pow(geomagnetic[0], 2) + pow(geomagnetic[1], 2) + pow(geomagnetic[2], 2))); - float c = (geomagnetic[0] * measuredValue[0] + geomagnetic[1] * measuredValue[1] + double c = (geomagnetic[0] * measuredValue[0] + geomagnetic[1] * measuredValue[1] + geomagnetic[2] * measuredValue[2]) * reciprocalE; - float s = (geomagnetic[0] * gravity[0] + geomagnetic[1] * gravity[1] + geomagnetic[2] * gravity[2]) * reciprocalE; + double s = (geomagnetic[0] * gravity[0] + geomagnetic[1] * gravity[1] + geomagnetic[2] * gravity[2]) * reciprocalE; int32_t rotationMatrixDimension = ((rotationMatrixLength == FOUR_DIMENSIONAL_MATRIX_LENGTH) ? QUATERNION_LENGTH : ROTATION_VECTOR_LENGTH); diff --git a/interfaces/native/test/unittest/sensor_algorithm_test.cpp b/interfaces/native/test/unittest/sensor_algorithm_test.cpp index ee74b3cd..34f5cd24 100755 --- a/interfaces/native/test/unittest/sensor_algorithm_test.cpp +++ b/interfaces/native/test/unittest/sensor_algorithm_test.cpp @@ -29,7 +29,7 @@ constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "Sens constexpr int32_t QUATERNION_LENGTH = 4; constexpr int32_t ROTATION_VECTOR_LENGTH = 3; constexpr int32_t THREE_DIMENSIONAL_MATRIX_LENGTH = 9; -constexpr float EPS = 0.01; +constexpr double EPS = 0.01; } // namespace class SensorAlgorithmTest : public testing::Test { @@ -53,12 +53,12 @@ SensorAlgorithm sensorAlgorithm; HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_001, TestSize.Level1) { SEN_HILOGI("SensorAlgorithmTest_001 in"); - std::vector rotationVector = {0.52, -0.336, -0.251}; - std::vector quaternion(QUATERNION_LENGTH); + std::vector rotationVector = {0.52, -0.336, -0.251}; + std::vector quaternion(QUATERNION_LENGTH); int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); ASSERT_EQ(quaternion.size(), QUATERNION_LENGTH); - std::vector result = {0.7441122531890869, 0.5199999809265137, -0.335999995470047, -0.25099998712539673}; + std::vector result = {0.7441122531890869, 0.5199999809265137, -0.335999995470047, -0.25099998712539673}; for (size_t i = 0; i < QUATERNION_LENGTH; ++i) { ASSERT_EQ(quaternion[i], result[i]); } @@ -67,8 +67,8 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_001, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_002, TestSize.Level1) { SEN_HILOGI("SensorAlgorithmTest_002 in"); - std::vector rotationVector = {}; - std::vector quaternion(QUATERNION_LENGTH); + std::vector rotationVector = {}; + std::vector quaternion(QUATERNION_LENGTH); int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } @@ -76,8 +76,8 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_002, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_003, TestSize.Level1) { SEN_HILOGI("SensorAlgorithmTest_003 in"); - std::vector rotationVector = {0.52, -0.336, -0.251}; - std::vector quaternion(ROTATION_VECTOR_LENGTH - 1); + std::vector rotationVector = {0.52, -0.336, -0.251}; + std::vector quaternion(ROTATION_VECTOR_LENGTH - 1); int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } @@ -85,11 +85,11 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_003, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_004, TestSize.Level1) { SEN_HILOGI("SensorAlgorithmTest_004 in"); - std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; - std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, 2, outRotationMatrix); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); - std::vector result = + std::vector result = {1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000}; ASSERT_EQ(outRotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { @@ -100,8 +100,8 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_004, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_005, TestSize.Level1) { SEN_HILOGI("SensorAlgorithmTest_005 in"); - std::vector inRotationMatrix(3); - std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inRotationMatrix(3); + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, 2, outRotationMatrix); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } @@ -109,8 +109,8 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_005, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_006, TestSize.Level1) { SEN_HILOGI("SensorAlgorithmTest_006 in"); - std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; - std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, -1, outRotationMatrix); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } @@ -118,15 +118,15 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_006, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_007, TestSize.Level1) { SEN_HILOGI("SensorAlgorithmTest_007 in"); - std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; - std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, -1, 1, outRotationMatrix); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_008, TestSize.Level1) { - float altitude = -1.0; + double altitude = -1.0; int32_t ret = sensorAlgorithm.GetAltitude(5.0, 0.0, &altitude); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); ASSERT_EQ(altitude, 44330.0); @@ -140,8 +140,8 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_009, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_010, TestSize.Level1) { - float geomagneticDip = -1.0; - std::vector inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + double geomagneticDip = -1.0; + std::vector inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); ASSERT_EQ(geomagneticDip, 0.8760581016540527); @@ -149,30 +149,30 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_010, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_011, TestSize.Level1) { - std::vector inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + std::vector inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, nullptr); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_012, TestSize.Level1) { - std::vector inclinationMatrix(3); - float geomagneticDip = -1.0; + std::vector inclinationMatrix(3); + double geomagneticDip = -1.0; int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_013, TestSize.Level1) { - std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; - std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; - std::vector angleChange(ROTATION_VECTOR_LENGTH); + std::vector angleChange(ROTATION_VECTOR_LENGTH); int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); ASSERT_EQ(angleChange.size(), ROTATION_VECTOR_LENGTH); - std::vector result = {0.0, -0.0, -0.0}; + std::vector result = {0.0, -0.0, -0.0}; for (size_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) { ASSERT_EQ(angleChange[i], result[i]); } @@ -180,42 +180,42 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_013, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_014, TestSize.Level1) { - std::vector currotationMatrix(3); - std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + std::vector currotationMatrix(3); + std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; - std::vector angleChange(ROTATION_VECTOR_LENGTH); + std::vector angleChange(ROTATION_VECTOR_LENGTH); int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_015, TestSize.Level1) { - std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; - std::vector preRotationMatrix(ROTATION_VECTOR_LENGTH); - std::vector angleChange(ROTATION_VECTOR_LENGTH); + std::vector preRotationMatrix(ROTATION_VECTOR_LENGTH); + std::vector angleChange(ROTATION_VECTOR_LENGTH); int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_016, TestSize.Level1) { - std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; - std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; - std::vector angleChange(ROTATION_VECTOR_LENGTH - 1); + std::vector angleChange(ROTATION_VECTOR_LENGTH - 1); int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_017, TestSize.Level1) { - std::vector rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; - std::vector rotationAngle(ROTATION_VECTOR_LENGTH); + std::vector rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + std::vector rotationAngle(ROTATION_VECTOR_LENGTH); int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); - std::vector result = {0.38050639629364014, -0.9783217310905457, -0.6610431671142578}; + std::vector result = {0.38050639629364014, -0.9783217310905457, -0.6610431671142578}; ASSERT_EQ(rotationAngle.size(), ROTATION_VECTOR_LENGTH); for (size_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) { ASSERT_EQ(rotationAngle[i], result[i]); @@ -224,27 +224,27 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_017, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_018, TestSize.Level1) { - std::vector rotationMatrix(5); - std::vector rotationAngle(ROTATION_VECTOR_LENGTH); + std::vector rotationMatrix(5); + std::vector rotationAngle(ROTATION_VECTOR_LENGTH); int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_019, TestSize.Level1) { - std::vector rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; - std::vector rotationAngle(ROTATION_VECTOR_LENGTH - 1); + std::vector rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + std::vector rotationAngle(ROTATION_VECTOR_LENGTH - 1); int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_020, TestSize.Level1) { - std::vector rotationVector = {0.0, 0.0, 0.0}; - std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector rotationVector = {0.0, 0.0, 0.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); - std::vector result = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; + std::vector result = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; ASSERT_EQ(rotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { ASSERT_EQ(rotationMatrix[i], result[i]); @@ -253,34 +253,34 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_020, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_021, TestSize.Level1) { - std::vector rotationVector(ROTATION_VECTOR_LENGTH - 1); - std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector rotationVector(ROTATION_VECTOR_LENGTH - 1); + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_022, TestSize.Level1) { - std::vector rotationVector = {0.0, 0.0, 0.0}; - std::vector rotationMatrix(ROTATION_VECTOR_LENGTH - 1); + std::vector rotationVector = {0.0, 0.0, 0.0}; + std::vector rotationMatrix(ROTATION_VECTOR_LENGTH - 1); int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_023, TestSize.Level1) { - std::vector gravity = {9.0, 9.0, 9.0}; - std::vector geomagnetic = {30.0, 25.0, 41.0}; - std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); - std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); ASSERT_EQ(rotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); ASSERT_EQ(inclinationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); - std::vector rotationMatrixResult = + std::vector rotationMatrixResult = {-0.7980074882507324, 0.5486301183700562, 0.24937734007835388, -0.17277367413043976, -0.6047078967094421, 0.7774815559387207, 0.5773502588272095, 0.5773502588272095, 0.5773502588272095}; - std::vector inclinationMatrixResult = {1.0, 0.0, 0.0, 0.0, 0.20444221794605255, + std::vector inclinationMatrixResult = {1.0, 0.0, 0.0, 0.0, 0.20444221794605255, 0.9788785576820374, 0, -0.9788785576820374, 0.20444221794605255}; for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { ASSERT_EQ(rotationMatrix[i], rotationMatrixResult[i]); @@ -290,40 +290,40 @@ HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_023, TestSize.Level1) HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_024, TestSize.Level1) { - std::vector gravity(ROTATION_VECTOR_LENGTH - 1); - std::vector geomagnetic = {30.0, 25.0, 41.0}; - std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); - std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector gravity(ROTATION_VECTOR_LENGTH - 1); + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_025, TestSize.Level1) { - std::vector gravity = {9.0, 9.0, 9.0}; - std::vector geomagnetic(ROTATION_VECTOR_LENGTH - 1); - std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); - std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic(ROTATION_VECTOR_LENGTH - 1); + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_026, TestSize.Level1) { - std::vector gravity = {9.0, 9.0, 9.0}; - std::vector geomagnetic = {30.0, 25.0, 41.0}; - std::vector rotationMatrix(ROTATION_VECTOR_LENGTH - 1); - std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(ROTATION_VECTOR_LENGTH - 1); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_027, TestSize.Level1) { - std::vector gravity = {9.0, 9.0, 9.0}; - std::vector geomagnetic = {30.0, 25.0, 41.0}; - std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); - std::vector inclinationMatrix(ROTATION_VECTOR_LENGTH - 1); + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(ROTATION_VECTOR_LENGTH - 1); int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); } diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h index ca4549ea..190b8115 100644 --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -34,6 +34,7 @@ bool IsMatchArrayType(const napi_env &env, const napi_value &value); bool GetNativeInt32(const napi_env &env, const napi_value &value, int32_t &number); bool GetNativeDouble(const napi_env &env, const napi_value &value, double &number); bool GetFloatArray(const napi_env &env, const napi_value &value, vector &array); +bool GetDoubleArray(const napi_env &env, const napi_value &value, vector &array); bool GetNativeInt64(const napi_env &env, const napi_value &value, int64_t &number); bool RegisterNapiCallback(const napi_env &env, const napi_value &value, napi_ref &callback); napi_value GetNamedProperty(const napi_env &env, const napi_value &object, string name); diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index c80df7c0..bb2008e4 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -552,8 +552,8 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf ThrowErr(env, PARAMETER_ERROR, "Wrong argument type"); return nullptr; } - std::vector inRotationVector; - if (!GetFloatArray(env, args[0], inRotationVector)) { + std::vector inRotationVector; + if (!GetDoubleArray(env, args[0], inRotationVector)) { ThrowErr(env, PARAMETER_ERROR, "Get inRotationVector fail"); return nullptr; } @@ -585,7 +585,7 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, TRANSFORM_COORDINATE_SYSTEM); CHKPP(asyncCallbackInfo); - std::vector outRotationVector(length); + std::vector outRotationVector(length); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationVector, axisX, axisY, outRotationVector); if (ret != OHOS::ERR_OK) { @@ -642,17 +642,17 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, GET_ANGLE_MODIFY); CHKPP(asyncCallbackInfo); - std::vector curRotationVector; - if (!GetFloatArray(env, args[0], curRotationVector)) { + std::vector curRotationVector; + if (!GetDoubleArray(env, args[0], curRotationVector)) { ThrowErr(env, PARAMETER_ERROR, "Get curRotationVector fail"); return nullptr; } - std::vector preRotationVector; - if (!GetFloatArray(env, args[1], preRotationVector)) { + std::vector preRotationVector; + if (!GetDoubleArray(env, args[1], preRotationVector)) { ThrowErr(env, PARAMETER_ERROR, "Get preRotationVector fail"); return nullptr; } - std::vector angleChange(ROTATION_VECTOR_LENGTH); + std::vector angleChange(ROTATION_VECTOR_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAngleModify(curRotationVector, preRotationVector, angleChange); if (ret != OHOS::ERR_OK) { @@ -705,12 +705,12 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, GET_DIRECTION); CHKPP(asyncCallbackInfo); - std::vector rotationMatrix; - if (!GetFloatArray(env, args[0], rotationMatrix)) { + std::vector rotationMatrix; + if (!GetDoubleArray(env, args[0], rotationMatrix)) { ThrowErr(env, PARAMETER_ERROR, "Get rotationMatrix fail"); return nullptr; } - std::vector rotationAngle(ROTATION_VECTOR_LENGTH); + std::vector rotationAngle(ROTATION_VECTOR_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); if (ret != OHOS::ERR_OK) { @@ -763,12 +763,12 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, CREATE_QUATERNION); CHKPP(asyncCallbackInfo); - std::vector rotationVector; - if (!GetFloatArray(env, args[0], rotationVector)) { + std::vector rotationVector; + if (!GetDoubleArray(env, args[0], rotationVector)) { ThrowErr(env, PARAMETER_ERROR, "Get rotationVector fail"); return nullptr; } - std::vector quaternion(QUATERNION_LENGTH); + std::vector quaternion(QUATERNION_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); if (ret != OHOS::ERR_OK) { @@ -821,17 +821,17 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, GET_ALTITUDE); CHKPP(asyncCallbackInfo); - float seaPressure = 0; - if (!GetNativeFloat(env, args[0], seaPressure)) { + double seaPressure = 0; + if (!GetNativeDouble(env, args[0], seaPressure)) { ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get seaPressure fail"); return nullptr; } - float currentPressure = 0; - if (!GetNativeFloat(env, args[1], currentPressure)) { + double currentPressure = 0; + if (!GetNativeDouble(env, args[1], currentPressure)) { ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get currentPressure fail"); return nullptr; } - float altitude = 0; + double altitude = 0; SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAltitude(seaPressure, currentPressure, &altitude); if (ret != OHOS::ERR_OK) { @@ -881,12 +881,12 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, GET_GEOMAGNETIC_DIP); CHKPP(asyncCallbackInfo); - std::vector inclinationMatrix; - if (!GetFloatArray(env, args[0], inclinationMatrix)) { + std::vector inclinationMatrix; + if (!GetDoubleArray(env, args[0], inclinationMatrix)) { ThrowErr(env, PARAMETER_ERROR, "Get inclinationMatrix fail"); return nullptr; } - float geomagneticDip = 0; + double geomagneticDip = 0; SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); if (ret != OHOS::ERR_OK) { @@ -925,18 +925,18 @@ static napi_value CreateRotationAndInclination(const napi_env &env, napi_value a ThrowErr(env, PARAMETER_ERROR, "The number of parameters is not valid"); return nullptr; } - std::vector gravity; - if (!GetFloatArray(env, args[0], gravity)) { + std::vector gravity; + if (!GetDoubleArray(env, args[0], gravity)) { ThrowErr(env, PARAMETER_ERROR, "Get gravity fail"); return nullptr; } - std::vector geomagnetic; - if (!GetFloatArray(env, args[1], geomagnetic)) { + std::vector geomagnetic; + if (!GetDoubleArray(env, args[1], geomagnetic)) { ThrowErr(env, PARAMETER_ERROR, "Get geomagnetic fail"); return nullptr; } - std::vector rotation(THREE_DIMENSIONAL_MATRIX_LENGTH); - std::vector inclination(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector rotation(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclination(THREE_DIMENSIONAL_MATRIX_LENGTH); sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, ROTATION_INCLINATION_MATRIX); CHKPP(asyncCallbackInfo); @@ -985,15 +985,15 @@ static napi_value GetRotationMatrix(const napi_env &env, napi_value args[], size ThrowErr(env, PARAMETER_ERROR, "The number of parameters is not valid"); return nullptr; } - std::vector rotationVector; - if (!GetFloatArray(env, args[0], rotationVector)) { + std::vector rotationVector; + if (!GetDoubleArray(env, args[0], rotationVector)) { ThrowErr(env, PARAMETER_ERROR, "Get rotationVector fail"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, CREATE_ROTATION_MATRIX); CHKPP(asyncCallbackInfo); - std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); if (ret != OHOS::ERR_OK) { diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index fdfba387..480bf532 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -66,6 +66,22 @@ bool GetFloatArray(const napi_env &env, const napi_value &value, vector & return true; } +bool GetDoubleArray(const napi_env &env, const napi_value &value, vector &array) +{ + CALL_LOG_ENTER; + uint32_t arrayLength = 0; + CHKNRF(env, napi_get_array_length(env, value, &arrayLength), "napi_get_array_length"); + for (size_t i = 0; i < arrayLength; ++i) { + napi_value element = nullptr; + CHKNRF(env, napi_get_element(env, value, i, &element), "napi_get_element"); + CHKNCF(env, IsMatchType(env, element, napi_number), "Wrong argument type. Number or function expected"); + double number = 0; + CHKNCF(env, GetNativeDouble(env, element, number), "Wrong argument type. get double fail"); + array.push_back(number); + } + return true; +} + napi_value GetNamedProperty(const napi_env &env, const napi_value &object, string name) { CALL_LOG_ENTER; -- Gitee