From 94fd499b95de33fd66b89c8d929673da266859ef Mon Sep 17 00:00:00 2001 From: dipterix Date: Tue, 7 Nov 2023 23:00:15 -0500 Subject: [PATCH] added xyzw to quaternion --- R/RcppExports.R | 32 +++++++++++ R/class-Quaternion.R | 30 +++++++++- src/RcppExports.cpp | 96 ++++++++++++++++++++++++++++++++ src/glQuaternion.cpp | 41 ++++++++++++++ tests/testthat/test-quaternion.R | 10 +++- 5 files changed, 206 insertions(+), 3 deletions(-) diff --git a/R/RcppExports.R b/R/RcppExports.R index 4ab12c3..8c69f83 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -205,6 +205,38 @@ Quaternion__to_array <- function(self) { .Call(`_ravetools_Quaternion__to_array`, self) } +Quaternion__getX <- function(self) { + .Call(`_ravetools_Quaternion__getX`, self) +} + +Quaternion__setX <- function(self, v) { + invisible(.Call(`_ravetools_Quaternion__setX`, self, v)) +} + +Quaternion__getY <- function(self) { + .Call(`_ravetools_Quaternion__getY`, self) +} + +Quaternion__setY <- function(self, v) { + invisible(.Call(`_ravetools_Quaternion__setY`, self, v)) +} + +Quaternion__getZ <- function(self) { + .Call(`_ravetools_Quaternion__getZ`, self) +} + +Quaternion__setZ <- function(self, v) { + invisible(.Call(`_ravetools_Quaternion__setZ`, self, v)) +} + +Quaternion__getW <- function(self) { + .Call(`_ravetools_Quaternion__getW`, self) +} + +Quaternion__setW <- function(self, v) { + invisible(.Call(`_ravetools_Quaternion__setW`, self, v)) +} + Quaternion__set_from_axis_angle <- function(self, axis, angle) { invisible(.Call(`_ravetools_Quaternion__set_from_axis_angle`, self, axis, angle)) } diff --git a/R/class-Quaternion.R b/R/class-Quaternion.R index 096147f..f86317c 100644 --- a/R/class-Quaternion.R +++ b/R/class-Quaternion.R @@ -8,7 +8,35 @@ Quaternion <- R6::R6Class( ), active = list( is_quaternion = function() { TRUE }, - pointer = function() { private$.extern_ptr } + pointer = function() { private$.extern_ptr }, + x = function( v ) { + if(!missing(v)) { + v <- as.double(v)[[1]] + Quaternion__setX(private$.extern_ptr, v) + } + Quaternion__getX(private$.extern_ptr) + }, + y = function( v ) { + if(!missing(v)) { + v <- as.double(v)[[1]] + Quaternion__setY(private$.extern_ptr, v) + } + Quaternion__getY(private$.extern_ptr) + }, + z = function( v ) { + if(!missing(v)) { + v <- as.double(v)[[1]] + Quaternion__setZ(private$.extern_ptr, v) + } + Quaternion__getZ(private$.extern_ptr) + }, + w = function( v ) { + if(!missing(v)) { + v <- as.double(v)[[1]] + Quaternion__setW(private$.extern_ptr, v) + } + Quaternion__getW(private$.extern_ptr) + } ), public = list( initialize = function() { diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 34879e5..d888101 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -1071,6 +1071,94 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } +// Quaternion__getX +double Quaternion__getX(const SEXP& self); +RcppExport SEXP _ravetools_Quaternion__getX(SEXP selfSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const SEXP& >::type self(selfSEXP); + rcpp_result_gen = Rcpp::wrap(Quaternion__getX(self)); + return rcpp_result_gen; +END_RCPP +} +// Quaternion__setX +void Quaternion__setX(const SEXP& self, const double& v); +RcppExport SEXP _ravetools_Quaternion__setX(SEXP selfSEXP, SEXP vSEXP) { +BEGIN_RCPP + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const SEXP& >::type self(selfSEXP); + Rcpp::traits::input_parameter< const double& >::type v(vSEXP); + Quaternion__setX(self, v); + return R_NilValue; +END_RCPP +} +// Quaternion__getY +double Quaternion__getY(const SEXP& self); +RcppExport SEXP _ravetools_Quaternion__getY(SEXP selfSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const SEXP& >::type self(selfSEXP); + rcpp_result_gen = Rcpp::wrap(Quaternion__getY(self)); + return rcpp_result_gen; +END_RCPP +} +// Quaternion__setY +void Quaternion__setY(const SEXP& self, const double& v); +RcppExport SEXP _ravetools_Quaternion__setY(SEXP selfSEXP, SEXP vSEXP) { +BEGIN_RCPP + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const SEXP& >::type self(selfSEXP); + Rcpp::traits::input_parameter< const double& >::type v(vSEXP); + Quaternion__setY(self, v); + return R_NilValue; +END_RCPP +} +// Quaternion__getZ +double Quaternion__getZ(const SEXP& self); +RcppExport SEXP _ravetools_Quaternion__getZ(SEXP selfSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const SEXP& >::type self(selfSEXP); + rcpp_result_gen = Rcpp::wrap(Quaternion__getZ(self)); + return rcpp_result_gen; +END_RCPP +} +// Quaternion__setZ +void Quaternion__setZ(const SEXP& self, const double& v); +RcppExport SEXP _ravetools_Quaternion__setZ(SEXP selfSEXP, SEXP vSEXP) { +BEGIN_RCPP + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const SEXP& >::type self(selfSEXP); + Rcpp::traits::input_parameter< const double& >::type v(vSEXP); + Quaternion__setZ(self, v); + return R_NilValue; +END_RCPP +} +// Quaternion__getW +double Quaternion__getW(const SEXP& self); +RcppExport SEXP _ravetools_Quaternion__getW(SEXP selfSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const SEXP& >::type self(selfSEXP); + rcpp_result_gen = Rcpp::wrap(Quaternion__getW(self)); + return rcpp_result_gen; +END_RCPP +} +// Quaternion__setW +void Quaternion__setW(const SEXP& self, const double& v); +RcppExport SEXP _ravetools_Quaternion__setW(SEXP selfSEXP, SEXP vSEXP) { +BEGIN_RCPP + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const SEXP& >::type self(selfSEXP); + Rcpp::traits::input_parameter< const double& >::type v(vSEXP); + Quaternion__setW(self, v); + return R_NilValue; +END_RCPP +} // Quaternion__set_from_axis_angle void Quaternion__set_from_axis_angle(const SEXP& self, const SEXP& axis, const double& angle); RcppExport SEXP _ravetools_Quaternion__set_from_axis_angle(SEXP selfSEXP, SEXP axisSEXP, SEXP angleSEXP) { @@ -3581,6 +3669,14 @@ static const R_CallMethodDef CallEntries[] = { {"_ravetools_Quaternion__set", (DL_FUNC) &_ravetools_Quaternion__set, 5}, {"_ravetools_Quaternion__copy", (DL_FUNC) &_ravetools_Quaternion__copy, 2}, {"_ravetools_Quaternion__to_array", (DL_FUNC) &_ravetools_Quaternion__to_array, 1}, + {"_ravetools_Quaternion__getX", (DL_FUNC) &_ravetools_Quaternion__getX, 1}, + {"_ravetools_Quaternion__setX", (DL_FUNC) &_ravetools_Quaternion__setX, 2}, + {"_ravetools_Quaternion__getY", (DL_FUNC) &_ravetools_Quaternion__getY, 1}, + {"_ravetools_Quaternion__setY", (DL_FUNC) &_ravetools_Quaternion__setY, 2}, + {"_ravetools_Quaternion__getZ", (DL_FUNC) &_ravetools_Quaternion__getZ, 1}, + {"_ravetools_Quaternion__setZ", (DL_FUNC) &_ravetools_Quaternion__setZ, 2}, + {"_ravetools_Quaternion__getW", (DL_FUNC) &_ravetools_Quaternion__getW, 1}, + {"_ravetools_Quaternion__setW", (DL_FUNC) &_ravetools_Quaternion__setW, 2}, {"_ravetools_Quaternion__set_from_axis_angle", (DL_FUNC) &_ravetools_Quaternion__set_from_axis_angle, 3}, {"_ravetools_Quaternion__set_from_rotation_matrix", (DL_FUNC) &_ravetools_Quaternion__set_from_rotation_matrix, 2}, {"_ravetools_Quaternion__set_from_unit_vectors", (DL_FUNC) &_ravetools_Quaternion__set_from_unit_vectors, 3}, diff --git a/src/glQuaternion.cpp b/src/glQuaternion.cpp index 7e4e125..05a7ff6 100644 --- a/src/glQuaternion.cpp +++ b/src/glQuaternion.cpp @@ -102,6 +102,47 @@ SEXP Quaternion__to_array(const SEXP& self) { return re; } +// [[Rcpp::export]] +double Quaternion__getX(const SEXP& self) { + Rcpp::XPtr ptr(self); + return ptr->x; +} +// [[Rcpp::export]] +void Quaternion__setX(const SEXP& self, const double& v) { + Rcpp::XPtr ptr(self); + ptr->x = v; +} +// [[Rcpp::export]] +double Quaternion__getY(const SEXP& self) { + Rcpp::XPtr ptr(self); + return ptr->y; +} +// [[Rcpp::export]] +void Quaternion__setY(const SEXP& self, const double& v) { + Rcpp::XPtr ptr(self); + ptr->y = v; +} +// [[Rcpp::export]] +double Quaternion__getZ(const SEXP& self) { + Rcpp::XPtr ptr(self); + return ptr->z; +} +// [[Rcpp::export]] +void Quaternion__setZ(const SEXP& self, const double& v) { + Rcpp::XPtr ptr(self); + ptr->z = v; +} +// [[Rcpp::export]] +double Quaternion__getW(const SEXP& self) { + Rcpp::XPtr ptr(self); + return ptr->w; +} +// [[Rcpp::export]] +void Quaternion__setW(const SEXP& self, const double& v) { + Rcpp::XPtr ptr(self); + ptr->w = v; +} + // Quaternion& Quaternion::setFromEuler(const Euler& euler, bool update) { // // const auto x = euler.x(), y = euler.y(), z = euler.z(); diff --git a/tests/testthat/test-quaternion.R b/tests/testthat/test-quaternion.R index e717ec5..9da5035 100644 --- a/tests/testthat/test-quaternion.R +++ b/tests/testthat/test-quaternion.R @@ -90,11 +90,17 @@ test_that("Quaternion", { # new THREE.Quaternion().set(1,2,3,4).premultiply(new THREE.Quaternion().set(4,3,2,1)).toArray() q$set(1,2,3,4) q1$set(4,3,2,1) - q$premultiply(q1) + expect_equal(q[], 1:4) + expect_equal(q1[], 4:1) + + expect_equal(c(q$x, q$y, q$z, q$w), 1:4) + expect_equal(c(q1$x, q1$y, q1$z, q1$w), 4:1) + + q$premultiply(q1) if(!all(q[] == c(22, 4, 16, -12))) { print(q) - stop(format(q)) + stop(format(q), "\n", q$x, " ", q$y, " ", q$z, " ", q$w) } expect_equal(