0031138: Visualization - Image_AlienPixMap fails to load PNG image with palette
[occt.git] / src / MeshTest / MeshTest.cxx
CommitLineData
b311480e 1// Created on: 1993-09-22
2// Created by: Didier PIFFAULT
3// Copyright (c) 1993-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
4006ca98 17#include <MeshTest.hxx>
18
19#include <stdio.h>
7fd59977 20
42cf5bc1 21#include <Bnd_Box.hxx>
7fd59977 22#include <BRep_Builder.hxx>
42cf5bc1 23#include <BRepAdaptor_Surface.hxx>
24#include <BRepBndLib.hxx>
25#include <BRepBuilderAPI_MakeFace.hxx>
26#include <BRepBuilderAPI_MakePolygon.hxx>
27#include <BRepBuilderAPI_MakeVertex.hxx>
28#include <BRepLib.hxx>
7fd59977 29#include <BRepMesh_IncrementalMesh.hxx>
42cf5bc1 30#include <BRepTest.hxx>
31#include <BRepTools.hxx>
32#include <CSLib.hxx>
42cf5bc1 33#include <DBRep.hxx>
42cf5bc1 34#include <Draw_Appli.hxx>
7fd59977 35#include <Draw_Segment2D.hxx>
42cf5bc1 36#include <DrawTrSurf.hxx>
42cf5bc1 37#include <GeometryTest.hxx>
4006ca98 38#include <IMeshData_Status.hxx>
7fd59977 39#include <Poly_Connect.hxx>
7fd59977 40#include <TopExp_Explorer.hxx>
d51c7072
O
41#include <TopTools_MapIteratorOfMapOfShape.hxx>
42
42cf5bc1 43//epa Memory leaks test
44//OAN: for triepoints
57c28b61 45#ifdef _WIN32
7fd59977 46Standard_IMPORT Draw_Viewer dout;
47#endif
48
49#define MAX2(X, Y) ( Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
50#define MAX3(X, Y, Z) ( MAX2 ( MAX2(X,Y) , Z) )
51
7fd59977 52#define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
53#define TWOTHIRD 0.666666666666666666666666666666666666666666666666666666666666
54
0797d9d3 55#ifdef OCCT_DEBUG_MESH_CHRONO
7fd59977 56#include <OSD_Chronometer.hxx>
57Standard_Integer D0Control, D0Internal, D0Unif, D0Edges, NbControls;
58OSD_Chronometer chTotal, chInternal, chControl, chUnif, chAddPoint;
59OSD_Chronometer chEdges, chMaillEdges, chEtuInter, chLastControl, chStock;
60OSD_Chronometer chAdd11, chAdd12, chAdd2, chUpdate, chPointValid;
61OSD_Chronometer chIsos, chPointsOnIsos;
62#endif
63
7fd59977 64//=======================================================================
65//function : incrementalmesh
66//purpose :
67//=======================================================================
d51c7072 68static Standard_Integer incrementalmesh(Draw_Interpretor& di, Standard_Integer nbarg, const char** argv)
7fd59977 69{
49cfd13d 70 if (nbarg < 3)
71 {
72 di << "\
73Builds triangular mesh for the shape\n\
74usage: incmesh Shape LinearDeflection [options]\n\
75options:\n\
46478ffe 76 -a val angular deflection for edges in deg\n\
74da0216 77 (default ~28.64 deg = 0.5 rad)\n\n\
46478ffe 78 -ai val angular deflection inside of faces in deg\n\
79 (default ~57.29 deg = 1 rad)\n\n\
80 -di val Linear deflection used to tessellate the face interior.\n\
74da0216 81 -min minimum size parameter limiting size of triangle's\n\
82 edges to prevent sinking into amplification in case\n\
83 of distorted curves and surfaces\n\n\
49cfd13d 84 -relative notifies that relative deflection is used\n\
74da0216 85 (switched off by default)\n\n\
a319f03f 86 -int_vert_off disables insertion of internal vertices into mesh\n\
87 (enabled by default)\n\
f43eff9f 88 -surf_def_off disables control of deflection of mesh from real\n\
89 surface (enabled by default)\n\
e71669c6 90 -parallel enables parallel execution (switched off by default)\n\
3c1b7084 91 -adjust_min enables local adjustment of min size depending on edge size (switched off by default)\n";
416d4426 92 return 0;
93 }
7fd59977 94
416d4426 95 TopoDS_Shape aShape = DBRep::Get(argv[1]);
49cfd13d 96 if (aShape.IsNull())
97 {
98 di << " Null shapes are not allowed here\n";
416d4426 99 return 0;
100 }
416d4426 101
7bd071ed 102 IMeshTools_Parameters aMeshParams;
46478ffe 103 aMeshParams.Deflection = aMeshParams.DeflectionInterior =
104 Max(Draw::Atof(argv[2]), Precision::Confusion());
49cfd13d 105
106 if (nbarg > 3)
107 {
108 Standard_Integer i = 3;
109 while (i < nbarg)
110 {
111 TCollection_AsciiString aOpt(argv[i++]);
112 aOpt.LowerCase();
113
114 if (aOpt == "")
115 continue;
116 else if (aOpt == "-relative")
7bd071ed 117 aMeshParams.Relative = Standard_True;
49cfd13d 118 else if (aOpt == "-parallel")
7bd071ed 119 aMeshParams.InParallel = Standard_True;
a319f03f 120 else if (aOpt == "-int_vert_off")
7bd071ed 121 aMeshParams.InternalVerticesMode = Standard_False;
f43eff9f 122 else if (aOpt == "-surf_def_off")
7bd071ed 123 aMeshParams.ControlSurfaceDeflection = Standard_False;
3c1b7084 124 else if (aOpt == "-adjust_min")
125 aMeshParams.AdjustMinSize = Standard_True;
49cfd13d 126 else if (i < nbarg)
127 {
128 Standard_Real aVal = Draw::Atof(argv[i++]);
129 if (aOpt == "-a")
7bd071ed 130 {
131 aMeshParams.Angle = aVal * M_PI / 180.;
132 }
46478ffe 133 else if (aOpt == "-ai")
134 {
135 aMeshParams.AngleInterior = aVal * M_PI / 180.;
136 }
74da0216 137 else if (aOpt == "-min")
7bd071ed 138 aMeshParams.MinSize = aVal;
46478ffe 139 else if (aOpt == "-di")
140 {
141 aMeshParams.DeflectionInterior = aVal;
142 }
49cfd13d 143 else
144 --i;
145 }
146 }
416d4426 147 }
49cfd13d 148
416d4426 149 di << "Incremental Mesh, multi-threading "
7bd071ed 150 << (aMeshParams.InParallel ? "ON" : "OFF") << "\n";
151
e71669c6 152 BRepMesh_IncrementalMesh aMesher (aShape, aMeshParams);
d51c7072 153
49cfd13d 154 di << "Meshing statuses: ";
7bd071ed 155 const Standard_Integer aStatus = aMesher.GetStatusFlags();
156 if (!aStatus)
d51c7072
O
157 {
158 di << "NoError";
159 }
160 else
161 {
162 Standard_Integer i;
7bd071ed 163 for (i = 0; i < 8; i++)
d51c7072 164 {
7bd071ed 165 Standard_Integer aFlag = aStatus & (1 << i);
166 if (aFlag)
d51c7072 167 {
7bd071ed 168 switch ((IMeshData_Status) aFlag)
d51c7072 169 {
7bd071ed 170 case IMeshData_OpenWire:
171 di << "OpenWire ";
172 break;
173 case IMeshData_SelfIntersectingWire:
174 di << "SelfIntersectingWire ";
175 break;
176 case IMeshData_Failure:
177 di << "Failure ";
178 break;
179 case IMeshData_ReMesh:
180 di << "ReMesh ";
181 break;
182 case IMeshData_UnorientedWire:
183 di << "UnorientedWire ";
184 break;
185 case IMeshData_TooFewPoints:
186 di << "TooFewPoints ";
187 break;
188 case IMeshData_Outdated:
189 di << "Outdated ";
190 break;
191 case IMeshData_Reused:
192 di << "Reused ";
193 break;
194 case IMeshData_NoError:
195 default:
196 break;
d51c7072
O
197 }
198 }
199 }
200 }
201
7fd59977 202 return 0;
203}
204
b508cbc5 205//=======================================================================
206//function : tessellate
207//purpose :
208//=======================================================================
209static Standard_Integer tessellate (Draw_Interpretor& /*di*/, Standard_Integer nbarg, const char** argv)
210{
211 if (nbarg != 5)
212 {
213 std::cerr << "Builds regular triangulation with specified number of triangles\n"
214 " Usage: tessellate result {surface|face} nbu nbv\n"
215 " Triangulation is put into the face with natural bounds (result);\n"
216 " it will have 2*nbu*nbv triangles and (nbu+1)*(nbv+1) nodes";
217 return 1;
218 }
219
220 const char *aResName = argv[1];
221 const char *aSrcName = argv[2];
222 int aNbU = Draw::Atoi (argv[3]);
223 int aNbV = Draw::Atoi (argv[4]);
224
225 if (aNbU <= 0 || aNbV <= 0)
226 {
227 std::cerr << "Error: Arguments nbu and nbv must be both greater than 0\n";
228 return 1;
229 }
230
231 Handle(Geom_Surface) aSurf = DrawTrSurf::GetSurface(aSrcName);
232 double aUMin, aUMax, aVMin, aVMax;
233 if (! aSurf.IsNull())
234 {
235 aSurf->Bounds (aUMin, aUMax, aVMin, aVMax);
236 }
237 else
238 {
239 TopoDS_Shape aShape = DBRep::Get(aSrcName);
240 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_FACE)
241 {
242 std::cerr << "Error: " << aSrcName << " is not a face\n";
243 return 1;
244 }
245 TopoDS_Face aFace = TopoDS::Face (aShape);
246 aSurf = BRep_Tool::Surface (aFace);
247 if (aSurf.IsNull())
248 {
249 std::cerr << "Error: Face " << aSrcName << " has no surface\n";
250 return 1;
251 }
252
253 BRepTools::UVBounds (aFace, aUMin, aUMax, aVMin, aVMax);
254 }
255 if (Precision::IsInfinite (aUMin) || Precision::IsInfinite (aUMax) ||
256 Precision::IsInfinite (aVMin) || Precision::IsInfinite (aVMax))
257 {
258 std::cerr << "Error: surface has infinite parametric range, aborting\n";
259 return 1;
260 }
261
262 BRepBuilderAPI_MakeFace aFaceMaker (aSurf, aUMin, aUMax, aVMin, aVMax, Precision::Confusion());
263 if (! aFaceMaker.IsDone())
264 {
265 std::cerr << "Error: cannot build face with natural bounds, aborting\n";
266 return 1;
267 }
268 TopoDS_Face aFace = aFaceMaker;
269
270 // create triangulation
271 int aNbNodes = (aNbU + 1) * (aNbV + 1);
272 int aNbTriangles = 2 * aNbU * aNbV;
273 Handle(Poly_Triangulation) aTriangulation =
274 new Poly_Triangulation (aNbNodes, aNbTriangles, Standard_False);
275
276 // fill nodes
277 TColgp_Array1OfPnt &aNodes = aTriangulation->ChangeNodes();
278 GeomAdaptor_Surface anAdSurf (aSurf);
279 double aDU = (aUMax - aUMin) / aNbU;
280 double aDV = (aVMax - aVMin) / aNbV;
281 for (int iU = 0, iShift = 1; iU <= aNbU; iU++, iShift += aNbV + 1)
282 {
283 double aU = aUMin + iU * aDU;
284 for (int iV = 0; iV <= aNbV; iV++)
285 {
286 double aV = aVMin + iV * aDV;
287 gp_Pnt aP = anAdSurf.Value (aU, aV);
288 aNodes.SetValue (iShift + iV, aP);
289 }
290 }
291
292 // fill triangles
293 Poly_Array1OfTriangle &aTriangles = aTriangulation->ChangeTriangles();
294 for (int iU = 0, iShift = 1, iTri = 0; iU < aNbU; iU++, iShift += aNbV + 1)
295 {
296 for (int iV = 0; iV < aNbV; iV++)
297 {
298 int iBase = iShift + iV;
299 Poly_Triangle aTri1 (iBase, iBase + aNbV + 2, iBase + 1);
300 Poly_Triangle aTri2 (iBase, iBase + aNbV + 1, iBase + aNbV + 2);
301 aTriangles.SetValue (++iTri, aTri1);
302 aTriangles.SetValue (++iTri, aTri2);
303 }
304 }
305
306 // put triangulation to face
307 BRep_Builder B;
308 B.UpdateFace (aFace, aTriangulation);
309
310 // fill edge polygons
311 TColStd_Array1OfInteger aUMinIso (1, aNbV + 1), aUMaxIso (1, aNbV + 1);
312 for (int iV = 0; iV <= aNbV; iV++)
313 {
314 aUMinIso.SetValue (1 + iV, 1 + iV);
315 aUMaxIso.SetValue (1 + iV, 1 + iV + aNbU * (1 + aNbV));
316 }
317 TColStd_Array1OfInteger aVMinIso (1, aNbU + 1), aVMaxIso (1, aNbU + 1);
318 for (int iU = 0; iU <= aNbU; iU++)
319 {
320 aVMinIso.SetValue (1 + iU, 1 + iU * (1 + aNbV));
321 aVMaxIso.SetValue (1 + iU, (1 + iU) * (1 + aNbV));
322 }
323 Handle(Poly_PolygonOnTriangulation) aUMinPoly = new Poly_PolygonOnTriangulation (aUMinIso);
324 Handle(Poly_PolygonOnTriangulation) aUMaxPoly = new Poly_PolygonOnTriangulation (aUMaxIso);
325 Handle(Poly_PolygonOnTriangulation) aVMinPoly = new Poly_PolygonOnTriangulation (aVMinIso);
326 Handle(Poly_PolygonOnTriangulation) aVMaxPoly = new Poly_PolygonOnTriangulation (aVMaxIso);
327 for (TopExp_Explorer exp (aFace, TopAbs_EDGE); exp.More(); exp.Next())
328 {
329 TopoDS_Edge anEdge = TopoDS::Edge (exp.Current());
330 Standard_Real aFirst, aLast;
331 Handle(Geom2d_Curve) aC = BRep_Tool::CurveOnSurface (anEdge, aFace, aFirst, aLast);
332 gp_Pnt2d aPFirst = aC->Value (aFirst);
333 gp_Pnt2d aPLast = aC->Value (aLast);
334 if (Abs (aPFirst.X() - aPLast.X()) < 0.1 * (aUMax - aUMin)) // U=const
335 {
336 if (BRep_Tool::IsClosed (anEdge, aFace))
337 B.UpdateEdge (anEdge, aUMinPoly, aUMaxPoly, aTriangulation);
338 else
339 B.UpdateEdge (anEdge, (aPFirst.X() < 0.5 * (aUMin + aUMax) ? aUMinPoly : aUMaxPoly), aTriangulation);
340 }
341 else // V=const
342 {
343 if (BRep_Tool::IsClosed (anEdge, aFace))
344 B.UpdateEdge (anEdge, aVMinPoly, aVMaxPoly, aTriangulation);
345 else
346 B.UpdateEdge (anEdge, (aPFirst.Y() < 0.5 * (aVMin + aVMax) ? aVMinPoly : aVMaxPoly), aTriangulation);
347 }
348 }
349
350 DBRep::Set (aResName, aFace);
351 return 0;
352}
353
7fd59977 354//=======================================================================
355//function : MemLeakTest
356//purpose :
357//=======================================================================
35e08fe8 358static Standard_Integer MemLeakTest(Draw_Interpretor&, Standard_Integer /*nbarg*/, const char** /*argv*/)
7fd59977 359{
360 for(int i=0;i<10000;i++)
361 {
362 BRepBuilderAPI_MakePolygon w(gp_Pnt(0,0,0),gp_Pnt(0,100,0),gp_Pnt(20,100,0),gp_Pnt(20,0,0));
363 w.Close();
364 TopoDS_Wire wireShape( w.Wire());
365 BRepBuilderAPI_MakeFace faceBuilder(wireShape);
366 TopoDS_Face f( faceBuilder.Face());
367 BRepMesh_IncrementalMesh im(f,1);
368 BRepTools::Clean(f);
369 }
370 return 0;
371}
372
7fd59977 373//=======================================================================
374//function : trianglesinfo
375//purpose :
376//=======================================================================
377static Standard_Integer trianglesinfo(Draw_Interpretor& di, Standard_Integer n, const char** a)
378{
379 if (n != 2) return 1;
380 TopoDS_Shape S = DBRep::Get(a[1]);
381 if (S.IsNull()) return 1;
382 TopExp_Explorer ex;
383 Handle(Poly_Triangulation) T;
384 TopLoc_Location L;
385
386 Standard_Real MaxDeflection = 0.0;
387 Standard_Integer nbtriangles = 0, nbnodes = 0;
388 for (ex.Init(S, TopAbs_FACE); ex.More(); ex.Next()) {
389 TopoDS_Face F = TopoDS::Face(ex.Current());
390 T = BRep_Tool::Triangulation(F, L);
391 if (!T.IsNull()) {
392 nbtriangles += T->NbTriangles();
393 nbnodes += T->NbNodes();
394 if (T->Deflection() > MaxDeflection)
395 MaxDeflection = T->Deflection();
396 }
397 }
398
399 di<<"\n";
586db386 400 di<<"This shape contains " <<nbtriangles<<" triangles.\n";
401 di<<" " <<nbnodes <<" nodes.\n";
7fd59977 402 di<<"Maximal deflection " <<MaxDeflection<<"\n";
403 di<<"\n";
0797d9d3 404#ifdef OCCT_DEBUG_MESH_CHRONO
7fd59977 405 Standard_Real tot, addp, unif, contr, inter;
406 Standard_Real edges, mailledges, etuinter, lastcontrol, stock;
407 Standard_Real add11, add12, add2, upda, pointvalid;
408 Standard_Real isos, pointsisos;
409 chTotal.Show(tot); chAddPoint.Show(addp); chUnif.Show(unif);
410 chControl.Show(contr); chInternal.Show(inter);
411 chEdges.Show(edges); chMaillEdges.Show(mailledges);
412 chEtuInter.Show(etuinter); chLastControl.Show(lastcontrol);
413 chStock.Show(stock);
414 chAdd11.Show(add11); chAdd12.Show(add12); chAdd2.Show(add2); chUpdate.Show(upda);
415 chPointValid.Show(pointvalid); chIsos.Show(isos); chPointsOnIsos.Show(pointsisos);
416
417 if (tot > 0.00001) {
586db386 418 di <<"temps total de maillage: "<<tot <<" seconds\n";
419 di <<"dont: \n";
420 di <<"discretisation des edges: "<<edges <<" seconds---> "<< 100*edges/tot <<" %\n";
421 di <<"maillage des edges: "<<mailledges <<" seconds---> "<< 100*mailledges/tot <<" %\n";
422 di <<"controle et points internes: "<<etuinter <<" seconds---> "<< 100*etuinter/tot <<" %\n";
423 di <<"derniers controles: "<<lastcontrol<<" seconds---> "<< 100*lastcontrol/tot<<" %\n";
424 di <<"stockage dans la S.D. "<<stock <<" seconds---> "<< 100*stock/tot <<" %\n";
0d88155b 425 di << "\n";
586db386 426 di <<"et plus precisement: \n";
427 di <<"Add 11ere partie : "<<add11 <<" seconds---> "<<100*add11/tot <<" %\n";
428 di <<"Add 12ere partie : "<<add12 <<" seconds---> "<<100*add12/tot <<" %\n";
429 di <<"Add 2eme partie : "<<add2 <<" seconds---> "<<100*add2/tot <<" %\n";
430 di <<"Update : "<<upda <<" seconds---> "<<100*upda/tot <<" %\n";
431 di <<"AddPoint : "<<addp <<" seconds---> "<<100*addp/tot <<" %\n";
432 di <<"UniformDeflection "<<unif <<" seconds---> "<<100*unif/tot <<" %\n";
433 di <<"Controle : "<<contr <<" seconds---> "<<100*contr/tot <<" %\n";
434 di <<"Points Internes: "<<inter <<" seconds---> "<<100*inter/tot <<" %\n";
435 di <<"calcul des isos et du, dv: "<<isos <<" seconds---> "<<100*isos/tot <<" %\n";
436 di <<"calcul des points sur isos: "<<pointsisos<<" seconds---> "<<100*pointsisos/tot <<" %\n";
437 di <<"IsPointValid: "<<pointvalid<<" seconds---> "<<100*pointvalid/tot <<" %\n";
0d88155b
O
438 di << "\n";
439
440
441 di <<"nombre d'appels de controle apres points internes : "<< NbControls << "\n";
442 di <<"nombre de points sur restrictions : "<< D0Edges << "\n";
443 di <<"nombre de points calcules par UniformDeflection : "<< D0Unif << "\n";
444 di <<"nombre de points calcules dans InternalVertices : "<< D0Internal << "\n";
445 di <<"nombre de points calcules dans Control : "<< D0Control << "\n";
446 if (nbnodes-D0Edges != 0) {
447 Standard_Real ratio = (Standard_Real)(D0Internal+D0Control)/ (Standard_Real)(nbnodes-D0Edges);
448 di <<"---> Ratio: (D0Internal+D0Control) / (nbNodes-nbOnEdges) : "<< ratio << "\n";
449 }
7fd59977 450
0d88155b 451 di << "\n";
7fd59977 452
0d88155b
O
453 chTotal.Reset(); chAddPoint.Reset(); chUnif.Reset();
454 chControl.Reset(); chInternal.Reset();
455 chEdges.Reset(); chMaillEdges.Reset();
456 chEtuInter.Reset(); chLastControl.Reset();
457 chStock.Reset();
458 chAdd11.Reset(); chAdd12.Reset(); chAdd2.Reset(); chUpdate.Reset();
459 chPointValid.Reset(); chIsos.Reset(); chPointsOnIsos.Reset();
7fd59977 460
461 }
462#endif
463 return 0;
464}
465
466//=======================================================================
467//function : veriftriangles
468//purpose :
469//=======================================================================
7fd59977 470static Standard_Integer veriftriangles(Draw_Interpretor& di, Standard_Integer n, const char** a)
471{
472 if (n < 2) return 1;
473 Standard_Boolean quiet = 1;
474 if (n == 3) quiet = 0;
475 TopoDS_Shape Sh = DBRep::Get(a[1]);
476 if (Sh.IsNull()) return 1;
477 TopExp_Explorer ex;
478 Handle(Poly_Triangulation) T;
479 TopLoc_Location L;
480 Standard_Integer i, n1, n2, n3;
481 gp_Pnt2d mitri, v1, v2, v3, mi2d1, mi2d2, mi2d3;
482 gp_XYZ vecEd1, vecEd2, vecEd3;
0d88155b 483 // Standard_Real dipo, dm, dv, d1, d2, d3, defle;
7fd59977 484 Standard_Real dipo, dv, d1, d2, d3, defle;
485 Handle(Geom_Surface) S;
486 Standard_Integer nbface = 0;
487 gp_Pnt PP;
488
489 for (ex.Init(Sh, TopAbs_FACE); ex.More(); ex.Next()) {
490 TopoDS_Face F = TopoDS::Face(ex.Current());
491 nbface++;
492 T = BRep_Tool::Triangulation(F, L);
493 Standard_Real deflemax = 0, deflemin = 1.e100;
494 if (!T.IsNull()) {
495 Standard_Real defstock = T->Deflection();
496 const Poly_Array1OfTriangle& triangles = T->Triangles();
497 const TColgp_Array1OfPnt2d& Nodes2d = T->UVNodes();
498 const TColgp_Array1OfPnt& Nodes = T->Nodes();
499
500 S = BRep_Tool::Surface(F, L);
501
502 for(i = 1; i <= triangles.Length(); i++) {
0d88155b
O
503 if (F.Orientation() == TopAbs_REVERSED)
504 triangles(i).Get(n1,n3,n2);
505 else
506 triangles(i).Get(n1,n2,n3);
507
508 const gp_XY& xy1 = Nodes2d(n1).XY();
509 const gp_XY& xy2 = Nodes2d(n2).XY();
510 const gp_XY& xy3 = Nodes2d(n3).XY();
511
512 mi2d1.SetCoord((xy2.X()+xy3.X())*0.5,
513 (xy2.Y()+xy3.Y())*0.5);
514 mi2d2.SetCoord((xy1.X()+xy3.X())*0.5,
515 (xy1.Y()+xy3.Y())*0.5);
516 mi2d3.SetCoord((xy1.X()+xy2.X())*0.5,
517 (xy1.Y()+xy2.Y())*0.5);
518
519 gp_XYZ p1 = Nodes(n1).Transformed(L.Transformation()).XYZ();
520 gp_XYZ p2 = Nodes(n2).Transformed(L.Transformation()).XYZ();
521 gp_XYZ p3 = Nodes(n3).Transformed(L.Transformation()).XYZ();
522
523 vecEd1=p2-p1;
524 vecEd2=p3-p2;
525 vecEd3=p1-p3;
526 d1=vecEd1.SquareModulus();
527 d2=vecEd2.SquareModulus();
528 d3=vecEd3.SquareModulus();
529
530 if (d1!=0. && d2!=0. && d3!=0.) {
531 gp_XYZ equa(vecEd1^vecEd2);
532 dv=equa.Modulus();
533 if (dv>0.) {
534 equa.SetCoord(equa.X()/dv, equa.Y()/dv, equa.Z()/dv);
535 dipo=equa*p1;
536
537
538 mitri.SetCoord(ONETHIRD*(xy1.X()+xy2.X()+xy3.X()),
539 ONETHIRD*(xy1.Y()+xy2.Y()+xy3.Y()));
540 v1.SetCoord(ONETHIRD*mi2d1.X()+TWOTHIRD*xy1.X(),
541 ONETHIRD*mi2d1.Y()+TWOTHIRD*xy1.Y());
542 v2.SetCoord(ONETHIRD*mi2d2.X()+TWOTHIRD*xy2.X(),
543 ONETHIRD*mi2d2.Y()+TWOTHIRD*xy2.Y());
544 v3.SetCoord(ONETHIRD*mi2d3.X()+TWOTHIRD*xy3.X(),
545 ONETHIRD*mi2d3.Y()+TWOTHIRD*xy3.Y());
546
547 S->D0(mi2d1.X(), mi2d1.Y(), PP);
548 PP = PP.Transformed(L.Transformation());
549 defle = Abs((equa*PP.XYZ())-dipo);
550 deflemax = Max(deflemax, defle);
551 deflemin = Min(deflemin, defle);
552
553 S->D0(mi2d2.X(), mi2d2.Y(), PP);
554 PP = PP.Transformed(L.Transformation());
555 defle = Abs((equa*PP.XYZ())-dipo);
556 deflemax = Max(deflemax, defle);
557 deflemin = Min(deflemin, defle);
558
559 S->D0(mi2d3.X(), mi2d3.Y(), PP);
560 PP = PP.Transformed(L.Transformation());
561 defle = Abs((equa*PP.XYZ())-dipo);
562 deflemax = Max(deflemax, defle);
563 deflemin = Min(deflemin, defle);
564
565 S->D0(v1.X(), v1.Y(), PP);
566 PP = PP.Transformed(L.Transformation());
567 defle = Abs((equa*PP.XYZ())-dipo);
568 deflemax = Max(deflemax, defle);
569 deflemin = Min(deflemin, defle);
570
571 S->D0(v2.X(), v2.Y(), PP);
572 PP = PP.Transformed(L.Transformation());
573 defle = Abs((equa*PP.XYZ())-dipo);
574 deflemax = Max(deflemax, defle);
575 deflemin = Min(deflemin, defle);
576
577 S->D0(v3.X(), v3.Y(), PP);
578 PP = PP.Transformed(L.Transformation());
579 defle = Abs((equa*PP.XYZ())-dipo);
580 deflemax = Max(deflemax, defle);
581 deflemin = Min(deflemin, defle);
582
583 S->D0(mitri.X(), mitri.Y(), PP);
584 PP = PP.Transformed(L.Transformation());
585 defle = Abs((equa*PP.XYZ())-dipo);
586 deflemax = Max(deflemax, defle);
587 deflemin = Min(deflemin, defle);
588
589 if (defle > defstock) {
586db386 590 di <<"face "<< nbface <<" deflection = " << defle <<" pour "<<defstock <<" stockee.\n";
0d88155b
O
591 }
592 }
593 }
7fd59977 594 }
595 if (!quiet) {
0d88155b 596 di <<"face "<< nbface<<", deflemin = "<< deflemin<<", deflemax = "<<deflemax<<"\n";
7fd59977 597 }
598
599 }
600 }
601
602
603 return 0;
604}
605
7fd59977 606//=======================================================================
607//function : tri2d
608//purpose :
609//=======================================================================
4006ca98 610static Standard_Integer tri2d(Draw_Interpretor&, Standard_Integer n, const char** a)
7fd59977 611{
612
613 if (n != 2) return 1;
614 TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
615 TopoDS_Face F = TopoDS::Face(aLocalShape);
0d88155b 616 // TopoDS_Face F = TopoDS::Face(DBRep::Get(a[1]));
7fd59977 617 if (F.IsNull()) return 1;
618 Handle(Poly_Triangulation) T;
619 TopLoc_Location L;
620
621 T = BRep_Tool::Triangulation(F, L);
622 if (!T.IsNull()) {
7fd59977 623 // Build the connect tool
624 Poly_Connect pc(T);
0d88155b 625
7fd59977 626 Standard_Integer i,j, nFree, nInternal, nbTriangles = T->NbTriangles();
627 Standard_Integer t[3];
0d88155b 628
7fd59977 629 // count the free edges
630 nFree = 0;
631 for (i = 1; i <= nbTriangles; i++) {
632 pc.Triangles(i,t[0],t[1],t[2]);
633 for (j = 0; j < 3; j++)
0d88155b 634 if (t[j] == 0) nFree++;
7fd59977 635 }
0d88155b 636
7fd59977 637 // allocate the arrays
638 TColStd_Array1OfInteger Free(1,2*nFree);
639 nInternal = (3*nbTriangles - nFree) / 2;
640 TColStd_Array1OfInteger Internal(0,2*nInternal);
0d88155b 641
7fd59977 642 Standard_Integer fr = 1, in = 1;
643 const Poly_Array1OfTriangle& triangles = T->Triangles();
644 Standard_Integer nodes[3];
645 for (i = 1; i <= nbTriangles; i++) {
646 pc.Triangles(i,t[0],t[1],t[2]);
647 triangles(i).Get(nodes[0],nodes[1],nodes[2]);
648 for (j = 0; j < 3; j++) {
0d88155b
O
649 Standard_Integer k = (j+1) % 3;
650 if (t[j] == 0) {
651 Free(fr) = nodes[j];
652 Free(fr+1) = nodes[k];
653 fr += 2;
654 }
655 // internal edge if this triangle has a lower index than the adjacent
656 else if (i < t[j]) {
657 Internal(in) = nodes[j];
658 Internal(in+1) = nodes[k];
659 in += 2;
660 }
7fd59977 661 }
662 }
0d88155b 663
7fd59977 664 // Display the edges
665 if (T->HasUVNodes()) {
666 const TColgp_Array1OfPnt2d& Nodes2d = T->UVNodes();
667
668 Handle(Draw_Segment2D) Seg;
669
670 // free edges
671 Standard_Integer nn;
672 nn = Free.Length() / 2;
673 for (i = 1; i <= nn; i++) {
0d88155b
O
674 Seg = new Draw_Segment2D(Nodes2d(Free(2*i-1)),
675 Nodes2d(Free(2*i)),
676 Draw_rouge);
677 dout << Seg;
7fd59977 678 }
0d88155b 679
7fd59977 680 // internal edges
0d88155b 681
7fd59977 682 nn = nInternal;
683 for (i = 1; i <= nn; i++) {
0d88155b
O
684 Seg = new Draw_Segment2D(Nodes2d(Internal(2*i-1)),
685 Nodes2d(Internal(2*i)),
686 Draw_bleu);
687 dout << Seg;
7fd59977 688 }
689 }
690 dout.Flush();
691 }
692
693 return 0;
694}
695
7fd59977 696//=======================================================================
697//function : wavefront
698//purpose :
699//=======================================================================
7fd59977 700static Standard_Integer wavefront(Draw_Interpretor&, Standard_Integer nbarg, const char** argv)
701{
702 if (nbarg < 2) return 1;
703
704 TopoDS_Shape S = DBRep::Get(argv[1]);
705 if (S.IsNull()) return 1;
706
707 // creation du maillage s'il n'existe pas.
708
709 Bnd_Box B;
710 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
711 BRepBndLib::Add(S, B);
712 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
713 Standard_Real aDeflection =
714 MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) * 0.004;
715
b3a7aa39 716 BRepMesh_IncrementalMesh aMesh (S, aDeflection);
7fd59977 717
718
719 TopLoc_Location L;
720 TopExp_Explorer ex;
721
722 Standard_Integer i, nbface = 0;
723 Standard_Boolean OK = Standard_True;
724 gp_Vec D1U,D1V;
725 gp_Vec D2U,D2V,D2UV;
726 gp_Dir Nor;
727 gp_Pnt P;
728 Standard_Real U, V;
9fd2d2c3 729 CSLib_DerivativeStatus aStatus;
7fd59977 730 CSLib_NormalStatus NStat;
731 Standard_Real x, y, z;
732 Standard_Integer n1, n2, n3;
733 Standard_Integer k1, k2, k3;
0d88155b 734
7fd59977 735 char ffile[100];
0d88155b 736
7fd59977 737 if (nbarg == 3) {
738 strcpy(ffile, argv[2]);
739 strcat(ffile, ".obj");
740 }
741 else strcpy(ffile, "wave.obj");
742 FILE* outfile = fopen(ffile, "w");
743
744
745 fprintf(outfile, "%s %s\n%s %s\n\n", "# CASCADE ","MATRA DATAVISION", "#", ffile);
746
747 Standard_Integer nbNodes, totalnodes = 0, nbpolygons = 0;
748 for (ex.Init(S, TopAbs_FACE); ex.More(); ex.Next()) {
749 nbface++;
750 TopoDS_Face F = TopoDS::Face(ex.Current());
751 Handle(Poly_Triangulation) Tr = BRep_Tool::Triangulation(F, L);
0d88155b 752
7fd59977 753 if (!Tr.IsNull()) {
754 nbNodes = Tr->NbNodes();
755 const TColgp_Array1OfPnt& Nodes = Tr->Nodes();
0d88155b 756
7fd59977 757 // les noeuds.
758 for (i = 1; i <= nbNodes; i++) {
0d88155b
O
759 gp_Pnt Pnt = Nodes(i).Transformed(L.Transformation());
760 x = Pnt.X();
761 y = Pnt.Y();
762 z = Pnt.Z();
763 fprintf(outfile, "%s %f %f %f\n", "v", x, y, z);
7fd59977 764 }
0d88155b 765
7fd59977 766 fprintf(outfile, "\n%s %d\n\n", "# number of vertex", nbNodes);
0d88155b
O
767
768
7fd59977 769 // les normales.
0d88155b 770
7fd59977 771 if (Tr->HasUVNodes()) {
0d88155b
O
772 const TColgp_Array1OfPnt2d& UVNodes = Tr->UVNodes();
773 BRepAdaptor_Surface BS(F, Standard_False);
774
775 for (i = 1; i <= nbNodes; i++) {
776 U = UVNodes(i).X();
777 V = UVNodes(i).Y();
778
779 BS.D1(U,V,P,D1U,D1V);
9fd2d2c3 780 CSLib::Normal (D1U, D1V, Precision::Angular(), aStatus, Nor);
781 if (aStatus != CSLib_Done) {
0d88155b
O
782 BS.D2(U,V,P,D1U,D1V,D2U,D2V,D2UV);
783 CSLib::Normal(D1U,D1V,D2U,D2V,D2UV,Precision::Angular(),OK,NStat,Nor);
784 }
785 if (F.Orientation() == TopAbs_REVERSED) Nor.Reverse();
786
787 fprintf(outfile, "%s %f %f %f\n", "vn", Nor.X(), Nor.Y(), Nor.Z());
788 }
789
790 fprintf(outfile, "\n%s %d\n\n", "# number of vertex normals", nbNodes);
7fd59977 791 }
0d88155b 792
7fd59977 793 fprintf(outfile, "%s %d\n", "s", nbface);
0d88155b 794
7fd59977 795 // les triangles.
796 Standard_Integer nbTriangles = Tr->NbTriangles();
797 const Poly_Array1OfTriangle& triangles = Tr->Triangles();
0d88155b
O
798
799
7fd59977 800 for (i = 1; i <= nbTriangles; i++) {
0d88155b
O
801 if (F.Orientation() == TopAbs_REVERSED)
802 triangles(i).Get(n1, n3, n2);
803 else
804 triangles(i).Get(n1, n2, n3);
805 k1 = n1+totalnodes;
806 k2 = n2+totalnodes;
807 k3 = n3+totalnodes;
808 fprintf(outfile, "%s %d%s%d %d%s%d %d%s%d\n", "fo", k1,"//", k1, k2,"//", k2, k3,"//", k3);
7fd59977 809 }
810 nbpolygons += nbTriangles;
811 totalnodes += nbNodes;
0d88155b 812
7fd59977 813 fprintf(outfile, "\n%s %d\n", "# number of smooth groups", nbface);
814 fprintf(outfile, "\n%s %d\n", "# number of polygons", nbpolygons);
0d88155b 815
7fd59977 816 }
817 }
818
819 fclose(outfile);
820
821 return 0;
822}
823
d51c7072
O
824//=======================================================================
825//function : triedgepoints
826//purpose :
827//=======================================================================
4006ca98 828static Standard_Integer triedgepoints(Draw_Interpretor& di, Standard_Integer nbarg, const char** argv)
d51c7072
O
829{
830 if( nbarg < 2 )
831 return 1;
7fd59977 832
d51c7072
O
833 for( Standard_Integer i = 1; i < nbarg; i++ )
834 {
835 TopoDS_Shape aShape = DBRep::Get(argv[i]);
836 if ( aShape.IsNull() )
837 continue;
838
839 Handle(Poly_PolygonOnTriangulation) aPoly;
840 Handle(Poly_Triangulation) aT;
841 TopLoc_Location aLoc;
842 TopTools_MapOfShape anEdgeMap;
843 TopTools_MapIteratorOfMapOfShape it;
844
845 if( aShape.ShapeType() == TopAbs_EDGE )
846 {
847 anEdgeMap.Add( aShape );
848 }
849 else
850 {
851 TopExp_Explorer ex(aShape, TopAbs_EDGE);
852 for(; ex.More(); ex.Next() )
853 anEdgeMap.Add( ex.Current() );
854 }
855
856 if ( anEdgeMap.Extent() == 0 )
857 continue;
858
859 char newname[1024];
860 strcpy(newname,argv[i]);
861 char* p = newname;
862 while (*p != '\0') p++;
863 *p = '_';
864 p++;
865
866 Standard_Integer nbEdge = 1;
867 for(it.Initialize(anEdgeMap); it.More(); it.Next())
868 {
869 BRep_Tool::PolygonOnTriangulation(TopoDS::Edge(it.Key()), aPoly, aT, aLoc);
870 if ( aT.IsNull() || aPoly.IsNull() )
871 continue;
872
873 const TColgp_Array1OfPnt& Nodes = aT->Nodes();
874 const TColStd_Array1OfInteger& Indices = aPoly->Nodes();
875 const Standard_Integer nbnodes = Indices.Length();
876
877 for( Standard_Integer j = 1; j <= nbnodes; j++ )
878 {
879 gp_Pnt P3d = Nodes(Indices(j));
880 if( !aLoc.IsIdentity() )
881 P3d.Transform(aLoc.Transformation());
882
883 if( anEdgeMap.Extent() > 1 )
91322f44 884 Sprintf(p,"%d_%d",nbEdge,j);
d51c7072 885 else
91322f44 886 Sprintf(p,"%d",j);
d51c7072
O
887 DBRep::Set( newname, BRepBuilderAPI_MakeVertex(P3d) );
888 di.AppendElement(newname);
889 }
890 nbEdge++;
891 }
892 }
893 return 0;
894}
7fd59977 895
7693827d 896//=======================================================================
897//function : correctnormals
898//purpose : Corrects normals in shape triangulation nodes (...)
899//=======================================================================
4006ca98 900static Standard_Integer correctnormals(Draw_Interpretor& theDI,
901 Standard_Integer /*theNArg*/,
902 const char** theArgVal)
7693827d 903{
904 TopoDS_Shape S = DBRep::Get(theArgVal[1]);
905
906 //Use "correctnormals shape"
907
908
909 if(!BRepLib::EnsureNormalConsistency(S))
910 {
911 theDI << "Normals have not been changed!\n";
912 }
913 else
914 {
915 theDI << "Some corrections in source shape have been made!\n";
916 }
917
918 return 0;
919}
920
7fd59977 921//=======================================================================
0d88155b 922void MeshTest::Commands(Draw_Interpretor& theCommands)
7fd59977 923//=======================================================================
924{
925 Draw::Commands(theCommands);
926 BRepTest::AllCommands(theCommands);
927 GeometryTest::AllCommands(theCommands);
928 MeshTest::PluginCommands(theCommands);
929 const char* g;
930
931 g = "Mesh Commands";
0d88155b 932
49cfd13d 933 theCommands.Add("incmesh","Builds triangular mesh for the shape, run w/o args for help",__FILE__, incrementalmesh, g);
b508cbc5 934 theCommands.Add("tessellate","Builds triangular mesh for the surface, run w/o args for help",__FILE__, tessellate, g);
7fd59977 935 theCommands.Add("MemLeakTest","MemLeakTest",__FILE__, MemLeakTest, g);
7fd59977 936
937 theCommands.Add("tri2d", "tri2d facename",__FILE__, tri2d, g);
938 theCommands.Add("trinfo","trinfo name, print triangles information on objects",__FILE__,trianglesinfo,g);
939 theCommands.Add("veriftriangles","veriftriangles name, verif triangles",__FILE__,veriftriangles,g);
940 theCommands.Add("wavefront","wavefront name",__FILE__, wavefront, g);
d51c7072 941 theCommands.Add("triepoints", "triepoints shape1 [shape2 ...]",__FILE__, triedgepoints, g);
7fd59977 942
7693827d 943 theCommands.Add("correctnormals", "correctnormals shape",__FILE__, correctnormals, g);
7fd59977 944}