OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CompositePlacer.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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 // Written by Wang Rui, (C) 2010
14 
15 #ifndef OSGPARTICLE_COMPOSITEPLACER
16 #define OSGPARTICLE_COMPOSITEPLACER
17 
18 #include <osgParticle/Placer>
19 #include <osgParticle/Particle>
20 
21 namespace osgParticle
22 {
23 
24 
26 class CompositePlacer : public Placer
27 {
28 public:
30 
32  : Placer(copy, copyop), _placers(copy._placers) {}
33 
35 
36  // Set a child placer at specific index
37  void setPlacer( unsigned int i, Placer* p )
38  {
39  if (i<_placers.size()) _placers[i] = p;
40  else addPlacer(p);
41  }
42 
44  void addPlacer( Placer* p ) { _placers.push_back(p); }
45 
47  void removePlacer( unsigned int i )
48  { if (i<_placers.size()) _placers.erase(_placers.begin()+i); }
49 
51  Placer* getPlacer( unsigned int i ) { return _placers[i].get(); }
52  const Placer* getPlacer( unsigned int i ) const { return _placers[i].get(); }
53 
55  unsigned int getNumPlacers() const { return _placers.size(); }
56 
58  inline void place( Particle* P ) const;
59 
61  inline float volume() const;
62 
64  inline osg::Vec3 getControlPosition() const;
65 
66 protected:
67  virtual ~CompositePlacer() {}
68  CompositePlacer& operator=( const CompositePlacer& ) { return *this; }
69 
70  typedef std::vector< osg::ref_ptr<Placer> > PlacerList;
71  PlacerList _placers;
72 };
73 
74 // INLINE METHODS
75 
76 inline void CompositePlacer::place( Particle* P ) const
77 {
78  rangef sizeRange( 0.0f, volume() );
79  float current = 0.0f, selected = sizeRange.get_random();
80  for ( PlacerList::const_iterator itr=_placers.begin(); itr!=_placers.end(); ++itr )
81  {
82  current += (*itr)->volume();
83  if ( selected<=current ) (*itr)->place( P );
84  }
85 }
86 
87 inline float CompositePlacer::volume() const
88 {
89  float total_size = 0.0f;
90  for ( PlacerList::const_iterator itr=_placers.begin(); itr!=_placers.end(); ++itr )
91  total_size += (*itr)->volume();
92  return total_size;
93 }
94 
96 {
97  if ( !_placers.size() ) return osg::Vec3();
98  return _placers.front()->getControlPosition();
99 }
100 
101 
102 }
103 
104 #endif
float volume() const
return the volume of the box
META_Object(osgParticle, CompositePlacer)
void setPlacer(unsigned int i, Placer *p)
void removePlacer(unsigned int i)
Remove a child placer.
unsigned int getNumPlacers() const
Get number of placers.
ValueType get_random() const
Get a random value between min and max.
Definition: range.h:57
void place(Particle *P) const
Place a particle. Do not call it manually.
Placer * getPlacer(unsigned int i)
Get a child placer.
CompositePlacer(const CompositePlacer &copy, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
std::vector< osg::ref_ptr< Placer > > PlacerList
const Placer * getPlacer(unsigned int i) const
CompositePlacer & operator=(const CompositePlacer &)
osg::Vec3 getControlPosition() const
return the control position
void addPlacer(Placer *p)
Add a child placer.