Salome HOME
Fix MA construction
[modules/smesh.git] / src / SMESHUtils / SMESH_MAT2d.hxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File      : SMESH_MAT2d.hxx
23 // Created   : Thu May 28 17:49:53 2015
24 // Author    : Edward AGAPOV (eap)
25
26 #ifndef __SMESH_MAT2d_HXX__
27 #define __SMESH_MAT2d_HXX__
28
29 #include "SMESH_Utils.hxx"
30
31 #include <TopoDS_Face.hxx>
32 #include <TopoDS_Edge.hxx>
33
34 #include <vector>
35 #include <map>
36
37 #include <boost/polygon/polygon.hpp>
38 #include <boost/polygon/voronoi.hpp>
39
40 class Adaptor3d_Curve;
41
42 // Medial Axis Transform 2D
43 namespace SMESH_MAT2d
44 {
45   class MedialAxis; // MedialAxis is the entry point
46   class Branch;
47   class BranchEnd;
48   class Boundary;
49   struct BoundaryPoint;
50
51   typedef boost::polygon::voronoi_diagram<double> TVD;
52   typedef TVD::cell_type                          TVDCell;
53   typedef TVD::edge_type                          TVDEdge;
54   typedef TVD::vertex_type                        TVDVertex;
55
56   //-------------------------------------------------------------------------------------
57   // type of Branch end point
58   enum BranchEndType { BE_UNDEF,
59                        BE_ON_VERTEX, // branch ends at a convex VRTEX
60                        BE_BRANCH_POINT, // branch meats 2 or more other branches
61                        BE_END // branch end equidistant from several adjacent segments
62   };
63   //-------------------------------------------------------------------------------------
64   /*!
65    * \brief End point of MA Branch
66    */
67   struct SMESHUtils_EXPORT BranchEnd
68   {
69     const TVDVertex*             _vertex;
70     BranchEndType                _type;
71     std::vector< const Branch* > _branches;
72
73     BranchEnd(): _vertex(0), _type( BE_UNDEF ) {}
74   };
75   //-------------------------------------------------------------------------------------
76   /*!
77    * \brief Point on MA Branch
78    */
79   struct SMESHUtils_EXPORT BranchPoint
80   {
81     const Branch* _branch;
82     std::size_t   _iEdge; // MA edge index within the branch
83     double        _edgeParam; // normalized param within the MA edge
84
85     BranchPoint(): _branch(0), _iEdge(0), _edgeParam(-1) {}
86   };
87   //-------------------------------------------------------------------------------------
88   /*!
89    * \brief Branch is a set of MA edges enclosed between branch points and/or MA ends.
90    *        It's main feature is to return two BoundaryPoint's per a point on it.
91    *        Points on a Branch are defined by [0,1] parameter 
92    */
93   class SMESHUtils_EXPORT Branch
94   {
95   public:
96     bool getBoundaryPoints(double param, BoundaryPoint& bp1, BoundaryPoint& bp2 ) const;
97     bool getBoundaryPoints(std::size_t iMAEdge, double maEdgeParam,
98                            BoundaryPoint& bp1, BoundaryPoint& bp2 ) const;
99     bool getBoundaryPoints(const BranchPoint& p,
100                            BoundaryPoint& bp1, BoundaryPoint& bp2 ) const;
101     bool getParameter(const BranchPoint& p, double & u ) const;
102
103     std::size_t      nbEdges() const { return _maEdges.size(); }
104
105     const BranchEnd* getEnd(bool the2nd) const { return & ( the2nd ? _endPoint2 : _endPoint1 ); }
106
107     bool hasEndOfType(BranchEndType type) const;
108
109     void getPoints( std::vector< gp_XY >& points, const double scale[2]) const;
110
111     void getGeomEdges( std::vector< std::size_t >& edgeIDs1,
112                        std::vector< std::size_t >& edgeIDs2 ) const;
113
114     void getOppositeGeomEdges( std::vector< std::size_t >& edgeIDs1,
115                                std::vector< std::size_t >& edgeIDs2,
116                                std::vector< BranchPoint >& divPoints) const;
117
118     bool isRemoved() const { return _proxyPoint._branch; }
119
120   public: // internal: construction
121
122     void init( std::vector<const TVDEdge*>&                maEdges,
123                const Boundary*                             boundary,
124                std::map< const TVDVertex*, BranchEndType > endType);
125     void setBranchesToEnds( const std::vector< Branch >&   branches);
126     BranchPoint getPoint( const TVDVertex* vertex ) const;
127     void setRemoved( const BranchPoint& proxyPoint );
128
129     static void        setGeomEdge  ( std::size_t geomIndex, const TVDEdge* maEdge );
130     static std::size_t getGeomEdge  ( const TVDEdge* maEdge );
131     static void        setBndSegment( std::size_t segIndex, const TVDEdge* maEdge );
132     static std::size_t getBndSegment( const TVDEdge* maEdge );
133
134   private:
135
136     bool addDivPntForConcaVertex( std::vector< std::size_t >&        edgeIDs1,
137                                   std::vector< std::size_t >&        edgeIDs2,
138                                   std::vector< BranchPoint >&        divPoints,
139                                   const std::vector<const TVDEdge*>& maEdges,
140                                   const std::vector<const TVDEdge*>& maEdgesTwin,
141                                   size_t &                           i) const;
142
143     // association of _maEdges with boundary segments is stored in this way:
144     // index of an EDGE:           TVDEdge->cell()->color()
145     // index of a segment on EDGE: TVDEdge->color()
146     std::vector<const TVDEdge*> _maEdges; // MA edges ending at points located at _params
147     std::vector<double>  _params; // params of points on MA, normalized [0;1] within this branch
148     const Boundary*      _boundary; // face boundary
149     BranchEnd            _endPoint1;
150     BranchEnd            _endPoint2;
151     BranchPoint          _proxyPoint;
152   };
153
154   //-------------------------------------------------------------------------------------
155   /*!
156    * \brief Data of a discretized EDGE allowing to get a point on MA by a parameter on EDGE
157    */
158   struct BndPoints
159   {
160     std::vector< double > _params; // params of discretization points on an EDGE
161     std::vector< std::pair< const Branch*, int > > _maEdges; /* index of TVDEdge in branch;
162                                                                 index sign means orientation;
163                                                                 index == Branch->nbEdges() means
164                                                                 end point of a Branch */
165   };
166   //-------------------------------------------------------------------------------------
167   /*!
168    * \brief Face boundary is discretized so that each its segment to correspond to
169    *        an edge of MA
170    */
171   class SMESHUtils_EXPORT Boundary
172   {
173   public:
174
175     Boundary( std::size_t nbEdges ): _pointsPerEdge( nbEdges ) {}
176     BndPoints&  getPoints( std::size_t iEdge ) { return _pointsPerEdge[ iEdge ]; }
177     std::size_t nbEdges() const { return _pointsPerEdge.size(); }
178
179     bool getPoint( std::size_t iEdge, std::size_t iSeg, double u, BoundaryPoint& bp ) const;
180
181     bool getBranchPoint( const std::size_t iEdge, double u, BranchPoint& p ) const;
182
183     bool isConcaveSegment( std::size_t iEdge, std::size_t iSeg ) const;
184
185     bool moveToClosestEdgeEnd( BoundaryPoint& bp ) const;
186
187   private:
188     std::vector< BndPoints > _pointsPerEdge;
189   };
190
191   //-------------------------------------------------------------------------------------
192   /*!
193    * \brief Point on FACE boundary
194    */
195   struct SMESHUtils_EXPORT BoundaryPoint
196   {
197     std::size_t _edgeIndex; // index of an EDGE in a sequence passed to MedialAxis()
198     double      _param;     // parameter of this EDGE
199   };
200   //-------------------------------------------------------------------------------------
201   /*!
202    * \brief Medial axis (MA) is defined as the loci of centres of locally
203    *        maximal balls inside 2D representation of a face. This class
204    *        implements a piecewise approximation of MA.
205    */
206   class SMESHUtils_EXPORT MedialAxis
207   {
208   public:
209     MedialAxis(const TopoDS_Face&                face,
210                const std::vector< TopoDS_Edge >& edges,
211                const double                      minSegLen,
212                const bool                        ignoreCorners = false );
213     std::size_t                            nbBranches() const { return _nbBranches; }
214     const Branch*                          getBranch(size_t i) const;
215     const std::vector< const BranchEnd* >& getBranchPoints() const { return _branchPnt; }
216     const Boundary&                        getBoundary() const { return _boundary; }
217
218     void getPoints( const Branch* branch, std::vector< gp_XY >& points) const;
219     Adaptor3d_Curve* make3DCurve(const Branch& branch) const;
220
221   private:
222
223   private:
224     TopoDS_Face                     _face;
225     TVD                             _vd;
226     std::vector< Branch >           _branch;
227     std::size_t                     _nbBranches; // removed branches ignored
228     std::vector< const BranchEnd* > _branchPnt;
229     Boundary                        _boundary;
230     double                          _scale[2];
231   };
232
233 }
234
235 #endif