Salome HOME
Merge branch V7_3_1_BR
[modules/hexablock.git] / src / HEXABLOCK / HexVertex.cxx
1
2 //  C++ Les noeuds
3
4 // Copyright (C) 2009-2014  CEA/DEN, EDF R&D
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
23 #include "HexVertex.hxx"
24 #include "HexEdge.hxx"
25 #include "HexVector.hxx"
26
27 #include "HexXmlWriter.hxx"
28 #include "HexDocument.hxx"
29 #include "HexNewShape.hxx"
30 #include "HexVertexShape.hxx"
31 #include "HexKas_functions.hxx"
32
33 BEGIN_NAMESPACE_HEXA
34
35 static bool db =  on_debug ();  // == getenv ("HEXA_DB") > 0
36
37 // ====================================================== Constructeur
38 Vertex::Vertex  (Document* doc, double x, double y, double z)
39       : EltBase (doc, EL_VERTEX)
40 {
41    gc_x = v_x = x;
42    gc_y = v_y = y;
43    gc_z = v_z = z;
44
45    v_shape  = NULL;
46    v_scalar = 0;
47    v_clone  = NULL;
48
49    if (el_root != NULL)
50        el_root->addVertex (this);
51 }
52 // ========================================================= Constructeur bis
53 Vertex::Vertex (Vertex* other)
54       : EltBase (other->dad(), EL_VERTEX)
55 {
56    if (other!= NULL)
57       {
58       v_x = other->v_x;
59       v_y = other->v_y;
60       v_z = other->v_z;
61       v_scalar = other->v_scalar;
62       gc_x   = other->gc_x;
63       gc_y   = other->gc_y;
64       gc_z   = other->gc_z;
65       v_clone  = NULL;
66       v_shape  = other->v_shape;
67       }
68    else
69       {
70       v_x  = v_y  = v_z  = 0;
71       gc_x = gc_y = gc_z = 0;
72       v_scalar = 0;
73       v_shape  = NULL;
74       v_clone  = NULL;
75       }
76 }
77 // ========================================================= getParent
78 Edge* Vertex::getParent  (int nro)
79 {
80    return static_cast <Edge*> (getFather (nro));
81 }
82 // ========================================================= saveXml
83 void Vertex::saveXml  (XmlWriter* xml)
84 {
85    char buffer[12], coord[80];
86
87    sprintf (coord, "%g %g %g", v_x, v_y, v_z);
88
89    xml->openMark     ("Vertex");
90    xml->addAttribute ("id",    getName (buffer));
91    xml->addAttribute ("coord", coord);
92    if (el_name!=buffer)
93        xml->addAttribute ("name", el_name);
94    xml->closeMark ();
95
96    if (v_shape != NULL)
97       {
98       sprintf (coord, "%g %g %g", gc_x, gc_y, gc_z);
99       v_shape->callXml (xml);
100       xml->addAttribute ("coord", coord);
101       xml->closeMark ();
102       }
103
104 // if (el_assoc!=NULL) xml->addAttribute ("shape", el_assoc->getBrep().c_str());
105 }
106 // ========================================================= translate
107 void  Vertex::translate  (Vector* vecteur, double fact)
108 {
109     v_x += fact*vecteur->getDx ();
110     v_y += fact*vecteur->getDy ();
111     v_z += fact*vecteur->getDz ();
112 }
113 // ========================================================= createMiddle
114 Vertex* Vertex::createMiddle  (Vertex* left, Vertex* right)
115 {
116     Vertex* milieu = new Vertex (left);
117
118     milieu->v_x = (left->v_x + right->v_x) / 2;
119     milieu->v_y = (left->v_y + right->v_y) / 2;
120     milieu->v_z = (left->v_z + right->v_z) / 2;
121
122     return milieu;
123 }
124 // ========================================================= setAssociation
125 int Vertex::setAssociation (VertexShape* forme)
126 {
127    if (forme==NULL)
128       return HERR;
129
130    forme->addAssociation (this);
131    forme->getCoords (gc_x, gc_y, gc_z);
132    v_shape = forme;
133
134    if (db) cout << " Vertex "           << el_name
135                 << " setAssociation-> " << forme->getName ()
136                 << " = (" << gc_x << ", " << gc_y << ", " << gc_z << ") \n" ;
137
138    is_associated = true;
139    return HOK;
140 }
141 // ========================================================= setAssociation
142 int Vertex::setAssociation (NewShape* geom, int subid)
143 {
144    if (geom==NULL)
145       return HERR;
146
147    VertexShape* shape = geom->findVertex (subid);
148    if (shape==NULL)
149       return HERR;
150
151    int ier = setAssociation (shape);
152    return ier;
153 }
154 // ========================================================= setAssociation
155 int Vertex::setAssociation (double* point)
156 {
157    NewShape* cloud = el_root->getCloud();
158
159    int subid = cloud->addPoint (point);
160    int ier   = setAssociation  (cloud, subid);
161    return ier;
162 }
163 // ========================================================= setAssociation
164 int Vertex::setAssociation (double px, double py, double pz)
165 {
166    Real3 point = { px, py, pz };
167    int   ier   = setAssociation (point);
168    return ier;
169 }
170 // ========================================================= clearAssociation
171 void Vertex::clearAssociation ()
172 {
173    v_shape = NULL;
174    is_associated = false;
175 }
176 // ========================================================= getAssoCoord
177 void Vertex::getAssoCoord (double &px, double &py, double &pz)
178 {
179    if (is_associated)
180       {
181       px = gc_x;
182       py = gc_y;
183       pz = gc_z;
184       }
185    else
186       {
187       px = v_x;
188       py = v_y;
189       pz = v_z;
190       }
191 }
192 // ========================================================= getAssoCoord
193 void Vertex::getAssoCoord (double* point)
194 {
195    getAssoCoord (point[dir_x], point[dir_y], point[dir_z]);
196 }
197 // ===================================================== getCoord
198 double Vertex::getCoord (int dir)
199 {
200    double val = 0;
201    switch (dir)
202           {
203           case dir_x : val = v_x;
204                break;
205           case dir_y : val = v_y;
206                break;
207           case dir_z : val = v_z;
208                break;
209           }
210    return val;
211 }
212 // ========================================================= dump
213 void Vertex::dump ()
214 {
215    printName (" = ");
216    if (NOT isHere ())
217       {
218       printf ("(*** deleted ***)\n");
219       return;
220       }
221
222    printf ("(%g, %g, %g)", v_x,v_y,v_z);
223    dumpRef ();
224 }
225 // ========================================================= setCoord
226 void Vertex::setCoord (double x, double y, double z)
227 {
228    v_x = x;
229    v_y = y;
230    v_z = z;
231 }
232 // ========================================================= isin
233 bool Vertex::isin  (double xmin, double xmax, double ymin, double ymax,
234                                              double zmin, double zmax)
235 {
236    bool   rep =   v_x >= xmin && v_x <= xmax
237                && v_y >= ymin && v_y <= ymax
238                && v_z >= zmin && v_z <= zmax;
239    return rep;
240 }
241 // ========================================================= getPoint
242 double* Vertex::getPoint (double point[])
243 {
244    point [dir_x] = v_x;
245    point [dir_y] = v_y;
246    point [dir_z] = v_z;
247    return point;
248 }
249 // ========================================================= duplicate
250 void Vertex::duplicate (Document* cible)
251 {
252    v_clone = new Vertex (cible, v_x, v_y, v_z);
253    v_clone->v_scalar = v_scalar;
254 }
255 // ========================================================= definedBy
256 bool Vertex::definedBy (double px, double py, double pz, double eps2)
257 {
258    double dist2 = carre (v_x-px) + carre (v_y-py) + carre (v_z-pz);
259    return dist2 < eps2;
260 }
261 // ========================================================= dist2
262 double Vertex::dist2 (Vertex* other)
263 {
264    if (BadElement (other))
265       return 1e+77;
266
267    double dist = carre (v_x-other->v_x) + carre (v_y-other->v_y) 
268                                         + carre (v_z-other->v_z);
269    return dist;
270 }
271 // ========================================================= anaVertices
272 void Vertex::anaVertices (Vertices& cont, double pmin[], double pmax[], 
273                                                          double center[])
274 {
275     Real3 point;
276     int nbre = cont.size();
277     if (nbre==0)
278        {
279        for (int nc=0 ; nc<DIM3 ; nc++)
280             pmin [nc] = pmax[nc] =  center[nc] = 0;
281        return;
282        }
283
284     cont[0]->getPoint (pmin);
285     cont[0]->getPoint (pmax);
286     for (int nro=1 ; nro<nbre ; ++nro)
287         {
288         cont[nro]->getPoint (point);
289         for (int nc=0 ; nc<DIM3 ; nc++)
290             {
291             pmin [nc] = std::min (pmin[nc], point[nc]);
292             pmax [nc] = std::max (pmax[nc], point[nc]);
293             }
294         }
295
296     for (int nc=0 ; nc<DIM3 ; nc++)
297         center [nc] = (pmin[nc] + pmax[nc])/2;
298 }
299 END_NAMESPACE_HEXA