Short
I need to find an optimized way to build a 2D numpy array representing a 3D plane (namely storing its Z-values) given three points (to mathematically define the plane) and the desired array size.
Details
Given three points p0, p1, p2, and a size variable:
import numpy as np
import pylab as plt
p0 = [0.2, -0.4, -0.2]
p1 = [-0.6, 0.1, -0.8]
p2 = [-0.1, 0.3, -0.6]
size = (12000, 17000)
The following function shows the idea of how to build a plane out of these 3 points:
def plane(p0, p1, p2, size):
p0 = np.array(p0)
p1 = np.array(p1)
p2 = np.array(p2)
ux, uy, uz = [p1[0] - p0[0], p1[1] - p0[1], p1[2] - p0[2]]
vx, vy, vz = [p2[0] - p0[0], p2[1] - p0[1], p2[2] - p0[2]]
uv = [uy * vz - uz * vy,
uz * vx - ux * vz,
ux * vy - uy * vx]
norm = np.array(uv)
d = -np.array(p0).dot(norm)
xx, yy = np.meshgrid(range(size[1]), range(size[0]))
z = np.array((-norm[0] * xx - norm[1] * yy - d) * 1. / norm[2])
return(z)
The resulting z looks like this:
This implementation eats my memory in a few seconds:
Question
How would you optimize this to achieve the exact same result with the lowest memory usage as possible (being fast is also not optional...)?
Note: size may be 10x larger in both dimensions -> which obviously crashes my Python on a mainstream 2016 laptop.
(Using the most common scientific libraries is perfectly fine)
I'm using Python 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0] on Ubuntu linux 18.04.5.

