Shape 20.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_SHAPE
#define OSG_SHAPE 1

#include <osg/Object>
#include <osg/Vec3>
#include <osg/Quat>
#include <osg/Plane>
#include <osg/Array>

namespace osg {

// forward decare visitors.
class ShapeVisitor;
class ConstShapeVisitor;


/** META_StateAttribute macro define the standard clone, isSameKindAs,
  * className and getType methods.
  * Use when subclassing from Object to make it more convenient to define 
  * the standard pure virtual methods which are required for all Object 
  * subclasses.*/
#define META_Shape(library,name) \
        virtual osg::Object* cloneType() const { return new name(); } \
        virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
        virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
        virtual const char* libraryName() const { return #library; } \
        virtual const char* className() const { return #name; } \
        virtual void accept(osg::ShapeVisitor& sv) { sv.apply(*this); } \
        virtual void accept(osg::ConstShapeVisitor& csv) const { csv.apply(*this); }

/** Base class for all shape types. 
  * Shapes are used to either for culling and collision detection or
  * to define the geometric shape of procedurally generate Geometry.
*/
class OSG_EXPORT Shape : public Object
{
    public:

        Shape() {}
        
        Shape(const Shape& sa,const CopyOp& copyop=CopyOp::SHALLOW_COPY): 
            Object(sa,copyop) {}
        
        /** Clone the type of an attribute, with Object* return type.
            Must be defined by derived classes.*/
        virtual Object* cloneType() const = 0;

        /** Clone an attribute, with Object* return type.
            Must be defined by derived classes.*/
        virtual Object* clone(const CopyOp&) const = 0;


        /** return true if this and obj are of the same kind of object.*/
        virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Shape*>(obj)!=NULL; }

        /** return the name of the attribute's library.*/
        virtual const char* libraryName() const { return "osg"; }

        /** return the name of the attribute's class type.*/
        virtual const char* className() const { return "Shape"; }

        /** accept a non const shape visitor which can be used on non const shape objects.
            Must be defined by derived classes.*/
        virtual void accept(ShapeVisitor&)=0;

        /** accept a const shape visitor which can be used on const shape objects.
            Must be defined by derived classes.*/
        virtual void accept(ConstShapeVisitor&) const =0;

    protected:
    
        virtual ~Shape();
};

// forward declarations of Shape types.
class Sphere;
class Box;
class Cone;
class Cylinder;
class Capsule;
class InfinitePlane;

class TriangleMesh;
class ConvexHull;
class HeightField;

class CompositeShape;

class OSG_EXPORT ShapeVisitor
{
    public:
    
        ShapeVisitor() {}
        virtual ~ShapeVisitor();

        virtual void apply(Shape&) {}
        virtual void apply(Sphere&) {}
        virtual void apply(Box&) {}
        virtual void apply(Cone&) {}
        virtual void apply(Cylinder&) {}
        virtual void apply(Capsule&) {}
        virtual void apply(InfinitePlane&) {}

        virtual void apply(TriangleMesh&) {}
        virtual void apply(ConvexHull&) {}
        virtual void apply(HeightField&) {}

        virtual void apply(CompositeShape&) {}
};

class OSG_EXPORT ConstShapeVisitor
{
    public:
    
        ConstShapeVisitor() {}
        virtual ~ConstShapeVisitor();

        virtual void apply(const Shape&) {}
        virtual void apply(const Sphere&) {}
        virtual void apply(const Box&) {}
        virtual void apply(const Cone&) {}
        virtual void apply(const Cylinder&) {}
        virtual void apply(const Capsule&) {}
        virtual void apply(const InfinitePlane&) {}

        virtual void apply(const TriangleMesh&) {}
        virtual void apply(const ConvexHull&) {}
        virtual void apply(const HeightField&) {}

        virtual void apply(const CompositeShape&) {}
};

class OSG_EXPORT Sphere : public Shape
{
    public:

        Sphere():
            _center(0.0f,0.0f,0.0f),
            _radius(1.0f) {}

        Sphere(const osg::Vec3& center,float radius):
            _center(center),
            _radius(radius) {}

        Sphere(const Sphere& sphere,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
            Shape(sphere,copyop),
            _center(sphere._center),
            _radius(sphere._radius) {}

        META_Shape(osg, Sphere);

        inline bool valid() const { return _radius>=0.0f; }

        inline void set(const Vec3& center,float radius)
        {
            _center = center;
            _radius = radius;
        }

        inline void setCenter(const Vec3& center) { _center = center; }
        inline const Vec3& getCenter() const { return _center; }

        inline void setRadius(float radius) { _radius = radius; }
        inline float getRadius() const { return _radius; }

    protected:

        virtual ~Sphere();

        Vec3    _center;
        float   _radius;

};

class OSG_EXPORT Box : public Shape
{
    public:

        Box():
            _center(0.0f,0.0f,0.0f),
            _halfLengths(0.5f,0.5f,0.5f) {}

        Box(const osg::Vec3& center,float width):
            _center(center),
            _halfLengths(width*0.5f,width*0.5f,width*0.5f) {}

        Box(const osg::Vec3& center,float lengthX,float lengthY, float lengthZ):
            _center(center),
            _halfLengths(lengthX*0.5f,lengthY*0.5f,lengthZ*0.5f) {}

        Box(const Box& box,const CopyOp& copyop=CopyOp::SHALLOW_COPY): 
            Shape(box,copyop),
            _center(box._center),
            _halfLengths(box._halfLengths),
            _rotation(box._rotation) {}

        META_Shape(osg, Box);

        inline bool valid() const { return _halfLengths.x()>=0.0f; }

        inline void set(const Vec3& center,const Vec3& halfLengths)
        {
            _center = center;
            _halfLengths = halfLengths;
        }

        inline void setCenter(const Vec3& center) { _center = center; }
        inline const Vec3& getCenter() const { return _center; }

        inline void setHalfLengths(const Vec3& halfLengths) { _halfLengths = halfLengths; }
        inline const Vec3& getHalfLengths() const { return _halfLengths; }

        inline void setRotation(const Quat& quat) { _rotation = quat; }
        inline const Quat&  getRotation() const { return _rotation; }
        inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
        inline bool zeroRotation() const { return _rotation.zeroRotation(); } 

    protected:
    
        virtual ~Box();
    
        Vec3    _center;
        Vec3    _halfLengths;
        Quat    _rotation;

};



class OSG_EXPORT Cone : public Shape
{
    public:

        Cone():
            _center(0.0f,0.0f,0.0f),
            _radius(1.0f),
            _height(1.0f) {}

        Cone(const osg::Vec3& center,float radius,float height):
            _center(center),
            _radius(radius),
            _height(height) {}

        Cone(const Cone& cone,const CopyOp& copyop=CopyOp::SHALLOW_COPY): 
            Shape(cone,copyop),
            _center(cone._center),
            _radius(cone._radius),
            _height(cone._height),
            _rotation(cone._rotation) {}

        META_Shape(osg, Cone);

        inline bool valid() const { return _radius>=0.0f; }

        inline void set(const Vec3& center,float radius, float height)
        {
            _center = center;
            _radius = radius;
            _height = height;
        }

        inline void setCenter(const Vec3& center) { _center = center; }
        inline const Vec3& getCenter() const { return _center; }

        inline void setRadius(float radius) { _radius = radius; }
        inline float getRadius() const { return _radius; }

        inline void setHeight(float height) { _height = height; }
        inline float getHeight() const { return _height; }

        inline void setRotation(const Quat& quat) { _rotation = quat; }
        inline const Quat& getRotation() const { return _rotation; }
        inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
        inline bool zeroRotation() const { return _rotation.zeroRotation(); } 

        inline float getBaseOffsetFactor() const { return 0.25f; } 
        inline float getBaseOffset() const { return -getBaseOffsetFactor()*getHeight(); } 

    protected:
    
        virtual ~Cone();
    
        Vec3    _center;
        float   _radius;
        float   _height;

        Quat    _rotation;
};

class OSG_EXPORT Cylinder : public Shape
{
    public:

        Cylinder():
            _center(0.0f,0.0f,0.0f),
            _radius(1.0f),
            _height(1.0f) {}

        Cylinder(const osg::Vec3& center,float radius,float height):
            _center(center),
            _radius(radius),
            _height(height) {}

        Cylinder(const Cylinder& cylinder,const CopyOp& copyop=CopyOp::SHALLOW_COPY): 
            Shape(cylinder,copyop),
            _center(cylinder._center),
            _radius(cylinder._radius),
            _height(cylinder._height),
            _rotation(cylinder._rotation) {}

        META_Shape(osg, Cylinder);

        inline bool valid() const { return _radius>=0.0f; }

        inline void set(const Vec3& center,float radius, float height)
        {
            _center = center;
            _radius = radius;
            _height = height;
        }

        inline void setCenter(const Vec3& center) { _center = center; }
        inline const Vec3& getCenter() const { return _center; }

        inline void setRadius(float radius) { _radius = radius; }
        inline float getRadius() const { return _radius; }

        inline void setHeight(float height) { _height = height; }
        inline float getHeight() const { return _height; }

        inline void setRotation(const Quat& quat) { _rotation = quat; }
        inline const Quat& getRotation() const { return _rotation; }
        inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
        bool zeroRotation() const { return _rotation.zeroRotation(); } 

    protected:
    
        virtual ~Cylinder();

        Vec3    _center;
        float   _radius;
        float   _height;
        Quat    _rotation;
};

class OSG_EXPORT Capsule : public Shape
{
    public:

        Capsule():
            _center(0.0f,0.0f,0.0f),
            _radius(1.0f),
            _height(1.0f) {}

        Capsule(const osg::Vec3& center,float radius,float height):
            _center(center),
            _radius(radius),
            _height(height) {}

        Capsule(const Capsule& capsule,const CopyOp& copyop=CopyOp::SHALLOW_COPY): 
            Shape(capsule,copyop),
            _center(capsule._center),
            _radius(capsule._radius),
            _height(capsule._height),
            _rotation(capsule._rotation) {}

        META_Shape(osg, Capsule);

        inline bool valid() const { return _radius>=0.0f; }

        inline void set(const Vec3& center,float radius, float height)
        {
            _center = center;
            _radius = radius;
            _height = height;
        }

        inline void setCenter(const Vec3& center) { _center = center; }
        inline const Vec3& getCenter() const { return _center; }

        inline void setRadius(float radius) { _radius = radius; }
        inline float getRadius() const { return _radius; }

        inline void setHeight(float height) { _height = height; }
        inline float getHeight() const { return _height; }

        inline void setRotation(const Quat& quat) { _rotation = quat; }
        inline const Quat& getRotation() const { return _rotation; }
        inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
        bool zeroRotation() const { return _rotation.zeroRotation(); } 

    protected:
    
        virtual ~Capsule();
    
        Vec3    _center;
        float   _radius;
        float   _height;
        Quat    _rotation;
};

class OSG_EXPORT InfinitePlane : public Shape, public Plane
{
    public:
        InfinitePlane() {}

        InfinitePlane(const InfinitePlane& plane,const CopyOp& copyop=CopyOp::SHALLOW_COPY): 
            Shape(plane,copyop),
            Plane(plane) {}

        META_Shape(osg, InfinitePlane);

     protected:
     
        virtual ~InfinitePlane();
};

/** Exists to support collision detection engines not for doing rendering, use \ref osg::Geometry instead.
 */
class OSG_EXPORT TriangleMesh : public Shape
{
    public:
    
        TriangleMesh() {}

        TriangleMesh(const TriangleMesh& mesh,const CopyOp& copyop=CopyOp::SHALLOW_COPY): 
            Shape(mesh,copyop),
            _vertices(mesh._vertices),
            _indices(mesh._indices) {}

        META_Shape(osg, TriangleMesh);


        void setVertices(Vec3Array* vertices) { _vertices = vertices; }
        Vec3Array* getVertices() { return _vertices.get(); }
        const Vec3Array* getVertices() const { return _vertices.get(); }


        void setIndices(IndexArray* indices) { _indices = indices; }
        IndexArray* getIndices() { return _indices.get(); }
        const IndexArray* getIndices() const { return _indices.get(); }

    protected:
    
        virtual ~TriangleMesh();

        ref_ptr<Vec3Array> _vertices;
        ref_ptr<IndexArray> _indices;

};

class OSG_EXPORT ConvexHull : public TriangleMesh
{
    public:
    
        ConvexHull() {}

        ConvexHull(const ConvexHull& hull,const CopyOp& copyop=CopyOp::SHALLOW_COPY): 
            TriangleMesh(hull,copyop) {}

        META_Shape(osg, TriangleMesh);

    protected:
    
        virtual ~ConvexHull();
};

class OSG_EXPORT HeightField : public Shape
{
    public:
    
        HeightField();

        HeightField(const HeightField& mesh,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
        
        META_Shape(osg, HeightField);

        typedef std::vector<float> HeightList;

        void allocate(unsigned int numColumns,unsigned int numRows);

        inline unsigned int getNumColumns() const { return _columns; }
        inline unsigned int getNumRows() const { return _rows; }

        inline void setOrigin(const osg::Vec3& origin) { _origin = origin; }
        inline const osg::Vec3& getOrigin() const { return _origin; }

        inline void setXInterval(float dx) { _dx = dx; }
        inline float getXInterval() const { return _dx; }

        inline void setYInterval(float dy) { _dy = dy; }
        inline float getYInterval() const { return _dy; }
    
        /** Get the FloatArray height data.*/
        osg::FloatArray* getFloatArray() { return _heights.get(); }

        /** Get the const FloatArray height data.*/
        const osg::FloatArray* getFloatArray() const { return _heights.get(); }

        HeightList& getHeightList() { return _heights->asVector(); }

        const HeightList& getHeightList() const { return _heights->asVector(); }

        /** Set the height of the skirt to render around the edge of HeightField.
          * The skirt is used as a means of disguising edge boundaries between adjacent HeightField, 
          * particularly of ones with different resolutions.*/
        void setSkirtHeight(float skirtHeight) { _skirtHeight = skirtHeight; }

        /** Get the height of the skirt to render around the edge of HeightField.*/
        float getSkirtHeight() const { return _skirtHeight; }

        /** Set the width in number of cells in from the edge that the height field should be rendered from.
          * This exists to allow gradient and curvature continutity to be maintained between adjacent HeightField, where
          * the border cells will overlap adjacent HeightField.*/
        void setBorderWidth(unsigned int borderWidth) { _borderWidth = borderWidth; }

        /** Get the width in number of cells in from the edge that the height field should be rendered from.*/
        unsigned int getBorderWidth() const { return _borderWidth; }

        inline void setRotation(const Quat& quat) { _rotation = quat; }
        inline const Quat& getRotation() const { return _rotation; }
        inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
        inline bool zeroRotation() const { return _rotation.zeroRotation(); } 

        /* set a single height point in the height field */
        inline void setHeight(unsigned int c,unsigned int r,float value)
        {
           (*_heights)[c+r*_columns] = value;
        }

        /* Get address of single height point in the height field, allows user to change. */
        inline float& getHeight(unsigned int c,unsigned int r)
        {
           return (*_heights)[c+r*_columns];
        }

        /* Get value of single height point in the height field, not editable. */
        inline float getHeight(unsigned int c,unsigned int r) const
        {
           return (*_heights)[c+r*_columns];
        }

        inline Vec3 getVertex(unsigned int c,unsigned int r) const
        {
            return Vec3(_origin.x()+getXInterval()*(float)c,
                        _origin.y()+getYInterval()*(float)r,
                        _origin.z()+(*_heights)[c+r*_columns]);  
        }

        Vec3 getNormal(unsigned int c,unsigned int r) const;

        Vec2 getHeightDelta(unsigned int c,unsigned int r) const;

    protected:
    
        virtual ~HeightField();

        unsigned int                    _columns,_rows;

        osg::Vec3                       _origin; // _origin is the min value of the X and Y coordinates.
        float                           _dx;
        float                           _dy;

        float                           _skirtHeight;
        unsigned int                    _borderWidth;

        Quat                            _rotation;
        osg::ref_ptr<osg::FloatArray>   _heights;

};

typedef HeightField Grid;


class OSG_EXPORT CompositeShape : public Shape
{
    public:


    
        typedef std::vector< ref_ptr<Shape> > ChildList;

        CompositeShape() {}

        CompositeShape(const CompositeShape& cs,const CopyOp& copyop=CopyOp::SHALLOW_COPY): 
            Shape(cs,copyop),
            _children(cs._children) {}

        META_Shape(osg, CompositeShape);

        /** Set the shape that encloses all of the children.*/
        void setShape(Shape* shape) { _shape = shape; }

        /** Get the shape that encloses all of the children.*/
        Shape* getShape() { return _shape.get(); }

        /** Get the const shape that encloses all of the children.*/
        const Shape* getShape() const { return _shape.get(); }

        /** Get the number of children of this composite shape.*/
        unsigned int getNumChildren() const { return static_cast<unsigned int>(_children.size()); }

        /** Get a child.*/
        Shape* getChild(unsigned int i) { return _children[i].get(); }

        /** Get a const child.*/
        const Shape* getChild(unsigned int i) const { return _children[i].get(); }

        /** Add a child to the list.*/
        void addChild(Shape* shape) { _children.push_back(shape); }

        /** remove a child from the list.*/
        void removeChild(unsigned int i) { _children.erase(_children.begin()+i); }

        /** find the index number of child, if child is not found then it returns getNumChildren(),
          * so should be used in similar style to STL's result!=end().*/ 
        unsigned int findChildNo(Shape* shape) const
        { 
            for (unsigned int childNo=0;childNo<_children.size();++childNo)
            {
                if (_children[childNo]==shape) return childNo;
            }
            return static_cast<unsigned int>(_children.size()); // node not found.

        }

    protected:
    
        virtual ~CompositeShape();

        ref_ptr<Shape>  _shape;
        ChildList       _children;

};

}

#endif