1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #ifndef __INTERPKERNELGEO2DEDGE_HXX__
22 #define __INTERPKERNELGEO2DEDGE_HXX__
24 #include "INTERPKERNELDefines.hxx"
25 #include "InterpKernelException.hxx"
26 #include "InterpKernelGeo2DBounds.hxx"
27 #include "InterpKernelGeo2DNode.hxx"
34 namespace INTERP_KERNEL
56 } TypeOfLocInEdge; //see Edge::OFFSET_FOR_TYPEOFLOCINEDGE
64 } TypeOfEdgeLocInPolygon;
66 class INTERPKERNEL_EXPORT MergePoints
71 //methods called during intersection edge-edge
72 void start1Replaced();
74 void start1OnStart2();
78 //methods to be called during aggregation
79 bool isStart1(unsigned rk) const;
80 bool isEnd1(unsigned rk) const;
81 bool isStart2(unsigned rk) const;
82 bool isEnd2(unsigned rk) const;
84 unsigned getNumberOfAssociations() const;
85 void updateMergedNodes(int e1Start, int e1End, int e2Start, int e2End, std::map<int,int>& mergedNodes);
87 static void PushInMap(int key, int value, std::map<int,int>& mergedNodes);
89 unsigned _ass1Start1 : 1;
90 unsigned _ass1End1 : 1;
91 unsigned _ass1Start2 : 1;
92 unsigned _ass1End2 : 1;
93 unsigned _ass2Start1 : 1;
94 unsigned _ass2End1 : 1;
95 unsigned _ass2Start2 : 1;
96 unsigned _ass2End2 : 1;
102 * This class is in charge to store an intersection point as result of \b non oververlapping edge intersection.
103 * This class manages the cases when intersect element is one of the extrimities of edge1 and/or edge2.
105 class INTERPKERNEL_EXPORT IntersectElement
108 IntersectElement(double val1, double val2, bool start1, bool end1, bool start2, bool end2, Node *node, const Edge& e1, const Edge& e2, bool keepOrder);
109 IntersectElement(const IntersectElement& other);
110 //! The sort operator is done on the edge 1 \b not edge 2.
111 bool operator<(const IntersectElement& other) const;
112 IntersectElement& operator=(const IntersectElement& other);
113 double getVal1() const { return _chararct_val_for_e1; }
114 double getVal2() const { return _chararct_val_for_e2; }
115 //! idem operator< method except that the orientation is done on edge 2 \b not edge 1.
116 bool isLowerOnOther(const IntersectElement& other) const;
117 unsigned isOnExtrForAnEdgeAndInForOtherEdge() const;
118 void attachLoc() { _node->setLoc(_loc_of_node); }
119 bool isOnMergedExtremity() const;
120 bool isIncludedByBoth() const;
121 void setNode(Node *node) const;
122 void performMerging(MergePoints& commonNode) const;
123 Node *getNodeOnly() const { return _node; }
124 Node *getNodeAndReleaseIt() { Node *tmp=_node; _node=0; return tmp; }
131 double _chararct_val_for_e1;
132 double _chararct_val_for_e2;
134 TypeOfLocInPolygon _loc_of_node;
138 static const unsigned LIMIT_ALONE = 22;
139 static const unsigned LIMIT_ON = 73;
140 static const unsigned NO_LIMIT = 19;
144 * This abstract interface specifies all the methods to be overloaded of all possibilities edge-intersection.
146 class INTERPKERNEL_EXPORT EdgeIntersector
149 //! All non symetric methods are relative to 'e1'.
150 EdgeIntersector(const Edge& e1, const Edge& e2):_e1(e1),_e2(e2) { }
152 virtual ~EdgeIntersector() { }
153 virtual bool keepOrder() const = 0;
154 virtual bool areColinears() const = 0;
155 //!to call only if 'areOverlapped' have been set to true when areOverlappedOrOnlyColinears was called
156 virtual bool haveTheySameDirection() const = 0;
157 //!to call only if 'areOverlapped' have been set to true when areOverlappedOrOnlyColinears was called
158 virtual void getPlacements(Node *start, Node *end, TypeOfLocInEdge& whereStart, TypeOfLocInEdge& whereEnd, MergePoints& commonNode) const = 0;
159 //! When true is returned, newNodes should contains at least 1 element. All merging nodes betw _e1 and _e2 extremities must be done.
160 bool intersect(const Bounds *whereToFind, std::vector<Node *>& newNodes, bool& order, MergePoints& commonNode);
161 //! Should be called only once per association.
162 virtual void areOverlappedOrOnlyColinears(const Bounds *whereToFind, bool& obviousNoIntersection, bool& areOverlapped) = 0;
163 //! The size of returned vector is equal to number of potential intersections point. The values are so that their are interpretable by virtual Edge::isIn method.
164 virtual std::list< IntersectElement > getIntersectionsCharacteristicVal() const = 0;
166 void obviousCaseForCurvAbscisse(Node *node, TypeOfLocInEdge& where, MergePoints& commonNode, bool& obvious) const;
172 class INTERPKERNEL_EXPORT SameTypeEdgeIntersector : public EdgeIntersector
175 SameTypeEdgeIntersector(const Edge& e1, const Edge& e2):EdgeIntersector(e1,e2) { }
176 bool keepOrder() const { return true; }
179 class INTERPKERNEL_EXPORT CrossTypeEdgeIntersector : public EdgeIntersector
182 CrossTypeEdgeIntersector(const Edge& e1, const Edge& e2, bool reverse):EdgeIntersector(e1,e2),_reverse(reverse) { }
183 bool keepOrder() const { return _reverse; }
184 bool haveTheySameDirection() const { throw Exception("Cross type intersector is not supposed to deal with overlapped in cross type."); }
185 const Edge *myE1() { if(_reverse) return &_e1; else return &_e2; }
186 const Edge *myE2() { if(_reverse) return &_e2; else return &_e1; }
188 //! boolean to inform intersector that unsymetrics treatments reverse of e1 and e2 should be done.
197 * Deal with an oriented edge of a polygon.
198 * An Edge is defined with a start node, an end node and an equation of 1D curve.
199 * All other attributes are mutable because they don't impact these 3 invariant attributes.
200 * To be exact start and end nodes can change (address) but their location remain
201 * the same (at precision).
203 class INTERPKERNEL_EXPORT Edge
206 Edge(Node *start, Node *end, bool direction=true):_cnt(1),_loc(FULL_UNKNOWN) { if(direction) { _start=start; _end=end; } else { _start=end; _end=start; } _start->incrRef(); _end->incrRef(); }
207 Edge(double sX, double sY, double eX, double eY);
208 TypeOfEdgeLocInPolygon getLoc() const { return _loc; }
209 void incrRef() const { _cnt++; }
211 void initLocs() const { _loc=FULL_UNKNOWN; _start->initLocs(); _end->initLocs(); }
212 void declareOn() const;
213 void declareIn() const;
214 void declareOut() const;
215 void initHitStatus() const { _hit=false; }
216 bool getHitStatus() const { return _hit; }
217 void hitMeAlone(double xBary, double yBary, double dimChar) { _hit=true; applySimilarity(xBary,yBary,dimChar); }
218 void unHitMeAlone(double xBary, double yBary, double dimChar) { _hit=true; unApplySimilarity(xBary,yBary,dimChar); }
219 void hitMeAfter(double xBary, double yBary, double dimChar) { if(!_hit) hitMeAlone(xBary,yBary,dimChar); }
220 void unHitMeAfter(double xBary, double yBary, double dimChar) { if(!_hit) unHitMeAlone(xBary,yBary,dimChar); }
221 const Bounds& getBounds() const { return _bounds; }
222 void fillXfigStreamForLoc(std::ostream& stream) const;
223 Node *getNode(TypeOfLocInEdge where) const { if(where==START) return _start; else if(where==END) return _end; else return 0; }
224 Node *getStartNode() const { return _start; }
225 Node *getEndNode() const { return _end; }
226 void setEndNodeWithoutChange(Node *newEnd);
227 void setStartNodeWithoutChange(Node *newStart);
228 bool changeStartNodeWith(Node *otherStartNode) const;
229 bool changeStartNodeWithAndKeepTrack(Node *otherStartNode, std::vector<Node *>& track) const;
230 bool changeEndNodeWith(Node *otherEndNode) const;
231 bool changeEndNodeWithAndKeepTrack(Node *otherEndNode, std::vector<Node *>& track) const;
232 void addSubEdgeInVector(Node *start, Node *end, ComposedEdge& vec) const;
233 void getNormalVector(double *vectOutput) const;
234 static EdgeIntersector *BuildIntersectorWith(const Edge *e1, const Edge *e2);
235 static Edge *BuildFromXfigLine(std::istream& str);
236 static Edge *BuildEdgeFrom(Node *start, Node *end);
237 template<TypeOfMod4QuadEdge type>
238 static Edge *BuildEdgeFrom(Node *start, Node *middle, Node *end);
239 static Edge *BuildEdgeFrom3Points(const double *start, const double *middle, const double *end);
240 virtual void update(Node *m) = 0;
241 //! returns area between this and axe Ox delimited along Ox by _start and _end.
242 virtual double getAreaOfZone() const = 0;
243 //! apply a similiraty transformation on 'this'
244 virtual void applySimilarity(double xBary, double yBary, double dimChar);
245 //! apply the inverse similiraty transformation on 'this'
246 virtual void unApplySimilarity(double xBary, double yBary, double dimChar);
247 //! return the length of arc. Value is always > 0. !
248 virtual double getCurveLength() const = 0;
249 virtual void getBarycenter(double *bary) const = 0;
250 virtual void getBarycenterOfZone(double *bary) const = 0;
251 virtual void getMiddleOfPoints(const double *p1, const double *p2, double *mid) const = 0;
252 //! Retrieves a point that is owning to this, well placed for IN/OUT detection of this. Typically midlle of this is returned.
253 virtual Node *buildRepresentantOfMySelf() const = 0;
254 //! Given a magnitude specified by sub-type returns if in or not. See getCharactValue method.
255 virtual bool isIn(double characterVal) const = 0;
256 //! With the same magnitude as defined in 'isIn' method perform a compararison. Precondition : val1 and val2 are different and exactly INSIDE this.
257 virtual bool isLower(double val1, double val2) const = 0;
258 //! node is expected to lay on 'this'. It returns a characteristic magnitude usable by isIn method.
259 virtual double getCharactValue(const Node& node) const = 0;
260 //! node is expected to lay on 'this'. It returns a characteristic magnitude between 0 and 1.
261 virtual double getCharactValueBtw0And1(const Node& node) const = 0;
262 //! retrieves the distance to this : The min distance from pt and any point of this.
263 virtual double getDistanceToPoint(const double *pt) const = 0;
264 //! return if node with coords 'coordOfNode' is on this (with precision).
265 virtual bool isNodeLyingOn(const double *coordOfNode) const = 0;
266 virtual TypeOfFunction getTypeOfFunc() const = 0;
267 virtual void dynCastFunction(const EdgeLin * &seg,
268 const EdgeArcCircle * &arcSeg) const = 0;
269 bool intersectWith(const Edge *other, MergePoints& commonNode,
270 ComposedEdge& outVal1, ComposedEdge& outVal2) const;
271 static bool IntersectOverlapped(const Edge *f1, const Edge *f2, EdgeIntersector *intersector, MergePoints& commonNode,
272 ComposedEdge& outValForF1, ComposedEdge& outValForF2);
273 static void Interpolate1DLin(const std::vector<double>& distrib1, const std::vector<double>& distrib2,
274 std::map<int, std::map<int,double> >& result);
275 virtual void dumpInXfigFile(std::ostream& stream, bool direction, int resolution, const Bounds& box) const = 0;
276 bool isEqual(const Edge& other) const;
278 bool sortSubNodesAbs(const double *coo, std::vector<int>& subNodes);
279 void sortIdsAbs(const std::vector<INTERP_KERNEL::Node *>& addNodes, const std::map<INTERP_KERNEL::Node *, int>& mapp1, const std::map<INTERP_KERNEL::Node *, int>& mapp2, std::vector<int>& edgesThis);
280 virtual void fillGlobalInfoAbs(bool direction, const std::map<INTERP_KERNEL::Node *,int>& mapThis, const std::map<INTERP_KERNEL::Node *,int>& mapOther, int offset1, int offset2, double fact, double baryX, double baryY,
281 std::vector<int>& edgesThis, std::vector<double>& addCoo, std::map<INTERP_KERNEL::Node *,int> mapAddCoo) const = 0;
282 virtual void fillGlobalInfoAbs2(const std::map<INTERP_KERNEL::Node *,int>& mapThis, const std::map<INTERP_KERNEL::Node *,int>& mapOther, int offset1, int offset2, double fact, double baryX, double baryY,
283 std::vector<int>& edgesOther, std::vector<double>& addCoo, std::map<INTERP_KERNEL::Node *,int>& mapAddCoo) const = 0;
284 virtual Edge *buildEdgeLyingOnMe(Node *start, Node *end, bool direction=true) const = 0;
286 Edge():_cnt(1),_loc(FULL_UNKNOWN),_start(0),_end(0) { }
288 static int CombineCodes(TypeOfLocInEdge code1, TypeOfLocInEdge code2);
289 static bool Intersect(const Edge *f1, const Edge *f2, EdgeIntersector *intersector, const Bounds *whereToFind, MergePoints& commonNode,
290 ComposedEdge& outValForF1, ComposedEdge& outValForF2);
291 //! The code 'code' is built by method combineCodes
292 static bool SplitOverlappedEdges(const Edge *e1, const Edge *e2, Node *nS, Node *nE, bool direction, int code,
293 ComposedEdge& outVal1, ComposedEdge& outVal2);
296 mutable unsigned char _cnt;
297 mutable TypeOfEdgeLocInPolygon _loc;
302 //In relation with max possible value of TypeOfLocInEdge.
303 static const int OFFSET_FOR_TYPEOFLOCINEDGE = 8;