PeriDyno 1.2.1
Loading...
Searching...
No Matches
Module.cpp
Go to the documentation of this file.
1#include "Module.h"
2#include "Node.h"
3#include "SceneGraph.h"
4
5//#define PYTHON
6
7namespace dyno
8{
9 Module::Module(std::string name)
10 : OBase()
11 , m_node(nullptr)
12 , m_initialized(false)
13 {
14 m_module_name = name;
15 }
16
18 {
19 mImportModules.clear();
20 mExportModules.clear();
21 }
22
24 {
25 if (m_initialized)
26 {
27 return true;
28 }
30
31 return m_initialized;
32 }
33
35 {
36#ifdef PYTHON
37 std::cout << "Module::update" << std::endl;
38#endif // PYTHON
39
40 if (!isInitialized())
41 {
42 bool ret = initialize();
43 if (ret == false)
44 return;
45 }
46
47 this->updateStarted();
48
49 if (!this->validateInputs()) {
50 return;
51 }
52
53 if (this->requireUpdate()) {
54 //pre processing
55 this->preprocess();
56
57 //do execution if any field is modified
58 this->updateImpl();
59
60 //post processing
61 this->postprocess();
62
63 //reset parameters
64 for (auto param : fields_param)
65 {
66 param->tack();
67 }
68
69 //reset input fields
70 for (auto f_in : fields_input)
71 {
72 f_in->tack();
73 }
74
75 //tag all output fields as modifed
76 for (auto f_out : fields_output)
77 {
78 f_out->tick();
79 }
80 }
81
82 if (!this->validateOutputs()) {
83 return;
84 }
85
86 this->updateEnded();
87 }
88
90 {
91 return isInputComplete();
92 }
93
95 {
96 //If any input field is empty, return false;
97 for (auto f_in : fields_input)
98 {
99 if (!f_in->isOptional() && f_in->isEmpty())
100 {
101 Node* par = this->getParentNode();
102 if (par != nullptr && par->getSceneGraph() != nullptr && par->getSceneGraph()->isValidationInfoPrintable())
103 {
104 std::string errMsg = std::string("The input field ") + f_in->getObjectName() +
105 std::string(" in Module ") + this->getClassInfo()->getClassName() + std::string(" is not set!");
106
108 }
109
110 return false;
111 }
112 }
113
114 return true;
115 }
116
118 {
119 //If any output field is empty, return false;
120 for (auto f_out : fields_output)
121 {
122 if (f_out->isEmpty())
123 {
124 Node* par = this->getParentNode();
125 if (par != nullptr && par->getSceneGraph() != nullptr && par->getSceneGraph()->isValidationInfoPrintable())
126 {
127 std::string errMsg = std::string("The output field ") + f_out->getObjectName() +
128 std::string(" in Module ") + this->getClassInfo()->getClassName() + std::string(" is not prepared!");
129
131 }
132 return false;
133 }
134 }
135
136 return true;
137 }
138
140 {
141 if (!this->allowExported() || !nPort->getParent()->allowImported())
142 return false;
143
144 nPort->notify();
145
146 return this->appendExportModule(nPort);
147 }
148
150 {
151 return this->removeExportModule(nPort);
152 }
153
155 {
156 }
157
159 {
160 }
161
163 {
164 auto it = find(mExportModules.begin(), mExportModules.end(), nodePort);
165 if (it != mExportModules.end()) {
166 //Log::sendMessage(Log::Info, FormatConnectionInfo(this, nodePort, true, false));
167 return false;
168 }
169
170 mExportModules.push_back(nodePort);
171
172 //Log::sendMessage(Log::Info, FormatConnectionInfo(this, nodePort, true, true));
173 return nodePort->addModule(this);
174 }
175
177 {
178 //TODO: this is a hack, otherwise the app will crash
179 if (mExportModules.size() == 0) {
180 return false;
181 }
182
183 auto it = find(mExportModules.begin(), mExportModules.end(), nodePort);
184 if (it == mExportModules.end()) {
185 //Log::sendMessage(Log::Info, FormatConnectionInfo(this, nodePort, false, false));
186 return false;
187 }
188
189 mExportModules.erase(it);
190
191 //Log::sendMessage(Log::Info, FormatConnectionInfo(this, nodePort, false, true));
192 return nodePort->removeModule(this);
193 }
194
196 {
197 mImportModules.push_back(port);
198
199 return true;
200 }
201
203 {
204 return isOutputCompete();
205 }
206
208 {
209 if (this->varForceUpdate()->getValue())
210 {
211 return true;
212 }
213
214 //check input fields
215 bool modified = false;
216 for (auto f_in : fields_input)
217 {
218 modified |= f_in->isModified();
219 }
220
221 //check control fields
222 for (auto var : fields_param)
223 {
224 modified |= var->isModified();
225 }
226
227 return modified;
228 }
229
230 void Module::setName(std::string name)
231 {
232 //m_module_name.setValue(name);
233 m_module_name = name;
234 }
235
237 {
238 m_node = node;
239 }
240
241 std::string Module::getName()
242 {
243 return m_module_name;
244 }
245
247 {
248 if (m_node == NULL) {
249 Log::sendMessage(Log::Error, "Parent node is not set!");
250 }
251
252 return m_node;
253 }
254
256 {
257 auto node = this->getParentNode();
258
259 if (node == NULL) return NULL;
260
261 return node->getSceneGraph();
262 }
263
265 {
266 this->varForceUpdate()->setValue(b);
267 }
268
270 {
271 return m_initialized;
272 }
273
275 {
276 return true;
277 }
278
280 {
281 }
282
283 bool Module::attachField(FBase* field, std::string name, std::string desc, bool autoDestroy)
284 {
285 field->setParent(this);
286 field->setObjectName(name);
287 field->setDescription(desc);
288 field->setAutoDestroy(autoDestroy);
289
290 bool ret = false;
291 auto fType = field->getFieldType();
292 switch (field->getFieldType())
293 {
295 ret = addInputField(field);
296 break;
297
299 ret = addInputField(field);
300 break;
301
303 ret = addOutputField(field);
304 break;
305
307 ret = addParameter(field);
308 break;
309
310 default:
311 break;
312 }
313
314 if (!ret)
315 {
316 Log::sendMessage(Log::Error, std::string("The field ") + name + std::string(" already exists!"));
317 }
318 return ret;
319 }
320}
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
@ 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
virtual void updateImpl()
Definition Module.cpp:279
Node * m_node
Definition Module.h:134
SceneGraph * getSceneGraph()
Definition Module.cpp:255
void setName(std::string name)
Definition Module.cpp:230
bool isInitialized()
Definition Module.cpp:269
virtual bool validateOutputs()
Definition Module.cpp:202
virtual bool allowImported()
Definition Module.h:88
~Module(void) override
Definition Module.cpp:17
virtual void setParentNode(Node *node)
Set the parent node.
Definition Module.cpp:236
Node * getParentNode()
Definition Module.cpp:246
friend class ModulePort
Definition Module.h:153
virtual void preprocess()
Definition Module.h:111
virtual bool allowExported()
Definition Module.h:89
std::vector< ModulePort * > mExportModules
Definition Module.h:140
bool isOutputCompete()
Definition Module.cpp:117
void setUpdateAlways(bool b)
Set the update strategy for the module.
Definition Module.cpp:264
void update()
Definition Module.cpp:34
virtual void postprocess()
Definition Module.h:113
bool initialize()
Definition Module.cpp:23
virtual bool initializeImpl()
Definition Module.cpp:274
bool appendExportModule(ModulePort *nodePort)
Definition Module.cpp:162
bool isInputComplete()
Check the completeness of input fields.
Definition Module.cpp:94
bool connect(ModulePort *nPort)
Definition Module.cpp:139
std::string getName() override
Definition Module.cpp:241
virtual void updateStarted()
Two functions called at the beginning and end of update() used for debug.
Definition Module.cpp:154
bool addModulePort(ModulePort *port)
Definition Module.cpp:195
Module(std::string name="default")
Definition Module.cpp:9
std::vector< ModulePort * > mImportModules
Definition Module.h:138
bool removeExportModule(ModulePort *nodePort)
Definition Module.cpp:176
virtual bool requireUpdate()
Definition Module.cpp:207
std::string m_module_name
Definition Module.h:135
bool disconnect(ModulePort *nPort)
Definition Module.cpp:149
virtual void updateEnded()
Definition Module.cpp:158
virtual bool validateInputs()
Definition Module.cpp:89
bool attachField(FBase *field, std::string name, std::string desc, bool autoDestroy=true) override
Attach a field to Base.
Definition Module.cpp:283
bool m_initialized
Definition Module.h:136
virtual bool removeModule(Module *m)=0
Module * getParent()
Definition ModulePort.h:56
virtual bool addModule(Module *m)=0
virtual void notify()
SceneGraph * getSceneGraph()
Definition Node.cpp:99
std::vector< FBase * > fields_param
Definition OBase.h:211
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
bool isValidationInfoPrintable()
Definition SceneGraph.h:64
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
@ In
Definition FBase.h:31
@ Param
Definition FBase.h:34
@ Out
Definition FBase.h:32
@ IO
Definition FBase.h:33