physicsObjectCollection.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Filename: physicsObjectCollection.cxx
  2. // Created by: joswilso (12Jul06)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "physicsObjectCollection.h"
  15. #include "indent.h"
  16. ////////////////////////////////////////////////////////////////////
  17. // Function: PhysicsObjectCollection::Constructor
  18. // Access: Published
  19. // Description:
  20. ////////////////////////////////////////////////////////////////////
  21. PhysicsObjectCollection::
  22. PhysicsObjectCollection() {
  23. }
  24. ////////////////////////////////////////////////////////////////////
  25. // Function: PhysicsObjectCollection::Copy Constructor
  26. // Access: Published
  27. // Description:
  28. ////////////////////////////////////////////////////////////////////
  29. PhysicsObjectCollection::
  30. PhysicsObjectCollection(const PhysicsObjectCollection &copy) :
  31. _physics_objects(copy._physics_objects)
  32. {
  33. }
  34. ////////////////////////////////////////////////////////////////////
  35. // Function: PhysicsObjectCollection::Copy Assignment Operator
  36. // Access: Published
  37. // Description:
  38. ////////////////////////////////////////////////////////////////////
  39. void PhysicsObjectCollection::
  40. operator = (const PhysicsObjectCollection &copy) {
  41. _physics_objects = copy._physics_objects;
  42. }
  43. ////////////////////////////////////////////////////////////////////
  44. // Function: PhysicsObjectCollection::add_physics_object
  45. // Access: Published
  46. // Description: Adds a new PhysicsObject to the collection.
  47. ////////////////////////////////////////////////////////////////////
  48. void PhysicsObjectCollection::
  49. add_physics_object(PT(PhysicsObject) physics_object) {
  50. // If the pointer to our internal array is shared by any other
  51. // PhysicsObjectCollections, we have to copy the array now so we won't
  52. // inadvertently modify any of our brethren PhysicsObjectCollection
  53. // objects.
  54. if (_physics_objects.get_ref_count() > 1) {
  55. PhysicsObjects old_physics_objects = _physics_objects;
  56. _physics_objects = PhysicsObjects::empty_array(0);
  57. _physics_objects.v() = old_physics_objects.v();
  58. }
  59. _physics_objects.push_back(physics_object);
  60. }
  61. ////////////////////////////////////////////////////////////////////
  62. // Function: PhysicsObjectCollection::remove_physics_object
  63. // Access: Published
  64. // Description: Removes the indicated PhysicsObject from the collection.
  65. // Returns true if the physics_object was removed, false if it was
  66. // not a member of the collection.
  67. ////////////////////////////////////////////////////////////////////
  68. bool PhysicsObjectCollection::
  69. remove_physics_object(PT(PhysicsObject) physics_object) {
  70. int object_index = -1;
  71. for (int i = 0; object_index == -1 && i < (int)_physics_objects.size(); i++) {
  72. if (_physics_objects[i] == physics_object) {
  73. object_index = i;
  74. }
  75. }
  76. if (object_index == -1) {
  77. // The indicated physics_object was not a member of the collection.
  78. return false;
  79. }
  80. // If the pointer to our internal array is shared by any other
  81. // PhysicsObjectCollections, we have to copy the array now so we won't
  82. // inadvertently modify any of our brethren PhysicsObjectCollection
  83. // objects.
  84. if (_physics_objects.get_ref_count() > 1) {
  85. PhysicsObjects old_physics_objects = _physics_objects;
  86. _physics_objects = PhysicsObjects::empty_array(0);
  87. _physics_objects.v() = old_physics_objects.v();
  88. }
  89. _physics_objects.erase(_physics_objects.begin() + object_index);
  90. return true;
  91. }
  92. ////////////////////////////////////////////////////////////////////
  93. // Function: PhysicsObjectCollection::add_physics_objects_from
  94. // Access: Published
  95. // Description: Adds all the PhysicsObjects indicated in the other
  96. // collection to this collection. The other
  97. // physics_objects are simply appended to the end of
  98. // the physics_objects in this list;
  99. // duplicates are not automatically removed.
  100. ////////////////////////////////////////////////////////////////////
  101. void PhysicsObjectCollection::
  102. add_physics_objects_from(const PhysicsObjectCollection &other) {
  103. int other_num_physics_objects = other.get_num_physics_objects();
  104. for (int i = 0; i < other_num_physics_objects; i++) {
  105. add_physics_object(other.get_physics_object(i));
  106. }
  107. }
  108. ////////////////////////////////////////////////////////////////////
  109. // Function: PhysicsObjectCollection::remove_physics_objects_from
  110. // Access: Published
  111. // Description: Removes from this collection all of the PhysicsObjects
  112. // listed in the other collection.
  113. ////////////////////////////////////////////////////////////////////
  114. void PhysicsObjectCollection::
  115. remove_physics_objects_from(const PhysicsObjectCollection &other) {
  116. PhysicsObjects new_physics_objects;
  117. int num_physics_objects = get_num_physics_objects();
  118. for (int i = 0; i < num_physics_objects; i++) {
  119. PT(PhysicsObject) physics_object = get_physics_object(i);
  120. if (!other.has_physics_object(physics_object)) {
  121. new_physics_objects.push_back(physics_object);
  122. }
  123. }
  124. _physics_objects = new_physics_objects;
  125. }
  126. ////////////////////////////////////////////////////////////////////
  127. // Function: PhysicsObjectCollection::remove_duplicate_physics_objects
  128. // Access: Published
  129. // Description: Removes any duplicate entries of the same PhysicsObjects
  130. // on this collection. If a PhysicsObject appears multiple
  131. // times, the first appearance is retained; subsequent
  132. // appearances are removed.
  133. ////////////////////////////////////////////////////////////////////
  134. void PhysicsObjectCollection::
  135. remove_duplicate_physics_objects() {
  136. PhysicsObjects new_physics_objects;
  137. int num_physics_objects = get_num_physics_objects();
  138. for (int i = 0; i < num_physics_objects; i++) {
  139. PT(PhysicsObject) physics_object = get_physics_object(i);
  140. bool duplicated = false;
  141. for (int j = 0; j < i && !duplicated; j++) {
  142. duplicated = (physics_object == get_physics_object(j));
  143. }
  144. if (!duplicated) {
  145. new_physics_objects.push_back(physics_object);
  146. }
  147. }
  148. _physics_objects = new_physics_objects;
  149. }
  150. ////////////////////////////////////////////////////////////////////
  151. // Function: PhysicsObjectCollection::has_physics_object
  152. // Access: Published
  153. // Description: Returns true if the indicated PhysicsObject appears in
  154. // this collection, false otherwise.
  155. ////////////////////////////////////////////////////////////////////
  156. bool PhysicsObjectCollection::
  157. has_physics_object(PT(PhysicsObject) physics_object) const {
  158. for (int i = 0; i < get_num_physics_objects(); i++) {
  159. if (physics_object == get_physics_object(i)) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. ////////////////////////////////////////////////////////////////////
  166. // Function: PhysicsObjectCollection::clear
  167. // Access: Published
  168. // Description: Removes all PhysicsObjects from the collection.
  169. ////////////////////////////////////////////////////////////////////
  170. void PhysicsObjectCollection::
  171. clear() {
  172. _physics_objects.clear();
  173. }
  174. ////////////////////////////////////////////////////////////////////
  175. // Function: PhysicsObjectCollection::is_empty
  176. // Access: Published
  177. // Description: Returns true if there are no PhysicsObjects in the
  178. // collection, false otherwise.
  179. ////////////////////////////////////////////////////////////////////
  180. bool PhysicsObjectCollection::
  181. is_empty() const {
  182. return _physics_objects.empty();
  183. }
  184. ////////////////////////////////////////////////////////////////////
  185. // Function: PhysicsObjectCollection::get_num_physics_objects
  186. // Access: Published
  187. // Description: Returns the number of PhysicsObjects in the collection.
  188. ////////////////////////////////////////////////////////////////////
  189. int PhysicsObjectCollection::
  190. get_num_physics_objects() const {
  191. return _physics_objects.size();
  192. }
  193. ////////////////////////////////////////////////////////////////////
  194. // Function: PhysicsObjectCollection::get_physics_object
  195. // Access: Published
  196. // Description: Returns the nth PhysicsObject in the collection.
  197. ////////////////////////////////////////////////////////////////////
  198. PT(PhysicsObject) PhysicsObjectCollection::
  199. get_physics_object(int index) const {
  200. nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
  201. return _physics_objects[index];
  202. }
  203. ////////////////////////////////////////////////////////////////////
  204. // Function: PhysicsObjectCollection::operator []
  205. // Access: Published
  206. // Description: Returns the nth PhysicsObject in the collection. This is
  207. // the same as get_physics_object(), but it may be a more
  208. // convenient way to access it.
  209. ////////////////////////////////////////////////////////////////////
  210. PT(PhysicsObject) PhysicsObjectCollection::
  211. operator [] (int index) const {
  212. nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
  213. return _physics_objects[index];
  214. }
  215. ////////////////////////////////////////////////////////////////////
  216. // Function: PhysicsObjectCollection::size
  217. // Access: Published
  218. // Description: Returns the number of physics objects in the
  219. // collection. This is the same thing as
  220. // get_num_physics_objects().
  221. ////////////////////////////////////////////////////////////////////
  222. int PhysicsObjectCollection::
  223. size() const {
  224. return _physics_objects.size();
  225. }
  226. ////////////////////////////////////////////////////////////////////
  227. // Function: PhysicsObjectCollection::output
  228. // Access: Published
  229. // Description: Writes a brief one-line description of the
  230. // PhysicsObjectCollection to the indicated output stream.
  231. ////////////////////////////////////////////////////////////////////
  232. void PhysicsObjectCollection::
  233. output(ostream &out) const {
  234. if (get_num_physics_objects() == 1) {
  235. out << "1 PhysicsObject";
  236. } else {
  237. out << get_num_physics_objects() << " PhysicsObjects";
  238. }
  239. }
  240. ////////////////////////////////////////////////////////////////////
  241. // Function: PhysicsObjectCollection::write
  242. // Access: Published
  243. // Description: Writes a complete multi-line description of the
  244. // PhysicsObjectCollection to the indicated output stream.
  245. ////////////////////////////////////////////////////////////////////
  246. void PhysicsObjectCollection::
  247. write(ostream &out, int indent_level) const {
  248. for (int i = 0; i < get_num_physics_objects(); i++) {
  249. indent(out, indent_level) << get_physics_object(i) << "\n";
  250. }
  251. }