/*
 * Created on Nov 3, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package de.tuberlin.polymake.common;

import de.jreality.math.Matrix;
import de.jreality.math.MatrixBuilder;
import de.jreality.scene.Appearance;
import de.jreality.scene.Geometry;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.scene.Transformation;
import de.jreality.scene.data.Attribute;
import de.jreality.scene.data.DoubleArrayArray;
import de.jreality.shader.CommonAttributes;
import de.jreality.tools.DraggingTool;
import de.jreality.tools.RotateTool;
import de.jreality.util.PickUtility;

/**
 * @author wotzlaw
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JRealityScene extends SceneGraphComponent {
	
	//the displayed geometry
	protected SceneGraphComponent geom;
	
	public JRealityScene(SceneGraphComponent geom) {
		super("Polymake Root");
		this.geom = geom;
		this.setAppearance(new Appearance());
		
		this.getAppearance().setAttribute(CommonAttributes.POINT_SHADER +"."+CommonAttributes.PICKABLE, true);
		this.getAppearance().setAttribute(CommonAttributes.POLYGON_SHADER+"."+CommonAttributes.PICKABLE, true);
		this.getAppearance().setAttribute(CommonAttributes.LINE_SHADER +"."+CommonAttributes.PICKABLE, true);
		
		addChild(geom);
	}
	
	/* Update geometry with the coordinates given by ps */
	public void update(PointSet ps) {
		
		int n_comp = geom.getChildComponentCount();
		if(! (n_comp == 0)) {
			
			Geometry centerGeom = geom.getChildComponent(0).getGeometry();
			
			if(centerGeom instanceof de.jreality.scene.PointSet) {			
				de.jreality.scene.PointSet newPs = (de.jreality.scene.PointSet)centerGeom;
				
				double[] coords = new double[ps.getDim() * ps.getNPoints()];
				int index = 0;
				for(int i = 0; i < ps.getNPoints(); ++i){
					PolymakePoint p = ps.getPoint(i);
					double[] pointCoords = p.getCoords();
					for(int k = 0; k < ps.getDim(); ++k) {
						coords[index++] = pointCoords[k];
					}
				}
				DoubleArrayArray.Inlined newCoords = new DoubleArrayArray.Inlined(coords, ps.getDim());		
				newPs.setVertexAttributes(Attribute.COORDINATES, newCoords);		
			}
		}
		
	}
	
	double[] barycenter(DoubleArrayArray coordinates)
	{
		double[] result = { 0, 0, 0 };
		int length = coordinates.getLength();
		
		for(int k = 0; k < length; ++k) {
			result[0] += coordinates.getValueAt(k, 0);
			result[1] += coordinates.getValueAt(k, 1);
			result[2] += coordinates.getValueAt(k, 2);
		}
		
		result[0] /= length;
		result[1] /= length;
		result[2] /= length;
		
		return result;
		
	}
	
	void explodeGeometry(double scale) {
		
		int n_comp = geom.getChildComponentCount();
		if(! (n_comp ==0)) {
			
			SceneGraphComponent center = geom.getChildComponent(0);
			Geometry center_geom = center.getGeometry();
			if(center_geom instanceof de.jreality.scene.PointSet) {
				
				de.jreality.scene.PointSet pts =(de.jreality.scene.PointSet)center_geom; 
				DoubleArrayArray coords = (DoubleArrayArray)pts.getVertexAttributes(Attribute.COORDINATES);
					
				
				double[] barycntr = barycenter(coords);
				
				for(int k = 1; k < n_comp ; ++k) {
					
					SceneGraphComponent scene_c = geom.getChildComponent(k);
					Geometry scene_geom = scene_c.getGeometry();
					if(scene_geom instanceof de.jreality.scene.PointSet) {
						
						pts = (de.jreality.scene.PointSet)scene_geom;
						coords = 
							(DoubleArrayArray)pts.getVertexAttributes(Attribute.COORDINATES);
						double[] cntr = barycenter(coords);
						Matrix m = MatrixBuilder.euclidean().translate(
								scale*(cntr[0] - barycntr[0]),
								scale*(cntr[1] - barycntr[1]),
								scale*(cntr[2] - barycntr[2])   ).getMatrix();
						
						double[] mat = m.getArray();
						Transformation trans = new Transformation(mat);
						scene_c.setTransformation(trans);
						
					} 
				}
			} 
		}
		
	}	
	/**
	 * @param val
	 */
	public void setTransparency(String name, double val) {	
		
		SceneGraphComponent g = null;
		int k = geom.getChildComponentCount();

		//System.err.println("POLYMAKE: count " + k);
		
		for(int j = 0; j < k ; ++j) {
			SceneGraphComponent sgc = geom.getChildComponent(j);
			if(sgc.getName() == name) {
				g = sgc;
				break;
			}
		}
			
		if(g!=null) {
			Appearance app = g.getAppearance();
			if(app == null)
				app = new Appearance();
			app.setAttribute("polygonShader.transparency", val);
			//System.err.println("POLYMAKE: Setting transparency:" + val);
			g.setAppearance(app);
			geom.setAppearance(app);
		} else {
			//System.err.println("POLYMAKE: geometry " + name + " does not exist!");
		}
		
	}

}

syntax highlighted by Code2HTML, v. 0.9.1