OTSOxfordImpl.cpp 13 KB

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