1 /************************************************************************
2  ************************************************************************
3     FAUST compiler
4     Copyright (C) 2003-2004 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 #ifndef __SCHEMA__
23 #define __SCHEMA__
24 
25 #include "device.h"
26 #include "garbageable.hh"
27 
28 #include <set>
29 #include <string>
30 #include <vector>
31 
32 using namespace std;
33 
34 const double dWire = 8;  ///< distance between two wires
35 // const double dLetter = 6;			///< width of a letter
36 const double dLetter = 4.3;  ///< width of a letter
37 const double dHorz   = 4;    ///< marge horizontale
38 const double dVert   = 4;    ///< marge verticale
39 
40 struct point : public virtual Garbageable {
41     double x;
42     double y;
43 
pointpoint44     point() : x(0.0), y(0.0) {}
pointpoint45     point(double u, double v) : x(u), y(v) {}
46 
47     bool operator<(const point& p) const
48     {
49         if (x < p.x)
50             return true;
51         else if (x > p.x)
52             return false;
53         else if (y < p.y)
54             return true;
55         else
56             return false;
57     }
58 };
59 
60 struct trait : public virtual Garbageable {
61     point start;
62     point end;
63     bool  hasRealInput;
64     bool  hasRealOutput;
65 
traittrait66     trait(const point& p1, const point& p2) : start(p1), end(p2), hasRealInput(false), hasRealOutput(false) {}
drawtrait67     void draw(device& dev) const { dev.trait(start.x, start.y, end.x, end.y); }
68 
69     bool operator<(const trait& t) const
70     {
71         if (start < t.start)
72             return true;
73         else if (t.start < start)
74             return false;
75         else if (end < t.end)
76             return true;
77         else
78             return false;
79     }
80 };
81 
82 struct collector : public virtual Garbageable {
83     set<point> fOutputs;     // collect real outputs
84     set<point> fInputs;      // collect real inputs
85     set<trait> fTraits;      // collect traits to draw
86     set<trait> fWithInput;   // collect traits with a real input
87     set<trait> fWithOutput;  // collect traits with a real output
88 
addOutputcollector89     void addOutput(const point& p) { fOutputs.insert(p); }
addInputcollector90     void addInput(const point& p) { fInputs.insert(p); }
addTraitcollector91     void addTrait(const trait& t) { fTraits.insert(t); }
92     void computeVisibleTraits();
93     bool isVisible(const trait& t);
94     void draw(device& dev);
95 };
96 
97 enum { kLeftRight = 1, kRightLeft = -1 };
98 
99 /**
100  * An abstract block diagram schema
101  */
102 
103 class schema : public virtual Garbageable {
104    private:
105     const unsigned int fInputs;
106     const unsigned int fOutputs;
107     const double       fWidth;
108     const double       fHeight;
109 
110     // fields only defined after place() is called
111     bool   fPlaced;  ///< false until place() is called
112     double fX;
113     double fY;
114     int    fOrientation;
115 
116    public:
schema(unsigned int inputs,unsigned int outputs,double width,double height)117     schema(unsigned int inputs, unsigned int outputs, double width, double height)
118         : fInputs(inputs),
119           fOutputs(outputs),
120           fWidth(width),
121           fHeight(height),
122           fPlaced(false),
123           fX(0),
124           fY(0),
125           fOrientation(0)
126     {
127     }
~schema()128     virtual ~schema() {}
129 
130     // constant fields
width()131     double       width() const { return fWidth; }
height()132     double       height() const { return fHeight; }
inputs()133     unsigned int inputs() const { return fInputs; }
outputs()134     unsigned int outputs() const { return fOutputs; }
135 
136     // starts and end placement
beginPlace(double x,double y,int orientation)137     void beginPlace(double x, double y, int orientation)
138     {
139         fX           = x;
140         fY           = y;
141         fOrientation = orientation;
142     }
endPlace()143     void endPlace() { fPlaced = true; }
144 
145     // fields available after placement
placed()146     bool   placed() const { return fPlaced; }
x()147     double x() const { return fX; }
y()148     double y() const { return fY; }
orientation()149     int    orientation() const { return fOrientation; }
150 
151     // abstract interface for subclasses
152     virtual void  place(double x, double y, int orientation) = 0;
153     virtual void  draw(device& dev)                          = 0;
154     virtual point inputPoint(unsigned int i) const           = 0;
155     virtual point outputPoint(unsigned int i) const          = 0;
156     virtual void  collectTraits(collector& c)                = 0;
157 };
158 
159 // various functions to create schemas
160 
161 schema* makeBlockSchema(unsigned int inputs, unsigned int outputs, const string& name, const string& color,
162                         const string& link);
163 schema* makeCableSchema(unsigned int n = 1);
164 schema* makeInverterSchema(const string& color);
165 schema* makeCutSchema();
166 schema* makeEnlargedSchema(schema* s, double width);
167 schema* makeParSchema(schema* s1, schema* s2);
168 schema* makeSeqSchema(schema* s1, schema* s2);
169 schema* makeMergeSchema(schema* s1, schema* s2);
170 schema* makeSplitSchema(schema* s1, schema* s2);
171 schema* makeRecSchema(schema* s1, schema* s2);
172 schema* makeTopSchema(schema* s1, double margin, const string& text, const string& link);
173 schema* makeDecorateSchema(schema* s1, double margin, const string& text);
174 schema* makeConnectorSchema();
175 schema* makeRouteSchema(unsigned int inputs, unsigned int outputs, const vector<int>& routes);
176 
177 #endif
178