/src/gdal/alg/marching_squares/point.h
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: Marching square algorithm |
4 | | * Purpose: Core algorithm implementation for contour line generation. |
5 | | * Author: Oslandia <infos at oslandia dot com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2018, Oslandia <infos at oslandia dot com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #ifndef MARCHING_SQUARE_POINT_H |
14 | | #define MARCHING_SQUARE_POINT_H |
15 | | |
16 | | #include "utility.h" |
17 | | #include <ostream> |
18 | | #include <algorithm> |
19 | | #include <cmath> |
20 | | #include <list> |
21 | | |
22 | | namespace marching_squares |
23 | | { |
24 | | |
25 | | // regular point |
26 | | struct Point |
27 | | { |
28 | 0 | Point() : x(NaN), y(NaN) |
29 | 0 | { |
30 | 0 | } // just to be able to make an uninitialized list |
31 | | |
32 | 0 | Point(double x_, double y_) : x(x_), y(y_) |
33 | 0 | { |
34 | 0 | } |
35 | | |
36 | | double x; |
37 | | double y; |
38 | | }; |
39 | | |
40 | | inline bool operator==(const Point &lhs, const Point &rhs) |
41 | 0 | { |
42 | 0 | return (lhs.x == rhs.x) && (lhs.y == rhs.y); |
43 | 0 | } |
44 | | |
45 | | inline std::ostream &operator<<(std::ostream &o, const Point &p) |
46 | 0 | { |
47 | 0 | o << p.x << " " << p.y; |
48 | 0 | return o; |
49 | 0 | } |
50 | | |
51 | | // Test if a point is to the left or right of an infinite line. |
52 | | // Returns true if it is to the left and right otherwise |
53 | | // 0 if p2 is on the line and less than if p2 is to the right of the line |
54 | | inline bool isLeft(const Point &p0, const Point &p1, const Point &p2) |
55 | 0 | { |
56 | 0 | return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y)) > 0; |
57 | 0 | } |
58 | | |
59 | | // LineString type |
60 | | typedef std::list<Point> LineString; |
61 | | |
62 | | inline std::ostream &operator<<(std::ostream &o, const LineString &ls) |
63 | 0 | { |
64 | 0 | o << "{"; |
65 | 0 | for (const auto &p : ls) |
66 | 0 | { |
67 | 0 | o << p << ", "; |
68 | 0 | } |
69 | 0 | o << "}"; |
70 | 0 | return o; |
71 | 0 | } |
72 | | |
73 | | // Point with a value |
74 | | struct ValuedPoint |
75 | | { |
76 | | ValuedPoint(double x_, double y_, double value_) |
77 | 0 | : x(x_), y(y_), value(value_) |
78 | 0 | { |
79 | 0 | } |
80 | | |
81 | | const double x; |
82 | | const double y; |
83 | | const double value; |
84 | | }; |
85 | | |
86 | | } // namespace marching_squares |
87 | | |
88 | | #endif |