UrdfOrigin.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. © Siemens AG, 2018
  3. Author: Suzannah Smith (suzannah.smith@siemens.com)
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. <http://www.apache.org/licenses/LICENSE-2.0>.
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. using UnityEngine;
  15. namespace RosSharp.Urdf
  16. {
  17. public static class UrdfOrigin
  18. {
  19. #region Import
  20. public static void ImportOriginData(Transform transform, Origin origin)
  21. {
  22. if (origin != null)
  23. {
  24. transform.Translate(GetPositionFromUrdf(origin));
  25. transform.Rotate(GetRotationFromUrdf(origin));
  26. }
  27. }
  28. public static Vector3 GetPositionFromUrdf(Origin origin)
  29. {
  30. if (origin.Xyz != null)
  31. return origin.Xyz.ToVector3().Ros2Unity();
  32. return Vector3.zero;
  33. }
  34. public static Vector3 GetRotationFromUrdf(Origin origin)
  35. {
  36. if (origin.Rpy != null)
  37. return new Vector3(
  38. (float)+origin.Rpy[1] * Mathf.Rad2Deg,
  39. (float)-origin.Rpy[2] * Mathf.Rad2Deg,
  40. (float)-origin.Rpy[0] * Mathf.Rad2Deg);
  41. return Vector3.zero;
  42. }
  43. #endregion
  44. #region Export
  45. public static Origin ExportOriginData(Transform transform)
  46. {
  47. double[] xyz = ExportXyzData(transform);
  48. double[] rpy = ExportRpyData(transform);
  49. if (xyz != null || rpy != null)
  50. return new Origin(xyz, rpy);
  51. return null;
  52. }
  53. private static double[] ExportXyzData(Transform transform)
  54. {
  55. Vector3 xyzVector = transform.localPosition.Unity2Ros();
  56. return xyzVector == Vector3.zero ? null : xyzVector.ToRoundedDoubleArray();
  57. }
  58. private static double[] ExportRpyData(Transform transform)
  59. {
  60. Vector3 rpyVector = new Vector3(
  61. -transform.localEulerAngles.z * Mathf.Deg2Rad,
  62. transform.localEulerAngles.x * Mathf.Deg2Rad,
  63. -transform.localEulerAngles.y * Mathf.Deg2Rad);
  64. return rpyVector == Vector3.zero ? null : rpyVector.ToRoundedDoubleArray();
  65. }
  66. #endregion
  67. }
  68. }