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