PeriDyno 1.0.0
Loading...
Searching...
No Matches
Node.cpp
Go to the documentation of this file.
1#include "Node.h"
2#include "Action.h"
3
4#include "SceneGraph.h"
5
6namespace dyno
7{
9 : OBase()
10 , m_node_name("default")
11 , mDt(0.016f)
12{
13}
14
15
17{
18 mModuleList.clear();
19
20// for (auto port : mImportNodes)
21// {
22// auto& nodes = port->getNodes();
23// for (auto node : nodes)
24// {
25// node->disconnect(port);
26// }
27// }
28
29 for (auto port : mExportNodes)
30 {
31 this->disconnect(port);
32 }
33
34 mImportNodes.clear();
35 mExportNodes.clear();
36}
37
38void Node::setName(std::string name)
39{
40 m_node_name = name;
41}
42
43std::string Node::getName()
44{
45 return m_node_name;
46}
47
48std::string Node::getNodeType()
49{
50 return "Default";
51}
52
54{
55 return mAutoSync;
56}
57
59{
60 return mAutoHidden;
61}
62
63void Node::setAutoSync(bool con)
64{
65 mAutoSync = con;
66}
67
68void Node::setAutoHidden(bool con)
69{
70 mAutoHidden = con;
71}
72
74{
75 return mPhysicsEnabled;
76}
77
78void Node::setActive(bool active)
79{
80 mPhysicsEnabled = active;
81}
82
84{
85 return mRenderingEnabled;
86}
87
88void Node::setVisible(bool visible)
89{
90 mRenderingEnabled = visible;
91}
92
94{
95 return mDt;
96}
97
99{
100 mDt = dt;
101}
102
104{
105 mSceneGraph = scn;
106}
107
112
113// Node* Node::addAncestor(Node* anc)
114// {
115// if (hasAncestor(anc) || anc == nullptr)
116// return nullptr;
117//
118// anc->addDescendant(this);
119//
120// mAncestors.push_back(anc);
121//
122// if (mSceneGraph) {
123// mSceneGraph->markQueueUpdateRequired();
124// }
125//
126// return anc;
127// }
128//
129// bool Node::hasAncestor(Node* anc)
130// {
131// auto it = find(mAncestors.begin(), mAncestors.end(), anc);
132//
133// return it == mAncestors.end() ? false : true;
134// }
135
137{
138
139}
140
142{
143 this->animationPipeline()->update();
144}
145
147{
148 if (!this->validateInputs()) {
149 return;
150 }
151
152 if (this->requireUpdate())
153 {
154 this->preUpdateStates();
155
156 if (mPhysicsEnabled)
157 this->updateStates();
158
159 this->postUpdateStates();
160
161 this->updateTopology();
162
163 //reset parameters
164 for (auto param : fields_param)
165 {
166 param->tack();
167 }
168
169 //reset input fields
170 for (auto f_in : fields_input)
171 {
172 f_in->tack();
173 }
174
175 //tag all output fields as modifed
176 for (auto f_out : fields_output)
177 {
178 f_out->tick();
179 }
180 }
181}
182
184{
185 if (this->validateInputs()) {
186 this->stateElapsedTime()->setValue(0.0f);
187 this->stateFrameNumber()->setValue(0);
188
189 this->resetStates();
190
191 //When the node is reset, call tick() to force updating all modules
192 this->tick();
193 }
194}
195
200
202{
203
204}
205
207{
209 {
210 this->graphicsPipeline()->update();
211 }
212}
213
215{
216 this->resetPipeline()->update();
217}
218
220{
221 //If any input field is empty, return false;
222 for(auto f_in : fields_input)
223 {
224 if (!f_in->isOptional() && f_in->isEmpty())
225 {
226 std::string errMsg = std::string("The field ") + f_in->getObjectName() +
227 std::string(" in Node ") + this->getClassInfo()->getClassName() + std::string(" is not set!");
228
230 return false;
231 }
232 }
233
234 return true;
235}
236
238{
239 //TODO: improve the following rules
240 if (mForceUpdate)
241 return true;
242
243 //check input fields
244 bool modified = false;
245
246 if (mImportNodes.size() > 0)
247 {
248 return true;
249 }
250
251
252 for (auto f_in : fields_input)
253 {
254 modified |= f_in->isModified();
255 }
256
257 //check control fields
258 for (auto var : fields_param)
259 {
260 modified |= var->isModified();
261 }
262
263 return modified;
264}
265
267{
268 std::vector<FBase*>& fields = this->getAllFields();
269 for(FBase * var : fields)
270 {
271 if (var != nullptr) {
272 if (var->getFieldType() == FieldTypeEnum::State || var->getFieldType() == FieldTypeEnum::Out)
273 {
274 var->tick();
275 }
276 }
277 }
278}
279
280// std::shared_ptr<DeviceContext> Node::getContext()
281// {
282// if (m_context == nullptr)
283// {
284// m_context = TypeInfo::New<DeviceContext>();
285// m_context->setParent(this);
286// addModule(m_context);
287// }
288// return m_context;
289// }
290//
291// void Node::setContext(std::shared_ptr<DeviceContext> context)
292// {
293// if (m_context != nullptr)
294// {
295// deleteModule(m_context);
296// }
297//
298// m_context = context;
299// addModule(m_context);
300// }
301
302std::shared_ptr<Pipeline> Node::resetPipeline()
303{
304 if (mResetPipeline == nullptr)
305 {
306 mResetPipeline = std::make_shared<AnimationPipeline>(this);
307 }
308 return mResetPipeline;
309}
310
311std::shared_ptr<AnimationPipeline> Node::animationPipeline()
312{
313 if (mAnimationPipeline == nullptr)
314 {
315 mAnimationPipeline = std::make_shared<AnimationPipeline>(this);
316 }
317 return mAnimationPipeline;
318}
319
320std::shared_ptr<GraphicsPipeline> Node::graphicsPipeline()
321{
322 if (mGraphicsPipeline == nullptr)
323 {
324 mGraphicsPipeline = std::make_shared<GraphicsPipeline>(this);
325 }
326 return mGraphicsPipeline;
327}
328
329/*
330std::shared_ptr<MechanicalState> Node::getMechanicalState()
331{
332 if (m_mechanical_state == nullptr)
333 {
334 m_mechanical_state = TypeInfo::New<MechanicalState>();
335 m_mechanical_state->setParent(this);
336 }
337 return m_mechanical_state;
338}*/
339/*
340bool Node::addModule(std::string name, Module* module)
341{
342 if (getContext() == nullptr || module == NULL)
343 {
344 std::cout << "Context or module does not exist!" << std::endl;
345 return false;
346 }
347
348 std::map<std::string, Module*>::iterator found = m_modules.find(name);
349 if (found != m_modules.end())
350 {
351 std::cout << "Module name already exists!" << std::endl;
352 return false;
353 }
354 else
355 {
356 m_modules[name] = module;
357 m_module_list.push_back(module);
358
359// module->insertToNode(this);
360 }
361
362 return true;
363}
364*/
365bool Node::addModule(std::shared_ptr<Module> module)
366{
367 bool ret = true;
368 ret &= addToModuleList(module);
369
370 return ret;
371}
372
373bool Node::deleteModule(std::shared_ptr<Module> module)
374{
375 bool ret = true;
376
377 ret &= deleteFromModuleList(module);
378
379 return ret;
380}
381
382// void Node::doTraverseBottomUp(Action* act)
383// {
384// act->start(this);
385// auto iter = mAncestors.begin();
386// for (; iter != mAncestors.end(); iter++)
387// {
388// (*iter)->doTraverseBottomUp(act);
389// }
390//
391// act->process(this);
392//
393// act->end(this);
394// }
395//
396// void Node::doTraverseTopDown(Action* act)
397// {
398// act->start(this);
399// act->process(this);
400//
401// auto iter = mAncestors.begin();
402// for (; iter != mAncestors.end(); iter++)
403// {
404// (*iter)->doTraverseTopDown(act);
405// }
406//
407// act->end(this);
408// }
409
410
411std::string FormatConnectionInfo(Node* node, NodePort* port, bool connecting, bool succeeded)
412{
413 Node* pOut = port != nullptr ? port->getParent() : nullptr;
414
415 std::string capIn = node->caption();
416 std::string capOut = pOut != nullptr ? pOut->caption() : "";
417
418 std::string nameIn = node->getName();
419 std::string nameOut = port != nullptr ? port->getPortName() : "";
420
421 if (connecting)
422 {
423 std::string message1 = capIn + ":" + nameIn + " is connected to " + capOut + ":" + nameOut;
424 std::string message2 = capIn + ":" + nameIn + " cannot be connected to " + capOut + ":" + nameOut;
425 return succeeded ? message1 : message2;
426 }
427 else
428 {
429 std::string message1 = capIn + ":" + nameIn + " is disconnected from " + capOut + ":" + nameOut;
430 std::string message2 = capIn + ":" + nameIn + " cannot be disconnected from " + capOut + ":" + nameOut;
431 return succeeded ? message1 : message2;
432 }
433}
434
436{
437 auto it = find(mExportNodes.begin(), mExportNodes.end(), nodePort);
438 if (it != mExportNodes.end()) {
439 Log::sendMessage(Log::Info, FormatConnectionInfo(this, nodePort, true, false));
440 return false;
441 }
442
443 mExportNodes.push_back(nodePort);
444
445 //Always show the last node
446 if (mAutoHidden)
447 this->setVisible(false);
448
449 Log::sendMessage(Log::Info, FormatConnectionInfo(this, nodePort, true, true));
450 return nodePort->addNode(this);
451}
452
454{
455 //TODO: this is a hack, otherwise the app will crash
456 if (mExportNodes.size() == 0) {
457 return false;
458 }
459
460 auto it = find(mExportNodes.begin(), mExportNodes.end(), nodePort);
461 if (it == mExportNodes.end()) {
462 Log::sendMessage(Log::Info, FormatConnectionInfo(this, nodePort, false, false));
463 return false;
464 }
465
466 mExportNodes.erase(it);
467
468 //Recover the visibility
469 if (mAutoHidden)
470 this->setVisible(true);
471
472 Log::sendMessage(Log::Info, FormatConnectionInfo(this, nodePort, false, true));
473 return nodePort->removeNode(this);
474}
475
477{
478
479}
480
482{
483 nPort->notify();
484
485 return this->appendExportNode(nPort);
486}
487
489{
490 return this->removeExportNode(nPort);
491}
492
493bool Node::attachField(FBase* field, std::string name, std::string desc, bool autoDestroy /*= true*/)
494{
495 field->setParent(this);
496 field->setObjectName(name);
497 field->setDescription(desc);
498 field->setAutoDestroy(autoDestroy);
499
500 bool ret = false;
501
502 auto fType = field->getFieldType();
503 switch (field->getFieldType())
504 {
506 ret = this->addField(field);
507 break;
508
510 ret = addParameter(field);
511 break;
512
514 ret = addInputField(field);
515 break;
516
518 ret = addOutputField(field);
519 break;
520
521 default:
522 break;
523 }
524
525
526 if (!ret)
527 {
528 Log::sendMessage(Log::Error, std::string("The field ") + name + std::string(" already exists!"));
529 }
530 return ret;
531}
532
534{
535 uint n = 0;
536 for(auto port : mImportNodes)
537 {
538 n += port->getNodes().size();
539 }
540
541 return n;
542}
543
545{
546 mForceUpdate = b;
547}
548
549// Node* Node::addDescendant(Node* descent)
550// {
551// if (hasDescendant(descent) || descent == nullptr)
552// return descent;
553//
554// mDescendants.push_back(descent);
555// return descent;
556// }
557//
558// bool Node::hasDescendant(Node* descent)
559// {
560// auto it = std::find(mDescendants.begin(), mDescendants.end(), descent);
561// return it == mDescendants.end() ? false : true;
562// }
563//
564// void Node::removeDescendant(Node* descent)
565// {
566// auto iter = mDescendants.begin();
567// for (; iter != mDescendants.end(); )
568// {
569// if (*iter == descent)
570// {
571// mDescendants.erase(iter++);
572// }
573// else
574// {
575// ++iter;
576// }
577// }
578// }
579
581{
582 mImportNodes.push_back(port);
583
584 return true;
585}
586
587// void Node::setAsCurrentContext()
588// {
589// getContext()->enable();
590// }
591
592// void Node::setTopologyModule(std::shared_ptr<TopologyModule> topology)
593// {
594// if (m_topology != nullptr)
595// {
596// deleteModule(m_topology);
597// }
598// m_topology = topology;
599// addModule(topology);
600// }
601//
602// void Node::setNumericalModel(std::shared_ptr<NumericalModel> numerical)
603// {
604// if (m_numerical_model != nullptr)
605// {
606// deleteModule(m_numerical_model);
607// }
608// m_numerical_model = numerical;
609// addModule(numerical);
610// }
611//
612// void Node::setCollidableObject(std::shared_ptr<CollidableObject> collidable)
613// {
614// if (m_collidable_object != nullptr)
615// {
616// deleteModule(m_collidable_object);
617// }
618// m_collidable_object = collidable;
619// addModule(collidable);
620// }
621
622std::shared_ptr<Module> Node::getModule(std::string name)
623{
624 std::shared_ptr<Module> base = nullptr;
625 std::list<std::shared_ptr<Module>>::iterator iter;
626 for (iter = mModuleList.begin(); iter != mModuleList.end(); iter++)
627 {
628 if ((*iter)->getName() == name)
629 {
630 base = *iter;
631 break;
632 }
633 }
634 return base;
635}
636
637bool Node::hasModule(std::string name)
638{
639 if (getModule(name) == nullptr)
640 return false;
641
642 return true;
643}
644
645/*Module* Node::getModule(std::string name)
646{
647 std::map<std::string, Module*>::iterator result = m_modules.find(name);
648 if (result == m_modules.end())
649 {
650 return NULL;
651 }
652
653 return result->second;
654}*/
655
656
657bool Node::addToModuleList(std::shared_ptr<Module> module)
658{
659 auto found = std::find(mModuleList.begin(), mModuleList.end(), module);
660 if (found == mModuleList.end())
661 {
662 mModuleList.push_back(module);
663 module->setParentNode(this);
664 return true;
665 }
666
667 return false;
668}
669
670bool Node::deleteFromModuleList(std::shared_ptr<Module> module)
671{
672 auto found = std::find(mModuleList.begin(), mModuleList.end(), module);
673 if (found != mModuleList.end())
674 {
675 mModuleList.erase(found);
676 return true;
677 }
678
679 return true;
680}
681
682}
double Real
Definition Typedef.inl:23
FieldTypeEnum getFieldType()
Definition FBase.cpp:300
void setParent(OBase *owner)
Definition FBase.cpp:36
void setObjectName(std::string name)
Definition FBase.h:58
void setAutoDestroy(bool autoDestroy)
Definition FBase.cpp:155
void setDescription(std::string description)
Definition FBase.h:59
@ Info
Information to user.
Definition Log.h:48
@ Error
Error information while executing something.
Definition Log.h:50
static void sendMessage(MessageType type, const std::string &text)
Add a new message to log.
Definition Log.cpp:41
uint sizeOfImportNodes() const
Definition Node.cpp:533
bool attachField(FBase *field, std::string name, std::string desc, bool autoDestroy=true) override
Attach a field to Node.
Definition Node.cpp:493
bool appendExportNode(NodePort *nodePort)
Definition Node.cpp:435
std::shared_ptr< GraphicsPipeline > graphicsPipeline()
Definition Node.cpp:320
bool mAutoHidden
Definition Node.h:322
std::vector< NodePort * > mExportNodes
Definition Node.h:348
virtual void updateTopology()
Definition Node.cpp:476
bool addModule(std::shared_ptr< Module > module)
Add a module to m_module_list and other special module lists.
Definition Node.cpp:365
virtual void resetStates()
Definition Node.cpp:214
virtual bool isActive()
Check the state of dynamics.
Definition Node.cpp:73
virtual void setActive(bool active)
Set the state of dynamics.
Definition Node.cpp:78
void setForceUpdate(bool b)
Definition Node.cpp:544
void setAutoSync(bool con)
Whether the node can be automatically synchronized when its ancestor is updated.
Definition Node.cpp:63
void setDt(Real dt)
Definition Node.cpp:98
virtual Real getDt()
Simulation timestep.
Definition Node.cpp:93
bool disconnect(NodePort *nPort)
Definition Node.cpp:488
virtual void updateStates()
Definition Node.cpp:141
bool isAutoSync()
Definition Node.cpp:53
friend class NodePort
Definition Node.h:352
std::string m_node_name
Definition Node.h:301
bool mPhysicsEnabled
Definition Node.h:314
bool addNodePort(NodePort *port)
Definition Node.cpp:580
std::shared_ptr< Pipeline > mResetPipeline
Pointer of the pipeline.
Definition Node.h:340
virtual std::string getNodeType()
Definition Node.cpp:48
virtual bool requireUpdate()
Definition Node.cpp:237
bool removeExportNode(NodePort *nodePort)
Definition Node.cpp:453
bool addToModuleList(std::shared_ptr< Module > module)
Definition Node.cpp:657
std::shared_ptr< AnimationPipeline > mAnimationPipeline
Definition Node.h:341
void update()
Called every time interval.
Definition Node.cpp:146
void setSceneGraph(SceneGraph *scn)
Definition Node.cpp:103
Real mDt
Time step size.
Definition Node.h:328
virtual void preUpdateStates()
Definition Node.cpp:136
std::shared_ptr< Pipeline > resetPipeline()
Definition Node.cpp:302
void updateGraphicsContext()
Definition Node.cpp:206
void tick()
notify all state and output fields are updated
Definition Node.cpp:266
bool mAutoSync
A parameter to control whether the node can be updated automatically by its ancestor.
Definition Node.h:312
SceneGraph * getSceneGraph()
Definition Node.cpp:108
SceneGraph * mSceneGraph
Definition Node.h:350
virtual NBoundingBox boundingBox()
Definition Node.cpp:196
bool deleteFromModuleList(std::shared_ptr< Module > module)
Definition Node.cpp:670
bool isAutoHidden()
Definition Node.cpp:58
~Node() override
Definition Node.cpp:16
bool deleteModule(std::shared_ptr< Module > module)
Definition Node.cpp:373
std::list< std::shared_ptr< Module > > mModuleList
A module list containing all modules.
Definition Node.h:334
void setName(std::string name)
Definition Node.cpp:38
void reset()
Definition Node.cpp:183
std::shared_ptr< TModule > getModule()
Get the Module by the module class name.
Definition Node.h:168
std::shared_ptr< AnimationPipeline > animationPipeline()
Definition Node.cpp:311
virtual bool validateInputs()
Definition Node.cpp:219
std::string getName() override
Definition Node.cpp:43
bool mRenderingEnabled
Definition Node.h:316
std::shared_ptr< GraphicsPipeline > mGraphicsPipeline
Definition Node.h:342
std::vector< NodePort * > mImportNodes
Definition Node.h:346
virtual void setVisible(bool visible)
Set the visibility of context.
Definition Node.cpp:88
bool connect(NodePort *nPort)
Depth-first tree traversal.
Definition Node.cpp:481
void setAutoHidden(bool con)
Definition Node.cpp:68
virtual bool isVisible()
Check the visibility of context.
Definition Node.cpp:83
bool mForceUpdate
Definition Node.h:320
virtual void postUpdateStates()
Definition Node.cpp:201
bool hasModule(std::string name)
Definition Node.cpp:637
Input ports for Node.
Definition NodePort.h:38
virtual bool removeNode(Node *node)=0
Node * getParent()
Definition NodePort.h:56
virtual bool addNode(Node *node)=0
virtual std::string getPortName()
Definition NodePort.h:43
virtual void notify()
Definition NodePort.cpp:36
std::vector< FBase * > & getAllFields()
Definition OBase.cpp:202
std::vector< FBase * > fields_param
Definition OBase.h:211
bool addField(FBase *data)
Add a field to Base FieldID will be set to the name of Field by default.
Definition OBase.cpp:45
virtual std::string caption()
Return the caption.
Definition OBase.cpp:19
bool addParameter(FBase *field)
Definition OBase.cpp:390
std::vector< FBase * > fields_output
Definition OBase.h:210
std::vector< FBase * > fields_input
Definition OBase.h:209
bool addOutputField(FBase *field)
Definition OBase.cpp:319
bool addInputField(FBase *field)
Definition OBase.cpp:276
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
std::string FormatConnectionInfo(FBase *fin, FBase *fout, bool connecting, bool succeeded)
Definition FBase.cpp:11
@ In
Definition FBase.h:31
@ Param
Definition FBase.h:34
@ Out
Definition FBase.h:32
@ State
Definition FBase.h:35
unsigned int uint
Definition VkProgram.h:14