|
From: <mk...@us...> - 2003-03-26 10:13:58
|
Update of /cvsroot/csp/APPLICATIONS/SimData/Source
In directory sc8-pr-cvs1:/tmp/cvs-serv2840/Source
Added Files:
Noise.cpp Real.cpp
Removed Files:
Spread.cpp
Log Message:
--- NEW FILE: Noise.cpp ---
/* SimDataCSP: Data Infrastructure for Simulations
* Copyright (C) 2002 Mark Rose <tm...@st...>
*
* This file is part of SimDataCSP.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <SimData/Noise.h>
#include <SimData/Random.h>
NAMESPACE_SIMDATA
Perlin1D::Perlin1D(double persistence, int octaves, Interpolation interpolation) {
m_persistence = persistence;
m_octaves = octaves;
m_interpolation = interpolation;
randomize();
}
void Perlin1D::setParameters(double persistence, int octaves) {
m_persistence = persistence;
m_octaves = octaves;
}
void Perlin1D::setInterpolation(Interpolation interpolation) {
m_interpolation = interpolation;
}
void Perlin1D::setOffset(int idx) {
m_offset = idx;
}
void Perlin1D::randomize() {
m_offset = int(g_Random.newRand()*1.0e+9);
}
double Perlin1D::_getInterpolatedNoise(double x) {
int integer_X = int(x);
double fractional_X = x - integer_X;
double v1 = _getSmoothedNoise(integer_X);
double v2 = _getSmoothedNoise(integer_X + 1);
if (m_interpolation == LINEAR) {
return _linearInterpolate(v1, v2, fractional_X);
} else
if (m_interpolation == COSINE) {
return _cosineInterpolate(v1, v2, fractional_X);
}
double v0 = _getSmoothedNoise(integer_X - 1);
double v3 = _getSmoothedNoise(integer_X + 2);
return _cubicInterpolate(v0, v1, v2, v3, fractional_X);
}
double Perlin1D::getValue(double x) {
double total = 0.0;
double amplitude = 1.0;
double frequency = 1.0;
for (int i = 0; i < m_octaves; i++) {
amplitude *= m_persistence;
total += _getInterpolatedNoise(x * frequency + m_offset) * amplitude;
frequency *= 2.0;
}
return total;
}
std::vector<float> Perlin1D::generate(int n, bool periodic, double timescale, double amplitude, double offset) {
std::vector<float> buffer(n);
if (n > 0) {
int i;
double s = timescale/n;
for (i=0; i<n; i++) {
double f = i*s;
buffer[i] = (float) (getValue(f)*amplitude + offset);
}
if (periodic) {
s = 1.0 / n;
for (i=0; i<n/2; i++) {
float f = 0.5 + i*s;
float a = buffer[i];
float b = buffer[n-i-1];
buffer[i] = f*a+(1-f)*b;
buffer[n-i-1] = f*b+(1-f)*a;
}
}
}
return buffer;
}
NAMESPACE_END // namespace simdata
--- NEW FILE: Real.cpp ---
/* SimDataCSP: Data Infrastructure for Simulations
* Copyright (C) 2002 Mark Rose <tm...@st...>
*
* This file is part of SimDataCSP.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <SimData/Real.h>
#include <SimData/Random.h>
#include <SimData/Pack.h>
NAMESPACE_SIMDATA
// class Real
void Real::pack(Packer &p) const {
p.pack(_mean);
p.pack(_sigma);
}
void Real::unpack(UnPacker &p) {
p.unpack(_mean);
p.unpack(_sigma);
regen();
}
void Real::parseXML(const char* cdata) {
std::string s(cdata);
float mean, sigma = 0.0;
bool ok = true;
std::string::size_type pos = s.find(':');
if (pos == std::string::npos) {
int n = sscanf(s.c_str(), "%f", &mean);
if (n != 1) ok = false;
} else {
int n = sscanf(s.c_str(), "%f:%f", &mean, &sigma);
if (n != 2) ok = false;
}
set(mean, sigma);
if (!ok) throw ParseException("SYNTAX ERROR: expect 'float' or 'float:float'");
}
Real::Real(float mean, float sigma) {
set(mean, sigma);
regen();
}
void Real::set(float mean, float sigma) {
_mean = mean;
_sigma = sigma;
regen();
}
void Real::regen() {
if (_sigma <= 0.0)
_value = _mean;
else
_value = box_muller(_mean, _sigma);
}
float Real::getMean() { return _mean; }
float Real::getSigma() { return _sigma; }
float Real::getValue() { return _value; }
std::string Real:: asString() const {
char fmt[128];
snprintf(fmt, 128, "%f", _value);
return std::string(fmt);
}
float Real::__neg__() { return -_value; }
float Real::__pos__() { return _value; }
float Real::__abs__() { return static_cast<float>(fabs(_value)); }
int Real::__nonzero__() { return _value != 0.0; }
float Real::__float__() { return _value; }
int Real::__int__() { return (int)_value; }
long Real::__long__() { return (long)_value; }
float Real::__add__(float v) { return v + _value; }
float Real::__radd__(float v) { return v + _value; }
float Real::__sub__(float v) { return _value - v; }
float Real::__rsub__(float v) { return v - _value; }
float Real::__mul__(float v) { return v * _value; }
float Real::__rmul__(float v) { return v * _value; }
float Real::__div__(float v) { assert(v != 0.0); return _value / v; }
float Real::__rdiv__(float v) { assert(_value != 0.0); return v / _value; }
float Real::__rpow__(float v) { return static_cast<float>(pow(v, _value)); }
float Real::__pow__(float v) { return static_cast<float>(pow(_value, v)); }
NAMESPACE_END // namespace simdata
--- Spread.cpp DELETED ---
|