summaryrefslogtreecommitdiff
path: root/graphics/epix/path.cc
blob: c78648567097866332618a9201d7015974e0937d (plain)
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
/* 
 * path.cc -- ePiX user class for polygons and paths
 *
 * This file is part of ePiX, a C++ library for creating high-quality 
 * figures in LaTeX 
 *
 * Version 1.2.0-2
 * Last Change: September 25, 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 <vector>
#include <cmath>

#include "errors.h"
#include "triples.h"
#include "constants.h"

#include "functions.h"

#include "Color.h"
#include "pen_data.h"
#include "state.h"

#include "camera.h"
#include "active_screen.h"

#include "spline_data.h"
#include "path_data.h"

#include "path.h"

namespace ePiX {

  class screen;
  class Sphere;

  path::path(const path& C)
    : m_segments(C.m_segments->clone()) { }

  path& path::operator= (const path& C)
  {
    if (this != &C)
      {
	path_data*   tmp_segs(C.m_segments->clone());

	delete m_segments;

	m_segments=tmp_segs;
      }

    return *this;
  }

  path::~path()
  {
    delete m_segments;
  }


  path::path()
    : m_segments(new path_data()) { }

  path::path(const std::vector<P>& data, bool closed, bool filled)
    : m_segments(new path_data(data, closed, filled)) { }


  // path constructors *do not* close/fill; this must be done by client code

  // lines, without and with specified number of points
  path::path(const P& tail, const P& head, double expand)
    : m_segments(new path_data())
  {
    double c(expm1(M_LN2*expand/100.0)); // 2^{expand/100} - 1
    P dir(head - tail);

    m_segments->pt(tail - (0.5*c)*dir);
    m_segments->pt(head + (0.5*c)*dir);
  } // end of line constructor


  path::path(const P& tail, const P& head, 
	     double expand, unsigned int num_pts)
    : m_segments(new path_data())
  {
    if (num_pts < 1)
      num_pts=1;

    double c(expm1(M_LN2*expand/100.0)); // 2^{expand/100} - 1

    // direction and starting location
    P dir(head - tail);
    P start(tail - (0.5*c)*dir);

    dir *= ((1+c)/num_pts);

    for (unsigned int i=0; i <= num_pts; ++i)
      m_segments->pt(start+i*dir);

  } // end of line constructor


  // ellipse drawn with N edges
  path::path(const P& center, const P& axis1, const P& axis2, 
	     double t_min, double t_max,
	     unsigned int N)
    : m_segments(new path_data())
  {
    double T_MAX(t_max);
    if (full_turn() < fabs(t_max-t_min))
      {
	epix_warning("Angle bounds in ellipse span more than one turn");
	T_MAX = t_min + full_turn();
      }

    const unsigned int num_pts(1 < N ? N : 1);
    const double dt((T_MAX - t_min)/num_pts);

    for (unsigned int i=0; i <= num_pts; ++i)
      {
	double t(t_min + i*dt);
	m_segments->pt(center + ((Cos(t)*axis1)+(Sin(t)*axis2)));
      }
  } // end of ellipse constructor

  // ellipse with default number of points
  path::path(const P& center, const P& axis1, const P& axis2, 
	     double t_min, double t_max)
    : m_segments(new path_data())
  {
    double frac(min(fabs(t_max-t_min)/full_turn(), 1));
    unsigned int num_pts((unsigned int) max(2, ceil(frac*EPIX_NUM_PTS)));

    const double dt((t_max - t_min)/num_pts);

    for (unsigned int i=0; i <= num_pts; ++i)
      {
	double t(t_min + i*dt);
	m_segments->pt(center + ((Cos(t)*axis1)+(Sin(t)*axis2)));
      }
  } // end of ellipse EPIX_NUM_PTS constructor


  // Splines
  path::path(const P& p1, const P& p2, const P& p3, unsigned int num_pts)
    : m_segments(new path_data())
  {
    if (num_pts < 2)
      num_pts=2; // draw at least V

    const double dt(1.0/num_pts);

    for (unsigned int i=0; i <= num_pts; ++i)
      m_segments->pt(spl_pt(p1, p2, p3, i*dt));
  }


  path::path(const P& p1, const P& p2, const P& p3)
    : m_segments(new path_data())
  {
    const double dt(1.0/EPIX_NUM_PTS);

    for (unsigned int i=0; i <= EPIX_NUM_PTS; ++i)
      m_segments->pt(spl_pt(p1, p2, p3, i*dt));
  }


  path::path(const P& p1, const P& p2, const P& p3, const P& p4,
	     unsigned int num_pts)
    : m_segments(new path_data())
  {
    if (num_pts < 3)
      num_pts=3; // draw at least N

    const double dt(1.0/num_pts);

    for (unsigned int i=0; i <= num_pts; ++i)
      m_segments->pt(spl_pt(p1, p2, p3, p4, i*dt));
  }


  path::path(const P& p1, const P& p2, const P& p3, const P& p4)
    : m_segments(new path_data())
  {
    const double dt(1.0/EPIX_NUM_PTS);

    for (unsigned int i=0; i <= EPIX_NUM_PTS; ++i)
      m_segments->pt(spl_pt(p1, p2, p3, p4, i*dt));
  }


  // parametrized path
  path::path(P f(double), double t_min, double t_max, unsigned int num_pts)
    : m_segments(new path_data())
  {
    if (num_pts < 2)
      num_pts = 2;

    const double dt((t_max - t_min)/num_pts);

    for (unsigned int i=0; i <= num_pts; ++i)
      m_segments->pt(f(t_min + i*dt));
  }

  // with default number of points
  path::path(P f(double), double t_min, double t_max)
    : m_segments(new path_data())
  {
    const double dt((t_max - t_min)/EPIX_NUM_PTS);

    for (unsigned int i=0; i <= EPIX_NUM_PTS; ++i)
      m_segments->pt(f(t_min + i*dt));
  }


  // graphs
  path::path(double f(double), double t_min, double t_max,
	     unsigned int num_pts)
    : m_segments(new path_data())
  {
    if (num_pts < 2)
      num_pts = 2;

    const double dt((t_max - t_min)/num_pts);

    for (unsigned int i=0; i <= num_pts; ++i)
      {
	double t(t_min + i*dt);
	m_segments->pt(P(t, f(t)));
      }
  }

  path::path(double f(double), double t_min, double t_max)
    : m_segments(new path_data())
  {
    const double dt((t_max - t_min)/EPIX_NUM_PTS);

    for (unsigned int i=0; i <= EPIX_NUM_PTS; ++i)
      {
	double t(t_min + i*dt);
	m_segments->pt(P(t, f(t)));
      }
  }


  // append a point
  path& path::pt(double x, double y, double z)
  {
    m_segments->pt(P(x, y, z));
    return *this;
  }

  path& path::pt(const P& loc)
  {
    m_segments->pt(loc);
    return *this;
  }


  // concatenate
  path& path::operator+= (const path& data)
  {
    *m_segments += *(data.m_segments);

    return *this;
  }

  path& path::operator-= (const path& data)
  {
    *m_segments -= *(data.m_segments);

    return *this;
  }

  path& path::close()
  {
    m_segments->close();
    return *this;
  }
  path& path::fill(const bool arg)
  {
    m_segments->fill(arg);
    return *this;
  }


  void path::clip()
  {
    m_segments->clip();
  }

  void path::clip_to(const Sphere& S, const P& viewpt, bool back)
  {
    m_segments->clip_to(S, viewpt, back);
  }

  void path::draw() const
  {
    m_segments->draw();
  }

  void path::draw(const Color& col, const pen_data& pen) const
  {
    m_segments->photo(*active_screen(), cam(), col, pen, Xfine());
  }
} // end of namespace