Java Code Examples for processing.core.PMatrix3D#invert()

The following examples show how to use processing.core.PMatrix3D#invert() . 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: ExtrinsicCalibrator.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void calibrateDepthToExternalExtr(ArrayList<ExtrinsicSnapshot> snapshots) {
    // Depth -> color  extrinsics
    PMatrix3D kinectExtr = depthCameraDevice.getStereoCalibration().get();

    // color -> depth  extrinsics
    kinectExtr.invert();

    // depth -> tracking
    PMatrix3D kinectCameraExtr = computeKinectCamExtrinsics(snapshots, kinectExtr);

    // // tracking -> depth
    kinectCameraExtr.invert();

    this.kinectCameraExtrinsics.set(kinectCameraExtr);
    saveKinectCameraExtrinsics(kinectCameraExtr);
}
 
Example 2
Source File: PaperScreen.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void drawOnTable() {
        if (!isDrawingOnDisplay) {
            return;
        }

//        PMatrix3D location = this.getLocation().get();
//        PMatrix3D t = tableInv.get();
//        t.apply(location);
//        PVector tableRelativePos = new PVector(t.m03, t.m13, t.m23);
        PVector p2 = getRelativePos(new PVector(20, 20, 0));
        PVector tableRelativePos = getRelativePos(new PVector(0, 0, 0));

        float r = PApplet.atan2(p2.y - tableRelativePos.y, p2.x - tableRelativePos.x);

        PMatrix3D locationInv = this.getLocation().get();
        locationInv.invert();
        currentGraphics.scale(1, -1, 1);
        currentGraphics.translate(0, -getSize().y, 0);
        currentGraphics.applyMatrix(locationInv);
        currentGraphics.applyMatrix(table);
        currentGraphics.translate(tableRelativePos.x, tableRelativePos.y);
        currentGraphics.rotate(r);
        currentGraphics.scale(1, -1, 1);
    }
 
Example 3
Source File: PaperScreen.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
     * *
     * Works only in 3D mode with beginDraw3D(). Change the currernt matrix to
     * the location of another PaperScreen. This could be used for drawing
     * objects or putting lights at another PaperScreen location.
     *
     * @param paperScreen PaperScreen to go to.
     */
    public void goTo(PaperScreen paperScreen) {

        if (this.isDrawingOnScreen == true) {
            throw new RuntimeException("Impossible to draw on another board. You need to draw using beginDraw3D() to do so.");
        }

//        if (this.currentGraphics != graphics) {
//            throw new RuntimeException("The given graphics context is not valid. Use the one given by beginDraw3D().");
//        }
        // get the location of this board...
        PMatrix3D loc = this.getLocation().get();
        loc.invert();
        loc.apply(paperScreen.getLocation());

        applyMatrix(loc);
    }
 
Example 4
Source File: PaperScreen.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Get the Location of a point located in another PaperScreen. Example: In
 * paperScreenA, there is an object at location (10, 10). We want to know
 * the 3D location of this point relative to this PaperScreen.
 *
 * @param paperScreen
 * @param point
 * @return
 */
public PVector getCoordFrom(PaperScreen paperScreen, PVector point) {

    // get a copy
    PMatrix3D thisLocationInv = this.getLocation().get();
    thisLocationInv.invert();

    PMatrix3D otherLocation = paperScreen.getLocation();
    PVector cameraViewOfPoint = new PVector();
    otherLocation.mult(point, cameraViewOfPoint);

    PVector thisViewOfPoint = new PVector();
    thisLocationInv.mult(cameraViewOfPoint, thisViewOfPoint);

    if (Float.isNaN(thisViewOfPoint.x)) {
        return INVALID_VECTOR;
    }

    return thisViewOfPoint;
}
 
Example 5
Source File: Papart.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
     * Get the extrinsics from a depth camera.
     */
    private void updateDepthCameraDeviceExtrinsics() {
        // Check if depthCamera is the same as the camera !
        if (cameraTracking instanceof CameraRGBIRDepth
                && cameraTracking == depthCameraDevice.getMainCamera()) {

            // No extrinsic used, it is already in the camera... 
            depthCameraDevice.getDepthCamera().setExtrinsics(depthCameraDevice.getStereoCalibration());

            // Specific
            // Important to use it for now ! Used in KinectTouchInput.projectPointToScreen
            ((DepthTouchInput) this.touchInput).useRawDepth();

//            System.out.println("Papart: Using Touchextrinsics from the device.");
        } else {
            // Two different cameras  
            // setExtrinsics must after the kinect stereo calibration is loaded
            PMatrix3D extr = (Papart.getPapart()).loadCalibration(Papart.kinectTrackingCalib);
            extr.invert();
            depthCameraDevice.getDepthCamera().setExtrinsics(extr);
//            System.out.println("Papart: Using Touchextrinsics from the calibrated File.");
        }
    }
 
