cmsisdsp_quaternion.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Python Wrapper
  3. * Title: cmsismodule.h
  4. * Description: C code for the CMSIS-DSP Python wrapper
  5. *
  6. * $Date: 27 April 2021
  7. * $Revision: V1.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #define MODNAME "cmsisdsp_quaternion"
  29. #define MODINITNAME cmsisdsp_quaternion
  30. #include "cmsisdsp_module.h"
  31. NUMPYVECTORFROMBUFFER(f32,float32_t,NPY_FLOAT);
  32. void typeRegistration(PyObject *module) {
  33. }
  34. static PyObject *
  35. cmsis_arm_quaternion_product_f32(PyObject *obj, PyObject *args)
  36. {
  37. PyObject *pSrcA=NULL; // input
  38. float32_t *pSrcA_converted=NULL; // input
  39. PyObject *pSrcB=NULL; // input
  40. float32_t *pSrcB_converted=NULL; // input
  41. float32_t *pDst=NULL; // output
  42. uint32_t nbQuaternions; // input
  43. if (PyArg_ParseTuple(args,"OO",&pSrcA,&pSrcB))
  44. {
  45. GETARGUMENT(pSrcA,NPY_DOUBLE,double,float32_t);
  46. GETARGUMENT(pSrcB,NPY_DOUBLE,double,float32_t);
  47. nbQuaternions = arraySizepSrcA / 4 ;
  48. pDst=PyMem_Malloc(4*sizeof(float32_t)*nbQuaternions);
  49. arm_quaternion_product_f32(pSrcA_converted,pSrcB_converted,pDst,nbQuaternions);
  50. FLOATARRAY1(pDstOBJ,4*nbQuaternions,pDst);
  51. PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
  52. FREEARGUMENT(pSrcA_converted);
  53. FREEARGUMENT(pSrcB_converted);
  54. Py_DECREF(pDstOBJ);
  55. return(pythonResult);
  56. }
  57. return(NULL);
  58. }
  59. static PyObject *
  60. cmsis_arm_quaternion_product_single_f32(PyObject *obj, PyObject *args)
  61. {
  62. PyObject *pSrcA=NULL; // input
  63. float32_t *pSrcA_converted=NULL; // input
  64. PyObject *pSrcB=NULL; // input
  65. float32_t *pSrcB_converted=NULL; // input
  66. float32_t *pDst=NULL; // output
  67. if (PyArg_ParseTuple(args,"OO",&pSrcA,&pSrcB))
  68. {
  69. GETARGUMENT(pSrcA,NPY_DOUBLE,double,float32_t);
  70. GETARGUMENT(pSrcB,NPY_DOUBLE,double,float32_t);
  71. pDst=PyMem_Malloc(4*sizeof(float32_t));
  72. arm_quaternion_product_single_f32(pSrcA_converted,pSrcB_converted,pDst);
  73. FLOATARRAY1(pDstOBJ,4,pDst);
  74. PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
  75. FREEARGUMENT(pSrcA_converted);
  76. FREEARGUMENT(pSrcB_converted);
  77. Py_DECREF(pDstOBJ);
  78. return(pythonResult);
  79. }
  80. return(NULL);
  81. }
  82. static PyObject *
  83. cmsis_arm_quaternion2rotation_f32(PyObject *obj, PyObject *args)
  84. {
  85. PyObject *pSrc=NULL; // input
  86. float32_t *pSrc_converted=NULL; // input
  87. float32_t *pDst=NULL; // output
  88. uint32_t nbQuaternions; // input
  89. if (PyArg_ParseTuple(args,"O",&pSrc))
  90. {
  91. GETARGUMENT(pSrc,NPY_DOUBLE,double,float32_t);
  92. nbQuaternions = arraySizepSrc / 4 ;
  93. pDst=PyMem_Malloc(9*sizeof(float32_t)*nbQuaternions);
  94. arm_quaternion2rotation_f32(pSrc_converted,pDst,nbQuaternions);
  95. FLOATARRAY1(pDstOBJ,9*nbQuaternions,pDst);
  96. PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
  97. FREEARGUMENT(pSrc_converted);
  98. Py_DECREF(pDstOBJ);
  99. return(pythonResult);
  100. }
  101. return(NULL);
  102. }
  103. static PyObject *
  104. cmsis_arm_rotation2quaternion_f32(PyObject *obj, PyObject *args)
  105. {
  106. PyObject *pSrc=NULL; // input
  107. float32_t *pSrc_converted=NULL; // input
  108. float32_t *pDst=NULL; // output
  109. uint32_t nbQuaternions; // input
  110. if (PyArg_ParseTuple(args,"O",&pSrc))
  111. {
  112. GETARGUMENT(pSrc,NPY_DOUBLE,double,float32_t);
  113. nbQuaternions = arraySizepSrc / 9 ;
  114. pDst=PyMem_Malloc(4*sizeof(float32_t)*nbQuaternions);
  115. arm_rotation2quaternion_f32(pSrc_converted,pDst,nbQuaternions);
  116. FLOATARRAY1(pDstOBJ,4*nbQuaternions,pDst);
  117. PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
  118. FREEARGUMENT(pSrc_converted);
  119. Py_DECREF(pDstOBJ);
  120. return(pythonResult);
  121. }
  122. return(NULL);
  123. }
  124. static PyObject *
  125. cmsis_arm_quaternion_normalize_f32(PyObject *obj, PyObject *args)
  126. {
  127. PyObject *pSrc=NULL; // input
  128. float32_t *pSrc_converted=NULL; // input
  129. float32_t *pDst=NULL; // output
  130. uint32_t nbQuaternions; // input
  131. if (PyArg_ParseTuple(args,"O",&pSrc))
  132. {
  133. GETARGUMENT(pSrc,NPY_DOUBLE,double,float32_t);
  134. nbQuaternions = arraySizepSrc / 4 ;
  135. pDst=PyMem_Malloc(4*sizeof(float32_t)*nbQuaternions);
  136. arm_quaternion_normalize_f32(pSrc_converted,pDst,nbQuaternions);
  137. FLOATARRAY1(pDstOBJ,4*nbQuaternions,pDst);
  138. PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
  139. FREEARGUMENT(pSrc_converted);
  140. Py_DECREF(pDstOBJ);
  141. return(pythonResult);
  142. }
  143. return(NULL);
  144. }
  145. static PyObject *
  146. cmsis_arm_quaternion_norm_f32(PyObject *obj, PyObject *args)
  147. {
  148. PyObject *pSrc=NULL; // input
  149. float32_t *pSrc_converted=NULL; // input
  150. float32_t *pDst=NULL; // output
  151. uint32_t nbQuaternions; // input
  152. if (PyArg_ParseTuple(args,"O",&pSrc))
  153. {
  154. GETARGUMENT(pSrc,NPY_DOUBLE,double,float32_t);
  155. nbQuaternions = arraySizepSrc / 4 ;
  156. pDst=PyMem_Malloc(sizeof(float32_t)*nbQuaternions);
  157. arm_quaternion_norm_f32(pSrc_converted,pDst,nbQuaternions);
  158. FLOATARRAY1(pDstOBJ,nbQuaternions,pDst);
  159. PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
  160. FREEARGUMENT(pSrc_converted);
  161. Py_DECREF(pDstOBJ);
  162. return(pythonResult);
  163. }
  164. return(NULL);
  165. }
  166. static PyObject *
  167. cmsis_arm_quaternion_conjugate_f32(PyObject *obj, PyObject *args)
  168. {
  169. PyObject *pSrc=NULL; // input
  170. float32_t *pSrc_converted=NULL; // input
  171. float32_t *pDst=NULL; // output
  172. uint32_t nbQuaternions; // input
  173. if (PyArg_ParseTuple(args,"O",&pSrc))
  174. {
  175. GETARGUMENT(pSrc,NPY_DOUBLE,double,float32_t);
  176. nbQuaternions = arraySizepSrc / 4 ;
  177. pDst=PyMem_Malloc(4*sizeof(float32_t)*nbQuaternions);
  178. arm_quaternion_conjugate_f32(pSrc_converted,pDst,nbQuaternions);
  179. FLOATARRAY1(pDstOBJ,4*nbQuaternions,pDst);
  180. PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
  181. FREEARGUMENT(pSrc_converted);
  182. Py_DECREF(pDstOBJ);
  183. return(pythonResult);
  184. }
  185. return(NULL);
  186. }
  187. static PyObject *
  188. cmsis_arm_quaternion_inverse_f32(PyObject *obj, PyObject *args)
  189. {
  190. PyObject *pSrc=NULL; // input
  191. float32_t *pSrc_converted=NULL; // input
  192. float32_t *pDst=NULL; // output
  193. uint32_t nbQuaternions; // input
  194. if (PyArg_ParseTuple(args,"O",&pSrc))
  195. {
  196. GETARGUMENT(pSrc,NPY_DOUBLE,double,float32_t);
  197. nbQuaternions = arraySizepSrc / 4 ;
  198. pDst=PyMem_Malloc(4*sizeof(float32_t)*nbQuaternions);
  199. arm_quaternion_inverse_f32(pSrc_converted,pDst,nbQuaternions);
  200. FLOATARRAY1(pDstOBJ,4*nbQuaternions,pDst);
  201. PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
  202. FREEARGUMENT(pSrc_converted);
  203. Py_DECREF(pDstOBJ);
  204. return(pythonResult);
  205. }
  206. return(NULL);
  207. }
  208. static PyMethodDef CMSISDSPMethods[] = {
  209. {"arm_quaternion_normalize_f32" , cmsis_arm_quaternion_normalize_f32, METH_VARARGS,""},
  210. {"arm_quaternion_conjugate_f32" , cmsis_arm_quaternion_conjugate_f32, METH_VARARGS,""},
  211. {"arm_quaternion_inverse_f32" , cmsis_arm_quaternion_inverse_f32, METH_VARARGS,""},
  212. {"arm_quaternion_norm_f32" , cmsis_arm_quaternion_norm_f32, METH_VARARGS,""},
  213. {"arm_quaternion2rotation_f32" , cmsis_arm_quaternion2rotation_f32, METH_VARARGS,""},
  214. {"arm_rotation2quaternion_f32" , cmsis_arm_rotation2quaternion_f32, METH_VARARGS,""},
  215. {"arm_quaternion_product_f32" , cmsis_arm_quaternion_product_f32, METH_VARARGS,""},
  216. {"arm_quaternion_product_single_f32" , cmsis_arm_quaternion_product_single_f32, METH_VARARGS,""},
  217. {"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
  218. {NULL, NULL, 0, NULL} /* Sentinel */
  219. };
  220. #ifdef IS_PY3K
  221. static int cmsisdsp_traverse(PyObject *m, visitproc visit, void *arg) {
  222. Py_VISIT(GETSTATE(m)->error);
  223. return 0;
  224. }
  225. static int cmsisdsp_clear(PyObject *m) {
  226. Py_CLEAR(GETSTATE(m)->error);
  227. return 0;
  228. }
  229. static struct PyModuleDef moduledef = {
  230. PyModuleDef_HEAD_INIT,
  231. MODNAME,
  232. NULL,
  233. sizeof(struct module_state),
  234. CMSISDSPMethods,
  235. NULL,
  236. cmsisdsp_traverse,
  237. cmsisdsp_clear,
  238. NULL
  239. };
  240. #define INITERROR return NULL
  241. PyMODINIT_FUNC
  242. CAT(PyInit_,MODINITNAME)(void)
  243. #else
  244. #define INITERROR return
  245. void CAT(init,MODINITNAME)(void)
  246. #endif
  247. {
  248. import_array();
  249. #ifdef IS_PY3K
  250. PyObject *module = PyModule_Create(&moduledef);
  251. #else
  252. PyObject *module = Py_InitModule(MODNAME, CMSISDSPMethods);
  253. #endif
  254. if (module == NULL)
  255. INITERROR;
  256. struct module_state *st = GETSTATE(module);
  257. st->error = PyErr_NewException(MODNAME".Error", NULL, NULL);
  258. if (st->error == NULL) {
  259. Py_DECREF(module);
  260. INITERROR;
  261. }
  262. typeRegistration(module);
  263. #ifdef IS_PY3K
  264. return module;
  265. #endif
  266. }