Nektar++
Node.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Node.h
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 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Mesh node object.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef NEKMESHUTILS_MESHELEMENTS_NODE
36 #define NEKMESHUTILS_MESHELEMENTS_NODE
37 
40 
41 #include <iomanip>
42 
47 
48 namespace Nektar
49 {
50 namespace NekMeshUtils
51 {
52 class Node;
53 typedef std::shared_ptr<Node> NodeSharedPtr;
54 
55 /**
56  * @brief Represents a point in the domain.
57  *
58  * Such points may either be element vertices, or simply control
59  * points on high-order edges/faces, although this information is not
60  * contained within this class.
61  */
62 class Node
63 {
64 public:
65  /// Create a new node at a specified coordinate.
67  : m_id(pId), m_x(pX), m_y(pY), m_z(pZ), m_geom()
68  {
69  }
70  /// Copy an existing node.
71  // Node(const Node& pSrc)
72  // : m_id(pSrc.m_id), m_x(pSrc.m_x), m_y(pSrc.m_y),
73  // m_z(pSrc.m_z), m_geom() {}
74  /// create an empty node
75  NEKMESHUTILS_EXPORT Node() : m_id(0), m_x(0.0), m_y(0.0), m_z(0.0), m_geom()
76  {
77  }
79  {
80  }
81 
82  /// Reset the local id;
83  NEKMESHUTILS_EXPORT void SetID(int pId)
84  {
85  m_id = pId;
86  }
87 
88  /// Get the local id;
90  {
91  return m_id;
92  }
93 
94  /// Define node ordering based on ID.
96  {
97  return (m_id < pSrc.m_id);
98  }
99  /// Define node equality based on coordinate.
101  {
102  return m_x == pSrc.m_x && m_y == pSrc.m_y && m_z == pSrc.m_z;
103  }
104 
106  {
107  return Node(m_id, m_x + pSrc.m_x, m_y + pSrc.m_y, m_z + pSrc.m_z);
108  }
109 
111  {
112  return Node(m_id, m_x - pSrc.m_x, m_y - pSrc.m_y, m_z - pSrc.m_z);
113  }
114 
116  {
117  return Node(m_id, m_x * pSrc.m_x, m_y * pSrc.m_y, m_z * pSrc.m_z);
118  }
119 
121  {
122  return Node(m_id, alpha * m_x, alpha * m_y, alpha * m_z);
123  }
124 
126  {
127  return Node(m_id, m_x / alpha, m_y / alpha, m_z / alpha);
128  }
129 
131  {
132  m_x += pSrc.m_x;
133  m_y += pSrc.m_y;
134  m_z += pSrc.m_z;
135  }
136 
138  {
139  m_x *= alpha;
140  m_y *= alpha;
141  m_z *= alpha;
142  }
143 
145  {
146  m_x /= alpha;
147  m_y /= alpha;
148  m_z /= alpha;
149  }
150 
151  NEKMESHUTILS_EXPORT NodeSharedPtr copy()
152  {
153  return std::shared_ptr<Node>(new Node(m_id, m_x, m_y, m_z));
154  }
155 
157  {
158  return m_x * m_x + m_y * m_y + m_z * m_z;
159  }
160 
162  {
163  return m_x * pSrc.m_x + m_y * pSrc.m_y + m_z * pSrc.m_z;
164  }
165 
166  NEKMESHUTILS_EXPORT Node curl(const Node &pSrc) const
167  {
168  return Node(m_id, m_y * pSrc.m_z - m_z * pSrc.m_y,
169  m_z * pSrc.m_x - m_x * pSrc.m_z,
170  m_x * pSrc.m_y - m_y * pSrc.m_x);
171  }
172 
174  {
175  Array<OneD, NekDouble> out(3);
176  out[0] = m_x;
177  out[1] = m_y;
178  out[2] = m_z;
179  return out;
180  }
181 
182  /// Generate a %SpatialDomains::PointGeom for this node.
184  {
187  coordDim, m_id, m_x, m_y, m_z);
188 
189  return ret;
190  }
191 
193  {
194  return sqrt((m_x - p->m_x) * (m_x - p->m_x) +
195  (m_y - p->m_y) * (m_y - p->m_y) +
196  (m_z - p->m_z) * (m_z - p->m_z));
197  }
198 
199  NEKMESHUTILS_EXPORT NekDouble Angle(NodeSharedPtr &a, NodeSharedPtr &b)
200  {
201  Array<OneD, NekDouble> va(3), vb(3), cn(3);
202  va[0] = a->m_x - m_x;
203  va[1] = a->m_y - m_y;
204  va[2] = a->m_z - m_z;
205  vb[0] = b->m_x - m_x;
206  vb[1] = b->m_y - m_y;
207  vb[2] = b->m_z - m_z;
208 
209  NekDouble lva = sqrt(va[0] * va[0] + va[1] * va[1] + va[2] * va[2]);
210  NekDouble lvb = sqrt(vb[0] * vb[0] + vb[1] * vb[1] + vb[2] * vb[2]);
211 
212  NekDouble aw = 1.0 / (lva * lvb);
213 
214  NekDouble cosw = (va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2]) * aw;
215 
216  cn[0] = vb[1] * va[2] - vb[2] * va[1];
217  cn[1] = vb[2] * va[0] - vb[0] * va[2];
218  cn[2] = vb[0] * va[1] - vb[1] * va[0];
219 
220  NekDouble lcn = sqrt(cn[0] * cn[0] + cn[1] * cn[1] + cn[2] * cn[2]);
221 
222  NekDouble sinw = aw * lcn;
223 
224  NekDouble an = atan2(sinw, cosw);
225 
226  if (an < 0)
227  an += 6.2831853071796;
228 
229  return an;
230  }
231 
232  // functions for cad information
233 
235  {
236  auto it = CADCurveList.find(c->GetId());
237  if (it != CADCurveList.end())
238  {
239  // already in list so remove it
240  CADCurveList.erase(it);
241  }
242  CADCurveList.insert(make_pair(c->GetId(), make_pair(c, t)));
243  }
244 
246  {
247  auto it = CADSurfList.find(s->GetId());
248  if (it != CADSurfList.end())
249  {
250  // already in list so remove it
251  CADSurfList.erase(it);
252  }
253  CADSurfList.insert(make_pair(s->GetId(), make_pair(s, uv)));
254  }
255 
257  {
258  auto search = CADCurveList.find(i);
259  ASSERTL0(search != CADCurveList.end(), "node not on this curve");
260 
261  return search->second.second;
262  }
263 
265  {
266  auto search = CADSurfList.find(i);
267  ASSERTL0(search != CADSurfList.end(), "surface not found");
268 
269  return search->second.second;
270  }
271 
272  std::vector<CADCurveSharedPtr> GetCADCurves()
273  {
274  std::vector<CADCurveSharedPtr> lst;
275  for (auto &c : CADCurveList)
276  {
277  lst.push_back(c.second.first);
278  }
279  return lst;
280  }
281 
282  std::vector<CADSurfSharedPtr> GetCADSurfs()
283  {
284  std::vector<CADSurfSharedPtr> lst;
285  for (auto &s : CADSurfList)
286  {
287  lst.push_back(s.second.first);
288  }
289  return lst;
290  }
291 
293  {
294  return CADCurveList.size();
295  }
296 
298  {
299  return CADSurfList.size();
300  }
301 
303  {
304  m_x = l[0];
305  m_y = l[1];
306  m_z = l[2];
307  CADSurfSharedPtr su = CADSurfList[s].first;
308  SetCADSurf(su, uv);
309  }
310 
311  void Move(NekDouble x, NekDouble y, NekDouble z, int s,
313  {
314  m_x = x;
315  m_y = y;
316  m_z = z;
317  CADSurfSharedPtr su = CADSurfList[s].first;
318  SetCADSurf(su, uv);
319  }
320 
322  {
323  m_x = l[0];
324  m_y = l[1];
325  m_z = l[2];
326  CADCurveSharedPtr cu = CADCurveList[c].first;
327  SetCADCurve(cu, t);
328  }
329 
330  void Move(NekDouble x, NekDouble y, NekDouble z, int c, NekDouble t)
331  {
332  m_x = x;
333  m_y = y;
334  m_z = z;
335  CADCurveSharedPtr cu = CADCurveList[c].first;
336  SetCADCurve(cu, t);
337  }
338 
339  void Rotate(std::string dir, NekDouble angle)
340  {
341  if (dir == "x")
342  {
343  NekDouble yrot = cos(angle) * m_y - sin(angle) * m_z;
344  NekDouble zrot = sin(angle) * m_y + cos(angle) * m_z;
345 
346  m_y = yrot;
347  m_z = zrot;
348  }
349  else if (dir == "y")
350  {
351  NekDouble zrot = cos(angle) * m_z - sin(angle) * m_x;
352  NekDouble xrot = sin(angle) * m_z + cos(angle) * m_x;
353 
354  m_z = zrot;
355  m_x = xrot;
356  }
357  else if (dir == "z")
358  {
359  NekDouble xrot = cos(angle) * m_x - sin(angle) * m_y;
360  NekDouble yrot = sin(angle) * m_x + cos(angle) * m_y;
361 
362  m_x = xrot;
363  m_y = yrot;
364  }
365  else
366  {
367  ASSERTL0(false, "Unrecognised rotational direction: " + dir);
368  }
369  }
370 
373  {
374  // calculates the angle between this node to a to this node to b
375  // Uses the CAD surface to orientate the angle
376  Array<OneD, NekDouble> A(3), B(3), CP(3);
377  A[0] = locA[0] - m_x;
378  A[1] = locA[1] - m_y;
379  A[2] = locA[2] - m_z;
380  B[0] = locB[0] - m_x;
381  B[1] = locB[1] - m_y;
382  B[2] = locB[2] - m_z;
383 
384  CP[0] = A[1] * B[2] - A[2] * B[1];
385  CP[1] = -1.0 * (A[0] * B[2] - A[2] * B[0]);
386  CP[2] = A[0] * B[1] - A[1] * B[0];
387 
388  NekDouble ang = sqrt(CP[0] * CP[0] + CP[1] * CP[1] + CP[2] * CP[2]);
389 
390  ang /= sqrt(A[0] * A[0] + A[1] * A[1] + A[2] * A[2]);
391  ang /= sqrt(B[0] * B[0] + B[1] * B[1] + B[2] * B[2]);
392 
393  NekDouble dot = N[0] * CP[0] + N[1] * CP[1] + N[2] * CP[2];
394 
395  ang = asin(ang);
396 
397  if (dot < 0.0)
398  {
399  ang = 2.0 * M_PI - ang;
400  }
401 
402  return ang;
403  }
404 
405  /// ID of node.
406  int m_id;
407  /// X-coordinate.
409  /// Y-coordinate.
411  /// Z-coordinate.
413 
414  /// list of cadcurves the node lies on
415  std::map<int, std::pair<CADCurveSharedPtr, NekDouble>> CADCurveList;
416  /// list of cadsurfs the node lies on
417  std::map<int, std::pair<CADSurfSharedPtr, Array<OneD, NekDouble>>>
419 
420 private:
422 };
423 /// Shared pointer to a Node.
424 
425 NEKMESHUTILS_EXPORT bool operator==(NodeSharedPtr const &p1,
426  NodeSharedPtr const &p2);
427 NEKMESHUTILS_EXPORT bool operator<(NodeSharedPtr const &p1,
428  NodeSharedPtr const &p2);
429 NEKMESHUTILS_EXPORT bool operator!=(NodeSharedPtr const &p1,
430  NodeSharedPtr const &p2);
431 NEKMESHUTILS_EXPORT std::ostream &operator<<(std::ostream &os,
432  const NodeSharedPtr &n);
433 
434 /**
435  * @brief Defines a hash function for nodes.
436  *
437  * The hash of a node is straight-forward; a combination of the x, y,
438  * and z co-ordinates in this order.
439  */
440 struct NodeHash : std::unary_function<NodeSharedPtr, std::size_t>
441 {
442  std::size_t operator()(NodeSharedPtr const &p) const
443  {
444  return hash_combine(p->m_x, p->m_y, p->m_z);
445  }
446 };
447 typedef std::unordered_set<NodeSharedPtr, NodeHash> NodeSet;
448 }
449 }
450 
451 #endif
static NekDouble ang(NekDouble x, NekDouble y)
Definition: Interpreter.cpp:91
NEKMESHUTILS_EXPORT SpatialDomains::PointGeomSharedPtr GetGeom(int coordDim)
Generate a SpatialDomains::PointGeom for this node.
Definition: Node.h:183
NEKMESHUTILS_EXPORT NekDouble Angle(NodeSharedPtr &a, NodeSharedPtr &b)
Definition: Node.h:199
std::shared_ptr< CADSurf > CADSurfSharedPtr
Definition: CADCurve.h:51
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
NEKMESHUTILS_EXPORT Node operator+(const Node &pSrc) const
Definition: Node.h:105
std::size_t operator()(NodeSharedPtr const &p) const
Definition: Node.h:442
std::ostream & operator<<(std::ostream &os, const NodeSharedPtr &n)
Print description of node to given ostream.
NEKMESHUTILS_EXPORT NekDouble abs2() const
Definition: Node.h:156
NEKMESHUTILS_EXPORT bool operator<(const Node &pSrc)
Define node ordering based on ID.
Definition: Node.h:95
void Move(NekDouble x, NekDouble y, NekDouble z, int c, NekDouble t)
Definition: Node.h:330
NEKMESHUTILS_EXPORT int GetID(void)
Get the local id;.
Definition: Node.h:89
NEKMESHUTILS_EXPORT ~Node()
Definition: Node.h:78
void hash_combine(std::size_t &seed)
Definition: HashUtils.hpp:46
NEKMESHUTILS_EXPORT Node operator*(const NekDouble &alpha) const
Definition: Node.h:120
NekDouble m_y
Y-coordinate.
Definition: Node.h:410
NEKMESHUTILS_EXPORT NekDouble dot(const Node &pSrc) const
Definition: Node.h:161
NEKMESHUTILS_EXPORT void operator*=(const NekDouble &alpha)
Definition: Node.h:137
Defines a hash function for nodes.
Definition: Node.h:440
void Move(Array< OneD, NekDouble > l, int s, Array< OneD, NekDouble > uv)
Definition: Node.h:302
Represents a point in the domain.
Definition: Node.h:62
std::shared_ptr< Node > NodeSharedPtr
Definition: CADVert.h:49
std::vector< CADSurfSharedPtr > GetCADSurfs()
Definition: Node.h:282
std::unordered_set< NodeSharedPtr, NodeHash > NodeSet
Definition: Node.h:447
void Move(NekDouble x, NekDouble y, NekDouble z, int s, Array< OneD, NekDouble > uv)
Definition: Node.h:311
void SetCADCurve(CADCurveSharedPtr c, NekDouble t)
Definition: Node.h:234
NEKMESHUTILS_EXPORT Node operator-(const Node &pSrc) const
Definition: Node.h:110
bool operator!=(NodeSharedPtr const &p1, NodeSharedPtr const &p2)
Compares two nodes for inequality based on IDs.
int m_id
ID of node.
Definition: Node.h:406
NEKMESHUTILS_EXPORT void operator/=(const NekDouble &alpha)
Definition: Node.h:144
void SetCADSurf(CADSurfSharedPtr s, Array< OneD, NekDouble > uv)
Definition: Node.h:245
NEKMESHUTILS_EXPORT void SetID(int pId)
Reset the local id;.
Definition: Node.h:83
std::shared_ptr< CADCurve > CADCurveSharedPtr
Definition: CADCurve.h:219
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
void Rotate(std::string dir, NekDouble angle)
Definition: Node.h:339
double NekDouble
NEKMESHUTILS_EXPORT NodeSharedPtr copy()
Definition: Node.h:151
std::shared_ptr< PointGeom > PointGeomSharedPtr
Definition: Geometry.h:59
NEKMESHUTILS_EXPORT Node operator*(const Node &pSrc) const
Definition: Node.h:115
NEKMESHUTILS_EXPORT bool operator==(const Node &pSrc)
Define node equality based on coordinate.
Definition: Node.h:100
NEKMESHUTILS_EXPORT Node()
Copy an existing node.
Definition: Node.h:75
NekDouble Angle(Array< OneD, NekDouble > locA, Array< OneD, NekDouble > locB, Array< OneD, NekDouble > N)
Definition: Node.h:371
NEKMESHUTILS_EXPORT Node curl(const Node &pSrc) const
Definition: Node.h:166
std::map< int, std::pair< CADSurfSharedPtr, Array< OneD, NekDouble > > > CADSurfList
list of cadsurfs the node lies on
Definition: Node.h:418
NEKMESHUTILS_EXPORT Node operator/(const NekDouble &alpha) const
Definition: Node.h:125
NEKMESHUTILS_EXPORT NekDouble Distance(NodeSharedPtr &p)
Definition: Node.h:192
NekDouble m_x
X-coordinate.
Definition: Node.h:408
std::map< int, std::pair< CADCurveSharedPtr, NekDouble > > CADCurveList
list of cadcurves the node lies on
Definition: Node.h:415
NEKMESHUTILS_EXPORT void operator+=(const Node &pSrc)
Definition: Node.h:130
#define NEKMESHUTILS_EXPORT
NekDouble GetCADCurveInfo(int i)
Definition: Node.h:256
void Move(Array< OneD, NekDouble > l, int c, NekDouble t)
Definition: Node.h:321
Array< OneD, NekDouble > GetCADSurfInfo(int i)
Definition: Node.h:264
NekDouble m_z
Z-coordinate.
Definition: Node.h:412
std::vector< CADCurveSharedPtr > GetCADCurves()
Definition: Node.h:272
NEKMESHUTILS_EXPORT Node(int pId, NekDouble pX, NekDouble pY, NekDouble pZ)
Create a new node at a specified coordinate.
Definition: Node.h:66
SpatialDomains::PointGeomSharedPtr m_geom
Definition: Node.h:421
NEKMESHUTILS_EXPORT Array< OneD, NekDouble > GetLoc()
Definition: Node.h:173