ROIController.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. using System;
  2. using HalconDotNet;
  3. using ViewWindow;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace ViewWindow.Model
  7. {
  8. public delegate void FuncROIDelegate();
  9. /// <summary>
  10. /// This class creates and manages ROI objects. It responds
  11. /// to mouse device inputs using the methods mouseDownAction and
  12. /// mouseMoveAction. You don't have to know this class in detail when you
  13. /// build your own C# project. But you must consider a few things if
  14. /// you want to use interactive ROIs in your application: There is a
  15. /// quite close connection between the ROIController and the HWndCtrl
  16. /// class, which means that you must 'register' the ROIController
  17. /// with the HWndCtrl, so the HWndCtrl knows it has to forward user input
  18. /// (like mouse events) to the ROIController class.
  19. /// The visualization and manipulation of the ROI objects is done
  20. /// by the ROIController.
  21. /// This class provides special support for the matching
  22. /// applications by calculating a model region from the list of ROIs. For
  23. /// this, ROIs are added and subtracted according to their sign.
  24. /// </summary>
  25. public class ROIController
  26. {
  27. public bool EditModel = true;
  28. /// <summary>
  29. /// Constant for setting the ROI mode: positive ROI sign.
  30. /// </summary>
  31. public const int MODE_ROI_POS = 21;
  32. /// <summary>
  33. /// Constant for setting the ROI mode: negative ROI sign.
  34. /// </summary>
  35. public const int MODE_ROI_NEG = 22;
  36. /// <summary>
  37. /// Constant for setting the ROI mode: no model region is computed as
  38. /// the sum of all ROI objects.
  39. /// </summary>
  40. public const int MODE_ROI_NONE = 23;
  41. /// <summary>Constant describing an update of the model region</summary>
  42. public const int EVENT_UPDATE_ROI = 50;
  43. public const int EVENT_CHANGED_ROI_SIGN = 51;
  44. /// <summary>Constant describing an update of the model region</summary>
  45. public const int EVENT_MOVING_ROI = 52;
  46. public const int EVENT_DELETED_ACTROI = 53;
  47. public const int EVENT_DELETED_ALL_ROIS = 54;
  48. public const int EVENT_ACTIVATED_ROI = 55;
  49. public const int EVENT_CREATED_ROI = 56;
  50. private ROI roiMode;
  51. private int stateROI;
  52. private double currX, currY;
  53. /// <summary>Index of the active ROI object</summary>
  54. public int activeROIidx;
  55. public int deletedIdx;
  56. /// <summary>List containing all created ROI objects so far</summary>
  57. public ArrayList ROIList;
  58. /// <summary>
  59. /// Region obtained by summing up all negative
  60. /// and positive ROI objects from the ROIList
  61. /// </summary>
  62. public HRegion ModelROI;
  63. private string activeCol = "blue";
  64. private string activeHdlCol = "green";
  65. private string inactiveCol = "blue";
  66. /// <summary>
  67. /// Reference to the HWndCtrl, the ROI Controller is registered to
  68. /// </summary>
  69. public HWndCtrl viewController;
  70. /// <summary>
  71. /// Delegate that notifies about changes made in the model region
  72. /// </summary>
  73. public IconicDelegate NotifyRCObserver;
  74. /// <summary>Constructor</summary>
  75. protected internal ROIController()
  76. {
  77. stateROI = MODE_ROI_NONE;
  78. ROIList = new ArrayList();
  79. activeROIidx = -1;
  80. ModelROI = new HRegion();
  81. NotifyRCObserver = new IconicDelegate(dummyI);
  82. deletedIdx = -1;
  83. currX = currY = -1;
  84. }
  85. /// <summary>Registers the HWndCtrl to this ROIController instance</summary>
  86. public void setViewController(HWndCtrl view)
  87. {
  88. viewController = view;
  89. }
  90. /// <summary>Gets the ModelROI object</summary>
  91. public HRegion getModelRegion()
  92. {
  93. return ModelROI;
  94. }
  95. /// <summary>Gets the List of ROIs created so far</summary>
  96. public ArrayList getROIList()
  97. {
  98. return ROIList;
  99. }
  100. /// <summary>Get the active ROI</summary>
  101. public ROI getActiveROI()
  102. {
  103. try
  104. {
  105. if (activeROIidx != -1)
  106. return ((ROI)ROIList[activeROIidx]);
  107. return null;
  108. }
  109. catch (Exception)
  110. {
  111. return null;
  112. }
  113. }
  114. public int getActiveROIIdx()
  115. {
  116. return activeROIidx;
  117. }
  118. public void setActiveROIIdx(int active)
  119. {
  120. activeROIidx = active;
  121. }
  122. public int getDelROIIdx()
  123. {
  124. return deletedIdx;
  125. }
  126. /// <summary>
  127. /// To create a new ROI object the application class initializes a
  128. /// 'seed' ROI instance and passes it to the ROIController.
  129. /// The ROIController now responds by manipulating this new ROI
  130. /// instance.
  131. /// </summary>
  132. /// <param name="r">
  133. /// 'Seed' ROI object forwarded by the application forms class.
  134. /// </param>
  135. public void setROIShape(ROI r)
  136. {
  137. roiMode = r;
  138. roiMode.setOperatorFlag(stateROI);
  139. }
  140. /// <summary>
  141. /// Sets the sign of a ROI object to the value 'mode' (MODE_ROI_NONE,
  142. /// MODE_ROI_POS,MODE_ROI_NEG)
  143. /// </summary>
  144. public void setROISign(int mode)
  145. {
  146. stateROI = mode;
  147. if (activeROIidx != -1)
  148. {
  149. ((ROI)ROIList[activeROIidx]).setOperatorFlag(stateROI);
  150. viewController.repaint();
  151. NotifyRCObserver(ROIController.EVENT_CHANGED_ROI_SIGN);
  152. }
  153. }
  154. /// <summary>
  155. /// Removes the ROI object that is marked as active.
  156. /// If no ROI object is active, then nothing happens.
  157. /// </summary>
  158. public void removeActive()
  159. {
  160. if (activeROIidx != -1)
  161. {
  162. ROIList.RemoveAt(activeROIidx);
  163. deletedIdx = activeROIidx;
  164. activeROIidx = -1;
  165. viewController.repaint();
  166. NotifyRCObserver(EVENT_DELETED_ACTROI);
  167. }
  168. }
  169. /// <summary>
  170. /// Calculates the ModelROI region for all objects contained
  171. /// in ROIList, by adding and subtracting the positive and
  172. /// negative ROI objects.
  173. /// </summary>
  174. public bool defineModelROI()
  175. {
  176. HRegion tmpAdd, tmpDiff, tmp;
  177. double row, col;
  178. if (stateROI == MODE_ROI_NONE)
  179. return true;
  180. tmpAdd = new HRegion();
  181. tmpDiff = new HRegion();
  182. tmpAdd.GenEmptyRegion();
  183. tmpDiff.GenEmptyRegion();
  184. for (int i=0; i < ROIList.Count; i++)
  185. {
  186. switch (((ROI)ROIList[i]).getOperatorFlag())
  187. {
  188. case ROI.POSITIVE_FLAG:
  189. tmp = ((ROI)ROIList[i]).getRegion();
  190. tmpAdd = tmp.Union2(tmpAdd);
  191. break;
  192. case ROI.NEGATIVE_FLAG:
  193. tmp = ((ROI)ROIList[i]).getRegion();
  194. tmpDiff = tmp.Union2(tmpDiff);
  195. break;
  196. default:
  197. break;
  198. }//end of switch
  199. }//end of for
  200. ModelROI = null;
  201. if (tmpAdd.AreaCenter(out row, out col) > 0)
  202. {
  203. tmp = tmpAdd.Difference(tmpDiff);
  204. if (tmp.AreaCenter(out row, out col) > 0)
  205. ModelROI = tmp;
  206. }
  207. //in case the set of positiv and negative ROIs dissolve
  208. if (ModelROI == null || ROIList.Count == 0)
  209. return false;
  210. return true;
  211. }
  212. /// <summary>
  213. /// Clears all variables managing ROI objects
  214. /// </summary>
  215. public void reset()
  216. {
  217. ROIList.Clear();
  218. activeROIidx = -1;
  219. ModelROI = null;
  220. roiMode = null;
  221. NotifyRCObserver(EVENT_DELETED_ALL_ROIS);
  222. }
  223. /// <summary>
  224. /// Deletes this ROI instance if a 'seed' ROI object has been passed
  225. /// to the ROIController by the application class.
  226. ///
  227. /// </summary>
  228. public void resetROI()
  229. {
  230. activeROIidx = -1;
  231. roiMode = null;
  232. }
  233. /// <summary>Defines the colors for the ROI objects</summary>
  234. /// <param name="aColor">Color for the active ROI object</param>
  235. /// <param name="inaColor">Color for the inactive ROI objects</param>
  236. /// <param name="aHdlColor">
  237. /// Color for the active handle of the active ROI object
  238. /// </param>
  239. public void setDrawColor(string aColor,
  240. string aHdlColor,
  241. string inaColor)
  242. {
  243. if (aColor != "")
  244. activeCol = aColor;
  245. if (aHdlColor != "")
  246. activeHdlCol = aHdlColor;
  247. if (inaColor != "")
  248. inactiveCol = inaColor;
  249. }
  250. /// <summary>
  251. /// Paints all objects from the ROIList into the HALCON window
  252. /// </summary>
  253. /// <param name="window">HALCON window</param>
  254. public void paintData(HalconDotNet.HWindow window,HWindowControl windowCtrl)
  255. {
  256. window.SetDraw("margin");
  257. window.SetLineWidth(1);
  258. if (ROIList.Count > 0)
  259. {
  260. //
  261. //window.SetColor(inactiveCol);
  262. window.SetDraw("margin");
  263. for (int i=0; i < ROIList.Count; i++)
  264. {
  265. window.SetColor(((ROI)ROIList[i]).Color);
  266. window.SetLineStyle(((ROI)ROIList[i]).flagLineStyle);
  267. ((ROI)ROIList[i]).draw(window,Convert .ToInt32 ( viewController .ImgCol2 -viewController .ImgCol1) ,Convert .ToInt32 (viewController .ImgRow2 -viewController .ImgRow1 ) );
  268. }
  269. if (activeROIidx != -1)
  270. {
  271. window.SetColor(activeCol);
  272. window.SetLineStyle(((ROI)ROIList[activeROIidx]).flagLineStyle);
  273. ((ROI)ROIList[activeROIidx]).draw(window, Convert.ToInt32(viewController.ImgCol2 - viewController.ImgCol1), Convert.ToInt32(viewController.ImgRow2 - viewController.ImgRow1));
  274. window.SetColor(activeHdlCol);
  275. ((ROI)ROIList[activeROIidx]).displayActive(window, Convert.ToInt32(viewController.ImgCol2 - viewController.ImgCol1), Convert.ToInt32(viewController.ImgRow2 - viewController.ImgRow1));
  276. }
  277. }
  278. }
  279. /// <summary>
  280. /// Reaction of ROI objects to the 'mouse button down' event: changing
  281. /// the shape of the ROI and adding it to the ROIList if it is a 'seed'
  282. /// ROI.
  283. /// </summary>
  284. /// <param name="imgX">x coordinate of mouse event</param>
  285. /// <param name="imgY">y coordinate of mouse event</param>
  286. /// <returns></returns>
  287. public int mouseDownAction(double imgX, double imgY)
  288. {
  289. int idxROI= -1;
  290. double max = 10000, dist = 0;
  291. double epsilon = 35.0; //maximal shortest distance to one of
  292. //the handles
  293. if (roiMode != null) //either a new ROI object is created
  294. {
  295. roiMode.createROI(imgX, imgY);
  296. ROIList.Add(roiMode);
  297. roiMode = null;
  298. activeROIidx = ROIList.Count - 1;
  299. viewController.repaint();
  300. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  301. }
  302. else if (ROIList.Count > 0) // ... or an existing one is manipulated
  303. {
  304. activeROIidx = -1;
  305. for (int i =0; i < ROIList.Count; i++)
  306. {
  307. dist = ((ROI)ROIList[i]).distToClosestHandle(imgX, imgY);
  308. if ((dist < max) && (dist < epsilon))
  309. {
  310. max = dist;
  311. idxROI = i;
  312. }
  313. }//end of for
  314. if (idxROI >= 0)
  315. {
  316. activeROIidx = idxROI;
  317. NotifyRCObserver(ROIController.EVENT_ACTIVATED_ROI);
  318. }
  319. viewController.repaint();
  320. }
  321. return activeROIidx;
  322. }
  323. /// <summary>
  324. /// Reaction of ROI objects to the 'mouse button move' event: moving
  325. /// the active ROI.
  326. /// </summary>
  327. /// <param name="newX">x coordinate of mouse event</param>
  328. /// <param name="newY">y coordinate of mouse event</param>
  329. public void mouseMoveAction(double newX, double newY, HWindowControl window)
  330. {
  331. try
  332. {
  333. if(EditModel ==false) return;
  334. if ((newX == currX) && (newY == currY))
  335. return;
  336. ((ROI)ROIList[activeROIidx]).moveByHandle(newX, newY,window );
  337. viewController.repaint();
  338. currX = newX;
  339. currY = newY;
  340. NotifyRCObserver(ROIController.EVENT_MOVING_ROI);
  341. }
  342. catch (Exception)
  343. {
  344. //没有显示roi的时候 移动鼠标会报错
  345. }
  346. }
  347. /***********************************************************/
  348. public void dummyI(int v)
  349. {
  350. }
  351. /*****************************/
  352. /// <summary>
  353. /// 在指定位置显示ROI--Rectangle1
  354. /// </summary>
  355. /// <param name="row1"></param>
  356. /// <param name="col1"></param>
  357. /// <param name="row2"></param>
  358. /// <param name="col2"></param>
  359. /// <param name="rois"></param>
  360. public void displayRect1(string color, double row1, double col1, double row2, double col2)
  361. {
  362. setROIShape(new ROIRectangle1());
  363. if (roiMode != null) //either a new ROI object is created
  364. {
  365. roiMode.createRectangle1(row1, col1, row2, col2);
  366. roiMode.Type = roiMode.GetType().Name;
  367. roiMode.Color = color;
  368. ROIList.Add(roiMode);
  369. roiMode = null;
  370. activeROIidx = ROIList.Count - 1;
  371. viewController.repaint();
  372. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  373. }
  374. }
  375. public void displayNurbs(string color,HTuple rows,HTuple cols)
  376. {
  377. setROIShape(new ROINurbs ());
  378. if (roiMode != null) //either a new ROI object is created
  379. {
  380. roiMode.createROINurbs (rows, cols);
  381. roiMode.Type = roiMode.GetType().Name;
  382. roiMode.Color = color;
  383. ROIList.Add(roiMode);
  384. roiMode = null;
  385. activeROIidx = ROIList.Count - 1;
  386. viewController.repaint();
  387. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  388. }
  389. }
  390. /// <summary>
  391. /// 在指定位置显示ROI--Rectangle2
  392. /// </summary>
  393. /// <param name="row"></param>
  394. /// <param name="col"></param>
  395. /// <param name="phi"></param>
  396. /// <param name="length1"></param>
  397. /// <param name="length2"></param>
  398. /// <param name="rois"></param>
  399. public void displayRect2(string color,double row, double col, double phi, double length1, double length2)
  400. {
  401. setROIShape(new ROIRectangle2());
  402. if (roiMode != null) //either a new ROI object is created
  403. {
  404. roiMode.createRectangle2(row, col, phi, length1, length2);
  405. roiMode.Type = roiMode.GetType().Name;
  406. roiMode.Color = color;
  407. ROIList.Add(roiMode);
  408. roiMode = null;
  409. activeROIidx = ROIList.Count - 1;
  410. viewController.repaint();
  411. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  412. }
  413. }
  414. /// <summary>
  415. /// 在指定位置生成ROI--Circle
  416. /// </summary>
  417. /// <param name="row"></param>
  418. /// <param name="col"></param>
  419. /// <param name="radius"></param>
  420. /// <param name="rois"></param>
  421. public void displayCircle(string color, double row, double col, double radius)
  422. {
  423. setROIShape(new ROICircle());
  424. if (roiMode != null) //either a new ROI object is created
  425. {
  426. roiMode.createCircle(row, col, radius);
  427. roiMode.Type = roiMode.GetType().Name;
  428. roiMode.Color = color;
  429. ROIList.Add(roiMode);
  430. roiMode = null;
  431. activeROIidx = ROIList.Count - 1;
  432. viewController.repaint();
  433. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  434. }
  435. }
  436. public void displayCircularArc(string color, double row, double col, double radius, double startPhi, double extentPhi)
  437. {
  438. setROIShape(new ROICircle());
  439. if (roiMode != null) //either a new ROI object is created
  440. {
  441. roiMode.createCircularArc(row, col, radius, startPhi, extentPhi, "positive");
  442. roiMode.Type = roiMode.GetType().Name;
  443. roiMode.Color = color;
  444. ROIList.Add(roiMode);
  445. roiMode = null;
  446. activeROIidx = ROIList.Count - 1;
  447. viewController.repaint();
  448. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  449. }
  450. }
  451. /// <summary>
  452. /// 在指定位置显示ROI--Line
  453. /// </summary>
  454. /// <param name="beginRow"></param>
  455. /// <param name="beginCol"></param>
  456. /// <param name="endRow"></param>
  457. /// <param name="endCol"></param>
  458. /// <param name="rois"></param>
  459. public void displayLine(string color, double beginRow, double beginCol, double endRow, double endCol)
  460. {
  461. this.setROIShape(new ROILine());
  462. if (roiMode != null) //either a new ROI object is created
  463. {
  464. roiMode.createLine(beginRow, beginCol, endRow, endCol);
  465. roiMode.Type = roiMode.GetType().Name;
  466. roiMode.Color = color;
  467. ROIList.Add(roiMode);
  468. roiMode = null;
  469. activeROIidx = ROIList.Count - 1;
  470. viewController.repaint();
  471. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  472. }
  473. }
  474. /// <summary>
  475. /// 在指定位置生成ROI--Rectangle1
  476. /// </summary>
  477. /// <param name="row1"></param>
  478. /// <param name="col1"></param>
  479. /// <param name="row2"></param>
  480. /// <param name="col2"></param>
  481. /// <param name="rois"></param>
  482. protected internal void genNurbs(HTuple rows, HTuple cols, ref System.Collections.Generic.List<ROI> rois)
  483. {
  484. setROIShape(new ROINurbs() );
  485. if (rois == null)
  486. {
  487. rois = new System.Collections.Generic.List<ROI>();
  488. }
  489. if (roiMode != null) //either a new ROI object is created
  490. {
  491. roiMode.createROINurbs (rows ,cols );
  492. roiMode.Type = roiMode.GetType().Name;
  493. rois.Add(roiMode);
  494. ROIList.Add(roiMode);
  495. roiMode = null;
  496. activeROIidx = ROIList.Count - 1;
  497. viewController.repaint();
  498. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  499. }
  500. }
  501. /// <summary>
  502. /// 在指定位置生成ROI--Rectangle1
  503. /// </summary>
  504. /// <param name="row1"></param>
  505. /// <param name="col1"></param>
  506. /// <param name="row2"></param>
  507. /// <param name="col2"></param>
  508. /// <param name="rois"></param>
  509. protected internal void genRect1(double row1, double col1, double row2, double col2, ref System.Collections.Generic.List<ROI> rois)
  510. {
  511. setROIShape(new ROIRectangle1());
  512. if (rois == null)
  513. {
  514. rois = new System.Collections.Generic.List<ROI>();
  515. }
  516. if (roiMode != null) //either a new ROI object is created
  517. {
  518. roiMode.createRectangle1(row1, col1, row2, col2);
  519. roiMode.Type = roiMode.GetType().Name;
  520. rois.Add(roiMode);
  521. ROIList.Add(roiMode);
  522. roiMode = null;
  523. activeROIidx = ROIList.Count - 1;
  524. viewController.repaint();
  525. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  526. }
  527. }
  528. /// <summary>
  529. /// 在指定位置生成ROI--Rectangle1
  530. /// </summary>
  531. /// <param name="row1"></param>
  532. /// <param name="col1"></param>
  533. /// <param name="row2"></param>
  534. /// <param name="col2"></param>
  535. /// <param name="rois"></param>
  536. protected internal void genInitRect1(int imageHeight, ref System.Collections.Generic.List<ROI> rois)
  537. {
  538. setROIShape(new ROIRectangle1());
  539. if (rois == null)
  540. {
  541. rois = new System.Collections.Generic.List<ROI>();
  542. }
  543. if (roiMode != null) //either a new ROI object is created
  544. {
  545. roiMode.createInitRectangle1(imageHeight );
  546. roiMode.Type = roiMode.GetType().Name;
  547. rois.Add(roiMode);
  548. ROIList.Add(roiMode);
  549. roiMode = null;
  550. activeROIidx = ROIList.Count - 1;
  551. viewController.repaint();
  552. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  553. }
  554. }
  555. /// <summary>
  556. /// 在指定位置生成ROI--Rectangle2
  557. /// </summary>
  558. /// <param name="row"></param>
  559. /// <param name="col"></param>
  560. /// <param name="phi"></param>
  561. /// <param name="length1"></param>
  562. /// <param name="length2"></param>
  563. /// <param name="rois"></param>
  564. protected internal void genRect2(double row, double col, double phi, double length1, double length2, ref System.Collections.Generic.List<ROI> rois)
  565. {
  566. setROIShape(new ROIRectangle2());
  567. if (rois == null)
  568. {
  569. rois = new System.Collections.Generic.List<ROI>();
  570. }
  571. if (roiMode != null) //either a new ROI object is created
  572. {
  573. roiMode.createRectangle2(row, col, phi, length1, length2);
  574. roiMode.Type = roiMode.GetType().Name;
  575. rois.Add(roiMode);
  576. ROIList.Add(roiMode);
  577. roiMode = null;
  578. activeROIidx = ROIList.Count - 1;
  579. viewController.repaint();
  580. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  581. }
  582. }
  583. /// <summary>
  584. /// 在指定位置生成ROI--Rectangle2
  585. /// </summary>
  586. /// <param name="row"></param>
  587. /// <param name="col"></param>
  588. /// <param name="phi"></param>
  589. /// <param name="length1"></param>
  590. /// <param name="length2"></param>
  591. /// <param name="rois"></param>
  592. protected internal void genInitRect2(double imageHeight, ref System.Collections.Generic.List<ROI> rois)
  593. {
  594. setROIShape(new ROIRectangle2());
  595. if (rois == null)
  596. {
  597. rois = new System.Collections.Generic.List<ROI>();
  598. }
  599. if (roiMode != null) //either a new ROI object is created
  600. {
  601. roiMode.createInitRectangle2(imageHeight );
  602. roiMode.Type = roiMode.GetType().Name;
  603. rois.Add(roiMode);
  604. ROIList.Add(roiMode);
  605. roiMode = null;
  606. activeROIidx = ROIList.Count - 1;
  607. viewController.repaint();
  608. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  609. }
  610. }
  611. /// <summary>
  612. /// 在指定位置生成ROI--Circle
  613. /// </summary>
  614. /// <param name="row"></param>
  615. /// <param name="col"></param>
  616. /// <param name="radius"></param>
  617. /// <param name="rois"></param>
  618. protected internal void genCircle(double row, double col, double radius, ref System.Collections.Generic.List<ROI> rois)
  619. {
  620. setROIShape(new ROICircle());
  621. if (rois == null)
  622. {
  623. rois = new System.Collections.Generic.List<ROI>();
  624. }
  625. if (roiMode != null) //either a new ROI object is created
  626. {
  627. roiMode.createCircle(row, col, radius);
  628. roiMode.Type = roiMode.GetType().Name;
  629. rois.Add(roiMode);
  630. ROIList.Add(roiMode);
  631. roiMode = null;
  632. activeROIidx = ROIList.Count - 1;
  633. viewController.repaint();
  634. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  635. }
  636. }
  637. protected internal void genCircularArc(double row, double col, double radius,double startPhi, double extentPhi,string direct, ref System.Collections.Generic.List<ROI> rois)
  638. {
  639. setROIShape(new ROICircularArc());
  640. if (rois == null)
  641. {
  642. rois = new System.Collections.Generic.List<ROI>();
  643. }
  644. if (roiMode != null) //either a new ROI object is created
  645. {
  646. roiMode.createCircularArc(row, col, radius, startPhi, extentPhi, direct);
  647. roiMode.Type = roiMode.GetType().Name;
  648. rois.Add(roiMode);
  649. ROIList.Add(roiMode);
  650. roiMode = null;
  651. activeROIidx = ROIList.Count - 1;
  652. viewController.repaint();
  653. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  654. }
  655. }
  656. /// <summary>
  657. /// 在指定位置生成ROI--Line
  658. /// </summary>
  659. /// <param name="beginRow"></param>
  660. /// <param name="beginCol"></param>
  661. /// <param name="endRow"></param>
  662. /// <param name="endCol"></param>
  663. /// <param name="rois"></param>
  664. protected internal void genLine(double beginRow, double beginCol, double endRow, double endCol, ref System.Collections.Generic.List<ROI> rois)
  665. {
  666. this.setROIShape(new ROILine());
  667. if (rois == null)
  668. {
  669. rois = new System.Collections.Generic.List<ROI>();
  670. }
  671. if (roiMode != null) //either a new ROI object is created
  672. {
  673. roiMode.createLine(beginRow, beginCol, endRow, endCol);
  674. roiMode.Type = roiMode.GetType().Name;
  675. rois.Add(roiMode);
  676. ROIList.Add(roiMode);
  677. roiMode = null;
  678. activeROIidx = ROIList.Count - 1;
  679. viewController.repaint();
  680. NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
  681. }
  682. }
  683. /// <summary>
  684. /// 获取当前选中ROI的信息
  685. /// </summary>
  686. /// <returns></returns>
  687. protected internal System.Collections.Generic.List<double> smallestActiveROI(out string name, out int index)
  688. {
  689. name = "";
  690. int activeROIIdx = this.getActiveROIIdx();
  691. index = activeROIIdx;
  692. if (activeROIIdx > -1)
  693. {
  694. ROI region = this.getActiveROI();
  695. Type type = region.GetType();
  696. name = type.Name;
  697. HTuple smallest = region.getModelData();
  698. System.Collections.Generic.List<double> resual = new System.Collections.Generic.List<double>();
  699. for (int i = 0; i < smallest.Length; i++)
  700. {
  701. resual.Add(smallest[i].D);
  702. }
  703. return resual;
  704. }
  705. else
  706. {
  707. return null;
  708. }
  709. }
  710. protected internal ROI smallestActiveROI(out System.Collections.Generic.List<double> data, out int index)
  711. {
  712. try
  713. {
  714. int activeROIIdx = this.getActiveROIIdx();
  715. index = activeROIIdx;
  716. data = new System.Collections.Generic.List<double>();
  717. if (activeROIIdx > -1)
  718. {
  719. ROI region = this.getActiveROI();
  720. Type type = region.GetType();
  721. HTuple smallest = region.getModelData();
  722. //////for (int i = 0; i < smallest.Length; i++)
  723. //////{
  724. ////// data.Add(smallest[i].D);
  725. //////}
  726. return region;
  727. }
  728. else
  729. {
  730. return null;
  731. }
  732. }
  733. catch (Exception)
  734. {
  735. data = null;
  736. index = 0;
  737. return null;
  738. }
  739. }
  740. /// <summary>
  741. /// 删除当前选中ROI
  742. /// </summary>
  743. /// <param name="roi"></param>
  744. protected internal void removeActiveROI(ref System.Collections.Generic.List<ROI> roi)
  745. {
  746. int activeROIIdx = this.getActiveROIIdx();
  747. if (activeROIIdx > -1)
  748. {
  749. this.removeActive();
  750. roi.RemoveAt(activeROIIdx);
  751. }
  752. }
  753. /// <summary>
  754. /// 选中激活ROI
  755. /// </summary>
  756. /// <param name="index"></param>
  757. protected internal void selectROI(int index)
  758. {
  759. this.activeROIidx = index;
  760. this.NotifyRCObserver(ROIController.EVENT_ACTIVATED_ROI);
  761. this.viewController.repaint();
  762. }
  763. /// <summary>
  764. /// 复位窗口显示
  765. /// </summary>
  766. protected internal void resetWindowImage()
  767. {
  768. //this.viewController.resetWindow();
  769. this.viewController.repaint();
  770. }
  771. protected internal void zoomWindowImage()
  772. {
  773. this.viewController.setViewState(HWndCtrl.MODE_VIEW_ZOOM);
  774. }
  775. protected internal void moveWindowImage()
  776. {
  777. this.viewController.setViewState(HWndCtrl.MODE_VIEW_MOVE);
  778. }
  779. protected internal void noneWindowImage()
  780. {
  781. this.viewController.setViewState(HWndCtrl.MODE_VIEW_NONE);
  782. }
  783. }//end of class
  784. }//end of namespace