Salome HOME
Update copyrights
[tools/medcoupling.git] / src / INTERP_KERNEL / Geometric2D / InterpKernelGeo2DElementaryEdge.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "InterpKernelGeo2DElementaryEdge.hxx"
22 #include "InterpKernelException.hxx"
23 #include "InterpKernelGeo2DEdge.hxx"
24 #include "InterpKernelGeo2DComposedEdge.hxx"
25
26 using namespace INTERP_KERNEL;
27
28 ElementaryEdge::ElementaryEdge(const ElementaryEdge& other):_direction(other._direction),_ptr(other._ptr)
29 {
30   _ptr->incrRef(); 
31 }
32
33 ElementaryEdge::~ElementaryEdge()
34 {
35   if(_ptr)
36     _ptr->decrRef();
37 }
38
39 bool ElementaryEdge::isNodeIn(Node *n) const
40 {
41   return _ptr->getStartNode()==n || _ptr->getEndNode()==n;
42 }
43
44 /*!
45  * \b WARNING contrary to INTERP_KERNEL::Edge::getBarycenterOfZone method called,
46  * this one is cumulative.
47  */
48 void ElementaryEdge::getBarycenterOfZone(double *bary) const
49 {
50   double tmp[2];
51   _ptr->getBarycenterOfZone(tmp);
52   if(_direction)
53     {
54       bary[0]+=tmp[0];
55       bary[1]+=tmp[1];
56     }
57   else
58     {
59       bary[0]-=tmp[0];
60       bary[1]-=tmp[1];
61     }
62 }
63
64 void ElementaryEdge::fillBounds(Bounds& output) const
65 {
66   output.aggregate(_ptr->getBounds());
67 }
68
69 void ElementaryEdge::getAllNodes(std::set<Node *>& output) const
70 {
71   output.insert(_ptr->getStartNode());
72   output.insert(_ptr->getEndNode());
73 }
74
75 bool ElementaryEdge::hasSameExtremities(const ElementaryEdge& other) const
76 {
77   std::set<Node *> s1, s2;
78   getAllNodes(s1);
79   other.getAllNodes(s2);
80   return (s1 == s2);
81 }
82
83 void ElementaryEdge::getBarycenter(double *bary, double& weigh) const
84 {
85   _ptr->getBarycenter(bary);
86   weigh=_ptr->getCurveLength();
87 }
88
89 ElementaryEdge *ElementaryEdge::clone() const
90 {
91   return new ElementaryEdge(*this);
92 }
93
94 void ElementaryEdge::initLocations() const
95 {
96   _ptr->initLocs();
97 }
98
99 /*!
100  * WARNING use this method if and only if this is so that it is completely in/out/on of @param pol.
101  */
102 TypeOfEdgeLocInPolygon ElementaryEdge::locateFullyMySelf(const ComposedEdge& pol, TypeOfEdgeLocInPolygon precEdgeLoc) const
103 {
104   if(getLoc()!=FULL_UNKNOWN)
105     return getLoc();
106   //obvious cases
107   if(precEdgeLoc==FULL_IN_1)
108     {
109       if(getStartNode()->getLoc()==ON_1)
110         {
111           declareOut();
112           return getLoc();
113         }
114       else if(getStartNode()->getLoc()==IN_1 || getStartNode()->getLoc()==ON_TANG_1)
115         {
116           declareIn();
117           return getLoc();
118         }
119     }
120   if(precEdgeLoc==FULL_OUT_1)
121     {
122       if(getStartNode()->getLoc()==ON_1)
123         {
124           declareIn();
125           return getLoc();
126         }
127       else if(getStartNode()->getLoc()==IN_1 || getStartNode()->getLoc()==ON_TANG_1)
128         {
129           declareOut();
130           return getLoc();
131         }
132     }
133   if(getStartNode()->getLoc()==IN_1 || getEndNode()->getLoc()==IN_1)
134     {
135       declareIn();
136       return getLoc();
137     }
138   if(getStartNode()->getLoc()==OUT_1 || getEndNode()->getLoc()==OUT_1)
139     {
140       declareOut();
141       return getLoc();
142     }
143   //a seek is requested
144   return locateFullyMySelfAbsolute(pol);
145 }
146
147 TypeOfEdgeLocInPolygon ElementaryEdge::locateFullyMySelfAbsolute(const ComposedEdge& pol) const
148 {
149   Node *node=_ptr->buildRepresentantOfMySelf(); // build barycenter used to detect if the edge is IN or OUT
150   if(pol.isInOrOut(node))
151     declareIn(); 
152   else
153     declareOut();
154   node->decrRef();
155   return getLoc();
156 }
157
158 Node *ElementaryEdge::getEndNode() const
159
160   if(_direction)
161     return _ptr->getEndNode();
162   else return _ptr->getStartNode();
163 }
164
165 Node *ElementaryEdge::getStartNode() const
166 {
167   if(_direction)
168     return _ptr->getStartNode();
169   else 
170     return _ptr->getEndNode();
171 }
172
173 bool ElementaryEdge::changeEndNodeWith(Node *node) const
174 {
175   if(_direction)
176     return _ptr->changeEndNodeWith(node);
177   else 
178     return _ptr->changeStartNodeWith(node);
179 }
180
181 bool ElementaryEdge::changeStartNodeWith(Node *node) const
182 {
183   if(_direction)
184     return _ptr->changeStartNodeWith(node);
185   else 
186     return _ptr->changeEndNodeWith(node);
187 }
188
189 void ElementaryEdge::dumpInXfigFile(std::ostream& stream, int resolution, const Bounds& box) const
190 {
191   _ptr->dumpInXfigFile(stream,_direction,resolution,box);
192 }
193
194 void ElementaryEdge::dumpToCout(const std::map<INTERP_KERNEL::Node *,int>& mapp, int index) const
195 {
196   _ptr->dumpToCout(mapp, index);
197 }
198
199 bool ElementaryEdge::intresicEqual(const ElementaryEdge *other) const
200 {
201   return _ptr==other->_ptr;
202 }
203
204 bool ElementaryEdge::intresicEqualDirSensitive(const ElementaryEdge *other) const
205 {
206   return ( _direction==other->_direction ) && (_ptr==other->_ptr);
207 }
208
209 bool ElementaryEdge::intresincEqCoarse(const Edge *other) const
210 {
211   return _ptr==other;
212 }
213
214 bool ElementaryEdge::isEqual(const ElementaryEdge& other) const
215 {
216   return _ptr->isEqual(*other._ptr);
217 }
218
219 /*!
220  * Called by QuadraticPolygon::splitAbs method.
221  */
222 void ElementaryEdge::fillGlobalInfoAbs(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,
223                                        std::vector<int>& edgesThis, std::vector<double>& addCoo, std::map<INTERP_KERNEL::Node *,int> mapAddCoo) const
224 {
225   _ptr->fillGlobalInfoAbs(_direction,mapThis,mapOther,offset1,offset2,fact,baryX,baryY,edgesThis,addCoo,mapAddCoo);
226 }
227
228 /*!
229  * Called by QuadraticPolygon::splitAbs method. Close to ElementaryEdge::fillGlobalInfoAbs method expect that here edgesOther (that replace edgesThis) is here an in/out parameter that only contains nodes
230  * unsorted because the "other" mesh is not subdivided yet.
231  */
232 void ElementaryEdge::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,
233                                         short skipStartOrEnd,
234                                         std::vector<int>& edgesOther, std::vector<double>& addCoo, std::map<INTERP_KERNEL::Node *,int>& mapAddCoo) const
235 {
236   if (!_direction)
237     skipStartOrEnd *= -1;  // invert value - see QuadraticPolygon::splitAbs()
238   _ptr->fillGlobalInfoAbs2(mapThis,mapOther,offset1,offset2,fact,baryX,baryY,skipStartOrEnd,edgesOther,addCoo,mapAddCoo);
239 }
240
241 /*!
242  * This method builds from a \a start node, an \a end node and a direction a new ElementaryEdge.
243  */
244 ElementaryEdge *ElementaryEdge::BuildEdgeFromStartEndDir(bool direction, INTERP_KERNEL::Node *start, INTERP_KERNEL::Node *end)
245 {
246   Edge *ptr=Edge::BuildEdgeFrom(start,end);
247   return new ElementaryEdge(ptr,direction);
248 }