CanvasCV: CanvasCV-doxygen/src/canvascv/shapes/line.h Source File
CanvasCV  1.0.0
line.h
1 #ifndef LINE_H
2 #define LINE_H
3 
4 #include "compoundshape.h"
5 #include "shapefactory.h"
6 #include "handle.h"
7 
8 #include <opencv2/highgui.hpp>
9 
10 namespace canvascv
11 {
12 
18 class Line : public CompoundShape
19 {
20 public:
22  void lockTail(bool isLocked);
23 
25  void lockHead(bool isLocked);
26 
28  void showTail(bool isVisible);
29 
31  void showHead(bool isVisible);
32 
34  void setTailPos(const cv::Point& pos);
35 
37  void setHeadPos(const cv::Point& pos);
38 
40  double length() const {
41  const cv::Point &p1 = (*pt1)();
42  const cv::Point &p2 = (*pt2)();
43  return sqrt(pow(p2.y-p1.y,2)+pow(p2.x-p1.x,2));
44  }
45 
47  bool isPointOnLine(const cv::Point &p3, int threshold=3) const
48  {
49  threshold += thickness/2;
50  const cv::Point &p1 = (*pt1)();
51  const cv::Point &p2 = (*pt2)();
52  double len = length();
53 
54  // Make sure it is roughly on the line
55  if (threshold <= abs((p2.y-p1.y)*p3.x-(p2.x-p1.x)*p3.y+p2.x*p1.y-p2.y*p1.x) / len)
56  {
57  return false;
58  }
59 
60  // For p3 to be in p1<->p2 range, distance from p3 to either p1 or p2
61  // cannot be more than distance from p1 to p2 (len).
62 
63  if(sqrt(pow(p3.y-p1.y,2)+pow(p3.x-p1.x,2)) > len)
64  {
65  return false;
66  }
67 
68  if(sqrt(pow(p3.y-p2.y,2)+pow(p3.x-p2.x,2)) > len)
69  {
70  return false;
71  }
72 
73  return true;
74  }
75 
77  Handle &getPT1();
78 
80  Handle &getPT2();
81 
83  const cv::Point &getTail() const;
84 
86  const cv::Point &getHead() const;
87 
88  virtual bool isAtPos(const cv::Point &pos)
89  {
90  return isPointOnLine(pos);
91  }
92 
93  virtual bool keyPressed(int &key);
94 
95  virtual std::list<Handle *> getConnectionTargets();
96 
97  virtual const char *getType() const;
98 
99  static const char * type;
100 
101 protected:
102  friend class ShapeFactory;
103  template <class T> friend class ShapeFactoryT;
104 
105  Line(const cv::Point &pos);
106 
107  virtual void draw(cv::Mat &canvas);
108  virtual bool mousePressed(const cv::Point &pos, bool onCreate = false);
109  virtual bool mouseMoved(const cv::Point &pos);
110  virtual bool mouseReleased(const cv::Point &pos);
111 
112  Handle* pt1;
113  Handle* pt2;
114 
115  virtual void reloadPointers(const std::list<Shape*> &lst, std::list<Shape*>::const_iterator &i);
116 };
117 
118 }
119 
124 #endif // LINE_H
virtual void draw(cv::Mat &canvas)
draw shape on the canvas
The ShapeFactory class.
Definition: shapefactory.h:24
virtual bool mouseMoved(const cv::Point &pos)
mouseMoved
virtual bool mouseReleased(const cv::Point &pos)
mouseReleased
virtual std::list< Handle * > getConnectionTargets()
getConnectionTargets
virtual bool keyPressed(int &key)
keyPressed will be called by Canvas for active shapes
The Line class.
Definition: line.h:18
virtual bool isAtPos(const cv::Point &pos)
returns true if shape is at pos, false otherwise
Definition: line.h:88
virtual bool mousePressed(const cv::Point &pos, bool onCreate=false)
mousePressed
void setHeadPos(const cv::Point &pos)
will move the head (Handle) of the line to pos
void lockHead(bool isLocked)
set if to lock the head (Handle) of the line
void lockTail(bool isLocked)
set if to lock the tail (Handle) of the line
Handle & getPT2()
get the Handle to the head of the
The CompoundShape class.
Definition: compoundshape.h:20
void showHead(bool isVisible)
set if to show the head (Handle) of the line
virtual const char * getType() const
getType is always implemented by derived to return the same static pointer per shape.
The ShapeFactoryT class.
Definition: shapefactory.h:45
const cv::Point & getHead() const
get the Point position og the head Handle
bool isPointOnLine(const cv::Point &p3, int threshold=3) const
returns true if the point &#39;p3&#39; is on the line, give or take &#39;threshold&#39; pixels
Definition: line.h:47
This namespace holds all the classes of the CanvasCV library.
Definition: canvas.h:20
double length() const
returns the length of the line in pixels
Definition: line.h:40
const cv::Point & getTail() const
get the Point position og the tail Handle
Handle & getPT1()
get the Handle to the tail of the
void showTail(bool isVisible)
set if to show the tail (Handle) of the line
void setTailPos(const cv::Point &pos)
will move the tail (Handle) of the line to pos
The Handle class.
Definition: handle.h:19