Rect3.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #include "Rect3.hpp"
  2. #include "Vector3.hpp"
  3. #include "Plane.hpp"
  4. #include <algorithm>
  5. namespace godot {
  6. bool Rect3::intersects(const Rect3& p_aabb) const {
  7. if ( pos.x >= (p_aabb.pos.x + p_aabb.size.x) )
  8. return false;
  9. if ( (pos.x+size.x) <= p_aabb.pos.x )
  10. return false;
  11. if ( pos.y >= (p_aabb.pos.y + p_aabb.size.y) )
  12. return false;
  13. if ( (pos.y+size.y) <= p_aabb.pos.y )
  14. return false;
  15. if ( pos.z >= (p_aabb.pos.z + p_aabb.size.z) )
  16. return false;
  17. if ( (pos.z+size.z) <= p_aabb.pos.z )
  18. return false;
  19. return true;
  20. }
  21. bool Rect3::intersects_inclusive(const Rect3& p_aabb) const {
  22. if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) )
  23. return false;
  24. if ( (pos.x+size.x) < p_aabb.pos.x )
  25. return false;
  26. if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) )
  27. return false;
  28. if ( (pos.y+size.y) < p_aabb.pos.y )
  29. return false;
  30. if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) )
  31. return false;
  32. if ( (pos.z+size.z) < p_aabb.pos.z )
  33. return false;
  34. return true;
  35. }
  36. bool Rect3::encloses(const Rect3 & p_aabb) const {
  37. Vector3 src_min=pos;
  38. Vector3 src_max=pos+size;
  39. Vector3 dst_min=p_aabb.pos;
  40. Vector3 dst_max=p_aabb.pos+p_aabb.size;
  41. return (
  42. (src_min.x <= dst_min.x) &&
  43. (src_max.x > dst_max.x) &&
  44. (src_min.y <= dst_min.y) &&
  45. (src_max.y > dst_max.y) &&
  46. (src_min.z <= dst_min.z) &&
  47. (src_max.z > dst_max.z) );
  48. }
  49. Vector3 Rect3::get_support(const Vector3& p_normal) const {
  50. Vector3 half_extents = size * 0.5;
  51. Vector3 ofs = pos + half_extents;
  52. return Vector3(
  53. (p_normal.x>0) ? -half_extents.x : half_extents.x,
  54. (p_normal.y>0) ? -half_extents.y : half_extents.y,
  55. (p_normal.z>0) ? -half_extents.z : half_extents.z
  56. )+ofs;
  57. }
  58. Vector3 Rect3::get_endpoint(int p_point) const {
  59. switch(p_point) {
  60. case 0: return Vector3( pos.x , pos.y , pos.z );
  61. case 1: return Vector3( pos.x , pos.y , pos.z+size.z );
  62. case 2: return Vector3( pos.x , pos.y+size.y , pos.z );
  63. case 3: return Vector3( pos.x , pos.y+size.y , pos.z+size.z );
  64. case 4: return Vector3( pos.x+size.x , pos.y , pos.z );
  65. case 5: return Vector3( pos.x+size.x , pos.y , pos.z+size.z );
  66. case 6: return Vector3( pos.x+size.x , pos.y+size.y , pos.z );
  67. case 7: return Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
  68. };
  69. ERR_FAIL_V(Vector3());
  70. }
  71. bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const {
  72. Vector3 half_extents = size * 0.5;
  73. Vector3 ofs = pos + half_extents;
  74. for(int i=0;i<p_plane_count;i++) {
  75. const Plane &p=p_planes[i];
  76. Vector3 point(
  77. (p.normal.x>0) ? -half_extents.x : half_extents.x,
  78. (p.normal.y>0) ? -half_extents.y : half_extents.y,
  79. (p.normal.z>0) ? -half_extents.z : half_extents.z
  80. );
  81. point+=ofs;
  82. if (p.is_point_over(point))
  83. return false;
  84. }
  85. return true;
  86. }
  87. bool Rect3::has_point(const Vector3& p_point) const {
  88. if (p_point.x<pos.x)
  89. return false;
  90. if (p_point.y<pos.y)
  91. return false;
  92. if (p_point.z<pos.z)
  93. return false;
  94. if (p_point.x>pos.x+size.x)
  95. return false;
  96. if (p_point.y>pos.y+size.y)
  97. return false;
  98. if (p_point.z>pos.z+size.z)
  99. return false;
  100. return true;
  101. }
  102. void Rect3::expand_to(const Vector3& p_vector) {
  103. Vector3 begin=pos;
  104. Vector3 end=pos+size;
  105. if (p_vector.x<begin.x)
  106. begin.x=p_vector.x;
  107. if (p_vector.y<begin.y)
  108. begin.y=p_vector.y;
  109. if (p_vector.z<begin.z)
  110. begin.z=p_vector.z;
  111. if (p_vector.x>end.x)
  112. end.x=p_vector.x;
  113. if (p_vector.y>end.y)
  114. end.y=p_vector.y;
  115. if (p_vector.z>end.z)
  116. end.z=p_vector.z;
  117. pos=begin;
  118. size=end-begin;
  119. }
  120. void Rect3::project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const {
  121. Vector3 half_extents( size.x * 0.5, size.y * 0.5, size.z * 0.5 );
  122. Vector3 center( pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z );
  123. real_t length = p_plane.normal.abs().dot(half_extents);
  124. real_t distance = p_plane.distance_to( center );
  125. r_min = distance - length;
  126. r_max = distance + length;
  127. }
  128. real_t Rect3::get_longest_axis_size() const {
  129. real_t max_size=size.x;
  130. if (size.y > max_size ) {
  131. max_size=size.y;
  132. }
  133. if (size.z > max_size ) {
  134. max_size=size.z;
  135. }
  136. return max_size;
  137. }
  138. real_t Rect3::get_shortest_axis_size() const {
  139. real_t max_size=size.x;
  140. if (size.y < max_size ) {
  141. max_size=size.y;
  142. }
  143. if (size.z < max_size ) {
  144. max_size=size.z;
  145. }
  146. return max_size;
  147. }
  148. bool Rect3::smits_intersect_ray(const Vector3 &from,const Vector3& dir, real_t t0, real_t t1) const {
  149. real_t divx=1.0/dir.x;
  150. real_t divy=1.0/dir.y;
  151. real_t divz=1.0/dir.z;
  152. Vector3 upbound=pos+size;
  153. real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
  154. if (dir.x >= 0) {
  155. tmin = (pos.x - from.x) * divx;
  156. tmax = (upbound.x - from.x) * divx;
  157. }
  158. else {
  159. tmin = (upbound.x - from.x) * divx;
  160. tmax = (pos.x - from.x) * divx;
  161. }
  162. if (dir.y >= 0) {
  163. tymin = (pos.y - from.y) * divy;
  164. tymax = (upbound.y - from.y) * divy;
  165. }
  166. else {
  167. tymin = (upbound.y - from.y) * divy;
  168. tymax = (pos.y - from.y) * divy;
  169. }
  170. if ( (tmin > tymax) || (tymin > tmax) )
  171. return false;
  172. if (tymin > tmin)
  173. tmin = tymin;
  174. if (tymax < tmax)
  175. tmax = tymax;
  176. if (dir.z >= 0) {
  177. tzmin = (pos.z - from.z) * divz;
  178. tzmax = (upbound.z - from.z) * divz;
  179. }
  180. else {
  181. tzmin = (upbound.z - from.z) * divz;
  182. tzmax = (pos.z - from.z) * divz;
  183. }
  184. if ( (tmin > tzmax) || (tzmin > tmax) )
  185. return false;
  186. if (tzmin > tmin)
  187. tmin = tzmin;
  188. if (tzmax < tmax)
  189. tmax = tzmax;
  190. return ( (tmin < t1) && (tmax > t0) );
  191. }
  192. void Rect3::grow_by(real_t p_amount) {
  193. pos.x-=p_amount;
  194. pos.y-=p_amount;
  195. pos.z-=p_amount;
  196. size.x+=2.0*p_amount;
  197. size.y+=2.0*p_amount;
  198. size.z+=2.0*p_amount;
  199. }
  200. real_t Rect3::get_area() const {
  201. return size.x*size.y*size.z;
  202. }
  203. bool Rect3::operator==(const Rect3& p_rval) const {
  204. return ((pos==p_rval.pos) && (size==p_rval.size));
  205. }
  206. bool Rect3::operator!=(const Rect3& p_rval) const {
  207. return ((pos!=p_rval.pos) || (size!=p_rval.size));
  208. }
  209. void Rect3::merge_with(const Rect3& p_aabb) {
  210. Vector3 beg_1,beg_2;
  211. Vector3 end_1,end_2;
  212. Vector3 min,max;
  213. beg_1=pos;
  214. beg_2=p_aabb.pos;
  215. end_1=Vector3(size.x,size.y,size.z)+beg_1;
  216. end_2=Vector3(p_aabb.size.x,p_aabb.size.y,p_aabb.size.z)+beg_2;
  217. min.x=(beg_1.x<beg_2.x)?beg_1.x:beg_2.x;
  218. min.y=(beg_1.y<beg_2.y)?beg_1.y:beg_2.y;
  219. min.z=(beg_1.z<beg_2.z)?beg_1.z:beg_2.z;
  220. max.x=(end_1.x>end_2.x)?end_1.x:end_2.x;
  221. max.y=(end_1.y>end_2.y)?end_1.y:end_2.y;
  222. max.z=(end_1.z>end_2.z)?end_1.z:end_2.z;
  223. pos=min;
  224. size=max-min;
  225. }
  226. Rect3 Rect3::intersection(const Rect3& p_aabb) const {
  227. Vector3 src_min=pos;
  228. Vector3 src_max=pos+size;
  229. Vector3 dst_min=p_aabb.pos;
  230. Vector3 dst_max=p_aabb.pos+p_aabb.size;
  231. Vector3 min,max;
  232. if (src_min.x > dst_max.x || src_max.x < dst_min.x )
  233. return Rect3();
  234. else {
  235. min.x= ( src_min.x > dst_min.x ) ? src_min.x :dst_min.x;
  236. max.x= ( src_max.x < dst_max.x ) ? src_max.x :dst_max.x;
  237. }
  238. if (src_min.y > dst_max.y || src_max.y < dst_min.y )
  239. return Rect3();
  240. else {
  241. min.y= ( src_min.y > dst_min.y ) ? src_min.y :dst_min.y;
  242. max.y= ( src_max.y < dst_max.y ) ? src_max.y :dst_max.y;
  243. }
  244. if (src_min.z > dst_max.z || src_max.z < dst_min.z )
  245. return Rect3();
  246. else {
  247. min.z= ( src_min.z > dst_min.z ) ? src_min.z :dst_min.z;
  248. max.z= ( src_max.z < dst_max.z ) ? src_max.z :dst_max.z;
  249. }
  250. return Rect3( min, max-min );
  251. }
  252. bool Rect3::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip,Vector3* r_normal) const {
  253. Vector3 c1, c2;
  254. Vector3 end = pos+size;
  255. real_t near=-1e20;
  256. real_t far=1e20;
  257. int axis=0;
  258. for (int i=0;i<3;i++){
  259. if (p_dir[i] == 0){
  260. if ((p_from[i] < pos[i]) || (p_from[i] > end[i])) {
  261. return false;
  262. }
  263. } else { // ray not parallel to planes in this direction
  264. c1[i] = (pos[i] - p_from[i]) / p_dir[i];
  265. c2[i] = (end[i] - p_from[i]) / p_dir[i];
  266. if(c1[i] > c2[i]){
  267. std::swap(c1,c2);
  268. }
  269. if (c1[i] > near){
  270. near = c1[i];
  271. axis=i;
  272. }
  273. if (c2[i] < far){
  274. far = c2[i];
  275. }
  276. if( (near > far) || (far < 0) ){
  277. return false;
  278. }
  279. }
  280. }
  281. if (r_clip)
  282. *r_clip=c1;
  283. if (r_normal) {
  284. *r_normal=Vector3();
  285. (*r_normal)[axis]=p_dir[axis]?-1:1;
  286. }
  287. return true;
  288. }
  289. bool Rect3::intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip,Vector3* r_normal) const {
  290. real_t min=0,max=1;
  291. int axis=0;
  292. real_t sign=0;
  293. for(int i=0;i<3;i++) {
  294. real_t seg_from=p_from[i];
  295. real_t seg_to=p_to[i];
  296. real_t box_begin=pos[i];
  297. real_t box_end=box_begin+size[i];
  298. real_t cmin,cmax;
  299. real_t csign;
  300. if (seg_from < seg_to) {
  301. if (seg_from > box_end || seg_to < box_begin)
  302. return false;
  303. real_t length=seg_to-seg_from;
  304. cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
  305. cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
  306. csign=-1.0;
  307. } else {
  308. if (seg_to > box_end || seg_from < box_begin)
  309. return false;
  310. real_t length=seg_to-seg_from;
  311. cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
  312. cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
  313. csign=1.0;
  314. }
  315. if (cmin > min) {
  316. min = cmin;
  317. axis=i;
  318. sign=csign;
  319. }
  320. if (cmax < max)
  321. max = cmax;
  322. if (max < min)
  323. return false;
  324. }
  325. Vector3 rel=p_to-p_from;
  326. if (r_normal) {
  327. Vector3 normal;
  328. normal[axis]=sign;
  329. *r_normal=normal;
  330. }
  331. if (r_clip)
  332. *r_clip=p_from+rel*min;
  333. return true;
  334. }
  335. bool Rect3::intersects_plane(const Plane &p_plane) const {
  336. Vector3 points[8] = {
  337. Vector3( pos.x , pos.y , pos.z ),
  338. Vector3( pos.x , pos.y , pos.z+size.z ),
  339. Vector3( pos.x , pos.y+size.y , pos.z ),
  340. Vector3( pos.x , pos.y+size.y , pos.z+size.z ),
  341. Vector3( pos.x+size.x , pos.y , pos.z ),
  342. Vector3( pos.x+size.x , pos.y , pos.z+size.z ),
  343. Vector3( pos.x+size.x , pos.y+size.y , pos.z ),
  344. Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ),
  345. };
  346. bool over=false;
  347. bool under=false;
  348. for (int i=0;i<8;i++) {
  349. if (p_plane.distance_to(points[i])>0)
  350. over=true;
  351. else
  352. under=true;
  353. }
  354. return under && over;
  355. }
  356. Vector3 Rect3::get_longest_axis() const {
  357. Vector3 axis(1,0,0);
  358. real_t max_size=size.x;
  359. if (size.y > max_size ) {
  360. axis=Vector3(0,1,0);
  361. max_size=size.y;
  362. }
  363. if (size.z > max_size ) {
  364. axis=Vector3(0,0,1);
  365. max_size=size.z;
  366. }
  367. return axis;
  368. }
  369. int Rect3::get_longest_axis_index() const {
  370. int axis=0;
  371. real_t max_size=size.x;
  372. if (size.y > max_size ) {
  373. axis=1;
  374. max_size=size.y;
  375. }
  376. if (size.z > max_size ) {
  377. axis=2;
  378. max_size=size.z;
  379. }
  380. return axis;
  381. }
  382. Vector3 Rect3::get_shortest_axis() const {
  383. Vector3 axis(1,0,0);
  384. real_t max_size=size.x;
  385. if (size.y < max_size ) {
  386. axis=Vector3(0,1,0);
  387. max_size=size.y;
  388. }
  389. if (size.z < max_size ) {
  390. axis=Vector3(0,0,1);
  391. max_size=size.z;
  392. }
  393. return axis;
  394. }
  395. int Rect3::get_shortest_axis_index() const {
  396. int axis=0;
  397. real_t max_size=size.x;
  398. if (size.y < max_size ) {
  399. axis=1;
  400. max_size=size.y;
  401. }
  402. if (size.z < max_size ) {
  403. axis=2;
  404. max_size=size.z;
  405. }
  406. return axis;
  407. }
  408. Rect3 Rect3::merge(const Rect3& p_with) const {
  409. Rect3 aabb=*this;
  410. aabb.merge_with(p_with);
  411. return aabb;
  412. }
  413. Rect3 Rect3::expand(const Vector3& p_vector) const {
  414. Rect3 aabb=*this;
  415. aabb.expand_to(p_vector);
  416. return aabb;
  417. }
  418. Rect3 Rect3::grow(real_t p_by) const {
  419. Rect3 aabb=*this;
  420. aabb.grow_by(p_by);
  421. return aabb;
  422. }
  423. void Rect3::get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const {
  424. ERR_FAIL_INDEX(p_edge,12);
  425. switch(p_edge) {
  426. case 0:{
  427. r_from=Vector3( pos.x+size.x , pos.y , pos.z );
  428. r_to=Vector3( pos.x , pos.y , pos.z );
  429. } break;
  430. case 1:{
  431. r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
  432. r_to=Vector3( pos.x+size.x , pos.y , pos.z );
  433. } break;
  434. case 2:{
  435. r_from=Vector3( pos.x , pos.y , pos.z+size.z );
  436. r_to=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
  437. } break;
  438. case 3:{
  439. r_from=Vector3( pos.x , pos.y , pos.z );
  440. r_to=Vector3( pos.x , pos.y , pos.z+size.z );
  441. } break;
  442. case 4:{
  443. r_from=Vector3( pos.x , pos.y+size.y , pos.z );
  444. r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
  445. } break;
  446. case 5:{
  447. r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
  448. r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
  449. } break;
  450. case 6:{
  451. r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
  452. r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
  453. } break;
  454. case 7:{
  455. r_from=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
  456. r_to=Vector3( pos.x , pos.y+size.y , pos.z );
  457. } break;
  458. case 8:{
  459. r_from=Vector3( pos.x , pos.y , pos.z+size.z );
  460. r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
  461. } break;
  462. case 9:{
  463. r_from=Vector3( pos.x , pos.y , pos.z );
  464. r_to=Vector3( pos.x , pos.y+size.y , pos.z );
  465. } break;
  466. case 10:{
  467. r_from=Vector3( pos.x+size.x , pos.y , pos.z );
  468. r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
  469. } break;
  470. case 11:{
  471. r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
  472. r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
  473. } break;
  474. }
  475. }
  476. Rect3::operator String() const {
  477. //return String()+pos +" - "+ size;
  478. return String(); // @Todo
  479. }
  480. }