xmsgrid  1.0
triangles.cpp
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
7 //------------------------------------------------------------------------------
8 
9 //----- Included files ---------------------------------------------------------
10 
11 #include <xmsgrid/geometry/geoms.h>
12 #include <xmscore/stl/vector.h>
14 
15 //----- Constants / Enumerations -----------------------------------------------
16 
17 //----- Forward declarations ---------------------------------------------------
18 
19 //----- External globals -------------------------------------------------------
20 
21 //----- Namespace declaration --------------------------------------------------
22 
23 namespace xms
24 {
25 //----- Global variables -------------------------------------------------------
26 
27 //----- File globals -----------------------------------------------------------
28 
29 //----- Internal function prototypes -------------------------------------------
30 
31 //----- Functions --------------------------------------------------------------
32 
33 //------------------------------------------------------------------------------
45 //------------------------------------------------------------------------------
46 double trArea(const Pt3d& a_pt1, const Pt3d& a_pt2, const Pt3d& a_pt3)
47 {
48  return (((a_pt2.x - a_pt1.x) * (a_pt3.y - a_pt1.y)) -
49  ((a_pt3.x - a_pt1.x) * (a_pt2.y - a_pt1.y))) *
50  0.5;
51 } // trArea
52 //------------------------------------------------------------------------------
71 //------------------------------------------------------------------------------
72 void trBuildGridTrianglePolys(int rows, int cols, VecPt3d& a_points, VecInt2d& a_polys)
73 {
74  trBuildGridPolys(rows, cols, a_points, a_polys);
75 
76  VecPt3d points = a_points;
77 
78  // add cell centers
79  for (size_t i = 0; i < a_polys.size(); ++i)
80  {
81  VecPt3d pts;
82  VecInt& poly = a_polys[i];
83  size_t numPts = poly.size();
84  pts.reserve(numPts);
85  for (size_t j = 0; j < numPts; ++j)
86  {
87  pts.push_back(a_points[poly[j]]);
88  }
89  Pt3d centroid = gmComputeCentroid(pts);
90  points.push_back(centroid);
91  }
92 
93  // Build triangle polys
94  int firstCellCenter = (int)a_points.size();
95  VecInt2d trianglePolys;
96 
97  for (size_t cellIdx = 0; cellIdx < a_polys.size(); ++cellIdx)
98  {
99  VecInt newPoly(3, 0);
100  VecInt& poly = a_polys[cellIdx];
101  size_t numPts = poly.size();
102  for (size_t pt = 0; pt < numPts; ++pt)
103  {
104  newPoly[0] = poly[pt];
105  newPoly[1] = poly[(pt + 1) % numPts];
106  newPoly[2] = firstCellCenter + (int)cellIdx;
107  trianglePolys.push_back(newPoly);
108  }
109  }
110 
111  a_points = points;
112  a_polys = trianglePolys;
113 } // trBuildGridTrianglePolys
114 //------------------------------------------------------------------------------
131 //------------------------------------------------------------------------------
132 void trBuildGridPolys(int rows, int cols, VecPt3d& pts, VecInt2d& polys)
133 {
134  pts.clear();
135  for (int i = 0; i <= rows; ++i)
136  {
137  for (int j = 0; j <= cols; ++j)
138  {
139  pts.push_back(Pt3d(j * 10.0, -i * 10.0, 0.0));
140  }
141  }
142 
143  polys.assign(rows * cols, VecInt());
144  int count = 0;
145  int node = 0;
146  int nodeNextRow = cols + 1;
147  for (int i = 0; i < rows; ++i)
148  {
149  for (int j = 0; j < cols; ++j)
150  {
151  VecInt& poly = polys[count++];
152  poly.push_back(node++);
153  poly.push_back(nodeNextRow++);
154  poly.push_back(nodeNextRow);
155  poly.push_back(node);
156  }
157  ++node;
158  ++nodeNextRow;
159  }
160 } // trBuildGridPolys
161 } // namespace xms
std::vector< int > VecInt
Functions dealing with triangles.
void trBuildGridPolys(int rows, int cols, VecPt3d &pts, VecInt2d &polys)
Build something like this:
Definition: triangles.cpp:132
std::vector< VecInt > VecInt2d
Functions dealing with geometry.
double trArea(const Pt3d &a_pt1, const Pt3d &a_pt2, const Pt3d &a_pt3)
Return the signed planar area of the triangle (CCW Positive).
Definition: triangles.cpp:46
void trBuildGridTrianglePolys(int rows, int cols, VecPt3d &a_points, VecInt2d &a_polys)
Create something like this:
Definition: triangles.cpp:72
Pt3d gmComputeCentroid(const VecPt3d &a_points)
Computes the centroid of points (but not of a polygon). Shouldn&#39;t pass an empty vector (no checks are...
Definition: geoms.cpp:895
Pt3< double > Pt3d
XMS Namespace.
Definition: geoms.cpp:34
std::vector< Pt3d > VecPt3d