Rigs of Rods 2023.09
Soft-body Physics Simulation
Loading...
Searching...
No Matches
Ellipsoid.cpp
Go to the documentation of this file.
1/*
2--------------------------------------------------------------------------------
3This source file is part of SkyX.
4Visit http://www.paradise-studios.net/products/skyx/
5
6Copyright (C) 2009-2012 Xavier Verguín González <xavyiy@gmail.com>
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt.
21--------------------------------------------------------------------------------
22*/
23
24#include "Ellipsoid.h"
25
26namespace 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
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}}
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
const bool isOutOfCells() const
Determines if the ellipsoid is out of the cells domain and needs to be removed.
int mA
Ellipsoid parameters.
Definition Ellipsoid.h:131
Ogre::Real mDensity
Cloud density.
Definition Ellipsoid.h:140
const float _getLength(const int &x, const int &y, const int &z) const
Get length.
Definition Ellipsoid.cpp:46
void setDimensions(const Ogre::Vector3 &Dimensions)
Set dimensions.
void updateProbabilities(DataManager::Cell ***c, const int &nx, const int &ny, const int &nz, const bool &delayedResponse=true)
Update probabilities.
Definition Ellipsoid.cpp:82
void move(const int &Ax, const int &Ay, const int &Az)
Move the ellipsoid.
const Ogre::Vector3 getProbabilities(const int &x, const int &y, const int &z) const
Get probabilities at a point.
Definition Ellipsoid.cpp:75