File: eepic.cc

package info (click to toggle)
epix 1.2.14-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,188 kB
  • ctags: 2,633
  • sloc: cpp: 16,444; sh: 5,074; makefile: 156
file content (297 lines) | stat: -rw-r--r-- 7,428 bytes parent folder | download | duplicates (4)
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
/*
 * eepic.cc -- ePiX's eepic output format
 *
 * This file is part of ePiX, a C++ library for creating high-quality 
 * figures in LaTeX 
 *
 * Version 1.2.0-2
 * Last Change: September 26, 2007
 */

/* 
 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
 * Andrew D. Hwang <rot 13 nujnat at zngupf dot ubylpebff dot rqh>
 * Department of Mathematics and Computer Science
 * College of the Holy Cross
 * Worcester, MA, 01610-2395, USA
 */
 
/*
 * ePiX is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * ePiX is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ePiX; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <cmath>
#include <list>

#include <string>
#include <sstream>

#include <set> // for palette

#include "constants.h"

#include "functions.h"

#include "pairs.h"
#include "edge_data.h"

#include "Color.h"

#include "path_style.h"
#include "pen_data.h"

#include "hatching.h"

#include "format.h"
#include "eepic.h"

namespace ePiX {

  // rotate successive hatches this much to avoid parallelity
  const double d_hatch_theta(111.24612); // ~90*(sqrt(5)-1)

  eepic::eepic()
    : m_ink(Black()), m_nib(PLAIN_WIDTH), m_hatch(0) { }

  eepic* eepic::clone() const
  {
    return new eepic(*this);
  }

  // Filled region with specified Cartesian edges, offset, and color
  std::string eepic::print_fill(const std::list<edge2d>& edges,
				const pair& offset,
				const Color& fc,
				const pen_data& line,
				const std::string& len) const
  {
    std::stringstream obuf;

    // set angle
    const double hatch_angle(m_hatch);
    m_hatch += d_hatch_theta;

    // draw hatch lines separated by pen.width(), actual line width
    // scaled by density
    double dens(fc.alpha()); // transparency
    if (dens < EPIX_EPSILON)
      return "";

    // else
    length hatch_width(length(PLAIN_WIDTH).to(len));

    path_state style; // solid

    if (EPIX_EPSILON < dens*hatch_width.magnitude())
      {
	// Use fill color, draw thin lines to simulate transparency.
	// Area covered by double-hatching is (supposed to be) dens.
	pen_data fill_pen(fc, (1-sqrt(1-dens))*hatch_width);

	// draw hatch-filled edges
	hatch_data bd1(hatch_angle, hatch_width.magnitude(), edges);
	for (unsigned int i=0; i < bd1.data().size(); ++i)
	  obuf << format::print_line(bd1.data().at(i), offset,
				     fill_pen, style, std::string(""), len);

	// re-draw hatch lines at right angles...
	hatch_data bd2(hatch_angle+90, hatch_width.magnitude(), edges);
	for (unsigned int i=0; i < bd2.data().size(); ++i)
	  obuf << format::print_line(bd2.data().at(i), offset,
				     fill_pen, style, "", len);
      }

    obuf << set_pen_state(line);
    obuf << print_paths(edges, offset, "", len);

    return obuf.str();
  } // end of eepic::print_fill()


  // may assume line is visible
  std::string eepic::print_line(const std::list<edge2d>& edges,
				const pair& offset,
				const pen_data& line,
				const pen_data& base,
				const path_state& style,
				const std::string& len) const
  {
    std::string value;

    // draw *solid* base first if necessary
    if (line.width() < base.width() && !base.color().is_unset())
      value += format::print_line(edges, offset, base, path_state(), "", len);

    return value += format::print_line(edges, offset, line, style, "", len);
  }


  std::string eepic::print_color(const std::string& model,
				 const std::string& name,
				 double d1, double d2, double d3) const
  {
    return format::xdefinecolor(model, name, d1, d2, d3);
  }

  std::string eepic::print_color(const std::string& model,
				 const std::string& name,
				 double d1, double d2,
				 double d3, double d4) const
  {
    return format::xdefinecolor(model, name, d1, d2, d3, d4);
  }

  // one-line comment
  std::string eepic::print_comment(const std::string& msg) const
  {
    std::stringstream obuf;
    obuf << "%% " << msg << std::endl;

    return obuf.str();
  }

  // verbatim string, newline protected
  std::string eepic::print_verbatim(const std::string& msg) const
  {
    std::stringstream obuf;
    obuf << msg << "%" << std::endl;

    return obuf.str();
  }

  void eepic::reset_state() const
  {
    m_ink = Neutral();
    m_nib = length(0);
  }


  //// private member functions ////
  std::string eepic::start_picture(const pair& sz, const pair& offset) const
  {
    std::stringstream obuf;
    obuf << "\\begin{picture}"
	 << format::print(sz) << format::print(-offset) << "%" << std::endl;

    return obuf.str();
  }

  std::string eepic::end_picture() const
  {
    std::stringstream obuf;
    obuf << "\\end{picture}%" << std::endl;
    return obuf.str();
  }

  std::string eepic::set_unitlength(const std::string& len) const
  {
    std::stringstream obuf;

    obuf << "\\setlength{\\unitlength}{1" << len << "}%" << std::endl;

    return obuf.str();
  }


  std::string eepic::usepackages() const
  {
    return "usepackages epic,eepic,xcolor";
  }

  // string argument for passing attributes local to this path/loop
  std::string eepic::start_open_path(const std::string& attribs) const
  {
    return "\\path";
  }

  std::string eepic::end_open_path(const std::string& attribs) const
  {
    std::stringstream obuf;
    obuf << std::endl;
    return obuf.str();
  }

  std::string eepic::start_closed_path(const std::string& attribs) const
  {
    return "\\path";
  }

  std::string eepic::end_closed_path(const std::string& attribs) const
  {
    std::stringstream obuf;
    obuf << std::endl;
    return obuf.str();
  }

  std::string eepic::set_fill_state(const Color& col) const // unused
  {
    std::stringstream obuf;

    if (m_ink != col)
      {
	m_ink = col;
	obuf << "\\color{" << m_ink.name() << "}%" << std::endl;
      }

    return obuf.str();
  }

  std::string eepic::set_pen_state(const pen_data& pen) const
  {
    std::stringstream obuf;

    if (m_nib != pen.width())
      {
	m_nib = pen.width();
	obuf << "\\allinethickness{" << m_nib.name() << "}%" << std::endl;
      }

    if (m_ink != pen.color())
      {
	m_ink = pen.color();
	obuf << "\\color{" << m_ink.name() << "}%" << std::endl;
      }

    return obuf.str();
  }

  // place a LaTeX box of width zero (containing string) at location (pair)
  std::string eepic::put_box(const pair& loc, const std::string& msg) const
  {
    std::stringstream obuf;
    obuf << "\\put" << print(loc) << "{" << msg << "}" << std::endl;

    return obuf.str();
  }

  std::string eepic::print_circle_marker(const pair& here, double diam,
					 bool fill, const Color& color,
					 const std::string& len) const
  {
    std::stringstream obuf;
    obuf << "\\put" << print(here)
	 << "{\\color{" << print(color) << "}$";

    if (fill)
      obuf << "\\allinethickness{" << 0.5*diam << len << "}"
	   << "\\circle{" << 0.5*diam << "}";

    else
      obuf << "\\circle{" << diam << "}";

    obuf << "$}%" << std::endl;

    return obuf.str();
  }
} // end of namespace