NEMoSys  0.63.0
A modular, extensible resource with robust automated mesh generation, mesh quality analysis, adaptive mesh refinement, and data transfer between arbitrary meshes.
convexContainer.C
Go to the documentation of this file.
1 /*******************************************************************************
2 * Promesh *
3 * Copyright (C) 2022, IllinoisRocstar LLC. All rights reserved. *
4 * *
5 * Promesh is the property of IllinoisRocstar LLC. *
6 * *
7 * IllinoisRocstar LLC *
8 * Champaign, IL *
9 * www.illinoisrocstar.com *
10 * promesh@illinoisrocstar.com *
11 *******************************************************************************/
12 /*******************************************************************************
13 * This file is part of Promesh *
14 * *
15 * This version of Promesh is free software: you can redistribute it and/or *
16 * modify it under the terms of the GNU Lesser General Public License as *
17 * published by the Free Software Foundation, either version 3 of the License, *
18 * or (at your option) any later version. *
19 * *
20 * Promesh is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more *
23 * details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with this program. If not, see <https://www.gnu.org/licenses/>. *
27 * *
28 *******************************************************************************/
30 
31 #include<sstream>
32 #include<string.h>
33 
34 namespace NEM {
35 
36 namespace GEO {
37 
39  _isReady(false)
40 {}
41 
42 
43 convexContainer::convexContainer(std::vector<std::vector<double> >& inVrts)
44 {
45  // deep copying
46  setVertex(inVrts);
47 }
48 
49 convexContainer::convexContainer(std::vector<quickhull::Vector3<double> >& inVrts)
50 {
51  // deep copying
52  for (auto itr=inVrts.begin(); itr!=inVrts.end(); itr++)
53  vrts.emplace_back(*itr);
54 }
55 
56 void convexContainer::setVertex(std::vector<std::vector<double> >& inVrts)
57 {
58  // deep copying
59  for (auto itr=inVrts.begin(); itr!=inVrts.end(); itr++)
60  vrts.emplace_back((*itr)[0], (*itr)[1], (*itr)[2]);
61 }
62 
64 {
65  // computes/re-computes convex hull of the verices
66  std::vector<size_t> indxBuf;
67  quickhull::VertexDataSource<double> vrtBuf;
68  qHull = new quickhull::QuickHull<double>();
69  auto hull = qHull->getConvexHull(vrts, false, false, 1.e-14);
70  indxBuf = hull.getIndexBuffer();
71  vrtBuf = hull.getVertexBuffer();
72 
73  // converting to face-vector format
74  //std::cout << "Size of index buffer = "
75  // << indxBuf.size() << std::endl;
76 
77  // saving to vf
78  for (int it=0; it<indxBuf.size(); it+=3)
79  {
80  Face f;
81  for (int idx=0; idx<3; idx++)
82  {
83  NEM::MTH::Vector pnt;
84  pnt.x = (vrtBuf[indxBuf[it+idx]]).x;
85  pnt.y = (vrtBuf[indxBuf[it+idx]]).y;
86  pnt.z = (vrtBuf[indxBuf[it+idx]]).z;
87  f.v.push_back(pnt);
88  }
89  fv.push_back(f);
90  }
91 
92  _isReady = true;
93 
94 }
95 
97  // check readiness
98  if (!_isReady)
100 
101  // looping through faces
102  for (Face const& f : fv) {
103  NEM::MTH::Vector p2f = f.v[0] - p; // f.v[0] is an arbitrary point on f
104  double d = p2f.dot(f.normal());
105  d /= p2f.norm(); // for numeric stability
106 
107  constexpr double bound = -1e-15; // use 1e15 to exclude boundaries
108  if (d < bound)
109  return false;
110  }
111  return true;
112 }
113 
114 bool convexContainer::isInConvexPoly(const std::vector<double>& p) {
115  if (p.size() != 3)
116  throw;
117  NEM::MTH::Vector pnt;
118  pnt.x = p[0];
119  pnt.y = p[1];
120  pnt.z = p[2];
121  return(isInConvexPoly(pnt));
122 }
123 
124 void convexContainer::toSTL(std::string file_name) const {
125 
126  //ascii file
127  std::ofstream myfile;
128  myfile.open((file_name + ".stl").c_str(), std::ios::out | std::ios::binary);
129  myfile << "solid "
130  << file_name
131  << "\n";
132 
133  //write down every triangle
134  for (auto it = fv.begin(); it!=fv.end(); it++){
135 
136  //normal vector coordinates
137  NEM::MTH::Vector n = it->normal();
138  myfile << "facet normal ";
139  myfile << n.x << " " << n.y << " " << n.z << "\n";
140 
141  myfile << "\touter loop\n";
142  //vertex coordinates
143  for (int iv=0; iv<3; iv++)
144  {
145  myfile << "\t\tvertex "
146  << (it->v[iv]).x << " "
147  << (it->v[iv]).y << " "
148  << (it->v[iv]).z << "\n";
149  }
150  myfile << "\tendloop\n";
151  myfile << "endfacet\n";
152  }
153  myfile << "endsolid " << file_name << "\n";
154  myfile.close();
155 
156 }
157 
158 } // namespace GEO
159 
160 } // namespace NEM
double norm() const
std::vector< NEM::MTH::Vector > v
void toSTL(std::string file_name) const
generate STL triangulation from the convex hull
double dot(Vector p) const
virtual void computeConvexHull()
calculating the convex hull
std::vector< quickhull::Vector3< double > > vrts
convexContainer()
default constructor
std::vector< Face > fv
bool isInConvexPoly(NEM::MTH::Vector const &p)
check for point containment
virtual void setVertex(std::vector< std::vector< double > > &verts)
re-/setting vertice coordinates
quickhull::QuickHull< double > * qHull