rect3.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*************************************************************************/
  2. /* rect3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "rect3.h"
  30. #include "print_string.h"
  31. real_t Rect3::get_area() const {
  32. return size.x*size.y*size.z;
  33. }
  34. bool Rect3::operator==(const Rect3& p_rval) const {
  35. return ((pos==p_rval.pos) && (size==p_rval.size));
  36. }
  37. bool Rect3::operator!=(const Rect3& p_rval) const {
  38. return ((pos!=p_rval.pos) || (size!=p_rval.size));
  39. }
  40. void Rect3::merge_with(const Rect3& p_aabb) {
  41. Vector3 beg_1,beg_2;
  42. Vector3 end_1,end_2;
  43. Vector3 min,max;
  44. beg_1=pos;
  45. beg_2=p_aabb.pos;
  46. end_1=Vector3(size.x,size.y,size.z)+beg_1;
  47. end_2=Vector3(p_aabb.size.x,p_aabb.size.y,p_aabb.size.z)+beg_2;
  48. min.x=(beg_1.x<beg_2.x)?beg_1.x:beg_2.x;
  49. min.y=(beg_1.y<beg_2.y)?beg_1.y:beg_2.y;
  50. min.z=(beg_1.z<beg_2.z)?beg_1.z:beg_2.z;
  51. max.x=(end_1.x>end_2.x)?end_1.x:end_2.x;
  52. max.y=(end_1.y>end_2.y)?end_1.y:end_2.y;
  53. max.z=(end_1.z>end_2.z)?end_1.z:end_2.z;
  54. pos=min;
  55. size=max-min;
  56. }
  57. Rect3 Rect3::intersection(const Rect3& p_aabb) const {
  58. Vector3 src_min=pos;
  59. Vector3 src_max=pos+size;
  60. Vector3 dst_min=p_aabb.pos;
  61. Vector3 dst_max=p_aabb.pos+p_aabb.size;
  62. Vector3 min,max;
  63. if (src_min.x > dst_max.x || src_max.x < dst_min.x )
  64. return Rect3();
  65. else {
  66. min.x= ( src_min.x > dst_min.x ) ? src_min.x :dst_min.x;
  67. max.x= ( src_max.x < dst_max.x ) ? src_max.x :dst_max.x;
  68. }
  69. if (src_min.y > dst_max.y || src_max.y < dst_min.y )
  70. return Rect3();
  71. else {
  72. min.y= ( src_min.y > dst_min.y ) ? src_min.y :dst_min.y;
  73. max.y= ( src_max.y < dst_max.y ) ? src_max.y :dst_max.y;
  74. }
  75. if (src_min.z > dst_max.z || src_max.z < dst_min.z )
  76. return Rect3();
  77. else {
  78. min.z= ( src_min.z > dst_min.z ) ? src_min.z :dst_min.z;
  79. max.z= ( src_max.z < dst_max.z ) ? src_max.z :dst_max.z;
  80. }
  81. return Rect3( min, max-min );
  82. }
  83. bool Rect3::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip,Vector3* r_normal) const {
  84. Vector3 c1, c2;
  85. Vector3 end = pos+size;
  86. real_t near=-1e20;
  87. real_t far=1e20;
  88. int axis=0;
  89. for (int i=0;i<3;i++){
  90. if (p_dir[i] == 0){
  91. if ((p_from[i] < pos[i]) || (p_from[i] > end[i])) {
  92. return false;
  93. }
  94. } else { // ray not parallel to planes in this direction
  95. c1[i] = (pos[i] - p_from[i]) / p_dir[i];
  96. c2[i] = (end[i] - p_from[i]) / p_dir[i];
  97. if(c1[i] > c2[i]){
  98. SWAP(c1,c2);
  99. }
  100. if (c1[i] > near){
  101. near = c1[i];
  102. axis=i;
  103. }
  104. if (c2[i] < far){
  105. far = c2[i];
  106. }
  107. if( (near > far) || (far < 0) ){
  108. return false;
  109. }
  110. }
  111. }
  112. if (r_clip)
  113. *r_clip=c1;
  114. if (r_normal) {
  115. *r_normal=Vector3();
  116. (*r_normal)[axis]=p_dir[axis]?-1:1;
  117. }
  118. return true;
  119. }
  120. bool Rect3::intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip,Vector3* r_normal) const {
  121. real_t min=0,max=1;
  122. int axis=0;
  123. real_t sign=0;
  124. for(int i=0;i<3;i++) {
  125. real_t seg_from=p_from[i];
  126. real_t seg_to=p_to[i];
  127. real_t box_begin=pos[i];
  128. real_t box_end=box_begin+size[i];
  129. real_t cmin,cmax;
  130. real_t csign;
  131. if (seg_from < seg_to) {
  132. if (seg_from > box_end || seg_to < box_begin)
  133. return false;
  134. real_t length=seg_to-seg_from;
  135. cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
  136. cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
  137. csign=-1.0;
  138. } else {
  139. if (seg_to > box_end || seg_from < box_begin)
  140. return false;
  141. real_t length=seg_to-seg_from;
  142. cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
  143. cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
  144. csign=1.0;
  145. }
  146. if (cmin > min) {
  147. min = cmin;
  148. axis=i;
  149. sign=csign;
  150. }
  151. if (cmax < max)
  152. max = cmax;
  153. if (max < min)
  154. return false;
  155. }
  156. Vector3 rel=p_to-p_from;
  157. if (r_normal) {
  158. Vector3 normal;
  159. normal[axis]=sign;
  160. *r_normal=normal;
  161. }
  162. if (r_clip)
  163. *r_clip=p_from+rel*min;
  164. return true;
  165. }
  166. bool Rect3::intersects_plane(const Plane &p_plane) const {
  167. Vector3 points[8] = {
  168. Vector3( pos.x , pos.y , pos.z ),
  169. Vector3( pos.x , pos.y , pos.z+size.z ),
  170. Vector3( pos.x , pos.y+size.y , pos.z ),
  171. Vector3( pos.x , pos.y+size.y , pos.z+size.z ),
  172. Vector3( pos.x+size.x , pos.y , pos.z ),
  173. Vector3( pos.x+size.x , pos.y , pos.z+size.z ),
  174. Vector3( pos.x+size.x , pos.y+size.y , pos.z ),
  175. Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ),
  176. };
  177. bool over=false;
  178. bool under=false;
  179. for (int i=0;i<8;i++) {
  180. if (p_plane.distance_to(points[i])>0)
  181. over=true;
  182. else
  183. under=true;
  184. }
  185. return under && over;
  186. }
  187. Vector3 Rect3::get_longest_axis() const {
  188. Vector3 axis(1,0,0);
  189. real_t max_size=size.x;
  190. if (size.y > max_size ) {
  191. axis=Vector3(0,1,0);
  192. max_size=size.y;
  193. }
  194. if (size.z > max_size ) {
  195. axis=Vector3(0,0,1);
  196. max_size=size.z;
  197. }
  198. return axis;
  199. }
  200. int Rect3::get_longest_axis_index() const {
  201. int axis=0;
  202. real_t max_size=size.x;
  203. if (size.y > max_size ) {
  204. axis=1;
  205. max_size=size.y;
  206. }
  207. if (size.z > max_size ) {
  208. axis=2;
  209. max_size=size.z;
  210. }
  211. return axis;
  212. }
  213. Vector3 Rect3::get_shortest_axis() const {
  214. Vector3 axis(1,0,0);
  215. real_t max_size=size.x;
  216. if (size.y < max_size ) {
  217. axis=Vector3(0,1,0);
  218. max_size=size.y;
  219. }
  220. if (size.z < max_size ) {
  221. axis=Vector3(0,0,1);
  222. max_size=size.z;
  223. }
  224. return axis;
  225. }
  226. int Rect3::get_shortest_axis_index() const {
  227. int axis=0;
  228. real_t max_size=size.x;
  229. if (size.y < max_size ) {
  230. axis=1;
  231. max_size=size.y;
  232. }
  233. if (size.z < max_size ) {
  234. axis=2;
  235. max_size=size.z;
  236. }
  237. return axis;
  238. }
  239. Rect3 Rect3::merge(const Rect3& p_with) const {
  240. Rect3 aabb=*this;
  241. aabb.merge_with(p_with);
  242. return aabb;
  243. }
  244. Rect3 Rect3::expand(const Vector3& p_vector) const {
  245. Rect3 aabb=*this;
  246. aabb.expand_to(p_vector);
  247. return aabb;
  248. }
  249. Rect3 Rect3::grow(real_t p_by) const {
  250. Rect3 aabb=*this;
  251. aabb.grow_by(p_by);
  252. return aabb;
  253. }
  254. void Rect3::get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const {
  255. ERR_FAIL_INDEX(p_edge,12);
  256. switch(p_edge) {
  257. case 0:{
  258. r_from=Vector3( pos.x+size.x , pos.y , pos.z );
  259. r_to=Vector3( pos.x , pos.y , pos.z );
  260. } break;
  261. case 1:{
  262. r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
  263. r_to=Vector3( pos.x+size.x , pos.y , pos.z );
  264. } break;
  265. case 2:{
  266. r_from=Vector3( pos.x , pos.y , pos.z+size.z );
  267. r_to=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
  268. } break;
  269. case 3:{
  270. r_from=Vector3( pos.x , pos.y , pos.z );
  271. r_to=Vector3( pos.x , pos.y , pos.z+size.z );
  272. } break;
  273. case 4:{
  274. r_from=Vector3( pos.x , pos.y+size.y , pos.z );
  275. r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
  276. } break;
  277. case 5:{
  278. r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
  279. r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
  280. } break;
  281. case 6:{
  282. r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
  283. r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
  284. } break;
  285. case 7:{
  286. r_from=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
  287. r_to=Vector3( pos.x , pos.y+size.y , pos.z );
  288. } break;
  289. case 8:{
  290. r_from=Vector3( pos.x , pos.y , pos.z+size.z );
  291. r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
  292. } break;
  293. case 9:{
  294. r_from=Vector3( pos.x , pos.y , pos.z );
  295. r_to=Vector3( pos.x , pos.y+size.y , pos.z );
  296. } break;
  297. case 10:{
  298. r_from=Vector3( pos.x+size.x , pos.y , pos.z );
  299. r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
  300. } break;
  301. case 11:{
  302. r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
  303. r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
  304. } break;
  305. }
  306. }
  307. Rect3::operator String() const {
  308. return String()+pos +" - "+ size;
  309. }