0024023: Revamp the OCCT Handle -- general
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_SplitSurfaceArea.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <ShapeUpgrade_SplitSurfaceArea.ixx>
15 #include <TColStd_HSequenceOfReal.hxx>
16 #include <GeomAdaptor_Surface.hxx>
17
18 //=======================================================================
19 //function : ShapeUpgrade_SplitSurfaceArea
20 //purpose  : 
21 //=======================================================================
22
23 ShapeUpgrade_SplitSurfaceArea::ShapeUpgrade_SplitSurfaceArea():
24        ShapeUpgrade_SplitSurface()
25 {
26   myNbParts =1;
27 }
28
29 //=======================================================================
30 //function : Compute
31 //purpose  : 
32 //=======================================================================
33
34  void ShapeUpgrade_SplitSurfaceArea::Compute(const Standard_Boolean /*Segment*/) 
35 {
36   if(myNbParts <= 1)
37     return;
38   
39   GeomAdaptor_Surface ads(mySurface,myUSplitValues->Value(1),myUSplitValues->Value(2),
40                           myVSplitValues->Value(1),myVSplitValues->Value(2));
41   Standard_Real aKoefU = ads.UResolution(1.);
42   Standard_Real aKoefV = ads.VResolution(1.);
43   if(aKoefU ==0)
44     aKoefU =1.;
45   if(aKoefV ==0)
46     aKoefV =1.;
47   Standard_Real aUSize = fabs(myUSplitValues->Value(2) - myUSplitValues->Value(1))/aKoefU;
48   Standard_Real aVSize = fabs(myVSplitValues->Value(2) - myVSplitValues->Value(1))/aKoefV;
49   Standard_Real  aNbUV =  aUSize/aVSize;
50   Handle(TColStd_HSequenceOfReal) aFirstSplit = (aNbUV <1. ? myVSplitValues : myUSplitValues);
51   Handle(TColStd_HSequenceOfReal) aSecondSplit = (aNbUV <1. ? myUSplitValues : myVSplitValues);
52   if(aNbUV<1)
53     aNbUV = 1./aNbUV;
54   
55   Standard_Integer nbSplitF = (aNbUV >=  myNbParts ? myNbParts : RealToInt(ceil(sqrt(myNbParts*ceil(aNbUV)))));
56   Standard_Integer nbSplitS = (aNbUV >=  myNbParts ? 0  : RealToInt(ceil((Standard_Real)myNbParts/(Standard_Real)nbSplitF)));
57   if(nbSplitS ==1)
58     nbSplitS++;
59   if(!nbSplitF)
60     return;
61   Standard_Real aStep = (aFirstSplit->Value(2) - aFirstSplit->Value(1))/nbSplitF;
62   Standard_Real aPrevPar = aFirstSplit->Value(1);
63   Standard_Integer i =1;
64   for( ; i < nbSplitF; i++) {
65     Standard_Real aNextPar = aPrevPar + aStep;
66     aFirstSplit->InsertBefore(i+1,aNextPar);
67     aPrevPar = aNextPar;
68   }
69   
70   if(nbSplitS) {
71     aStep = (aSecondSplit->Value(2) - aSecondSplit->Value(1))/nbSplitS;
72     aPrevPar = aSecondSplit->Value(1);
73     for(i =1 ; i < nbSplitS; i++) {
74       Standard_Real aNextPar = aPrevPar + aStep;
75       aSecondSplit->InsertBefore(i+1,aNextPar);
76       aPrevPar = aNextPar;
77     }
78   }
79 }
80