/*-
 * Copyright 2016 Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.eclipse.dawnsci.plotting.examples;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.dawnsci.analysis.api.dataset.IDataset;
import org.eclipse.dawnsci.analysis.api.dataset.ILazyDataset;
import org.eclipse.dawnsci.analysis.api.dataset.Slice;
import org.eclipse.dawnsci.analysis.api.io.IDataHolder;
import org.eclipse.dawnsci.analysis.dataset.impl.Dataset;
import org.eclipse.dawnsci.analysis.dataset.impl.DatasetFactory;
import org.eclipse.dawnsci.plotting.api.PlotType;
import org.eclipse.dawnsci.plotting.api.trace.IPlane3DTrace;
import org.eclipse.dawnsci.plotting.examples.util.BundleUtils;
import org.eclipse.swt.widgets.Composite;

public class Plane3DExample extends PlotExample {

	@Override
	protected void createExampleContent(Composite parent) {

		try {
			// We create a basic plot
			system.createPlotPart(parent, "Plane 3D Example", getViewSite()
					.getActionBars(), PlotType.PLANE3D, this);
			system.setPlotType(PlotType.PLANE3D);
			File loc = new File(
					BundleUtils.getBundleLocation(Activator.PLUGIN_ID),
					getFileName());

			String fp = loc.getAbsolutePath();
			IDataHolder dh = service.getData(fp, true, null);

			ILazyDataset data = dh.getLazyDataset(0).getTransposedView(1,0);
			int l = 700;
			List<? extends IDataset> axes = generateAxes(data, l, 500, 600, 700);

			IPlane3DTrace plane1 = system.createPlane3DTrace("Plane1");
			plane1.setData(data, new double[] {300, 600}, new double[] {0,0,0}, new double[] {0,0,1}, axes);
			system.addTrace(plane1);

			IPlane3DTrace plane2 = system.createPlane3DTrace("Plane2");
			// FIXME plane normal does not change orientation
			double c = Math.cos(Math.toRadians(10));
			double s = Math.sin(Math.toRadians(10));
			plane2.setData(data.getSliceView(new Slice(null, null, -1)), new double[] {300, 600}, new double[] {300,0,300}, new double[] {s,0,-c}, axes);
			system.addTrace(plane2);

		} catch (Throwable ne) {
			ne.printStackTrace(); // Or your favourite logging.
		}
	}

	private List<? extends IDataset> generateAxes(ILazyDataset data, int length, double... axesDims) {
		// FIXME for some reason axes have to be the same length
		List<IDataset> axes = new ArrayList<>();
		Dataset x = DatasetFactory.createLinearSpace(0, axesDims[0], length, Dataset.FLOAT64);
		x.setName("x");
		axes.add(x);
		Dataset y = DatasetFactory.createLinearSpace(0, axesDims[1], length, Dataset.FLOAT64);
		y.setName("y");
		axes.add(y);
		Dataset z = DatasetFactory.createLinearSpace(0, axesDims[2], length, Dataset.FLOAT64);
		z.setName("z");
		axes.add(z);
		return axes;
	}

	protected String getFileName() {
		return "test.png";
	}

}
@@ -1542,31 +1542,8 @@
 t(0)
-.getTransposedView(1,0)
 ;%0A%09%09
@@ -1885,16 +1885,68 @@
 ane2%22);%0A
+%09%09%09plane2.setOpacity(0.85); // FIXME does not work%0A%0A
 %09%09%09// FI
@@ -1989,16 +1989,36 @@
 entation
+ in anti-z direction
 %0A%09%09%09doub
@@ -2749,22 +2749,34 @@
 ims%5B1%5D, 
+(int) (
 length
+*1.5)
 , Datase
@@ -2881,32 +2881,36 @@
 sDims%5B2%5D, length
+ * 2
 , Dataset.FLOAT6

Fix transposition of image and add different length axes