|
| 1 | +#include <Inventor/SoDB.h> |
| 2 | +#include <Inventor/SbViewportRegion.h> |
| 3 | +#include <Inventor/SbVec3f.h> |
| 4 | +#include <Inventor/actions/SoCallbackAction.h> |
| 5 | +#include <Inventor/actions/SoGetPrimitiveCountAction.h> |
| 6 | +#include <Inventor/actions/SoRayPickAction.h> |
| 7 | +#include <Inventor/nodes/SoCoordinate3.h> |
| 8 | +#include <Inventor/nodes/SoCube.h> |
| 9 | +#include <Inventor/nodes/SoLineSet.h> |
| 10 | +#include <Inventor/nodes/SoPointSet.h> |
| 11 | +#include <Inventor/nodes/SoSeparator.h> |
| 12 | +#include <Inventor/nodes/SoShape.h> |
| 13 | + |
| 14 | +#include <cassert> |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +struct CallbackCounts { |
| 19 | + int triangles = 0; |
| 20 | + int lines = 0; |
| 21 | + int points = 0; |
| 22 | +}; |
| 23 | + |
| 24 | +void |
| 25 | +triangle_callback(void * userdata, SoCallbackAction *, |
| 26 | + const SoPrimitiveVertex *, const SoPrimitiveVertex *, |
| 27 | + const SoPrimitiveVertex *) |
| 28 | +{ |
| 29 | + static_cast<CallbackCounts *>(userdata)->triangles++; |
| 30 | +} |
| 31 | + |
| 32 | +void |
| 33 | +line_callback(void * userdata, SoCallbackAction *, |
| 34 | + const SoPrimitiveVertex *, const SoPrimitiveVertex *) |
| 35 | +{ |
| 36 | + static_cast<CallbackCounts *>(userdata)->lines++; |
| 37 | +} |
| 38 | + |
| 39 | +void |
| 40 | +point_callback(void * userdata, SoCallbackAction *, const SoPrimitiveVertex *) |
| 41 | +{ |
| 42 | + static_cast<CallbackCounts *>(userdata)->points++; |
| 43 | +} |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +int |
| 48 | +main() |
| 49 | +{ |
| 50 | + SoDB::init(); |
| 51 | + |
| 52 | + SoSeparator * root = new SoSeparator; |
| 53 | + SoCube * cube = new SoCube; |
| 54 | + root->addChild(cube); |
| 55 | + |
| 56 | + SoCoordinate3 * coordinates = new SoCoordinate3; |
| 57 | + coordinates->point.set1Value(0, SbVec3f(-2.0f, 0.0f, 0.0f)); |
| 58 | + coordinates->point.set1Value(1, SbVec3f(-1.0f, 0.0f, 0.0f)); |
| 59 | + coordinates->point.set1Value(2, SbVec3f(2.0f, 0.0f, 0.0f)); |
| 60 | + root->addChild(coordinates); |
| 61 | + |
| 62 | + SoLineSet * lines = new SoLineSet; |
| 63 | + lines->numVertices.set1Value(0, 2); |
| 64 | + root->addChild(lines); |
| 65 | + |
| 66 | + SoPointSet * points = new SoPointSet; |
| 67 | + points->numPoints = 1; |
| 68 | + root->addChild(points); |
| 69 | + root->ref(); |
| 70 | + |
| 71 | + { |
| 72 | + CallbackCounts callbacks; |
| 73 | + SoCallbackAction callback_action; |
| 74 | + callback_action.addTriangleCallback(SoShape::getClassTypeId(), |
| 75 | + triangle_callback, &callbacks); |
| 76 | + callback_action.addLineSegmentCallback(SoShape::getClassTypeId(), |
| 77 | + line_callback, &callbacks); |
| 78 | + callback_action.addPointCallback(SoShape::getClassTypeId(), |
| 79 | + point_callback, &callbacks); |
| 80 | + callback_action.apply(root); |
| 81 | + assert(callbacks.triangles > 0); |
| 82 | + assert(callbacks.lines > 0); |
| 83 | + assert(callbacks.points > 0); |
| 84 | + |
| 85 | + SoGetPrimitiveCountAction count_action; |
| 86 | + count_action.apply(root); |
| 87 | + assert(count_action.getTriangleCount() > 0); |
| 88 | + assert(count_action.getLineCount() > 0); |
| 89 | + assert(count_action.getPointCount() > 0); |
| 90 | + |
| 91 | + SoRayPickAction pick_action(SbViewportRegion(100, 100)); |
| 92 | + pick_action.setRay(SbVec3f(0.0f, 0.0f, 5.0f), |
| 93 | + SbVec3f(0.0f, 0.0f, -1.0f)); |
| 94 | + pick_action.apply(root); |
| 95 | + assert(pick_action.getPickedPoint() != NULL); |
| 96 | + } |
| 97 | + |
| 98 | + root->unref(); |
| 99 | + SoDB::finish(); |
| 100 | + return 0; |
| 101 | +} |
0 commit comments