source: cpp/frams/_demos/resize_f0.cpp @ 1255

Last change on this file since 1255 was 1035, checked in by Maciej Komosinski, 4 years ago

Added a demo program to scale a Model and a simple test

File size: 3.9 KB
Line 
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 <stdlib.h>
6#include <stdio.h>
7#include <time.h>
8#include <common/virtfile/stdiofile.h>
9
10#include <frams/model/model.h>
11#include <common/loggers/loggertostdout.h>
12#include <frams/genetics/preconfigured.h>
13
14/**
15 @file
16 Sample code: Resize f0 geometry
17 Usage: resize_f0 scale [genotype_or_stdin]
18
19 Examples:
20
21 # make a big X(X,X)
22 ./shapeconvert -s3 "X(X,X)" | ./resize_f0 2 -
23
24   //0s
25   p:1.0, sh=2, sy=0.4000000059604645, sz=0.4000000059604645, rx=-1.5707963267948966, rz=3.141592653589793
26   p:2.499997879272546, -0.8660266281835431, sh=2, sy=0.4000000059604645, sz=0.4000000059604645, rz=2.0943926535897934
27   p:2.499997879272546, 0.8660266281835431, sh=2, sy=0.4000000059604645, sz=0.4000000059604645, rz=-2.0943926535897934
28   j:0, 1, sh=1
29   j:0, 2, sh=1
30
31 # make a small quadruped
32 ./loader_test data/walking.gen "Basic Quadruped" | ./shapeconvert -s3 - | ./resize_f0 0.5 -
33
34   //0s
35   p:0.25, sh=3, sx=0.25, sy=0.10000000149011612, sz=0.10000000149011612, rx=-1.5707963267948966, rz=3.141592653589793
36   p:0.4999990816987242, -0.24999999999831343, sh=3, sx=0.24999999999999997, sy=0.10000000149011612, sz=0.10000000149011612, rz=1.5707926535897934
37   p:0.3468893514573017, -0.6249989074155372, -0.1530782964706476, sh=3, sx=0.24999999999999997, sy=0.10000000149011612, sz=0.10000000149011612, rx=1.1072319391268979, 0.6589830922573491, 0.68466467448983
38   p:0.75, sh=3, sx=0.25, sy=0.10000000149011612, sz=0.10000000149011612, rx=-1.5707963267948966, rz=3.141592653589793
39   p:0.9999990816987241, -0.24999999999831343, sh=3, sx=0.24999999999999997, sy=0.10000000149011612, sz=0.10000000149011612, rz=1.5707926535897931
40   p:1.153106057040214, -0.6250000322123028, -0.1530782964706476, sh=3, sx=0.24999999999999994, sy=0.10000000149011612, sz=0.10000000149011612, rx=-1.1072259414054058, 0.6589830922573492, 2.4569206326897564
41   p:0.9999990816987241, 0.24999999999831343, sh=3, sx=0.24999999999999997, sy=0.10000000149011612, sz=0.10000000149011612, rz=-1.5707926535897931
42   p:1.153106057040214, 0.6250000322123028, -0.1530782964706476, sh=3, sx=0.24999999999999994, sy=0.10000000149011612, sz=0.10000000149011612, rx=-2.0343667121843874, 0.6589830922573492, -2.4569206326897564
43   p:0.4999990816987242, 0.24999999999831343, sh=3, sx=0.24999999999999997, sy=0.10000000149011612, sz=0.10000000149011612, rz=-1.5707926535897934
44   p:0.3468893514573017, 0.6249989074155372, -0.1530782964706476, sh=3, sx=0.24999999999999997, sy=0.10000000149011612, sz=0.10000000149011612, rx=2.0343607144628955, 0.6589830922573491, -0.68466467448983
45   j:0, 1, sh=1
46   j:0, 3, sh=1
47   j:0, 8, sh=1
48   j:1, 2, sh=1
49   j:3, 4, sh=1
50   j:3, 6, sh=1
51   j:4, 5, sh=1
52   j:6, 7, sh=1
53   j:8, 9, sh=1
54
55*/
56
57int main(int argc, char*argv[])
58{
59        StdioFILE::setStdio(); //setup VirtFILE::Vstdin/out/err
60        LoggerToStdout messages_to_stderr(LoggerBase::Enable | LoggerBase::DontBlock, VirtFILE::Vstderr); //errors -> stderr, don't interfere with stdout
61
62        PreconfiguredGenetics genetics;
63
64        if (argc<2)
65        {
66                printf("Usage: %s scale [genotype_or_stdin]\n", argv[0]);
67                return 1;
68        }
69
70        double scale = atof(argv[1]);   
71        SString gen;
72        if ((argc>=3) && (strcmp(argv[2],"-")!=0))
73                gen = argv[2];
74        else
75                loadSString(VirtFILE::Vstdin, gen);
76        Geno g(gen);
77        Model m(g, Model::SHAPETYPE_UNKNOWN);
78
79        if (!m.isValid())
80        {
81                logPrintf(argv[0], "main", LOG_ERROR, "Cannot build Model from the supplied genotype.\n");
82                return 2;
83        }
84
85        m.open();
86        for (int i = 0; i < m.getPartCount(); i++)
87        {
88                auto *p = m.getPart(i);
89                p->p *= scale;
90                if (m.getShapeType()==Model::SHAPETYPE_SOLIDS)
91                        p->scale *= scale;
92        }
93        for (int i = 0; i < m.getJointCount(); i++)
94        {
95                auto *j = m.getJoint(i);
96                if (j->isDelta())
97                        j->d *= scale;
98        }
99        m.close();
100
101        puts(m.getF0Geno().getGenesAndFormat().c_str());
102
103        return 0;
104}
Note: See TracBrowser for help on using the repository browser.