The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

604 lines
13KB

  1. /*
  2. * Copyright (c) 2007-2009 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "b2Distance.h"
  19. #include "Shapes/b2CircleShape.h"
  20. #include "Shapes/b2EdgeShape.h"
  21. #include "Shapes/b2ChainShape.h"
  22. #include "Shapes/b2PolygonShape.h"
  23. // GJK using Voronoi regions (Christer Ericson) and Barycentric coordinates.
  24. int32 b2_gjkCalls, b2_gjkIters, b2_gjkMaxIters;
  25. void b2DistanceProxy::Set(const b2Shape* shape, int32 index)
  26. {
  27. switch (shape->GetType())
  28. {
  29. case b2Shape::e_circle:
  30. {
  31. const b2CircleShape* circle = (b2CircleShape*)shape;
  32. m_vertices = &circle->m_p;
  33. m_count = 1;
  34. m_radius = circle->m_radius;
  35. }
  36. break;
  37. case b2Shape::e_polygon:
  38. {
  39. const b2PolygonShape* polygon = (b2PolygonShape*)shape;
  40. m_vertices = polygon->m_vertices;
  41. m_count = polygon->m_vertexCount;
  42. m_radius = polygon->m_radius;
  43. }
  44. break;
  45. case b2Shape::e_chain:
  46. {
  47. const b2ChainShape* chain = (b2ChainShape*)shape;
  48. b2Assert(0 <= index && index < chain->m_count);
  49. m_buffer[0] = chain->m_vertices[index];
  50. if (index + 1 < chain->m_count)
  51. {
  52. m_buffer[1] = chain->m_vertices[index + 1];
  53. }
  54. else
  55. {
  56. m_buffer[1] = chain->m_vertices[0];
  57. }
  58. m_vertices = m_buffer;
  59. m_count = 2;
  60. m_radius = chain->m_radius;
  61. }
  62. break;
  63. case b2Shape::e_edge:
  64. {
  65. const b2EdgeShape* edge = (b2EdgeShape*)shape;
  66. m_vertices = &edge->m_vertex1;
  67. m_count = 2;
  68. m_radius = edge->m_radius;
  69. }
  70. break;
  71. default:
  72. b2Assert(false);
  73. }
  74. }
  75. struct b2SimplexVertex
  76. {
  77. b2Vec2 wA; // support point in proxyA
  78. b2Vec2 wB; // support point in proxyB
  79. b2Vec2 w; // wB - wA
  80. float32 a; // barycentric coordinate for closest point
  81. int32 indexA; // wA index
  82. int32 indexB; // wB index
  83. };
  84. struct b2Simplex
  85. {
  86. void ReadCache( const b2SimplexCache* cache,
  87. const b2DistanceProxy* proxyA, const b2Transform& transformA,
  88. const b2DistanceProxy* proxyB, const b2Transform& transformB)
  89. {
  90. b2Assert(cache->count <= 3);
  91. // Copy data from cache.
  92. m_count = cache->count;
  93. b2SimplexVertex* vertices = &m_v1;
  94. for (int32 i = 0; i < m_count; ++i)
  95. {
  96. b2SimplexVertex* v = vertices + i;
  97. v->indexA = cache->indexA[i];
  98. v->indexB = cache->indexB[i];
  99. b2Vec2 wALocal = proxyA->GetVertex(v->indexA);
  100. b2Vec2 wBLocal = proxyB->GetVertex(v->indexB);
  101. v->wA = b2Mul(transformA, wALocal);
  102. v->wB = b2Mul(transformB, wBLocal);
  103. v->w = v->wB - v->wA;
  104. v->a = 0.0f;
  105. }
  106. // Compute the new simplex metric, if it is substantially different than
  107. // old metric then flush the simplex.
  108. if (m_count > 1)
  109. {
  110. float32 metric1 = cache->metric;
  111. float32 metric2 = GetMetric();
  112. if (metric2 < 0.5f * metric1 || 2.0f * metric1 < metric2 || metric2 < b2_epsilon)
  113. {
  114. // Reset the simplex.
  115. m_count = 0;
  116. }
  117. }
  118. // If the cache is empty or invalid ...
  119. if (m_count == 0)
  120. {
  121. b2SimplexVertex* v = vertices + 0;
  122. v->indexA = 0;
  123. v->indexB = 0;
  124. b2Vec2 wALocal = proxyA->GetVertex(0);
  125. b2Vec2 wBLocal = proxyB->GetVertex(0);
  126. v->wA = b2Mul(transformA, wALocal);
  127. v->wB = b2Mul(transformB, wBLocal);
  128. v->w = v->wB - v->wA;
  129. m_count = 1;
  130. }
  131. }
  132. void WriteCache(b2SimplexCache* cache) const
  133. {
  134. cache->metric = GetMetric();
  135. cache->count = uint16(m_count);
  136. const b2SimplexVertex* vertices = &m_v1;
  137. for (int32 i = 0; i < m_count; ++i)
  138. {
  139. cache->indexA[i] = uint8(vertices[i].indexA);
  140. cache->indexB[i] = uint8(vertices[i].indexB);
  141. }
  142. }
  143. b2Vec2 GetSearchDirection() const
  144. {
  145. switch (m_count)
  146. {
  147. case 1:
  148. return -m_v1.w;
  149. case 2:
  150. {
  151. b2Vec2 e12 = m_v2.w - m_v1.w;
  152. float32 sgn = b2Cross(e12, -m_v1.w);
  153. if (sgn > 0.0f)
  154. {
  155. // Origin is left of e12.
  156. return b2Cross(1.0f, e12);
  157. }
  158. else
  159. {
  160. // Origin is right of e12.
  161. return b2Cross(e12, 1.0f);
  162. }
  163. }
  164. default:
  165. b2Assert(false);
  166. return b2Vec2_zero;
  167. }
  168. }
  169. b2Vec2 GetClosestPoint() const
  170. {
  171. switch (m_count)
  172. {
  173. case 0:
  174. b2Assert(false);
  175. return b2Vec2_zero;
  176. case 1:
  177. return m_v1.w;
  178. case 2:
  179. return m_v1.a * m_v1.w + m_v2.a * m_v2.w;
  180. case 3:
  181. return b2Vec2_zero;
  182. default:
  183. b2Assert(false);
  184. return b2Vec2_zero;
  185. }
  186. }
  187. void GetWitnessPoints(b2Vec2* pA, b2Vec2* pB) const
  188. {
  189. switch (m_count)
  190. {
  191. case 0:
  192. b2Assert(false);
  193. break;
  194. case 1:
  195. *pA = m_v1.wA;
  196. *pB = m_v1.wB;
  197. break;
  198. case 2:
  199. *pA = m_v1.a * m_v1.wA + m_v2.a * m_v2.wA;
  200. *pB = m_v1.a * m_v1.wB + m_v2.a * m_v2.wB;
  201. break;
  202. case 3:
  203. *pA = m_v1.a * m_v1.wA + m_v2.a * m_v2.wA + m_v3.a * m_v3.wA;
  204. *pB = *pA;
  205. break;
  206. default:
  207. b2Assert(false);
  208. break;
  209. }
  210. }
  211. float32 GetMetric() const
  212. {
  213. switch (m_count)
  214. {
  215. case 0:
  216. b2Assert(false);
  217. return 0.0f;
  218. case 1:
  219. return 0.0f;
  220. case 2:
  221. return b2Distance(m_v1.w, m_v2.w);
  222. case 3:
  223. return b2Cross(m_v2.w - m_v1.w, m_v3.w - m_v1.w);
  224. default:
  225. b2Assert(false);
  226. return 0.0f;
  227. }
  228. }
  229. void Solve2();
  230. void Solve3();
  231. b2SimplexVertex m_v1, m_v2, m_v3;
  232. int32 m_count;
  233. };
  234. // Solve a line segment using barycentric coordinates.
  235. //
  236. // p = a1 * w1 + a2 * w2
  237. // a1 + a2 = 1
  238. //
  239. // The vector from the origin to the closest point on the line is
  240. // perpendicular to the line.
  241. // e12 = w2 - w1
  242. // dot(p, e) = 0
  243. // a1 * dot(w1, e) + a2 * dot(w2, e) = 0
  244. //
  245. // 2-by-2 linear system
  246. // [1 1 ][a1] = [1]
  247. // [w1.e12 w2.e12][a2] = [0]
  248. //
  249. // Define
  250. // d12_1 = dot(w2, e12)
  251. // d12_2 = -dot(w1, e12)
  252. // d12 = d12_1 + d12_2
  253. //
  254. // Solution
  255. // a1 = d12_1 / d12
  256. // a2 = d12_2 / d12
  257. void b2Simplex::Solve2()
  258. {
  259. b2Vec2 w1 = m_v1.w;
  260. b2Vec2 w2 = m_v2.w;
  261. b2Vec2 e12 = w2 - w1;
  262. // w1 region
  263. float32 d12_2 = -b2Dot(w1, e12);
  264. if (d12_2 <= 0.0f)
  265. {
  266. // a2 <= 0, so we clamp it to 0
  267. m_v1.a = 1.0f;
  268. m_count = 1;
  269. return;
  270. }
  271. // w2 region
  272. float32 d12_1 = b2Dot(w2, e12);
  273. if (d12_1 <= 0.0f)
  274. {
  275. // a1 <= 0, so we clamp it to 0
  276. m_v2.a = 1.0f;
  277. m_count = 1;
  278. m_v1 = m_v2;
  279. return;
  280. }
  281. // Must be in e12 region.
  282. float32 inv_d12 = 1.0f / (d12_1 + d12_2);
  283. m_v1.a = d12_1 * inv_d12;
  284. m_v2.a = d12_2 * inv_d12;
  285. m_count = 2;
  286. }
  287. // Possible regions:
  288. // - points[2]
  289. // - edge points[0]-points[2]
  290. // - edge points[1]-points[2]
  291. // - inside the triangle
  292. void b2Simplex::Solve3()
  293. {
  294. b2Vec2 w1 = m_v1.w;
  295. b2Vec2 w2 = m_v2.w;
  296. b2Vec2 w3 = m_v3.w;
  297. // Edge12
  298. // [1 1 ][a1] = [1]
  299. // [w1.e12 w2.e12][a2] = [0]
  300. // a3 = 0
  301. b2Vec2 e12 = w2 - w1;
  302. float32 w1e12 = b2Dot(w1, e12);
  303. float32 w2e12 = b2Dot(w2, e12);
  304. float32 d12_1 = w2e12;
  305. float32 d12_2 = -w1e12;
  306. // Edge13
  307. // [1 1 ][a1] = [1]
  308. // [w1.e13 w3.e13][a3] = [0]
  309. // a2 = 0
  310. b2Vec2 e13 = w3 - w1;
  311. float32 w1e13 = b2Dot(w1, e13);
  312. float32 w3e13 = b2Dot(w3, e13);
  313. float32 d13_1 = w3e13;
  314. float32 d13_2 = -w1e13;
  315. // Edge23
  316. // [1 1 ][a2] = [1]
  317. // [w2.e23 w3.e23][a3] = [0]
  318. // a1 = 0
  319. b2Vec2 e23 = w3 - w2;
  320. float32 w2e23 = b2Dot(w2, e23);
  321. float32 w3e23 = b2Dot(w3, e23);
  322. float32 d23_1 = w3e23;
  323. float32 d23_2 = -w2e23;
  324. // Triangle123
  325. float32 n123 = b2Cross(e12, e13);
  326. float32 d123_1 = n123 * b2Cross(w2, w3);
  327. float32 d123_2 = n123 * b2Cross(w3, w1);
  328. float32 d123_3 = n123 * b2Cross(w1, w2);
  329. // w1 region
  330. if (d12_2 <= 0.0f && d13_2 <= 0.0f)
  331. {
  332. m_v1.a = 1.0f;
  333. m_count = 1;
  334. return;
  335. }
  336. // e12
  337. if (d12_1 > 0.0f && d12_2 > 0.0f && d123_3 <= 0.0f)
  338. {
  339. float32 inv_d12 = 1.0f / (d12_1 + d12_2);
  340. m_v1.a = d12_1 * inv_d12;
  341. m_v2.a = d12_2 * inv_d12;
  342. m_count = 2;
  343. return;
  344. }
  345. // e13
  346. if (d13_1 > 0.0f && d13_2 > 0.0f && d123_2 <= 0.0f)
  347. {
  348. float32 inv_d13 = 1.0f / (d13_1 + d13_2);
  349. m_v1.a = d13_1 * inv_d13;
  350. m_v3.a = d13_2 * inv_d13;
  351. m_count = 2;
  352. m_v2 = m_v3;
  353. return;
  354. }
  355. // w2 region
  356. if (d12_1 <= 0.0f && d23_2 <= 0.0f)
  357. {
  358. m_v2.a = 1.0f;
  359. m_count = 1;
  360. m_v1 = m_v2;
  361. return;
  362. }
  363. // w3 region
  364. if (d13_1 <= 0.0f && d23_1 <= 0.0f)
  365. {
  366. m_v3.a = 1.0f;
  367. m_count = 1;
  368. m_v1 = m_v3;
  369. return;
  370. }
  371. // e23
  372. if (d23_1 > 0.0f && d23_2 > 0.0f && d123_1 <= 0.0f)
  373. {
  374. float32 inv_d23 = 1.0f / (d23_1 + d23_2);
  375. m_v2.a = d23_1 * inv_d23;
  376. m_v3.a = d23_2 * inv_d23;
  377. m_count = 2;
  378. m_v1 = m_v3;
  379. return;
  380. }
  381. // Must be in triangle123
  382. float32 inv_d123 = 1.0f / (d123_1 + d123_2 + d123_3);
  383. m_v1.a = d123_1 * inv_d123;
  384. m_v2.a = d123_2 * inv_d123;
  385. m_v3.a = d123_3 * inv_d123;
  386. m_count = 3;
  387. }
  388. void b2Distance(b2DistanceOutput* output,
  389. b2SimplexCache* cache,
  390. const b2DistanceInput* input)
  391. {
  392. ++b2_gjkCalls;
  393. const b2DistanceProxy* proxyA = &input->proxyA;
  394. const b2DistanceProxy* proxyB = &input->proxyB;
  395. b2Transform transformA = input->transformA;
  396. b2Transform transformB = input->transformB;
  397. // Initialize the simplex.
  398. b2Simplex simplex;
  399. simplex.ReadCache(cache, proxyA, transformA, proxyB, transformB);
  400. // Get simplex vertices as an array.
  401. b2SimplexVertex* vertices = &simplex.m_v1;
  402. const int32 k_maxIters = 20;
  403. // These store the vertices of the last simplex so that we
  404. // can check for duplicates and prevent cycling.
  405. int32 saveA[3], saveB[3];
  406. int32 saveCount = 0;
  407. b2Vec2 closestPoint = simplex.GetClosestPoint();
  408. float32 distanceSqr1 = closestPoint.LengthSquared();
  409. float32 distanceSqr2;// = distanceSqr1;
  410. // Main iteration loop.
  411. int32 iter = 0;
  412. while (iter < k_maxIters)
  413. {
  414. // Copy simplex so we can identify duplicates.
  415. saveCount = simplex.m_count;
  416. for (int32 i = 0; i < saveCount; ++i)
  417. {
  418. saveA[i] = vertices[i].indexA;
  419. saveB[i] = vertices[i].indexB;
  420. }
  421. switch (simplex.m_count)
  422. {
  423. case 1:
  424. break;
  425. case 2:
  426. simplex.Solve2();
  427. break;
  428. case 3:
  429. simplex.Solve3();
  430. break;
  431. default:
  432. b2Assert(false);
  433. }
  434. // If we have 3 points, then the origin is in the corresponding triangle.
  435. if (simplex.m_count == 3)
  436. {
  437. break;
  438. }
  439. // Compute closest point.
  440. b2Vec2 p = simplex.GetClosestPoint();
  441. distanceSqr2 = p.LengthSquared();
  442. // Ensure progress
  443. if (distanceSqr2 >= distanceSqr1)
  444. {
  445. //break;
  446. }
  447. distanceSqr1 = distanceSqr2;
  448. // Get search direction.
  449. b2Vec2 d = simplex.GetSearchDirection();
  450. // Ensure the search direction is numerically fit.
  451. if (d.LengthSquared() < b2_epsilon * b2_epsilon)
  452. {
  453. // The origin is probably contained by a line segment
  454. // or triangle. Thus the shapes are overlapped.
  455. // We can't return zero here even though there may be overlap.
  456. // In case the simplex is a point, segment, or triangle it is difficult
  457. // to determine if the origin is contained in the CSO or very close to it.
  458. break;
  459. }
  460. // Compute a tentative new simplex vertex using support points.
  461. b2SimplexVertex* vertex = vertices + simplex.m_count;
  462. vertex->indexA = proxyA->GetSupport(b2MulT(transformA.q, -d));
  463. vertex->wA = b2Mul(transformA, proxyA->GetVertex(vertex->indexA));
  464. b2Vec2 wBLocal;
  465. vertex->indexB = proxyB->GetSupport(b2MulT(transformB.q, d));
  466. vertex->wB = b2Mul(transformB, proxyB->GetVertex(vertex->indexB));
  467. vertex->w = vertex->wB - vertex->wA;
  468. // Iteration count is equated to the number of support point calls.
  469. ++iter;
  470. ++b2_gjkIters;
  471. // Check for duplicate support points. This is the main termination criteria.
  472. bool duplicate = false;
  473. for (int32 i = 0; i < saveCount; ++i)
  474. {
  475. if (vertex->indexA == saveA[i] && vertex->indexB == saveB[i])
  476. {
  477. duplicate = true;
  478. break;
  479. }
  480. }
  481. // If we found a duplicate support point we must exit to avoid cycling.
  482. if (duplicate)
  483. {
  484. break;
  485. }
  486. // New vertex is ok and needed.
  487. ++simplex.m_count;
  488. }
  489. b2_gjkMaxIters = b2Max(b2_gjkMaxIters, iter);
  490. // Prepare output.
  491. simplex.GetWitnessPoints(&output->pointA, &output->pointB);
  492. output->distance = b2Distance(output->pointA, output->pointB);
  493. output->iterations = iter;
  494. // Cache the simplex.
  495. simplex.WriteCache(cache);
  496. // Apply radii if requested.
  497. if (input->useRadii)
  498. {
  499. float32 rA = proxyA->m_radius;
  500. float32 rB = proxyB->m_radius;
  501. if (output->distance > rA + rB && output->distance > b2_epsilon)
  502. {
  503. // Shapes are still no overlapped.
  504. // Move the witness points to the outer surface.
  505. output->distance -= rA + rB;
  506. b2Vec2 normal = output->pointB - output->pointA;
  507. normal.Normalize();
  508. output->pointA += rA * normal;
  509. output->pointB -= rB * normal;
  510. }
  511. else
  512. {
  513. // Shapes are overlapped when radii are considered.
  514. // Move the witness points to the middle.
  515. b2Vec2 p = 0.5f * (output->pointA + output->pointB);
  516. output->pointA = p;
  517. output->pointB = p;
  518. output->distance = 0.0f;
  519. }
  520. }
  521. }