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

http://mt4j.googlecode.com/ · Java · 228 lines · 156 code · 40 blank · 32 comment · 17 complexity · 9afc752cf3b917154cd3da78f38a1ab5 MD5 · raw file

  1. package org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.GroupVisualizations;
  2. import java.util.ArrayList;
  3. import org.mt4j.MTApplication;
  4. import org.mt4j.components.MTComponent;
  5. import org.mt4j.components.TransformSpace;
  6. import org.mt4j.components.bounds.BoundingSphere;
  7. import org.mt4j.components.visibleComponents.shapes.AbstractShape;
  8. import org.mt4j.components.visibleComponents.shapes.MTPolygon;
  9. import org.mt4j.input.IMTEventListener;
  10. import org.mt4j.input.MTEvent;
  11. import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
  12. import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
  13. import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;
  14. import org.mt4j.util.MTColor;
  15. import org.mt4j.util.camera.MTCamera;
  16. import org.mt4j.util.math.ConvexQuickHull2D;
  17. import org.mt4j.util.math.Matrix;
  18. import org.mt4j.util.math.Tools3D;
  19. import org.mt4j.util.math.Vector3D;
  20. import org.mt4j.util.math.Vertex;
  21. import org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.Cluster;
  22. import org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.MTClusterEvent;
  23. import org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.MTLassoSelectionEvent;
  24. import processing.core.PApplet;
  25. public class LassoVisualizationAction implements IMTEventListener {
  26. //THIS CLASS IS NOT MAINTAINED ANYMORE
  27. private PApplet pApplet;
  28. public LassoVisualizationAction(PApplet pApplet)
  29. {
  30. this.pApplet = pApplet;
  31. }
  32. public void processMTEvent(MTEvent mtEvent) {
  33. if(mtEvent instanceof MTLassoSelectionEvent)
  34. {
  35. MTLassoSelectionEvent lassoEvent = (MTLassoSelectionEvent)mtEvent;
  36. switch(lassoEvent.getId())
  37. {
  38. case MTLassoSelectionEvent.SELECTION_ENDED:
  39. if(lassoEvent.getCluster()!=null&&lassoEvent.getSelectedComps().size()>1)
  40. {
  41. lassoEvent.getSelectionPoly().setFillColor(new MTColor(100,150,250,50));
  42. lassoEvent.getSelectionPoly().setGestureAllowance(DragProcessor.class, true);
  43. lassoEvent.getSelectionPoly().setGestureAllowance(RotateProcessor.class, true);
  44. lassoEvent.getSelectionPoly().setGestureAllowance(ScaleProcessor.class, true);
  45. lassoEvent.getSelectionPoly().setPickable(false);
  46. lassoEvent.getSelectionPoly().setBoundsAutoCompute(true);
  47. lassoEvent.getSelectionPoly().setBoundsBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);
  48. packClusterPolygon(lassoEvent.getSelectionPoly(),lassoEvent.getCluster());
  49. lassoEvent.getSelectionPoly().setLineStipple((short)0xDDDD);
  50. lassoEvent.getSelectionPoly().setStrokeColor(new MTColor(0,0,0,255));
  51. MTComponent visualComponentGroup = new MTComponent(pApplet, lassoEvent.getCluster().getAttachedCamera());
  52. visualComponentGroup.addChild(lassoEvent.getSelectionPoly());
  53. //System.out.println("lassoEvent " + lassoEvent.getSelectionPoly().toString());
  54. lassoEvent.getCluster().setVisualComponentGroup(visualComponentGroup);
  55. ((MTApplication)pApplet).getCurrentScene().getCanvas().addChild(lassoEvent.getSelectionPoly());
  56. }
  57. break;
  58. case MTLassoSelectionEvent.SELECTION_UPDATED:
  59. break;
  60. }
  61. }
  62. else if(mtEvent instanceof MTClusterEvent)
  63. {
  64. MTClusterEvent clEvent = (MTClusterEvent)mtEvent;
  65. switch(clEvent.getId())
  66. {
  67. case MTClusterEvent.CLUSTER_UPDATED:
  68. MTPolygon polygon = createNewPolygon(clEvent.getCluster());
  69. MTComponent visualComponentGroup = new MTComponent(pApplet, clEvent.getCluster().getAttachedCamera());
  70. visualComponentGroup.addChild(polygon);
  71. clEvent.getCluster().setVisualComponentGroup(visualComponentGroup);
  72. break;
  73. case MTClusterEvent.CLUSTER_DELETED:
  74. for(MTComponent comp : clEvent.getCluster().getChildren())
  75. {
  76. if(comp instanceof MTPolygon)
  77. {
  78. clEvent.getCluster().removeChild(comp);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. private MTPolygon createNewPolygon(Cluster cluster)
  85. {
  86. MTPolygon polygon = new MTPolygon(pApplet, new Vertex[0]);
  87. polygon.setFillColor(new MTColor(100,150,250,50));
  88. polygon.setGestureAllowance(DragProcessor.class, true);
  89. polygon.setGestureAllowance(RotateProcessor.class, true);
  90. polygon.setGestureAllowance(ScaleProcessor.class, true);
  91. polygon.setBoundsAutoCompute(true);
  92. polygon.setBoundsBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);
  93. packClusterPolygon(polygon,cluster);
  94. polygon.setLineStipple((short)0xDDDD);
  95. polygon.setStrokeColor(new MTColor(0,0,0,255));
  96. polygon.attachCamera(cluster.getAttachedCamera());
  97. return polygon;
  98. }
  99. /**
  100. * Calculates the convex hull of all its children.
  101. * Then changes the cluster-polygon to represent that convex hull
  102. * and adds it as a child.
  103. */
  104. private void packClusterPolygon(MTPolygon polygon,Cluster cluster){
  105. ArrayList<Vector3D> allClusteredVerts = new ArrayList<Vector3D>();
  106. MTComponent[] children = cluster.getChildren();
  107. for (int i = 0; i < children.length; i++) {
  108. allClusteredVerts.addAll(getAllClusteredVerts(children[i],polygon,cluster));
  109. }
  110. //if (shapes != 0){// && shapes == children.length){ //If all children are of type abstractShape
  111. ArrayList<Vector3D> hull = ConvexQuickHull2D.getConvexHull2D(allClusteredVerts);
  112. if (hull.size() > 0){
  113. //Correctly close polygon with 1.st vertex again
  114. hull.add(hull.get(0).getCopy());
  115. Vertex[] newVerts = new Vertex[hull.size()];
  116. for (int i = 0; i < hull.size(); i++) {
  117. Vector3D vec = hull.get(i);
  118. newVerts[i] = new Vertex(vec);
  119. }
  120. // Vertex[] newVerts = (Vertex[])hull.toArray(new Vertex[hull.size()]);
  121. // System.out.println("Hull vertices: ");
  122. for (Vertex v : newVerts){
  123. v.setRGBA(100,150,250, 50);
  124. }
  125. polygon.setVertices(newVerts);
  126. polygon.setBoundsBehaviour(AbstractShape.BOUNDS_DONT_USE);
  127. // clusterPoly.setBoundingShape(new BoundsArbitraryPlanarPolygon(clusterPoly, clusterPoly.getVerticesLocal()));
  128. //Reset matrix of the clusterpoly because the new vertices are set at the global location
  129. polygon.setLocalMatrix(new Matrix());
  130. //FIXME center are is negative if verts are in counterclockwise order?
  131. // Vector3D clusterCenter = clusterPoly.getCenterPointGlobal();
  132. // clusterPoly.scaleGlobal(1.1f, 1.1f, 1, new Vector3D(-1* clusterCenter.x, -1 * clusterCenter.y, clusterCenter.z));
  133. polygon.scale(1.1f, 1.1f, 1, polygon.getCenterPointLocal(), TransformSpace.LOCAL);
  134. }else{
  135. System.err.println("Couldnt pack polygon.");
  136. }
  137. //shapes = new Integer(0); //reset for next call to zero
  138. }
  139. private ArrayList<Vector3D> getAllClusteredVerts(MTComponent comp,MTPolygon polygon,Cluster cluster)
  140. {
  141. ArrayList<Vector3D> allClusteredVerts = new ArrayList<Vector3D>();
  142. if(comp.getChildren().length==0)
  143. {
  144. //Get vertices for convex hull of all selected components
  145. if (comp instanceof AbstractShape){
  146. //shapes++;
  147. AbstractShape shape = (AbstractShape)comp;
  148. // Vertex[] verts = shape.getVerticesPickingWorld();
  149. Vector3D[] verts = null;
  150. if (shape.hasBounds()){
  151. verts = shape.getBounds().getVectorsGlobal();
  152. //FIXME add for all not only for boundingsphere
  153. if(shape.getBounds() instanceof BoundingSphere)
  154. {
  155. BoundingSphere bSphere = (BoundingSphere)shape.getBounds();
  156. verts = bSphere.getVectorsOnBoundingSphereGlobal(4);
  157. }
  158. //check if points should be projected in case of 3D grouping
  159. // Vector3D[] newVerts = new Vector3D[verts.length];
  160. // int i = 0;
  161. /*for(Vector3D vert : verts)
  162. {
  163. if(polygon.getVerticesGlobal()[0].z!=vert.z)
  164. {
  165. //Tools3D.projectPointToPlane(vert, this.getAttachedCamera().getFrustum(),selectionPolygon.getVerticesGlobal()[0].z,(MTApplication)this.getRenderer());
  166. }
  167. }*/
  168. }else{
  169. verts = shape.getVerticesGlobal();
  170. }
  171. for (Vector3D v : verts){
  172. if(cluster.getAttachedCamera().getFrustum().getZValueOfNearPlane()!=v.z)
  173. {
  174. v = Tools3D.projectPointToPlaneInPerspectiveMode(v, cluster.getAttachedCamera().getFrustum(), cluster.getAttachedCamera().getFrustum().getZValueOfNearPlane(),((MTApplication)pApplet));
  175. }
  176. allClusteredVerts.add(v);
  177. }
  178. }
  179. return allClusteredVerts;
  180. }
  181. for(int i=0;i<comp.getChildren().length;i++)
  182. {
  183. allClusteredVerts.addAll(this.getAllClusteredVerts(comp.getChildren()[i],polygon,cluster));
  184. }
  185. return allClusteredVerts;
  186. }
  187. }