TransformExtensions.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. © Siemens AG, 2017-2018
  3. Author: Dr. Martin Bischoff (martin.bischoff@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 System;
  15. using System.IO;
  16. using UnityEngine;
  17. using Object = UnityEngine.Object;
  18. namespace RosSharp
  19. {
  20. public static class TransformExtensions
  21. {
  22. private const int RoundDigits = 6;
  23. public static void DestroyImmediateIfExists<T>(this Transform transform) where T : Component
  24. {
  25. T component = transform.GetComponent<T>();
  26. if (component != null)
  27. Object.DestroyImmediate(component);
  28. }
  29. public static T AddComponentIfNotExists<T>(this Transform transform) where T : Component
  30. {
  31. T component = transform.GetComponent<T>();
  32. if (component == null)
  33. component = transform.gameObject.AddComponent<T>();
  34. return component;
  35. }
  36. public static void SetParentAndAlign(this Transform transform, Transform parent, bool keepLocalTransform = true)
  37. {
  38. Vector3 localPosition = transform.localPosition;
  39. Quaternion localRotation = transform.localRotation;
  40. transform.parent = parent;
  41. if (keepLocalTransform)
  42. {
  43. transform.position = transform.parent.position + localPosition;
  44. transform.rotation = transform.parent.rotation * localRotation;
  45. }
  46. else
  47. {
  48. transform.localPosition = Vector3.zero;
  49. transform.localRotation = Quaternion.identity;
  50. }
  51. }
  52. public static bool HasExactlyOneChild(this Transform transform)
  53. {
  54. return transform.childCount == 1;
  55. }
  56. public static void MoveChildTransformToParent(this Transform parent, bool transferRotation = true)
  57. {
  58. //Detach child in order to get a transform indenpendent from parent
  59. Transform childTransform = parent.GetChild(0);
  60. parent.DetachChildren();
  61. //Copy transform from child to parent
  62. parent.position = childTransform.position;
  63. parent.localScale = childTransform.localScale;
  64. if (transferRotation)
  65. {
  66. parent.rotation = childTransform.rotation;
  67. childTransform.localRotation = Quaternion.identity;
  68. }
  69. childTransform.parent = parent;
  70. childTransform.localPosition = Vector3.zero;
  71. childTransform.localScale = Vector3.one;
  72. }
  73. public static Vector3 Ros2Unity(this Vector3 vector3)
  74. {
  75. // Original.
  76. return new Vector3(-vector3.y, vector3.z, vector3.x);
  77. // Try to fit to point clouds.
  78. //return new Vector3(vector3.x,vector3.z,vector3.y);
  79. }
  80. public static Vector3 Unity2Ros(this Vector3 vector3)
  81. {
  82. // Original.
  83. return new Vector3(vector3.z, -vector3.x, vector3.y);
  84. // Try to fit to point clouds.
  85. //return new Vector3(vector3.x,vector3.z,vector3.y);
  86. }
  87. public static Vector3 Ros2UnityScale(this Vector3 vector3)
  88. {
  89. return new Vector3(vector3.y, vector3.z, vector3.x);
  90. }
  91. public static Vector3 Unity2RosScale(this Vector3 vector3)
  92. {
  93. return new Vector3(vector3.z, vector3.x, vector3.y);
  94. }
  95. public static Quaternion Ros2Unity(this Quaternion quaternion)
  96. {
  97. return new Quaternion(quaternion.y, -quaternion.z, -quaternion.x, quaternion.w);
  98. // Try to fit to point clouds.
  99. //return new Quaternion(-quaternion.x,-quaternion.z,-quaternion.y, quaternion.w);
  100. }
  101. public static Quaternion Unity2Ros(this Quaternion quaternion)
  102. {
  103. return new Quaternion(-quaternion.z, quaternion.x, -quaternion.y, quaternion.w);
  104. // Try to fit to Point clouds.
  105. //return new Quaternion(-quaternion.x,-quaternion.z,-quaternion.y,quaternion.w);
  106. }
  107. public static double[] ToRoundedDoubleArray(this Vector3 vector3)
  108. {
  109. double[] arr = new double[3];
  110. for (int i = 0; i < 3; i++)
  111. arr[i] = Math.Round(vector3[i], RoundDigits);
  112. return arr;
  113. }
  114. public static Vector3 ToVector3(this double[] array)
  115. {
  116. return new Vector3((float)array[0], (float)array[1], (float)array[2]);
  117. }
  118. public static string SetSeparatorChar(this string path)
  119. {
  120. return path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
  121. }
  122. }
  123. }