SmplMeasureInclution.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using OTSCLRINTERFACE;
  2. using OTSCommon.DBOperate.Model;
  3. using OTSDataType;
  4. using OTSMeasureApp._0_OTSModel.OTSDataType;
  5. using OTSModelSharp.ServiceCenter;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.Linq;
  10. using static OTSDataType.otsdataconst;
  11. namespace OTSModelSharp
  12. {
  13. class CSmplMeasureIncA : CSmplMeasure
  14. {
  15. public CSmplMeasureIncA(string a_strWorkingFolder, COTSSample a_pSample) : base(a_strWorkingFolder, a_pSample)
  16. {
  17. SetWorkingFolder(a_strWorkingFolder);
  18. SetSample(a_pSample);
  19. m_classifyEngine = new CClassifyEngine();
  20. }
  21. public override void ClassifyFieldParticles(COTSField curFldData)
  22. {
  23. try
  24. {
  25. var anylysisparts = curFldData.GetListAnalysisParticles();
  26. var finalParts = new List<COTSParticleClr>();
  27. int nSize = anylysisparts.Count();
  28. // go through all analysis particles
  29. for (int i = 0; i < nSize; ++i)
  30. {
  31. string libname = m_Sample.GetMsrParams().GetSTDName();
  32. if (!IsLowCounts(anylysisparts[i]))
  33. {
  34. ClassifyIncAParticle(anylysisparts[i], libname);
  35. finalParts.Add(anylysisparts[i]);
  36. }
  37. }
  38. curFldData.SetListAnalysisParticles(finalParts);
  39. }
  40. catch (Exception e)
  41. {
  42. log.Info(" classify failed. " + e.Message);
  43. }
  44. }
  45. public bool ClassifyIncAParticle(COTSParticleClr particle, string libname)// classify particles
  46. {
  47. int steelTech = (int)m_Sample.GetMsrParams().GetSteelTechnology();
  48. particle.SetBasicClassifyId((int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED);
  49. if (m_Sample.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.InclutionPlusExpressionParse)
  50. {
  51. if (libname != "NoSTDDB")
  52. {
  53. //var m_classifyEngine = new CClassifyEngine();
  54. IClassifyEngine engine = m_classifyEngine.GetExpressionClassifyEngine(libname);
  55. engine.ClassifyByExpression(particle);
  56. }
  57. if (particle.GetBasicClassifyId() == (int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED || particle.GetBasicClassifyId() == (int)OTS_PARTICLE_TYPE.UNCLASSIFY)
  58. {
  59. IClassifyEngine engine;
  60. engine = m_classifyEngine.GetIncClassifyEngine();
  61. engine.ClassifyIncA(particle, steelTech);
  62. }
  63. }
  64. else if (m_Sample.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.ExpressionParse)
  65. {
  66. if (libname != "NoSTDDB")
  67. {
  68. //var m_classifyEngine = new CClassifyEngine();
  69. IClassifyEngine engine = m_classifyEngine.GetExpressionClassifyEngine(libname);
  70. engine.ClassifyByExpression(particle);
  71. }
  72. }
  73. else if (m_Sample.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.SpectrumMatch)
  74. {
  75. if (libname != "NoSTDDB")
  76. {
  77. //var m_classifyEngine = new CClassifyEngine();
  78. IClassifyEngine engine = m_classifyEngine.GetSpectrumCompareEngine(libname);
  79. engine.ClassifyBySpectrum(particle);
  80. }
  81. }
  82. else if (m_Sample.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.InclustionEngine)
  83. {
  84. IClassifyEngine engine;
  85. engine = m_classifyEngine.GetIncClassifyEngine();
  86. engine.ClassifyIncA(particle, steelTech);
  87. }
  88. return true;
  89. }
  90. public override void CollectParticlesXrayData(COTSField curFldData)
  91. {
  92. LinkParticlesByGB30834Standard(curFldData);
  93. //collect the particles xray data
  94. base.CollectParticlesXrayData(curFldData);
  95. var parts = curFldData.GetListAnalysisParticles();
  96. string libname = m_Sample.GetMsrParams().GetSTDName();
  97. //process the maxeds rules
  98. if (m_Sample.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.InclutionPlusExpressionParse ||
  99. m_Sample.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.ExpressionParse)
  100. {
  101. if (libname != "NoSTDDB")
  102. {
  103. IClassifyEngine engine = m_classifyEngine.GetExpressionClassifyEngine(libname);
  104. double maxedstime = 0;
  105. List<COTSParticleClr> maxedsparts = new List<COTSParticleClr>();
  106. foreach (var p in parts)
  107. {
  108. if (engine.IfNeedMaxEDS(p, ref maxedstime))
  109. {
  110. maxedsparts.Add(p);
  111. }
  112. }
  113. if (maxedsparts.Count > 0)
  114. {
  115. log.Warn("Begin to collect MaxEDS particles:" + maxedsparts.Count + "(" + maxedstime.ToString() + ") on Point mode");
  116. m_EDSController.GetXRayByParts(maxedsparts, (uint)maxedstime, true);
  117. }
  118. }
  119. }
  120. }
  121. class COTSLinkedParticle
  122. {
  123. public COTSLinkedParticle(COTSParticleClr mypart)
  124. {
  125. _myPart = mypart;
  126. }
  127. private bool _merged=false;
  128. private COTSParticleClr _myPart;
  129. private List<COTSLinkedParticle> _linkedParts = new List<COTSLinkedParticle>();
  130. public void AddLinkedParticle(COTSLinkedParticle part)
  131. {
  132. if (!_linkedParts.Contains(part))
  133. {
  134. _linkedParts.Add(part);
  135. }
  136. }
  137. public List<COTSLinkedParticle> GetLinkedParticles() { return _linkedParts; }
  138. public COTSParticleClr MyPart { get => _myPart; set => _myPart = value; }
  139. public bool IsMerged { get => _merged; set => _merged = value; }
  140. internal void MergeLinkedParticles()
  141. {
  142. List<COTSSegmentClr > segs= new List<COTSSegmentClr>();
  143. int l=0, t=0, r=0, b = 0;
  144. foreach (var seg in _myPart.GetFeature().GetSegmentsList())
  145. {
  146. segs.Add(seg);
  147. }
  148. _myPart.GetOTSRect(ref l, ref t, ref r, ref b);
  149. Rectangle rec =(Rectangle)_myPart.GetParticleRect();
  150. int l1 = 0, t1 = 0, r1 = 0, b1 = 0;
  151. COTSParticleClr biggestPart = _myPart;
  152. foreach (var p in _linkedParts)
  153. {
  154. p.MyPart.GetOTSRect(ref l1,ref t1, ref r1, ref b1);
  155. if (l1 < l) l = l1;
  156. if(t1 > t) t = t1;
  157. if(r1 > r) r = r1;
  158. if (b1 < b) b = b1;
  159. if(p.MyPart.GetActualArea() > biggestPart.GetActualArea())
  160. {
  161. biggestPart = p.MyPart;
  162. }
  163. foreach (var seg in p.MyPart.GetFeature().GetSegmentsList())
  164. {
  165. if (!segs.Contains(seg))
  166. {
  167. segs.Add(seg);
  168. }
  169. }
  170. }
  171. _myPart.SetOTSRect(l, t, r, b);
  172. _myPart.GetFeature().SetSegmentsList(segs,true);
  173. _myPart.CalCoverRectFromSegment();
  174. _myPart.SetXRayPos(biggestPart.GetXRayPos());
  175. }
  176. }
  177. private void LinkParticlesByGB30834Standard(COTSField curFldData)
  178. {
  179. var parts = curFldData.GetInitialParticles();
  180. if (parts.Count == 0)
  181. {
  182. return;
  183. }
  184. List<COTSLinkedParticle> linkedParticles = new List<COTSLinkedParticle>();
  185. foreach (var part in parts)
  186. {
  187. COTSLinkedParticle linkedParticle = new COTSLinkedParticle(part);
  188. linkedParticles.Add(linkedParticle);
  189. }
  190. for (int i = 0; i < linkedParticles.Count; i++)
  191. {
  192. var part = linkedParticles[i];
  193. int l = 0, r = 0, t = 0, b = 0;
  194. part.MyPart.GetOTSRect(ref l, ref t, ref r, ref b);
  195. COTSRect partrect = new COTSRect(l, t, r, b);
  196. //PointF centerPoint = partrect.GetCenterPoint();
  197. Point centerPoint =(Point) part.MyPart.GetXRayPos();
  198. for (int j = i + 1; j < linkedParticles.Count; j++)
  199. {
  200. var part1 = linkedParticles[j];
  201. part1.MyPart.GetOTSRect(ref l, ref t, ref r, ref b);
  202. COTSRect partrect1 = new COTSRect(l, t, r, b);
  203. //PointF centerPoint1 = partrect1.GetCenterPoint();
  204. Point centerPoint1 = (Point)part1.MyPart.GetXRayPos();
  205. double d = Math.Sqrt(Math.Pow(centerPoint.X - centerPoint1.X, 2) + Math.Pow(centerPoint.Y - centerPoint1.Y, 2));
  206. if (Math.Abs(part.MyPart.GetAveGray() - part1.MyPart.GetAveGray()) > 10 )
  207. {
  208. // If the average gray values are not similar, do not link them
  209. continue;
  210. }
  211. if (d - part.MyPart.GetDMAX() / 2 - part1.MyPart.GetDMAX() / 2 < 40 && d > part.MyPart.GetDMAX() / 2+part1.MyPart.GetDMAX()/2)
  212. {
  213. double aspect1 = part.MyPart.GetDMAX() / part.MyPart.GetDMIN();
  214. double aspect2 = part1.MyPart.GetDMAX() / part.MyPart.GetDMIN();
  215. var angle1 = part.MyPart.GetORIENTATION();
  216. var angle2= part1.MyPart.GetORIENTATION();
  217. if (aspect1 > 2 && aspect2 > 2)
  218. {
  219. // If the aspect ratio of both particles is greater than 2 and their orientations are similar, link them
  220. var angle = calculateTwoPointAngle(centerPoint, centerPoint1);
  221. if (Math.Abs(angle - angle1) < 5 && Math.Abs(angle - angle2) < 5)
  222. {
  223. part.AddLinkedParticle(part1);
  224. part1.AddLinkedParticle(part);
  225. continue;
  226. }
  227. else
  228. {
  229. continue;
  230. }
  231. }
  232. else if (aspect1 > 2 && aspect2 < 2)
  233. {
  234. var angle = calculateTwoPointAngle(centerPoint, centerPoint1);
  235. if (Math.Abs(angle - angle1) < 5)
  236. {
  237. part.AddLinkedParticle(part1);
  238. part1.AddLinkedParticle(part);
  239. continue;
  240. }
  241. else
  242. {
  243. continue;
  244. }
  245. }
  246. else if (aspect1 < 2 && aspect2 > 2)
  247. {
  248. var angle = calculateTwoPointAngle(centerPoint, centerPoint1);
  249. if (Math.Abs(angle - angle2) < 5)
  250. {
  251. part.AddLinkedParticle(part1);
  252. part1.AddLinkedParticle(part);
  253. continue;
  254. }
  255. else
  256. {
  257. continue;
  258. }
  259. }
  260. else if (aspect1 <= 2 && aspect2 <=2)
  261. {
  262. if (Math.Abs(part.MyPart.GetActualArea() - part1.MyPart.GetActualArea()) < 10)
  263. {
  264. // Link the particles
  265. part.AddLinkedParticle(part1);
  266. part1.AddLinkedParticle(part);
  267. continue;
  268. }
  269. else
  270. {
  271. // If the areas are not similar, do not link them
  272. continue;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. //DFS all the linked particles
  279. Stack<COTSLinkedParticle> partStack=new Stack<COTSLinkedParticle>();
  280. List<COTSParticleClr> mergedParts = new List<COTSParticleClr>();
  281. List<COTSParticleClr> finalOTSParts = new List<COTSParticleClr>();
  282. foreach (var pParticle in linkedParticles)
  283. {
  284. if (!pParticle.IsMerged)
  285. {
  286. COTSLinkedParticle finalpart = new COTSLinkedParticle(pParticle.MyPart);
  287. foreach (var connP in pParticle.GetLinkedParticles())
  288. {
  289. if (!connP.IsMerged)
  290. {
  291. partStack.Push(connP);
  292. }
  293. }
  294. while (partStack.Count > 0)
  295. {
  296. COTSLinkedParticle currpart = partStack.Pop();
  297. currpart.IsMerged = true;
  298. finalpart.AddLinkedParticle(currpart);
  299. if (currpart.GetLinkedParticles().Count>0)
  300. {
  301. foreach (var connP in currpart.GetLinkedParticles())
  302. {
  303. if (!connP.IsMerged)
  304. {
  305. partStack.Push(connP);
  306. }
  307. }
  308. }
  309. }
  310. if (finalpart.GetLinkedParticles().Count > 1)
  311. {
  312. //merge the particles
  313. finalpart.MergeLinkedParticles();
  314. mergedParts.Add(finalpart.MyPart);
  315. }
  316. else
  317. {
  318. finalOTSParts.Add(finalpart.MyPart); // If no linked particles, just add the original particle
  319. }
  320. }
  321. }
  322. curFldData.CalParticleImageProp(mergedParts);
  323. finalOTSParts.AddRange(mergedParts);
  324. curFldData.SetListAnalysisParticles(finalOTSParts);
  325. }
  326. public double calculateTwoPointAngle(PointF p1, PointF p2)
  327. {
  328. if (p1.Y > p2.Y)
  329. {
  330. PointF temp = p1;
  331. p1 = p2;
  332. p2 = temp;
  333. }
  334. double angle = Math.Atan2(p2.Y - p1.Y, p2.X - p1.X) * 180 / Math.PI;
  335. if (angle < 0)
  336. {
  337. angle += 360;
  338. }
  339. return angle;
  340. }
  341. }
  342. }