I have a number of points from which I want to interpolate a plane which I do as followed:
rng = np.random.default_rng()
x = rng.random(10) - 0.5
y = rng.random(10) - 0.5
z = np.hypot(x, y)
X = np.linspace(min(x), max(x))
Y = np.linspace(min(y), max(y))
X, Y = np.meshgrid(X, Y) # 2D grid for interpolation
interp = CloughTocher2DInterpolator(list(zip(x, y)), z)
I defined x, y and z as random for this example. x, y and z can also be sparse.
Now I want to be able to get an intersection from ths interpolation with a plane I've defined as x*a + y*b + c = z
(with a
, b
and c
variables)
I couldn't find anything to do this in scipy. I've tried getting all the points, getting a triangulation and using this for intersection (see: How to visualize 3D delaunay triangulation in Python?) but the triangulation I get is really bad. It doesn't connect at all to the closest points.
I've also tried using curve_fit but didn't have any luck since the interpolator might return nan values when you request values outside of the convex hull and then the optimizer seems to get stuck:
from scipy import optimize
def func(x, y):
z = uslope[0] * x + uslope[1] * y + uslope[2]
dist = interp(x, y) - z
dist[np.isnan(dist)] = np.Infinity
return dist
xdata = np.linspace(0, 1000, 50)
ydata = np.zeros(xdata.shape)
ydata.fill(500)
popt, pcov = optimize.curve_fit(func, xdata, ydata, p0=500, check_finite=True, method="dogbox")
print(popt)
print(pcov)
If it's complicated to do with CloughTocher2DInterpolator
I can also use another 2D interpolator that is continuous.