Android Open Source - TinyVoxel Binvox Importer






From Project

Back to project page TinyVoxel.

License

The source code is released under:

GNU General Public License

If you think the Android project TinyVoxel 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 com.toet.TinyVoxel.Importer;
//from  w ww.j a v a2  s  . c  om
/**
 * Created by Kajos on 8/7/2014.
 */
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector3;
import com.toet.TinyVoxel.Config;
import com.toet.TinyVoxel.Debug.LogHandler;
import com.toet.TinyVoxel.Renderer.Bundles.Bundle;

import java.io.IOException;
import java.io.InputStream;

public class BinvoxImporter {
    private static byte[] voxels;
    private static int depth, height, width;
    private static int size;

    private static boolean readBinvox(InputStream inputStream) throws Exception
    {
        DataInputStream binvox_data = new DataInputStream(inputStream);

        //
        // read header
        //
        String line = binvox_data.readLine();  // deprecated function though
        if (!line.startsWith("#binvox")) {
            LogHandler.log("Error: first line reads [" + line + "] instead of [#binvox]");
            return false;
        }

        String version_string = line.substring(8);
        int version = Integer.parseInt(version_string);
        LogHandler.log("reading binvox version " + version);

        depth = height = width = 0;
        boolean done = false;

        while(!done) {

            line = binvox_data.readLine();

            if (line.startsWith("data")) done = true;
            else {
                if (line.startsWith("dim")) {
                    String[] dimensions = line.split(" ");
                    depth = Integer.parseInt(dimensions[1]);
                    height = Integer.parseInt(dimensions[2]);
                    width = Integer.parseInt(dimensions[3]);
                }
                else {
                    if (line.startsWith("translate")) {
                        // tx = binvox_data.readDouble();
                        // ty = binvox_data.readDouble();
                        // tz = binvox_data.readDouble();
                    }
                    else {
                        if (line.startsWith("scale")) {
                            // scale = binvox_data.readDouble();
                        }
                        else {
                            LogHandler.log("  unrecognized keyword [" + line + "], skipping");
                        }
                    }
                }
            }
        }  // while

        if (!done) {
            LogHandler.log("  error reading header");
            return false;
        }
        if (depth == 0) {
            LogHandler.log("  missing dimensions in header");
            return false;
        }

        size = width * height * depth;
        voxels = new byte[size];

        //
        // read voxel data
        //
        byte value;
        int count;
        int index = 0;
        int end_index = 0;
        int nr_voxels = 0;

        // *input >> value;  // read the linefeed char

        while(end_index < size) {

            value = binvox_data.readByte();
            // idiotic Java language doesn't have unsigned types, so we have to use an int for 'count'
            // and make sure that we don't interpret it as a negative number if bit 7 (the sign bit) is on
            count = binvox_data.readByte() & 0xff;

            end_index = index + count;
            if (end_index > size) return false;
            for(int i = index; i < end_index; i++) voxels[i] = value;

            if (value > 0) nr_voxels += count;
            index = end_index;

        }  // while

        LogHandler.log("  read " + nr_voxels + " voxels");
        return true;

    }  // read_binvox

    public static boolean readBinvox(String filespec, final Bundle gridBundle, final Vector3 endPos, final int r, final int g, final int b)
    {
        LogHandler.log("File: " + filespec);

        InputStream in = null;
        try {
            FileHandle file = Gdx.files.internal(filespec);
            if (!file.exists()) {
                LogHandler.log("File doesn't exist.");
                return false;
            }

            in = file.read();

            readBinvox(in);

            LogHandler.log("Read " + size + " voxels");

            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    Vector3 tmp = new Vector3();
                    for (int z = 0; z < depth; z++) {
                        int i = z * width * height + y * width + x;
                        tmp.set(x, y, z);
                        tmp.scl(1f / (float) Config.TINY_GRID_SIZE);
                        tmp.add(endPos);
                        if (voxels[i] > 0) {
                            gridBundle.addVoxel(tmp, r, g, b, false);
                        }
                    }
                }
            }

