jntarray.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /** \file itasc/kdl/jntarray.cpp
  2. * \ingroup itasc
  3. */
  4. // Copyright (C) 2007 Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  5. // Version: 1.0
  6. // Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  7. // Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  8. // URL: http://www.orocos.org/kdl
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Lesser General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2.1 of the License, or (at your option) any later version.
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Lesser General Public License for more details.
  17. // You should have received a copy of the GNU Lesser General Public
  18. // License along with this library; if not, write to the Free Software
  19. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. #include "jntarray.hpp"
  21. namespace KDL
  22. {
  23. JntArray::JntArray():
  24. size(0),
  25. data(NULL)
  26. {
  27. }
  28. JntArray::JntArray(unsigned int _size):
  29. size(_size)
  30. {
  31. assert(0 < size);
  32. data = new double[size];
  33. SetToZero(*this);
  34. }
  35. JntArray::JntArray(const JntArray& arg):
  36. size(arg.size)
  37. {
  38. data = ((0 < size) ? new double[size] : NULL);
  39. for(unsigned int i=0;i<size;i++)
  40. data[i]=arg.data[i];
  41. }
  42. JntArray& JntArray::operator = (const JntArray& arg)
  43. {
  44. assert(size==arg.size);
  45. for(unsigned int i=0;i<size;i++)
  46. data[i]=arg.data[i];
  47. return *this;
  48. }
  49. JntArray::~JntArray()
  50. {
  51. delete [] data;
  52. }
  53. void JntArray::resize(unsigned int newSize)
  54. {
  55. delete [] data;
  56. size = newSize;
  57. data = new double[size];
  58. SetToZero(*this);
  59. }
  60. double JntArray::operator[](unsigned int i)const
  61. {
  62. assert(i<size);
  63. return data[i];
  64. }
  65. double& JntArray::operator[](unsigned int i)
  66. {
  67. assert(i<size);
  68. return data[i];
  69. }
  70. double* JntArray::operator()(unsigned int i)
  71. {
  72. if (i>=size)
  73. return NULL;
  74. return &data[i];
  75. }
  76. unsigned int JntArray::rows()const
  77. {
  78. return size;
  79. }
  80. unsigned int JntArray::columns()const
  81. {
  82. return 0;
  83. }
  84. void Add(const JntArray& src1,const JntArray& src2,JntArray& dest)
  85. {
  86. assert(src1.size==src2.size&&src1.size==dest.size);
  87. for(unsigned int i=0;i<dest.size;i++)
  88. dest.data[i]=src1.data[i]+src2.data[i];
  89. }
  90. void Subtract(const JntArray& src1,const JntArray& src2,JntArray& dest)
  91. {
  92. assert(src1.size==src2.size&&src1.size==dest.size);
  93. for(unsigned int i=0;i<dest.size;i++)
  94. dest.data[i]=src1.data[i]-src2.data[i];
  95. }
  96. void Multiply(const JntArray& src,const double& factor,JntArray& dest)
  97. {
  98. assert(src.size==dest.size);
  99. for(unsigned int i=0;i<dest.size;i++)
  100. dest.data[i]=factor*src.data[i];
  101. }
  102. void Divide(const JntArray& src,const double& factor,JntArray& dest)
  103. {
  104. assert(src.rows()==dest.size);
  105. for(unsigned int i=0;i<dest.size;i++)
  106. dest.data[i]=src.data[i]/factor;
  107. }
  108. void MultiplyJacobian(const Jacobian& jac, const JntArray& src, Twist& dest)
  109. {
  110. assert(jac.columns()==src.size);
  111. SetToZero(dest);
  112. for(unsigned int i=0;i<6;i++)
  113. for(unsigned int j=0;j<src.size;j++)
  114. dest(i)+=jac(i,j)*src.data[j];
  115. }
  116. void SetToZero(JntArray& array)
  117. {
  118. for(unsigned int i=0;i<array.size;i++)
  119. array.data[i]=0;
  120. }
  121. bool Equal(const JntArray& src1, const JntArray& src2,double eps)
  122. {
  123. assert(src1.size==src2.size);
  124. bool ret = true;
  125. for(unsigned int i=0;i<src1.size;i++)
  126. ret = ret && Equal(src1.data[i],src2.data[i],eps);
  127. return ret;
  128. }
  129. bool operator==(const JntArray& src1,const JntArray& src2){return Equal(src1,src2);};
  130. //bool operator!=(const JntArray& src1,const JntArray& src2){return Equal(src1,src2);};
  131. }