Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ProcessSpherigon.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessSpherigon.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: Apply Spherigon surface smoothing technique to a 3D mesh.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <string>
37 using namespace std;
38 
39 #include "MeshElements.h"
40 #include "ProcessSpherigon.h"
41 
42 #include <LocalRegions/SegExp.h>
43 #include <LocalRegions/QuadExp.h>
44 #include <LocalRegions/TriExp.h>
49 
50 #define TOL_BLEND 1.0e-8
51 
52 namespace Nektar
53 {
54  namespace Utilities
55  {
56  ModuleKey ProcessSpherigon::className =
58  ModuleKey(eProcessModule, "spherigon"),
59  ProcessSpherigon::create);
60 
61  /**
62  * @class ProcessSpherigon
63  *
64  * This class implements the spherigon surface smoothing technique which
65  * is documented in
66  *
67  * "The SPHERIGON: A Simple Polygon Patch for Smoothing Quickly your
68  * Polygonal Meshes": P. Volino and N. Magnenat Thalmann, Computer
69  * Animation Proceedings (1998).
70  *
71  * This implementation works in both a 2D manifold setting (for
72  * triangles and quadrilaterals embedded in 3-space) and in a full 3D
73  * enviroment (prisms, tetrahedra and hexahedra).
74  *
75  * No additional information needs to be supplied directly to the module
76  * in order for it to work. However, 3D elements do rely on the mapping
77  * Mesh::spherigonSurfs to be populated by the relevant input modules.
78  *
79  * Additionally, since the algorithm assumes normals are supplied which
80  * are perpendicular to the true surface defined at each vertex. If
81  * these are specified in Mesh::vertexNormals by the input module,
82  * better smoothing results can be obtained. Otherwise, normals are
83  * estimated by taking the average of all edge/face normals which
84  * connect to the vertex.
85  */
86 
87  /**
88  * @brief Default constructor.
89  */
90  ProcessSpherigon::ProcessSpherigon(MeshSharedPtr m) : ProcessModule(m)
91  {
92  m_config["N"] = ConfigOption(false, "5",
93  "Number of points to add to face edges.");
94  m_config["surf"] = ConfigOption(false, "-1",
95  "Tag identifying surface to process.");
96  m_config["BothTriFacesOnPrism"] = ConfigOption(true, "-1",
97  "Curve both triangular faces of prism on boundary.");
98  m_config["usenormalfile"] = ConfigOption(false,"NoFile",
99  "Use alternative file for Spherigon definition");
100  m_config["scalefile"] = ConfigOption(false,"1.0",
101  "Apply scaling factor to coordinates in file ");
102  m_config["normalnoise"] = ConfigOption(false,"NotSpecified",
103  "Add randowm noise to normals of amplitude AMP in specified region. input string is Amp,xmin,xmax,ymin,ymax,zmin,zmax");
104  }
105 
106  /**
107  * @brief Destructor.
108  */
110  {
111 
112  }
113 
114  /*
115  * @brief Calcuate the normalised cross product \f$ \vec{c} =
116  * \vec{a}\times\vec{b} / \| \vec{a}\times\vec{b} \| \f$.
117  */
119  {
120  double inv_mag;
121 
122  c.m_x = a.m_y*b.m_z - a.m_z*b.m_y;
123  c.m_y = a.m_z*b.m_x - a.m_x*b.m_z;
124  c.m_z = a.m_x*b.m_y - a.m_y*b.m_x;
125 
126  inv_mag = 1.0/sqrt(c.m_x*c.m_x + c.m_y*c.m_y + c.m_z*c.m_z);
127 
128  c.m_x = c.m_x*inv_mag;
129  c.m_y = c.m_y*inv_mag;
130  c.m_z = c.m_z*inv_mag;
131  }
132 
133  /**
134  * @brief Calculate the magnitude of the cross product \f$
135  * \vec{a}\times\vec{b} \f$.
136  */
138  {
139  double tmp1 = a.m_y*b.m_z - a.m_z*b.m_y;
140  double tmp2 = a.m_z*b.m_x - a.m_x*b.m_z;
141  double tmp3 = a.m_x*b.m_y - a.m_y*b.m_x;
142  return sqrt(tmp1*tmp1 + tmp2*tmp2 + tmp3*tmp3);
143  }
144 
145  /**
146  * @brief Calculate the \f$ C^0 \f$ blending function for spherigon
147  * implementation.
148  *
149  * See equation (5) of the paper.
150  *
151  * @param r Barycentric coordinate.
152  */
153  double ProcessSpherigon::Blend(double r)
154  {
155  return r*r;
156  }
157 
158  /**
159  * @brief Calculate the \f$ C^1 \f$ blending function for spherigon
160  * implementation.
161  *
162  * See equation (10) of the paper.
163  *
164  * @param r Generalised barycentric coordinates of the point P.
165  * @param Q Vector of vertices denoting this triangle/quad.
166  * @param P Point in the triangle to apply blending to.
167  * @param blend The resulting blending components for each vertex.
168  */
170  vector<double> &r, vector<Node> &Q, Node &P, vector<double> &blend)
171  {
172  vector<double> tmp(r.size());
173  double totBlend = 0.0;
174  int i;
175  int nV = r.size();
176 
177  for (i = 0; i < nV; ++i)
178  {
179  blend[i] = 0.0;
180  tmp [i] = (Q[i]-P).abs2();
181  }
182 
183  for (i = 0; i < nV; ++i)
184  {
185  int ip = (i+1) % nV, im = (i-1+nV) % nV;
186 
187  if (r[i] > TOL_BLEND && r[i] < (1-TOL_BLEND))
188  {
189  blend[i] = r[i]*r[i]*(
190  r[im]*r[im]*tmp[im]/(tmp[im] + tmp[i]) +
191  r[ip]*r[ip]*tmp[ip]/(tmp[ip] + tmp[i]));
192  totBlend += blend[i];
193  }
194  }
195 
196  for (i = 0; i < nV; ++i)
197  {
198  blend[i] /= totBlend;
199  if (r[i] >= (1-TOL_BLEND))
200  {
201  blend[i] = 1.0;
202  }
203  if (r[i] <= TOL_BLEND)
204  {
205  blend[i] = 0.0;
206  }
207  }
208  }
209 
210  /**
211  * @brief Generate a set of approximate vertex normals to a surface
212  * represented by line segments in 2D and a hybrid
213  * triangular/quadrilateral mesh in 3D.
214  *
215  * This routine approximates the true vertex normals to a surface by
216  * averaging the normals of all edges/faces which connect to the
217  * vertex. It is better to use the exact surface normals which can be
218  * set in Mesh::vertexNormals, but where they are not supplied this
219  * routine calculates an approximation for the spherigon implementation.
220  *
221  * @param el Vector of elements denoting the surface mesh.
222  */
223  void ProcessSpherigon::GenerateNormals(std::vector<ElementSharedPtr> &el,
224  MeshSharedPtr &mesh)
225  {
227 
228 
229 
230  for (int i = 0; i < el.size(); ++i)
231  {
232  ElementSharedPtr e = el[i];
233 
234  // Ensure that element is a line, triangle or quad.
235  ASSERTL0(e->GetConf().m_e == LibUtilities::eSegment ||
236  e->GetConf().m_e == LibUtilities::eTriangle ||
237  e->GetConf().m_e == LibUtilities::eQuadrilateral,
238  "Spherigon expansions must be lines, triangles or "
239  "quadrilaterals.");
240 
241  // Calculate normal for this element.
242  int nV = e->GetVertexCount();
243  vector<NodeSharedPtr> node(nV);
244 
245  for (int j = 0; j < nV; ++j)
246  {
247  node[j] = e->GetVertex(j);
248  }
249 
250  Node n;
251 
252  if (mesh->m_spaceDim == 3)
253  {
254  // Create two tangent vectors and take unit cross product.
255  Node v1 = *(node[1]) - *(node[0]);
256  Node v2 = *(node[2]) - *(node[0]);
257  UnitCrossProd(v1, v2, n);
258  }
259  else
260  {
261  // Calculate gradient vector and invert.
262  Node dx = *(node[1]) - *(node[0]);
263  dx /= sqrt(dx.abs2());
264  n.m_x = -dx.m_y;
265  n.m_y = dx.m_x;
266  n.m_z = 0;
267  }
268 
269  // Insert face normal into vertex normal list or add to existing
270  // value.
271  for (int j = 0; j < nV; ++j)
272  {
273  nIt = mesh->m_vertexNormals.find(e->GetVertex(j)->m_id);
274  if (nIt == mesh->m_vertexNormals.end())
275  {
276  mesh->m_vertexNormals[e->GetVertex(j)->m_id] = n;
277  }
278  else
279  {
280  nIt->second += n;
281  }
282  }
283  }
284 
285  // Normalize resulting vectors.
286  for (nIt = mesh->m_vertexNormals.begin();
287  nIt != mesh->m_vertexNormals.end (); ++nIt)
288  {
289  Node &n = mesh->m_vertexNormals[nIt->first];
290  n /= sqrt(n.abs2());
291  }
292  }
293 
294  /**
295  * @brief Perform the spherigon smoothing technique on the mesh.
296  */
298  {
299  ASSERTL0(m_mesh->m_spaceDim == 3 || m_mesh->m_spaceDim == 2,
300  "Spherigon implementation only valid in 2D/3D.");
301 
303  boost::unordered_set<int> visitedEdges;
304 
305  // First construct vector of elements to process.
306  vector<ElementSharedPtr> el;
307 
308  if (m_mesh->m_verbose)
309  {
310  cout << "ProcessSpherigon: Smoothing mesh..." << endl;
311  }
312 
313  if (m_mesh->m_expDim == 2 && m_mesh->m_spaceDim == 3)
314  {
315  // Manifold case - copy expansion dimension.
316  el = m_mesh->m_element[m_mesh->m_expDim];
317  }
318  else if (m_mesh->m_spaceDim == m_mesh->m_expDim)
319  {
320  // Full 2D or 3D case - iterate over stored edges/faces and
321  // create segments/triangles representing those edges/faces.
322  set<pair<int,int> >::iterator it;
323  vector<int> t;
324  t.push_back(0);
325 
326 
327  // Construct list of spherigon edges/faces from a tag.
328  string surfTag = m_config["surf"].as<string>();
329  bool prismTag = m_config["BothTriFacesOnPrism"].beenSet;
330 
331  if (surfTag != "")
332  {
333  vector<unsigned int> surfs;
334  ParseUtils::GenerateSeqVector(surfTag.c_str(), surfs);
335  sort(surfs.begin(), surfs.end());
336 
337  m_mesh->m_spherigonSurfs.clear();
338  for (int i = 0; i < m_mesh->m_element[m_mesh->m_expDim].size(); ++i)
339  {
340  ElementSharedPtr el = m_mesh->m_element[m_mesh->m_expDim][i];
341  int nSurf = m_mesh->m_expDim == 3 ? el->GetFaceCount() :
342  el->GetEdgeCount();
343 
344  for (int j = 0; j < nSurf; ++j)
345  {
346  int bl = el->GetBoundaryLink(j);
347  if (bl == -1)
348  {
349  continue;
350  }
351 
352  ElementSharedPtr bEl = m_mesh->m_element[m_mesh->m_expDim-1][bl];
353  vector<int> tags = bEl->GetTagList();
354  vector<int> inter;
355 
356  sort(tags.begin(), tags.end());
357  set_intersection(surfs.begin(), surfs.end(),
358  tags .begin(), tags .end(),
359  back_inserter(inter));
360 
361  if (inter.size() == 1)
362  {
363  m_mesh->m_spherigonSurfs.insert(make_pair(i, j));
364 
365  // Curve other tri face on Prism. Note could be
366  // problem on pyramid when implemented.
367  if(nSurf == 5 && prismTag)
368  {
369  // add other end of prism on boundary for
370  // smoothing
371  int triFace = j == 1 ? 3 : 1;
372  m_mesh->m_spherigonSurfs.insert(
373  make_pair(i, triFace));
374  }
375  }
376  }
377  }
378  }
379 
380  if (m_mesh->m_spherigonSurfs.size() == 0)
381  {
382  cerr << "WARNING: Spherigon surfaces have not been defined "
383  << "-- ignoring smoothing." << endl;
384  return;
385  }
386 
387  if (m_mesh->m_expDim == 3)
388  {
389  for (it = m_mesh->m_spherigonSurfs.begin();
390  it != m_mesh->m_spherigonSurfs.end (); ++it)
391  {
392  FaceSharedPtr f = m_mesh->m_element[m_mesh->m_expDim][it->first]->
393  GetFace(it->second);
394  vector<NodeSharedPtr> nodes = f->m_vertexList;
395  LibUtilities::ShapeType eType = (LibUtilities::ShapeType)(nodes.size());
396  ElmtConfig conf(eType, 1, false, false);
397 
398  // Create 2D element.
400  CreateInstance(eType,conf,nodes,t);
401 
402  // Copy vertices/edges from face.
403  for (int i = 0; i < f->m_vertexList.size(); ++i)
404  {
405  elmt->SetVertex(i, f->m_vertexList[i]);
406  }
407  for (int i = 0; i < f->m_edgeList.size(); ++i)
408  {
409  elmt->SetEdge(i, f->m_edgeList[i]);
410  }
411 
412  el.push_back(elmt);
413  }
414  }
415  else
416  {
417  for (it = m_mesh->m_spherigonSurfs.begin();
418  it != m_mesh->m_spherigonSurfs.end (); ++it)
419  {
420  EdgeSharedPtr edge = m_mesh->m_element[m_mesh->m_expDim][it->first]->
421  GetEdge(it->second);
422  vector<NodeSharedPtr> nodes;
424  ElmtConfig conf(eType, 1, false, false);
425 
426  nodes.push_back(edge->m_n1);
427  nodes.push_back(edge->m_n2);
428 
429  // Create 2D element.
431  CreateInstance(eType,conf,nodes,t);
432 
433  // Copy vertices/edges from original element.
434  elmt->SetVertex(0, nodes[0]);
435  elmt->SetVertex(1, nodes[1]);
436  elmt->SetEdge(0, m_mesh->m_element[m_mesh->m_expDim][it->first]->
437  GetEdge(it->second));
438  el.push_back(elmt);
439  }
440  }
441  }
442  else
443  {
444  ASSERTL0(false, "Spherigon expansions must be 2/3 dimensional");
445  }
446 
447 
448  // See if vertex normals have been generated. If they have not,
449  // approximate them by summing normals of surrounding elements.
450  bool normalsGenerated = false;
451 
452  // Read Normal file if one exists
453  std::string normalfile = m_config["usenormalfile"].as<string>();
454  if(normalfile.compare("NoFile") != 0)
455  {
456  NekDouble scale = m_config["scalefile"].as<NekDouble>();
457 
458  if (m_mesh->m_verbose)
459  {
460  cout << "Inputing normal file: " << normalfile << " with scaling of " << scale << endl;
461  }
462 
463  ifstream inply;
464  InputPlySharedPtr plyfile;
465 
466  inply.open(normalfile.c_str());
467 
468  int j;
469  MeshSharedPtr m = boost::shared_ptr<Mesh>(new Mesh());
470  plyfile = boost::shared_ptr<InputPly>(new InputPly(m));
471  plyfile->ReadPly(inply,scale);
472  plyfile->ProcessVertices();
473 
474  MeshSharedPtr plymesh = plyfile->GetMesh();
475  GenerateNormals(plymesh->m_element[plymesh->m_expDim],plymesh);
476 
477  // finaly find nearest vertex and set normal to mesh surface file normal.
478  // probably should have a hex tree search ?
479  Array<OneD, NekDouble> len2(plymesh->m_vertexSet.size());
480  Node minx(0,0.0,0.0,0.0), tmp,tmpsav;
481  NodeSet::iterator it;
483  map<int,NodeSharedPtr> surfverts;
484 
485  // make a map of normal vertices to visit based on elements el
486  for (int i = 0; i < el.size(); ++i)
487  {
488  ElementSharedPtr e = el[i];
489  int nV = e->GetVertexCount();
490  for (int j = 0; j < nV; ++j)
491  {
492  int id = e->GetVertex(j)->m_id;
493  surfverts[id] = e->GetVertex(j);
494  }
495  }
496 
497  //loop over all element in ply mesh and determine
498  //xmin,xmax,ymin,ymax as search criterion
499 
500 
501  NekDouble mindiff,diff;
502  int cntmin;
503 
504  if (m_mesh->m_verbose)
505  {
506  cout << "\t Processing surface normals " << endl;
507  }
508  int cnt = 0;
509  map<int,int> locnorm;
510  for(vIt = surfverts.begin(); vIt != surfverts.end(); ++vIt,++cnt)
511  {
512  mindiff = 1e12;
513 
514  for(j = 0, it = plymesh->m_vertexSet.begin(); it != plymesh->m_vertexSet.end(); ++it, ++j)
515  {
516  tmp = *(vIt->second)- *(*it);
517  diff = tmp.abs2();
518 
519  if(diff < mindiff)
520  {
521  mindiff = diff;
522  cntmin = (*it)->m_id;
523  tmpsav = tmp;
524  }
525  }
526  locnorm[cntmin] = vIt->first;
527 
528  ASSERTL1(cntmin < plymesh->m_vertexNormals.size(),"cntmin is out of range");
529  m_mesh->m_vertexNormals[vIt->first] = plymesh->m_vertexNormals[cntmin];
530 
531  }
532  if (m_mesh->m_verbose)
533  {
534  cout << "\t end of processing surface normals " << endl;
535  }
536  normalsGenerated = true;
537  }
538  else if (m_mesh->m_vertexNormals.size() == 0)
539  {
541  normalsGenerated = true;
542  }
543 
544 
545  // See if we should add noise to normals
546  std::string normalnoise = m_config["normalnoise"].as<string>();
547  if(normalnoise.compare("NotSpecified") != 0)
548  {
549  vector<NekDouble> values;
550  ASSERTL0(ParseUtils::GenerateUnOrderedVector(normalnoise.c_str(),values),"Failed to interpret normal noise string");
551 
552  int nvalues = values.size()/2;
553  NekDouble amp = values[0];
554 
555 
556  if (m_mesh->m_verbose)
557  {
558  cout << "\t adding noise to normals of amplitude "<< amp << " in range: ";
559  for(int i = 0; i < nvalues; ++i)
560  {
561  cout << values[2*i+1] <<"," << values[2*i+2] << " ";
562  }
563  cout << endl;
564  }
565 
567  map<int,NodeSharedPtr> surfverts;
568 
569  // make a map of normal vertices to visit based on elements el
570  for (int i = 0; i < el.size(); ++i)
571  {
572  ElementSharedPtr e = el[i];
573  int nV = e->GetVertexCount();
574  for (int j = 0; j < nV; ++j)
575  {
576  int id = e->GetVertex(j)->m_id;
577  surfverts[id] = e->GetVertex(j);
578  }
579  }
580 
581  for(vIt = surfverts.begin(); vIt != surfverts.end(); ++vIt)
582  {
583  bool AddNoise = false;
584 
585  for(int i = 0; i < nvalues; ++i)
586  {
587  // check to see if point is in range
588  switch(nvalues)
589  {
590  case 1:
591  {
592  if(((vIt->second)->m_x > values[2*i+1])&&((vIt->second)->m_x < values[2*i+2]))
593  {
594  AddNoise = true;
595  }
596  }
597  break;
598  case 2:
599  {
600  if(((vIt->second)->m_x > values[2*i+1])&&
601  ((vIt->second)->m_x < values[2*i+2])&&
602  ((vIt->second)->m_y > values[2*i+3])&&
603  ((vIt->second)->m_y < values[2*i+4]))
604  {
605  AddNoise = true;
606  }
607  }
608  break;
609  case 3:
610  {
611  if(((vIt->second)->m_x > values[2*i+1])&&
612  ((vIt->second)->m_x < values[2*i+2])&&
613  ((vIt->second)->m_y > values[2*i+3])&&
614  ((vIt->second)->m_y < values[2*i+4])&&
615  ((vIt->second)->m_z > values[2*i+5])&&
616  ((vIt->second)->m_z < values[2*i+6]))
617 
618  {
619  AddNoise = true;
620  }
621  }
622  break;
623  }
624 
625  if(AddNoise)
626  {
627  // generate random unit vector;
628  Node rvec(0,rand(),rand(),rand());
629  rvec *= values[0]/sqrt(rvec.abs2());
630 
631  Node normal = m_mesh->m_vertexNormals[vIt->first];
632 
633  normal += rvec;
634  normal /= sqrt(normal.abs2());
635 
636  m_mesh->m_vertexNormals[vIt->first] = normal;
637  }
638  }
639  }
640  }
641 
642 
643  // Allocate storage for interior points.
644  int nq = m_config["N"].as<int>();
645  int nquad = m_mesh->m_spaceDim == 3 ? nq*nq : nq;
646  Array<OneD, NekDouble> x(nq*nq);
647  Array<OneD, NekDouble> y(nq*nq);
648  Array<OneD, NekDouble> z(nq*nq);
649 
650  Array<OneD, NekDouble> xc(nq*nq);
651  Array<OneD, NekDouble> yc(nq*nq);
652  Array<OneD, NekDouble> zc(nq*nq);
653 
654  ASSERTL0(nq > 2, "Number of points must be greater than 2.");
655 
667 
668  Array<OneD, NekDouble> xnodal(nq*(nq+1)/2), ynodal(nq*(nq+1)/2);
669  stdtri->GetNodalPoints(xnodal, ynodal);
670 
671  int edgeMap[3][4][2] = {
672  {{0, 1}, {-1, -1}, {-1, -1 }, {-1, -1}}, // seg
673  {{0, 1}, {nq-1, nq}, {nq*(nq-1), -nq}, {-1, -1}}, // tri
674  {{0, 1}, {nq-1, nq}, {nq*nq-1, -1 }, {nq*(nq-1), -nq}} // quad
675  };
676 
677  int vertMap[3][4][2] = {
678  {{0, 1}, {0, 0}, {0, 0}, {0, 0}}, // seg
679  {{0, 1}, {1, 2}, {2, 3}, {0, 0}}, // tri
680  {{0, 1}, {1, 2}, {2, 3}, {3, 0}}, // quad
681  };
682 
683  for (int i = 0; i < el.size(); ++i)
684  {
685  // Construct a Nektar++ element to obtain coordinate points
686  // inside the element. TODO: Add options for various
687  // nodal/tensor point distributions + number of points to add.
688  ElementSharedPtr e = el[i];
689 
694 
695  if (e->GetConf().m_e == LibUtilities::eSegment)
696  {
698  boost::dynamic_pointer_cast<SpatialDomains::SegGeom>(
699  e->GetGeom(m_mesh->m_spaceDim));
702  B2, geom);
703  seg->GetCoords(x,y,z);
704  nquad = nq;
705  }
706  else if (e->GetConf().m_e == LibUtilities::eTriangle)
707  {
709  boost::dynamic_pointer_cast<SpatialDomains::TriGeom>(
710  e->GetGeom(3));
714  B0, B1, LibUtilities::eNodalTriElec, geom);
715 
716  Array<OneD, NekDouble> coord(2);
717  tri->GetCoords(xc,yc,zc);
718  nquad = nq*(nq+1)/2;
719 
720  for (int j = 0; j < nquad; ++j)
721  {
722  coord[0] = xnodal[j];
723  coord[1] = ynodal[j];
724  x[j] = stdtri->PhysEvaluate(coord, xc);
725  }
726 
727  for (int j = 0; j < nquad; ++j)
728  {
729  coord[0] = xnodal[j];
730  coord[1] = ynodal[j];
731  y[j] = stdtri->PhysEvaluate(coord, yc);
732  }
733 
734  for (int j = 0; j < nquad; ++j)
735  {
736  coord[0] = xnodal[j];
737  coord[1] = ynodal[j];
738  z[j] = stdtri->PhysEvaluate(coord, zc);
739  }
740  }
741  else if (e->GetConf().m_e == LibUtilities::eQuadrilateral)
742  {
744  boost::dynamic_pointer_cast<SpatialDomains::QuadGeom>(
745  e->GetGeom(3));
748  B2, B2, geom);
749  quad->GetCoords(x,y,z);
750  nquad = nq*nq;
751  }
752  else
753  {
754  ASSERTL0(false, "Unknown expansion type.");
755  }
756 
757  // Zero z-coordinate in 2D.
758  if (m_mesh->m_spaceDim == 2)
759  {
760  Vmath::Zero(nquad, z, 1);
761  }
762 
763  // Find vertex normals.
764  int nV = e->GetVertexCount();
765  vector<Node> v, vN;
766  for (int j = 0; j < nV; ++j)
767  {
768  v.push_back(*(e->GetVertex(j)));
769  ASSERTL1(m_mesh->m_vertexNormals.count(v[j].m_id) != 0,"Normal has not been defined");
770  vN.push_back(m_mesh->m_vertexNormals[v[j].m_id]);
771  }
772 
773  vector<Node> tmp (nV);
774  vector<double> r (nV);
775  vector<Node> K (nV);
776  vector<Node> Q (nV);
777  vector<Node> Qp (nV);
778  vector<double> blend(nV);
779  vector<Node> out (nquad);
780 
781  // Calculate segment length for 2D spherigon routine.
782  double segLength = sqrt((v[0] - v[1]).abs2());
783 
784  // Perform Spherigon method to smooth manifold.
785  for (int j = 0; j < nquad; ++j)
786  {
787  Node P(0, x[j], y[j], z[j]);
788  Node N(0,0,0,0);
789 
790  // Calculate generalised barycentric coordinates r[] and the
791  // Phong normal N = vN . r for this point of the element.
792  if (m_mesh->m_spaceDim == 2)
793  {
794  // In 2D the coordinates are given by a ratio of the
795  // segment length to the distance from one of the
796  // endpoints.
797  r[0] = sqrt((P - v[0]).abs2()) / segLength;
798  r[0] = max(min(1.0, r[0]), 0.0);
799  r[1] = 1.0 - r[0];
800 
801  // Calculate Phong normal.
802  N = vN[0]*r[0] + vN[1]*r[1];
803  }
804  else if (m_mesh->m_spaceDim == 3)
805  {
806  for (int k = 0; k < nV; ++k)
807  {
808  tmp[k] = P - v[k];
809  }
810 
811  // Calculate generalized barycentric coordinate system
812  // (see equation 6 of paper).
813  double weight = 0.0;
814  for (int k = 0; k < nV; ++k)
815  {
816  r[k] = 1.0;
817  for (int l = 0; l < nV-2; ++l)
818  {
819  r[k] *= CrossProdMag(tmp[(k+l+1) % nV],
820  tmp[(k+l+2) % nV]);
821  }
822  weight += r[k];
823  }
824 
825  // Calculate Phong normal (equation 1).
826  for (int k = 0; k < nV; ++k)
827  {
828  r[k] /= weight;
829  N += vN[k]*r[k];
830  }
831  }
832 
833  // Normalise Phong normal.
834  N /= sqrt(N.abs2());
835 
836  for (int k = 0; k < nV; ++k)
837  {
838  // Perform steps denoted in equations 2, 3, 8 for C1
839  // smoothing.
840  double tmp1;
841  K [k] = P+N*((v[k]-P).dot(N));
842  tmp1 = (v[k]-K[k]).dot(vN[k]) / (1.0 + N.dot(vN[k]));
843  Q [k] = K[k] + N*tmp1;
844  Qp[k] = v[k] - N*((v[k]-P).dot(N));
845  }
846 
847  // Apply C1 blending function to the surface. TODO: Add
848  // option to do (more efficient) C0 blending function.
849  SuperBlend(r, Qp, P, blend);
850  P.m_x = P.m_y = P.m_z = 0.0;
851 
852  // Apply blending (equation 4).
853  for (int k = 0; k < nV; ++k)
854  {
855  P += Q[k]*blend[k];
856  }
857 
858  out[j] = P;
859  }
860 
861  // Push nodes into lines - TODO: face interior nodes.
862  // offset = 0 (seg), 1 (tri) or 2 (quad)
863  int offset = (int)e->GetConf().m_e-2;
864 
865  for (int edge = 0; edge < e->GetEdgeCount(); ++edge)
866  {
867  eIt = visitedEdges.find(e->GetEdge(edge)->m_id);
868  if (eIt == visitedEdges.end())
869  {
870  bool reverseEdge = !(v[vertMap[offset][edge][0]] ==
871  *(e->GetEdge(edge)->m_n1));
872 
873  // Clear existing curvature.
874  e->GetEdge(edge)->m_edgeNodes.clear();
875 
876  if (e->GetConf().m_e != LibUtilities::eTriangle)
877  {
878  for (int j = 1; j < nq-1; ++j)
879  {
880  int v = edgeMap[offset][edge][0] +
881  j*edgeMap[offset][edge][1];
882  e->GetEdge(edge)->m_edgeNodes.push_back(
883  NodeSharedPtr(new Node(out[v])));
884  }
885  }
886  else
887  {
888  for (int j = 0; j < nq-2; ++j)
889  {
890  int v = 3 + edge*(nq-2) + j;
891  e->GetEdge(edge)->m_edgeNodes.push_back(
892  NodeSharedPtr(new Node(out[v])));
893  }
894  }
895 
896  if (reverseEdge)
897  {
898  reverse(e->GetEdge(edge)->m_edgeNodes.begin(),
899  e->GetEdge(edge)->m_edgeNodes.end());
900  }
901 
902  e->GetEdge(edge)->m_curveType =
904 
905  visitedEdges.insert(e->GetEdge(edge)->m_id);
906  }
907  }
908 
909  // Add face nodes in manifold and full 3D case, but not for 2D.
910  if (m_mesh->m_spaceDim == 3)
911  {
912  vector<NodeSharedPtr> volNodes;
913 
914  if (e->GetConf().m_e == LibUtilities::eQuadrilateral)
915  {
916  volNodes.resize((nq-2)*(nq-2));
917  for (int j = 1; j < nq-1; ++j)
918  {
919  for (int k = 1; k < nq-1; ++k)
920  {
921  int v = j*nq+k;
922  volNodes[(j-1)*(nq-2)+(k-1)] =
923  NodeSharedPtr(new Node(out[v]));
924  }
925  }
926  }
927  else
928  {
929  for (int j = 3+3*(nq-2); j < nquad; ++j)
930  {
931  volNodes.push_back(NodeSharedPtr(new Node(out[j])));
932  }
933  }
934 
935  e->SetVolumeNodes(volNodes);
936  }
937  }
938 
939  // Copy face nodes back into 3D element faces.
940  if (m_mesh->m_expDim == 3)
941  {
942  set<pair<int,int> >::iterator it;
943  int elmt = 0;
944  for (it = m_mesh->m_spherigonSurfs.begin();
945  it != m_mesh->m_spherigonSurfs.end (); ++it, ++elmt)
946  {
947  FaceSharedPtr f = m_mesh->m_element[m_mesh->m_expDim][it->first]->
948  GetFace(it->second);
949 
950  f->m_faceNodes = el[elmt]->GetVolumeNodes();
951  f->m_curveType = f->m_vertexList.size() == 3 ?
954  }
955  }
956 
957  if (normalsGenerated)
958  {
959  m_mesh->m_vertexNormals.clear();
960  }
961  }
962  }
963 }