File: recSchema.cpp

package info (click to toggle)
faust 0.9.46-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 15,256 kB
  • ctags: 9,961
  • sloc: cpp: 47,746; sh: 2,254; ansic: 1,503; makefile: 1,211; ruby: 950; yacc: 468; objc: 459; lex: 200; xml: 177
file content (282 lines) | stat: -rw-r--r-- 8,338 bytes parent folder | download
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/************************************************************************
 ************************************************************************
    FAUST compiler
	Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    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., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/


#include "recSchema.h"
#include <iostream>
#include <assert.h>

using namespace std;

/**
 * Creates a new recursive schema (s1 ~ s2). The smallest component is
 * enlarged to the width of the other. The left and right horizontal
 * margins are computed according to the number of internal connections.
 */
schema* makeRecSchema (schema* s1, schema* s2)
{
	schema*	a = makeEnlargedSchema(s1, s2->width());
	schema*	b = makeEnlargedSchema(s2, s1->width());
	double 	m = dWire * max(b->inputs(), b->outputs());
	double 	w = a->width() + 2*m;

	return new recSchema(a,b,w);
}

/**
 * Constructor of a recursive schema (s1 ~ s2). The two components
 * are supposed to have the same width.
 */
recSchema::recSchema (schema* s1, schema* s2, double width)
	: 	schema(	s1->inputs() - s2->outputs(),
				s1->outputs(),
				width,
				s1->height() + s2->height() ),
		fSchema1(s1),
		fSchema2(s2)
{
	// this version only accepts legal expressions of same width
	assert(s1->inputs() >= s2->outputs());
	assert(s1->outputs() >= s2->inputs());
	assert(s1->width() >= s2->width());

	// create the input and output points
    for (unsigned int i=0; i<inputs(); i++) 	fInputPoint.push_back(point(0,0));
    for (unsigned int i=0; i<outputs(); i++) 	fOutputPoint.push_back(point(0,0));

}

/**
 * The two subschema are placed centered vertically, s2 on top of s1.
 * The input and output points are computed.
 */
void recSchema::place(double ox, double oy, int orientation)
{
	beginPlace(ox, oy, orientation);

	double dx1 = (width() - fSchema1->width())/2;
	double dx2 = (width() - fSchema2->width())/2;

	// place the two sub diagrams
	if (orientation == kLeftRight) {
		fSchema2->place(ox+dx2, oy, kRightLeft);
		fSchema1->place(ox+dx1, oy+fSchema2->height(), kLeftRight);
	} else {
		fSchema1->place(ox+dx1, oy, kRightLeft);
		fSchema2->place(ox+dx2, oy+fSchema1->height(), kLeftRight);
	}


	// adjust delta space to orientation
	if (orientation == kRightLeft) { dx1 = -dx1; }

	// place input points
	for (unsigned int i=0; i<inputs(); i++) {
		point p = fSchema1->inputPoint(i+fSchema2->outputs());
		fInputPoint[i] = point(p.x-dx1, p.y);
	}

	// place output points
	for (unsigned int i=0; i<outputs(); i++) {
		point p = fSchema1->outputPoint(i);
		fOutputPoint[i] = point(p.x+dx1, p.y);
	}

	endPlace();
}


/**
 * The input points s1 ~ s2
 */
point recSchema::inputPoint(unsigned int i) const
{
	return fInputPoint[i];
}


/**
 * The output points s1 ~ s2
 */
point recSchema::outputPoint(unsigned int i) const
{
	return fOutputPoint[i];
}


/**
 * Draw the two subschema s1 and s2 as well as the feedback
 * connections between s1 and s2, and the feedfrom connections
 * beetween s2 and s1.
 */
void recSchema::draw(device& dev)
{
    assert(placed());

    // draw the two subdiagrams
    fSchema1->draw(dev);
    fSchema2->draw(dev);

    // draw the output lines
    for (unsigned int i=0; i<outputs(); i++) {
        point p = fSchema1->outputPoint(i);
        point q = outputPoint(i);
        //dev.trait(p.x, p.y, q.x, q.y);
    }

    // draw the input lines
    unsigned int skip = fSchema2->outputs();
    for (unsigned int i=0; i<inputs(); i++) {
        point p = fSchema1->inputPoint(i+skip);
        point q = inputPoint(i);
        //dev.trait(p.x, p.y, q.x, q.y);
    }

    // draw the feedback connections to each fSchema2 input
    for (unsigned int i=0; i<fSchema2->inputs(); i++) {
        drawFeedback(dev, fSchema1->outputPoint(i), fSchema2->inputPoint(i), i*dWire);
    }

    // draw the feedfront connections from each fSchema2 output
    for (unsigned int i=0; i<fSchema2->outputs(); i++) {
        drawFeedfront(dev, fSchema2->outputPoint(i), fSchema1->inputPoint(i), i*dWire);
    }
}


