forked from wassimj/topologicpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFace.py
1810 lines (1567 loc) · 67.8 KB
/
Face.py
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import topologicpy
import topologic
from topologicpy.Vector import Vector
from topologicpy.Wire import Wire
import math
class Face(topologic.Face):
@staticmethod
def AddInternalBoundaries(face: topologic.Face, wires: list) -> topologic.Face:
"""
Adds internal boundaries (closed wires) to the input face. Internal boundaries are considered holes in the input face.
Parameters
----------
face : topologic.Face
The input face.
wires : list
The input list of internal boundaries (closed wires).
Returns
-------
topologic.Face
The created face with internal boundaries added to it.
"""
if not face:
return None
if not isinstance(face, topologic.Face):
return None
if not wires:
return face
if not isinstance(wires, list):
return face
wireList = [w for w in wires if isinstance(w, topologic.Wire)]
if len(wireList) < 1:
return face
faceeb = face.ExternalBoundary()
faceibList = []
_ = face.InternalBoundaries(faceibList)
for wire in wires:
faceibList.append(wire)
return topologic.Face.ByExternalInternalBoundaries(faceeb, faceibList)
@staticmethod
def AddInternalBoundariesCluster(face: topologic.Face, cluster: topologic.Cluster) -> topologic.Face:
"""
Adds the input cluster of internal boundaries (closed wires) to the input face. Internal boundaries are considered holes in the input face.
Parameters
----------
face : topologic.Face
The input face.
cluster : topologic.Cluster
The input cluster of internal boundaries (topologic wires).
Returns
-------
topologic.Face
The created face with internal boundaries added to it.
"""
if not face:
return None
if not isinstance(face, topologic.Face):
return None
if not cluster:
return face
if not isinstance(cluster, topologic.Cluster):
return face
wires = []
_ = cluster.Wires(None, wires)
return Face.AddInternalBoundaries(face, wires)
@staticmethod
def Angle(faceA: topologic.Face, faceB: topologic.Face, mantissa: int = 4) -> float:
"""
Returns the angle in degrees between the two input faces.
Parameters
----------
faceA : topologic.Face
The first input face.
faceB : topologic.Face
The second input face.
mantissa : int , optional
The desired length of the mantissa. The default is 4.
Returns
-------
float
The angle in degrees between the two input faces.
"""
from topologicpy.Vector import Vector
if not faceA or not isinstance(faceA, topologic.Face):
return None
if not faceB or not isinstance(faceB, topologic.Face):
return None
dirA = Face.NormalAtParameters(faceA, 0.5, 0.5, "xyz", 3)
dirB = Face.NormalAtParameters(faceB, 0.5, 0.5, "xyz", 3)
return round((Vector.Angle(dirA, dirB)), mantissa)
@staticmethod
def Area(face: topologic.Face, mantissa: int = 4) -> float:
"""
Returns the area of the input face.
Parameters
----------
face : topologic.Face
The input face.
mantissa : int , optional
The desired length of the mantissa. The default is 4.
Returns
-------
float
The area of the input face.
"""
if not isinstance(face, topologic.Face):
return None
area = None
try:
area = round(topologic.FaceUtility.Area(face), mantissa)
except:
area = None
return area
@staticmethod
def BoundingRectangle(topology: topologic.Topology, optimize: int = 0) -> topologic.Face:
"""
Returns a face representing a bounding rectangle of the input topology. The returned face contains a dictionary with key "zrot" that represents rotations around the Z axis. If applied the resulting face will become axis-aligned.
Parameters
----------
topology : topologic.Topology
The input topology.
optimize : int , optional
If set to an integer from 1 (low optimization) to 10 (high optimization), the method will attempt to optimize the bounding rectangle so that it reduces its surface area. The default is 0 which will result in an axis-aligned bounding rectangle. The default is 0.
Returns
-------
topologic.Face
The bounding rectangle of the input topology.
"""
from topologicpy.Wire import Wire
from topologicpy.Face import Face
from topologicpy.Cluster import Cluster
from topologicpy.Topology import Topology
from topologicpy.Dictionary import Dictionary
def bb(topology):
vertices = []
_ = topology.Vertices(None, vertices)
x = []
y = []
for aVertex in vertices:
x.append(aVertex.X())
y.append(aVertex.Y())
minX = min(x)
minY = min(y)
maxX = max(x)
maxY = max(y)
return [minX, minY, maxX, maxY]
if not isinstance(topology, topologic.Topology):
return None
vertices = Topology.SubTopologies(topology, subTopologyType="vertex")
topology = Cluster.ByTopologies(vertices)
boundingBox = bb(topology)
minX = boundingBox[0]
minY = boundingBox[1]
maxX = boundingBox[2]
maxY = boundingBox[3]
w = abs(maxX - minX)
l = abs(maxY - minY)
best_area = l*w
orig_area = best_area
best_z = 0
best_bb = boundingBox
origin = Topology.Centroid(topology)
optimize = min(max(optimize, 0), 10)
if optimize > 0:
factor = (round(((11 - optimize)/30 + 0.57), 2))
flag = False
for n in range(10,0,-1):
if flag:
break
za = n
zb = 90+n
zc = n
for z in range(za,zb,zc):
if flag:
break
t = Topology.Rotate(topology, origin=origin, x=0,y=0,z=1, degree=z)
minX, minY, maxX, maxY = bb(t)
w = abs(maxX - minX)
l = abs(maxY - minY)
area = l*w
if area < orig_area*factor:
best_area = area
best_z = z
best_bb = [minX, minY, maxX, maxY]
flag = True
break
if area < best_area:
best_area = area
best_z = z
best_bb = [minX, minY, maxX, maxY]
else:
best_bb = boundingBox
minX, minY, maxX, maxY = best_bb
vb1 = topologic.Vertex.ByCoordinates(minX, minY, 0)
vb2 = topologic.Vertex.ByCoordinates(maxX, minY, 0)
vb3 = topologic.Vertex.ByCoordinates(maxX, maxY, 0)
vb4 = topologic.Vertex.ByCoordinates(minX, maxY, 0)
baseWire = Wire.ByVertices([vb1, vb2, vb3, vb4], close=True)
baseFace = Face.ByWire(baseWire)
baseFace = Topology.Rotate(baseFace, origin=origin, x=0,y=0,z=1, degree=-best_z)
dictionary = Dictionary.ByKeysValues(["zrot"], [best_z])
baseFace = Topology.SetDictionary(baseFace, dictionary)
return baseFace
@staticmethod
def ByEdges(edges: list) -> topologic.Face:
"""
Creates a face from the input list of edges.
Parameters
----------
edges : list
The input list of edges.
Returns
-------
face : topologic.Face
The created face.
"""
from topologicpy.Wire import Wire
wire = Wire.ByEdges(edges)
if not wire:
return None
if not isinstance(wire, topologic.Wire):
return None
return Face.ByWire(wire)
@staticmethod
def ByEdgesCluster(cluster: topologic.Cluster) -> topologic.Face:
"""
Creates a face from the input cluster of edges.
Parameters
----------
cluster : topologic.Cluster
The input cluster of edges.
Returns
-------
face : topologic.Face
The created face.
"""
from topologicpy.Cluster import Cluster
if not isinstance(cluster, topologic.Cluster):
return None
edges = Cluster.Edges(cluster)
return Face.ByEdges(edges)
@staticmethod
def ByOffset(face: topologic.Face, offset: float = 1.0, miter: bool = False, miterThreshold: float = None, offsetKey: str = None, miterThresholdKey: str = None, step: bool = True) -> topologic.Face:
"""
Creates an offset wire from the input wire.
Parameters
----------
wire : topologic.Wire
The input wire.
offset : float , optional
The desired offset distance. The default is 1.0.
miter : bool , optional
if set to True, the corners will be mitered. The default is False.
miterThreshold : float , optional
The distance beyond which a miter should be added. The default is None which means the miter threshold is set to the offset distance multiplied by the square root of 2.
offsetKey : str , optional
If specified, the dictionary of the edges will be queried for this key to sepcify the desired offset. The default is None.
miterThresholdKey : str , optional
If specified, the dictionary of the vertices will be queried for this key to sepcify the desired miter threshold distance. The default is None.
step : bool , optional
If set to True, The transition between collinear edges with different offsets will be a step. Otherwise, it will be a continous edge. The default is True.
Returns
-------
topologic.Wire
The created wire.
"""
from topologicpy.Wire import Wire
eb = Face.Wire(face)
internal_boundaries = Face.InternalBoundaries(face)
offset_external_boundary = Wire.ByOffset(wire=eb, offset=offset, miter=miter, miterThreshold=miterThreshold, offsetKey=offsetKey, miterThresholdKey=miterThresholdKey, step=step)
offset_internal_boundaries = []
for internal_boundary in internal_boundaries:
offset_internal_boundaries.append(Wire.ByOffset(wire=internal_boundary, offset=offset, miter=miter, miterThreshold=miterThreshold, offsetKey=offsetKey, miterThresholdKey=miterThresholdKey, step=step))
return Face.ByWires(offset_external_boundary, offset_internal_boundaries)
@staticmethod
def ByShell(shell: topologic.Shell, angTolerance: float = 0.1)-> topologic.Face:
"""
Creates a face by merging the faces of the input shell.
Parameters
----------
shell : topologic.Shell
The input shell.
angTolerance : float , optional
The desired angular tolerance. The default is 0.1.
Returns
-------
topologic.Face
The created face.
"""
from topologicpy.Vertex import Vertex
from topologicpy.Wire import Wire
from topologicpy.Shell import Shell
from topologicpy.Topology import Topology
def planarizeList(wireList):
returnList = []
for aWire in wireList:
returnList.append(Wire.Planarize(aWire))
return returnList
ext_boundary = Shell.ExternalBoundary(shell)
ext_boundary = Wire.RemoveCollinearEdges(ext_boundary, angTolerance)
if not Topology.IsPlanar(ext_boundary):
ext_boundary = Wire.Planarize(ext_boundary)
if isinstance(ext_boundary, topologic.Wire):
try:
return topologic.Face.ByExternalBoundary(Wire.RemoveCollinearEdges(ext_boundary, angTolerance))
except:
try:
w = Wire.Planarize(ext_boundary)
f = Face.ByWire(w)
return f
except:
print("FaceByPlanarShell - Error: The input Wire is not planar and could not be fixed. Returning None.")
return None
elif isinstance(ext_boundary, topologic.Cluster):
wires = []
_ = ext_boundary.Wires(None, wires)
faces = []
areas = []
for aWire in wires:
try:
aFace = topologic.Face.ByExternalBoundary(Wire.RemoveCollinearEdges(aWire, angTolerance))
except:
aFace = topologic.Face.ByExternalBoundary(Wire.Planarize(Wire.RemoveCollinearEdges(aWire, angTolerance)))
anArea = topologic.FaceUtility.Area(aFace)
faces.append(aFace)
areas.append(anArea)
max_index = areas.index(max(areas))
ext_boundary = faces[max_index]
int_boundaries = list(set(faces) - set([ext_boundary]))
int_wires = []
for int_boundary in int_boundaries:
temp_wires = []
_ = int_boundary.Wires(None, temp_wires)
int_wires.append(Wire.RemoveCollinearEdges(temp_wires[0], angTolerance))
temp_wires = []
_ = ext_boundary.Wires(None, temp_wires)
ext_wire = Wire.RemoveCollinearEdges(temp_wires[0], angTolerance)
try:
return topologic.Face.ByExternalInternalBoundaries(ext_wire, int_wires)
except:
return topologic.Face.ByExternalInternalBoundaries(Wire.Planarize(ext_wire), planarizeList(int_wires))
else:
return None
@staticmethod
def ByVertices(vertices: list) -> topologic.Face:
"""
Creates a face from the input list of vertices.
Parameters
----------
vertices : list
The input list of vertices.
Returns
-------
topologic.Face
The created face.
"""
from topologicpy.Topology import Topology
from topologicpy.Wire import Wire
if not isinstance(vertices, list):
return None
vertexList = [x for x in vertices if isinstance(x, topologic.Vertex)]
if len(vertexList) < 3:
return None
w = Wire.ByVertices(vertexList)
f = Face.ByExternalBoundary(w)
return f
@staticmethod
def ByVerticesCluster(cluster: topologic.Cluster) -> topologic.Face:
"""
Creates a face from the input cluster of vertices.
Parameters
----------
cluster : topologic.Cluster
The input cluster of vertices.
Returns
-------
topologic.Face
The crearted face.
"""
from topologicpy.Cluster import Cluster
if not isinstance(cluster, topologic.Cluster):
return None
vertices = Cluster.Vertices(cluster)
return Face.ByVertices(vertices)
@staticmethod
def ByWire(wire: topologic.Wire) -> topologic.Face:
"""
Creates a face from the input closed wire.
Parameters
----------
wire : topologic.Wire
The input wire.
Returns
-------
topologic.Face or list
The created face. If the wire is non-planar, the method will attempt to triangulate the wire and return a list of faces.
"""
from topologicpy.Vertex import Vertex
from topologicpy.Wire import Wire
from topologicpy.Shell import Shell
from topologicpy.Cluster import Cluster
from topologicpy.Topology import Topology
from topologicpy.Dictionary import Dictionary
import random
def triangulateWire(wire):
wire = Wire.RemoveCollinearEdges(wire)
vertices = Wire.Vertices(wire)
shell = Shell.Delaunay(vertices)
if isinstance(shell, topologic.Shell):
return Shell.Faces(shell)
else:
return []
if not isinstance(wire, topologic.Wire):
return None
if not Wire.IsClosed(wire):
return None
edges = Wire.Edges(wire)
wire = Topology.SelfMerge(Cluster.ByTopologies(edges))
vertices = Wire.Vertices(wire)
#print("This wire has:", len(vertices), "vertices.")
try:
#print(Topology.IsPlanar(wire))
fList = topologic.Face.ByExternalBoundary(wire)
except:
if len(vertices) > 3:
print("This wire has:", len(vertices), "vertices.")
print("Non planar wire, triangulating")
fList = triangulateWire(wire)
print("After triangulation", fList)
else:
fList = []
if not isinstance(fList, list):
fList = [fList]
returnList = []
for f in fList:
if Face.Area(f) < 0:
wire = Face.ExternalBoundary(f)
wire = Wire.Invert(wire)
try:
f = topologic.Face.ByExternalBoundary(wire)
returnList.append(f)
except:
pass
else:
returnList.append(f)
if len(returnList) == 0:
return None
elif len(returnList) == 1:
return returnList[0]
else:
return returnList
@staticmethod
def ByWires(externalBoundary: topologic.Wire, internalBoundaries: list = []) -> topologic.Face:
"""
Creates a face from the input external boundary (closed wire) and the input list of internal boundaries (closed wires).
Parameters
----------
externalBoundary : topologic.Wire
The input external boundary.
internalBoundaries : list , optional
The input list of internal boundaries (closed wires). The default is an empty list.
Returns
-------
topologic.Face
The created face.
"""
if not isinstance(externalBoundary, topologic.Wire):
return None
if not Wire.IsClosed(externalBoundary):
return None
ibList = [x for x in internalBoundaries if isinstance(x, topologic.Wire) and Wire.IsClosed(x)]
return topologic.Face.ByExternalInternalBoundaries(externalBoundary, ibList)
@staticmethod
def ByWiresCluster(externalBoundary: topologic.Wire, internalBoundariesCluster: topologic.Cluster = None) -> topologic.Face:
"""
Creates a face from the input external boundary (closed wire) and the input cluster of internal boundaries (closed wires).
Parameters
----------
externalBoundary : topologic.Wire
The input external boundary (closed wire).
internalBoundariesCluster : topologic.Cluster
The input cluster of internal boundaries (closed wires). The default is None.
Returns
-------
topologic.Face
The created face.
"""
from topologicpy.Wire import Wire
from topologicpy.Cluster import Cluster
if not isinstance(externalBoundary, topologic.Wire):
return None
if not Wire.IsClosed(externalBoundary):
return None
if not internalBoundariesCluster:
internalBoundaries = []
elif not isinstance(internalBoundariesCluster, topologic.Cluster):
return None
else:
internalBoundaries = Cluster.Wires(internalBoundariesCluster)
return Face.ByWires(externalBoundary, internalBoundaries)
@staticmethod
def Circle(origin: topologic.Vertex = None, radius: float = 0.5, sides: int = 16, fromAngle: float = 0.0, toAngle: float = 360.0, direction: list = [0,0,1],
placement: str = "center", tolerance: float = 0.0001) -> topologic.Face:
"""
Creates a circle.
Parameters
----------
origin : topologic.Vertex, optional
The location of the origin of the circle. The default is None which results in the circle being placed at (0,0,0).
radius : float , optional
The radius of the circle. The default is 1.
sides : int , optional
The number of sides of the circle. The default is 16.
fromAngle : float , optional
The angle in degrees from which to start creating the arc of the circle. The default is 0.
toAngle : float , optional
The angle in degrees at which to end creating the arc of the circle. The default is 360.
direction : list , optional
The vector representing the up direction of the circle. The default is [0,0,1].
placement : str , optional
The description of the placement of the origin of the circle. This can be "center", "lowerleft", "upperleft", "lowerright", or "upperright". It is case insensitive. The default is "center".
tolerance : float , optional
The desired tolerance. The default is 0.0001.
Returns
-------
topologic.Face
The created circle.
"""
from topologicpy.Wire import Wire
wire = Wire.Circle(origin=origin, radius=radius, sides=sides, fromAngle=fromAngle, toAngle=toAngle, close=True, direction=direction, placement=placement, tolerance=tolerance)
if not isinstance(wire, topologic.Wire):
return None
return Face.ByWire(wire)
@staticmethod
def Compactness(face: topologic.Face, mantissa: int = 4) -> float:
"""
Returns the compactness measure of the input face. See https://en.wikipedia.org/wiki/Compactness_measure_of_a_shape
Parameters
----------
face : topologic.Face
The input face.
mantissa : int , optional
The desired length of the mantissa. The default is 4.
Returns
-------
float
The compactness measure of the input face.
"""
exb = face.ExternalBoundary()
edges = []
_ = exb.Edges(None, edges)
perimeter = 0.0
for anEdge in edges:
perimeter = perimeter + abs(topologic.EdgeUtility.Length(anEdge))
area = abs(topologic.FaceUtility.Area(face))
compactness = 0
#From https://en.wikipedia.org/wiki/Compactness_measure_of_a_shape
if area <= 0:
return None
if perimeter <= 0:
return None
compactness = (math.pi*(2*math.sqrt(area/math.pi)))/perimeter
return round(compactness, mantissa)
@staticmethod
def CompassAngle(face: topologic.Face, north: list = None, mantissa: int = 4) -> float:
"""
Returns the horizontal compass angle in degrees between the normal vector of the input face and the input vector. The angle is measured in counter-clockwise fashion. Only the first two elements of the vectors are considered.
Parameters
----------
face : topologic.Face
The input face.
north : list , optional
The second vector representing the north direction. The default is the positive YAxis ([0,1,0]).
mantissa : int, optional
The length of the desired mantissa. The default is 4.
tolerance : float , optional
The desired tolerance. The default is 0.0001.
Returns
-------
float
The horizontal compass angle in degrees between the direction of the face and the second input vector.
"""
from topologicpy.Vector import Vector
if not isinstance(face, topologic.Face):
return None
if not north:
north = Vector.North()
dirA = Face.NormalAtParameters(face,mantissa=mantissa)
return Vector.CompassAngle(vectorA=dirA, vectorB=north, mantissa=mantissa)
@staticmethod
def Edges(face: topologic.Face) -> list:
"""
Returns the edges of the input face.
Parameters
----------
face : topologic.Face
The input face.
Returns
-------
list
The list of edges.
"""
if not isinstance(face, topologic.Face):
return None
edges = []
_ = face.Edges(None, edges)
return edges
@staticmethod
def Einstein(origin: topologic.Vertex = None, radius: float = 0.5, direction: list = [0,0,1], placement: str = "center") -> topologic.Face:
"""
Creates an aperiodic monotile, also called an 'einstein' tile (meaning one tile in German, not the name of the famous physist). See https://arxiv.org/abs/2303.10798
Parameters
----------
origin : topologic.Vertex , optional
The location of the origin of the tile. The default is None which results in the tiles first vertex being placed at (0,0,0).
radius : float , optional
The radius of the hexagon determining the size of the tile. The default is 0.5.
direction : list , optional
The vector representing the up direction of the ellipse. The default is [0,0,1].
placement : str , optional
The description of the placement of the origin of the hexagon determining the location of the tile. This can be "center", or "lowerleft". It is case insensitive. The default is "center".
"""
from topologicpy.Wire import Wire
wire = Wire.Einstein(origin=origin, radius=radius, direction=direction, placement=placement)
if not isinstance(wire, topologic.Wire):
return None
return Face.ByWire(wire)
@staticmethod
def ExternalBoundary(face: topologic.Face) -> topologic.Wire:
"""
Returns the external boundary (closed wire) of the input face.
Parameters
----------
face : topologic.Face
The input face.
Returns
-------
topologic.Wire
The external boundary of the input face.
"""
return face.ExternalBoundary()
@staticmethod
def FacingToward(face: topologic.Face, direction: list = [0,0,-1], asVertex: bool = False, tolerance: float = 0.0001) -> bool:
"""
Returns True if the input face is facing toward the input direction.
Parameters
----------
face : topologic.Face
The input face.
direction : list , optional
The input direction. The default is [0,0,-1].
asVertex : bool , optional
If set to True, the direction is treated as an actual vertex in 3D space. The default is False.
tolerance : float , optional
The desired tolerance. The default is 0.0001.
Returns
-------
bool
True if the face is facing toward the direction. False otherwise.
"""
faceNormal = topologic.FaceUtility.NormalAtParameters(face,0.5, 0.5)
faceCenter = topologic.FaceUtility.VertexAtParameters(face,0.5,0.5)
cList = [faceCenter.X(), faceCenter.Y(), faceCenter.Z()]
try:
vList = [direction.X(), direction.Y(), direction.Z()]
except:
try:
vList = [direction[0], direction[1], direction[2]]
except:
raise Exception("Face.FacingToward - Error: Could not get the vector from the input direction")
if asVertex:
dV = [vList[0]-cList[0], vList[1]-cList[1], vList[2]-cList[2]]
else:
dV = vList
uV = Vector.Normalize(dV)
dot = sum([i*j for (i, j) in zip(uV, faceNormal)])
if dot < tolerance:
return False
return True
@staticmethod
def Flatten(face: topologic.Face, originA: topologic.Vertex = None, originB: topologic.Vertex = None, direction: list = None) -> topologic.Face:
"""
Flattens the input face such that its center of mass is located at the origin and its normal is pointed in the positive Z axis.
Parameters
----------
face : topologic.Face
The input face.
originA : topologic.Vertex , optional
The old location to use as the origin of the movement. If set to None, the center of mass of the input topology is used. The default is None.
originB : topologic.Vertex , optional
The new location at which to place the topology. If set to None, the world origin (0,0,0) is used. The default is None.
direction : list , optional
The direction, expressed as a list of [X,Y,Z] that signifies the direction of the face. If set to None, the normal at *u* 0.5 and *v* 0.5 is considered the direction of the face. The deafult is None.
Returns
-------
topologic.Face
The flattened face.
"""
def leftMost(vertices, tolerance = 0.0001):
xCoords = []
for v in vertices:
xCoords.append(Vertex.Coordinates(vertices[0])[0])
minX = min(xCoords)
lmVertices = []
for v in vertices:
if abs(Vertex.Coordinates(vertices[0])[0] - minX) <= tolerance:
lmVertices.append(v)
return lmVertices
def bottomMost(vertices, tolerance = 0.0001):
yCoords = []
for v in vertices:
yCoords.append(Vertex.Coordinates(vertices[0])[1])
minY = min(yCoords)
bmVertices = []
for v in vertices:
if abs(Vertex.Coordinates(vertices[0])[1] - minY) <= tolerance:
bmVertices.append(v)
return bmVertices
def vIndex(v, vList, tolerance):
for i in range(len(vList)):
if topologic.VertexUtility.Distance(v, vList[i]) < tolerance:
return i+1
return None
# rotate cycle path such that it begins with the smallest node
def rotate_to_smallest(path):
n = path.index(min(path))
return path[n:]+path[:n]
# rotate vertices list so that it begins with the input vertex
def rotate_vertices(vertices, vertex):
n = vertices.index(vertex)
return vertices[n:]+vertices[:n]
from topologicpy.Vertex import Vertex
from topologicpy.Topology import Topology
from topologicpy.Dictionary import Dictionary
if not isinstance(face, topologic.Face):
return None
if not isinstance(originA, topologic.Vertex):
originA = Topology.CenterOfMass(face)
if not isinstance(originB, topologic.Vertex):
originB = Vertex.ByCoordinates(0,0,0)
cm = originA
world_origin = originB
if not direction or len(direction) < 3:
direction = Face.NormalAtParameters(face, 0.5, 0.5)
x1 = Vertex.X(cm)
y1 = Vertex.Y(cm)
z1 = Vertex.Z(cm)
x2 = Vertex.X(cm) + direction[0]
y2 = Vertex.Y(cm) + direction[1]
z2 = Vertex.Z(cm) + direction[2]
dx = x2 - x1
dy = y2 - y1
dz = z2 - z1
dist = math.sqrt(dx**2 + dy**2 + dz**2)
phi = math.degrees(math.atan2(dy, dx)) # Rotation around Y-Axis
if dist < 0.0001:
theta = 0
else:
theta = math.degrees(math.acos(dz/dist)) # Rotation around Z-Axis
flatFace = Topology.Translate(face, -cm.X(), -cm.Y(), -cm.Z())
flatFace = Topology.Rotate(flatFace, world_origin, 0, 0, 1, -phi)
flatFace = Topology.Rotate(flatFace, world_origin, 0, 1, 0, -theta)
# Ensure flatness. Force Z to be zero
flatExternalBoundary = Face.ExternalBoundary(flatFace)
flatFaceVertices = Topology.SubTopologies(flatExternalBoundary, subTopologyType="vertex")
tempVertices = []
for ffv in flatFaceVertices:
tempVertices.append(Vertex.ByCoordinates(ffv.X(), ffv.Y(), 0))
temp_v = bottomMost(leftMost(tempVertices))[0]
tempVertices = rotate_vertices(tempVertices, temp_v)
flatExternalBoundary = Wire.ByVertices(tempVertices)
internalBoundaries = Face.InternalBoundaries(flatFace)
flatInternalBoundaries = []
for internalBoundary in internalBoundaries:
ibVertices = Wire.Vertices(internalBoundary)
tempVertices = []
for ibVertex in ibVertices:
tempVertices.append(Vertex.ByCoordinates(ibVertex.X(), ibVertex.Y(), 0))
temp_v = bottomMost(leftMost(tempVertices))[0]
tempVertices = rotate_vertices(tempVertices, temp_v)
flatInternalBoundaries.append(Wire.ByVertices(tempVertices))
flatFace = Face.ByWires(flatExternalBoundary, flatInternalBoundaries)
dictionary = Dictionary.ByKeysValues(["xTran", "yTran", "zTran", "phi", "theta"], [cm.X(), cm.Y(), cm.Z(), phi, theta])
flatFace = Topology.SetDictionary(flatFace, dictionary)
return flatFace
@staticmethod
def Harmonize(face: topologic.Face) -> topologic.Face:
"""
Returns a harmonized version of the input face such that the *u* and *v* origins are always in the upperleft corner.
Parameters
----------
face : topologic.Face
The input face.
Returns
-------
topologic.Face
The harmonized face.
"""
from topologicpy.Vertex import Vertex
from topologicpy.Wire import Wire
from topologicpy.Topology import Topology
from topologicpy.Dictionary import Dictionary
if not isinstance(face, topologic.Face):
return None
flatFace = Face.Flatten(face)
world_origin = Vertex.ByCoordinates(0,0,0)
# Retrieve the needed transformations
dictionary = Topology.Dictionary(flatFace)
xTran = Dictionary.ValueAtKey(dictionary,"xTran")
yTran = Dictionary.ValueAtKey(dictionary,"yTran")
zTran = Dictionary.ValueAtKey(dictionary,"zTran")
phi = Dictionary.ValueAtKey(dictionary,"phi")
theta = Dictionary.ValueAtKey(dictionary,"theta")
vertices = Wire.Vertices(Face.ExternalBoundary(flatFace))
harmonizedEB = Wire.ByVertices(vertices)
internalBoundaries = Face.InternalBoundaries(flatFace)
harmonizedIB = []
for ib in internalBoundaries:
ibVertices = Wire.Vertices(ib)
harmonizedIB.append(Wire.ByVertices(ibVertices))
harmonizedFace = Face.ByWires(harmonizedEB, harmonizedIB)
harmonizedFace = Topology.Rotate(harmonizedFace, origin=world_origin, x=0, y=1, z=0, degree=theta)
harmonizedFace = Topology.Rotate(harmonizedFace, origin=world_origin, x=0, y=0, z=1, degree=phi)
harmonizedFace = Topology.Translate(harmonizedFace, xTran, yTran, zTran)
return harmonizedFace
@staticmethod
def InternalBoundaries(face: topologic.Face) -> list:
"""
Returns the internal boundaries (closed wires) of the input face.
Parameters
----------
face : topologic.Face
The input face.
Returns
-------
list
The list of internal boundaries (closed wires).
"""
if not isinstance(face, topologic.Face):
return None
wires = []
_ = face.InternalBoundaries(wires)
return list(wires)
@staticmethod
def InternalVertex(face: topologic.Face, tolerance: float = 0.0001) -> topologic.Vertex:
"""
Creates a vertex guaranteed to be inside the input face.
Parameters
----------
face : topologic.Face
The input face.
tolerance : float , optional
The desired tolerance. The default is 0.0001.
Returns
-------
topologic.Vertex
The created vertex.
"""
if not isinstance(face, topologic.Face):
return None
v = topologic.FaceUtility.InternalVertex(face, tolerance)
return v
@staticmethod
def Invert(face: topologic.Face) -> topologic.Face:
"""
Creates a face that is an inverse (mirror) of the input face.
Parameters
----------
face : topologic.Face
The input face.
Returns
-------
topologic.Face