| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | //osgParticle - Copyright (C) 2002 Marco Jez |
|---|
| 14 | |
|---|
| 15 | #ifndef OSGPARTICLE_MULTISEGMENT_PLACER |
|---|
| 16 | #define OSGPARTICLE_MULTISEGMENT_PLACER 1 |
|---|
| 17 | |
|---|
| 18 | #include <osgParticle/Export> |
|---|
| 19 | #include <osgParticle/Placer> |
|---|
| 20 | #include <osgParticle/Particle> |
|---|
| 21 | |
|---|
| 22 | #include <vector> |
|---|
| 23 | #include <utility> |
|---|
| 24 | |
|---|
| 25 | #include <osg/Notify> |
|---|
| 26 | #include <osg/CopyOp> |
|---|
| 27 | #include <osg/Object> |
|---|
| 28 | #include <osg/Vec3> |
|---|
| 29 | |
|---|
| 30 | namespace osgParticle { |
|---|
| 31 | |
|---|
| 32 | /** A polyline-shaped particle placer. |
|---|
| 33 | This placer class sets the position of incoming particles by choosing a random point on the |
|---|
| 34 | specified sequence of connected segments. |
|---|
| 35 | */ |
|---|
| 36 | class OSGPARTICLE_EXPORT MultiSegmentPlacer: public Placer { |
|---|
| 37 | public: |
|---|
| 38 | MultiSegmentPlacer(); |
|---|
| 39 | MultiSegmentPlacer(const MultiSegmentPlacer& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); |
|---|
| 40 | |
|---|
| 41 | META_Object(osgParticle, MultiSegmentPlacer); |
|---|
| 42 | |
|---|
| 43 | /// Get the number of vertices which define the segments. |
|---|
| 44 | inline int numVertices() const; |
|---|
| 45 | |
|---|
| 46 | /// Get a vertex. |
|---|
| 47 | inline const osg::Vec3& getVertex(int i) const; |
|---|
| 48 | |
|---|
| 49 | /// Set a vertex. |
|---|
| 50 | inline void setVertex(int i, const osg::Vec3& v); |
|---|
| 51 | |
|---|
| 52 | /// Set a vertex. |
|---|
| 53 | inline void setVertex(int i, float x, float y, float z); |
|---|
| 54 | |
|---|
| 55 | /// Add a vertex. |
|---|
| 56 | inline void addVertex(const osg::Vec3& v); |
|---|
| 57 | |
|---|
| 58 | /// Add a vertex. |
|---|
| 59 | inline void addVertex(float x, float y, float z); |
|---|
| 60 | |
|---|
| 61 | /// Remove a vertex. |
|---|
| 62 | inline void removeVertex(int i); |
|---|
| 63 | |
|---|
| 64 | /// Place a partice. Called automatically by <CODE>ModularEmitter</CODE>, do not call this method manually. |
|---|
| 65 | void place(Particle* P) const; |
|---|
| 66 | |
|---|
| 67 | /// return the length of the multi-segment |
|---|
| 68 | inline float volume() const; |
|---|
| 69 | |
|---|
| 70 | /// return the control position |
|---|
| 71 | inline osg::Vec3 getControlPosition() const; |
|---|
| 72 | |
|---|
| 73 | protected: |
|---|
| 74 | virtual ~MultiSegmentPlacer() {} |
|---|
| 75 | MultiSegmentPlacer& operator=(const MultiSegmentPlacer&) { return *this; } |
|---|
| 76 | |
|---|
| 77 | private: |
|---|
| 78 | typedef std::pair<osg::Vec3, float> Vertex_data; |
|---|
| 79 | typedef std::vector<Vertex_data> Vertex_vector; |
|---|
| 80 | |
|---|
| 81 | Vertex_vector _vx; |
|---|
| 82 | float _total_length; |
|---|
| 83 | |
|---|
| 84 | void recompute_length(); |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | // INLINE FUNCTIONS |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | inline int MultiSegmentPlacer::numVertices() const |
|---|
| 91 | { |
|---|
| 92 | return static_cast<int>(_vx.size()); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | inline const osg::Vec3& MultiSegmentPlacer::getVertex(int i) const |
|---|
| 96 | { |
|---|
| 97 | return _vx[i].first; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | inline void MultiSegmentPlacer::setVertex(int i, const osg::Vec3& v) |
|---|
| 101 | { |
|---|
| 102 | _vx[i].first = v; |
|---|
| 103 | recompute_length(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | inline void MultiSegmentPlacer::setVertex(int i, float x, float y, float z) |
|---|
| 107 | { |
|---|
| 108 | _vx[i].first.set(x, y, z); |
|---|
| 109 | recompute_length(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | inline void MultiSegmentPlacer::addVertex(const osg::Vec3& v) |
|---|
| 113 | { |
|---|
| 114 | float l = 0; |
|---|
| 115 | if (_vx.size() > 0) { |
|---|
| 116 | l = (v - _vx.back().first).length(); |
|---|
| 117 | } |
|---|
| 118 | _total_length += l; |
|---|
| 119 | _vx.push_back(std::make_pair(v, _total_length)); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | inline void MultiSegmentPlacer::addVertex(float x, float y, float z) |
|---|
| 123 | { |
|---|
| 124 | addVertex(osg::Vec3(x, y, z)); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | inline void MultiSegmentPlacer::removeVertex(int i) |
|---|
| 128 | { |
|---|
| 129 | _vx.erase(_vx.begin()+i); |
|---|
| 130 | recompute_length(); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | inline float MultiSegmentPlacer::volume() const |
|---|
| 134 | { |
|---|
| 135 | return _total_length; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | inline osg::Vec3 MultiSegmentPlacer::getControlPosition() const |
|---|
| 139 | { |
|---|
| 140 | return _vx.empty() ? osg::Vec3(0.0f,0.0f,0.0f) : _vx[0].first; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | } |
|---|
| 144 | #endif |
|---|