File: facet.cc

package info (click to toggle)
epix 1.2.10-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,160 kB
  • sloc: cpp: 16,442; sh: 5,058; makefile: 198
file content (505 lines) | stat: -rw-r--r-- 13,823 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
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
/*
 * facet.cc -- Utility classes for shaded surface plotting
 *
 * This file is part of ePiX, a C++ library for creating high-quality
 * figures in LaTeX
 *
 * Version 1.2.2
 * Last Change: December 06, 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 "constants.h"
#include "errors.h"
#include "functions.h"

#include "frame.h"
#include "camera.h"

#include "Color.h"
#include "paint_style.h"

#include "facet.h"

namespace ePiX {

  facet::facet(P f(double, double),
	       double u0, double v0,
	       double du, double dv,
	       const unsigned int N1, const unsigned int N2)
    : m_tint(the_paint_style().fill_color()),
      m_line(the_paint_style().line_pen()),
      m_fill(the_paint_style().fill_flag()),
      pt1(f(u0,         v0)),
      pt2(f(u0 + N1*du, v0)),
      pt3(f(u0 + N1*du, v0 + N2*dv)),
      pt4(f(u0,         v0 + N2*dv)),
      center(0.25*(pt1 + pt2 + pt3 + pt4)),
      direction(center-cam().viewpt()),
      distance(norm(direction))
  {
    perp = ((pt1 - center)*(pt2 - center));

    if (norm(perp) < EPIX_EPSILON)
      perp = (pt3 - center)*(pt4 - center);

    perp *= recip(norm(perp));

    // bottom edge
    for (unsigned int i=0; i<N1; ++i)
      bd.pt(f(u0 + i*du, v0));

    // right edge
    for (unsigned int j=0; j<N2; ++j)
      bd.pt(f(u0 + N1*du, v0 + j*dv));

    // top edge (backward)
    for (unsigned int i=0; i<N1; ++i)
      bd.pt(f(u0 + (N1-i)*du, v0 + N2*dv));

    // left edge (downward)
    for (unsigned int j=0; j<N2; ++j)
      bd.pt(f(u0, v0 + (N2-j)*dv));

    bd.close().fill(!m_tint.is_unset());
  }


  // facet constructor for f(double, double, double)
  facet::facet(P f(double, double, double),
	       double u0, double v0, double w0,
	       double du, double dv, double dw,
	       const unsigned int N1, const unsigned int N2)
    : m_tint(the_paint_style().fill_color()),
      m_line(the_paint_style().line_pen()),
      m_fill(the_paint_style().fill_flag()),
      pt1(f(u0, v0, w0))
  {
    if (du == 0) // (y,z)
      {
	// bottom edge
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0, v0 + i*dv, w0));

	// right edge
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0, v0 + N1*dv, w0 + j*dw));

	// top edge (backward)
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0, v0 + (N1-i)*dv, w0 + N2*dw));

	// left edge (downward)
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0, v0, w0 + (N2-j)*dw));

	// use corners to approximate distance to camera
	pt2 = f(u0, v0 + N1*dv, w0);
	pt3 = f(u0, v0 + N1*dv, w0 + N2*dw);
	pt4 = f(u0, v0,         w0 + N2*dw);
      }

    else if (dv == 0) // (x,z)
      {
	// bottom edge
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0 + i*du, v0, w0));

	// right edge
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0 + N1*du, v0, w0 + j*dw));

	// top edge (backward)
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0 + (N1-i)*du, v0, w0 + N2*dw));

	// left edge (downward)
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0, v0, w0 + (N2-j)*dw));

	// use corners to approximate distance to camera
	pt2 = f(u0 + N1*du, v0, w0);
	pt3 = f(u0 + N1*du, v0, w0 + N2*dw);
	pt4 = f(u0,         v0, w0 + N2*dw);
      }

    else if (dw == 0) // (x,y)
      {
	// bottom edge
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0 + i*du, v0, w0));

	// right edge
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0 + N1*du, v0 + j*dv, w0));

	// top edge (backward)
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0 + (N1-i)*du, v0 + N2*dv, w0));

	// left edge (downward)
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0, v0 + (N2-j)*dv, w0));

	// use corners to approximate distance to camera
	pt2 = f(u0 + N1*du, v0,         w0);
	pt3 = f(u0 + N1*du, v0 + N2*dv, w0);
	pt4 = f(u0,         v0 + N2*dv, w0);
      }

    else
      epix_error("Bad call to facet constructor"); // exits

    bd.close().fill(!m_tint.is_unset());

    center = 0.25*(pt1 + pt2 + pt3 + pt4);

    direction = center-cam().viewpt();
    distance = norm(direction);

    perp = ((pt1 - center)*(pt2 - center));

    if (norm(perp) < EPIX_EPSILON)
      perp = (pt3 - center)*(pt4 - center);

    perp *= recip(norm(perp));

  } // end of facet(f(double, double, double), ...)


  // for surface of rotation
  facet::facet(double f(double), double g(double),
	       double u0, double v0,
	       double du, double dv,
	       const unsigned int N1, const unsigned int N2,
	       const frame& coords)
    : m_tint(the_paint_style().fill_color()),
      m_line(the_paint_style().line_pen()),
      m_fill(the_paint_style().fill_flag()),
      pt1(f(u0)*coords.sea() +
	  g(u0)*Cos(v0)*coords.sky() +
	  g(u0)*Sin(v0)*coords.eye()),
      pt2(f(u0 + N1*du)*coords.sea() +
	  g(u0 + N1*du)*Cos(v0)*coords.sky() +
	  g(u0 + N1*du)*Sin(v0)*coords.eye()),
      pt3(f(u0 + N1*du)*coords.sea() +
	  g(u0 + N1*du)*Cos(v0 + N2*dv)*coords.sky() +
	  g(u0 + N1*du)*Sin(v0 + N2*dv)*coords.eye()),
      pt4(f(u0)*coords.sea() +
	  g(u0)*Cos(v0 + N2*dv)*coords.sky() +
	  g(u0)*Sin(v0 + N2*dv)*coords.eye()),
      center(0.25*(pt1 + pt2 + pt3 + pt4)),
      direction(center-cam().viewpt()), distance(norm(direction))
  {
    perp = ((pt1 - center)*(pt2 - center));

    if (norm(perp) < EPIX_EPSILON)
      perp = (pt3 - center)*(pt4 - center);

    perp *= recip(norm(perp));

    // bottom edge
    for (unsigned int i=0; i<N1; ++i)
      bd.pt(f(u0 + i*du)*coords.sea() +
	    g(u0 + i*du)*Cos(v0)*coords.sky() +
	    g(u0 + i*du)*Sin(v0)*coords.eye());

    // right edge
    for (unsigned int j=0; j<N2; ++j)
      bd.pt(f(u0 + N1*du)*coords.sea() +
	    g(u0 + N1*du)*Cos(v0 + j*dv)*coords.sky() +
	    g(u0 + N1*du)*Sin(v0 + j*dv)*coords.eye());

    // top edge (backward)
    for (unsigned int i=0; i<N1; ++i)
      bd.pt(f(u0 + (N1-i)*du)*coords.sea() +
	    g(u0 + (N1-i)*du)*Cos(v0 + N2*dv)*coords.sky() +
	    g(u0 + (N1-i)*du)*Sin(v0 + N2*dv)*coords.eye());

    // left edge (downward)
    for (unsigned int j=0; j<N2; ++j)
      bd.pt(f(u0)*coords.sea() +
	    g(u0)*Cos(v0 + (N2-j)*dv)*coords.sky() +
	    g(u0)*Sin(v0 + (N2-j)*dv)*coords.eye());

    bd.close().fill(!m_tint.is_unset());
  }

  //// Color-dependent constructors
  facet::facet(P f(double, double),
	       double u0, double v0,
	       double du, double dv,
	       const unsigned int N1, const unsigned int N2,
	       const Color& tint)
    : m_tint(tint), m_line(the_paint_style().line_pen()),
      m_fill(the_paint_style().fill_flag()),
      pt1(f(u0,         v0)),
      pt2(f(u0 + N1*du, v0)),
      pt3(f(u0 + N1*du, v0 + N2*dv)),
      pt4(f(u0,         v0 + N2*dv)),
      center(0.25*(pt1 + pt2 + pt3 + pt4)),
      direction(center-cam().viewpt()),
      distance(norm(direction))
  {
    perp = ((pt1 - center)*(pt2 - center));

    if (norm(perp) < EPIX_EPSILON)
      perp = (pt3 - center)*(pt4 - center);

    perp *= recip(norm(perp));

    // bottom edge
    for (unsigned int i=0; i<N1; ++i)
      bd.pt(f(u0 + i*du, v0));

    // right edge
    for (unsigned int j=0; j<N2; ++j)
      bd.pt(f(u0 + N1*du, v0 + j*dv));

    // top edge (backward)
    for (unsigned int i=0; i<N1; ++i)
      bd.pt(f(u0 + (N1-i)*du, v0 + N2*dv));

    // left edge (downward)
    for (unsigned int j=0; j<N2; ++j)
      bd.pt(f(u0, v0 + (N2-j)*dv));

    bd.close().fill(!m_tint.is_unset());
  }


  // facet constructor for f(double, double, double)
  facet::facet(P f(double, double, double),
	       double u0, double v0, double w0,
	       double du, double dv, double dw,
	       const unsigned int N1, const unsigned int N2,
	       const Color& tint)
    : m_tint(tint), m_line(the_paint_style().line_pen()),
      m_fill(the_paint_style().fill_flag()),
      pt1(f(u0, v0, w0))
  {
    if (du == 0) // (y,z)
      {
	// bottom edge
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0, v0 + i*dv, w0));

	// right edge
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0, v0 + N1*dv, w0 + j*dw));

	// top edge (backward)
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0, v0 + (N1-i)*dv, w0 + N2*dw));

	// left edge (downward)
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0, v0, w0 + (N2-j)*dw));

	// use corners to approximate distance to camera
	pt2 = f(u0, v0 + N1*dv, w0);
	pt3 = f(u0, v0 + N1*dv, w0 + N2*dw);
	pt4 = f(u0, v0,         w0 + N2*dw);
      }

    else if (dv == 0) // (x,z)
      {
	// bottom edge
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0 + i*du, v0, w0));

	// right edge
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0 + N1*du, v0, w0 + j*dw));

	// top edge (backward)
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0 + (N1-i)*du, v0, w0 + N2*dw));

	// left edge (downward)
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0, v0, w0 + (N2-j)*dw));

	// use corners to approximate distance to camera
	pt2 = f(u0 + N1*du, v0, w0);
	pt3 = f(u0 + N1*du, v0, w0 + N2*dw);
	pt4 = f(u0,         v0, w0 + N2*dw);
      }

    else if (dw == 0) // (x,y)
      {
	// bottom edge
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0 + i*du, v0, w0));

	// right edge
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0 + N1*du, v0 + j*dv, w0));

	// top edge (backward)
	for (unsigned int i=0; i<N1; ++i)
	  bd.pt(f(u0 + (N1-i)*du, v0 + N2*dv, w0));

	// left edge (downward)
	for (unsigned int j=0; j<N2; ++j)
	  bd.pt(f(u0, v0 + (N2-j)*dv, w0));

	// use corners to approximate distance to camera
	pt2 = f(u0 + N1*du, v0,         w0);
	pt3 = f(u0 + N1*du, v0 + N2*dv, w0);
	pt4 = f(u0,         v0 + N2*dv, w0);
      }

    else
      epix_error("Bad call to facet constructor"); // exits

    bd.close().fill(!m_tint.is_unset());

    center = 0.25*(pt1 + pt2 + pt3 + pt4);

    direction = center-cam().viewpt();
    distance = norm(direction);

    perp = ((pt1 - center)*(pt2 - center));

    if (norm(perp) < EPIX_EPSILON)
      perp = (pt3 - center)*(pt4 - center);

    perp *= recip(norm(perp));

  } // end of facet(f(double, double, double), ...)


  // for surface of rotation
  facet::facet(double f(double), double g(double),
	       double u0, double v0,
	       double du, double dv,
	       const unsigned int N1, const unsigned int N2,
	       const Color& tint, const frame& coords)
    : m_tint(tint), m_line(the_paint_style().line_pen()),
      m_fill(the_paint_style().fill_flag()),
      pt1(f(u0)*coords.sea() +
	  g(u0)*Cos(v0)*coords.sky() +
	  g(u0)*Sin(v0)*coords.eye()),
      pt2(f(u0 + N1*du)*coords.sea() +
	  g(u0 + N1*du)*Cos(v0)*coords.sky() +
	  g(u0 + N1*du)*Sin(v0)*coords.eye()),
      pt3(f(u0 + N1*du)*coords.sea() +
	  g(u0 + N1*du)*Cos(v0 + N2*dv)*coords.sky() +
	  g(u0 + N1*du)*Sin(v0 + N2*dv)*coords.eye()),
      pt4(f(u0)*coords.sea() +
	  g(u0)*Cos(v0 + N2*dv)*coords.sky() +
	  g(u0)*Sin(v0 + N2*dv)*coords.eye()),
      center(0.25*(pt1 + pt2 + pt3 + pt4)),
      direction(center-cam().viewpt()), distance(norm(direction))
  {
    perp = ((pt1 - center)*(pt2 - center));

    if (norm(perp) < EPIX_EPSILON)
      perp = (pt3 - center)*(pt4 - center);

    perp *= recip(norm(perp));

    // bottom edge
    for (unsigned int i=0; i<N1; ++i)
      bd.pt(f(u0 + i*du)*coords.sea() +
	    g(u0 + i*du)*Cos(v0)*coords.sky() +
	    g(u0 + i*du)*Sin(v0)*coords.eye());

    // right edge
    for (unsigned int j=0; j<N2; ++j)
      bd.pt(f(u0 + N1*du)*coords.sea() +
	    g(u0 + N1*du)*Cos(v0 + j*dv)*coords.sky() +
	    g(u0 + N1*du)*Sin(v0 + j*dv)*coords.eye());

    // top edge (backward)
    for (unsigned int i=0; i<N1; ++i)
      bd.pt(f(u0 + (N1-i)*du)*coords.sea() +
	    g(u0 + (N1-i)*du)*Cos(v0 + N2*dv)*coords.sky() +
	    g(u0 + (N1-i)*du)*Sin(v0 + N2*dv)*coords.eye());

    // left edge (downward)
    for (unsigned int j=0; j<N2; ++j)
      bd.pt(f(u0)*coords.sea() +
	    g(u0)*Cos(v0 + (N2-j)*dv)*coords.sky() +
	    g(u0)*Sin(v0 + (N2-j)*dv)*coords.eye());

    bd.close().fill(!m_tint.is_unset());
  }


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

  double facet::how_far() const { return distance; }

  bool facet::front_facing() const
  {
    return (-direction|perp) > -EPIX_EPSILON;
  }

  // N.B. We assume the fill state is stored by our caller
  void facet::draw(int cull) const
  {
    if (( cull ==  1 && front_facing() ) || ( cull == -1 && !front_facing() ))
      return;

    // else
    Color paint(m_tint);
    if (paint.is_unset())
      paint = White();

    Color ink(m_line.color());
    if (ink.is_unset())
      ink = paint;

    if (m_fill)
      {
	// calculate cosine^2 of normal angle
	// Magic formula (simulated ambient lighting)
	const double dens(0.5*(1+pow(perp|(recip(distance)*direction), 2)));

	paint *= dens;
	ink   *= dens;
      }

    bd.draw(paint, pen_data(ink, m_line.width()));
  } // end of facet::draw(bool, int)


  bool by_distance::operator() (const facet& arg1, const facet& arg2)
  {
    return arg1.how_far() > arg2.how_far();
  }

  bool by_distance::operator() (const facet* arg1, const facet* arg2)
  {
    return arg1->how_far() > arg2->how_far();
  }
} // end of namespace