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