FAUST compiler  0.9.9.6b8
recSchema.cpp
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 
23 #include "recSchema.h"
24 #include <iostream>
25 #include <assert.h>
26 
27 using namespace std;
28 
35 {
36  schema* a = makeEnlargedSchema(s1, s2->width());
37  schema* b = makeEnlargedSchema(s2, s1->width());
38  double m = dWire * max(b->inputs(), b->outputs());
39  double w = a->width() + 2*m;
40 
41  return new recSchema(a,b,w);
42 }
43 
48 recSchema::recSchema (schema* s1, schema* s2, double width)
49  : schema( s1->inputs() - s2->outputs(),
50  s1->outputs(),
51  width,
52  s1->height() + s2->height() ),
53  fSchema1(s1),
54  fSchema2(s2)
55 {
56  // this version only accepts legal expressions of same width
57  assert(s1->inputs() >= s2->outputs());
58  assert(s1->outputs() >= s2->inputs());
59  assert(s1->width() >= s2->width());
60 
61  // create the input and output points
62  for (unsigned int i=0; i<inputs(); i++) fInputPoint.push_back(point(0,0));
63  for (unsigned int i=0; i<outputs(); i++) fOutputPoint.push_back(point(0,0));
64 
65 }
66 
71 void recSchema::place(double ox, double oy, int orientation)
72 {
73  beginPlace(ox, oy, orientation);
74 
75  double dx1 = (width() - fSchema1->width())/2;
76  double dx2 = (width() - fSchema2->width())/2;
77 
78  // place the two sub diagrams
79  if (orientation == kLeftRight) {
80  fSchema2->place(ox+dx2, oy, kRightLeft);
81  fSchema1->place(ox+dx1, oy+fSchema2->height(), kLeftRight);
82  } else {
83  fSchema1->place(ox+dx1, oy, kRightLeft);
84  fSchema2->place(ox+dx2, oy+fSchema1->height(), kLeftRight);
85  }
86 
87 
88  // adjust delta space to orientation
89  if (orientation == kRightLeft) { dx1 = -dx1; }
90 
91  // place input points
92  for (unsigned int i=0; i<inputs(); i++) {
94  fInputPoint[i] = point(p.x-dx1, p.y);
95  }
96 
97  // place output points
98  for (unsigned int i=0; i<outputs(); i++) {
99  point p = fSchema1->outputPoint(i);
100  fOutputPoint[i] = point(p.x+dx1, p.y);
101  }
102 
103  endPlace();
104 }
105 
106 
110 point recSchema::inputPoint(unsigned int i) const
111 {
112  return fInputPoint[i];
113 }
114 
115 
119 point recSchema::outputPoint(unsigned int i) const
120 {
121  return fOutputPoint[i];
122 }
123 
124 
130 {
131  assert(placed());
132 
133  // draw the two subdiagrams
134  fSchema1->draw(dev);
135  fSchema2->draw(dev);
136 
137  // draw the implicit feedback delay to each fSchema2 input
138  double dw = (orientation()==kLeftRight) ? dWire : -dWire;
139  for (unsigned int i=0; i<fSchema2->inputs(); i++) {
140  const point& p = fSchema1->outputPoint(i);
141  drawDelaySign(dev, p.x + i*dw, p.y, dw/2);
142 
143  }
144 }
145 
149 void recSchema::drawDelaySign(device& dev, double x, double y, double size)
150 {
151  dev.trait(x-size/2, y, x-size/2, y-size);
152  dev.trait(x-size/2, y-size, x+size/2, y-size);
153  dev.trait(x+size/2, y-size, x+size/2, y);
154 }
155 
156 
163 {
164  assert(placed());
165 
166  // draw the two subdiagrams
169 
170  // draw the feedback connections to each fSchema2 input
171  for (unsigned int i=0; i<fSchema2->inputs(); i++) {
173  }
174 
175  // draw the non recursive output lines
176  for (unsigned int i=fSchema2->inputs(); i<outputs(); i++) {
177  point p = fSchema1->outputPoint(i);
178  point q = outputPoint(i);
179  c.addTrait(trait(p,q)); // in->out order
180  }
181 
182  // draw the input lines
183  unsigned int skip = fSchema2->outputs();
184  for (unsigned int i=0; i<inputs(); i++) {
185  point p = inputPoint(i);
186  point q = fSchema1->inputPoint(i+skip);
187  c.addTrait(trait(p,q)); // in->out order
188  }
189 
190  // draw the feedfront connections from each fSchema2 output
191  for (unsigned int i=0; i<fSchema2->outputs(); i++) {
193  }
194 }
195 
196 
197 
202 void recSchema::collectFeedback(collector& c, const point& src, const point& dst, double dx, const point& out)
203 {
204  double ox = src.x + ((orientation()==kLeftRight) ? dx : -dx);
205  double ct = (orientation()==kLeftRight) ? dWire/2 : -dWire/2;
206 
207  point up(ox, src.y-ct);
208  point br(ox+ct/2.0, src.y);
209 
210  c.addOutput(up);
211  c.addOutput(br);
212  c.addInput(br);
213 
214  c.addTrait(trait(up, point(ox, dst.y)));
215  c.addTrait(trait(point(ox, dst.y), point(dst.x, dst.y)));
216  c.addTrait(trait(src,br));
217  c.addTrait(trait(br,out));
218 
219 }
220 
221 
226 void recSchema::collectFeedfront(collector& c, const point& src, const point& dst, double dx)
227 {
228  double ox = src.x + ((orientation()==kLeftRight) ? -dx : dx);
229 
230  c.addTrait(trait(point(src.x, src.y), point(ox, src.y)));
231  c.addTrait(trait(point(ox, src.y), point(ox, dst.y)));
232  c.addTrait(trait(point(ox, dst.y), point(dst.x, dst.y)));
233 }
void drawDelaySign(device &dev, double x, double y, double size)
Draw the delay sign of a feedback connection.
Definition: recSchema.cpp:149
void addOutput(const point &p)
Definition: schema.h:82
void addTrait(const trait &t)
Definition: schema.h:84
void endPlace()
Definition: schema.h:134
Definition: schema.h:56
unsigned int outputs() const
Definition: schema.h:129
const double dWire
distance between two wires
Definition: schema.h:33
virtual void collectTraits(collector &c)
Draw the two subschema s1 and s2 as well as the feedback connections between s1 and s2...
Definition: recSchema.cpp:162
virtual point inputPoint(unsigned int i) const =0
void beginPlace(double x, double y, int orientation)
Definition: schema.h:132
virtual void draw(device &dev)=0
virtual void collectTraits(collector &c)=0
void collectFeedback(collector &c, const point &src, const point &dst, double dx, const point &out)
Draw a feedback connection between two points with an horizontal displacement dx. ...
Definition: recSchema.cpp:202
unsigned int inputs() const
Definition: schema.h:128
double max(double x, double y)
Definition: interval.hh:60
void collectFeedfront(collector &c, const point &src, const point &dst, double dx)
Draw a feedfrom connection between two points with an horizontal displacement dx. ...
Definition: recSchema.cpp:226
vector< point > fInputPoint
Definition: recSchema.h:37
schema * makeRecSchema(schema *s1, schema *s2)
Creates a new recursive schema (s1 ~ s2).
Definition: recSchema.cpp:34
double width() const
Definition: schema.h:126
double y
Definition: schema.h:43
bool placed() const
Definition: schema.h:137
Definition: device.h:32
virtual point inputPoint(unsigned int i) const
The input points s1 ~ s2.
Definition: recSchema.cpp:110
int orientation() const
Definition: schema.h:140
virtual void place(double x, double y, int orientation)=0
double x
Definition: schema.h:42
Definition: schema.h:40
virtual point outputPoint(unsigned int i) const =0
An abstract block diagram schema.
Definition: schema.h:97
double height() const
Definition: schema.h:127
schema * fSchema1
Definition: recSchema.h:35
schema * fSchema2
Definition: recSchema.h:36
place and connect two diagrams in recursive composition
Definition: recSchema.h:33
vector< point > fOutputPoint
Definition: recSchema.h:38
void addInput(const point &p)
Definition: schema.h:83
virtual void place(double ox, double oy, int orientation)
The two subschema are placed centered vertically, s2 on top of s1.
Definition: recSchema.cpp:71
virtual point outputPoint(unsigned int i) const
The output points s1 ~ s2.
Definition: recSchema.cpp:119
virtual void trait(double x1, double y1, double x2, double y2)=0
recSchema(schema *s1, schema *s2, double width)
Constructor of a recursive schema (s1 ~ s2).
Definition: recSchema.cpp:48
virtual void draw(device &dev)
Draw the two subschema s1 and s2 as well as the implicit feedback delays between s1 and s2...
Definition: recSchema.cpp:129
schema * makeEnlargedSchema(schema *s, double width)
Returns an enlarged schema, but only if really needed that is if the requiered width is greater that ...