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