| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- #include "stdafx.h"
- #include "TrioCal.h"
- #include "OTSFileSys.h"
- #include "OTSHelper.h"
- #include "PropParam.h"
- #include "SmplMsrResultFileMgr.h"
- #include<math.h>
- //计算三元相图
- namespace OTSMODEL
- {
- TrioCal::TrioCal(CReportProjFileMgr * rptMgrPtr)
- {
- m_rptMgrPtr = rptMgrPtr;
-
- }
- TrioCal::~TrioCal()
- {
- }
- // 主要功能为解决多数据源问题
- CGridDatasList TrioCal::CompParticleTrioInfo()
- {
- // use the dll resource
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- //定义返回的Grid数据结构
- CGridDatasList listGridData;
- listGridData.clear();
- // depart compound source name
- std::vector<int> listWorkIndex;
- std::vector<int> listPos;
- //std::vector<CString> listDataSourceOut = m_rptMgrPtr->GetFullDataSource(a_pGridIn, listWorkIndex, listPos);
- int dataSourceId;//用户在propertygrid(sourcegrid)界面上选择的样品下拉列表ID号,如果是有需要对比的复合样品源,会在第0项出现。
- CPropParamPtr prop = m_rptMgrPtr->GetCurrentPropParam();
- dataSourceId = prop->GetDataSourceId();
-
- std::vector<CString> listDataSource = m_rptMgrPtr->GetCurrentPropParam()->GetDataSourceList();
- int nSelectedDataSourceIndex = prop->GetDataSourceId();
- CString sDataSourceNames = listDataSource[nSelectedDataSourceIndex];
- std::vector<CString> listSelectedDataSource = COTSHelper::SplitString(sDataSourceNames, _T("+"));
- int nIndex = 0;
- for (auto strDataSourceName : listSelectedDataSource)
- {
- CGridDataPtr pGridData = CGridDataPtr(new CGridData());
- // data source id
- std::vector<CString> listDataSource;
- listDataSource.clear();
- listDataSource = prop->GetDataSourceList();
- pGridData->SetDataSourceList(listDataSource);
- int nDataSourceId = prop->GetDataSourceId();
- pGridData->SetDataSourceId(nDataSourceId);
-
- COTSParticleList listParticleAll = m_rptMgrPtr->GetAnalysisParticleList(strDataSourceName);//找到这个样品中所有的有效颗粒
- CPosXraysList listXray= m_rptMgrPtr->GetAnalysisXrayList(strDataSourceName);//找到所有颗粒对应的Xray
- map<int, CPosXrayPtr> particleInfoMap;
- for (auto p : listParticleAll)
- {
- //using fieldid combined analysisid to vector key
- int xrayId = p->GetAnalysisId();
- int fieldId = p->GetFieldId();
- if (xrayId < 0 || fieldId < 0) { continue; }
- CString fieldidwithxrayid = "";
- fieldidwithxrayid.Format(_T("%d%d"), fieldId, xrayId);
- int i_fieldidwithxrayid = atoi(fieldidwithxrayid);
- auto itr = std::find_if(listXray.begin(), listXray.end(), [xrayId, fieldId](CPosXrayPtr x) { return (x->GetIndex() == xrayId && x->GetScanFieldId() == fieldId); });
- if (itr != listXray.end())
- {
- CPosXrayPtr posXray = *itr;//迭代器是指针,所以要解引用
- particleInfoMap.insert(pair<int, CPosXrayPtr>(i_fieldidwithxrayid, posXray));
- }
- }
-
- //TrioTemplate trioTem;
- TrioTemplateMulti trioTemMulti;
- if (prop->GetCalChartType() == CALCULATE_CHART_TYPE::TRIO_CHART)
- {
- CTriTempItemList lsCTriTempItemList = m_rptMgrPtr->GetRptParamFilePtr()->GetTriTempFile()->GetTriTempItemList();
- trioTemMulti.elementA = lsCTriTempItemList[prop->GetTrioChartType()]->GetTopName();
- trioTemMulti.elementB = lsCTriTempItemList[prop->GetTrioChartType()]->GetLeftName();
- trioTemMulti.elementC = lsCTriTempItemList[prop->GetTrioChartType()]->GetRightName();
- for (int i = 0; i < lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList().size(); i++)
- {
- if (lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetItemTypeName() == "Top")
- {
- trioTemMulti.velementA.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementName());
- trioTemMulti.vAnum.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementNumber());
- }
- if (lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetItemTypeName() == "Left")
- {
- trioTemMulti.velementB.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementName());
- trioTemMulti.vBnum.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementNumber());
- }
- if (lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetItemTypeName() == "Right")
- {
- trioTemMulti.velementC.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementName());
- trioTemMulti.vCnum.push_back(lsCTriTempItemList[prop->GetTrioChartType()]->GetTriTempItemElementList()[i]->GetElementNumber());
- }
- }
- }
- //get pixel size
- CSmplMsrResultFileMgrPtr pSmplMsrResultFileMgr = m_rptMgrPtr->GetASmplMsrResultMgrByFileName(strDataSourceName);
- ASSERT(pSmplMsrResultFileMgr);
- if (!pSmplMsrResultFileMgr)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CompParticleTrioInfo: can't get sample result file manager."));
- return listGridData;
- }
- CSmplMsrResultFilePtr pSmplMsrResultFile = pSmplMsrResultFileMgr->GetSmplMsrResultFile();
- ASSERT(pSmplMsrResultFile);
- if (!pSmplMsrResultFile)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CompParticleTrioInfo: can't get sample result file."));
- return listGridData;
- }
- COTSSamplePtr pSample = pSmplMsrResultFile->GetSample();
- ASSERT(pSample);
- if (!pSample)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CompParticleTrioInfo: can't get sample."));
- return listGridData;
- }
- double dPixelSize = pSample->CalculatePixelSize();
- if (!GetTrioInfoFromParticleList(listParticleAll, dPixelSize, prop->GetSizeCalMethodType(), particleInfoMap, trioTemMulti,pGridData))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CompParticleTrioInfo: can't get general information."));
- return listGridData;
- }
- listGridData.push_back(pGridData);
- nIndex++;
- }
- return listGridData;
- }
-
-
- BOOL TrioCal::GetTrioInfoFromParticleList(COTSParticleList a_listParticle, double a_dPixelSize, SIZE_CAL_METHOD_TYPE a_nMethod, map<int, CPosXrayPtr> particleInfoMap, TrioTemplateMulti t1, CGridDataPtr a_pGridData)
- {
- ASSERT(a_pGridData);
- if (!a_pGridData)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("GetGenInfoFromParticleList: invalid grid data pointer."));
- return FALSE;
- }
-
- //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.
- COTSParticleList listPartReport;
- map<int, COTSParticleList> mapSameSTDItemPartList;
- map<int, COTSParticleList>::iterator partListIter;
- //get the same type particle together
- for (auto pParticle : a_listParticle) {
- int nType = pParticle->GetType();
- //only consider the type value bigger than NOT_IDENTIFIED
- if (nType >= (int)(int)OTS_PARTCLE_TYPE::NOT_IDENTIFIED_SIC)
- {
- partListIter = mapSameSTDItemPartList.find(nType);
- if (partListIter == mapSameSTDItemPartList.end())
- {
- COTSParticleList listParticle;
- listParticle.push_back(pParticle);
- mapSameSTDItemPartList.insert(pair<int, COTSParticleList>(nType, listParticle));
- }
- else {
- partListIter->second.push_back(pParticle);
- }
- }
- }
- for (partListIter = mapSameSTDItemPartList.begin(); partListIter != mapSameSTDItemPartList.end(); partListIter++) {
- listPartReport.insert(listPartReport.end(), partListIter->second.begin(), partListIter->second.end());
- }
- int nColumnNum = ((int)(REPORT_PARTICLE_TRIO_INFO_GRID_CLUMN::MAX) - (int)(REPORT_PARTICLE_TRIO_INFO_GRID_CLUMN::MIN) + 1);
- CGridColumnsList listGridColumn;
- for (int i = 0; i < nColumnNum; i++)
- {
- CGridColumnPtr pColumn = CGridColumnPtr(new CGridColumn());
- CGridRowsList listGridRows;
- listGridRows.clear();
- int nType = 0;
- int nPos = 0;
- switch (i)
- {
- case (int)REPORT_PARTICLE_TRIO_INFO_GRID_CLUMN::SIZE:
- pColumn->SetName("sizeLevel");
- listGridRows.clear();
- for (auto pParticle : listPartReport)
- {
- CGridRowPtr pGridRow = CGridRowPtr(new CGridRow());
- pGridRow->SetDataType(REPORT_GRID_DATA_TYPE::STRING);
-
- double dSize = m_rptMgrPtr->GetValueAsMethod(pParticle, a_nMethod, a_dPixelSize);
- CPartSizeFilePtr pPartSize = m_rptMgrPtr->GetCurrentPropParam()->GetWorkingSizeFile ();
- CPartSizeItemList listPartSize = pPartSize->GetPartSizeList();
- if (!listPartSize.empty())
- {
- for (auto partsizeItem : listPartSize)
- {
- CDoubleRangePtr levelPtr = partsizeItem->GetSizeLevel();
-
- double dStart = levelPtr->GetStart();
- double theEnd = levelPtr->GetEnd();
- double dEnd = theEnd;
- if (dSize >= dStart && dSize < dEnd)
- {
- CString str, str1;
- if ((dStart - (int)dStart) == 0)
- {
- //小数位为零
- str.Format(_T("%.0f"), dStart);
- }
- else
- {
- str.Format(_T("%0.1lf"), dStart);
- }
- if (dEnd == 1000000)
- {
- str1 = "Max";
- }
- else
- {
- if ((dEnd - (int)dEnd) == 0)
- {
- //小数位为零
- str1.Format(_T("%.0f"), dEnd);
- }
- else
- {
- str1.Format(_T("%0.1lf"), dEnd);
- }
- }
- CString level = str + _T(" ~ ") + str1;//“0-5” 或 “5-10”等
- pGridRow->SetStringValue(level);
- }
- }
- }
- listGridRows.push_back(pGridRow);
- }
- break;
- case (int)REPORT_PARTICLE_TRIO_INFO_GRID_CLUMN::Location:
- pColumn->SetName("particleLocation");
- listGridRows.clear();
- for (auto pParticle : listPartReport)
- {
- CGridRowPtr pGridRow = CGridRowPtr(new CGridRow());
- pGridRow->SetDataType(REPORT_GRID_DATA_TYPE::STRING);
- //using field id combined xray id to vector key
- int xrayId = pParticle->GetAnalysisId();
- int fieldId = pParticle->GetFieldId();
- CString fieldidwithxrayid = "";
- fieldidwithxrayid.Format(_T("%d%d"), fieldId, xrayId);
- int i_fieldidwithxrayid = atoi(fieldidwithxrayid);
- auto itr = particleInfoMap.find(i_fieldidwithxrayid);
- if (itr != particleInfoMap.end())
- {
- CString loc;
- loc = GetTrioMoleRatio(*itr, t1);
- pGridRow->SetStringValue(loc);
- }
- else
- {
- CString loc;
- loc = _T("0,0,0");
- pGridRow->SetStringValue(loc);
- }
- listGridRows.push_back(pGridRow);
- }
- break;
- }
- pColumn->SetGridRowsList(listGridRows);
- listGridColumn.push_back(pColumn);
- }
- a_pGridData->SetGridColumnList(listGridColumn);
- return TRUE;
- }
- CString TrioCal::GetTrioMoleRatio(pair<int, CPosXrayPtr> p, TrioTemplateMulti t1)
- {
- CElementChemistriesList chemistriesList = p.second->GetElementQuantifyData();
- CString strRet;//由三条边上的位置组成,如三角形的三个顶点分别为a b c,则strRet为:aTob,bToc,cToa
- double aPercent=0, bPercent=0, cPercent=0;
-
- double d_ASum = 0;
- double d_BSum = 0;
- double d_CSum = 0;
- auto aElements = t1.velementA;
- auto bElements = t1.velementB;
- auto cElements = t1.velementC;
- auto aNums = t1.vAnum;
- auto bNums = t1.vBnum;
- auto cNums = t1.vCnum;
- auto partElements = p.second->GetElementQuantifyData();
- for (int i = 0; i < aElements.size(); i++)
- {
- for (int k = 0; k < partElements.size (); k++)
- {
- if (aElements[i].MakeUpper() == partElements[k]->GetName().MakeUpper())
- {
- //找到有包含的元素了,并乘以该元素对应的比例
- d_ASum = d_ASum + (partElements[k]->GetPercentage() / CElement::GetAtomWeight(partElements[k]->GetName())) * aNums[i];
- }
- }
- }
- for (int i = 0; i < bElements.size(); i++)
- {
- for (int k = 0; k < partElements.size(); k++)
- {
- if (bElements[i] == partElements[k]->GetName())
- {
- //找到有包含的元素了,并乘以该元素对应的比例
- d_BSum = d_BSum + (partElements[k]->GetPercentage() / CElement::GetAtomWeight(partElements[k]->GetName())) * bNums[i];
- }
- }
- }
- for (int i = 0; i < cElements.size(); i++)
- {
- for (int k = 0; k < partElements.size(); k++)
- {
- if (cElements[i] == partElements[k]->GetName())
- {
- //找到有包含的元素了,并乘以该元素对应的比例
- d_CSum = d_CSum + (partElements[k]->GetPercentage() / CElement::GetAtomWeight(partElements[k]->GetName())) * cNums[i];
- }
- }
- }
- double allNums = d_ASum + d_BSum + d_CSum;
- if ((allNums) == 0)
- {
- return "0,0,0";
- }
- else
- {
- if (d_ASum != 0)
- {
- aPercent = d_ASum / allNums;
- }
- if (d_BSum != 0)
- {
- bPercent = d_BSum / allNums;
- }
- if (d_CSum != 0)
- {
- cPercent = d_CSum / allNums;
- }
- CString ls_str;
- if (aPercent == 0 || aPercent == 1)
- {
- ls_str.Format(_T("%.0f"), aPercent);
- }
- else
- {
- ls_str.Format(_T("%.4f"), aPercent);
- }
- strRet = strRet + ls_str + ",";
- if (bPercent == 0 || bPercent == 1)
- {
- ls_str.Format(_T("%.0f"), bPercent);
- }
- else
- {
- ls_str.Format(_T("%.4f"), bPercent);
- }
- strRet = strRet + ls_str + ",";
- if (cPercent == 0 || cPercent == 1)
- {
- ls_str.Format(_T("%.0f"), cPercent);
- }
- else
- {
- ls_str.Format(_T("%.4f"), cPercent);
- }
- strRet = strRet + ls_str;
- return strRet;
- }
-
- //四元相图计算扩展
- /*strRet = strRet + ls_str + ",";
- if (atod == 0 || atod == 1)
- {
- ls_str.Format(_T("%.0f"), atod);
- }
- else
- {
- ls_str.Format(_T("%.4f"), atod);
- }
- strRet = strRet + ls_str + ",";
- if (dtob == 0 || dtob == 1)
- {
- ls_str.Format(_T("%.0f"), dtob);
- }
- else
- {
- ls_str.Format(_T("%.4f"), dtob);
- }
- strRet = strRet + ls_str;*/
- return strRet;
- }
- //传入,两个项顶点的元素List及对应的比例,再传入颗粒所有包含元素列表,通过综合运算,返回两个顶点中包含颗粒的元素的摩尔比之合,再弄成摩尔比
- double TrioCal::GetMoleRatio(vector<CString> oneitem, vector<float> voneitemNum, vector<CString> twoitem, vector<float> vtwoitemNum, vector<CString> thirdItem, vector<float> thirdItemNum, CElementChemistriesList particleitem)
- {
- double d_oneSum = 0;
- double d_twoSum = 0;
- for (int i = 0; i < oneitem.size(); i++)
- {
- for (int k = 0; k < particleitem.size(); k++)
- {
- if (oneitem[i] == particleitem[k]->GetName())
- {
- //找到有包含的元素了,并乘以该元素对应的比例
- //d_oneSum = d_oneSum + particleitem[k]->GetPercentage() * voneitemNum[i];
- d_oneSum = d_oneSum + (particleitem[k]->GetPercentage()/ CElement::GetAtomWeight(particleitem[k]->GetName())) * voneitemNum[i];
- }
- }
- }
- //CElement::GetAtomWeight("");
- for (int i = 0; i < twoitem.size(); i++)
- {
- for (int k = 0; k < particleitem.size(); k++)
- {
- if (twoitem[i] == particleitem[k]->GetName())
- {
- //找到有包含的元素了,并乘以该元素对应的比例
- //d_twoSum = d_twoSum + particleitem[k]->GetPercentage() * vtwoitemNum[i];
- d_twoSum = d_twoSum + (particleitem[k]->GetPercentage() / CElement::GetAtomWeight(particleitem[k]->GetName())) * vtwoitemNum[i];
- }
- }
- }
- if ((d_oneSum + d_twoSum) == 0)
- //if(d_oneSum == 0 || d_twoSum == 0)//如果有任何对比为0情况就返回0?,那么顶点又无法返回了
- {
- return 0;
- }
- else
- {
- return 1 - d_oneSum / (d_oneSum + d_twoSum);
- }
- }
- }
|