Android Open Source - android-plotter Meshes






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  ww  . j  a  v  a 2s . c o  m
import org.solovyev.android.plotter.Check;
import org.solovyev.android.plotter.Plot;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

public final class Meshes {

  public static final int BYTES_IN_FLOAT = 4;
  public static final int BYTES_IN_SHORT = 2;

  private Meshes() {
  }

  @Nonnull
  static String getTag() {
    return Plot.getTag("Meshes");
  }

  @Nonnull
  static String getTag(@Nonnull String tag) {
    return getTag() + "/" + tag;
  }

  @Nonnull
  public static FloatBuffer allocateBuffer(float[] array) {
    return allocateBuffer(array, 0, array.length);
  }

  @Nonnull
  public static FloatBuffer allocateBuffer(float[] array, int start, int length) {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(length * BYTES_IN_FLOAT);
    buffer.order(ByteOrder.nativeOrder());
    final FloatBuffer floatBuffer = buffer.asFloatBuffer();
    return putBuffer(array, start, length, floatBuffer);
  }

  @Nonnull
  public static FloatBuffer putBuffer(float[] array, int start, int length, @Nonnull FloatBuffer to) {
    if (to.capacity() != length) {
      throw new IllegalArgumentException("Arrays should have save size");
    }
    to.position(0);
    to.put(array, start, length);
    to.position(0);
    return to;
  }

  @Nonnull
  static FloatBuffer allocateOrPutBuffer(@Nonnull float[] array, int start, int length, @Nullable FloatBuffer buffer) {
    FloatBuffer newBuffer;
    if (buffer != null && buffer.capacity() == length) {
      newBuffer = putBuffer(array, start, length, buffer);
    } else {
      newBuffer = allocateBuffer(array, start, length);
    }
    return newBuffer;
  }

  @Nonnull
  static FloatBuffer allocateOrPutBuffer(@Nonnull float[] array, @Nullable FloatBuffer buffer) {
    return allocateOrPutBuffer(array, 0, array.length, buffer);
  }

  @Nonnull
  public static ShortBuffer allocateBuffer(short[] array, int start, int length) {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(length * BYTES_IN_SHORT);
    buffer.order(ByteOrder.nativeOrder());
    final ShortBuffer shortBuffer = buffer.asShortBuffer();
    return putBuffer(array, start, length, shortBuffer);
  }

  @Nonnull
  public static ShortBuffer putBuffer(short[] array, int start, int length, @Nonnull ShortBuffer to) {
    if (to.capacity() != length) {
      throw new IllegalArgumentException("Arrays should have save size");
    }
    to.position(0);
    to.put(array, start, length);
    to.position(0);
    return to;
  }

  @Nonnull
  static ShortBuffer allocateOrPutBuffer(@Nonnull short[] array, @Nullable ShortBuffer buffer) {
    return allocateOrPutBuffer(array, 0, array.length, buffer);
  }

  @Nonnull
  static ShortBuffer allocateOrPutBuffer(@Nonnull short[] array, int start, int length, @Nullable ShortBuffer buffer) {
    ShortBuffer newBuffer;
    if (buffer != null && buffer.capacity() == length) {
      newBuffer = putBuffer(array, start, length, buffer);
    } else {
      newBuffer = allocateBuffer(array, start, length);
    }
    return newBuffer;
  }

  static float getTickStep(float width, int ticks) {
    Check.isTrue(width > 0f, "Width must be positive");
    final float rawTickStep = width / (ticks - 1);
    final int power = getPower(rawTickStep);

    final float tickStepUp = (float) Math.pow(10f, power + 1) / 2;
    final float tickStepMiddle = (float) Math.pow(10f, power);
    final float tickStepDown = tickStepMiddle / 2;

    final float diffDown = Math.abs(tickStepDown - rawTickStep);
    final float diffMiddle = Math.abs(tickStepMiddle - rawTickStep);
    final float diffUp = Math.abs(tickStepUp - rawTickStep);

    if (diffUp < diffMiddle && diffUp < diffDown) {
      return tickStepUp;
    }

    if (diffDown < diffMiddle && diffDown < diffUp) {
      return tickStepDown;
    }

    return tickStepMiddle;
  }

  static int getPower(float value) {
    float i = 1;
    int power = 0;
    if (value > 1) {
      while (i < value) {
        i *= 10f;
        power++;
      }
    } else {
      while (i > value) {
        i /= 10f;
        power--;
      }
    }
    return power;
  }
}




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