Einzelnen Beitrag anzeigen

Benutzerbild von milos
milos

Registriert seit: 14. Jul 2008
Ort: Bern (CH)
508 Beiträge
 
Delphi 11 Alexandria
 
#1

3D Ray-Plane Intersection

  Alt 29. Apr 2016, 09:10
Hallo,

ich habe hier im Forum diese funktion gefunden um den Schnittpunkt eines Graden und einer Form im 3D Raum zu berechnen.

Delphi-Quellcode:
function intersectPlaneLine(const planeA, planeB, planeC: TPoint3D;
  const lineA, lineB: TPoint3D): TPoint3D;
var
  AB, AC: TPoint3D;
  planeNormal: TPoint3D;
  DE: TPoint3D;
  d, t: Single;
  divisor: Single;
begin
  // 1a
  AB := Point3D(planeB.X - planeA.X, planeB.Y - planeA.Y, planeB.Z - planeA.Z);
  AC := Point3D(planeC.X - planeA.X, planeC.Y - planeA.Y, planeC.Z - planeA.Z);
  // 1b
  // Kreuzprodukt (siehe Wikipedia, sofern nicht verständlich)
  planeNormal := Point3D(AB.Y*AC.Z - AB.Z*AC.Y, AB.Z*AC.X - AB.X*AC.Z, AB.X*AC.Y - AB.Y*AC.X);
  d := planeA.X*planeNormal.X + planeA.Y*planeNormal.Y + planeA.Z*planeNormal.Z;
  // 2a
  DE := Point3D(lineB.X - lineA.X, lineB.Y - lineA.Y, lineB.Z - lineA.Z);
  // 3
  divisor := planeNormal.X*DE.X + planeNormal.Y*DE.Y + planeNormal.Z*DE.Z;
  if divisor = 0 then
    // die Gerade steht 90° zur Ebenennormale --> sie ist parallel
    // Lösung: keine oder unendlich viele - wir liefern niks
    Result := Point3D(0, 0, 0)
  else
  begin
    t := (d - planeNormal.X*lineA.X - planeNormal.Y*lineA.Y - planeNormal.Z*lineA.Z) / divisor;
    // 4
    Result := Point3D(lineA.X + t*DE.X, lineA.Y + t*DE.Y, lineA.Z + t*DE.Z);
  end;
end;
Diese funktioniert einwandfrei, jedoch gibt es mir auch Werte aus wenn kein Schnittpunkt dazwischen liegt.
z.B. das:

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  LPoint : TPoint3D;
begin
  LPoint := intersectPlaneLine(
    Point3d(100,100,100),
    Point3d(200,200,100),
    Point3d(200,100,100),
    Point3d(0,0,-1000),
    Point3d(0,0,1000)
  )
end;
bekomme ich Point3D(0,0,100) zurück obwohl da gar nichts liegen sollte? Komisch ist das die Z Achse korrekt ist. Wo ist da der Wurm drin?

MfG
Milos
  Mit Zitat antworten Zitat