TransducerImgPlaneToWorkCoordMatrix.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using AI.Common;
  3. using AI.Common.Tools;
  4. namespace AI.Reconstruction
  5. {
  6. /// <summary>
  7. /// 探头位姿
  8. /// 以探头的中心点为原点,探头深度方向为y轴正方向,探头标记点方向为x轴负方向,建立坐标系
  9. /// 该位姿表示的是将该成像平面上的点转换到目标坐标系下的转换矩阵
  10. /// 图像像素坐标,应该 (x - w/2) * spacingx,-y * spacingy 才能转换到成像平面
  11. /// </summary>
  12. public class TransducerImgPlaneToWorkCoordMatrix
  13. {
  14. #region private
  15. private float _tx;
  16. private float _ty;
  17. private float _tz;
  18. private float _r11;
  19. private float _r12;
  20. private float _r13;
  21. private float _r21;
  22. private float _r22;
  23. private float _r23;
  24. private float _r31;
  25. private float _r32;
  26. private float _r33;
  27. private float[] _translationVector;
  28. private float[] _rotationMatrix;
  29. #endregion
  30. #region property
  31. /// <summary>
  32. /// x方向的平移
  33. /// </summary>
  34. public float TransX { get => _tx; set => _tx = value; }
  35. /// <summary>
  36. /// y方向的平移
  37. /// </summary>
  38. public float TransY { get => _ty; set => _ty = value; }
  39. /// <summary>
  40. /// z方向的平移
  41. /// </summary>
  42. public float TransZ { get => _tz; set => _tz = value; }
  43. /// <summary>
  44. /// 旋转矩阵的第一行第一列
  45. /// </summary>
  46. public float Rotate11 { get => _r11; set => _r11 = value; }
  47. /// <summary>
  48. /// 旋转矩阵的第一行第二列
  49. /// </summary>
  50. public float Rotate12 { get => _r12; set => _r12 = value; }
  51. /// <summary>
  52. /// 旋转矩阵的第一行第三列
  53. /// </summary>
  54. public float Rotate13 { get => _r13; set => _r13 = value; }
  55. /// <summary>
  56. /// 旋转矩阵的第二行第一列
  57. /// </summary>
  58. public float Rotate21 { get => _r21; set => _r21 = value; }
  59. /// <summary>
  60. /// 旋转矩阵的第二行第二列
  61. /// </summary>
  62. public float Rotate22 { get => _r22; set => _r22 = value; }
  63. /// <summary>
  64. /// 旋转矩阵的第二行第三列
  65. /// </summary>
  66. public float Rotate23 { get => _r23; set => _r23 = value; }
  67. /// <summary>
  68. /// 旋转矩阵的第三行第一列
  69. /// </summary>
  70. public float Rotate31 { get => _r31; set => _r31 = value; }
  71. /// <summary>
  72. /// 旋转矩阵的第三行第二列
  73. /// </summary>
  74. public float Rotate32 { get => _r32; set => _r32 = value; }
  75. /// <summary>
  76. /// 旋转矩阵的第三行第三列
  77. /// </summary>
  78. public float Rotate33 { get => _r33; set => _r33 = value; }
  79. /// <summary>
  80. /// 平移向量
  81. /// </summary>
  82. public float[] TranslationVector
  83. {
  84. get => _translationVector;
  85. set => _translationVector = value;
  86. }
  87. /// <summary>
  88. /// 旋转矩阵
  89. /// </summary>
  90. public float[] RotationMatrix
  91. {
  92. get => _rotationMatrix;
  93. set => _rotationMatrix = value;
  94. }
  95. #endregion
  96. #region constructor
  97. /// <summary>
  98. /// 构造函数,只有平移
  99. /// </summary>
  100. /// <param name="tx"></param>
  101. /// <param name="ty"></param>
  102. /// <param name="tz"></param>
  103. public TransducerImgPlaneToWorkCoordMatrix(float tx, float ty, float tz)
  104. {
  105. _tx = tx;
  106. _ty = ty;
  107. _tz = tz;
  108. _r11 = 1;
  109. _r12 = 0;
  110. _r13 = 0;
  111. _r21 = 0;
  112. _r22 = 1;
  113. _r23 = 0;
  114. _r31 = 0;
  115. _r32 = 0;
  116. _r33 = 1;
  117. GenRotationMatrix();
  118. GenTranslationVector();
  119. }
  120. /// <summary>
  121. /// 构造函数,只有旋转
  122. /// </summary>
  123. /// <param name="r11"></param>
  124. /// <param name="r12"></param>
  125. /// <param name="r13"></param>
  126. /// <param name="r21"></param>
  127. /// <param name="r22"></param>
  128. /// <param name="r23"></param>
  129. /// <param name="r31"></param>
  130. /// <param name="r32"></param>
  131. /// <param name="r33"></param>
  132. public TransducerImgPlaneToWorkCoordMatrix(float r11, float r12, float r13,
  133. float r21, float r22, float r23, float r31, float r32, float r33)
  134. {
  135. _tx = 0;
  136. _ty = 0;
  137. _tz = 0;
  138. _r11 = r11;
  139. _r12 = r12;
  140. _r13 = r13;
  141. _r21 = r21;
  142. _r22 = r22;
  143. _r23 = r23;
  144. _r31 = r31;
  145. _r32 = r32;
  146. _r33 = r33;
  147. GenRotationMatrix();
  148. GenTranslationVector();
  149. }
  150. /// <summary>
  151. /// 构造函数,旋转和平移都有
  152. /// </summary>
  153. /// <param name="tx"></param>
  154. /// <param name="ty"></param>
  155. /// <param name="tz"></param>
  156. /// <param name="r11"></param>
  157. /// <param name="r12"></param>
  158. /// <param name="r13"></param>
  159. /// <param name="r21"></param>
  160. /// <param name="r22"></param>
  161. /// <param name="r23"></param>
  162. /// <param name="r31"></param>
  163. /// <param name="r32"></param>
  164. /// <param name="r33"></param>
  165. public TransducerImgPlaneToWorkCoordMatrix(float tx, float ty, float tz,
  166. float r11, float r12, float r13, float r21, float r22, float r23,
  167. float r31, float r32, float r33)
  168. {
  169. _tx = tx;
  170. _ty = ty;
  171. _tz = tz;
  172. _r11 = r11;
  173. _r12 = r12;
  174. _r13 = r13;
  175. _r21 = r21;
  176. _r22 = r22;
  177. _r23 = r23;
  178. _r31 = r31;
  179. _r32 = r32;
  180. _r33 = r33;
  181. GenRotationMatrix();
  182. GenTranslationVector();
  183. }
  184. /// <summary>
  185. /// 构造函数
  186. /// </summary>
  187. /// <param name="matrix"></param>
  188. /// <param name="len"></param>
  189. public TransducerImgPlaneToWorkCoordMatrix(float[] matrix, int len)
  190. {
  191. if (matrix.Length != len)
  192. {
  193. throw new ArgumentException("the matrix length(" + matrix.Length + ") is not equal to " + len + " .");
  194. }
  195. if (len == 3)
  196. {
  197. _tx = matrix[0];
  198. _ty = matrix[1];
  199. _tz = matrix[2];
  200. _r11 = 1;
  201. _r12 = 0;
  202. _r13 = 0;
  203. _r21 = 0;
  204. _r22 = 1;
  205. _r23 = 0;
  206. _r31 = 0;
  207. _r32 = 0;
  208. _r33 = 1;
  209. }
  210. else if (len == 12)
  211. {
  212. _tx = matrix[0];
  213. _ty = matrix[1];
  214. _tz = matrix[2];
  215. _r11 = matrix[3];
  216. _r12 = matrix[4];
  217. _r13 = matrix[5];
  218. _r21 = matrix[6];
  219. _r22 = matrix[7];
  220. _r23 = matrix[8];
  221. _r31 = matrix[9];
  222. _r32 = matrix[10];
  223. _r33 = matrix[11];
  224. }
  225. else
  226. {
  227. throw new ArgumentException("unexpected matrix length(" + matrix.Length + "), the matrix length should be 3 or 12.");
  228. }
  229. GenRotationMatrix();
  230. GenTranslationVector();
  231. }
  232. #endregion
  233. #region public
  234. /// <summary>
  235. /// 将某个点从成像平面坐标系转换到真实物理坐标系下
  236. /// </summary>
  237. /// <param name="point"></param>
  238. /// <returns></returns>
  239. public Point3DF Transform(Point3DF point)
  240. {
  241. float x = point.X;
  242. float y = point.Y;
  243. float z = point.Z;
  244. float xdst = _r11 * x + _r12 * y + _r13 * z + _tx;
  245. float ydst = _r21 * x + _r22 * y + _r23 * z + _ty;
  246. float zdst = _r31 * x + _r32 * y + _r33 * z + _tz;
  247. return new Point3DF(xdst, ydst, zdst);
  248. }
  249. /// <summary>
  250. /// 判断三个变换矩阵所代表的探头成像平面平行
  251. /// </summary>
  252. /// <param name="matrix1"></param>
  253. /// <param name="matrix2"></param>
  254. /// <returns></returns>
  255. public bool ImagePlaneParallels(TransducerImgPlaneToWorkCoordMatrix matrix1, TransducerImgPlaneToWorkCoordMatrix matrix2)
  256. {
  257. if (!MathHelper.AlmostEqual(matrix1.Rotate11, _r11) || !MathHelper.AlmostEqual(matrix2.Rotate11, _r11))
  258. {
  259. return false;
  260. }
  261. if (!MathHelper.AlmostEqual(matrix1.Rotate12, _r12) || !MathHelper.AlmostEqual(matrix2.Rotate12, _r12))
  262. {
  263. return false;
  264. }
  265. if (!MathHelper.AlmostEqual(matrix1.Rotate13, _r13) || !MathHelper.AlmostEqual(matrix2.Rotate13, _r13))
  266. {
  267. return false;
  268. }
  269. if (!MathHelper.AlmostEqual(matrix1.Rotate21, _r21) || !MathHelper.AlmostEqual(matrix2.Rotate21, _r21))
  270. {
  271. return false;
  272. }
  273. if (!MathHelper.AlmostEqual(matrix1.Rotate22, _r22) || !MathHelper.AlmostEqual(matrix2.Rotate22, _r22))
  274. {
  275. return false;
  276. }
  277. if (!MathHelper.AlmostEqual(matrix1.Rotate23, _r23) || !MathHelper.AlmostEqual(matrix2.Rotate23, _r23))
  278. {
  279. return false;
  280. }
  281. if (!MathHelper.AlmostEqual(matrix1.Rotate31, _r31) || !MathHelper.AlmostEqual(matrix2.Rotate31, _r31))
  282. {
  283. return false;
  284. }
  285. if (!MathHelper.AlmostEqual(matrix1.Rotate32, _r32) || !MathHelper.AlmostEqual(matrix2.Rotate32, _r32))
  286. {
  287. return false;
  288. }
  289. if (!MathHelper.AlmostEqual(matrix1.Rotate33, _r33) || !MathHelper.AlmostEqual(matrix2.Rotate33, _r33))
  290. {
  291. return false;
  292. }
  293. return true;
  294. }
  295. #endregion
  296. #region private
  297. private void GenRotationMatrix()
  298. {
  299. _rotationMatrix = new float[9];
  300. _rotationMatrix[0] = _r11;
  301. _rotationMatrix[1] = _r12;
  302. _rotationMatrix[2] = _r13;
  303. _rotationMatrix[3] = _r21;
  304. _rotationMatrix[4] = _r22;
  305. _rotationMatrix[5] = _r23;
  306. _rotationMatrix[6] = _r31;
  307. _rotationMatrix[7] = _r32;
  308. _rotationMatrix[8] = _r33;
  309. }
  310. private void GenTranslationVector()
  311. {
  312. _translationVector = new float[3];
  313. _translationVector[0] = _tx;
  314. _translationVector[1] = _ty;
  315. _translationVector[2] = _tz;
  316. }
  317. #endregion
  318. }
  319. }