1 // Created on: 1993-02-03
2 // Created by: Laurent BUCHARD
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and / or modify it
9 // under the terms of the GNU Lesser General Public version 2.1 as published
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <IntPatch_Polyhedron.ixx>
19 #include <IntPatch_HInterTool.hxx>
23 #include <TColgp_Array2OfPnt.hxx>
24 #include <TColStd_Array2OfReal.hxx>
25 #include <Bnd_Array1OfBox.hxx>
26 #include <Standard_ConstructionError.hxx>
27 #include <Precision.hxx>
32 #define LONGUEUR_MINI_EDGE_TRIANGLE 1e-14
33 #define DEFLECTION_COEFF 1.1
36 //================================================================================
37 static Standard_Integer NbPOnU (const Handle(Adaptor3d_HSurface)& S)
39 const Standard_Real u0 = S->FirstUParameter();
40 const Standard_Real u1 = S->LastUParameter();
41 const Standard_Integer nbpu = IntPatch_HInterTool::NbSamplesU(S,u0,u1);
42 return (nbpu>NBMAXUV? NBMAXUV : nbpu);
44 //================================================================================
45 static Standard_Integer NbPOnV (const Handle(Adaptor3d_HSurface)& S)
47 const Standard_Real v0 = S->FirstVParameter();
48 const Standard_Real v1 = S->LastVParameter();
49 const Standard_Integer nbpv = IntPatch_HInterTool::NbSamplesV(S,v0,v1);
50 return (nbpv>NBMAXUV? NBMAXUV : nbpv);
53 //=======================================================================
56 //=======================================================================
57 void IntPatch_Polyhedron::Destroy()
59 gp_Pnt *CMyPnts = (gp_Pnt *)C_MyPnts; if(C_MyPnts) delete [] CMyPnts;
60 Standard_Real *CMyU = (Standard_Real *)C_MyU; if(C_MyU) delete [] CMyU;
61 Standard_Real *CMyV = (Standard_Real *)C_MyV; if(C_MyV) delete [] CMyV;
62 C_MyPnts=C_MyU=C_MyV=NULL;
65 //=======================================================================
66 //function : IntPatch_Polyhedron
68 //=======================================================================
69 IntPatch_Polyhedron::IntPatch_Polyhedron (const Handle(Adaptor3d_HSurface)& Surface)
70 : TheDeflection(Epsilon(100.)),
71 nbdeltaU(NbPOnU(Surface)),
72 nbdeltaV(NbPOnV(Surface)),
73 C_MyPnts(NULL),C_MyU(NULL),C_MyV(NULL),
74 UMinSingular(IntPatch_HInterTool::SingularOnVMin(Surface)),
75 UMaxSingular(IntPatch_HInterTool::SingularOnVMin(Surface)),
76 VMinSingular(IntPatch_HInterTool::SingularOnVMin(Surface)),
77 VMaxSingular(IntPatch_HInterTool::SingularOnVMin(Surface))
79 const Standard_Integer t = (nbdeltaU+1)*(nbdeltaV+1)+1;
80 gp_Pnt *CMyPnts = new gp_Pnt[t];
81 Standard_Real *CMyU = new Standard_Real[t];
82 Standard_Real *CMyV = new Standard_Real[t];
87 const Standard_Real u0 = Surface->FirstUParameter();
88 const Standard_Real u1 = Surface->LastUParameter();
89 const Standard_Real v0 = Surface->FirstVParameter();
90 const Standard_Real v1 = Surface->LastVParameter();
92 const Standard_Real U1mU0sNbdeltaU = (u1-u0)/(Standard_Real)nbdeltaU;
93 const Standard_Real V1mV0sNbdeltaV = (v1-v0)/(Standard_Real)nbdeltaV;
97 Standard_Integer i1, i2, Index=1;
98 for (i1 = 0, U = u0; i1 <= nbdeltaU; i1++, U+= U1mU0sNbdeltaU) {
99 for (i2 = 0, V = v0; i2 <= nbdeltaV; i2++, V+= V1mV0sNbdeltaV ) {
109 Standard_Real tol=0.0;
110 const Standard_Integer nbtriangles = NbTriangles();
111 for (i1=1; i1<=nbtriangles; i1++) {
112 const Standard_Real tol1 = DeflectionOnTriangle(Surface,i1);
113 if(tol1>tol) tol=tol1;
116 tol*=DEFLECTION_COEFF;
118 DeflectionOverEstimation(tol);
122 //=======================================================================
123 //function : IntPatch_Polyhedron
125 //=======================================================================
126 IntPatch_Polyhedron::IntPatch_Polyhedron (const Handle(Adaptor3d_HSurface)& Surface,
127 const Standard_Integer nbu,
128 const Standard_Integer nbv)
129 : TheDeflection(Epsilon(100.)),
132 C_MyPnts(NULL),C_MyU(NULL),C_MyV(NULL),
133 UMinSingular(IntPatch_HInterTool::SingularOnVMin(Surface)),
134 UMaxSingular(IntPatch_HInterTool::SingularOnVMin(Surface)),
135 VMinSingular(IntPatch_HInterTool::SingularOnVMin(Surface)),
136 VMaxSingular(IntPatch_HInterTool::SingularOnVMin(Surface))
138 const Standard_Integer t = (nbdeltaU+1)*(nbdeltaV+1)+1;
139 gp_Pnt *CMyPnts = new gp_Pnt[t];
140 Standard_Real *CMyU = new Standard_Real[t];
141 Standard_Real *CMyV = new Standard_Real[t];
146 const Standard_Real u0 = Surface->FirstUParameter();
147 const Standard_Real u1 = Surface->LastUParameter();
148 const Standard_Real v0 = Surface->FirstVParameter();
149 const Standard_Real v1 = Surface->LastVParameter();
151 const Standard_Real U1mU0sNbdeltaU = (u1-u0)/(Standard_Real)nbdeltaU;
152 const Standard_Real V1mV0sNbdeltaV = (v1-v0)/(Standard_Real)nbdeltaV;
156 Standard_Integer i1, i2, Index=1;
157 for (i1 = 0, U = u0; i1 <= nbdeltaU; i1++, U+= U1mU0sNbdeltaU) {
158 for (i2 = 0, V = v0; i2 <= nbdeltaV; i2++, V+= V1mV0sNbdeltaV ) {
168 Standard_Real tol=0.0;
169 const Standard_Integer nbtriangles = NbTriangles();
170 for (i1=1; i1<=nbtriangles; i1++) {
171 const Standard_Real tol1 = DeflectionOnTriangle(Surface,i1);
172 if(tol1>tol) tol=tol1;
175 tol*=DEFLECTION_COEFF;
177 DeflectionOverEstimation(tol);
182 //=======================================================================
183 //function : DeflectionOnTriangle
185 //=======================================================================
187 Standard_Real IntPatch_Polyhedron::DeflectionOnTriangle
188 (const Handle(Adaptor3d_HSurface)& Surface,
189 const Standard_Integer Triang) const
191 Standard_Integer i1,i2,i3;
193 Triangle(Triang,i1,i2,i3);
194 //-- Calcul de l eqution du plan
195 Standard_Real u1,v1,u2,v2,u3,v3;
197 P1 = Point(i1,u1,v1);
198 P2 = Point(i2,u2,v2);
199 P3 = Point(i3,u3,v3);
200 if(P1.SquareDistance(P2)<=LONGUEUR_MINI_EDGE_TRIANGLE) return(0);
201 if(P1.SquareDistance(P3)<=LONGUEUR_MINI_EDGE_TRIANGLE) return(0);
202 if(P2.SquareDistance(P3)<=LONGUEUR_MINI_EDGE_TRIANGLE) return(0);
203 gp_XYZ XYZ1=P2.XYZ()-P1.XYZ();
204 gp_XYZ XYZ2=P3.XYZ()-P2.XYZ();
205 gp_XYZ XYZ3=P1.XYZ()-P3.XYZ();
206 gp_Vec NormalVector((XYZ1^XYZ2)+(XYZ2^XYZ3)+(XYZ3^XYZ1));
207 NormalVector.Normalize();
208 //-- Calcul du point u,v au centre du triangle
209 Standard_Real u = (u1+u2+u3)/3.0;
210 Standard_Real v = (v1+v2+v3)/3.0;
211 gp_Vec P1P(P1,Surface->Value(u,v));
212 return(Abs(P1P.Dot(NormalVector)));
215 //=======================================================================
216 //function : Parameters
218 //=======================================================================
219 void IntPatch_Polyhedron::Parameters( const Standard_Integer Index
221 ,Standard_Real &V) const
223 U = ((Standard_Real *)C_MyU)[Index];
224 V = ((Standard_Real *)C_MyV)[Index];
227 //=======================================================================
228 //function : DeflectionOverEstimation
230 //=======================================================================
231 void IntPatch_Polyhedron::DeflectionOverEstimation(const Standard_Real flec)
234 TheDeflection=0.0001;
235 TheBnd.Enlarge(0.0001);
239 TheBnd.Enlarge(flec);
243 //=======================================================================
244 //function : DeflectionOverEstimation
246 //=======================================================================
247 Standard_Real IntPatch_Polyhedron::DeflectionOverEstimation() const
249 return TheDeflection;
252 //=======================================================================
253 //function : Bounding
255 //=======================================================================
256 const Bnd_Box& IntPatch_Polyhedron::Bounding() const
261 //=======================================================================
262 //function : FillBounding
264 //=======================================================================
265 void IntPatch_Polyhedron::FillBounding()
267 TheComponentsBnd=new Bnd_HArray1OfBox(1, NbTriangles());
269 Standard_Integer p1, p2, p3;
270 Standard_Integer nbtriangles = NbTriangles();
271 for (Standard_Integer iTri=1; iTri<=nbtriangles; iTri++) {
272 Triangle(iTri, p1, p2, p3);
274 const gp_Pnt& P1 = Point(p1);
275 const gp_Pnt& P2 = Point(p2);
276 const gp_Pnt& P3 = Point(p3);
277 if(P1.SquareDistance(P2)>LONGUEUR_MINI_EDGE_TRIANGLE) {
278 if(P1.SquareDistance(P3)>LONGUEUR_MINI_EDGE_TRIANGLE) {
279 if(P2.SquareDistance(P3)>LONGUEUR_MINI_EDGE_TRIANGLE) {
286 Boite.Enlarge(TheDeflection);
287 TheComponentsBnd->SetValue(iTri,Boite);
291 //=======================================================================
292 //function : ComponentsBounding
294 //=======================================================================
295 const Handle(Bnd_HArray1OfBox)& IntPatch_Polyhedron::ComponentsBounding () const
297 return TheComponentsBnd;
300 //=======================================================================
301 //function : NbTriangles
303 //=======================================================================
304 Standard_Integer IntPatch_Polyhedron::NbTriangles () const
306 return nbdeltaU*nbdeltaV*2;
309 //=======================================================================
310 //function : NbPoints
312 //=======================================================================
313 Standard_Integer IntPatch_Polyhedron::NbPoints () const
315 return (nbdeltaU+1)*(nbdeltaV+1);
318 //=======================================================================
319 //function : TriConnex
321 //=======================================================================
322 Standard_Integer IntPatch_Polyhedron::TriConnex (const Standard_Integer Triang,
323 const Standard_Integer Pivot,
324 const Standard_Integer Pedge,
325 Standard_Integer& TriCon,
326 Standard_Integer& OtherP) const {
328 Standard_Integer Pivotm1 = Pivot-1;
329 Standard_Integer nbdeltaVp1 = nbdeltaV+1;
330 Standard_Integer nbdeltaVm2 = nbdeltaV + nbdeltaV;
332 // Pivot position in the MaTriangle :
333 Standard_Integer ligP = Pivotm1/nbdeltaVp1;
334 Standard_Integer colP = Pivotm1 - ligP * nbdeltaVp1;
336 // Point sur Edge position in the MaTriangle and edge typ :
337 Standard_Integer ligE = 0, colE = 0, typE = 0;
339 ligE= (Pedge-1)/nbdeltaVp1;
340 colE= (Pedge-1) - (ligE * nbdeltaVp1);
342 if (ligP==ligE) typE=1;
344 else if (colP==colE) typE=2;
352 // Triangle position General case :
353 Standard_Integer linT = 0, colT = 0;
354 Standard_Integer linO = 0, colO = 0;
355 Standard_Integer t,tt;
357 t = (Triang-1)/(nbdeltaVm2);
358 tt= (Triang-1)-t*nbdeltaVm2;
368 if (colT==ligP+ligP) {
381 case 1: // Horizontal
385 colO=(colP>colE)? colP : colE; //--colO=Max(colP, colE);
390 colO=(colP<colE)? colP : colE; //--colO=Min(colP, colE);
394 if (colT==(colP+colP)) {
396 linO=(ligP>ligE)? ligP : ligE; //--linO=Max(ligP, ligE);
401 linO=(ligP<ligE)? ligP : ligE; //--linO=Min(ligP, ligE);
408 linO=(ligP>ligE)? ligP : ligE; //--linO=Max(ligP, ligE);
409 colO=(colP<colE)? colP : colE; //--colO=Min(colP, colE);
413 linO=(ligP<ligE)? ligP : ligE; //--linO=Min(ligP, ligE);
414 colO=(colP>colE)? colP : colE; //--colO=Max(colP, colE);
420 // Unknown Triangle position :
423 linT=(1>ligP)? 1 : ligP; //--linT=Max(1, ligP);
424 colT=(1>(colP+colP))? 1 : (colP+colP); //--colT=Max(1, colP+colP);
425 if (ligP==0) linO=ligP+1;
430 // Known edge We take the left or down connectivity :
432 case 1: // Horizontal
434 colT=(colP>colE)? colP : colE; //--colT=Max(colP,colE);
437 colO=(colP>colE)? colP : colE; //--colO=Max(colP,colE);
440 linT=(ligP>ligE)? ligP : ligE; //--linT=Max(ligP, ligE);
442 linO=(ligP<ligE)? ligP : ligE; //--linO=Min(ligP, ligE);
446 linT=(ligP>ligE)? ligP : ligE; //--linT=Max(ligP, ligE);
448 linO=(ligP>ligE)? ligP : ligE; //--linO=Max(ligP, ligE);
449 colO=(colP<colE)? colP : colE; //--colO=Min(colP, colE);
455 TriCon=(linT-1)*nbdeltaVm2 + colT;
460 if (colO<0) {colO=0;linO=1;}
461 else if (colO>nbdeltaV) {colO=nbdeltaV;linO=1;}
464 else if (linT>nbdeltaU) {
467 if (colO<0) {colO=0;linO=nbdeltaU-1;}
468 else if (colO>nbdeltaV) {colO=nbdeltaV;linO=nbdeltaU-1;}
475 if (linO<0) {linO=0;colO=1;}
476 else if (linO>nbdeltaU) {linO=nbdeltaU;colO=1;}
479 else if (colT>nbdeltaV) {
482 if (linO<0) {linO=0;colO=nbdeltaV-1;}
483 else if (linO>nbdeltaU) {linO=nbdeltaU;colO=nbdeltaV-1;}
487 OtherP=linO*nbdeltaVp1 + colO+1;
490 //----------------------------------------------------
491 //-- Detection des cas ou le triangle retourne est
492 //-- invalide. Dans ce cas, on retourne le triangle
493 //-- suivant par un nouvel appel a TriConnex.
495 //-- Si En entree : Point(Pivot)==Point(Pedge)
496 //-- Alors on retourne OtherP a 0
497 //-- et Tricon = Triangle
499 if(Point(Pivot).SquareDistance(Point(Pedge))<=LONGUEUR_MINI_EDGE_TRIANGLE) {
503 cout<<" Probleme ds IntCurveSurface_Polyhedron : Pivot et PEdge Confondus "<<endl;
507 if(Point(OtherP).SquareDistance(Point(Pedge))<=LONGUEUR_MINI_EDGE_TRIANGLE) {
509 cout<<" Probleme ds IntCurveSurface_Polyhedron : OtherP et PEdge Confondus "<<endl;
511 Standard_Integer TempTri,TempOtherP;
515 return(0); //-- BUG NON CORRIGE ( a revoir le role de nbdeltaU et nbdeltaV)
516 // return(TriConnex(TempTri,Pivot,TempOtherP,TriCon,OtherP));
523 //=======================================================================
524 //function : PlaneEquation
526 //=======================================================================
528 void IntPatch_Polyhedron::PlaneEquation (const Standard_Integer Triang,
529 gp_XYZ& NormalVector,
530 Standard_Real& PolarDistance) const
532 Standard_Integer i1,i2,i3;
533 Triangle(Triang,i1,i2,i3);
535 gp_XYZ Pointi1(Point(i1).XYZ());
536 gp_XYZ Pointi2(Point(i2).XYZ());
537 gp_XYZ Pointi3(Point(i3).XYZ());
540 gp_XYZ v1= Pointi2 - Pointi1;
541 gp_XYZ v2= Pointi3 - Pointi2;
542 gp_XYZ v3= Pointi1 - Pointi3;
544 if(v1.SquareModulus()<=LONGUEUR_MINI_EDGE_TRIANGLE) { NormalVector.SetCoord(1.0,0.0,0.0); return; }
545 if(v2.SquareModulus()<=LONGUEUR_MINI_EDGE_TRIANGLE) { NormalVector.SetCoord(1.0,0.0,0.0); return; }
546 if(v3.SquareModulus()<=LONGUEUR_MINI_EDGE_TRIANGLE) { NormalVector.SetCoord(1.0,0.0,0.0); return; }
548 NormalVector= (v1^v2)+(v2^v3)+(v3^v1);
549 NormalVector.Normalize();
550 PolarDistance = NormalVector * Point(i1).XYZ();
552 //=======================================================================
555 //=======================================================================
556 Standard_Boolean IntPatch_Polyhedron::Contain (const Standard_Integer Triang,
557 const gp_Pnt& ThePnt) const
559 Standard_Integer i1,i2,i3;
560 Triangle(Triang,i1,i2,i3);
561 gp_XYZ Pointi1(Point(i1).XYZ());
562 gp_XYZ Pointi2(Point(i2).XYZ());
563 gp_XYZ Pointi3(Point(i3).XYZ());
565 gp_XYZ v1=(Pointi2-Pointi1)^(ThePnt.XYZ()-Pointi1);
566 gp_XYZ v2=(Pointi3-Pointi2)^(ThePnt.XYZ()-Pointi2);
567 gp_XYZ v3=(Pointi1-Pointi3)^(ThePnt.XYZ()-Pointi3);
568 if (v1*v2 >= 0. && v2*v3 >= 0. && v3*v1>=0.)
569 return Standard_True;
571 return Standard_False;
573 //=======================================================================
576 //=======================================================================
578 void IntPatch_Polyhedron::Dump()const
581 //=======================================================================
584 //=======================================================================
585 void IntPatch_Polyhedron::Size(Standard_Integer& nbdu,
586 Standard_Integer& nbdv) const
591 //=======================================================================
592 //function : Triangle
594 //=======================================================================
595 void IntPatch_Polyhedron::Triangle (const Standard_Integer Index,
596 Standard_Integer& P1,
597 Standard_Integer& P2,
598 Standard_Integer& P3) const
600 Standard_Integer line=1+((Index-1)/(nbdeltaV*2));
601 Standard_Integer colon=1+((Index-1)%(nbdeltaV*2));
602 Standard_Integer colpnt=(colon+1)/2;
604 // General formula = (line-1)*(nbdeltaV+1)+colpnt
606 // Position of P1 = MesXYZ(line,colpnt);
607 P1= (line-1)*(nbdeltaV+1) + colpnt;
609 // Position of P2= MesXYZ(line+1,colpnt+((colon-1)%2));
610 P2= line*(nbdeltaV+1) + colpnt+((colon-1)%2);
612 // Position of P3= MesXYZ(line+(colon%2),colpnt+1);
613 P3= (line-1+(colon%2))*(nbdeltaV+1) + colpnt + 1;
614 //-- printf("\nTriangle %4d P1:%4d P2:%4d P3:%4d",Index,P1,P2,P3);
616 //=======================================================================
618 //=======================================================================
619 const gp_Pnt& IntPatch_Polyhedron::Point( const Standard_Integer Index
621 ,Standard_Real& V) const
623 gp_Pnt *CMyPnts = (gp_Pnt *)C_MyPnts;
624 Standard_Real *CMyU = (Standard_Real *)C_MyU;
625 Standard_Real *CMyV = (Standard_Real *)C_MyV;
628 return CMyPnts[Index];
630 //=======================================================================
632 //=======================================================================
633 const gp_Pnt& IntPatch_Polyhedron::Point(const Standard_Integer Index) const {
634 gp_Pnt *CMyPnts = (gp_Pnt *)C_MyPnts;
635 return CMyPnts[Index];
638 //=======================================================================
640 //=======================================================================
641 void IntPatch_Polyhedron::Point (const gp_Pnt& /*p*/,
642 const Standard_Integer /*lig*/,
643 const Standard_Integer /*col*/,
644 const Standard_Real /*u*/,
645 const Standard_Real /*v*/)
647 //printf("\n IntPatch_Polyhedron::Point : Ne dois pas etre appelle\n");
650 //=======================================================================
652 //=======================================================================
653 void IntPatch_Polyhedron::Point (const Standard_Integer Index, gp_Pnt& P) const
655 gp_Pnt *CMyPnts = (gp_Pnt *)C_MyPnts;
658 //=======================================================================