| 1 |
{%MainUnit castlevectors.pas} |
| 2 |
{ |
| 3 |
Copyright 2003-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ CastleVectors routines (global functions, procedures) |
| 18 |
for Double precision of vectors and matrices. } |
| 19 |
|
| 20 |
{$ifdef read_interface} |
| 21 |
|
| 22 |
{ Intersection of three 3D planes, results in a single 3D point. |
| 23 |
If the intersection is not a single 3D point, result is undefined, |
| 24 |
so don't try to use this. |
| 25 |
@groupBegin } |
| 26 |
function ThreePlanesIntersectionPointDouble( |
| 27 |
const Plane0, Plane1, Plane2: TVector4Double): TVector3Double; overload; |
| 28 |
{ @groupEnd } |
| 29 |
|
| 30 |
{$endif read_interface} |
| 31 |
|
| 32 |
{$ifdef read_implementation} |
| 33 |
|
| 34 |
{ Unfortunately, this is an ugly copy-paste of a subset |
| 35 |
of castlevectors_records_single.inc now. |
| 36 |
As Delphi does not support, |
| 37 |
nor does it support generics using mathematical expressions |
| 38 |
parameterized with floats (see castlevectors_records_float.inc |
| 39 |
comments, you cannot "narrow" generics to something like "numer"), |
| 40 |
... there seems to be no better way now. |
| 41 |
|
| 42 |
Also, we need to rename functions to XxxDouble, to avoid FPC bugs |
| 43 |
with "duplicate ASM label", see |
| 44 |
https://bugs.freepascal.org/view.php?id=32188 |
| 45 |
} |
| 46 |
|
| 47 |
function MaxAbsVectorCoordDouble(const v: TVector3Double): integer; |
| 48 |
begin |
| 49 |
Result := 0; |
| 50 |
if Abs(v.Y) > Abs(v[Result]) then Result := 1; |
| 51 |
if Abs(v.Z) > Abs(v[Result]) then Result := 2; |
| 52 |
end; |
| 53 |
|
| 54 |
procedure TwoPlanesIntersectionLineDouble(const Plane0, Plane1: TVector4Double; |
| 55 |
out Line0, LineVector: TVector3Double); |
| 56 |
var |
| 57 |
Plane0Dir: TVector3Double absolute Plane0; |
| 58 |
Plane1Dir: TVector3Double absolute Plane1; |
| 59 |
NonZeroIndex, Index1, Index2: T3DAxis; |
| 60 |
PlaneWithNonZeroIndex1: PVector4Double; |
| 61 |
PlaneMultiply, Sum_Index2, Sum_3: Double; |
| 62 |
begin |
| 63 |
LineVector := TVector3Double.CrossProduct(Plane0Dir, Plane1Dir); |
| 64 |
|
| 65 |
NonZeroIndex := MaxAbsVectorCoordDouble(LineVector); |
| 66 |
if IsZero(LineVector[NonZeroIndex]) then |
| 67 |
raise EPlanesParallel.Create( |
| 68 |
'Unable to calculate intersection line of two planes ' + |
| 69 |
Plane0.ToRawString + ' and ' + Plane1.ToRawString + ' because ' + |
| 70 |
'planes are parallel'); |
| 71 |
|
| 72 |
{ Since LineVector[NonZeroIndex] <> 0, we know that we can find exactly |
| 73 |
one point on this line by assuming that Point[NonZeroIndex] = 0. } |
| 74 |
Line0.Data[NonZeroIndex] := 0; |
| 75 |
RestOf3DCoords(NonZeroIndex, Index1, Index2); |
| 76 |
|
| 77 |
{ Now we must solve |
| 78 |
Plane0[Index1] * Line0[Index1] + Plane0[Index2] * Line0[Index2] + Plane0.W = 0 |
| 79 |
Plane1[Index1] * Line0[Index1] + Plane1[Index2] * Line0[Index2] + Plane1.W = 0 |
| 80 |
We want to sum these two equations to eliminate Line0[Index1]: |
| 81 |
0 + Sum_Index2 * Line0[Index2] + Sum_3 = 0 |
| 82 |
} |
| 83 |
if not IsZero(Plane0[Index1]) then |
| 84 |
begin |
| 85 |
PlaneWithNonZeroIndex1 := @Plane0; |
| 86 |
PlaneMultiply := - Plane1[Index1] / Plane0[Index1]; |
| 87 |
Sum_Index2 := Plane0[Index2] * PlaneMultiply + Plane1[Index2]; |
| 88 |
Sum_3 := Plane0.W * PlaneMultiply + Plane1.W; |
| 89 |
end else |
| 90 |
if not IsZero(Plane1[Index1]) then |
| 91 |
begin |
| 92 |
PlaneWithNonZeroIndex1 := @Plane1; |
| 93 |
PlaneMultiply := - Plane0[Index1] / Plane1[Index1]; |
| 94 |
Sum_Index2 := Plane0[Index2] + Plane1[Index2] * PlaneMultiply; |
| 95 |
Sum_3 := Plane0.W + Plane1.W * PlaneMultiply; |
| 96 |
end else |
| 97 |
begin |
| 98 |
{ If Plane0[Index1] = Plane1[Index1] = 0, this is simple. |
| 99 |
Sum_Index2 := Plane0[Index2] + Plane1[Index2]; |
| 100 |
Sum_3 := Plane0.W + Plane1.W ; |
| 101 |
PlaneWithNonZeroIndex1 := ???; |
| 102 |
But it's useless, because then I will not be able to calculate |
| 103 |
Line0[Index1] (after obtaining Line0[Index2]). |
| 104 |
TODO -- some proof that this cannot happen for correct input ? } |
| 105 |
raise Exception.Create('Cannot calculate intersection line of two planes'); |
| 106 |
end; |
| 107 |
|
| 108 |
{ Now we know that |
| 109 |
Sum_Index2 * Line0[Index2] + Sum_3 = 0 |
| 110 |
Sum_Index2 must be <> 0, since we know that Line0[Index2] must be uniquely |
| 111 |
determined ? Right ? TODO -- I'm not sure, how to prove this simply ? |
| 112 |
} |
| 113 |
Line0.Data[Index2] := - Sum_3 / Sum_Index2; |
| 114 |
|
| 115 |
{ Note we have |
| 116 |
PlaneWithNonZeroIndex1^[Index1] * Line0[Index1] + |
| 117 |
PlaneWithNonZeroIndex1^[Index2] * Line0[Index2] + |
| 118 |
PlaneWithNonZeroIndex1^.W = 0 |
| 119 |
All is known except Line0[Index1], |
| 120 |
PlaneWithNonZeroIndex1^[Index1] is for sure <> 0. } |
| 121 |
Line0.Data[Index1] := - |
| 122 |
(PlaneWithNonZeroIndex1^[Index2] * Line0[Index2] + |
| 123 |
PlaneWithNonZeroIndex1^.W) / |
| 124 |
PlaneWithNonZeroIndex1^[Index1]; |
| 125 |
end; |
| 126 |
|
| 127 |
function TryPlaneLineIntersectionDouble(out t: Double; |
| 128 |
const plane: TVector4Double; const line0, lineVector: TVector3Double): boolean; overload; |
| 129 |
var |
| 130 |
PlaneDir: TVector3Double absolute plane; |
| 131 |
Dot: Double; |
| 132 |
begin |
| 133 |
Dot := TVector3Double.DotProduct(LineVector, PlaneDir); |
| 134 |
if not IsZero(Dot) then |
| 135 |
begin |
| 136 |
Result := true; |
| 137 |
t := -(plane.X*line0.X + plane.Y*line0.Y + plane.Z*line0.Z + plane.W)/Dot; |
| 138 |
end else |
| 139 |
Result := false; |
| 140 |
end; |
| 141 |
|
| 142 |
function TryPlaneLineIntersectionDouble(out intersection: TVector3Double; |
| 143 |
const plane: TVector4Double; const line0, lineVector: TVector3Double): boolean; overload; |
| 144 |
var |
| 145 |
T: Double; |
| 146 |
begin |
| 147 |
Result := TryPlaneLineIntersectionDouble(t, Plane, Line0, LineVector); |
| 148 |
if Result then Intersection := Line0 + LineVector * t; |
| 149 |
end; |
| 150 |
|
| 151 |
function ThreePlanesIntersectionPointDouble( |
| 152 |
const Plane0, Plane1, Plane2: TVector4Double): TVector3Double; |
| 153 |
var |
| 154 |
Line0, LineVector: TVector3Double; |
| 155 |
begin |
| 156 |
TwoPlanesIntersectionLineDouble(Plane0, Plane1, Line0, LineVector); |
| 157 |
if not TryPlaneLineIntersectionDouble(Result, Plane2, Line0, LineVector) then |
| 158 |
raise Exception.Create('Cannot calculate intersection point of three planes :' + |
| 159 |
'intersection line of first two planes is parallel to the 3rd plane'); |
| 160 |
end; |
| 161 |
|
| 162 |
{$endif read_implementation} |