-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathline.py
More file actions
285 lines (265 loc) · 10.2 KB
/
line.py
File metadata and controls
285 lines (265 loc) · 10.2 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
import scriptcontext
import utility as rhutil
import Rhino
def LineClosestPoint(line, testpoint):
"""Finds the point on an infinite line that is closest to a test point
Parameters:
line ([point, point]): List of 6 numbers or 2 Point3d. Two 3-D points identifying the starting and ending points of the line.
testpoint (point): List of 3 numbers or Point3d. The test point.
Returns:
point: the point on the line that is closest to the test point if successful, otherwise None
Example:
import rhinoscriptsyntax as rs
line = (0,0,0), (5,5,0)
point = (15, 10, 0)
result = rs.LineClosestPoint( line, point)
if result: rs.AddPoint(result)
See Also:
LineIsFartherThan
LineMaxDistanceTo
LineMinDistanceTo
LinePlane
LineTransform
"""
line = rhutil.coerceline(line, True)
testpoint = rhutil.coerce3dpoint(testpoint, True)
return line.ClosestPoint(testpoint, False)
def LineCylinderIntersection(line, cylinder_plane, cylinder_height, cylinder_radius):
"""Calculates the intersection of a line and a cylinder
Parameters:
line (guid|line): the line to intersect
cylinder_plane (plane): base plane of the cylinder
cylinder_height (number): height of the cylinder
cylinder_radius (number): radius of the cylinder
Returns:
list(point, ...): list of intersection points (0, 1, or 2 points)
Example:
import rhinoscriptsyntax as rs
plane = rs.WorldXYPlane()
line = (-10,0,0), (10,0,10)
points = rs.LineCylinderIntersection(line, plane, cylinder_height=10, cylinder_radius=5)
if points:
for point in points: rs.AddPoint(point)
See Also:
LineLineIntersection
LinePlaneIntersection
LineSphereIntersection
"""
line = rhutil.coerceline(line, True)
cylinder_plane = rhutil.coerceplane(cylinder_plane, True)
circle = Rhino.Geometry.Circle( cylinder_plane, cylinder_radius )
if not circle.IsValid: raise ValueError("unable to create valid circle with given plane and radius")
cyl = Rhino.Geometry.Cylinder( circle, cylinder_height )
if not cyl.IsValid: raise ValueError("unable to create valid cylinder with given circle and height")
rc, pt1, pt2 = Rhino.Geometry.Intersect.Intersection.LineCylinder(line, cyl)
if rc==Rhino.Geometry.Intersect.LineCylinderIntersection.None:
return []
if rc==Rhino.Geometry.Intersect.LineCylinderIntersection.Single:
return [pt1]
return [pt1, pt2]
def LineIsFartherThan(line, distance, point_or_line):
"""Determines if the shortest distance from a line to a point or another
line is greater than a specified distance
Parameters:
line (line | [point, point]): List of 6 numbers, 2 Point3d, or Line.
distance (number): the distance
point_or_line (point|line) the test point or the test line
Returns:
bool: True if the shortest distance from the line to the other project is
greater than distance, False otherwise
None: on error
Example:
import rhinoscriptsyntax as rs
line = (0,0,0), (10,10,0)
testPoint = (10,5,0)
print rs.LineIsFartherThan(line, 3, testPoint)
See Also:
LineClosestPoint
LineMaxDistanceTo
LineMinDistanceTo
LinePlane
LineTransform
"""
line = rhutil.coerceline(line, True)
test = rhutil.coerceline(point_or_line)
if not test: test = rhutil.coerce3dpoint(point_or_line, True)
minDist = line.MinimumDistanceTo(test)
return minDist>distance
def LineLineIntersection(lineA, lineB):
"""Calculates the intersection of two non-parallel lines. Note, the two
lines do not have to intersect for an intersection to be found. (see help)
Parameters:
lineA, lineB (line): lines to intersect
Returns:
tuple(point, point): containing a point on the first line and a point on the second line if successful
None: on error
Example:
import rhinoscriptsyntax as rs
lineA = (1,1,0), (5,0,0)
lineB = (1,3,0), (5,5,0)
point = rs.LineLineIntersection(lineA, lineB)
if point:
rs.AddPoint(point[0])
rs.AddPoint(point[1])
See Also:
IntersectPlanes
LinePlaneIntersection
PlanePlaneIntersection
"""
lineA = rhutil.coerceline(lineA, True)
lineB = rhutil.coerceline(lineB, True)
rc, a, b = Rhino.Geometry.Intersect.Intersection.LineLine(lineA, lineB)
if not rc: return None
return lineA.PointAt(a), lineB.PointAt(b)
def LineMaxDistanceTo(line, point_or_line):
"""Finds the longest distance between a line as a finite chord, and a point
or another line
Parameters:
line (line | [point, point]): List of 6 numbers, two Point3d, or Line.
point_or_line (point|line): the test point or test line.
Returns:
number: A distance (D) such that if Q is any point on the line and P is any point on the other object, then D >= Rhino.Distance(Q, P).
None: on error
Example:
import rhinoscriptsyntax as rs
line = (0,0,0), (10,10,0)
print rs.LineMaxDistanceTo( line, (10,5,0) )
See Also:
LineClosestPoint
LineIsFartherThan
LineMinDistanceTo
LinePlane
LineTransform
"""
line = rhutil.coerceline(line, True)
test = rhutil.coerceline(point_or_line)
if test is None: test = rhutil.coerce3dpoint(point_or_line, True)
return line.MaximumDistanceTo(test)
def LineMinDistanceTo(line, point_or_line):
"""Finds the shortest distance between a line as a finite chord, and a point
or another line
Parameters:
line (line | [point, point]): List of 6 numbers, two Point3d, or Line.
point_or_line (point|line): the test point or test line.
Returns:
number: A distance (D) such that if Q is any point on the line and P is any point on the other object, then D <= Rhino.Distance(Q, P).
None: on error
Example:
import rhinoscriptsyntax as rs
line = (0,0,0), (10,10,0)
print rs.LineMinDistanceTo(line, (10,5,0))
See Also:
LineClosestPoint
LineIsFartherThan
LineMaxDistanceTo
LinePlane
LineTransform
"""
line = rhutil.coerceline(line, True)
test = rhutil.coerceline(point_or_line)
if test is None: test = rhutil.coerce3dpoint(point_or_line, True)
return line.MinimumDistanceTo(test)
def LinePlane(line):
"""Returns a plane that contains the line. The origin of the plane is at the start of
the line. If possible, a plane parallel to the world XY, YZ, or ZX plane is returned
Parameters:
line (line | [point, point]): List of 6 numbers, two Point3d, or Line.
Returns:
plane: the plane if successful
None: if not successful
Example:
import rhinoscriptsyntax as rs
lineFrom = (0,0,0)
lineTo = (10,10,0)
distance = rs.Distance(lineFrom, lineTo)
plane = rs.LinePlane([lineFrom, lineTo])
rs.AddPlaneSurface( plane, distance, distance )
See Also:
LineClosestPoint
LineIsFartherThan
LineMaxDistanceTo
LineMinDistanceTo
LineTransform
"""
line = rhutil.coerceline(line, True)
rc, plane = line.TryGetPlane()
if not rc: return scriptcontext.errorhandler()
return plane
def LinePlaneIntersection(line, plane):
"""Calculates the intersection of a line and a plane.
Parameters:
line ([point, point]): Two 3D points identifying the starting and ending points of the line to intersect.
plane (plane): The plane to intersect.
Returns:
point: The 3D point of intersection is successful.
None: if not successful, or on error.
Example:
import rhinoscriptsyntax as rs
plane = rs.WorldXYPlane()
line = (2, 11, 13), (20, 4, -10)
point = rs.LinePlaneIntersection(line, plane)
if( point!=None ): rs.AddPoint(point)
See Also:
LineLineIntersection
PlanePlaneIntersection
"""
plane = rhutil.coerceplane(plane, True)
line_points = rhutil.coerce3dpointlist(line, True)
line = Rhino.Geometry.Line(line_points[0], line_points[1])
rc, t = Rhino.Geometry.Intersect.Intersection.LinePlane(line, plane)
if not rc: return scriptcontext.errorhandler()
return line.PointAt(t)
def LineSphereIntersection(line, sphere_center, sphere_radius):
"""Calculates the intersection of a line and a sphere
Parameters:
line (line | [point, point]): the line
sphere_center (point): the center point of the sphere
sphere_radius (number): the radius of the sphere
Returns:
list(point, ...): list of intersection points if successful, otherwise None
Example:
import rhinoscriptsyntax as rs
radius = 10
line = (-10,0,0), (10,0,10)
points = rs.LineSphereIntersection(line, (0,0,0), radius)
if points:
for point in points: rs.AddPoint(point)
See Also:
LineCylinderIntersection
LineLineIntersection
LinePlaneIntersection
"""
line = rhutil.coerceline(line, True)
sphere_center = rhutil.coerce3dpoint(sphere_center, True)
sphere = Rhino.Geometry.Sphere(sphere_center, sphere_radius)
rc, pt1, pt2 = Rhino.Geometry.Intersect.Intersection.LineSphere(line, sphere)
if rc==Rhino.Geometry.Intersect.LineSphereIntersection.None: return []
if rc==Rhino.Geometry.Intersect.LineSphereIntersection.Single: return [pt1]
return [pt1, pt2]
def LineTransform(line, xform):
"""Transforms a line
Parameters:
line (guid): the line to transform
xform (transform): the transformation to apply
Returns:
Geometry.Line: The transformed line geometry. ( Just the geometry, not a drawn line Object)
Example:
import rhinoscriptsyntax as rs
line = (0,0,0), (10,10,0)
rs.AddLine( line[0], line[1] )
plane = rs.WorldXYPlane()
xform = rs.XformRotation(30, plane.Zaxis, plane.Origin)
line = rs.LineTransform(line, xform)
rs.AddLine( line.From, line.To )
See Also:
LineClosestPoint
LineIsFartherThan
LineMaxDistanceTo
LineMinDistanceTo
LinePlane
"""
line = rhutil.coerceline(line, True)
xform = rhutil.coercexform(xform, True)
success = line.Transform(xform)
if not success: raise Execption("unable to transform line")
return line