AICommonAdditional.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. using AI.Common.Tools;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Runtime.Serialization;
  7. using System.Text;
  8. namespace AI.Reconstruction
  9. {
  10. ///重要说明: 项目开发时使用的AI.Common(1.0.1.22)、AI.DiagSystem(1.0.1.31) 均为老版本
  11. ///现替换为正式发布的Release版AI.Common(1.0.1.31)、AI.DiagSystem(1.0.1.44),
  12. ///但与之前版本比较,缺少Point3DF、Point2DF等结构体定义,所以在此临时添加接口
  13. ///等添加接口后,仍需改为release版
  14. /// <summary>
  15. /// 2维点坐标
  16. /// 代替System.Drawing.Point
  17. /// </summary>
  18. [DataContract]
  19. [StructLayout(LayoutKind.Sequential)]
  20. public struct Point2DF : IEquatable<Point2DF>
  21. {
  22. private float _x;
  23. private float _y;
  24. /// <summary>
  25. /// X坐标
  26. /// </summary>
  27. [DataMember]
  28. public float X { get => _x; set => _x = value; }
  29. /// <summary>
  30. /// Y坐标
  31. /// </summary>
  32. [DataMember]
  33. public float Y { get => _y; set => _y = value; }
  34. /// <summary>
  35. /// 构造函数
  36. /// </summary>
  37. /// <param name="x"></param>
  38. /// <param name="y"></param>
  39. public Point2DF(float x, float y)
  40. {
  41. _x = x;
  42. _y = y;
  43. }
  44. /// <summary>
  45. /// 空的点
  46. /// </summary>
  47. public static readonly Point2DF Empty = new Point2DF(0, 0);
  48. /// <summary>
  49. /// 判断是否相等
  50. /// </summary>
  51. /// <param name="other"></param>
  52. /// <returns></returns>
  53. public bool Equals(Point2DF other)
  54. {
  55. return Equals(this, other);
  56. }
  57. /// <summary>
  58. /// 判断是否相等
  59. /// </summary>
  60. /// <param name="obj"></param>
  61. /// <returns></returns>
  62. public override bool Equals(object obj)
  63. {
  64. if (obj == null || !(obj is Point2DF))
  65. {
  66. return false;
  67. }
  68. Point2DF point = (Point2DF)obj;
  69. return Equals(this, point);
  70. }
  71. /// <summary>
  72. /// 获得哈希值
  73. /// </summary>
  74. /// <returns></returns>
  75. public override int GetHashCode()
  76. {
  77. return string.Format("{0}-{1}", _x, _y).GetHashCode();
  78. }
  79. /// <summary>
  80. /// 转成字符串
  81. /// </summary>
  82. /// <returns></returns>
  83. public override string ToString()
  84. {
  85. return SerializeDeserializeHelper.SerializeToJsonString(this);
  86. }
  87. /// <summary>
  88. /// 从字符串中重建
  89. /// </summary>
  90. /// <param name="str"></param>
  91. /// <returns></returns>
  92. public static Point2DF FromString(string str)
  93. {
  94. return SerializeDeserializeHelper.DeserializeJsonResult<Point2DF>(str);
  95. }
  96. /// <summary>
  97. /// 转为bytes
  98. /// </summary>
  99. /// <returns></returns>
  100. public byte[] ToBytes()
  101. {
  102. byte[] result;
  103. using (var stream = new MemoryStream())
  104. {
  105. var writer = new AIStreamWriter(stream);
  106. writer.WriteFloat(_x);
  107. writer.WriteFloat(_y);
  108. result = stream.ToArray();
  109. }
  110. return result;
  111. }
  112. /// <summary>
  113. /// 从bytes里重建
  114. /// </summary>
  115. /// <param name="bytes"></param>
  116. /// <returns></returns>
  117. public static Point2DF FromBytes(byte[] bytes)
  118. {
  119. Point2DF result;
  120. using (var stream = new MemoryStream(bytes))
  121. {
  122. stream.Position = 0;
  123. var reader = new AIStreamReader(stream);
  124. var x = reader.ReadFloat();
  125. var y = reader.ReadFloat();
  126. result = new Point2DF(x, y);
  127. }
  128. return result;
  129. }
  130. public static bool Equals(Point2DF one, Point2DF other)
  131. {
  132. return one.X == other.X && one.Y == other.Y;
  133. }
  134. public static bool operator ==(Point2DF left, Point2DF right)
  135. {
  136. return Equals(left, right);
  137. }
  138. public static bool operator !=(Point2DF left, Point2DF right)
  139. {
  140. return !Equals(left, right);
  141. }
  142. /// <summary>
  143. /// 计算两点之间的距离
  144. /// </summary>
  145. /// <param name="one"></param>
  146. /// <param name="other"></param>
  147. /// <returns></returns>
  148. public static float Distance(Point2DF one, Point2DF other)
  149. {
  150. return (float)Math.Sqrt(Math.Pow(one.X - other.X, 2) + Math.Pow(one.Y - other.Y, 2));
  151. }
  152. /// <summary>
  153. /// 求两点之间的中点
  154. /// </summary>
  155. /// <param name="one"></param>
  156. /// <param name="other"></param>
  157. /// <returns></returns>
  158. public static Point2DF MidPoint(Point2DF one, Point2DF other)
  159. {
  160. return new Point2DF((one.X + other.X) / 2, (one.Y + other.Y) / 2);
  161. }
  162. }
  163. /// <summary>
  164. /// 3维点坐标
  165. /// </summary>
  166. [DataContract]
  167. [StructLayout(LayoutKind.Sequential)]
  168. public struct Point3DF : IEquatable<Point3DF>
  169. {
  170. private float _x;
  171. private float _y;
  172. private float _z;
  173. /// <summary>
  174. /// X坐标
  175. /// </summary>
  176. [DataMember]
  177. public float X { get => _x; set => _x = value; }
  178. /// <summary>
  179. /// Y坐标
  180. /// </summary>
  181. [DataMember]
  182. public float Y { get => _y; set => _y = value; }
  183. /// <summary>
  184. /// Z坐标
  185. /// </summary>
  186. [DataMember]
  187. public float Z { get => _z; set => _z = value; }
  188. /// <summary>
  189. /// 构造函数
  190. /// </summary>
  191. /// <param name="x"></param>
  192. /// <param name="y"></param>
  193. /// <param name="z"></param>
  194. public Point3DF(float x, float y, float z)
  195. {
  196. _x = x;
  197. _y = y;
  198. _z = z;
  199. }
  200. /// <summary>
  201. /// 空的点
  202. /// </summary>
  203. public static readonly Point3DF Empty = new Point3DF(0, 0, 0);
  204. /// <summary>
  205. /// 判断是否相等
  206. /// </summary>
  207. /// <param name="other"></param>
  208. /// <returns></returns>
  209. public bool Equals(Point3DF other)
  210. {
  211. return Equals(this, other);
  212. }
  213. /// <summary>
  214. /// 判断是否相等
  215. /// </summary>
  216. /// <param name="obj"></param>
  217. /// <returns></returns>
  218. public override bool Equals(object obj)
  219. {
  220. if (obj == null || !(obj is Point3DF))
  221. {
  222. return false;
  223. }
  224. Point3DF point = (Point3DF)obj;
  225. return Equals(this, point);
  226. }
  227. /// <summary>
  228. /// 获取哈希值
  229. /// </summary>
  230. /// <returns></returns>
  231. public override int GetHashCode()
  232. {
  233. return string.Format("{0}-{1}-{2}", _x, _y, _z).GetHashCode();
  234. }
  235. /// <summary>
  236. /// 转成字符串
  237. /// </summary>
  238. /// <returns></returns>
  239. public override string ToString()
  240. {
  241. return SerializeDeserializeHelper.SerializeToJsonString(this);
  242. }
  243. /// <summary>
  244. /// 从字符串中生成
  245. /// </summary>
  246. /// <param name="str"></param>
  247. /// <returns></returns>
  248. public Point3DF FromString(string str)
  249. {
  250. return SerializeDeserializeHelper.DeserializeJsonResult<Point3DF>(str);
  251. }
  252. /// <summary>
  253. /// 转为bytes
  254. /// </summary>
  255. /// <returns></returns>
  256. public byte[] ToBytes()
  257. {
  258. byte[] result;
  259. using (var stream = new MemoryStream())
  260. {
  261. var writer = new AIStreamWriter(stream);
  262. writer.WriteFloat(_x);
  263. writer.WriteFloat(_y);
  264. writer.WriteFloat(_z);
  265. result = stream.ToArray();
  266. }
  267. return result;
  268. }
  269. /// <summary>
  270. /// 从bytes里重建
  271. /// </summary>
  272. /// <param name="bytes"></param>
  273. /// <returns></returns>
  274. public static Point3DF FromBytes(byte[] bytes)
  275. {
  276. Point3DF result;
  277. using (var stream = new MemoryStream(bytes))
  278. {
  279. stream.Position = 0;
  280. var reader = new AIStreamReader(stream);
  281. var x = reader.ReadFloat();
  282. var y = reader.ReadFloat();
  283. var z = reader.ReadFloat();
  284. result = new Point3DF(x, y, z);
  285. }
  286. return result;
  287. }
  288. public static bool Equals(Point3DF one, Point3DF other)
  289. {
  290. return one.X == other.X && one.Y == other.Y && one.Z == other.Z;
  291. }
  292. public static bool operator ==(Point3DF left, Point3DF right)
  293. {
  294. return Equals(left, right);
  295. }
  296. public static bool operator !=(Point3DF left, Point3DF right)
  297. {
  298. return !Equals(left, right);
  299. }
  300. public static float Distance(Point3DF one, Point3DF other)
  301. {
  302. return (float)Math.Sqrt(Math.Pow(one.X - other.X, 2) + Math.Pow(one.Y - other.Y, 2) + Math.Pow(one.Z - other.Z, 2));
  303. }
  304. }
  305. /// <summary>
  306. /// 矩形框
  307. /// 代替System.Drawing.Rectangle
  308. /// </summary>
  309. [DataContract]
  310. public struct RectF : IEquatable<RectF>
  311. {
  312. private float _left;
  313. private float _top;
  314. private float _right;
  315. private float _bottom;
  316. private float _width;
  317. private float _height;
  318. /// <summary>
  319. /// 面积
  320. /// </summary>
  321. public float Area
  322. {
  323. get => _width * _height;
  324. }
  325. /// <summary>
  326. /// 右
  327. /// </summary>
  328. public float Right { get => _right; }
  329. /// <summary>
  330. /// 下
  331. /// </summary>
  332. public float Bottom { get => _bottom; }
  333. /// <summary>
  334. /// 左
  335. /// </summary>
  336. [DataMember]
  337. public float Left
  338. {
  339. get => _left;
  340. set { _left = value; _right = _left + _width; }
  341. }
  342. /// <summary>
  343. /// 上
  344. /// </summary>
  345. [DataMember]
  346. public float Top
  347. {
  348. get => _top;
  349. set { _top = value; _bottom = _top + _height; }
  350. }
  351. /// <summary>
  352. /// 宽度
  353. /// </summary>
  354. [DataMember]
  355. public float Width
  356. {
  357. get => _width;
  358. set
  359. {
  360. if (value < 0)
  361. {
  362. throw new ArgumentException("width", "Width should be greater than 0.");
  363. };
  364. _width = value;
  365. _right = _left + _width;
  366. }
  367. }
  368. /// <summary>
  369. /// 高度
  370. /// </summary>
  371. [DataMember]
  372. public float Height
  373. {
  374. get => _height;
  375. set
  376. {
  377. if (value < 0)
  378. {
  379. throw new ArgumentException("height", "Height should be greater than 0");
  380. };
  381. _height = value;
  382. _bottom = _top + _height;
  383. }
  384. }
  385. /// <summary>
  386. /// 构造函数
  387. /// </summary>
  388. /// <param name="left"></param>
  389. /// <param name="top"></param>
  390. /// <param name="width"></param>
  391. /// <param name="height"></param>
  392. public RectF(float left, float top, float width, float height)
  393. {
  394. if (width < 0)
  395. {
  396. throw new ArgumentException("width", "Width should be greater than 0.");
  397. }
  398. if (height < 0)
  399. {
  400. throw new ArgumentException("height", "Height should be greater than 0");
  401. }
  402. _left = left;
  403. _top = top;
  404. _width = width;
  405. _height = height;
  406. _right = left + width;
  407. _bottom = top + height;
  408. }
  409. /// <summary>
  410. /// 判断是否为未被初始化的Rect结构
  411. /// </summary>
  412. /// <returns></returns>
  413. public bool IsEmpty()
  414. {
  415. return Equals(this, Empty);
  416. }
  417. /// <summary>
  418. /// 空矩形框
  419. /// </summary>
  420. public static readonly RectF Empty = new RectF(0, 0, 0, 0);
  421. /// <summary>
  422. /// 判断是否相等
  423. /// </summary>
  424. /// <param name="rect"></param>
  425. /// <returns></returns>
  426. public bool Equals(RectF rect)
  427. {
  428. return Equals(this, rect);
  429. }
  430. /// <summary>
  431. /// 判断是否相等
  432. /// </summary>
  433. /// <param name="obj"></param>
  434. /// <returns></returns>
  435. public override bool Equals(object obj)
  436. {
  437. if (obj == null || !(obj is RectF))
  438. {
  439. return false;
  440. }
  441. RectF point = (RectF)obj;
  442. return Equals(this, point);
  443. }
  444. /// <summary>
  445. /// 获得哈希值
  446. /// </summary>
  447. /// <returns></returns>
  448. public override int GetHashCode()
  449. {
  450. return string.Format("{0}-{1}-{2}-{3}", _left, _right, _width, _height).GetHashCode();
  451. }
  452. /// <summary>
  453. /// 转成字符串
  454. /// </summary>
  455. /// <returns></returns>
  456. public override string ToString()
  457. {
  458. return SerializeDeserializeHelper.SerializeToJsonString(this);
  459. }
  460. /// <summary>
  461. /// 从字符串中生成
  462. /// </summary>
  463. /// <param name="str"></param>
  464. /// <returns></returns>
  465. public static RectF FromString(string str)
  466. {
  467. return SerializeDeserializeHelper.DeserializeJsonResult<RectF>(str);
  468. }
  469. /// <summary>
  470. /// 转为bytes
  471. /// </summary>
  472. /// <returns></returns>
  473. public byte[] ToBytes()
  474. {
  475. byte[] result;
  476. using (var stream = new MemoryStream())
  477. {
  478. var writer = new AIStreamWriter(stream);
  479. writer.WriteFloat(_left);
  480. writer.WriteFloat(_top);
  481. writer.WriteFloat(_width);
  482. writer.WriteFloat(_height);
  483. result = stream.ToArray();
  484. }
  485. return result;
  486. }
  487. /// <summary>
  488. /// 从bytes里重建
  489. /// </summary>
  490. /// <param name="bytes"></param>
  491. /// <returns></returns>
  492. public static RectF FromBytes(byte[] bytes)
  493. {
  494. RectF result;
  495. using (var stream = new MemoryStream(bytes))
  496. {
  497. stream.Position = 0;
  498. var reader = new AIStreamReader(stream);
  499. var left = reader.ReadFloat();
  500. var top = reader.ReadFloat();
  501. var width = reader.ReadFloat();
  502. var height = reader.ReadFloat();
  503. result = new RectF(left, top, width, height);
  504. }
  505. return result;
  506. }
  507. public static bool Equals(RectF one, RectF other)
  508. {
  509. return one.Left == other.Left && one.Top == other.Top && one.Width == other.Width && one.Height == other.Height;
  510. }
  511. public static bool operator ==(RectF left, RectF right)
  512. {
  513. return Equals(left, right);
  514. }
  515. public static bool operator !=(RectF left, RectF right)
  516. {
  517. return !Equals(left, right);
  518. }
  519. /// <summary>
  520. /// 在原来的rect的基础上,上下左右一起偏移指定像素数
  521. /// 当offset为正时,为一起向正方向偏移(向右或向下)
  522. /// 当offset为负时,为一起向负方向偏移(向左或向上)
  523. /// </summary>
  524. /// <param name="offsetX"></param>
  525. /// <param name="offsetY"></param>
  526. /// <returns></returns>
  527. public static RectF Offset(RectF rect, float offsetX, float offsetY)
  528. {
  529. return new RectF(rect.Left + offsetX, rect.Top + offsetY, rect.Width, rect.Height);
  530. }
  531. /// <summary>
  532. /// 以原来的rect中心为中心,上下左右一起向外扩张或内缩指定像素数
  533. /// 当offset为正时,为外扩,当offset为负时,为内缩
  534. /// </summary>
  535. /// <param name="rect"></param>
  536. /// <param name="offset"></param>
  537. /// <returns></returns>
  538. public static RectF AdjustRect(RectF rect, float offsetX, float offsetY)
  539. {
  540. // 往内缩时,不能缩成负的了
  541. if (offsetX < -rect.Width / 2)
  542. {
  543. offsetX = -rect.Width / 2;
  544. }
  545. if (offsetY < -rect.Height / 2)
  546. {
  547. offsetY = -rect.Height / 2;
  548. }
  549. var correctedLeft = rect.Left - offsetX;
  550. var correctedRight = rect.Right + offsetX;
  551. var correctedTop = rect.Top - offsetY;
  552. var correctedBottom = rect.Bottom + offsetY;
  553. var correctedWidth = correctedRight - correctedLeft;
  554. var correctedHeight = correctedBottom - correctedTop;
  555. return new RectF(correctedLeft, correctedTop, correctedWidth, correctedHeight);
  556. }
  557. /// <summary>
  558. /// 以clipBound为边界,截断当前rect
  559. /// 即,左上右下角,改为不超过clipBound所限定的范围
  560. /// </summary>
  561. /// <param name="rect"></param>
  562. /// <param name="clipBound"></param>
  563. /// <returns></returns>
  564. public static RectF Clip(RectF rect, RectF clipBound)
  565. {
  566. var left = Math.Min(Math.Max(rect.Left, clipBound.Left), clipBound.Right);
  567. var right = Math.Min(Math.Max(rect.Right, clipBound.Left), clipBound.Right);
  568. var top = Math.Min(Math.Max(rect.Top, clipBound.Top), clipBound.Bottom);
  569. var bottom = Math.Min(Math.Max(rect.Bottom, clipBound.Top), clipBound.Bottom);
  570. var width = right - left;
  571. var height = bottom - top;
  572. return new RectF(left, top, width, height);
  573. }
  574. /// <summary>
  575. /// 计算两个rect的相交部分
  576. /// </summary>
  577. /// <param name="a"></param>
  578. /// <param name="b"></param>
  579. /// <returns></returns>
  580. public static RectF Intersection(RectF a, RectF b)
  581. {
  582. var left = Math.Max(a.Left, b.Left);
  583. var top = Math.Max(a.Top, b.Top);
  584. var right = Math.Min(a.Right, b.Right);
  585. var bottom = Math.Min(a.Bottom, b.Bottom);
  586. if (left > right || top > bottom)
  587. {
  588. return RectF.Empty;
  589. }
  590. var width = right - left;
  591. var height = bottom - top;
  592. return new RectF(left, top, width, height);
  593. }
  594. /// <summary>
  595. /// 计算两个rect的并集
  596. /// </summary>
  597. /// <param name="a"></param>
  598. /// <param name="b"></param>
  599. /// <returns></returns>
  600. public static RectF Union(RectF a, RectF b)
  601. {
  602. var left = Math.Min(a.Left, b.Left);
  603. var top = Math.Min(a.Top, b.Top);
  604. var right = Math.Max(a.Right, b.Right);
  605. var bottom = Math.Max(a.Bottom, b.Bottom);
  606. var width = right - left;
  607. var height = bottom - top;
  608. return new RectF(left, top, width, height);
  609. }
  610. /// <summary>
  611. /// 计算两个rect的IOU
  612. /// 注意:计算IOU时,分子是a和b的交集的面积(= Intersect(a,b).Area)
  613. /// 分母是a和b的并集的面积(≠Union(a,b).Area,而应该是a.Area+b.Area-Intersection(a,b).Area)
  614. /// </summary>
  615. /// <param name="a"></param>
  616. /// <param name="b"></param>
  617. /// <returns></returns>
  618. public static float CalcIOU(RectF a, RectF b)
  619. {
  620. var intersection = Intersection(a, b);
  621. var areaInter = intersection.Area;
  622. var areaUnion = a.Area + b.Area - areaInter;
  623. if (areaUnion <= 0)
  624. {
  625. return 0;
  626. }
  627. else
  628. {
  629. return areaInter * 1.0f / areaUnion;
  630. }
  631. }
  632. }
  633. }