libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNStroke.h
Go to the documentation of this file.
1 /* Copyright 2008-2016 INSA Lyon, CoReNum, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNStroke.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNSTROKE_HEADER
23 #define CRNSTROKE_HEADER
24 
25 #include <CRNObject.h>
26 #include <CRNData/CRNVector.h>
28 #include <CRNGeometry/CRNRect.h>
29 
30 namespace crn
31 {
32  class Stroke;
33  CRN_ALIAS_SMART_PTR(Stroke)
34 }
35 namespace crn
36 {
37  /****************************************************************************/
47  class Stroke: public Object
48  {
49  public:
50  using datatype = std::vector<Point2DInt>;
51 
53  Stroke();
55  virtual ~Stroke() override;
56 
57  Stroke(const Stroke &other);
58  Stroke& operator=(const Stroke &other);
59  Stroke(Stroke&&) = default;
60  Stroke& operator=(Stroke&&) = default;
61 
63  void AddPoint(const Point2DInt &p);
64 
66  void MergeAtEnd(const Stroke &str);
68  void MergeWithXInterpolation(const Stroke &str, unsigned int step = 1);
69 
71  size_t GetNbPoints() const noexcept { return points.size(); }
73  Point2DInt& GetPoint(size_t index);
75  const Point2DInt& GetPoint(size_t index) const;
77  Point2DInt& operator[](size_t index) { return points[index]; }
79  const Point2DInt& operator[](size_t index) const { return points[index]; }
80 
82  double GetFirstYAtX(double x) const;
83 
85  Rect GetBBox() const;
86 
88  Stroke MakeIntersection(const Rect &rect) const;
90  Stroke MakeFirstIntersection(const Rect &rect) const;
91 
92  using iterator = datatype::iterator;
94  iterator begin() { return points.begin(); }
96  iterator end() { return points.end(); }
97  using const_iterator = datatype::const_iterator;
99  const_iterator begin() const { return points.begin(); }
101  const_iterator end() const { return points.end(); }
103  const_iterator cbegin() const { return points.cbegin(); }
105  const_iterator cend() const { return points.cend(); }
106 
108  void Deserialize(xml::Element &el);
110  xml::Element Serialize(xml::Element &parent) const;
111 
112  private:
113  datatype points;
114  mutable SRect bbox;
118  };
119  template<> struct IsSerializable<Stroke> : public std::true_type {};
120  template<> struct IsClonable<Stroke> : public std::true_type {};
121 
123  inline size_t Size(const Stroke &s) noexcept { return s.GetNbPoints(); }
124 }
127 
128 #endif
const_iterator begin() const
Returns a new const_iterator on the first point of the stroke.
Definition: CRNStroke.h:99
#define CRN_SERIALIZATION_CONSTRUCTOR(classname)
Defines a default constructor from xml element.
Definition: CRNObject.h:165
#define CRN_ADD_RANGED_FOR_TO_CONST_POINTERS(TYPE)
Enables ranged for for smart pointers on a type.
Definition: CRNForeach.h:51
Rect GetBBox() const
Returns the bounding box using integer values.
Definition: CRNStroke.cpp:257
Stroke & operator=(const Stroke &other)
Definition: CRNStroke.cpp:47
XML element.
Definition: CRNXml.h:135
Stroke MakeIntersection(const Rect &rect) const
Creates a new stroke that is the intersection of the stroke with a rectangle.
Definition: CRNStroke.cpp:286
void AddPoint(const Point2DInt &p)
Adds a point to the stroke.
Definition: CRNStroke.cpp:107
Stroke()
Default constructor.
A list of points.
Definition: CRNStroke.h:47
#define CRN_ADD_RANGED_FOR_TO_POINTERS(TYPE)
Enables ranged for for smart pointers on a type.
Definition: CRNForeach.h:37
Stroke MakeFirstIntersection(const Rect &rect) const
Creates a new stroke that is the first intersection of the stroke with a rectangle.
Definition: CRNStroke.cpp:306
void MergeAtEnd(const Stroke &str)
Adds a batch of points directly at the end of the stroke.
Definition: CRNStroke.cpp:121
void MergeWithXInterpolation(const Stroke &str, unsigned int step=1)
Adds a batch of points at their original position and adds points to interpolate. ...
Definition: CRNStroke.cpp:152
std::vector< Point2DInt > datatype
Definition: CRNStroke.h:50
size_t GetNbPoints() const noexcept
Returns the number of points in the stroke.
Definition: CRNStroke.h:71
const_iterator end() const
Returns a new const_iterator after the last point of the stroke.
Definition: CRNStroke.h:101
size_t Size(const Vector &v) noexcept
Size of a vector.
Definition: CRNVector.h:169
Point2DInt & GetPoint(size_t index)
Returns a reference to a point.
Definition: CRNStroke.cpp:208
const Point2DInt & operator[](size_t index) const
Returns a reference to a point (fast and unsafe).
Definition: CRNStroke.h:79
const_iterator cbegin() const
Returns a new const_iterator on the first point of the stroke.
Definition: CRNStroke.h:103
Point2DInt & operator[](size_t index)
Returns a reference to a point (fast and unsafe).
Definition: CRNStroke.h:77
iterator begin()
Returns a new iterator on the first point of the stroke.
Definition: CRNStroke.h:94
void Deserialize(xml::Element &el)
Initializes the object from an XML element.
Definition: CRNStroke.cpp:63
double GetFirstYAtX(double x) const
Returns the ordinate of the first point with abscissa = x.
Definition: CRNStroke.cpp:242
virtual ~Stroke() override
Destructor.
const_iterator cend() const
Returns a new const_iterator after the last point of the stroke.
Definition: CRNStroke.h:105
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Definition: CRNObject.h:173
CRN_ALIAS_SMART_PTR(ImageBW)
A 2D point class.
Definition: CRNPoint2DInt.h:39
iterator end()
Returns a new iterator after the last point of the stroke.
Definition: CRNStroke.h:96
xml::Element Serialize(xml::Element &parent) const
Dumps the object to an XML element.
Definition: CRNStroke.cpp:87
A rectangle class.
Definition: CRNRect.h:46