Suppose we have 4 points say A($x_1,y_1,z_1$), B($x_2,y_2,z_2$),C($x_3,y_3,z_3$), D($x_4,y_4,z_4$). Where $x_1,x_2,x_3,x_4,y_1,y_2,y_3,y_4$ are known points and rest $z_1,z_2,z_3,z_4$ are unknown points .
$4$ points will make a rectangle we want to find the corresponding Z - Coordinate using optimization .
We can make 4 equations as
$AD^2 = AB^2$
$$(z_4- z_1)^2 - (z_3 - z_2)^2 = (x_3 - x_2)^2 + (y_3 - y_2)^2 - (x_4 - x_1)^2 - (y_4 - y_1)^2 $$$AB^2 = CD^2$
$$(z_2- z_1)^2 - (z_3 - z_4)^2 = (x_3 - x_4)^2 + (y_3 - y_4)^2 - (x_2 - x_1)^2 - (y_2 - y_1)^2 $$(AB perpendicular to AD) $$(z_2- z_1) (z_4 - z_1) = -[(x_2 - x_1)(x_4 - x_1) + (y_2 - y_1)(y_4 - y_1)] $$
(BC perpendicular to CD) $$(z_2- z_3) (z_4 - z_3) = -[(x_2 - x_3)(x_4 - x_3) + (y_2 - y_3)(y_4 - y_3)] $$
Objective equation can be to minimize the area of rectangle i.e. $$Minimize (AB.AD) $$ $$i.e (Length* breadth)$$
How to solve this optimization so that we can we find other $4$ z-coordinate (i.e. $z_1, z_2, z_3, z_4$) corresponding to other 4 point .
Given four points $p_1,p_2,p_3,p_4$ if they pertain to a rectangle then they are contained into the same plane $\pi$. Also the coordinates $x,y$ in each point, represent the $XY$ plane projection which should be a parallelogram. Assuming this projection is not a line (degenerate case) from this projection we can select a sequence to form a rectangle. From here we assume that the sequence $p_1\to p_2\to p_3\to p_4\to p_1$ gives us a rectangle. Curiously, only three of $z_1,z_2,z_3,z_4$ are independent so we assume $z_4 = z_1+z_3-2z_2$. If the objective is to determine the minimum perimeter rectangle then the optimization problem
$$ \min_{z_i}|p_1-p_2|+|p_2-p_3|+|p_3-p_4|+|p_4-p_1| \ \ \ \text{s. t.} \ \ \cases{(p_1-p_2)\cdot(p_2-p_3)=0\\ (p_2-p_3)\cdot(p_4-p_3)=0\\ (p_4-p_3)\cdot(p_4-p_1)=0 } $$
comes with the solution.
Follows a MATHEMATICA script to accomplish with the calculations
NOTE
To obtain the minimum area use instead
In python a possible solution script could be