Android Open Source - android-plotter Base Curve






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. j ava  2s . c om
import org.solovyev.android.plotter.Check;
import org.solovyev.android.plotter.Dimensions;
import org.solovyev.android.plotter.MeshConfig;

import javax.annotation.Nonnull;
import javax.microedition.khronos.opengles.GL11;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

public abstract class BaseCurve extends BaseMesh implements DimensionsAware {

  @Nonnull
  protected volatile Dimensions dimensions;

  // create on the background thread and accessed from GL thread
  private volatile FloatBuffer verticesBuffer;
  private volatile ShortBuffer indicesBuffer;

  @Nonnull
  private final Graph graph = Graph.create();

  protected BaseCurve(float width, float height) {
    this(makeDimensions(width, height));
  }

  @Nonnull
  private static Dimensions makeDimensions(float width, float height) {
    final Dimensions dimensions = new Dimensions();
    dimensions.graph.set(width, height);
    return dimensions;
  }

  protected BaseCurve(@Nonnull Dimensions dimensions) {
    this.dimensions = 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();
    }
  }

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

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

    if (!dimensions.isEmpty()) {
      fillGraph(graph);
      verticesBuffer = Meshes.allocateOrPutBuffer(graph.vertices, graph.start, graph.length(), verticesBuffer);
      indicesBuffer = Meshes.allocateOrPutBuffer(graph.getIndices(), 0, graph.getIndicesCount(), indicesBuffer);
    } else {
      setDirty();
    }
  }

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

    Check.isNotNull(verticesBuffer);

    setVertices(verticesBuffer);
    setIndices(indicesBuffer, IndicesOrder.LINE_STRIP);
  }

  void fillGraph(@Nonnull Graph graph) {
    final float add = 0;//dimensions.graph.width;
    final float newXMin = dimensions.graph.rect.left - add;
    final float newXMax = dimensions.graph.rect.right + 2 * add;

    // prepare graph
    if (false && !graph.isEmpty()) {
      if (newXMin >= graph.xMin()) {
        // |------[---erased---|------data----|---erased--]------ old data
        // |-------------------[------data----]------------------ new data
        //                    xMin           xMax
        //
        // OR
        //
        // |------[---erased---|------data----]----------- old data
        // |-------------------[------data----<---->]----- new data
        //                    xMin                 xMax
        graph.moveStartTo(newXMin);
        if (newXMax <= graph.xMax()) {
          graph.moveEndTo(newXMax);
          // nothing to compute
        }
      } else {
        // |--------------------[-----data----|---erased----]-- old data
        // |------[<------------>-----data----]---------------- new data
        //       xMin                        xMax
        //
        // OR
        //
        // |--------------------[------data--]----|----------- old data
        // |-------[<----------->------data--<--->]-----------new data
        //        xMin                           xMax

        if (newXMax <= graph.xMax()) {
          graph.moveEndTo(newXMax);
        }
      }
    }

    compute(newXMin, newXMax, graph);
  }

  protected abstract float y(float x);

  void compute(final float newXMin,
         final float newXMax,
         @Nonnull Graph graph) {
    float x;
    final Dimensions.Graph g = dimensions.graph;

    final float density = dimensions.scene.view.width() / 10f;
    final float step = Math.abs((newXMax - newXMin) / density);
    final float ratio = graph.accuracy / step;
    if (true) {
      graph.accuracy = step;
      graph.clear();
    }

    final float xMin;
    final float xMax;
    if (!graph.isEmpty()) {
      xMin = graph.xMin();
      xMax = graph.xMax();
    } else {
      xMin = newXMin;
      xMax = newXMin;
    }

    x = graph.isEmpty() ? xMin : xMin - step;
    while (x > newXMin) {
      graph.prepend(g.toScreenX(x), g.toScreenY(y(x)));
      x -= step;
    }

    x = graph.isEmpty() ? xMax : xMax + step;
    while (x < newXMax) {
      graph.append(g.toScreenX(x), g.toScreenY(y(x)));
      x += step;
    }
  }

}




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