1 | package com.framsticks.model; |
---|
2 | |
---|
3 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
4 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
5 | import com.framsticks.util.lang.Casting; |
---|
6 | import com.framsticks.util.lang.Containers; |
---|
7 | import com.framsticks.util.lang.IterableIterator; |
---|
8 | import com.framsticks.util.math.Orientation; |
---|
9 | import org.apache.log4j.Logger; |
---|
10 | |
---|
11 | import java.util.ArrayList; |
---|
12 | import java.util.Iterator; |
---|
13 | import java.util.List; |
---|
14 | |
---|
15 | /** |
---|
16 | * Author: Piotr Śniegowski |
---|
17 | */ |
---|
18 | @FramsClassAnnotation(id = "m") |
---|
19 | public class Model { |
---|
20 | |
---|
21 | private final static Logger log = Logger.getLogger(Model.class); |
---|
22 | |
---|
23 | @ParamAnnotation(id = "se") |
---|
24 | public double startingEnergy; |
---|
25 | |
---|
26 | @ParamAnnotation |
---|
27 | public double getEnerg0() { return startingEnergy; } |
---|
28 | @ParamAnnotation |
---|
29 | public void setEnerg0(double energ0) { startingEnergy = energ0; } |
---|
30 | |
---|
31 | |
---|
32 | @ParamAnnotation(id = "Vstyle") |
---|
33 | public String visualizationStyle; |
---|
34 | |
---|
35 | @ParamAnnotation |
---|
36 | public final List<Part> parts = new ArrayList<Part>(); |
---|
37 | |
---|
38 | @ParamAnnotation |
---|
39 | public final List<Joint> joints = new ArrayList<Joint>(); |
---|
40 | |
---|
41 | @ParamAnnotation |
---|
42 | public final List<NeuroDef> neurodefs = new ArrayList<NeuroDef>(); |
---|
43 | |
---|
44 | //TODO: why those methods returns and accepts doubles? |
---|
45 | @ParamAnnotation |
---|
46 | public double getNumparts() { return (double)parts.size(); } |
---|
47 | @ParamAnnotation |
---|
48 | public double getNumjoints() { return (double)joints.size(); } |
---|
49 | @ParamAnnotation |
---|
50 | public double getNumneurons() { return (double)neurodefs.size(); } |
---|
51 | |
---|
52 | //this is impossible to use, because numparts field is marked as readonly |
---|
53 | @ParamAnnotation |
---|
54 | public void setNumparts(double numparts) { Containers.resizeList(parts, (int) (double) numparts); } |
---|
55 | @ParamAnnotation |
---|
56 | public void setNumjoints(double numjoints) { Containers.resizeList(joints, (int)(double)numjoints); } |
---|
57 | @ParamAnnotation |
---|
58 | public void setNumneurons(double numneurons) { Containers.resizeList(neurodefs, (int)(double)numneurons); } |
---|
59 | |
---|
60 | public List<Part> getParts() { return parts; } |
---|
61 | public List<Joint> getJoints() { return joints; } |
---|
62 | public List<NeuroDef> getNeuroDefs() { return neurodefs; } |
---|
63 | |
---|
64 | public static Model build(List<Object> objects) { |
---|
65 | Iterator<Object> i = objects.iterator(); |
---|
66 | if (!i.hasNext()) { |
---|
67 | return null; |
---|
68 | } |
---|
69 | Model f0Genotype = Casting.tryCast(Model.class, i.next()); |
---|
70 | if (f0Genotype == null) { |
---|
71 | log.fatal("first object is not a Model"); |
---|
72 | return null; |
---|
73 | } |
---|
74 | for (Object object : new IterableIterator<Object>(i)) { |
---|
75 | if (object instanceof Joint) { |
---|
76 | f0Genotype.joints.add((Joint)object); |
---|
77 | continue; |
---|
78 | } |
---|
79 | if (object instanceof Part) { |
---|
80 | f0Genotype.parts.add((Part)object); |
---|
81 | continue; |
---|
82 | } |
---|
83 | if (object instanceof NeuroDef) { |
---|
84 | f0Genotype.neurodefs.add((NeuroDef) object); |
---|
85 | continue; |
---|
86 | } |
---|
87 | log.error("invalid class: " + object.getClass().getCanonicalName()); |
---|
88 | } |
---|
89 | |
---|
90 | for (Part p : f0Genotype.getParts()) { |
---|
91 | p.setOrientation(new Orientation().rotate(p.getRotation())); |
---|
92 | } |
---|
93 | for (Joint j : f0Genotype.getJoints()) { |
---|
94 | /** based on c++ Joint::attachToParts*/ |
---|
95 | Part p1 = f0Genotype.parts.get(j.part1); |
---|
96 | Part p2 = f0Genotype.parts.get(j.part2); |
---|
97 | assert p1 != null && p2 != null; |
---|
98 | Orientation o = new Orientation().rotate(j.getRotation()); |
---|
99 | p2.setOrientation(p1.getOrientation().transform(o)); |
---|
100 | p2.setPosition(p2.getOrientation().transform(j.getDelta()).add(p1.getPosition())); |
---|
101 | } |
---|
102 | return f0Genotype; |
---|
103 | } |
---|
104 | } |
---|