Panda3D
 All Classes Functions Variables Enumerations
physicsObjectCollection.cxx
00001 // Filename: physicsObjectCollection.cxx
00002 // Created by:  joswilso (12Jul06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "physicsObjectCollection.h"
00016 
00017 #include "indent.h"
00018 
00019 ////////////////////////////////////////////////////////////////////
00020 //     Function: PhysicsObjectCollection::Constructor
00021 //       Access: Published
00022 //  Description:
00023 ////////////////////////////////////////////////////////////////////
00024 PhysicsObjectCollection::
00025 PhysicsObjectCollection() {
00026 }
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: PhysicsObjectCollection::Copy Constructor
00030 //       Access: Published
00031 //  Description:
00032 ////////////////////////////////////////////////////////////////////
00033 PhysicsObjectCollection::
00034 PhysicsObjectCollection(const PhysicsObjectCollection &copy) :
00035   _physics_objects(copy._physics_objects)
00036 {
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: PhysicsObjectCollection::Copy Assignment Operator
00041 //       Access: Published
00042 //  Description:
00043 ////////////////////////////////////////////////////////////////////
00044 void PhysicsObjectCollection::
00045 operator = (const PhysicsObjectCollection &copy) {
00046   _physics_objects = copy._physics_objects;
00047 }
00048 
00049 ////////////////////////////////////////////////////////////////////
00050 //     Function: PhysicsObjectCollection::add_physics_object
00051 //       Access: Published
00052 //  Description: Adds a new PhysicsObject to the collection.
00053 ////////////////////////////////////////////////////////////////////
00054 void PhysicsObjectCollection::
00055 add_physics_object(PT(PhysicsObject) physics_object) {
00056   // If the pointer to our internal array is shared by any other
00057   // PhysicsObjectCollections, we have to copy the array now so we won't
00058   // inadvertently modify any of our brethren PhysicsObjectCollection
00059   // objects.
00060 
00061   if (_physics_objects.get_ref_count() > 1) {
00062     PhysicsObjects old_physics_objects = _physics_objects;
00063     _physics_objects = PhysicsObjects::empty_array(0);
00064     _physics_objects.v() = old_physics_objects.v();
00065   }
00066 
00067   _physics_objects.push_back(physics_object);
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: PhysicsObjectCollection::remove_physics_object
00072 //       Access: Published
00073 //  Description: Removes the indicated PhysicsObject from the collection.
00074 //               Returns true if the physics_object was removed, false if it was
00075 //               not a member of the collection.
00076 ////////////////////////////////////////////////////////////////////
00077 bool PhysicsObjectCollection::
00078 remove_physics_object(PT(PhysicsObject) physics_object) {
00079   int object_index = -1;
00080   for (int i = 0; object_index == -1 && i < (int)_physics_objects.size(); i++) {
00081     if (_physics_objects[i] == physics_object) {
00082       object_index = i;
00083     }
00084   }
00085 
00086   if (object_index == -1) {
00087     // The indicated physics_object was not a member of the collection.
00088     return false;
00089   }
00090 
00091   // If the pointer to our internal array is shared by any other
00092   // PhysicsObjectCollections, we have to copy the array now so we won't
00093   // inadvertently modify any of our brethren PhysicsObjectCollection
00094   // objects.
00095 
00096   if (_physics_objects.get_ref_count() > 1) {
00097     PhysicsObjects old_physics_objects = _physics_objects;
00098     _physics_objects = PhysicsObjects::empty_array(0);
00099     _physics_objects.v() = old_physics_objects.v();
00100   }
00101 
00102   _physics_objects.erase(_physics_objects.begin() + object_index);
00103   return true;
00104 }
00105 
00106 ////////////////////////////////////////////////////////////////////
00107 //     Function: PhysicsObjectCollection::add_physics_objects_from
00108 //       Access: Published
00109 //  Description: Adds all the PhysicsObjects indicated in the other
00110 //               collection to this collection.  The other 
00111 //               physics_objects are simply appended to the end of 
00112 //               the physics_objects in this list;
00113 //               duplicates are not automatically removed.
00114 ////////////////////////////////////////////////////////////////////
00115 void PhysicsObjectCollection::
00116 add_physics_objects_from(const PhysicsObjectCollection &other) {
00117   int other_num_physics_objects = other.get_num_physics_objects();
00118   for (int i = 0; i < other_num_physics_objects; i++) {
00119     add_physics_object(other.get_physics_object(i));
00120   }
00121 }
00122 
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: PhysicsObjectCollection::remove_physics_objects_from
00126 //       Access: Published
00127 //  Description: Removes from this collection all of the PhysicsObjects
00128 //               listed in the other collection.
00129 ////////////////////////////////////////////////////////////////////
00130 void PhysicsObjectCollection::
00131 remove_physics_objects_from(const PhysicsObjectCollection &other) {
00132   PhysicsObjects new_physics_objects;
00133   int num_physics_objects = get_num_physics_objects();
00134   for (int i = 0; i < num_physics_objects; i++) {
00135     PT(PhysicsObject) physics_object = get_physics_object(i);
00136     if (!other.has_physics_object(physics_object)) {
00137       new_physics_objects.push_back(physics_object);
00138     }
00139   }
00140   _physics_objects = new_physics_objects;
00141 }
00142 
00143 ////////////////////////////////////////////////////////////////////
00144 //     Function: PhysicsObjectCollection::remove_duplicate_physics_objects
00145 //       Access: Published
00146 //  Description: Removes any duplicate entries of the same PhysicsObjects
00147 //               on this collection.  If a PhysicsObject appears multiple
00148 //               times, the first appearance is retained; subsequent
00149 //               appearances are removed.
00150 ////////////////////////////////////////////////////////////////////
00151 void PhysicsObjectCollection::
00152 remove_duplicate_physics_objects() {
00153   PhysicsObjects new_physics_objects;
00154 
00155   int num_physics_objects = get_num_physics_objects();
00156   for (int i = 0; i < num_physics_objects; i++) {
00157     PT(PhysicsObject) physics_object = get_physics_object(i);
00158     bool duplicated = false;
00159 
00160     for (int j = 0; j < i && !duplicated; j++) {
00161       duplicated = (physics_object == get_physics_object(j));
00162     }
00163 
00164     if (!duplicated) {
00165       new_physics_objects.push_back(physics_object);
00166     }
00167   }
00168 
00169   _physics_objects = new_physics_objects;
00170 }
00171 
00172 ////////////////////////////////////////////////////////////////////
00173 //     Function: PhysicsObjectCollection::has_physics_object
00174 //       Access: Published
00175 //  Description: Returns true if the indicated PhysicsObject appears in
00176 //               this collection, false otherwise.
00177 ////////////////////////////////////////////////////////////////////
00178 bool PhysicsObjectCollection::
00179 has_physics_object(PT(PhysicsObject) physics_object) const {
00180   for (int i = 0; i < get_num_physics_objects(); i++) {
00181     if (physics_object == get_physics_object(i)) {
00182       return true;
00183     }
00184   }
00185   return false;
00186 }
00187 
00188 ////////////////////////////////////////////////////////////////////
00189 //     Function: PhysicsObjectCollection::clear
00190 //       Access: Published
00191 //  Description: Removes all PhysicsObjects from the collection.
00192 ////////////////////////////////////////////////////////////////////
00193 void PhysicsObjectCollection::
00194 clear() {
00195   _physics_objects.clear();
00196 }
00197 
00198 ////////////////////////////////////////////////////////////////////
00199 //     Function: PhysicsObjectCollection::is_empty
00200 //       Access: Published
00201 //  Description: Returns true if there are no PhysicsObjects in the
00202 //               collection, false otherwise.
00203 ////////////////////////////////////////////////////////////////////
00204 bool PhysicsObjectCollection::
00205 is_empty() const {
00206   return _physics_objects.empty();
00207 }
00208 
00209 ////////////////////////////////////////////////////////////////////
00210 //     Function: PhysicsObjectCollection::get_num_physics_objects
00211 //       Access: Published
00212 //  Description: Returns the number of PhysicsObjects in the collection.
00213 ////////////////////////////////////////////////////////////////////
00214 int PhysicsObjectCollection::
00215 get_num_physics_objects() const {
00216   return _physics_objects.size();
00217 }
00218 
00219 ////////////////////////////////////////////////////////////////////
00220 //     Function: PhysicsObjectCollection::get_physics_object
00221 //       Access: Published
00222 //  Description: Returns the nth PhysicsObject in the collection.
00223 ////////////////////////////////////////////////////////////////////
00224 PT(PhysicsObject) PhysicsObjectCollection::
00225 get_physics_object(int index) const {
00226   nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
00227 
00228   return _physics_objects[index];
00229 }
00230 
00231 ////////////////////////////////////////////////////////////////////
00232 //     Function: PhysicsObjectCollection::operator []
00233 //       Access: Published
00234 //  Description: Returns the nth PhysicsObject in the collection.  This is
00235 //               the same as get_physics_object(), but it may be a more
00236 //               convenient way to access it.
00237 ////////////////////////////////////////////////////////////////////
00238 PT(PhysicsObject) PhysicsObjectCollection::
00239 operator [] (int index) const {
00240   nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
00241 
00242   return _physics_objects[index];
00243 }
00244 
00245 ////////////////////////////////////////////////////////////////////
00246 //     Function: PhysicsObjectCollection::size
00247 //       Access: Published
00248 //  Description: Returns the number of physics objects in the
00249 //               collection.  This is the same thing as
00250 //               get_num_physics_objects().
00251 ////////////////////////////////////////////////////////////////////
00252 int PhysicsObjectCollection::
00253 size() const {
00254   return _physics_objects.size();
00255 }
00256 
00257 ////////////////////////////////////////////////////////////////////
00258 //     Function: PhysicsObjectCollection::output
00259 //       Access: Published
00260 //  Description: Writes a brief one-line description of the
00261 //               PhysicsObjectCollection to the indicated output stream.
00262 ////////////////////////////////////////////////////////////////////
00263 void PhysicsObjectCollection::
00264 output(ostream &out) const {
00265   if (get_num_physics_objects() == 1) {
00266     out << "1 PhysicsObject";
00267   } else {
00268     out << get_num_physics_objects() << " PhysicsObjects";
00269   }
00270 }
00271 
00272 ////////////////////////////////////////////////////////////////////
00273 //     Function: PhysicsObjectCollection::write
00274 //       Access: Published
00275 //  Description: Writes a complete multi-line description of the
00276 //               PhysicsObjectCollection to the indicated output stream.
00277 ////////////////////////////////////////////////////////////////////
00278 void PhysicsObjectCollection::
00279 write(ostream &out, int indent_level) const {
00280   for (int i = 0; i < get_num_physics_objects(); i++) {
00281     indent(out, indent_level) << get_physics_object(i) << "\n";
00282   }
00283 }
00284 
 All Classes Functions Variables Enumerations