source: cpp/frams/_demos/part_shapes.cpp @ 113

Last change on this file since 113 was 109, checked in by sz, 10 years ago

source reorganization (see README)
new feature added: part/joint shapes (see frams/_demos/part_shapes.cpp)

  • Property svn:eol-style set to native
File size: 1.7 KB
Line 
1#include <stdio.h>
2#include <frams/model/model.h>
3#include <frams/model/modelparts.h>
4
5int main()
6{
7Model *m=new Model;
8Part *p1,*p2;
9Joint *j;
10
11m->open();
12
13// chain of ellipsoids - subsequent parts are placed relative to the previous part's orientation and location
14p1=m->addNewPart(Part::SHAPE_ELLIPSOID); //initial part
15p1->scale=Pt3D(1.0,0.7,0.4);
16
17Orient rotation=Orient_1; //must be initialized explicitly because the default Orient constructor does not initialize anything
18rotation.rotate(Pt3D(0.1,0.2,0.3));
19
20for(int N=10;N>0;N--,p1=p2)
21        {
22        p2=m->addNewPart(Part::SHAPE_ELLIPSOID);
23        p2->scale=p1->scale*0.9; //each part is smaller than its predecessor
24       
25        Pt3D advance(p1->scale.x,0,0); //advance by previous part's ellipsoid x radius
26        p2->p = p1->p + p1->o.transform(advance); //advance vector transformed by p1 orientation == in p1 local coordinates
27        p2->setOrient(p1->o.transform(rotation)); //rotation transformed by p1 orientation
28
29        m->addNewJoint(p1,p2,Joint::SHAPE_SOLID); //all parts must be connected
30        }
31
32// chain of cyllinders - line segments between points calculated from the parametric formula P(a)=(2-2*cos(a),2*sin(a)) (circle with r=2)
33Pt3D prev,next;
34p1=m->getPart(0);
35for(float a=0;a<M_PI;a+=M_PI/10)
36        {
37        Pt3D next(2-2*cos(a),0,2*sin(a));
38        if (a>0)
39                {
40                p2=m->addNewPart(Part::SHAPE_CYLINDER);
41                p2->setPositionAndRotationFromAxis(prev,next);
42                p2->scale=Pt3D(prev.distanceTo(next)*0.5,0.05,0.05);// distance*0.5 because scale is "radius", not cylinder length
43
44                m->addNewJoint(p1,p2,Joint::SHAPE_SOLID); //all parts must be connected
45                }
46        p1=p2;
47        prev=next;
48        }
49
50m->close();
51puts((const char*)m->getF0Geno().toString());
52// the genotype can be fed directly to the genotype viewer, like this:
53// part_shapes | theater -g -vpart_shapes -
54}
Note: See TracBrowser for help on using the repository browser.