            LogHandler.log("Printed voxels");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try
            {
                if (in != null)
                    in.close();
            }
            catch (IOException e)
            {
                return false;
            }
        }


        return true;
    }
}




Java Source Code List

com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration.java
com.badlogic.gdx.backends.gwt.GwtApplication.java
com.badlogic.gdx.backends.gwt.GwtGL20.java
com.badlogic.gdx.backends.gwt.GwtInput.java
com.badlogic.gdx.backends.gwt.GwtNet.java
com.badlogic.gdx.graphics.Pixmap.java
com.toet.TinyVoxel.Config.java
com.toet.TinyVoxel.Game.java
com.toet.TinyVoxel.IOSConfig.java
com.toet.TinyVoxel.IOSLauncher.java
com.toet.TinyVoxel.OuyaController.java
com.toet.TinyVoxel.Time.java
com.toet.TinyVoxel.Character.Character.java
com.toet.TinyVoxel.Debug.LogHandler.java
com.toet.TinyVoxel.GameControllers.CharacterController.java
com.toet.TinyVoxel.GameControllers.CustomTouchPad.java
com.toet.TinyVoxel.GameControllers.KeyBoardController.java
com.toet.TinyVoxel.GameControllers.TouchPadController.java
com.toet.TinyVoxel.Importer.BinvoxImporter.java
com.toet.TinyVoxel.Importer.DataInputStream.java
com.toet.TinyVoxel.Importer.MeshImporter.java
com.toet.TinyVoxel.Renderer.BlockBuilder.java
com.toet.TinyVoxel.Renderer.Floor.java
com.toet.TinyVoxel.Renderer.Manager.java
com.toet.TinyVoxel.Renderer.Bundles.ArrayBundle.java
com.toet.TinyVoxel.Renderer.Bundles.Bundle.java
com.toet.TinyVoxel.Renderer.Bundles.GridBundle.java
com.toet.TinyVoxel.Renderer.Bundles.GridInterface.java
com.toet.TinyVoxel.Renderer.Bundles.Grid.java
com.toet.TinyVoxel.Renderer.Bundles.GroundBundle.java
com.toet.TinyVoxel.Renderer.Bundles.SingleBundle.java
com.toet.TinyVoxel.Renderer.Bundles.TinyGrid.java
com.toet.TinyVoxel.Renderer.Tools.BrushUtils.java
com.toet.TinyVoxel.Renderer.Tools.GridUtils.java
com.toet.TinyVoxel.Renderer.Wrapped.WrappedBoolean.java
com.toet.TinyVoxel.Renderer.Wrapped.WrappedInteger.java
com.toet.TinyVoxel.Screens.GUI.java
com.toet.TinyVoxel.Screens.Menu.java
com.toet.TinyVoxel.Shaders.ShaderManager.java
com.toet.TinyVoxel.Shadow.ShadowManager.java
com.toet.TinyVoxel.Util.Box.java
com.toet.TinyVoxel.Util.FullscreenQuad.java
com.toet.TinyVoxel.Util.JobManager.java
com.toet.TinyVoxel.Util.NonBackedTexture.java
com.toet.TinyVoxel.Util.Position.java
com.toet.TinyVoxel.Util.RLEInputStream.java
com.toet.TinyVoxel.Util.RLEOutputStream.java
com.toet.TinyVoxel.Util.SimpleMath.java
com.toet.TinyVoxel.Util.StreamUtil.java
com.toet.TinyVoxel.android.AndroidConfig.java
com.toet.TinyVoxel.android.AndroidConfig.java
com.toet.TinyVoxel.android.AndroidLauncher.java
com.toet.TinyVoxel.android.AndroidLauncher.java
com.toet.TinyVoxel.client.GwtConfig.java
com.toet.TinyVoxel.client.HtmlLauncher.java
com.toet.TinyVoxel.desktop.DesktopConfig.java
com.toet.TinyVoxel.desktop.DesktopLauncher.java