File: seqSchema.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 (363 lines) | stat: -rw-r--r-- 10,837 bytes parent folder | download | duplicates (2)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/************************************************************************
 ************************************************************************
    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 "seqSchema.h"
#include <iostream>
#include <assert.h>

using namespace std;

enum {kHorDir, kUpDir, kDownDir}; ///< directions of connections

static double computeHorzGap(schema* a, schema* b);
static int direction(const point& a, const point& b);


//----------------------------INTERFACE--------------------------------

/**
 * Make a sequential schema. May add cables to ensure the internal
 * connections are between the same number of outputs and inputs.
 * Compute an horizontal gap based on the number of upward and
 * downward connections.
 */
schema * makeSeqSchema (schema* s1, schema* s2)
{
	unsigned int o = s1->outputs();
	unsigned int i = s2->inputs();

	schema* a = (o < i) ? makeParSchema(s1, makeCableSchema(i-o)) : s1;
	schema* b = (o > i) ? makeParSchema(s2, makeCableSchema(o-i)) : s2;

	return new seqSchema(a, b, computeHorzGap(a,b));
}



//-----------------------IMPLEMENTATION------------------------------

/**
 * Constructor for a sequential schema (s1:s2). The components s1 and s2
 * are supposed to be "compatible" (s1 : n->m and s2 : m->q)
 */
seqSchema::seqSchema (schema* s1, schema* s2, double hgap)
	: 	schema(	s1->inputs(),
				s2->outputs(),
				s1->width() + hgap + s2->width(),
				max(s1->height(), s2->height()) ),
		fSchema1(s1),
		fSchema2(s2),
		fHorzGap(hgap)
{
	assert(s1->outputs() == s2->inputs());
}


//-----------------------placement------------------------------


/**
 * Place the two components horizontally with enough space
 * for the connections
 */
void seqSchema::place(double ox, double oy, int orientation)
{
	beginPlace(ox, oy, orientation);

	double y1 = max(0.0, 0.5*(fSchema2->height() - fSchema1->height()));
	double y2 = max(0.0, 0.5*(fSchema1->height() - fSchema2->height()));

	if (orientation == kLeftRight) {
		fSchema1->place(ox, oy+y1, orientation);
		fSchema2->place(ox+fSchema1->width()+fHorzGap, oy+y2, orientation);
	} else {
		fSchema2->place(ox, oy+y2, orientation);
		fSchema1->place(ox+fSchema2->width()+fHorzGap, oy+y1, orientation);
	}
	endPlace();
}


/**
 * The input points are the input points of the first component
 */
point seqSchema::inputPoint(unsigned int i) const
{
	return fSchema1->inputPoint(i);
}


/**
 * The output points are the output points of the second component
 */
point seqSchema::outputPoint(unsigned int i) const
{
	return fSchema2->outputPoint(i);
}



//--------------------------drawing------------------------------


/**
 * Draw the two components as well as the internal wires
 */
void seqSchema::draw(device& dev)
{
    assert(placed());
    assert(fSchema1->outputs() == fSchema2->inputs());

    fSchema1->draw(dev);
    fSchema2->draw(dev);
    //drawInternalWires(dev);
}

/**
 * Draw the two components as well as the internal wires
 */
void seqSchema::collectTraits(collector& c)
{
    assert(placed());
    assert(fSchema1->outputs() == fSchema2->inputs());

    fSchema1->collectTraits(c);
    fSchema2->collectTraits(c);
    collectInternalWires(c);
}


/**
 * Draw the internal wires aligning the vertical segments in
 * a symetric way when possible.
 */

void seqSchema::drawInternalWires(device& dev)
{
	assert (fSchema1->outputs() == fSchema2->inputs());

	const int 	N 	= fSchema1->outputs();
	double 		dx 	= 0;
	double		mx 	= 0;
	int			dir	=-1;

	if (orientation() == kLeftRight) {
		// draw left right cables
		for (int i=0; i<N; i++) {
			point src = fSchema1->outputPoint(i);
			point dst = fSchema2->inputPoint(i);

            int d = direction(src,dst);
            if (d != dir) {
                // compute attributes of new direction
                switch (d) {
                    case kUpDir 	: mx = 0; dx = dWire; break;
                    case kDownDir	: mx = fHorzGap; dx = -dWire; break;
                    default			: mx = 0; dx = 0; break;
                }
                dir = d;
            } else {
                // move in same direction
                mx = mx +dx;
            }
            if (src.y == dst.y) {
                // draw straight cable
                dev.trait(src.x, src.y, dst.x, dst.y);
            } else {
                // draw zizag cable
                dev.trait(src.x, src.y, src.x+mx, src.y);
                dev.trait(src.x+mx, src.y, src.x+mx, dst.y);
                dev.trait(src.x+mx, dst.y, dst.x, dst.y);
            }

        }
	} else {
		// draw right left cables
		for (int i=0; i<N; i++) {
			point src = fSchema1->outputPoint(i);
			point dst = fSchema2->inputPoint(i);

            int d = direction(src,dst);
            if (d != dir) {
                // compute attributes of new direction
                switch (d) {
                    case kUpDir 	: mx = -fHorzGap; dx = dWire; break;
                    case kDownDir	: mx = 0; dx = -dWire; break;
                    default			: mx = 0; dx = 0; break;
                }
                dir = d;
            } else {
                // move in same direction
                mx = mx +dx;
            }
            if (src.y == dst.y) {
                // draw straight cable
                dev.trait(src.x, src.y, dst.x, dst.y);
            } else {
                // draw zizag cable
                dev.trait(src.x, src.y, src.x+mx, src.y);
                dev.trait(src.x+mx, src.y, src.x+mx, dst.y);
                dev.trait(src.x+mx, dst.y, dst.x, dst.y);
            }

        }
	}
}



