Sector 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #ifndef OSGSIM_SECTOR
  14. #define OSGSIM_SECTOR 1
  15. #include <osgSim/Export>
  16. #include <osg/Quat>
  17. #include <osg/Vec3>
  18. #include <osg/Vec4>
  19. #include <osg/Matrix>
  20. #include <osg/Math>
  21. #include <osg/Object>
  22. namespace osgSim {
  23. class Sector : public osg::Object
  24. {
  25. public:
  26. Sector() {}
  27. Sector(const Sector& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
  28. osg::Object(copy,copyop) {}
  29. virtual const char *libraryName() const { return "osgSim"; }
  30. virtual const char *className() const { return "Sector"; }
  31. virtual bool isSameKindAs(const osg::Object *obj) const { return dynamic_cast<const Sector *>(obj) != 0; }
  32. virtual float operator() (const osg::Vec3& /*eyeLocal*/) const = 0;
  33. protected:
  34. virtual ~Sector() {}
  35. };
  36. class OSGSIM_EXPORT AzimRange
  37. {
  38. public:
  39. AzimRange():
  40. _cosAzim(1.0f),
  41. _sinAzim(0.0f),
  42. _cosAngle(-1.0f),
  43. _cosFadeAngle(-1.0f) {}
  44. void setAzimuthRange(float minAzimuth,float maxAzimuth,float fadeAngle=0.0f);
  45. void getAzimuthRange(float& minAzimuth, float& maxAzimuth, float& fadeAngle) const;
  46. inline float azimSector(const osg::Vec3& eyeLocal) const
  47. {
  48. float dotproduct = eyeLocal.x()*_sinAzim+eyeLocal.y()*_cosAzim;
  49. float length = sqrt(osg::square(eyeLocal.x())+osg::square(eyeLocal.y()));
  50. if (dotproduct<_cosFadeAngle*length) return 0.0f; // out of sector.
  51. if (dotproduct>=_cosAngle*length) return 1.0f; // fully in sector.
  52. return (dotproduct-_cosFadeAngle*length)/((_cosAngle-_cosFadeAngle)*length);
  53. }
  54. protected:
  55. float _cosAzim;
  56. float _sinAzim;
  57. float _cosAngle;
  58. float _cosFadeAngle;
  59. };
  60. class OSGSIM_EXPORT ElevationRange
  61. {
  62. public:
  63. ElevationRange():
  64. _cosMinElevation(-1.0f),
  65. _cosMinFadeElevation(-1.0f),
  66. _cosMaxElevation(1.0),
  67. _cosMaxFadeElevation(1.0) {}
  68. void setElevationRange(float minElevation,float maxElevation,float fadeAngle=0.0f);
  69. float getMinElevation() const;
  70. float getMaxElevation() const;
  71. float getFadeAngle() const;
  72. inline float elevationSector(const osg::Vec3& eyeLocal) const
  73. {
  74. float dotproduct = eyeLocal.z(); // against z axis - eyeLocal*(0,0,1).
  75. float length = eyeLocal.length();
  76. if (dotproduct>_cosMaxFadeElevation*length) return 0.0f; // out of sector
  77. if (dotproduct<_cosMinFadeElevation*length) return 0.0f; // out of sector
  78. if (dotproduct>_cosMaxElevation*length)
  79. {
  80. // in uppoer fade band.
  81. return (dotproduct-_cosMaxFadeElevation*length)/((_cosMaxElevation-_cosMaxFadeElevation)*length);
  82. }
  83. if (dotproduct<_cosMinElevation*length)
  84. {
  85. // in lower fade band.
  86. return (dotproduct-_cosMinFadeElevation*length)/((_cosMinElevation-_cosMinFadeElevation)*length);
  87. }
  88. return 1.0f; // fully in sector
  89. }
  90. protected:
  91. float _cosMinElevation;
  92. float _cosMinFadeElevation;
  93. float _cosMaxElevation;
  94. float _cosMaxFadeElevation;
  95. };
  96. class OSGSIM_EXPORT AzimSector : public Sector, public AzimRange
  97. {
  98. public:
  99. AzimSector():
  100. Sector(),
  101. AzimRange() {}
  102. AzimSector(const AzimSector& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
  103. Sector(copy,copyop),
  104. AzimRange(copy) {}
  105. AzimSector(float minAzimuth,float maxAzimuth,float fadeAngle=0.0f);
  106. META_Object(osgSim,AzimSector);
  107. virtual float operator() (const osg::Vec3& eyeLocal) const;
  108. protected:
  109. virtual ~AzimSector() {}
  110. };
  111. class OSGSIM_EXPORT ElevationSector : public Sector, public ElevationRange
  112. {
  113. public:
  114. ElevationSector():
  115. Sector(),
  116. ElevationRange() {}
  117. ElevationSector(const ElevationSector& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
  118. Sector(copy,copyop),
  119. ElevationRange(copy) {}
  120. ElevationSector(float minElevation,float maxElevation,float fadeAngle=0.0f);
  121. META_Object(osgSim,ElevationSector);
  122. virtual float operator() (const osg::Vec3& eyeLocal) const;
  123. protected:
  124. virtual ~ElevationSector() {}
  125. };
  126. class OSGSIM_EXPORT AzimElevationSector : public Sector, public AzimRange, public ElevationRange
  127. {
  128. public:
  129. AzimElevationSector():
  130. Sector(),
  131. AzimRange(),
  132. ElevationRange() {}
  133. AzimElevationSector(const AzimElevationSector& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
  134. Sector(copy,copyop),
  135. AzimRange(copy),
  136. ElevationRange(copy) {}
  137. AzimElevationSector(float minAzimuth,float maxAzimuth,float minElevation,float maxElevation,float fadeAngle=0.0f);
  138. META_Object(osgSim,AzimElevationSector);
  139. virtual float operator() (const osg::Vec3& eyeLocal) const;
  140. protected:
  141. virtual ~AzimElevationSector() {}
  142. };
  143. class OSGSIM_EXPORT ConeSector : public Sector
  144. {
  145. public:
  146. ConeSector():
  147. Sector(),
  148. _axis(0.0f,0.0f,1.0f),
  149. _cosAngle(-1.0f),
  150. _cosAngleFade(-1.0f) {}
  151. ConeSector(const ConeSector& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
  152. Sector(copy,copyop),
  153. _axis(copy._axis),
  154. _cosAngle(copy._cosAngle),
  155. _cosAngleFade(copy._cosAngleFade) {}
  156. ConeSector(const osg::Vec3& axis,float angle,float fadeangle=0.0f);
  157. META_Object(osgSim,ConeSector);
  158. void setAxis(const osg::Vec3& axis);
  159. const osg::Vec3& getAxis() const;
  160. void setAngle(float angle,float fadeangle=0.0f);
  161. float getAngle() const;
  162. float getFadeAngle() const;
  163. virtual float operator() (const osg::Vec3& eyeLocal) const;
  164. protected:
  165. virtual ~ConeSector() {}
  166. osg::Vec3 _axis;
  167. float _cosAngle;
  168. float _cosAngleFade;
  169. };
  170. /* The DirectionalSector class was created to better handle OpenFlight directional
  171. lightpoints. The Elevation and Azimuth Sectors above impose invalid limits on
  172. the elevation range which cause lightpoints whose direction vectors are not
  173. on the XY plane to be displayed incorrectly. Corbin Holtz 4/04 */
  174. class OSGSIM_EXPORT DirectionalSector : public Sector
  175. {
  176. public:
  177. DirectionalSector():
  178. Sector(),
  179. _direction(0.0f, 0.0f, 1.0f),
  180. _rollAngle(0.0f),
  181. _cosHorizAngle(-1.0f),
  182. _cosVertAngle(-1.0f),
  183. _cosHorizFadeAngle(-1.0f),
  184. _cosVertFadeAngle(-1.0f) {computeMatrix();}
  185. DirectionalSector(const DirectionalSector& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
  186. Sector(copy,copyop),
  187. _direction(copy._direction),
  188. _rollAngle(copy._rollAngle),
  189. _local_to_LP(copy._local_to_LP),
  190. _cosHorizAngle(copy._cosHorizAngle),
  191. _cosVertAngle(copy._cosVertAngle),
  192. _cosHorizFadeAngle(copy._cosHorizFadeAngle),
  193. _cosVertFadeAngle(copy._cosVertFadeAngle) {}
  194. DirectionalSector(const osg::Vec3& direction,float horizLobeAngle, float vertLobeAngle, float lobeRollAngle, float fadeAngle=0.0f);
  195. META_Object(osgSim,DirectionalSector);
  196. void setDirection(const osg::Vec3& direction);
  197. const osg::Vec3& getDirection() const;
  198. void setHorizLobeAngle(float angle);
  199. float getHorizLobeAngle() const;
  200. void setLobeRollAngle(float angle);
  201. float getLobeRollAngle() const;
  202. void setVertLobeAngle(float angle);
  203. float getVertLobeAngle() const;
  204. void setFadeAngle(float angle);
  205. float getFadeAngle() const;
  206. virtual float operator() (const osg::Vec3& eyeLocal) const;
  207. void computeMatrix() ;
  208. protected:
  209. virtual ~DirectionalSector() {}
  210. osg::Vec3 _direction ;
  211. float _rollAngle ;
  212. osg::Matrix _local_to_LP ;
  213. float _cosHorizAngle;
  214. float _cosVertAngle;
  215. float _cosHorizFadeAngle;
  216. float _cosVertFadeAngle;
  217. };
  218. }
  219. #endif