GXS.GeometryBB.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. //
  2. // The graphics engine GXScene
  3. //
  4. unit GXS.GeometryBB;
  5. (* Calculations and manipulations on Bounding Boxes *)
  6. interface
  7. {$I Stage.Defines.inc}
  8. uses
  9. System.SysUtils,
  10. Stage.VectorGeometry,
  11. Stage.VectorTypes,
  12. GXS.VectorLists;
  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. TGClipRect = 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) : TgxAffineVectorList;
  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: TGClipRect; 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): TGClipRect;
  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. implementation // -------------------------------------------------------------
  178. // ------------------------------------------------------------------------------
  179. // ----------------- BB functions -------------------------------------------
  180. // ------------------------------------------------------------------------------
  181. procedure SetPlanBB(var BB: THmgBoundingBox; const NumPlan: Integer;
  182. const Valeur: Double);
  183. var
  184. I: Integer;
  185. begin
  186. for I := 0 to 3 do
  187. begin
  188. BB.BBox[CBBPlans[NumPlan][I]].V[CDirPlan[NumPlan]] := Valeur;
  189. BB.BBox[CBBPlans[NumPlan][I]].W := 1;
  190. end;
  191. end;
  192. function BoundingBoxesAreEqual(const ABoundingBox1, ABoundingBox2
  193. : THmgBoundingBox): Boolean;
  194. begin
  195. Result := CompareMem(@ABoundingBox1, @ABoundingBox2, SizeOf(THmgBoundingBox));
  196. end;
  197. function BoundingBoxesAreEqual(const ABoundingBox1, ABoundingBox2
  198. : PHmgBoundingBox): Boolean;
  199. begin
  200. Result := CompareMem(ABoundingBox1, ABoundingBox2, SizeOf(THmgBoundingBox));
  201. end;
  202. function AddBB(var C1: THmgBoundingBox; const C2: THmgBoundingBox): THmgBoundingBox;
  203. var
  204. I, J: Integer;
  205. begin
  206. for I := 0 to 7 do
  207. begin
  208. for J := 0 to 3 do
  209. if C1.BBox[CBBFront[J]].X < C2.BBox[I].X then
  210. SetPlanBB(C1, 0, C2.BBox[I].X);
  211. for J := 0 to 3 do
  212. if C1.BBox[CBBBack[J]].X > C2.BBox[I].X then
  213. SetPlanBB(C1, 1, C2.BBox[I].X);
  214. for J := 0 to 3 do
  215. if C1.BBox[CBBLeft[J]].Y < C2.BBox[I].Y then
  216. SetPlanBB(C1, 2, C2.BBox[I].Y);
  217. for J := 0 to 3 do
  218. if C1.BBox[CBBRight[J]].Y > C2.BBox[I].Y then
  219. SetPlanBB(C1, 3, C2.BBox[I].Y);
  220. for J := 0 to 3 do
  221. if C1.BBox[CBBTop[J]].Z < C2.BBox[I].Z then
  222. SetPlanBB(C1, 4, C2.BBox[I].Z);
  223. for J := 0 to 3 do
  224. if C1.BBox[CBBBottom[J]].Z > C2.BBox[I].Z then
  225. SetPlanBB(C1, 5, C2.BBox[I].Z);
  226. end;
  227. Result := C1;
  228. end;
  229. procedure AddAABB(var Aabb: TAABB; const Aabb1: TAABB);
  230. begin
  231. if Aabb1.Min.X < Aabb.Min.X then
  232. Aabb.Min.X := Aabb1.Min.X;
  233. if Aabb1.Min.Y < Aabb.Min.Y then
  234. Aabb.Min.Y := Aabb1.Min.Y;
  235. if Aabb1.Min.Z < Aabb.Min.Z then
  236. Aabb.Min.Z := Aabb1.Min.Z;
  237. if Aabb1.Max.X > Aabb.Max.X then
  238. Aabb.Max.X := Aabb1.Max.X;
  239. if Aabb1.Max.Y > Aabb.Max.Y then
  240. Aabb.Max.Y := Aabb1.Max.Y;
  241. if Aabb1.Max.Z > Aabb.Max.Z then
  242. Aabb.Max.Z := Aabb1.Max.Z;
  243. end;
  244. procedure SetBB(var C: THmgBoundingBox; const V: TGLVector);
  245. begin
  246. SetPlanBB(C, 0, V.X);
  247. SetPlanBB(C, 1, -V.X);
  248. SetPlanBB(C, 2, V.Y);
  249. SetPlanBB(C, 3, -V.Y);
  250. SetPlanBB(C, 4, V.Z);
  251. SetPlanBB(C, 5, -V.Z);
  252. end;
  253. procedure SetAABB(var Bb: TAABB; const V: TGLVector);
  254. begin
  255. Bb.Max.X := Abs(V.X);
  256. Bb.Max.Y := Abs(V.Y);
  257. Bb.Max.Z := Abs(V.Z);
  258. Bb.Min.X := -Bb.Max.X;
  259. Bb.Min.Y := -Bb.Max.Y;
  260. Bb.Min.Z := -Bb.Max.Z;
  261. end;
  262. procedure BBTransform(var C: THmgBoundingBox; const M: TGLMatrix);
  263. var
  264. I: Integer;
  265. begin
  266. for I := 0 to 7 do
  267. C.BBox[I] := VectorTransform(C.BBox[I], M);
  268. end;
  269. procedure AABBTransform(var Bb: TAABB; const M: TGLMatrix);
  270. var
  271. OldMin, OldMax: TAffineVector;
  272. begin
  273. OldMin := Bb.Min;
  274. OldMax := Bb.Max;
  275. Bb.Min := VectorTransform(OldMin, M);
  276. Bb.Max := Bb.Min;
  277. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMin.X,
  278. OldMin.Y, OldMax.Z), M));
  279. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMin.X,
  280. OldMax.Y, OldMin.Z), M));
  281. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMin.X,
  282. OldMax.Y, OldMax.Z), M));
  283. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMax.X,
  284. OldMin.Y, OldMin.Z), M));
  285. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMax.X,
  286. OldMin.Y, OldMax.Z), M));
  287. AABBInclude(Bb, VectorTransform(AffineVectorMake(OldMax.X,
  288. OldMax.Y, OldMin.Z), M));
  289. AABBInclude(Bb, VectorTransform(OldMax, M));
  290. end;
  291. procedure AABBScale(var Bb: TAABB; const V: TAffineVector);
  292. begin
  293. ScaleVector(Bb.Min, V);
  294. ScaleVector(Bb.Max, V);
  295. end;
  296. function BBMinX(const C: THmgBoundingBox): Single;
  297. var
  298. I: Integer;
  299. begin
  300. Result := C.BBox[0].X;
  301. for I := 1 to 7 do
  302. Result := MinFloat(Result, C.BBox[I].X);
  303. end;
  304. function BBMaxX(const C: THmgBoundingBox): Single;
  305. var
  306. I: Integer;
  307. begin
  308. Result := C.BBox[0].X;
  309. for I := 1 to 7 do
  310. Result := MaxFloat(Result, C.BBox[I].X);
  311. end;
  312. function BBMinY(const C: THmgBoundingBox): Single;
  313. var
  314. I: Integer;
  315. begin
  316. Result := C.BBox[0].Y;
  317. for I := 1 to 7 do
  318. Result := MinFloat(Result, C.BBox[I].Y);
  319. end;
  320. function BBMaxY(const C: THmgBoundingBox): Single;
  321. var
  322. I: Integer;
  323. begin
  324. Result := C.BBox[0].Y;
  325. for I := 1 to 7 do
  326. Result := MaxFloat(Result, C.BBox[I].Y);
  327. end;
  328. function BBMinZ(const C: THmgBoundingBox): Single;
  329. var
  330. I: Integer;
  331. begin
  332. Result := C.BBox[0].Z;
  333. for I := 1 to 7 do
  334. Result := MinFloat(Result, C.BBox[I].Z);
  335. end;
  336. function BBMaxZ(const C: THmgBoundingBox): Single;
  337. var
  338. I: Integer;
  339. begin
  340. Result := C.BBox[0].Z;
  341. for I := 1 to 7 do
  342. Result := MaxFloat(Result, C.BBox[I].Z);
  343. end;
  344. procedure AABBInclude(var Bb: TAABB; const P: TAffineVector);
  345. begin
  346. if P.X < Bb.Min.X then
  347. Bb.Min.X := P.X;
  348. if P.X > Bb.Max.X then
  349. Bb.Max.X := P.X;
  350. if P.Y < Bb.Min.Y then
  351. Bb.Min.Y := P.Y;
  352. if P.Y > Bb.Max.Y then
  353. Bb.Max.Y := P.Y;
  354. if P.Z < Bb.Min.Z then
  355. Bb.Min.Z := P.Z;
  356. if P.Z > Bb.Max.Z then
  357. Bb.Max.Z := P.Z;
  358. end;
  359. procedure AABBFromSweep(var SweepAABB: TAABB; const Start, Dest: TGLVector;
  360. const Radius: Single);
  361. begin
  362. if Start.X < Dest.X then
  363. begin
  364. SweepAABB.Min.X := Start.X - Radius;
  365. SweepAABB.Max.X := Dest.X + Radius;
  366. end
  367. else
  368. begin
  369. SweepAABB.Min.X := Dest.X - Radius;
  370. SweepAABB.Max.X := Start.X + Radius;
  371. end;
  372. if Start.Y < Dest.Y then
  373. begin
  374. SweepAABB.Min.Y := Start.Y - Radius;
  375. SweepAABB.Max.Y := Dest.Y + Radius;
  376. end
  377. else
  378. begin
  379. SweepAABB.Min.Y := Dest.Y - Radius;
  380. SweepAABB.Max.Y := Start.Y + Radius;
  381. end;
  382. if Start.Z < Dest.Z then
  383. begin
  384. SweepAABB.Min.Z := Start.Z - Radius;
  385. SweepAABB.Max.Z := Dest.Z + Radius;
  386. end
  387. else
  388. begin
  389. SweepAABB.Min.Z := Dest.Z - Radius;
  390. SweepAABB.Max.Z := Start.Z + Radius;
  391. end;
  392. end;
  393. function AABBIntersection(const Aabb1, Aabb2: TAABB): TAABB;
  394. var
  395. I: Integer;
  396. begin
  397. for I := 0 to 2 do
  398. begin
  399. Result.Min.V[I] := MaxFloat(Aabb1.Min.V[I], Aabb2.Min.V[I]);
  400. Result.Max.V[I] := MinFloat(Aabb1.Max.V[I], Aabb2.Max.V[I]);
  401. end;
  402. end;
  403. function BBToAABB(const ABB: THmgBoundingBox): TAABB;
  404. var
  405. I: Integer;
  406. begin
  407. SetVector(Result.Min, ABB.BBox[0]);
  408. SetVector(Result.Max, ABB.BBox[0]);
  409. for I := 1 to 7 do
  410. begin
  411. if ABB.BBox[I].X < Result.Min.X then
  412. Result.Min.X := ABB.BBox[I].X;
  413. if ABB.BBox[I].X > Result.Max.X then
  414. Result.Max.X := ABB.BBox[I].X;
  415. if ABB.BBox[I].Y < Result.Min.Y then
  416. Result.Min.Y := ABB.BBox[I].Y;
  417. if ABB.BBox[I].Y > Result.Max.Y then
  418. Result.Max.Y := ABB.BBox[I].Y;
  419. if ABB.BBox[I].Z < Result.Min.Z then
  420. Result.Min.Z := ABB.BBox[I].Z;
  421. if ABB.BBox[I].Z > Result.Max.Z then
  422. Result.Max.Z := ABB.BBox[I].Z;
  423. end;
  424. end;
  425. function AABBToBB(const AnAABB: TAABB): THmgBoundingBox;
  426. begin
  427. with AnAABB do
  428. begin
  429. SetPlanBB(Result, 0, Max.X);
  430. SetPlanBB(Result, 1, Min.X);
  431. SetPlanBB(Result, 2, Max.Y);
  432. SetPlanBB(Result, 3, Min.Y);
  433. SetPlanBB(Result, 4, Max.Z);
  434. SetPlanBB(Result, 5, Min.Z);
  435. end;
  436. end;
  437. function AABBToBB(const AnAABB: TAABB; const M: TGLMatrix): THmgBoundingBox;
  438. begin
  439. Result := AABBToBB(AnAABB);
  440. BBTransform(Result, M);
  441. end;
  442. procedure OffsetAABB(var Aabb: TAABB; const Delta: TAffineVector);
  443. begin
  444. AddVector(Aabb.Min, Delta);
  445. AddVector(Aabb.Max, Delta);
  446. end;
  447. procedure OffsetAABB(var Aabb: TAABB; const Delta: TGLVector);
  448. begin
  449. AddVector(Aabb.Min, Delta);
  450. AddVector(Aabb.Max, Delta);
  451. end;
  452. procedure OffsetBB(var Bb: THmgBoundingBox; const Delta: TAffineVector);
  453. var
  454. I: Integer;
  455. TempVector: TGLVector;
  456. begin
  457. TempVector := VectorMake(Delta, 0);
  458. for I := 0 to 7 do
  459. AddVector(Bb.BBox[I], TempVector);
  460. end;
  461. procedure OffsetBB(var Bb: THmgBoundingBox; const Delta: TGLVector);
  462. var
  463. I: Integer;
  464. begin
  465. for I := 0 to 7 do
  466. AddVector(Bb.BBox[I], Delta);
  467. end;
  468. procedure OffsetBBPoint(var Bb: THmgBoundingBox; const Delta: TGLVector);
  469. var
  470. I: Integer;
  471. begin
  472. for I := 0 to 7 do
  473. AddPoint(Bb.BBox[I], Delta);
  474. end;
  475. function IntersectAABBs(const Aabb1, Aabb2: TAABB;
  476. const M1To2, M2To1: TGLMatrix): Boolean;
  477. const
  478. CWires: array [0 .. 11, 0 .. 1] of Integer // Points of the wire
  479. = ((0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (0, 4),
  480. (1, 5), (2, 6), (3, 7));
  481. CPlanes: array [0 .. 5, 0 .. 3] of Integer // points of the planes
  482. = ((1, 2, 6, 5), (2, 3, 7, 6), (0, 1, 2, 3), (0, 3, 7, 4), (0, 1, 5, 4),
  483. (5, 6, 7, 4));
  484. procedure MakeAABBPoints(const AABB: TAABB; var Pt: array of TVertex);
  485. begin
  486. with AABB do
  487. begin
  488. SetVector(Pt[0], Min.X, Min.Y, Min.Z);
  489. SetVector(Pt[1], Max.X, Min.Y, Min.Z);
  490. SetVector(Pt[2], Max.X, Max.Y, Min.Z);
  491. SetVector(Pt[3], Min.X, Max.Y, Min.Z);
  492. SetVector(Pt[4], Min.X, Min.Y, Max.Z);
  493. SetVector(Pt[5], Max.X, Min.Y, Max.Z);
  494. SetVector(Pt[6], Max.X, Max.Y, Max.Z);
  495. SetVector(Pt[7], Min.X, Max.Y, Max.Z);
  496. end;
  497. end;
  498. procedure MakePlanes(const Pt: array of TVertex;
  499. var Planes: array of THmgPlane);
  500. var
  501. I: Integer;
  502. begin
  503. for I := 0 to 5 do
  504. Planes[I] := PlaneMake(Pt[CPlanes[I, 0]], Pt[CPlanes[I, 1]],
  505. Pt[CPlanes[I, 2]]);
  506. end;
  507. var
  508. Pt1, Pt2: array [0 .. 7] of TVertex;
  509. Pt: TVertex;
  510. Planes2: array [0 .. 5] of THmgPlane;
  511. I, T: Integer;
  512. V: TVertex;
  513. P: TGLVector;
  514. begin
  515. Result := False;
  516. // Build Points
  517. MakeAABBPoints(AABB1, Pt1);
  518. MakeAABBPoints(AABB2, Pt2);
  519. for I := 0 to 7 do
  520. begin
  521. Pt := VectorTransform(Pt2[I], M2To1);
  522. // check for inclusion (points of Obj2 in Obj1)
  523. if IsInRange(Pt.X, AABB1.Min.X, AABB1.Max.X) and
  524. IsInRange(Pt.Y, AABB1.Min.Y, AABB1.Max.Y) and
  525. IsInRange(Pt.Z, AABB1.Min.Z, AABB1.Max.Z) then
  526. begin
  527. Result := True;
  528. Exit;
  529. end;
  530. end;
  531. for I := 0 to 7 do
  532. begin
  533. Pt1[I] := VectorTransform(Pt1[I], M1To2);
  534. // check for inclusion (points of Obj1 in Obj2)
  535. if IsInRange(Pt1[I].X, AABB2.Min.X, AABB2.Max.X) and
  536. IsInRange(Pt1[I].Y, AABB2.Min.Y, AABB2.Max.Y) and
  537. IsInRange(Pt1[I].Z, AABB2.Min.Z, AABB2.Max.Z) then
  538. begin
  539. Result := True;
  540. Exit;
  541. end;
  542. end;
  543. // Build Planes
  544. MakePlanes(Pt2, Planes2);
  545. // Wire test
  546. for I := 0 to 11 do
  547. begin
  548. for T := 0 to 5 do
  549. begin
  550. // Build Vector of Ray
  551. V := VectorSubtract(Pt1[CWires[I, 0]], Pt1[CWires[I, 1]]);
  552. if IntersectLinePlane(VectorMake(Pt1[CWires[I, 0]]), VectorMake(V),
  553. Planes2[T], @P) = 1 then
  554. begin
  555. // check point in Wire
  556. if IsInRange(P.X, Pt1[CWires[I, 0]].X,
  557. Pt1[CWires[I, 1]].X) and
  558. IsInRange(P.Y, Pt1[CWires[I, 0]].Y,
  559. Pt1[CWires[I, 1]].Y) and
  560. IsInRange(P.Z, Pt1[CWires[I, 0]].Z,
  561. Pt1[CWires[I, 1]].Z) then
  562. begin
  563. // check point in Plane
  564. if IsInRange(P.X, Pt2[CPlanes[T, 0]].X,
  565. Pt2[CPlanes[T, 2]].X) and
  566. IsInRange(P.Y, Pt2[CPlanes[T, 0]].Y,
  567. Pt2[CPlanes[T, 2]].Y) and
  568. IsInRange(P.Z, Pt2[CPlanes[T, 0]].Z,
  569. Pt2[CPlanes[T, 2]].Z) then
  570. begin
  571. Result := True;
  572. Exit;
  573. end;
  574. end;
  575. end;
  576. end;
  577. end;
  578. end;
  579. function IntersectAABBsAbsoluteXY(const Aabb1, Aabb2: TAABB): Boolean;
  580. begin
  581. Result := False;
  582. if (AABB2.Min.X > AABB1.Max.X) or
  583. (AABB2.Min.Y > AABB1.Max.Y) then
  584. Exit
  585. else if (AABB2.Max.X < AABB1.Min.X) or
  586. (AABB2.Max.Y < AABB1.Min.Y) then
  587. Exit
  588. else
  589. Result := True;
  590. end;
  591. function IntersectAABBsAbsoluteXZ(const Aabb1, Aabb2: TAABB): Boolean;
  592. begin
  593. Result := ((AABB1.Min.X < AABB2.Max.X) and
  594. (AABB1.Min.Z < AABB2.Max.Z) and
  595. (AABB2.Min.X < AABB1.Max.X) and
  596. (AABB2.Min.Z < AABB1.Max.Z));
  597. end;
  598. function IntersectAABBsAbsolute(const Aabb1, Aabb2: TAABB): Boolean;
  599. begin
  600. Result := not((AABB1.Min.X > AABB2.Max.X) or
  601. (AABB1.Min.Y > AABB2.Max.Y) or
  602. (AABB1.Min.Z > AABB2.Max.Z) or
  603. (AABB2.Min.X > AABB1.Max.X) or
  604. (AABB2.Min.Y > AABB1.Max.Y) or
  605. (AABB2.Min.Z > AABB1.Max.Z));
  606. end;
  607. function AABBFitsInAABBAbsolute(const Aabb1, Aabb2: TAABB): Boolean;
  608. begin
  609. // AABB1 fits completely inside AABB2?
  610. // AABB1 min must be >= to AABB2 min
  611. // AABB1 max must be <= to AABB2 max
  612. Result := (AABB1.Min.X >= AABB2.Min.X) and
  613. (AABB1.Min.Y >= AABB2.Min.Y) and
  614. (AABB1.Min.Z >= AABB2.Min.Z) and
  615. (AABB1.Max.X <= AABB2.Max.X) and
  616. (AABB1.Max.Y <= AABB2.Max.Y) and
  617. (AABB1.Max.Z <= AABB2.Max.Z);
  618. end;
  619. function PointInAABB(const P: TAffineVector; const Aabb: TAABB): Boolean;
  620. begin
  621. Result := (P.X <= Aabb.Max.X) and
  622. (P.X >= Aabb.Min.X) and (P.Y <= Aabb.Max.Y) and
  623. (P.Y >= Aabb.Min.Y) and (P.Z <= Aabb.Max.Z) and
  624. (P.Z >= Aabb.Min.Z);
  625. end;
  626. function PointInAABB(const P: TGLVector; const Aabb: TAABB): Boolean;
  627. begin
  628. Result := (P.X <= Aabb.Max.X) and
  629. (P.X >= Aabb.Min.X) and (P.Y <= Aabb.Max.Y) and
  630. (P.Y >= Aabb.Min.Y) and (P.Z <= Aabb.Max.Z) and
  631. (P.Z >= Aabb.Min.Z);
  632. end;
  633. function PlaneIntersectAABB(const Normal: TAffineVector; D: Single; const Aabb: TAABB): Boolean;
  634. var
  635. Vmax, Vmin: TAffineVector;
  636. I: Integer;
  637. begin
  638. Result := False;
  639. for I := 0 to 2 do
  640. if Normal.V[I] > 0.0 then
  641. begin
  642. VMin.V[I] := Aabb.Min.V[I];
  643. VMax.V[I] := Aabb.Max.V[I];
  644. end
  645. else
  646. begin
  647. VMin.V[I] := Aabb.Max.V[I];
  648. VMax.V[I] := Aabb.Min.V[I];
  649. end;
  650. if VectorDotProduct(Normal, Vmin) + D > 0 then
  651. Exit;
  652. if VectorDotProduct(Normal, Vmax) + D >= 0 then
  653. Result := True;
  654. end;
  655. procedure FindMinMax(X0, X1, X2: Single; out Min, Max: Single);
  656. begin
  657. Min := X0;
  658. Max := X0;
  659. if (X1 < Min) then
  660. Min := X1;
  661. if (X1 > Max) then
  662. Max := X1;
  663. if (X2 < Min) then
  664. Min := X2;
  665. if (X2 > Max) then
  666. Max := X2;
  667. end;
  668. function PlaneAABBIntersection(const plane : THmgPlane;const AABB : TAABB) : TgxAffineVectorList;
  669. var
  670. i, j, annexe : Integer;
  671. index : array[0..2] of Integer;
  672. vec, temp : TVector3f;
  673. box : array [0..1] of TVector3f;
  674. V: array [0..7] of TVector3f;
  675. function EdgesStripPlaneIntersection(const pt0, pt1, pt4, pt7: TVector3f;
  676. const plane : THmgPlane; var inter : TVector3f): Boolean;
  677. begin
  678. Result := True;
  679. if not SegmentPlaneIntersection(pt0, pt1, plane, inter) then
  680. if not SegmentPlaneIntersection(pt1, pt4, plane, inter) then
  681. if not SegmentPlaneIntersection(pt4, pt7, plane, inter) then
  682. Result := False;
  683. end;
  684. begin
  685. box[0] := AABB.min;
  686. box[1] := AABB.max;
  687. Result := TgxAffineVectorList.Create;
  688. // loop on vertices
  689. for i := 0 to 7 do
  690. begin
  691. for j := 0 to 2 do
  692. begin
  693. index[j] := (i div (1 shl j)) mod 2;
  694. vec.V[j] := box[index[j]].V[j];
  695. end;
  696. // try to find the right orientation to proceed intersection
  697. if (i = 0) then
  698. begin
  699. // prepare V 0 -> 7 array
  700. V[0] := vec;
  701. for j := 0 to 5 do
  702. begin
  703. temp := vec;
  704. temp.V[j mod 3] := box[(index[j mod 3] + 1) mod 2].V[j mod 3];
  705. if (j div 3) > 0 then
  706. begin
  707. temp.V[(j+1) mod 3] := box[(index[(j+1) mod 3] + 1) mod 2].V[(j+1) mod 3];
  708. if (j div 3) > 1 then
  709. begin
  710. temp.V[(j+2) mod 3] := box[(index[(j+2) mod 3] + 1) mod 2].V[(j+2) mod 3];
  711. end;
  712. end;
  713. V[j+1] := temp;
  714. end;
  715. for j := 0 to 2 do
  716. vec.V[j] := box[(index[j]+1) mod 2].V[j];
  717. V[7] := vec;
  718. end;
  719. end;
  720. //compute edge plane intersections
  721. for j := 0 to 2 do
  722. begin
  723. if j = 0 then annexe := 6 else annexe := j+3;
  724. // computes intersection with annexe edge
  725. if SegmentPlaneIntersection(V[j+1], V[annexe], plane, temp) then
  726. Result.Add(temp);
  727. // computes intersection with edge strip from main vertex V0 to opposite vertex V7
  728. if EdgesStripPlaneIntersection(V[0], V[j+1], V[j+4], V[7], plane, temp) then
  729. Result.Add(temp);
  730. end;
  731. end;
  732. function PlaneBoxOverlap(const Normal: TAffineVector; D: Single;
  733. const Maxbox: TAffineVector): Boolean;
  734. var
  735. Q: Integer;
  736. Vmin, Vmax: TAffineVector;
  737. begin
  738. Result := False;
  739. for Q := 0 to 2 do
  740. begin
  741. if (Normal.V[Q] > 0.0) then
  742. begin
  743. Vmin.V[Q] := -Maxbox.V[Q];
  744. Vmax.V[Q] := Maxbox.V[Q];
  745. end
  746. else
  747. begin
  748. Vmin.V[Q] := Maxbox.V[Q];
  749. Vmax.V[Q] := -Maxbox.V[Q];
  750. end;
  751. end;
  752. if (VectorDotProduct(Normal, Vmin) + D) > 0 then
  753. Exit;
  754. if (VectorDotProduct(Normal, Vmax) + D) >= 0 then
  755. Result := True;
  756. end;
  757. function TriangleIntersectAABB(const Aabb: TAABB;
  758. const V1, V2, V3: TAffineVector): Boolean;
  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: TGLVector; 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: TGClipRect; 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: TGLMatrix;
  1140. ViewportSizeX, ViewportSizeY: Integer): TGClipRect;
  1141. var
  1142. I: Integer;
  1143. V, Vt: TGLVector;
  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: TGLVector;
  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: TGLVector;
  1212. const Aabb: TAABB; IntersectPoint: PGLVector = 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.