PageRenderTime 52ms CodeModel.GetById 19ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

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