/extensions/org/mt4jx/input/inputProcessors/componentProcessors/Group3DProcessorNew/GroupVisualizations/BlinkingLineVisualizationAction.java

http://mt4j.googlecode.com/ · Java · 168 lines · 134 code · 29 blank · 5 comment · 11 complexity · 8586ed05dc524707c1423d966f887d40 MD5 · raw file

  1. package org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.GroupVisualizations;
  2. import java.util.ArrayList;
  3. import javax.media.opengl.GL;
  4. import org.mt4j.components.MTComponent;
  5. import org.mt4j.components.visibleComponents.shapes.MTLine;
  6. import org.mt4j.input.IMTEventListener;
  7. import org.mt4j.input.MTEvent;
  8. import org.mt4j.input.inputProcessors.IGestureEventListener;
  9. import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
  10. import org.mt4j.input.inputProcessors.componentProcessors.lassoProcessor.IdragClusterable;
  11. import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
  12. import org.mt4j.util.math.Tools3D;
  13. import org.mt4j.util.math.Vector3D;
  14. import org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.Cluster;
  15. import org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.IVisualizeMethodProvider;
  16. import org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.MTClusterEvent;
  17. import org.mt4jx.util.extension3D.ComponentHelper;
  18. import org.mt4jx.util.extension3D.MergeHelper;
  19. import processing.core.PApplet;
  20. public class BlinkingLineVisualizationAction implements IMTEventListener,IVisualizeMethodProvider {
  21. private PApplet pApplet;
  22. private long startTime;
  23. public BlinkingLineVisualizationAction(PApplet pApplet)
  24. {
  25. this.pApplet = pApplet;
  26. startTime = System.currentTimeMillis();
  27. }
  28. public void processMTEvent(MTEvent mtEvent) {
  29. if(mtEvent instanceof MTClusterEvent)
  30. {
  31. MTClusterEvent clEvent = (MTClusterEvent)mtEvent;
  32. switch(clEvent.getId())
  33. {
  34. case MTClusterEvent.CLUSTER_CREATED:
  35. clEvent.getCluster().setVisualizeProvider(this);
  36. clEvent.getCluster().addGestureListener(DragProcessor.class,new ActivateVisualizationAction(clEvent.getCluster(),this));
  37. break;
  38. case MTClusterEvent.CLUSTER_SELECTED:
  39. if(clEvent.getCluster().getVisualizeProvider()!=this)
  40. {
  41. startTime = System.currentTimeMillis();
  42. clEvent.getCluster().addGestureListener(DragProcessor.class,new ActivateVisualizationAction(clEvent.getCluster(),this));
  43. clEvent.getCluster().setVisualizeProvider(this);
  44. }
  45. break;
  46. }
  47. }
  48. }
  49. public void visualize(Cluster cluster)
  50. {
  51. if((long)((((double)(System.currentTimeMillis()-startTime))/500.0))%2==0)
  52. {
  53. GL gl = Tools3D.getGL(pApplet);
  54. Tools3D.beginGL(pApplet);
  55. gl.glBegin(gl.GL_LINES);
  56. MTLine[] lines = getVisualizationLines(cluster.getChildren());
  57. for(MTLine line : lines)
  58. {
  59. gl.glVertex3f(line.getVerticesLocal()[0].x,line.getVerticesLocal()[0].y,line.getVerticesLocal()[0].z);
  60. gl.glVertex3f(line.getVerticesLocal()[1].x,line.getVerticesLocal()[1].y,line.getVerticesLocal()[1].z);
  61. }
  62. gl.glEnd();
  63. Tools3D.endGL(pApplet);
  64. }
  65. }
  66. private MTLine[] getVisualizationLines(MTComponent[] selectedComps)
  67. {
  68. ArrayList<Vector3D> centerPoints = new ArrayList<Vector3D>();
  69. for(MTComponent comp : selectedComps)
  70. {
  71. if(comp instanceof MTComponent)
  72. {
  73. MTComponent mtcomp = (MTComponent)comp;
  74. centerPoints.add(ComponentHelper.getCenterPointGlobal(mtcomp));
  75. }
  76. }
  77. ArrayList<Vector3D> sortedCenterPoints = getSortedListForShortedDistance(centerPoints);
  78. MTLine[] lines = null;
  79. if(sortedCenterPoints.size()>2)
  80. {
  81. lines = new MTLine[sortedCenterPoints.size()];
  82. }else
  83. {
  84. lines = new MTLine[sortedCenterPoints.size()-1];
  85. }
  86. int j=0;
  87. for(int i=0;i<sortedCenterPoints.size()-1;i++)
  88. {
  89. MTLine line = new MTLine(pApplet, sortedCenterPoints.get(i).x,sortedCenterPoints.get(i).y,sortedCenterPoints.get(i).z,
  90. sortedCenterPoints.get(i+1).x,sortedCenterPoints.get(i+1).y,sortedCenterPoints.get(i+1).z);
  91. //line.setStrokeWeight(5.0f);
  92. //line.setStrokeColor(new MTColor(1.0f,1.0f,1.0f,0.7f));//TODO make color configurable
  93. lines[i]= line;
  94. }
  95. //close circle
  96. int size = sortedCenterPoints.size();
  97. if(sortedCenterPoints.size()>2)
  98. {
  99. MTLine line = new MTLine(pApplet, sortedCenterPoints.get(size-1).x,sortedCenterPoints.get(size-1).y,sortedCenterPoints.get(size-1).z,
  100. sortedCenterPoints.get(0).x,sortedCenterPoints.get(0).y,sortedCenterPoints.get(0).z);
  101. //line.setStrokeWeight(5.0f);
  102. //line.setStrokeColor(new MTColor(1.0f,1.0f,1.0f,0.7f));//TODO make color configurable
  103. lines[lines.length-1] = line;
  104. }
  105. return lines;
  106. }
  107. private ArrayList<Vector3D> getSortedListForShortedDistance(ArrayList<Vector3D> centerPoints)
  108. {
  109. Vector3D startPoint = null;
  110. ArrayList<Vector3D> sortedVectors = new ArrayList<Vector3D>();
  111. startPoint = centerPoints.get(0);
  112. sortedVectors.add(startPoint);
  113. centerPoints.remove(startPoint);
  114. Vector3D lastPoint = startPoint;
  115. while(centerPoints.size()>0)
  116. {
  117. Vector3D nextPoint = getNextPoint(centerPoints,lastPoint);
  118. sortedVectors.add(nextPoint);
  119. centerPoints.remove(nextPoint);
  120. lastPoint = nextPoint;
  121. }
  122. return sortedVectors;
  123. }
  124. private Vector3D getNextPoint(ArrayList<Vector3D> centerPoints,Vector3D lastPoint)
  125. {
  126. Vector3D currentShortestDistancePoint = null;
  127. float minLength = 99999999.0f;
  128. for(int i=0;i<centerPoints.size();i++)
  129. {
  130. float currentLength = lastPoint.getSubtracted(centerPoints.get(i)).length();
  131. if(currentLength<minLength)
  132. {
  133. minLength = currentLength;
  134. currentShortestDistancePoint = centerPoints.get(i);
  135. }
  136. }
  137. return currentShortestDistancePoint;
  138. }
  139. }