forked from anstmichaels/emopt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
868 lines (696 loc) · 21.1 KB
/
Copy pathGrid.cpp
File metadata and controls
868 lines (696 loc) · 21.1 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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
#include "Grid.hpp"
#include <iostream>
#include <climits>
#include <ctime>
#include <exception>
#include <omp.h>
using namespace Grid;
/**************************************** Materials ****************************************/
//------------------------------ Grid Material ------------------------------------/
GridMaterial2D::GridMaterial2D(int M, int N, ArrayXXcd grid) : _M(M), _N(N), _grid(grid) {}
std::complex<double> GridMaterial2D::get_value(double x, double y)
{
int xx = int(x),
yy = int(y);
if(xx > 0 && xx < _M && yy > 0 && yy < _N)
return _grid(yy,xx);
else
return 1.0;
}
void GridMaterial2D::get_values(ArrayXcd& grid, int k1, int k2, int j1, int j2, double sx, double sy)
{
int N = k2 - k1;
for(int i = j1; i < j2; i++) {
for(int j = k1; j < k2; j++) {
grid((i-j1)*N + j-k1) = _grid(i,j);
}
}
}
void GridMaterial2D::set_grid(int M, int N, ArrayXXcd grid)
{
_M = M;
_N = N;
_grid = grid;
}
int GridMaterial2D::get_M() { return _M; }
int GridMaterial2D::get_N() { return _N; }
//------------------------------ MaterialPrimitives ------------------------------------/
GridCell::GridCell()
{
}
void GridCell::set_vertices(double xmin, double xmax, double ymin, double ymax)
{
double area;
_verts.clear();
Polygon_2D new_poly;
boost::geometry::append(new_poly, Point_2D(xmin, ymin));
boost::geometry::append(new_poly, Point_2D(xmin, ymax));
boost::geometry::append(new_poly, Point_2D(xmax, ymax));
boost::geometry::append(new_poly, Point_2D(xmax, ymin));
boost::geometry::append(new_poly, Point_2D(xmin, ymin));
boost::geometry::correct(new_poly);
boost::geometry::assign(_original, new_poly);
_verts.push_back(new_poly);
area = boost::geometry::area(new_poly);
_area = fabs(area);
_max_area = _area;
}
double GridCell::intersect(const Polygon_2D poly)
{
double area = 0.0,
intersected_area,
geo_area;
_diffs.clear();
std::vector<Polygon_2D>::const_iterator i;
// Do the difference
for(i = _verts.begin(); i != _verts.end(); ++i) {
boost::geometry::difference((*i), poly, _diffs);
}
_verts.clear();
for(i = _diffs.begin(); i != _diffs.end(); i++) {
_verts.push_back(*i);
geo_area = boost::geometry::area(*i);
area += fabs(geo_area);
}
intersected_area = _area - area;
//if(intersected_area < 0) {
// std::cout << intersected_area << std::endl;
// std::cout << "Size: " << _verts.size() << std::endl;
// std::cout << "Poly 1: " << boost::geometry::dsv(_verts.front()) << std::endl;
// std::cout << "Poly original: " << boost::geometry::dsv(_original) << std::endl;
// std::cout << "Poly intersecting: " << boost::geometry::dsv(poly) << std::endl;
//}
_area = area;
return intersected_area/_max_area;
}
double GridCell::get_area()
{
return _area;
}
double GridCell::get_max_area()
{
return _max_area;
}
double GridCell::get_area_ratio()
{
return _area/_max_area;
}
//------------------------------ MaterialPrimitives ------------------------------------/
MaterialPrimitive::MaterialPrimitive()
{
_layer = 1;
}
int MaterialPrimitive::get_layer() const { return _layer; }
void MaterialPrimitive::set_layer(int layer) { _layer = layer; }
bool MaterialPrimitive::operator<(const MaterialPrimitive& rhs)
{
return _layer < rhs.get_layer();
}
Circle::Circle(double x0, double y0, double r) : _x0(x0), _y0(y0), _r(r) {}
Circle::~Circle() {}
bool Circle::contains_point(double x, double y)
{
double dx = x - _x0,
dy = y - _y0;
return dx*dx + dy*dy < _r*_r;
}
bool Circle::bbox_contains_point(double x, double y)
{
double xmin = _x0-_r,
xmax = _x0+_r,
ymin = _y0-_r,
ymax = _y0+_r;
return (x > xmin) && (x < xmax) && (y > ymin) && (y < ymax);
}
std::complex<double> Circle::get_material(double x, double y)
{
return _mat;
}
// TODO: Implement this properly
double Circle::get_cell_overlap(GridCell& cell)
{
return 0.0;
}
void Circle::set_material(std::complex<double> mat)
{
_mat = mat;
}
void Circle::set_position(double x0, double y0)
{
_x0 = x0;
_y0 = y0;
}
void Circle::set_radius(double r)
{
_r = r;
}
double Circle::get_x0()
{
return _x0;
}
double Circle::get_y0()
{
return _y0;
}
double Circle::get_r()
{
return _r;
}
Rectangle::Rectangle(double x0, double y0, double width, double height) :
_x0(x0), _y0(y0), _width(width), _height(height)
{
// Points must be defined clockwise and the polygon must be closed
boost::geometry::append(_poly_rep, Point_2D(x0-width/2.0, y0-height/2.0));
boost::geometry::append(_poly_rep, Point_2D(x0-width/2.0, y0+height/2.0));
boost::geometry::append(_poly_rep, Point_2D(x0+width/2.0, y0+height/2.0));
boost::geometry::append(_poly_rep, Point_2D(x0+width/2.0, y0-height/2.0));
boost::geometry::append(_poly_rep, Point_2D(x0-width/2.0, y0-height/2.0));
}
Rectangle::~Rectangle()
{
}
bool Rectangle::contains_point(double x, double y)
{
double hwidth = _width/2,
hheight = _height/2;
return (x > _x0-hwidth) && (x < _x0+hwidth) && (y > _y0-hheight) && (y < _y0+hheight);
}
bool Rectangle::bbox_contains_point(double x, double y)
{
return contains_point(x,y);
}
std::complex<double> Rectangle::get_material(double x, double y)
{
return _mat;
}
double Rectangle::get_cell_overlap(GridCell& cell)
{
return cell.intersect(_poly_rep);
}
void Rectangle::set_material(std::complex<double> mat)
{
_mat = mat;
}
void Rectangle::set_width(double w)
{
_width = w;
std::vector<Point_2D>& outer = _poly_rep.outer();
outer[0].x(_x0-_width/2.0);
outer[1].x(_x0-_width/2.0);
outer[2].x(_x0+_width/2.0);
outer[3].x(_x0+_width/2.0);
outer[4].x(_x0-_width/2.0);
}
void Rectangle::set_height(double h)
{
_height = h;
std::vector<Point_2D>& outer = _poly_rep.outer();
outer[0].y(_y0-_height/2.0);
outer[1].y(_y0+_height/2.0);
outer[2].y(_y0+_height/2.0);
outer[3].y(_y0-_height/2.0);
outer[4].y(_y0-_height/2.0);
}
void Rectangle::set_position(double x0, double y0)
{
_x0 = x0;
_y0 = y0;
std::vector<Point_2D>& outer = _poly_rep.outer();
outer[0].x(_x0-_width/2.0);
outer[1].x(_x0-_width/2.0);
outer[2].x(_x0+_width/2.0);
outer[3].x(_x0+_width/2.0);
outer[4].x(_x0-_width/2.0);
outer[0].y(_y0-_height/2.0);
outer[1].y(_y0+_height/2.0);
outer[2].y(_y0+_height/2.0);
outer[3].y(_y0-_height/2.0);
outer[4].y(_y0-_height/2.0);
}
//------------------------------ Polygon ------------------------------------/
Polygon::Polygon()
{
}
Polygon::Polygon(double* x, double* y, int n)
{
set_points(x, y, n);
}
Polygon::~Polygon()
{
_verts.clear();
}
void Polygon::add_point(double x, double y)
{
boost::geometry::append(_verts, boost::geometry::make<Point_2D>(x,y));
// update the bounding box
boost::geometry::envelope(_verts, _bbox);
// correct the geometry
boost::geometry::correct(_verts);
}
/**
* NOTE: Currently a copy of the input points is made. This will be slowish.
*/
void Polygon::add_points(double* x, double* y, int n)
{
for(int i = 0; i < n; i++) {
boost::geometry::append(_verts, boost::geometry::make<Point_2D>(x[i], y[i]));
}
// update the bounding box
boost::geometry::envelope(_verts, _bbox);
// correct the geometry
boost::geometry::correct(_verts);
}
void Polygon::set_point(double x, double y, int index)
{
Point_2D& p = _verts.outer()[index];
p.x(x);
p.y(y);
// update the bounding box
boost::geometry::envelope(_verts, _bbox);
// assume the geometry is correct.
}
void Polygon::set_points(double* x, double* y, int n)
{
_verts.clear();
add_points(x,y,n);
// update the bounding box
boost::geometry::envelope(_verts, _bbox);
// correct the geometry
boost::geometry::correct(_verts);
}
bool Polygon::contains_point(double x, double y)
{
Point_2D p(x, y);
bool inside = boost::geometry::within(p, _verts);
return inside;
}
bool Polygon::bbox_contains_point(double x, double y)
{
Point_2D p(x,y);
bool inside = boost::geometry::within(p, _bbox);
return inside;
}
std::complex<double> Polygon::get_material(double x, double y)
{
//if(contains_point(x,y))
return _mat;
}
double Polygon::get_cell_overlap(GridCell& cell)
{
return cell.intersect(_verts);
}
void Polygon::set_material(std::complex<double> mat)
{
_mat = mat;
}
/////////////////////////////////////////////////////////////////////////////////////
// StructuredMaterial2D
/////////////////////////////////////////////////////////////////////////////////////
StructuredMaterial2D::StructuredMaterial2D(double w, double h, double dx, double dy) :
_w(w), _h(h), _dx(dx), _dy(dy)
{}
StructuredMaterial2D::~StructuredMaterial2D() {}
/* It is important to the material averaging algorithm that primitives be stored in an
* ordered list according to their layer. Lower layers are stored first (have priority).
* This means that once you have added a primitive to a list, you cannot change its
* layer!
*/
void StructuredMaterial2D::add_primitive(MaterialPrimitive* prim)
{
std::list<MaterialPrimitive*>::iterator it, insert_pos = _primitives.end();
if(_primitives.size() == 0) {
_primitives.push_back(prim);
}
else {
for(it = _primitives.begin(); it != _primitives.end(); it++) {
if( prim->get_layer() < (*it)->get_layer() ) {
insert_pos = it;
break;
}
}
_primitives.insert(it, prim);
}
}
void StructuredMaterial2D::add_primitives(std::list<MaterialPrimitive*> primitives)
{
std::list<MaterialPrimitive*>::iterator it;
for(it = primitives.begin(); it != primitives.end(); it++) {
add_primitive(*it);
}
}
void StructuredMaterial2D::get_values(ArrayXcd& grid, int k1, int k2, int j1, int j2, double sx, double sy)
{
int N = k2 - k1;
for(int j = j1; j < j2; j++) {
for(int k = k1; k < k2; k++) {
grid((j-j1)*N+k-k1) = get_value(k+sx, j+sy);
}
}
}
// This attempts to compute a reasonable average of the materials in a given Yee cell
// Note that there are a few situations where this average will not quite be what they
// should be. In particular, if three or more materials intersect a cell, this
// average will begin to deviate from the "correct" average
std::complex<double> StructuredMaterial2D::get_value(double x, double y)
{
std::complex<double> val = 0.0;
std::list<MaterialPrimitive*>::iterator it = _primitives.begin();
MaterialPrimitive* prim;
GridCell cell;
double xd = x*_dx, //+ _dx/2.0,
yd = y*_dy; //+ _dy/2.0;
double xmin = xd - _dx/2.0,
xmax = xd + _dx/2.0,
ymin = yd - _dy/2.0,
ymax = yd + _dy/2.0,
overlap = 1.0;
bool contains_p1,
contains_p2,
contains_p3,
contains_p4;
cell.set_vertices(xmin,xmax,ymin,ymax);
if(_primitives.size() == 0) {
std::cerr << "Error: StructuredMaterial list is empty." << std::endl;
return 0.0;
}
//std::cout << "------------------------" << std::endl;
while(it != _primitives.end()) {
prim = (*it);
// These values are used twice, so we recompute them
contains_p1 = prim->contains_point(xmin,ymin);
contains_p2 = prim->contains_point(xmax,ymin);
contains_p3 = prim->contains_point(xmax,ymax);
contains_p4 = prim->contains_point(xmin,ymax);
if(contains_p1 && contains_p2 &&
contains_p3 && contains_p4 &&
cell.get_area_ratio() == 1.0)
{
return prim->get_material(xd,yd);
}
else if(contains_p1 || contains_p2 ||
contains_p3 || contains_p4)
{
overlap = prim->get_cell_overlap(cell);
val += overlap * prim->get_material(xd,yd);
}
it++;
if(cell.get_area_ratio() == 0) {
break;
}
}
// assume background has index of 1.0
if(cell.get_area_ratio() > 0) {
val += cell.get_area_ratio()*1.0;
}
return val;
}
std::list<MaterialPrimitive*> StructuredMaterial2D::get_primitives()
{
return _primitives;
}
////////////////////////////////////////////////////////////////////////////////////
// ConstantMaterial2D
////////////////////////////////////////////////////////////////////////////////////
ConstantMaterial2D::ConstantMaterial2D(std::complex<double> value)
{
_value = value;
}
std::complex<double> ConstantMaterial2D::get_value(double x, double y)
{
return _value;
}
void ConstantMaterial2D::get_values(ArrayXcd& grid, int k1, int k2, int j1, int j2, double sx, double sy)
{
int N = k2 - k1;
for(int i = j1; i < j2; i++) {
for(int j = k1; j < k2; j++) {
grid((i-j1)*N + j-k1) = _value;
}
}
}
void ConstantMaterial2D::set_material(std::complex<double> val)
{
_value = val;
}
std::complex<double> ConstantMaterial2D::get_material()
{
return _value;
}
////////////////////////////////////////////////////////////////////////////////////
// ConstantMaterial3D
////////////////////////////////////////////////////////////////////////////////////
ConstantMaterial3D::ConstantMaterial3D(std::complex<double> value)
{
_value = value;
}
std::complex<double> ConstantMaterial3D::get_value(double k, double j, double i)
{
return _value;
}
void ConstantMaterial3D::get_values(ArrayXcd& grid, int k1, int k2, int j1, int j2,
int i1, int i2, double sx, double sy, double sz)
{
int N = k2 - k1,
M = j2 - j1;
for(int i = i1; i < i2; i++) {
for(int j = j1; j < j2; j++) {
for(int k = k1; k < k2; k++) {
grid((i-i1)*N*M + (j-j1)*N + k-k1) = _value;
}
}
}
}
void ConstantMaterial3D::set_material(std::complex<double> val)
{
_value = val;
}
std::complex<double> ConstantMaterial3D::get_material()
{
return _value;
}
////////////////////////////////////////////////////////////////////////////////////
// StructuredMaterial3D
////////////////////////////////////////////////////////////////////////////////////
StructuredMaterial3D::StructuredMaterial3D(double X, double Y, double Z,
double dx, double dy, double dz) :
_X(X), _Y(Y), _Z(Z),
_dx(dx), _dy(dy), _dz(dz)
{
_background = 1.0;
_use_cache = true;
_cache_active = false;
}
// We allocate memory -- Need to free it!
StructuredMaterial3D::~StructuredMaterial3D()
{
for(auto it = _layers.begin(); it != _layers.end(); it++) {
delete (*it);
}
}
void StructuredMaterial3D::add_primitive(MaterialPrimitive* prim, double z1, double z2)
{
// Dummy variables
StructuredMaterial2D* layer;
double znew[2] = {z1, z2},
z = 0;
// Get access to relevant lists
auto itl = _layers.begin();
auto itz = _zs.begin();
std::list<StructuredMaterial2D*>::iterator itl_ins;
std::list<double>::iterator itz_ins;
// Make sure the layer has a thickness
if(z1 == z2) {
std::cout << "Warning in Structured3DMaterial: Provided layer has no \
thickness. It will be ignored." << std :: endl;
return;
}
else if(z2 < z1) {
std::cout << "Warning in Structured3DMaterial: Provided layer has negative \
thickness. It will be ignored." << std :: endl;
return;
}
// If this is the first addition, things are simple
if(itz == _zs.end()) {
_zs.push_back(z1);
_zs.push_back(z2);
layer = new StructuredMaterial2D(_X, _Y, _dx, _dy);
layer->add_primitive(prim);
_layers.push_back(layer);
return;
}
// now we insert the beginning and end point of the layer one at a time, breaking
// up or inserting new layers as necessary
for(int i = 0; i < 2; i++) {
z = znew[i];
itz = _zs.begin();
itl = _layers.begin();
itz_ins = _zs.end();
itl_ins = _layers.end();
// figure out where the point is going to go
while(itz != _zs.end()) {
if(z >= *itz) {
itz_ins = itz;
itl_ins = itl;
}
itz++;
if(itl != _layers.end())
itl++;
}
// Three cases to consider: (1) point below stack (2) point above stack (3)
// point in stack
if(itz_ins == _zs.end()) {
layer = new StructuredMaterial2D(_X, _Y, _dx, _dy);
_layers.push_front(layer);
_zs.push_front(z);
}
else if(itz_ins == --_zs.end() && z != *itz_ins) {
layer = new StructuredMaterial2D(_X, _Y, _dx, _dy);
_layers.push_back(layer);
_zs.push_back(z);
}
else {
// make sure the point to insert is not already in the stack
if(z != *itz_ins) {
layer = new StructuredMaterial2D(_X, _Y, _dx, _dy);
layer->add_primitives( (*itl_ins)->get_primitives() );
_layers.insert(itl_ins, layer);
_zs.insert(++itz_ins, z);
}
}
}
// Finally, insert the supplied MaterialPrimitve into the desired locations
itz = _zs.begin();
itl = _layers.begin();
// figure out where the point is going to go
while(itl != _layers.end()) {
z = (*itz);
if(z >= z1 && z < z2) {
(*itl)->add_primitive(prim);
}
itz++;
itl++;
}
//for(itl = _layers.begin(); itl != _layers.end(); itl++) {
// std::cout << std::endl;
// std::list<MaterialPrimitive*> prims = (*itl)->get_primitives();
// for(auto ip = prims.begin(); ip != prims.end(); ip++)
// std::cout << *ip << std::endl;
//}
// aaannnddd we're done!
}
std::complex<double> StructuredMaterial3D::get_value(double k, double j, double i)
{
double zmin = (i-0.5) * _dz,
zmax = (i+0.5) * _dz;
std::complex<double> value = 0.0,
mat_val;
std::list<double>::iterator itz = _zs.begin(),
itz_next;
auto itl = _layers.begin();
auto itcv = _cached_values.begin();
auto itcf = _cached_flags.begin();
bool cached = false;
int jc = 0, kc = 0;
// Check if i is below the stack
if(zmax <= *itz) {
return _background;
}
if(zmax > *itz && zmin < *itz) {
value = (*itz - zmin) / _dz * 1.0;
zmin = *itz;
}
while(itl != _layers.end())
{
itz_next = std::next(itz);
if(zmin >= *itz && zmax <= *itz_next)
{
if(_use_cache and _cache_active) {
jc = int(j) - _cache_j0;
kc = int(k) - _cache_k0;
cached = (*itcf)(jc, kc);
if(cached) {
mat_val = (*itcv)(jc, kc);
}
else {
mat_val = (*itl)->get_value(k, j);
(*itcv)(jc, kc) = mat_val;
(*itcf)(jc, kc) = true;
}
}
else {
mat_val = (*itl)->get_value(k, j);
}
value += (zmax - zmin) / _dz * mat_val;
return value;
}
else if(zmin >= *itz && zmin < *itz_next && zmax > *itz_next)
{
if(_use_cache and _cache_active) {
jc = int(j) - _cache_j0;
kc = int(k) - _cache_k0;
cached = (*itcf)(jc, kc);
if(cached) {
mat_val = (*itcv)(jc, kc);
}
else {
mat_val = (*itl)->get_value(k, j);
(*itcv)(jc, kc) = mat_val;
(*itcf)(jc, kc) = true;
}
}
else {
mat_val = (*itl)->get_value(k, j);
}
value += (*itz_next - zmin) / _dz * mat_val;
zmin = *itz_next;
}
itl++;
itz++;
itcv++;
itcf++;
}
value += (zmax - zmin) / _dz * 1.0;
return value;
}
// Note that this takes a 1D array!
void StructuredMaterial3D::get_values(ArrayXcd& grid, int k1, int k2,
int j1, int j2,
int i1, int i2,
double sx, double sy, double sz)
{
int index = 0,
Nx = k2-k1,
Ny = j2-j1;
int Nl, Nc;
// if caching is enabled, setup all of the cache arrays
// We need two arrays per slab: one to keep track of which values are already cached
// and one to actually store the cached values
if(_use_cache) {
Nl = _layers.size();
Nc = _cached_values.size();
if(Nc != Nl) {
_cached_values.resize(Nl);
_cached_flags.resize(Nl);
}
for(auto ic = _cached_values.begin(); ic != _cached_values.end(); ic++) {
(*ic).setZero(j2-j1, k2-k1);
}
for(auto flag = _cached_flags.begin(); flag != _cached_flags.end(); flag++) {
(*flag).setZero(j2-j1, k2-k1);
}
_cache_j0 = int(j1+sy);
_cache_k0 = int(k1+sx);
_cache_J = j2-j1;
_cache_K = k2-k1;
_cache_active = true;
}
for(int i = i1; i < i2; i++) {
for(int j = j1; j < j2; j++) {
for(int k = k1; k < k2; k++) {
index = (i-i1)*Nx*Ny + (j-j1)*Nx + (k-k1);
grid(index) = get_value(k+sx, j+sy, i+sz);
}
}
}
_cache_active = false;
}