-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.hpp
593 lines (502 loc) · 17.2 KB
/
grid.hpp
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
/******************************************************************************
* GRIDGEN: Grid Generating Compiler
* By: Andy Stone ([email protected])
* (C) Copyright 2011 Colorado State University
*****************************************************************************/
/** \addtogroup Environmental
* @{
*/
#ifndef GRID_HPP_
#define GRID_HPP_
#include <iostream>
#include <string>
#include <vector>
#include "error.hpp"
#include "iprintable.hpp"
#include "iserializable.hpp"
#include "ienvironmental.hpp"
//#include <set>
//#include <list>
//#include "cellFieldPicture.hpp"
class Subgrid;
/**
* Initialization function for the grid module.
*/
void initializeModule_grid();
/**
* An orientation corresponds to one of four corners in a region.
*/
enum Orientation {
BL = 0, // bottom-left
BR, // bottom-right
TL, // top-left
TR // top-right
};
/** Represents a location in the global coordinate space */
struct GlobalCoordinate : public virtual IPrintable {
Subgrid *sg;
int x, y;
GlobalCoordinate(Subgrid *_sg, int _x, int _y) :
sg(_sg),
x(_x),
y(_y)
{ }
virtual void print(std::ostream &out) const;
virtual void printSimp(std::ostream &out) const;
bool operator<(const struct GlobalCoordinate& rhs) const;
};
/**
* @ingroup Environmental
*
* A region is a rectangular index space that has an orientation.
* The index space may optionally be empty.
*/
class Region : public virtual IPrintable,
public virtual ISerializable
{
public:
// =======================
// - [Construction] -
// =======================
/** @name Construction */
///@{
/** Construct an empty region */
Region();
/**
* Construct a region that represents the set of indices
* {(x, y) | lowx <= x <= highx and lowy <= y <= highy} where
* lowx = min(x1, x2), highx = max(x1, x2), lowy = min(y1, y2), and
* highy = min(y1, y2). Infer the orientation of the rectangle based
* on the relative orders of x1 and x2, and y1 and y2.
*/
Region(int x1, int y1, int x2, int y2);
///@}
// =======================
// - [Input/Output] -
// =======================
/** @name Input/Output */
///@{
virtual void print(std::ostream &out) const;
virtual void printSimp(std::ostream &out) const;
virtual void output(std::ostream &out) const;
virtual void input(std::istream &in);
///@}
// =======================
// - [Accessors] -
// =======================
/** @name Accessors */
///@{
/** Return the smallest X index in the region. */
int lowX() const { return mLowX; }
/** Return the smallest Y index in the region. */
int lowY() const { return mLowY; }
/** Return the highest X index in the region. */
int highX() const { return mHighX; }
/** Return the highest Y index in the region. */
int highY() const { return mHighY; }
/** Return the highest Y index in the region. */
bool isEmpty() const { return mEmpty; }
/** Return the highest Y index in the region. */
Orientation orientation() const { return mOrientation; }
///@}
// =======================
// - [Queries] -
// =======================
/** @name Queries */
///@{
/**
* Return the key X location in the region. The key point is determined
* by the region's orientation
*/
int keyX() const;
/**
* Return the key Y location in the region. The key point is determined
* by the region's orientation
*/
int keyY() const;
/** Return true if (x,y) is contained in this region */
bool contains(int x, int y) const;
/**
* Return true if rhs is horizontally flipped relative to this
* region.
*/
bool isHorizFlipRelativeTo(const Region &rhs) const;
/**
* Return true if rhs is vertically flipped relative to this
* region.
*/
bool isVertFlipRelativeTo(const Region &rhs) const;
/**
* Return true if the shape of rhs is a 90 degree flip of this
* region.
*/
bool is90degFlip(const Region &rhs) const;
/**
* Reorient the point to use the orientation of tgt
*/
void reorient(Region tgt, int &x, int &y, bool accountForSelf=true, bool flip=false) const;
///@}
// ================================
// - [Arithmetic Operations] -
// ================================
/** @name Arithmetic Operations */
///@{
/**
* Determine the intersection of two regions.
*
* Example:
*/
Region intersect(const Region &rhs) const;
/**
* Given a region embedded in this region determine the analogous region
* embedded in the target.
*/
Region analogousRegion(const Region ®, const Region &tgt) const;
/**
* Find the intersection of this region and rhs, then determine the
* analogous region of the cut in tgt.
*/
void cutAnalogously(const Region &rhs, const Region &tgt,
Region &resInSrc, Region &resInTgt) const;
/**
* Flip the region horizontally.
*/
void flipHoriz();
/**
* Flip the region vertically.
*/
void flipVert();
//Region operator+(Neighbor *neigh);
//Region operator-(Neighbor *neigh);
//Region intersect(Region rhs);
//Region expand(vector<Neighbor*> neighs);
//Region expandIntoMultiple(vector<int> sizes);
///@}
// =======================
// - [Mutators] -
// =======================
/** @name Mutators */
///@{
/**
* Reshape the region so that it represents the set of indices
* {(x, y) | lowx <= x <= highx and lowy <= y <= highy} where
* lowx = min(x1, x2), highx = max(x1, x2), lowy = min(y1, y2), and
* highy = min(y1, y2). Infer the orientation of the rectangle based
* on the relative orders of x1 and x2, and y1 and y2.
*/
void reshape(int x1, int y1, int x2, int y2);
/**
* Expand the region by a given amount in each dimension.
*/
void expand(int deltaLowX, int deltaLowY,
int deltaHighX, int deltaHighY);
/**
* Expand the region so that its x coordinate fall along a multiple
* of a given value; do the same for the y coordinate. You can think of
* this as "snapping" the size of the region.
*/
void expandToMultiple(int multipleX, int multipleY);
/**
* Reshape this region to only include indices that intersect with rhs.
*/
void cut(const Region &rhs);
/**
* Move the region to an offset location.
*/
void translate(int deltaX, int deltaY);
/**
* Set region to be empty
*/
void clear();
///@}
//static void reorient(int &ptX, int &ptY, Orientation o1, Orientation o2);
//static void reorient(int &ptX, int &ptY, Orientation o1);
private:
int mLowX, mLowY, mHighX, mHighY;
Orientation mOrientation;
bool mEmpty;
};
/**
* Two cells on a grid neighbor one another if the are adjacent to one another.
* Neighbor objects are used to identify the relative positioning of the values
* of two neighboring cells when stored using an array representation.
* Stencil computations are also defined in terms of what neighbors they
* access.
*
* Neighbor objects can be represented as a two-dimensional vector (x,y) where
* x is the offset of the neighbor in the x-dimension of the array and y
* is the offset of the neighbor in the y-dimension.
*/
class Neighbor : public virtual IPrintable,
public virtual ISerializable,
public virtual IEnvironmental
{
public:
friend class Environment; // Must be a friend so that environment may
// construct objects of this type.
// =======================
// - [Input and Output] -
// =======================
/** @name Input and Output */
///@{
virtual void print(std::ostream &out) const;
virtual void printSimp(std::ostream &out) const;
virtual void output(std::ostream &out) const;
virtual void input(std::istream &in);
///@}
// =======================
// - [Accessors] -
// =======================
/** @name Accessors */
///@{
virtual std::string getID() const { return mName; }
int x() const { return mX; }
int y() const { return mY; }
///@}
// =======================
// - [Mutators] -
// =======================
/** @name Mutators */
///@{
void setX(int x) { mX = x; }
void setY(int y) { mY = y; }
void set(int x, int y) { mX = x; mY = y; }
///@}
protected:
// =======================
// - [Construction] -
// =======================
/** Instantiate a new neighbor object using the specified identifier. */
Neighbor(const std::string &name);
private:
std::string mName;
int mX, mY;
};
/**
* A subgrid is a two-dimensional index space. Subgrids can be connected to
* one another to form a grid.
*/
class Subgrid : public virtual IPrintable,
public virtual ISerializable,
public virtual IEnvironmental
{
public:
friend class Environment; // Must be a friend so that environment may
// construct objects of this type.
// =======================
// - [Input/Output] -
// =======================
/** @name Input/Output */
///@{
virtual void print(std::ostream &out) const;
virtual void printSimp(std::ostream &out) const;
virtual void output(std::ostream &out) const;
virtual void input(std::istream &in);
///@}
// =======================
// - [Accessors] -
// =======================
/** @name Accessors */
///@{
virtual std::string getID() const { return mName; }
/** Return the width of the subgrid. */
int w() const { return mW; }
/** Return the height of the subgrid. */
int h() const { return mH; }
int sgid() const { return mSGID; }
std::string symbolicWidth() { return msW; }
std::string symbolicHeight() { return msH; }
/**
* Return the region of the subgrid that includes the topmost elements
*/
Region topRegion() const;
/**
* Return the region of the subgrid that includes the bottommost
* elements
*/
Region bottomRegion() const;
/**
* Return the region of the subgrid that includes the leftmost elements
*/
Region leftRegion() const;
/**
* Return the region of the subgrid that includes the rightmost
* elements
*/
Region rightRegion() const;
/**
* Return the region of the subgrid that includes the topmost ghost
* elements (this does not include the corners)
*/
Region topGhostRegion() const;
/**
* Return the region of the subgrid that includes the bottommost ghost
* elements (this does not include the corners)
*/
Region bottomGhostRegion() const;
/**
* Return the region of the subgrid that includes the leftmost ghost
* elements (this does not include the corners)
*/
Region leftGhostRegion() const;
/**
* Return the region of the subgrid that includes the rightmost ghost
* elements (this does not include the corners)
*/
Region rightGhostRegion() const;
///@}
// =======================
// - [Queries] -
// =======================
/** @name Queries */
///@{
/** Return true if position (x,y) is included in the subgrid */
bool contains(int x, int y);
/** Return a region that represents the index space of this subgrid */
Region region() const;
///@}
// =======================
// - [Mutators] -
// =======================
/** @name Mutators */
///@{
void setWidth(int w) { mW = w; }
void setHeight(int h) { mH = h; }
void setExtents(int w, int h) { mW = w; mH = h; }
void setSymbolicExtents(std::string w, std::string h) { msW = w; msH = h; }
///@}
protected:
// =======================
// - [Construction] -
// =======================
/** Instantiate a new subgrid object using the specified identifier. */
Subgrid(const std::string &name, const int sgid);
private:
std::string mName;
std::string msW, msH;
int mW, mH, mSGID;
};
/**
* A grid is a set of subgrids that connect to one another to form a unified
* index space.
*/
class Grid : public virtual IPrintable,
public virtual ISerializable,
public virtual IEnvironmental
{
public:
friend class Environment; // Must be a friend so that environment may
// construct objects of this type.
// =======================
// - [Input and Output] -
// =======================
/** @name Input and Output */
///@{
virtual void print(std::ostream &out) const;
virtual void printSimp(std::ostream &out) const;
virtual void output(std::ostream &out) const;
virtual void input(std::istream &in);
///@}
// =======================
// - [Accessors] -
// =======================
/** @name Accessors */
///@{
virtual std::string getID() const { return mName; }
/**
* Return the number of subgrids. Subgrid ID's (SGIDs) range between
* [1, numSubgrids()].
*/
int numSubgrids() const { return mSubgrids.size(); }
/**
* Return the subgrid with the specified ID. Note: ID's begin at 1.
*/
Subgrid *subgrid(int sgid) const;
/**
* Return true if this grid contains the specified subgrid.
*/
bool containsSubgrid(Subgrid *sg) const;
/**
* Return how many neighbors we have to pass through to rotate 0
* degrees in this grid.
*/
int orient0() const;
/**
* Return how many neighbors we have to pass through to rotate 45
* degrees in this grid.
*/
int orient45() const;
/**
* Return how many neighbors we have to pass through to rotate 90
* degrees in this grid.
*/
int orient90() const;
/**
* Return how many neighbors we have to pass through to rotate 135
* degrees in this grid.
*/
int orient135() const;
/**
* Return how many neighbors we have to pass through to rotate 180
* degrees in this grid.
*/
int orient180() const;
/**
* Return how many neighbors we have to pass through to rotate 225
* degrees in this grid.
*/
int orient225() const;
/**
* Return how many neighbors we have to pass through to rotate 270
* degrees in this grid.
*/
int orient270() const;
/**
* Return how many neighbors we have to pass through to rotate 315
* degrees in this grid.
*/
int orient315() const;
/**
* Return how many neighbors we have to pass through to rotate 360
* degrees in this grid.
*/
int orient360() const;
/**
* If the specified coordinate lies in a border map translate it.
*/
GlobalCoordinate resolveBMap(const GlobalCoordinate &src) const;
///@}
// =======================
// - [Mutators] -
// =======================
/** @name Mutators */
///@{
/** Specify that subgrid sg should be connected to this grid */
void addSubgrid(Subgrid *sg);
/**
* Add a connectivity relationship between two subgrid.
* Note that srcRegion should be in the halo of srcSG, and tgtRegion
* should not be in the halo of the target subgrid.
**/
void addBorder(const Region &srcRegion, Subgrid *srcSG,
const Region &tgtRegion, Subgrid *tgtSG, int rotation);
///@}
std::string mName;
std::vector<Subgrid*> mSubgrids;
std::vector<Region> mBorderSrcRegions;
std::vector<Subgrid*> mBorderSrcSubgrids;
std::vector<Region> mBorderTgtRegions;
std::vector<Subgrid*> mBorderTgtSubgrids;
std::vector<int> mBorderRotation;
protected:
// =======================
// - [Construction] -
// =======================
Grid(const std::string &name);
// =======================
// - [Constants] -
// =======================
//static const int BMAP_HDL__NO_MAP = -1; // Specifies a lack of a border
// mapping.
};
/** @}*/