MegaFlowFGA.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. public class MegaFlowFGA
  7. {
  8. static int index = 0;
  9. static public string MakeFileName(string file, ref int format)
  10. {
  11. string ret = "";
  12. format = 0;
  13. for ( int i = file.Length - 1; i >= 0; i-- )
  14. {
  15. char c = file[i];
  16. if ( Char.IsNumber(c) )
  17. format++;
  18. else
  19. {
  20. ret = file.Substring(0, i + 1);
  21. break;
  22. }
  23. }
  24. return ret;
  25. }
  26. public static MegaFlowFrame LoadFrame(string filename, int frame, string namesplit, int decformat)
  27. {
  28. MegaFlowFrame flow = null;
  29. string dir= Path.GetDirectoryName(filename);
  30. string file = Path.GetFileNameWithoutExtension(filename);
  31. file = MakeFileName(file, ref decformat);
  32. #if false
  33. char[] splits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  34. string[] names;
  35. if ( namesplit.Length > 0 )
  36. {
  37. names = file.Split(namesplit[0]);
  38. names[0] += namesplit[0];
  39. }
  40. else
  41. names = file.Split(splits);
  42. if ( names.Length > 0 )
  43. {
  44. string newfname = dir + "/" + names[0] + frame.ToString("D" + decformat) + ".fga";
  45. flow = LoadFrame(newfname);
  46. }
  47. #else
  48. if ( file.Length > 0 )
  49. {
  50. string newfname = dir + "/" + file + frame.ToString("D" + decformat) + ".fga";
  51. flow = LoadFrame(newfname);
  52. }
  53. #endif
  54. return flow;
  55. }
  56. #if false
  57. public static MegaFlowFrame LoadFrame(string filename, int frame)
  58. {
  59. MegaFlowFrame flow = null;
  60. char[] splits = { '.' };
  61. string fname = filename; // use unity get path
  62. string[] names = fname.Split(splits);
  63. if ( names.Length > 0 )
  64. {
  65. string newfname = names[0] + "." + frame + ".fga";
  66. flow = LoadFrame(newfname);
  67. }
  68. return flow;
  69. }
  70. #endif
  71. public static MegaFlowFrame LoadFrame(string filename)
  72. {
  73. MegaFlowFrame flow = null;
  74. //Debug.Log("FGA file " + filename);
  75. if ( File.Exists(filename) )
  76. {
  77. flow = ScriptableObject.CreateInstance<MegaFlowFrame>();
  78. flow.Init();
  79. LoadFGA(flow, filename);
  80. }
  81. return flow;
  82. }
  83. static Vector3 ReadV3(string[] vals)
  84. {
  85. Vector3 v = Vector3.zero;
  86. v.x = float.Parse(vals[index++]);
  87. v.y = float.Parse(vals[index++]);
  88. v.z = float.Parse(vals[index++]);
  89. return v;
  90. }
  91. static Vector3 ReadV3Adj(string[] vals)
  92. {
  93. Vector3 v = ReadV3(vals);
  94. v.z = -v.z;
  95. return v;
  96. }
  97. static public void LoadFGA(MegaFlowFrame flow, string filename)
  98. {
  99. StreamReader reader = File.OpenText(filename);
  100. string file = reader.ReadToEnd();
  101. string[] vals = file.Split(',');
  102. index = 0;
  103. flow.gridDim2[0] = (int)float.Parse(vals[index++]);
  104. flow.gridDim2[1] = (int)float.Parse(vals[index++]);
  105. flow.gridDim2[2] = (int)float.Parse(vals[index++]);
  106. Vector3 bmin = ReadV3(vals);
  107. Vector3 bmax = ReadV3(vals);
  108. flow.size = bmax - bmin;
  109. flow.gsize = flow.size;
  110. // griddim should have a name change
  111. flow.spacing.x = flow.size.x / flow.gridDim2[0];
  112. flow.spacing.y = flow.size.y / flow.gridDim2[1];
  113. flow.spacing.z = flow.size.z / flow.gridDim2[2];
  114. flow.oos.x = 1.0f / flow.spacing.x;
  115. flow.oos.y = 1.0f / flow.spacing.y;
  116. flow.oos.z = 1.0f / flow.spacing.z;
  117. //Debug.Log("spacing " + flow.spacing);
  118. //Debug.Log("griddim " + flow.gridDim2[0] + " " + flow.gridDim2[1] + " " + flow.gridDim2[2]);
  119. flow.vel.Clear();
  120. Vector3[] vels = new Vector3[flow.gridDim2[0] * flow.gridDim2[1] * flow.gridDim2[2]];
  121. for ( int z = 0; z < flow.gridDim2[2]; z++ )
  122. {
  123. for ( int y = 0; y < flow.gridDim2[1]; y++ )
  124. {
  125. for ( int x = 0; x < flow.gridDim2[0]; x++ )
  126. vels[(x * flow.gridDim2[2] * flow.gridDim2[1]) + ((flow.gridDim2[2] - z - 1) * flow.gridDim2[1]) + y] = ReadV3Adj(vals);
  127. }
  128. }
  129. flow.framenumber = 0;
  130. flow.vel.AddRange(vels);
  131. reader.Close();
  132. reader = null;
  133. GC.Collect();
  134. }
  135. static void WriteV3(StreamWriter file, Vector3 v)
  136. {
  137. file.Write(v.x.ToString("0.#####") + ",");
  138. file.Write(v.y.ToString("0.#####") + ",");
  139. file.Write(v.z.ToString("0.#####") + ",");
  140. }
  141. static void WriteV3Adj(StreamWriter file, Vector3 v)
  142. {
  143. v.z = -v.z;
  144. file.Write(v.x.ToString("0.#####") + ",");
  145. file.Write(v.y.ToString("0.#####") + ",");
  146. file.Write(v.z.ToString("0.#####") + ",");
  147. }
  148. static public void SaveFGA(MegaFlowFrame flow, string filename)
  149. {
  150. StreamWriter file = new StreamWriter(filename);
  151. file.Write(flow.gridDim2[0].ToString("0.#####") + ",");
  152. file.Write(flow.gridDim2[1].ToString("0.#####") + ",");
  153. file.Write(flow.gridDim2[2].ToString("0.#####") + ",");
  154. Vector3 sz = flow.size * 0.5f;
  155. WriteV3(file, -sz);
  156. WriteV3(file, sz);
  157. for ( int z = 0; z < flow.gridDim2[2]; z++ )
  158. {
  159. for ( int y = 0; y < flow.gridDim2[1]; y++ )
  160. {
  161. for ( int x = 0; x < flow.gridDim2[0]; x++ )
  162. WriteV3Adj(file, flow.vel[(x * flow.gridDim2[2] * flow.gridDim2[1]) + ((flow.gridDim2[2] - z - 1) * flow.gridDim2[1]) + y]);
  163. }
  164. }
  165. file.Close();
  166. }
  167. // NetCDF example
  168. /*
  169. netCDF volume {
  170. dimensions:
  171.  nx = 3;
  172.  ny = 3;
  173.  nz = 3;
  174. variables:
  175.  float field_data(nx, ny, nz);
  176. data:
  177.  field_data =
  178.  0, 0, 0
  179.  0, 0, 0
  180.  0, 5, 0
  181.  0, 0, 5
  182.  0, 0, 0
  183.  0, 0, 0
  184.  5, 0, 0
  185.  0, 0, 0
  186.  0, 0, 0;
  187. }
  188. */
  189. static public void SaveNetCDF(MegaFlowFrame flow, string filename)
  190. {
  191. StreamWriter file = new StreamWriter(filename);
  192. file.Write("netCDF volume {\n");
  193. file.Write("dimensions:\n");
  194. file.Write("\tnx = " + flow.gridDim2[0].ToString("0.#####") + ";\n");
  195. file.Write("\tny = " + flow.gridDim2[1].ToString("0.#####") + ";\n");
  196. file.Write("\tnz = " + flow.gridDim2[2].ToString("0.#####") + ";\n");
  197. file.Write("\nvariables:\n\t float field_data(nx, ny, nz);\n");
  198. file.Write("\ndata:\n");
  199. file.Write("\tfield_data =\n");
  200. for ( int z = 0; z < flow.gridDim2[2]; z++ )
  201. {
  202. for ( int y = 0; y < flow.gridDim2[1]; y++ )
  203. {
  204. for ( int x = 0; x < flow.gridDim2[0]; x++ )
  205. {
  206. WriteV3Adj(file, flow.vel[(x * flow.gridDim2[2] * flow.gridDim2[1]) + ((flow.gridDim2[2] - z - 1) * flow.gridDim2[1]) + y]);
  207. }
  208. file.Write("\n");
  209. }
  210. file.Write("\n\n");
  211. }
  212. file.Write("}");
  213. file.Close();
  214. }
  215. public static MegaFlowFrame LoadFrameNetCDF(string filename)
  216. {
  217. MegaFlowFrame flow = null;
  218. if ( File.Exists(filename) )
  219. {
  220. flow = ScriptableObject.CreateInstance<MegaFlowFrame>();
  221. flow.Init();
  222. LoadNetCDF(flow, filename);
  223. }
  224. return flow;
  225. }
  226. static public void LoadNetCDF(MegaFlowFrame flow, string filename)
  227. {
  228. StreamReader reader = File.OpenText(filename);
  229. // Cant do this way will take to long, need to read from stream
  230. char[] split = { '=', ';' };
  231. reader.ReadLine();
  232. string dim = reader.ReadLine();
  233. if ( dim.Contains("dimensions") )
  234. {
  235. string line = reader.ReadLine();
  236. string[] parts = line.Split(split);
  237. flow.gridDim2[0] = int.Parse(parts[1]);
  238. line = reader.ReadLine();
  239. parts = line.Split(split);
  240. flow.gridDim2[1] = int.Parse(parts[1]);
  241. line = reader.ReadLine();
  242. parts = line.Split(split);
  243. flow.gridDim2[2] = int.Parse(parts[1]);
  244. flow.size = new Vector3(flow.gridDim2[0], flow.gridDim2[1], flow.gridDim2[2]);
  245. flow.gsize = flow.size;
  246. flow.spacing.x = flow.size.x / flow.gridDim2[0];
  247. flow.spacing.y = flow.size.y / flow.gridDim2[1];
  248. flow.spacing.z = flow.size.z / flow.gridDim2[2];
  249. flow.oos.x = 1.0f / flow.spacing.x;
  250. flow.oos.y = 1.0f / flow.spacing.y;
  251. flow.oos.z = 1.0f / flow.spacing.z;
  252. }
  253. reader.ReadLine();
  254. reader.ReadLine();
  255. reader.ReadLine();
  256. reader.ReadLine();
  257. reader.ReadLine();
  258. string data = reader.ReadLine();
  259. Vector3[] vels = new Vector3[flow.gridDim2[0] * flow.gridDim2[1] * flow.gridDim2[2]];
  260. bool cancel = false;
  261. if ( data.Contains("field_data") )
  262. {
  263. flow.vel.Clear();
  264. // Read data in
  265. Vector3 rval = Vector3.zero;
  266. string[] vals;
  267. for ( int z = 0; z < flow.gridDim2[2]; z++ )
  268. {
  269. if ( !EditorUtility.DisplayCancelableProgressBar("Importing NetCDF File", "Importing Slice " + z + " of " + flow.gridDim2[2], (float)z / (float)flow.gridDim2[2]) )
  270. {
  271. for ( int y = 0; y < flow.gridDim2[1]; y++ )
  272. {
  273. while ( true )
  274. {
  275. vals = reader.ReadLine().Split(',');
  276. if ( vals.Length > 1 )
  277. break;
  278. }
  279. int index = 0;
  280. for ( int x = 0; x < flow.gridDim2[0]; x++ )
  281. {
  282. rval.x = float.Parse(vals[index++]);
  283. rval.y = float.Parse(vals[index++]);
  284. rval.z = -float.Parse(vals[index++]);
  285. vels[(x * flow.gridDim2[2] * flow.gridDim2[1]) + ((flow.gridDim2[2] - z - 1) * flow.gridDim2[1]) + y] = rval;
  286. }
  287. }
  288. }
  289. else
  290. {
  291. cancel = true;
  292. break;
  293. }
  294. }
  295. }
  296. EditorUtility.ClearProgressBar();
  297. if ( !cancel )
  298. {
  299. flow.framenumber = 0;
  300. flow.vel.AddRange(vels);
  301. }
  302. reader.Close();
  303. reader = null;
  304. GC.Collect();
  305. return;
  306. }
  307. }