/**
 * Draw the internal wires aligning the vertical segments in
 * a symetric way when possible.
 */

void seqSchema::collectInternalWires(collector& c)
{
    assert (fSchema1->outputs() == fSchema2->inputs());

    const int 	N 	= fSchema1->outputs();
    double 		dx 	= 0;
    double		mx 	= 0;
    int			dir	=-1;

    if (orientation() == kLeftRight) {
        // draw left right cables
        for (int i=0; i<N; i++) {
            point src = fSchema1->outputPoint(i);
            point dst = fSchema2->inputPoint(i);

            int d = direction(src,dst);
            if (d != dir) {
                // compute attributes of new direction
                switch (d) {
                    case kUpDir 	: mx = 0; dx = dWire; break;
                    case kDownDir	: mx = fHorzGap; dx = -dWire; break;
                    default			: mx = 0; dx = 0; break;
                }
                dir = d;
            } else {
                // move in same direction
                mx = mx +dx;
            }
            if (src.y == dst.y) {
                // draw straight cable
                c.addTrait(trait(point(src.x, src.y), point(dst.x, dst.y)));
            } else {
                // draw zizag cable
                c.addTrait(trait(point(src.x, src.y), point(src.x+mx, src.y)));
                c.addTrait(trait(point(src.x+mx, src.y), point(src.x+mx, dst.y)));
                c.addTrait(trait(point(src.x+mx, dst.y), point(dst.x, dst.y)));
            }

        }
    } else {
        // draw right left cables
        for (int i=0; i<N; i++) {
            point src = fSchema1->outputPoint(i);
            point dst = fSchema2->inputPoint(i);

            int d = direction(src,dst);
            if (d != dir) {
                // compute attributes of new direction
                switch (d) {
                    case kUpDir 	: mx = -fHorzGap; dx = dWire; break;
                    case kDownDir	: mx = 0; dx = -dWire; break;
                    default			: mx = 0; dx = 0; break;
                }
                dir = d;
            } else {
                // move in same direction
                mx = mx +dx;
            }
            if (src.y == dst.y) {
                // draw straight cable
                c.addTrait(trait(point(src.x, src.y), point(dst.x, dst.y)));
            } else {
                // draw zizag cable
                c.addTrait(trait(point(src.x, src.y), point(src.x+mx, src.y)));
                c.addTrait(trait(point(src.x+mx, src.y), point(src.x+mx, dst.y)));
                c.addTrait(trait(point(src.x+mx, dst.y), point(dst.x, dst.y)));
            }

        }
    }
}

//--------------------------helpers------------------------------



/**
 * Compute the direction of a connection. Note that
 * Y axis goes from top to bottom
 */
static int direction(const point& a, const point& b)
{
	if (a.y > b.y) return kUpDir; 		// upward connections
	if (a.y < b.y) return kDownDir; 	// downward connection
	return kHorDir;						// horizontal connections
}

/**
 * Compute the horizontal gap needed to draw the internal wires.
 * It depends on the largest group of connections that go in the same
 * direction.
 */
static double computeHorzGap(schema* a, schema* b)
{
	assert(a->outputs() == b->inputs());

	if (a->outputs() == 0) {
		return 0;
	} else {
		// store here the size of the largest group for each direction
		int	MaxGroupSize[3]; for(int i=0; i<3; i++) MaxGroupSize[i]=0;

		// place a and b to have valid connection points
		double ya = max(0.0, 0.5*(b->height() - a->height()));
		double yb = max(0.0, 0.5*(a->height() - b->height()));
		a->place(0,ya,kLeftRight);
		b->place(0,yb,kLeftRight);

		// init current group direction and size
		int	gdir 	= direction(a->outputPoint(0), b->inputPoint(0));
		int	gsize 	= 1;

		// analyze direction of remaining points
		for (unsigned int i=1; i<a->outputs(); i++) {
			int d = direction(a->outputPoint(i), b->inputPoint(i));
			if (d == gdir) {
				gsize++;
			} else {
				if (gsize > MaxGroupSize[gdir])  MaxGroupSize[gdir]=gsize;
				gsize = 1;
				gdir = d;
			}
		}

		// update for last group
		if (gsize > MaxGroupSize[gdir])  MaxGroupSize[gdir]=gsize;

		// the gap required for the connections
		return dWire * max(MaxGroupSize[kUpDir],MaxGroupSize[kDownDir]);
	}
}