Panda3D
odeJointCollection.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file odeJointCollection.cxx
10  * @author joswilso
11  * @date 2006-12-27
12  */
13 
14 #include "odeJointCollection.h"
15 
16 OdeJointCollection::
17 OdeJointCollection() {
18 }
19 
20 OdeJointCollection::
21 OdeJointCollection(const OdeJointCollection &copy) :
22  _joints(copy._joints) {
23 }
24 
25 void OdeJointCollection::
26 operator = (const OdeJointCollection &copy) {
27  _joints = copy._joints;
28 }
29 
30 void OdeJointCollection::
31 add_joint(const OdeJoint &joint) {
32  // If the pointer to our internal array is shared by any other
33  // OdeJointCollections, we have to copy the array now so we won't
34  // inadvertently modify any of our brethren OdeJointCollection objects.
35 
36  if (_joints.get_ref_count() > 1) {
37  Joints old_joints = _joints;
38  _joints = Joints::empty_array(0);
39  _joints.v() = old_joints.v();
40  }
41 
42  _joints.push_back(joint);
43 }
44 
45 bool OdeJointCollection::
46 remove_joint(const OdeJoint &joint) {
47  int joint_index = -1;
48  for (int i = 0; joint_index == -1 && i < (int)_joints.size(); i++) {
49  if (_joints[i] == joint) {
50  joint_index = i;
51  }
52  }
53 
54  if (joint_index == -1) {
55  // The indicated joint was not a member of the collection.
56  return false;
57  }
58 
59  // If the pointer to our internal array is shared by any other
60  // OdeJointCollections, we have to copy the array now so we won't
61  // inadvertently modify any of our brethren JointCollection objects.
62 
63  if (_joints.get_ref_count() > 1) {
64  Joints old_joints = _joints;
65  _joints = Joints::empty_array(0);
66  _joints.v() = old_joints.v();
67  }
68 
69  _joints.erase(_joints.begin() + joint_index);
70  return true;
71 }
72 
73 void OdeJointCollection::
74 add_joints_from(const OdeJointCollection &other) {
75  int other_num_joints = other.get_num_joints();
76  for (int i = 0; i < other_num_joints; i++) {
77  add_joint(other.get_joint(i));
78  }
79 }
80 
81 void OdeJointCollection::
82 remove_joints_from(const OdeJointCollection &other) {
83  Joints new_joints;
84  int num_joints = get_num_joints();
85  for (int i = 0; i < num_joints; i++) {
86  OdeJoint joint = get_joint(i);
87  if (!other.has_joint(joint)) {
88  new_joints.push_back(joint);
89  }
90  }
91  _joints = new_joints;
92 }
93 
94 void OdeJointCollection::
95 remove_duplicate_joints() {
96  Joints new_joints;
97 
98  int num_joints = get_num_joints();
99  for (int i = 0; i < num_joints; i++) {
100  OdeJoint joint = get_joint(i);
101  bool duplicated = false;
102 
103  for (int j = 0; j < i && !duplicated; j++) {
104  duplicated = (joint == get_joint(j));
105  }
106 
107  if (!duplicated) {
108  new_joints.push_back(joint);
109  }
110  }
111 
112  _joints = new_joints;
113 }
114 
115 bool OdeJointCollection::
116 has_joint(const OdeJoint &joint) const {
117  for (int i = 0; i < get_num_joints(); i++) {
118  if (joint == get_joint(i)) {
119  return true;
120  }
121  }
122  return false;
123 }
124 
125 void OdeJointCollection::
126 clear() {
127  _joints.clear();
128 }
129 
130 bool OdeJointCollection::
131 is_empty() const {
132  return _joints.empty();
133 }
134 
135 int OdeJointCollection::
136 get_num_joints() const {
137  return _joints.size();
138 }
139 
140 OdeJoint OdeJointCollection::
141 get_joint(int index) const {
142  nassertr(index >= 0 && index < (int)_joints.size(), OdeJoint());
143  return _joints[index];
144 }
145 
146 OdeJoint OdeJointCollection::
147 operator [] (int index) const {
148  return get_joint(index);
149 }
150 
151 /**
152  * Returns the number of joints in the collection. This is the same thing as
153  * get_num_joints().
154  */
156 size() const {
157  return _joints.size();
158 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int size() const
Returns the number of joints in the collection.