Matrix: after translate/rotate in P3D: can we save the Matrix other than with pushMatrix()?

edited August 2017 in Programming Questions

Hello all,

a question regarding the Matrix: after translate / rotate in P3D: can we save the Matrix other than with pushMatrix()?

You know we can change the rotation matrix by using rotate() and translate() in processing.

One wish would be that it would be possible to store the current matrix like Matrix m = getMatrix();

and then say setMatrix(m);.

How is that possible please?

Thank you!

Best, Chrisir ;-)

Answers

  • edited August 2017

    As you may already know, we can print the current matrix via printMatrix():
    https://Processing.org/reference/printMatrix_.html

    ... it would be possible to store the current matrix like: Matrix m = getMatrix(); and then say setMatrix(m);.

    Processing's web reference contains merely a fraction of its API: 8-|
    https://Processing.org/reference/

    For the complete Processing's API, we need to look up its JavaDocs: ~O)

    1. http://Processing.GitHub.io/processing-javadocs/everything/
    2. http://Processing.GitHub.io/processing-javadocs/everything/processing/core/PMatrix.html

    Or study directly at its source repo: *-:)

    1. https://GitHub.com/processing/processing/tree/master/core/src/processing
    2. https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PMatrix.java

    Inside class PMatrix, we can find its getter & setter as methods get() & set(): O:-)

    1. get(): https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PMatrix.java#L53
    2. set(): https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PMatrix.java#L65
  • Hoho...!!!

  • Sorry, I fail to adopt that into processing code

    PMatrix is the name of the class but what is the name of the used matrix?

    I try to store the current matrix into a hashMap:

    (I marked the line where it fails)

    thank you!

    // HashMap 
    HashMap<String, float []> hmStoreTurtleMatrix = new HashMap<String, float []>();
    
    
    
    
    void learnPosition(String name) {
        //  
        //  similar to pushMatrix(); but given the matrix position a name 
        float [] result2 = null;  
        float [] result = PMatrix.get(result2); // ??????????????????????????????
    
        hmStoreTurtleMatrix.put(name, 
          result);
      }
    
    
    
      void retrievePosition(String name) {
    
        float [] result = null;
        result = hmStoreTurtleMatrix.get(name);
      }
    
  • edited August 2017

    Brilliant!

    Thank you SO much, GoToLoop!

    That worked.

    Here is what I did.

    // HashMap 
    HashMap<String, PMatrix3D > hmStoreTurtleMatrix = new HashMap<String, PMatrix3D>();
    
    
    
    
      // -----------------------------------------------
    
      void learnPosition(String name) {
        // similar to pushMatrix(); but giving the matrix position a name 
        // (and not using the stack)
        name=name.trim(); 
        PMatrix3D result = null;
        result = getMatrix(result);
    
        // Putting key-value pairs in the HashMap
        hmStoreTurtleMatrix.put(name, result);
      }//method
    
      boolean retrievePosition(String name) {
        // similar to popMatrix(); but with a name for that matrix position.
        // Returns if it succeded.
        name=name.trim(); 
        PMatrix3D result = null;
    
        // We can access values by their key
        result = hmStoreTurtleMatrix.get(name);
        if (result!=null) {
          setMatrix(result);
          return true; // success
        }//if
        return false; // fail
      }//method
    
      // ---------------------------------------------------
    

    That's the version for P3D. For 2D probably use PMatrix2D.

    keywords: getMatrix setMatrix pushMatrix with name popMatrix with String

  • edited August 2017

    @Chrisir -- does your example saving code work for stacks that are nested more than one transform deep?

    When I last played with getMatrix/setMatrix I thought getMatrix() was returning the last transformation, not the cumulative transformation. So, if the stack was:

    1. translate(100,100)
    2. rotate(2)
    3. translate(0,50)

    ...then getMatrix() would return a PMatrix/PMatrix3D object for translate(0,50), rather than one representing all the steps on the stack.

    Or maybe I need to look at this again....

  • In my tests they delivered the entire matrix (cumulative)

    But I have to admit I wasn't aware of your question at the time

  • The commands like translate / rotate are not on a stack btw

    They just change the matrix

    Only pushMatrix and popmatrix are using a stack

    It might be relevant to pass a PMatrix with value null as parameter to getMatrix as I did

  • commands like translate / rotate are not on a stack btw

    Yes -- sorry, I should have been much more clear. I meant working with cumulative stacks (several pushMatrix calls) specifically. So:

    1. push + translate
    2. push + rotate
    3. push + translate

    ...being able to retrieve a PMatrix that represents the cumulative transformation of all stack frames, as opposed to the cumulative changes in one frame.

  • I tried it only with my turtle script

    Haven't tried this

  • edited August 2017

    not sure what you mean...

    • pushMatrix() + translate
    • pushMatrix()+ rotate
    • pushMatrix()+ translate

      // HashMap 
      HashMap<String, PMatrix3D > hmStoreTurtleMatrix = new HashMap<String, PMatrix3D>();
      
      int state=0; 
      
      
      void setup() {
        size(1100, 900, P3D);
        stroke(255, 0, 0);
        background(0);
      }
      
      void draw() {
      
        if (state==0) {
      
          background(0);
      
          pushMatrix(); 
          translate(100, 100, 0);
          // popMatrix(); 
      
          pushMatrix(); 
          rotateZ(2.2);
          // popMatrix();
      
          pushMatrix(); 
          translate(0, 50, -111); 
          //popMatrix();
      
          sphere (21); 
          learnPosition("c12"); 
      
          popMatrix();
          popMatrix();
          popMatrix();
          //
        } else if (state==1) {
          retrievePosition("c12"); 
          box(11);
        }
      }
      
      void keyPressed() {
        state = 1;
      }
      
      
      // -----------------------------------------------
      
      void learnPosition(String name) {
        // similar to pushMatrix(); but giving the matrix position a name 
        // (and not using the stack)
        name=name.trim(); 
        PMatrix3D result = null;
        result = getMatrix(result);
      
        // Putting key-value pairs in the HashMap
        hmStoreTurtleMatrix.put(name, result);
      }//method
      
      boolean retrievePosition(String name) {
        // similar to popMatrix(); but with a name for that matrix position.
        // Returns if it succeded.
        name=name.trim(); 
        PMatrix3D result = null;
      
        // We can access values by their key
        result = hmStoreTurtleMatrix.get(name);
        if (result!=null) {
          setMatrix(result);
          return true; // success
        }//if
        return false; // fail
      }//method
      
      // ---------------------------------------------------
      
Sign In or Register to comment.