Panda3D
 All Classes Functions Variables Enumerations
odeJointCollection.cxx
1 // Filename: odeJointCollection.cxx
2 // Created by: joswilso (27Dec06)
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 
15 #include "odeJointCollection.h"
16 
17 OdeJointCollection::
18 OdeJointCollection() {
19 }
20 
21 OdeJointCollection::
22 OdeJointCollection(const OdeJointCollection &copy) :
23  _joints(copy._joints) {
24 }
25 
26 void OdeJointCollection::
27 operator = (const OdeJointCollection &copy) {
28  _joints = copy._joints;
29 }
30 
31 void OdeJointCollection::
32 add_joint(const OdeJoint &joint) {
33  // If the pointer to our internal array is shared by any other
34  // OdeJointCollections, we have to copy the array now so we won't
35  // inadvertently modify any of our brethren OdeJointCollection
36  // objects.
37 
38  if (_joints.get_ref_count() > 1) {
39  Joints old_joints = _joints;
40  _joints = Joints::empty_array(0);
41  _joints.v() = old_joints.v();
42  }
43 
44  _joints.push_back(joint);
45 }
46 
47 bool OdeJointCollection::
48 remove_joint(const OdeJoint &joint) {
49  int joint_index = -1;
50  for (int i = 0; joint_index == -1 && i < (int)_joints.size(); i++) {
51  if (_joints[i] == joint) {
52  joint_index = i;
53  }
54  }
55 
56  if (joint_index == -1) {
57  // The indicated joint was not a member of the collection.
58  return false;
59  }
60 
61  // If the pointer to our internal array is shared by any other
62  // OdeJointCollections, we have to copy the array now so we won't
63  // inadvertently modify any of our brethren JointCollection
64  // objects.
65 
66  if (_joints.get_ref_count() > 1) {
67  Joints old_joints = _joints;
68  _joints = Joints::empty_array(0);
69  _joints.v() = old_joints.v();
70  }
71 
72  _joints.erase(_joints.begin() + joint_index);
73  return true;
74 }
75 
76 void OdeJointCollection::
77 add_joints_from(const OdeJointCollection &other) {
78  int other_num_joints = other.get_num_joints();
79  for (int i = 0; i < other_num_joints; i++) {
80  add_joint(other.get_joint(i));
81  }
82 }
83 
84 void OdeJointCollection::
85 remove_joints_from(const OdeJointCollection &other) {
86  Joints new_joints;
87  int num_joints = get_num_joints();
88  for (int i = 0; i < num_joints; i++) {
89  OdeJoint joint = get_joint(i);
90  if (!other.has_joint(joint)) {
91  new_joints.push_back(joint);
92  }
93  }
94  _joints = new_joints;
95 }
96 
97 void OdeJointCollection::
98 remove_duplicate_joints() {
99  Joints new_joints;
100 
101  int num_joints = get_num_joints();
102  for (int i = 0; i < num_joints; i++) {
103  OdeJoint joint = get_joint(i);
104  bool duplicated = false;
105 
106  for (int j = 0; j < i && !duplicated; j++) {
107  duplicated = (joint == get_joint(j));
108  }
109 
110  if (!duplicated) {
111  new_joints.push_back(joint);
112  }
113  }
114 
115  _joints = new_joints;
116 }
117 
118 bool OdeJointCollection::
119 has_joint(const OdeJoint &joint) const {
120  for (int i = 0; i < get_num_joints(); i++) {
121  if (joint == get_joint(i)) {
122  return true;
123  }
124  }
125  return false;
126 }
127 
128 void OdeJointCollection::
129 clear() {
130  _joints.clear();
131 }
132 
133 bool OdeJointCollection::
134 is_empty() const {
135  return _joints.empty();
136 }
137 
138 int OdeJointCollection::
139 get_num_joints() const {
140  return _joints.size();
141 }
142 
143 OdeJoint OdeJointCollection::
144 get_joint(int index) const {
145  nassertr(index >= 0 && index < (int)_joints.size(), OdeJoint());
146  return _joints[index];
147 }
148 
149 OdeJoint OdeJointCollection::
150 operator [] (int index) const {
151  return get_joint(index);
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: OdeJointCollection::size
156 // Access: Published
157 // Description: Returns the number of joints in the collection. This
158 // is the same thing as get_num_joints().
159 ////////////////////////////////////////////////////////////////////
161 size() const {
162  return _joints.size();
163 }
int size() const
Returns the number of joints in the collection.