GLGeometryBB.pas 41 KB

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