1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include <common/nonstd_math.h> |
---|
6 | #include "model.h" |
---|
7 | #include <common/log.h> |
---|
8 | #include <frams/util/multimap.h> |
---|
9 | #include <common/loggers/loggers.h> |
---|
10 | |
---|
11 | #define F0_CHECKPOINT_LINE "checkpoint:" |
---|
12 | |
---|
13 | Model::Model(ShapeType sh) |
---|
14 | { |
---|
15 | autobuildmaps = false; |
---|
16 | init(sh); |
---|
17 | } |
---|
18 | |
---|
19 | void Model::init(ShapeType shtype) |
---|
20 | { |
---|
21 | partmappingchanged = 0; |
---|
22 | using_checkpoints = false; |
---|
23 | is_checkpoint = false; |
---|
24 | buildstatus = empty; |
---|
25 | modelfromgenotype = 0; |
---|
26 | startenergy = 1.0; |
---|
27 | checklevel = 1; |
---|
28 | map = 0; |
---|
29 | f0map = 0; |
---|
30 | f0genoknown = 1; |
---|
31 | shapetype = SHAPETYPE_UNKNOWN; |
---|
32 | declared_shapetype = shtype; |
---|
33 | } |
---|
34 | |
---|
35 | void Model::declareShapeType(ShapeType shtype) |
---|
36 | { |
---|
37 | declared_shapetype = shtype; |
---|
38 | } |
---|
39 | |
---|
40 | void Model::moveElementsFrom(Model &source) |
---|
41 | { |
---|
42 | int i; |
---|
43 | open(); |
---|
44 | for (i = 0; i < source.getPartCount(); i++) |
---|
45 | addPart(source.getPart(i)); |
---|
46 | for (i = 0; i < source.getJointCount(); i++) |
---|
47 | addJoint(source.getJoint(i)); |
---|
48 | for (i = 0; i < source.getNeuroCount(); i++) |
---|
49 | addNeuro(source.getNeuro(i)); |
---|
50 | source.parts.clear(); source.joints.clear(); source.neurons.clear(); |
---|
51 | source.clear(); |
---|
52 | } |
---|
53 | |
---|
54 | void Model::internalCopy(const Model &mod) |
---|
55 | { |
---|
56 | geno = mod.geno; |
---|
57 | f0genoknown = 0; |
---|
58 | shapetype = mod.shapetype; |
---|
59 | declared_shapetype = mod.declared_shapetype; |
---|
60 | startenergy = mod.startenergy; |
---|
61 | modelfromgenotype = mod.modelfromgenotype; |
---|
62 | for (int i = 0; i < mod.getPartCount(); i++) |
---|
63 | addPart(new Part(*mod.getPart(i))); |
---|
64 | for (int i = 0; i < mod.getJointCount(); i++) |
---|
65 | { |
---|
66 | Joint *oldj = mod.getJoint(i); |
---|
67 | Joint *j = new Joint(*oldj); |
---|
68 | addJoint(j); |
---|
69 | j->attachToParts(oldj->part1->refno, oldj->part2->refno); |
---|
70 | } |
---|
71 | for (int i = 0; i < mod.getNeuroCount(); i++) |
---|
72 | { |
---|
73 | Neuro *oldn = mod.getNeuro(i); |
---|
74 | Neuro *n = new Neuro(*oldn); |
---|
75 | addNeuro(n); |
---|
76 | if (oldn->part_refno >= 0) n->attachToPart(oldn->part_refno); |
---|
77 | else n->attachToJoint(oldn->joint_refno); |
---|
78 | } |
---|
79 | for (int i = 0; i < mod.getNeuroCount(); i++) |
---|
80 | { |
---|
81 | Neuro *oldn = mod.getNeuro(i); |
---|
82 | Neuro *n = getNeuro(i); |
---|
83 | for (int ni = 0; ni < oldn->getInputCount(); ni++) |
---|
84 | { |
---|
85 | double w; |
---|
86 | Neuro *oldinput = oldn->getInput(ni, w); |
---|
87 | SString info = n->getInputInfo(ni); |
---|
88 | n->addInput(getNeuro(oldinput->refno), w, &info); |
---|
89 | } |
---|
90 | } |
---|
91 | updateRefno(); |
---|
92 | if (using_checkpoints) |
---|
93 | for (vector<Model *>::const_iterator it = mod.checkpoints.begin(); it != mod.checkpoints.end(); it++) |
---|
94 | { |
---|
95 | Model *m = *it; |
---|
96 | Model *n = new Model(*m, m->autobuildmaps, false, true); |
---|
97 | checkpoints.push_back(n); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | Model::Model(const Geno &src, ShapeType st, bool buildmaps, bool _using_checkpoints, bool _is_checkpoint) |
---|
103 | :autobuildmaps(buildmaps) |
---|
104 | { |
---|
105 | init(src, st, _using_checkpoints, _is_checkpoint); |
---|
106 | } |
---|
107 | |
---|
108 | void Model::operator=(const Model &mod) |
---|
109 | { |
---|
110 | clear(); |
---|
111 | open(mod.isUsingCheckpoints(), mod.isCheckpoint()); |
---|
112 | internalCopy(mod); |
---|
113 | buildstatus = mod.buildstatus; |
---|
114 | } |
---|
115 | |
---|
116 | Model::Model(const Model &mod, bool buildmaps, bool _using_checkpoints, bool _is_checkpoint) |
---|
117 | :autobuildmaps(buildmaps) |
---|
118 | { |
---|
119 | init(mod.declared_shapetype); |
---|
120 | open(_using_checkpoints, _is_checkpoint); |
---|
121 | internalCopy(mod); |
---|
122 | if (is_checkpoint) |
---|
123 | close(); |
---|
124 | else |
---|
125 | buildstatus = mod.buildstatus; |
---|
126 | if (mod.map) |
---|
127 | map = new MultiMap(*mod.map); |
---|
128 | if (mod.f0map) |
---|
129 | f0map = new MultiMap(*mod.f0map); |
---|
130 | } |
---|
131 | |
---|
132 | void Model::init(const Geno &src, ShapeType sh, bool _using_checkpoints, bool _is_checkpoint) |
---|
133 | { |
---|
134 | init(sh); |
---|
135 | using_checkpoints = _using_checkpoints; |
---|
136 | is_checkpoint = _is_checkpoint; |
---|
137 | modelfromgenotype = 1; |
---|
138 | geno = src; |
---|
139 | build(); |
---|
140 | } |
---|
141 | |
---|
142 | void Model::resetAllDelta() |
---|
143 | { |
---|
144 | for (int i = 0; i < getJointCount(); i++) |
---|
145 | getJoint(i)->resetDelta(); |
---|
146 | } |
---|
147 | |
---|
148 | void Model::useAllDelta(bool yesno) |
---|
149 | { |
---|
150 | for (int i = 0; i < getJointCount(); i++) |
---|
151 | getJoint(i)->useDelta(yesno); |
---|
152 | } |
---|
153 | |
---|
154 | Model::~Model() |
---|
155 | { |
---|
156 | delmodel_list.action((intptr_t)this); |
---|
157 | clear(); |
---|
158 | } |
---|
159 | |
---|
160 | void Model::clear() |
---|
161 | { |
---|
162 | FOREACH(Part *, p, parts) |
---|
163 | delete p; |
---|
164 | FOREACH(Joint *, j, joints) |
---|
165 | delete j; |
---|
166 | FOREACH(Neuro *, n, neurons) |
---|
167 | delete n; |
---|
168 | parts.clear(); joints.clear(); neurons.clear(); |
---|
169 | delMap(); |
---|
170 | delF0Map(); |
---|
171 | init(declared_shapetype); |
---|
172 | geno = Geno(); |
---|
173 | f0geno = Geno(); |
---|
174 | for (vector<Model *>::iterator it = checkpoints.begin(); it != checkpoints.end(); it++) |
---|
175 | delete *it; |
---|
176 | checkpoints.clear(); |
---|
177 | } |
---|
178 | |
---|
179 | Part *Model::addPart(Part *p) |
---|
180 | { |
---|
181 | p->owner = this; |
---|
182 | p->refno = parts.size(); |
---|
183 | parts += p; |
---|
184 | return p; |
---|
185 | } |
---|
186 | |
---|
187 | Joint *Model::addJoint(Joint *j) |
---|
188 | { |
---|
189 | j->owner = this; |
---|
190 | j->refno = joints.size(); |
---|
191 | joints += j; |
---|
192 | return j; |
---|
193 | } |
---|
194 | |
---|
195 | Neuro *Model::addNeuro(Neuro *n) |
---|
196 | { |
---|
197 | n->owner = this; |
---|
198 | n->refno = neurons.size(); |
---|
199 | neurons += n; |
---|
200 | return n; |
---|
201 | } |
---|
202 | |
---|
203 | void Model::removeNeuros(SList &nlist) |
---|
204 | { |
---|
205 | FOREACH(Neuro *, nu, nlist) |
---|
206 | { |
---|
207 | int i = findNeuro(nu); |
---|
208 | if (i >= 0) removeNeuro(i); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | void Model::removePart(int partindex, int removeattachedjoints, int removeattachedneurons) |
---|
213 | { |
---|
214 | Part *p = getPart(partindex); |
---|
215 | if (removeattachedjoints) |
---|
216 | { |
---|
217 | SList jlist; |
---|
218 | findJoints(jlist, p); |
---|
219 | FOREACH(Joint *, j, jlist) |
---|
220 | { |
---|
221 | int i = findJoint(j); |
---|
222 | if (i >= 0) removeJoint(i, removeattachedneurons); |
---|
223 | } |
---|
224 | } |
---|
225 | if (removeattachedneurons) |
---|
226 | { |
---|
227 | SList nlist; |
---|
228 | findNeuros(nlist, 0, p); |
---|
229 | removeNeuros(nlist); |
---|
230 | } |
---|
231 | parts -= partindex; |
---|
232 | delete p; |
---|
233 | } |
---|
234 | |
---|
235 | void Model::removeJoint(int jointindex, int removeattachedneurons) |
---|
236 | { |
---|
237 | Joint *j = getJoint(jointindex); |
---|
238 | if (removeattachedneurons) |
---|
239 | { |
---|
240 | SList nlist; |
---|
241 | findNeuros(nlist, 0, 0, j); |
---|
242 | removeNeuros(nlist); |
---|
243 | } |
---|
244 | joints -= jointindex; |
---|
245 | delete j; |
---|
246 | } |
---|
247 | |
---|
248 | void Model::removeNeuro(int neuroindex, bool removereferences) |
---|
249 | { |
---|
250 | Neuro *thisN = getNeuro(neuroindex); |
---|
251 | |
---|
252 | if (removereferences) |
---|
253 | { |
---|
254 | Neuro *n; |
---|
255 | // remove all references to thisN |
---|
256 | for (int i = 0; n = (Neuro *)neurons(i); i++) |
---|
257 | { |
---|
258 | Neuro *inp; |
---|
259 | for (int j = 0; inp = n->getInput(j); j++) |
---|
260 | if (inp == thisN) |
---|
261 | { |
---|
262 | n->removeInput(j); |
---|
263 | j--; |
---|
264 | } |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | neurons -= neuroindex; |
---|
269 | delete thisN; |
---|
270 | } |
---|
271 | |
---|
272 | MultiMap &Model::getMap() |
---|
273 | { |
---|
274 | if (!map) map = new MultiMap(); |
---|
275 | return *map; |
---|
276 | } |
---|
277 | |
---|
278 | void Model::delMap() |
---|
279 | { |
---|
280 | if (map) { delete map; map = 0; } |
---|
281 | } |
---|
282 | void Model::delF0Map() |
---|
283 | { |
---|
284 | if (f0map) { delete f0map; f0map = 0; } |
---|
285 | } |
---|
286 | |
---|
287 | void Model::makeGenToGenMap(MultiMap &result, const MultiMap &gen1tomodel, const MultiMap &gen2tomodel) |
---|
288 | { |
---|
289 | result.clear(); |
---|
290 | MultiMap m; |
---|
291 | m.addReversed(gen2tomodel); |
---|
292 | result.addCombined(gen1tomodel, m); |
---|
293 | } |
---|
294 | |
---|
295 | void Model::getCurrentToF0Map(MultiMap &result) |
---|
296 | { |
---|
297 | result.clear(); |
---|
298 | if (!map) return; |
---|
299 | const MultiMap &f0m = getF0Map(); |
---|
300 | makeGenToGenMap(result, *map, f0m); |
---|
301 | } |
---|
302 | |
---|
303 | void Model::rebuild(bool buildm) |
---|
304 | { |
---|
305 | autobuildmaps = buildm; |
---|
306 | clear(); |
---|
307 | build(); |
---|
308 | } |
---|
309 | |
---|
310 | void Model::initMap() |
---|
311 | { |
---|
312 | if (!map) map = new MultiMap(); |
---|
313 | else map->clear(); |
---|
314 | } |
---|
315 | |
---|
316 | void Model::initF0Map() |
---|
317 | { |
---|
318 | if (!f0map) f0map = new MultiMap(); |
---|
319 | else f0map->clear(); |
---|
320 | } |
---|
321 | |
---|
322 | Model::ItemType Model::itemTypeFromLinePrefix(const char *line) |
---|
323 | { |
---|
324 | struct PrefixAndItem { const char *prefix; ItemType type; }; |
---|
325 | static const PrefixAndItem types[] = { { "m:", ModelType }, { "p:", PartType }, { "j:", JointType }, { "n:", NeuronType }, { "c:", NeuronConnectionType }, { F0_CHECKPOINT_LINE, CheckpointType }, { NULL } }; |
---|
326 | for (const PrefixAndItem *t = types; t->prefix != NULL; t++) |
---|
327 | { |
---|
328 | const char *in = line; |
---|
329 | const char *pattern = t->prefix; |
---|
330 | for (; *in == *pattern; in++, pattern++) |
---|
331 | if (*pattern == ':') |
---|
332 | return t->type; |
---|
333 | } |
---|
334 | return UnknownType; |
---|
335 | } |
---|
336 | |
---|
337 | void Model::build() |
---|
338 | { |
---|
339 | f0errorposition = -1; |
---|
340 | f0warnposition = -1; |
---|
341 | MultiMap *convmap = autobuildmaps ? new MultiMap() : NULL; |
---|
342 | if (declared_shapetype == SHAPETYPE_UNKNOWN) |
---|
343 | f0geno = geno.getConverted(Geno::F0_FORMAT_LIST, convmap, using_checkpoints); |
---|
344 | else |
---|
345 | f0geno = geno.getConverted(genoFormatForShapeType(declared_shapetype), convmap, using_checkpoints); |
---|
346 | f0genoknown = 1; |
---|
347 | if (f0geno.isInvalid()) |
---|
348 | { |
---|
349 | buildstatus = invalid; |
---|
350 | if (convmap) delete convmap; |
---|
351 | return; |
---|
352 | } |
---|
353 | if (declared_shapetype == SHAPETYPE_UNKNOWN) |
---|
354 | declareShapeType(shapeTypeForGenoFormat(f0geno.getFormat())); |
---|
355 | SString f0txt = f0geno.getGenes(); |
---|
356 | buildstatus = building; // was: open(); |
---|
357 | if (autobuildmaps) |
---|
358 | { |
---|
359 | partmappingchanged = 0; |
---|
360 | initMap(); |
---|
361 | initF0Map(); |
---|
362 | } |
---|
363 | int pos = 0, lnum = 1, lastpos = 0; |
---|
364 | SString line; |
---|
365 | MultiRange frommap; |
---|
366 | LoggerToMemory mh(LoggerBase::Enable | LoggerBase::DontBlock); |
---|
367 | Model *current_model = this; |
---|
368 | for (; f0txt.getNextToken(pos, line, '\n'); lnum++) |
---|
369 | { |
---|
370 | const char *line_ptr = line.c_str(); |
---|
371 | for (; *line_ptr; line_ptr++) |
---|
372 | if (!strchr(" \r\t", *line_ptr)) break; |
---|
373 | if (*line_ptr == '#') continue; |
---|
374 | if (!*line_ptr) continue; |
---|
375 | |
---|
376 | const char *colon = strchr(line_ptr, ':'); |
---|
377 | ItemType type = UnknownType; |
---|
378 | SString excluding_prefix; |
---|
379 | if (colon != NULL) |
---|
380 | { |
---|
381 | colon++; |
---|
382 | type = itemTypeFromLinePrefix(line_ptr); |
---|
383 | for (; *colon; colon++) |
---|
384 | if (!strchr(" \r\t", *colon)) break; |
---|
385 | excluding_prefix = colon; |
---|
386 | } |
---|
387 | |
---|
388 | if (autobuildmaps) |
---|
389 | { |
---|
390 | frommap.clear(); |
---|
391 | frommap.add(lastpos, pos - 1); |
---|
392 | } |
---|
393 | mh.reset(); |
---|
394 | if (type == CheckpointType) |
---|
395 | { |
---|
396 | current_model->close(); |
---|
397 | current_model = new Model; |
---|
398 | current_model->open(false, true); |
---|
399 | checkpoints.push_back(current_model); |
---|
400 | } |
---|
401 | else if (current_model->addFromString(type, excluding_prefix, lnum, autobuildmaps ? (&frommap) : 0) == -1) |
---|
402 | { |
---|
403 | buildstatus = invalid; |
---|
404 | f0errorposition = lastpos; |
---|
405 | if (convmap) delete convmap; |
---|
406 | return; |
---|
407 | } |
---|
408 | if (mh.getWarningCount()) |
---|
409 | { |
---|
410 | if (f0warnposition < 0) f0warnposition = lastpos; |
---|
411 | } |
---|
412 | lastpos = pos; |
---|
413 | } |
---|
414 | mh.disable(); |
---|
415 | current_model->close(); |
---|
416 | if (convmap) |
---|
417 | { |
---|
418 | *f0map = *map; |
---|
419 | if (!Geno::formatIsOneOf(geno.getFormat(), Geno::F0_FORMAT_LIST)) |
---|
420 | { |
---|
421 | MultiMap tmp; |
---|
422 | tmp.addCombined(*convmap, getMap()); |
---|
423 | *map = tmp; |
---|
424 | } |
---|
425 | delete convmap; |
---|
426 | } |
---|
427 | } |
---|
428 | |
---|
429 | const MultiMap &Model::getF0Map() |
---|
430 | { |
---|
431 | if (!f0map) |
---|
432 | { |
---|
433 | f0map = new MultiMap(); |
---|
434 | makeGeno(f0geno, f0map); |
---|
435 | f0genoknown = 1; |
---|
436 | } |
---|
437 | return *f0map; |
---|
438 | } |
---|
439 | |
---|
440 | Geno Model::rawGeno() |
---|
441 | { |
---|
442 | Geno tmpgen; |
---|
443 | makeGeno(tmpgen); |
---|
444 | return tmpgen; |
---|
445 | } |
---|
446 | |
---|
447 | void Model::makeGeno(Geno &g, MultiMap *map, bool handle_defaults) |
---|
448 | { |
---|
449 | if ((buildstatus != valid) && (buildstatus != building)) |
---|
450 | { |
---|
451 | g = Geno("", Geno::FORMAT_INVALID, "", "invalid model"); |
---|
452 | return; |
---|
453 | } |
---|
454 | |
---|
455 | SString gen; |
---|
456 | |
---|
457 | Param modelparam(f0_model_paramtab); |
---|
458 | Param partparam(f0_part_paramtab); |
---|
459 | Param jointparam(f0_joint_paramtab); |
---|
460 | Param neuroparam(f0_neuro_paramtab); |
---|
461 | Param connparam(f0_neuroconn_paramtab); |
---|
462 | |
---|
463 | static Part defaultpart; |
---|
464 | static Joint defaultjoint; |
---|
465 | static Neuro defaultneuro; |
---|
466 | static Model defaultmodel; |
---|
467 | static NeuroConn defaultconn; |
---|
468 | //static NeuroItem defaultneuroitem; |
---|
469 | |
---|
470 | Part *p; |
---|
471 | Joint *j; |
---|
472 | Neuro *n; |
---|
473 | int i; |
---|
474 | int len; |
---|
475 | int a, b; |
---|
476 | //NeuroItem *ni; |
---|
477 | |
---|
478 | SString mod_props; |
---|
479 | modelparam.select(this); |
---|
480 | modelparam.saveSingleLine(mod_props, handle_defaults ? &defaultmodel : NULL, true, !handle_defaults); |
---|
481 | if (mod_props.length() > 1) //are there any non-default values? ("\n" is empty) |
---|
482 | { |
---|
483 | gen += "m:"; |
---|
484 | gen += mod_props; |
---|
485 | } |
---|
486 | |
---|
487 | for (i = 0; p = (Part *)parts(i); i++) |
---|
488 | { |
---|
489 | partparam.select(p); |
---|
490 | len = gen.length(); |
---|
491 | gen += "p:"; |
---|
492 | partparam.saveSingleLine(gen, handle_defaults ? &defaultpart : NULL, true, !handle_defaults); |
---|
493 | if (map) |
---|
494 | map->add(len, gen.length() - 1, partToMap(i)); |
---|
495 | } |
---|
496 | for (i = 0; j = (Joint *)joints(i); i++) |
---|
497 | { |
---|
498 | jointparam.select(j); |
---|
499 | len = gen.length(); |
---|
500 | jointparam.setParamTab(j->usedelta ? f0_joint_paramtab : f0_nodeltajoint_paramtab); |
---|
501 | gen += "j:"; |
---|
502 | jointparam.saveSingleLine(gen, handle_defaults ? &defaultjoint : NULL, true, !handle_defaults); |
---|
503 | if (map) |
---|
504 | map->add(len, gen.length() - 1, jointToMap(i)); |
---|
505 | } |
---|
506 | for (i = 0; n = (Neuro *)neurons(i); i++) |
---|
507 | { |
---|
508 | neuroparam.select(n); |
---|
509 | len = gen.length(); |
---|
510 | gen += "n:"; |
---|
511 | neuroparam.saveSingleLine(gen, handle_defaults ? &defaultneuro : NULL, true, !handle_defaults); |
---|
512 | if (map) |
---|
513 | map->add(len, gen.length() - 1, neuroToMap(i)); |
---|
514 | } |
---|
515 | for (a = 0; a < neurons.size(); a++) |
---|
516 | { // inputs |
---|
517 | n = (Neuro *)neurons(a); |
---|
518 | // if ((n->getInputCount()==1)&&(n->getInput(0).refno <= n->refno)) |
---|
519 | // continue; // already done with Neuro::conn_refno |
---|
520 | |
---|
521 | for (b = 0; b < n->getInputCount(); b++) |
---|
522 | { |
---|
523 | double w; |
---|
524 | NeuroConn nc; |
---|
525 | Neuro *n2 = n->getInput(b, w); |
---|
526 | // if (((n2.parentcount==1)&&(n2.parent)&&(n2.parent->refno < n2.refno)) ^ |
---|
527 | // (n2.neuro_refno>=0)) |
---|
528 | // printf("!!!! bad Neuro::neuro_refno ?!\n"); |
---|
529 | |
---|
530 | // if ((n2.parentcount==1)&&(n2.parent)&&(n2.parent->refno < n2.refno)) |
---|
531 | // if (n2.neuro_refno>=0) |
---|
532 | // continue; // already done with Neuro::neuro_refno |
---|
533 | |
---|
534 | nc.n1_refno = n->refno; nc.n2_refno = n2->refno; |
---|
535 | nc.weight = w; |
---|
536 | SString **s = n->inputInfo(b); |
---|
537 | if ((s) && (*s)) |
---|
538 | nc.info = **s; |
---|
539 | connparam.select(&nc); |
---|
540 | len = gen.length(); |
---|
541 | gen += "c:"; |
---|
542 | connparam.saveSingleLine(gen, handle_defaults ? &defaultconn : NULL, true, !handle_defaults); |
---|
543 | if (map) |
---|
544 | map->add(len, gen.length() - 1, neuroToMap(n->refno)); |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | for (vector<Model *>::const_iterator it = checkpoints.begin(); it != checkpoints.end(); it++) |
---|
549 | { |
---|
550 | Geno g = (*it)->getF0Geno(); |
---|
551 | gen += F0_CHECKPOINT_LINE "\n"; |
---|
552 | gen += g.getGenes(); |
---|
553 | } |
---|
554 | |
---|
555 | g = Geno(gen.c_str(), genoFormatForShapeType(getShapeType()), "", ""); |
---|
556 | } |
---|
557 | |
---|
558 | SString Model::genoFormatForShapeType(ShapeType st) |
---|
559 | { |
---|
560 | switch (st) |
---|
561 | { |
---|
562 | case SHAPETYPE_BALL_AND_STICK: |
---|
563 | return "0"; |
---|
564 | case SHAPETYPE_SOLIDS: |
---|
565 | return "0s"; |
---|
566 | default: |
---|
567 | return Geno::FORMAT_INVALID; |
---|
568 | } |
---|
569 | } |
---|
570 | |
---|
571 | Model::ShapeType Model::shapeTypeForGenoFormat(const SString& format) |
---|
572 | { |
---|
573 | if (format == "0") |
---|
574 | return SHAPETYPE_BALL_AND_STICK; |
---|
575 | else if (format == "0s") |
---|
576 | return SHAPETYPE_SOLIDS; |
---|
577 | else |
---|
578 | return SHAPETYPE_UNKNOWN; |
---|
579 | |
---|
580 | } |
---|
581 | |
---|
582 | const char* Model::getShapeTypeName(ShapeType sh) |
---|
583 | { |
---|
584 | switch (sh) |
---|
585 | { |
---|
586 | case SHAPETYPE_BALL_AND_STICK: return "ball-and-stick"; |
---|
587 | case SHAPETYPE_SOLIDS: return "solid shapes"; |
---|
588 | case SHAPETYPE_UNKNOWN: return "unknown"; |
---|
589 | |
---|
590 | case SHAPETYPE_ILLEGAL: |
---|
591 | default: |
---|
592 | return "illegal"; |
---|
593 | } |
---|
594 | } |
---|
595 | |
---|
596 | ////////////// |
---|
597 | |
---|
598 | void Model::open(bool _using_checkpoints, bool _is_checkpoint) |
---|
599 | { |
---|
600 | if (buildstatus == building) return; |
---|
601 | using_checkpoints = _using_checkpoints; |
---|
602 | is_checkpoint = _is_checkpoint; |
---|
603 | buildstatus = building; |
---|
604 | modelfromgenotype = 0; |
---|
605 | partmappingchanged = 0; |
---|
606 | f0genoknown = 0; |
---|
607 | delMap(); |
---|
608 | } |
---|
609 | |
---|
610 | int Model::getCheckpointCount() |
---|
611 | { |
---|
612 | return checkpoints.size(); |
---|
613 | } |
---|
614 | |
---|
615 | Model *Model::getCheckpoint(int i) |
---|
616 | { |
---|
617 | return checkpoints[i]; |
---|
618 | } |
---|
619 | |
---|
620 | void Model::checkpoint() |
---|
621 | { |
---|
622 | if (!using_checkpoints) return; |
---|
623 | updateRefno(); |
---|
624 | Model *m = new Model(*this, false, false, true); |
---|
625 | checkpoints.push_back(m); |
---|
626 | } |
---|
627 | |
---|
628 | void Model::setGeno(const Geno &newgeno) |
---|
629 | { |
---|
630 | geno = newgeno; |
---|
631 | } |
---|
632 | |
---|
633 | void Model::clearMap() |
---|
634 | { |
---|
635 | Part *p; Joint *j; Neuro *n; |
---|
636 | int i; |
---|
637 | delMap(); |
---|
638 | delF0Map(); |
---|
639 | for (i = 0; p = (Part *)parts(i); i++) |
---|
640 | p->clearMapping(); |
---|
641 | for (i = 0; j = (Joint *)joints(i); i++) |
---|
642 | j->clearMapping(); |
---|
643 | for (i = 0; n = (Neuro *)neurons(i); i++) |
---|
644 | n->clearMapping(); |
---|
645 | } |
---|
646 | |
---|
647 | int Model::close(bool building_live_model) |
---|
648 | { |
---|
649 | if (buildstatus != building) |
---|
650 | logPrintf("Model", "close", LOG_WARN, "Unexpected close() - no open()"); |
---|
651 | if (internalcheck(is_checkpoint ? CHECKPOINT_CHECK : (building_live_model ? LIVE_CHECK : FINAL_CHECK)) > 0) |
---|
652 | { |
---|
653 | buildstatus = valid; |
---|
654 | |
---|
655 | if (partmappingchanged) |
---|
656 | { |
---|
657 | getMap(); |
---|
658 | Part *p; Joint *j; Neuro *n; |
---|
659 | int i; |
---|
660 | for (i = 0; p = (Part *)parts(i); i++) |
---|
661 | if (p->getMapping()) |
---|
662 | map->add(*p->getMapping(), partToMap(i)); |
---|
663 | for (i = 0; j = (Joint *)joints(i); i++) |
---|
664 | if (j->getMapping()) |
---|
665 | map->add(*j->getMapping(), jointToMap(i)); |
---|
666 | for (i = 0; n = (Neuro *)neurons(i); i++) |
---|
667 | if (n->getMapping()) |
---|
668 | map->add(*n->getMapping(), neuroToMap(i)); |
---|
669 | } |
---|
670 | } |
---|
671 | else |
---|
672 | buildstatus = invalid; |
---|
673 | |
---|
674 | return (buildstatus == valid); |
---|
675 | } |
---|
676 | |
---|
677 | int Model::validate() |
---|
678 | { |
---|
679 | return internalcheck(EDITING_CHECK); |
---|
680 | } |
---|
681 | |
---|
682 | Pt3D Model::whereDelta(const Part &start, const Pt3D &rot, const Pt3D &delta) |
---|
683 | { |
---|
684 | Orient roto; |
---|
685 | roto = rot; |
---|
686 | Orient o; |
---|
687 | roto.transform(o, start.o); |
---|
688 | //o.x=start.o/roto.x; |
---|
689 | //o.y=start.o/roto.y; |
---|
690 | //o.z=start.o/roto.z; |
---|
691 | return o.transform(delta) + start.p; |
---|
692 | } |
---|
693 | |
---|
694 | int Model::addFromString(ItemType item_type, const SString &singleline, const MultiRange *srcrange) |
---|
695 | { |
---|
696 | return addFromString(item_type, singleline, 0, srcrange); |
---|
697 | } |
---|
698 | |
---|
699 | int Model::addFromString(ItemType item_type, const SString &singleline, int line_num, const MultiRange *srcrange) |
---|
700 | { |
---|
701 | SString error_message; |
---|
702 | int result = addFromStringNoLog(item_type, singleline, error_message, srcrange); |
---|
703 | if (result < 0) |
---|
704 | { |
---|
705 | if (error_message.length() == 0) // generic error when no detailed message is available |
---|
706 | error_message = "Invalid f0 code"; |
---|
707 | if (line_num > 0) |
---|
708 | error_message += SString::sprintf(", line #%d", line_num); |
---|
709 | error_message += nameForErrors(); |
---|
710 | logPrintf("Model", "build", LOG_ERROR, "%s", error_message.c_str()); |
---|
711 | } |
---|
712 | return result; |
---|
713 | } |
---|
714 | |
---|
715 | int Model::addFromStringNoLog(ItemType item_type, const SString &line, SString &error_message, const MultiRange *srcrange) |
---|
716 | { |
---|
717 | error_message = SString::empty(); |
---|
718 | ParamInterface::LoadOptions opts; |
---|
719 | switch (item_type) |
---|
720 | { |
---|
721 | case PartType: |
---|
722 | { |
---|
723 | Param partparam(f0_part_paramtab); |
---|
724 | Part *p = new Part(); |
---|
725 | partparam.select(p); |
---|
726 | partparam.load(ParamInterface::FormatSingleLine, line, &opts); |
---|
727 | if (opts.parse_failed) { delete p; error_message = "Invalid 'p:'"; return -1; } |
---|
728 | p->o.rotate(p->rot); |
---|
729 | parts += p; |
---|
730 | p->owner = this; |
---|
731 | if (srcrange) p->setMapping(*srcrange); |
---|
732 | return getPartCount() - 1; |
---|
733 | } |
---|
734 | |
---|
735 | case ModelType: |
---|
736 | { |
---|
737 | Param modelparam(f0_model_paramtab); |
---|
738 | modelparam.select(this); |
---|
739 | modelparam.load(ParamInterface::FormatSingleLine, line, &opts); |
---|
740 | if (opts.parse_failed) { error_message = "Invalid 'm:'"; return -1; } |
---|
741 | return 0; |
---|
742 | } |
---|
743 | |
---|
744 | case JointType: |
---|
745 | { |
---|
746 | Param jointparam(f0_joint_paramtab); |
---|
747 | Joint *j = new Joint(); |
---|
748 | jointparam.select(j); |
---|
749 | j->owner = this; |
---|
750 | jointparam.load(ParamInterface::FormatSingleLine, line, &opts); |
---|
751 | if (opts.parse_failed) { delete j; error_message = "Invalid 'j:'"; return -1; } |
---|
752 | bool p1_ok = false, p2_ok = false; |
---|
753 | if ((p1_ok = ((j->p1_refno >= 0) && (j->p1_refno < getPartCount()))) && |
---|
754 | (p2_ok = ((j->p2_refno >= 0) && (j->p2_refno < getPartCount())))) |
---|
755 | { |
---|
756 | addJoint(j); |
---|
757 | if ((j->d.x != JOINT_DELTA_MARKER) || (j->d.y != JOINT_DELTA_MARKER) || (j->d.z != JOINT_DELTA_MARKER)) |
---|
758 | { |
---|
759 | j->useDelta(1); |
---|
760 | j->resetDeltaMarkers(); |
---|
761 | } |
---|
762 | j->attachToParts(j->p1_refno, j->p2_refno); |
---|
763 | if (srcrange) j->setMapping(*srcrange); |
---|
764 | return j->refno; |
---|
765 | } |
---|
766 | else |
---|
767 | { |
---|
768 | error_message = SString::sprintf("Invalid reference to Part #%d", p1_ok ? j->p1_refno : j->p2_refno); |
---|
769 | delete j; |
---|
770 | return -1; |
---|
771 | } |
---|
772 | } |
---|
773 | |
---|
774 | case NeuronType: |
---|
775 | { |
---|
776 | Param neuroparam(f0_neuro_paramtab); |
---|
777 | Neuro *nu = new Neuro(); |
---|
778 | neuroparam.select(nu); |
---|
779 | neuroparam.load(ParamInterface::FormatSingleLine, line, &opts); |
---|
780 | if (opts.parse_failed) { delete nu; error_message = "Invalid 'n:'"; return -1; } |
---|
781 | { |
---|
782 | // default class for unparented units: standard neuron |
---|
783 | if (nu->getClassName().length() == 0) nu->setClassName("N"); |
---|
784 | } |
---|
785 | /* |
---|
786 | if (nu->conn_refno>=0) // input specified... |
---|
787 | { |
---|
788 | if (nu->conn_refno >= getNeuroCount()) // and it's illegal |
---|
789 | { |
---|
790 | delete nu; |
---|
791 | return -1; |
---|
792 | } |
---|
793 | Neuro *inputNU=getNeuro(nu->conn_refno); |
---|
794 | nu->addInput(inputNU,nu->weight); |
---|
795 | } |
---|
796 | */ |
---|
797 | nu->owner = this; |
---|
798 | // attach to part/joint |
---|
799 | if (nu->part_refno >= 0) |
---|
800 | { |
---|
801 | nu->attachToPart(nu->part_refno); |
---|
802 | if (nu->part == NULL) |
---|
803 | { |
---|
804 | error_message = SString::sprintf("Invalid reference to Part #%d", nu->part_refno); delete nu; return -1; |
---|
805 | } |
---|
806 | } |
---|
807 | if (nu->joint_refno >= 0) |
---|
808 | { |
---|
809 | nu->attachToJoint(nu->joint_refno); |
---|
810 | if (nu->joint == NULL) |
---|
811 | { |
---|
812 | error_message = SString::sprintf("Invalid reference to Joint #%d", nu->joint_refno); delete nu; return -1; |
---|
813 | } |
---|
814 | } |
---|
815 | if (srcrange) nu->setMapping(*srcrange); |
---|
816 | // todo: check part/joint ref# |
---|
817 | { |
---|
818 | neurons += nu; |
---|
819 | return neurons.size() - 1; |
---|
820 | } |
---|
821 | } |
---|
822 | |
---|
823 | case NeuronConnectionType: |
---|
824 | { |
---|
825 | Param ncparam(f0_neuroconn_paramtab); |
---|
826 | NeuroConn c; |
---|
827 | ncparam.select(&c); |
---|
828 | ncparam.load(ParamInterface::FormatSingleLine, line, &opts); |
---|
829 | if (opts.parse_failed) { error_message = "Invalid 'c:'"; return -1; } |
---|
830 | bool n1_ok = false, n2_ok = false; |
---|
831 | if ((n1_ok = ((c.n1_refno >= 0) && (c.n1_refno < getNeuroCount()))) |
---|
832 | && (n2_ok = ((c.n2_refno >= 0) && (c.n2_refno < getNeuroCount())))) |
---|
833 | { |
---|
834 | Neuro *na = getNeuro(c.n1_refno); |
---|
835 | Neuro *nb = getNeuro(c.n2_refno); |
---|
836 | na->addInput(nb, c.weight, &c.info); |
---|
837 | if (srcrange) |
---|
838 | na->addMapping(*srcrange); |
---|
839 | return 0; |
---|
840 | } |
---|
841 | error_message = SString::sprintf("Invalid reference to Neuro #%d", n1_ok ? c.n2_refno : c.n1_refno); |
---|
842 | return -1; |
---|
843 | } |
---|
844 | |
---|
845 | case CheckpointType: case UnknownType: //handled by addFromString for uniform error handling |
---|
846 | return -1; |
---|
847 | } |
---|
848 | return -1; |
---|
849 | } |
---|
850 | |
---|
851 | |
---|
852 | ///////////// |
---|
853 | |
---|
854 | void Model::updateRefno() |
---|
855 | { |
---|
856 | for (int i = 0; i < parts.size(); i++) |
---|
857 | getPart(i)->refno = i; |
---|
858 | for (int i = 0; i < joints.size(); i++) |
---|
859 | { |
---|
860 | Joint *j = getJoint(i); |
---|
861 | j->refno = i; |
---|
862 | if (j->part1 && j->part2 && (j->part1 != j->part2)) |
---|
863 | { |
---|
864 | j->p1_refno = j->part1->refno; |
---|
865 | j->p2_refno = j->part2->refno; |
---|
866 | } |
---|
867 | } |
---|
868 | for (int i = 0; i < neurons.size(); i++) |
---|
869 | getNeuro(i)->refno = i; |
---|
870 | } |
---|
871 | |
---|
872 | #define VALIDMINMAX(var,template,field) \ |
---|
873 | if (var -> field < getMin ## template () . field) \ |
---|
874 | { var->field= getMin ## template () . field; \ |
---|
875 | logPrintf("Model","internalCheck",LOG_WARN,# field " too small in " # template " #%d (adjusted)",i);} \ |
---|
876 | else if (var -> field > getMax ## template () . field) \ |
---|
877 | { var->field= getMax ## template () . field; \ |
---|
878 | logPrintf("Model","internalCheck",LOG_WARN,# field " too big in " # template " #%d (adjusted)",i);} |
---|
879 | |
---|
880 | #define LINKFLAG 0x8000000 |
---|
881 | |
---|
882 | SString Model::nameForErrors() const |
---|
883 | { |
---|
884 | if (geno.getName().length() > 0) |
---|
885 | return SString::sprintf(" in '%s'", geno.getName().c_str()); |
---|
886 | return SString::empty(); |
---|
887 | } |
---|
888 | |
---|
889 | int Model::internalcheck(CheckType check) |
---|
890 | { |
---|
891 | Part *p; |
---|
892 | Joint *j; |
---|
893 | Neuro *n; |
---|
894 | int i, k; |
---|
895 | int ret = 1; |
---|
896 | shapetype = SHAPETYPE_UNKNOWN; |
---|
897 | updateRefno(); |
---|
898 | if ((parts.size() == 0) && (neurons.size() == 0)) return 0; |
---|
899 | if (parts.size() == 0) |
---|
900 | size = Pt3D_0; |
---|
901 | else |
---|
902 | { |
---|
903 | Pt3D bbmin = ((Part *)parts(0))->p, bbmax = bbmin; |
---|
904 | for (i = 0; i < parts.size(); i++) |
---|
905 | { |
---|
906 | p = (Part *)parts(i); |
---|
907 | p->owner = this; |
---|
908 | if (checklevel > 0) |
---|
909 | p->mass = 0.0; |
---|
910 | //VALIDMINMAX(p,part,mass);//mass is very special |
---|
911 | // VALIDMINMAX are managed manually when adding part properties in f0-def! |
---|
912 | // (could be made dynamic but not really worth the effort) |
---|
913 | VALIDMINMAX(p, Part, size); |
---|
914 | VALIDMINMAX(p, Part, scale.x); |
---|
915 | VALIDMINMAX(p, Part, scale.y); |
---|
916 | VALIDMINMAX(p, Part, scale.z); |
---|
917 | VALIDMINMAX(p, Part, hollow); |
---|
918 | VALIDMINMAX(p, Part, density); |
---|
919 | VALIDMINMAX(p, Part, friction); |
---|
920 | VALIDMINMAX(p, Part, ingest); |
---|
921 | VALIDMINMAX(p, Part, assim); |
---|
922 | VALIDMINMAX(p, Part, vsize); |
---|
923 | VALIDMINMAX(p, Part, vcolor.x); |
---|
924 | VALIDMINMAX(p, Part, vcolor.y); |
---|
925 | VALIDMINMAX(p, Part, vcolor.z); |
---|
926 | p->flags &= ~LINKFLAG; // for delta joint cycle detection |
---|
927 | if (p->p.x - p->size < bbmin.x) bbmin.x = p->p.x - p->size; |
---|
928 | if (p->p.y - p->size < bbmin.y) bbmin.y = p->p.y - p->size; |
---|
929 | if (p->p.z - p->size < bbmin.z) bbmin.z = p->p.z - p->size; |
---|
930 | if (p->p.x + p->size > bbmax.x) bbmax.x = p->p.x + p->size; |
---|
931 | if (p->p.y + p->size > bbmax.y) bbmax.y = p->p.y + p->size; |
---|
932 | if (p->p.z + p->size > bbmax.z) bbmax.z = p->p.z + p->size; |
---|
933 | if (shapetype == SHAPETYPE_UNKNOWN) |
---|
934 | shapetype = (p->shape == Part::SHAPE_BALL) ? SHAPETYPE_BALL_AND_STICK : SHAPETYPE_SOLIDS; |
---|
935 | else if (shapetype != SHAPETYPE_ILLEGAL) |
---|
936 | { |
---|
937 | if ((p->shape == Part::SHAPE_BALL) ^ (shapetype == SHAPETYPE_BALL_AND_STICK)) |
---|
938 | { |
---|
939 | shapetype = SHAPETYPE_ILLEGAL; |
---|
940 | logPrintf("Model", "internalCheck", LOG_WARN, "Inconsistent part shapes (mixed ball-and-stick and solids shape types)%s", nameForErrors().c_str()); |
---|
941 | } |
---|
942 | } |
---|
943 | } |
---|
944 | size = bbmax - bbmin; |
---|
945 | for (i = 0; i < joints.size(); i++) |
---|
946 | { |
---|
947 | j = (Joint *)joints(i); |
---|
948 | // VALIDMINMAX are managed manually when adding joint properties in f0-def! |
---|
949 | // (could be made dynamic but not really worth the effort) |
---|
950 | VALIDMINMAX(j, Joint, stamina); |
---|
951 | VALIDMINMAX(j, Joint, stif); |
---|
952 | VALIDMINMAX(j, Joint, rotstif); |
---|
953 | VALIDMINMAX(p, Part, vcolor.x); |
---|
954 | VALIDMINMAX(p, Part, vcolor.y); |
---|
955 | VALIDMINMAX(p, Part, vcolor.z); |
---|
956 | j->refno = i; |
---|
957 | j->owner = this; |
---|
958 | if (j->part1 && j->part2 && (j->part1 != j->part2)) |
---|
959 | { |
---|
960 | j->p1_refno = j->part1->refno; |
---|
961 | j->p2_refno = j->part2->refno; |
---|
962 | if (checklevel > 0) |
---|
963 | { |
---|
964 | j->part1->mass += 1.0; |
---|
965 | j->part2->mass += 1.0; |
---|
966 | } |
---|
967 | if ((j->usedelta) && ((j->d.x != JOINT_DELTA_MARKER) || (j->d.y != JOINT_DELTA_MARKER) || (j->d.z != JOINT_DELTA_MARKER))) |
---|
968 | { // delta positioning -> calc. orient. |
---|
969 | if (j->part2->flags & LINKFLAG) |
---|
970 | { |
---|
971 | ret = 0; |
---|
972 | logPrintf("Model", "internalCheck", LOG_ERROR, |
---|
973 | "Delta joint cycle detected at Joint #%d%s", |
---|
974 | i, nameForErrors().c_str()); |
---|
975 | } |
---|
976 | j->resetDeltaMarkers(); |
---|
977 | j->o = j->rot; |
---|
978 | j->part1->o.transform(j->part2->o, j->o); |
---|
979 | // j->part2->o.x=j->part1->o/j->o.x; |
---|
980 | // j->part2->o.y=j->part1->o/j->o.y; |
---|
981 | // j->part2->o.z=j->part1->o/j->o.z; |
---|
982 | j->part2->p = j->part2->o.transform(j->d) + j->part1->p; |
---|
983 | j->part2->flags |= LINKFLAG; j->part1->flags |= LINKFLAG; // for delta joint cycle detection |
---|
984 | } |
---|
985 | else |
---|
986 | { // abs.positioning -> calc. delta |
---|
987 | if (check != EDITING_CHECK) |
---|
988 | { |
---|
989 | // calc orient delta |
---|
990 | // Orient tmpo(j->part2->o); |
---|
991 | // tmpo*=j->part1->o; |
---|
992 | Orient tmpo; |
---|
993 | j->part1->o.revTransform(tmpo, j->part2->o); |
---|
994 | tmpo.getAngles(j->rot); |
---|
995 | j->o = j->rot; |
---|
996 | // calc position delta |
---|
997 | Pt3D tmpp(j->part2->p); |
---|
998 | tmpp -= j->part1->p; |
---|
999 | j->d = j->part2->o.revTransform(tmpp); |
---|
1000 | } |
---|
1001 | } |
---|
1002 | if ((check != LIVE_CHECK) && (check != CHECKPOINT_CHECK)) |
---|
1003 | { |
---|
1004 | if (j->shape == Joint::SHAPE_STICK) |
---|
1005 | { |
---|
1006 | if (j->d() > getMaxJoint().d.x) |
---|
1007 | { |
---|
1008 | ret = 0; |
---|
1009 | logPrintf("Model", "internalCheck", LOG_ERROR, "Joint #%d too long (its length %g exceeds allowed %g)%s", i, j->d(), getMaxJoint().d.x, nameForErrors().c_str()); |
---|
1010 | } |
---|
1011 | } |
---|
1012 | } |
---|
1013 | } |
---|
1014 | else |
---|
1015 | { |
---|
1016 | logPrintf("Model", "internalCheck", LOG_ERROR, "Illegal part references in Joint #%d%s", i, nameForErrors().c_str()); |
---|
1017 | ret = 0; |
---|
1018 | } |
---|
1019 | if (shapetype != SHAPETYPE_ILLEGAL) |
---|
1020 | { |
---|
1021 | if ((j->shape == Joint::SHAPE_STICK) ^ (shapetype == SHAPETYPE_BALL_AND_STICK)) |
---|
1022 | { |
---|
1023 | shapetype = SHAPETYPE_ILLEGAL; |
---|
1024 | logPrintf("Model", "internalCheck", LOG_WARN, "Inconsistent joint shapes (mixed old and new shapes)%s", nameForErrors().c_str()); |
---|
1025 | } |
---|
1026 | } |
---|
1027 | } |
---|
1028 | } |
---|
1029 | |
---|
1030 | for (i = 0; i < neurons.size(); i++) |
---|
1031 | { |
---|
1032 | n = (Neuro *)neurons(i); |
---|
1033 | n->part_refno = (n->part) ? n->part->refno : -1; |
---|
1034 | n->joint_refno = (n->joint) ? n->joint->refno : -1; |
---|
1035 | } |
---|
1036 | |
---|
1037 | if (check != CHECKPOINT_CHECK) |
---|
1038 | { |
---|
1039 | |
---|
1040 | if (parts.size() && (checklevel > 0)) |
---|
1041 | { |
---|
1042 | for (i = 0; i < parts.size(); i++) |
---|
1043 | { |
---|
1044 | p = (Part *)parts(i); |
---|
1045 | if (p->mass <= 0.001) |
---|
1046 | p->mass = 1.0; |
---|
1047 | p->flags &= ~LINKFLAG; |
---|
1048 | } |
---|
1049 | getPart(0)->flags |= LINKFLAG; |
---|
1050 | int change = 1; |
---|
1051 | while (change) |
---|
1052 | { |
---|
1053 | change = 0; |
---|
1054 | for (i = 0; i < joints.size(); i++) |
---|
1055 | { |
---|
1056 | j = (Joint *)joints(i); |
---|
1057 | if (j->part1->flags & LINKFLAG) |
---|
1058 | { |
---|
1059 | if (!(j->part2->flags & LINKFLAG)) |
---|
1060 | { |
---|
1061 | change = 1; |
---|
1062 | j->part2->flags |= LINKFLAG; |
---|
1063 | } |
---|
1064 | } |
---|
1065 | else |
---|
1066 | if (j->part2->flags & LINKFLAG) |
---|
1067 | { |
---|
1068 | if (!(j->part1->flags & LINKFLAG)) |
---|
1069 | { |
---|
1070 | change = 1; |
---|
1071 | j->part1->flags |= LINKFLAG; |
---|
1072 | } |
---|
1073 | } |
---|
1074 | } |
---|
1075 | } |
---|
1076 | for (i = 0; i < parts.size(); i++) |
---|
1077 | { |
---|
1078 | p = (Part *)parts(i); |
---|
1079 | if (!(p->flags & LINKFLAG)) |
---|
1080 | { |
---|
1081 | logPrintf("Model", "internalCheck", LOG_ERROR, "Not all parts connected (eg. Part #0 and Part #%d)%s", i, nameForErrors().c_str()); |
---|
1082 | ret = 0; |
---|
1083 | break; |
---|
1084 | } |
---|
1085 | } |
---|
1086 | } |
---|
1087 | |
---|
1088 | for (i = 0; i < joints.size(); i++) |
---|
1089 | { |
---|
1090 | j = (Joint *)joints(i); |
---|
1091 | if (j->p1_refno == j->p2_refno) |
---|
1092 | { |
---|
1093 | logPrintf("Model", "internalCheck", LOG_ERROR, "Illegal self connection, Joint #%d%s", i, nameForErrors().c_str()); |
---|
1094 | ret = 0; |
---|
1095 | break; |
---|
1096 | } |
---|
1097 | for (k = i + 1; k < joints.size(); k++) |
---|
1098 | { |
---|
1099 | Joint *j2 = (Joint *)joints(k); |
---|
1100 | if (((j->p1_refno == j2->p1_refno) && (j->p2_refno == j2->p2_refno)) |
---|
1101 | || ((j->p1_refno == j2->p2_refno) && (j->p2_refno == j2->p1_refno))) |
---|
1102 | { |
---|
1103 | logPrintf("Model", "internalCheck", LOG_ERROR, "Illegal duplicate Joint #%d and Joint #%d%s", i, k, nameForErrors().c_str()); |
---|
1104 | ret = 0; |
---|
1105 | break; |
---|
1106 | } |
---|
1107 | } |
---|
1108 | } |
---|
1109 | } |
---|
1110 | |
---|
1111 | if (shapetype == SHAPETYPE_ILLEGAL) |
---|
1112 | ret = 0; |
---|
1113 | else if ((declared_shapetype != SHAPETYPE_UNKNOWN) && (declared_shapetype != shapetype)) |
---|
1114 | { |
---|
1115 | logPrintf("Model", "internalCheck", LOG_ERROR, "Model shape type '%s' does not match the declared type '%s'", getShapeTypeName(shapetype), getShapeTypeName(declared_shapetype)); |
---|
1116 | ret = 0; |
---|
1117 | } |
---|
1118 | |
---|
1119 | return ret; |
---|
1120 | } |
---|
1121 | |
---|
1122 | ///////////// |
---|
1123 | |
---|
1124 | int Model::getErrorPosition(bool includingwarnings) |
---|
1125 | { |
---|
1126 | return includingwarnings ? |
---|
1127 | ((f0errorposition >= 0) ? f0errorposition : f0warnposition) |
---|
1128 | : |
---|
1129 | f0errorposition; |
---|
1130 | } |
---|
1131 | |
---|
1132 | const Geno &Model::getGeno() const |
---|
1133 | { |
---|
1134 | return geno; |
---|
1135 | } |
---|
1136 | |
---|
1137 | const Geno Model::getF0Geno() |
---|
1138 | { |
---|
1139 | if (buildstatus == building) |
---|
1140 | logPrintf("Model", "getGeno", LOG_WARN, "Model was not completed - missing close()"); |
---|
1141 | if (buildstatus != valid) |
---|
1142 | return Geno("", '0', "", "invalid"); |
---|
1143 | if (!f0genoknown) |
---|
1144 | { |
---|
1145 | if (autobuildmaps) |
---|
1146 | { |
---|
1147 | initF0Map(); |
---|
1148 | makeGeno(f0geno, f0map); |
---|
1149 | } |
---|
1150 | else |
---|
1151 | { |
---|
1152 | delF0Map(); |
---|
1153 | makeGeno(f0geno); |
---|
1154 | } |
---|
1155 | f0genoknown = 1; |
---|
1156 | } |
---|
1157 | return f0geno; |
---|
1158 | } |
---|
1159 | |
---|
1160 | int Model::getPartCount() const |
---|
1161 | { |
---|
1162 | return parts.size(); |
---|
1163 | } |
---|
1164 | |
---|
1165 | Part *Model::getPart(int i) const |
---|
1166 | { |
---|
1167 | return ((Part *)parts(i)); |
---|
1168 | } |
---|
1169 | |
---|
1170 | int Model::getJointCount() const |
---|
1171 | { |
---|
1172 | return joints.size(); |
---|
1173 | } |
---|
1174 | |
---|
1175 | Joint *Model::getJoint(int i) const |
---|
1176 | { |
---|
1177 | return ((Joint *)joints(i)); |
---|
1178 | } |
---|
1179 | |
---|
1180 | int Model::findJoints(SList &result, const Part *part) |
---|
1181 | { |
---|
1182 | Joint *j; |
---|
1183 | int n0 = result.size(); |
---|
1184 | if (part) |
---|
1185 | for (int i = 0; j = (Joint *)joints(i); i++) |
---|
1186 | if ((j->part1 == part) || (j->part2 == part)) result += (void *)j; |
---|
1187 | return result.size() - n0; |
---|
1188 | } |
---|
1189 | |
---|
1190 | int Model::findNeuro(Neuro *n) |
---|
1191 | { |
---|
1192 | return neurons.find(n); |
---|
1193 | } |
---|
1194 | |
---|
1195 | int Model::findPart(Part *p) |
---|
1196 | { |
---|
1197 | return parts.find(p); |
---|
1198 | } |
---|
1199 | |
---|
1200 | int Model::findJoint(Joint *j) |
---|
1201 | { |
---|
1202 | return joints.find(j); |
---|
1203 | } |
---|
1204 | |
---|
1205 | int Model::findJoint(Part *p1, Part *p2) |
---|
1206 | { |
---|
1207 | Joint *j; |
---|
1208 | for (int i = 0; j = getJoint(i); i++) |
---|
1209 | if ((j->part1 == p1) && (j->part2 == p2)) return i; |
---|
1210 | return -1; |
---|
1211 | } |
---|
1212 | |
---|
1213 | /////////////////////// |
---|
1214 | |
---|
1215 | int Model::getNeuroCount() const |
---|
1216 | { |
---|
1217 | return neurons.size(); |
---|
1218 | } |
---|
1219 | |
---|
1220 | Neuro *Model::getNeuro(int i) const |
---|
1221 | { |
---|
1222 | return (Neuro *)neurons(i); |
---|
1223 | } |
---|
1224 | |
---|
1225 | int Model::getConnectionCount() const |
---|
1226 | { |
---|
1227 | int n = 0; |
---|
1228 | for (int i = 0; i < getNeuroCount(); i++) |
---|
1229 | n += getNeuro(i)->getInputCount(); |
---|
1230 | return n; |
---|
1231 | } |
---|
1232 | |
---|
1233 | int Model::findNeuros(SList &result, |
---|
1234 | const char *classname, const Part *part, const Joint *joint) |
---|
1235 | { |
---|
1236 | Neuro *nu; |
---|
1237 | SString cn(classname); |
---|
1238 | int n0 = result.size(); |
---|
1239 | for (int i = 0; nu = (Neuro *)neurons(i); i++) |
---|
1240 | { |
---|
1241 | if (part) |
---|
1242 | if (nu->part != part) continue; |
---|
1243 | if (joint) |
---|
1244 | if (nu->joint != joint) continue; |
---|
1245 | if (classname) |
---|
1246 | if (nu->getClassName() != cn) continue; |
---|
1247 | result += (void *)nu; |
---|
1248 | } |
---|
1249 | return result.size() - n0; |
---|
1250 | } |
---|
1251 | |
---|
1252 | /////////////////// |
---|
1253 | |
---|
1254 | void Model::disturb(double amount) |
---|
1255 | { |
---|
1256 | int i; |
---|
1257 | if (amount <= 0) return; |
---|
1258 | for (i = 0; i < parts.size(); i++) |
---|
1259 | { |
---|
1260 | Part *p = getPart(i); |
---|
1261 | p->p.x += (rndDouble(1) - 0.5) * amount; |
---|
1262 | p->p.y += (rndDouble(1) - 0.5) * amount; |
---|
1263 | p->p.z += (rndDouble(1) - 0.5) * amount; |
---|
1264 | } |
---|
1265 | for (i = 0; i < joints.size(); i++) |
---|
1266 | { |
---|
1267 | Joint *j = getJoint(i); |
---|
1268 | Pt3D tmpp(j->part2->p); |
---|
1269 | tmpp -= j->part1->p; |
---|
1270 | j->d = j->part2->o.revTransform(tmpp); |
---|
1271 | } |
---|
1272 | } |
---|
1273 | |
---|
1274 | void Model::move(const Pt3D &shift) |
---|
1275 | { |
---|
1276 | FOREACH(Part *, p, parts) |
---|
1277 | p->p += shift; |
---|
1278 | } |
---|
1279 | |
---|
1280 | void Model::rotate(const Orient &rotation) |
---|
1281 | { |
---|
1282 | FOREACH(Part *, p, parts) |
---|
1283 | { |
---|
1284 | p->p = rotation.transform(p->p); |
---|
1285 | p->setOrient(rotation.transform(p->o)); |
---|
1286 | } |
---|
1287 | } |
---|
1288 | |
---|
1289 | void Model::buildUsingSolidShapeTypes(const Model &src_ballandstick_shapes, Part::Shape use_shape, double thickness) |
---|
1290 | { |
---|
1291 | for (int i = 0; i < src_ballandstick_shapes.getJointCount(); i++) |
---|
1292 | { |
---|
1293 | Joint *oj = src_ballandstick_shapes.getJoint(i); |
---|
1294 | Part *p = addNewPart(use_shape); |
---|
1295 | p->p = (oj->part1->p + oj->part2->p) / 2; |
---|
1296 | Orient o; |
---|
1297 | o.lookAt(oj->part1->p - oj->part2->p); |
---|
1298 | p->setRot(o.getAngles()); |
---|
1299 | p->scale.x = oj->part1->p.distanceTo(oj->part2->p) / 2; |
---|
1300 | p->scale.y = thickness; |
---|
1301 | p->scale.z = thickness; |
---|
1302 | } |
---|
1303 | if (src_ballandstick_shapes.getJointCount() == 0) //single part "ball-and-stick" models are valid so let's make a valid solid shape model |
---|
1304 | for (int i = 0; i < src_ballandstick_shapes.getPartCount(); i++) |
---|
1305 | { |
---|
1306 | Part *op = src_ballandstick_shapes.getPart(i); |
---|
1307 | Part *p = addNewPart(Part::SHAPE_ELLIPSOID); //always using spherical shape regardless of the 'use_shape' parameter - 'use shape' is meant for sticks! |
---|
1308 | p->p = op->p; |
---|
1309 | p->rot = op->rot; |
---|
1310 | p->scale.x = p->scale.y = p->scale.z = thickness; |
---|
1311 | } |
---|
1312 | for (int i = 0; i < src_ballandstick_shapes.getPartCount(); i++) |
---|
1313 | { |
---|
1314 | Part *op = src_ballandstick_shapes.getPart(i); |
---|
1315 | for (int j = 0; j < src_ballandstick_shapes.getJointCount(); j++) |
---|
1316 | { |
---|
1317 | Joint *oj = src_ballandstick_shapes.getJoint(j); |
---|
1318 | if ((oj->part1 == op) || (oj->part2 == op)) |
---|
1319 | { |
---|
1320 | for (int j2 = j + 1; j2 < src_ballandstick_shapes.getJointCount(); j2++) |
---|
1321 | { |
---|
1322 | Joint *oj2 = src_ballandstick_shapes.getJoint(j2); |
---|
1323 | if ((oj2->part1 == op) || (oj2->part2 == op)) |
---|
1324 | { |
---|
1325 | addNewJoint(getPart(j), getPart(j2), Joint::SHAPE_FIXED); |
---|
1326 | } |
---|
1327 | } |
---|
1328 | break; |
---|
1329 | } |
---|
1330 | } |
---|
1331 | } |
---|
1332 | } |
---|
1333 | |
---|
1334 | SolidsShapeTypeModel::SolidsShapeTypeModel(Model &m, Part::Shape use_shape, double thickness) |
---|
1335 | { |
---|
1336 | using_model = converted_model = NULL; |
---|
1337 | if (m.getShapeType() == Model::SHAPETYPE_BALL_AND_STICK) |
---|
1338 | { |
---|
1339 | converted_model = new Model; |
---|
1340 | converted_model->open(); |
---|
1341 | converted_model->buildUsingSolidShapeTypes(m, use_shape, thickness); |
---|
1342 | converted_model->close(); |
---|
1343 | using_model = converted_model; |
---|
1344 | } |
---|
1345 | else |
---|
1346 | { |
---|
1347 | converted_model = NULL; |
---|
1348 | using_model = &m; |
---|
1349 | } |
---|
1350 | } |
---|
1351 | |
---|
1352 | ////////////////////// |
---|
1353 | |
---|
1354 | int Model::elementToMap(ItemType type, int index) |
---|
1355 | { |
---|
1356 | switch (type) |
---|
1357 | { |
---|
1358 | case PartType: return partToMap(index); |
---|
1359 | case JointType: return jointToMap(index); |
---|
1360 | case NeuronType: return neuroToMap(index); |
---|
1361 | default: return -1; |
---|
1362 | } |
---|
1363 | } |
---|
1364 | |
---|
1365 | Model::TypeAndIndex Model::mapToElement(int map_index) |
---|
1366 | { |
---|
1367 | if ((map_index >= 0) && (map_index < MODEL_MAPPING_OFFSET)) |
---|
1368 | return TypeAndIndex(PartType, mapToPart(map_index)); |
---|
1369 | if ((map_index >= MODEL_MAPPING_OFFSET) && (map_index < 2 * MODEL_MAPPING_OFFSET)) |
---|
1370 | return TypeAndIndex(JointType, mapToJoint(map_index)); |
---|
1371 | if ((map_index >= 2 * MODEL_MAPPING_OFFSET) && (map_index < 3 * MODEL_MAPPING_OFFSET)) |
---|
1372 | return TypeAndIndex(NeuronType, mapToNeuro(map_index)); |
---|
1373 | return TypeAndIndex(); |
---|
1374 | } |
---|
1375 | |
---|
1376 | int Model::partToMap(int i) { return MODEL_MAPPING_OFFSET + i; } |
---|
1377 | int Model::jointToMap(int i) { return 2 * MODEL_MAPPING_OFFSET + i; } |
---|
1378 | int Model::neuroToMap(int i) { return 3 * MODEL_MAPPING_OFFSET + i; } |
---|
1379 | int Model::mapToPart(int i) { return i - MODEL_MAPPING_OFFSET; } |
---|
1380 | int Model::mapToJoint(int i) { return i - 2 * MODEL_MAPPING_OFFSET; } |
---|
1381 | int Model::mapToNeuro(int i) { return i - 3 * MODEL_MAPPING_OFFSET; } |
---|
1382 | |
---|
1383 | |
---|
1384 | ////////////////////// |
---|
1385 | |
---|
1386 | class MinPart : public Part_MinMaxDef { public: MinPart() { Param par(f0_part_paramtab, this); par.setMin(); Param par2(f0_part_minmaxdef_paramtab, this); par2.setMin(); } }; |
---|
1387 | class MaxPart : public Part_MinMaxDef { public: MaxPart() { Param par(f0_part_paramtab, this); par.setMax(); Param par2(f0_part_minmaxdef_paramtab, this); par2.setMax(); } }; |
---|
1388 | class MinJoint : public Joint { public: MinJoint() { Param par(f0_joint_paramtab, this); par.setMin(); } }; |
---|
1389 | class MaxJoint : public Joint { public: MaxJoint() { Param par(f0_joint_paramtab, this); par.setMax(); } }; |
---|
1390 | class MinNeuro : public Neuro { public: MinNeuro() { Param par(f0_neuro_paramtab, this); par.setMin(); } }; |
---|
1391 | class MaxNeuro : public Neuro { public: MaxNeuro() { Param par(f0_neuro_paramtab, this); par.setMax(); } }; |
---|
1392 | |
---|
1393 | Part_MinMaxDef &Model::getMinPart() { static MinPart part; return part; } |
---|
1394 | Part_MinMaxDef &Model::getMaxPart() { static MaxPart part; return part; } |
---|
1395 | Part_MinMaxDef &Model::getDefPart() { static Part_MinMaxDef part; return part; } |
---|
1396 | Joint &Model::getMinJoint() { static MinJoint joint; return joint; } |
---|
1397 | Joint &Model::getMaxJoint() { static MaxJoint joint; return joint; } |
---|
1398 | Joint &Model::getDefJoint() { static Joint joint; return joint; } |
---|
1399 | Neuro &Model::getMinNeuro() { static MinNeuro neuro; return neuro; } |
---|
1400 | Neuro &Model::getMaxNeuro() { static MaxNeuro neuro; return neuro; } |
---|
1401 | Neuro &Model::getDefNeuro() { static Neuro neuro; return neuro; } |
---|