TrioCal.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. #include "stdafx.h"
  2. #include "TrioCal.h"
  3. #include "OTSFileSys.h"
  4. #include "OTSHelper.h"
  5. #include "PropParam.h"
  6. #include "SmplMsrResultFileMgr.h"
  7. #include<math.h>
  8. //计算三元相图
  9. namespace OTSMODEL
  10. {
  11. TrioCal::TrioCal(CReportProjFileMgr * rptMgrPtr)
  12. {
  13. m_rptMgrPtr = rptMgrPtr;
  14. }
  15. TrioCal::~TrioCal()
  16. {
  17. }
  18. // 主要功能为解决多数据源问题
  19. CGridDatasList TrioCal::CompParticleTrioInfo()
  20. {
  21. // use the dll resource
  22. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  23. //定义返回的Grid数据结构
  24. CGridDatasList listGridData;
  25. listGridData.clear();
  26. // depart compound source name
  27. std::vector<int> listWorkIndex;
  28. std::vector<int> listPos;
  29. //std::vector<CString> listDataSourceOut = m_rptMgrPtr->GetFullDataSource(a_pGridIn, listWorkIndex, listPos);
  30. int dataSourceId;//用户在propertygrid(sourcegrid)界面上选择的样品下拉列表ID号,如果是有需要对比的复合样品源,会在第0项出现。
  31. CPropParamPtr prop = m_rptMgrPtr->GetCurrentPropParam();
  32. dataSourceId = prop->GetDataSourceId();
  33. std::vector<CString> listDataSource = m_rptMgrPtr->GetCurrentPropParam()->GetDataSourceList();
  34. int nSelectedDataSourceIndex = prop->GetDataSourceId();
  35. CString sDataSourceNames = listDataSource[nSelectedDataSourceIndex];
  36. std::vector<CString> listSelectedDataSource = COTSHelper::SplitString(sDataSourceNames, _T("+"));
  37. int nIndex = 0;
  38. for (auto strDataSourceName : listSelectedDataSource)
  39. {
  40. CGridDataPtr pGridData = CGridDataPtr(new CGridData());
  41. // data source id
  42. std::vector<CString> listDataSource;
  43. listDataSource.clear();
  44. listDataSource = prop->GetDataSourceList();
  45. pGridData->SetDataSourceList(listDataSource);
  46. int nDataSourceId = prop->GetDataSourceId();
  47. pGridData->SetDataSourceId(nDataSourceId);
  48. COTSParticleList listParticleAll = m_rptMgrPtr->GetAnalysisParticleList(strDataSourceName);//找到这个样品中所有的有效颗粒
  49. CPosXraysList listXray= m_rptMgrPtr->GetAnalysisXrayList(strDataSourceName);//找到所有颗粒对应的Xray
  50. map<int, CPosXrayPtr> particleInfoMap;
  51. for (auto p : listParticleAll)
  52. {
  53. //using fieldid combined analysisid to vector key
  54. int xrayId = p->GetAnalysisId();
  55. int fieldId = p->GetFieldId();
  56. if (xrayId < 0 || fieldId < 0) { continue; }
  57. CString fieldidwithxrayid = "";
  58. fieldidwithxrayid.Format(_T("%d%d"), fieldId, xrayId);
  59. int i_fieldidwithxrayid = atoi(fieldidwithxrayid);
  60. auto itr = std::find_if(listXray.begin(), listXray.end(), [xrayId, fieldId](CPosXrayPtr x) { return (x->GetIndex() == xrayId && x->GetScanFieldId() == fieldId); });
  61. if (itr != listXray.end())
  62. {
  63. CPosXrayPtr posXray = *itr;//迭代器是指针,所以要解引用
  64. particleInfoMap.insert(pair<int, CPosXrayPtr>(i_fieldidwithxrayid, posXray));
  65. }
  66. }
  67. //TrioTemplate trioTem;
  68. TrioTemplateMulti trioTemMulti;
  69. if (prop->GetCalChartType() == CALCULATE_CHART_TYPE::TRIO_CHART)
  70. {
  71. CTriTempItemList lsCTriTempItemList = m_rptMgrPtr->GetRptParamFilePtr()->GetTriTempFile()->GetTriTempItemList();
  72. trioTemMulti.elementA = lsCTriTempItemList[prop->GetTrioChartType()]->GetTopName();
  73. trioTemMulti.elementB = lsCTriTempItemList[prop->GetTrioChartType()]->GetLeftName();
  74. trioTemMulti.elementC = lsCTriTempItemList[prop->GetTrioChartType()]->GetRightName();
  75. for (int i = 0; i < lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList().size(); i++)
  76. {
  77. if (lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetItemTypeName() == "Top")
  78. {
  79. trioTemMulti.velementA.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementName());
  80. trioTemMulti.vAnum.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementNumber());
  81. }
  82. if (lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetItemTypeName() == "Left")
  83. {
  84. trioTemMulti.velementB.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementName());
  85. trioTemMulti.vBnum.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementNumber());
  86. }
  87. if (lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetItemTypeName() == "Right")
  88. {
  89. trioTemMulti.velementC.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementName());
  90. trioTemMulti.vCnum.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementNumber());
  91. }
  92. }
  93. }
  94. //get pixel size
  95. CSmplMsrResultFileMgrPtr pSmplMsrResultFileMgr = m_rptMgrPtr->GetASmplMsrResultMgrByFileName(strDataSourceName);
  96. ASSERT(pSmplMsrResultFileMgr);
  97. if (!pSmplMsrResultFileMgr)
  98. {
  99. LogErrorTrace(__FILE__, __LINE__, _T("CompParticleTrioInfo: can't get sample result file manager."));
  100. return listGridData;
  101. }
  102. CSmplMsrResultFilePtr pSmplMsrResultFile = pSmplMsrResultFileMgr->GetSmplMsrResultFile();
  103. ASSERT(pSmplMsrResultFile);
  104. if (!pSmplMsrResultFile)
  105. {
  106. LogErrorTrace(__FILE__, __LINE__, _T("CompParticleTrioInfo: can't get sample result file."));
  107. return listGridData;
  108. }
  109. COTSSamplePtr pSample = pSmplMsrResultFile->GetSample();
  110. ASSERT(pSample);
  111. if (!pSample)
  112. {
  113. LogErrorTrace(__FILE__, __LINE__, _T("CompParticleTrioInfo: can't get sample."));
  114. return listGridData;
  115. }
  116. double dPixelSize = pSample->CalculatePixelSize();
  117. if (!GetTrioInfoFromParticleList(listParticleAll, dPixelSize, prop->GetSizeCalMethodType(), particleInfoMap, trioTemMulti,pGridData))
  118. {
  119. LogErrorTrace(__FILE__, __LINE__, _T("CompParticleTrioInfo: can't get general information."));
  120. return listGridData;
  121. }
  122. listGridData.push_back(pGridData);
  123. nIndex++;
  124. }
  125. return listGridData;
  126. }
  127. BOOL TrioCal::GetTrioInfoFromParticleList(COTSParticleList a_listParticle, double a_dPixelSize, SIZE_CAL_METHOD_TYPE a_nMethod, map<int, CPosXrayPtr> particleInfoMap, TrioTemplateMulti t1, CGridDataPtr a_pGridData)
  128. {
  129. ASSERT(a_pGridData);
  130. if (!a_pGridData)
  131. {
  132. LogErrorTrace(__FILE__, __LINE__, _T("GetGenInfoFromParticleList: invalid grid data pointer."));
  133. return FALSE;
  134. }
  135. //devide all the particles into groups,according to the particle type,use map to keep it,Id is the key,the particle list is the value.
  136. COTSParticleList listPartReport;
  137. map<int, COTSParticleList> mapSameSTDItemPartList;
  138. map<int, COTSParticleList>::iterator partListIter;
  139. //get the same type particle together
  140. for (auto pParticle : a_listParticle) {
  141. int nType = pParticle->GetType();
  142. //only consider the type value bigger than NOT_IDENTIFIED
  143. if (nType >= (int)(int)OTS_PARTCLE_TYPE::NOT_IDENTIFIED_SIC)
  144. {
  145. partListIter = mapSameSTDItemPartList.find(nType);
  146. if (partListIter == mapSameSTDItemPartList.end())
  147. {
  148. COTSParticleList listParticle;
  149. listParticle.push_back(pParticle);
  150. mapSameSTDItemPartList.insert(pair<int, COTSParticleList>(nType, listParticle));
  151. }
  152. else {
  153. partListIter->second.push_back(pParticle);
  154. }
  155. }
  156. }
  157. for (partListIter = mapSameSTDItemPartList.begin(); partListIter != mapSameSTDItemPartList.end(); partListIter++) {
  158. listPartReport.insert(listPartReport.end(), partListIter->second.begin(), partListIter->second.end());
  159. }
  160. int nColumnNum = ((int)(REPORT_PARTICLE_TRIO_INFO_GRID_CLUMN::MAX) - (int)(REPORT_PARTICLE_TRIO_INFO_GRID_CLUMN::MIN) + 1);
  161. CGridColumnsList listGridColumn;
  162. for (int i = 0; i < nColumnNum; i++)
  163. {
  164. CGridColumnPtr pColumn = CGridColumnPtr(new CGridColumn());
  165. CGridRowsList listGridRows;
  166. listGridRows.clear();
  167. int nType = 0;
  168. int nPos = 0;
  169. switch (i)
  170. {
  171. case (int)REPORT_PARTICLE_TRIO_INFO_GRID_CLUMN::SIZE:
  172. pColumn->SetName("sizeLevel");
  173. listGridRows.clear();
  174. for (auto pParticle : listPartReport)
  175. {
  176. CGridRowPtr pGridRow = CGridRowPtr(new CGridRow());
  177. pGridRow->SetDataType(REPORT_GRID_DATA_TYPE::STRING);
  178. double dSize = m_rptMgrPtr->GetValueAsMethod(pParticle, a_nMethod, a_dPixelSize);
  179. CPartSizeFilePtr pPartSize = m_rptMgrPtr->GetCurrentPropParam()->GetWorkingSizeFile ();
  180. CPartSizeItemList listPartSize = pPartSize->GetPartSizeList();
  181. if (!listPartSize.empty())
  182. {
  183. for (auto partsizeItem : listPartSize)
  184. {
  185. CDoubleRangePtr levelPtr = partsizeItem->GetSizeLevel();
  186. double dStart = levelPtr->GetStart();
  187. double theEnd = levelPtr->GetEnd();
  188. double dEnd = theEnd;
  189. if (dSize >= dStart && dSize < dEnd)
  190. {
  191. CString str, str1;
  192. if ((dStart - (int)dStart) == 0)
  193. {
  194. //小数位为零
  195. str.Format(_T("%.0f"), dStart);
  196. }
  197. else
  198. {
  199. str.Format(_T("%0.1lf"), dStart);
  200. }
  201. if (dEnd == 1000000)
  202. {
  203. str1 = "Max";
  204. }
  205. else
  206. {
  207. if ((dEnd - (int)dEnd) == 0)
  208. {
  209. //小数位为零
  210. str1.Format(_T("%.0f"), dEnd);
  211. }
  212. else
  213. {
  214. str1.Format(_T("%0.1lf"), dEnd);
  215. }
  216. }
  217. CString level = str + _T(" ~ ") + str1;//“0-5” 或 “5-10”等
  218. pGridRow->SetStringValue(level);
  219. }
  220. }
  221. }
  222. listGridRows.push_back(pGridRow);
  223. }
  224. break;
  225. case (int)REPORT_PARTICLE_TRIO_INFO_GRID_CLUMN::Location:
  226. pColumn->SetName("particleLocation");
  227. listGridRows.clear();
  228. for (auto pParticle : listPartReport)
  229. {
  230. CGridRowPtr pGridRow = CGridRowPtr(new CGridRow());
  231. pGridRow->SetDataType(REPORT_GRID_DATA_TYPE::STRING);
  232. //using field id combined xray id to vector key
  233. int xrayId = pParticle->GetAnalysisId();
  234. int fieldId = pParticle->GetFieldId();
  235. CString fieldidwithxrayid = "";
  236. fieldidwithxrayid.Format(_T("%d%d"), fieldId, xrayId);
  237. int i_fieldidwithxrayid = atoi(fieldidwithxrayid);
  238. auto itr = particleInfoMap.find(i_fieldidwithxrayid);
  239. if (itr != particleInfoMap.end())
  240. {
  241. CString loc;
  242. loc = GetTrioMoleRatio(*itr, t1);
  243. pGridRow->SetStringValue(loc);
  244. }
  245. else
  246. {
  247. CString loc;
  248. loc = _T("0,0,0");
  249. pGridRow->SetStringValue(loc);
  250. }
  251. listGridRows.push_back(pGridRow);
  252. }
  253. break;
  254. }
  255. pColumn->SetGridRowsList(listGridRows);
  256. listGridColumn.push_back(pColumn);
  257. }
  258. a_pGridData->SetGridColumnList(listGridColumn);
  259. return TRUE;
  260. }
  261. CString TrioCal::GetTrioMoleRatio(pair<int, CPosXrayPtr> p, TrioTemplateMulti t1)
  262. {
  263. CElementChemistriesList chemistriesList = p.second->GetElementQuantifyData();
  264. CString strRet;//由三条边上的位置组成,如三角形的三个顶点分别为a b c,则strRet为:aTob,bToc,cToa
  265. double aPercent=0, bPercent=0, cPercent=0;
  266. double d_ASum = 0;
  267. double d_BSum = 0;
  268. double d_CSum = 0;
  269. auto aElements = t1.velementA;
  270. auto bElements = t1.velementB;
  271. auto cElements = t1.velementC;
  272. auto aNums = t1.vAnum;
  273. auto bNums = t1.vBnum;
  274. auto cNums = t1.vCnum;
  275. auto partElements = p.second->GetElementQuantifyData();
  276. for (int i = 0; i < aElements.size(); i++)
  277. {
  278. for (int k = 0; k < partElements.size (); k++)
  279. {
  280. if (aElements[i].MakeUpper() == partElements[k]->GetName().MakeUpper())
  281. {
  282. //找到有包含的元素了,并乘以该元素对应的比例
  283. d_ASum = d_ASum + (partElements[k]->GetPercentage() / CElement::GetAtomWeight(partElements[k]->GetName())) * aNums[i];
  284. }
  285. }
  286. }
  287. for (int i = 0; i < bElements.size(); i++)
  288. {
  289. for (int k = 0; k < partElements.size(); k++)
  290. {
  291. if (bElements[i] == partElements[k]->GetName())
  292. {
  293. //找到有包含的元素了,并乘以该元素对应的比例
  294. d_BSum = d_BSum + (partElements[k]->GetPercentage() / CElement::GetAtomWeight(partElements[k]->GetName())) * bNums[i];
  295. }
  296. }
  297. }
  298. for (int i = 0; i < cElements.size(); i++)
  299. {
  300. for (int k = 0; k < partElements.size(); k++)
  301. {
  302. if (cElements[i] == partElements[k]->GetName())
  303. {
  304. //找到有包含的元素了,并乘以该元素对应的比例
  305. d_CSum = d_CSum + (partElements[k]->GetPercentage() / CElement::GetAtomWeight(partElements[k]->GetName())) * cNums[i];
  306. }
  307. }
  308. }
  309. double allNums = d_ASum + d_BSum + d_CSum;
  310. if ((allNums) == 0)
  311. {
  312. return "0,0,0";
  313. }
  314. else
  315. {
  316. if (d_ASum != 0)
  317. {
  318. aPercent = d_ASum / allNums;
  319. }
  320. if (d_BSum != 0)
  321. {
  322. bPercent = d_BSum / allNums;
  323. }
  324. if (d_CSum != 0)
  325. {
  326. cPercent = d_CSum / allNums;
  327. }
  328. CString ls_str;
  329. if (aPercent == 0 || aPercent == 1)
  330. {
  331. ls_str.Format(_T("%.0f"), aPercent);
  332. }
  333. else
  334. {
  335. ls_str.Format(_T("%.4f"), aPercent);
  336. }
  337. strRet = strRet + ls_str + ",";
  338. if (bPercent == 0 || bPercent == 1)
  339. {
  340. ls_str.Format(_T("%.0f"), bPercent);
  341. }
  342. else
  343. {
  344. ls_str.Format(_T("%.4f"), bPercent);
  345. }
  346. strRet = strRet + ls_str + ",";
  347. if (cPercent == 0 || cPercent == 1)
  348. {
  349. ls_str.Format(_T("%.0f"), cPercent);
  350. }
  351. else
  352. {
  353. ls_str.Format(_T("%.4f"), cPercent);
  354. }
  355. strRet = strRet + ls_str;
  356. return strRet;
  357. }
  358. //四元相图计算扩展
  359. /*strRet = strRet + ls_str + ",";
  360. if (atod == 0 || atod == 1)
  361. {
  362. ls_str.Format(_T("%.0f"), atod);
  363. }
  364. else
  365. {
  366. ls_str.Format(_T("%.4f"), atod);
  367. }
  368. strRet = strRet + ls_str + ",";
  369. if (dtob == 0 || dtob == 1)
  370. {
  371. ls_str.Format(_T("%.0f"), dtob);
  372. }
  373. else
  374. {
  375. ls_str.Format(_T("%.4f"), dtob);
  376. }
  377. strRet = strRet + ls_str;*/
  378. return strRet;
  379. }
  380. //传入,两个项顶点的元素List及对应的比例,再传入颗粒所有包含元素列表,通过综合运算,返回两个顶点中包含颗粒的元素的摩尔比之合,再弄成摩尔比
  381. double TrioCal::GetMoleRatio(vector<CString> oneitem, vector<float> voneitemNum, vector<CString> twoitem, vector<float> vtwoitemNum, vector<CString> thirdItem, vector<float> thirdItemNum, CElementChemistriesList particleitem)
  382. {
  383. double d_oneSum = 0;
  384. double d_twoSum = 0;
  385. for (int i = 0; i < oneitem.size(); i++)
  386. {
  387. for (int k = 0; k < particleitem.size(); k++)
  388. {
  389. if (oneitem[i] == particleitem[k]->GetName())
  390. {
  391. //找到有包含的元素了,并乘以该元素对应的比例
  392. //d_oneSum = d_oneSum + particleitem[k]->GetPercentage() * voneitemNum[i];
  393. d_oneSum = d_oneSum + (particleitem[k]->GetPercentage()/ CElement::GetAtomWeight(particleitem[k]->GetName())) * voneitemNum[i];
  394. }
  395. }
  396. }
  397. //CElement::GetAtomWeight("");
  398. for (int i = 0; i < twoitem.size(); i++)
  399. {
  400. for (int k = 0; k < particleitem.size(); k++)
  401. {
  402. if (twoitem[i] == particleitem[k]->GetName())
  403. {
  404. //找到有包含的元素了,并乘以该元素对应的比例
  405. //d_twoSum = d_twoSum + particleitem[k]->GetPercentage() * vtwoitemNum[i];
  406. d_twoSum = d_twoSum + (particleitem[k]->GetPercentage() / CElement::GetAtomWeight(particleitem[k]->GetName())) * vtwoitemNum[i];
  407. }
  408. }
  409. }
  410. if ((d_oneSum + d_twoSum) == 0)
  411. //if(d_oneSum == 0 || d_twoSum == 0)//如果有任何对比为0情况就返回0?,那么顶点又无法返回了
  412. {
  413. return 0;
  414. }
  415. else
  416. {
  417. return 1 - d_oneSum / (d_oneSum + d_twoSum);
  418. }
  419. }
  420. }