Java Code Examples for javax.vecmath.Vector3f#length()

The following examples show how to use javax.vecmath.Vector3f#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Stroke.java    From justaline-android with Apache License 2.0 5 votes vote down vote up
public void finishStroke() {
    finished = true;

    // Calculate total distance traveled
    float dist = 0;

    Vector3f d = new Vector3f();
    for (int i = 0; i < points.size() - 1; i++) {
        d.sub(points.get(i), points.get(i + 1));
        dist += d.length();
    }

    // If line is very short, overwrite it
    if (dist < 0.01) {
        if (points.size() > 2) {
            Vector3f p1 = points.get(0);
            Vector3f p2 = points.get(points.size() - 1);

            points.clear();
            points.add(p1);
            points.add(p2);
        } else if (points.size() == 1) {
            Vector3f v = new Vector3f(points.get(0));
            v.y += 0.0005;
            points.add(v);
        }
    }
}
 
Example 2
Source File: Stroke.java    From justaline-android with Apache License 2.0 5 votes vote down vote up
private float calculateDistance(int index1, int index2) {
    Vector3f p1 = points.get(index1);
    Vector3f p2 = points.get(index2);
    Vector3f n1 = new Vector3f();
    n1.sub(p2, p1);
    return n1.length();
}
 
Example 3
Source File: Stroke.java    From justaline-android with Apache License 2.0 5 votes vote down vote up
public void calculateTotalLength() {
    totalLength = 0;
    for (int i = 1; i < points.size(); i++) {
        Vector3f dist = new Vector3f(points.get(i));
        dist.sub(points.get(i - 1));
        totalLength += dist.length();
    }

}
 
Example 4
Source File: LineUtils.java    From ar-drawing-java with Apache License 2.0 5 votes vote down vote up
/**
 * @param newPoint
 * @param lastPoint
 * @return
 */
public static boolean distanceCheck(Vector3f newPoint, Vector3f lastPoint) {
    Vector3f temp = new Vector3f();
    temp.sub(newPoint, lastPoint);
    if (temp.length() > AppSettings.getMinDistance()) {
        return true;
    }
    return false;
}
 
Example 5
Source File: LineShaderRenderer.java    From justaline-android with Apache License 2.0 4 votes vote down vote up
/**
     * AddLine takes in the 3D positions adds to the buffers to create the stroke and the degenerate
     * faces needed so the lines render properly.
     */
    private int addLine(Stroke line, int offset) {
        if (line == null || line.size() < 2)
            return offset;


        int lineSize = line.size();

        float mLineWidthMax = mLineWidth = line.getLineWidth();

        float length = 0;
        float totalLength;
        int ii = offset;

        if (line.localLine) {
            totalLength = line.totalLength;
        } else {
            totalLength = line.animatedLength;
        }

        for (int i = 0; i < lineSize; i++) {

            int iGood = i;
            if (iGood >= lineSize) iGood = lineSize - 1;

            int i_m_1 = (iGood - 1) < 0 ? iGood : iGood - 1;
            int i_p_1 = (iGood + 1) > (lineSize - 1) ? iGood : iGood + 1;

            Vector3f current = line.get(iGood);
            Vector3f previous = line.get(i_m_1);
            Vector3f next = line.get(i_p_1);

            Vector3f dist = new Vector3f(current);
            dist.sub(previous);
            length += dist.length();


//            if (i < line.mTapperPoints) {
//                mLineWidth = mLineWidthMax * line.mTaperLookup[i];
//            } else if (i > lineSize - line.mTapperPoints) {
//                mLineWidth = mLineWidthMax * line.mTaperLookup[lineSize - i];
//            } else {
            mLineWidth = line.getLineWidth();
//            }


            mLineWidth = Math.max(0, Math.min(mLineWidthMax, mLineWidth));


            if (i == 0) {
                setMemory(ii++, current, previous, next, mLineWidth, 1f, length, totalLength);
            }

            setMemory(ii++, current, previous, next, mLineWidth, 1f, length, totalLength);
            setMemory(ii++, current, previous, next, mLineWidth, -1f, length, totalLength);

            if (i == lineSize - 1) {
                setMemory(ii++, current, previous, next, mLineWidth, -1f, length, totalLength);
            }


        }
        return ii;
    }
 
