contrek 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -3
  3. data/CHANGELOG.md +25 -0
  4. data/Gemfile.lock +1 -1
  5. data/README.md +133 -33
  6. data/Rakefile +3 -0
  7. data/contrek.gemspec +6 -3
  8. data/ext/cpp_polygon_finder/PolygonFinder/Makefile +44 -0
  9. data/ext/cpp_polygon_finder/PolygonFinder/images/sample_10240x10240.png +0 -0
  10. data/ext/cpp_polygon_finder/PolygonFinder/src/Main.cpp +14 -5
  11. data/ext/cpp_polygon_finder/PolygonFinder/src/Tests.cpp +136 -15
  12. data/ext/cpp_polygon_finder/PolygonFinder/src/Tests.h +5 -4
  13. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/CpuTimer.h +44 -0
  14. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/Bitmap.cpp +8 -0
  15. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/Bitmap.h +3 -4
  16. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/FastPngBitmap.cpp +63 -573
  17. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/FastPngBitmap.h +17 -18
  18. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/RemoteFastPngBitmap.cpp +22 -5
  19. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/RemoteFastPngBitmap.h +4 -8
  20. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/spng.c +6980 -0
  21. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/spng.h +537 -0
  22. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/FinderUtils.cpp +101 -0
  23. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/FinderUtils.h +16 -0
  24. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/List.cpp +0 -3
  25. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/List.h +4 -7
  26. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/Lists.cpp +13 -13
  27. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/Lists.h +5 -7
  28. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/Node.cpp +47 -41
  29. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/Node.h +15 -10
  30. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/NodeCluster.cpp +181 -178
  31. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/NodeCluster.h +19 -20
  32. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/Polygon.h +20 -0
  33. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/PolygonFinder.cpp +52 -137
  34. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/PolygonFinder.h +85 -16
  35. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/RectBounds.h +39 -0
  36. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/ClippedPolygonFinder.cpp +14 -0
  37. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/ClippedPolygonFinder.h +17 -0
  38. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Cluster.cpp +117 -0
  39. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Cluster.h +32 -0
  40. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Cursor.cpp +344 -0
  41. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Cursor.h +46 -0
  42. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/EndPoint.cpp +14 -0
  43. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/EndPoint.h +22 -0
  44. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/FakeCluster.cpp +13 -0
  45. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/FakeCluster.h +17 -0
  46. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Finder.cpp +139 -0
  47. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Finder.h +52 -0
  48. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Hub.cpp +23 -0
  49. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Hub.h +40 -0
  50. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Part.cpp +70 -0
  51. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Part.h +47 -0
  52. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/PartPool.cpp +24 -0
  53. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/PartPool.h +23 -0
  54. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Partitionable.cpp +182 -0
  55. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Partitionable.h +30 -0
  56. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Polyline.cpp +108 -0
  57. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Polyline.h +52 -0
  58. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Poolable.cpp +59 -0
  59. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Poolable.h +52 -0
  60. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Position.cpp +31 -0
  61. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Position.h +25 -0
  62. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Queue.h +36 -0
  63. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Queueable.h +230 -0
  64. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Sequence.cpp +35 -0
  65. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Sequence.h +20 -0
  66. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Shape.cpp +26 -0
  67. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Shape.h +23 -0
  68. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Tile.cpp +105 -0
  69. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Tile.h +56 -0
  70. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/matchers/Matcher.cpp +3 -3
  71. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/matchers/Matcher.h +5 -8
  72. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/matchers/RGBMatcher.h +3 -7
  73. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/matchers/RGBNotMatcher.cpp +2 -2
  74. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/matchers/RGBNotMatcher.h +4 -8
  75. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/matchers/ValueNotMatcher.cpp +2 -2
  76. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/matchers/ValueNotMatcher.h +3 -7
  77. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/LinearReducer.cpp +23 -15
  78. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/LinearReducer.h +6 -8
  79. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/Reducer.cpp +2 -5
  80. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/Reducer.h +4 -8
  81. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/UniqReducer.cpp +9 -12
  82. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/UniqReducer.h +3 -7
  83. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/VisvalingamReducer.cpp +26 -27
  84. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/VisvalingamReducer.h +76 -87
  85. data/ext/cpp_polygon_finder/cpp_polygon_finder.cpp +167 -138
  86. data/ext/cpp_polygon_finder/extconf.rb +14 -0
  87. data/lib/contrek/bitmaps/sample_generator.rb +56 -0
  88. data/lib/contrek/cpp/cpp_concurrent_finder.rb +9 -0
  89. data/lib/contrek/finder/bounds.rb +17 -0
  90. data/lib/contrek/finder/concurrent/cluster.rb +2 -2
  91. data/lib/contrek/finder/concurrent/cursor.rb +8 -8
  92. data/lib/contrek/finder/concurrent/finder.rb +19 -15
  93. data/lib/contrek/finder/concurrent/part.rb +2 -2
  94. data/lib/contrek/finder/concurrent/partitionable.rb +1 -1
  95. data/lib/contrek/finder/concurrent/polyline.rb +17 -15
  96. data/lib/contrek/finder/concurrent/sequence.rb +11 -0
  97. data/lib/contrek/finder/concurrent/tile.rb +3 -3
  98. data/lib/contrek/finder/node_cluster.rb +16 -8
  99. data/lib/contrek/finder/polygon_finder.rb +9 -10
  100. data/lib/contrek/finder/result.rb +13 -0
  101. data/lib/contrek/results/cpp_result.rb +21 -0
  102. data/lib/contrek/version.rb +1 -1
  103. data/lib/contrek.rb +11 -1
  104. metadata +59 -10
  105. data/ext/cpp_polygon_finder/PolygonFinder/.cproject +0 -136
  106. data/ext/cpp_polygon_finder/PolygonFinder/.project +0 -27
  107. data/ext/cpp_polygon_finder/PolygonFinder/.settings/org.eclipse.ltk.core.refactoring.prefs +0 -2
  108. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/PngBitmap.cpp +0 -48
  109. data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/PngBitmap.h +0 -32
