HolonCanvasController.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package ui.controller;
  2. import classes.*;
  3. import ui.model.Model;
  4. import java.awt.*;
  5. import java.util.ArrayList;
  6. public class HolonCanvasController {
  7. // Ball objects
  8. private ArrayList<HolonBody> bodies = new ArrayList<>();
  9. private int subCount;
  10. private Dimension center;
  11. private Model model;
  12. private ArrayList<HolonBody> sortedSize = new ArrayList<>();
  13. private ArrayList<HolonBody> sortedDist = new ArrayList<>();
  14. private int toDrag;
  15. private boolean beingDragged;
  16. /**
  17. * Constructor.
  18. *
  19. * @param model
  20. * the Model
  21. * @param mp
  22. * the MultipurposeController
  23. */
  24. public HolonCanvasController(Model model) {
  25. this.model = model;
  26. }
  27. // sort the HolonBodys after size
  28. public void rearrangeAfterSize() {
  29. int j = 0;
  30. sortedSize.addAll(bodies);
  31. insertionSizeSort(sortedSize);
  32. sortedDist.addAll(bodies);
  33. ArrayList<Vector2d> pos = insertionDistSort(sortedDist);
  34. for (int i = 0; i < subCount; i++) {
  35. int ID = sortedSize.get(subCount - 1 - i).getId();
  36. for (j = 0; j < subCount; j++) {
  37. if (ID == bodies.get(j).getId()) {
  38. bodies.get(j).position = pos.get(i);
  39. break;
  40. }
  41. }
  42. }
  43. }
  44. // updates the bodies according to the changes of subnets
  45. public void addNewBodies(int subCount, ArrayList<SubNet> subnets, Dimension center, int HolonBodyScale) {
  46. this.subCount = subCount;
  47. this.center = center;
  48. // find correct color for existing HolonBodys
  49. ArrayList<HolonBody> newBodies = new ArrayList<>();
  50. for (int i = 0; i < subCount; i++) {
  51. Color subNetColor = model.getSubNetColors().get(i);
  52. for (int j = 0; j < bodies.size(); j++) {
  53. if (subNetColor == bodies.get(j).getColor()) {
  54. int objectsSize = subnets.get(i).getObjects().size();
  55. bodies.get(j).setRadius((objectsSize * 5 + 10) * HolonBodyScale / 100);
  56. bodies.get(j).setMass((float) Math.pow((objectsSize + 1) * 5, 3));
  57. newBodies.add(bodies.get(j));
  58. newBodies.get(i).setId(i);
  59. break;
  60. }
  61. }
  62. }
  63. bodies = newBodies;
  64. // adding new HolonBodys
  65. for (int i = bodies.size(); i < subCount; i++) {
  66. float radius = (subnets.get(i).getObjects().size() * 5 + 10) * HolonBodyScale / 100;
  67. HolonBody temp = new HolonBody((float) (center.width + Math.pow(-1, i)), (float)(center.height + Math.pow(-1, i)), radius,
  68. (float) Math.pow((subnets.get(i).getObjects().size() + 1) * 5, 3), model.getSubNetColors().get(i));
  69. temp.setId(i);
  70. bodies.add(temp);
  71. }
  72. }
  73. public int getActiveElements(ArrayList<AbstractCpsObject> objects) {
  74. int val = 0;
  75. for (AbstractCpsObject obj : objects) {
  76. if (obj instanceof HolonObject) {
  77. for (HolonElement ele : ((HolonObject) obj).getElements()) {
  78. if (ele.isActive()) {
  79. val += 1;
  80. }
  81. }
  82. } else if (obj instanceof CpsUpperNode) {
  83. val += getTotalProduction(((CpsUpperNode) obj).getNodes());
  84. }
  85. }
  86. return val;
  87. }
  88. public int getTotalProducers(ArrayList<AbstractCpsObject> objects) {
  89. float val = 0;
  90. int prod = 0;
  91. int tStep = model.getCurIteration();
  92. for (AbstractCpsObject obj : objects) {
  93. if (obj instanceof HolonObject) {
  94. for (HolonElement ele : ((HolonObject) obj).getElements()) {
  95. if (ele.getEnergyAtTimeStep(tStep) > 0 && ele.isActive()) {
  96. val += ele.getEnergyAtTimeStep(tStep) * ele.getAmount();
  97. }
  98. }
  99. if (val > 0)
  100. prod += 1;
  101. } else if (obj instanceof CpsUpperNode) {
  102. val += getTotalProduction(((CpsUpperNode) obj).getNodes());
  103. }
  104. }
  105. return prod;
  106. }
  107. public int getTotalElements(ArrayList<AbstractCpsObject> objects) {
  108. int val = 0;
  109. for (AbstractCpsObject obj : objects) {
  110. if (obj instanceof HolonObject) {
  111. val += ((HolonObject) obj).getElements().size();
  112. } else if (obj instanceof CpsUpperNode) {
  113. val += getTotalConsumption(((CpsUpperNode) obj).getNodes());
  114. }
  115. }
  116. return val;
  117. }
  118. public float getTotalConsumption(ArrayList<AbstractCpsObject> objects) {
  119. float val = 0;
  120. int tStep = model.getCurIteration();
  121. for (AbstractCpsObject obj : objects) {
  122. if (obj instanceof HolonObject) {
  123. for (HolonElement ele : ((HolonObject) obj).getElements()) {
  124. if (ele.getEnergyAtTimeStep(tStep) < 0 && ele.isActive()) {
  125. val += ele.getEnergyAtTimeStep(tStep) * ele.getAmount();
  126. }
  127. }
  128. } else if (obj instanceof CpsUpperNode) {
  129. val += getTotalConsumption(((CpsUpperNode) obj).getNodes());
  130. }
  131. }
  132. return val;
  133. }
  134. public float getTotalProduction(ArrayList<AbstractCpsObject> objects) {
  135. float val = 0;
  136. int tStep = model.getCurIteration();
  137. for (AbstractCpsObject obj : objects) {
  138. if (obj instanceof HolonObject) {
  139. for (HolonElement ele : ((HolonObject) obj).getElements()) {
  140. if (ele.getEnergyAtTimeStep(tStep) > 0 && ele.isActive()) {
  141. val += ele.getEnergyAtTimeStep(tStep) * ele.getAmount();
  142. }
  143. }
  144. } else if (obj instanceof CpsUpperNode) {
  145. val += getTotalProduction(((CpsUpperNode) obj).getNodes());
  146. }
  147. }
  148. return val;
  149. }
  150. public void updateBodies(float elapsedSeconds) {
  151. // step the position of movable objects based off their velocity/gravity
  152. // and elapsedTime
  153. for (int i = 0; i < subCount; i++) {
  154. if (toDrag != bodies.get(i).getId() || !beingDragged) {
  155. bodies.get(i).position.setX(
  156. (float) (bodies.get(i).position.getX() + (bodies.get(i).velocity.getX() * (elapsedSeconds))
  157. - ((bodies.get(i).position.getX() - center.getWidth()) / (50 + subCount))));
  158. bodies.get(i).position.setY(
  159. (float) (bodies.get(i).position.getY() + (bodies.get(i).velocity.getY() * (elapsedSeconds))
  160. - ((bodies.get(i).position.getY() - center.getHeight()) / (50 + subCount))));
  161. float epsilon = 0.000009f;
  162. if (Math.abs(bodies.get(i).velocity.getX()) < epsilon)
  163. bodies.get(i).velocity.setX(0);
  164. if (Math.abs(bodies.get(i).velocity.getY()) < epsilon)
  165. bodies.get(i).velocity.setY(0);
  166. }
  167. }
  168. checkCollisions();
  169. }
  170. // Insertion sort for Sweep and Prune
  171. public void insertionSort(ArrayList<HolonBody> a) {
  172. for (int p = 1; p < subCount; p++) {
  173. Comparable<HolonBody> tmp = a.get(p);
  174. int j = p;
  175. for (; j > 0 && tmp.compareTo(a.get(j - 1)) < 0; j--)
  176. a.set(j, a.get(j - 1));
  177. a.set(j, (HolonBody) tmp);
  178. }
  179. }
  180. // Insertion sort for subnet size
  181. private void insertionSizeSort(ArrayList<HolonBody> a) {
  182. for (int p = 1; p < subCount; p++) {
  183. HolonBody tmp = a.get(p);
  184. int j = p;
  185. for (; j > 0 && tmp.compareSizeTo(a.get(j - 1)) < 0; j--)
  186. a.set(j, a.get(j - 1));
  187. a.set(j, (HolonBody) tmp);
  188. }
  189. }
  190. // Insertion sort for HolonBody distance
  191. private ArrayList<Vector2d> insertionDistSort(ArrayList<HolonBody> a) {
  192. ArrayList<Vector2d> pos = new ArrayList<>();
  193. for (int p = 1; p < subCount; p++) {
  194. HolonBody tmp = a.get(p);
  195. int j = p;
  196. for (; j > 0 && tmp.compareDistTo(a.get(j - 1), center) < 0; j--)
  197. a.set(j, a.get(j - 1));
  198. a.set(j, (HolonBody) tmp);
  199. }
  200. for (int i = 0; i < subCount; i++)
  201. pos.add(a.get(i).position);
  202. return pos;
  203. }
  204. public void checkCollisions() {
  205. insertionSort(bodies);
  206. for (int i = 0; i < subCount; i++) {
  207. // Ball to Ball collision
  208. float radiusI = bodies.get(i).getRadius();
  209. float xPlusradius = (bodies.get(i).position.getX() + radiusI);
  210. float yPlusradius = (bodies.get(i).position.getY() + radiusI);
  211. float yMinusRadius = (bodies.get(i).position.getY()- radiusI);
  212. for (int j = i + 1; j < subCount; j++) {
  213. float radiusJ = bodies.get(j).getRadius();
  214. if (xPlusradius < (bodies.get(j).position.getX()- radiusJ))
  215. break;
  216. if (yPlusradius < (bodies.get(j).position.getY()- radiusJ)
  217. || (bodies.get(j).position.getY() + radiusJ) < yMinusRadius)
  218. continue;
  219. bodies.get(i).resolveCollision(bodies.get(j));
  220. }
  221. }
  222. }
  223. public ArrayList<HolonBody> getBodies() {
  224. return bodies;
  225. }
  226. public void setDrag(boolean beingDragged) {
  227. this.beingDragged = beingDragged;
  228. }
  229. public void setBodyToDrag(int i) {
  230. this.toDrag = i;
  231. }
  232. }