Changeset 896 for cpp/frams/model/model.cpp
 Timestamp:
 11/30/19 01:30:22 (3 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

cpp/frams/model/model.cpp
r815 r896 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999201 8Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 19992019 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 83 83 updateRefno(); 84 84 if (using_checkpoints) 85 for (vector<Model *>::const_iterator it = mod.checkpoints.begin(); it != mod.checkpoints.end(); it++)86 { 87 Model *m = *it;88 Model *n = new Model(*m, m>autobuildmaps, false, true);89 checkpoints.push_back(n);85 for (vector<Model *>::const_iterator it = mod.checkpoints.begin(); it != mod.checkpoints.end(); it++) 86 { 87 Model *m = *it; 88 Model *n = new Model(*m, m>autobuildmaps, false, true); 89 checkpoints.push_back(n); 90 90 } 91 91 } … … 152 152 void Model::clear() 153 153 { 154 FOREACH(Part *, p, parts)154 FOREACH(Part *, p, parts) 155 155 delete p; 156 FOREACH(Joint *, j, joints)156 FOREACH(Joint *, j, joints) 157 157 delete j; 158 FOREACH(Neuro *, n, neurons)158 FOREACH(Neuro *, n, neurons) 159 159 delete n; 160 160 parts.clear(); joints.clear(); neurons.clear(); … … 164 164 geno = Geno(); 165 165 f0geno = Geno(); 166 for (vector<Model *>::iterator it = checkpoints.begin(); it != checkpoints.end(); it++)166 for (vector<Model *>::iterator it = checkpoints.begin(); it != checkpoints.end(); it++) 167 167 delete *it; 168 168 checkpoints.clear(); … … 195 195 void Model::removeNeuros(SList &nlist) 196 196 { 197 FOREACH(Neuro *, nu, nlist)197 FOREACH(Neuro *, nu, nlist) 198 198 { 199 199 int i = findNeuro(nu); … … 209 209 SList jlist; 210 210 findJoints(jlist, p); 211 FOREACH(Joint *, j, jlist)211 FOREACH(Joint *, j, jlist) 212 212 { 213 213 int i = findJoint(j); … … 240 240 void Model::removeNeuro(int neuroindex, bool removereferences) 241 241 { 242 Neuro *thisN = getNeuro(neuroindex);242 Neuro *thisN = getNeuro(neuroindex); 243 243 244 244 if (removereferences) 245 245 { 246 Neuro *n;246 Neuro *n; 247 247 // remove all references to thisN 248 for (int i = 0; n = (Neuro *)neurons(i); i++)248 for (int i = 0; n = (Neuro *)neurons(i); i++) 249 249 { 250 250 Neuro *inp; … … 252 252 if (inp == thisN) 253 253 { 254 n>removeInput(j);255 j;254 n>removeInput(j); 255 j; 256 256 } 257 257 } … … 262 262 } 263 263 264 MultiMap &Model::getMap()264 MultiMap &Model::getMap() 265 265 { 266 266 if (!map) map = new MultiMap(); … … 277 277 } 278 278 279 void Model::makeGenToGenMap(MultiMap & result, const MultiMap& gen1tomodel, const MultiMap&gen2tomodel)279 void Model::makeGenToGenMap(MultiMap &result, const MultiMap &gen1tomodel, const MultiMap &gen2tomodel) 280 280 { 281 281 result.clear(); … … 285 285 } 286 286 287 void Model::getCurrentToF0Map(MultiMap &result)287 void Model::getCurrentToF0Map(MultiMap &result) 288 288 { 289 289 result.clear(); 290 290 if (!map) return; 291 const MultiMap &f0m = getF0Map();291 const MultiMap &f0m = getF0Map(); 292 292 makeGenToGenMap(result, *map, f0m); 293 293 } … … 312 312 } 313 313 314 Model::ItemType Model::itemTypeFromLinePrefix(const char *line)315 { 316 struct PrefixAndItem { const char *prefix; ItemType type; };314 Model::ItemType Model::itemTypeFromLinePrefix(const char *line) 315 { 316 struct PrefixAndItem { const char *prefix; ItemType type; }; 317 317 static const PrefixAndItem types[] = { { "m:", ModelType }, { "p:", PartType }, { "j:", JointType }, { "n:", NeuronType }, { "c:", NeuronConnectionType }, { F0_CHECKPOINT_LINE, CheckpointType }, { NULL } }; 318 318 for (const PrefixAndItem *t = types; t>prefix != NULL; t++) 319 319 { 320 const char *in = line;321 const char *pattern = t>prefix;320 const char *in = line; 321 const char *pattern = t>prefix; 322 322 for (; *in == *pattern; in++, pattern++) 323 323 if (*pattern == ':') … … 355 355 for (; f0txt.getNextToken(pos, line, '\n'); lnum++) 356 356 { 357 const char *line_ptr = line.c_str();357 const char *line_ptr = line.c_str(); 358 358 for (; *line_ptr; line_ptr++) 359 359 if (!strchr(" \r\t", *line_ptr)) break; … … 361 361 if (!*line_ptr) continue; 362 362 363 const char *colon = strchr(line_ptr, ':');363 const char *colon = strchr(line_ptr, ':'); 364 364 ItemType type = UnknownType; 365 365 SString excluding_prefix; … … 414 414 } 415 415 416 const MultiMap &Model::getF0Map()416 const MultiMap &Model::getF0Map() 417 417 { 418 418 if (!f0map) … … 472 472 } 473 473 474 for (i = 0; p = (Part *)parts(i); i++)474 for (i = 0; p = (Part *)parts(i); i++) 475 475 { 476 476 partparam.select(p); … … 481 481 map>add(len, gen.len()  1, partToMap(i)); 482 482 } 483 for (i = 0; j = (Joint *)joints(i); i++)483 for (i = 0; j = (Joint *)joints(i); i++) 484 484 { 485 485 jointparam.select(j); … … 491 491 map>add(len, gen.len()  1, jointToMap(i)); 492 492 } 493 for (i = 0; n = (Neuro *)neurons(i); i++)493 for (i = 0; n = (Neuro *)neurons(i); i++) 494 494 { 495 495 neuroparam.select(n); … … 502 502 for (a = 0; a < neurons.size(); a++) 503 503 { // inputs 504 n = (Neuro *)neurons(a);504 n = (Neuro *)neurons(a); 505 505 // if ((n>getInputCount()==1)&&(n>getInput(0).refno <= n>refno)) 506 506 // continue; // already done with Neuro::conn_refno … … 510 510 double w; 511 511 NeuroConn nc; 512 Neuro *n2 = n>getInput(b, w);512 Neuro *n2 = n>getInput(b, w); 513 513 // if (((n2.parentcount==1)&&(n2.parent)&&(n2.parent>refno < n2.refno)) ^ 514 514 // (n2.neuro_refno>=0)) … … 533 533 } 534 534 535 for (vector<Model *>::const_iterator it = checkpoints.begin(); it != checkpoints.end(); it++)535 for (vector<Model *>::const_iterator it = checkpoints.begin(); it != checkpoints.end(); it++) 536 536 { 537 537 Geno g = (*it)>getF0Geno(); … … 562 562 } 563 563 564 Model *Model::getCheckpoint(int i)564 Model *Model::getCheckpoint(int i) 565 565 { 566 566 return checkpoints[i]; … … 575 575 } 576 576 577 void Model::setGeno(const Geno &newgeno)577 void Model::setGeno(const Geno &newgeno) 578 578 { 579 579 geno = newgeno; … … 586 586 delMap(); 587 587 delF0Map(); 588 for (i = 0; p = (Part *)parts(i); i++)588 for (i = 0; p = (Part *)parts(i); i++) 589 589 p>clearMapping(); 590 for (i = 0; j = (Joint *)joints(i); i++)590 for (i = 0; j = (Joint *)joints(i); i++) 591 591 j>clearMapping(); 592 for (i = 0; n = (Neuro *)neurons(i); i++)592 for (i = 0; n = (Neuro *)neurons(i); i++) 593 593 n>clearMapping(); 594 594 } … … 607 607 Part *p; Joint *j; Neuro *n; 608 608 int i; 609 for (i = 0; p = (Part *)parts(i); i++)609 for (i = 0; p = (Part *)parts(i); i++) 610 610 if (p>getMapping()) 611 611 map>add(*p>getMapping(), partToMap(i)); 612 for (i = 0; j = (Joint *)joints(i); i++)612 for (i = 0; j = (Joint *)joints(i); i++) 613 613 if (j>getMapping()) 614 614 map>add(*j>getMapping(), jointToMap(i)); 615 for (i = 0; n = (Neuro *)neurons(i); i++)615 for (i = 0; n = (Neuro *)neurons(i); i++) 616 616 if (n>getMapping()) 617 617 map>add(*n>getMapping(), neuroToMap(i)); … … 629 629 } 630 630 631 Pt3D Model::whereDelta(const Part & start, const Pt3D& rot, const Pt3D&delta)631 Pt3D Model::whereDelta(const Part &start, const Pt3D &rot, const Pt3D &delta) 632 632 { 633 633 Orient roto; … … 641 641 } 642 642 643 int Model::addFromString(ItemType item_type, const SString &singleline, const MultiRange *srcrange)643 int Model::addFromString(ItemType item_type, const SString &singleline, const MultiRange *srcrange) 644 644 { 645 645 return addFromString(item_type, singleline, 0, srcrange); 646 646 } 647 647 648 int Model::addFromString(ItemType item_type, const SString &singleline, int line_num, const MultiRange *srcrange)648 int Model::addFromString(ItemType item_type, const SString &singleline, int line_num, const MultiRange *srcrange) 649 649 { 650 650 SString error_message; … … 654 654 if (error_message.len() == 0) // generic error when no detailed message is available 655 655 error_message = "Invalid f0 code"; 656 if (line_num >0)656 if (line_num > 0) 657 657 error_message += SString::sprintf(", line #%d", line_num); 658 658 error_message += nameForErrors(); … … 662 662 } 663 663 664 int Model::addFromStringNoLog(ItemType item_type, const SString &line, SString & error_message, const MultiRange*srcrange)664 int Model::addFromStringNoLog(ItemType item_type, const SString &line, SString &error_message, const MultiRange *srcrange) 665 665 { 666 666 error_message = SString::empty(); … … 831 831 SString Model::nameForErrors() const 832 832 { 833 if (geno.getName().len() >0)833 if (geno.getName().len() > 0) 834 834 return SString::sprintf(" in '%s'", geno.getName().c_str()); 835 835 return SString::empty(); … … 850 850 else 851 851 { 852 Pt3D bbmin = ((Part *)parts(0))>p, bbmax = bbmin;852 Pt3D bbmin = ((Part *)parts(0))>p, bbmax = bbmin; 853 853 for (i = 0; i < parts.size(); i++) 854 854 { 855 p = (Part *)parts(i);855 p = (Part *)parts(i); 856 856 p>owner = this; 857 857 if (checklevel > 0) … … 894 894 for (i = 0; i < joints.size(); i++) 895 895 { 896 j = (Joint *)joints(i);896 j = (Joint *)joints(i); 897 897 // VALIDMINMAX are managed manually when adding joint properties in f0def! 898 898 // (could be made dynamic but not really worth the effort) … … 979 979 for (i = 0; i < neurons.size(); i++) 980 980 { 981 n = (Neuro *)neurons(i);981 n = (Neuro *)neurons(i); 982 982 n>part_refno = (n>part) ? n>part>refno : 1; 983 983 n>joint_refno = (n>joint) ? n>joint>refno : 1; … … 991 991 for (i = 0; i < parts.size(); i++) 992 992 { 993 p = (Part *)parts(i);993 p = (Part *)parts(i); 994 994 if (p>mass <= 0.001) 995 995 p>mass = 1.0; … … 1003 1003 for (i = 0; i < joints.size(); i++) 1004 1004 { 1005 j = (Joint *)joints(i);1006 if (j>part1>flags &LINKFLAG)1005 j = (Joint *)joints(i); 1006 if (j>part1>flags & LINKFLAG) 1007 1007 { 1008 if (!(j>part2>flags &LINKFLAG))1008 if (!(j>part2>flags & LINKFLAG)) 1009 1009 { 1010 1010 change = 1; … … 1013 1013 } 1014 1014 else 1015 if (j>part2>flags &LINKFLAG)1015 if (j>part2>flags & LINKFLAG) 1016 1016 { 1017 if (!(j>part1>flags&LINKFLAG))1018 {1019 change = 1;1020 j>part1>flags = LINKFLAG;1021 }1017 if (!(j>part1>flags & LINKFLAG)) 1018 { 1019 change = 1; 1020 j>part1>flags = LINKFLAG; 1021 } 1022 1022 } 1023 1023 } … … 1025 1025 for (i = 0; i < parts.size(); i++) 1026 1026 { 1027 p = (Part *)parts(i);1028 if (!(p>flags &LINKFLAG))1027 p = (Part *)parts(i); 1028 if (!(p>flags & LINKFLAG)) 1029 1029 { 1030 1030 logPrintf("Model", "internalCheck", LOG_ERROR, "Not all parts connected (eg. Part #0 and Part #%d)%s", i, nameForErrors().c_str()); … … 1037 1037 for (i = 0; i < joints.size(); i++) 1038 1038 { 1039 j = (Joint *)joints(i);1039 j = (Joint *)joints(i); 1040 1040 if (j>p1_refno == j>p2_refno) 1041 1041 { … … 1046 1046 for (k = i + 1; k < joints.size(); k++) 1047 1047 { 1048 Joint * j2 = (Joint*)joints(k);1048 Joint *j2 = (Joint *)joints(k); 1049 1049 if (((j>p1_refno == j2>p1_refno) && (j>p2_refno == j2>p2_refno)) 1050 1050  ((j>p1_refno == j2>p2_refno) && (j>p2_refno == j2>p1_refno))) … … 1073 1073 } 1074 1074 1075 const Geno &Model::getGeno() const1075 const Geno &Model::getGeno() const 1076 1076 { 1077 1077 return geno; … … 1106 1106 } 1107 1107 1108 Part *Model::getPart(int i) const1109 { 1110 return ((Part *)parts(i));1108 Part *Model::getPart(int i) const 1109 { 1110 return ((Part *)parts(i)); 1111 1111 } 1112 1112 … … 1116 1116 } 1117 1117 1118 Joint *Model::getJoint(int i) const1119 { 1120 return ((Joint *)joints(i));1121 } 1122 1123 int Model::findJoints(SList & result, const Part*part)1118 Joint *Model::getJoint(int i) const 1119 { 1120 return ((Joint *)joints(i)); 1121 } 1122 1123 int Model::findJoints(SList &result, const Part *part) 1124 1124 { 1125 1125 Joint *j; 1126 1126 int n0 = result.size(); 1127 1127 if (part) 1128 for (int i = 0; j = (Joint *)joints(i); i++)1129 if ((j>part1 == part)  (j>part2 == part)) result += (void *)j;1128 for (int i = 0; j = (Joint *)joints(i); i++) 1129 if ((j>part1 == part)  (j>part2 == part)) result += (void *)j; 1130 1130 return result.size()  n0; 1131 1131 } 1132 1132 1133 int Model::findNeuro(Neuro *n)1133 int Model::findNeuro(Neuro *n) 1134 1134 { 1135 1135 return neurons.find(n); 1136 1136 } 1137 1137 1138 int Model::findPart(Part *p)1138 int Model::findPart(Part *p) 1139 1139 { 1140 1140 return parts.find(p); 1141 1141 } 1142 1142 1143 int Model::findJoint(Joint *j)1143 int Model::findJoint(Joint *j) 1144 1144 { 1145 1145 return joints.find(j); … … 1148 1148 int Model::findJoint(Part *p1, Part *p2) 1149 1149 { 1150 Joint *j;1150 Joint *j; 1151 1151 for (int i = 0; j = getJoint(i); i++) 1152 1152 if ((j>part1 == p1) && (j>part2 == p2)) return i; … … 1161 1161 } 1162 1162 1163 Neuro *Model::getNeuro(int i) const1164 { 1165 return (Neuro *)neurons(i);1163 Neuro *Model::getNeuro(int i) const 1164 { 1165 return (Neuro *)neurons(i); 1166 1166 } 1167 1167 … … 1174 1174 } 1175 1175 1176 int Model::findNeuros(SList &result,1177 const char * classname, const Part* part, const Joint*joint)1176 int Model::findNeuros(SList &result, 1177 const char *classname, const Part *part, const Joint *joint) 1178 1178 { 1179 1179 Neuro *nu; 1180 1180 SString cn(classname); 1181 1181 int n0 = result.size(); 1182 for (int i = 0; nu = (Neuro *)neurons(i); i++)1182 for (int i = 0; nu = (Neuro *)neurons(i); i++) 1183 1183 { 1184 1184 if (part) … … 1188 1188 if (classname) 1189 1189 if (nu>getClassName() != cn) continue; 1190 result += (void *)nu;1190 result += (void *)nu; 1191 1191 } 1192 1192 return result.size()  n0; … … 1202 1202 { 1203 1203 Part *p = getPart(i); 1204 p>p.x += (rnd 01  0.5)*amount;1205 p>p.y += (rnd 01  0.5)*amount;1206 p>p.z += (rnd 01  0.5)*amount;1204 p>p.x += (rndDouble(1)  0.5) * amount; 1205 p>p.y += (rndDouble(1)  0.5) * amount; 1206 p>p.z += (rndDouble(1)  0.5) * amount; 1207 1207 } 1208 1208 for (i = 0; i < joints.size(); i++) … … 1215 1215 } 1216 1216 1217 void Model::move(const Pt3D &shift)1218 { 1219 FOREACH(Part *, p, parts)1217 void Model::move(const Pt3D &shift) 1218 { 1219 FOREACH(Part *, p, parts) 1220 1220 p>p += shift; 1221 1221 } 1222 1222 1223 void Model::rotate(const Orient &rotation)1224 { 1225 FOREACH(Part *, p, parts)1223 void Model::rotate(const Orient &rotation) 1224 { 1225 FOREACH(Part *, p, parts) 1226 1226 { 1227 1227 p>p = rotation.transform(p>p); … … 1230 1230 } 1231 1231 1232 void Model::buildUsingSolidShapeTypes(const Model &src_ballandstick_shapes, Part::Shape use_shape, double thickness)1232 void Model::buildUsingSolidShapeTypes(const Model &src_ballandstick_shapes, Part::Shape use_shape, double thickness) 1233 1233 { 1234 1234 for (int i = 0; i < src_ballandstick_shapes.getJointCount(); i++) … … 1247 1247 for (int i = 0; i < src_ballandstick_shapes.getPartCount(); i++) 1248 1248 { 1249 Part *op = src_ballandstick_shapes.getPart(i);1250 Part *p = addNewPart(Part::SHAPE_ELLIPSOID); //always using spherical shape regardless of the 'use_shape' parameter  'use shape' is meant for sticks!1251 p>p = op>p;1252 p>rot = op>rot;1253 p>scale.x = p>scale.y = p>scale.z = thickness;1249 Part *op = src_ballandstick_shapes.getPart(i); 1250 Part *p = addNewPart(Part::SHAPE_ELLIPSOID); //always using spherical shape regardless of the 'use_shape' parameter  'use shape' is meant for sticks! 1251 p>p = op>p; 1252 p>rot = op>rot; 1253 p>scale.x = p>scale.y = p>scale.z = thickness; 1254 1254 } 1255 1255 for (int i = 0; i < src_ballandstick_shapes.getPartCount(); i++) … … 1275 1275 } 1276 1276 1277 SolidsShapeTypeModel::SolidsShapeTypeModel(Model &m, Part::Shape use_shape, double thickness)1277 SolidsShapeTypeModel::SolidsShapeTypeModel(Model &m, Part::Shape use_shape, double thickness) 1278 1278 { 1279 1279 using_model = converted_model = NULL; … … 1334 1334 class MaxNeuro : public Neuro { public: MaxNeuro() { Param par(f0_neuro_paramtab, this); par.setMax(); } }; 1335 1335 1336 Part &Model::getMinPart() { static MinPart part; return part; }1337 Part &Model::getMaxPart() { static MaxPart part; return part; }1338 Part &Model::getDefPart() { static Part part; return part; }1339 Joint &Model::getMinJoint() { static MinJoint joint; return joint; }1340 Joint &Model::getMaxJoint() { static MaxJoint joint; return joint; }1341 Joint &Model::getDefJoint() { static Joint joint; return joint; }1342 Neuro &Model::getMinNeuro() { static MinNeuro neuro; return neuro; }1343 Neuro &Model::getMaxNeuro() { static MaxNeuro neuro; return neuro; }1344 Neuro &Model::getDefNeuro() { static Neuro neuro; return neuro; }1336 Part &Model::getMinPart() { static MinPart part; return part; } 1337 Part &Model::getMaxPart() { static MaxPart part; return part; } 1338 Part &Model::getDefPart() { static Part part; return part; } 1339 Joint &Model::getMinJoint() { static MinJoint joint; return joint; } 1340 Joint &Model::getMaxJoint() { static MaxJoint joint; return joint; } 1341 Joint &Model::getDefJoint() { static Joint joint; return joint; } 1342 Neuro &Model::getMinNeuro() { static MinNeuro neuro; return neuro; } 1343 Neuro &Model::getMaxNeuro() { static MaxNeuro neuro; return neuro; } 1344 Neuro &Model::getDefNeuro() { static Neuro neuro; return neuro; }
Note: See TracChangeset
for help on using the changeset viewer.