OTSOxfordImpl.cpp 13 KB

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