Tools.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using Autodesk.Max;
  6. using MaxSharp;
  7. using SharpDX;
  8. using Color = MaxSharp.Color;
  9. namespace Max2Babylon
  10. {
  11. public static class Tools
  12. {
  13. public const float Epsilon = 0.001f;
  14. public static bool IsEqualTo(this float[] value, float[] other)
  15. {
  16. if (value.Length != other.Length)
  17. {
  18. return false;
  19. }
  20. return !value.Where((t, i) => Math.Abs(t - other[i]) > Epsilon).Any();
  21. }
  22. public static float[] ToArray(this IMatrix3 value)
  23. {
  24. var row0 = value.GetRow(0).ToArraySwitched();
  25. var row1 = value.GetRow(1).ToArraySwitched();
  26. var row2 = value.GetRow(2).ToArraySwitched();
  27. var row3 = value.GetRow(3).ToArraySwitched();
  28. return new[]
  29. {
  30. row0[0], row0[1], row0[2], 0,
  31. row2[0], row2[1], row2[2], 0,
  32. row1[0], row1[1], row1[2], 0,
  33. row3[0], row3[1], row3[2], 1
  34. };
  35. }
  36. public static Quaternion ToQuat(this IQuat value)
  37. {
  38. return new Quaternion(value.X, value.Z, value.Y, value.W );
  39. }
  40. public static float[] ToArray(this IQuat value)
  41. {
  42. return new[] { value.X, value.Z, value.Y, value.W };
  43. }
  44. public static float[] Scale(this Color value, float scale)
  45. {
  46. return new[] { value.r * scale, value.g * scale, value.b * scale };
  47. }
  48. public static float[] ToArray(this Color value)
  49. {
  50. return new[] { value.r, value.g, value.b };
  51. }
  52. public static float[] ToArray(this IPoint3 value)
  53. {
  54. return new[] { value.X, value.Y, value.Z };
  55. }
  56. public static float[] ToArray(this IPoint2 value)
  57. {
  58. return new[] { value.X, value.Y };
  59. }
  60. public static float[] ToArraySwitched(this IPoint2 value)
  61. {
  62. return new[] { value.X, 1.0f - value.Y };
  63. }
  64. public static float[] ToArraySwitched(this IPoint3 value)
  65. {
  66. return new[] { value.X, value.Z, value.Y };
  67. }
  68. public static float[] ToArray(this IColor value)
  69. {
  70. return new[] { value.R, value.G, value.B };
  71. }
  72. public static IEnumerable<Node> NodesListBySuperClass(this Scene scene, SuperClassID sid)
  73. {
  74. return from n in scene.NodeTree where n.Object != null && n.Object.SuperClassID == sid select n;
  75. }
  76. public static float ConvertFov(float fov)
  77. {
  78. return (float)(2.0f * Math.Atan(Math.Tan(fov / 2.0f) / Loader.Core.ImageAspRatio));
  79. }
  80. public static bool HasParent(this Node node)
  81. {
  82. return node.Parent != null && node.Parent.Object != null;
  83. }
  84. public static Guid GetGuid(this Animatable node)
  85. {
  86. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  87. var uidData = appData.GetChunk(0);
  88. Guid uid;
  89. if (uidData != null)
  90. {
  91. uid = new Guid(uidData);
  92. }
  93. else
  94. {
  95. uid = Guid.NewGuid();
  96. appData.AddChunk(0, uid.ToByteArray());
  97. }
  98. return uid;
  99. }
  100. public static Guid GetGuid(this IINode node)
  101. {
  102. return GetGuid(Animatable.CreateWrapper<Node>(node));
  103. }
  104. public static string GetLocalData(this Node node)
  105. {
  106. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  107. var uidData = appData.GetChunk(1);
  108. if (uidData != null)
  109. {
  110. return System.Text.Encoding.UTF8.GetString(uidData);
  111. }
  112. return "";
  113. }
  114. public static void SetLocalData(this Node node, string value)
  115. {
  116. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  117. var uidData = appData.GetChunk(1);
  118. if (uidData != null)
  119. {
  120. appData.RemoveChunk(1);
  121. }
  122. appData.AddChunk(1, System.Text.Encoding.UTF8.GetBytes(value));
  123. }
  124. public static IMatrix3 GetWorldMatrix(this Node node, TimeValue t, bool parent)
  125. {
  126. var innerNode = node._Node;
  127. var tm = innerNode.GetNodeTM(t, Interval.Forever._IInterval);
  128. var ptm = innerNode.ParentNode.GetNodeTM(t, Interval.Forever._IInterval);
  129. if (!parent)
  130. return tm;
  131. if (innerNode.ParentNode.SuperClassID == SuperClassID.Camera)
  132. {
  133. var r = ptm.GetRow(3);
  134. ptm.IdentityMatrix();
  135. ptm.SetRow(3, r);
  136. }
  137. ptm.Invert();
  138. return tm.Multiply(ptm);
  139. }
  140. public static IMesh GetMesh(this IObject obj)
  141. {
  142. if (obj.CanConvertToType(ClassID.TriObject._IClass_ID) == 0)
  143. return null;
  144. var tri = obj.ConvertToType(0, ClassID.TriObject._IClass_ID) as ITriObject;
  145. return tri == null ? null : tri.Mesh;
  146. }
  147. public static bool IsAlmostEqualTo(this IPoint3 current, IPoint3 other, float epsilon)
  148. {
  149. if (Math.Abs(current.X - other.X) > epsilon)
  150. {
  151. return false;
  152. }
  153. if (Math.Abs(current.Y - other.Y) > epsilon)
  154. {
  155. return false;
  156. }
  157. if (Math.Abs(current.Z - other.Z) > epsilon)
  158. {
  159. return false;
  160. }
  161. return true;
  162. }
  163. public static bool IsAlmostEqualTo(this IPoint2 current, IPoint2 other, float epsilon)
  164. {
  165. if (Math.Abs(current.X - other.X) > epsilon)
  166. {
  167. return false;
  168. }
  169. if (Math.Abs(current.Y - other.Y) > epsilon)
  170. {
  171. return false;
  172. }
  173. return true;
  174. }
  175. public static bool GetBoolProperty(this IINode node, string propertyName, int defaultState = 0)
  176. {
  177. int state = defaultState;
  178. node.GetUserPropBool(ref propertyName, ref state);
  179. return state == 1;
  180. }
  181. public static float GetFloatProperty(this IINode node, string propertyName, float defaultState = 0)
  182. {
  183. float state = defaultState;
  184. node.GetUserPropFloat(ref propertyName, ref state);
  185. return state;
  186. }
  187. public static float[] GetVector3Property(this IINode node, string propertyName)
  188. {
  189. float state0 = 0;
  190. string name = propertyName + "_x";
  191. node.GetUserPropFloat(ref name, ref state0);
  192. float state1 = 0;
  193. name = propertyName + "_y";
  194. node.GetUserPropFloat(ref name, ref state1);
  195. float state2 = 0;
  196. name = propertyName + "_z";
  197. node.GetUserPropFloat(ref name, ref state2);
  198. return new[] { state0, state1, state2 };
  199. }
  200. public static void PrepareCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName, int defaultState = 0)
  201. {
  202. checkBox.CheckState = CheckState.Indeterminate;
  203. foreach (var node in nodes)
  204. {
  205. var state = node.GetBoolProperty(propertyName, defaultState);
  206. if (checkBox.CheckState == CheckState.Indeterminate)
  207. {
  208. checkBox.CheckState = state ? CheckState.Checked : CheckState.Unchecked;
  209. }
  210. else
  211. {
  212. if (!state && checkBox.CheckState == CheckState.Checked ||
  213. state && checkBox.CheckState == CheckState.Unchecked)
  214. {
  215. checkBox.CheckState = CheckState.Indeterminate;
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. public static void UpdateCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName)
  222. {
  223. foreach (var node in nodes)
  224. {
  225. if (checkBox.CheckState != CheckState.Indeterminate)
  226. {
  227. node.SetUserPropBool(ref propertyName, checkBox.CheckState == CheckState.Checked);
  228. }
  229. }
  230. }
  231. public static void PrepareNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName, float defaultState = 0)
  232. {
  233. nup.Value = (decimal)nodes[0].GetFloatProperty(propertyName, defaultState);
  234. }
  235. public static void UpdateNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName)
  236. {
  237. foreach (var node in nodes)
  238. {
  239. node.SetUserPropFloat(ref propertyName, (float)nup.Value);
  240. }
  241. }
  242. public static void PrepareVector3Control(Vector3Control vector3Control, IINode node, string propertyName, float defaultX = 0, float defaultY = 0, float defaultZ = 0)
  243. {
  244. vector3Control.X = node.GetFloatProperty(propertyName + "_x", defaultX);
  245. vector3Control.Y = node.GetFloatProperty(propertyName + "_y", defaultY);
  246. vector3Control.Z = node.GetFloatProperty(propertyName + "_z", defaultZ);
  247. }
  248. public static void UpdateVector3Control(Vector3Control vector3Control, IINode node, string propertyName)
  249. {
  250. string name = propertyName + "_x";
  251. node.SetUserPropFloat(ref name, vector3Control.X);
  252. name = propertyName + "_y";
  253. node.SetUserPropFloat(ref name, vector3Control.Y);
  254. name = propertyName + "_z";
  255. node.SetUserPropFloat(ref name, vector3Control.Z);
  256. }
  257. public static void UpdateVector3Control(Vector3Control vector3Control, List<IINode> nodes, string propertyName)
  258. {
  259. foreach (var node in nodes)
  260. {
  261. UpdateVector3Control(vector3Control, node, propertyName);
  262. }
  263. }
  264. }
  265. }