-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutput.h
More file actions
657 lines (584 loc) · 17.8 KB
/
Output.h
File metadata and controls
657 lines (584 loc) · 17.8 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
* Output.h
* Morph
*
* Created by Christian Brunschen on 19/12/2010.
* Copyright 2010 Christian Brunschen. All rights reserved.
*
*/
#ifndef __Output_h__
#define __Output_h__
#include <iostream>
#include "Chain.h"
#include "ColorImage.h"
using namespace Primitives;
using namespace Chaining;
using namespace Images;
using namespace std;
template<typename Pen> class Output {
protected:
int width_;
int height_;
public:
Output() { }
virtual ~Output() { }
virtual void open() { }
virtual void beginPage(int width, int height) {
width_ = width;
height_ = height;
}
virtual void setPen(const Pen &pen) = 0;
virtual void outputChain(const Chain &chain) = 0;
virtual void outputChains(const Chains &chains) {
for (Chains::const_iterator i = chains.begin();
i != chains.end();
++i) {
outputChain(*i);
}
}
virtual void endPage() { }
virtual void close() { }
virtual ostream &describe(ostream &out) const { return out << "<abstract output>"; }
};
template <typename Pen>
ostream &operator<<(ostream &out, const Output<Pen> &output) {
output.describe(out);
return out;
}
template<typename Pen> class FileStreamOutput : public Output<Pen> {
typedef Output<Pen> Base;
protected:
const string filename_;
public:
ostream *out_;
FileStreamOutput(ostream &out) : Base(), filename_(""), out_(&out) { }
FileStreamOutput(const char *filename) : Base(), filename_(filename), out_(NULL) { }
FileStreamOutput(const string &filename) : Base(), filename_(filename), out_(NULL) { }
virtual ~FileStreamOutput() { if (out_) { close(); } }
virtual void open() {
if (out_ == NULL) {
if (filename_.length() > 0) {
if ("-" == filename_) {
out_ = &cout;
} else {
ofstream *fout;
out_ = fout = new ofstream;
fout->open(filename_.c_str());
}
}
}
}
virtual void endPage() {
*out_ << flush;
}
virtual void close() {
*out_ << flush;
if (filename_.length() > 0 && filename_ != "-") {
static_cast<ofstream *>(out_)->close();
delete out_;
out_ = NULL;
}
}
virtual ostream &describe(ostream &out) const {
return out << "<abstract output to '" << filename_ << "'>";
}
};
template<typename Pen> class PostScriptOutput : public FileStreamOutput<Pen> {
typedef FileStreamOutput<Pen> Base;
protected:
using Base::out_;
using Base::filename_;
public:
PostScriptOutput(ostream &out) : Base(out) { }
PostScriptOutput(const char *filename) : Base(filename) { }
PostScriptOutput(const string &filename) : Base(filename) { }
virtual ~PostScriptOutput() { }
virtual void beginPage(int width, int height) {
Base::beginPage(width, height);
*out_ << "%!PS-Adobe-3.0" << endl;
*out_ << "%%BoundingBox: -1 -1 " << width+1 << " " << height+1 << endl;
*out_ << "%%Page: only 1" << endl;
*out_ << "gsave initclip clippath pathbbox grestore" << endl;
*out_ << "/ury exch def /urx exch def /lly exch def /llx exch def" << endl;
*out_ << "/W urx llx sub def /H ury lly sub def" << endl;
*out_ << "/w " << width << " def /h " << height << " def" << endl;
*out_ << "/xs W w div def /ys H h div def" << endl;
*out_ << "/s xs ys lt { xs } { ys } ifelse def" << endl;
*out_ << "llx urx add 2 div lly ury add 2 div translate" << endl;
*out_ << "s dup scale w 2 div neg h 2 div neg translate" << endl;
*out_ << "1 setlinejoin 1 setlinecap" << endl;
}
virtual void setPen(const Pen &pen) {
double r = 1.0 - pen.cFraction();
double g = 1.0 - pen.mFraction();
double b = 1.0 - pen.yFraction();
double w = 2.0 * pen.r();
*out_ << r << " " << g << " " << b << " setrgbcolor ";
*out_ << w << " setlinewidth" << endl;
}
virtual void outputChain(const Chain &chain) {
Chain::const_iterator k = chain.begin();
*out_ << k->x() << " " << k->y() << " moveto ";
++k;
if (k == chain.end()) {
*out_ << "currentpoint lineto stroke" << endl;
} else {
for (; k != chain.end(); ++k) {
*out_ << k->x() << " " << k->y() << " lineto ";
}
*out_ << "stroke" << endl;
}
}
virtual void endPage() {
*out_ << "showpage" << endl;
Base::endPage();
}
virtual ostream &describe(ostream &out) const {
return out << "PostScript to '" << filename_ << "'";
}
};
template<typename Pen> class SvgOutput : public FileStreamOutput<Pen> {
typedef FileStreamOutput<Pen> Base;
protected:
using Base::out_;
using Base::filename_;
string color_;
double w_;
int height_;
bool startGroup_;
bool endGroup_;
public:
SvgOutput(ostream &out) : Base(out) { }
SvgOutput(const char *filename) : Base(filename) { }
SvgOutput(const string &filename) : Base(filename) { }
virtual ~SvgOutput() { }
virtual void open() {
Base::open();
*out_ << "<?xml version=\"1.0\" standalone=\"yes\"?>" << endl;
*out_ << "<svg xmlns=\"http://www.w3.org/2000/svg\">" << endl;
}
virtual void beginPage(int width, int height) {
Base::beginPage(width, height);
height_ = height;
*out_ << "<svg width=\"";
*out_ << width << "\" height=\"" << height << "\" viewBox=\"0 0 ";
*out_ << width << " " << height << "\">" << endl;
*out_ << "<g transform=\"translate(0, " << height_ << ") scale(1, -1)\">" << endl;
}
virtual void setPen(const Pen &pen) {
double r = 1.0 - pen.cFraction();
double g = 1.0 - pen.mFraction();
double b = 1.0 - pen.yFraction();
ostringstream cs;
cs << "rgb(";
cs << (100 * r) << "%,";
cs << (100 * g) << "%,";
cs << (100 * b) << "%)";
color_ = cs.str();
w_ = 2.0 * pen.r() + 1;
if (endGroup_) {
*out_ << "</g>" << endl;
endGroup_ = false;
}
startGroup_ = true;
}
virtual void outputChain(const Chain &chain) {
if (startGroup_) {
*out_ << "<g stroke-width=\"" << w_ << "\" stroke-linejoin=\"round\" stroke-linecap=\"round\" fill=\"none\" stroke=\"";
*out_ << color_ << "\">" << endl;
startGroup_ = false;
endGroup_ = true;
}
if (chain.size() == 1) {
const IPoint &p = chain.front();
*out_ << "<polyline points=\"" << p.x() << "," << p.y() << " " << p.x() << "," << p.y() << "\" />" << endl;
} else {
*out_ << "<polyline points=\"";
bool first = true;
for (Chain::const_iterator k = chain.begin(); k != chain.end(); ++k) {
if (first) {
first = false;
} else {
*out_ << " ";
}
*out_ << k->x() << "," << k->y();
}
*out_ << "\" />" << endl;
}
}
virtual void endPage() {
if (endGroup_) {
*out_ << "</g>" << endl;
}
*out_ << "</g>" << endl << "</svg>" << endl;
Base::endPage();
}
virtual void close() {
*out_ << "</svg>" << endl;
Base::close();
}
virtual ostream &describe(ostream &out) const {
return out << "SVG to '" << filename_ << "'";
}
};
template<int base> struct HpEncode {
int value_;
HpEncode(int value) : value_(value) { }
};
typedef HpEncode<32> Hp32Encode;
typedef HpEncode<64> Hp64Encode;
template<int base> ostream &operator<<(ostream &out, const HpEncode<base> &h) {
unsigned int v = h.value_ >= 0
? ((unsigned) h.value_) << 1
: ((unsigned)-h.value_) << 1 | 0x1;
while (v >= base) {
out << (char)(63 + v % base);
v = v / base;
}
out << (char)((base == 64 ? 191 : 95) + v);
return out;
}
template<typename Pen> class HPGLOutput : public FileStreamOutput<Pen> {
typedef FileStreamOutput<Pen> Base;
protected:
using Base::out_;
using Base::filename_;
using Output<Pen>::width_;
using Output<Pen>::height_;
bool betweenPages_;
public:
HPGLOutput(ostream &out) : Base(out), betweenPages_(true) { }
HPGLOutput(const char *filename) : Base(filename), betweenPages_(true) { }
HPGLOutput(const string &filename) : Base(filename), betweenPages_(true) { }
virtual void setPen(const Pen &pen) {
betweenPages_ = false;
*out_ << "SP" << pen.hpglIndex() << ";" << endl;
}
virtual void outputChain(const Chain &chain) {
betweenPages_ = false;
}
virtual void endPage() {
betweenPages_ = true;
*out_ << "PU;SP0;PG;" << endl;
}
virtual void close() {
Base::close();
}
virtual ostream &describe(ostream &out) const {
return out << "<abstract HPGL to '" << filename_ << "'>";
}
};
template<typename Pen> class HPGLAbsoluteOutput : public HPGLOutput<Pen> {
typedef HPGLOutput<Pen> Base;
using Base::out_;
protected:
using Base::filename_;
public:
HPGLAbsoluteOutput(ostream &out) : Base(out) { }
HPGLAbsoluteOutput(const char *filename) : Base(filename) { }
HPGLAbsoluteOutput(const string &filename) : Base(filename) { }
virtual void outputChain(const Chain &chain) {
Base::outputChain(chain);
Chain::const_iterator k = chain.begin();
IPoint p = *k;
*out_ << "PU " << p.x() << " " << p.y() << ";PD ";
bool first = true;
for (++k; k != chain.end(); ++k) {
if (first) { first = false; } else { *out_ << " "; }
p = *k;
*out_ << p.x() << " " << p.y();
}
if (first) {
k = chain.begin();
*out_ << p.x() << " " << p.y();
}
*out_ << ";" << endl;
}
virtual ostream &describe(ostream &out) const {
return out << "HPGL (absolute coordinates) to '" << filename_ << "'";
}
};
template<typename Pen> class HPGLRelativeOutput : public HPGLOutput<Pen> {
typedef HPGLOutput<Pen> Base;
using Base::out_;
protected:
using Base::filename_;
IPoint currentPoint_;
bool haveCurrentPoint_;
public:
HPGLRelativeOutput(ostream &out) : Base(out) { }
HPGLRelativeOutput(const char *filename) : Base(filename) { }
HPGLRelativeOutput(const string &filename) : Base(filename) { }
virtual void outputChain(const Chain &chain) {
Base::outputChain(chain);
Chain::const_iterator k = chain.begin();
IPoint p = *k;
if (haveCurrentPoint_) {
int dx = p.x() - currentPoint_.x();
int dy = p.y() - currentPoint_.y();
*out_ << "PU " << dx << " " << dy << ";PD ";
} else {
*out_ << "PA;PU " << p.x() << " " << p.y() << ";PR;PD ";
}
bool first = true;
for (++k; k != chain.end(); ++k) {
if (first) { first = false; } else { *out_ << " "; }
IPoint q = *k;
int dx = q.x() - p.x();
int dy = q.y() - p.y();
*out_ << dx << " " << dy;
p = q;
}
if (first) {
*out_ << "0 0";
}
*out_ << ";" << endl;
currentPoint_ = p;
haveCurrentPoint_ = true;
}
virtual void endPage() {
Base::endPage();
haveCurrentPoint_ = false;
}
virtual ostream &describe(ostream &out) const {
return out << "HPGL (relative coordinates) to '" << filename_ << "'";
}
};
template<typename Pen, int base> class HPGLEncodedOutput : public HPGLOutput<Pen> {
typedef HPGLOutput<Pen> Base;
using Base::out_;
typedef ::HpEncode<base> HpEncode;
protected:
using Base::filename_;
IPoint currentPoint_;
bool haveCurrentPoint_;
public:
HPGLEncodedOutput(ostream &out) : Base(out) { }
HPGLEncodedOutput(const char *filename) : Base(filename) { }
HPGLEncodedOutput(const string &filename) : Base(filename) { }
virtual void outputChain(const Chain &chain) {
Base::outputChain(chain);
Chain::const_iterator k = chain.begin();
// first, an absolute move to the beginning of the start of the chain
IPoint p = *k;
if (haveCurrentPoint_) {
int dx = p.x() - currentPoint_.x();
int dy = p.y() - currentPoint_.y();
*out_ << (base == 32 ? "PE7<" : "PE<") << HpEncode(dx) << HpEncode(dy);
} else {
*out_ << (base == 32 ? "PE7<=" : "PE<=") << HpEncode(p.x()) << HpEncode(p.y());
}
bool first = true;
for (++k; k != chain.end(); ++k) {
// relative moves for the rest of the chain
IPoint q = *k;
int dx = q.x() - p.x();
int dy = q.y() - p.y();
*out_ << HpEncode(dx) << HpEncode(dy);
p = q;
}
if (first) {
// only a single point - a pen-down move with a delta of 0 to mark this point
*out_ << HpEncode(0) << HpEncode(0);
}
*out_ << ";" << endl;
currentPoint_ = p;
haveCurrentPoint_ = true;
}
virtual void endPage() {
Base::endPage();
haveCurrentPoint_ = false;
}
virtual ostream &describe(ostream &out) const {
return out << "HPGL (using Polyline Encoded) to '" << filename_ << "'";
}
};
template<typename Tool> class RML1Output : public FileStreamOutput<Tool> {
typedef FileStreamOutput<Tool> Base;
using Base::out_;
using Base::filename_;
int x_;
int y_;
int z_;
int v_;
int vMoveXY_;
int vMoveZ_;
int vCutXY_;
int vCutZ_;
int zUp_;
int zDown_;
public:
void init() {
x_ = y_ = z_ = 0;
vMoveXY_ = vMoveZ_ = 15;
vCutXY_ = 5;
vCutZ_ = 1;
zUp_ = 40;
zDown_ = 0;
}
RML1Output(ostream &out) : Base(out) { init(); }
RML1Output(const char *filename) : Base(filename) { init(); }
RML1Output(const string &filename) : Base(filename) { init(); }
virtual void setPen(const Tool &tool) {
zUp_ = tool.zUp();
zDown_ = tool.zDown();
}
virtual void open() {
Base::open();
(*out_) << ";;^IN;!MC0;V15.0;^PR;Z0,0,2420;^PA;!MC1;\n";
x_ = 0;
y_ = 0;
z_ = 2420;
}
void setZUp(int zUp) {
zUp_ = zUp;
}
void setZDown(int zDown) {
zDown_ = zDown;
}
void setV(int v) {
if (v != v_) {
v_ = v;
(*out_) << "V" << v << ";\n";
}
}
void _moveTo(int x, int y, int z) {
if (x != x_ || y != y_ || z != z_) {
(*out_) << "Z" << x << "," << y << "," << z << ";\n";
}
x_ = x;
y_ = y;
z_ = z;
}
void moveTo(int x, int y, int z) {
// If we're trying to move below zero, let's move up to be safe.
if (z < 0) z = zUp_;
if (x != x_ || y != y_) {
// movement along the XY plane
if (z != z_) {
// movement in Z as well! Let's make a straight-up move first.
setV(vMoveZ_);
_moveTo(x_, y_, z);
}
setV(vMoveXY_);
_moveTo(x, y, z);
}
}
void cutTo(int x, int y, int z) {
if (x != x_ || y != y_) {
// cutting along the XY plane
if (z >= z_) {
// and movement level or up - consider this an XY move-or-cut for speed purposes
if (z_ <= 0) {
// at least partly at or below zero => cut.
setV(vCutXY_);
} else {
// above zero => move.
setV(vMoveXY_);
}
} else {
// movement down as well - always considered a z cut
setV(vCutZ_);
}
} else if (z < z_) {
// cut straight down
setV(vCutZ_);
} else if (z > z_) {
// move straight up
setV(vMoveZ_);
}
_moveTo(x, y, z);
}
virtual void outputChain(const Chain &chain) {
Chain::const_iterator k = chain.begin();
IPoint p = *k;
moveTo(p.x(), p.y(), zUp_);
cutTo(p.x(), p.y(), zDown_);
for (++k; k != chain.end(); ++k) {
p = *k;
cutTo(p.x(), p.y(), z_);
}
cutTo(p.x(), p.y(), zUp_);
}
virtual void close() {
(*out_) << "!VZ15.0;!ZM0;!MC0;^IN;\n";
Base::close();
}
virtual ostream &describe(ostream &out) const {
return out << "<RML1 to '" << filename_ << "'>";
}
};
template<typename Tool> class PostScript3DOutput : public PostScriptOutput<Tool> {
typedef PostScriptOutput<Tool> Base;
using Base::out_;
protected:
using Base::filename_;
public:
PostScript3DOutput(ostream &out) : Base(out) { }
PostScript3DOutput(const char *filename) : Base(filename) { }
PostScript3DOutput(const string &filename) : Base(filename) { }
virtual ~PostScript3DOutput() { }
virtual void setPen(const Tool &tool) {
double w = tool.r() * 2.0;
*out_ << 1 - (0.1 + 0.9 * tool.zLevel()) << " setgray ";
*out_ << w << " setlinewidth" << endl;
}
};
template<typename Pen> Output<Pen> *makeOutput(const string &outputType, const string &outputFile) {
if ("hp" == outputType || "ha" == outputType) {
return new HPGLAbsoluteOutput<Pen>(outputFile);
} else if ("hr" == outputType) {
return new HPGLRelativeOutput<Pen>(outputFile);
} else if ("he" == outputType) {
return new HPGLEncodedOutput<Pen, 64>(outputFile);
} else if ("he7" == outputType || "h7" == outputType) {
return new HPGLEncodedOutput<Pen, 32>(outputFile);
} else if ("ps" == outputType) {
return new PostScriptOutput<Pen>(outputFile);
} else if ("svg" == outputType) {
return new SvgOutput<Pen>(outputFile);
} else if (string::npos != outputType.find("hp")) {
if (string::npos != outputType.find("abs")) {
return new HPGLAbsoluteOutput<Pen>(outputFile);
} else if (string::npos != outputType.find("rel")) {
return new HPGLRelativeOutput<Pen>(outputFile);
} else if (string::npos != outputType.find("enc")) {
if (string::npos != outputType.find("7") || string::npos != outputType.find("32")) {
return new HPGLEncodedOutput<Pen, 32>(outputFile);
} else {
return new HPGLEncodedOutput<Pen, 64>(outputFile);
}
} else {
return new HPGLAbsoluteOutput<Pen>(outputFile);
}
} else {
return new HPGLAbsoluteOutput<Pen>(outputFile);
}
}
template<typename Tool> Output<Tool> *make3DOutput(const string &outputType, const string &outputFile) {
if (string::npos != outputType.find("rml")) {
return new RML1Output<Tool>(outputFile);
} else if (string::npos != outputType.find("ps") || string::npos != outputType.find("post")) {
return new PostScript3DOutput<Tool>(outputFile);
} else {
// fall back onto 2D output options
return makeOutput<Tool>(outputType, outputFile);
}
}
template<typename Pen, typename C> class ColorImageOutput : public Output<Pen> {
typedef Output<Pen> Base;
typedef Images::ColorImage<C> ColorImage;
ColorImage &image_;
Bitmap ¤tPenCoverage_;
public:
ColorImageOutput(ColorImage &image) : image_(image), currentPenCoverage_(image.width(), image.height()) { }
virtual void open() {
Base::open();
RGBPixel<C> whitePixel(1.0, 1.0, 1.0);
image_.set(whitePixel);
}
virtual void outputChain(const Chain &chain) {
// Chain::const_iterator k = chain.begin();
}
};
#endif // __Output_h__