Android Open Source - android-plotter Axis






From Project

Back to project page android-plotter.

License

The source code is released under:

Apache License

If you think the Android project android-plotter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.solovyev.android.plotter.meshes;
/*from  w w w.  ja  v a 2s. c  o m*/
import org.solovyev.android.plotter.Dimensions;
import org.solovyev.android.plotter.MeshConfig;

import javax.annotation.Nonnull;
import javax.microedition.khronos.opengles.GL11;

public class Axis extends BaseMesh implements DimensionsAware {

  private static enum Direction {
    X(new int[]{1, 0, 0},
        new int[]{0, 1, 0}),
    Y(new int[]{0, 1, 0},
        new int[]{1, 0, 0}),
    Z(new int[]{0, 0, 1}, new int[]{0, 1, 0});

    final int[] vector;
    final int[] arrow;

    Direction(int[] vector, int[] arrow) {
      this.vector = vector;
      this.arrow = arrow;
    }
  }

  @Nonnull
  private final Direction direction;

  @Nonnull
  private final Arrays arrays = new Arrays();

  @Nonnull
  protected volatile Dimensions dimensions;

  @Nonnull
  private final ArrayInitializer initializer = new ArrayInitializer();

  private Axis(@Nonnull Direction direction, @Nonnull Dimensions dimensions) {
    this.direction = direction;
    this.dimensions = dimensions;
  }

  @Nonnull
  public static Axis x(@Nonnull Dimensions dimensions) {
    return new Axis(Direction.X, dimensions);
  }

  @Nonnull
  public static Axis y(@Nonnull Dimensions dimensions) {
    return new Axis(Direction.Y, dimensions);
  }

  @Nonnull
  public static Axis z(@Nonnull Dimensions dimensions) {
    return new Axis(Direction.Z, dimensions);
  }

  @Nonnull
  public DoubleBufferMesh<Axis> toDoubleBuffer() {
    return DoubleBufferMesh.wrap(this, DimensionsAwareSwapper.INSTANCE);
  }

  @Override
  protected void onInit() {
    super.onInit();

    if (!dimensions.scene.isEmpty()) {
      initializer.init();
      arrays.createBuffers();
    } else {
      setDirty();
    }
  }

  @Override
  protected void onInitGl(@Nonnull GL11 gl, @Nonnull MeshConfig config) {
    super.onInitGl(gl, config);

    setVertices(arrays.getVerticesBuffer());
    setIndices(arrays.getIndicesBuffer(), IndicesOrder.LINES);
  }

  @Nonnull
  @Override
  protected BaseMesh makeCopy() {
    return new Axis(direction, dimensions);
  }

  @Override
  public void setDimensions(@Nonnull Dimensions dimensions) {
    // todo serso: might be called on GL thread, requires synchronization
    if (!this.dimensions.equals(dimensions)) {
      this.dimensions = dimensions;
      setDirty();
    }
  }

  @Nonnull
  @Override
  public Dimensions getDimensions() {
    return this.dimensions;
  }

  private class ArrayInitializer {

    private Scene.Axis axis;
    private Scene.Ticks ticks;

    public void init() {
      final boolean y = direction == Direction.Y;
      axis = Scene.Axis.create(dimensions.scene, y);
      ticks = Scene.Ticks.create(dimensions.graph, axis);
      arrays.init(3 * (2 + 2 + 2 * ticks.count), 2 + 2 * 2 + 2 * ticks.count);

      initLine();
      initArrow();
      initTicks();
    }

    private void initTicks() {
      final int[] dv = direction.vector;
      final int[] da = direction.arrow;
      float x = -dv[0] * (ticks.axisLength / 2 + ticks.step) + da[0] * ticks.width / 2;
      float y = -dv[1] * (ticks.axisLength / 2 + ticks.step) + da[1] * ticks.width / 2;
      float z = -dv[2] * (ticks.axisLength / 2 + ticks.step) + da[2] * ticks.width / 2;
      for (int i = 0; i < ticks.count; i++) {
        x += dv[0] * ticks.step;
        y += dv[1] * ticks.step;
        z += dv[2] * ticks.step;

        arrays.add(arrays.vertex / 3, x, y, z);
        arrays.add(arrays.vertex / 3, x - da[0] * ticks.width, y - da[1] * ticks.width, z - da[2] * ticks.width);
      }
    }

