ofMatri3x3 * ofVec3f Invalid operands to binary expression error

Hello,

Beginner here. I am trying to multiply an ofMatrix3x3 by an ofVec3f unsuccessfully. I get an Semantic Issue error saying “Invalid operands to binary expression.” I assume it is probably a very easy solution to this, but I have not been able to find the answer.

This is my code
Header:

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{
	public:
		void setup();
		void update();
		void draw();
		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);

    ofMatrix3x3 rotateXY;
    ofVec3f v1; //vectorForward;    
};

.cpp:

#include "ofApp.h"

float theta = 45.0;
float thetaRad = (theta * 3.14159265358979323846) / 180;

void ofApp::keyPressed(int key){
   
    rotateXY.set(cos(thetaRad), -(sin(thetaRad)), 0.0,
                 sin(thetaRad), cos(thetaRad), 0.0,
                 0.0, 0.0, 1.0);
    
     v1.set(0.0,   1.0,  1.0);
    
    rotateXY * v1;
}

Thanks in advance,
Irving.

Hi irv,

In ofMatrix3x3.h I don’t see any definition for the multiplication between an ofMatrix3x3 and an ofVec3f. I don’t know why.
If you want to apply some linear transformation to an ofVec3f you can use an ofMatrix4x4. You can do the same geometrical transformations, and more (translations for example).

float theta = 45.0;
ofMatrix4x4 rotateXY;
rotateXY.rotate( theta, 0, 0, 1 ); // simplier than computing each values with trigonometry

ofVec3f v1(0.0,   1.0,  1.0);

ofVec3f v2 = v1 * rotateXY; // Take care: v1 * rotateXY, not rotateXY * v1
cout << v2.x << endl; // -0.707107
cout << v2.y << endl; // 0.707107
cout << v2.z << endl; // 1

Some explanations here: Matrix usage

1 Like

Hello Lilive,

Thanks a lot for your explanation and the links. It makes much more sense to me. What is confusing to me is why a 4x4 matrix multiplied by a 3x1 matrix (vector). But it works just fine.

Thanks again, this does solve my issues.
Irving.

This also confused me at first.
4x4 matrix are often used for 3D.
You can search for “homogeneous coordinates” topics (the wikipedia maths explanations, as usual, are not the simpliest).

My basic understanding is:

  • Each 3D point P is a 3x1 matrix P[x,y,z]. In homogeneous coordinates, a 4th coordinate is added: Q[x,y,z,1]

  • Then you can transform Q with a 4x4 matrix M with multiplication: M*Q (math notation)
    This is the standard multiplication between a 4x4 and a 4x1 matrix.

  • There are 4x4 matrix which don’t affect the 4th coordinate of Q. After the multiplication, the 4th coordinate is still 1. You can ignore it after the multiplication, and retrieve your 3D space. This is why in OF you write newP=P*M. No need to use a 4x1 vector Q.

  • This multiplication is still a linear algebra operation. But this homogeneous coordinates system allow some extended possibilities. The translation is one of them. You’ve got rotations, scaling, symetries, as usual with 3x3 matrices, but also translations. Really convenient. Perhaps there are other possibilities, I don’t know, something about dealing with 2D projection and perspective maybe?

I’m not expert about this topic, this is just what I have understood :slightly_smiling:

Yes, I assumed with your example that it is adding the one automatically in the fourth vector row. It threw me off completely before I saw the example. The good thing is that it works well.

Thanks again for your help. I am already applying it and it works as I expect it. Now I am facing other issues with the drawings and what not.

Best,
Irving.