fileAirstreamFilter.cpp
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
*  AirstreamFilter.cpp
*
*  Created by Daisuke Nogami on Nov, 05.
*  opyright 2005 Daisuke Nogami [null-null.net]. Some rights reserved.
*/
 
#include "stdafx.h"
#include "AirstreamFilter.h"
 
 
#include "math.h"
#include "time.h"
 
 
////////////////////////////////////////////////////
// AirstreamSource
////////////////////////////////////////////////////
 
// param
#define DAMPING 0.95
#define MASS 30.0
#define SPRING_CONSTANT 0.2
 
AirstreamSource::AirstreamSource(){
  dirX = nextDirX = 0;
  dirY = nextDirY = 0;
  dirZ = nextDirZ = 0;
  relative_velx = relative_vely = relative_velz = 0;
  velx = vely = velz = 0;
}
 
void AirstreamSource::next( float wind_dir ){
  nextDirX = ((float)rand()/RAND_MAX-0.5+wind_dir)*1024;
  nextDirY = ((float)rand()/RAND_MAX-0.5)*768;
  nextDirZ = (float)rand()/RAND_MAX*768;
}
 
void AirstreamSource::move(){
  float force, accel;
 
  // x axis
  force = -SPRING_CONSTANT * ( dirX - (nextDirX+relative_velx) );  //f=-kx
  accel = force/MASS;  //f=ma == a=f/m
  velx = DAMPING * ( velx + accel );
  dirX += velx;
 
  // y axis
  force = -SPRING_CONSTANT * ( dirY - (nextDirY+relative_vely) );  //f=-ky
  accel = force/MASS;  //f=ma == a=f/m
  vely = DAMPING * ( vely + accel );
  dirY += vely;
 
  // z axis
  force = -SPRING_CONSTANT * ( dirZ - (nextDirZ+relative_velz) );  //f=-ky
  accel = force/MASS;  //f=ma == a=f/m
  velz = DAMPING * ( velz + accel );
  dirZ += velz;
}
 
////////////////////////////////////////////////////
// AirstreamFilter
////////////////////////////////////////////////////
 
#define BLOWING_INTERVEL 30
 
AirstreamFilter::AirstreamFilter(){
  wind_dir = 0; // -->
  wind_speed = 0;
  next_wind_speed = 0;
 
  // initialize random seed
  srand( time( NULL ) );
}
 
// parameters:
//  wind_dir - wind direction [-0.5~0.5]
//  wind_spped - wind power
void AirstreamFilter::sync( float wind_dir, float wind_spped ){
  this->wind_dir = wind_dir;
  this->next_wind_speed = wind_spped;
}
 
void AirstreamFilter::update(){
  // data interpolation
  if( next_wind_speed != wind_speed ){
    wind_speed += (next_wind_speed-wind_speed)*0.1;
    if( abs(wind_speed-next_wind_speed) < 0.1 ){
      wind_speed = next_wind_speed;
    }
  }
 
  // update airstream source
  static timer = 0;
  if( timer++ > BLOWING_INTERVEL ){
    as_src.next( wind_dir );
    timer = 0;
  }
  as_src.move();
}
 
void AirstreamFilter::mappingTo( CellSet* cellset ){
  // wind source power
  float windsrc_pow_a = 0.02*wind_speed; // to cell A
  float windsrc_pow_b = windsrc_pow_a*0.8; // to cell B
 
  int x, y, idx;
  float alpha = 0, beta = 0;
  Cell *tcell_a, *tcell_b;
  Cell *tcell_na, *tcell_nb;
 
  // -->
  if( wind_dir >= 0 ){
 
    // set airstream
    for( y = 0; y < cellset->hdiv; y++ ){
      idx = y*cellset->wdiv;
      tcell_a = &cellset->cells_a[idx];
      tcell_b = &cellset->cells_b[idx];
 
      // cell A
      alpha = ( 1 - ((float)abs( as_src.dirZ - tcell_a->ypos )/768) ) +0.3;
      alpha = (alpha*alpha) * windsrc_pow_a;
      tcell_a->next( as_src.dirX*alpha, as_src.dirY*alpha );
 
      // cell B
      beta = ( 1 - ((float)abs( as_src.dirZ - tcell_b->ypos )/768) ) +0.3;
      beta = (beta*beta) * windsrc_pow_b;
      tcell_b->next( as_src.dirX*beta, as_src.dirY*beta );
    }
 
    // transmission
    for( y = 0; y < cellset->hdiv; y++ ){
      for( x = cellset->wdiv-1; x >= 1; x-- ){
 
        idx = x+y*cellset->wdiv;
        tcell_a = &cellset->cells_a[idx];
        tcell_b = &cellset->cells_b[idx];
 
        idx = (x-1)+y*cellset->wdiv;
        tcell_na = &cellset->cells_a[idx];
        tcell_nb = &cellset->cells_b[idx];
 
        tcell_a->next( tcell_na->nextx, tcell_na->nexty );
        tcell_b->next( tcell_nb->nextx, tcell_nb->nexty );
      }
    }
  }
 
  // <--
  else{
    // set airstream
    for( y = 0; y < cellset->hdiv; y++ ){
      idx = (cellset->wdiv-1)+y*cellset->wdiv;
      tcell_a = &cellset->cells_a[idx];
      tcell_b = &cellset->cells_b[idx];
 
      // cell A
      alpha = ( 1 - ((float)abs( as_src.dirZ - tcell_a->ypos )/768) ) +0.3;
      alpha = (alpha*alpha) * windsrc_pow_a;
      tcell_a->next( as_src.dirX*alpha, as_src.dirY*alpha );
 
      // cell B
      beta = ( 1 - ((float)abs( as_src.dirZ - tcell_b->ypos)/768) )+0.3;
      beta = (beta*beta) * windsrc_pow_b;
      tcell_b->next( as_src.dirX*beta, as_src.dirY*beta );
    }
 
    // transmission
    for( y = 0; y < cellset->hdiv; y++ ){
      for( x = 0; x < cellset->wdiv-1; x++ ){
 
        idx = x+y*cellset->wdiv;
        tcell_a = &cellset->cells_a[idx];
        tcell_b = &cellset->cells_b[idx];
 
        idx = (x+1)+y*cellset->wdiv;
        tcell_na = &cellset->cells_a[idx];
        tcell_nb = &cellset->cells_b[idx];
 
        tcell_a->next( tcell_na->nextx, tcell_na->nexty );
        tcell_b->next( tcell_nb->nextx, tcell_nb->nexty );
      }
    }
  }
}
[トップ] [一覧] [最終更新] [検索] [バックアップ]