appkit  1.5.1
RoadNarrows Robotics Application Kit
State.orig.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: State.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-03-24 08:20:22 -0600 (Sun, 24 Mar 2013) $
14  * $Rev: 2782 $
15  *
16  * \brief Workflow state class.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2012-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 // Permission is hereby granted, without written agreement and without
27 // license or royalty fees, to use, copy, modify, and distribute this
28 // software and its documentation for any purpose, provided that
29 // (1) The above copyright notice and the following two paragraphs
30 // appear in all copies of the source code and (2) redistributions
31 // including binaries reproduces these notices in the supporting
32 // documentation. Substantial modifications to this software may be
33 // copyrighted by their authors and need not follow the licensing terms
34 // described here, provided that the new terms are clearly indicated in
35 // all files where they apply.
36 //
37 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
38 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
39 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
40 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
41 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
42 // THE POSSIBILITY OF SUCH DAMAGE.
43 //
44 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
45 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
46 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
47 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
48 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
49 //
50 ////////////////////////////////////////////////////////////////////////////////
51 
52 #ifndef _RNR_STATE_H
53 #define _RNR_STATE_H
54 
55 #include <vector>
56 
57 #include "rnr/rnrconfig.h"
58 
59 #include "opencv/cv.h"
60 #include "opencv/highgui.h"
61 
62 #include "rnr/Camera.h"
63 #include "rnr/win.h"
64 #include "rnr/Session.h"
65 
66 /*!
67  * \brief RoadNarrows Robotics
68  */
69 namespace rnr
70 {
71  // class StateMach
72  // addState(State)
73  // addStates(State, ...)
74  // setStartState(State)
75  // state enums start, terminate, this
76  // getCurStateId()
77  // getPrevStateId()
78  // start()
79 
80  //----------------------------------------------------------------------------
81  // State Class
82  //----------------------------------------------------------------------------
83 
84  // data: ref, name, value, transition table
85  // receiveEvent()
86  // newstate = dispatchEvent()
87  // fnGuard
88  // fnTrans
89  // fnTransDefault
90  // getStateId()
91  // getRefId()
92  // getStateName()
93  // getCurEvent()
94  // queueNextEvent()
95 
96  /*!
97  * Workflow Simplified State Machine Class
98  */
99  class State
100  {
101  public:
102  State(Session &session, int nInitAction=UIActionNone);
103 
104  virtual ~State();
105 
106  virtual int getNextAction(Session &session, int nTimeoutMs);
107 
108  /*!
109  * \brief Set the next input action to process.
110  *
111  * Subsequent call to getNextAction() will retrieve this action.
112  *
113  * \return The to-be-retrieved next action.
114  */
115  virtual int setNextAction(int nAction)
116  {
117  if( m_pMenu != NULL )
118  {
119  m_pMenu->SetCurrentAction(nAction);
120  }
121  }
122 
123  /*!
124  * \brief Get the currently retrieved input action.
125  *
126  * \return Current action.
127  */
129  {
130  return m_nCurAction;
131  }
132 
133  /*!
134  * \brief Test if state has fatal condition.
135  *
136  * \return Returns true or false.
137  */
138  bool hasFatal()
139  {
140  return m_bFatal;
141  }
142 
143  void setFatal()
144  {
145  m_bFatal = true;
146  }
147 
148  bool isModified()
149  {
150  return m_bModified;
151  }
152 
153  void setModifiedState(bool bModified)
154  {
155  m_bModified = bModified;
156  }
157 
158  /*!
159  * \brief Enable/disable masking of hard button input.
160  *
161  * \param bEnDis Enable (true) or disable (false) reading hard button input.
162  */
163  void maskHardButton(bool bEnDis)
164  {
165  m_bHardBttnEnable = bEnDis;
166  }
167 
168  virtual bool isHardButtonPushed(Session &session);
169 
170  /*!
171  * \brief Prompt user, given the state
172  *
173  * \param session Session data.
174  */
175  virtual void prompt(Session &session) { }
176 
177  virtual void setMenuStates(Session &session) { }
178 
179  //.........................................................................
180  // State Transition Functions
181  //.........................................................................
182  virtual void transQuit(Session &session)
183  {
184  session.PushState(Session::StateIdEnd);
185  }
186 
187  virtual void transPrev(Session &session)
188  {
189  session.PopState();
190  }
191 
192  protected:
193  Window *m_pWin; ///< bound window
194  Menu *m_pMenu; ///< button menu
195  int m_nInitAction; ///< initial state input action
196  int m_nCurAction; ///< current state input action
197  bool m_bFatal; ///< fatal error
198  bool m_bModified; ///< state [not] modified
199  bool m_bHardBttnEnable; ///< enable/disable hard button input
200 
201  virtual void buildInterface(Session &session);
202  virtual void destroyInterface();
203  virtual void initButtonMenu() { }
204  };
205 
206 
207  //----------------------------------------------------------------------------
208  // StateImage Class
209  //----------------------------------------------------------------------------
210 
211  /*!
212  * Image Manipulation Workflow State Data Class
213  */
214  class StateImage : public State
215  {
216  public:
217  Mouse m_mouse; ///< mouse instance
218  IplImage *m_pImgIoI; ///< pointer to current image of interest
219  IplImage *m_pImgShadow; ///< pointer to "shadow" (low-res) IoI
220  IplImage *m_pImgMarked; ///< pointer to marked up image
221  IplImage *m_pImgDisplay; ///< pointer to displayed image
222  CvSize m_sizeDisplay; ///< size of display region
223  CvSize m_sizeIoI; ///< image of interest size
224  WinIoI *m_pTransIoI; ///< displayed ioi transformation
225  WinIoI *m_pTransShadow; ///< displayed shadow transformation
226  CvRect m_rectIoIRoI; ///< image of interest region of interest
227  CvPoint m_ptPanRaw; ///< last pan raw point position
228  CvPoint m_ptPan; ///< last transformed pan point position
229  bool m_bIoICanZoomIn; ///< image of interest can [not] zoom in
230  bool m_bIoICanZoomOut; ///< image of interest can [not] zoom out
231  bool m_bIoIIsPanning; ///< image of interest panning [not] active
232  bool m_bShadowChanged; ///< shadow image does [not] need to update
233 
234  StateImage(Session &session,
235  const CvSize &sizeDisplay,
236  const CvSize &sizeIoI,
237  int nInitAction=UIActionNone);
238 
239  virtual ~StateImage();
240 
241  virtual int getNextAction(Session &session, int nTimeoutMs);
242 
243  virtual void resetView(Session &session);
244 
245  void setIoI(IplImage *pImgIoI)
246  {
247  m_pImgIoI = pImgIoI;
248  }
249 
250  void markupPanImg(Session &session);
251 
252  virtual void markupImg(Session &session) { }
253  virtual void showMarkupImg(Session &session);
254  virtual void showImg(Session &session, IplImage *pImg);
255  virtual CvPoint mapPt(CvPoint& ptHiRes);
256  virtual CvPoint mapPtPan(CvPoint& ptHiRes);
257  virtual CvRect mapRect(CvRect& rectHiRes);
258 
259  //.........................................................................
260  // State Transition Functions
261  //.........................................................................
262  virtual void transZoomIn(Session &session);
263  virtual void transZoomOut(Session &session);
264  virtual void transPan(Session &session);
265 
266  protected:
267  virtual void buildInterface(Session &session);
268  virtual void destroyInterface();
269  };
270 
271 
272  //----------------------------------------------------------------------------
273  // StateCvCamera Class
274  //----------------------------------------------------------------------------
275 
276  /*!
277  * OpenCV Video Camera Control Workflow State Data Class
278  */
279  class StateCvCamera : public State
280  {
281  public:
282  CvCamera m_camera; ///< video camera instance
283  IplImage *m_pImgMarked; ///< pointer to marked up image
284  IplImage *m_pImgDisplay; ///< pointer to displayed image
285  CvSize m_sizeDisplay; ///< size of display region
286  CameraRes m_eVideoRes; ///< video resolution
287  WinIoI *m_pTransIoI; ///< displayed ioi transformation
288  bool m_bPaused; ///< capturing images is [not] paused
289 
290  StateCvCamera(Session &session,
291  const CvSize &sizeDisplay,
292  const CameraRes eVideoRes,
293  const CameraRes eImageRes,
294  int nInitAction=UIActionNone);
295 
296  virtual ~StateCvCamera();
297 
298  virtual int getNextAction(Session &session, int nTimeoutMs);
299 
300  /*!
301  * \brief Test if state is paused.
302  *
303  * \return Returns true or false.
304  */
305  bool isPaused()
306  {
307  return m_bPaused;
308  }
309 
310  void setPauseState(bool bPaused)
311  {
312  m_bPaused = bPaused;
313  }
314 
315  virtual void cloneFrame(Session &session);
316  virtual void markupImg(Session &session) { }
317  virtual void showMarkupImg(Session &session);
318  virtual void showImg(Session &session, IplImage *pImg);
319 
320  //.........................................................................
321  // State Transition Functions
322  //.........................................................................
323  virtual void transRunVideo(Session &session);
324  virtual void transRunMarkupVideo(Session &session);
325  virtual void transStopVideo(Session &session);
326 
327  protected:
328  virtual void buildInterface(Session &session);
329  virtual void destroyInterface();
330  };
331 
332 
333  //----------------------------------------------------------------------------
334  // StateGstCamera Class
335  //----------------------------------------------------------------------------
336 
337  /*!
338  * OpenCV Video Camera Control Workflow State Data Class
339  */
340  class StateGstCamera : public State
341  {
342  public:
343  GstCamera m_camera; ///< video camera instance
344  bool m_bPaused; ///< capturing images is [not] paused
345  CvSize m_sizeVidWin; ///< size of video window
346 
347  StateGstCamera(Session &session,
348  const CvSize &sizeDisplay,
349  const CameraRes eVideoRes,
350  const CameraRes eImageRes,
351  int nInitAction=UIActionNone);
352 
353  virtual ~StateGstCamera();
354 
355  virtual int getNextAction(Session &session, int nTimeoutMs);
356 
357  /*!
358  * \brief Test if state is paused.
359  *
360  * \return Returns true or false.
361  */
362  bool isPaused()
363  {
364  return m_bPaused;
365  }
366 
367  void setPauseState(bool bPaused)
368  {
369  m_bPaused = bPaused;
370  }
371 
372  virtual void cloneFrame(Session &session);
373  virtual void markupImg(Session &session) { }
374  virtual void showMarkupImg(Session &session);
375  virtual void showImg(Session &session, IplImage *pImg);
376 
377  //.........................................................................
378  // State Transition Functions
379  //.........................................................................
380  virtual void transRunVideo(Session &session);
381  virtual void transRunMarkupVideo(Session &session);
382  virtual void transStopVideo(Session &session);
383 
384  protected:
385  virtual void buildInterface(Session &session);
386  virtual void destroyInterface();
387  };
388 
389 } // namespace rnr
390 
391 
392 #endif // _RNR_STATE_H
bool m_bIoICanZoomIn
image of interest can [not] zoom in
Definition: State.orig.h:229
CvRect m_rectIoIRoI
image of interest region of interest
Definition: State.orig.h:226
bool isPaused()
Test if state is paused.
Definition: State.orig.h:305
WinIoI * m_pTransIoI
displayed ioi transformation
Definition: State.orig.h:287
CvSize m_sizeDisplay
size of display region
Definition: State.orig.h:222
int m_nCurAction
current state input action
Definition: State.orig.h:196
virtual int setNextAction(int nAction)
Set the next input action to process.
Definition: State.orig.h:115
bool m_bPaused
capturing images is [not] paused
Definition: State.orig.h:288
bool m_bIoICanZoomOut
image of interest can [not] zoom out
Definition: State.orig.h:230
CvCamera m_camera
video camera instance
Definition: State.orig.h:282
bool m_bPaused
capturing images is [not] paused
Definition: State.orig.h:344
Menu * m_pMenu
button menu
Definition: State.orig.h:194
IplImage * m_pImgDisplay
pointer to displayed image
Definition: State.orig.h:284
bool m_bHardBttnEnable
enable/disable hard button input
Definition: State.orig.h:199
bool hasFatal()
Test if state has fatal condition.
Definition: State.orig.h:138
virtual void prompt(Session &session)
Prompt user, given the state.
Definition: State.orig.h:175
CvPoint m_ptPan
last transformed pan point position
Definition: State.orig.h:228
WinIoI * m_pTransShadow
displayed shadow transformation
Definition: State.orig.h:225
State(int nStateId, const std::string &strStateName="", const std::string &strRefTag="")
Initialization constructor.
Definition: State.h:180
Window * m_pWin
bound window
Definition: State.orig.h:193
bool m_bIoIIsPanning
image of interest panning [not] active
Definition: State.orig.h:231
IplImage * m_pImgMarked
pointer to marked up image
Definition: State.orig.h:283
IplImage * m_pImgIoI
pointer to current image of interest
Definition: State.orig.h:218
Mouse m_mouse
mouse instance
Definition: State.orig.h:217
IplImage * m_pImgShadow
pointer to "shadow" (low-res) IoI
Definition: State.orig.h:219
Session Class.
Definition: Session.h:84
int m_nInitAction
initial state input action
Definition: State.orig.h:195
IplImage * m_pImgMarked
pointer to marked up image
Definition: State.orig.h:220
GstCamera m_camera
video camera instance
Definition: State.orig.h:343
void maskHardButton(bool bEnDis)
Enable/disable masking of hard button input.
Definition: State.orig.h:163
bool m_bModified
state [not] modified
Definition: State.orig.h:198
CameraRes m_eVideoRes
video resolution
Definition: State.orig.h:286
CvSize m_sizeDisplay
size of display region
Definition: State.orig.h:285
bool isPaused()
Test if state is paused.
Definition: State.orig.h:362
CvSize m_sizeIoI
image of interest size
Definition: State.orig.h:223
CvPoint m_ptPanRaw
last pan raw point position
Definition: State.orig.h:227
bool m_bShadowChanged
shadow image does [not] need to update
Definition: State.orig.h:232
WinIoI * m_pTransIoI
displayed ioi transformation
Definition: State.orig.h:224
virtual ~State()
Destructor.
Definition: State.cxx:134
RoadNarrows Robotics.
Definition: Camera.h:74
CvSize m_sizeVidWin
size of video window
Definition: State.orig.h:345
int getCurAction()
Get the currently retrieved input action.
Definition: State.orig.h:128
IplImage * m_pImgDisplay
pointer to displayed image
Definition: State.orig.h:221
bool m_bFatal
fatal error
Definition: State.orig.h:197