KrisLibrary  1.0.0
Rasterize.h
Go to the documentation of this file.
1 #ifndef MESHING_RASTERIZE_H
2 #define MESHING_RASTERIZE_H
3 
4 #include <KrisLibrary/structs/array2d.h>
5 #include <KrisLibrary/math3d/Triangle2D.h>
6 #include <KrisLibrary/math3d/AABB2D.h>
7 #include <KrisLibrary/math3d/Polygon2D.h>
8 #include <KrisLibrary/utils/IntPair.h>
9 #include <vector>
10 
16 namespace Meshing {
17 
18  using namespace Math3D;
19 
22 
25 void GetSegmentCells(const Segment2D& tri,std::vector<IntPair>& cells);
26 
29 void GetTriangleCells(const Triangle2D& tri,std::vector<IntPair>& cells);
30 
33 void GetTriangleCells_Clipped(const Triangle2D& tri,std::vector<IntPair>& cells,int imin,int jmin,int imax,int jmax);
34 
35 
48 {
49  virtual ~Rasterizer2D() {}
50  void Rasterize(const Triangle2D& t);
51  void ClippedRasterize(const Triangle2D& t,const AABB2D& aabb);
52  void Rasterize(const AABB2D& b);
53  void ClippedRasterize(const AABB2D& t,const AABB2D& aabb);
54 
55  //helpers
56  void Rasterize(const Triangle2D& t,const Vector3& baryA,const Vector3& baryB,const Vector3& baryC);
57  //Rasterizes the segment with x coordinate i, y coordinates [y1,y2]
58  void RasterizeVerticalSegment(int i,Real y1,Real y2,const Vector3& baryA,const Vector3& baryB);
59 
60  //fill a point
61  //params = barycentric coords of triangle, or (u,v) parameters of rect
62  virtual void VisitCell(const Vector3& params,int i,int j)=0;
63 };
64 
66 template <class T>
68 {
69  FillRasterizer2D() : grid(NULL) {}
70  FillRasterizer2D(Array2D<T>& _grid) : grid(&_grid) {}
71  virtual ~FillRasterizer2D() {}
72 
73  inline void Rasterize(const Triangle2D& tri) { Rasterizer2D::Rasterize(tri); }
74 
75  inline void Rasterize(const Triangle2D& tri,const T& val) {
76  fillVal = val;
77  Rasterizer2D::Rasterize(tri);
78  }
79 
80  inline void Rasterize(const AABB2D& b,const T& val) {
81  fillVal = val;
82  Rasterizer2D::Rasterize(b);
83  }
84 
85  virtual void VisitCell(const Vector3& bary,int i,int j)
86  {
87  if(grid&& i>=0 && j>=0 && i<grid->m && j<grid->n) {
88  Fill(bary,(*grid)(i,j));
89  }
90  }
91 
92  virtual void Fill(const Vector3& params,T&cell) { cell = fillVal; }
93 
94  //the destination grid
95  Array2D<T>* grid;
96  //set this to the desired fill value
97  T fillVal;
98 };
99 
101 template <class T>
103 {
104  inline void Rasterize(const Triangle2D& tri) { FillRasterizer2D<T>::Rasterize(tri); }
105 
106  inline void Rasterize(const Triangle2D& tri,const T& flatColor) {
107  fillA = fillB = fillC = flatColor;
108  Rasterizer2D::Rasterize(tri);
109  }
110 
111  inline void Rasterize(const Triangle2D& tri,const T& a,const T& b,const T& c) {
112  fillA = a;
113  fillB = b;
114  fillC = c;
115  Rasterizer2D::Rasterize(tri);
116  }
117 
118  virtual void Fill(const Vector3& bary,T& cell) { cell = bary.x*fillA+bary.y*fillB+bary.z*fillC; }
119 
120  //set these to the desired values at the corners of the triangle
121  T fillA,fillB,fillC;
122 };
123 
124 
127 } //namespace Meshing
128 
129 #endif
The namespace for all classes/functions in the Meshing package.
Definition: AnyGeometry.h:11
A 3D vector class.
Definition: math3d/primitives.h:136
A 2D triangle class.
Definition: Triangle2D.h:25
A rasterizer that flat-fills elements of a grid.
Definition: Rasterize.h:67
A two-dimensional m x n array.
Definition: array2d.h:30
A 2D segment class.
Definition: Segment2D.h:17
Contains all the definitions in the Math3D package.
Definition: AnyGeometry.h:13
void GetTriangleCells(const Triangle2D &t, vector< IntPair > &cells)
Returns a list of cells that the triangle overlaps, given an infinite unit grid.
Definition: Rasterize.cpp:295
A smooth-fill rasterizer.
Definition: Rasterize.h:102
void GetTriangleCells_Clipped(const Triangle2D &torig, std::vector< IntPair > &cells, int imin, int jmin, int imax, int jmax)
Returns a list of cells that the triangle overlaps, in a unit grid from [imin,imax)x[jmin,jmax)
Definition: Rasterize.cpp:99
A 2D axis-aligned bounding box.
Definition: AABB2D.h:13
void GetSegmentCells(const Segment2D &s, vector< IntPair > &cells)
Returns a list of cells that the segment overlaps, given an infinite unit grid.
Definition: Rasterize.cpp:54
A base class that allows rasterizing of 2D triangles into a grid.
Definition: Rasterize.h:47