FAUST compiler  0.9.9.6b8
schema.h
Go to the documentation of this file.
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 
26 #include "device.h"
27 #include <vector>
28 #include <string>
29 #include <set>
30 
31 using namespace std;
32 
33 const double dWire = 8;
34 //const double dLetter = 6; ///< width of a letter
35 const double dLetter = 4.3;
36 const double dHorz = 4;
37 const double dVert = 4;
38 
39 
40 struct point
41 {
42  double x;
43  double y;
44 
45  point(double u, double v) : x(u), y(v) {}
46  point(const point& p) : x(p.x), y(p.y) {}
47 
48  bool operator<(const point& p) const {
49  if (x < p.x) return true;
50  else if (x > p.x) return false;
51  else if (y < p.y) return true;
52  else return false;
53  }
54 };
55 
56 struct trait
57 {
62 
63  trait(const point& p1, const point& p2) : start(p1), end(p2) {}
64  void draw(device& dev) const { dev.trait(start.x, start.y, end.x, end.y); }
65 
66  bool operator<(const trait& t) const {
67  if (start < t.start) return true;
68  else if (t.start < start) return false;
69  else if (end < t.end) return true;
70  else return false;
71  }
72 };
73 
74 struct collector
75 {
76  set<point> fOutputs; // collect real outputs
77  set<point> fInputs; // collect real inputs
78  set<trait> fTraits; // collect traits to draw
79  set<trait> fWithInput; // collect traits with a real input
80  set<trait> fWithOutput; // collect traits with a real output
81 
82  void addOutput(const point& p) { fOutputs.insert(p); }
83  void addInput(const point& p) { fInputs.insert(p); }
84  void addTrait(const trait& t) { fTraits.insert(t); }
85  void computeVisibleTraits();
86  bool isVisible(const trait& t);
87  void draw(device& dev);
88 };
89 
90 enum { kLeftRight=1, kRightLeft=-1 };
91 
92 
93 
97 class schema
98 {
99  private:
100  const unsigned int fInputs;
101  const unsigned int fOutputs;
102  const double fWidth;
103  const double fHeight;
104 
105  // fields only defined after place() is called
106  bool fPlaced;
107  double fX;
108  double fY;
110 
111  public:
112 
113  schema(unsigned int inputs, unsigned int outputs, double width, double height)
114  : fInputs(inputs),
115  fOutputs(outputs),
116  fWidth(width),
117  fHeight(height),
118  fPlaced(false),
119  fX(0),
120  fY(0),
121  fOrientation(0)
122  {}
123  virtual ~schema() {}
124 
125  // constant fields
126  double width() const { return fWidth; }
127  double height() const { return fHeight; }
128  unsigned int inputs() const { return fInputs; }
129  unsigned int outputs() const { return fOutputs; }
130 
131  // starts and end placement
132  void beginPlace (double x, double y, int orientation)
133  { fX = x; fY = y; fOrientation = orientation; }
134  void endPlace () { fPlaced = true; }
135 
136  // fields available after placement
137  bool placed() const { return fPlaced; }
138  double x() const { return fX; }
139  double y() const { return fY; }
140  int orientation() const { return fOrientation; }
141 
142 
143  // abstract interface for subclasses
144  virtual void place(double x, double y, int orientation) = 0;
145  virtual void draw(device& dev) = 0;
146  virtual point inputPoint(unsigned int i) const = 0;
147  virtual point outputPoint(unsigned int i)const = 0;
148  virtual void collectTraits(collector& c) = 0;
149 };
150 
151 // various functions to create schemas
152 
153 schema* makeBlockSchema (unsigned int inputs,
154  unsigned int outputs,
155  const string& name,
156  const string& color,
157  const string& link);
158 schema* makeCableSchema (unsigned int n=1);
159 schema* makeInverterSchema (const string& color);
161 schema* makeEnlargedSchema (schema* s, double width);
162 schema* makeParSchema (schema* s1, schema* s2);
163 schema* makeSeqSchema (schema* s1, schema* s2);
164 schema* makeMergeSchema (schema* s1, schema* s2);
165 schema* makeSplitSchema (schema* s1, schema* s2);
166 schema* makeRecSchema (schema* s1, schema* s2);
167 schema* makeTopSchema (schema* s1, double margin, const string& text, const string& link);
168 schema* makeDecorateSchema (schema* s1, double margin, const string& text);
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 #endif
180 
181 
void addOutput(const point &p)
Definition: schema.h:82
void addTrait(const trait &t)
Definition: schema.h:84
void endPlace()
Definition: schema.h:134
schema * makeInverterSchema(const string &color)
Build n cables in parallel.
Definition: schema.h:56
unsigned int outputs() const
Definition: schema.h:129
schema * makeConnectorSchema()
Connectors are used to ensure unused inputs and outputs are drawn.
const double dWire
distance between two wires
Definition: schema.h:33
schema * makeEnlargedSchema(schema *s, double width)
Returns an enlarged schema, but only if really needed that is if the requiered width is greater that ...
set< trait > fWithOutput
Definition: schema.h:80
const unsigned int fOutputs
Definition: schema.h:101
void beginPlace(double x, double y, int orientation)
Definition: schema.h:132
int fOrientation
Definition: schema.h:109
void draw(device &dev) const
Definition: schema.h:64
bool fPlaced
false until place() is called
Definition: schema.h:106
bool hasRealOutput
Definition: schema.h:61
schema * makeCableSchema(unsigned int n=1)
Build n cables in parallel.
Definition: cableSchema.cpp:32
const double dVert
marge verticale
Definition: schema.h:37
unsigned int inputs() const
Definition: schema.h:128
bool operator<(const trait &t) const
Definition: schema.h:66
schema * makeMergeSchema(schema *s1, schema *s2)
Creates a new merge schema.
Definition: mergeSchema.cpp:35
point(double u, double v)
Definition: schema.h:45
double y() const
Definition: schema.h:139
double width() const
Definition: schema.h:126
double y
Definition: schema.h:43
const unsigned int fInputs
Definition: schema.h:100
trait(const point &p1, const point &p2)
Definition: schema.h:63
bool placed() const
Definition: schema.h:137
schema * makeSplitSchema(schema *s1, schema *s2)
Creates a new split schema.
Definition: splitSchema.cpp:34
schema * makeParSchema(schema *s1, schema *s2)
Definition: parSchema.cpp:28
Definition: device.h:32
schema * makeBlockSchema(unsigned int inputs, unsigned int outputs, const string &name, const string &color, const string &link)
Build a simple colored blockSchema with a certain number of inputs and outputs, a text to be displaye...
Definition: blockSchema.cpp:41
schema * makeTopSchema(schema *s1, double margin, const string &text, const string &link)
Creates a new top schema.
Definition: topSchema.cpp:33
const double fHeight
Definition: schema.h:103
double x() const
Definition: schema.h:138
int orientation() const
Definition: schema.h:140
double fX
Definition: schema.h:107
bool hasRealInput
Definition: schema.h:60
set< point > fOutputs
Definition: schema.h:76
virtual ~schema()
Definition: schema.h:123
schema(unsigned int inputs, unsigned int outputs, double width, double height)
Definition: schema.h:113
double x
Definition: schema.h:42
Definition: schema.h:40
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98
point(const point &p)
Definition: schema.h:46
An abstract block diagram schema.
Definition: schema.h:97
const double fWidth
Definition: schema.h:102
double fY
Definition: schema.h:108
set< trait > fWithInput
Definition: schema.h:79
double height() const
Definition: schema.h:127
set< trait > fTraits
Definition: schema.h:78
point end
Definition: schema.h:59
point start
Definition: schema.h:58
const double dHorz
marge horizontale
Definition: schema.h:36
schema * makeRecSchema(schema *s1, schema *s2)
Creates a new recursive schema (s1 ~ s2).
Definition: recSchema.cpp:34
void addInput(const point &p)
Definition: schema.h:83
const double dLetter
width of a letter
Definition: schema.h:35
schema * makeSeqSchema(schema *s1, schema *s2)
Make a sequential schema.
Definition: seqSchema.cpp:43
schema * makeDecorateSchema(schema *s1, double margin, const string &text)
Creates a new decorated schema.
bool operator<(const point &p) const
Definition: schema.h:48
set< point > fInputs
Definition: schema.h:77
virtual void trait(double x1, double y1, double x2, double y2)=0
schema * makeCutSchema()
Creates a new Cut schema.
Definition: cutSchema.cpp:33