OTSClassifyOnCurveCompEng.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "OTSSTDLib.h"
  4. #include "OTSClassifyOnCurveCompEng.h"
  5. #include "OTSHelper.h"
  6. using namespace std;
  7. using namespace OTSTools;
  8. namespace OTSClassifyEngine
  9. {
  10. CClassifyOnCurveCompEng::CClassifyOnCurveCompEng() // constructor
  11. {
  12. }
  13. CClassifyOnCurveCompEng::~CClassifyOnCurveCompEng() // detractor
  14. {
  15. }
  16. int CClassifyOnCurveCompEng::GetCompareMineral(int iStartChannel, const vector<CString>& vecIgnoreElementNames, CPosXrayPtr posxray, CSTDLibPtr& oretype, double& dValue)
  17. {
  18. if (oretype->GetSTDItemCount() < 1)
  19. {
  20. return -1;
  21. }
  22. int id = 0; // 存储最相似的那个mineralid
  23. double dcos = 0;
  24. dValue = -100000; // 获取最大的相似值
  25. for (unsigned int i = 0; i < oretype->GetSTDItemCount(); i++)
  26. {
  27. dcos = GetCosValue(iStartChannel, vecIgnoreElementNames, posxray, oretype->GetSTDItem(i)->GetXrayData(), oretype->GetSTDItem (i)->GetChannelsNo());
  28. if (dcos > dValue)
  29. {
  30. dValue = dcos;
  31. id = oretype->GetSTDItem (i)->GetID();
  32. }
  33. }
  34. return id;
  35. }
  36. CStandardItemPtr CClassifyOnCurveCompEng::GetCompareSTD(int iStartChannel, const vector<CString>& vecIgnoreElementNames, CPosXrayPtr posxray, CSTDLibPtr& oretype, double& dValue)
  37. {
  38. CStandardItemPtr itm; // 存储最相似的那个mineral
  39. if (oretype->GetSTDItemCount() < 1)
  40. {
  41. return itm;
  42. }
  43. double dcos = 0;
  44. dValue = -100000; // 获取最大的相似值
  45. for (unsigned int i = 0; i < oretype->GetSTDItemCount(); i++)
  46. {
  47. dcos = GetCosValue(iStartChannel, vecIgnoreElementNames, posxray, oretype->GetSTDItem(i)->GetXrayData(), oretype->GetSTDItem(i)->GetChannelsNo());
  48. if (dcos > dValue)
  49. {
  50. dValue = dcos;
  51. itm = oretype->GetSTDItem(i);
  52. }
  53. }
  54. return itm;
  55. }
  56. // 20190903:增加参数iStartChannel哪个位置开始比较, vecIgnoreChannel里面的通道不处理,
  57. double CClassifyOnCurveCompEng::GetCosValue(int iStartChannel, const vector<CString>& vecIgnoreElementNames, CPosXrayPtr posxray, DWORD* pXrayData1, int iDataLen1)
  58. {
  59. if (posxray->GetChannelsNo() != iDataLen1)
  60. {
  61. return 0;
  62. }
  63. vector<int> vecIgnoreChannel;
  64. GetElementChannel(vecIgnoreElementNames, vecIgnoreChannel);
  65. if (iStartChannel < 0 || iStartChannel >= iDataLen1)
  66. {
  67. return 1;
  68. }
  69. bool bignore = false;
  70. int j = 0;
  71. // 公式: (x1y1+x2y2+x3y3+...x2000y2000) / (sqrt(x1^2 + x2^2 + ...x2000^2) * sqrt(y1^2 + y2^2 + ...y2000^2))
  72. double dotpro = 0;
  73. double d1 = 0;
  74. double d2 = 0;
  75. for (int i = iStartChannel; i < iDataLen1; i++)
  76. {
  77. bignore = false;
  78. for (j = 0; j < (int)vecIgnoreChannel.size(); j++)
  79. {
  80. if (vecIgnoreChannel[j] == i)
  81. {
  82. bignore = true;
  83. break;
  84. }
  85. }
  86. if (!bignore)
  87. {
  88. dotpro = dotpro + posxray->GetXrayData()[i] * pXrayData1[i];
  89. d1 = d1 + posxray->GetXrayData()[i] * posxray->GetXrayData()[i];
  90. d2 = d2 + pXrayData1[i] * pXrayData1[i];
  91. }
  92. }
  93. d1 = sqrt(d1);
  94. d2 = sqrt(d2);
  95. // return (0 == d1 || 0 == d2) ? 0 : dotpro / (d1 * d2);
  96. // 算法改进, 加上距离权重
  97. if (0 == d1 || 0 == d2)
  98. {
  99. return 0;
  100. }
  101. double dresult = 0;
  102. dresult = (d1 < d2) ? d1 / d2 : d2 / d1;
  103. return 0.4 * dotpro / (d1 * d2) + 0.3 * dresult + 0.3 * GetStdEvp(iStartChannel, vecIgnoreChannel, posxray, pXrayData1, iDataLen1);
  104. }
  105. // 20190903:增加参数iStartChannel哪个位置开始比较, vecIgnoreChannel里面的通道不处理
  106. double CClassifyOnCurveCompEng::GetStdEvp(int iStartChannel, const vector<int>& vecIgnoreChannel, CPosXrayPtr posxray, DWORD* pXrayData1, int iDataLen1)
  107. {
  108. if (posxray->GetChannelsNo() != iDataLen1)
  109. {
  110. return 0;
  111. }
  112. if (iStartChannel < 0 || iStartChannel >= iDataLen1)
  113. {
  114. return 1;
  115. }
  116. bool bignore = false;
  117. int i = 0;
  118. int j = 0;
  119. int iSum1 = 0;
  120. int iSum2 = 0;
  121. double dAva1 = 0;
  122. double dAva2 = 0;
  123. double dSquare1 = 0;
  124. double dSquare2 = 0;
  125. vector<int> vecdata1;
  126. vector<int> vecdata2;
  127. vecdata1.clear();
  128. vecdata2.clear();
  129. // 求出能谱的平均值
  130. for (i = iStartChannel; i < iDataLen1; i++)
  131. {
  132. bignore = false;
  133. for (j = 0; j < (int)vecIgnoreChannel.size(); j++)
  134. {
  135. if (vecIgnoreChannel[j] == i)
  136. {
  137. bignore = true;
  138. break;
  139. }
  140. }
  141. // 能谱差之和
  142. if (!bignore)
  143. {
  144. iSum1 = iSum1 + (int)pXrayData1[i];
  145. vecdata1.push_back((int)pXrayData1[i]);
  146. iSum2 = iSum2 + (int)posxray->GetXrayData()[i];
  147. vecdata2.push_back((int)posxray->GetXrayData()[i]);
  148. }
  149. }
  150. dAva1 = iSum1 / vecdata1.size();
  151. dAva2 = iSum2 / vecdata2.size();
  152. for (i = 0; i < (int)vecdata1.size(); i++)
  153. {
  154. dSquare1 = dSquare1 + (vecdata1[i] - dAva1) * (vecdata1[i] - dAva1);
  155. dSquare2 = dSquare2 + (vecdata2[i] - dAva1) * (vecdata2[i] - dAva1);
  156. }
  157. dSquare1 = dSquare1 / vecdata1.size();
  158. dSquare2 = dSquare2 / vecdata2.size();
  159. dSquare1 = sqrt(dSquare1);
  160. dSquare2 = sqrt(dSquare2);
  161. dSquare1 = dSquare1 * (double)iSum2 / iSum1; // 归一化
  162. return (dSquare1 < dSquare2) ? (dSquare1 / dSquare2) : (dSquare2 / dSquare1);
  163. }
  164. void CClassifyOnCurveCompEng::GetElementChannel(const vector<CString>& vecstrElementNames, vector<int>& veciChannel)
  165. {
  166. veciChannel.clear();
  167. for (unsigned int i = 0; i < (int)vecstrElementNames.size(); i++)
  168. {
  169. GetElementChannel(vecstrElementNames[i], veciChannel);
  170. }
  171. }
  172. void CClassifyOnCurveCompEng::GetElementChannel(CString strElementName, vector<int>& veciChannel)
  173. {
  174. CElement* pElement = NULL;
  175. pElement = new CElement(strElementName);
  176. if (pElement)
  177. {
  178. AddChannel(pElement->GetEnergyValueK(), veciChannel);
  179. AddChannel(pElement->GetEnergyValueL(), veciChannel);
  180. AddChannel(pElement->GetEnergyValueM(), veciChannel);
  181. delete pElement;
  182. pElement = NULL;
  183. }
  184. }
  185. void CClassifyOnCurveCompEng::AddChannel(double dEnergy, vector<int>& veciChannel)
  186. {
  187. int i = 0;
  188. int iChannel = 0;
  189. if (dEnergy >= 0 && dEnergy < 20)
  190. {
  191. dEnergy = dEnergy * 100;
  192. iChannel = (int)dEnergy;
  193. for (i = 0; i < (int)veciChannel.size(); i++)
  194. {
  195. if (veciChannel[i] == iChannel)
  196. {
  197. break;
  198. }
  199. }
  200. if (i >= (int)veciChannel.size())
  201. {
  202. veciChannel.push_back(iChannel);
  203. }
  204. }
  205. }
  206. }