1 /* 2 ----------------------------------------------------------------------------- 3 This source file is part of OGRE 4 (Object-oriented Graphics Rendering Engine) 5 For the latest info, see http://www.ogre3d.org/ 6 7 Copyright (c) 2000-2014 Torus Knot Software Ltd 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 THE SOFTWARE. 26 ----------------------------------------------------------------------------- 27 */ 28 #ifndef __Ogre_Volume_CSGSource_H__ 29 #define __Ogre_Volume_CSGSource_H__ 30 31 #include "OgreVolumeSource.h" 32 #include "OgreVector3.h" 33 #include "OgreAxisAlignedBox.h" 34 #include "OgreVolumePrerequisites.h" 35 #include "OgreVolumeSimplexNoise.h" 36 37 namespace Ogre { 38 namespace Volume { 39 /** \addtogroup Optional 40 * @{ 41 */ 42 /** \addtogroup Volume 43 * @{ 44 */ 45 /** A sphere. 46 */ 47 class _OgreVolumeExport CSGSphereSource : public Source 48 { 49 protected: 50 /** The radius. 51 */ 52 const Real mR; 53 54 /** The center. 55 */ 56 const Vector3 mCenter; 57 public: 58 59 /** Constructor. 60 @param r 61 The radius. 62 @param center 63 The center. 64 */ 65 CSGSphereSource(const Real r, const Vector3 ¢er); 66 67 /** Overridden from Source. 68 */ 69 virtual Vector4 getValueAndGradient(const Vector3 &position) const; 70 71 /** Overridden from Source. 72 */ 73 virtual Real getValue(const Vector3 &position) const; 74 }; 75 76 /** A plane. 77 */ 78 class _OgreVolumeExport CSGPlaneSource : public Source 79 { 80 protected: 81 82 /// The distance to zero of the plane. 83 const Real mD; 84 85 /// The normal of the plane. 86 Vector3 mNormal; 87 88 public: 89 90 /** Constructor. 91 @param d 92 The distance to zero of the plane. 93 @param normal 94 The planes normal. 95 */ 96 CSGPlaneSource(const Real d, const Vector3 &normal); 97 98 /** Overridden from Source. 99 */ 100 virtual Vector4 getValueAndGradient(const Vector3 &position) const; 101 102 /** Overridden from Source. 103 */ 104 virtual Real getValue(const Vector3 &position) const; 105 }; 106 107 /** A not rotated cube. 108 */ 109 class _OgreVolumeExport CSGCubeSource : public Source 110 { 111 protected: 112 113 /// The six possible normals of the cube. 114 static Vector3 mBoxNormals[6]; 115 116 /// The box. 117 AxisAlignedBox mBox; 118 119 /** Gets the distance of a point to the nearest cube element. 120 @param position 121 The point to test. 122 @return 123 The distance. 124 */ distanceTo(const Vector3 & position)125 inline Real distanceTo(const Vector3 &position) const 126 { 127 Real distance; 128 const Vector3 dMin = position - mBox.getMinimum(); 129 const Vector3 dMax = mBox.getMaximum() - position; 130 131 // Check if inside of the box 132 if (dMin.x >= (Real)0.0 && dMin.y >= (Real)0.0 && dMin.z >= (Real)0.0 && 133 dMax.x >= (Real)0.0 && dMax.y >= (Real)0.0 && dMax.z >= (Real)0.0) 134 { 135 const Real d[6] = {dMin.x, dMin.y, dMin.z, dMax.x, dMax.y, dMax.z}; 136 distance = Math::POS_INFINITY; 137 for (size_t i = 0; i < 6; ++i) 138 { 139 if (d[i] < distance) 140 { 141 distance = d[i]; 142 } 143 } 144 } 145 else 146 { 147 distance = -mBox.distance(position); 148 } 149 return distance; 150 } 151 152 public: 153 154 /** Constructor. 155 @param min 156 The lower back left corner. 157 @param max 158 The upper front right corner. 159 */ 160 CSGCubeSource(const Vector3 &min, const Vector3 &max); 161 162 /** Overridden from Source. 163 */ 164 virtual Vector4 getValueAndGradient(const Vector3 &position) const; 165 166 /** Overridden from Source. 167 */ 168 virtual Real getValue(const Vector3 &position) const; 169 }; 170 171 /** Abstract operation volume source holding two sources as operants. 172 */ 173 class _OgreVolumeExport CSGOperationSource : public Source 174 { 175 protected: 176 177 /// The first operant. 178 const Source *mA; 179 180 /// The second operant. 181 const Source *mB; 182 183 /** Constructor. Protected to be callable from child classes. 184 @param a 185 The first operator. 186 @param b 187 The second operator. 188 */ 189 CSGOperationSource(const Source *a, const Source *b); 190 191 /** Constructor, sets the sources to null. 192 Protected to be callable from child classes. 193 */ 194 CSGOperationSource(void); 195 public: 196 197 /** Gets the first operator source. 198 @return 199 The first operator source. 200 */ 201 virtual const Source* getSourceA() const; 202 203 /** Sets the first operator source. 204 @param a 205 The first operator source. 206 */ 207 virtual void setSourceA(Source *a); 208 209 /** Gets the second operator source. 210 @return 211 The second operator source. 212 */ 213 virtual const Source* getSourceB(void) const; 214 215 /** Sets the second operator source. 216 @param b 217 The second operator source. 218 */ 219 virtual void setSourceB(Source *b); 220 }; 221 222 /** Builds the intersection between two sources. 223 */ 224 class _OgreVolumeExport CSGIntersectionSource : public CSGOperationSource 225 { 226 public: 227 228 /** Constructor. 229 @param a 230 The first operator. 231 @param b 232 The second operator. 233 */ 234 CSGIntersectionSource(const Source *a, const Source *b); 235 236 /** Constructor, sets the sources to null. 237 */ 238 CSGIntersectionSource(void); 239 240 /** Overridden from Source. 241 */ 242 virtual Vector4 getValueAndGradient(const Vector3 &position) const; 243 244 /** Overridden from Source. 245 */ 246 virtual Real getValue(const Vector3 &position) const; 247 }; 248 249 /** Builds the union between two sources. 250 */ 251 class _OgreVolumeExport CSGUnionSource : public CSGOperationSource 252 { 253 public: 254 255 /** Constructor. 256 @param a 257 The first operator. 258 @param b 259 The second operator. 260 */ 261 CSGUnionSource(const Source *a, const Source *b); 262 263 /** Constructor, sets the sources to null. 264 */ 265 CSGUnionSource(void); 266 267 /** Overridden from Source. 268 */ 269 virtual Vector4 getValueAndGradient(const Vector3 &position) const; 270 271 /** Overridden from Source. 272 */ 273 virtual Real getValue(const Vector3 &position) const; 274 }; 275 276 /** Builds the difference between two sources. 277 */ 278 class _OgreVolumeExport CSGDifferenceSource : public CSGOperationSource 279 { 280 public: 281 282 283 /** Constructor. 284 @param a 285 The first operator. 286 @param b 287 The second operator. 288 */ 289 CSGDifferenceSource(const Source *a, const Source *b); 290 291 /** Constructor, sets the sources to null. 292 */ 293 CSGDifferenceSource(void); 294 295 /** Overridden from Source. 296 */ 297 virtual Vector4 getValueAndGradient(const Vector3 &position) const; 298 299 /** Overridden from Source. 300 */ 301 virtual Real getValue(const Vector3 &position) const; 302 }; 303 304 /** Source which does a unary operation to another one. 305 */ 306 class _OgreVolumeExport CSGUnarySource : public Source 307 { 308 protected: 309 310 /// The first operant. 311 const Source *mSrc; 312 313 /** Constructor. Protected to be callable from child classes. 314 @param src 315 The operator. 316 */ 317 CSGUnarySource(const Source *src); 318 319 /** Constructor. Sets the source to null. 320 Protected to be callable from child classes. 321 */ 322 CSGUnarySource(void); 323 324 public: 325 326 /** Gets the source. 327 @return 328 The source. 329 */ 330 virtual const Source* getSource(void) const; 331 332 /** Sets the source. 333 @param a 334 The source. 335 */ 336 virtual void setSource(Source *a); 337 }; 338 339 /** Negates the given volume. 340 */ 341 class _OgreVolumeExport CSGNegateSource : public CSGUnarySource 342 { 343 public: 344 345 /** Constructor. 346 @param src 347 The source to negate. 348 */ 349 explicit CSGNegateSource(const Source *src); 350 351 /** Constructor. Sets the source to null. 352 */ 353 CSGNegateSource(void); 354 355 /** Overridden from Source. 356 */ 357 virtual Vector4 getValueAndGradient(const Vector3 &position) const; 358 359 /** Overridden from Source. 360 */ 361 virtual Real getValue(const Vector3 &position) const; 362 }; 363 364 /** Scales the given volume source. 365 */ 366 class _OgreVolumeExport CSGScaleSource : public CSGUnarySource 367 { 368 protected: 369 370 /// Holds the dimensions of the volume. 371 Real mScale; 372 public: 373 374 /** Constructor. 375 @param src 376 The source to scale. 377 @param scale 378 The scale of the source. 379 */ 380 CSGScaleSource(const Source *src, const Real scale); 381 382 /** Overridden from Source. 383 */ 384 virtual Vector4 getValueAndGradient(const Vector3 &position) const; 385 386 /** Overridden from Source. 387 */ 388 virtual Real getValue(const Vector3 &position) const; 389 }; 390 391 class _OgreVolumeExport CSGNoiseSource: public CSGUnarySource 392 { 393 protected: 394 395 /// The frequencies of the octaves. 396 Real *mFrequencies; 397 398 /// The amplitudes of the octaves. 399 Real *mAmplitudes; 400 401 /// The amount of octaves. 402 size_t mNumOctaves; 403 404 /// To make some noise. 405 SimplexNoise mNoise; 406 407 /// To calculate the gradient. 408 Real mGradientOff; 409 410 /// The initial seed. 411 long mSeed; 412 413 /// Prepares the node members. 414 void setData(void); 415 416 /* Gets the density value. 417 @param position 418 The position of the value. 419 @return 420 The value. 421 */ getInternalValue(const Vector3 & position)422 inline Real getInternalValue(const Vector3 &position) const 423 { 424 Real toAdd = (Real)0.0; 425 for (size_t i = 0; i < mNumOctaves; ++i) 426 { 427 toAdd += mNoise.noise(position.x * mFrequencies[i], position.y * mFrequencies[i], position.z * mFrequencies[i]) * mAmplitudes[i]; 428 } 429 return mSrc->getValue(position) + toAdd; 430 } 431 432 public: 433 434 /** Constructor. 435 @param src 436 The source to add the noise to. 437 @param frequencies 438 The frequencies of the added noise octaves. 439 @param amplitudes 440 The amplitudes of the added noise octaves. 441 @param numOctaves 442 The amount of octaves. 443 @param seed 444 The seed to initialize the random number generator with. 445 */ 446 CSGNoiseSource(const Source *src, Real *frequencies, Real *amplitudes, size_t numOctaves, long seed); 447 448 /** Constructor with current time as seed. 449 @param src 450 The source to add the noise to. 451 @param frequencies 452 The frequencies of the added noise octaves. 453 @param amplitudes 454 The amplitudes of the added noise octaves. 455 @param numOctaves 456 The amount of octaves. 457 */ 458 CSGNoiseSource(const Source *src, Real *frequencies, Real *amplitudes, size_t numOctaves); 459 460 /** Overridden from Source. 461 */ 462 virtual Vector4 getValueAndGradient(const Vector3 &position) const; 463 464 /** Overridden from Source. 465 */ 466 virtual Real getValue(const Vector3 &position) const; 467 468 /** Gets the initial seed. 469 @return 470 The initial seed. 471 */ 472 long getSeed(void) const; 473 }; 474 /** @} */ 475 /** @} */ 476 } 477 } 478 479 #endif 480