Salome HOME
Merge from V6_main 13/12/2012
[modules/hexablock.git] / src / HEXABLOCK / HexElements_asso.cxx
1 //
2 // CC++ : Interface Cascade de la classe Elements
3 //
4 // Copyright (C) 2009-2012  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.
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 "HexElements.hxx"
24 #include "HexEdge.hxx"
25 #include "HexDocument.hxx"
26
27 #ifndef NO_CASCADE
28 #include "HexKasBiCylinder.hxx"
29 #include "HexKasPoint.hxx"
30 #include "HexKasLine.hxx"
31
32 #include <BRepTools.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35 #include <BRepBuilderAPI_Transform.hxx>
36 #include <BRepBuilderAPI_MakeVertex.hxx>
37
38 #include <GCPnts_AbscissaPoint.hxx>
39
40 #include <TopoDS.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <TopoDS_Vertex.hxx>
45
46 #include <gp_Pln.hxx>
47 #include <gp_Pnt.hxx>
48 #include <gp_Dir.hxx>
49 #include <gp_Lin.hxx>
50
51 // HEXABLOCK includes
52 #include "HexVertex.hxx"
53 #include "HexOldShape.hxx"
54 #include "HexDiagnostics.hxx"
55                                     // Cercles
56 #include <GEOMImpl_CircleDriver.hxx>
57 #include <GEOMImpl_ICircle.hxx>
58
59 #include <BRepBuilderAPI_MakeEdge.hxx>
60 #include <BRep_Tool.hxx>
61
62 #include <GC_MakeCircle.hxx>
63 #include <Geom_Circle.hxx>
64
65 #include <gp_Circ.hxx>
66                                     // Sphere
67 #include <BRepPrimAPI_MakeSphere.hxx>
68                                     // Cylindre
69 #include <GEOMImpl_CylinderDriver.hxx>
70 #include <GEOMImpl_ICylinder.hxx>
71 #include <BRepPrimAPI_MakeCylinder.hxx>
72 #include <BRepAlgoAPI_Section.hxx>
73
74 BEGIN_NAMESPACE_HEXA
75
76 static bool db = false;
77
78 void clear_associations (Edge* edge);
79
80 // ---------------------------------------------------------------------
81
82 static KasLine      current_line;
83 static Shape        current_shape ("");
84
85 // ========================================================= cutAssociation
86 void Elements::cutAssociation (Shapes& tshapes, Edges& tedges, bool exist)
87 {
88    db = on_debug ();
89    char foo[18];
90    int nbedges  = tedges.size();
91    int nbshapes = tshapes.size ();
92    if (nbshapes==0)
93       return;
94
95    std::vector <KasLine>  tab_gline  (nbshapes);
96
97    Vertex* prems = tedges [0]         -> getVertex (V_AMONT);
98    Vertex* derns = tedges [nbedges-1] -> getVertex (V_AVAL);
99
100    KasPoint pnt_first, pnt_last;
101    pnt_first.definePoint (prems);
102    pnt_last .definePoint (derns);
103
104    if (exist)
105       {
106       if (pnt_first.isBad())
107          {
108          el_root->putError (W_ASSO_CUT1, prems->getName (foo));
109          return;
110          }
111       else if (pnt_last.isBad ())
112          {
113          el_root->putError (W_ASSO_CUT2, derns->getName (foo));
114          return;
115          }
116       }
117                             // ----------- Define + longueur totale
118    double  longueur = 0;
119    for (int ns = 0 ; ns<nbshapes ; ns++)
120        {
121        tab_gline[ns].defineLine (tshapes[ns]);
122        longueur += tab_gline[ns].getLength ();
123        }
124
125                             // ----------- Ordonnancement des shapes
126    double  abscisse = 0;
127    int     nslast   = 0;
128    double* coord    = pnt_first.getCoord ();
129    if (nbshapes==1)
130       {
131       tab_gline [0].setRank (0, 0, abscisse);
132       }
133    else
134       {
135
136       for (int rg = 0 ; rg<nbshapes ; rg++)
137           {
138           bool more = true;
139           for (int ns = 0 ; ns<nbshapes && more ; ns++)
140               {
141               if (tab_gline[ns].getRank()==NOTHING)
142                  {
143                  int pos = tab_gline[ns].findBound (coord);
144                  if (pos!=NOTHING)
145                     {
146                     more   = false;
147                     nslast = ns;
148                     tab_gline [ns].setRank (rg, pos, abscisse);
149                     coord = tab_gline [ns].getEnd ();
150                     }
151                  }
152               }
153                           // Pas trouve
154           if (more)
155              {
156              el_root->putError (W_ASSO_CUT3, derns->getName (foo));
157              return;
158              }
159           }
160       }
161                             // ----------- Dernier
162    if (exist)
163       {
164       coord = pnt_last.getCoord ();
165       int pos = tab_gline[nslast].findBound (coord);
166       if (pos != V_AVAL)
167          {
168          el_root->putError (W_ASSO_CUT4, derns->getName (foo));
169          return;
170          }
171       }
172                             // ----------- Associations
173    double delta = longueur / nbedges;
174    for (int ned = 0 ; ned<nbedges ; ned++)
175        {
176        if (db) cout << " ++ Association Edge nro "
177                     << ned << "/" <<nbedges << endl;
178        Edge*  edge = tedges[ned];
179        double  sm1 = ned*delta;
180        double  sm2 = sm1 + delta;
181        for (int ns = 0 ; ns<nbshapes ; ns++)
182            {
183            tab_gline[ns].associate (edge, sm1, sm2);
184            }
185
186        }
187    if (db) cout << " +++ End of Elements::cutAssociation" << endl;
188 }
189 // ====================================================== geom_define_line
190 void geom_define_line (string& brep)
191 {
192    current_shape.setBrep   (brep);
193    current_shape.setBounds (0, 1);
194    current_line.defineLine (&current_shape);
195 }
196 // ====================================================== geom_asso_point
197 void geom_asso_point (double angle, Vertex* node)
198 {
199    if (node!=NULL && node->getAssociation() == NULL)
200        current_line.assoPoint (angle, node);
201 }
202 // ====================================================== geom_asso_point
203 void geom_asso_point (Vertex* node)
204 {
205    if (node==NULL || node->getAssociation() != NULL)
206       return;
207
208    Real3 koord = { node->getX(), node->getY(), node->getZ() };
209    KasPoint  asso_point ;
210    asso_point.definePoint (koord);
211    asso_point.associate   (node);
212 }
213 // ====================================================== geom_create_circle
214 void geom_create_circle (double* milieu, double rayon, double* normale,
215                          double* base, string& brep)
216 {
217    db = on_debug ();
218    if (db) printf ("geom_create_circle c=(%g,%g,%g), r=%g\n",
219                     milieu[0], milieu[1], milieu[2], rayon);
220    if (db) printf ("    -------- base=(%g,%g,%g)\n", base[0], base[1], base[2]);
221    if (db) printf ("    -------- norm=(%g,%g,%g)\n", normale[0], normale[1],
222                                                   normale[2]);
223
224    gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
225    gp_Vec gp_vx     (base   [dir_x], base   [dir_y], base   [dir_z]);
226    gp_Vec gp_norm   (normale[dir_x], normale[dir_y], normale[dir_z]);
227
228    gp_Ax2  gp_axes (gp_center, gp_norm, gp_vx);
229    gp_Circ gp_circ (gp_axes,   rayon);
230
231    TopoDS_Edge    geom_circ = BRepBuilderAPI_MakeEdge(gp_circ).Edge();
232    ostringstream  stream_shape;
233    BRepTools::Write(geom_circ, stream_shape);
234    brep = stream_shape.str();
235
236    // geom_make_brep (geom_circ, brep);
237    if (NOT db)
238       return;
239                              // Impressions de mise au point
240    double umin = 0, umax = 0;
241    TopLoc_Location    loc;
242    Handle(Geom_Curve) handle = BRep_Tool::Curve (geom_circ, loc, umin, umax);
243    GeomAdaptor_Curve  AdaptCurve (handle);
244    double length = GCPnts_AbscissaPoint::Length(AdaptCurve, umin, umax);
245
246    BRepAdaptor_Curve geom_curve (geom_circ);
247
248    for (int pk=0; pk<=4; pk++)
249        {
250        GCPnts_AbscissaPoint s1 (geom_curve, pk*length/4,
251                                 geom_curve.FirstParameter());
252        double u1    = s1.Parameter ();
253        gp_Pnt point = geom_curve.Value (u1);
254        if (db)
255           printf ( " ..... pnt%d = (%g, %g, %g)\n", pk, point.X(),
256                                             point.Y(),  point.Z());
257        }
258 }
259 // ====================================================== geom_create_sphere
260 void geom_create_sphere (double* milieu, double radius, string& brep)
261 {
262    gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
263    gp_Ax2 gp_axis = gp_Ax2 (gp_center, gp_Dir(0,0,1), gp_Dir(1,0,0));
264
265    BRepPrimAPI_MakeSphere make_sphere (gp_axis, radius);
266
267    make_sphere.Build();
268
269    ostringstream     stream_shape;
270    TopoDS_Shape      geom_sphere = make_sphere.Face();
271    BRepTools::Write (geom_sphere, stream_shape);
272    brep = stream_shape.str();
273 }
274 // ====================================================== geom_dump_asso
275 void geom_dump_asso (Edge* edge)
276 {
277    printf (" %s dump_edge :\n",
278            "_______________________________________________________________");
279    if (edge==NULL || NOT edge->isHere ())
280       {
281       printf ("*** deleted ***)\n");
282       return;
283       }
284
285    bool db0 = db;
286    db  = false;
287    edge->printName(" = (");
288    edge->getVertex (V_AMONT)-> printName (", ");
289    edge->getVertex (V_AVAL) -> printName (")\n");
290
291    KasPoint asso_point;
292    for (int nro=0 ; nro<V_TWO ; nro++)
293        {
294        Vertex* vertex = edge->getVertex (nro);
295        vertex->printName ("");
296        printf (" = (%g, %g, %g)", vertex->getX(),  vertex->getY(),
297                                   vertex->getZ());
298
299        int ier = asso_point.definePoint (vertex);
300        if (ier==HOK)
301           {
302           double* coord = asso_point.getCoord();
303           printf (",  pnt_asso  = (%g, %g, %g)", coord[dir_x],  coord[dir_y],
304                                      coord[dir_z]);
305           }
306        printf ("\n");
307        }
308
309    KasLine asso_line;
310    const Shapes&  tshapes = edge->getAssociations ();
311    for (int nro=0 ; nro<tshapes.size() ; nro++)
312        {
313        printf ( " ------------------- Edge.tab_shape[%d] :\n", nro);
314        Shape* shape = tshapes[nro];
315        if (shape == NULL)
316           {
317           printf ( "                =  NULL\n");
318           }
319        else
320           {
321           asso_line.defineLine (shape);
322           double* deb = asso_line.getStart  ();
323           double* fin = asso_line.getEnd    ();
324           double  lg  = asso_line.getLength ();
325           printf (" Longueur = %g\n", lg);
326           printf (" Debut = %g = (%g, %g, %g)\n", shape->getStart(),
327                                                   deb[0], deb[1], deb[2]);
328           printf (" Fin   = %g = (%g, %g, %g)\n", shape->getEnd(),
329                                                   fin[0], fin[1], fin[2]);
330           }
331        }
332    db = db0;
333 }
334 // ====================================================== translate_brep
335 void translate_brep (string& brep, double dir[], string& trep)
336 {
337    gp_Trsf       transfo;
338    BRep_Builder  builder;
339    TopoDS_Shape  orig;
340
341    gp_Vec      vecteur       (dir [dir_x], dir [dir_y], dir [dir_z]);
342    transfo.SetTranslation    (vecteur);
343    istringstream stream_brep (brep);
344    BRepTools::Read           (orig, stream_brep, builder);
345
346    TopLoc_Location  loc_orig   = orig.Location();
347    gp_Trsf          trans_orig = loc_orig.Transformation();
348    TopLoc_Location  loc_result (transfo * trans_orig);
349    TopoDS_Shape     result = orig.Located (loc_result);
350
351    ostringstream stream_shape;
352    BRepTools::Write (result, stream_shape);
353    trep = stream_shape.str();
354 }
355 // ====================================================== transfo_brep
356 void transfo_brep (string& brep, Matrix* matrice, string& trep)
357 {
358    BRep_Builder  builder;
359    TopoDS_Shape  shape_orig;
360    gp_Trsf       transfo;
361
362    double             a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34;
363    matrice->getCoeff (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34);
364    transfo.SetValues (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34,
365                       Epsil2, Epsil2);
366
367    istringstream stream_brep (brep);
368    BRepTools::Read           (shape_orig, stream_brep, builder);
369
370    BRepBuilderAPI_Transform brep_transfo (shape_orig, transfo, Standard_True);
371    TopoDS_Shape result = brep_transfo.Shape();
372
373    ostringstream stream_shape;
374    BRepTools::Write (result, stream_shape);
375    trep = stream_shape.str();
376 }
377 // ====================================================== clear_associations
378 void clear_associations (Edge* edge)
379 {
380    edge->clearAssociation();
381    edge->getVertex(V_AMONT)->clearAssociation();
382    edge->getVertex(V_AVAL )->clearAssociation();
383 }
384 // ====================================================== associateShapes
385 int associateShapes (Edges& mline, int msens[], Shape* gstart, Shapes& gline,
386                     double pstart, double pend, bool closed, bool inv)
387 {
388    db = on_debug ();
389    if (db)
390       {
391       cout << "____________________________________________"
392            << " associateShapes" << endl;
393       PutData (gline.size());
394       PutData (pstart);
395       PutData (pend);
396       PutData (closed);
397       PutData (inv);
398       }
399
400    int nbshapes = gline.size ();
401    int nblines  = nbshapes + 1;
402
403    vector <KasLine*> buff_line (nblines); // car nblines != 0
404    vector <KasLine*> geom_line (nblines);
405
406                                     // -------- Bufferisation des shapes
407    for (int ns=0 ; ns<nbshapes ; ns++)
408        buff_line [ns] = new KasLine (gline [ns], 0.0, 1.0);
409
410                                     // -------- Premiere ligne
411    int    sdepart  = NOTHING;
412    double*  extrem    = NULL;
413    double abscisse = 0;
414    double pfin0    = 1;
415    if (closed)
416       {
417       if (pstart > UnEpsil)
418          {
419          inv    = true;
420          pstart = 0;
421          }
422       else if (pstart < Epsil)
423          {
424          inv    = false;
425          }
426       }
427    else if (nbshapes==1)
428       pfin0 = pend;
429
430    KasLine* prems = geom_line[0]  = new KasLine (gstart, pstart, pfin0);
431
432                           // Ligne fermee : sens impose par l'utilisateur
433    if (closed)
434       {
435       sdepart = inv ? V_AVAL : V_AMONT;
436       }
437                           // PolyLigne ouverte : trouver le sens
438    else if (nbshapes>0)
439       {
440       sdepart  = NOTHING;
441       extrem = prems -> getEnd();
442       for (int ns=V_AMONT ; ns<=V_AVAL && sdepart==NOTHING ; ns++)
443           {
444           for (int nb=0 ; nb<nbshapes && sdepart==NOTHING ; nb++)
445               {
446               if (buff_line[nb]->findBound(extrem) != NOTHING)
447                  sdepart = ns;
448               }
449           extrem = prems -> getStart();
450           }
451
452       if (sdepart==NOTHING)
453          {
454          printf (" ***************** Erreur dans associateShapes\n");
455          printf (" ***************** La ligne ouverte est interrompue\n");
456          return HERR;
457          }
458       }
459                           // Une seule ligne ouverte : depend de pstart et pend
460    else if (pstart < pend)
461       {
462       sdepart = V_AMONT;
463       pfin0   = pend;
464       }
465    else
466       {
467       sdepart = V_AVAL;
468       pfin0   = pend;
469       }
470
471    prems -> setRank (0, sdepart, abscisse);
472    extrem = prems -> getEnd();
473                                     // -------- Rangement des autres lignes
474    for (int nl=1 ; nl<nblines ; nl++)
475        {
476        bool more = true;
477        for (int nb=0 ; more && nb<nbshapes ; nb++)
478            {
479            KasLine*  ligne = buff_line[nb];
480            int sens = ligne==NULL ? NOTHING : ligne->findBound (extrem);
481            if (sens != NOTHING)
482               {
483                                  // Derniere ligne si ouverte
484               if (NOT closed && nl==nblines-1)
485                  {
486                  ligne -> setBounds (0, pend);
487                  }
488               ligne -> setRank (nl, sens, abscisse);
489               buff_line [nb] = NULL;
490               geom_line [nl] = ligne;
491               extrem = ligne->getEnd();
492               more   = false;
493               }
494            }
495        if (more)
496           {
497           printf (" ***************** Erreur dans KasLine\n");
498           return HERR;
499           }
500        }
501    if (closed && pstart > Epsil)
502       {
503       KasLine* ligne = new KasLine (gstart, 0, pstart);
504       ligne->setRank   (nblines, sdepart, abscisse);
505       geom_line.push_back (ligne);
506       }
507
508    int ntlines = geom_line.size();
509    int nbedges = mline.size ();
510
511    if (db)
512       cout << "=============================================================="
513            << endl;
514                                     // -------- Menage
515    for (int ned=0 ; ned<nbedges ; ned++)
516        clear_associations (mline[ned]);
517                                     // -------- Associations
518    double delta = abscisse / nbedges;
519    for (int ned=0 ; ned<nbedges ; ned++)
520        {
521        double sm1 = ned * delta;
522        double sm2 = sm1 + delta;
523        for (int ns = 0 ; ns<ntlines ; ns++)
524            geom_line[ns]->associate (mline[ned], sm1, sm2, msens[ned]);
525        }
526                                     // -------- Liberation
527    for (int nl=1 ; nl<ntlines ; nl++)
528        delete geom_line [nl];
529
530    return HOK;
531 }
532 // ====================================================== set_debug_asso
533 void set_debug_asso (bool boule)
534 {
535    db = boule;
536    if (db)
537        printf (" ... Traces actives dans  HexAssoElements_asso.cxx\n");
538 }
539 // ====================================================== clean_brep
540 void clean_brep (string& brep)
541 {
542    TopoDS_Shape  shape;
543    BRep_Builder  builder;
544    istringstream stream_brep (brep);
545
546    BRepTools::Read  (shape, stream_brep, builder);
547    BRepTools::Clean (shape);
548
549    ostringstream    stream_shape;
550    BRepTools::Write (shape, stream_shape);
551    brep = stream_shape.str();
552 }
553 END_NAMESPACE_HEXA
554
555 // ------------------------------------------------------------------------
556 #else    // #ifndef NO_CASCADE
557 // ------------------------------------------------------------------------
558
559 BEGIN_NAMESPACE_HEXA
560
561 // ====================================================== geom_create_circle
562 void geom_create_circle (double* milieu, double rayon, double* normale,
563                          double* base, string& brep)
564 {
565    char buffer [80];
566    sprintf (buffer, "(Cercle c=(%g,%g,%g), r=%g",
567                     milieu[0], milieu[1], milieu[2], rayon);
568    brep = buffer;
569 }
570 // ====================================================== geom_create_sphere
571 void geom_create_sphere (double* milieu, double radius, string& brep)
572 {
573 }
574 //
575 // ========================================================= cutAssociation
576 void Elements::cutAssociation (Shapes& tshapes, Edges& tedges, bool exist)
577 {
578 }
579 // ====================================================== geom_define_line
580 void geom_define_line (string& brep)
581 {
582 }
583 // ====================================================== geom_dump_asso
584 void geom_dump_asso (Edge* edge)
585 {
586 }
587 // ====================================================== geom_asso_point
588 void geom_asso_point (double angle, Vertex* node)
589 {
590 }
591 // ====================================================== geom_asso_point
592 void geom_asso_point (Vertex* node)
593 {
594 }
595 // ====================================================== translate_brep
596 void translate_brep (string& orig, double dir[], string& result)
597 {
598    result = orig;
599 }
600 // ====================================================== transfo_brep
601 void transfo_brep (string& brep, Matrix* matrice, string& trep)
602 {
603    trep = brep;
604 }
605 // ====================================================== dump_edge
606 void dump_edge (Edge* edge)
607 {
608 }
609 // ====================================================== associateShapes
610 int associateShapes (Edges& mline, int msens[], Shape* gstart, Shapes& gline,
611                      double pstart, double pend, bool closed, bool inv)
612 {
613    return HOK;
614 }
615 // ====================================================== set_debug_asso
616 void set_debug_asso (bool boule)
617 {
618 }
619 // ====================================================== geom_create_cylcyl
620 int geom_create_cylcyl (double* borig, double* bnorm, double* bbase,
621                         double  bray,  double  bhaut,
622                         double* sorig, double* snorm, double* sbase,
623                         double  sray,  double  shaut)
624 {
625    return HOK;
626 }
627 // ====================================================== geom_asso_cylcyl
628 int geom_asso_cylcyl (Edge* edge)
629 {
630    return HOK;
631 }
632 // ====================================================== clean_brep
633 void clean_brep (string& brep)
634 {
635 }
636 END_NAMESPACE_HEXA
637 #endif