pdan 0.1.5__tar.gz

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.
pdan-0.1.5/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Georgy Skorobogatov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
pdan-0.1.5/PKG-INFO ADDED
@@ -0,0 +1,147 @@
1
+ Metadata-Version: 2.1
2
+ Name: pdan
3
+ Version: 0.1.5
4
+ Summary:
5
+ Author: GeorgySk
6
+ Author-email: skorobogatov@phystech.edu
7
+ Requires-Python: >=3.8,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.8
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Requires-Dist: gon (>=5.0.0,<6.0.0)
14
+ Requires-Dist: matplotlib (>=3.6.2,<4.0.0)
15
+ Description-Content-Type: text/markdown
16
+
17
+ pdan
18
+ ===========
19
+
20
+
21
+ [![](https://travis-ci.org/LostFan123/pdan.svg?branch=master)](https://travis-ci.org/LostFan123/pdan "Travis CI")
22
+ [![](https://dev.azure.com/skorobogatov/pdan/_apis/build/status/LostFan123.pdan?branchName=master)](https://dev.azure.com/skorobogatov/pdan/_build/latest?definitionId=2&branchName=master "Azure Pipelines")
23
+ [![](https://codecov.io/gh/LostFan123/pdan/branch/master/graph/badge.svg)](https://codecov.io/gh/LostFan123/pdan "Codecov")
24
+ [![](https://img.shields.io/github/license/LostFan123/pdan.svg)](https://github.com/LostFan123/pdan/blob/master/LICENSE "License")
25
+ [![](https://badge.fury.io/py/pdan.svg)](https://badge.fury.io/py/pdan "PyPI")
26
+
27
+ In what follows
28
+ - `python` is an alias for `python3.8` or any later
29
+ version (`python3.9` and so on).
30
+
31
+ Installation
32
+ ------------
33
+
34
+ Install the latest `pip` & `setuptools` packages versions:
35
+ ```bash
36
+ python -m pip install --upgrade pip setuptools
37
+ ```
38
+
39
+ ### User
40
+
41
+ Download and install the latest stable version from `PyPI` repository:
42
+ ```bash
43
+ python -m pip install --upgrade pdan
44
+ ```
45
+
46
+ ### Developer
47
+
48
+ Download the latest version from `GitHub` repository
49
+ ```bash
50
+ git clone https://github.com/LostFan123/pdan.git
51
+ cd pdan
52
+ ```
53
+
54
+ Install dependencies:
55
+ ```bash
56
+ poetry install
57
+ ```
58
+
59
+ Usage
60
+ -----------
61
+ ```python
62
+ >>> from pdan import minimizing_split, Contour, Point, Polygon
63
+ >>> contour = Contour([Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)])
64
+ >>> part, other = minimizing_split(contour, 0.5, key=lambda x, y: x.length)
65
+ >>> assert Polygon(part).area == Polygon(other).area == 0.5
66
+ ```
67
+
68
+ Development
69
+ -----------
70
+
71
+ ### Bumping version
72
+
73
+ #### Preparation
74
+
75
+ Install
76
+ [bump2version](https://github.com/c4urself/bump2version#installation).
77
+
78
+ #### Pre-release
79
+
80
+ Choose which version number category to bump following [semver
81
+ specification](http://semver.org/).
82
+
83
+ Test bumping version
84
+ ```bash
85
+ bump2version --dry-run --verbose $CATEGORY
86
+ ```
87
+
88
+ where `$CATEGORY` is the target version number category name, possible
89
+ values are `patch`/`minor`/`major`.
90
+
91
+ Bump version
92
+ ```bash
93
+ bump2version --verbose $CATEGORY
94
+ ```
95
+
96
+ This will set version to `major.minor.patch-alpha`.
97
+
98
+ #### Release
99
+
100
+ Test bumping version
101
+ ```bash
102
+ bump2version --dry-run --verbose release
103
+ ```
104
+
105
+ Bump version
106
+ ```bash
107
+ bump2version --verbose release
108
+ ```
109
+
110
+ This will set version to `major.minor.patch`.
111
+
112
+ #### Notes
113
+
114
+ To avoid inconsistency between branches and pull requests,
115
+ bumping version should be merged into `master` branch
116
+ as separate pull request.
117
+
118
+ ### Running tests
119
+
120
+ Plain:
121
+ ```bash
122
+ pytest
123
+ ```
124
+
125
+ Inside `Docker` container:
126
+ ```bash
127
+ docker-compose --file docker-compose.cpython.yml up
128
+ ```
129
+
130
+ `Bash` script (e.g. can be used in `Git` hooks):
131
+ ```bash
132
+ ./run-tests.sh
133
+ ```
134
+ or
135
+ ```bash
136
+ ./run-tests.sh cpython
137
+ ```
138
+
139
+ `PowerShell` script (e.g. can be used in `Git` hooks):
140
+ ```powershell
141
+ .\run-tests.ps1
142
+ ```
143
+ or
144
+ ```powershell
145
+ .\run-tests.ps1 cpython
146
+ ```
147
+
pdan-0.1.5/README.md ADDED
@@ -0,0 +1,130 @@
1
+ pdan
2
+ ===========
3
+
4
+
5
+ [![](https://travis-ci.org/LostFan123/pdan.svg?branch=master)](https://travis-ci.org/LostFan123/pdan "Travis CI")
6
+ [![](https://dev.azure.com/skorobogatov/pdan/_apis/build/status/LostFan123.pdan?branchName=master)](https://dev.azure.com/skorobogatov/pdan/_build/latest?definitionId=2&branchName=master "Azure Pipelines")
7
+ [![](https://codecov.io/gh/LostFan123/pdan/branch/master/graph/badge.svg)](https://codecov.io/gh/LostFan123/pdan "Codecov")
8
+ [![](https://img.shields.io/github/license/LostFan123/pdan.svg)](https://github.com/LostFan123/pdan/blob/master/LICENSE "License")
9
+ [![](https://badge.fury.io/py/pdan.svg)](https://badge.fury.io/py/pdan "PyPI")
10
+
11
+ In what follows
12
+ - `python` is an alias for `python3.8` or any later
13
+ version (`python3.9` and so on).
14
+
15
+ Installation
16
+ ------------
17
+
18
+ Install the latest `pip` & `setuptools` packages versions:
19
+ ```bash
20
+ python -m pip install --upgrade pip setuptools
21
+ ```
22
+
23
+ ### User
24
+
25
+ Download and install the latest stable version from `PyPI` repository:
26
+ ```bash
27
+ python -m pip install --upgrade pdan
28
+ ```
29
+
30
+ ### Developer
31
+
32
+ Download the latest version from `GitHub` repository
33
+ ```bash
34
+ git clone https://github.com/LostFan123/pdan.git
35
+ cd pdan
36
+ ```
37
+
38
+ Install dependencies:
39
+ ```bash
40
+ poetry install
41
+ ```
42
+
43
+ Usage
44
+ -----------
45
+ ```python
46
+ >>> from pdan import minimizing_split, Contour, Point, Polygon
47
+ >>> contour = Contour([Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)])
48
+ >>> part, other = minimizing_split(contour, 0.5, key=lambda x, y: x.length)
49
+ >>> assert Polygon(part).area == Polygon(other).area == 0.5
50
+ ```
51
+
52
+ Development
53
+ -----------
54
+
55
+ ### Bumping version
56
+
57
+ #### Preparation
58
+
59
+ Install
60
+ [bump2version](https://github.com/c4urself/bump2version#installation).
61
+
62
+ #### Pre-release
63
+
64
+ Choose which version number category to bump following [semver
65
+ specification](http://semver.org/).
66
+
67
+ Test bumping version
68
+ ```bash
69
+ bump2version --dry-run --verbose $CATEGORY
70
+ ```
71
+
72
+ where `$CATEGORY` is the target version number category name, possible
73
+ values are `patch`/`minor`/`major`.
74
+
75
+ Bump version
76
+ ```bash
77
+ bump2version --verbose $CATEGORY
78
+ ```
79
+
80
+ This will set version to `major.minor.patch-alpha`.
81
+
82
+ #### Release
83
+
84
+ Test bumping version
85
+ ```bash
86
+ bump2version --dry-run --verbose release
87
+ ```
88
+
89
+ Bump version
90
+ ```bash
91
+ bump2version --verbose release
92
+ ```
93
+
94
+ This will set version to `major.minor.patch`.
95
+
96
+ #### Notes
97
+
98
+ To avoid inconsistency between branches and pull requests,
99
+ bumping version should be merged into `master` branch
100
+ as separate pull request.
101
+
102
+ ### Running tests
103
+
104
+ Plain:
105
+ ```bash
106
+ pytest
107
+ ```
108
+
109
+ Inside `Docker` container:
110
+ ```bash
111
+ docker-compose --file docker-compose.cpython.yml up
112
+ ```
113
+
114
+ `Bash` script (e.g. can be used in `Git` hooks):
115
+ ```bash
116
+ ./run-tests.sh
117
+ ```
118
+ or
119
+ ```bash
120
+ ./run-tests.sh cpython
121
+ ```
122
+
123
+ `PowerShell` script (e.g. can be used in `Git` hooks):
124
+ ```powershell
125
+ .\run-tests.ps1
126
+ ```
127
+ or
128
+ ```powershell
129
+ .\run-tests.ps1 cpython
130
+ ```
@@ -0,0 +1,8 @@
1
+ """Polygon decomposition"""
2
+
3
+ __version__ = '0.1.5'
4
+
5
+ from .pdan import (minimizing_split,
6
+ Contour,
7
+ Point,
8
+ Polygon)
@@ -0,0 +1,501 @@
1
+ from collections import deque
2
+ from dataclasses import dataclass
3
+ from decimal import Decimal
4
+ from fractions import Fraction
5
+ from typing import (Callable,
6
+ Iterator,
7
+ List,
8
+ Sequence,
9
+ Tuple)
10
+
11
+ from gon.base import (Contour,
12
+ EMPTY,
13
+ Orientation,
14
+ Point,
15
+ Polygon,
16
+ Segment)
17
+ from gon.hints import Maybe
18
+
19
+
20
+ @dataclass
21
+ class Partition:
22
+ domain: Segment
23
+ countersegment: Segment
24
+ right_vertices: List[Point]
25
+ left_vertices: List[Point]
26
+ area_difference: Fraction
27
+
28
+
29
+ def minimizing_split(contour: Contour,
30
+ area_requirement: Fraction,
31
+ *,
32
+ key: Callable[[Contour, Contour], Fraction]
33
+ ) -> Tuple[Contour, Maybe[Contour]]:
34
+ """
35
+ Splits convex polygon defined by the input contour,
36
+ finds the part with the given area requirement that yields
37
+ the minimum possible value when applied to `key` function,
38
+ and returns it along with the remaining part as contours.
39
+ :param contour: defines a convex polygon
40
+ :param area_requirement: area of a part to return
41
+ :param key: function to be applied to a pair of contours
42
+ :return: two contours if the area requirement is not zero
43
+ or equal to area of the polygon; one contour and empty geometry
44
+ otherwise.
45
+ """
46
+ if area_requirement == Polygon(contour).area:
47
+ return contour, EMPTY
48
+ partitions = to_partitions(contour=contour,
49
+ area_requirement=area_requirement)
50
+ min_key_value = float('inf')
51
+ min_key_contours = None
52
+ for partition in partitions:
53
+ splitter = to_splitter(domain=partition.domain,
54
+ countersegment=partition.countersegment,
55
+ area_requirement=partition.area_difference)
56
+ right_contour, left_contour = to_right_left_contours(
57
+ domain=partition.domain,
58
+ countersegment=partition.countersegment,
59
+ splitter=splitter,
60
+ right_vertices=partition.right_vertices,
61
+ left_vertices=partition.left_vertices)
62
+ key_value = key(right_contour, left_contour)
63
+ if key_value < min_key_value:
64
+ min_key_contours = right_contour, left_contour
65
+ min_key_value = key_value
66
+ if min_key_contours is None:
67
+ raise ValueError("Couldn't find the contour with min perimeter")
68
+ return min_key_contours
69
+
70
+
71
+ def to_partitions(contour: Contour,
72
+ area_requirement: Fraction) -> Iterator[Partition]:
73
+ """
74
+ Returns an iterator over `Partition` objects, containing
75
+ information about domain segments, their countersegments,
76
+ "right" and "left" parts' vertices,
77
+ where "right" vertices are all the original vertices
78
+ located between the domain and countersegment when iterating
79
+ in counterclockwise order, and left vertices are
80
+ all the original vertices located between the countersegment and
81
+ domain segment when iterating in counterclockwise order,
82
+ and, finally, the area differences that should be found in the
83
+ quadrilateral based on the domains and the countersegments.
84
+ """
85
+ segments = contour.segments
86
+ # in gon, the last segment goes first, the first one goes second, and so on
87
+ tails = iter([*segments[1:], segments[0]])
88
+ heads = iter([*segments[2:], *segments])
89
+ tail = next(tails)
90
+ accumulated_area = 0
91
+ right_vertices = deque([tail.start])
92
+ left_vertices = deque(contour.vertices[1:])
93
+ for head in heads:
94
+ right_vertices.append(head.start)
95
+ left_vertices.popleft()
96
+ covered_triangle_area = Polygon(Contour([tail.start,
97
+ head.start,
98
+ head.end])).area
99
+ accumulated_area += covered_triangle_area
100
+ if accumulated_area < area_requirement:
101
+ continue
102
+ elif accumulated_area == area_requirement:
103
+ right_vertices.append(head.end) # remove
104
+ left_vertices.popleft()
105
+ head = next(heads)
106
+ countervertex = head.start
107
+ is_head_based_on_original_vertex = True
108
+ else:
109
+ area = covered_triangle_area + area_requirement - accumulated_area
110
+ countervertex = inverse_shoelace(area=area,
111
+ endpoint=tail.start,
112
+ base_start=head.start,
113
+ base_end=head.end)
114
+ right_vertices.append(countervertex)
115
+ is_head_based_on_original_vertex = False
116
+ break
117
+ else:
118
+ raise ValueError("Couldn't find countervertex.")
119
+ is_tail_start_original = True
120
+ while True:
121
+ tail_based_triangle_area = Polygon(Contour([tail.start,
122
+ tail.end,
123
+ countervertex])).area
124
+ head_based_triangle_area = Polygon(Contour([tail.end,
125
+ countervertex,
126
+ head.end])).area
127
+ right_vertices.popleft()
128
+ left_vertices.append(tail.start)
129
+ if tail_based_triangle_area == head_based_triangle_area:
130
+ domain = tail
131
+ domain_based_area = tail_based_triangle_area
132
+ image_end = head.end
133
+ tail = next(tails, None)
134
+ head = next(heads)
135
+ elif head_based_triangle_area > tail_based_triangle_area:
136
+ domain = tail
137
+ domain_based_area = tail_based_triangle_area
138
+ image_end = inverse_shoelace(area=tail_based_triangle_area,
139
+ endpoint=tail.end,
140
+ base_start=countervertex,
141
+ base_end=head.end)
142
+ left_vertices.appendleft(image_end)
143
+ tail = next(tails, None)
144
+ head = Segment(image_end, head.end)
145
+ else:
146
+ area_difference = (tail_based_triangle_area
147
+ - head_based_triangle_area)
148
+ domain_end = inverse_shoelace(area=area_difference,
149
+ endpoint=head.end,
150
+ base_start=tail.end,
151
+ base_end=tail.start)
152
+ right_vertices.appendleft(domain_end)
153
+ domain = Segment(tail.start, domain_end)
154
+ domain_based_area = Polygon(Contour([domain.start,
155
+ domain.end,
156
+ countervertex])).area
157
+ image_end = head.end
158
+ tail = Segment(domain_end, tail.end)
159
+ head = next(heads)
160
+ countersegment = Segment(countervertex, image_end)
161
+ resulting_right_vertices = (list(right_vertices)
162
+ if len(right_vertices) > 2 else [])
163
+ resulting_left_vertices = (list(left_vertices)
164
+ if len(left_vertices) > 2 else [])
165
+ yield Partition(domain=domain,
166
+ countersegment=countersegment,
167
+ right_vertices=resulting_right_vertices,
168
+ left_vertices=resulting_left_vertices,
169
+ area_difference=domain_based_area)
170
+ if tail is None:
171
+ return
172
+ countervertex = head.start
173
+ if not is_head_based_on_original_vertex:
174
+ right_vertices.pop()
175
+ if not is_tail_start_original:
176
+ left_vertices.pop()
177
+ right_vertices.append(countervertex)
178
+ left_vertices.popleft()
179
+ is_head_based_on_original_vertex = (head_based_triangle_area
180
+ <= tail_based_triangle_area)
181
+ is_tail_start_original = (head_based_triangle_area
182
+ >= tail_based_triangle_area)
183
+
184
+
185
+ def to_splitter(domain: Segment,
186
+ countersegment: Segment,
187
+ area_requirement: Fraction) -> Segment:
188
+ """
189
+ For the given "domain" and its corresponding "countersegment",
190
+ finds another "splitter" segment connecting these two
191
+ so that the area on the right would be
192
+ equal to the given area requirement
193
+ """
194
+ if (domain.start.x == domain.end.x
195
+ and countersegment.end.x == countersegment.start.x):
196
+ return vertical_segments_splitter(area_requirement=area_requirement,
197
+ domain=domain,
198
+ countersegment=countersegment)
199
+ if domain.start.x == domain.end.x:
200
+ return vertical_domain_splitter(area_requirement=area_requirement,
201
+ domain=domain,
202
+ countersegment=countersegment)
203
+ if countersegment.start.x == countersegment.end.x:
204
+ return vertical_countersegment_splitter(
205
+ area_requirement=area_requirement,
206
+ domain=domain,
207
+ countersegment=countersegment)
208
+ domain_slope, domain_intercept = slope_intercept(domain.start,
209
+ domain.end)
210
+ countersegment_slope, countersegment_intercept = slope_intercept(
211
+ countersegment.start,
212
+ countersegment.end)
213
+ intercept_diff = domain_intercept - countersegment_intercept
214
+ if countersegment_slope == domain_slope:
215
+ return parallel_inclined_segments_splitter(
216
+ area_requirement=area_requirement,
217
+ intercept_diff=intercept_diff,
218
+ domain=domain,
219
+ domain_intercept=domain_intercept,
220
+ countersegment=countersegment,
221
+ countersegment_intercept=countersegment_intercept,
222
+ slope=domain_slope)
223
+ return general_case_splitter(
224
+ area_requirement=area_requirement,
225
+ intercept_diff=intercept_diff,
226
+ domain=domain,
227
+ domain_intercept=domain_intercept,
228
+ domain_slope=domain_slope,
229
+ countersegment=countersegment,
230
+ countersegment_intercept=countersegment_intercept,
231
+ image_slope=countersegment_slope)
232
+
233
+
234
+ def to_right_left_contours(domain: Segment,
235
+ countersegment: Segment,
236
+ splitter: Segment,
237
+ right_vertices: List[Point],
238
+ left_vertices: List[Point]) -> Tuple[Contour,
239
+ Contour]:
240
+ """
241
+ Constructs two contours from splitter
242
+ and vertices delimiting right and left parts
243
+ based on the splitter's endpoints location
244
+ """
245
+ if not right_vertices:
246
+ if splitter.start == domain.start:
247
+ right_contour = Contour([domain.start, domain.end,
248
+ countersegment.start])
249
+ elif splitter.start == domain.end:
250
+ right_contour = Contour([domain.end, countersegment.start,
251
+ countersegment.end])
252
+ else:
253
+ right_contour = Contour([splitter.start, domain.end,
254
+ countersegment.start, splitter.end])
255
+ else:
256
+ if domain.end in Segment(splitter.start, right_vertices[1]):
257
+ try:
258
+ right_vertices = right_vertices[1:]
259
+ except TypeError:
260
+ print("right_vertices == ", right_vertices)
261
+ raise TypeError
262
+ if countersegment.start in Segment(right_vertices[-2], splitter.end):
263
+ right_vertices = right_vertices[:-1]
264
+ right_contour = Contour([splitter.start,
265
+ *right_vertices,
266
+ splitter.end])
267
+ if not left_vertices:
268
+ if splitter.start == domain.start:
269
+ left_contour = Contour([countersegment.start, countersegment.end,
270
+ domain.start])
271
+ elif splitter.start == domain.end:
272
+ left_contour = Contour([countersegment.end, domain.start,
273
+ domain.end])
274
+ else:
275
+ left_contour = Contour([splitter.end, countersegment.end,
276
+ domain.start, splitter.start])
277
+ else:
278
+ if domain.start in Segment(left_vertices[-2], splitter.start):
279
+ left_vertices = left_vertices[:-1]
280
+ if countersegment.end in Segment(splitter.end, left_vertices[1]):
281
+ left_vertices = left_vertices[1:]
282
+ left_contour = Contour([splitter.end, *left_vertices, splitter.start])
283
+ return right_contour, left_contour
284
+
285
+
286
+ def vertical_segments_splitter(area_requirement: Fraction,
287
+ domain: Segment,
288
+ countersegment: Segment) -> Segment:
289
+ """
290
+ Finds segment connecting two vertical "domain"
291
+ and "countersegment" segments
292
+ so that the area on the right would be
293
+ equal to the area requirement
294
+ """
295
+ tail_y = ((2 * area_requirement
296
+ + countersegment.start.x * countersegment.start.y
297
+ + countersegment.start.x * domain.end.y
298
+ - domain.start.x * domain.end.y
299
+ - domain.start.x * countersegment.start.y)
300
+ / (2 * (countersegment.start.x - domain.start.x)))
301
+ if is_before_domain(tail_y, domain.start.y, domain.end.y):
302
+ return Segment(domain.start, countersegment.start)
303
+ if is_after_domain(tail_y, domain.start.y, domain.end.y):
304
+ return Segment(domain.end, countersegment.end)
305
+ tail_point = Point(domain.end.x, tail_y)
306
+ head_point = Point(countersegment.start.x, tail_y)
307
+ return Segment(tail_point, head_point)
308
+
309
+
310
+ def vertical_domain_splitter(area_requirement: Fraction,
311
+ domain: Segment,
312
+ countersegment: Segment) -> Segment:
313
+ """
314
+ Finds segment connecting a vertical "domain"
315
+ and an inclined "countersegment" segments
316
+ so that the area on the right would be
317
+ equal to the area requirement
318
+ """
319
+ slope, intercept = slope_intercept(countersegment.start,
320
+ countersegment.end)
321
+ domain_image_intersection_y = slope * domain.end.x + intercept
322
+ const = (2 * area_requirement
323
+ - domain_image_intersection_y * countersegment.start.x
324
+ + domain.end.y * (countersegment.start.x - domain.end.x))
325
+ fourth_power_tail_intersection_diff_y = (
326
+ (domain_image_intersection_y * domain.end.x + const) ** 2
327
+ * (1 + slope ** 2))
328
+ sign = 1 if domain_image_intersection_y < domain.end.y else -1
329
+ tail_intersection_diff_y = robust_sqrt(robust_sqrt(
330
+ fourth_power_tail_intersection_diff_y))
331
+ tail_y = (Fraction(sign * tail_intersection_diff_y)
332
+ + domain_image_intersection_y)
333
+ if is_before_domain(tail_y, domain.start.y, domain.end.y):
334
+ return Segment(domain.start, countersegment.start)
335
+ if is_after_domain(tail_y, domain.start.y, domain.end.y):
336
+ return Segment(domain.end, countersegment.end)
337
+ head_x = ((const + domain.end.x * tail_y)
338
+ / (tail_y - domain_image_intersection_y))
339
+ head_y = slope * head_x + intercept
340
+ tail_point = Point(domain.end.x, tail_y)
341
+ head_point = Point(head_x, head_y)
342
+ return Segment(tail_point, head_point)
343
+
344
+
345
+ def vertical_countersegment_splitter(area_requirement: Fraction,
346
+ domain: Segment,
347
+ countersegment: Segment) -> Segment:
348
+ """
349
+ Finds segment connecting an inclined "domain"
350
+ and a vertical "countersegment" segments
351
+ so that the area on the right would be
352
+ equal to the area requirement
353
+ """
354
+ slope, intercept = slope_intercept(domain.start, domain.end)
355
+ const = (2 * area_requirement
356
+ + slope * domain.end.x * countersegment.start.x
357
+ + intercept * domain.end.x - countersegment.start.y * domain.end.x
358
+ + countersegment.start.x * countersegment.start.y)
359
+ domain_image_intersection_y = slope * countersegment.start.x + intercept
360
+ fourth_power_tail_intersection_diff_x = (
361
+ (const - domain_image_intersection_y * countersegment.start.x) ** 2
362
+ / (1 + slope ** 2))
363
+ sign = 1 if domain.start.x > countersegment.end.x else -1
364
+ tail_intersection_diff_x = robust_sqrt(robust_sqrt(
365
+ fourth_power_tail_intersection_diff_x))
366
+ tail_x = Fraction(countersegment.start.x + sign * tail_intersection_diff_x)
367
+ if is_before_domain(tail_x, domain.start.x, domain.end.x):
368
+ return Segment(domain.start, countersegment.start)
369
+ if is_after_domain(tail_x, domain.start.x, domain.end.x):
370
+ return Segment(domain.end, countersegment.end)
371
+ tail_y = tail_x * slope + intercept
372
+ head_y = ((const - tail_x * domain_image_intersection_y)
373
+ / (countersegment.start.x - tail_x))
374
+ tail_point = Point(tail_x, tail_y)
375
+ head_point = Point(countersegment.start.x, head_y)
376
+ return Segment(tail_point, head_point)
377
+
378
+
379
+ def parallel_inclined_segments_splitter(area_requirement: Fraction,
380
+ intercept_diff: Fraction,
381
+ domain: Segment,
382
+ domain_intercept: Fraction,
383
+ countersegment: Segment,
384
+ countersegment_intercept: Fraction,
385
+ slope: Fraction) -> Segment:
386
+ """
387
+ Finds segment connecting parallel and non-vertical "domain"
388
+ and "countersegment" segments so that the area on the right
389
+ would be equal to the area requirement
390
+ """
391
+ tail_x = (area_requirement / intercept_diff
392
+ + (domain.end.x + countersegment.start.x) / 2
393
+ - slope * intercept_diff / (2 * (1 + slope ** 2)))
394
+ if is_before_domain(tail_x, domain.start.x, domain.end.x):
395
+ return Segment(domain.start, countersegment.start)
396
+ if is_after_domain(tail_x, domain.start.x, domain.end.x):
397
+ return Segment(domain.end, countersegment.end)
398
+ tail_y = slope * tail_x + domain_intercept
399
+ head_x = (2 * area_requirement / intercept_diff + domain.end.x
400
+ + countersegment.start.x - tail_x)
401
+ head_y = slope * head_x + countersegment_intercept
402
+ tail_point = Point(tail_x, tail_y)
403
+ head_point = Point(head_x, head_y)
404
+ return Segment(tail_point, head_point)
405
+
406
+
407
+ def general_case_splitter(area_requirement: Fraction,
408
+ intercept_diff: Fraction,
409
+ domain: Segment,
410
+ domain_intercept: Fraction,
411
+ domain_slope: Fraction,
412
+ countersegment: Segment,
413
+ countersegment_intercept: Fraction,
414
+ image_slope: Fraction) -> Segment:
415
+ """
416
+ Finds segment connecting non-parallel and non-vertical "domain"
417
+ and "countersegment" segments so that the area on the right
418
+ would be equal to the area requirement
419
+ """
420
+ slope_diff = domain_slope - image_slope
421
+ const = (2 * area_requirement
422
+ + slope_diff * domain.end.x * countersegment.start.x
423
+ + intercept_diff * (domain.end.x + countersegment.start.x))
424
+ sign = (1
425
+ if domain.start.y > image_slope * domain.start.x
426
+ + countersegment_intercept
427
+ else -1)
428
+ tail_x = (sign
429
+ * robust_sqrt((robust_sqrt((1 + image_slope ** 2)
430
+ / (1 + domain_slope ** 2))
431
+ * abs(intercept_diff ** 2 + slope_diff * const)))
432
+ - intercept_diff) / slope_diff
433
+ if is_before_domain(tail_x, domain.start.x, domain.end.x):
434
+ return Segment(domain.start, countersegment.start)
435
+ if is_after_domain(tail_x, domain.start.x, domain.end.x):
436
+ return Segment(domain.end, countersegment.end)
437
+ tail_y = tail_x * domain_slope + domain_intercept
438
+ head_x = ((const - tail_x * intercept_diff)
439
+ / (tail_x * slope_diff + intercept_diff))
440
+ head_y = head_x * image_slope + countersegment_intercept
441
+ tail_point = Point(tail_x, tail_y)
442
+ head_point = Point(head_x, head_y)
443
+ return Segment(tail_point, head_point)
444
+
445
+
446
+ def is_before_domain(value: Fraction,
447
+ start: Fraction,
448
+ end: Fraction) -> bool:
449
+ """
450
+ Checks if X or Y coordinate of the segment's tail
451
+ was found before the starting vertex of the "domain"
452
+ """
453
+ return value <= start < end or value >= start > end
454
+
455
+
456
+ def is_after_domain(value: Fraction,
457
+ start: Fraction,
458
+ end: Fraction) -> bool:
459
+ """
460
+ Checks if X or Y coordinate of the segment's tail
461
+ was found after the second vertex of the "domain"
462
+ """
463
+ return value >= end > start or value <= end < start
464
+
465
+
466
+ def robust_sqrt(fraction: Fraction) -> Fraction:
467
+ return Fraction((fraction.numerator
468
+ / Decimal(fraction.denominator)).sqrt())
469
+
470
+
471
+ def inverse_shoelace(area: Fraction,
472
+ endpoint: Point,
473
+ base_start: Point,
474
+ base_end: Point) -> Point:
475
+ """
476
+ Finds point in triangle based on inverse shoelace algorithm.
477
+ Coordinates of the points must be of `Fraction` type.
478
+ """
479
+ orientation = Contour([endpoint, base_start, base_end]).orientation
480
+ sign = 1 if orientation is Orientation.COUNTERCLOCKWISE else -1
481
+ if base_end.x - base_start.x == 0:
482
+ numerator = 2 * area + sign * (base_start.x * base_start.y
483
+ - endpoint.x * base_start.y)
484
+ denominator = sign * (base_start.x - endpoint.x)
485
+ y = numerator / denominator
486
+ return Point(base_start.x, y)
487
+ slope, intercept = slope_intercept(base_start, base_end)
488
+ x_diff = base_start.x - endpoint.x
489
+ numerator = 2 * area + sign * (base_start.x * endpoint.y
490
+ - endpoint.x * base_start.y
491
+ - intercept * x_diff)
492
+ denominator = sign * (endpoint.y - base_start.y + slope * x_diff)
493
+ x = numerator / denominator
494
+ y = slope * x + intercept
495
+ return Point(x, y)
496
+
497
+
498
+ def slope_intercept(start: Point, end: Point) -> Tuple[Fraction, Fraction]:
499
+ slope = (end.y - start.y) / (end.x - start.x)
500
+ intercept = end.y - slope * end.x
501
+ return slope, intercept
@@ -0,0 +1,21 @@
1
+ [tool.poetry]
2
+ name = "pdan"
3
+ version = "0.1.5"
4
+ description = ""
5
+ authors = ["GeorgySk <skorobogatov@phystech.edu>"]
6
+ readme = "README.md"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = "^3.8"
10
+ gon = "^5.0.0"
11
+ matplotlib = "^3.6.2"
12
+
13
+ [tool.poetry.group.dev.dependencies]
14
+ pytest = "^7.2.0"
15
+ pytest-cov = "^4.0.0"
16
+ hypothesis = "^6.61.0"
17
+ hypothesis-geometry = "^7.3.0"
18
+
19
+ [build-system]
20
+ requires = ["poetry-core", "poetry>=1.0"]
21
+ build-backend = "poetry.core.masonry.api"
pdan-0.1.5/setup.py ADDED
@@ -0,0 +1,30 @@
1
+ # -*- coding: utf-8 -*-
2
+ from setuptools import setup
3
+
4
+ packages = \
5
+ ['pdan']
6
+
7
+ package_data = \
8
+ {'': ['*']}
9
+
10
+ install_requires = \
11
+ ['gon>=5.0.0,<6.0.0', 'matplotlib>=3.6.2,<4.0.0']
12
+
13
+ setup_kwargs = {
14
+ 'name': 'pdan',
15
+ 'version': '0.1.5',
16
+ 'description': '',
17
+ 'long_description': 'pdan\n===========\n\n\n[![](https://travis-ci.org/LostFan123/pdan.svg?branch=master)](https://travis-ci.org/LostFan123/pdan "Travis CI")\n[![](https://dev.azure.com/skorobogatov/pdan/_apis/build/status/LostFan123.pdan?branchName=master)](https://dev.azure.com/skorobogatov/pdan/_build/latest?definitionId=2&branchName=master "Azure Pipelines")\n[![](https://codecov.io/gh/LostFan123/pdan/branch/master/graph/badge.svg)](https://codecov.io/gh/LostFan123/pdan "Codecov")\n[![](https://img.shields.io/github/license/LostFan123/pdan.svg)](https://github.com/LostFan123/pdan/blob/master/LICENSE "License")\n[![](https://badge.fury.io/py/pdan.svg)](https://badge.fury.io/py/pdan "PyPI")\n\nIn what follows\n- `python` is an alias for `python3.8` or any later\nversion (`python3.9` and so on).\n\nInstallation\n------------\n\nInstall the latest `pip` & `setuptools` packages versions:\n ```bash\n python -m pip install --upgrade pip setuptools\n ```\n\n### User\n\nDownload and install the latest stable version from `PyPI` repository:\n ```bash\n python -m pip install --upgrade pdan\n ```\n\n### Developer\n\nDownload the latest version from `GitHub` repository\n```bash\ngit clone https://github.com/LostFan123/pdan.git\ncd pdan\n```\n\nInstall dependencies:\n ```bash\n poetry install\n ```\n\nUsage\n-----------\n```python\n>>> from pdan import minimizing_split, Contour, Point, Polygon\n>>> contour = Contour([Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)])\n>>> part, other = minimizing_split(contour, 0.5, key=lambda x, y: x.length)\n>>> assert Polygon(part).area == Polygon(other).area == 0.5\n```\n\nDevelopment\n-----------\n\n### Bumping version\n\n#### Preparation\n\nInstall\n[bump2version](https://github.com/c4urself/bump2version#installation).\n\n#### Pre-release\n\nChoose which version number category to bump following [semver\nspecification](http://semver.org/).\n\nTest bumping version\n```bash\nbump2version --dry-run --verbose $CATEGORY\n```\n\nwhere `$CATEGORY` is the target version number category name, possible\nvalues are `patch`/`minor`/`major`.\n\nBump version\n```bash\nbump2version --verbose $CATEGORY\n```\n\nThis will set version to `major.minor.patch-alpha`. \n\n#### Release\n\nTest bumping version\n```bash\nbump2version --dry-run --verbose release\n```\n\nBump version\n```bash\nbump2version --verbose release\n```\n\nThis will set version to `major.minor.patch`.\n\n#### Notes\n\nTo avoid inconsistency between branches and pull requests,\nbumping version should be merged into `master` branch \nas separate pull request.\n\n### Running tests\n\nPlain:\n ```bash\n pytest\n ```\n\nInside `Docker` container:\n ```bash\n docker-compose --file docker-compose.cpython.yml up\n ```\n\n`Bash` script (e.g. can be used in `Git` hooks):\n ```bash\n ./run-tests.sh\n ```\n or\n ```bash\n ./run-tests.sh cpython\n ```\n\n`PowerShell` script (e.g. can be used in `Git` hooks):\n ```powershell\n .\\run-tests.ps1\n ```\n or\n ```powershell\n .\\run-tests.ps1 cpython\n ```\n',
18
+ 'author': 'GeorgySk',
19
+ 'author_email': 'skorobogatov@phystech.edu',
20
+ 'maintainer': 'None',
21
+ 'maintainer_email': 'None',
22
+ 'url': 'None',
23
+ 'packages': packages,
24
+ 'package_data': package_data,
25
+ 'install_requires': install_requires,
26
+ 'python_requires': '>=3.8,<4.0',
27
+ }
28
+
29
+
30
+ setup(**setup_kwargs)