summaryrefslogtreecommitdiff
path: root/graphics/epix/domain.cc
blob: cbb86a4d42095c9741f12394ee74f1faa7bc68c1 (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
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
/* 
 * domain.cc -- ePiX domain slicing operations
 *
 * This file is part of ePiX, a C++ library for creating high-quality 
 * figures in LaTeX 
 *
 * Version 1.1.10
 * Last Change: August09, 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 <iostream>

#include "constants.h"
#include "triples.h"
#include "functions.h"

#include "domain.h"

namespace ePiX {

  typedef std::list<domain>::const_iterator dolci;
  const double EPS(EPIX_EPSILON);

  mesh::mesh(int n1, int n2, int n3)
    : n_1((int)max(1, fabs(n1))),
      n_2((int)max(1, fabs(n2))),
      n_3((int)max(1, fabs(n3))) { }

  mesh::mesh(int n)
    : n_1((int)max(1, fabs(n))),
      n_2((int)max(1, fabs(n))),
      n_3((int)max(1, fabs(n))) { }

  mesh::mesh() : n_1(1), n_2(1), n_3(1) { }


  domain::domain(const P& arg1, const P& arg2, const mesh& c, const mesh& f)
    : m_corner1(arg1), m_corner2(arg2)
  {
    int c1(c.n1()), c2(c.n2()), c3(c.n3());
    int f1(f.n1()), f2(f.n2()), f3(f.n3());

    if (m_corner1.x1() == m_corner2.x1()) { c1 = f1 = 1; }
    if (m_corner1.x2() == m_corner2.x2()) { c2 = f2 = 1; }
    if (m_corner1.x3() == m_corner2.x3()) { c3 = f3 = 1; }

    m_coarse = mesh(c1,c2,c3);
    m_fine   = mesh(f1,f2,f3);
  }

  domain::domain(const P& arg1, const P& arg2, const mesh& c)
    : m_corner1(arg1), m_corner2(arg2)
  {
    int c1(c.n1()), c2(c.n2()), c3(c.n3());

    if (m_corner1.x1() == m_corner2.x1()) { c1 = 1; }
    if (m_corner1.x2() == m_corner2.x2()) { c2 = 1; }
    if (m_corner1.x3() == m_corner2.x3()) { c3 = 1; }

    m_coarse = mesh(c1,c2,c3);
    m_fine   = mesh(c1,c2,c3);
  }

  // 1-dim'l domain
  domain::domain(double t_min, double t_max, int num_pts)
    : m_corner1(t_min), m_corner2(t_max), m_coarse(num_pts), m_fine(num_pts)
  { }


  int domain::dim() const
  {
    int D(0);
    if (fabs(m_corner2.x1() - m_corner1.x1()) > EPIX_EPSILON)
      ++D;
    if (fabs(m_corner2.x2() - m_corner1.x2()) > EPIX_EPSILON)
      ++D;
    if (fabs(m_corner2.x3() - m_corner1.x3()) > EPIX_EPSILON)
      ++D;

    return D;
  }

  double domain::step1() const
  {
    return (m_corner2.x1() - m_corner1.x1())/m_coarse.n1();
  }

  double domain::step2() const
  {
    return (m_corner2.x2() - m_corner1.x2())/m_coarse.n2();
  }

  double domain::step3() const
  {
    return (m_corner2.x3() - m_corner1.x3())/m_coarse.n3();
  }

  double domain::dx1() const
  {
    return (m_corner2.x1() - m_corner1.x1())/m_fine.n1();
  }

  double domain::dx2() const
  {
    return (m_corner2.x2() - m_corner1.x2())/m_fine.n2();
  }

  double domain::dx3() const
  {
    return (m_corner2.x3() - m_corner1.x3())/m_fine.n3();
  }


  // resizing attempts to preserve real resolution
  domain domain::resize1(double a1, double b1) const
  {
    P new_corner1(a1, m_corner1.x2(), m_corner1.x3());
    P new_corner2(b1, m_corner2.x2(), m_corner2.x3());

    // hack to avoid spurious round-down; should never cause round-up
    double ratio((b1-a1)/(m_corner2.x1()-m_corner1.x1()) + EPS);
    mesh new_c((int)floor(ratio*m_coarse.n1()), m_coarse.n2(),  m_coarse.n3());
    mesh new_f((int)floor(ratio*m_fine.n1()),   m_fine.n2(),    m_fine.n3());

    if (step1() == 0)
      {
	new_c = mesh(1, m_coarse.n2(),  m_coarse.n3());
	new_f = mesh(1,   m_fine.n2(),    m_fine.n3());
      }

    return domain(new_corner1, new_corner2, new_c, new_f);
  }

  domain domain::resize2(double a2, double b2) const
  {
    P new_corner1(m_corner1.x1(),  a2, m_corner1.x3());
    P new_corner2(m_corner2.x1(),  b2, m_corner2.x3());

    double ratio((b2-a2)/(m_corner2.x2()-m_corner1.x2()) + EPS);
    mesh new_c(m_coarse.n1(), (int)floor(ratio*m_coarse.n2()),  m_coarse.n3());
    mesh new_f(  m_fine.n1(), (int)floor(ratio*m_fine.n2()),    m_fine.n3());

    if (step2() == 0)
      {
	new_c = mesh(m_coarse.n1(), 1,  m_coarse.n3());
	new_f = mesh(  m_fine.n1(), 1,    m_fine.n3());
      }

    return domain(new_corner1, new_corner2, new_c, new_f);
  }

  domain domain::resize3(double a3, double b3) const
  {
    P new_corner1(m_corner1.x1(),  m_corner1.x2(), a3);
    P new_corner2(m_corner2.x1(),  m_corner2.x2(), b3);

    double ratio((b3-a3)/(m_corner2.x3()-m_corner1.x3()) + EPS);
    mesh new_c(m_coarse.n1(), m_coarse.n2(), (int)floor(ratio*m_coarse.n3()));
    mesh new_f(  m_fine.n1(),   m_fine.n2(), (int)floor(ratio*m_fine.n3()));

    if (step3() == 0)
      {
	new_c = mesh(m_coarse.n1(), m_coarse.n2(),  1);
	new_f = mesh(  m_fine.n1(),   m_fine.n2(),  1);
      }

    return domain(new_corner1, new_corner2, new_c, new_f);
  }


  // "snip_to" is defined in functions.h and performs the "obvious"
  // truncation: snip_to(x, a, b) = x, min(a,b), or max(a,b)

  // one slice
  domain domain::slice1(double a1) const
  {
    a1 = snip_to(a1, m_corner1.x1(), m_corner2.x1());

    return domain(P(a1,  m_corner1.x2(), m_corner1.x3()),
		  P(a1,  m_corner2.x2(), m_corner2.x3()),
		  mesh(1, m_coarse.n2(),  m_coarse.n3()),
		  mesh(1,   m_fine.n2(),    m_fine.n3()));
  }

  domain domain::slice2(double a2) const
  {
    a2 = snip_to(a2, m_corner1.x2(), m_corner2.x2());

    return domain(P(m_corner1.x1(),  a2, m_corner1.x3()),
		  P(m_corner2.x1(),  a2, m_corner2.x3()),
		  mesh(m_coarse.n1(), 1,  m_coarse.n3()),
		  mesh(  m_fine.n1(), 1,    m_fine.n3()));
  }

  domain domain::slice3(double a3) const
  {
    a3 = snip_to(a3, m_corner1.x3(), m_corner2.x3());

    return domain(P(m_corner1.x1(),  m_corner1.x2(), a3),
		  P(m_corner2.x1(),  m_corner2.x2(), a3),
		  mesh(m_coarse.n1(), m_coarse.n2(),  1),
		  mesh(  m_fine.n1(),   m_fine.n2(),  1));
  }


  // coordinate slices
  std::list<domain> domain::slices1(const unsigned int n) const
  {
    unsigned int N(m_coarse.n1());
    double du(step1());

    if (n > 0)
      {
	du *= N*1.0/n;
	N = n;
      }

    std::list<domain> val;

    for (unsigned int i=0; i <= N; ++i)
      val.push_back(slice1(m_corner1.x1() + i*du));

    return val;
  }

  std::list<domain> domain::slices2(const unsigned int n) const
  {
    unsigned int N(m_coarse.n2());
    double du(step2());

    if (n > 0)
      {
	du *= N*1.0/n;
	N = n;
      }

    std::list<domain> val;

    for (unsigned int i=0; i <= N; ++i)
      val.push_back(slice2(m_corner1.x2() + i*du));

    return val;
  }

  std::list<domain> domain::slices3(const unsigned int n) const
  {
    unsigned int N(m_coarse.n3());
    double du(step3());

    if (n > 0)
      {
	du *= N*1.0/n;
	N = n;
      }

    std::list<domain> val;

    for (unsigned int i=0; i <= N; ++i)
      val.push_back(slice3(m_corner1.x3() + i*du));

    return val;
  }

  P domain::corner1() const
  {
    return m_corner1;
  }
  P domain::corner2() const
  {
    return m_corner2;
  }

  double domain::corner1_x1() const
  {
    return m_corner1.x1();
  }
  double domain::corner1_x2() const
  {
    return m_corner1.x2();
  }
  double domain::corner1_x3() const
  {
    return m_corner1.x3();
  }

  double domain::corner2_x1() const
  {
    return m_corner2.x1();
  }
  double domain::corner2_x2() const
  {
    return m_corner2.x2();
  }
  double domain::corner2_x3() const
  {
    return m_corner2.x3();
  }


  int domain::coarse_n1() const
  {
    return m_coarse.n1();
  }
  int domain::coarse_n2() const
  {
    return m_coarse.n2();
  }
  int domain::coarse_n3() const
  {
    return m_coarse.n3();
  }

  int domain::fine_n1() const
  {
    return m_fine.n1();
  }
  int domain::fine_n2() const
  {
    return m_fine.n2();
  }
  int domain::fine_n3() const
  {
    return m_fine.n3();
  }


  domain_list::domain_list(std::list<domain> arg)
    : m_list(arg) { }

  domain_list& domain_list::add(const domain& arg)
  {
    m_list.push_back(arg);
    return *this;
  }

  domain_list& domain_list::add(const domain_list& arg)
  {
    // Less efficient than m_list.splice(m_list.end(), arg), but preserves arg
    for (dolci p=arg.m_list.begin(); p != arg.m_list.end(); ++p)
      m_list.push_back(*p);

    return *this;
  }
} // end of namespace