Example 6
Source File: Papart.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Return the x,y positions of a 3D location projected onto a given
 * reference. The Z axis is the angle (in radians) given by the rotation of
 * the positionToFind.
 *
 * @param positionToFind
 * @param reference
 * @return
 */
public PVector projectPositionTo2D(PMatrix3D positionToFind,
        PMatrix3D reference,
        float referenceHeight) {
    PMatrix3D referenceInv = reference.get();
    referenceInv.invert();
    PMatrix3D relative = positionToFind.get();
    relative.preApply(referenceInv);

    PMatrix3D positionToFind2 = positionToFind.get();
    positionToFind2.translate(100, 0, 0);
    PMatrix3D relative2 = positionToFind2.get();
    relative2.preApply(referenceInv);

    PVector out = new PVector();
    float x = relative.m03 - relative2.m03;
    float y = relative.m13 - relative2.m13;
    out.z = PApplet.atan2(x, y);

    out.x = relative.m03;
    out.y = referenceHeight - relative.m13;

    return out;
}
 
Example 7
Source File: Papart.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Return the x,y positions of a 3D location projected onto a given
 * reference. The Z axis is the angle (in radians) given by the rotation of
 * the positionToFind.
 *
 * @param positionToFind
 * @param reference
 * @return
 */
public PVector projectPositionTo(PMatrix3D positionToFind,
        PaperScreen reference) {
    PMatrix3D referenceInv = reference.getLocation().get();
    referenceInv.invert();
    PMatrix3D relative = positionToFind.get();
    relative.preApply(referenceInv);

    PMatrix3D positionToFind2 = positionToFind.get();
    positionToFind2.translate(100, 0, 0);
    PMatrix3D relative2 = positionToFind2.get();
    relative2.preApply(referenceInv);

    PVector out = new PVector();
    float x = relative.m03 - relative2.m03;
    float y = relative.m13 - relative2.m13;
    out.z = PApplet.atan2(x, y);

    out.x = relative.m03;
    out.y = reference.drawingSize.y - relative.m13;

    return out;
}
 
Example 8
Source File: ExtrinsicCalibrator.java    From PapARt with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static PMatrix3D computeExtrinsics(PMatrix3D camPaper, PMatrix3D projPaper) {
    PMatrix3D extr = projPaper.get();
    extr.invert();
    extr.preApply(camPaper);
    extr.invert();
    return extr;
}
 
Example 9
Source File: DwSkyLightShader.java    From PixelFlow with MIT License 4 votes vote down vote up
void setUniforms() {

    // 1) modelview (camera) -> model (world)
    PMatrix3D mat_modelviewInv = geombuffer.pg_geom.modelviewInv.get();

    // camera -> world -> shadowmap
    PMatrix3D mat_shadow = shadowmap.getShadowmapMatrix();
    mat_shadow.apply(mat_modelviewInv);
    mat_shadow.transpose(); // processing
    
    PMatrix3D mat_shadow_normal = mat_shadow.get();
    mat_shadow_normal.invert();
    mat_shadow_normal.transpose(); // processing
    
    PMatrix3D mat_shadow_normal_modelview = shadowmap.getModelView().get();
    mat_shadow_normal_modelview.apply(mat_modelviewInv);
    mat_shadow_normal_modelview.transpose(); // processing
    mat_shadow_normal_modelview.invert();
    mat_shadow_normal_modelview.transpose(); // processing
    
    PMatrix3D mat_shadow_normal_projection = shadowmap.getProjection().get();
    mat_shadow_normal_projection.invert();
    mat_shadow_normal_projection.transpose(); // processing
    
//    PMatrix3D mat_shadow_modelview = new PMatrix3D(shadowmap.pg_shadowmap.modelview);
//    mat_shadow_modelview.apply(mat_modelviewInv);
//    mat_shadow_modelview.transpose();

    // 2) transform light direction into camera space = inverse-transpose-modelView * direction
    mat_modelviewInv.transpose();
    PVector light_dir_cameraspace = mat_modelviewInv.mult(shadowmap.lightdir, null);
    light_dir_cameraspace.normalize();
    
    // projection matrix of the geometry buffer
    PMatrix3D mat_projection = geombuffer.pg_geom.projection.get();
    mat_projection.transpose(); // processing
    
    // temporal averaging
    float pass_mix = RENDER_PASS/(RENDER_PASS+1.0f);
    
    float w_shadow = shadowmap.pg_shadowmap.width;
    float h_shadow = shadowmap.pg_shadowmap.height;
    
    // shadow offset
    float shadow_map_size = Math.min(w_shadow, h_shadow);
    float shadow_bias_mag = 0.33f/shadow_map_size;
    
//    shadow_bias_mag = scene_scale/ shadow_map_size;

    
//    PMatrix3D mat_screen_to_eye = new PMatrix3D();
//    mat_screen_to_eye.scale(w, h, 1);
//    mat_screen_to_eye.scale(0.5f);
//    mat_screen_to_eye.translate(1,1,1);
//    mat_screen_to_eye.apply(pg.projection);
//    mat_screen_to_eye.invert();
//    mat_screen_to_eye.transpose(); // processing, row-col switch
    

    
    // 3) update shader uniforms
    shader.set("mat_projection"              , mat_projection                    );
    shader.set("mat_shadow"                  , mat_shadow                        );
    shader.set("mat_shadow_normal_modelview" , mat_shadow_normal_modelview , true);
    shader.set("mat_shadow_normal_projection", mat_shadow_normal_projection, true);
//    shader.set("mat_shadow_normal", mat_shadow_normal);
//    shader.set("mat_screen_to_eye", mat_screen_to_eye);
//    shader.set("mat_shadow_modelview", mat_shadow_modelview);
    shader.set("dir_light", light_dir_cameraspace);
    shader.set("pass_mix", pass_mix);
    shader.set("wh_shadow", w_shadow, h_shadow); // should match the dimensions of the shading buffers
    shader.set("shadow_bias_mag", shadow_bias_mag);

    
//    getBuffer(buf_mat_projection              , mat_projection                    );
//    getBuffer(buf_mat_shadow                  , mat_shadow                        );
//    getBuffer(buf_mat_shadow_normal_modelview , mat_shadow_normal_modelview , true);
//    getBuffer(buf_mat_shadow_normal_projection, mat_shadow_normal_projection, true);
//
//    boolean transpose = !false;
//    shader_.uniformMatrix4fv("mat_projection"              , 1, transpose, buf_mat_projection              , 0);
//    shader_.uniformMatrix4fv("mat_shadow"                  , 1, transpose, buf_mat_shadow                  , 0);
//    shader_.uniformMatrix3fv("mat_shadow_normal_modelview" , 1, transpose, buf_mat_shadow_normal_modelview , 0);
//    shader_.uniformMatrix3fv("mat_shadow_normal_projection", 1, transpose, buf_mat_shadow_normal_projection, 0);
//    shader_.uniform3f       ("dir_light", light_dir_cameraspace.x, light_dir_cameraspace.y, light_dir_cameraspace.z);
//    shader_.uniform1f       ("pass_mix", pass_mix);
//    shader_.uniform2f       ("wh_shadow", w_shadow, h_shadow);
//    shader_.uniform1f       ("shadow_bias_mag", shadow_bias_mag);
  }
 
