| 1 |
|
| 2 |
// Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without modification, |
| 6 |
// are permitted provided that the following conditions are met: |
| 7 |
// |
| 8 |
// 1. Redistributions of source code must retain the above copyright notice, |
| 9 |
// this list of conditions, and the following disclaimer. |
| 10 |
// |
| 11 |
// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
// this list of conditions, and the following disclaimer in the documentation |
| 13 |
// and/or other materials provided with the distribution. |
| 14 |
// |
| 15 |
// 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 |
// may be used to endorse or promote products derived from this software without |
| 17 |
// specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 |
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 |
// THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 |
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 |
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 |
// SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 |
// OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 |
// ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 |
// |
| 32 |
|
| 33 |
#include "xsmatrix.h" |
| 34 |
#include <stdlib.h> |
| 35 |
#include <math.h> |
| 36 |
#include "xsdebugcounters.h" |
| 37 |
#include <string.h> |
| 38 |
#include "xsmalloc.h" |
| 39 |
#include "xsquaternion.h" |
| 40 |
#include <assert.h> |
| 41 |
|
| 42 |
/*! \class XsMatrix |
| 43 |
\brief A class that represents a matrix of real numbers |
| 44 |
*/ |
| 45 |
|
| 46 |
/*! \addtogroup cinterface C Interface |
| 47 |
@{ |
| 48 |
*/ |
| 49 |
|
| 50 |
/*! \relates XsMatrix \brief Construct the %XsMatrix as a reference to data in \a buffer */ |
| 51 |
void XsMatrix_ref(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, XsReal* buffer, XsDataFlags flags) |
| 52 |
{ |
| 53 |
//assert(!(thisPtr->m_flags & XSDF_FixedSize)); |
| 54 |
//XsMatrix_destruct(thisPtr); |
| 55 |
|
| 56 |
*((XsReal**) &thisPtr->m_data) = buffer; |
| 57 |
*((XsSize*) &thisPtr->m_flags) = (XsSize) flags; |
| 58 |
*((XsSize*) &thisPtr->m_rows) = rows; |
| 59 |
*((XsSize*) &thisPtr->m_cols) = cols; |
| 60 |
*((XsSize*) &thisPtr->m_stride) = stride; |
| 61 |
} |
| 62 |
|
| 63 |
/*! \relates XsMatrix \brief Init the %XsMatrix and copy the data from \a src into the matrix if \a src is not null */ |
| 64 |
void XsMatrix_construct(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal* src, XsSize srcStride) |
| 65 |
{ |
| 66 |
XsSize r; |
| 67 |
XsSize size = rows*stride; |
| 68 |
|
| 69 |
if (stride == 0) |
| 70 |
{ |
| 71 |
stride = cols; |
| 72 |
size = rows * stride; |
| 73 |
} |
| 74 |
if (size) |
| 75 |
{ |
| 76 |
// init to size |
| 77 |
XsReal* data = (XsReal*) xsMathMalloc(size*sizeof(XsReal)); |
| 78 |
assert(data); |
| 79 |
*((XsReal**) &thisPtr->m_data) = data; |
| 80 |
XsMatrix_incAllocCount(); |
| 81 |
} |
| 82 |
else |
| 83 |
*((XsReal**) &thisPtr->m_data) = 0; |
| 84 |
*((XsSize*) &thisPtr->m_flags) = XSDF_Managed; |
| 85 |
*((XsSize*) &thisPtr->m_rows) = rows; |
| 86 |
*((XsSize*) &thisPtr->m_cols) = cols; |
| 87 |
*((XsSize*) &thisPtr->m_stride) = stride; |
| 88 |
|
| 89 |
if (src && size) |
| 90 |
{ |
| 91 |
if (srcStride == 0 || srcStride == stride) |
| 92 |
memcpy(thisPtr->m_data, src, size*sizeof(XsReal)); |
| 93 |
else |
| 94 |
{ |
| 95 |
for (r = 0; r < rows; ++r) |
| 96 |
{ |
| 97 |
#if XSREAL_ALLOWS_MEMCPY |
| 98 |
memcpy(thisPtr->m_data + r*stride, src + r*srcStride, cols*sizeof(XsReal)); |
| 99 |
#else |
| 100 |
XsSize c; |
| 101 |
for (c = 0; c < cols; ++c) |
| 102 |
thisPtr->m_data[r*stride+c] = src[r*srcStride + c]; |
| 103 |
#endif |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/*! \relates XsMatrix \brief Init the %XsMatrix and copy the data from \a src into the matrix if \a src is not null */ |
| 110 |
void XsMatrix_assign(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal* src, XsSize srcStride) |
| 111 |
{ |
| 112 |
XsSize r; |
| 113 |
XsSize size = rows*stride; |
| 114 |
|
| 115 |
if (thisPtr->m_flags & XSDF_FixedSize) |
| 116 |
{ |
| 117 |
if (rows == 0 && cols == 0) |
| 118 |
{ |
| 119 |
*((XsSize*) &thisPtr->m_flags) |= XSDF_Empty; |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
assert(thisPtr->m_rows == rows && thisPtr->m_cols == cols); |
| 124 |
stride = thisPtr->m_stride; |
| 125 |
size = thisPtr->m_rows * stride; |
| 126 |
*((XsSize*) &thisPtr->m_flags) &= ~XSDF_Empty; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
if (thisPtr->m_rows == rows && thisPtr->m_cols == cols && |
| 131 |
(stride == 0 || stride == thisPtr->m_stride)) |
| 132 |
{ |
| 133 |
stride = thisPtr->m_stride; |
| 134 |
size = rows * stride; |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
if (stride == 0) |
| 139 |
{ |
| 140 |
stride = cols; |
| 141 |
size = rows * stride; |
| 142 |
} |
| 143 |
if (size > thisPtr->m_rows*thisPtr->m_stride || thisPtr->m_rows == 0) |
| 144 |
{ |
| 145 |
XsMatrix_destruct(thisPtr); |
| 146 |
if (size) |
| 147 |
{ |
| 148 |
// init to size |
| 149 |
XsReal* data = (XsReal*) xsMathMalloc(size*sizeof(XsReal)); |
| 150 |
assert(data); |
| 151 |
*((XsReal**) &thisPtr->m_data) = data; |
| 152 |
*((XsSize*) &thisPtr->m_flags) = XSDF_Managed; |
| 153 |
XsMatrix_incAllocCount(); |
| 154 |
} |
| 155 |
} |
| 156 |
*((XsSize*) &thisPtr->m_rows) = rows; |
| 157 |
*((XsSize*) &thisPtr->m_cols) = cols; |
| 158 |
*((XsSize*) &thisPtr->m_stride) = stride; |
| 159 |
} |
| 160 |
} |
| 161 |
if (src && size) |
| 162 |
{ |
| 163 |
if (srcStride == 0 || srcStride == stride) |
| 164 |
memcpy(thisPtr->m_data, src, size*sizeof(XsReal)); |
| 165 |
else |
| 166 |
{ |
| 167 |
for (r = 0; r < rows; ++r) |
| 168 |
{ |
| 169 |
#if XSREAL_ALLOWS_MEMCPY |
| 170 |
memcpy(thisPtr->m_data + r*stride, src + r*srcStride, cols*sizeof(XsReal)); |
| 171 |
#else |
| 172 |
XsSize c; |
| 173 |
for (c = 0; c < cols; ++c) |
| 174 |
thisPtr->m_data[r*stride+c] = src[r*srcStride + c]; |
| 175 |
#endif |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/*! \relates XsMatrix \brief Clear the XsMatrix and release allocated resources */ |
| 182 |
void XsMatrix_destruct(XsMatrix* thisPtr) |
| 183 |
{ |
| 184 |
if (thisPtr->m_data && (thisPtr->m_flags & XSDF_Managed)) |
| 185 |
{ |
| 186 |
// clear contents |
| 187 |
xsMathFree((void*) thisPtr->m_data); |
| 188 |
XsMatrix_incFreeCount(); |
| 189 |
} |
| 190 |
// init to 0 |
| 191 |
if (!(thisPtr->m_flags & XSDF_FixedSize)) |
| 192 |
{ |
| 193 |
*((XsReal**) &thisPtr->m_data) = 0; |
| 194 |
*((XsSize*) &thisPtr->m_rows) = 0; |
| 195 |
*((XsSize*) &thisPtr->m_cols) = 0; |
| 196 |
*((XsSize*) &thisPtr->m_stride) = 0; |
| 197 |
*((XsSize*) &thisPtr->m_flags) = 0; |
| 198 |
} |
| 199 |
else |
| 200 |
*((XsSize*) &thisPtr->m_flags) |= XSDF_Empty; |
| 201 |
} |
| 202 |
|
| 203 |
/*! \relates XsMatrix \brief Copy the contents of \a copy to the %XsMatrix */ |
| 204 |
void XsMatrix_copy(XsMatrix* copy, XsMatrix const* src) |
| 205 |
{ |
| 206 |
if (copy == src) |
| 207 |
return; |
| 208 |
XsMatrix_assign(copy, src->m_rows, src->m_cols, 0, src->m_data, src->m_stride); |
| 209 |
} |
| 210 |
|
| 211 |
/*! \relates XsMatrix \brief Set all the values in the matrix to zero */ |
| 212 |
void XsMatrix_setZero(XsMatrix* thisPtr) |
| 213 |
{ |
| 214 |
#if XSREAL_ALLOWS_MEMCPY |
| 215 |
if (thisPtr->m_stride == thisPtr->m_cols) |
| 216 |
memset(thisPtr->m_data, 0, sizeof(XsReal)*thisPtr->m_rows*thisPtr->m_cols); |
| 217 |
else |
| 218 |
{ |
| 219 |
XsSize r; |
| 220 |
for (r = 0; r < thisPtr->m_rows; ++r) |
| 221 |
memset(thisPtr->m_data + r*thisPtr->m_stride, 0, sizeof(XsReal) * thisPtr->m_cols); |
| 222 |
} |
| 223 |
#else |
| 224 |
XsSize r,c,stride = thisPtr->m_stride; |
| 225 |
for (r = 0; r < thisPtr->m_rows; ++r) |
| 226 |
for (c = 0; c < thisPtr->m_cols; ++c) |
| 227 |
thisPtr->m_data[r*stride+c] = XsMath_zero; |
| 228 |
#endif |
| 229 |
} |
| 230 |
|
| 231 |
/*! \relates XsMatrix \brief Returns not zero if the matrix contains no values */ |
| 232 |
int XsMatrix_empty(const XsMatrix* thisPtr) |
| 233 |
{ |
| 234 |
return (thisPtr->m_rows == 0) || (thisPtr->m_cols == 0) || (thisPtr->m_flags & XSDF_Empty); |
| 235 |
} |
| 236 |
|
| 237 |
/*! \relates XsMatrix \brief Multiplies all values in this XsMatrix by \a scalar |
| 238 |
\param scalar : Value to multiply by |
| 239 |
\param dest : The XsMatrix to store the result in |
| 240 |
*/ |
| 241 |
void XsMatrix_multiplyScalar(const XsMatrix* thisPtr, XsReal scalar, XsMatrix* dest) |
| 242 |
{ |
| 243 |
XsSize r,c,stride = thisPtr->m_stride, stride2; |
| 244 |
XsMatrix_assign(dest, thisPtr->m_rows, thisPtr->m_cols, 0, 0, 0); |
| 245 |
stride2 = dest->m_stride; |
| 246 |
for (r = 0; r < thisPtr->m_rows; ++r) |
| 247 |
for (c = 0; c < thisPtr->m_cols; ++c) |
| 248 |
dest->m_data[r*stride2+c] = thisPtr->m_data[r*stride+c] * scalar; |
| 249 |
} |
| 250 |
|
| 251 |
/*! \relates XsMatrix |
| 252 |
\brief Returns the offset in the data for accessing the value at \a row and \a column |
| 253 |
\param row The row of the value |
| 254 |
\param column The column of the value |
| 255 |
\returns The offset of the requested item in the internal buffer |
| 256 |
*/ |
| 257 |
XsSize XsMatrix_offset(const XsMatrix* thisPtr, XsSize row, XsSize column) |
| 258 |
{ |
| 259 |
return XsMatrix_offsetM(thisPtr, row, column); |
| 260 |
} |
| 261 |
|
| 262 |
//! \relates XsMatrix \brief Returns the data value at \a row and \a column |
| 263 |
XsReal XsMatrix_value(const XsMatrix* thisPtr, XsSize row, XsSize column) |
| 264 |
{ |
| 265 |
return thisPtr->m_data[XsMatrix_offsetM(thisPtr, row, column)]; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
//! \relates XsMatrix \brief Sets the data \a value at \a row and \a column |
| 270 |
void XsMatrix_setValue(XsMatrix* thisPtr, XsSize row, XsSize column, XsReal value) |
| 271 |
{ |
| 272 |
thisPtr->m_data[XsMatrix_offsetM(thisPtr, row, column)] = value; |
| 273 |
} |
| 274 |
|
| 275 |
//! \relates XsMatrix \brief Returns not zero if the dimensions of the %XsMatrix are equal to \a rows and \a columns |
| 276 |
int XsMatrix_dimensionsMatch(const XsMatrix* thisPtr, XsSize rows, XsSize columns) |
| 277 |
{ |
| 278 |
return thisPtr->m_rows == rows && thisPtr->m_cols == columns; |
| 279 |
} |
| 280 |
|
| 281 |
/*! \relates XsMatrix \brief Get an orientation matrix representation of the quaternion. */ |
| 282 |
void XsMatrix_fromQuaternion(XsMatrix* thisPtr, const XsQuaternion* quat) |
| 283 |
{ |
| 284 |
XsReal q00, q11, q22, q33, q01, q02, q03, q12, q13, q23; |
| 285 |
|
| 286 |
if (XsQuaternion_empty(quat)) |
| 287 |
{ |
| 288 |
XsMatrix_destruct(thisPtr); |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
q00 = quat->m_w*quat->m_w; |
| 293 |
q11 = quat->m_x*quat->m_x; |
| 294 |
q22 = quat->m_y*quat->m_y; |
| 295 |
q33 = quat->m_z*quat->m_z; |
| 296 |
|
| 297 |
q01 = quat->m_w*quat->m_x; |
| 298 |
q02 = quat->m_w*quat->m_y; |
| 299 |
q03 = quat->m_w*quat->m_z; |
| 300 |
|
| 301 |
q12 = quat->m_x*quat->m_y; |
| 302 |
q13 = quat->m_x*quat->m_z; |
| 303 |
q23 = quat->m_y*quat->m_z; |
| 304 |
|
| 305 |
XsMatrix_assign(thisPtr, 3, 3, 3, 0, 0); |
| 306 |
|
| 307 |
XsMatrix_setValue(thisPtr, 0, 0, (q00 + q11 - q22) - q33); |
| 308 |
XsMatrix_setValue(thisPtr, 0, 1, (q12 - q03) * XsMath_two); |
| 309 |
XsMatrix_setValue(thisPtr, 0, 2, (q13 + q02) * XsMath_two); |
| 310 |
|
| 311 |
XsMatrix_setValue(thisPtr, 1, 0, (q12 + q03) * XsMath_two); |
| 312 |
XsMatrix_setValue(thisPtr, 1, 1, (q00 - q11) + (q22 - q33)); |
| 313 |
XsMatrix_setValue(thisPtr, 1, 2, (q23 - q01) * XsMath_two); |
| 314 |
|
| 315 |
XsMatrix_setValue(thisPtr, 2, 0, (q13 - q02) * XsMath_two); |
| 316 |
XsMatrix_setValue(thisPtr, 2, 1, (q23 + q01) * XsMath_two); |
| 317 |
XsMatrix_setValue(thisPtr, 2, 2, ((q00 - q11) - q22) + q33); |
| 318 |
} |
| 319 |
|
| 320 |
/*! \relates XsMatrix \brief Swap the contents of \a a and \a b |
| 321 |
\details This function swaps the internal buffers so no actual data is moved around. |
| 322 |
This won't work for unmanaged data such as fixed size matrices (XsMatrix3x3) |
| 323 |
\param a Object whose contents will be placed in \a b |
| 324 |
\param b Object whose contents will be placed in \a a |
| 325 |
*/ |
| 326 |
void XsMatrix_swap(XsMatrix* a, XsMatrix* b) |
| 327 |
{ |
| 328 |
#ifdef __ICCARM__ |
| 329 |
#pragma diag_suppress=Pe370 |
| 330 |
#endif |
| 331 |
XsMatrix tmp; |
| 332 |
if ((!a->m_data || (a->m_flags & XSDF_Managed)) && (!b->m_data || (b->m_flags & XSDF_Managed))) |
| 333 |
{ |
| 334 |
*((XsReal**) &tmp.m_data) = a->m_data; |
| 335 |
*((XsSize*) &tmp.m_rows) = a->m_rows; |
| 336 |
*((XsSize*) &tmp.m_cols) = a->m_cols; |
| 337 |
*((XsSize*) &tmp.m_stride) = a->m_stride; |
| 338 |
*((XsSize*) &tmp.m_flags) = a->m_flags; |
| 339 |
|
| 340 |
*((XsReal**) &a->m_data) = b->m_data; |
| 341 |
*((XsSize*) &a->m_rows) = b->m_rows; |
| 342 |
*((XsSize*) &a->m_cols) = b->m_cols; |
| 343 |
*((XsSize*) &a->m_stride) = b->m_stride; |
| 344 |
*((XsSize*) &a->m_flags) = b->m_flags; |
| 345 |
|
| 346 |
*((XsReal**) &b->m_data) = tmp.m_data; |
| 347 |
*((XsSize*) &b->m_rows) = tmp.m_rows; |
| 348 |
*((XsSize*) &b->m_cols) = tmp.m_cols; |
| 349 |
*((XsSize*) &b->m_stride) = tmp.m_stride; |
| 350 |
*((XsSize*) &b->m_flags) = tmp.m_flags; |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
XsSize r, c; |
| 355 |
XsReal v; |
| 356 |
assert(a->m_data && b->m_data && a->m_rows == b->m_rows && a->m_cols == b->m_cols); |
| 357 |
for (r = 0; r < a->m_rows; ++r) |
| 358 |
{ |
| 359 |
XsReal* aa = a->m_data + r*a->m_stride*sizeof(XsReal); |
| 360 |
XsReal* bb = b->m_data + r*b->m_stride*sizeof(XsReal); |
| 361 |
for (c = 0; c < a->m_cols; ++c, ++aa, ++bb) |
| 362 |
{ |
| 363 |
v = *aa; |
| 364 |
*aa = *bb; |
| 365 |
*bb = v; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/*! @} */ |