OTSOxfordImpl.cpp 13 KB

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