libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNPoint2DInt.cpp
Go to the documentation of this file.
1 /* Copyright 2008-2014 INSA Lyon, CoReNum
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: CRNPoint2DInt.cpp
19  * \author Yann LEYDIER
20  */
21 
23 #include <CRNData/CRNDataFactory.h>
24 #include <CRNException.h>
25 #include <CRNi18n.h>
26 
27 using namespace crn;
28 
39 {
40  if (el.GetName() != "Point2DInt")
41  {
42  throw ExceptionInvalidArgument(StringUTF8("void Point2DInt::Deserialize(xml::Element &el): ") +
43  _("Wrong XML element."));
44  }
45  int x = el.GetAttribute<int>("x", false); // may throw
46  int y = el.GetAttribute<int>("y", false); // may throw
47  X = x;
48  Y = y;
49 }
50 
58 {
59  xml::Element el(parent.PushBackElement("Point2DInt"));
60 
61  el.SetAttribute("x", X);
62  el.SetAttribute("y", Y);
63 
64  return el;
65 }
66 
71 void Point2DInt::Advance(const Direction &dir, int step)
72 {
73  if (dir == Direction::LEFT)
74  X -= step;
75  else if (dir == Direction::RIGHT)
76  X += step;
77  else if (dir == Direction::TOP)
78  Y -= step;
79  else if (dir == Direction::BOTTOM)
80  Y += step;
81 }
82 
88 std::vector<Point2DInt> crn::MakeSegment(const Point2DInt &p_begin, const Point2DInt &p_end)
89 {
90  std::vector<Point2DInt> segment;
91 
92  int x1 = p_begin.X;
93  int y1 = p_begin.Y;
94  int x2 = p_end.X;
95  int y2 = p_end.Y;
96 
97  segment.push_back(Point2DInt(x1, y1));
98 
99  int lg_delta, sh_delta, cycle, lg_step, sh_step;
100  lg_delta = (int)x2 - (int)x1;
101  sh_delta = (int)y2 - (int)y1;
102  if (lg_delta < 0)
103  {
104  lg_delta = -lg_delta;
105  lg_step = -1;
106  }
107  else
108  lg_step = 1;
109 
110  if (sh_delta < 0)
111  {
112  sh_delta = -sh_delta;
113  sh_step = -1;
114  }
115  else
116  sh_step = 1;
117 
118  if (sh_delta < lg_delta)
119  {
120  cycle = lg_delta >> 1;
121  while (x1 != x2)
122  {
123  x1 += lg_step;
124  cycle = cycle + sh_delta;
125  if (cycle > lg_delta)
126  {
127  y1 += sh_step;
128  cycle = cycle - lg_delta;
129 
130  segment.push_back(Point2DInt(x1, y1));
131  }
132  }
133  }
134  else
135  {
136  cycle = sh_delta >> 1;
137  int tmp = lg_delta;
138  lg_delta = sh_delta;
139  sh_delta = tmp;
140  tmp = lg_step;
141  lg_step = sh_step;
142  sh_step = tmp;
143  while (y1 != y2)
144  {
145  y1 += lg_step;
146  cycle = cycle + sh_delta;
147  if (cycle > lg_delta)
148  {
149  x1 += sh_step;
150  cycle = cycle - lg_delta;
151 
152  segment.push_back(Point2DInt(x1, y1));
153  }
154  }
155  }
156 
157  segment.push_back(Point2DInt(x2, y2));
158 
159  return segment;
160 }
161 
163  CRN_DATA_FACTORY_REGISTER(U"Point2DInt", Point2DInt)
164  Cloner::Register<Point2DInt>();
165 CRN_END_CLASS_CONSTRUCTOR(Point2DInt)
166 
std::vector< Point2DInt > MakeSegment(const Point2DInt &p_begin, const Point2DInt &p_end)
Make a segment between two points.
XML element.
Definition: CRNXml.h:135
StringUTF8 GetName() const
Gets the label of the element.
Definition: CRNXml.h:146
#define _(String)
Definition: CRNi18n.h:51
Direction
An enumeration of directions.
Definition: CRNMath.h:122
#define CRN_END_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:198
void Advance(const Direction &dir, int step=1)
Moves the point towards a direction.
#define CRN_DATA_FACTORY_REGISTER(elemname, classname)
Registers a class to the data factory.
void SetAttribute(const StringUTF8 &name, const StringUTF8 &value)
Sets the value of an attribute.
Definition: CRNXml.cpp:595
T GetAttribute(const StringUTF8 &name, bool silent=true) const
Gets an attribute.
Definition: CRNXml.h:219
xml::Element Serialize(xml::Element &parent) const
Dumps the object to an XML element. Unsafe.
A character string class.
Definition: CRNStringUTF8.h:49
void Deserialize(xml::Element &el)
Initializes the object from an XML element. Unsafe.
A 2D point class.
Definition: CRNPoint2DInt.h:39
Element PushBackElement(const StringUTF8 &name)
Adds an element at the end of the children list.
Definition: CRNXml.cpp:355
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107
#define CRN_BEGIN_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:185