Debian Codegraph @

vcg/wrap/gui/coordinateframe.cpp

raw symbols SCIP
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: coordinateframe.cpp,v $
26 Revision 1.9 2008/03/14 17:15:59 benedetti
27 improved comments
28
29 Revision 1.8 2008/03/02 16:44:18 benedetti
30 moved ActiveCoordinateFrame to its own files
31
32 Revision 1.7 2008/02/26 18:22:42 benedetti
33 corrected after quaternion/similarity/trackball changes
34
35 Revision 1.6 2008/02/22 20:34:35 benedetti
36 corrected typo
37
38 Revision 1.5 2008/02/22 20:04:02 benedetti
39 many user interface improvements, cleaned up a little
40
41 Revision 1.4 2008/02/17 20:52:53 benedetti
42 some generalization made
43
44 Revision 1.3 2008/02/16 14:12:30 benedetti
45 first version
46
47
48 ****************************************************************************/
49
50 #include <GL/glew.h>
51 #include <wrap/gl/math.h>
52 #include <wrap/gl/space.h>
53 #include <wrap/gl/addons.h>
54
55 #include "coordinateframe.h"
56
57 using namespace vcg;
58
59 CoordinateFrame::CoordinateFrame(float s)
60 :basecolor(Color4b::White),xcolor(Color4b::Red)
61 ,ycolor(Color4b::Green),zcolor(Color4b::Blue),size(s),linewidth(2.0)
62 ,font(),drawaxis(true),drawlabels(true),drawvalues(false)
63 {
64 font.setFamily("Helvetica");
65 }
66
67 void CoordinateFrame::Render(QGLWidget* glw)
68 {
69 assert( glw!= NULL);
70 glPushAttrib(GL_ALL_ATTRIB_BITS);
71 glDisable(GL_LIGHTING);
72 glDisable(GL_TEXTURE_2D);
73 glEnable(GL_BLEND);
74 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
75 glEnable(GL_LINE_SMOOTH);
76 glEnable(GL_POINT_SMOOTH);
77 glLineWidth(linewidth);
78 glPointSize(linewidth*1.5);
79
80 Point3d o(0,0,0);
81 Point3d a(size,0,0);
82 Point3d b(0,size,0);
83 Point3d c(0,0,size);
84 // Get gl state values
85 double mm[16],mp[16];
86 GLint vp[4];
87 glGetDoublev(GL_MODELVIEW_MATRIX,mm);
88 glGetDoublev(GL_PROJECTION_MATRIX,mp);
89 glGetIntegerv(GL_VIEWPORT,vp);
90 float slope_a=calcSlope(-a,a,2*size,10,mm,mp,vp);
91 float slope_b=calcSlope(-b,b,2*size,10,mm,mp,vp);
92 float slope_c=calcSlope(-c,c,2*size,10,mm,mp,vp);
93 float scalefactor = size*0.02f;
94 if(drawaxis){
95 glBegin(GL_LINES);
96 glColor(xcolor);
97 glVertex(-a); glVertex(a);
98 glColor(ycolor);
99 glVertex(-b); glVertex(b);
100 glColor(zcolor);
101 glVertex(-c); glVertex(c);
102 glEnd();
103 glColor(basecolor);
104 // positive axes
105 drawTickedLine(o,a,size,slope_a,linewidth); // Draws x axis
106 drawTickedLine(o,b,size,slope_b,linewidth); // Draws y axis
107 drawTickedLine(o,c,size,slope_c,linewidth); // Draws z axis
108 // negative axes
109 drawTickedLine(o,-a,size,slope_a,linewidth); // Draws x axis
110 drawTickedLine(o,-b,size,slope_b,linewidth); // Draws y axis
111 drawTickedLine(o,-c,size,slope_c,linewidth); // Draws z axis
112 glPushMatrix();
113 glTranslate(a);
114 glScalef(scalefactor,scalefactor,scalefactor);
115 Add_Ons::Cone(10,linewidth*1.5,linewidth*0.5,true);
116 glPopMatrix();
117 glPushMatrix();
118 glTranslate(b);
119 glRotatef(90,0,0,1);
120 glScalef(scalefactor,scalefactor,scalefactor);
121 Add_Ons::Cone(10,linewidth*1.5,linewidth*0.5,true);
122 glPopMatrix();
123 glPushMatrix();
124 glTranslate(c);
125 glRotatef(-90,0,1,0);
126 glScalef(scalefactor,scalefactor,scalefactor);
127 Add_Ons::Cone(10,linewidth*1.5,linewidth*0.5,true);
128 glPopMatrix();
129 }
130 if(drawlabels){
131 font.setBold(true);
132 font.setPixelSize(12);
133 float d=size+scalefactor*linewidth*1.5;
134 glColor(xcolor);
135 glw->renderText(d,0,0,QString("X"),font);
136 glColor(ycolor);
137 glw->renderText(0,d,0,QString("Y"),font);
138 glColor(zcolor);
139 glw->renderText(0,0,d,QString("Z"),font);
140 }
141 if(drawvalues){
142 font.setBold(false);
143 font.setPixelSize(8);
144 float i;
145 glColor(Color4b::LightGray);
146 for(i=slope_a;i<size;i+=slope_a){
147 glw->renderText( i,0,0,QString(" %1").arg(i,3,'f',1),font);
148 glw->renderText(-i,0,0,QString("-%1").arg(i,3,'f',1),font);
149 }
150 for(i=slope_b;i<size;i+=slope_b){
151 glw->renderText(0, i,0,QString(" %1").arg(i,3,'f',1),font);
152 glw->renderText(0,-i,0,QString("-%1").arg(i,3,'f',1),font);
153 }
154 for(i=slope_c;i<size;i+=slope_c){
155 glw->renderText(0,0, i,QString(" %1").arg(i,3,'f',1),font);
156 glw->renderText(0,0,-i,QString("-%1").arg(i,3,'f',1),font);
157 }
158 }
159
160 glPopAttrib();
161 assert(!glGetError());
162 }
163
164 void CoordinateFrame::drawTickedLine(const Point3d &a,const Point3d &b, float dim,float tickDist,float linewidth)
165 {
166 Point3d v(b-a);
167 v = v /dim; // normalize without computing square roots and powers
168
169 glBegin(GL_POINTS);
170 float i;
171 for(i=tickDist;i<dim;i+=tickDist)
172 glVertex3f(a[0] + i*v[0],a[1] + i*v[1],a[2] + i*v[2]);
173 glEnd();
174
175 glPushAttrib(GL_POINT_BIT);
176 glPointSize(linewidth*3);
177 glBegin(GL_POINTS);
178 glVertex3f(a[0] + dim*v[0],a[1] + dim*v[1],a[2] + dim*v[2]);
179 glEnd();
180
181 glPopAttrib();
182 }
183
184 float CoordinateFrame::calcSlope(const Point3d &a,const Point3d &b,float dim,int spacing,double *mm,double *mp,GLint *vp)
185 {
186 Point3d p1,p2;
187
188 gluProject(a[0],a[1],a[2],mm,mp,vp,&p1[0],&p1[1],&p1[2]);
189 gluProject(b[0],b[1],b[2],mm,mp,vp,&p2[0],&p2[1],&p2[2]);
190 p1[2]=p2[2]=0;
191
192 float tickNum = spacing/Distance(p2,p1);// pxl spacing
193 float slope = dim*tickNum;
194 float nslope = math::Min(
195 math::Min(niceRound(slope), 0.5f*niceRound(2.0f*slope)),
196 0.2f*niceRound(5.0f*slope));
197 nslope = math::Max<float>(niceRound(dim*.001f),nslope); // prevent too small slope
198 return nslope;
199 }
200
201 float CoordinateFrame::niceRound(float val)
202 {
203 return powf(10.f,ceil(log10(val)));
204 }
205
206 MovableCoordinateFrame::MovableCoordinateFrame(float size)
207 :CoordinateFrame(size),position(0,0,0),rotation(0,Point3f(1,0,0))
208 {
209 // nothing here
210 }
211
212 void MovableCoordinateFrame::Render(QGLWidget* gla)
213 {
214 glPushMatrix();
215
216 glTranslate(position);
217 Matrix44f mrot;
218 rotation.ToMatrix(mrot);
219
220 glMultMatrix(Inverse(mrot));
221
222 CoordinateFrame::Render(gla);
223
224 glPopMatrix();
225 }
226
227 void MovableCoordinateFrame::GetTransform(Matrix44f & transform)
228 {
229 // build the matrix that moves points in world coordinates
230
231 // clean transform
232 transform.SetIdentity();
233
234 // apply rotation
235 Matrix44f rot;
236 rotation.ToMatrix(rot);
237
238 transform = Inverse(rot) * transform ;
239
240 // apply translation
241 Matrix44f pos;
242 pos.SetTranslate(position);
243
244 transform = pos * transform;
245
246 }
247
248 void MovableCoordinateFrame::Reset(bool reset_position,bool reset_alignment)
249 {
250 if(reset_position)
251 position = Point3f(0,0,0);
252 if(reset_alignment)
253 rotation = Quaternionf(0,Point3f(1,0,0));
254 }
255
256 void MovableCoordinateFrame::SetPosition(const Point3f newpos)
257 {
258 position = newpos;
259 }
260
261 void MovableCoordinateFrame::SetRotation(const Quaternionf newrot)
262 {
263 rotation = newrot;
264 }
265
266 Point3f MovableCoordinateFrame::GetPosition()
267 {
268 return position;
269 }
270
271 Quaternionf MovableCoordinateFrame::GetRotation()
272 {
273 return rotation;
274 }
275
276 void MovableCoordinateFrame::Rot(float angle_deg,const Point3f axis)
277 {
278 Similarityf s;
279 s.SetRotate(angle_deg*M_PI/180.0f,(rotation).Rotate(axis));
280 Move(s);
281 }
282
283 void MovableCoordinateFrame::AlignWith(const Point3f pri,const Point3f secondary,const char c1, const char c2)
284 {
285 const float EPSILON=1e-6;
286 Point3f primary=pri;
287
288 if( primary.Norm() < EPSILON*size )
289 return;
290
291 primary.Normalize();
292 Plane3f plane(0,primary); // projection plane for the second rotation
293
294 Point3f x(1,0,0),y(0,1,0),z(0,0,1);
295 Point3f first(0,0,0),second(0,0,0),third(0,0,0);
296
297 if(c1=='X'){ first = x;
298 if((c2=='Y')||(c2==' ')){ second = y; third = z; }
299 else if(c2=='Z'){ second = z; third = y; }
300 else assert (0);
301 } else if(c1=='Y'){ first = y;
302 if((c2=='Z')||(c2==' ')){ second = z; third = x; }
303 else if(c2=='X'){ second = x; third = z; }
304 else assert (0);
305 } else if(c1=='Z'){ first = z;
306 if((c2=='X')||(c2==' ')){ second = x; third = y; }
307 else if(c2=='Y'){ second = y; third = x; }
308 else assert (0);
309 } else assert (0);
310
311 Point3f old_first = Inverse(rotation).Rotate(first); // axis 1
312 Point3f old_second_pro = plane.Projection(Inverse(rotation).Rotate(second)); // axis 2 projection
313 Point3f old_third_pro = plane.Projection(Inverse(rotation).Rotate(third)); // axis 3 projection
314
315 // align axis 1 to primary
316 RotateToAlign(old_first,primary);
317
318 Point3f secondary_pro = plane.Projection(secondary); // secondary's projection
319 Point3f new_second_pro = plane.Projection(Inverse(rotation).Rotate(second)); // axis 2 projection after the first rotation
320
321 if( secondary.Norm() > EPSILON*size && secondary_pro.Norm() > EPSILON ){ // secondary is not null nor parallel to primary
322 // align axis 2 projection after the first rotation to secondary's projection
323 secondary_pro.Normalize();
324 RotateToAlign(new_second_pro,secondary_pro);
325 return;
326 }
327
328 if ( old_second_pro.Norm() > EPSILON ) { // can realign axis 2
329 // align axis 2 projection after the first rotation to old axis 2 projection
330 old_second_pro.Normalize();
331 RotateToAlign(new_second_pro,old_second_pro);
332 return;
333 }
334
335 // realign axis 3
336 Point3f new_third_pro = plane.Projection(Inverse(rotation).Rotate(third));// axis 3 projection after the first rotation
337 assert(old_third_pro.Norm() > EPSILON ); // old axis 3 projection should not be null
338 // align axis 3 projection after the first rotation to old axis 3 projection
339 old_third_pro.Normalize();
340 RotateToAlign(new_third_pro,old_third_pro);
341 }
342
343 void MovableCoordinateFrame::Move(const Similarityf track)
344 {
345 position = position + track.tra;
346 rotation = rotation * Inverse(track.rot);
347 }
348
349 void MovableCoordinateFrame::RotateToAlign(const Point3f source, const Point3f dest)
350 {
351 const float EPSILON=1e-6;
352 // source and dest must be versors
353 assert( math::Abs(source.Norm() - 1) < EPSILON);
354 assert( math::Abs(dest.Norm() - 1) < EPSILON);
355
356 Point3f axis = dest ^ source;
357 float sinangle = axis.Norm();
358 float cosangle = dest * source;
359 float angle = math::Atan2(sinangle,cosangle);
360
361 if( math::Abs(angle) < EPSILON )
362 return; // angle ~ 0, aborting
363
364 if( math::Abs(math::Abs(angle)-M_PI) < EPSILON){
365 // must find a axis to flip on
366 Plane3f plane(0,source);
367 axis=plane.Projection(Point3f(1,0,0)); // project a "random" point on source's normal plane
368 if(axis.Norm() < EPSILON){ // source was ~ [1,0,0]...
369 axis=plane.Projection(Point3f(0,1,0));
370 assert(axis.Norm() > EPSILON); // this point must be good
371 }
372 }
373 rotation = rotation * Quaternionf(angle,axis);
374 }