iWaveOcean
3ds Max ocean simulation plugin
Convolution.h
Go to the documentation of this file.
1 #pragma once
2 #include "IConvolution.h"
3 
7 {
14 };
15 
21 template <int radius, ConvolutionEdgeBehavior behavior>
22 class Convolution : public IConvolution<radius>
23 {
24  static const int kernel_width = 2 * radius + 1;
25  float _kernel[kernel_width][kernel_width];
26 
27 public:
28  Convolution(float kernel[kernel_width][kernel_width])
29  {
30  for (int i = 0; i < kernel_width; i++)
31  {
32  for (int j = 0; j < kernel_width; j++)
33  {
34  _kernel[i][j] = kernel[i][j];
35  }
36  }
37  }
38 
39  ~Convolution(void)
40  {
41  }
42 
43  inline void Convolve(float* in, float* out, int rows, int cols) const
44  {
45  for (int i = 0; i < rows; i++) {
46  for (int j = 0; j < cols; j++) {
47  int vtx = i * cols + j;
48  float val = 0.0f; // Output value of kernel at specific point.
49 
50  for (int kern_x = -radius; kern_x <= radius; kern_x++) {
51  for (int kern_y = -radius; kern_y <= radius; kern_y++) {
52  int other_vtx_x = 0;
53  int other_vtx_y = 0;
54  if (behavior == ExtendEdges)
55  {
56  other_vtx_x = min(max(i + kern_x, 0), rows - 1);
57  other_vtx_y = min(max(j + kern_y, 0), cols - 1);
58  }
59  else if (behavior == WrapEdges)
60  {
61  other_vtx_x = (((i + kern_x) % rows) + rows) % rows;
62  other_vtx_y = (((j + kern_y) % rows) + rows) % rows;
63  }
64  else if (behavior == ReflectEdges)
65  {
66  // Flip negative values (i.e. -x => x).
67  other_vtx_x = abs(i + kern_x);
68  other_vtx_y = abs(j + kern_y);
69 
70  // Flip all values over the boundary (i.e. BOUND + x => BOUND - x).
71  int diff_x = abs(other_vtx_x - (rows - 1));
72  other_vtx_x = (rows - 1) - diff_x;
73 
74  int diff_y = abs(other_vtx_y - (cols - 1));
75  other_vtx_y = (cols - 1) - diff_y;
76  }
77 
78  int other_vtx = other_vtx_x * cols + other_vtx_y;
79  val += _kernel[kern_x + radius][kern_y + radius] * in[other_vtx];
80  }
81  }
82 
83  out[vtx] = val;
84  }
85  }
86  }
87 };
88 
Definition: IConvolution.h:5
Definition: Convolution.h:22
Definition: Convolution.h:13
Definition: Convolution.h:9
ConvolutionEdgeBehavior
Definition: Convolution.h:6
Definition: Convolution.h:11
void Convolve(float *in, float *out, int rows, int cols) const
Definition: Convolution.h:43