PageRenderTime 20ms CodeModel.GetById 9ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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