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