Toshihiro Shimizu 890ddd
// #include <iostream></iostream>
Toshihiro Shimizu 890ddd
#include <cmath> /* pow(), ceil() */</cmath>
Toshihiro Shimizu 890ddd
#include "igs_motion_wind_table.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int igs::motion_wind::table_size(
Toshihiro Shimizu 890ddd
	const double length_min, const double length_max)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (length_min < length_max) {
Toshihiro Shimizu 890ddd
		return static_cast<int>(ceil(length_max));</int>
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return static_cast<int>(ceil(length_min));</int>
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
引数の(3つの)ランダムクラスは別々でもいいし、同じものでもいい。
Toshihiro Shimizu 890ddd
別々でシード値を同じにすればlength,force,densityの3つとも
Toshihiro Shimizu 890ddd
同じランダム列にすることができる。
Toshihiro Shimizu 890ddd
同じクラスを使う場合は、length,force,densityを混ぜてランダムになる。
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
int igs::motion_wind::make_table(
Toshihiro Shimizu 890ddd
	std::vector<double> &table, igs::math::random &length_random, igs::math::random &force_random, igs::math::random &density_random, const double length_min, const double length_max, const double length_bias // 0<...1...</double>
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double force_min, const double force_max, const double force_bias // 0<...1...
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double density_min, const double density_max, const double density_bias // 0<...1...
Toshihiro Shimizu 890ddd
	)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* length_minと、length_maxが同じ値なら固定長 */
Toshihiro Shimizu 890ddd
	double length = length_min;
Toshihiro Shimizu 890ddd
	if (length_min != length_max) {			 /* minとmaxの間でrandomな長さ */
Toshihiro Shimizu 890ddd
		double dif = length_random.next_d(); /* 0...1 */
Toshihiro Shimizu 890ddd
		if (0.0 != length_bias) {			 /* 長さの片寄り */
Toshihiro Shimizu 890ddd
			dif = pow(dif, 1.0 / length_bias);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		length += dif * (length_max - length_min); /* 実際の長さ */
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	// std::cout << "l " << length;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* force_minと、force_maxが同じ値なら固定カーブ */
Toshihiro Shimizu 890ddd
	double force = force_min;
Toshihiro Shimizu 890ddd
	if (force_min != force_max) {			/* minとmaxの間でrandomなカーブ */
Toshihiro Shimizu 890ddd
		double dif = force_random.next_d(); /* 0...1 */
Toshihiro Shimizu 890ddd
		if (0.0 != force_bias) {			/* カーブの片寄り */
Toshihiro Shimizu 890ddd
			dif = pow(dif, 1.0 / force_bias);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		force += dif * (force_max - force_min); /* 実際のカーブ値 */
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	// std::cout << "  c " << force;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* density_minと、density_maxが同じ値なら固定高さ */
Toshihiro Shimizu 890ddd
	double density = density_min;
Toshihiro Shimizu 890ddd
	if (density_min != density_max) {		  /* minからmaxの間でrandomな高 */
Toshihiro Shimizu 890ddd
		double dif = density_random.next_d(); /* 0...1 */
Toshihiro Shimizu 890ddd
		if (0.0 != density_bias) {			  /* 高さの片寄り */
Toshihiro Shimizu 890ddd
			dif = pow(dif, 1.0 / density_bias);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		density += dif * (density_max - density_min); /* 実際の高さ値 */
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	// std::cout << "  h " << density << "\n";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const int table_len = static_cast<int>(ceil(length));</int>
Toshihiro Shimizu 890ddd
	/* 長さ:リニア減衰列を設定する
Toshihiro Shimizu 890ddd
		table.at[0]          [1]            [2]	
Toshihiro Shimizu 890ddd
	length	ii
Toshihiro Shimizu 890ddd
	3.0 --> 3 -->	1.0(3.0/3.0) 0.666(2.0/3.0) 0.333(1.0/3.0)
Toshihiro Shimizu 890ddd
	2.5 --> 3 -->	1.0(2.5/2.5) 0.6(1.5/2.5)   0.2(0.5/2.5)
Toshihiro Shimizu 890ddd
	2.0 --> 2 -->	1.0(2.0/2.0) 0.5(1.0/2.0)   [0.0(0.0/1.5)]
Toshihiro Shimizu 890ddd
	1.5 --> 2 -->	1.0(1.5/1.5) 0.333(0.5/1.5) [-0.333(-0.5/1.5)]
Toshihiro Shimizu 890ddd
	1.0 --> 1 -->	1.0(1.0/1.0) [0.0(0.0/1.0)]
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
	for (int ii = 0; ii < table_len; ++ii) {
Toshihiro Shimizu 890ddd
		table.at(ii) = (length - ii) / length;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 勢い:ガンマ計算によりカーブを設定する */
Toshihiro Shimizu 890ddd
	if (1.0 != force) { /* 変化のないとき(1のとき)は動作しない */
Toshihiro Shimizu 890ddd
		if (0.0 < force) {
Toshihiro Shimizu 890ddd
			for (int ii = 1; ii < table_len; ++ii) {
Toshihiro Shimizu 890ddd
				table.at(ii) = pow(table.at(ii), 1.0 / force);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else { /* ゼロ以下はゼロを設定する */
Toshihiro Shimizu 890ddd
			for (int ii = 1; ii < table_len; ++ii) {
Toshihiro Shimizu 890ddd
				table.at(ii) = 0.0;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 濃さ:(高さ)の制御 */
Toshihiro Shimizu 890ddd
	for (int ii = 1; ii < table_len; ++ii) {
Toshihiro Shimizu 890ddd
		table.at(ii) *= density;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return table_len;
Toshihiro Shimizu 890ddd
}