OTSOxfordImpl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #include "stdafx.h"
  2. #include "OTSOxfordImpl.h"
  3. #include "../OTSControl/ControllerHelper.h"
  4. #include "COTSUtilityDllFunExport.h"
  5. #include "OxfordImplConst.h"
  6. //#include "boost/smart_ptr/scoped_array.hpp"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. namespace OTSController
  11. {
  12. OxfordImpl::OxfordImpl(void)
  13. : m_bInit(false)
  14. {
  15. /*features=new OxfordXrayData[nCollectCount]*/
  16. }
  17. OxfordImpl::~OxfordImpl(void)
  18. {
  19. CloseClient();
  20. m_oxfordPtr.reset();
  21. }
  22. // Close Client
  23. // return true if success
  24. void OxfordImpl::CloseClient(void)
  25. {
  26. m_oxfordPtr->CloseClient();
  27. }
  28. bool OxfordImpl::Connect()
  29. {
  30. if (!m_oxfordPtr)
  31. {
  32. m_oxfordPtr.reset(new OxfordController);
  33. //m_bInit = m_oxfordDll->LoadDll(OXFORD_DLL_NAME);
  34. //m_bInit = true;
  35. }
  36. if (!m_bInit)
  37. {
  38. m_bInit = m_oxfordPtr->CreateController();
  39. }
  40. return m_bInit;
  41. }
  42. bool OxfordImpl::IsConnected()
  43. {
  44. if (m_oxfordPtr && m_bInit)
  45. {
  46. return m_oxfordPtr->IsConnected();
  47. }
  48. return false;
  49. }
  50. bool OxfordImpl::GetPositionXY(double& a_dPosX, double& a_dPosY)
  51. {
  52. if (m_oxfordPtr && m_bInit)
  53. {
  54. return m_oxfordPtr->GetPositionXY(a_dPosX, a_dPosY);
  55. }
  56. return false;
  57. }
  58. bool OxfordImpl::SetPositionXY(double a_dPosX, double a_dPosY)
  59. {
  60. if (m_oxfordPtr && m_bInit)
  61. {
  62. return m_oxfordPtr->SetPositionXY(a_dPosX, a_dPosY);
  63. }
  64. return false;
  65. }
  66. bool OxfordImpl::GetWorkingDistance(double& a_dWorkingDistance)
  67. {
  68. if (m_oxfordPtr && m_bInit)
  69. {
  70. return m_oxfordPtr->GetWorkingDistance(a_dWorkingDistance);
  71. }
  72. return false;
  73. }
  74. bool OxfordImpl::SetWorkingDistance(double a_dWorkingDistance)
  75. {
  76. if (m_oxfordPtr && m_bInit)
  77. {
  78. return m_oxfordPtr->SetWorkingDistance(a_dWorkingDistance);
  79. }
  80. return false;
  81. }
  82. bool OxfordImpl::GetMagnification(double& a_dMagnification)
  83. {
  84. if (m_oxfordPtr && m_bInit)
  85. {
  86. return m_oxfordPtr->GetMagnification(a_dMagnification);
  87. }
  88. return false;
  89. }
  90. bool OxfordImpl::SetMagnification(double a_dMagnification)
  91. {
  92. if (m_oxfordPtr && m_bInit)
  93. {
  94. return m_oxfordPtr->SetMagnification(a_dMagnification);
  95. }
  96. return false;
  97. }
  98. bool OxfordImpl::GetHighVoltage(double& a_dHighVoltage)
  99. {
  100. if (m_oxfordPtr && m_bInit)
  101. {
  102. return m_oxfordPtr->GetHighVoltage(a_dHighVoltage);
  103. }
  104. return false;
  105. }
  106. bool OxfordImpl::SetHighVoltage(double a_dHighVoltage)
  107. {
  108. if (m_oxfordPtr && m_bInit)
  109. {
  110. return m_oxfordPtr->SetHighVoltage(a_dHighVoltage);
  111. }
  112. return false;
  113. }
  114. bool OxfordImpl::GetBeamOn(bool& a_bBeamOn)
  115. {
  116. if (m_oxfordPtr && m_bInit)
  117. {
  118. return m_oxfordPtr->GetBeamOn(a_bBeamOn);
  119. }
  120. return false;
  121. }
  122. bool OxfordImpl::SetBeamOn(bool a_bBeamOn)
  123. {
  124. if (m_oxfordPtr && m_bInit)
  125. {
  126. return m_oxfordPtr->SetBeamOn(a_bBeamOn);
  127. }
  128. return false;
  129. }
  130. bool OxfordImpl::GetBeamBlank(bool& a_bBeamBlank)
  131. {
  132. if (m_oxfordPtr && m_bInit)
  133. {
  134. return m_oxfordPtr->GetBeamBlank(a_bBeamBlank);
  135. }
  136. return false;
  137. }
  138. bool OxfordImpl::SetBeamBlank(bool a_bBeamBlank)
  139. {
  140. if (m_oxfordPtr && m_bInit)
  141. {
  142. return m_oxfordPtr->SetBeamBlank(a_bBeamBlank);
  143. }
  144. return false;
  145. }
  146. bool OxfordImpl::GetExternal(bool& a_bExternal)
  147. {
  148. if (m_oxfordPtr && m_bInit)
  149. {
  150. return m_oxfordPtr->GetExternal(a_bExternal);
  151. }
  152. return false;
  153. }
  154. bool OxfordImpl::SetExternal(bool a_bExternal)
  155. {
  156. if (m_oxfordPtr && m_bInit)
  157. {
  158. return m_oxfordPtr->SetExternal(a_bExternal);
  159. }
  160. return false;
  161. }
  162. bool OxfordImpl::CollectXrayData(const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize)
  163. {
  164. if (m_oxfordPtr && m_bInit)
  165. {
  166. return m_oxfordPtr->CollectXrayData(a_nAcTime, a_pnCounts, a_nBufferSize);
  167. }
  168. return false;
  169. }
  170. bool OxfordImpl::CollectXrayDataAtPos(const double a_dPosX, const double a_dPosY, const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize)
  171. {
  172. if (m_oxfordPtr && m_bInit)
  173. {
  174. return m_oxfordPtr->CollectXrayDataAtPos(a_dPosX, a_dPosY, a_nAcTime, a_pnCounts, a_nBufferSize);
  175. }
  176. return false;
  177. }
  178. bool OxfordImpl::SetBeamPosition(const double a_dPosX, const double a_dPosY)
  179. {
  180. if (m_oxfordPtr && m_bInit)
  181. {
  182. return m_oxfordPtr->SetBeamPosition(a_dPosX, a_dPosY);
  183. }
  184. return false;
  185. }
  186. bool OxfordImpl::SetScanSpeed(const long a_nMilliseconds)
  187. {
  188. if (m_oxfordPtr && m_bInit)
  189. {
  190. return m_oxfordPtr->SetScanSpeed(a_nMilliseconds);
  191. }
  192. return false;
  193. }
  194. bool OxfordImpl::GetImageSize(long& a_nWidth, long& a_nHeight)
  195. {
  196. if (m_oxfordPtr && m_bInit)
  197. {
  198. return m_oxfordPtr->GetImageSize(a_nWidth, a_nHeight);
  199. }
  200. return false;
  201. }
  202. bool OxfordImpl::SetImageSize(const long a_nWidth, const long a_nHeight)
  203. {
  204. if (m_oxfordPtr && m_bInit)
  205. {
  206. return m_oxfordPtr->SetImageSize(a_nWidth, a_nHeight);
  207. }
  208. return false;
  209. }
  210. bool OxfordImpl::CollectImage(BYTE* a_pImageBits)
  211. {
  212. if (m_oxfordPtr && m_bInit)
  213. {
  214. int i= m_oxfordPtr->CollectImage(a_pImageBits);
  215. return i== (int)OxfordWrapperErrorCode::SUCCEED;
  216. }
  217. return false;
  218. }
  219. bool OxfordImpl::QuantifySpectrum(unsigned char* cResult)
  220. {
  221. if (m_oxfordPtr && m_bInit)
  222. {
  223. return m_oxfordPtr->QuantifySpectrum(cResult);
  224. }
  225. return false;
  226. }
  227. BOOL OxfordImpl::GetXRayByPoints(CPosXraysList& a_listXrayPois, DWORD a_nACTimeMS)
  228. {
  229. try
  230. {
  231. // oxford dll handle check
  232. ASSERT(m_oxfordPtr);
  233. if (!m_oxfordPtr)
  234. {
  235. // error, invalid m_oxfordDll
  236. LogErrorTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: invalid m_oxfordDll."));
  237. return FALSE;
  238. }
  239. // do nothing if points list is empty
  240. if (a_listXrayPois.empty())
  241. {
  242. // points list is empty
  243. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  244. return TRUE;
  245. }
  246. // create array of BrukerSegment
  247. long nCollectCount = (long)a_listXrayPois.size();
  248. //boost::scoped_array<OxfordXrayData> segmentArray(new OxfordXrayData[nCollectCount]);
  249. OxfordXrayData* segmentArray(new OxfordXrayData[nCollectCount]);
  250. for (int i = 0; i < nCollectCount; ++i)
  251. {
  252. CPoint poi = a_listXrayPois[i]->GetPosition();
  253. segmentArray[i].m_nPosX = poi.x;
  254. segmentArray[i].m_nPosY = poi.y;
  255. }
  256. if (!m_oxfordPtr->CollectXrayList(a_nACTimeMS, segmentArray, nCollectCount, GENERALXRAYCHANNELS))
  257. {
  258. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  259. return false;
  260. }
  261. // get the specs for a_vXPoints
  262. for (int i = 0; i < nCollectCount; ++i)
  263. {
  264. // set spectrum data for the x-ray point
  265. a_listXrayPois[i]->SetXrayData((DWORD*)(segmentArray[i].m_pXrayData));
  266. if (m_bDoQuantification)
  267. {
  268. // quantify the spectrum
  269. CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)segmentArray[i].m_strElementResult));
  270. a_listXrayPois[i]->SetElementQuantifyData(vElement);
  271. }
  272. }
  273. for (int i = 0; i < nCollectCount; ++i)
  274. {
  275. // check spectrum
  276. DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  277. if (nTatolXrayCount < 20)
  278. {
  279. // captured an empty spectrum
  280. CPoint poi = a_listXrayPois[i]->GetPosition();
  281. // try to redo x-ray collection at the position
  282. static DWORD nChannelData[GENERALXRAYCHANNELS];
  283. memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS);
  284. if (!m_oxfordPtr->CollectXrayData(a_nACTimeMS, (long*)nChannelData, GENERALXRAYCHANNELS))
  285. {
  286. // error
  287. /*LogError(_T("Call CollectOneXRayPoint failed: index = %d(x:%d, y:%d)"),
  288. i, poi.x, poi.y);*/
  289. return false;
  290. }
  291. // set spectrum with new spectrum
  292. a_listXrayPois[i]->SetXrayData(nChannelData);
  293. nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  294. if (nTatolXrayCount < 20)
  295. {
  296. /*LogWarn(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."),
  297. nTatolXrayCount, i, poi.x, poi.y);*/
  298. }
  299. else
  300. {
  301. /*LogWarn(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"),
  302. nTatolXrayCount, i, poi.x, poi.y);*/
  303. }
  304. }
  305. }
  306. delete segmentArray;
  307. // ok return TRUE
  308. return TRUE;
  309. }
  310. catch (const std::exception&)
  311. {
  312. LogErrorTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: exception."));
  313. }
  314. // error, return false
  315. return FALSE;
  316. }
  317. BOOL OxfordImpl::GetXRayByFeatures(CPosXraysList&a_listXrayPois, std::vector<std::vector<BrukerSegment>>& a_vFeatures, DWORD a_nXRayAQTime)
  318. {
  319. try
  320. {
  321. // oxford dll handle check
  322. ASSERT(m_oxfordPtr);
  323. if (!m_oxfordPtr)
  324. {
  325. // error, invalid m_oxfordDll
  326. LogErrorTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: invalid m_oxfordDll."));
  327. return FALSE;
  328. }
  329. // do nothing if points list is empty
  330. if (a_listXrayPois.empty())
  331. {
  332. // points list is empty
  333. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  334. return TRUE;
  335. }
  336. if (a_listXrayPois.size() != a_vFeatures.size())
  337. {
  338. return FALSE;
  339. }
  340. // create array of BrukerSegment
  341. long nCollectCount = (long)a_listXrayPois.size();
  342. OxfordXrayData* features(new OxfordXrayData[nCollectCount]);
  343. int nTotalNum = 0;
  344. for (size_t i = 0; i < a_listXrayPois.size(); i++)
  345. {
  346. long nSegmentCount = (long)a_vFeatures[i].size();
  347. features[i].m_nChordNum = nSegmentCount;
  348. long nPixelCount = 0;
  349. if (nSegmentCount > 0)
  350. {
  351. for (int j = 0; j < nSegmentCount; j++)
  352. {
  353. features[i].m_ChordList[j].m_nX = a_vFeatures[i][j].XStart;
  354. features[i].m_ChordList[j].m_nY = a_vFeatures[i][j].Y;
  355. features[i].m_ChordList[j].m_nLength = a_vFeatures[i][j].XCount;
  356. nPixelCount += a_vFeatures[i][j].XCount;
  357. }
  358. features[i].m_nPixelNum = nPixelCount;
  359. }
  360. else
  361. {
  362. // will generate according to the x-ray position
  363. // this shouldn't happen
  364. }
  365. nTotalNum += nPixelCount;
  366. }
  367. int a_nACTimeMS = a_nXRayAQTime / nTotalNum;
  368. if (!m_oxfordPtr->CollectXrayArea(a_nXRayAQTime, features, nCollectCount, GENERALXRAYCHANNELS))
  369. {
  370. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  371. return false;
  372. }
  373. // get the specs for a_vXPoints
  374. for (int i = 0; i < nCollectCount; ++i)
  375. {
  376. // set spectrum data for the x-ray point
  377. a_listXrayPois[i]->SetXrayData((DWORD*)(features[i].m_pXrayData));
  378. // set spectrum data for the x-ray point
  379. if (m_bDoQuantification)
  380. {
  381. // quantify the spectrum
  382. CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)features[i].m_strElementResult));
  383. a_listXrayPois[i]->SetElementQuantifyData(vElement);
  384. }
  385. }
  386. for (int i = 0; i < nCollectCount; ++i)
  387. {
  388. // check spectrum
  389. DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  390. if (nTatolXrayCount < 20)
  391. {
  392. // captured an empty spectrum
  393. CPoint poi = a_listXrayPois[i]->GetPosition();
  394. // try to redo x-ray collection at the position
  395. static DWORD nChannelData[GENERALXRAYCHANNELS];
  396. memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS);
  397. if (!m_oxfordPtr->CollectXrayData(a_nXRayAQTime, (long*)nChannelData, GENERALXRAYCHANNELS))
  398. {
  399. return false;
  400. }
  401. // set spectrum with new spectrum
  402. a_listXrayPois[i]->SetXrayData(nChannelData);
  403. nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  404. if (nTatolXrayCount < 20)
  405. {
  406. /*LogWarn(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."),
  407. nTatolXrayCount, i, poi.x, poi.y);*/
  408. }
  409. else
  410. {
  411. /*LogWarn(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"),
  412. nTatolXrayCount, i, poi.x, poi.y);*/
  413. }
  414. }
  415. }
  416. delete features;
  417. // ok return TRUE
  418. return TRUE;
  419. }
  420. catch (const std::exception&)
  421. {
  422. LogErrorTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: exception."));
  423. }
  424. // error, return false
  425. return FALSE;
  426. }
  427. } // namespace Controller