Panda3D

physicalNode.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 physicalNode.cxx
10 * @author charles
11 * @date 2000-08-01
12 */
13
14 #include "physicalNode.h"
15 #include "physicsManager.h"
16
17 #ifdef PHAVE_ATOMIC
18 #include <atomic>
19 #endif
20
21 // static stuff.
22 static std::atomic_flag warned_copy_physical_node = ATOMIC_FLAG_INIT;
23
24 TypeHandle PhysicalNode::_type_handle;
25
26 /**
27 * default constructor
28 */
29 PhysicalNode::
30 PhysicalNode(const std::string &name) :
31 PandaNode(name)
32 {
33 }
34
35 /**
36 * copy constructor
37 */
38 PhysicalNode::
39 PhysicalNode(const PhysicalNode &copy) :
40 PandaNode(copy), _physicals(copy._physicals) {
41 }
42
43 /**
44 * destructor
45 */
46 PhysicalNode::
47 ~PhysicalNode() {
48 for (Physical *physical : _physicals) {
49 if (physical->_physical_node == this) {
50 physical->_physical_node = nullptr;
51 if (physical->_physics_manager != nullptr) {
52 physical->_physics_manager->remove_physical(physical);
53 }
54 }
55 }
56 }
57
58 /**
59 * dynamic child copy
60 */
61 PandaNode *PhysicalNode::
62 make_copy() const {
63 if (!_physicals.empty() && !warned_copy_physical_node.test_and_set()) {
64 // This is a problem, because a Physical can only be on one PhysicalNode.
65 //FIXME: Figure out a solution.
66 physics_cat.warning()
67 << "Detected attempt to copy PhysicalNode object with physicals.\n";
68 }
69 return new PhysicalNode(*this);
70 }
71
72 /**
73 * append operation
74 */
75 void PhysicalNode::
76 add_physicals_from(const PhysicalNode &other) {
77 size_t num_physicals = _physicals.size();
78 _physicals.insert(_physicals.end(),
79 other._physicals.begin(), other._physicals.end());
80
81 for (size_t i = num_physicals; i < _physicals.size(); ++i) {
82 _physicals[i]->_physical_node = this;
83 }
84 }
85
86 /**
87 * replace operation
88 */
89 void PhysicalNode::
90 set_physical(size_t index, Physical *physical) {
91 nassertv(index < _physicals.size());
92
93 _physicals[index]->_physical_node = nullptr;
94 _physicals[index] = physical;
95 physical->_physical_node = this;
96 }
97
98 /**
99 * insert operation
100 */
101 void PhysicalNode::
102 insert_physical(size_t index, Physical *physical) {
103 if (index > _physicals.size()) {
104 index = _physicals.size();
105 }
106
107 _physicals.insert(_physicals.begin() + index, physical);
108 physical->_physical_node = this;
109 }
110
111 /**
112 * remove operation
113 */
114 void PhysicalNode::
115 remove_physical(Physical *physical) {
116 pvector< PT(Physical) >::iterator found;
117 PT(Physical) ptp = physical;
118 found = find(_physicals.begin(), _physicals.end(), ptp);
119 if (found == _physicals.end()) {
120 return;
121 }
122 _physicals.erase(found);
123
124 nassertv(ptp->_physical_node == this);
125 ptp->_physical_node = nullptr;
126 }
127
128 /**
129 * remove operation
130 */
131 void PhysicalNode::
132 remove_physical(size_t index) {
133 nassertv(index <= _physicals.size());
134
135 pvector< PT(Physical) >::iterator remove;
136 remove = _physicals.begin() + index;
137 (*remove)->_physical_node = nullptr;
138
139 _physicals.erase(remove);
140 }
141
142 /**
143 * Write a string representation of this instance to <out>.
144 */
145 void PhysicalNode::
146 write(std::ostream &out, int indent) const {
147 #ifndef NDEBUG //[
148 out.width(indent); out<<""; out<<"PhysicalNode:\n";
149 // PandaNode::write(out, indent+2);
150 #endif //] NDEBUG
151 }
PandaNode
A basic node of the scene graph or data graph.
Definition: pandaNode.h:65
PhysicalNode
Graph node that encapsulated a series of physical objects.
Definition: physicalNode.h:28
PhysicalNode::make_copy
virtual PandaNode * make_copy() const
dynamic child copy
Definition: physicalNode.cxx:62
PhysicalNode::remove_physical
remove_physical
remove operation
Definition: physicalNode.h:44
PhysicalNode::add_physicals_from
void add_physicals_from(const PhysicalNode &other)
append operation
Definition: physicalNode.cxx:76
PhysicalNode::PhysicalNode
PhysicalNode(const std::string &name)
default constructor
Definition: physicalNode.cxx:30
PhysicalNode::~PhysicalNode
virtual ~PhysicalNode()
destructor
Definition: physicalNode.cxx:47
PhysicalNode::insert_physical
insert_physical
insert operation
Definition: physicalNode.h:44
PhysicalNode::set_physical
set_physical
replace operation
Definition: physicalNode.h:44
PhysicalNode::write
virtual void write(std::ostream &out, int indent=0) const
Write a string representation of this instance to <out>.
Definition: physicalNode.cxx:146
Physical
Defines a set of physically modeled attributes.
Definition: physical.h:37
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
pvector
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
indent
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
physicalNode.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physicsManager.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.