-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeom.py
More file actions
96 lines (63 loc) · 2.69 KB
/
geom.py
File metadata and controls
96 lines (63 loc) · 2.69 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
import adsk.core
import adsk.fusion
def distToPoint(fromPoint: adsk.core.Point3D, toPoint: adsk.core.Point3D):
return fromPoint.distanceTo(toPoint)
ORIGIN = adsk.core.Point3D.create(0, 0, 0)
def distToOrigin(fromPoint: adsk.core.Point3D) -> float:
return distToPoint(fromPoint, ORIGIN)
def lineLength(line: adsk.core.Line3D) -> float:
return line.startPoint.distanceTo(line.endPoint)
def sketchLineLength(line: adsk.fusion.SketchLine) -> float:
return lineLength(line.geometry)
def edgeLength(edge: adsk.fusion.BRepEdge) -> float:
return edge.length
def edgeDirection(edge: adsk.fusion.BRepEdge) -> adsk.core.Vector3D:
return edge.geometry.asInfiniteLine().direction
def areFacesCoplanar(f1: adsk.fusion.BRepFace, f2: adsk.fusion.BRepFace) -> bool:
return f1.geometry.isCoPlanarTo(f2.geometry)
def arePlanesParallel(f1: adsk.fusion.BRepFace, f2: adsk.fusion.BRepFace) -> bool:
return f1.geometry.isParallelToPlane(f2.geometry)
def distBetweenFaces(f1: adsk.fusion.BRepFace, f2: adsk.fusion.BRepFace) -> float:
normal = faceNormal(f1)
line = adsk.core.InfiniteLine3D.create(ORIGIN, normal)
rl1 = f1.geometry.intersectWithLine(line)
rl2 = f2.geometry.intersectWithLine(line)
if rl1 is None:
raise Exception("f1 doesn't have an intersection?")
if rl2 is None:
raise Exception("f2 doesn't have an intersection?")
# compute the distance vector between the two intersection points,
# and correct the sign if they're going the opposite direction of the normal
return rl1.vectorTo(rl2).dotProduct(normal)
def faceNormal(face: adsk.fusion.BRepFace) -> adsk.core.Vector3D:
normal = face.geometry.normal.copy()
if face.isParamReversed:
normal.scaleBy(-1)
return normal
def edgeDirectionForComparison(edge):
direction = edgeDirection(edge)
import math
rads = math.atan2(direction.x, direction.y)
if rads <= 0:
rads += math.pi
rads = rads % math.pi
# round to a low enough precision for comparision
# return (round(rads, 8), round(direction.x, 8), round(direction.y, 8))
return round(rads, 8)
def lerp(p1: adsk.core.Point3D, p2: adsk.core.Point3D, amt: float) -> adsk.core.Point3D:
amt = max(min(amt, 1), 0)
tma = 1 - amt
return adsk.core.Point3D.create(
(p1.x * amt) + (p2.x * tma),
(p1.y * amt) + (p2.y * tma),
(p1.z * amt) + (p2.z * tma),
)
def adskList(collection, klass):
ret = []
for i in range(collection.count):
item = collection.item(i)
casted = klass.cast(item)
if casted is None:
raise Exception("item {} at {} was None for {}".format(item, i, klass))
ret.append(casted)
return ret