Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CADSurf.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: CADSystem.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: cad object methods.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <boost/geometry.hpp>
37 #include <boost/geometry/algorithms/assign.hpp>
38 #include <boost/geometry/geometries/point_xy.hpp>
39 #include <boost/geometry/geometries/polygon.hpp>
40 
41 #include "CADSurf.h"
42 #include "CADCurve.h"
43 
44 namespace bg = boost::geometry;
45 typedef bg::model::d2::point_xy<double> point_xy;
46 
47 using namespace std;
48 
49 namespace Nektar
50 {
51 namespace NekMeshUtils
52 {
53 
54 void CADSurf::OrientateEdges(CADSurfSharedPtr surf,
55  vector<CADSystem::EdgeLoopSharedPtr> &ein)
56 {
57  // this piece of code orientates the surface,
58  // it used to be face mesh but its easier to have it here
59  int np = 20;
60  vector<vector<Array<OneD, NekDouble> > > loopt;
61  for (int i = 0; i < ein.size(); i++)
62  {
63  vector<Array<OneD, NekDouble> > loop;
64  for (int j = 0; j < ein[i]->edges.size(); j++)
65  {
66  Array<OneD, NekDouble> bnds = ein[i]->edges[j]->GetBounds();
67  NekDouble dt = (bnds[1] - bnds[0]) / (np - 1);
68  if (ein[i]->edgeo[j] == CADOrientation::eForwards)
69  {
70  for (int k = 0; k < np - 1; k++)
71  {
72  NekDouble t = bnds[0] + dt * k;
73  Array<OneD, NekDouble> l = ein[i]->edges[j]->P(t);
74  Array<OneD, NekDouble> uv = surf->locuv(l);
75  loop.push_back(uv);
76  }
77  }
78  else
79  {
80  for (int k = np - 1; k > 0; k--)
81  {
82  NekDouble t = bnds[0] + dt * k;
83  Array<OneD, NekDouble> l = ein[i]->edges[j]->P(t);
84  Array<OneD, NekDouble> uv = surf->locuv(l);
85  loop.push_back(uv);
86  }
87  }
88  }
89  loopt.push_back(loop);
90  }
91 
92  vector<bg::model::polygon<point_xy, false, true> > polygons;
93 
94  for (int i = 0; i < loopt.size(); i++)
95  {
96  bg::model::polygon<point_xy, false, true> polygon;
97  vector<point_xy> points;
98  for (int j = 0; j < loopt[i].size(); j++)
99  {
100  points.push_back(point_xy(loopt[i][j][0], loopt[i][j][1]));
101  }
102  // boost requires for closed polygons (last point == first point)
103  points.push_back(point_xy(loopt[i][0][0], loopt[i][0][1]));
104 
105  bg::assign_points(polygon, points);
106 
107  NekDouble area = bg::area(polygon);
108 
109  ein[i]->area = area;
110 
111  point_xy cen;
112  bg::centroid(polygon, cen);
113 
114  ein[i]->center = Array<OneD, NekDouble>(2);
115  ein[i]->center[0] = cen.x();
116  ein[i]->center[1] = cen.y();
117 
118  polygons.push_back(polygon);
119  }
120 
121  // order by absoulte area
122  int ct = 0;
123  do
124  {
125  ct = 0;
126  for (int i = 0; i < ein.size() - 1; i++)
127  {
128  if (fabs(ein[i]->area) < fabs(ein[i + 1]->area))
129  {
130  // swap
131  swap(ein[i], ein[i + 1]);
132  swap(loopt[i], loopt[i + 1]);
133  swap(polygons[i], polygons[i + 1]);
134  ct += 1;
135  }
136  }
137 
138  } while (ct > 0);
139 
140  // only need center points for inner loops
141  for (int i = 1; i < ein.size(); i++)
142  {
143  point_xy p(ein[i]->center[0], ein[i]->center[1]);
144 
145  if (!bg::within(p, polygons[i]))
146  {
147  Array<OneD, NekDouble> n1 = loopt[i][0];
148  Array<OneD, NekDouble> n2 = loopt[i][1];
149 
151  NekDouble mag = sqrt((n1[0] - n2[0]) * (n1[0] - n2[0]) +
152  (n1[1] - n2[1]) * (n1[1] - n2[1]));
153 
154  N[0] = (n2[1] - n1[1]) / mag;
155  N[1] = -1.0 * (n2[0] - n1[0]) / mag;
156 
158  P[0] = n1[0] + N[0];
159  P[1] = n1[1] + N[1];
160 
161  ein[i]->center = P;
162 
163  p = point_xy(P[0], P[1]);
164 
165  ASSERTL0(boost::geometry::within(p, polygons[i]), "point is not side loop");
166  }
167  }
168 }
169 }
170 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
STL namespace.
bg::model::d2::point_xy< double > point_xy
Definition: CADSurf.cpp:45
double NekDouble
boost::shared_ptr< CADSurf > CADSurfSharedPtr
Definition: CADSurf.h:172