1 /************************************************************************
2  ************************************************************************
3     FAUST compiler
4     Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
5     ---------------------------------------------------------------------
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  ************************************************************************
20  ************************************************************************/
21 
22 #include <algorithm>
23 #include <iostream>
24 
25 #include "exception.hh"
26 #include "splitSchema.h"
27 
28 using namespace std;
29 
30 /**
31  * Creates a new split schema. Cables are enlarged to dWire.
32  * The horizontal gap between the two subschema is such that
33  * the connections are not too sloppy.
34  */
makeSplitSchema(schema * s1,schema * s2)35 schema* makeSplitSchema(schema* s1, schema* s2)
36 {
37     // make sure a and b are at least dWire large
38     schema* a = makeEnlargedSchema(s1, dWire);
39     schema* b = makeEnlargedSchema(s2, dWire);
40 
41     // horizontal gap to avoid too sloppy connections
42     double hgap = (a->height() + b->height()) / 4;
43 
44     return new splitSchema(a, b, hgap);
45 }
46 
47 /**
48  * Constructor for a split schema s1 <: s2 where the outputs
49  * of s1 are distributed to the inputs of s2. The constructor is
50  * private in order to enforce the usage of makeSplitSchema
51  */
splitSchema(schema * s1,schema * s2,double hgap)52 splitSchema::splitSchema(schema* s1, schema* s2, double hgap)
53     : schema(s1->inputs(), s2->outputs(), s1->width() + s2->width() + hgap, max(s1->height(), s2->height())),
54       fSchema1(s1),
55       fSchema2(s2),
56       fHorzGap(hgap)
57 {
58 }
59 
60 /**
61  * Places the two subschema horizontaly, centered, with enough gap for
62  * the connections
63  */
place(double ox,double oy,int orientation)64 void splitSchema::place(double ox, double oy, int orientation)
65 {
66     beginPlace(ox, oy, orientation);
67 
68     double dy1 = max(0.0, fSchema2->height() - fSchema1->height()) / 2.0;
69     double dy2 = max(0.0, fSchema1->height() - fSchema2->height()) / 2.0;
70 
71     if (orientation == kLeftRight) {
72         fSchema1->place(ox, oy + dy1, orientation);
73         fSchema2->place(ox + fSchema1->width() + fHorzGap, oy + dy2, orientation);
74     } else {
75         fSchema2->place(ox, oy + dy2, orientation);
76         fSchema1->place(ox + fSchema2->width() + fHorzGap, oy + dy1, orientation);
77     }
78     endPlace();
79 }
80 
81 /**
82  * The inputs of s1 <: s2 are the inputs of s1
83  */
inputPoint(unsigned int i) const84 point splitSchema::inputPoint(unsigned int i) const
85 {
86     return fSchema1->inputPoint(i);
87 }
88 
89 /**
90  * The outputs of s1 <: s2 are the outputs of s2
91  */
outputPoint(unsigned int i) const92 point splitSchema::outputPoint(unsigned int i) const
93 {
94     return fSchema2->outputPoint(i);
95 }
96 
97 /**
98  * Draw the two sub schema and the connections between them
99  */
draw(device & dev)100 void splitSchema::draw(device& dev)
101 {
102     faustassert(placed());
103 
104     // draw the two subdiagrams
105     fSchema1->draw(dev);
106     fSchema2->draw(dev);
107 
108     unsigned int r = fSchema1->outputs();
109     faustassert(r > 0);
110 #if 0
111     // draw the connections between them
112     for (unsigned int i=0; i<fSchema2->inputs(); i++) {
113         point p = fSchema1->outputPoint(i%r);
114         point q = fSchema2->inputPoint(i);
115         if(p.z>0) {
116             dev.trait(p.x, p.y, q.x, q.y);
117         }
118     }
119 #endif
120 }
121 
122 /**
123  * Draw the two sub schema and the connections between them
124  */
collectTraits(collector & c)125 void splitSchema::collectTraits(collector& c)
126 {
127     faustassert(placed());
128 
129     // draw the two subdiagrams
130     fSchema1->collectTraits(c);
131     fSchema2->collectTraits(c);
132 
133     unsigned int r = fSchema1->outputs();
134     faustassert(r > 0);
135 
136     // draw the connections between them
137     for (unsigned int i = 0; i < fSchema2->inputs(); i++) {
138         point p = fSchema1->outputPoint(i % r);
139         point q = fSchema2->inputPoint(i);
140         c.addTrait(trait(point(p.x, p.y), point(q.x, q.y)));
141     }
142 }
143