KSquare Utilities
Point.cpp
Go to the documentation of this file.
1 /* Point.cpp -- Represents the coordinates in a Raster image.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #include "FirstIncludes.h"
6 #include <string>
7 #include <iostream>
8 #include <vector>
9 #include <math.h>
10 #include "MemoryDebug.h"
11 using namespace std;
12 
13 
14 #include "Point.h"
15 using namespace KKB;
16 
17 
19  row (0),
20  col (0)
21 {
22 }
23 
24 
25 
26 Point::Point (const Point& point):
27  row (point.row),
28  col (point.col)
29 {
30 }
31 
32 
33 
35  kkint16 _col
36  ):
37  row (_row),
38  col (_col)
39 {}
40 
41 
42 
44  kkint32 _col
45  ):
46  row (_row),
47  col (_col)
48 {}
49 
50 
51 
52 Point::Point (float _row,
53  float _col
54  ):
55  row ((kkint32)(_row + 0.5)),
56  col ((kkint32)(_col + 0.5))
57 {}
58 
59 
60 bool Point::operator== (Point& r) const
61 {
62  return ((row == r.row) && (col == r.col));
63 }
64 
65 
66 bool Point::operator!= (const Point& r) const
67 {
68  return ((row != r.row) || (col != r.col));
69 }
70 
71 
72 Point Point::operator+ (const Point& r) const
73 {
74  return Point (row + r.row, col + r.col);
75 }
76 
77 
78 Point Point::operator- (const Point& r) const
79 {
80  return Point (row - r.row, col - r.col);
81 }
82 
83 
84 Point& Point::operator= (const Point& r)
85 {
86  row = r.row;
87  col = r.col;
88  return *this;
89 }
90 
91 
92 
93 PointList::PointList (const PointList& pointList):
94  KKQueue<Point> (true)
95 {
96  PointList::const_iterator idx;
97  for (idx = pointList.begin (); idx != pointList.end (); idx++)
98  {
99  PushOnBack (new Point (*(*idx)));
100  }
101 }
102 
103 
104 
105 PointList::PointList (bool _owner):
106  KKQueue<Point> (_owner)
107 {
108 }
109 
110 
112  kkint32& minCol,
113  kkint32& maxRow,
114  kkint32& maxCol
115  )
116 {
117  minRow = minCol = 999999;
118  maxRow = maxCol = -1;
119 
120  for (iterator x = begin (); x != end (); x++)
121  {
122  PointPtr p = *x;
123  if (p->Row () < minRow)
124  minRow = p->Row ();
125 
126  if (p->Row () > maxRow)
127  maxRow = p->Row ();
128 
129  if (p->Col () < minCol)
130  minCol = p->Col ();
131 
132  if (p->Col () > maxCol)
133  maxCol = p->Col ();
134  }
135 }
136 
137 
139 {
140  kkint32 totalRow = 0;
141  kkint32 totalCol = 0;
142  for (iterator x = begin (); x != end (); x++)
143  {
144  PointPtr p = *x;
145  totalRow += p->Row ();
146  totalCol += p->Col ();
147  }
148 
149  kkint32 centerRow = (kkint32)((double)totalRow / (double)size () + 0.5);
150  kkint32 centerCol = (kkint32)((double)totalCol / (double)size () + 0.5);
151  return Point (centerRow, centerCol);
152 }
153 
154 
155 
156 KKStr PointList::ToDelStr (char del) const
157 {
158  if (QueueSize () < 1)
159  return "[]";
160 
161  KKStr result (QueueSize () * 10);
162 
163  int count = 0;
164  PointList::const_iterator idx;
165  for (idx = begin (); idx != end (); ++idx, ++count)
166  {
167  PointPtr p = *idx;
168  if (count > 0)
169  result << del;
170  result << p->Row () << del << p->Col ();
171  }
172  return result;
173 } /* ToDelStr */
174 
175 
176 PointListPtr PointList::FromDelStr (const KKStr& _s)
177 {
178  PointListPtr result = new PointList (true);
179 
180  KKStr s (_s);
181  s.TrimLeft ();
182 
183  while (s.Len () > 0)
184  {
185  char nextCh = s.FirstChar ();
186  char endPairChar = 0;
187  if (nextCh == '[')
188  endPairChar = ']';
189 
190  else if (nextCh == '(')
191  endPairChar = ')';
192 
193  else
194  {
195  // Not Bracketed.
196  endPairChar = 0;
197  kkint16 row = (kkint16)s.ExtractTokenInt (",\t\n\t");
198  kkint16 col = (kkint16)s.ExtractTokenInt (",\t\n\t");
199  result->PushOnBack (new Point (row, col));
200  }
201 
202  if (endPairChar != 0)
203  {
204  KKStr pairStr = "";
205  kkint32 idx = s.Find (endPairChar);
206  if (idx >= 0)
207  {
208  pairStr = s.SubStrPart (0, idx - 1);
209  s = s.SubStrPart (idx + 1);
210  }
211  else
212  {
213  pairStr = s;
214  s = "";
215  }
216 
217  kkint16 row = (kkint16)pairStr.ExtractTokenInt (",");
218  kkint16 col = (kkint16) pairStr.ExtractTokenInt (",");
219  result->PushOnBack (new Point (row, col));
220  nextCh = s.FirstChar ();
221  if ((nextCh == ',') || (nextCh == '\n') || (nextCh == '\r') || (nextCh == '\t'))
223  }
224  s.TrimLeft ();
225  }
226 
227  return result;
228 } /* FromDelStr */
229 
230 
231 
232 
233 float PointList::ComputeSegmentLens (float heightFactor,
234  float widthFactor
235  ) const
236 {
237  if (QueueSize () < 1)
238  return 0.0f;
239 
240  float totalLen = 0.0f;
241 
242  const_iterator idx;
243  idx = begin ();
244  PointPtr lastPoint = *idx;
245  ++idx;
246 
247  while (idx != end ())
248  {
249  PointPtr nextPoint = *idx; ++idx;
250 
251  float deltaHeight = (float)(nextPoint->Row () - lastPoint->Row ()) * heightFactor;
252  float deltaWidth = (float)(nextPoint->Col () - lastPoint->Col ()) * widthFactor;
253 
254  float segmentLen = sqrt (deltaHeight * deltaHeight + deltaWidth * deltaWidth);
255 
256  totalLen += segmentLen;
257  lastPoint = nextPoint;
258  }
259 
260  return totalLen;
261 } /* ComputeSegmentLens */
262 
263 
264 
265 KKStr& KKB::operator<< (KKStr& left,
266  const Point& right
267  )
268 {
269  left << "[" << right.Row () << ", " << right.Col () << "]";
270  return left;
271 }
272 
273 
274 
275 ostream& KKB::operator<< (std::ostream& left,
276  const Point& right
277  )
278 {
279  left << "[" << right.Row () << ", " << right.Col () << "]";
280  return left;
281 }
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
__int32 kkint32
Definition: KKBaseTypes.h:88
Point & operator=(const Point &r)
Definition: Point.cpp:84
kkint32 Find(char c, kkint32 pos=0) const
Definition: KKStr.cpp:3952
kkint32 Col() const
Definition: Point.h:40
PointList(const PointList &pointList)
Definition: Point.cpp:93
Point operator+(const Point &r) const
Definition: Point.cpp:72
kkint32 Row() const
Definition: Point.h:39
KKStr & operator=(const char *src)
Definition: KKStr.cpp:1442
static PointListPtr FromDelStr(const KKStr &s)
Definition: Point.cpp:176
bool operator!=(const Point &r) const
Definition: Point.cpp:66
char FirstChar() const
Definition: KKStr.cpp:1970
PointList(bool _owner)
Definition: Point.cpp:105
float ComputeSegmentLens(float heightFactor, float widthFactor) const
Definition: Point.cpp:233
Point(float _row, float _col)
Definition: Point.cpp:52
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
Used by Raster class and MorphOp derived classes to denote a single pixel location in Raster image...
Definition: Point.h:20
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
KKTHread * KKTHreadPtr
PointList * PointListPtr
Definition: Point.h:78
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
void TrimLeft(const char *whiteSpaceChars="\n\r\t ")
Definition: KKStr.cpp:1745
KKStr SubStrPart(kkint32 firstChar, kkint32 lastChar) const
returns a SubString consisting of all characters starting at index &#39;firstChar&#39; and ending at &#39;lastInd...
Definition: KKStr.cpp:2802
KKStr ToDelStr(char del) const
Definition: Point.cpp:156
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
Point(const Point &point)
Definition: Point.cpp:26
std::ostream &__cdecl operator<<(std::ostream &os, const KKStr &str)
kkint32 ExtractTokenInt(const char *delStr)
Definition: KKStr.cpp:3129
Point(kkint32 _row, kkint32 _col)
Definition: Point.cpp:43
void ChopFirstChar()
Definition: KKStr.cpp:1649
Point CalculateCenterPoint()
Definition: Point.cpp:138
Point operator-(const Point &r) const
Definition: Point.cpp:78
KKStr & operator=(const KKStr &src)
Definition: KKStr.cpp:1390
Container object used to maintaining a list of pixel locations.
Definition: Point.h:75
void BoxCoordinites(kkint32 &minRow, kkint32 &minCol, kkint32 &maxRow, kkint32 &maxCol)
Definition: Point.cpp:111
Point(kkint16 _row, kkint16 _col)
Definition: Point.cpp:34
KKStr SubStrPart(kkint32 firstChar) const
returns a SubString consisting of all characters starting at index &#39;firstChar&#39; until the end of the s...
Definition: KKStr.cpp:2780
bool operator==(Point &r) const
Definition: Point.cpp:60
Point * PointPtr
Definition: Point.h:66