0

Yes, I know that it is a popular problem. But I found nowhere the full clear implementing code without using OpenGL classes or a lot of headers files.

Okay, the math solution is to transfer ellipsoid to sphere. Then find intersections dots (if they exist of course) and make inverse transformation. Because affine transformation respect intersection.

But I have difficulties when trying to implement this.

I tried something for sphere but it is completely incorrect.

double CountDelta(Point X, Point Y, Sphere S) 
{       
    double a = 0.0;
    for(int i = 0; i < 3; i++){
        a += (Y._coordinates[i] - X._coordinates[i]) * (Y._coordinates[i] - X._coordinates[i]);
    }
        
    double b = 0.0;
    for(int i = 0; i < 3; i++) 
        b += (Y._coordinates[i] - X._coordinates[i]) * (X._coordinates[i] - S._coordinates[i]);
    b *= 2;
    
    double c = - S.r * S.r;
    for(int i = 0; i < 3; i++)
        c += (X._coordinates[i] - S._coordinates[i]) * (X._coordinates[i] - S._coordinates[i]);
    
        
    return b * b - 4 * a * c;
}

Let I have start point P = (Px, Py, Pz), direction V = (Vx, Vy, Vz), ellipsoid = (Ex, Ey, Ec) and (a, b, c). How to construct clear code?

DumbSimon
  • 65
  • 1
  • 7
  • 3
    What do you mean by 'completely incorrect'? The code which you have posted can be used to detect if a line is intersecting a sphere, but there is no code presented for the calculation of actual intersection points. – Alex Sveshnikov Jul 06 '22 at 08:19
  • Care to explain what the parameters describe. –  Jul 06 '22 at 09:54
  • see [Ray and ellipsoid intersection accuracy improvement](https://stackoverflow.com/q/25470493/2521214) its GLSL however it does not use any funny things just vector vec3/dvec3 as a container you can easily rewrite to struct or to array of floats/doubles instead ... or use directly mine [GLSL_math.h](https://ulozto.net/tamhle/Lac6wsTA65Ni#!ZJD0MwR2AJR0ZmR5LJAyZwHlLJH1MJyBAGL0I3MvJKDlF2L3LD==) which enables GLSL stuff inside CPU C++ code ... or use GLM or whatever else – Spektre Jul 07 '22 at 08:15

1 Answers1

2

Let a line from P to P + D intersecting a sphere of center C and radius R.

WLOG, C can be the origin and R unit (otherwise translate by -C and scale by 1/R). Now using the parametric equation of the line and the implicit equation of the sphere,

(Px + t Dx)² + (Py + t Dy)² + (Pz + t Dz)² = 1

or

(Dx² + Dy² + Dz²) t² + 2 (Dx Px + Dy Py + Dz Pz) t + Px² + Py² + Pz² - 1 = 0

(Vectorially, D² t² + 2 D P t + P² - 1 = 0 and t = (- D P ±√((D P)² - D²(P² - 1))) / D².)

Solve this quadratic equation for t and get the two intersections as P + t D. (Don't forget to invert the initial transformations.)


For the ellipsoid, you can either plug the parametric equation of the line directly into the implicit equation of the conic, or reduce the conic (and the points simultaneously) and plug in the reduced equation.