|
@@ -0,0 +1,228 @@
|
|
|
+#pragma once
|
|
|
+#include "stdafx.h"
|
|
|
+#include "PartSTDRuleItem.h"
|
|
|
+#include "XMLSerialization.h"
|
|
|
+#include "OTSFileSys.h"
|
|
|
+#include "Element.h"
|
|
|
+using namespace OTSTools;
|
|
|
+using namespace OTSDATA;
|
|
|
+using namespace xmls;
|
|
|
+std::map<std::string, OTSDATA::CElementPtr>& PartSTDRuleItem::GetMapElements()
|
|
|
+{
|
|
|
+ if (m_mapEleItems.size() == 0)
|
|
|
+ {
|
|
|
+ for (auto ele : m_KeyelementList)
|
|
|
+ {
|
|
|
+ m_mapEleItems[ele->GetName().GetBuffer()] = ele;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return m_mapEleItems;
|
|
|
+}
|
|
|
+
|
|
|
+void PartSTDRuleItem::Serialize(bool isStoring, tinyxml2::XMLDocument* classDoc, tinyxml2::XMLElement* rootNode)
|
|
|
+{
|
|
|
+ // name
|
|
|
+ xmls::xString xstrName;
|
|
|
+ // color
|
|
|
+ xmls::xString xsColor;//COLORREF, C# can't access
|
|
|
+
|
|
|
+ xmls::xInt xnSTDId;
|
|
|
+ xmls::xString xKeyElementListStr;
|
|
|
+ xmls::xString xSubElementListStr;
|
|
|
+
|
|
|
+ xmls::xString xUsingConstants;
|
|
|
+ xmls::xString xImgPropertyListStr;
|
|
|
+ xmls::xString xOtherPropertyListStr;
|
|
|
+ xmls::xString xExpstr;
|
|
|
+ //xmls::Collection< CElementRange> xElementRanges;
|
|
|
+ xmls::xString xHardness;
|
|
|
+ xmls::xString xDensity;
|
|
|
+ xmls::xString xElectrical_conductivity;
|
|
|
+
|
|
|
+ xmls::Slo slo;
|
|
|
+ slo.Register("StrName", &xstrName);
|
|
|
+ slo.Register("Color", &xsColor);
|
|
|
+
|
|
|
+ slo.Register("STDId", &xnSTDId);
|
|
|
+
|
|
|
+ slo.Register("KeyElementList", &xKeyElementListStr);
|
|
|
+ slo.Register("SubElementList", &xSubElementListStr);
|
|
|
+ slo.Register("UsingConstants", &xUsingConstants);
|
|
|
+ slo.Register("UsingImgPropertyList", &xImgPropertyListStr);
|
|
|
+ slo.Register("UsingOtherPropertyList", &xOtherPropertyListStr);
|
|
|
+ slo.Register("Expression", &xExpstr);
|
|
|
+ slo.Register("Hardness", &xHardness);
|
|
|
+ slo.Register("Density", &xDensity);
|
|
|
+ slo.Register("Electrical_conductivity", &xElectrical_conductivity);
|
|
|
+ if (isStoring)
|
|
|
+ {
|
|
|
+ xstrName = GetName();
|
|
|
+ xsColor = GetColor();
|
|
|
+ xHardness = m_Hardness;
|
|
|
+ xDensity = m_Density;
|
|
|
+ xElectrical_conductivity = m_Electrical_conductivity;
|
|
|
+ xnSTDId = GetID();
|
|
|
+
|
|
|
+ CString s = "";
|
|
|
+ for (auto poElement : m_KeyelementList)
|
|
|
+ {
|
|
|
+ s += poElement->GetName() + ",";
|
|
|
+ }
|
|
|
+ s = s.TrimRight(",");
|
|
|
+ xKeyElementListStr = s;
|
|
|
+
|
|
|
+ s = "";
|
|
|
+ for (auto poElement : m_SubelementList)
|
|
|
+ {
|
|
|
+ s += poElement->GetName() + ",";
|
|
|
+ }
|
|
|
+ s = s.TrimRight(",");
|
|
|
+ xSubElementListStr = s;
|
|
|
+
|
|
|
+ for (auto pName : m_ImgPropertyList)
|
|
|
+ {
|
|
|
+ s += pName.c_str();
|
|
|
+ s += ",";
|
|
|
+ }
|
|
|
+ s = s.TrimRight(",");
|
|
|
+ xImgPropertyListStr = s;
|
|
|
+
|
|
|
+ for (auto pName : m_OtherpropertyList)
|
|
|
+ {
|
|
|
+ s += pName.c_str();
|
|
|
+ s += ",";
|
|
|
+ }
|
|
|
+ s = s.TrimRight(",");
|
|
|
+ xOtherPropertyListStr = s;
|
|
|
+
|
|
|
+ slo.Serialize(true, classDoc, rootNode);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ slo.Serialize(false, classDoc, rootNode);
|
|
|
+
|
|
|
+ SetName(xstrName.value().c_str());
|
|
|
+ SetColor(xsColor.value().c_str());
|
|
|
+ SetExpressionStr(xExpstr.value());
|
|
|
+ m_Hardness = xHardness.value();
|
|
|
+ m_Density = xDensity.value();
|
|
|
+ m_Electrical_conductivity = xElectrical_conductivity.value();
|
|
|
+ SetID(xnSTDId.value());
|
|
|
+ std::vector<string> eles;
|
|
|
+
|
|
|
+ std::string elements = xKeyElementListStr.c_str();
|
|
|
+ xmls::SplitString(elements, eles, ",");
|
|
|
+ for (int i = 0; i < eles.size(); ++i)
|
|
|
+ {
|
|
|
+ CString ss = eles[i].c_str();
|
|
|
+ m_KeyelementList.push_back(CElementPtr(new CElement(ss)));
|
|
|
+ }
|
|
|
+
|
|
|
+ elements = xSubElementListStr.c_str();
|
|
|
+ eles.clear();
|
|
|
+ xmls::SplitString(elements, eles, ",");
|
|
|
+ for (int i = 0; i < eles.size(); ++i)
|
|
|
+ {
|
|
|
+ CString ss = eles[i].c_str();
|
|
|
+ m_SubelementList.push_back(CElementPtr(new CElement(ss)));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ std::vector<string> pNames;
|
|
|
+ std::string propertynames = xImgPropertyListStr.c_str();
|
|
|
+ xmls::SplitString(propertynames, pNames, ",");
|
|
|
+ for (int i = 0; i < pNames.size(); ++i)
|
|
|
+ {
|
|
|
+ std::string ss = pNames[i];
|
|
|
+ m_ImgPropertyList.push_back(ss);
|
|
|
+ }
|
|
|
+ std::vector<string> pOthers;
|
|
|
+ std::string otherPropertynames = xOtherPropertyListStr.c_str();
|
|
|
+ xmls::SplitString(otherPropertynames, pOthers, ",");
|
|
|
+ for (int i = 0; i < pOthers.size(); ++i)
|
|
|
+ {
|
|
|
+ std::string ss = pOthers[i];
|
|
|
+ m_OtherpropertyList.push_back(ss);
|
|
|
+ }
|
|
|
+ for (auto ele : m_KeyelementList)
|
|
|
+ {
|
|
|
+ m_mapEleItems[ele->GetName().GetBuffer()] = ele;
|
|
|
+ }
|
|
|
+ xmls::SplitString(xUsingConstants.value(), m_usingConstants, ",");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+CElementsList PartSTDRuleItem::GetAllSortedEleList()
|
|
|
+{
|
|
|
+ std::multimap<int, CElementPtr> mapEles;// sorted all the using elements by the name's length,so that we can always handle the longer name element first.
|
|
|
+ //because if we handle short name element first ,we may treat "Cu" as "C" or other have the same character in name element.
|
|
|
+ for (auto ele : m_KeyelementList)
|
|
|
+ {
|
|
|
+ mapEles.insert(std::pair<int, CElementPtr>(ele->GetName().GetLength(), ele));
|
|
|
+ }
|
|
|
+ for (auto ele : m_SubelementList)
|
|
|
+ {
|
|
|
+ mapEles.insert(std::pair<int, CElementPtr>(ele->GetName().GetLength(), ele));
|
|
|
+
|
|
|
+ }
|
|
|
+ CElementsList eleList;
|
|
|
+
|
|
|
+ for (auto itr = mapEles.rbegin(); itr != mapEles.rend(); itr++)
|
|
|
+ {
|
|
|
+ eleList.push_back(itr->second);
|
|
|
+
|
|
|
+ }
|
|
|
+ return eleList;
|
|
|
+
|
|
|
+}
|
|
|
+void PartSTDRuleItem::AddXraySpectrum(CPosXrayPtr xray)
|
|
|
+{
|
|
|
+ if (m_spectrums.size() < 50)//only memory 50 spectrum for one item.
|
|
|
+ {
|
|
|
+ m_spectrums.push_back(xray);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+double PartSTDRuleItem::CalculateSimilarity(CPosXrayPtr xray)
|
|
|
+{
|
|
|
+ double maxSimilarity=0;
|
|
|
+ for (auto mySpectrum : m_spectrums)
|
|
|
+ {
|
|
|
+ double sim=GetCosValue(mySpectrum, xray, GENERALXRAYCHANNELS);
|
|
|
+ if (sim > maxSimilarity)
|
|
|
+ {
|
|
|
+ maxSimilarity = sim;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return maxSimilarity;
|
|
|
+}
|
|
|
+double PartSTDRuleItem::GetCosValue(CPosXrayPtr posXray, CPosXrayPtr posXray1, int iDataLen1)
|
|
|
+{
|
|
|
+ if (posXray->GetChannelsNum() != iDataLen1)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ DWORD* pXrayData = posXray->GetXrayData();
|
|
|
+ DWORD* pXrayData1 = posXray1->GetXrayData();
|
|
|
+
|
|
|
+
|
|
|
+ // ¹«Ê½: (x1y1+x2y2+x3y3+...x2000y2000) / (sqrt(x1^2 + x2^2 + ...x2000^2) * sqrt(y1^2 + y2^2 + ...y2000^2))
|
|
|
+ double dotProduct = 0;
|
|
|
+ double d1 = 0;
|
|
|
+ double d2 = 0;
|
|
|
+ for (int i = 0; i < iDataLen1; i++)
|
|
|
+ {
|
|
|
+ double r1 = pXrayData[i];
|
|
|
+ double r2 = pXrayData1[i];
|
|
|
+ r1 *= r2;
|
|
|
+ dotProduct = dotProduct + r1;
|
|
|
+
|
|
|
+ }
|
|
|
+ d1 = posXray->GetXrayDataVectorNorm();
|
|
|
+ d2 = posXray1->GetXrayDataVectorNorm();
|
|
|
+ return (0 == d1 || 0 == d2) ? 0 : dotProduct / (d1 * d2);
|
|
|
+
|
|
|
+}
|