Rotation with Quaternions help!!

Hi there,

There may be a simple solution to this but I’m a bit stumped!

I am using quaternions to rotate a 3d object (See code below). All I need to do is stop the rotation from going more than, say, 45 degrees left/right or up/down. I have tried accumulating the mouse vectors used for the rotation and checking when they reach a limit, but this ends up being quite inaccurate for some reason.

Any help would be great! Thanks.
This code is an example of quaternion rotation provided to me by obviousjim on this forum!

        //current state of the rotation    
	ofQuaternion curRot;    
	//a place to store the mouse position so we can measure incremental change    
	ofVec2f lastMouse;   

void testApp::draw(){  
	ofVec3f axis;    
    float angle;         
    curRot.getRotate(angle, axis);    
    ofTranslate(ofGetWidth()/2, ofGetHeight()/2, 40);    
    ofRotate(angle, axis.x, axis.y, axis.z);    
    ofBox(0, 0, 0, 200); //i'm drawing a box b/c it's easier to see it rotate!    

void testApp::mouseDragged(int x, int y, int button){  
	ofVec2f mouse(x,y);    
    ofQuaternion yRot(x-lastMouse.x, ofVec3f(0,1,0));    
    ofQuaternion xRot(y-lastMouse.y, ofVec3f(-1,0,0));    
    curRot *= yRot*xRot;    
    lastMouse = mouse;    

void testApp::mousePressed(int x, int y, int button){  
	lastMouse = ofVec2f(x,y);    

it may help,-get-translationrotation-from-matrix/4335/10