Debian Codegraph @

apps/demos/noodle/LineManip.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 //
38 // A very specialized node; this manipulator completely takes care of
39 // editing a set of line segments. This is somewhat different from
40 // other manipulators, because you insert it in the scene graph and it
41 // edits itself (instead of pointing it at the thing you want it to
42 // edit).
43 //
44
45 #include <cstdio>
46 #include <assert.h>
47
48 #include <Inventor/SbColor.h>
49 #include <Inventor/SoPickedPoint.h>
50 #include <Inventor/actions/SoHandleEventAction.h>
51 #include <Inventor/details/SoDetail.h>
52 #include <Inventor/details/SoLineDetail.h>
53 #include <Inventor/details/SoPointDetail.h>
54 #include <Inventor/elements/SoViewVolumeElement.h>
55 #include <Inventor/elements/SoViewportRegionElement.h>
56 #include <Inventor/events/SoEvent.h>
57 #include <Inventor/events/SoKeyboardEvent.h>
58 #include <Inventor/events/SoLocation2Event.h>
59 #include <Inventor/events/SoMouseButtonEvent.h>
60 #include <Inventor/misc/SoState.h>
61 #include <Inventor/nodes/SoCoordinate3.h>
62 #include <Inventor/nodes/SoDrawStyle.h>
63 #include <Inventor/nodes/SoLightModel.h>
64 #include <Inventor/nodes/SoLineSet.h>
65 #include <Inventor/nodes/SoPointSet.h>
66 #include <Inventor/nodes/SoTransform.h>
67 #include <Inventor/projectors/SbLineProjector.h>
68 #include <Inventor/projectors/SbPlaneProjector.h>
69 #include <Inventor/sensors/SoFieldSensor.h>
70
71 #include "LineManip.h"
72
73 SO_NODE_SOURCE(LineManip2);
74
75 //
76 // Constructor; sets up scene graph
77 //
78 LineManip2::LineManip2()
79 {
80 SO_NODE_CONSTRUCTOR(LineManip2);
81
82 coord = new SoCoordinate3();
83 addChild(coord);
84 coord->point.deleteValues(0);
85
86 pset = new SoPointSet;
87 pset->numPoints.setValue(0);
88 addChild(pset);
89
90 drawStyle = new SoDrawStyle;
91 drawStyle->style = SoDrawStyle::LINES;
92 addChild(drawStyle);
93
94 lset = new SoLineSet;
95 lset->numVertices.setValue(0);
96 addChild(lset);
97
98 current_coord = -1;
99 planeProj = new SbPlaneProjector(FALSE);
100 planeProj->setPlane( SbPlane( SbVec3f(0,0,1), 0 ) );
101 lineProj = new SbLineProjector();
102 lineProj->setLine( SbLine( SbVec3f(0,0,0),SbVec3f(0,0,1) ) );
103
104 hilightSize = 0.05;
105
106 initHilightStuff();
107
108 myHandleEventAction = NULL;
109
110 // This sensor will insure that the pset and lset nodes will have
111 // the correct number of points.
112 coordFieldSensor = new SoFieldSensor(&LineManip2::coordFieldSensorCB, this);
113 coordFieldSensor->attach( &coord->point );
114 coordFieldSensor->setPriority(0);
115 }
116
117 //
118 // Destructor. This will be called when this is unref'ed.
119 LineManip2::~LineManip2()
120 {
121 //
122 // Don't need to delete children; they will have already been
123 // unref'ed and deleted (since this is derived from SoGroup).
124 //
125
126 delete planeProj;
127 delete lineProj;
128 delete coordFieldSensor;
129 }
130
131 //
132 // Magic stuff to register this node with the database
133 //
134 void
135 LineManip2::initClass()
136 {
137 SO_NODE_INIT_CLASS(LineManip2, SoSeparator, "Separator");
138 }
139
140 //
141 // This tiny routine is how this Node gets used. After creating a
142 // LineManip2, call this routine and put a data sensor on the Coordinate3
143 // returned.
144 //
145 SoCoordinate3 *
146 LineManip2::getCoordinate3()
147 {
148 return coord;
149 }
150
151 //
152 // This tiny routine is how this Node gets used. After creating a
153 // LineManip2, call this routine and put a data sensor on the Coordinate3
154 // returned.
155 //
156 void
157 LineManip2::setCoordinate3( SoCoordinate3 *newNode )
158 {
159 if ( newNode == coord)
160 return;
161
162 if ( coord != NULL ) {
163 removeChild( coord );
164 coordFieldSensor->detach();
165 }
166 if ( newNode != NULL ) {
167 insertChild(newNode,0);
168 coordFieldSensor->attach( &newNode->point );
169 }
170 coord = newNode;
171
172 coordFieldSensorCB( this, NULL);
173 }
174
175 //
176 // For setting the plane that the manip works in. You pass it a normal.
177 //
178 void
179 LineManip2::setPlaneNormal( const SbVec3f &newNormal )
180 {
181 SbVec3f zeroVec(0,0,0);
182 planeNormal = newNormal;
183 planeProj->setPlane( SbPlane( planeNormal, zeroVec ) );
184 lineProj->setLine( SbLine( zeroVec, zeroVec + planeNormal ) );
185 hilightTransform->rotation.setValue( SbRotation(SbVec3f(0,0,1), SbVec3f(planeNormal)));
186 }
187
188 //
189 // Note:
190 // This node eats events. It uses the entire surface of the render
191 // area as its playground, sucking up mouse motion, left mouse and
192 // backspace/delete events, even if the mouse isn't on top of it.
193 //
194 void
195 LineManip2::handleEvent(SoHandleEventAction *ha)
196 {
197 myHandleEventAction = ha;
198
199 const SoEvent *e = ha->getEvent();
200
201 if (SO_MOUSE_PRESS_EVENT(e, BUTTON1)) {
202 if (dragStart())
203 ha->setHandled();
204 }
205 else if (e->isOfType(SoLocation2Event::getClassTypeId())) {
206 // If not dragging, locate hilight
207 if (ha->getGrabber() != this) {
208 if (locateHilight())
209 ha->setHandled();
210 }
211 else { // Are dragging, move coordinate:
212 moveCoord();
213 ha->setHandled();
214 }
215 }
216 else if (SO_MOUSE_RELEASE_EVENT(e, BUTTON1) &&
217 ha->getGrabber() == this) {
218 ha->releaseGrabber();
219 ha->setHandled();
220 }
221 else if (SO_KEY_PRESS_EVENT(e, BACKSPACE) ||
222 SO_KEY_PRESS_EVENT(e, DELETE)) {
223 if (remove())
224 ha->setHandled();
225 // If we are in mid-motion and we remove the point,
226 // we better stop grabber, cause we got nothin to grab anymore.
227 if (ha->getGrabber() == this)
228 ha->releaseGrabber();
229 current_coord = -1;
230 }
231 }
232
233 static SbColor locateColor(0.5, 0.5, 0.5);
234 static SbColor pickColor(0.9, 0.9, 0.9);
235
236 //
237 // Figures out which part of the line manipulator is picked by the
238 // given detail. This will also set current_position and
239 // current_coord.
240 //
241 #ifdef __C_PLUS_PLUS_2
242 LineManipPart
243 #else
244 LineManip2::Part
245 #endif
246 LineManip2::whichPart()
247 {
248 // Can't pick if we don't have an action, now can we?
249 if (myHandleEventAction == NULL)
250 return NOTHING;
251
252 // We're also out of luck if there's no coord node.
253 if (coord == NULL)
254 return NOTHING;
255
256 const SoPickedPoint *picked_point = myHandleEventAction->getPickedPoint();
257
258 projectMouse(current_position);
259
260 int numCoords = coord->point.getNum();
261
262 if (numCoords == 0 || picked_point == NULL) {
263 //
264 // Endpoints
265 // Pick the endPoint that's closer to where the mouse is now.
266 if (numCoords == 0) {
267 current_coord = -1;
268 }
269 else {
270 SbVec3f v1, v2;
271 v1 = current_position - coord->point[0];
272 v2 = current_position - coord->point[numCoords-1];
273 //
274 // Choose the endPoint closer to the current position.
275 //
276 if (v1.dot(v1) < v2.dot(v2)) {
277 current_coord = 0;
278 }
279 else {
280 current_coord = numCoords;
281 }
282 }
283 return ENDPOINTS;
284 }
285
286 const SoPath *dpath = picked_point->getPath();
287 const SoDetail *detail = picked_point->getDetail();
288
289 if (dpath->containsNode(pset)) {
290 current_coord =
291 ((SoPointDetail *)detail)->getCoordinateIndex();
292 return POINTS;
293 }
294 if (dpath->containsNode(lset)) {
295 current_coord =
296 ((SoLineDetail *)detail)->getPartIndex();
297 return LINES;
298 }
299 return NOTHING;
300 }
301
302 //
303 // Called when the mouse goes down to figure out what is being picked.
304 //
305 SbBool
306 LineManip2::dragStart()
307 {
308 if ( coord == NULL)
309 return FALSE;
310
311 SbBool gotSomething = TRUE;
312
313 switch(whichPart()) {
314 case POINTS:
315 myHandleEventAction->setGrabber(this);
316 hilightVertex(coord->point[current_coord], pickColor);
317 break;
318
319 case LINES:
320 // Create a new vertex
321 coord->point.insertSpace(current_coord+1, 1);
322 coord->point.set1Value(current_coord+1, current_position);
323 myHandleEventAction->setGrabber(this);
324 ++current_coord;
325 hilightVertex(coord->point[current_coord], pickColor);
326 break;
327
328 case ENDPOINTS:
329 // Create a new vertex
330 if (current_coord != -1) {
331 coord->point.insertSpace(current_coord, 1);
332 }
333 else current_coord = 0;
334 coord->point.set1Value(current_coord, current_position);
335 myHandleEventAction->setGrabber(this);
336 hilightVertex(coord->point[current_coord], pickColor);
337 break;
338
339 case NOTHING:
340 current_coord = -1;
341 removeHilights();
342 gotSomething = FALSE;
343 break;
344 }
345
346 return gotSomething;
347 }
348
349 //
350 // Called by SoHandleEventAction when we grab events
351 // using myHandleEventAction->setGrabber( this );
352 //
353 // We will record the viewport and viewvolume.
354 // This is because once we've grabbed, we can't rely on the new handleEvent
355 // action to have correct view information.
356 // Why? Because grabbing bypasses traversal and sends the event directly
357 // to this node. The camera node has no chance to modify the state.
358 //
359
360 void
361 LineManip2::grabEventsSetup()
362 {
363 extractViewingParams(myHandleEventAction);
364 savedRenderCachingVal
365 = (SoSeparator::CacheEnabled) renderCaching.getValue();
366 renderCaching = OFF;
367 }
368
369
370 void
371 LineManip2::grabEventsCleanup()
372 {
373 removeHilights();
374 current_coord = -1;
375
376 renderCaching = savedRenderCachingVal;
377 }
378
379 void
380 LineManip2::extractViewingParams( SoHandleEventAction *ha )
381 {
382 if (ha == NULL) {
383 // If the action is NULL, use default values for view info.
384 myViewVolume.ortho(-1,1,-1,1,1,10);
385 myVpRegion = SbViewportRegion(1,1);
386 }
387 else {
388 // Get the view info from the action.
389 SoState *state = ha->getState();
390 myViewVolume = SoViewVolumeElement::get( state );
391 myVpRegion = SoViewportRegionElement::get( state );
392 }
393 }
394
395 //
396 // Called when the backspace or delete keys is hit. Note that if the
397 // mouse is close to the middle of a line segment (so a pick would
398 // insert a point) nothing is done. This used to delete the entire
399 // line segment, but that was not intuitive. That might be a good
400 // interface for creating disconnected line segments, though...
401 //
402 SbBool
403 LineManip2::remove()
404 {
405 if (coord == NULL)
406 return FALSE;
407
408 SbBool removedSomething = TRUE;
409
410 if ( myHandleEventAction->getGrabber() == this ) {
411 // Remove whatever we're working on.
412 if (current_coord != -1)
413 coord->point.deleteValues(current_coord, 1);
414 }
415 else {
416 // Remove whatever the cursor indicates should be gone.
417 switch (whichPart()) {
418 case POINTS:
419 coord->point.deleteValues(current_coord, 1);
420 break;
421
422 case LINES:
423 // Do nothing.
424 break;
425
426 case ENDPOINTS:
427 if (current_coord != -1) {
428 if (current_coord != coord->point.getNum())
429 coord->point.deleteValues(current_coord, 1);
430 else
431 coord->point.deleteValues(current_coord-1, 1);
432 }
433 break;
434
435 case NOTHING:
436 removedSomething = FALSE;
437 break;
438 }
439 }
440
441 removeHilights();
442
443 return removedSomething;
444 }
445
446 //
447 // Called when the mouse is moving around, but the left mouse button
448 // is up.
449 //
450 SbBool
451 LineManip2::locateHilight()
452 {
453 if (coord == NULL )
454 return FALSE;
455
456 SbBool hilitSomething = TRUE;
457
458 switch (whichPart()) {
459 case POINTS:
460 hilightVertex(coord->point[current_coord], locateColor);
461 break;
462
463 case LINES:
464 hilightLine(current_coord, current_position, locateColor);
465 break;
466
467 case ENDPOINTS:
468 if (current_coord == -1) {
469 // There are no coordinates at all. We can't perform a
470 // hilight.
471 hilitSomething = FALSE;
472 }
473 else if (current_coord == 0)
474 hilightLine(-1, current_position, locateColor);
475 else
476 hilightLine(current_coord-1, current_position, locateColor);
477 break;
478
479 case NOTHING:
480 hilitSomething = FALSE;
481 break;
482 }
483
484 return hilitSomething;
485 }
486
487 //
488 // Called to move the selected point when the mouse is moved and the
489 // left mouse button is pressed.
490 //
491 void
492 LineManip2::moveCoord()
493 {
494 if (coord == NULL)
495 return;
496
497 assert(current_coord > -1 && current_coord < coord->point.getNum());
498 updateProjectors( coord->point[current_coord] );
499 SbVec3f t;
500 projectMouse(t);
501 coord->point.set1Value(current_coord, t);
502
503 hilightVertex(coord->point[current_coord], pickColor);
504 }
505
506 void
507 LineManip2::updateProjectors( const SbVec3f &curPt )
508 {
509 SbVec3f dir = lineProj->getLine().getDirection();
510 lineProj->setLine( SbLine( curPt, curPt + dir ) );
511
512 dir = planeProj->getPlane().getNormal();
513 planeProj->setPlane( SbPlane( dir, curPt ) );
514 }
515
516 //
517 // Does the grunt work of projecting the mouse onto the plane
518 // containing the line manipulator.
519 //
520 void
521 LineManip2::projectMouse(SbVec3f &result)
522 {
523 const SoEvent *e = myHandleEventAction->getEvent();
524
525 // If we're grabbing, we've already got our view parameters.
526 // If not, then we'd better get them now.
527 // NOTE: If you're grabbing and you try to get view parameters, you'll
528 // get a bad answer. Grabbing causes you to skip over camera
529 // nodes directly to this one. So the state has no valid view info.
530 if ( myHandleEventAction->getGrabber() != this )
531 extractViewingParams(myHandleEventAction);
532
533 // If alt key is down, then we move in line perpendicular to plane
534 if ( e->wasAltDown() == FALSE ) {
535 planeProj->setViewVolume(myViewVolume);
536 result = planeProj->project(e->getNormalizedPosition(myVpRegion));
537 }
538 else {
539 lineProj->setViewVolume(myViewVolume);
540 result = lineProj->project(e->getNormalizedPosition(myVpRegion));
541 }
542 }
543
544 // This sensor will insure that the pset and lset nodes will have
545 // the correct number of points.
546 void
547 LineManip2::coordFieldSensorCB( void *myPtr, SoSensor *)
548 {
549 LineManip2 *myself = (LineManip2 *)myPtr;
550
551 int numPoints = 0;
552 if (myself->coord)
553 numPoints = myself->coord->point.getNum();
554
555 if (myself->pset)
556 myself->pset->numPoints.setValue(numPoints);
557 if (myself->lset)
558 myself->lset->numVertices.setValue(numPoints);
559 }