Cube3f.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using Island.StandardLib.Storage;
  2. using System;
  3. namespace Island.StandardLib.Math
  4. {
  5. public struct Cube3f : IStorable
  6. {
  7. float _XStart, _XEnd, _YStart, _YEnd, _ZStart, _ZEnd;
  8. public float XStart
  9. {
  10. get
  11. {
  12. return _XStart;
  13. }
  14. set
  15. {
  16. _XStart = value;
  17. if (_XEnd < _XStart)
  18. {
  19. float f = _XEnd;
  20. _XEnd = _XStart;
  21. _XStart = f;
  22. }
  23. }
  24. }
  25. public float YStart
  26. {
  27. get
  28. {
  29. return _YStart;
  30. }
  31. set
  32. {
  33. _YStart = value;
  34. if (_YEnd < _YStart)
  35. {
  36. float f = _YEnd;
  37. _YEnd = _YStart;
  38. _YStart = f;
  39. }
  40. }
  41. }
  42. public float ZStart
  43. {
  44. get
  45. {
  46. return _ZStart;
  47. }
  48. set
  49. {
  50. _ZStart = value;
  51. if (_ZEnd < _ZStart)
  52. {
  53. float f = _ZEnd;
  54. _ZEnd = _ZStart;
  55. _ZStart = f;
  56. }
  57. }
  58. }
  59. public float XEnd
  60. {
  61. get
  62. {
  63. return _XEnd;
  64. }
  65. set
  66. {
  67. _XEnd = value;
  68. if (_XEnd < _XStart)
  69. {
  70. float f = _XEnd;
  71. _XEnd = _XStart;
  72. _XStart = f;
  73. }
  74. }
  75. }
  76. public float YEnd
  77. {
  78. get
  79. {
  80. return _YEnd;
  81. }
  82. set
  83. {
  84. _YEnd = value;
  85. if (_YEnd < _YStart)
  86. {
  87. float f = _YEnd;
  88. _YEnd = _YStart;
  89. _YStart = f;
  90. }
  91. }
  92. }
  93. public float ZEnd
  94. {
  95. get
  96. {
  97. return _ZEnd;
  98. }
  99. set
  100. {
  101. _ZEnd = value;
  102. if (_ZEnd < _ZStart)
  103. {
  104. float f = _ZEnd;
  105. _ZEnd = _ZStart;
  106. _ZStart = f;
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// 以完整格式初始化<see cref="Cube3f"/>
  112. /// </summary>
  113. /// <param name="xStart">X起始点</param>
  114. /// <param name="xEnd">X终点</param>
  115. /// <param name="yStart">Y起始点</param>
  116. /// <param name="yEnd">Y终点</param>
  117. /// <param name="zStart">Z起始点</param>
  118. /// <param name="zEnd">Z终点</param>
  119. public Cube3f(float xStart, float xEnd, float yStart, float yEnd, float zStart, float zEnd)
  120. {
  121. _XStart = xStart;
  122. _XEnd = xEnd;
  123. _YStart = yStart;
  124. _YEnd = yEnd;
  125. _ZStart = zStart;
  126. _ZEnd = zEnd;
  127. Trim();
  128. }
  129. /// <summary>
  130. /// 以三边长度初始化<see cref="Cube3f"/>,并将起始点设为<see cref="Vector3.Zero"/>
  131. /// </summary>
  132. /// <param name="xlen">X长度</param>
  133. /// <param name="ylen">Y长度</param>
  134. /// <param name="zlen">Z长度</param>
  135. public Cube3f(float xlen, float ylen, float zlen)
  136. {
  137. _XStart = _YStart = _ZStart = 0f;
  138. _XEnd = xlen;
  139. _YEnd = ylen;
  140. _ZEnd = zlen;
  141. }
  142. /// <summary>
  143. /// 以三边长度初始化<see cref="Cube3f"/>,并将起始点设为<see cref="Vector3.Zero"/>
  144. /// </summary>
  145. /// <param name="size">三边长度</param>
  146. public Cube3f(Vector3 size)
  147. {
  148. _XStart = _YStart = _ZStart = 0f;
  149. _XEnd = size.x;
  150. _YEnd = size.y;
  151. _ZEnd = size.z;
  152. }
  153. void Trim()
  154. {
  155. if (_XEnd < _XStart)
  156. {
  157. float f = _XEnd;
  158. _XEnd = _XStart;
  159. _XStart = f;
  160. }
  161. if (_YEnd < _YStart)
  162. {
  163. float f = _YEnd;
  164. _YEnd = _YStart;
  165. _YStart = f;
  166. }
  167. if (_ZEnd < _ZStart)
  168. {
  169. float f = _ZEnd;
  170. _ZEnd = _ZStart;
  171. _ZStart = f;
  172. }
  173. }
  174. /// <summary>
  175. /// 获取三边长度
  176. /// </summary>
  177. public Vector3 Length
  178. {
  179. get
  180. {
  181. return new Vector3(_XEnd - _XStart, _YEnd - _YStart, _ZEnd - _ZStart);
  182. }
  183. }
  184. /// <summary>
  185. /// 获取或设置起始点
  186. /// </summary>
  187. public Vector3 Start
  188. {
  189. get
  190. {
  191. return new Vector3(_XStart, _YStart, _ZStart);
  192. }
  193. set
  194. {
  195. Vector3 delta = value.RED(Start);
  196. _XEnd += delta.x;
  197. _YEnd += delta.y;
  198. _ZEnd += delta.z;
  199. _XStart = value.x;
  200. _YStart = value.y;
  201. _ZStart = value.z;
  202. }
  203. }
  204. /// <summary>
  205. /// 获取和设置终点
  206. /// </summary>
  207. public Vector3 End
  208. {
  209. get
  210. {
  211. return new Vector3(_XEnd, _YEnd, _ZEnd);
  212. }
  213. set
  214. {
  215. Vector3 delta = value.RED(End);
  216. _XStart += delta.x;
  217. _YStart += delta.y;
  218. _ZStart += delta.z;
  219. _XEnd = value.x;
  220. _YEnd = value.y;
  221. _ZEnd = value.z;
  222. }
  223. }
  224. /// <summary>
  225. /// 获取和设置底面中心点
  226. /// </summary>
  227. public Vector3 BottomCenter
  228. {
  229. get
  230. {
  231. Vector3 hfsize = Length.MUL(0.5f);
  232. return new Vector3(_XStart + hfsize.x, _YStart, _ZStart + hfsize.z);
  233. }
  234. set
  235. {
  236. Vector3 hfsize = Length.MUL(0.5f);
  237. hfsize.y = 0;
  238. Start = value.RED(hfsize);
  239. }
  240. }
  241. /// <summary>
  242. /// 测试此<see cref="Cube3f"/>是否包含指定的点
  243. /// </summary>
  244. /// <param name="pt"></param>
  245. /// <returns></returns>
  246. public bool ContainPoint(Vector3 pt)
  247. {
  248. return pt.x >= _XStart && pt.x <= _XEnd && pt.y >= _YStart && pt.y <= _YEnd && pt.z >= _ZStart && pt.z <= _ZEnd;
  249. }
  250. /// <summary>
  251. /// 测试此<see cref="Cube3f"/>是否与指定的<see cref="Cube3f"/>相交
  252. /// </summary>
  253. /// <param name="cb"></param>
  254. /// <returns></returns>
  255. public bool ContainCube(Cube3f cb)
  256. {
  257. if (cb.XStart > XEnd)
  258. return false;
  259. if (cb.XEnd < XStart)
  260. return false;
  261. if (cb.YStart > YEnd)
  262. return false;
  263. if (cb.YEnd < YStart)
  264. return false;
  265. if (cb.ZStart > ZEnd)
  266. return false;
  267. if (cb.ZEnd < ZStart)
  268. return false;
  269. return true;
  270. }
  271. public void WriteToData(DataStorage data)
  272. {
  273. data.Write(_XStart);
  274. data.Write(_XEnd);
  275. data.Write(_YStart);
  276. data.Write(_YEnd);
  277. data.Write(_ZStart);
  278. data.Write(_ZEnd);
  279. }
  280. public void ReadFromData(DataStorage data)
  281. {
  282. data.Read(out _XStart);
  283. data.Read(out _XEnd);
  284. data.Read(out _YStart);
  285. data.Read(out _YEnd);
  286. data.Read(out _ZStart);
  287. data.Read(out _ZEnd);
  288. }
  289. public static Cube3f operator +(Cube3f cube, Vector3 vec)
  290. {
  291. return new Cube3f(
  292. cube.XStart + vec.x, cube.XEnd + vec.x,
  293. cube.YStart + vec.y, cube.YEnd + vec.y,
  294. cube.ZStart + vec.z, cube.ZEnd + vec.z);
  295. }
  296. public static Cube3f operator -(Cube3f cube, Vector3 vec)
  297. {
  298. return new Cube3f(
  299. cube.XStart - vec.x, cube.XEnd - vec.x,
  300. cube.YStart - vec.y, cube.YEnd - vec.y,
  301. cube.ZStart - vec.z, cube.ZEnd - vec.z);
  302. }
  303. }
  304. }