PageRenderTime 17ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/extensions/org/mt4jx/util/extension3D/MergeHelper.java

http://mt4j.googlecode.com/
Java | 175 lines | 115 code | 37 blank | 23 comment | 11 complexity | 29a4cfcd1ad62481eda067ef1b9692a6 MD5 | raw file
  1package org.mt4jx.util.extension3D;
  2
  3import java.util.ArrayList;
  4import java.util.HashMap;
  5
  6import org.mt4j.components.MTCanvas;
  7import org.mt4j.components.MTComponent;
  8import org.mt4j.components.StateChange;
  9import org.mt4j.components.StateChangeEvent;
 10import org.mt4j.components.StateChangeListener;
 11import org.mt4j.components.TransformSpace;
 12import org.mt4j.components.bounds.IBoundingShape;
 13import org.mt4j.components.bounds.IBoundingShapeMergable;
 14import org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.Cluster;
 15
 16public class MergeHelper implements StateChangeListener {
 17
 18	HashMap<Integer,IBoundingShape> boundingShapes = new HashMap<Integer,IBoundingShape>();
 19
 20	private int mergedCounter = 0;
 21
 22	private int normalCounter = 0;
 23
 24	private int dirtyCounter = 0;
 25
 26	private int normalInsideCounter;
 27
 28	private int addedCounter;
 29
 30	private int isNorMergedCounter;
 31
 32	private int isMergedCounter;
 33
 34	private int case1;
 35
 36	private int case2;
 37
 38	private int getboundsinside;
 39	
 40	private static MergeHelper helperSingleton;
 41	private static long counter = 0;
 42	
 43	private MergeHelper()
 44	{
 45		
 46	}
 47	
 48	public static MergeHelper getInstance()
 49	{
 50		if(helperSingleton==null)
 51		{
 52			helperSingleton = new MergeHelper();
 53			return helperSingleton;
 54		}else
 55		{
 56			return helperSingleton;
 57		}
 58	}
 59	
 60	/**
 61	 * return the merged Bounds of the component with all children
 62	 * @param comp, the component which should be merged
 63	 * @param dirty if the passed component has changed its matrix
 64	 *  	  true in case of calling this method after a statechange	     	 
 65	 * @return
 66	 */	
 67	private IBoundingShape mergeBoundsWithChildren(MTComponent comp,boolean dirty)
 68	{	
 69		
 70	//	System.out.println("Dirtycounter " + dirtyCounter + " normalCounter " + normalCounter + " mergedCounter " + mergedCounter);
 71		if (isMergedOfChildrenBounds(comp)==true&&!dirty) return getMergedBoundsForComponent(comp);
 72		//System.out.println("normalinside " + normalInsideCounter++);
 73		
 74		if(comp.getChildren().length==0)
 75		{		
 76			addMTComponentWithMergedBounding(comp,comp.getBounds());
 77			return comp.getBounds();
 78		}
 79		
 80		ArrayList<IBoundingShapeMergable> shapesToMerge = new ArrayList<IBoundingShapeMergable>();
 81		
 82		if(comp.hasBounds())
 83		{
 84			if (comp.getBounds() instanceof IBoundingShapeMergable) {
 85				IBoundingShapeMergable mergeableBounds = (IBoundingShapeMergable)comp.getBounds(); 
 86				addMTComponentWithMergedBounding(comp, mergeableBounds);
 87				shapesToMerge.add(mergeableBounds);
 88			}
 89			
 90		}
 91		
 92		for(int i=0;i < comp.getChildren().length;i++)
 93		{				
 94			MTComponent children = comp.getChildren()[i];
 95			IBoundingShape shape1 = mergeBoundsWithChildren(children,false);
 96			
 97			if (shape1 instanceof IBoundingShapeMergable) {
 98				IBoundingShapeMergable mergeableBounds = (IBoundingShapeMergable)shape1; 
 99				IBoundingShapeMergable shape = mergeableBounds.getBoundsTransformed(TransformSpace.RELATIVE_TO_PARENT);
100				shapesToMerge.add(shape);
101			}
102		}
103		
104		for(int i=shapesToMerge.size()-1;i>0;i--)
105		{
106			IBoundingShapeMergable mergedShape = shapesToMerge.get(i).merge(shapesToMerge.get(i-1));
107			shapesToMerge.set(i-1,mergedShape);			
108		}
109				
110		if(shapesToMerge.size()>0)
111		{
112			addMTComponentWithMergedBounding(comp,shapesToMerge.get(0));			
113			return shapesToMerge.get(0);
114		}else
115		{
116			return null;
117		}
118	}
119	
120	private void addMTComponentWithMergedBounding(MTComponent comp,IBoundingShape shape)
121	{			 
122		//System.out.println("Added counter " +  comp.getID() + " " + addedCounter++ + " " + boundingShapes.size());
123		boundingShapes.put(comp.getID(),shape);		
124	}
125	
126	private void removeMTComponentWithMergedBounding(MTComponent comp)
127	{
128		boundingShapes.remove(comp.getID());
129	}
130	
131	public IBoundingShape getMergedBoundsForComponent(MTComponent comp)
132	{
133		if(boundingShapes.containsKey(comp.getID()))
134		{		
135			return boundingShapes.get(comp.getID());
136		}else
137		{
138			IBoundingShape shape =  mergeBoundsWithChildren(comp,true);			
139			updateParentAfterMerge(comp);
140			return shape;
141		}		
142	}
143	
144	/**
145	 * 
146	 * @param comp
147	 */
148	private void updateParentAfterMerge(MTComponent comp)
149	{		
150		if(comp.getParent()!=null&&boundingShapes.containsKey(comp.getParent().getID())==true)
151		{
152			mergeBoundsWithChildren(comp.getParent(),true);		
153		}
154	}
155	
156	public boolean isMergedOfChildrenBounds(MTComponent comp)
157	{
158		//System.out.println(comp.getID());
159		
160		return boundingShapes.containsKey(comp.getID());
161	}
162
163	public void stateChanged(StateChangeEvent evt) {
164			
165		//IF needed add a new state to StateChange.GLOBAL_TRANSFORM_CHANGED to StateChange
166		//and in MTComponent propagteMatrixChange fire this StateChange
167		/*if(evt.getSource() instanceof MTComponent&&evt.getState()==StateChange.GLOBAL_TRANSFORM_CHANGED)
168		{			
169			MTComponent comp = (MTComponent)evt.getSource();
170			mergeBoundsWithChildren(comp,true);	
171			updateParentAfterMerge(comp);	
172		}*/
173	}
174	
175}