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 #include "CADSurf.h"
36 #include "CADCurve.h"
37 
38 #include <boost/geometry.hpp>
39 #include <boost/geometry/algorithms/assign.hpp>
40 #include <boost/geometry/geometries/point_xy.hpp>
41 #include <boost/geometry/geometries/polygon.hpp>
42 
43 namespace bg = boost::geometry;
44 typedef bg::model::d2::point_xy<double> point_xy;
45 
46 using namespace std;
47 
48 namespace Nektar
49 {
50 namespace NekMeshUtils
51 {
52 
53 void CADSurf::OrientateEdges(CADSurfSharedPtr surf,
54  vector<CADSystem::EdgeLoopSharedPtr> &ein)
55 {
56  // this piece of code orientates the surface,
57  // it used to be face mesh but its easier to have it here
58  int np = 20;
59  vector<vector<Array<OneD, NekDouble> > > loopt;
60  for (int i = 0; i < ein.size(); i++)
61  {
62  vector<Array<OneD, NekDouble> > loop;
63  for (int j = 0; j < ein[i]->edges.size(); j++)
64  {
65  Array<OneD, NekDouble> bnds = ein[i]->edges[j]->GetBounds();
66  NekDouble dt = (bnds[1] - bnds[0]) / (np - 1);
67  if (ein[i]->edgeo[j] == CADOrientation::eForwards)
68  {
69  for (int k = 0; k < np - 1; k++)
70  {
71  NekDouble t = bnds[0] + dt * k;
72  Array<OneD, NekDouble> l = ein[i]->edges[j]->P(t);
73  Array<OneD, NekDouble> uv = surf->locuv(l);
74  loop.push_back(uv);
75  }
76  }
77  else
78  {
79  for (int k = np - 1; k > 0; k--)
80  {
81  NekDouble t = bnds[0] + dt * k;
82  Array<OneD, NekDouble> l = ein[i]->edges[j]->P(t);
83  Array<OneD, NekDouble> uv = surf->locuv(l);
84  loop.push_back(uv);
85  }
86  }
87  }
88  loopt.push_back(loop);
89  }
90 
91  vector<bg::model::polygon<point_xy, false, true> > polygons;
92 
93  for (int i = 0; i < loopt.size(); i++)
94  {
95  bg::model::polygon<point_xy, false, true> polygon;
96  vector<point_xy> points;
97  for (int j = 0; j < loopt[i].size(); j++)
98  {
99  points.push_back(point_xy(loopt[i][j][0], loopt[i][j][1]));
100  }
101  // boost requires for closed polygons (last point == first point)
102  points.push_back(point_xy(loopt[i][0][0], loopt[i][0][1]));
103 
104  bg::assign_points(polygon, points);
105 
106  NekDouble area = bg::area(polygon);
107 
108  ein[i]->area = area;
109 
110  point_xy cen;
111  bg::centroid(polygon, cen);
112 
113  ein[i]->center = Array<OneD, NekDouble>(2);
114  ein[i]->center[0] = cen.x();
115  ein[i]->center[1] = cen.y();
116 
117  polygons.push_back(polygon);
118  }
119 
120  // order by absoulte area
121  int ct = 0;
122  do
123  {
124  ct = 0;
125  for (int i = 0; i < ein.size() - 1; i++)
126  {
127  if (fabs(ein[i]->area) < fabs(ein[i + 1]->area))
128  {
129  // swap
130  swap(ein[i], ein[i + 1]);
131  swap(loopt[i], loopt[i + 1]);
132  swap(polygons[i], polygons[i + 1]);
133  ct += 1;
134  }
135  }
136 
137  } while (ct > 0);
138 
139  // only need center points for inner loops
140  for (int i = 1; i < ein.size(); i++)
141  {
142  point_xy p(ein[i]->center[0], ein[i]->center[1]);
143 
144  if (!bg::within(p, polygons[i]))
145  {
146  Array<OneD, NekDouble> n1 = loopt[i][0];
147  Array<OneD, NekDouble> n2 = loopt[i][1];
148 
150  NekDouble mag = sqrt((n1[0] - n2[0]) * (n1[0] - n2[0]) +
151  (n1[1] - n2[1]) * (n1[1] - n2[1]));
152 
153  N[0] = (n2[1] - n1[1]) / mag;
154  N[1] = -1.0 * (n2[0] - n1[0]) / mag;
155 
157  P[0] = n1[0] + N[0];
158  P[1] = n1[1] + N[1];
159 
160  ein[i]->center = P;
161 
162  p = point_xy(P[0], P[1]);
163 
164  ASSERTL0(boost::geometry::within(p, polygons[i]), "point is not side loop");
165  }
166  }
167 }
168 }
169 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
STL namespace.
bg::model::d2::point_xy< double > point_xy
Definition: CADSurf.cpp:44
double NekDouble
boost::shared_ptr< CADSurf > CADSurfSharedPtr
Definition: CADSurf.h:172