Example 6
Source File: Stroke.java    From justaline-android with Apache License 2.0 4 votes vote down vote up
public void add(Vector3f point) {
        int s = points.size();

        if (s == 0) {
            // Prepare the biquad filter
            biquadFilter = new BiquadFilter(AppSettings.getSmoothing(), 3);
            for (int i = 0; i < AppSettings.getSmoothingCount(); i++) {
                biquadFilter.update(point);
            }
        }

        // Filter the point
        point = biquadFilter.update(point);

        // Check distance, and only add if moved far enough
        if (s > 0) {
            Vector3f lastPoint = points.get(s - 1);

            Vector3f temp = new Vector3f();
            temp.sub(point, lastPoint);

            if (temp.length() < lineWidth / 10) {
                return;
            }
        }

        // Add the point
        points.add(point);

        // Cleanup vertices that are redundant
        if (s > 3) {
            float angle = calculateAngle(s - 2);
            // Remove points that have very low angle change
            if (angle < 0.05) {
                points.remove(s - 2);
            } else {
                subdivideSection(s - 3, 0.3f, 0);
            }
        }

        // Cleanup beginning, remove points that are close to each other
        // This makes the end seem straing
//        if(s < 5 && s > 2){
//            float dist = calculateDistance(0, s-1);
//            if(dist < 0.005) {
//                for (int i = 0; i < s - 2; i++) {
//                    if (calculateDistance(i, i + 1) < 0.005) {
//                        points.remove(i + 1);
//                        startCap.clear();
//                    }
//                }
//            }
//        }

        calculateTotalLength();
    }
 
Example 7
Source File: DrawAR.java    From ar-drawing-java with Apache License 2.0 4 votes vote down vote up
/**
 * update() is executed on the GL Thread.
 * The method handles all operations that need to take place before drawing to the screen.
 * The method :
 * extracts the current projection matrix and view matrix from the AR Pose
 * handles adding stroke and points to the data collections
 * updates the ZeroMatrix and performs the matrix multiplication needed to re-center the drawing
 * updates the Line Renderer with the current strokes, color, distance scale, line width etc
 */
private void update() {

    if (mSession == null) {
        return;
    }

    mDisplayRotationHelper.updateSessionIfNeeded(mSession);

    try {

        mSession.setCameraTextureName(mBackgroundRenderer.getTextureId());

        mFrame = mSession.update();
        Camera camera = mFrame.getCamera();

        mState = camera.getTrackingState();

        // Update tracking states
        if (mState == TrackingState.TRACKING && !bIsTracking.get()) {
            bIsTracking.set(true);
        } else if (mState== TrackingState.STOPPED && bIsTracking.get()) {
            bIsTracking.set(false);
            bTouchDown.set(false);
        }

        // Get projection matrix.
        camera.getProjectionMatrix(projmtx, 0, AppSettings.getNearClip(), AppSettings.getFarClip());
        camera.getViewMatrix(viewmtx, 0);

        float[] position = new float[3];
        camera.getPose().getTranslation(position, 0);

        // Check if camera has moved much, if thats the case, stop touchDown events
        // (stop drawing lines abruptly through the air)
        if (mLastFramePosition != null) {
            Vector3f distance = new Vector3f(position[0], position[1], position[2]);
            distance.sub(new Vector3f(mLastFramePosition[0], mLastFramePosition[1], mLastFramePosition[2]));

            if (distance.length() > 0.15) {
                bTouchDown.set(false);
            }
        }
        mLastFramePosition = position;

        // Multiply the zero matrix
        Matrix.multiplyMM(viewmtx, 0, viewmtx, 0, mZeroMatrix, 0);


        if (bNewStroke.get()) {
            bNewStroke.set(false);
            addStroke(lastTouch.get());
            mLineShaderRenderer.bNeedsUpdate.set(true);
        } else if (bTouchDown.get()) {
            addPoint(lastTouch.get());
            mLineShaderRenderer.bNeedsUpdate.set(true);
        }

        if (bReCenterView.get()) {
            bReCenterView.set(false);
            mZeroMatrix = getCalibrationMatrix();
        }

        if (bClearDrawing.get()) {
            bClearDrawing.set(false);
            clearDrawing();
            mLineShaderRenderer.bNeedsUpdate.set(true);
        }

        if (bUndo.get()) {
            bUndo.set(false);
            if (mStrokes.size() > 0) {
                mStrokes.remove(mStrokes.size() - 1);
                mLineShaderRenderer.bNeedsUpdate.set(true);
            }
        }
        mLineShaderRenderer.setDrawDebug(bLineParameters.get());
        if (mLineShaderRenderer.bNeedsUpdate.get()) {
            mLineShaderRenderer.setColor(AppSettings.getColor());
            mLineShaderRenderer.mDrawDistance = AppSettings.getStrokeDrawDistance();
            mLineShaderRenderer.setDistanceScale(mDistanceScale);
            mLineShaderRenderer.setLineWidth(mLineWidthMax);
            mLineShaderRenderer.clear();
            mLineShaderRenderer.updateStrokes(mStrokes);
            mLineShaderRenderer.upload();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}