UnitCSVReader.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using Logger;
  8. namespace CSVReader
  9. {
  10. public class UnitCSVReader : AbstractUnitCSVReader, ICSVReader
  11. {
  12. public UnitCSVReader(string filename)
  13. {
  14. try
  15. {
  16. string fileName = Path.Combine(".", "Assets", "CSVInput", filename);
  17. Reader = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
  18. CityLogger.Log("Opened file with " + Reader.BaseStream.Length / ROWLENGTHINBYTES + " rows of data", LogLevel.INFO);
  19. Defined = true;
  20. }
  21. catch (Exception e)
  22. {
  23. CityLogger.LogError("Datei nicht gefunden, " + e.Message);
  24. Defined = false;
  25. }
  26. }
  27. #region exposed
  28. public double JumpToEntry(double time, bool forward)
  29. {
  30. if (double.IsNaN(time) || !TestBinaryReader() || !IsInBounds(time))
  31. return -1;
  32. long skip;
  33. double currTime = GetTimeAtRelativeOffset(0);
  34. int jumpDirection = (currTime < time) ? 1 : -1;
  35. skip = jumpDirection * ROWLENGTHINBYTES;
  36. lock (LockObject)
  37. {
  38. while (true)
  39. {
  40. if (!TestNewPosition(Reader.BaseStream.Position + skip))
  41. return -1;
  42. double test = GetTimeAtRelativeOffset(jumpDirection);
  43. bool step = IsStep(test, time, jumpDirection, forward);
  44. if (step)
  45. Reader.BaseStream.Position += skip;
  46. else
  47. {
  48. if (forward && jumpDirection == 1 || !forward && jumpDirection == -1)
  49. Reader.BaseStream.Position += skip;
  50. break;
  51. }
  52. }
  53. }
  54. return GetTimeAtRelativeOffset((forward) ? 0 : -1);
  55. }
  56. public InputObject ReadNextEntry(bool forward)
  57. {
  58. if (!TestBinaryReader())
  59. return null;
  60. long skip;
  61. if (forward)
  62. skip = 0;
  63. else
  64. skip = (-1 * ROWLENGTHINBYTES);
  65. lock (LockObject)
  66. {
  67. if (!TestNewPosition(Reader.BaseStream.Position + skip))
  68. return null;
  69. Reader.BaseStream.Position += skip;
  70. InputObject o = ParseObject();
  71. if (!forward)//compensate the streams natural behaviour of striving forward ;)
  72. Reader.BaseStream.Position += skip;
  73. return o;
  74. }
  75. }
  76. public bool IsInBounds(double time)
  77. {
  78. if (!TestBinaryReader())
  79. return false;
  80. double startTime = !double.IsNaN(ChunkStartTimestamp) ? ChunkStartTimestamp : GetTimeAtAbsolutPosition(0);
  81. double endTime = !double.IsNaN(ChunkEndTimestamp) ? ChunkEndTimestamp : GetTimeAtAbsolutPosition((Reader.BaseStream.Length / ROWLENGTHINBYTES) - 1);
  82. if (startTime <= time && time <= endTime)
  83. return true;
  84. return false;
  85. }
  86. public double Peek(bool forward)
  87. {
  88. if (!TestBinaryReader())
  89. return -1.0;
  90. //return GetTimeAtRelativOffset(0);
  91. if (forward)
  92. return GetTimeAtRelativeOffset(0);
  93. else
  94. return GetTimeAtRelativeOffset(-1);
  95. }
  96. public double GetLastTimestamp()
  97. {
  98. return GetTimeAtAbsolutPosition((Reader.BaseStream.Length / ROWLENGTHINBYTES) - 1);
  99. }
  100. public double GetFirstTimestamp()
  101. {
  102. return GetTimeAtAbsolutPosition(0);
  103. }
  104. public double GetChunkStarttimestamp()
  105. {
  106. return ChunkStartTimestamp;
  107. }
  108. public double GetChunkEndtimestamp()
  109. {
  110. return ChunkEndTimestamp;
  111. }
  112. public void SetChunkStarttimestamp(double time)
  113. {
  114. double firstFileTimestamp = GetFirstTimestamp();
  115. double lastFileTimestamp = GetLastTimestamp();
  116. if (time < firstFileTimestamp || time > lastFileTimestamp)
  117. {
  118. ChunkStartTimestamp = double.NaN;
  119. }
  120. else
  121. {
  122. double currTime = GetTimeAtRelativeOffset(0);
  123. if (currTime < time)
  124. {
  125. currTime = JumpToEntry(time, true);
  126. if (currTime > -1)
  127. {
  128. ChunkStartTimestamp = currTime;
  129. return;
  130. }
  131. }
  132. ChunkStartTimestamp = time;
  133. }
  134. }
  135. public void SetChunkEndtimestamp(double time)
  136. {
  137. double firstFileTimestamp = GetFirstTimestamp();
  138. double lastFileTimestamp = GetLastTimestamp();
  139. if (time > lastFileTimestamp || time < firstFileTimestamp)
  140. {
  141. ChunkEndTimestamp = double.NaN;
  142. }
  143. else
  144. {
  145. double currTime = GetTimeAtRelativeOffset(0);
  146. Debug.Log("currTime" + currTime.ToString());
  147. if (currTime > time)
  148. {
  149. currTime = JumpToEntry(time, false);
  150. Debug.Log("currTime" + currTime.ToString());
  151. if (currTime > -1)
  152. {
  153. ChunkEndTimestamp = currTime;
  154. return;
  155. }
  156. }
  157. ChunkEndTimestamp = time;
  158. }
  159. }
  160. public void TearDown()
  161. {
  162. if (TestBinaryReader())
  163. Reader.Close();
  164. Defined = true;
  165. }
  166. #endregion
  167. #region internal
  168. protected override InputObject ParseObject()
  169. {
  170. try
  171. {
  172. float[] posRot = new float[9];
  173. int id = BitConverter.ToInt32(Reader.ReadBytes(sizeof(int)), 0);
  174. double time = BitConverter.ToDouble(Reader.ReadBytes(sizeof(double)), 0);
  175. for (int i = 0; i < posRot.Length; i++)
  176. posRot[i] = BitConverter.ToSingle(Reader.ReadBytes(sizeof(float)), 0);
  177. uint toC = BitConverter.ToUInt32(Reader.ReadBytes(sizeof(uint)), 0);
  178. uint boC = BitConverter.ToUInt32(Reader.ReadBytes(sizeof(uint)), 0);
  179. int type = BitConverter.ToInt32(Reader.ReadBytes(sizeof(int)), 0);
  180. int sensorID = BitConverter.ToInt32(Reader.ReadBytes(sizeof(int)), 0);
  181. return new InputObject(id, time, posRot[0], posRot[1], posRot[2], posRot[3], posRot[4], posRot[5], posRot[6], posRot[7], posRot[8], toC, boC, (EntityType)type, sensorID);
  182. }
  183. catch
  184. {
  185. CityLogger.LogWarning("Unable to read From File");
  186. }
  187. return null;
  188. }
  189. protected override double GetTimeAtRelativeOffset(long shift)
  190. {
  191. double curTime = -1;
  192. long skip = (shift * ROWLENGTHINBYTES) + sizeof(int);
  193. lock (LockObject)
  194. {
  195. if (!TestNewPosition(Reader.BaseStream.Position + skip))
  196. if (skip < 0)
  197. return GetFirstTimestamp();
  198. else
  199. return GetLastTimestamp();
  200. Reader.BaseStream.Position += skip;
  201. curTime = BitConverter.ToDouble(Reader.ReadBytes(sizeof(double)), 0);
  202. Reader.BaseStream.Position -= (skip + sizeof(double));
  203. //try
  204. //{
  205. // curTime = BitConverter.ToDouble(Reader.ReadBytes(sizeof(double)), 0);
  206. //}
  207. //finally
  208. //{
  209. // Reader.BaseStream.Position -= (skip + sizeof(double));
  210. //}
  211. }
  212. return curTime;
  213. }
  214. protected override double GetTimeAtAbsolutPosition(long offset)
  215. {
  216. double curTime = -1;
  217. long newPos = (offset * ROWLENGTHINBYTES) + sizeof(int);
  218. if (!TestNewPosition(newPos))
  219. return curTime;
  220. lock (LockObject)
  221. {
  222. long oldPos = Reader.BaseStream.Position;
  223. Reader.BaseStream.Position = newPos;
  224. try
  225. {
  226. curTime = BitConverter.ToDouble(Reader.ReadBytes(sizeof(double)), 0);
  227. }
  228. finally
  229. {
  230. Reader.BaseStream.Position = oldPos;
  231. }
  232. }
  233. return curTime;
  234. }
  235. protected override bool TestNewPosition(long newPos)
  236. {
  237. if (newPos >= Reader.BaseStream.Length || newPos < 0)
  238. {
  239. CityLogger.LogWarning("Tried to move filepointer out of bounds");
  240. return false;
  241. }
  242. return true;
  243. }
  244. protected override bool TestBinaryReader()
  245. {
  246. if (!Defined)
  247. {
  248. CityLogger.LogError("Reader is not opened on File. unable to procced");
  249. return false;
  250. }
  251. return true;
  252. }
  253. protected override bool IsStep(double newTime, double targetTime, int jumpdirection, bool forward)
  254. {
  255. if (forward)
  256. return jumpdirection == 1 ? newTime < targetTime : newTime >= targetTime;
  257. else
  258. return jumpdirection == 1 ? newTime <= targetTime : newTime > targetTime;
  259. }
  260. #endregion
  261. }
  262. }