diff --git a/cpp_frep/frep.cpp b/cpp_frep/frep.cpp index 59b88bc801e620477c0698f1d44ea3d419a55327..7883ec4e63258e158e66f1b7ffc35ca0bcdca15c 100755 --- a/cpp_frep/frep.cpp +++ b/cpp_frep/frep.cpp @@ -471,6 +471,10 @@ namespace cpp_frep { Scale():sx(0.5f),sy(0.5f),ox(0.0f),oy(0.0f){ name="Scale"; }; + Scale(Frep* shape_,scalar scale_factor_):sx(scale_factor_),sy(scale_factor_),ox(0.0f),oy(0.0f){ + shape=shape_; + name="Scale"; + }; Scale(Frep* shape_,scalar ox_, scalar oy_,scalar sx_,scalar sy_) { shape=shape_; sx=sx_; diff --git a/cpp_frep/main.cpp b/cpp_frep/main.cpp index 53bb6a5f7ee73963cc3fad1cb474458eba33fedf..c85fa9b4a226c4c3260e1cc9c7283acd12f1e878 100755 --- a/cpp_frep/main.cpp +++ b/cpp_frep/main.cpp @@ -462,73 +462,73 @@ int main(int const, char const**) { scalar dy=res; - std::vector<std::vector<scalar>> genome; + // std::vector<std::vector<scalar>> genome; - std::vector<scalar> node1; - node1.push_back(0.564); - node1.push_back(0.53); - node1.push_back(0.445); - node1.push_back(0.5024); - node1.push_back(0.7475); + // std::vector<scalar> node1; + // node1.push_back(0.564); + // node1.push_back(0.53); + // node1.push_back(0.445); + // node1.push_back(0.5024); + // node1.push_back(0.7475); - genome.push_back(node1); - - std::vector<scalar> node2; - node2.push_back(0.48); - node2.push_back(0.3); - node2.push_back(0.9); - node2.push_back(0.17); - node2.push_back(0.227); + // genome.push_back(node1); + + // std::vector<scalar> node2; + // node2.push_back(0.48); + // node2.push_back(0.3); + // node2.push_back(0.9); + // node2.push_back(0.17); + // node2.push_back(0.227); - genome.push_back(node2); + // genome.push_back(node2); - // std::vector<scalar> node3; - // node3.push_back(0.2472); - // node3.push_back(0.452); - // node3.push_back(0.727); - // node3.push_back(0.17); - // node3.push_back(0.227); - // genome.push_back(node3); + // // std::vector<scalar> node3; + // // node3.push_back(0.2472); + // // node3.push_back(0.452); + // // node3.push_back(0.727); + // // node3.push_back(0.17); + // // node3.push_back(0.227); + // // genome.push_back(node3); - Frep_tree tree(genome,smooth,parametric,bound); + // Frep_tree tree(genome,smooth,parametric,bound); - // Circle c(0.5f,0.0f,0.0f); - // Rectangle r(-0.3f,0.3f,-0.3f,0.3f); - // Subtract s(&r,&c); - // PolarArray pa(&r,45.0f,1.0f); + // // Circle c(0.5f,0.0f,0.0f); + // // Rectangle r(-0.3f,0.3f,-0.3f,0.3f); + // // Subtract s(&r,&c); + // // PolarArray pa(&r,45.0f,1.0f); - // PolarArray pa2(&r,45.0f,0.5f); - // InvoluteGear gear( 1.0f, 1.0f, 1.1f, 20.0f, 8.0f) ; - // Scale sc(&gear,0.0f,0.0f,0.25f,0.25f); - // drawFrep(&sc,minX, maxX, minY, maxY, dx, dy,"gear",true); + // // PolarArray pa2(&r,45.0f,0.5f); + // // InvoluteGear gear( 1.0f, 1.0f, 1.1f, 20.0f, 8.0f) ; + // // Scale sc(&gear,0.0f,0.0f,0.25f,0.25f); + // // drawFrep(&sc,minX, maxX, minY, maxY, dx, dy,"gear",true); - Circle c(1.0f,0.0f,0.0f); - Circle c1(0.25f,0.0f,0.0f); - // Scale sc(tree.getFrep(),0.0f,0.0f,0.25f,0.25f); - // PolarArray pa2(&sc,45.0f,0.75f); - // Add add(&pa2,&c); - // Subtract sub (&add,&c1); - // drawFrep(&sub,minX, maxX, minY, maxY, dx, dy,"scaleAndRotated",true); + // Circle c(1.0f,0.0f,0.0f); + // Circle c1(0.25f,0.0f,0.0f); + // // Scale sc(tree.getFrep(),0.0f,0.0f,0.25f,0.25f); + // // PolarArray pa2(&sc,45.0f,0.75f); + // // Add add(&pa2,&c); + // // Subtract sub (&add,&c1); + // // drawFrep(&sub,minX, maxX, minY, maxY, dx, dy,"scaleAndRotated",true); - InvoluteGear gear( 1.0f, 1.0f, 1.1f, 20.0f, 8.0f) ; - Rotate rot (&gear,0.0f,0.0f,15.0f); - Scale sc1(&rot,0.0f,0.0f,0.3f,0.3f); - Add add1(&sc1,&c); - Subtract target (&add1,&c1); - drawFrep(&target,minX, maxX, minY, maxY, dx, dy,"target",false); + // InvoluteGear gear( 1.0f, 1.0f, 1.1f, 20.0f, 8.0f) ; + // Rotate rot (&gear,0.0f,0.0f,15.0f); + // Scale sc1(&rot,0.0f,0.0f,0.3f,0.3f); + // Add add1(&sc1,&c); + // Subtract target (&add1,&c1); + // drawFrep(&target,minX, maxX, minY, maxY, dx, dy,"target",false); - int maxSteps=100; - precision=0.009; - // nelderMead(&tree,&target, precision, maxSteps, tuneParameters, bound, res); + // int maxSteps=100; + // precision=0.009; + // // nelderMead(&tree,&target, precision, maxSteps, tuneParameters, bound, res); - drawFrep(tree,minX, maxX, minY, maxY, dx, dy,"final",false); + // drawFrep(tree,minX, maxX, minY, maxY, dx, dy,"final",false); - tree.printGenome(); + // tree.printGenome(); @@ -545,12 +545,49 @@ int main(int const, char const**) { // drawFrep(tree,minX, maxX, minY, maxY, dx, dy,"finalt",false); //json - std::ifstream i1("2_nodes.json"); - std::ifstream i2("5_nodes.json"); - json results_2_nodes; - i1 >> results_2_nodes; - - std::cout<<results_2_nodes["population"]["0"][0]["point"]<<'\n'; + unsigned n_nodes_=2; + std::ifstream i2("2_nodes.json"); + std::ifstream i5("5_nodes.json"); + scalar scale_factor=0.8; + int n_teeth_=8; + json results; + i2 >> results; + + std::cout<<results["population"]["0"][0]["point"]<<'\n'; + + std::cout<<results["Best Index"].size()<<'\n'; + unsigned numGenerations=results["Best Index"].size(); + + for (unsigned generation=0;generation<numGenerations;generation++ ){ + std::vector<std::vector<scalar>> genome; + std::vector<std::vector<scalar>> nodes; + std::cout<<std::to_string(generation)<<'\n'; + unsigned bestPointIndex= results["Best Index"][std::to_string(generation)]; + std::cout<<bestPointIndex<<'\n'; + auto bestGenome=results["population"][std::to_string(generation)][bestPointIndex]["point"]; + std::cout<<bestGenome<<'\n'; + for (unsigned i=0;i<n_nodes_;i++ ){ + std::vector<scalar> node; + for (unsigned j=0;j<5;j++ ){ + node.push_back(bestGenome[i*5+j]); + } + nodes.push_back(node); + genome.push_back(nodes[i]); + Frep_tree tree(genome,smooth,parametric,bound); + Scale gear ( //scale + new Add ( //body and teeth + new Circle(1.0 ,0.0,0.0),//body + new PolarArray ( //teeth + new Scale ( //scale + tree.getFrep() //tooth //get teeth + ,0.0f,0.0f, 0.28f,0.28f) //scale + , 360.0/(scalar)n_teeth_,0.75) ) //body and teeth + ,scale_factor);//scale + + drawFrep(&gear,minX, maxX, minY, maxY, dx, dy,"node"+std::to_string(n_nodes_)+"/"+std::to_string(generation),false); + //std::to_string() + } + } // // write prettified JSON to another file // std::ofstream o("pretty_5_nodes.json");