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