dragplanes.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #if !defined(INCLUDED_DRAGPLANES_H)
  18. #define INCLUDED_DRAGPLANES_H
  19. #include "selectable.h"
  20. #include "selectionlib.h"
  21. #include "math/aabb.h"
  22. #include "math/line.h"
  23. class DragPlanes
  24. {
  25. public:
  26. ObservedSelectable m_selectable_right; // +x
  27. ObservedSelectable m_selectable_left; // -x
  28. ObservedSelectable m_selectable_front; // +y
  29. ObservedSelectable m_selectable_back; // -y
  30. ObservedSelectable m_selectable_top; // +z
  31. ObservedSelectable m_selectable_bottom; // -z
  32. AABB m_bounds;
  33. DragPlanes(const SelectionChangeCallback& onchanged) :
  34. m_selectable_right(onchanged),
  35. m_selectable_left(onchanged),
  36. m_selectable_front(onchanged),
  37. m_selectable_back(onchanged),
  38. m_selectable_top(onchanged),
  39. m_selectable_bottom(onchanged)
  40. {
  41. }
  42. bool isSelected() const
  43. {
  44. return m_selectable_right.isSelected()
  45. || m_selectable_left.isSelected()
  46. || m_selectable_front.isSelected()
  47. || m_selectable_back.isSelected()
  48. || m_selectable_top.isSelected()
  49. || m_selectable_bottom.isSelected();
  50. }
  51. void setSelected(bool selected)
  52. {
  53. m_selectable_right.setSelected(selected);
  54. m_selectable_left.setSelected(selected);
  55. m_selectable_front.setSelected(selected);
  56. m_selectable_back.setSelected(selected);
  57. m_selectable_top.setSelected(selected);
  58. m_selectable_bottom.setSelected(selected);
  59. }
  60. void selectPlanes(const AABB& aabb, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback)
  61. {
  62. Line line(test.getNear(), test.getFar());
  63. Vector3 corners[8];
  64. aabb_corners(aabb, corners);
  65. Plane3 planes[6];
  66. aabb_planes(aabb, planes);
  67. for(Vector3* i = corners; i != corners + 8; ++i)
  68. {
  69. *i = vector3_subtracted(line_closest_point(line, *i), *i);
  70. }
  71. if(vector3_dot(planes[0].normal(), corners[1]) > 0
  72. && vector3_dot(planes[0].normal(), corners[2]) > 0
  73. && vector3_dot(planes[0].normal(), corners[5]) > 0
  74. && vector3_dot(planes[0].normal(), corners[6]) > 0)
  75. {
  76. Selector_add(selector, m_selectable_right);
  77. selectedPlaneCallback(planes[0]);
  78. //globalOutputStream() << "right\n";
  79. }
  80. if(vector3_dot(planes[1].normal(), corners[0]) > 0
  81. && vector3_dot(planes[1].normal(), corners[3]) > 0
  82. && vector3_dot(planes[1].normal(), corners[4]) > 0
  83. && vector3_dot(planes[1].normal(), corners[7]) > 0)
  84. {
  85. Selector_add(selector, m_selectable_left);
  86. selectedPlaneCallback(planes[1]);
  87. //globalOutputStream() << "left\n";
  88. }
  89. if(vector3_dot(planes[2].normal(), corners[0]) > 0
  90. && vector3_dot(planes[2].normal(), corners[1]) > 0
  91. && vector3_dot(planes[2].normal(), corners[4]) > 0
  92. && vector3_dot(planes[2].normal(), corners[5]) > 0)
  93. {
  94. Selector_add(selector, m_selectable_front);
  95. selectedPlaneCallback(planes[2]);
  96. //globalOutputStream() << "front\n";
  97. }
  98. if(vector3_dot(planes[3].normal(), corners[2]) > 0
  99. && vector3_dot(planes[3].normal(), corners[3]) > 0
  100. && vector3_dot(planes[3].normal(), corners[6]) > 0
  101. && vector3_dot(planes[3].normal(), corners[7]) > 0)
  102. {
  103. Selector_add(selector, m_selectable_back);
  104. selectedPlaneCallback(planes[3]);
  105. //globalOutputStream() << "back\n";
  106. }
  107. if(vector3_dot(planes[4].normal(), corners[0]) > 0
  108. && vector3_dot(planes[4].normal(), corners[1]) > 0
  109. && vector3_dot(planes[4].normal(), corners[2]) > 0
  110. && vector3_dot(planes[4].normal(), corners[3]) > 0)
  111. {
  112. Selector_add(selector, m_selectable_top);
  113. selectedPlaneCallback(planes[4]);
  114. //globalOutputStream() << "top\n";
  115. }
  116. if(vector3_dot(planes[5].normal(), corners[4]) > 0
  117. && vector3_dot(planes[5].normal(), corners[5]) > 0
  118. && vector3_dot(planes[5].normal(), corners[6]) > 0
  119. && vector3_dot(planes[5].normal(), corners[7]) > 0)
  120. {
  121. Selector_add(selector, m_selectable_bottom);
  122. //globalOutputStream() << "bottom\n";
  123. selectedPlaneCallback(planes[5]);
  124. }
  125. m_bounds = aabb;
  126. }
  127. void selectReversedPlanes(const AABB& aabb, Selector& selector, const SelectedPlanes& selectedPlanes)
  128. {
  129. Plane3 planes[6];
  130. aabb_planes(aabb, planes);
  131. if(selectedPlanes.contains(plane3_flipped(planes[0])))
  132. {
  133. Selector_add(selector, m_selectable_right);
  134. }
  135. if(selectedPlanes.contains(plane3_flipped(planes[1])))
  136. {
  137. Selector_add(selector, m_selectable_left);
  138. }
  139. if(selectedPlanes.contains(plane3_flipped(planes[2])))
  140. {
  141. Selector_add(selector, m_selectable_front);
  142. }
  143. if(selectedPlanes.contains(plane3_flipped(planes[3])))
  144. {
  145. Selector_add(selector, m_selectable_back);
  146. }
  147. if(selectedPlanes.contains(plane3_flipped(planes[4])))
  148. {
  149. Selector_add(selector, m_selectable_top);
  150. }
  151. if(selectedPlanes.contains(plane3_flipped(planes[5])))
  152. {
  153. Selector_add(selector, m_selectable_bottom);
  154. }
  155. }
  156. Matrix4 evaluateTransform(const Vector3& translation) const
  157. {
  158. Vector3 min = m_bounds.origin - m_bounds.extents;
  159. Vector3 max = m_bounds.origin + m_bounds.extents;
  160. Vector3 origin = m_bounds.origin;
  161. Vector3 extents = m_bounds.extents;
  162. if(extents[0] != 0)
  163. {
  164. if(m_selectable_right.isSelected())
  165. {
  166. max[0] += translation[0];
  167. //globalOutputStream() << "moving right\n";
  168. }
  169. if(m_selectable_left.isSelected())
  170. {
  171. min[0] += translation[0];
  172. //globalOutputStream() << "moving left\n";
  173. }
  174. }
  175. if(extents[1] != 0)
  176. {
  177. if(m_selectable_front.isSelected())
  178. {
  179. max[1] += translation[1];
  180. //globalOutputStream() << "moving front\n";
  181. }
  182. if(m_selectable_back.isSelected())
  183. {
  184. min[1] += translation[1];
  185. //globalOutputStream() << "moving back\n";
  186. }
  187. }
  188. if(extents[2] != 0)
  189. {
  190. if(m_selectable_top.isSelected())
  191. {
  192. max[2] += translation[2];
  193. //globalOutputStream() << "moving top\n";
  194. }
  195. if(m_selectable_bottom.isSelected())
  196. {
  197. min[2] += translation[2];
  198. //globalOutputStream() << "moving bottom\n";
  199. }
  200. }
  201. Vector3 originTransformed(vector3_mid(min, max));
  202. Vector3 scale(vector3_scaled(vector3_subtracted(max, min), 0.5));
  203. if(extents[0] != 0)
  204. {
  205. scale[0] /= extents[0];
  206. }
  207. else
  208. {
  209. scale[0] = 1;
  210. }
  211. if(extents[1] != 0)
  212. {
  213. scale[1] /= extents[1];
  214. }
  215. else
  216. {
  217. scale[1] = 1;
  218. }
  219. if(extents[2] != 0)
  220. {
  221. scale[2] /= extents[2];
  222. }
  223. else
  224. {
  225. scale[2] = 1;
  226. }
  227. Matrix4 matrix(matrix4_translation_for_vec3(originTransformed - origin));
  228. matrix4_pivoted_scale_by_vec3(matrix, scale, origin);
  229. return matrix;
  230. }
  231. };
  232. #endif