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