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