| 1 | /**************************************************************************** |
| 2 | * MeshLab o o * |
| 3 | * A versatile mesh processing toolbox o o * |
| 4 | * _ O _ * |
| 5 | * Copyright(C) 2008 \/)\/ * |
| 6 | * Visual Computing Lab /\/| * |
| 7 | * ISTI - Italian National Research Council | * |
| 8 | * \ * |
| 9 | * All rights reserved. * |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published by * |
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
| 14 | * (at your option) any later version. * |
| 15 | * * |
| 16 | * This program is distributed in the hope that it will be useful, * |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 19 | * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * |
| 20 | * for more details. * |
| 21 | * * |
| 22 | ****************************************************************************/ |
| 23 | /**************************************************************************** |
| 24 | History |
| 25 | $Log: activecoordinateframe.cpp,v $ |
| 26 | Revision 1.2 2008/03/21 16:09:02 benedetti |
| 27 | improved comments |
| 28 | |
| 29 | Revision 1.1 2008/03/02 16:44:18 benedetti |
| 30 | moved ActiveCoordinateFrame to its own files |
| 31 | |
| 32 | |
| 33 | ****************************************************************************/ |
| 34 | |
| 35 | #include <GL/glew.h> |
| 36 | #include <wrap/gl/math.h> |
| 37 | #include <wrap/gl/space.h> |
| 38 | #include <wrap/gl/addons.h> |
| 39 | |
| 40 | #include "activecoordinateframe.h" |
| 41 | |
| 42 | using namespace vcg; |
| 43 | |
| 44 | ActiveCoordinateFrame::ActiveCoordinateFrame(float size) |
| 45 | :MovableCoordinateFrame(size),manipulator(NULL),drawmoves(true), |
| 46 | drawrotations(true),move_button(Trackball::BUTTON_RIGHT), |
| 47 | rotate_button(Trackball::BUTTON_LEFT),x_modifier(Trackball::BUTTON_NONE), |
| 48 | y_modifier(Trackball::KEY_CTRL),z_modifier(Trackball::KEY_SHIFT), |
| 49 | x_axis(1,0,0),y_axis(0,1,0),z_axis(0,0,1),rot_snap_rad(0.0f),mov_snap(0.0f), |
| 50 | movx(move_button | x_modifier),movy(move_button | y_modifier), |
| 51 | movz(move_button | z_modifier),rotx(rotate_button | x_modifier), |
| 52 | roty(rotate_button | y_modifier),rotz(rotate_button | z_modifier) |
| 53 | { |
| 54 | manipulator=new Trackball(); |
| 55 | Update(); |
| 56 | } |
| 57 | |
| 58 | ActiveCoordinateFrame::~ActiveCoordinateFrame() |
| 59 | { |
| 60 | if(manipulator!=NULL) { |
| 61 | delete manipulator; |
| 62 | manipulator=NULL; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void ActiveCoordinateFrame::Render(QGLWidget* glw) |
| 67 | { |
| 68 | glPushMatrix(); |
| 69 | |
| 70 | manipulator->radius=size; |
| 71 | manipulator->center=position; |
| 72 | manipulator->GetView(); |
| 73 | manipulator->Apply(false); |
| 74 | |
| 75 | MovableCoordinateFrame::Render(glw); |
| 76 | |
| 77 | // got nothing to draw |
| 78 | if(!drawmoves && !drawrotations){ |
| 79 | glPopMatrix(); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | int current_mode=manipulator->current_button; |
| 84 | bool rotating=(current_mode==rotx)||(current_mode==roty)||(current_mode==rotz); |
| 85 | bool moving=(current_mode==movx)||(current_mode==movy)||(current_mode==movz); |
| 86 | |
| 87 | // maybe got something to draw |
| 88 | glPushAttrib(GL_ALL_ATTRIB_BITS); |
| 89 | glDisable(GL_LIGHTING); |
| 90 | glDisable(GL_TEXTURE_2D); |
| 91 | glEnable(GL_BLEND); |
| 92 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 93 | glEnable(GL_LINE_SMOOTH); |
| 94 | glEnable(GL_POINT_SMOOTH); |
| 95 | |
| 96 | QString message("this should never be seen"); |
| 97 | char axis_name; |
| 98 | float verse; |
| 99 | |
| 100 | if(current_mode==x_modifier){ |
| 101 | glColor(xcolor); message = QString("move or rotate on X axis"); |
| 102 | } else if(current_mode==y_modifier){ |
| 103 | glColor(ycolor); message = QString("move or rotate on Y axis"); |
| 104 | } else if(current_mode==z_modifier){ |
| 105 | glColor(zcolor); message = QString("move or rotate on Z axis"); |
| 106 | } else |
| 107 | if(rotating && drawrotations){ // draw a rotation |
| 108 | Point3f axis, arc_point; |
| 109 | float angle; |
| 110 | manipulator->track.rot.ToAxis(angle,axis); |
| 111 | angle = -angle; |
| 112 | if(current_mode==rotx){ |
| 113 | verse=((axis+x_axis).Norm()<1?-1:1); |
| 114 | glColor(xcolor); axis_name='x'; arc_point=y_axis*(size*0.8); |
| 115 | } else if(current_mode==roty) { |
| 116 | verse=((axis+y_axis).Norm()<1?-1:1); |
| 117 | glColor(ycolor); axis_name='y'; arc_point=z_axis*(size*0.8); |
| 118 | } else if(current_mode==rotz) { |
| 119 | verse=((axis+z_axis).Norm()<1?-1:1); |
| 120 | glColor(zcolor); axis_name='z'; arc_point=x_axis*(size*0.8); |
| 121 | } else assert(0); |
| 122 | // normalizing rotation between -180 and 180 degrees |
| 123 | float sign = ((angle*verse)<0) ? -1 : 1; |
| 124 | float abs_angle = (angle<0) ? -angle : angle; |
| 125 | angle = sign * ( (abs_angle>M_PI) ? 2*M_PI-abs_angle : abs_angle ); |
| 126 | axis = axis * verse; |
| 127 | message = QString("rotated %1 deg around %2") |
| 128 | .arg(((angle*180.0)/M_PI),5,'f',3) |
| 129 | .arg(axis_name); |
| 130 | Quaternionf arc_rot; |
| 131 | arc_rot.FromAxis(angle/18.0,axis); |
| 132 | glBegin(GL_POLYGON); |
| 133 | glVertex(position); |
| 134 | glVertex(position+arc_point); |
| 135 | for(int i=0;i<18;i++){ |
| 136 | arc_point = arc_rot.Rotate(arc_point); |
| 137 | glVertex(position+arc_point); |
| 138 | } |
| 139 | glEnd(); |
| 140 | } else if(moving && drawmoves){ // draw a traslation |
| 141 | Point3f ntra=manipulator->track.tra; |
| 142 | ntra.Normalize(); |
| 143 | if(current_mode==movx){ |
| 144 | verse=((ntra+x_axis).Norm()<1?-1:1); |
| 145 | glColor(xcolor); axis_name='x'; |
| 146 | }else if(current_mode==movy){ |
| 147 | verse=((ntra+y_axis).Norm()<1?-1:1); |
| 148 | glColor(ycolor); axis_name='y'; |
| 149 | }else if(current_mode==movz){ |
| 150 | verse=((ntra+z_axis).Norm()<1?-1:1); |
| 151 | glColor(zcolor); axis_name='z'; |
| 152 | }else assert(0); |
| 153 | message = QString("moved %1 units along %2") |
| 154 | .arg(verse*manipulator->track.tra.Norm(),5,'f',3) |
| 155 | .arg(axis_name); |
| 156 | Point3f old_pos = position-manipulator->track.tra; |
| 157 | glLineWidth(2*linewidth); |
| 158 | glPointSize(4*linewidth); |
| 159 | glBegin(GL_LINES); |
| 160 | glVertex(position); |
| 161 | glVertex(old_pos); |
| 162 | glEnd(); |
| 163 | glBegin(GL_POINTS); |
| 164 | glVertex(old_pos); |
| 165 | glEnd(); |
| 166 | } else { // got nothing to draw |
| 167 | glPopAttrib(); |
| 168 | glPopMatrix(); |
| 169 | return; |
| 170 | } |
| 171 | // draw message below cursor |
| 172 | font.setBold(true); |
| 173 | font.setPixelSize(12); |
| 174 | QPoint cursor=glw->mapFromGlobal(glw->cursor().pos()); |
| 175 | glw->renderText(cursor.x()+16,cursor.y()+16,message,font); |
| 176 | |
| 177 | glPopAttrib(); |
| 178 | glPopMatrix(); |
| 179 | } |
| 180 | |
| 181 | void ActiveCoordinateFrame::Reset(bool reset_position,bool reset_alignment) |
| 182 | { |
| 183 | MovableCoordinateFrame::Reset(reset_position, reset_alignment); |
| 184 | Update(); |
| 185 | manipulator->Reset(); |
| 186 | } |
| 187 | |
| 188 | void ActiveCoordinateFrame::SetPosition(const Point3f newpos) |
| 189 | { |
| 190 | MovableCoordinateFrame::SetPosition(newpos); |
| 191 | Update(); |
| 192 | manipulator->Reset(); |
| 193 | } |
| 194 | |
| 195 | void ActiveCoordinateFrame::SetRotation(const Quaternionf newrot) |
| 196 | { |
| 197 | MovableCoordinateFrame::SetRotation(newrot); |
| 198 | Update(); |
| 199 | manipulator->Reset(); |
| 200 | } |
| 201 | |
| 202 | void ActiveCoordinateFrame::AlignWith(const Point3f primary,const Point3f secondary,const char c1,const char c2) |
| 203 | { |
| 204 | MovableCoordinateFrame::AlignWith(primary,secondary,c1,c2); |
| 205 | Update(); |
| 206 | manipulator->Reset(); |
| 207 | } |
| 208 | |
| 209 | void ActiveCoordinateFrame::MouseDown(int x, int y, /*Button*/ int button) |
| 210 | { |
| 211 | Move(manipulator->track); |
| 212 | manipulator->Reset(); |
| 213 | manipulator->MouseDown(x,y,button); |
| 214 | } |
| 215 | |
| 216 | void ActiveCoordinateFrame::MouseMove(int x, int y) |
| 217 | { |
| 218 | manipulator->MouseMove(x,y); |
| 219 | } |
| 220 | |
| 221 | void ActiveCoordinateFrame::MouseUp(int x, int y, /*Button */ int button) |
| 222 | { |
| 223 | Move(manipulator->track); |
| 224 | manipulator->Reset(); |
| 225 | manipulator->MouseUp(x, y, button); |
| 226 | } |
| 227 | |
| 228 | void ActiveCoordinateFrame::ButtonUp(int button) |
| 229 | { |
| 230 | Move(manipulator->track); |
| 231 | manipulator->Reset(); |
| 232 | manipulator->ButtonUp((Trackball::Button) button); |
| 233 | } |
| 234 | |
| 235 | void ActiveCoordinateFrame::ButtonDown(int button) |
| 236 | { |
| 237 | Move(manipulator->track); |
| 238 | manipulator->Reset(); |
| 239 | manipulator->ButtonDown((Trackball::Button) button); |
| 240 | } |
| 241 | |
| 242 | void ActiveCoordinateFrame::SetSnap(float rot_deg) |
| 243 | { |
| 244 | assert((rot_deg>=0.0)&&(rot_deg<=180)); |
| 245 | rot_snap_rad=rot_deg*M_PI/180.0; |
| 246 | Update(); |
| 247 | } |
| 248 | |
| 249 | void ActiveCoordinateFrame::Move(const Similarityf track) |
| 250 | { |
| 251 | MovableCoordinateFrame::Move(track); |
| 252 | Update(); |
| 253 | } |
| 254 | |
| 255 | void ActiveCoordinateFrame::Update() |
| 256 | { |
| 257 | movx=(move_button | x_modifier); |
| 258 | movy=(move_button | y_modifier); |
| 259 | movz=(move_button | z_modifier); |
| 260 | rotx=(rotate_button | x_modifier); |
| 261 | roty=(rotate_button | y_modifier); |
| 262 | rotz=(rotate_button | z_modifier); |
| 263 | |
| 264 | Point3f p=position; |
| 265 | Quaternionf r=Inverse(rotation); |
| 266 | x_axis=r.Rotate(Point3f(1,0,0)); |
| 267 | y_axis=r.Rotate(Point3f(0,1,0)); |
| 268 | z_axis=r.Rotate(Point3f(0,0,1)); |
| 269 | |
| 270 | std::map<int, TrackMode *>::iterator it; |
| 271 | for(it = manipulator->modes.begin(); it != manipulator->modes.end(); it++) |
| 272 | { |
| 273 | if ((*it).second) |
| 274 | delete (*it).second; |
| 275 | } |
| 276 | manipulator->modes.clear(); |
| 277 | manipulator->modes[0] = NULL; |
| 278 | manipulator->modes[movx] = new AxisMode(p,x_axis); |
| 279 | manipulator->modes[movy] = new AxisMode(p,y_axis); |
| 280 | manipulator->modes[movz] = new AxisMode(p,z_axis); |
| 281 | manipulator->modes[rotx] = new CylinderMode(p,x_axis,rot_snap_rad); |
| 282 | manipulator->modes[roty] = new CylinderMode(p,y_axis,rot_snap_rad); |
| 283 | manipulator->modes[rotz] = new CylinderMode(p,z_axis,rot_snap_rad); |
| 284 | manipulator->SetCurrentAction(); |
| 285 | } |