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.H
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 *******************************************************************************/
29 #ifndef NEMOSYS_CONVEXCONTAINER_H_
30 #define NEMOSYS_CONVEXCONTAINER_H_
31 
32 // Nemosys headers
33 #include "nemosys_export.h"
34 
35 #include <iostream>
36 #include <iterator>
37 #include <fstream>
38 #include <cstddef>
39 #include <vector>
40 #include <map>
41 #include <cassert>
42 #include <cmath>
43 
44 #include <QuickHull.hpp>
45 #include <Structs/Vector3.hpp>
46 
47 // Vector struct used for convexContainer object
48 namespace NEM {
49 
50 namespace MTH {
51 
52 // TODO: move this to math elementaries
53 
54 struct Vector {
55  double x, y, z;
56 
58  {
59  return Vector{x - p.x, y - p.y, z - p.z};
60  }
61 
62  Vector cross(Vector p) const
63  {
64  return Vector{
65  y * p.z - p.y * z,
66  z * p.x - p.z * x,
67  x * p.y - p.x * y
68  };
69  }
70 
71  double dot(Vector p) const
72  {
73  return x * p.x + y * p.y + z * p.z;
74  }
75 
76  double norm() const
77  {
78  return std::sqrt(x*x + y*y + z*z);
79  }
80 };
81 
82 } // namespace GEO
83 
84 } // namespace NEM
85 
86 
87 
88 namespace NEM {
89 
90 namespace GEO {
91 
92 
93 struct Face {
94  std::vector<NEM::MTH::Vector> v;
95 
97  {
98  assert(v.size() > 2);
99  NEM::MTH::Vector dir1 = v[1] - v[0];
100  NEM::MTH::Vector dir2 = v[2] - v[0];
101  NEM::MTH::Vector n = dir1.cross(dir2);
102  double d = n.norm();
103  return NEM::MTH::Vector{n.x / d, n.y / d, n.z / d};
104  }
105 };
106 
107 /** @brief An implementation of convex container object for point
108  The class implements methods to generate a convex hull for
109  a point cloud, perform point location, and output to STL
110  format.
111  @note Only use for convex objects.
112 **/
113 class NEMOSYS_EXPORT convexContainer
114 {
115 
116  public:
117  /** @brief default constructor
118  **/
119  convexContainer();
120 
121  /** @brief construct from a vector point coordinates
122  @param verts vector of vector of doubles of point coordinats
123  **/
124  convexContainer(std::vector<std::vector<double> >& verts);
125  convexContainer(std::vector<quickhull::Vector3<double> >& verts);
126 
127  virtual ~convexContainer() {};
128 
129  /** @brief re-/setting vertice coordinates
130  @param verts vector of vector of doubles of point coordinats
131  **/
132  virtual void setVertex(std::vector<std::vector<double> >& verts);
133 
134 
135  /** @brief calculating the convex hull
136  **/
137  virtual void computeConvexHull();
138 
139 
140  /** @brief check for point containment
141  @param p vector of coordinates of the point
142  **/
143  bool isInConvexPoly(NEM::MTH::Vector const& p);
144  bool isInConvexPoly(const std::vector<double>& p);
145 
146 
147  /** @brief generate STL triangulation from the convex hull
148  @param file_name full path to the output STL file
149  **/
150  void toSTL(std::string file_name) const;
151 
152  private:
153  bool _isReady;
154  std::vector<quickhull::Vector3<double> > vrts;
155  quickhull::QuickHull<double>* qHull;
156  std::vector<Face> fv;
157 
158 };
159 
160 
161 } // namespace GEO
162 
163 } // namespace NEM
164 
165 #endif // NEMOSYS_CONVEXCONTAINER_H_
double norm() const
std::vector< NEM::MTH::Vector > v
double dot(Vector p) const
Vector operator-(Vector p) const
std::vector< quickhull::Vector3< double > > vrts
std::vector< Face > fv
An implementation of convex container object for point The class implements methods to generate a con...
Vector cross(Vector p) const
quickhull::QuickHull< double > * qHull
NEM::MTH::Vector normal() const