MegaFlowFLW.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. public class MegaFlowFLW
  6. {
  7. public static MegaFlowFrame LoadFrame(string filename)
  8. {
  9. MegaFlowFrame flow = null;
  10. if ( File.Exists(filename) )
  11. {
  12. flow = ScriptableObject.CreateInstance<MegaFlowFrame>();
  13. flow.Init();
  14. LoadFLW(flow, filename);
  15. }
  16. return flow;
  17. }
  18. public static MegaFlowFrame LoadFrame(string filename, int frame, string namesplit)
  19. {
  20. int decformat = 0;
  21. MegaFlowFrame flow = null;
  22. string dir= Path.GetDirectoryName(filename);
  23. string file = Path.GetFileNameWithoutExtension(filename);
  24. file = MegaFlowFGA.MakeFileName(file, ref decformat);
  25. #if false
  26. char[] splits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  27. string[] names;
  28. if ( namesplit.Length > 0 )
  29. {
  30. names = file.Split(namesplit[0]);
  31. names[0] += namesplit[0];
  32. }
  33. else
  34. names = file.Split(splits);
  35. if ( names.Length > 0 )
  36. {
  37. string newfname = dir + "/" + names[0] + frame + ".flw";
  38. flow = LoadFrame(newfname);
  39. }
  40. #else
  41. if ( file.Length > 0 )
  42. {
  43. string newfname = dir + "/" + file + frame.ToString("D" + decformat) + ".flw";
  44. flow = LoadFrame(newfname);
  45. }
  46. #endif
  47. return flow;
  48. }
  49. #if false
  50. public static MegaFlowFrame LoadFrame(string filename, int frame)
  51. {
  52. MegaFlowFrame flow = null;
  53. char[] splits = { '-' };
  54. string fname = filename; // use unity get path
  55. string[] names = fname.Split(splits);
  56. if ( names.Length > 0 )
  57. {
  58. string newfname = names[0] + "-" + frame + ".flw";
  59. flow = LoadFrame(newfname);
  60. }
  61. return flow;
  62. }
  63. #endif
  64. static public void LoadFLW(MegaFlowFrame flow, string filename)
  65. {
  66. StreamReader streamReader = new StreamReader(filename);
  67. string data = streamReader.ReadToEnd();
  68. streamReader.Close();
  69. MegaFlowXMLReader xml = new MegaFlowXMLReader();
  70. MegaFlowXMLNode node = xml.read(data);
  71. ParseXML1(flow, node);
  72. xml = null;
  73. data = null;
  74. GC.Collect();
  75. }
  76. static Vector3 ReadV3(string[] vals)
  77. {
  78. Vector3 v = Vector3.zero;
  79. v.x = float.Parse(vals[index++], System.Globalization.CultureInfo.InvariantCulture);
  80. v.y = float.Parse(vals[index++], System.Globalization.CultureInfo.InvariantCulture);
  81. v.z = float.Parse(vals[index++], System.Globalization.CultureInfo.InvariantCulture);
  82. return v;
  83. }
  84. static Vector3 ReadV3Adj(string[] vals)
  85. {
  86. Vector3 v = ReadV3(vals);
  87. v.z = -v.z;
  88. return v;
  89. }
  90. static int index = 0;
  91. static public void ParseXML1(MegaFlowFrame flow, MegaFlowXMLNode node)
  92. {
  93. foreach ( MegaFlowXMLNode n in node.children )
  94. {
  95. switch ( n.tagName )
  96. {
  97. case "Fluid": ParseFluid(flow, n); break;
  98. default: Debug.Log("Unknown Fluid Node " + n.tagName); break;
  99. }
  100. }
  101. }
  102. static void ParseFluid(MegaFlowFrame flow, MegaFlowXMLNode node)
  103. {
  104. for ( int i = 0; i < node.values.Count; i++ )
  105. {
  106. MegaFlowXMLValue val = node.values[i];
  107. switch ( val.name )
  108. {
  109. case "grid":
  110. {
  111. string[] vals = val.value.Split(',');
  112. flow.gridDim2[0] = int.Parse(vals[0]);
  113. flow.gridDim2[1] = int.Parse(vals[1]);
  114. flow.gridDim2[2] = int.Parse(vals[2]);
  115. break;
  116. }
  117. case "size":
  118. {
  119. string[] vals = val.value.Split(',');
  120. index = 0;
  121. Vector3 bmin = ReadV3(vals);
  122. Vector3 bmax = ReadV3(vals);
  123. flow.size = bmax - bmin;
  124. flow.gsize = flow.size;
  125. // griddim should have a name change
  126. flow.spacing.x = flow.size.x / flow.gridDim2[0];
  127. flow.spacing.y = flow.size.y / flow.gridDim2[1];
  128. flow.spacing.z = flow.size.z / flow.gridDim2[2];
  129. flow.oos.x = 1.0f / flow.spacing.x;
  130. flow.oos.y = 1.0f / flow.spacing.y;
  131. flow.oos.z = 1.0f / flow.spacing.z;
  132. break;
  133. }
  134. default: Debug.Log("Unknown Fluid attribute " + val.name); break;
  135. }
  136. }
  137. for ( int i = 0; i < node.children.Count; i++ )
  138. {
  139. MegaFlowXMLNode n = (MegaFlowXMLNode)node.children[i];
  140. switch ( n.tagName )
  141. {
  142. case "Vel":
  143. ParseVel(flow, n);
  144. break;
  145. case "Force":
  146. break;
  147. case "Density":
  148. break;
  149. default: Debug.Log("Unknown Fluid node " + n.tagName); break;
  150. }
  151. }
  152. }
  153. static void ParseVel(MegaFlowFrame flow, MegaFlowXMLNode node)
  154. {
  155. for ( int i = 0; i < node.values.Count; i++ )
  156. {
  157. MegaFlowXMLValue val = node.values[i];
  158. switch ( val.name )
  159. {
  160. case "data":
  161. {
  162. string[] vals = val.value.Split(',');
  163. index = 0;
  164. //int len = vals.Length / 3;
  165. flow.vel.Clear();
  166. Vector3[] vels = new Vector3[flow.gridDim2[0] * flow.gridDim2[1] * flow.gridDim2[2]];
  167. for ( int z = 0; z < flow.gridDim2[2]; z++ )
  168. {
  169. for ( int y = 0; y < flow.gridDim2[1]; y++ )
  170. {
  171. for ( int x = 0; x < flow.gridDim2[0]; x++ )
  172. vels[(x * flow.gridDim2[2] * flow.gridDim2[1]) + ((flow.gridDim2[2] - z - 1) * flow.gridDim2[1]) + y] = ReadV3Adj(vals);
  173. }
  174. }
  175. flow.framenumber = 0;
  176. flow.vel.AddRange(vels);
  177. break;
  178. }
  179. default: Debug.Log("Unknown Vel attribute " + val.name); break;
  180. }
  181. }
  182. }
  183. }