Loading...
Searching...
No Matches
levelSetSegmentationObject.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25# ifndef __BITPIT_LEVELSET_SEGMENTATION_OBJECT_HPP__
26# define __BITPIT_LEVELSET_SEGMENTATION_OBJECT_HPP__
27
28#include "levelSetCartesianKernel.hpp"
29#include "levelSetBooleanObject.hpp"
30#include "levelSetBooleanObject.tpp"
31#include "levelSetCommon.hpp"
32#include "levelSetComplementObject.hpp"
33#include "levelSetComplementObject.tpp"
34#include "levelSetKernel.hpp"
35#include "levelSetObject.hpp"
36
37#include "bitpit_common.hpp"
38#include "bitpit_surfunstructured.hpp"
39#include "bitpit_volcartesian.hpp"
40#include "bitpit_voloctree.hpp"
41
42# include <array>
43# include <vector>
44# include <unordered_set>
45# include <unordered_map>
46
47namespace bitpit{
48
49class SurfaceSkdTree;
50
52
53public:
54 typedef SurfUnstructured::CellIterator SegmentIterator;
55 typedef SurfUnstructured::CellConstIterator SegmentConstIterator;
56
57 BITPIT_PUBLIC_API static const double DEFAULT_FEATURE_ANGLE;
58
64 LevelSetSegmentationSurfaceInfo(std::unique_ptr<const SurfUnstructured> &&surface, double featureAngle, LevelSetSurfaceSmoothing surfaceSmoothing = LevelSetSurfaceSmoothing::LOW_ORDER);
65
66 const SurfUnstructured & getSurface() const;
67 void setSurface(std::unique_ptr<const SurfUnstructured> &&surface, double featureAngle = DEFAULT_FEATURE_ANGLE);
68 void setSurface(const SurfUnstructured *surface, double featureAngle = DEFAULT_FEATURE_ANGLE);
69 void setSurface(const SurfUnstructured *surface, double featureAngle, LevelSetSurfaceSmoothing surfaceSmoothing);
70
71 const SurfaceSkdTree & getSearchTree() const;
72
73 double getFeatureAngle() const;
75
76 double evalDistance(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, bool signedDistance, std::array<double, 3> *distanceVector) const;
77 double evalDistance(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, bool signedDistance) const;
78 std::array<double, 3> evalDistanceVector(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr) const;
79
80 std::array<double, 3> evalNormal(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr) const;
81 std::array<double,3> evalPseudoNormal(const SurfUnstructured::CellConstIterator &segmentIterator, const double *lambda) const;
82
83 void evalProjection(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
84 void evalProjection(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint) const;
85 std::array<double, 3> evalProjection(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, double *lambda) const;
86
87private:
88 typedef std::pair<long, int> SegmentVertexKey;
89
90 const SurfUnstructured *m_surface;
91 std::unique_ptr<const SurfUnstructured> m_ownedSurface;
92 double m_featureAngle;
93 LevelSetSurfaceSmoothing m_surfaceSmoothing;
94
95 std::unique_ptr<SurfaceSkdTree> m_searchTree;
96
97 PiercedStorage<std::size_t> m_segmentVertexOffset;
98
99 mutable PiercedStorage<bool> m_segmentNormalsValid;
100 mutable PiercedStorage<std::array<double,3>> m_segmentNormalsStorage;
101 mutable PiercedStorage<bool> m_unlimitedVertexNormalsValid;
102 mutable PiercedStorage<std::array<double,3>> m_unlimitedVertexNormalsStorage;
103 mutable std::vector<bool> m_limitedSegmentVertexNormalValid;
104 mutable std::unordered_map<SegmentVertexKey, std::array<double,3>, utils::hashing::hash<SegmentVertexKey>> m_limitedSegmentVertexNormalStorage;
105
106 void evalProjectionOnVertex(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
107 void evalHighOrderProjectionOnLine(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
108 void evalHighOrderProjectionOnTriangle(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
109 void evalHighOrderProjectionOnPolygon(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
110 void evalHighOrderProjection(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
111
112 void evalLowOrderProjectionOnLine(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
113 void evalLowOrderProjectionOnTriangle(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
114 void evalLowOrderProjectionOnPolygon(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
115 void evalLowOrderProjection(const std::array<double, 3> &point, const SegmentConstIterator &segmentItr, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
116
117 std::array<double,3> computePseudoNormal( const SurfUnstructured::CellConstIterator &segmentIterator, const double *lambda ) const;
118 std::array<double,3> computeSurfaceNormal( const SurfUnstructured::CellConstIterator &segmentIterator, const double *lambda ) const;
119
120 std::array<double,3> computeSegmentNormal( const SurfUnstructured::CellConstIterator &segmentIterator ) const;
121 std::array<double,3> computeSegmentEdgeNormal(const SegmentConstIterator &segmentItr, int edge, bool limited ) const;
122 std::array<double,3> computeSegmentVertexNormal( const SurfUnstructured::CellConstIterator &segmentIterator, int vertex, bool limited ) const;
123
124};
125
126class LevelSetSegmentationBaseObject : public LevelSetObject {
127
128public:
129 BITPIT_PUBLIC_API static const double AUTOMATIC_SEARCH_RADIUS;
130
131 friend class LevelSetBooleanObject<LevelSetSegmentationBaseObject>;
132
134
135 LevelSetFieldset getSupportedFields() const override;
136
137 const SurfUnstructured & evalCellSurface(long id) const;
138 long evalCellSupport(long id, double searchRadius = AUTOMATIC_SEARCH_RADIUS) const;
139 int evalCellPart(long id) const;
140 std::array<double,3> evalCellNormal(long id, bool signedLevelSet) const;
141
142 const SurfUnstructured & evalSurface(const std::array<double,3> &point) const;
143 long evalSupport(const std::array<double,3> &point) const;
144 long evalSupport(const std::array<double,3> &point, double searchRadius) const;
145 int evalPart(const std::array<double,3> &point) const;
146 std::array<double,3> evalNormal(const std::array<double,3> &point, bool signedLevelSet) const;
147 void evalProjection(const std::array<double,3> &point, bool signedLevelSet, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
148
149 BITPIT_DEPRECATED(int getPart(long cellId) const);
150 BITPIT_DEPRECATED(std::array<double BITPIT_COMMA 3> getNormal(long cellId) const);
151 BITPIT_DEPRECATED(long getSupport(long cellId) const);
152 BITPIT_DEPRECATED(double getSurfaceFeatureSize(long cellId) const);
153
154 BITPIT_DEPRECATED(int getPart(const std::array<double,3> &point) const);
155 BITPIT_DEPRECATED(std::array<double BITPIT_COMMA 3> getNormal(const std::array<double,3> &point) const);
156 BITPIT_DEPRECATED(long getSupport(const std::array<double,3> &point) const);
157 BITPIT_DEPRECATED(double getSurfaceFeatureSize(const std::array<double,3> &point) const);
158
159protected:
160 LevelSetSegmentationBaseObject(int, const LevelSetSegmentationSurfaceInfo *surfaceInfo);
161
163 std::size_t createFieldCellCache(LevelSetField field, std::size_t cacheId = CellCacheCollection::NULL_CACHE_ID) override;
164 void fillFieldCellCache(LevelSetField field, long id) override;
166
168 virtual LevelSetIntersectionStatus _isInterfaceIntersected(long id, bool invert, std::array<std::array<double, 3>, 2> *intersection, std::vector<std::array<double, 3>> *polygon) const override;
169
170 virtual const SurfUnstructured & _evalCellSurface(long id) const = 0;
171 virtual int _evalCellPart(long id) const;
172 virtual std::array<double,3> _evalCellNormal(long id, bool signedLevelSet) const = 0;
173 virtual long _evalCellSupport(long id, double searchRadius = AUTOMATIC_SEARCH_RADIUS) const = 0;
174
175 virtual const SurfUnstructured & _evalSurface(const std::array<double,3> &point) const = 0;
176 virtual int _evalPart(const std::array<double,3> &point) const;
177 virtual long _evalSupport(const std::array<double,3> &point) const = 0;
178 virtual long _evalSupport(const std::array<double,3> &point, double searchRadius) const = 0;
179 virtual void _evalProjection(const std::array<double,3> &point, bool signedLevelSet, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const = 0;
180
181 void addVTKOutputData(LevelSetField field, const std::string &objectName) override;
182 std::string getVTKOutputFieldName(LevelSetField field) const override;
183 void flushVTKOutputData(std::fstream &stream, VTKFormat format, LevelSetField field) const override;
185};
186
187class LevelSetSegmentationObject : public LevelSetSegmentationBaseObject {
188
189public:
191 LevelSetSegmentationObject(int, std::unique_ptr<const SurfUnstructured> &&surface, double featureAngle = 2. * BITPIT_PI);
192 LevelSetSegmentationObject(int, const SurfUnstructured *surface, double featureAngle = 2. * BITPIT_PI);
193 LevelSetSegmentationObject(int, const SurfUnstructured *surface, double featureAngle, LevelSetSurfaceSmoothing surfaceSmoothing);
196
197 bool empty() const override;
198
199 LevelSetSegmentationObject * clone() const override;
200
201 const SurfUnstructured & getSurface() const;
202 void setSurface(std::unique_ptr<const SurfUnstructured> &&surface, bool force = false);
203 void setSurface(std::unique_ptr<const SurfUnstructured> &&surface, double featureAngle, bool force = false);
204 void setSurface(const SurfUnstructured *surface, bool force = false);
205 void setSurface(const SurfUnstructured *surface, double featureAngle, bool force = false);
206 void setSurface(const SurfUnstructured *surface, double featureAngle, LevelSetSurfaceSmoothing surfaceSmoothing, bool force = false);
207
208 const SurfaceSkdTree & getSearchTree() const;
209
210 double getFeatureAngle() const;
212
215
216protected:
217 void fillCellLocationCache() override;
218 void fillCellLocationCache(const std::vector<adaption::Info> &adaptionData) override;
220
221 short _evalCellSign(long id) const override;
222 double _evalCellValue(long id, bool signedLevelSet) const override;
223 std::array<double,3> _evalCellGradient(long id, bool signedLevelSet) const override;
224 const SurfUnstructured & _evalCellSurface(long id) const override;
225 long _evalCellSupport(long id, double searchRadius = AUTOMATIC_SEARCH_RADIUS) const override;
226 std::array<double,3> _evalCellNormal(long id, bool signedLevelSet) const override;
227
228 short _evalSign(const std::array<double,3> &point) const override;
229 double _evalValue(const std::array<double,3> &point, bool signedLevelSet) const override;
230 std::array<double,3> _evalGradient(const std::array<double,3> &point, bool signedLevelSet) const override;
231 const SurfUnstructured & _evalSurface(const std::array<double,3> &point) const override;
232 long _evalSupport(const std::array<double,3> &point) const override;
233 long _evalSupport(const std::array<double,3> &point, double searchRadius) const override;
234 void _evalProjection(const std::array<double,3> &point, bool signedLevelSet, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const override;
235
236 LevelSetIntersectionStatus _isInterfaceIntersected(long id, bool invert, std::array<std::array<double, 3>, 2> *intersection, std::vector<std::array<double, 3>> *polygon) const override;
237
238private:
239 std::unique_ptr<LevelSetSegmentationSurfaceInfo> m_surfaceInfo;
240
241 void fillCartesianCellZoneCache();
242
243 short _evalSign(const std::array<double,3> &point, long support) const;
244 double _evalValue(const std::array<double,3> &point, long support, bool signedLevelSet) const;
245 std::array<double,3> _evalGradient(const std::array<double,3> &point, long support, bool signedLevelSet) const;
246 void _evalProjection(const std::array<double,3> &point, long support, bool signedLevelSet, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const;
247
248};
249
250template<>
252
253public:
255 LevelSetBooleanObject(int, LevelSetBooleanOperation, const std::vector<const LevelSetSegmentationBaseObject *> &);
256
258
259protected:
260 const SurfUnstructured & _evalCellSurface(long id) const override;
261 long _evalCellSupport(long id, double searchRadius = AUTOMATIC_SEARCH_RADIUS) const override;
262 int _evalCellPart(long id) const override;
263 std::array<double,3> _evalCellNormal(long id, bool signedLevelSet) const override;
264
265 const SurfUnstructured & _evalSurface(const std::array<double,3> &point) const override;
266 long _evalSupport(const std::array<double,3> &point) const override;
267 long _evalSupport(const std::array<double,3> &point, double searchRadius) const override;
268 int _evalPart(const std::array<double,3> &point) const override;
269 void _evalProjection(const std::array<double,3> &point, bool signedLevelSet, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const override;
270
271};
272
273template<>
275
276public:
278
280
281protected:
282 const SurfUnstructured & _evalCellSurface(long id) const override;
283 long _evalCellSupport(long id, double searchRadius = AUTOMATIC_SEARCH_RADIUS) const override;
284 int _evalCellPart(long id) const override;
285 std::array<double,3> _evalCellNormal(long id, bool signedLevelSet) const override;
286
287 const SurfUnstructured & _evalSurface(const std::array<double,3> &point) const override;
288 long _evalSupport(const std::array<double,3> &point) const override;
289 long _evalSupport(const std::array<double,3> &point, double searchRadius) const override;
290 int _evalPart(const std::array<double,3> &point) const override;
291 void _evalProjection(const std::array<double,3> &point, bool signedLevelSet, std::array<double, 3> *projectionPoint, std::array<double, 3> *projectionNormal) const override;
292
293};
294
295// Typedefs for compatibility with older versions
296typedef LevelSetSegmentationObject LevelSetSegmentation;
297
298}
299
300#endif
LevelSetBooleanBaseObject(int, LevelSetBooleanOperation, const LevelSetSegmentationBaseObject *, const LevelSetSegmentationBaseObject *)
LevelSetBooleanObject< LevelSetSegmentationBaseObject > * clone() const override
const SurfUnstructured & _evalSurface(const std::array< double, 3 > &point) const override
long _evalCellSupport(long id, double searchRadius=AUTOMATIC_SEARCH_RADIUS) const override
LevelSetBooleanObject(int, LevelSetBooleanOperation, const LevelSetSegmentationBaseObject *, const LevelSetSegmentationBaseObject *)
int _evalPart(const std::array< double, 3 > &point) const override
long _evalSupport(const std::array< double, 3 > &point) const override
void _evalProjection(const std::array< double, 3 > &point, bool signedLevelSet, std::array< double, 3 > *projectionPoint, std::array< double, 3 > *projectionNormal) const override
std::array< double, 3 > _evalCellNormal(long id, bool signedLevelSet) const override
Class which deals with boolean operation between two LevelSetObjects.
LevelSetComplementBaseObject(int id, const LevelSetSegmentationBaseObject *source)
LevelSetComplementObject< LevelSetSegmentationBaseObject > * clone() const override
long _evalCellSupport(long id, double searchRadius=AUTOMATIC_SEARCH_RADIUS) const override
long _evalSupport(const std::array< double, 3 > &point) const override
void _evalProjection(const std::array< double, 3 > &point, bool signedLevelSet, std::array< double, 3 > *projectionPoint, std::array< double, 3 > *projectionNormal) const override
LevelSetComplementObject(int id, const LevelSetSegmentationBaseObject *source)
const SurfUnstructured & _evalSurface(const std::array< double, 3 > &point) const override
int _evalPart(const std::array< double, 3 > &point) const override
std::array< double, 3 > _evalCellNormal(long id, bool signedLevelSet) const override
void fillFieldCellCache(LevelSetField field, const std::vector< long > &cellIds)
virtual void flushVTKOutputData(std::fstream &stream, VTKFormat format, LevelSetField field) const
virtual std::size_t createFieldCellCache(LevelSetField field, std::size_t cacheId=CellCacheCollection::NULL_CACHE_ID)
Implements visitor pattern fo segmentated geometries.
void evalProjection(const std::array< double, 3 > &point, bool signedLevelSet, std::array< double, 3 > *projectionPoint, std::array< double, 3 > *projectionNormal) const
void fillFieldCellCache(LevelSetField field, long id) override
void flushVTKOutputData(std::fstream &stream, VTKFormat format, LevelSetField field) const override
std::array< double, 3 > evalNormal(const std::array< double, 3 > &point, bool signedLevelSet) const
const SurfUnstructured & evalSurface(const std::array< double, 3 > &point) const
LevelSetFieldset getSupportedFields() const override
std::array< double BITPIT_COMMA 3 > getNormal(long cellId) const
virtual int _evalPart(const std::array< double, 3 > &point) const
void addVTKOutputData(LevelSetField field, const std::string &objectName) override
std::array< double, 3 > evalCellNormal(long id, bool signedLevelSet) const
std::size_t createFieldCellCache(LevelSetField field, std::size_t cacheId=CellCacheCollection::NULL_CACHE_ID) override
LevelSetIntersectionStatus _isCellIntersected(long, double distance, LevelSetIntersectionMode=LevelSetIntersectionMode::FAST_FUZZY) const override
int evalPart(const std::array< double, 3 > &point) const
long evalCellSupport(long id, double searchRadius=AUTOMATIC_SEARCH_RADIUS) const
static BITPIT_PUBLIC_API const double AUTOMATIC_SEARCH_RADIUS
virtual LevelSetIntersectionStatus _isInterfaceIntersected(long id, bool invert, std::array< std::array< double, 3 >, 2 > *intersection, std::vector< std::array< double, 3 > > *polygon) const override
long evalSupport(const std::array< double, 3 > &point) const
const SurfUnstructured & evalCellSurface(long id) const
std::string getVTKOutputFieldName(LevelSetField field) const override
Implements visitor pattern fo segmentated geometries.
const SurfUnstructured & _evalSurface(const std::array< double, 3 > &point) const override
LevelSetSegmentationObject * clone() const override
long _evalCellSupport(long id, double searchRadius=AUTOMATIC_SEARCH_RADIUS) const override
LevelSetCellLocation fillCellGeometricNarrowBandLocationCache(long id) override
LevelSetIntersectionStatus _isInterfaceIntersected(long id, bool invert, std::array< std::array< double, 3 >, 2 > *intersection, std::vector< std::array< double, 3 > > *polygon) const override
long _evalSupport(const std::array< double, 3 > &point) const override
short _evalSign(const std::array< double, 3 > &point) const override
std::array< double, 3 > _evalGradient(const std::array< double, 3 > &point, bool signedLevelSet) const override
double _evalCellValue(long id, bool signedLevelSet) const override
std::array< double, 3 > _evalCellGradient(long id, bool signedLevelSet) const override
LevelSetSurfaceSmoothing getSurfaceSmoothing() const
double _evalValue(const std::array< double, 3 > &point, bool signedLevelSet) const override
void setSurface(std::unique_ptr< const SurfUnstructured > &&surface, bool force=false)
const SurfUnstructured & getSurface() const
std::array< double, 3 > _evalCellNormal(long id, bool signedLevelSet) const override
const SurfUnstructured & _evalCellSurface(long id) const override
void _evalProjection(const std::array< double, 3 > &point, bool signedLevelSet, std::array< double, 3 > *projectionPoint, std::array< double, 3 > *projectionNormal) const override
void evalProjection(const std::array< double, 3 > &point, const SegmentConstIterator &segmentItr, std::array< double, 3 > *projectionPoint, std::array< double, 3 > *projectionNormal) const
double evalDistance(const std::array< double, 3 > &point, const SegmentConstIterator &segmentItr, bool signedDistance, std::array< double, 3 > *distanceVector) const
std::array< double, 3 > evalDistanceVector(const std::array< double, 3 > &point, const SegmentConstIterator &segmentItr) const
void setSurface(std::unique_ptr< const SurfUnstructured > &&surface, double featureAngle=DEFAULT_FEATURE_ANGLE)
std::array< double, 3 > evalNormal(const std::array< double, 3 > &point, const SegmentConstIterator &segmentItr) const
static BITPIT_PUBLIC_API const double DEFAULT_FEATURE_ANGLE
LevelSetSurfaceSmoothing getSurfaceSmoothing() const
std::array< double, 3 > evalPseudoNormal(const SurfUnstructured::CellConstIterator &segmentIterator, const double *lambda) const
Metafunction for generating a pierced storage.
The SurfUnstructured class defines an unstructured surface triangulation.
The SurfaceSkdTree implements a Bounding Volume Hierarchy tree for surface patches.
VTKFormat
Definition VTK.hpp:92
#define BITPIT_DEPRECATED(func)
Definition compiler.hpp:87