GLS.GeometryBB.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. //
  2. // The graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLS.GeometryBB;
  5. (* Calculations and manipulations on Bounding Boxes *)
  6. interface
  7. {$I Scenario.inc}
  8. uses
  9. System.SysUtils,
  10. GLS.VectorGeometry,
  11. GLS.VectorLists,
  12. GLS.VectorTypes;
  13. type
  14. // Structure for storing Bounding Boxes
  15. PHmgBoundingBox = ^THmgBoundingBox;
  16. THmgBoundingBox = record
  17. BBox: array [0 .. 7] of TGLVector;
  18. end;
  19. // Structure for storing Axis Aligned Bounding Boxes
  20. TAABB = record
  21. Min, Max: TAffineVector;
  22. end;
  23. PAABB = ^TAABB;
  24. // Structure for storing BoundingSpheres. Similar to TAABB
  25. TBSphere = record
  26. // Center of Bounding Sphere
  27. Center: TAffineVector;
  28. // Radius of Bounding Sphere
  29. Radius: Single;
  30. end;
  31. TGLClipRect = record
  32. Left, Top: Single;
  33. Right, Bottom: Single;
  34. end;
  35. (* Result type for space intersection tests, like AABBContainsAABB or
  36. BSphereContainsAABB *)
  37. TSpaceContains = (ScNoOverlap, ScContainsFully, ScContainsPartially);
  38. // Structure for storing the corners of an AABB, used with ExtractAABBCorners
  39. TAABBCorners = array [0 .. 7] of TAffineVector;
  40. const
  41. NullBoundingBox: THmgBoundingBox =
  42. (BBox:((X: 0; Y: 0; Z: 0; W: 1),
  43. (X: 0; Y: 0; Z: 0; W: 1),
  44. (X: 0; Y: 0; Z: 0; W: 1),
  45. (X: 0; Y: 0; Z: 0; W: 1),
  46. (X: 0; Y: 0; Z: 0; W: 1),
  47. (X: 0; Y: 0; Z: 0; W: 1),
  48. (X: 0; Y: 0; Z: 0; W: 1),
  49. (X: 0; Y: 0; Z: 0; W: 1)));
  50. // ------------------------------------------------------------------------------
  51. // Bounding Box functions
  52. // ------------------------------------------------------------------------------
  53. function BoundingBoxesAreEqual(const ABoundingBox1, ABoundingBox2: THmgBoundingBox): Boolean; overload;
  54. function BoundingBoxesAreEqual(const ABoundingBox1, ABoundingBox2: PHmgBoundingBox): Boolean; overload;
  55. (* Adds a BB into another BB.
  56. The original BB (c1) is extended if necessary to contain c2. *)
  57. function AddBB(var C1: THmgBoundingBox; const C2: THmgBoundingBox): THmgBoundingBox;
  58. procedure AddAABB(var Aabb: TAABB; const Aabb1: TAABB);
  59. procedure SetBB(var C: THmgBoundingBox; const V: TGLVector);
  60. procedure SetAABB(var Bb: TAABB; const V: TGLVector); inline;
  61. procedure BBTransform(var C: THmgBoundingBox; const M: TGLMatrix);
  62. procedure AABBTransform(var Bb: TAABB; const M: TGLMatrix);
  63. procedure AABBScale(var Bb: TAABB; const V: TAffineVector);
  64. function BBMinX(const C: THmgBoundingBox): Single;
  65. function BBMaxX(const C: THmgBoundingBox): Single;
  66. function BBMinY(const C: THmgBoundingBox): Single;
  67. function BBMaxY(const C: THmgBoundingBox): Single;
  68. function BBMinZ(const C: THmgBoundingBox): Single;
  69. function BBMaxZ(const C: THmgBoundingBox): Single;
  70. // Resize the AABB if necessary to include p.
  71. procedure AABBInclude(var Bb: TAABB; const P: TAffineVector);
  72. // Make an AABB that is formed by sweeping a sphere (or AABB) from Start to Dest
  73. procedure AABBFromSweep(var SweepAABB: TAABB; const Start, Dest: TGLVector; const Radius: Single);
  74. (* Returns the intersection AABB of two AABBs.
  75. If the AABBs don't intersect, will return a degenerated AABB (plane, line or point). *)
  76. function AABBIntersection(const Aabb1, Aabb2: TAABB): TAABB;
  77. // Extract AABB information from a BB.
  78. function BBToAABB(const ABB: THmgBoundingBox): TAABB;
  79. // Converts an AABB to its canonical BB.
  80. function AABBToBB(const AnAABB: TAABB): THmgBoundingBox; overload;
  81. // Transforms an AABB to a BB.
  82. function AABBToBB(const AnAABB: TAABB; const M: TGLMatrix): THmgBoundingBox; overload;
  83. // Adds delta to min and max of the AABB.
  84. procedure OffsetAABB(var Aabb: TAABB; const Delta: TAffineVector); overload;
  85. procedure OffsetAABB(var Aabb: TAABB; const Delta: TGLVector); overload;
  86. // Adds delta to min and max of the BB.
  87. procedure OffsetBB(var Bb: THmgBoundingBox; const Delta: TAffineVector); overload;
  88. procedure OffsetBB(var Bb: THmgBoundingBox; const Delta: TGLVector); overload;
  89. // The same as above but uses AddPoint() instead of AddVector().
  90. procedure OffsetBBPoint(var Bb: THmgBoundingBox; const Delta: TGLVector); overload;
  91. (* Determines if two AxisAlignedBoundingBoxes intersect.
  92. The matrices are the ones that convert one point to the other's AABB system *)
  93. function IntersectAABBs(const Aabb1, Aabb2: TAABB; const M1To2, M2To1: TGLMatrix): Boolean; overload;
  94. // Checks whether two Bounding boxes aligned with the world axes collide in the XY plane.
  95. function IntersectAABBsAbsoluteXY(const Aabb1, Aabb2: TAABB): Boolean;
  96. // Checks whether two Bounding boxes aligned with the world axes collide in the XZ plane.
  97. function IntersectAABBsAbsoluteXZ(const Aabb1, Aabb2: TAABB): Boolean;
  98. // Checks whether two Bounding boxes aligned with the world axes collide.
  99. function IntersectAABBsAbsolute(const Aabb1, Aabb2: TAABB): Boolean;
  100. (* Checks whether one Bounding box aligned with the world axes fits within
  101. another Bounding box *)
  102. function AABBFitsInAABBAbsolute(const Aabb1, Aabb2: TAABB): Boolean;
  103. // Checks if a point "p" is inside an AABB
  104. function PointInAABB(const P: TAffineVector; const Aabb: TAABB): Boolean; overload;
  105. function PointInAABB(const P: TGLVector; const Aabb: TAABB): Boolean; overload;
  106. // Checks if a plane (given by the normal+d) intersects the AABB
  107. function PlaneIntersectAABB(const Normal: TAffineVector; D: Single; const Aabb: TAABB): Boolean;
  108. // Compute the intersection between a plane and the AABB
  109. function PlaneAABBIntersection(const plane : THmgPlane; const AABB : TAABB) : TGLAffineVectorList;
  110. (*
  111. Original source code by Tomas Akenine-Möller
  112. Based on the paper "Fast 3D Triangle-Box Overlap Testing"
  113. http://www.cs.lth.se/home/Tomas_Akenine_Moller/pubs/tribox.pdf
  114. http://jgt.akpeters.com/papers/AkenineMoller01/ (code)
  115. Use separating axis theorem to test overlap between triangle and box
  116. need to test for overlap in these directions:
  117. 1) the (x,y,z)-directions (actually, since we use the AABB of the triangle
  118. we do not even need to test these)
  119. 2) normal of the triangle
  120. 3) crossproduct(edge from tri, {x,y,z}-directin)
  121. this gives 3x3=9 more tests
  122. Checks if a triangle (given by vertices v1, v2 and v3) intersects an AABB
  123. *)
  124. function TriangleIntersectAABB(const Aabb: TAABB; const V1, V2, V3: TAffineVector): Boolean;
  125. // Extract the corners from an AABB
  126. procedure ExtractAABBCorners(const AABB: TAABB; var AABBCorners: TAABBCorners);
  127. // Convert an AABB to a BSphere
  128. procedure AABBToBSphere(const AABB: TAABB; var BSphere: TBSphere);
  129. // Convert a BSphere to an AABB
  130. procedure BSphereToAABB(const BSphere: TBSphere; var AABB: TAABB); overload;
  131. function BSphereToAABB(const Center: TAffineVector; Radius: Single): TAABB; overload;
  132. function BSphereToAABB(const Center: TGLVector; Radius: Single): TAABB; overload;
  133. // Determines to which extent one AABB contains another AABB
  134. function AABBContainsAABB(const MainAABB, TestAABB: TAABB): TSpaceContains;
  135. // Determines to which extent a BSphere contains an AABB
  136. function BSphereContainsAABB(const MainBSphere: TBSphere; const TestAABB: TAABB): TSpaceContains;
  137. // Determines to which extent one BSphere contains another BSphere
  138. function BSphereContainsBSphere(const MainBSphere, TestBSphere: TBSphere): TSpaceContains;
  139. // Determines to which extent an AABB contains a BSpher
  140. function AABBContainsBSphere(const MainAABB: TAABB; const TestBSphere: TBSphere): TSpaceContains;
  141. // Determines to which extent a plane contains a BSphere
  142. function PlaneContainsBSphere(const Location, Normal: TAffineVector;
  143. const TestBSphere: TBSphere): TSpaceContains;
  144. // Determines to which extent a frustum contains a BSphere
  145. function FrustumContainsBSphere(const Frustum: TFrustum;
  146. const TestBSphere: TBSphere): TSpaceContains;
  147. // Determines to which extent a frustum contains an AABB
  148. function FrustumContainsAABB(const Frustum: TFrustum; const TestAABB: TAABB): TSpaceContains;
  149. // Clips a position to an AABB
  150. function ClipToAABB(const V: TAffineVector; const AABB: TAABB): TAffineVector;
  151. // Determines if one BSphere intersects another BSphere
  152. function BSphereIntersectsBSphere(const MainBSphere, TestBSphere: TBSphere): Boolean;
  153. // Extend the clip rect to include given coordinate.
  154. procedure IncludeInClipRect(var ClipRect: TGLClipRect; X, Y: Single);
  155. // Projects an AABB and determines the extent of its projection as a clip rect.
  156. function AABBToClipRect(const Aabb: TAABB; const ModelViewProjection: TGLMatrix;
  157. ViewportSizeX, ViewportSizeY: Integer): TGLClipRect;
  158. // Finds the intersection between a ray and an axis aligned bounding box.
  159. function RayCastAABBIntersect(const RayOrigin, RayDirection: TGLVector;
  160. const Aabb: TAABB; out TNear, TFar: Single): Boolean; overload;
  161. function RayCastAABBIntersect(const RayOrigin, RayDirection: TGLVector;
  162. const Aabb: TAABB; IntersectPoint: PGLVector = nil): Boolean; overload;
  163. type
  164. TPlanIndices = array [0 .. 3] of Integer;
  165. TPlanBB = array [0 .. 5] of TPlanIndices;
  166. TDirPlan = array [0 .. 5] of Integer;
  167. const
  168. CBBFront: TPlanIndices = (0, 1, 2, 3);
  169. CBBBack: TPlanIndices = (4, 5, 6, 7);
  170. CBBLeft: TPlanIndices = (0, 4, 7, 3);
  171. CBBRight: TPlanIndices = (1, 5, 6, 2);
  172. CBBTop: TPlanIndices = (0, 1, 5, 4);
  173. CBBBottom: TPlanIndices = (2, 3, 7, 6);
  174. CBBPlans: TPlanBB = ((0, 1, 2, 3), (4, 5, 6, 7), (0, 4, 7, 3), (1, 5, 6, 2),
  175. (0, 1, 5, 4), (2, 3, 7, 6));
  176. CDirPlan: TDirPlan = (0, 0, 1, 1, 2, 2);
  177. // --------------------------------------------------------------
  178. implementation
  179. // --------------------------------------------------------------
  180. // ------------------------------------------------------------------------------
  181. // ----------------- BB functions -------------------------------------------
  182. // ------------------------------------------------------------------------------
  183. procedure SetPlanBB(var BB: THmgBoundingBox; const NumPlan: Integer;
  184. const Valeur: Double);
  185. var
  186. I: Integer;
  187. begin
  188. for I := 0 to 3 do
  189. begin
  190. BB.BBox[CBBPlans[NumPlan][I]].V[CDirPlan[NumPlan]] := Valeur;
  191. BB.BBox[CBBPlans[NumPlan][I]].W := 1;
  192. end;
  193. end;
  194. function BoundingBoxesAreEqual(const ABoundingBox1, ABoundingBox2
  195. : THmgBoundingBox): Boolean;
  196. begin
  197. Result := CompareMem(@ABoundingBox1, @ABoundingBox2, SizeOf(THmgBoundingBox));
  198. end;
  199. function BoundingBoxesAreEqual(const ABoundingBox1, ABoundingBox2
  200. : PHmgBoundingBox): Boolean;
  201. begin
  202. Result := CompareMem(ABoundingBox1, ABoundingBox2, SizeOf(THmgBoundingBox));
  203. end;
  204. function AddBB(var C1: THmgBoundingBox; const C2: THmgBoundingBox): THmgBoundingBox;
  205. var
  206. I, J: Integer;
  207. begin
  208. for I := 0 to 7 do
  209. begin
  210. for J := 0 to 3 do
  211. if C1.BBox[CBBFront[J]].X < C2.BBox[I].X then
  212. SetPlanBB(C1, 0, C2.BBox[I].X);
  213. for J := 0 to 3 do
  214. if C1.BBox[CBBBack[J]].X > C2.BBox[I].X then
  215. SetPlanBB(C1, 1, C2.BBox[I].X);
  216. for J := 0 to 3 do
  217. if C1.BBox[CBBLeft[J]].Y < C2.BBox[I].Y then
  218. SetPlanBB(C1, 2, C2.BBox[I].Y);
  219. for J := 0 to 3 do
  220. if C1.BBox[CBBRight[J]].Y > C2.BBox[I].Y then
  221. SetPlanBB(C1, 3, C2.BBox[I].Y);
  222. for J := 0 to 3 do
  223. if C1.BBox[CBBTop[J]].Z < C2.BBox[I].Z then
  224. SetPlanBB(C1, 4, C2.BBox[I].Z);
  225. for J := 0 to 3 do
  226. if C1.BBox[CBBBottom[J]].Z > C2.BBox[I].Z then
  227. SetPlanBB(C1, 5, C2.BBox[I].Z);
  228. end;
  229. Result := C1;
  230. end;
  231. procedure AddAABB(var Aabb: TAABB; const Aabb1: TAABB);
  232. begin
  233. if Aabb1.Min.X < Aabb.Min.X then
  234. Aabb.Min.X := Aabb1.Min.X;
  235. if Aabb1.Min.Y < Aabb.Min.Y then
  236. Aabb.Min.Y := Aabb1.Min.Y;
  237. if Aabb1.Min.Z < Aabb.Min.Z then
  238. Aabb.Min.Z := Aabb1.Min.Z;
  239. if Aabb1.Max.X > Aabb.Max.X then
  240. Aabb.Max.X := Aabb1.Max.X;
  241. if Aabb1.Max.Y > Aabb.Max.Y then
  242. Aabb.Max.Y := Aabb1.Max.Y;
  243. if Aabb1.Max.Z > Aabb.Max.Z then
  244. Aabb.Max.Z := Aabb1.Max.Z;
  245. end;
  246. procedure SetBB(var C: THmgBoundingBox; const V: TGLVector);
  247. begin
  248. SetPlanBB(C, 0, V.X);
  249. SetPlanBB(C, 1, -V.X);
  250. SetPlanBB(C, 2, V.Y);
  251. SetPlanBB(C, 3, -V.Y);
  252. SetPlanBB(C, 4, V.Z);
  253. SetPlanBB(C, 5, -V.Z);
  254. end;
  255. procedure SetAABB(var Bb: TAABB; const V: TGLVector);
  256. begin
  257. Bb.Max.X := Abs(V.X);
  258. Bb.Max.Y := Abs(V.Y);
  259. Bb.Max.Z := Abs(V.Z);
  260. Bb.Min.X := -Bb.Max.X;
  261. Bb.Min.Y := -Bb.Max.Y;
  262. Bb.Min.Z := -Bb.Max.Z;
  263. end;
  264. procedure BBTransform(var C: THmgBoundingBox; const M: TGLMatrix);
  265. var
  266. I: Integer;
  267. begin
  268. for I := 0 to 7 do
  269. C.BBox[I] := VectorTransform(C.BBox[I], M);
  270. end;
  271. procedure AABBTransform(var Bb: TAABB; const M: TGLMatrix);
  272. var
  273. OldMin, OldMax: TAffineVector;
  274. begin
  275. OldMin := Bb.Min;
  276. OldMax := Bb.Max;
  277. Bb.Min := VectorTransform(OldMin, M);
  278. Bb.Max := Bb.Min;
  279. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMin.X,
  280. OldMin.Y, OldMax.Z), M));
  281. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMin.X,
  282. OldMax.Y, OldMin.Z), M));
  283. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMin.X,
  284. OldMax.Y, OldMax.Z), M));
  285. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMax.X,
  286. OldMin.Y, OldMin.Z), M));
  287. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMax.X,
  288. OldMin.Y, OldMax.Z), M));
  289. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMax.X,
  290. OldMax.Y, OldMin.Z), M));
  291. AABBInclude(Bb, VectorTransform(OldMax, M));
  292. end;
  293. procedure AABBScale(var Bb: TAABB; const V: TAffineVector);
  294. begin
  295. ScaleVector(Bb.Min, V);
  296. ScaleVector(Bb.Max, V);
  297. end;
  298. function BBMinX(const C: THmgBoundingBox): Single;
  299. var
  300. I: Integer;
  301. begin
  302. Result := C.BBox[0].X;
  303. for I := 1 to 7 do
  304. Result := MinFloat(Result, C.BBox[I].X);
  305. end;
  306. function BBMaxX(const C: THmgBoundingBox): Single;
  307. var
  308. I: Integer;
  309. begin
  310. Result := C.BBox[0].X;
  311. for I := 1 to 7 do
  312. Result := MaxFloat(Result, C.BBox[I].X);
  313. end;
  314. function BBMinY(const C: THmgBoundingBox): Single;
  315. var
  316. I: Integer;
  317. begin
  318. Result := C.BBox[0].Y;
  319. for I := 1 to 7 do
  320. Result := MinFloat(Result, C.BBox[I].Y);
  321. end;
  322. function BBMaxY(const C: THmgBoundingBox): Single;
  323. var
  324. I: Integer;
  325. begin
  326. Result := C.BBox[0].Y;
  327. for I := 1 to 7 do
  328. Result := MaxFloat(Result, C.BBox[I].Y);
  329. end;
  330. function BBMinZ(const C: THmgBoundingBox): Single;
  331. var
  332. I: Integer;
  333. begin
  334. Result := C.BBox[0].Z;
  335. for I := 1 to 7 do
  336. Result := MinFloat(Result, C.BBox[I].Z);
  337. end;
  338. function BBMaxZ(const C: THmgBoundingBox): Single;
  339. var
  340. I: Integer;
  341. begin
  342. Result := C.BBox[0].Z;
  343. for I := 1 to 7 do
  344. Result := MaxFloat(Result, C.BBox[I].Z);
  345. end;
  346. procedure AABBInclude(var Bb: TAABB; const P: TAffineVector);
  347. begin
  348. if P.X < Bb.Min.X then
  349. Bb.Min.X := P.X;
  350. if P.X > Bb.Max.X then
  351. Bb.Max.X := P.X;
  352. if P.Y < Bb.Min.Y then
  353. Bb.Min.Y := P.Y;
  354. if P.Y > Bb.Max.Y then
  355. Bb.Max.Y := P.Y;
  356. if P.Z < Bb.Min.Z then
  357. Bb.Min.Z := P.Z;
  358. if P.Z > Bb.Max.Z then
  359. Bb.Max.Z := P.Z;
  360. end;
  361. procedure AABBFromSweep(var SweepAABB: TAABB; const Start, Dest: TGLVector;
  362. const Radius: Single);
  363. begin
  364. if Start.X < Dest.X then
  365. begin
  366. SweepAABB.Min.X := Start.X - Radius;
  367. SweepAABB.Max.X := Dest.X + Radius;
  368. end
  369. else
  370. begin
  371. SweepAABB.Min.X := Dest.X - Radius;
  372. SweepAABB.Max.X := Start.X + Radius;
  373. end;
  374. if Start.Y < Dest.Y then
  375. begin
  376. SweepAABB.Min.Y := Start.Y - Radius;
  377. SweepAABB.Max.Y := Dest.Y + Radius;
  378. end
  379. else
  380. begin
  381. SweepAABB.Min.Y := Dest.Y - Radius;
  382. SweepAABB.Max.Y := Start.Y + Radius;
  383. end;
  384. if Start.Z < Dest.Z then
  385. begin
  386. SweepAABB.Min.Z := Start.Z - Radius;
  387. SweepAABB.Max.Z := Dest.Z + Radius;
  388. end
  389. else
  390. begin
  391. SweepAABB.Min.Z := Dest.Z - Radius;
  392. SweepAABB.Max.Z := Start.Z + Radius;
  393. end;
  394. end;
  395. function AABBIntersection(const Aabb1, Aabb2: TAABB): TAABB;
  396. var
  397. I: Integer;
  398. begin
  399. for I := 0 to 2 do
  400. begin
  401. Result.Min.V[I] := MaxFloat(Aabb1.Min.V[I], Aabb2.Min.V[I]);
  402. Result.Max.V[I] := MinFloat(Aabb1.Max.V[I], Aabb2.Max.V[I]);
  403. end;
  404. end;
  405. function BBToAABB(const ABB: THmgBoundingBox): TAABB;
  406. var
  407. I: Integer;
  408. begin
  409. SetVector(Result.Min, ABB.BBox[0]);
  410. SetVector(Result.Max, ABB.BBox[0]);
  411. for I := 1 to 7 do
  412. begin
  413. if ABB.BBox[I].X < Result.Min.X then
  414. Result.Min.X := ABB.BBox[I].X;
  415. if ABB.BBox[I].X > Result.Max.X then
  416. Result.Max.X := ABB.BBox[I].X;
  417. if ABB.BBox[I].Y < Result.Min.Y then
  418. Result.Min.Y := ABB.BBox[I].Y;
  419. if ABB.BBox[I].Y > Result.Max.Y then
  420. Result.Max.Y := ABB.BBox[I].Y;
  421. if ABB.BBox[I].Z < Result.Min.Z then
  422. Result.Min.Z := ABB.BBox[I].Z;
  423. if ABB.BBox[I].Z > Result.Max.Z then
  424. Result.Max.Z := ABB.BBox[I].Z;
  425. end;
  426. end;
  427. function AABBToBB(const AnAABB: TAABB): THmgBoundingBox;
  428. begin
  429. with AnAABB do
  430. begin
  431. SetPlanBB(Result, 0, Max.X);
  432. SetPlanBB(Result, 1, Min.X);
  433. SetPlanBB(Result, 2, Max.Y);
  434. SetPlanBB(Result, 3, Min.Y);
  435. SetPlanBB(Result, 4, Max.Z);
  436. SetPlanBB(Result, 5, Min.Z);
  437. end;
  438. end;
  439. function AABBToBB(const AnAABB: TAABB; const M: TGLMatrix): THmgBoundingBox;
  440. begin
  441. Result := AABBToBB(AnAABB);
  442. BBTransform(Result, M);
  443. end;
  444. procedure OffsetAABB(var Aabb: TAABB; const Delta: TAffineVector);
  445. begin
  446. AddVector(Aabb.Min, Delta);
  447. AddVector(Aabb.Max, Delta);
  448. end;
  449. procedure OffsetAABB(var Aabb: TAABB; const Delta: TGLVector);
  450. begin
  451. AddVector(Aabb.Min, Delta);
  452. AddVector(Aabb.Max, Delta);
  453. end;
  454. procedure OffsetBB(var Bb: THmgBoundingBox; const Delta: TAffineVector);
  455. var
  456. I: Integer;
  457. TempVector: TGLVector;
  458. begin
  459. TempVector := VectorMake(Delta, 0);
  460. for I := 0 to 7 do
  461. AddVector(Bb.BBox[I], TempVector);
  462. end;
  463. procedure OffsetBB(var Bb: THmgBoundingBox; const Delta: TGLVector);
  464. var
  465. I: Integer;
  466. begin
  467. for I := 0 to 7 do
  468. AddVector(Bb.BBox[I], Delta);
  469. end;
  470. procedure OffsetBBPoint(var Bb: THmgBoundingBox; const Delta: TGLVector);
  471. var
  472. I: Integer;
  473. begin
  474. for I := 0 to 7 do
  475. AddPoint(Bb.BBox[I], Delta);
  476. end;
  477. function IntersectAABBs(const Aabb1, Aabb2: TAABB;
  478. const M1To2, M2To1: TGLMatrix): Boolean;
  479. const
  480. CWires: array [0 .. 11, 0 .. 1] of Integer // Points of the wire
  481. = ((0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (0, 4),
  482. (1, 5), (2, 6), (3, 7));
  483. CPlanes: array [0 .. 5, 0 .. 3] of Integer // points of the planes
  484. = ((1, 2, 6, 5), (2, 3, 7, 6), (0, 1, 2, 3), (0, 3, 7, 4), (0, 1, 5, 4),
  485. (5, 6, 7, 4));
  486. procedure MakeAABBPoints(const AABB: TAABB; var Pt: array of TVertex);
  487. begin
  488. with AABB do
  489. begin
  490. SetVector(Pt[0], Min.X, Min.Y, Min.Z);
  491. SetVector(Pt[1], Max.X, Min.Y, Min.Z);
  492. SetVector(Pt[2], Max.X, Max.Y, Min.Z);
  493. SetVector(Pt[3], Min.X, Max.Y, Min.Z);
  494. SetVector(Pt[4], Min.X, Min.Y, Max.Z);
  495. SetVector(Pt[5], Max.X, Min.Y, Max.Z);
  496. SetVector(Pt[6], Max.X, Max.Y, Max.Z);
  497. SetVector(Pt[7], Min.X, Max.Y, Max.Z);
  498. end;
  499. end;
  500. procedure MakePlanes(const Pt: array of TVertex;
  501. var Planes: array of THmgPlane);
  502. var
  503. I: Integer;
  504. begin
  505. for I := 0 to 5 do
  506. Planes[I] := PlaneMake(Pt[CPlanes[I, 0]], Pt[CPlanes[I, 1]],
  507. Pt[CPlanes[I, 2]]);
  508. end;
  509. var
  510. Pt1, Pt2: array [0 .. 7] of TVertex;
  511. Pt: TVertex;
  512. Planes2: array [0 .. 5] of THmgPlane;
  513. I, T: Integer;
  514. V: TVertex;
  515. P: TGLVector;
  516. begin
  517. Result := False;
  518. // Build Points
  519. MakeAABBPoints(AABB1, Pt1);
  520. MakeAABBPoints(AABB2, Pt2);
  521. for I := 0 to 7 do
  522. begin
  523. Pt := VectorTransform(Pt2[I], M2To1);
  524. // check for inclusion (points of Obj2 in Obj1)
  525. if IsInRange(Pt.X, AABB1.Min.X, AABB1.Max.X) and
  526. IsInRange(Pt.Y, AABB1.Min.Y, AABB1.Max.Y) and
  527. IsInRange(Pt.Z, AABB1.Min.Z, AABB1.Max.Z) then
  528. begin
  529. Result := True;
  530. Exit;
  531. end;
  532. end;
  533. for I := 0 to 7 do
  534. begin
  535. Pt1[I] := VectorTransform(Pt1[I], M1To2);
  536. // check for inclusion (points of Obj1 in Obj2)
  537. if IsInRange(Pt1[I].X, AABB2.Min.X, AABB2.Max.X) and
  538. IsInRange(Pt1[I].Y, AABB2.Min.Y, AABB2.Max.Y) and
  539. IsInRange(Pt1[I].Z, AABB2.Min.Z, AABB2.Max.Z) then
  540. begin
  541. Result := True;
  542. Exit;
  543. end;
  544. end;
  545. // Build Planes
  546. MakePlanes(Pt2, Planes2);
  547. // Wire test
  548. for I := 0 to 11 do
  549. begin
  550. for T := 0 to 5 do
  551. begin
  552. // Build Vector of Ray
  553. V := VectorSubtract(Pt1[CWires[I, 0]], Pt1[CWires[I, 1]]);
  554. if IntersectLinePlane(VectorMake(Pt1[CWires[I, 0]]), VectorMake(V),
  555. Planes2[T], @P) = 1 then
  556. begin
  557. // check point in Wire
  558. if IsInRange(P.X, Pt1[CWires[I, 0]].X,
  559. Pt1[CWires[I, 1]].X) and
  560. IsInRange(P.Y, Pt1[CWires[I, 0]].Y,
  561. Pt1[CWires[I, 1]].Y) and
  562. IsInRange(P.Z, Pt1[CWires[I, 0]].Z,
  563. Pt1[CWires[I, 1]].Z) then
  564. begin
  565. // check point in Plane
  566. if IsInRange(P.X, Pt2[CPlanes[T, 0]].X,
  567. Pt2[CPlanes[T, 2]].X) and
  568. IsInRange(P.Y, Pt2[CPlanes[T, 0]].Y,
  569. Pt2[CPlanes[T, 2]].Y) and
  570. IsInRange(P.Z, Pt2[CPlanes[T, 0]].Z,
  571. Pt2[CPlanes[T, 2]].Z) then
  572. begin
  573. Result := True;
  574. Exit;
  575. end;
  576. end;
  577. end;
  578. end;
  579. end;
  580. end;
  581. function IntersectAABBsAbsoluteXY(const Aabb1, Aabb2: TAABB): Boolean;
  582. begin
  583. Result := False;
  584. if (AABB2.Min.X > AABB1.Max.X) or
  585. (AABB2.Min.Y > AABB1.Max.Y) then
  586. Exit
  587. else if (AABB2.Max.X < AABB1.Min.X) or
  588. (AABB2.Max.Y < AABB1.Min.Y) then
  589. Exit
  590. else
  591. Result := True;
  592. end;
  593. function IntersectAABBsAbsoluteXZ(const Aabb1, Aabb2: TAABB): Boolean;
  594. begin
  595. Result := ((AABB1.Min.X < AABB2.Max.X) and
  596. (AABB1.Min.Z < AABB2.Max.Z) and
  597. (AABB2.Min.X < AABB1.Max.X) and
  598. (AABB2.Min.Z < AABB1.Max.Z));
  599. end;
  600. function IntersectAABBsAbsolute(const Aabb1, Aabb2: TAABB): Boolean;
  601. begin
  602. Result := not((AABB1.Min.X > AABB2.Max.X) or
  603. (AABB1.Min.Y > AABB2.Max.Y) or
  604. (AABB1.Min.Z > AABB2.Max.Z) or
  605. (AABB2.Min.X > AABB1.Max.X) or
  606. (AABB2.Min.Y > AABB1.Max.Y) or
  607. (AABB2.Min.Z > AABB1.Max.Z));
  608. end;
  609. function AABBFitsInAABBAbsolute(const Aabb1, Aabb2: TAABB): Boolean;
  610. begin
  611. // AABB1 fits completely inside AABB2?
  612. // AABB1 min must be >= to AABB2 min
  613. // AABB1 max must be <= to AABB2 max
  614. Result := (AABB1.Min.X >= AABB2.Min.X) and
  615. (AABB1.Min.Y >= AABB2.Min.Y) and
  616. (AABB1.Min.Z >= AABB2.Min.Z) and
  617. (AABB1.Max.X <= AABB2.Max.X) and
  618. (AABB1.Max.Y <= AABB2.Max.Y) and
  619. (AABB1.Max.Z <= AABB2.Max.Z);
  620. end;
  621. function PointInAABB(const P: TAffineVector; const Aabb: TAABB): Boolean;
  622. begin
  623. Result := (P.X <= Aabb.Max.X) and
  624. (P.X >= Aabb.Min.X) and (P.Y <= Aabb.Max.Y) and
  625. (P.Y >= Aabb.Min.Y) and (P.Z <= Aabb.Max.Z) and
  626. (P.Z >= Aabb.Min.Z);
  627. end;
  628. function PointInAABB(const P: TGLVector; const Aabb: TAABB): Boolean;
  629. begin
  630. Result := (P.X <= Aabb.Max.X) and
  631. (P.X >= Aabb.Min.X) and (P.Y <= Aabb.Max.Y) and
  632. (P.Y >= Aabb.Min.Y) and (P.Z <= Aabb.Max.Z) and
  633. (P.Z >= Aabb.Min.Z);
  634. end;
  635. function PlaneIntersectAABB(const Normal: TAffineVector; D: Single; const Aabb: TAABB): Boolean;
  636. var
  637. Vmax, Vmin: TAffineVector;
  638. I: Integer;
  639. begin
  640. Result := False;
  641. for I := 0 to 2 do
  642. if Normal.V[I] > 0.0 then
  643. begin
  644. VMin.V[I] := Aabb.Min.V[I];
  645. VMax.V[I] := Aabb.Max.V[I];
  646. end
  647. else
  648. begin
  649. VMin.V[I] := Aabb.Max.V[I];
  650. VMax.V[I] := Aabb.Min.V[I];
  651. end;
  652. if VectorDotProduct(Normal, Vmin) + D > 0 then
  653. Exit;
  654. if VectorDotProduct(Normal, Vmax) + D >= 0 then
  655. Result := True;
  656. end;
  657. procedure FindMinMax(X0, X1, X2: Single; out Min, Max: Single);
  658. begin
  659. Min := X0;
  660. Max := X0;
  661. if (X1 < Min) then
  662. Min := X1;
  663. if (X1 > Max) then
  664. Max := X1;
  665. if (X2 < Min) then
  666. Min := X2;
  667. if (X2 > Max) then
  668. Max := X2;
  669. end;
  670. function PlaneAABBIntersection(const plane : THmgPlane;const AABB : TAABB) : TGLAffineVectorList;
  671. var
  672. i, j, annexe : Integer;
  673. index : array[0..2] of Integer;
  674. vec, temp : TVector3f;
  675. box : array [0..1] of TVector3f;
  676. V: array [0..7] of TVector3f;
  677. function EdgesStripPlaneIntersection(const pt0, pt1, pt4, pt7: TVector3f;
  678. const plane : THmgPlane; var inter : TVector3f): Boolean;
  679. begin
  680. Result := True;
  681. if not SegmentPlaneIntersection(pt0, pt1, plane, inter) then
  682. if not SegmentPlaneIntersection(pt1, pt4, plane, inter) then
  683. if not SegmentPlaneIntersection(pt4, pt7, plane, inter) then
  684. Result := False;
  685. end;
  686. begin
  687. box[0] := AABB.min;
  688. box[1] := AABB.max;
  689. Result := TGLAffineVectorList.Create;
  690. // loop on vertices
  691. for i := 0 to 7 do
  692. begin
  693. for j := 0 to 2 do
  694. begin
  695. index[j] := (i div (1 shl j)) mod 2;
  696. vec.V[j] := box[index[j]].V[j];
  697. end;
  698. // try to find the right orientation to proceed intersection
  699. if (i = 0) then
  700. begin
  701. // prepare V 0 -> 7 array
  702. V[0] := vec;
  703. for j := 0 to 5 do
  704. begin
  705. temp := vec;
  706. temp.V[j mod 3] := box[(index[j mod 3] + 1) mod 2].V[j mod 3];
  707. if (j div 3) > 0 then
  708. begin
  709. temp.V[(j+1) mod 3] := box[(index[(j+1) mod 3] + 1) mod 2].V[(j+1) mod 3];
  710. if (j div 3) > 1 then
  711. begin
  712. temp.V[(j+2) mod 3] := box[(index[(j+2) mod 3] + 1) mod 2].V[(j+2) mod 3];
  713. end;
  714. end;
  715. V[j+1] := temp;
  716. end;
  717. for j := 0 to 2 do
  718. vec.V[j] := box[(index[j]+1) mod 2].V[j];
  719. V[7] := vec;
  720. end;
  721. end;
  722. //compute edge plane intersections
  723. for j := 0 to 2 do
  724. begin
  725. if j = 0 then annexe := 6 else annexe := j+3;
  726. // computes intersection with annexe edge
  727. if SegmentPlaneIntersection(V[j+1], V[annexe], plane, temp) then
  728. Result.Add(temp);
  729. // computes intersection with edge strip from main vertex V0 to opposite vertex V7
  730. if EdgesStripPlaneIntersection(V[0], V[j+1], V[j+4], V[7], plane, temp) then
  731. Result.Add(temp);
  732. end;
  733. end;
  734. function PlaneBoxOverlap(const Normal: TAffineVector; D: Single;
  735. const Maxbox: TAffineVector): Boolean;
  736. var
  737. Q: Integer;
  738. Vmin, Vmax: TAffineVector;
  739. begin
  740. Result := False;
  741. for Q := 0 to 2 do
  742. begin
  743. if (Normal.V[Q] > 0.0) then
  744. begin
  745. Vmin.V[Q] := -Maxbox.V[Q];
  746. Vmax.V[Q] := Maxbox.V[Q];
  747. end
  748. else
  749. begin
  750. Vmin.V[Q] := Maxbox.V[Q];
  751. Vmax.V[Q] := -Maxbox.V[Q];
  752. end;
  753. end;
  754. if (VectorDotProduct(Normal, Vmin) + D) > 0 then
  755. Exit;
  756. if (VectorDotProduct(Normal, Vmax) + D) >= 0 then
  757. Result := True;
  758. end;
  759. function TriangleIntersectAABB(const Aabb: TAABB;
  760. const V1, V2, V3: TAffineVector): Boolean;
  761. var
  762. Boxcenter, Boxhalfsize: TAffineVector;
  763. Tv0, Tv1, Tv2: TAffineVector;
  764. Min, Max, D, P0, P1, P2, Rad, Fex, Fey, Fez: Single;
  765. Normal, E0, E1, E2: TAffineVector;
  766. begin
  767. Result := False;
  768. Boxhalfsize := VectorSubtract(VectorScale(Aabb.Max, 0.5),
  769. VectorScale(Aabb.Min, 0.5));
  770. Boxcenter := VectorAdd(VectorScale(Aabb.Max, 0.5),
  771. VectorScale(Aabb.Min, 0.5));
  772. // move everything so that the boxcenter is in (0,0,0)
  773. VectorSubtract(V1, Boxcenter, Tv0);
  774. VectorSubtract(V2, Boxcenter, Tv1);
  775. VectorSubtract(V3, Boxcenter, Tv2);
  776. // compute triangle edges
  777. VectorSubtract(Tv1, Tv0, E0);
  778. VectorSubtract(Tv2, Tv1, E1);
  779. VectorSubtract(Tv0, Tv2, E2);
  780. // Bullet 3:
  781. // test the 9 tests first (this was faster)
  782. Fex := Abs(E0.X);
  783. Fey := Abs(E0.Y);
  784. Fez := Abs(E0.Z);
  785. // AXISTEST_X01(e0[Z], e0[Y], fez, fey);
  786. P0 := E0.Z * Tv0.Y - E0.Y * Tv0.Z;
  787. P2 := E0.Z * Tv2.Y - E0.Y * Tv2.Z;
  788. Min := MinFloat(P0, P2);
  789. Max := MaxFloat(P0, P2);
  790. Rad := Fez * Boxhalfsize.Y + Fey * Boxhalfsize.Z;
  791. if (Min > Rad) or (Max < -Rad) then
  792. Exit;
  793. // AXISTEST_Y02(e0[Z], e0[X], fez, fex);
  794. P0 := -E0.Z * Tv0.X + E0.X * Tv0.Z;
  795. P2 := -E0.Z * Tv2.X + E0.X * Tv2.Z;
  796. Min := MinFloat(P0, P2);
  797. Max := MaxFloat(P0, P2);
  798. Rad := Fez * Boxhalfsize.X + Fex * Boxhalfsize.Z;
  799. if (Min > Rad) or (Max < -Rad) then
  800. Exit;
  801. // AXISTEST_Z12(e0[Y], e0[X], fey, fex);
  802. P1 := E0.Y * Tv1.X - E0.X * Tv1.Y;
  803. P2 := E0.Y * Tv2.X - E0.X * Tv2.Y;
  804. Min := MinFloat(P1, P2);
  805. Max := MaxFloat(P1, P2);
  806. Rad := Fey * Boxhalfsize.X + Fex * Boxhalfsize.Y;
  807. if (Min > Rad) or (Max < -Rad) then
  808. Exit;
  809. Fex := Abs(E1.X);
  810. Fey := Abs(E1.Y);
  811. Fez := Abs(E1.Z);
  812. // AXISTEST_X01(e1[Z], e1[Y], fez, fey);
  813. P0 := E1.Z * Tv0.Y - E1.Y * Tv0.Z;
  814. P2 := E1.Z * Tv2.Y - E1.Y * Tv2.Z;
  815. Min := MinFloat(P0, P2);
  816. Max := MaxFloat(P0, P2);
  817. Rad := Fez * Boxhalfsize.Y + Fey * Boxhalfsize.Z;
  818. if (Min > Rad) or (Max < -Rad) then
  819. Exit;
  820. // AXISTEST_Y02(e1[Z], e1[X], fez, fex);
  821. P0 := -E1.Z * Tv0.X + E1.X * Tv0.Z;
  822. P2 := -E1.Z * Tv2.X + E1.X * Tv2.Z;
  823. Min := MinFloat(P0, P2);
  824. Max := MaxFloat(P0, P2);
  825. Rad := Fez * Boxhalfsize.X + Fex * Boxhalfsize.Z;
  826. if (Min > Rad) or (Max < -Rad) then
  827. Exit;
  828. // AXISTEST_Z0(e1[Y], e1[X], fey, fex);
  829. P0 := E1.Y * Tv0.X - E1.X * Tv0.Y;
  830. P1 := E1.Y * Tv1.X - E1.X * Tv1.Y;
  831. Min := MinFloat(P0, P1);
  832. Max := MaxFloat(P0, P1);
  833. Rad := Fey * Boxhalfsize.X + Fex * Boxhalfsize.Y;
  834. if (Min > Rad) or (Max < -Rad) then
  835. Exit;
  836. Fex := Abs(E2.X);
  837. Fey := Abs(E2.Y);
  838. Fez := Abs(E2.Z);
  839. // AXISTEST_X2(e2[Z], e2[Y], fez, fey);
  840. P0 := E2.Z * Tv0.Y - E2.Y * Tv0.Z;
  841. P1 := E2.Z * Tv1.Y - E2.Y * Tv1.Z;
  842. Min := MinFloat(P0, P1);
  843. Max := MaxFloat(P0, P1);
  844. Rad := Fez * Boxhalfsize.Y + Fey * Boxhalfsize.Z;
  845. if (Min > Rad) or (Max < -Rad) then
  846. Exit;
  847. // AXISTEST_Y1(e2[Z], e2[X], fez, fex);
  848. P0 := -E2.Z * Tv0.X + E2.X * Tv0.Z;
  849. P1 := -E2.Z * Tv1.X + E2.X * Tv1.Z;
  850. Min := MinFloat(P0, P1);
  851. Max := MaxFloat(P0, P1);
  852. Rad := Fez * Boxhalfsize.X + Fex * Boxhalfsize.Z;
  853. if (Min > Rad) or (Max < -Rad) then
  854. Exit;
  855. // AXISTEST_Z12(e2[Y], e2[X], fey, fex);
  856. P1 := E2.Y * Tv1.X - E2.X * Tv1.Y;
  857. P2 := E2.Y * Tv2.X - E2.X * Tv2.Y;
  858. Min := MinFloat(P1, P2);
  859. Max := MaxFloat(P1, P2);
  860. Rad := Fey * Boxhalfsize.X + Fex * Boxhalfsize.Y;
  861. if (Min > Rad) or (Max < -Rad) then
  862. Exit;
  863. // Bullet 1:
  864. // first test overlap in the {x,y,z}-directions
  865. // find min, max of the triangle each direction, and test for overlap in
  866. // that direction -- this is equivalent to testing a minimal AABB around
  867. // the triangle against the AABB
  868. // test in X-direction
  869. FindMinMax(Tv0.X, Tv1.X, Tv2.X, Min, Max);
  870. if (Min > Boxhalfsize.X) or (Max < -Boxhalfsize.X) then
  871. Exit;
  872. // test in Y-direction
  873. FindMinMax(Tv0.Y, Tv1.Y, Tv2.Y, Min, Max);
  874. if (Min > Boxhalfsize.Y) or (Max < -Boxhalfsize.Y) then
  875. Exit;
  876. // test in Z-direction
  877. FindMinMax(Tv0.Z, Tv1.Z, Tv2.Z, Min, Max);
  878. if (Min > Boxhalfsize.Z) or (Max < -Boxhalfsize.Z) then
  879. Exit;
  880. // Bullet 2:
  881. // test if the box intersects the plane of the triangle
  882. // compute plane equation of triangle: normal * x + d = 0
  883. VectorCrossProduct(E0, E1, Normal);
  884. D := -VectorDotProduct(Normal, Tv0); // plane eq: normal.x + d = 0
  885. if not PlaneBoxOverlap(Normal, D, Boxhalfsize) then
  886. Exit;
  887. // box and triangle overlaps
  888. Result := True;
  889. end;
  890. procedure ExtractAABBCorners(const AABB: TAABB; var AABBCorners: TAABBCorners);
  891. begin
  892. MakeVector(AABBCorners[0], AABB.Min.X, AABB.Min.Y,
  893. AABB.Min.Z);
  894. MakeVector(AABBCorners[1], AABB.Min.X, AABB.Min.Y,
  895. AABB.Max.Z);
  896. MakeVector(AABBCorners[2], AABB.Min.X, AABB.Max.Y,
  897. AABB.Min.Z);
  898. MakeVector(AABBCorners[3], AABB.Min.X, AABB.Max.Y,
  899. AABB.Max.Z);
  900. MakeVector(AABBCorners[4], AABB.Max.X, AABB.Min.Y,
  901. AABB.Min.Z);
  902. MakeVector(AABBCorners[5], AABB.Max.X, AABB.Min.Y,
  903. AABB.Max.Z);
  904. MakeVector(AABBCorners[6], AABB.Max.X, AABB.Max.Y,
  905. AABB.Min.Z);
  906. MakeVector(AABBCorners[7], AABB.Max.X, AABB.Max.Y,
  907. AABB.Max.Z);
  908. end;
  909. procedure AABBToBSphere(const AABB: TAABB; var BSphere: TBSphere);
  910. begin
  911. BSphere.Center := VectorScale(VectorAdd(AABB.Min, AABB.Max), 0.5);
  912. BSphere.Radius := VectorDistance(AABB.Min, AABB.Max) * 0.5;
  913. end;
  914. procedure BSphereToAABB(const BSphere: TBSphere; var AABB: TAABB);
  915. begin
  916. AABB.Min := VectorSubtract(BSphere.Center, BSphere.Radius);
  917. AABB.Max := VectorAdd(BSphere.Center, BSphere.Radius);
  918. end;
  919. function BSphereToAABB(const Center: TAffineVector; Radius: Single): TAABB;
  920. begin
  921. Result.Min := VectorSubtract(Center, Radius);
  922. Result.Max := VectorAdd(Center, Radius);
  923. end;
  924. function BSphereToAABB(const Center: TGLVector; Radius: Single): TAABB;
  925. begin
  926. SetVector(Result.Min, VectorSubtract(Center, Radius));
  927. SetVector(Result.Max, VectorAdd(Center, Radius));
  928. end;
  929. function AABBContainsAABB(const MainAABB, TestAABB: TAABB): TSpaceContains;
  930. begin
  931. // AABB1 fits completely inside AABB2?
  932. // AABB1 min must be >= to AABB2 min
  933. // AABB1 max must be <= to AABB2 max
  934. if ((MainAABB.Min.X < TestAABB.Max.X) and
  935. (MainAABB.Min.Y < TestAABB.Max.Y) and
  936. (MainAABB.Min.Z < TestAABB.Max.Z) and
  937. (TestAABB.Min.X < MainAABB.Max.X) and
  938. (TestAABB.Min.Y < MainAABB.Max.Y) and
  939. (TestAABB.Min.Z < MainAABB.Max.Z)) then
  940. begin
  941. if (TestAABB.Min.X >= MainAABB.Min.X) and
  942. (TestAABB.Min.Y >= MainAABB.Min.Y) and
  943. (TestAABB.Min.Z >= MainAABB.Min.Z) and
  944. (TestAABB.Max.X <= MainAABB.Max.X) and
  945. (TestAABB.Max.Y <= MainAABB.Max.Y) and
  946. (TestAABB.Max.Z <= MainAABB.Max.Z) then
  947. Result := ScContainsFully
  948. else
  949. Result := ScContainsPartially;
  950. end
  951. else
  952. Result := ScNoOverlap;
  953. end;
  954. function AABBContainsBSphere(const MainAABB: TAABB; const TestBSphere: TBSphere)
  955. : TSpaceContains;
  956. var
  957. TestAABB: TAABB;
  958. begin
  959. BSphereToAABB(TestBSphere, TestAABB);
  960. Result := AABBContainsAABB(MainAABB, TestAABB);
  961. end;
  962. function PlaneContainsBSphere(const Location, Normal: TAffineVector;
  963. const TestBSphere: TBSphere): TSpaceContains;
  964. var
  965. Dist: Single;
  966. begin
  967. Dist := PointPlaneDistance(TestBSphere.Center, Location, Normal);
  968. if Dist > TestBSphere.Radius then
  969. Result := ScNoOverlap
  970. else if Abs(Dist) <= TestBSphere.Radius then
  971. Result := ScContainsPartially
  972. else
  973. Result := ScContainsFully;
  974. end;
  975. function FrustumContainsBSphere(const Frustum: TFrustum;
  976. const TestBSphere: TBSphere): TSpaceContains;
  977. var
  978. NegRadius: Single;
  979. HitCount: Integer;
  980. Distance: Single;
  981. I: Integer;
  982. type
  983. TPlaneArray = array [0 .. 5] of THmgPlane;
  984. begin
  985. NegRadius := -TestBSphere.Radius;
  986. HitCount := 0;
  987. // This would be fractionally faster to unroll, but oh so ugly!?
  988. for I := 0 to 5 do
  989. begin
  990. Distance := PlaneEvaluatePoint(TPlaneArray(Frustum)[I], TestBSphere.Center);
  991. if Distance < NegRadius then
  992. begin
  993. Result := ScNoOverlap;
  994. Exit;
  995. end
  996. else if Distance >= TestBSphere.Radius then
  997. Inc(HitCount);
  998. end; // }
  999. if HitCount = 6 then
  1000. Result := ScContainsFully
  1001. else
  1002. Result := ScContainsPartially;
  1003. end;
  1004. // see http://www.flipcode.com/articles/article_frustumculling.shtml
  1005. function FrustumContainsAABB(const Frustum: TFrustum; const TestAABB: TAABB)
  1006. : TSpaceContains;
  1007. type
  1008. TPlaneArray = array [0 .. 5] of THmgPlane;
  1009. var
  1010. IPlane, ICorner: Integer;
  1011. PointIn: Boolean;
  1012. AABBCorners: TAABBCorners;
  1013. InCount: Integer;
  1014. TotalIn: Integer;
  1015. begin
  1016. ExtractAABBCorners(TestAABB, AABBCorners);
  1017. TotalIn := 0;
  1018. // test all 8 corners against the 6 sides
  1019. // if all points are behind 1 specific plane, we are out
  1020. // if we are in with all points, then we are fully in
  1021. // For each plane
  1022. for IPlane := Low(TPlaneArray) to High(TPlaneArray) do
  1023. begin
  1024. // We're about to test 8 corners
  1025. InCount := 8;
  1026. PointIn := True;
  1027. // For each corner
  1028. for ICorner := Low(AABBCorners) to High(AABBCorners) do
  1029. begin
  1030. if PlaneEvaluatePoint(TPlaneArray(Frustum)[IPlane], AABBCorners[ICorner]
  1031. ) < 0 then
  1032. begin
  1033. PointIn := False;
  1034. Dec(InCount);
  1035. end;
  1036. end;
  1037. if InCount = 0 then
  1038. begin
  1039. Result := ScNoOverlap;
  1040. Exit;
  1041. end
  1042. else if PointIn then
  1043. Inc(TotalIn);
  1044. end;
  1045. if TotalIn = 6 then
  1046. Result := ScContainsFully
  1047. else
  1048. Result := ScContainsPartially;
  1049. end;
  1050. function BSphereContainsAABB(const MainBSphere: TBSphere; const TestAABB: TAABB)
  1051. : TSpaceContains;
  1052. var
  1053. R2: Single;
  1054. ClippedCenter: TAffineVector;
  1055. AABBCorners: TAABBCorners;
  1056. CornerHitCount: Integer;
  1057. begin
  1058. R2 := Sqr(MainBSphere.Radius);
  1059. ClippedCenter := ClipToAABB(MainBSphere.Center, TestAABB);
  1060. if VectorDistance2(ClippedCenter, MainBSphere.Center) < R2 then
  1061. begin
  1062. ExtractAABBCorners(TestAABB, AABBCorners);
  1063. CornerHitCount := 0;
  1064. // BSphere fully contains aabb if all corners of aabb are within bsphere.
  1065. if (VectorDistance2(MainBSphere.Center, AABBCorners[0]) < R2) then
  1066. Inc(CornerHitCount);
  1067. if (VectorDistance2(MainBSphere.Center, AABBCorners[1]) < R2) then
  1068. Inc(CornerHitCount);
  1069. if (VectorDistance2(MainBSphere.Center, AABBCorners[2]) < R2) then
  1070. Inc(CornerHitCount);
  1071. if (VectorDistance2(MainBSphere.Center, AABBCorners[3]) < R2) then
  1072. Inc(CornerHitCount);
  1073. if (VectorDistance2(MainBSphere.Center, AABBCorners[4]) < R2) then
  1074. Inc(CornerHitCount);
  1075. if (VectorDistance2(MainBSphere.Center, AABBCorners[5]) < R2) then
  1076. Inc(CornerHitCount);
  1077. if (VectorDistance2(MainBSphere.Center, AABBCorners[6]) < R2) then
  1078. Inc(CornerHitCount);
  1079. if (VectorDistance2(MainBSphere.Center, AABBCorners[7]) < R2) then
  1080. Inc(CornerHitCount);
  1081. if CornerHitCount = 7 then
  1082. Result := ScContainsFully
  1083. else
  1084. Result := ScContainsPartially;
  1085. end
  1086. else
  1087. Result := ScNoOverlap;
  1088. end;
  1089. function BSphereContainsBSphere(const MainBSphere, TestBSphere: TBSphere)
  1090. : TSpaceContains;
  1091. var
  1092. D2: Single;
  1093. begin
  1094. D2 := VectorDistance2(MainBSphere.Center, TestBSphere.Center);
  1095. if D2 < Sqr(MainBSphere.Radius + TestBSphere.Radius) then
  1096. begin
  1097. if D2 < Sqr(MainBSphere.Radius - TestBSphere.Radius) then
  1098. Result := ScContainsFully
  1099. else
  1100. Result := ScContainsPartially;
  1101. end
  1102. else
  1103. Result := ScNoOverlap;
  1104. end;
  1105. function BSphereIntersectsBSphere(const MainBSphere,
  1106. TestBSphere: TBSphere): Boolean;
  1107. begin
  1108. Result := VectorDistance2(MainBSphere.Center, TestBSphere.Center) <
  1109. Sqr(MainBSphere.Radius + TestBSphere.Radius);
  1110. end;
  1111. function ClipToAABB(const V: TAffineVector; const AABB: TAABB): TAffineVector;
  1112. begin
  1113. Result := V;
  1114. if Result.X < AABB.Min.X then
  1115. Result.X := AABB.Min.X;
  1116. if Result.Y < AABB.Min.Y then
  1117. Result.Y := AABB.Min.Y;
  1118. if Result.Z < AABB.Min.Z then
  1119. Result.Z := AABB.Min.Z;
  1120. if Result.X > AABB.Max.X then
  1121. Result.X := AABB.Max.X;
  1122. if Result.Y > AABB.Max.Y then
  1123. Result.Y := AABB.Max.Y;
  1124. if Result.Z > AABB.Max.Z then
  1125. Result.Z := AABB.Max.Z;
  1126. end;
  1127. procedure IncludeInClipRect(var ClipRect: TGLClipRect; X, Y: Single);
  1128. begin
  1129. with ClipRect do
  1130. begin
  1131. if X < Left then
  1132. Left := X;
  1133. if X > Right then
  1134. Right := X;
  1135. if Y < Top then
  1136. Top := Y;
  1137. if Y > Bottom then
  1138. Bottom := Y;
  1139. end;
  1140. end;
  1141. function AABBToClipRect(const Aabb: TAABB; const ModelViewProjection: TGLMatrix;
  1142. ViewportSizeX, ViewportSizeY: Integer): TGLClipRect;
  1143. var
  1144. I: Integer;
  1145. V, Vt: TGLVector;
  1146. Minmax: array [0 .. 1] of PAffineVector;
  1147. begin
  1148. Minmax[0] := @Aabb.Min;
  1149. Minmax[1] := @Aabb.Max;
  1150. V.W := 1;
  1151. for I := 0 to 7 do
  1152. begin
  1153. V.X := Minmax[I and 1].X;
  1154. V.Y := Minmax[(I shr 1) and 1].Y;
  1155. V.Z := Minmax[(I shr 2) and 1].Z;
  1156. // Project
  1157. Vt := VectorTransform(V, ModelViewProjection);
  1158. ScaleVector(Vt, 1 / Vt.W);
  1159. // Convert to screen coordinates
  1160. if I > 0 then
  1161. IncludeInClipRect(Result, ViewportSizeX * (Vt.X + 1) * 0.5,
  1162. ViewportSizeY * (Vt.Y + 1) * 0.5)
  1163. else
  1164. begin
  1165. Result.Left := ViewportSizeX * (Vt.X + 1) * 0.5;
  1166. Result.Top := ViewportSizeY * (Vt.Y + 1) * 0.5;
  1167. Result.Right := Result.Left;
  1168. Result.Bottom := Result.Top;
  1169. end;
  1170. end;
  1171. end;
  1172. function RayCastAABBIntersect(const RayOrigin, RayDirection: TGLVector;
  1173. const Aabb: TAABB; out TNear, TFar: Single): Boolean; overload;
  1174. const
  1175. Infinity = 1.0 / 0.0;
  1176. var
  1177. P: Integer;
  1178. InvDir: Double;
  1179. T0, T1, Tmp: Single;
  1180. begin
  1181. Result := False;
  1182. TNear := -Infinity;
  1183. TFar := Infinity;
  1184. for P := 0 to 2 do
  1185. begin
  1186. if (RayDirection.V[P] = 0) then
  1187. begin
  1188. if ((RayOrigin.V[P] < Aabb.Min.V[P]) or
  1189. (RayOrigin.V[P] > Aabb.Max.V[P])) then
  1190. Exit;
  1191. end
  1192. else
  1193. begin
  1194. InvDir := 1 / RayDirection.V[P];
  1195. T0 := (Aabb.Min.V[P] - RayOrigin.V[P]) * InvDir;
  1196. T1 := (Aabb.Max.V[P] - RayOrigin.V[P]) * InvDir;
  1197. if (T0 > T1) then
  1198. begin
  1199. Tmp := T0;
  1200. T0 := T1;
  1201. T1 := Tmp;
  1202. end;
  1203. if (T0 > TNear) then
  1204. TNear := T0;
  1205. if (T1 < TFar) then
  1206. TFar := T1;
  1207. if ((TNear > TFar) or (TFar < 0)) then
  1208. Exit;
  1209. end;
  1210. end;
  1211. Result := True;
  1212. end;
  1213. function RayCastAABBIntersect(const RayOrigin, RayDirection: TGLVector;
  1214. const Aabb: TAABB; IntersectPoint: PGLVector = nil): Boolean; overload;
  1215. var
  1216. TNear, TFar: Single;
  1217. begin
  1218. Result := RayCastAABBIntersect(RayOrigin, RayDirection, Aabb, TNear, TFar);
  1219. if Result and Assigned(IntersectPoint) then
  1220. begin
  1221. if TNear >= 0 then
  1222. // origin outside the box
  1223. IntersectPoint^ := VectorCombine(RayOrigin, RayDirection, 1, TNear)
  1224. else
  1225. // origin inside the box, near is "behind", use far
  1226. IntersectPoint^ := VectorCombine(RayOrigin, RayDirection, 1, TFar);
  1227. end;
  1228. end;
  1229. end.