Skip to content
Navigation Menu
{{ message }}
forked from tobiasgrosser/islplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.py
More file actions
403 lines (326 loc) · 12.3 KB
/
Copy pathsupport.py
File metadata and controls
403 lines (326 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import islpy as _islpy
from islpy import *
def get_point_coordinates(point, scale=1):
result = []
for i in range(point.space.dim(_islpy.dim_type.set)):
result.append(int(point.get_coordinate_val(_islpy.dim_type.set, i)
.get_num_si())/ scale)
return result
def _vertex_to_rational_point(vertex):
"""
Given an n-dimensional vertex, this function returns an n-tuple consisting
of pairs of integers. Each pair represents the rational value of the
specific dimension with the first element of the pair being the nominator
and the second element being the denominator.
"""
expr = vertex.get_expr()
value = []
for i in range(expr.dim(dim_type.out)):
subexpr = expr.get_aff(i)
val = subexpr.get_constant_val()
value.append((val.get_num_si(), val.get_den_val().to_python()))
return value
def _vertex_get_coordinates(vertex, scale=1):
"""
Get the coordinates of the an isl vertex as a tuple of float values.
To extract the coordinates we first get the expression defining the vertex.
This expression is given as a rational set that specifies its (possibly
rational) coordinates. We then convert this set into the tuple we will
return.
Example:
For a vertex defined by the rational set
{ rat: S[i, j] : 2i = 7 and 2j = 9 } we produce the output (7/2, 9/2).
:param vertex: The vertex from which we extract the coordinates.
"""
r = _vertex_to_rational_point(vertex)
return [(1.0 * x[0] / x[1])/scale for x in r]
def _is_vertex_on_constraint(vertex, constraint):
"""
Given a vertex and a constraint, check if the vertex is on the plane defined
by the constraint. For inequality constraints, the plane we look at is the
extremal plane that separates the elements that fulfill an inequality
constraint from the elements that do not fulfill this constraints.
"""
r = _vertex_to_rational_point(vertex)
dims = constraint.space.dim(dim_type.set)
v = []
for d in range(dims):
v.append(constraint.get_coefficient_val(dim_type.set, d).get_num_si())
summ = 0
import numpy
for i in range(dims):
prod = 1
for j in range(dims):
if i == j:
prod *= r[j][0]
else:
prod *= r[j][1]
summ += v[i] * prod
constant = constraint.get_constant_val().get_num_si()
summ += numpy.product([x[1] for x in r]) * constant
return int(summ) == 0
def bset_get_vertex_coordinates(bset_data, scale=1):
"""
Given a basic set return the list of vertices at the corners.
:param bset_data: The basic set to get the vertices from
"""
# Get the vertices.
vertices = []
bset_data.compute_vertices().foreach_vertex(vertices.append)
f = lambda x: _vertex_get_coordinates(x, scale)
vertices = list(map(f, vertices))
if len(vertices) <= 1:
return vertices
# Sort the vertices in clockwise order.
#
# We select a 'center' point that lies within the convex hull of the
# vertices. We then sort all points according to the direction (given as an
# angle in radiens) they lie in respect to the center point.
from math import atan2 as _atan2
center = ((vertices[0][0] + vertices[1][0]) / 2.0,
(vertices[0][1] + vertices[1][1]) / 2.0)
f = lambda x: _atan2(x[0]-center[0], x[1]-center[1])
vertices = sorted(vertices, key=f)
return vertices
from math import sqrt
from math import degrees
from math import acos
def cross(a, b):
c = [a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]]
return c
def sub(A,B):
return [A[0]-B[0], A[1]-B[1], A[2]-B[2]]
def norm(A,B,C):
return(cross(sub(A,C),sub(B,C)))
def dotProduct(A,B):
return A[0] * B[0] + A[1] * B[1] + A[2] * B[2]
def magnitude(A):
return sqrt(A[0]*A[0] + A[1]*A[1] + A[2]*A[2])
def formular(A,B):
res = dotProduct(A,B) / (magnitude(A) * magnitude(B))
res = float(str(res))
# Due to rounding errors res may be smaller than one. We fix this here.
res = max(-1, res)
res = acos(res)
res = degrees(res)
return res
def angle(Q,M,O,N):
if Q == M:
return 0
OM = sub(M,O)
OQ = sub(Q,O)
sig = dotProduct(N,cross(OM, OQ))
if sig >= 0:
return formular(OQ,OM)
else:
return -formular(OQ,OM)
def get_vertices_for_constraint(vertices, constraint):
"""
Return the list of vertices within a hyperspace.
Given a constraint and a list of vertices, we filter the list of vertices
such that only the vertices that are on the plane defined by the constraint
are returned. We then sort the vertices such that the order defines a
convex shape.
"""
points = []
for v in vertices:
if _is_vertex_on_constraint(v, constraint):
points.append(_vertex_get_coordinates(v))
if len(points) == 0:
return None
points.sort()
import itertools
points = list(points for points,_ in itertools.groupby(points))
A = points[0]
if len(points) == 1:
return [A]
B = points[1]
if len(points) == 2:
return [A, B]
C = points[2]
N = norm(A,B,C)
center = [(A[0] + B[0]) / 2, (A[1] + B[1]) / 2, (A[2] + B[2]) / 2]
f = lambda a: angle(A, a, center, N)
points = sorted(points, key=f)
return points
def isSubset(parent, child):
if len(parent) <= len(child):
return False
for c in child:
contained = False
for p in parent:
if p == c:
contained = True
break
if not contained:
return False
return True
def bset_get_faces(basicSet):
"""
Get a list of faces from a basic set
Given a basic set we return a list of faces, where each face is represented
by a list of restricting vertices. The list of vertices is sorted in
clockwise (or counterclockwise) order around the center of the face.
Vertices may have rational coordinates. A vertice is represented as a three
tuple.
"""
faces = []
vertices = []
basicSet.compute_vertices().foreach_vertex(vertices.append)
f = lambda c: faces.append(get_vertices_for_constraint(vertices, c))
basicSet.foreach_constraint(f)
# Remove empty elements, duplicates and subset of elements
faces = filter(lambda x: not x == None, faces)
faces = list(faces)
faces = [x for x in faces if not
any(isSubset(y, x) for y in faces if x is not y)]
faces.sort()
import itertools
faces = list(faces for faces,_ in itertools.groupby(faces))
return faces
def set_get_faces(set_data):
"""
Get a list of faces from a set
Given a basic set we return a list of faces, where each face is represented
by a list of restricting vertices. The list of vertices is sorted in
clockwise (or counterclockwise) order around the center of the face.
Vertices may have rational coordinates. A vertice is represented as a three
tuple.
"""
bsets = []
f = lambda x: bsets.append(x)
set_data.foreach_basic_set(f)
return list(map(bset_get_faces, bsets))
def make_tuple(vertex):
return (vertex[0], vertex[1], vertex[2])
def get_vertex_to_index_map(vertexlist):
res = {}
i = 0
for v in vertexlist:
res[make_tuple(v)] = i
i += 1
return res
def translate_faces_to_indexes(faces, vertexmap):
"""
Given a list of faces, translate the vertex defining it from their explit
offsets to their index as provided by the vertexmap, a mapping from vertices
to vertex indices.
"""
new_faces = []
for face in faces:
new_face = []
for vertex in face:
new_face.append(vertexmap[make_tuple(vertex)])
new_faces.append(new_face)
return new_faces
def get_vertices_and_faces(set_data):
"""
Given an isl set, return a tuple that contains the vertices and faces of
this set. The vertices are sorted in lexicographic order. In the faces,
the vertices are referenced by their position within the vertex list. The
vertices of a face are sorted such that connecting subsequent vertices
yields a convex form.
"""
data = set_get_faces(set_data)
if len(data) == 0:
return ([], [])
faces = data[0]
vertices = [vertex for face in faces for vertex in face]
vertices.sort()
import itertools
vertices = list(vertices for vertices, _ in itertools.groupby(vertices))
vertexmap = get_vertex_to_index_map(vertices)
faces = translate_faces_to_indexes(faces, vertexmap)
return (vertices, faces)
def _constraint_make_equality_set(x):
e = Constraint.equality_alloc(x.get_local_space())
e = e.set_constant_val(x.get_constant_val().get_num_si())
for i in range(x.space.dim(dim_type.set)):
e = e.set_coefficient_val(dim_type.set, i,
x.get_coefficient_val(dim_type.set, i).get_num_si())
for i in range(x.space.dim(dim_type.param)):
e = e.set_coefficient_val(dim_type.param, i,
x.get_coefficient_val(dim_type.param, i).get_num_si())
return BasicSet.universe(x.space).add_constraint(e)
def bset_get_points(bset_data, only_hull=False, scale=1):
"""
Given a basic set return the points within this set
:param bset_data: The set that contains the points.
:param only_hull: Only return the point that are on the hull of the set.
:param scale: Scale the values.
"""
if only_hull:
hull = [None]
hull[0] = Set.empty(bset_data.space)
def add(c):
const_eq = _constraint_make_equality_set(c)
const_eq = const_eq.intersect(bset_data)
hull[0] = hull[0].union(const_eq)
bset_data.foreach_constraint(add)
bset_data = hull[0]
points = []
f = lambda x: points.append(get_point_coordinates(x, scale))
bset_data.foreach_point(f)
points = sorted(points)
return points
def get_rectangular_hull(set_data, offset=0):
uset_data = Set.universe(set_data.get_space())
for dim in range(0,2):
ls = LocalSpace.from_space(set_data.get_space())
c = Constraint.inequality_alloc(ls)
incr = Map("{{[i]->[i+{0}]}}".format(offset))
decr = Map("{{[i]->[i-{0}]}}".format(offset))
dim_val = Aff.zero_on_domain(ls).set_coefficient_val(dim_type.in_, dim,
1)
dim_val = PwAff.from_aff(dim_val)
dim_val = Map.from_pw_aff(dim_val)
space = dim_val.get_space()
dim_cst = Map.universe(space)
max_set = Set.from_pw_aff(set_data.dim_max(dim))
dim_cst = dim_cst.intersect_range(max_set)
dim_cst = dim_cst.apply_range(incr)
diff = Map.lex_le_map(dim_val, dim_cst)
uset_data = uset_data.intersect(diff.domain())
dim_cst = Map.universe(space)
min_set = Set.from_pw_aff(set_data.dim_min(dim))
dim_cst = dim_cst.intersect_range(min_set)
dim_cst = dim_cst.apply_range(decr)
diff = Map.lex_ge_map(dim_val, dim_cst)
uset_data = uset_data.intersect(diff.domain())
return uset_data
def cmp_points(a, b):
a = Set.from_point(a)
b = Set.from_point(b)
if a.lex_le_set(b).is_empty():
return 1
else:
return -1
def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
class Key(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return Key
def sort_points(points):
"""
Given a list of points, sort them lexicographically.
:param points: The list of points that will be sorted.
"""
return sorted(points, key=cmp_to_key(cmp_points))
__all__ = ['bset_get_vertex_coordinates', 'bset_get_faces', 'set_get_faces',
'get_vertices_and_faces', 'get_point_coordinates', 'bset_get_points',
'get_rectangular_hull', 'sort_points']
You can’t perform that action at this time.
