Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def line(self):
dim = int( self.read_next_non_whitespace().strip())
start = np.array(next(self.fstream).split(), dtype=float)
direction= np.array(next(self.fstream).split(), dtype=float)
finite = next(self.fstream).strip() != '0'
param = np.array(next(self.fstream).split(), dtype=float)
reverse = next(self.fstream).strip() != '0'
d = np.array(direction)
s = np.array(start)
# d /= np.linalg.norm(d)
if not finite:
param = [-state.unlimited, +state.unlimited]
result = CurveFactory.line(s+d*param[0], s+d*param[1])
if reverse:
result.reverse()
return result
raise OrientationError("Mismatching physical dimensions")
if Counter(shape_a) != Counter(shape_b):
raise OrientationError("Non-matching objects")
# Enumerate all permutations of directions
for perm in permutations(range(pardim)):
transposed = cpb.controlpoints.transpose(perm + (pardim,))
if transposed.shape != shape_a:
continue
# Enumerate all possible direction reversals
for flip in product([False, True], repeat=pardim):
slices = tuple(slice(None, None, -1) if f else slice(None) for f in flip)
test_b = transposed[slices + (slice(None),)]
if np.allclose(cpa.controlpoints, test_b,
rtol=state.controlpoint_relative_tolerance,
atol=state.controlpoint_absolute_tolerance):
if all([cpa.bases[i].matches(cpb.bases[perm[i]], reverse=flip[i]) for i in range(pardim)]):
return cls(perm, flip)
raise OrientationError("Non-matching objects")
:param matrix-like x: Matrix *X[i,j]* of interpolation points *x_i* with
components *j*
:param int boundary: Any value from :class:`Boundary`.
:param array-like t: parametric values at interpolation points, defaults
to Euclidean distance between evaluation points
:param matrix-like tangents: Tangent information according to the boundary
conditions.
:return: Interpolated curve
:rtype: Curve
"""
# if periodic input is not closed, make sure we do it now
if boundary == Boundary.PERIODIC and not (
np.allclose(x[0,:], x[-1,:],
rtol = state.controlpoint_relative_tolerance,
atol = state.controlpoint_absolute_tolerance)):
x = np.append(x, [x[0,:]], axis=0)
if t is not None:
# augment interpolation knot by euclidian distance to end
t = list(t) + [t[-1] + norm(np.array(x[0,:])- np.array(x[-2,:]))]
n = len(x)
if t is None:
t = [0.0]
for (x0,x1) in zip(x[:-1,:], x[1:,:]):
# eucledian distance between two consecutive points
dist = norm(np.array(x1)-np.array(x0))
t.append(t[-1]+dist)
# modify knot vector for chosen boundary conditions
knot = [t[0]]*3 + list(t) + [t[-1]]*3
if boundary == Boundary.FREE:
def _eq(self, a, b):
"""Check whether two numpy arrays are almost equal, according to the given
tolerances.
"""
return np.allclose(a, b,
rtol=state.controlpoint_relative_tolerance,
atol=state.controlpoint_absolute_tolerance)
if len(curves) == 2:
crv1 = curves[0].clone()
crv2 = curves[1].clone()
Curve.make_splines_identical(crv1, crv2)
(n, d) = crv1.controlpoints.shape # d = dimension + rational
controlpoints = np.zeros((2 * n, d))
controlpoints[:n, :] = crv1.controlpoints
controlpoints[n:, :] = crv2.controlpoints
linear = BSplineBasis(2)
return Surface(crv1.bases[0], linear, controlpoints, crv1.rational)
elif len(curves) == 4:
# reorganize input curves so they form a directed loop around surface
rtol = state.controlpoint_relative_tolerance
atol = state.controlpoint_absolute_tolerance
mycurves = [c.clone() for c in curves] # wrap into list and clone all since we're changing them
dim = np.max([c.dimension for c in mycurves])
rat = np.any([c.rational for c in mycurves])
for i in range(4):
for j in range(i+1,4):
Curve.make_splines_compatible(mycurves[i], mycurves[j])
if not (np.allclose(mycurves[0][-1], mycurves[1][0], rtol=rtol, atol=atol) and
np.allclose(mycurves[1][-1], mycurves[2][0], rtol=rtol, atol=atol) and
np.allclose(mycurves[2][-1], mycurves[3][0], rtol=rtol, atol=atol) and
np.allclose(mycurves[3][-1], mycurves[0][0], rtol=rtol, atol=atol)):
reorder = [mycurves[0]]
del mycurves[0]
for j in range(3):
found_match = False
for i in range(len(mycurves)):
b = np.array([ np.dot(pt1,pt1) - np.dot(pt0,pt0),
np.dot(pt2,pt2) - np.dot(pt0,pt0),
np.dot(normal,pt0)])
center = np.linalg.solve(A,b)
radius = norm(pt2-center)
v2 = pt2-center
v1 = pt1-center
v0 = pt0-center
w0 = pt0-pt2
w1 = pt1-pt2
w2 = np.cross(w0, w1)
normal = np.cross(v0,v2)
len_v2 = norm(v2)
len_v0 = norm(v0)
theta = np.arccos(np.dot(v2,v0) / len_v2 / len_v0)
if not np.all([np.sign(i)==np.sign(j) or abs(i-j) < state.controlpoint_absolute_tolerance for (i,j) in zip(w2,normal)]):
theta = 2*pi - theta
normal = -normal
result = circle_segment(theta, radius, center, np.cross(v0,v1), v0)
# spit out 2D curve if all input points were 2D, otherwise return 3D
result.set_dimension(np.max([len(x0), len(x1), len(x2)]))
return result
space), or if they are not closed, or if they are not looping properly
"""
super(Surface, self).__init__([basis1, basis2], controlpoints, rational, **kwargs)
# make sure to make deep copies of the loops so nothing bad happens
self.boundaries = [[l.clone() for l in one_loop] for one_loop in loops]
# error check input curves
for one_loop in self.boundaries:
for curve in one_loop:
if not curve.dimension == 2:
raise RuntimeError('Boundary curves need to have dimension 2')
for i in range(len(one_loop)):
# print(state.parametric_absolute_tolerance)
# print(one_loop[i-1][-1,:], ' ', one_loop[i][0,:])
if not np.allclose(one_loop[i-1][-1,:], one_loop[i][0,:],
rtol=state.parametric_relative_tolerance,
atol=state.parametric_absolute_tolerance):
raise RuntimeError('Boundary curves not closed')
self.__compute_convex_hulls()
curve_piece = (curve_factory.circle_segment(delta_t)*[rx,ry]).rotate(theta1) + center
# curve_piece = curve_factory.circle_segment(delta_t)
elif piece[0] == 'z' or piece[0] == 'Z':
# periodic curve
# curve_piece = Curve(BSplineBasis(2), [startpoint, last_curve[0]])
# curve_piece.reparam([0, curve_piece.length()])
# last_curve.append(curve_piece).make_periodic(0)
last_curve.make_periodic(0)
result.append(last_curve)
last_curve = None
continue
else:
raise RuntimeError('Unknown path parameter:' + piece)
if(curve_piece.length()>state.controlpoint_absolute_tolerance):
curve_piece.reparam([0, curve_piece.length()])
if last_curve is None:
last_curve = curve_piece
else:
last_curve.append(curve_piece)
startpoint = last_curve[-1,:2] # disregard rational weight (if any)
if last_curve is not None:
result.append(last_curve)
return result
def _eq(self, a, b):
"""Check whether two numpy arrays are almost equal, according to the given
tolerances.
"""
return np.allclose(a, b,
rtol=state.controlpoint_relative_tolerance,
atol=state.controlpoint_absolute_tolerance)