Example 10
Source File: PaperScreen.java    From PapARt with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Init VR rendering. The VR rendering creates a 3D "screen". It is used to
 * create 3D pop-up effects.
 *
 * @param cam Rendering origin.
 * @param userPos Position of the user, relative to the PaperScreen
 * @param nearPlane Close disance for OpengL in millimeters.
 * @param farPlane Far distance for OpenGL in millimeters.
 * @param isAnaglyph Use Anaglyph.
 * @param isLeft When analygph is it left or right, ignored otherwise.
 */
public void initDraw(Camera cam, PVector userPos, float nearPlane, float farPlane, boolean isAnaglyph, boolean isLeft) {

    PGraphicsOpenGL graphics = getGraphics();

    if (initPosM == null) {
        this.isOpenGL = true;
        // Transformation  Camera -> Marker

        initPosM = this.getLocation(cam);

        initPosM.translate(this.getRenderingSizeX() / 2, this.getRenderingSizeY() / 2);
        // All is relative to the paper's center. not the corner. 
        initPosM.scale(-1, 1, 1);

    }

    // get the current transformation... 
    PMatrix3D newPos = this.getLocation(cam);

    newPos.translate(this.getRenderingSizeX() / 2, this.getRenderingSizeY() / 2);
    newPos.scale(-1, 1, 1);

    newPos.invert();
    newPos.apply(initPosM);

    PVector user = new PVector();

    if (isAnaglyph && isLeft) {
        userPos.add(-halfEyeDist * 2, 0, 0);
    }
    newPos.mult(userPos, user);
    PVector paperCameraPos = user;

    // Camera must look perpendicular to the screen. 
    graphics.camera(paperCameraPos.x, paperCameraPos.y, paperCameraPos.z,
            paperCameraPos.x, paperCameraPos.y, 0,
            0, 1, 0);

    // http://www.gamedev.net/topic/597564-view-and-projection-matrices-for-vr-window-using-head-tracking/
    float nearFactor = nearPlane / paperCameraPos.z;

    float left = nearFactor * (-drawingSize.x / 2f - paperCameraPos.x);
    float right = nearFactor * (drawingSize.x / 2f - paperCameraPos.x);
    float top = nearFactor * (drawingSize.y / 2f - paperCameraPos.y);
    float bottom = nearFactor * (-drawingSize.y / 2f - paperCameraPos.y);

    graphics.frustum(left, right, bottom, top, nearPlane, farPlane);
    graphics.projection.m11 = -graphics.projection.m11;

    // No detection?
    PMatrix3D transformation = this.getLocation(cam);
    if (transformation.m03 == 0 && transformation.m13 == 0 && transformation.m23 == 0) {
        resetPos();
    }
}