RigsofRods
Soft-body Physics Simulation
Ellipsoid.cpp
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------------------
3 This source file is part of SkyX.
4 Visit http://www.paradise-studios.net/products/skyx/
5 
6 Copyright (C) 2009-2012 Xavier Verguín González <xavyiy@gmail.com>
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt.
21 --------------------------------------------------------------------------------
22 */
23 
24 #include "Ellipsoid.h"
25 
26 namespace SkyX { namespace VClouds
27 {
28 
30  const int &a, const int &b, const int &c,
31  const int &nx, const int &ny, const int &nz,
32  const int &x, const int &y, const int &z,
33  const Ogre::Real& Density)
34  : mA(a), mB(b), mC(c)
35  , mA2(Ogre::Math::Pow(a, 2)), mB2(Ogre::Math::Pow(b, 2)), mC2(Ogre::Math::Pow(c, 2))
36  , mNx(nx), mNy(ny), mNz(nz)
37  , mX(x), mY(y), mZ(z)
38  , mDensity(Density)
39  {
40  }
41 
43  {
44  }
45 
46  const float Ellipsoid::_getLength(const int &x, const int &y, const int &z) const
47  {
48  if (x == mX && y == mY && z == mZ)
49  {
50  return 0.0f;
51  }
52 
53  // x^2 y^2 z^2
54  // / + / + / = 1 (Ellipsoid ecuation)
55  // a^2 b^2 c^2
56  //
57  // maxradatdir = lambda (Xo, Yo, Zo) = lambda; where Xo, Yo and Zo are the components of the normaliced direction vector
58  //
59  // => lambda^2 = 1 / ( EllipsoidEcuation...)
60  //
61  // => lambda = sqrt (...) => maxradatdir = lambda
62 
63  Ogre::Vector3 Direction = Ogre::Vector3(x-mX, y-mY, z-mZ),
64  DirectionNormalized = Direction.normalisedCopy();
65 
66  Ogre::Real a = Ogre::Math::Pow(DirectionNormalized.x, 2) / mA2 +
67  Ogre::Math::Pow(DirectionNormalized.y, 2) / mB2 +
68  Ogre::Math::Pow(DirectionNormalized.z, 2) / mC2,
69 
70  lambda = 1.0f / Ogre::Math::Sqrt(a);
71 
72  return Ogre::Math::Clamp<Ogre::Real>(Direction.length() / lambda, 0, 1);
73  }
74 
75  const Ogre::Vector3 Ellipsoid::getProbabilities(const int& x, const int& y, const int& z) const
76  {
77  float density = Ogre::Math::Pow(1-_getLength(x, y, z), 1.0f/mDensity);
78 
79  return Ogre::Vector3(density, 1-density, density);
80  }
81 
82  void Ellipsoid::updateProbabilities(DataManager::Cell ***c, const int &nx, const int &ny, const int &nz, const bool& delayedResponse)
83  {
84  int u, v, w, uu, vv;
85 
86  float length;
87 
88  for (u = mX-mA; u < mX+mA; u++)
89  {
90  uu = (u<0) ? (u + nx) : u; if (u>=nx) { uu-= nx; }
91 
92  for (v = mY-mB; v < mY+mB; v++)
93  {
94  vv = (v<0) ? (v + ny) : v; if (v>=ny) { vv-= ny; }
95 
96  for (w = mZ-mC; w < mZ+mC; w++)
97  {
98  length = _getLength(u, v, w);
99 
100  if (length < 1)
101  {
102  c[uu][vv][w].phum = 0.005f;
103  c[uu][vv][w].pext = 0.05f;
104  c[uu][vv][w].pact = 0.01f;
105 
106  if (!delayedResponse)
107  {
108  c[uu][vv][w].cld = Ogre::Math::RangeRandom(0,1) > length ? true : false;
109  }
110  }
111  }
112  }
113  }
114  }
115 
116  void Ellipsoid::move(const int& Ax, const int& Ay, const int& Az)
117  {
118  mX += Ax; mY += Ay; mZ += Az;
119  }
120 
121  const bool Ellipsoid::isOutOfCells() const
122  {
123  if ( (mX+mA) >= mNx || (mX-mA) < 0 ||
124  (mY+mB) >= mNy || (mY-mB) < 0 ||
125  (mZ+mZ) >= mNz || (mZ-mZ) < 0 )
126  {
127  return true;
128  }
129 
130  return false;
131  }
132 
133  void Ellipsoid::setDimensions(const Ogre::Vector3& Dimensions)
134  {
135  mA = Dimensions.x;
136  mB = Dimensions.y;
137  mC = Dimensions.z;
138 
139  mA2 = Ogre::Math::Pow(mA, 2);
140  mB2 = Ogre::Math::Pow(mB, 2);
141  mC2 = Ogre::Math::Pow(mC, 2);
142  }
143 
144 }}
SkyX::VClouds::Ellipsoid::mZ
int mZ
Definition: Ellipsoid.h:134
SkyX::VClouds::Ellipsoid::mNx
int mNx
Cells size.
Definition: Ellipsoid.h:137
y
float y
Definition: (ValueTypes) quaternion.h:6
z
float z
Definition: (ValueTypes) quaternion.h:7
SkyX::VClouds::Ellipsoid::getProbabilities
const Ogre::Vector3 getProbabilities(const int &x, const int &y, const int &z) const
Get probabilities at a point.
Definition: Ellipsoid.cpp:75
SkyX::VClouds::DataManager::Cell::phum
float phum
Probabilities.
Definition: DataManager.h:47
SkyX::VClouds::Ellipsoid::mNz
int mNz
Definition: Ellipsoid.h:137
SkyX
Definition: AtmosphereManager.cpp:30
SkyX::VClouds::Ellipsoid::mDensity
Ogre::Real mDensity
Cloud density.
Definition: Ellipsoid.h:140
SkyX::VClouds::DataManager::Cell::pact
float pact
Definition: DataManager.h:47
Ellipsoid.h
w
float w
Definition: (ValueTypes) quaternion.h:4
SkyX::VClouds::Ellipsoid::mB2
int mB2
Definition: Ellipsoid.h:131
SkyX::VClouds::Ellipsoid::mA
int mA
Ellipsoid parameters.
Definition: Ellipsoid.h:131
SkyX::VClouds::Ellipsoid::isOutOfCells
const bool isOutOfCells() const
Determines if the ellipsoid is out of the cells domain and needs to be removed.
Definition: Ellipsoid.cpp:121
SkyX::VClouds::Ellipsoid::mC2
int mC2
Definition: Ellipsoid.h:131
SkyX::VClouds::Ellipsoid::setDimensions
void setDimensions(const Ogre::Vector3 &Dimensions)
Set dimensions.
Definition: Ellipsoid.cpp:133
SkyX::VClouds::Ellipsoid::updateProbabilities
void updateProbabilities(DataManager::Cell ***c, const int &nx, const int &ny, const int &nz, const bool &delayedResponse=true)
Update probabilities.
Definition: Ellipsoid.cpp:82
SkyX::VClouds::DataManager::Cell
Cell struct.
Definition: DataManager.h:41
SkyX::VClouds::Ellipsoid::mNy
int mNy
Definition: Ellipsoid.h:137
SkyX::VClouds::Ellipsoid::mC
int mC
Definition: Ellipsoid.h:131
SkyX::VClouds::Ellipsoid::move
void move(const int &Ax, const int &Ay, const int &Az)
Move the ellipsoid.
Definition: Ellipsoid.cpp:116
SkyX::VClouds::Ellipsoid::~Ellipsoid
~Ellipsoid()
Destructor.
Definition: Ellipsoid.cpp:42
SkyX::VClouds::DataManager::Cell::cld
bool cld
Definition: DataManager.h:44
SkyX::VClouds::Ellipsoid::mB
int mB
Definition: Ellipsoid.h:131
SkyX::VClouds::Ellipsoid::mA2
int mA2
Definition: Ellipsoid.h:131
SkyX::VClouds::Ellipsoid::mY
int mY
Definition: Ellipsoid.h:134
Ogre
Definition: ExtinguishableFireAffector.cpp:35
SkyX::VClouds::Ellipsoid::Ellipsoid
Ellipsoid(const int &a, const int &b, const int &c, const int &nx, const int &ny, const int &nz, const int &x, const int &y, const int &z, const Ogre::Real &DynLibManager=1.0f)
Constructor.
Definition: Ellipsoid.cpp:29
SkyX::VClouds::Ellipsoid::_getLength
const float _getLength(const int &x, const int &y, const int &z) const
Get length.
Definition: Ellipsoid.cpp:46
x
float x
Definition: (ValueTypes) quaternion.h:5
SkyX::VClouds::DataManager::Cell::pext
float pext
Definition: DataManager.h:47
SkyX::VClouds::Ellipsoid::mX
int mX
Position.
Definition: Ellipsoid.h:134