File: solver.h

package info (click to toggle)
din 5.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,180 kB
  • ctags: 2,511
  • sloc: cpp: 9,369; sh: 6,563; ansic: 2,977; tcl: 1,770; makefile: 283
file content (228 lines) | stat: -rw-r--r-- 5,193 bytes parent folder | download | duplicates (3)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * This file is part of din.
 *
 * din is copyright (c) 2006 - 2012 S Jagannathan <jag@dinisnoise.org>
 * For more information, please visit http://dinisnoise.org
 *
 * din 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.
 *
 * din 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 din.  If not, see <http://www.gnu.org/licenses/>.
 *
*/
#ifndef __SOLVER
#define __SOLVER

#include "crvpt.h"
#include "multi_curve.h"
#include <vector>


struct solver;

struct xhandler {
  virtual void operator() (solver& s, float& x, float& dx) = 0;
  virtual ~xhandler () {}
};

struct atmin: xhandler {
  void operator() (solver& s, float& x, float& dx);
};

struct atmax: xhandler {
  void operator() (solver& s, float& x, float& dx);
};

struct tomin: xhandler {
  void operator() (solver& s, float& x, float& dx);
};

struct loopmin : atmin {
  void operator() (solver& s, float& x, float& dx);
};

struct loopmax : tomin {
  void operator() (solver& s, float& x, float& dx);
};

struct pongmax: atmax {
  void operator() (solver& s, float& x, float& dx);
};

struct pongmin: atmin {
  void operator() (solver& s, float& x, float& dx);
};

struct curve_editor;

struct gotog : xhandler {
  float g;
  curve_editor* ed;
  gotog (float gg, curve_editor* e); 
  void operator() (solver& s, float& x, float& dx);
  void set (float gg);
};

extern atmin _atmin;
extern atmax _atmax;
extern tomin _tomin;
extern loopmin _loopmin;
extern loopmax _loopmax;
extern pongmin _pongmin;
extern pongmax _pongmax;
extern gotog _gotog;

struct solver {

  //
  // given x return y
  //
  // assumes x such that
  // x0 <= x1 <= x2 <= ...... <= xlast
  //

  // solved curve
  multi_curve* mcrv;

  int ncurvs, icurv, last_curv;
  int iseg;

  // line segment on curve expected to contain x
  float startx, starty;
  float endx, endy;
  float m, inf;
  float ycomp;

  // first and last points
  float firstx, firsty;
  float lastx, lasty;

  float result; // last result

  inline int inseg (float x, int c, int i) {
    std::vector<curve>& curv = mcrv->curv;
    std::vector<crvpt>& vpts = curv[c].vpts;
    float leftx = vpts[i].x, rightx = vpts[i+1].x;

    if (x >= leftx && x <= rightx) {
        setseg (c, i);
        return 1;
    }
    return 0;
  }

  void setseg (int c, int i) {

    std::vector<curve>& curv = mcrv->curv;
    std::vector<crvpt>& vpts = curv[c].vpts;

    crvpt& start = vpts[i];
    crvpt& end = vpts[i+1];

    startx = start.x;
    starty = start.y;
    endx = end.x;
    endy = end.y;
    m = start.m;
    inf = start.inf;

    if (inf == 1) ycomp = endy; else ycomp = starty - m * startx;

    icurv = c;
    iseg = i;

  }

  inline int lastseg (int c) {
    std::vector<curve>& curv = mcrv->curv;
    std::vector<crvpt>& vpts = curv[c].vpts;
    return vpts.size() - 2;
    return 0;
  }

  inline int getsegs (int c) {
    return (mcrv->curv[c].vpts.size() - 1);
  }

  inline int searchleft (float x) {
    int lseg = iseg, lcurv = icurv;
    while (1) {
      if (--lseg < 0) {
        --lcurv;
        if (lcurv < 0) break;
        lseg = lastseg (lcurv);
      }
      if (inseg (x, lcurv, lseg)) return 1;
    }
    return 0;

  }

  inline int searchright (float x) {
    int rseg = iseg, rcurv = icurv;
    int rsegs = getsegs (rcurv);
    while (1) {
      if (++rseg < rsegs); else {
        ++rcurv;
        if (rcurv >= ncurvs) break;
        rsegs = getsegs (rcurv);
        rseg = 0;
      }
      if (inseg (x, rcurv, rseg)) return 1;
    }
    return 0;

  }

  inline int findseg (float x) {

    int fcurv = 0, fseg = 0, nsegs = getsegs (fcurv);
    while (1) {
      if (inseg (x, fcurv, fseg)) return 1; else {
        ++fseg;
        if (fseg >= nsegs) {
          if (++fcurv >= ncurvs) {
            setseg (ncurvs - 1, nsegs - 1);
            break;
          }
          fseg = 0;
          nsegs = getsegs (fcurv);
        }
      }
    }
    return 0;

  }

  solver ();
  solver (multi_curve* crv);

  void operator() (multi_curve* crv);

  // given x, solve y
  float operator() (float x);
  float operator() (float &x, float& dx, xhandler& xmin = _atmin, xhandler& xmax = _tomin);

  void operator() (float& x, float& dx, int n, float* soln, xhandler& xmin = _atmin, xhandler& xmax = _tomin); // fast solver when x is incremented by dx n times, solution stored in soln
  void operator() (float& x, float& dx, int n, float* mod, float* soln, xhandler& xmin = _atmin, xhandler& xmax = _tomin); // same as last solver but x is also modulated by mod.
  void operator() (float* ax, int n, xhandler& xmin = _atmin, xhandler& xmax = _atmax); // given an array of x, store solution in same array --> used by compressor

  void init ();
  
  void update (); // when multi curve has changed


};




#endif