Panda3D
Loading...
Searching...
No Matches
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
16OdeJointCollection::
17OdeJointCollection() {
18}
19
20OdeJointCollection::
21OdeJointCollection(const OdeJointCollection &copy) :
22 _joints(copy._joints) {
23}
24
25void OdeJointCollection::
26operator = (const OdeJointCollection &copy) {
27 _joints = copy._joints;
28}
29
30void OdeJointCollection::
31add_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
45bool OdeJointCollection::
46remove_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
73void OdeJointCollection::
74add_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
81void OdeJointCollection::
82remove_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
94void OdeJointCollection::
95remove_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
115bool OdeJointCollection::
116has_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
125void OdeJointCollection::
126clear() {
127 _joints.clear();
128}
129
130bool OdeJointCollection::
131is_empty() const {
132 return _joints.empty();
133}
134
135int OdeJointCollection::
136get_num_joints() const {
137 return _joints.size();
138}
139
140OdeJoint OdeJointCollection::
141get_joint(int index) const {
142 nassertr(index >= 0 && index < (int)_joints.size(), OdeJoint());
143 return _joints[index];
144}
145
146OdeJoint OdeJointCollection::
147operator [] (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 */
156size() const {
157 return _joints.size();
158}
int size() const
Returns the number of joints in the collection.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.