Search in sources :

Example 1 with Line3D

use of org.rajawali3d.primitives.Line3D in project Rajawali by Rajawali.

the class LoaderGCode method readGCode.

/**
	 * @param buffer
	 * @return
	 * @throws IOException
	 */
protected Object3D readGCode(BufferedInputStream buffer) throws IOException {
    RajLog.i("GCodePaser: reading file");
    Object3D ret = new Object3D();
    String codeLine;
    // Read the facet
    SupportedCommands cmd = null;
    GCodeLine motion = null, prevMotion = null;
    GCodeLayer currentLayer = new GCodeLayer();
    float lastZPos = 0, lastExtrudeZ = 0, x, y;
    @SuppressWarnings("unused") float // default for millis
    units = 1F;
    boolean relative = false;
    prevMotion = new GCodeLine();
    prevMotion.setX(0.0f);
    prevMotion.setY(0.0f);
    prevMotion.setZ(0.0f);
    prevMotion.setE(0.0f);
    prevMotion.setF(0.0f);
    StringBuilder lnBuilder = new StringBuilder(1024);
    int lineNum = 0;
    while (buffer.available() > 0) {
        byte b = (byte) buffer.read();
        if (b != '\n') {
            lnBuilder.append((char) b);
            continue;
        } else {
            codeLine = lnBuilder.toString();
            lnBuilder.delete(0, lnBuilder.length());
        }
        // while ((codeline = buffer.readLine()) != null) {
        if (codeLine.startsWith(";")) {
            continue;
        }
        String[] tokens = codeLine.split(" ");
        if (null == tokens || tokens.length == 0) {
            continue;
        }
        try {
            cmd = SupportedCommands.fromString(tokens[0]);
        } catch (IllegalArgumentException e) {
            RajLog.w("encountered unsupported gcode:" + tokens[0]);
            continue;
        }
        switch(cmd) {
            case G1:
                motion = new GCodeLine(codeLine);
                motion.setOrigin(prevMotion);
                // TODO color lines based on travel and speed
                if (motion.getX() == 0xFFFFFFFF && motion.getY() == 0xFFFFFFFF && motion.getZ() == 0xFFFFFFFF) {
                    // extrusion or speed setting
                    continue;
                }
                x = motion.getX() == 0xFFFFFFFF ? 0 : motion.getX();
                y = motion.getY() == 0xFFFFFFFF ? 0 : motion.getY();
                if (motion.getZ() != 0xFFFFFFFF) {
                    lastZPos = motion.getZ();
                    if (x == 0 && y == 0) {
                        continue;
                    }
                }
                currentLayer.getPoints().add(new Vector3(x, y, lastZPos));
                if (motion.getE() != 0xFFFFFFFF) {
                    float delta;
                    if (relative) {
                        delta = motion.getE() - prevMotion.getE();
                    } else {
                        delta = motion.getE();
                        if (delta == 0xFFFFFFFF) {
                            delta = 0;
                        }
                    }
                    if (delta > 0) {
                        // check if extruding at new Z
                        if (lastZPos > lastExtrudeZ) {
                            // || layers.empty()
                            lastExtrudeZ = lastZPos;
                            ret.addChild(new Line3D(currentLayer.getPoints(), 1f, Color.argb(255, 0x55, 0x11, 0xEF)));
                            currentLayer = new GCodeLayer();
                        }
                    }
                }
                prevMotion = motion;
                break;
            case G21:
                // G21: Set Units to Millimeters
                // Example: G21
                units = 1F;
                break;
            case G90:
                // G90: Set to Absolute Positioning
                // Example: G90
                relative = false;
                break;
            case G91:
                // G91: Set to Relative Positioning
                // Example: G91
                relative = true;
                break;
            case G92:
                // G92: Set Position
                // Example: G92 E0
                motion = new GCodeLine(codeLine);
                if (motion.getX() != -0xFFFFFFFF) {
                    prevMotion.setX(motion.getX());
                }
                if (motion.getY() != -0xFFFFFFFF) {
                    prevMotion.setY(motion.getY());
                }
                if (motion.getZ() != -0xFFFFFFFF) {
                    prevMotion.setZ(motion.getZ());
                }
                if (motion.getE() != -0xFFFFFFFF) {
                    prevMotion.setE(motion.getE());
                }
                break;
            case M82:
                // no-op, so long as M83 is not supported.
                break;
            case M84:
                // no-op
                break;
        }
    // RajLog.d("gcode parser parsed line #" + lineNum++);
    }
    return ret;
}
Also used : Vector3(org.rajawali3d.math.vector.Vector3) Line3D(org.rajawali3d.primitives.Line3D) Object3D(org.rajawali3d.Object3D)

Example 2 with Line3D

use of org.rajawali3d.primitives.Line3D in project Rajawali by Rajawali.

the class DebugLight method createLines.

private void createLines() {
    Stack<Vector3> points = new Stack<>();
    float segmentSize = 10;
    float radius = .2f;
    int count = 0;
    for (int i = 0; i < 360; i += segmentSize) {
        if (count++ % 2 == 0)
            continue;
        float radians1 = (float) MathUtil.degreesToRadians(i);
        float radians2 = (float) MathUtil.degreesToRadians(i + segmentSize);
        Vector3 p1 = new Vector3();
        p1.x = Math.cos(radians1) * radius;
        p1.y = Math.sin(radians1) * radius;
        Vector3 p2 = new Vector3();
        p2.x = Math.cos(radians2) * radius;
        p2.y = Math.sin(radians2) * radius;
        points.add(p1);
        points.add(p2);
    }
    Material material = new Material();
    mCircle = new Line3D(points, mLineThickness, mColor);
    mCircle.setMaterial(material);
    mCircle.setDrawingMode(GLES20.GL_LINES);
    mCircle.enableLookAt();
    addChild(mCircle);
    points = new Stack<>();
    if (mLight.getLightType() == mLight.DIRECTIONAL_LIGHT || mLight.getLightType() == mLight.SPOT_LIGHT) {
        for (int i = 0; i < 20; i += 2) {
            Vector3 p1 = new Vector3();
            p1.z = i * 0.5f;
            Vector3 p2 = new Vector3();
            p2.z = (i + 1) * 0.5f;
            points.add(p1);
            points.add(p2);
        }
        mLine = new Line3D(points, mLineThickness, mColor);
        mLine.setMaterial(material);
        mLine.setDrawingMode(GLES20.GL_LINES);
        mLine.enableLookAt();
        addChild(mLine);
    }
}
Also used : Vector3(org.rajawali3d.math.vector.Vector3) Material(org.rajawali3d.materials.Material) Line3D(org.rajawali3d.primitives.Line3D) Stack(java.util.Stack)

Aggregations

Vector3 (org.rajawali3d.math.vector.Vector3)2 Line3D (org.rajawali3d.primitives.Line3D)2 Stack (java.util.Stack)1 Object3D (org.rajawali3d.Object3D)1 Material (org.rajawali3d.materials.Material)1