I have a list of points in 3D space $(x_{Ci}, y_{Ci}, z_{Ci})$. These points are in the "C" coordinate system.
I want to fit a shape (an egg) into these points, the shape is described by the following implicit equation:
$x_E^2+y_E^2+z_E^2=b^2-a^2(x_E^2+y_E^2+z_E^2)+2*a*z_E*\sqrt{x_E^2+y_E^2+z_E^2}$
(the subscript E indicating that this is the Egg coordinate system)
The shape has two parameters, $a$ and $b$. The goal is to determine the "best" $a$ and $b$. ("Best" as in least square fit for example, but other approximations are also possible).
This is where it gets complicated. The "best" fit should respect translation and also rotation. That means that the implicit equation cannot be used as-is, but also has to include translation (expressed through $x_{C0}$, $y_{C0}$ and $z_{C0}$ and rotation around the X-axis and Z-Axis (I'll call the rotation angles $k_x$ and $k_z$).
I came up with the following equations for calculating a point in the E coordinate system from a point in the C system.
$x_E = x_{C} \cos{\left(k_{Z} \right)} - \left(y_{C} \cos{\left(k_{X} \right)} - z_{C} \sin{\left(k_{X} \right)}\right) \sin{\left(k_{Z} \right)} + x_{C0}$
$y_E = x_{C} \sin{\left(k_{Z} \right)} + \left(y_{C} \cos{\left(k_{X} \right)} - z_{C} \sin{\left(k_{X} \right)}\right) \cos{\left(k_{Z} \right)} + y_{C0} $
$z_E = y_{C} \sin{\left(k_{X} \right)} + z_{C} \cos{\left(k_{X} \right)} + z_{C0}$
For doing the actual fitting, I tried following the approach described in this article by Charles Jekel, where he fitted a sphere into data points https://jekel.me/2015/Least-Squares-Sphere-Fit/
After inserting the equations for $x_E$, $y_E$ and $z_E$ into the first implicit equation, I brought it up to the matrix form $f=A*c$ (as done in the article). These are the values I came up with:
$f=\begin{bmatrix}x_{Ci}^2+y_{Ci}^2+z_{Ci}^2 \\ x_ {Ci+1}^2+y_{Ci+1}^2+z_{Ci+1}^2 \\ ...\end{bmatrix}$
$A=\begin{bmatrix} x_{Ci} & y_{Ci} & z_{Ci} & 1 \\ x_{Ci+1} & y_{Ci+1} & z_{Ci+1} & 1 \\ ... & ... & ... & ... \end{bmatrix}$
$c=\begin{bmatrix} -2*x_{C0}*\cos{k_z}-2*y_{c0}*\sin{k_z} \\ -2*y_{C0}*\cos{k_x}*\cos{k_z}+2*x_{C0}*\sin{k_z}*cos{k_x}-2*z_{C0}*\sin{k_x} \\ -2*z_{C0}*\cos{k_x}-2*x_{C0}*\sin{k_x}*\sin{k_z}+2*y_{C0}*\sin{k_x}*cos{k_z} \\ -x_{C0}^2-y_{C0}^2-z_{C0}^2+b^2-a^2*(x_E^2+y_E^2+z_E^2)+2*a*z_E*\sqrt{(x_E^2+y_E^2+z_E^2)} \end{bmatrix}$
While this will give me numerical values for the entries of $c$, I see no way to calculate the unknowns from that. Four equations, but seven unknowns: $a$, $b$, $k_x$, $k_z$, $x_{C0}$, $y_{C0}$ and $z_{C0}$ (although I am only interested in knowing $a$ and $b$.
I'd appreciate any ideas how I could make this work.