/**
 * Draw the delay sign of a feedback connection
 */
void recSchema::drawDelaySign(device& dev, double x, double y, double size)
{
    dev.trait(x-size/2, y, x-size/2, y-size);
    dev.trait(x-size/2, y-size, x+size/2, y-size);
    dev.trait(x+size/2, y-size, x+size/2, y);
}


/**
 * Draw the two subschema s1 and s2 as well as the feedback
 * connections between s1 and s2, and the feedfrom connections
 * beetween s2 and s1.
 */
void recSchema::collectTraits(collector& c)
{
    assert(placed());

    // draw the two subdiagrams
    fSchema1->collectTraits(c);
    fSchema2->collectTraits(c);

    // draw the feedback connections to each fSchema2 input
    for (unsigned int i=0; i<fSchema2->inputs(); i++) {
        collectFeedback(c, fSchema1->outputPoint(i), fSchema2->inputPoint(i), i*dWire, outputPoint(i));
    }

    // draw the non recursive output lines
    for (unsigned int i=fSchema2->inputs(); i<outputs(); i++) {
        point p = fSchema1->outputPoint(i);
        point q = outputPoint(i);
        c.addTrait(trait(p,q));     // in->out order
    }

    // draw the input lines
    unsigned int skip = fSchema2->outputs();
    for (unsigned int i=0; i<inputs(); i++) {
        point p = inputPoint(i);
        point q = fSchema1->inputPoint(i+skip);
        c.addTrait(trait(p,q));     // in->out order
    }

    // draw the feedfront connections from each fSchema2 output
    for (unsigned int i=0; i<fSchema2->outputs(); i++) {
        collectFeedfront(c, fSchema2->outputPoint(i), fSchema1->inputPoint(i), i*dWire);
    }
}



/**
 * Draw a feedback connection between two points with an horizontal
 * displacement dx
 */
void recSchema::drawFeedback(device& dev, const point& src, const point& dst, double dx)
{
    double	ox = src.x + ((orientation()==kLeftRight) ? dx : -dx);
    double	ct = (orientation()==kLeftRight) ? dWire/2 : -dWire/2;

    drawDelaySign(dev, ox, src.y, ct);
    //dev.trait(ox, src.y-ct, ox, dst.y);
    //dev.trait(ox, dst.y, dst.x, dst.y);
}



/**
 * Draw a feedback connection between two points with an horizontal
 * displacement dx
 */
void recSchema::collectFeedback(collector& c, const point& src, const point& dst, double dx, const point& out)
{
    double	ox = src.x + ((orientation()==kLeftRight) ? dx : -dx);
    double	ct = (orientation()==kLeftRight) ? dWire/2 : -dWire/2;

    point   up(ox, src.y-ct);
    point   br(ox+ct/2.0, src.y);

    c.addOutput(up);
    c.addOutput(br);
    c.addInput(br);

    c.addTrait(trait(up, point(ox, dst.y)));
    c.addTrait(trait(point(ox, dst.y), point(dst.x, dst.y)));
    c.addTrait(trait(src,br));
    c.addTrait(trait(br,out));

}


/**
 * Draw a feedfrom connection between two points with an horizontal
 * displacement dx
 */
void recSchema::drawFeedfront(device& dev, const point& src, const point& dst, double dx)
{
//    double	ox = src.x + ((orientation()==kLeftRight) ? -dx : dx);

//	dev.trait(ox, src.y, src.x, src.y);
//	dev.trait(ox, src.y, ox, dst.y);
//	dev.trait(ox, dst.y, dst.x, dst.y);
}


/**
 * Draw a feedfrom connection between two points with an horizontal
 * displacement dx
 */
void recSchema::collectFeedfront(collector& c, const point& src, const point& dst, double dx)
{
    double	ox = src.x + ((orientation()==kLeftRight) ? -dx : dx);

    c.addTrait(trait(point(src.x, src.y), point(ox, src.y)));
    c.addTrait(trait(point(ox, src.y), point(ox, dst.y)));
    c.addTrait(trait(point(ox, dst.y), point(dst.x, dst.y)));
}