GteNode.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
9 #include <Graphics/GteNode.h>
10 using namespace gte;
11 
12 
14 {
15  for (auto& child : mChild)
16  {
17  if (child)
18  {
19  child->SetParent(nullptr);
20  child = nullptr;
21  }
22  }
23 }
24 
26 {
27 }
28 
30 {
31  return static_cast<int>(mChild.size());
32 }
33 
34 int Node::AttachChild(std::shared_ptr<Spatial> const& child)
35 {
36  if (!child)
37  {
38  LogError("You cannot attach null children to a node.");
39  return -1;
40  }
41 
42  if (child->GetParent())
43  {
44  LogError("The child already has a parent.");
45  return -1;
46  }
47 
48  child->SetParent(this);
49 
50  // Insert the child in the first available slot (if any).
51  int i = 0;
52  for (auto& current : mChild)
53  {
54  if (!current)
55  {
56  current = child;
57  return i;
58  }
59  ++i;
60  }
61 
62  // All slots are used, so append the child to the array.
63  int const numChildren = static_cast<int>(mChild.size());
64  mChild.push_back(child);
65  return numChildren;
66 }
67 
68 int Node::DetachChild(std::shared_ptr<Spatial> const& child)
69 {
70  if (child)
71  {
72  int i = 0;
73  for (auto& current : mChild)
74  {
75  if (current == child)
76  {
77  current->SetParent(nullptr);
78  current = nullptr;
79  return i;
80  }
81  ++i;
82  }
83  }
84  return -1;
85 }
86 
87 std::shared_ptr<Spatial> Node::DetachChildAt(int i)
88 {
89  if (0 <= i && i < static_cast<int>(mChild.size()))
90  {
91  std::shared_ptr<Spatial> child = mChild[i];
92  if (child)
93  {
94  child->SetParent(nullptr);
95  mChild[i] = nullptr;
96  }
97  return child;
98  }
99  return nullptr;
100 }
101 
103 {
104  for (auto& current : mChild)
105  {
106  DetachChild(current);
107  }
108 }
109 
110 std::shared_ptr<Spatial> Node::SetChild(int i,
111  std::shared_ptr<Spatial> const& child)
112 {
113  if (child)
114  {
115  LogAssert(!child->GetParent(), "The child already has a parent.");
116  }
117 
118  int const numChildren = static_cast<int>(mChild.size());
119  if (0 <= i && i < numChildren)
120  {
121  // Remove the child currently in the slot.
122  std::shared_ptr<Spatial> previousChild = mChild[i];
123  if (previousChild)
124  {
125  previousChild->SetParent(nullptr);
126  }
127 
128  // Insert the new child in the slot.
129  if (child)
130  {
131  child->SetParent(this);
132  }
133 
134  mChild[i] = child;
135  return previousChild;
136  }
137 
138  // The index is out of range, so append the child to the array.
139  if (child)
140  {
141  child->SetParent(this);
142  }
143  mChild.push_back(child);
144  return nullptr;
145 }
146 
147 std::shared_ptr<Spatial> Node::GetChild(int i)
148 {
149  if (0 <= i && i < static_cast<int>(mChild.size()))
150  {
151  return mChild[i];
152  }
153  return nullptr;
154 }
155 
156 void Node::UpdateWorldData(double applicationTime)
157 {
158  Spatial::UpdateWorldData(applicationTime);
159 
160  for (auto& child : mChild)
161  {
162  if (child)
163  {
164  child->Update(applicationTime, false);
165  }
166  }
167 }
168 
170 {
171  if (!worldBoundIsCurrent)
172  {
173  // Start with an invalid bound.
174  worldBound.SetCenter({ 0.0f, 0.0f, 0.0f, 1.0f });
175  worldBound.SetRadius(0.0f);
176 
177  for (auto& child : mChild)
178  {
179  if (child)
180  {
181  // GrowToContain ignores invalid child bounds. If the world
182  // bound is invalid and a child bound is valid, the child
183  // bound is copied to the world bound. If the world bound and
184  // child bound are valid, the smallest bound containing both
185  // bounds is assigned to the world bound.
186  worldBound.GrowToContain(child->worldBound);
187  }
188  }
189  }
190 }
191 
193  std::shared_ptr<Camera> const& camera, bool noCull)
194 {
195  for (auto& child : mChild)
196  {
197  if (child)
198  {
199  child->OnGetVisibleSet(culler, camera, noCull);
200  }
201  }
202 }
203 
virtual void UpdateWorldData(double applicationTime)
Definition: GteNode.cpp:156
#define LogAssert(condition, message)
Definition: GteLogger.h:86
int GetNumChildren() const
Definition: GteNode.cpp:29
virtual void GetVisibleSet(Culler &culler, std::shared_ptr< Camera > const &camera, bool noCull)
Definition: GteNode.cpp:192
std::shared_ptr< Spatial > SetChild(int i, std::shared_ptr< Spatial > const &child)
Definition: GteNode.cpp:110
void SetRadius(float radius)
#define LogError(message)
Definition: GteLogger.h:92
bool worldBoundIsCurrent
Definition: GteSpatial.h:58
std::shared_ptr< Spatial > GetChild(int i)
Definition: GteNode.cpp:147
std::vector< std::shared_ptr< Spatial > > mChild
Definition: GteNode.h:100
int AttachChild(std::shared_ptr< Spatial > const &child)
Definition: GteNode.cpp:34
virtual void UpdateWorldData(double applicationTime)
Definition: GteSpatial.cpp:59
std::shared_ptr< Spatial > DetachChildAt(int i)
Definition: GteNode.cpp:87
BoundingSphere worldBound
Definition: GteSpatial.h:56
GLfloat f
Definition: glcorearb.h:1921
void GrowToContain(BoundingSphere const &sphere)
virtual void UpdateWorldBound()
Definition: GteNode.cpp:169
int DetachChild(std::shared_ptr< Spatial > const &child)
Definition: GteNode.cpp:68
void DetachAllChildren()
Definition: GteNode.cpp:102
virtual ~Node()
Definition: GteNode.cpp:13
void SetCenter(Vector4< float > const &center)


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01