    private void initArrow() {
      final int[] dv = direction.vector;
      final int[] da = direction.arrow;
      arrays.add(0,
          arrays.vertices[0] - dv[0] * axis.arrowLength - da[0] * axis.arrowWidth / 2,
          arrays.vertices[1] - dv[1] * axis.arrowLength - da[1] * axis.arrowWidth / 2,
          arrays.vertices[2] - dv[2] * axis.arrowLength - da[2] * axis.arrowWidth / 2);
      arrays.indices[arrays.index++] = 2;

      arrays.add(0,
          arrays.vertices[0] - dv[0] * axis.arrowLength + da[0] * axis.arrowWidth / 2,
          arrays.vertices[1] - dv[1] * axis.arrowLength + da[1] * axis.arrowWidth / 2,
          arrays.vertices[2] - dv[2] * axis.arrowLength + da[2] * axis.arrowWidth / 2);
      arrays.indices[arrays.index++] = 3;
    }

    private void initLine() {
      arrays.add(0,
          direction.vector[0] * axis.length / 2,
          direction.vector[1] * axis.length / 2,
          direction.vector[2] * axis.length / 2);

      arrays.add(1,
          -arrays.vertices[0],
          -arrays.vertices[1],
          -arrays.vertices[2]);
    }
  }

  @Override
  public String toString() {
    return "Axis{" +
        "direction=" + direction +
        '}';
  }
}




Java Source Code List

com.android.texample.GLText.java
com.android.texample.SpriteBatch.java
com.android.texample.TexampleRenderer.java
com.android.texample.TextureRegion.java
com.android.texample.Vertices.java
org.solovyev.android.plotter.Angle.java
org.solovyev.android.plotter.AxisStyle.java
org.solovyev.android.plotter.Check.java
org.solovyev.android.plotter.Color.java
org.solovyev.android.plotter.DefaultPlotter.java
org.solovyev.android.plotter.Dimensions.java
org.solovyev.android.plotter.Frustum.java
org.solovyev.android.plotter.Function0.java
org.solovyev.android.plotter.Function1.java
org.solovyev.android.plotter.Function2.java
org.solovyev.android.plotter.Function.java
org.solovyev.android.plotter.LineStyle.java
org.solovyev.android.plotter.MeshConfig.java
org.solovyev.android.plotter.MultisampleConfigChooser.java
org.solovyev.android.plotter.PinchZoomTracker.java
org.solovyev.android.plotter.PlotData.java
org.solovyev.android.plotter.PlotFunction.java
org.solovyev.android.plotter.PlotRenderer.java
org.solovyev.android.plotter.PlotView.java
org.solovyev.android.plotter.Plot.java
org.solovyev.android.plotter.Plotter.java
org.solovyev.android.plotter.PlottingView.java
org.solovyev.android.plotter.Spf.java
org.solovyev.android.plotter.SuperFunction.java
org.solovyev.android.plotter.TouchHandler.java
org.solovyev.android.plotter.ZoomLevels.java
org.solovyev.android.plotter.Zoomer.java
org.solovyev.android.plotter.app.MainActivity.java
org.solovyev.android.plotter.app.PlotterApplication.java
org.solovyev.android.plotter.meshes.Arrays.java
org.solovyev.android.plotter.meshes.AxisGrid.java
org.solovyev.android.plotter.meshes.Axis.java
org.solovyev.android.plotter.meshes.BaseCube.java
org.solovyev.android.plotter.meshes.BaseCurve.java
org.solovyev.android.plotter.meshes.BaseMesh.java
org.solovyev.android.plotter.meshes.BaseSurface.java
org.solovyev.android.plotter.meshes.DimensionsAwareSwapper.java
org.solovyev.android.plotter.meshes.DimensionsAware.java
org.solovyev.android.plotter.meshes.DoubleBufferGroup.java
org.solovyev.android.plotter.meshes.DoubleBufferMesh.java
org.solovyev.android.plotter.meshes.FunctionGraph2d.java
org.solovyev.android.plotter.meshes.FunctionGraph3d.java
org.solovyev.android.plotter.meshes.FunctionGraphSwapper.java
org.solovyev.android.plotter.meshes.FunctionGraph.java
org.solovyev.android.plotter.meshes.Graph.java
org.solovyev.android.plotter.meshes.Group.java
org.solovyev.android.plotter.meshes.IndicesOrder.java
org.solovyev.android.plotter.meshes.ListGroup.java
org.solovyev.android.plotter.meshes.ListPool.java
org.solovyev.android.plotter.meshes.Mesh.java
org.solovyev.android.plotter.meshes.Meshes.java
org.solovyev.android.plotter.meshes.Pool.java
org.solovyev.android.plotter.meshes.Scene.java
org.solovyev.android.plotter.meshes.SolidCube.java
org.solovyev.android.plotter.meshes.SurfaceInitializer.java
org.solovyev.android.plotter.meshes.WireFrameCube.java