Panda3D
|
00001 // Filename: odeJointCollection.cxx 00002 // Created by: joswilso (27Dec06) 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 "odeJointCollection.h" 00016 00017 OdeJointCollection:: 00018 OdeJointCollection() { 00019 } 00020 00021 OdeJointCollection:: 00022 OdeJointCollection(const OdeJointCollection ©) : 00023 _joints(copy._joints) { 00024 } 00025 00026 void OdeJointCollection:: 00027 operator = (const OdeJointCollection ©) { 00028 _joints = copy._joints; 00029 } 00030 00031 void OdeJointCollection:: 00032 add_joint(const OdeJoint &joint) { 00033 // If the pointer to our internal array is shared by any other 00034 // OdeJointCollections, we have to copy the array now so we won't 00035 // inadvertently modify any of our brethren OdeJointCollection 00036 // objects. 00037 00038 if (_joints.get_ref_count() > 1) { 00039 Joints old_joints = _joints; 00040 _joints = Joints::empty_array(0); 00041 _joints.v() = old_joints.v(); 00042 } 00043 00044 _joints.push_back(joint); 00045 } 00046 00047 bool OdeJointCollection:: 00048 remove_joint(const OdeJoint &joint) { 00049 int joint_index = -1; 00050 for (int i = 0; joint_index == -1 && i < (int)_joints.size(); i++) { 00051 if (_joints[i] == joint) { 00052 joint_index = i; 00053 } 00054 } 00055 00056 if (joint_index == -1) { 00057 // The indicated joint was not a member of the collection. 00058 return false; 00059 } 00060 00061 // If the pointer to our internal array is shared by any other 00062 // OdeJointCollections, we have to copy the array now so we won't 00063 // inadvertently modify any of our brethren JointCollection 00064 // objects. 00065 00066 if (_joints.get_ref_count() > 1) { 00067 Joints old_joints = _joints; 00068 _joints = Joints::empty_array(0); 00069 _joints.v() = old_joints.v(); 00070 } 00071 00072 _joints.erase(_joints.begin() + joint_index); 00073 return true; 00074 } 00075 00076 void OdeJointCollection:: 00077 add_joints_from(const OdeJointCollection &other) { 00078 int other_num_joints = other.get_num_joints(); 00079 for (int i = 0; i < other_num_joints; i++) { 00080 add_joint(other.get_joint(i)); 00081 } 00082 } 00083 00084 void OdeJointCollection:: 00085 remove_joints_from(const OdeJointCollection &other) { 00086 Joints new_joints; 00087 int num_joints = get_num_joints(); 00088 for (int i = 0; i < num_joints; i++) { 00089 OdeJoint joint = get_joint(i); 00090 if (!other.has_joint(joint)) { 00091 new_joints.push_back(joint); 00092 } 00093 } 00094 _joints = new_joints; 00095 } 00096 00097 void OdeJointCollection:: 00098 remove_duplicate_joints() { 00099 Joints new_joints; 00100 00101 int num_joints = get_num_joints(); 00102 for (int i = 0; i < num_joints; i++) { 00103 OdeJoint joint = get_joint(i); 00104 bool duplicated = false; 00105 00106 for (int j = 0; j < i && !duplicated; j++) { 00107 duplicated = (joint == get_joint(j)); 00108 } 00109 00110 if (!duplicated) { 00111 new_joints.push_back(joint); 00112 } 00113 } 00114 00115 _joints = new_joints; 00116 } 00117 00118 bool OdeJointCollection:: 00119 has_joint(const OdeJoint &joint) const { 00120 for (int i = 0; i < get_num_joints(); i++) { 00121 if (joint == get_joint(i)) { 00122 return true; 00123 } 00124 } 00125 return false; 00126 } 00127 00128 void OdeJointCollection:: 00129 clear() { 00130 _joints.clear(); 00131 } 00132 00133 bool OdeJointCollection:: 00134 is_empty() const { 00135 return _joints.empty(); 00136 } 00137 00138 int OdeJointCollection:: 00139 get_num_joints() const { 00140 return _joints.size(); 00141 } 00142 00143 OdeJoint OdeJointCollection:: 00144 get_joint(int index) const { 00145 nassertr(index >= 0 && index < (int)_joints.size(), OdeJoint()); 00146 return _joints[index]; 00147 } 00148 00149 OdeJoint OdeJointCollection:: 00150 operator [] (int index) const { 00151 return get_joint(index); 00152 } 00153 00154 //////////////////////////////////////////////////////////////////// 00155 // Function: OdeJointCollection::size 00156 // Access: Published 00157 // Description: Returns the number of joints in the collection. This 00158 // is the same thing as get_num_joints(). 00159 //////////////////////////////////////////////////////////////////// 00160 int OdeJointCollection:: 00161 size() const { 00162 return _joints.size(); 00163 }