@@ -9,12 +9,12 @@
9
9
  #include "ValueNotMatcher.h"
10
10
  #include <iostream>
11
11
 
12
- ValueNotMatcher::ValueNotMatcher(char value) : Matcher(value) {
12
+ ValueNotMatcher::ValueNotMatcher(unsigned int value) : Matcher(value) {
13
13
  }
14
14
 
15
15
  ValueNotMatcher::~ValueNotMatcher() {
16
16
  }
17
17
 
18
- bool ValueNotMatcher::match(char value) {
18
+ bool ValueNotMatcher::match(unsigned int value) {
19
19
  return(this->value != value);
20
20
  }
@@ -6,16 +6,12 @@
6
6
  * Copyright 2025 Emanuele Cesaroni
7
7
  */
8
8
 
9
- #ifndef POLYGON_MATCHERS_VALUENOTMATCHER_H_
10
- #define POLYGON_MATCHERS_VALUENOTMATCHER_H_
11
-
9
+ #pragma once
12
10
  #include "Matcher.h"
13
11
 
14
12
  class ValueNotMatcher : public Matcher {
15
13
  public:
16
- ValueNotMatcher(char value);
14
+ explicit ValueNotMatcher(unsigned int value);
17
15
  virtual ~ValueNotMatcher();
18
- bool match(char value);
16
+ bool match(unsigned int value);
19
17
  };
20
-
21
- #endif /* POLYGON_MATCHERS_VALUENOTMATCHER_H_ */
@@ -8,33 +8,41 @@
8
8
 
9
9
  #include <iterator>
10
10
  #include <list>
11
+ #include <iostream>
12
+ #include <algorithm>
11
13
  #include "LinearReducer.h"
12
14
  #include "Reducer.h"
13
15
 
14
- LinearReducer::LinearReducer(std::list<Point*> *list_of_points) : Reducer(list_of_points) {
15
- }
16
-
17
- LinearReducer::~LinearReducer() {
16
+ LinearReducer::LinearReducer(std::vector<Point*> &list_of_points)
17
+ : Reducer(list_of_points) {
18
18
  }
19
19
 
20
20
  void LinearReducer::reduce() {
21
- std::list<Point*>::iterator start_p = this->points->begin();
22
- std::list<Point*>::iterator end_p = std::next(start_p, 1);
21
+ if (points.size() < 2) return;
23
22
 
24
- int *dir = seq_dir(*start_p, *end_p);
23
+ Point* start_p = points[0];
24
+ Point* end_p = points[1];
25
+ auto dir = seq_dir(start_p, end_p);
25
26
 
26
- for (std::list<Point*>::iterator point = std::next(end_p, 1); point != this->points->end(); ++point)
27
- { int *act_seq = seq_dir(*end_p, *point);
28
- if (act_seq[0] == dir[0] && act_seq[1] == dir[1])
29
- { point = this->points->erase(end_p);
30
- end_p = point;
27
+ for (size_t i = 2; i < points.size(); ++i) {
28
+ Point* point = points[i];
29
+ auto act_seq = seq_dir(end_p, point);
30
+ if (act_seq == dir) {
31
+ auto it = std::find_if(points.begin(), points.end(), [&](Point* p) { // TODO(ema): optimize...
32
+ return p->x == end_p->x && p->y == end_p->y;
33
+ });
34
+ if (it != points.end()) {
35
+ size_t removed_idx = std::distance(points.begin(), it);
36
+ points.erase(it);
37
+ if (removed_idx <= i) i--;
38
+ }
31
39
  } else {
32
40
  dir = act_seq;
33
- end_p = point;
34
41
  }
42
+ end_p = point;
35
43
  }
36
44
  }
37
45
 
38
- int *LinearReducer::seq_dir(Point *a, Point *b)
39
- { return(new int[2] {b->x - a->x, b->y - a->y});
46
+ std::array<int, 2> LinearReducer::seq_dir(Point *a, Point *b)
47
+ { return { b->x - a->x, b->y - a->y };
40
48
  }
@@ -6,18 +6,16 @@
6
6
  * Copyright 2025 Emanuele Cesaroni
7
7
  */
8
8
 
9
- #ifndef POLYGON_REDUCERS_LINEARREDUCER_H_
10
- #define POLYGON_REDUCERS_LINEARREDUCER_H_
11
-
9
+ #pragma once
10
+ #include <vector>
11
+ #include <array>
12
12
  #include "Reducer.h"
13
13
 
14
14
  class LinearReducer : public Reducer {
15
15
  public:
16
- LinearReducer(std::list<Point*> *list_of_points);
17
- virtual ~LinearReducer();
16
+ explicit LinearReducer(std::vector<Point*>& list_of_points);
18
17
  void reduce();
18
+
19
19
  private:
20
- int *seq_dir(Point *a, Point *b);
20
+ std::array<int, 2> seq_dir(Point *a, Point *b);
21
21
  };
22
-
23
- #endif /* POLYGON_REDUCERS_LINEARREDUCER_H_ */
@@ -8,11 +8,8 @@
8
8
 
9
9
  #include "Reducer.h"
10
10
 
11
- Reducer::Reducer(std::list<Point*> *list_of_points) {
12
- this->points = list_of_points;
13
- }
14
-
15
- Reducer::~Reducer() {
11
+ Reducer::Reducer(std::vector<Point*>& list_of_points)
12
+ : points(list_of_points) {
16
13
  }
17
14
 
18
15
  void Reducer::reduce() {
@@ -6,20 +6,16 @@
6
6
  * Copyright 2025 Emanuele Cesaroni
7
7
  */
8
8
 
9
- #ifndef POLYGON_REDUCERS_REDUCER_H_
10
- #define POLYGON_REDUCERS_REDUCER_H_
11
-
9
+ #pragma once
12
10
  #include <list>
11
+ #include <vector>
13
12
  #include "../finder/Node.h"
14
13
 
15
14
  struct Point;
16
15
  class Reducer {
17
16
  public:
18
- Reducer(std::list<Point*> *list_of_points);
19
- virtual ~Reducer();
17
+ explicit Reducer(std::vector<Point*>& list_of_points);
20
18
  virtual void reduce();
21
19
  protected:
22
- std::list<Point*> *points;
20
+ std::vector<Point*> &points;
23
21
  };
24
-
25
- #endif /* POLYGON_REDUCERS_REDUCER_H_ */
@@ -6,25 +6,22 @@
6
6
  * Copyright 2025 Emanuele Cesaroni
7
7
  */
8
8
 
9
+ #include <vector>
10
+ #include <algorithm>
11
+ #include <iostream>
9
12
  #include "UniqReducer.h"
10
13
  #include "Reducer.h"
11
- #include "algorithm"
12
- #include "list"
13
- #include "iterator"
14
- #include "iostream"
15
14
 
16
- UniqReducer::UniqReducer(std::list<Point*> *list_of_points) : Reducer(list_of_points) {
17
- }
18
-
19
- UniqReducer::~UniqReducer() {
15
+ UniqReducer::UniqReducer(std::vector<Point*>& list_of_points) : Reducer(list_of_points) {
20
16
  }
21
17
 
22
18
  struct is_near {
23
- bool operator() (Point *first, Point *second)
24
- { return (first->x == second->x && first->y == second->y); }
19
+ bool operator() (Point* first, Point* second) const {
20
+ return first->x == second->x && first->y == second->y;
21
+ }
25
22
  };
26
23
 
27
24
  void UniqReducer::reduce() {
28
- this->points->unique(is_near());
25
+ auto last = std::unique(points.begin(), points.end(), is_near());
26
+ points.erase(last, points.end());
29
27
  }
30
-
@@ -6,16 +6,12 @@
6
6
  * Copyright 2025 Emanuele Cesaroni
7
7
  */
8
8
 
9
- #ifndef POLYGON_REDUCERS_UNIQREDUCER_H_
10
- #define POLYGON_REDUCERS_UNIQREDUCER_H_
11
-
9
+ #pragma once
10
+ #include <vector>
12
11
  #include "Reducer.h"
13
12
 
14
13
  class UniqReducer : public Reducer {
15
14
  public:
16
- UniqReducer(std::list<Point*> *list_of_points);
17
- virtual ~UniqReducer();
15
+ explicit UniqReducer(std::vector<Point*>& list_of_points);
18
16
  void reduce();
19
17
  };
20
-
21
- #endif /* POLYGON_REDUCERS_UNIQREDUCER_H_ */
@@ -6,45 +6,44 @@
6
6
  * Copyright 2025 Emanuele Cesaroni
7
7
  */
8
8
 
9
- #include <iostream>
10
- #include <list>
9
+ #include <vector>
11
10
  #include "VisvalingamReducer.h"
12
- #include "Reducer.h"
13
11
 
14
- VisvalingamReducer::VisvalingamReducer(std::list<Point*> *list_of_points, float tolerance) : Reducer(list_of_points) {
12
+ VisvalingamReducer::VisvalingamReducer(std::vector<Point*>& list_of_points, float tolerance)
13
+ : Reducer(list_of_points) {
15
14
  this->tolerance = tolerance * tolerance;
16
15
  }
17
16
 
18
- VisvalingamReducer::~VisvalingamReducer() {
19
- }
17
+ VisvalingamReducer::~VisvalingamReducer() {}
20
18
 
21
19
  void VisvalingamReducer::reduce() {
22
- std::list<Point*> *new_points = this->simplify();
23
- this->points->assign(new_points->begin(), new_points->end());
20
+ std::vector<Point*> new_points = this->simplify();
21
+ points.assign(new_points.begin(), new_points.end());
24
22
  }
25
- std::list<Point*> *VisvalingamReducer::simplify() {
26
- Vertex *vwLine = VisvalingamReducer::Vertex::buildLine(this->points);
23
+
24
+ std::vector<Point*> VisvalingamReducer::simplify() {
25
+ Vertex* vwLine = Vertex::buildLine(points);
27
26
  float minArea = this->tolerance;
28
27
  for (;;) {
29
- minArea = this->simplifyVertex(vwLine);
30
- if (minArea >= this->tolerance) break;
28
+ minArea = simplifyVertex(vwLine);
29
+ if (minArea >= this->tolerance) break;
31
30
  }
32
- std::list<Point*> *simp = vwLine->getCoordinates();
33
- return(simp);
31
+ return Vertex::getCoordinates(vwLine);
34
32
  }
35
- float VisvalingamReducer::simplifyVertex(VisvalingamReducer::Vertex *vwLine) {
36
- VisvalingamReducer::Vertex *curr = vwLine;
33
+
34
+ float VisvalingamReducer::simplifyVertex(Vertex* vwLine) {
35
+ Vertex* curr = vwLine;
37
36
  float minArea = curr->getArea();
38
- VisvalingamReducer::Vertex *minVertex = nullptr;
39
- while (curr != nullptr) {
40
- float area = curr->getArea();
41
- if (area < minArea) {
42
- minArea = area;
43
- minVertex = curr;
44
- }
45
- curr = curr->get_next();
37
+ Vertex* minVertex = nullptr;
38
+ while (curr) {
39
+ float area = curr->getArea();
40
+ if (area < minArea) {
41
+ minArea = area;
42
+ minVertex = curr;
43
+ }
44
+ curr = curr->get_next();
46
45
  }
47
- if (minVertex != nullptr && minArea < this->tolerance) minVertex->remove();
48
- if (!vwLine->isLiving()) return -1;
49
- return(minArea);
46
+ if (minVertex && minArea < this->tolerance) minVertex->remove();
47
+ if (!vwLine->isLiving()) return -1;
48
+ return minArea;
50
49
  }
@@ -6,116 +6,105 @@
6
6
  * Copyright 2025 Emanuele Cesaroni
7
7
  */
8
8
 
9
- #ifndef POLYGON_REDUCERS_VISVALINGAMREDUCER_H_
10
- #define POLYGON_REDUCERS_VISVALINGAMREDUCER_H_
11
-
9
+ #pragma once
10
+ #include <vector>
12
11
  #include <cmath>
13
12
  #include <limits>
14
- #include <list>
15
13
  #include "Reducer.h"
16
- #include "../finder/Node.h"
17
14
 
18
15
  struct Point;
19
16
 
20
17
  class VisvalingamReducer : public Reducer {
21
18
  public:
22
- VisvalingamReducer(std::list<Point*> *list_of_points, float tolerance);
19
+ VisvalingamReducer(std::vector<Point*>& list_of_points, float tolerance);
23
20
  virtual ~VisvalingamReducer();
24
21
  void reduce();
25
- std::list<Point*> *simplify();
22
+ std::vector<Point*> simplify();
23
+
26
24
  class Triangle {
27
25
  public:
28
- static int area(Point *a, Point *b, Point *c) {
29
- return( std::abs( ((c->x - a->x) * (b->y - a->y) - (b->x - a->x) * (c->y - a->y)) / 2));
26
+ static int area(Point* a, Point* b, Point* c) {
27
+ return std::abs(((c->x - a->x) * (b->y - a->y) - (b->x - a->x) * (c->y - a->y)) / 2);
30
28
  }
31
29
  };
32
- class Vertex {
33
- const float MAX_AREA = std::numeric_limits<float>::max();
34
- Vertex(Point *pt) {
35
- this->pt = pt;
36
- this->prev = nullptr;
37
- this->next = nullptr;
38
- this->area = MAX_AREA;
39
- this->isLive = true;
40
- }
41
30
 
42
- public:
43
- Point *pt;
44
- void setNext(Vertex *nextp) {
45
- this->next = nextp;
46
- }
47
- void setPrec(Vertex *prev) {
48
- this->prev = prev;
49
- }
50
- void updateArea() {
51
- if (this->prev == nullptr || this->next == nullptr)
52
- { this->area = MAX_AREA;
53
- return;
54
- }
55
- this->area = Triangle::area(this->prev->pt, this->pt, this->next->pt);
56
- }
57
- float getArea()
58
- { return(this->area);
59
- }
60
- Vertex *get_next() {
61
- return(this->next);
62
- }
63
- bool isLiving()
64
- { return(this->isLive);
31
+ class Vertex {
32
+ const float MAX_AREA = std::numeric_limits<float>::max();
33
+ explicit Vertex(Point* pt) {
34
+ this->pt = pt;
35
+ this->prev = nullptr;
36
+ this->next = nullptr;
37
+ this->area = MAX_AREA;
38
+ this->isLive = true;
39
+ }
40
+
41
+ public:
42
+ Point* pt;
43
+ void setNext(Vertex* nextp) { this->next = nextp; }
44
+ void setPrec(Vertex* prev) { this->prev = prev; }
45
+ void updateArea() {
46
+ if (!prev || !next)
47
+ { this->area = MAX_AREA;
48
+ return;
65
49
  }
66
- static Vertex *buildLine(std::list<Point*> *pts) {
67
- Vertex *first = nullptr;
68
- Vertex *prev = nullptr;
69
- int i = 0;
70
- for (std::list<Point*>::iterator it = pts->begin(); it != pts->end(); ++it, i++)
71
- { Vertex *v = new Vertex(*it);
72
- if (first == nullptr) first = v;
73
- v->setPrec(prev);
74
- if (prev != nullptr)
75
- { prev->setNext(v);
50
+ this->area = Triangle::area(this->prev->pt, this->pt, this->next->pt);
51
+ }
52
+ float getArea() { return this->area; }
53
+ Vertex* get_next() { return this->next; }
54
+ bool isLiving() { return this->isLive; }
55
+
56
+ static Vertex* buildLine(std::vector<Point*>& pts) {
57
+ Vertex* first = nullptr;
58
+ Vertex* prev = nullptr;
59
+ for (Point* p : pts) {
60
+ Vertex* v = new Vertex(p);
61
+ if (!first) first = v;
62
+ v->setPrec(prev);
63
+ if (prev) {
64
+ prev->setNext(v);
76
65
  prev->updateArea();
77
- }
78
- prev = v;
79
66
  }
80
- return(first);
67
+ prev = v;
81
68
  }
82
- Vertex* remove() {
83
- Vertex *tmpPrev = this->prev;
84
- Vertex *tmpNext = this->next;
85
- Vertex *result = nullptr;
86
- if (this->prev != nullptr)
87
- { this->prev->setNext(tmpNext);
88
- this->prev->updateArea();
89
- result = prev;
90
- }
91
- if (this->next != nullptr)
92
- { this->next->setPrec(tmpPrev);
93
- this->next->updateArea();
94
- if (result == nullptr) result = this->next;
95
- }
96
- this->isLive = false;
97
- return(result);
69
+ return first;
70
+ }
71
+
72
+ Vertex* remove() {
73
+ Vertex* tmpPrev = this->prev;
74
+ Vertex* tmpNext = this->next;
75
+ Vertex* result = nullptr;
76
+ if (tmpPrev) {
77
+ tmpPrev->setNext(tmpNext);
78
+ tmpPrev->updateArea();
79
+ result = tmpPrev;
98
80
  }
99
- std::list<Point*> *getCoordinates() {
100
- std::list<Point*> *coords = new std::list<Point*>();
101
- Vertex *curr = this;
102
- for (;;)
103
- { coords->push_back(curr->pt);
104
- curr = curr->get_next();
105
- if (curr == nullptr) break;
106
- }
107
- return(coords);
81
+ if (tmpNext) {
82
+ tmpNext->setPrec(tmpPrev);
83
+ tmpNext->updateArea();
84
+ if (!result) result = tmpNext;
108
85
  }
86
+ this->isLive = false;
87
+ return result;
88
+ }
89
+
90
+ static std::vector<Point*> getCoordinates(Vertex* head) {
91
+ std::vector<Point*> coords;
92
+ Vertex* curr = head;
93
+ while (curr) {
94
+ coords.push_back(curr->pt);
95
+ curr = curr->get_next();
96
+ }
97
+ return coords;
98
+ }
109
99
 
110
- private:
111
- Vertex *next, *prev;
112
- double area = MAX_AREA;
113
- bool isLive = true;
114
- };
100
+ private:
101
+ Vertex* next;
102
+ Vertex* prev;
103
+ float area;
104
+ bool isLive;
105
+ };
115
106
 
116
107
  private:
117
- float simplifyVertex(VisvalingamReducer::Vertex *vwLine);
108
+ float simplifyVertex(Vertex* vwLine);
118
109
  float tolerance;
119
110
  };
120
-
121
- #endif /* POLYGON_REDUCERS_VISVALINGAMREDUCER_H_ */