Debian Codegraph @

vcg/wrap/gui/trackmode.cpp

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
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
26 $Log: trackmode.cpp,v $
27 Revision 1.29 2008/02/26 18:00:41 benedetti
28 corrected CylinderMode::Apply
29
30 Revision 1.28 2008/02/24 18:05:08 ponchio
31 Should work as before. I didn't test cylinder and other exotic modes.
32
33 Revision 1.27 2008/02/24 14:37:00 ponchio
34 Restored trackball functionality. Not very much tested, and code will need some
35 cleanup.
36
37 Revision 1.26 2008/02/15 20:56:41 benedetti
38 removed some variable initialization related warning, added snap to CylinderMode
39
40 Revision 1.25 2007/10/24 10:39:07 ponchio
41 #include <gl/glew.h> -> #include <GL/glew.h>
42
43 (it is case sensitive under linux...)
44
45 Revision 1.24 2007/07/09 22:47:18 benedetti
46 Removed using namespace std and modified accordingly.
47
48 Revision 1.23 2007/06/13 17:15:09 benedetti
49 Added one-level undo system and sticky trackmodes.
50
51 Revision 1.22 2007/05/28 08:10:47 fiorin
52 Removed type cast warnings
53
54 Revision 1.21 2007/05/16 08:44:05 ganovelli
55 added inclusion of glew.h
56
57 Revision 1.20 2007/05/15 14:58:57 benedetti
58 Main restructuring. added many new modes
59
60 Revision 1.19 2006/08/30 07:01:54 cignoni
61 Reverted to version 1.17. Version 1.18 was wrongly done starting from a outdated version.
62
63 Revision 1.17 2006/07/26 13:54:45 cignoni
64 Reversed the direction of wheel scaling and added middle mouse panning
65
66 Revision 1.16 2006/02/13 13:10:27 cignoni
67 Added Zmode for moving objects along the perpendicular to the viewplane
68
69 Revision 1.15 2006/01/12 15:40:05 cignoni
70 Corrected small bugs on rotating after scaling+translating
71 changed void PlaneMode::Apply and void SphereMode::Apply
72
73 Revision 1.14 2005/07/15 16:39:30 callieri
74 in SphereMode::Hit added a check on the sphere intersection, if no intersection, calculating distance could generate a NAN exception
75
76 Revision 1.13 2005/06/29 15:22:26 callieri
77 changed the name of some intersection functions to avoid ambiguity
78
79 Revision 1.12 2005/02/11 11:44:07 tommyfranken
80 Trackball translation corrected
81
82 Revision 1.11 2005/02/02 16:46:41 pietroni
83 some warning corrected
84
85 Revision 1.10 2005/01/14 15:44:03 ganovelli
86 PlaneMode completed
87
88 Revision 1.9 2004/09/09 22:59:21 cignoni
89 Removed many small warnings
90
91 Revision 1.8 2004/07/18 06:54:08 cignoni
92 Added Scaling
93
94 Revision 1.7 2004/07/11 22:06:56 cignoni
95 Added scaling by wheel
96
97 Revision 1.6 2004/06/09 14:01:13 cignoni
98 Heavily restructured. To be completed only rotation works...
99
100 Revision 1.5 2004/05/14 03:15:09 ponchio
101 Redesigned partial version.
102
103 Revision 1.4 2004/05/07 12:46:08 cignoni
104 Restructured and adapted in a better way to opengl
105
106 Revision 1.3 2004/04/07 10:54:11 cignoni
107 Commented out unused parameter names and other minor warning related issues
108
109 Revision 1.2 2004/03/25 14:55:25 ponchio
110 Adding copyright.
111
112
113 ****************************************************************************/
114
115 #include <GL/glew.h>
116
117 #include <wrap/gui/trackmode.h>
118 #include <wrap/gui/trackball.h>
119 #include <wrap/gui/trackutils.h>
120
121 using namespace vcg;
122 using namespace vcg::trackutils;
123
124 // the old implementation is not used anymore, some of the old support functions,
125 // like HitViewPlane, GetViewPlane, HitHyper and SphereMode::Hit were made
126 // class-independent and moved to trackutils.h
127 /*
128 void TrackMode::Apply(Trackball *trackball, float WheelNotch) {
129 }
130 void ScaleMode::Apply(Trackball *tb, Point3f new_point) {
131 }
132 Plane3f TrackMode::GetViewPlane(const View<float> &camera, const Point3f &center) {
133 }
134 Point3f TrackMode::HitViewPlane(Trackball *tb, const Point3f &p) {
135 }
136 void SphereMode::Apply(Trackball *tb, Point3f new_point) {
137 }
138 bool SphereMode::HitHyper(Point3f center, float radius, Point3f viewpoint, Plane3f vp, Point3f hitplane, Point3f &hit)
139 {
140 }
141 Point3f SphereMode::Hit(Trackball *tb, const Point3f &p) {
142 }
143 void PlaneMode::Apply(Trackball *tb, Point3f new_point) {
144
145 }
146 void ZMode::Apply(Trackball *tb, Point3f new_point) {
147 }
148 */
149
150 // Track mode implementation, dummy.
151 void TrackMode::Apply (Trackball * , float ){}
152
153 void TrackMode::Apply (Trackball * , Point3f ){}
154
155 void TrackMode::Draw(Trackball * ){}
156
157 void TrackMode::SetAction (){}
158
159 void TrackMode::Reset (){}
160
161 bool TrackMode::isSticky() {
162 return false;
163 }
164
165 void TrackMode::Undo(){}
166
167 // draw an inactive trackball
168 void InactiveMode::Draw(Trackball * tb){
169 DrawSphereIcon(tb,false);
170 }
171
172 // Sphere mode implementation.
173 // the most important function; given a new point in window coord,
174 // it update the transformation computed by the trackball.
175 // General scheme : the transformation is a function of just
176 // the begin and current mouse positions, with greater precision
177 // is function of just two 3d points over the manipulator.
178 void SphereMode::Apply (Trackball * tb, Point3f new_point)
179 {
180 Point3f hitOld = HitSphere (tb, tb->last_point);
181 Point3f hitNew = HitSphere (tb, new_point);
182 tb->Hits.push_back (hitNew);
183 Point3f center = tb->center;
184 Point3f axis = (hitNew - center) ^ (hitOld - center);
185 // Figure out how much to rotate around that axis.
186 float phi = Distance (hitNew, hitOld) / tb->radius;
187 // tb->track.rot = tb->last_track.rot * Quaternionf (-phi, axis);
188 tb->track.rot = Quaternionf (-phi, axis) * tb->last_track.rot;
189 }
190
191 void SphereMode::Draw(Trackball * tb){
192 DrawSphereIcon(tb,true );
193 }
194
195 // Pan mode implementation.
196 void PanMode::Apply (Trackball * tb, Point3f new_point)
197 {
198 Point3f hitOld = HitViewPlane (tb, tb->last_point);
199 Point3f hitNew = HitViewPlane (tb, new_point);
200 tb->Translate (hitNew - hitOld);
201 }
202
203 void PanMode::Draw(Trackball * tb){
204 DrawSphereIcon(tb,true );
205 DrawUglyPanMode(tb);
206 }
207
208 // Z mode implementation.
209 void ZMode::Apply (Trackball * tb, float WheelNotch)
210 {
211 Point3f dir= (GetViewPlane (tb->camera, tb->center)).Direction();
212 dir.Normalize();
213 tb->Translate (dir * (-WheelNotch));
214 }
215
216 void ZMode::Apply (Trackball * tb, Point3f new_point)
217 {
218 Point3f dir= (GetViewPlane (tb->camera, tb->center)).Direction();
219 dir.Normalize();
220 tb->Translate (dir * ( -2.0f * getDeltaY(tb,new_point)));
221 }
222
223 void ZMode::Draw(Trackball * tb){
224 DrawSphereIcon(tb,true );
225 DrawUglyZMode(tb);
226 }
227
228 // Scale mode implementation.
229 void ScaleMode::Apply (Trackball * tb, float WheelNotch)
230 {
231 tb->track.sca *= pow (1.2f, -WheelNotch);
232 }
233
234 void ScaleMode::Apply (Trackball * tb, Point3f new_point)
235 {
236 tb->track.sca = tb->last_track.sca * pow (3.0f, -(getDeltaY(tb,new_point)));
237 }
238
239 void ScaleMode::Draw(Trackball * tb){
240 DrawSphereIcon(tb,true );
241 DrawUglyScaleMode(tb);
242 }
243
244 // Axis mode implementation.
245 void AxisMode::Apply (Trackball * tb, float WheelNotch)
246 {
247 tb->Translate (axis.Direction () * (WheelNotch / 10.0f));
248 }
249
250 void AxisMode::Apply (Trackball * tb, Point3f new_point)
251 {
252 std::pair< Point3f,bool > hitOld = HitNearestPointOnAxis (tb, axis, tb->last_point);
253 std::pair< Point3f,bool > hitNew = HitNearestPointOnAxis (tb, axis, new_point);
254 if (hitOld.second && hitNew.second){
255 tb->Translate (hitNew.first - hitOld.first);
256 }
257 }
258
259 void AxisMode::Draw(Trackball * tb){
260 DrawSphereIcon(tb,true );
261 DrawUglyAxisMode(tb,axis);
262 }
263
264 // Plane mode implementation.
265 void PlaneMode::Apply (Trackball * tb, Point3f new_point)
266 {
267 std::pair< Point3f, bool > hitOld = HitPlane(tb,tb->last_point,plane);
268 std::pair< Point3f, bool > hitNew = HitPlane(tb,new_point,plane);
269 if(hitOld.second && hitNew.second){
270 tb->Translate (hitNew.first - hitOld.first);
271 }
272 }
273
274 void PlaneMode::Draw(Trackball * tb){
275 DrawSphereIcon(tb,true );
276 DrawUglyPlaneMode(tb, plane);
277 }
278
279 // Cylinder mode implementation.
280 void CylinderMode::Apply (Trackball * tb, float WheelNotch)
281 {
282 const float PI2=6.283185307179586232f;
283 float angle= (snap==0.0) ? WheelNotch/(tb->radius * PI2) : WheelNotch * snap;
284 tb->track.rot = tb->last_track.rot * Quaternionf (angle,axis.Direction());
285 }
286
287 void CylinderMode::Apply (Trackball * tb, Point3f new_point)
288 {
289 Plane3f viewplane=GetViewPlane (tb->camera, tb->center);
290 Line3f axisproj;
291 axisproj=ProjectLineOnPlane(axis,viewplane);
292 float angle;
293 const float EPSILON=0.005f; // this IS scale independent
294 if(axisproj.Direction().Norm() < EPSILON){
295 angle=(10.0f * getDeltaY(tb,new_point)) / tb->radius;
296 } else {
297 Point3f hitOld = HitViewPlane (tb, tb->last_point);
298 Point3f hitNew = HitViewPlane (tb, new_point);
299 axisproj.Normalize();
300 Point3f plusdir= viewplane.Direction() ^ axisproj.Direction();
301 float distOld = signedDistance(axisproj,hitOld,plusdir);
302 float distNew = signedDistance(axisproj,hitNew,plusdir);
303 angle= (distNew-distOld) / tb->radius;
304 }
305 if(snap>0.0){
306 angle = ((angle<0)?-1:1)* floor((((angle<0)?-angle:angle)/snap)+0.5)*snap;
307 }
308 // tb->track.rot = tb->last_track.rot * Quaternionf (angle,axis.Direction());
309 tb->track.rot = Quaternionf (-angle,axis.Direction()) * tb->last_track.rot;
310 }
311
312 void CylinderMode::Draw(Trackball * tb){
313 DrawSphereIcon(tb,true );
314 DrawUglyCylinderMode(tb,axis);
315 }
316
317 // Path mode implementation.
318 void PathMode::Init(const std::vector < Point3f > &pts)
319 {
320 unsigned int npts = int(pts.size());
321 assert(npts >= 2);
322 points.reserve(npts);
323 for(unsigned int i=0;i<npts;i++){
324 points.push_back(pts[i]);
325 }
326 path_length=0.0f;
327 min_seg_length=Distance(points[0],points[1]);
328 float seg_length;
329 for(unsigned int i=1;i<npts;i++){
330 seg_length=Distance(points[i-1],points[i]);
331 path_length += seg_length;
332 min_seg_length = std::min(seg_length,min_seg_length);
333 }
334 if(wrap){
335 seg_length=Distance(points[npts-1],points[0]);
336 path_length += seg_length;
337 min_seg_length = std::min(seg_length,min_seg_length);
338 }
339 }
340
341 void PathMode::Reset()
342 {
343 current_state=initial_state;
344 }
345
346 Point3f PathMode::SetStartNear(Point3f point)
347 {
348 float p0_state=0;
349 Point3f p0,p1;
350 float nearest_state=0;
351 Point3f nearest_point=points[0];
352 float nearest_distance=Distance(nearest_point,point);
353 unsigned int npts = int(points.size());
354 for(unsigned int i = 1;i <= npts;i++){
355 if( i == npts){
356 if (wrap){
357 p0=points[npts-1];
358 p1=points[0];
359 } else {
360 break;
361 }
362 } else {
363 p0=points[i-1];
364 p1=points[i];
365 }
366 Point3f segment_point=ClosestPoint(Segment3f(p0,p1),point);
367 float distance=Distance(segment_point,point);
368 if(distance<nearest_distance){
369 nearest_point=segment_point;
370 nearest_distance=distance;
371 nearest_state=p0_state+(Distance(p0,nearest_point)/path_length);
372 }
373 float segment_norm= Distance(p0,p1) / path_length;
374 p0_state+=segment_norm;
375 }
376 assert( nearest_state >= 0.0 );
377 if(nearest_state > 1.0){
378 nearest_state=1.0;
379 nearest_point=( wrap ? points[0] : points[npts-1] );
380 }
381 initial_state=nearest_state;
382 return nearest_point;
383 }
384
385 void PathMode::GetPoints(float state, Point3f & point, Point3f & prev_point, Point3f & next_point)
386 {
387 assert(state >= 0.0f);
388 assert(state <= 1.0f);
389 float remaining_norm=state;
390 Point3f p0(0,0,0),p1(0,0,0);
391 unsigned int npts = int(points.size());
392 for(unsigned int i = 1;i <= npts;i++){
393 if( i == npts){
394 if (wrap){
395 p0=points[npts-1];
396 p1=points[0];
397 } else {
398 break;
399 }
400 } else {
401 p0=points[i-1];
402 p1=points[i];
403 }
404 float segment_norm= Distance(p0,p1) / path_length;
405 if (segment_norm < remaining_norm){
406 remaining_norm -= segment_norm;
407 continue;
408 }
409 prev_point = p0;
410 next_point = p1;
411 float ratio= remaining_norm / segment_norm;
412 point = prev_point + (( next_point - prev_point ) * ratio);
413 const float EPSILON=min_seg_length * 0.01f;
414 if(Distance(point,prev_point) < EPSILON){
415 point=prev_point;
416 if (i > 1){
417 prev_point=points[i-2];
418 } else if (wrap){
419 prev_point=points[npts-1];
420 }
421 } else if (Distance(point,next_point) < EPSILON){
422 point=next_point;
423 if( i < (npts-1)){
424 next_point=points[i+1];
425 } else {
426 if (wrap){
427 next_point=points[1];
428 } else {
429 next_point=points[npts-1];
430 }
431 }
432 }
433 return;
434 }
435 // rounding errors can lead out of the for..
436 prev_point = p0;
437 point = p1;
438 if (wrap){
439 next_point=points[1];
440 }else{
441 next_point = points[npts-1];
442 }
443 }
444
445 void PathMode::Apply (Trackball * tb, float WheelNotch)
446 {
447 undo_current_state=current_state;
448 undo_old_hitpoint=old_hitpoint;
449
450 const float STEP_COEFF = min_seg_length * 0.5f;
451 float delta=(WheelNotch*STEP_COEFF)/path_length;
452 Point3f old_point,new_point,prev_point,next_point;
453 GetPoints(current_state,old_point,prev_point,next_point);
454 current_state=Normalize(current_state+delta);
455 GetPoints(current_state,new_point,prev_point,next_point);
456 tb->Translate (new_point - old_point);
457 }
458
459 float PathMode::Normalize(float state)
460 {
461 if ( wrap ) {
462 double intpart;
463 float fractpart;
464 fractpart =(float) modf(state,&intpart);
465 if( fractpart < 0.0f )
466 fractpart += 1.0f;
467 return fractpart;
468 }
469 if ( state < 0.0f )
470 return 0.0f;
471 if ( state > 1.0f )
472 return 1.0f;
473 return state;
474 }
475
476 int PathMode::Verse(Point3f reference_point,Point3f current_point,Point3f prev_point,Point3f next_point)
477 {
478 Point3f reference_dir = reference_point - current_point ;
479 Point3f prev_dir = prev_point - current_point ;
480 Point3f next_dir = next_point - current_point ;
481 const float EPSILON=min_seg_length * 0.005f;
482 if (reference_dir.Norm() < EPSILON)
483 reference_dir = Point3f(0,0,0);
484 if (prev_dir.Norm() < EPSILON)
485 prev_dir = Point3f(0,0,0);
486 if (next_dir.Norm() < EPSILON)
487 next_dir = Point3f(0,0,0);
488 reference_dir.Normalize();
489 prev_dir.Normalize();
490 next_dir.Normalize();
491 float prev_coeff,next_coeff;
492 prev_coeff = prev_dir * reference_dir;
493 next_coeff = next_dir * reference_dir;
494 if (prev_coeff < 0.0f)
495 prev_coeff = 0.0f;
496 if (next_coeff < 0.0f)
497 next_coeff = 0.0f;
498 if( (prev_coeff == 0.0f) && (next_coeff == 0.0f)){
499 return 0;
500 }
501 if ( prev_coeff <= next_coeff ){
502 return 1;
503 }
504 return -1;
505 }
506
507 float PathMode::HitPoint(float state, Ray3fN ray, Point3f &hit_point)
508 {
509 Point3f current_point, next_point, prev_point;
510 GetPoints(state,current_point,prev_point,next_point);
511
512 Point3f closest_point;
513 closest_point=ray.ClosestPoint(current_point);
514 int verse=Verse(closest_point,current_point,prev_point,next_point);
515 if (verse == 0){
516 hit_point=current_point;
517 return 0.0f;
518 }
519
520 Segment3f active_segment;
521 if (verse > 0){
522 active_segment=Segment3f(current_point,next_point);
523 } else {
524 active_segment= Segment3f(current_point,prev_point);
525 }
526
527 hit_point=ClosestPoint(active_segment,closest_point);
528
529 return verse * ((hit_point-current_point).Norm() / path_length);
530 }
531
532 void PathMode::SetAction (){
533 Point3f temp1,temp2;
534 GetPoints(current_state,old_hitpoint,temp1,temp2);
535 }
536
537 void PathMode::Apply (Trackball * tb, Point3f new_point)
538 {
539 undo_current_state=current_state;
540 undo_old_hitpoint=old_hitpoint;
541
542 Ray3fN ray = line2ray(tb->camera.ViewLineFromWindow (new_point));
543 Point3f hit_point;
544 float delta_state=HitPoint(current_state,ray,hit_point);
545 current_state=Normalize(current_state+delta_state);
546 tb->Translate (hit_point - old_hitpoint);
547 }
548
549 bool PathMode::isSticky() {
550 return true;
551 }
552
553 void PathMode::Undo(){
554 current_state=undo_current_state;
555 old_hitpoint=undo_old_hitpoint;
556 }
557
558 void PathMode::Draw(Trackball * tb){
559 DrawSphereIcon(tb,true );
560 Point3f current_point,prev_point,next_point;
561 GetPoints(current_state,current_point,prev_point,next_point);
562 DrawUglyPathMode(tb,points,current_point,prev_point,
563 next_point,old_hitpoint,wrap);
564 }
565
566 // Area mode implementation.
567 void AreaMode::Init(const std::vector < Point3f > &pts)
568 {
569 unsigned int npts = int(pts.size());
570
571 assert(npts >= 3);
572 //get the plane
573 Point3f p0=pts[0];
574 unsigned int onethird=(unsigned int)floor(npts/3.0);
575 const float EPSILON = 0.005f;
576 bool pts_not_in_line=false;
577 Point3f a,b;
578 for(unsigned int i=0;i<onethird;i++){
579 a=(pts[(i+ onethird )%npts] - pts[i%npts]).Normalize();
580 b=(pts[(i+(2*onethird))%npts] - pts[i%npts]).Normalize();
581 pts_not_in_line = (a ^ b).Norm() > EPSILON;
582 if(pts_not_in_line){
583 plane.Init( pts[i%npts],
584 pts[(i+(onethird))%npts],
585 pts[(i+(2*onethird))%npts]);
586 break;
587 }
588 }
589 assert(pts_not_in_line);
590 float ncx,ncy,ncz;
591 ncx=fabs(plane.Direction()[0]);
592 ncy=fabs(plane.Direction()[1]);
593 ncz=fabs(plane.Direction()[2]);
594 if(( ncx > ncy ) && ( ncx > ncz )){
595 first_coord_kept=1;
596 second_coord_kept=2;
597 } else if(( ncy > ncx ) && ( ncy > ncz)){
598 first_coord_kept=0;
599 second_coord_kept=2;
600 } else {
601 first_coord_kept=0;
602 second_coord_kept=1;
603 }
604 points.reserve(npts);
605 for(unsigned int i=0;i<npts;i++){
606 points.push_back(plane.Projection(pts[i]));
607 }
608 min_side_length=Distance(points[0],points[1]);
609 for(unsigned int i=1;i<npts;i++){
610 min_side_length=std::min(Distance(points[i-1],points[i]),min_side_length);
611 }
612 rubberband_handle=old_status=status=initial_status=p0;
613 }
614
615 void AreaMode::Reset()
616 {
617 rubberband_handle=old_status=status=initial_status;
618 path.clear();
619 }
620
621 void AreaMode::Apply (Trackball * tb, Point3f new_point)
622 {
623 undo_begin_action=begin_action;
624 undo_status=status;
625 undo_delta_mouse=delta_mouse;
626 undo_old_status=old_status;
627 undo_rubberband_handle=rubberband_handle;
628 undo_path_index=path.size();
629
630 if(begin_action){
631 delta_mouse=tb->camera.Project(status)-new_point;
632 begin_action=false;
633 }
634 std::pair< Point3f, bool > hitNew = HitPlane(tb,new_point+delta_mouse,plane);
635 if(! hitNew.second){
636 return;
637 }
638 Point3f hit_point=hitNew.first;
639 Point3f delta_status=Move(status,hit_point);
640 status += delta_status;
641 tb->Translate (status - old_status);
642 rubberband_handle=hit_point;
643 }
644
645
646 void AreaMode::SetAction ()
647 {
648 begin_action=true;
649 old_status=status;
650
651 path.clear();
652 path.push_back(status);
653 rubberband_handle=status;
654 }
655
656 Point3f AreaMode::Move(Point3f start,Point3f end)
657 {
658 const float EPSILON=min_side_length*0.001f;
659 Point3f pt=start;
660 bool done=false;
661 bool end_inside=Inside(end);
662 while(!done){
663 path.push_back(pt);
664 Segment3f segment(pt,end);
665 bool p_on_side = false;
666 bool hit=false;
667
668 Point3f pside(0,0,0),phit(0,0,0);
669 bool slide=false,mid_inside;
670
671 int np = int(points.size()), i, j;
672 for (i = 0, j = np-1; i < np; j = i++) {
673 Segment3f side(points[i],points[j]);
674 Point3f pseg,psid;
675 std::pair<float,bool> res=SegmentSegmentDistance(segment,side,pseg,psid);
676 if(res.first < EPSILON && ! res.second){
677 float dist= Distance(pt,pseg);
678 if(dist < EPSILON){
679 Point3f pn=ClosestPoint(side,end);
680 if(!p_on_side || (Distance(pn,end)<Distance(end,pside))){
681 pside=pn;
682 p_on_side=true;
683 }
684 } else {
685 if (!hit || Distance(pt,pseg) < Distance(pt,phit)){
686 phit=pseg;
687 hit=true;
688 }
689 }
690 }
691 }
692 if (p_on_side)
693 slide = Distance(pside,pt) > EPSILON;
694
695 if (hit)
696 mid_inside = Inside( pt + ( ( phit - pt ) / 2) );
697
698 if ( !hit && end_inside ){
699 pt = end;
700 done = true;
701 } else if ( hit && (!p_on_side || (p_on_side && mid_inside))) {
702 pt = phit;
703 } else if ( p_on_side && slide) {
704 pt = pside;
705 } else {
706 done = true;
707 }
708 }
709 path.push_back(pt);
710 return pt - start;
711 }
712
713 // adapted from the original C code by W. Randolph Franklin
714 // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
715 bool AreaMode::Inside(Point3f point)
716 {
717 bool inside=false;
718 float x=point[first_coord_kept];
719 float y=point[second_coord_kept];
720 float yi, yj, xi, xj;
721 int i, j, np=int(points.size());
722 for (i = 0, j = np-1; i < np; j = i++) {
723 xi=points[i][first_coord_kept];
724 yi=points[i][second_coord_kept];
725 xj=points[j][first_coord_kept];
726 yj=points[j][second_coord_kept];
727 if ( ( ( (yi<=y) && (y<yj) ) || ( (yj<=y) && (y<yi) ) ) &&
728 ( x < ( xj - xi ) * ( y - yi ) / ( yj - yi ) + xi ) )
729 {
730 inside=!inside;
731 }
732 }
733 return inside;
734 }
735
736 Point3f AreaMode::SetStartNear(Point3f point)
737 {
738 Point3f candidate=plane.Projection(point);
739 if (Inside(candidate)){
740 initial_status=candidate;
741 return initial_status;
742 }
743 Point3f nearest_point=initial_status;
744 float nearest_distance=Distance(nearest_point,candidate);
745 int i, j, np=int(points.size());
746 for (i = 0, j = np-1; i < np; j = i++) {
747 Segment3f side(points[i],points[j]);
748 Point3f side_point=ClosestPoint(side,candidate);
749 float distance=Distance(side_point,candidate);
750 if( distance < nearest_distance ){
751 nearest_point=side_point;
752 nearest_distance=distance;
753 }
754 }
755 initial_status=nearest_point;
756 return initial_status;
757 }
758
759 bool AreaMode::isSticky() {
760 return true;
761 }
762
763 void AreaMode::Undo(){
764 begin_action=undo_begin_action;
765 status=undo_status;
766 delta_mouse=undo_delta_mouse;
767 old_status=undo_old_status;
768 rubberband_handle=undo_rubberband_handle;
769 for(unsigned int i=path.size() - 1; i > undo_path_index; --i)
770 path.pop_back();
771 }
772
773 void AreaMode::Draw(Trackball * tb)
774 {
775 DrawSphereIcon(tb,true );
776 DrawUglyAreaMode(tb,points,status,old_status,plane,path,rubberband_handle);
777 }