0024166: Unable to create file with "Save" menu of voxeldemo Qt sample
[occt.git] / src / Aspect / Aspect_CircularGrid.cxx
CommitLineData
b311480e 1// Copyright (c) 1999-2012 OPEN CASCADE SAS
2//
3// The content of this file is subject to the Open CASCADE Technology Public
4// License Version 6.5 (the "License"). You may not use the content of this file
5// except in compliance with the License. Please obtain a copy of the License
6// at http://www.opencascade.org and read it completely before using this file.
7//
8// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10//
11// The Original Code and all software distributed under the License is
12// distributed on an "AS IS" basis, without warranty of any kind, and the
13// Initial Developer hereby disclaims all such warranties, including without
14// limitation, any warranties of merchantability, fitness for a particular
15// purpose or non-infringement. Please see the License for the specific terms
16// and conditions governing the rights and limitations under the License.
17
7fd59977 18// Modified 23/02/98 : FMN ; Remplacement PI par Standard_PI
19
20#define CSR577 //GG 25/09/00 Avoid to have unaccuracy coordinates computation
21// when the grid is activated.
22
23#define OCC192_193 // jfa 27/02/2002
24// for big rotation angles - error of negative values expression in round numbers
25
26#define xTRACE
27
28#include <Aspect_CircularGrid.ixx>
29#include <Aspect_Grid.hxx>
30#include <Standard_NumericError.hxx>
31Aspect_CircularGrid::Aspect_CircularGrid
32 (const Quantity_Length aRadiusStep,
33 const Standard_Integer aDivisionNumber,
34 const Quantity_Length anXOrigin,
35 const Quantity_Length anYOrigin,
36 const Quantity_PlaneAngle aRotationAngle)
37:Aspect_Grid(anXOrigin,anYOrigin,aRotationAngle),myRadiusStep(aRadiusStep),
38myDivisionNumber(aDivisionNumber) {
39 }
40
41void Aspect_CircularGrid::SetRadiusStep(const Standard_Real aRadiusStep) {
42 Standard_NegativeValue_Raise_if(aRadiusStep < 0., "invalid radius step");
43 Standard_NullValue_Raise_if(aRadiusStep == 0. , "invalid radius step");
44 myRadiusStep= aRadiusStep;
45 Init();
46 UpdateDisplay();
47}
48void Aspect_CircularGrid::SetDivisionNumber(const Standard_Integer aNumber) {
49 Standard_NegativeValue_Raise_if(aNumber < 0., "invalid division number");
50 Standard_NullValue_Raise_if(aNumber == 0. , "invalid division number");
51 myDivisionNumber = aNumber;
52 Init();
53 UpdateDisplay();
54}
55void Aspect_CircularGrid::SetGridValues
56 (const Quantity_Length theXOrigin,
57 const Quantity_Length theYOrigin,
58 const Quantity_Length theRadiusStep,
59 const Standard_Integer theDivisionNumber,
60 const Quantity_PlaneAngle theRotationAngle) {
61 myXOrigin = theXOrigin;
62 myYOrigin = theYOrigin;
63 Standard_NegativeValue_Raise_if(theRadiusStep < 0., "invalid radius step");
64 Standard_NullValue_Raise_if(theRadiusStep == 0. , "invalid radius step");
65 myRadiusStep= theRadiusStep;
66 Standard_NegativeValue_Raise_if(theDivisionNumber < 0., "invalid division number");
67 Standard_NullValue_Raise_if(theDivisionNumber == 0. , "invalid division number");
68 myDivisionNumber = theDivisionNumber;
69 myRotationAngle = theRotationAngle;
70 Init();
71 UpdateDisplay();
72}
73void Aspect_CircularGrid::Compute(const Quantity_Length X,
74 const Quantity_Length Y,
75 Quantity_Length& gridX,
76 Quantity_Length& gridY) const {
77
78#ifdef TRACE
79 if( X == 0. || Y == 0. ) {
80 cout << " Aspect_CircularGrid" << endl;
81 }
82#endif
83 Standard_Real xo = XOrigin();
84 Standard_Real yo = YOrigin();
85 Standard_Real d = Sqrt( (xo-X)*(xo-X) + (yo-Y)*(yo-Y) );
86 Standard_Integer n = (Standard_Integer ) ( d/myRadiusStep + 0.5 ) ;
87 Standard_Real radius = Standard_Real(n) * myRadiusStep;
88 Standard_Real cosinus = (X-xo)/d;
89 Standard_Real a = ACos(cosinus);
90 Standard_Real ra = RotationAngle();
c6541a0c 91 if ( Y < yo ) a = 2 * M_PI - a;
7fd59977 92#ifdef OCC192_193
93 n = (Standard_Integer ) ((a-ra)/myAlpha + Sign(0.5, a-ra)) ;
94#else
95 n = (Standard_Integer ) ((a-ra)/myAlpha +0.5 ) ;
96#endif
97
98#ifdef CSR577
99 Standard_Real cs=0,sn=0;
100 Standard_Boolean done = Standard_False;
101 Standard_Integer nmax = 2*myDivisionNumber;
102 Standard_Integer nquad,qmax;
103
104 if( ra == 0. ) {
105 nquad = 4; qmax = nmax/nquad;
106 if( (n == 0) || (!(nmax % nquad) && !(n % qmax)) ) {
107 Standard_Integer q = n/qmax;
108 switch (q) {
109 default:
110 case 0:
111 cs = 1.; sn = 0.;
112 break;
113 case 1:
114 cs = 0.; sn = 1.;
115 break;
116 case 2:
117 cs = -1.; sn = 0.;
118 break;
119 case 3:
120 cs = 0.; sn = -1.;
121 break;
122 }
123 done = Standard_True;
124 } else {
125 nquad = 2; qmax = nmax/nquad;
126 if( !(nmax % nquad) && !(n % qmax) ) {
127 Standard_Integer q = n/qmax;
128 switch (q) {
129 default:
130 case 0:
131 cs = 1.; sn = 0.;
132 break;
133 case 1:
134 cs = -1.; sn = 0.;
135 break;
136 }
137 done = Standard_True;
138 }
139 }
140 }
141
142 if( !done ) {
143 Standard_Real ang = ra + Standard_Real(n)*myAlpha;
144 cs = Cos(ang); sn = Sin(ang);
145 }
146#else
147 Standard_Real ang = RotationAngle()+ Standard_Real(n)*myAlpha;
148 Standard_Real cs = Cos(ang);
149 Standard_Real sn = Sin(ang);
150#endif
151 gridX = xo + cs * radius;
152 gridY = yo + sn * radius;
153#ifdef TRACE
154 cout << "Aspect_CircularGrid::Compute (" << Quantity_Length (X) << ", "
155 << Quantity_Length (Y) << ", " << Quantity_Length (gridX) << ", "
156 << Quantity_Length (gridY) << ")" << endl;
157#endif
158}
159
160Quantity_Length Aspect_CircularGrid::RadiusStep() const {
161 return myRadiusStep;
162}
163
164Standard_Integer Aspect_CircularGrid::DivisionNumber () const {
165return myDivisionNumber;
166}
167
168void Aspect_CircularGrid::Init () {
c6541a0c 169 myAlpha = M_PI / Standard_Real(myDivisionNumber);
7fd59977 170 myA1 = Cos(myAlpha); myB1=Sin(myAlpha);
171}