View Javadoc

1   package SteeringProperties;
2   
3   import XMLSteeringProperties.XMLForcePoint;
4   import XMLSteeringProperties.XMLForce_packet;
5   import java.util.ArrayList;
6   import java.util.LinkedList;
7   
8   
9   /**
10   * The packet of all things which belog to one force of one location in the Target Approaching Steering.
11   * It includes mainly the forcePoints, which is list of ForcePoint. It defines the force.
12   * @author Marki
13   */
14  public class Force_packet {
15  
16      private LinkedList<ForcePoint> forcePoints;
17      private int LAST_DISTANCE = 4000;
18  
19      //<editor-fold defaultstate="collapsed" desc="Constructors">
20  
21      public Force_packet(LinkedList<ForcePoint> forcePoints) {
22          this.forcePoints = forcePoints;
23      }
24  
25      public Force_packet(int forceValue) {
26          forcePoints = new LinkedList<ForcePoint>();
27          forcePoints.add(new ForcePoint(0, forceValue, true));
28      }
29  
30      public Force_packet(int firstForceValue, int lastForceValue) {
31          forcePoints = new LinkedList<ForcePoint>();
32          forcePoints.add(new ForcePoint(0, firstForceValue, true));
33          forcePoints.add(new ForcePoint(LAST_DISTANCE, lastForceValue, true));
34      }
35  
36      public Force_packet(XMLForce_packet xmlPacket) {
37          forcePoints = new LinkedList<ForcePoint>();
38          for(XMLForcePoint x : xmlPacket.forces) {
39              forcePoints.add(new ForcePoint(x));
40          }
41      }
42  
43      /**Copy constructor.*/
44      public Force_packet(Force_packet force_packet) {
45          forcePoints = new LinkedList<ForcePoint>();
46          setForcePacket(force_packet);
47      }
48  
49      // </editor-fold>
50  
51      //<editor-fold defaultstate="collapsed" desc="Getters and setters">
52  
53      public void setForcePacket(Force_packet f_packet) {
54          this.forcePoints.clear();
55          for(ForcePoint fPoint : f_packet.forcePoints) {
56              forcePoints.add(new ForcePoint(fPoint));
57          }
58          LAST_DISTANCE = f_packet.LAST_DISTANCE;
59      }
60      
61      public LinkedList<ForcePoint> getForcePoints() {
62          return forcePoints;
63      }
64  
65      public int getLAST_DISTANCE() {
66          return LAST_DISTANCE;
67      }
68  
69      /**Adds the forcePoint after the fp with the nearest but smaller (or same) distance.*/
70      public void addForcePoint(ForcePoint forcePoint) {
71          int index = 0;
72          for(ForcePoint fp : forcePoints) {
73              if (fp.distance < forcePoint.distance) index++;
74          }
75          forcePoints.add(index, forcePoint);
76      }
77  
78      /**Deletes the force point - if it's not the first force point.*/
79      public void deleteForcePoint(ForcePoint forcePoint) {
80          if (forcePoints.getFirst() != forcePoint) forcePoints.remove(forcePoint);
81      }
82  
83      public int getValueOfTheDistance(double distance) {
84          int value = 0;
85          ForcePoint lowerFP = getLowerFP(distance);
86          ForcePoint higherFP = getHigherFP(distance);
87          if (lowerFP != null) {
88              if (lowerFP.continues) {        //Pokud toto není splněné, tak je síla v tomto úseku nulová.
89                  if (higherFP == null) {     //Pokud není žádný vyšší bod, tak se použije sklon z předchozího úseku nebo konstantní sklon, pokud je to zároveň první úsek.
90                      higherFP = getInifinityPoint();
91                  }
92                  float direction;
93                  if (higherFP.distance != lowerFP.distance) {
94                      direction = ((float)(higherFP.forceValue - lowerFP.forceValue))/(higherFP.distance - lowerFP.distance);
95                  } else {
96                      direction = 0;
97                  }
98                  value = getValueOfTheLine(lowerFP.distance, lowerFP.forceValue, distance, direction);
99              }
100         }
101         return value;
102     }
103 
104     public String getSpecialText() {
105         String result = "";
106         for(ForcePoint fp : forcePoints) {
107             result += fp.getSpecialText();
108         }
109         return result;
110     }
111 
112     public XMLForce_packet getXMLForce_packet() {
113         XMLForce_packet xmlPacket = new XMLForce_packet();
114         ArrayList<XMLForcePoint> xmlForces = new ArrayList<XMLForcePoint>();
115         for(ForcePoint fp : forcePoints) {
116             xmlForces.add(fp.getXMLForcePoint());
117         }
118         xmlPacket.forces = xmlForces;
119         return xmlPacket;
120     }
121     // </editor-fold>
122 
123     //<editor-fold defaultstate="collapsed" desc="Inner methods - work with the list of forcePoints.">
124 
125     /**Gets the ForcePoint, which's distance is the nearest smaller (or equal) than the distance of the coord.*/
126     public ForcePoint getLowerFP(double distance) {
127         ForcePoint result = null;
128         for(ForcePoint fp : forcePoints) {
129             if (fp.distance <= distance) result = fp;
130         }
131         return result;
132     }
133 
134     /**Gets the ForcePoint, which's distance is the nearest smaller (or equal) than the distance of the coord.*/
135     public ForcePoint getHigherFP(double distance) {
136         ForcePoint result = null;
137         for(int index = forcePoints.size()-1; index>= 0; index--) {
138             ForcePoint fp = forcePoints.get(index);
139             if (fp.distance >= distance) result = fp;
140         }
141         return result;
142     }
143 
144     /**Returns the y-value of point on the line defined by 2 points.*/
145     public int getValueOfTheLine(int fromDistance, int fromValue, double toDistance, float direction) {
146         return Math.round(fromValue + ((float)(toDistance - fromDistance)*direction)) ;
147     }
148 
149 
150     /**Returns some ForcePoint with very distant distance.*/
151     public ForcePoint getInifinityPoint() {
152         ForcePoint fp1 = null;
153         ForcePoint fp2 = null;
154         int size = forcePoints.size();
155         if (size > 1) {
156             fp1 = forcePoints.get(size - 2);
157         }
158         if (size > 0) {
159             fp2 = forcePoints.get(size - 1);
160         }
161         return getInifinityPoint(fp1, fp2);
162     }
163 
164     /**Returns some ForcePoint with very distant distance - uses the two last ForcePoints of the list.*/
165     public ForcePoint getInifinityPoint(ForcePoint fp1, ForcePoint fp2) {
166         ForcePoint result = null;
167         if (fp1 != null) {
168             int infinityDistance = LAST_DISTANCE;
169             int valueOfInfinityDistance = getValueOfTheLine(fp2.distance, fp2.forceValue, infinityDistance, ((float)(fp2.forceValue - fp1.forceValue))/(fp2.distance - fp1.distance)) ;
170             result = new ForcePoint(infinityDistance, valueOfInfinityDistance, false);
171         } else {
172             int infinityDistance = LAST_DISTANCE;
173             int valueOfInfinityDistance = fp2.forceValue;
174             result = new ForcePoint(infinityDistance, valueOfInfinityDistance, false);
175         }
176         return result;
177     }
178     // </editor-fold>
179 }