973c2be1 |
1 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
2 | // |
973c2be1 |
3 | // This file is part of Open CASCADE Technology software library. |
b311480e |
4 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
10 | // |
973c2be1 |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
b311480e |
13 | |
42cf5bc1 |
14 | #include <Aspect_CircularGrid.hxx> |
475c2302 |
15 | |
42cf5bc1 |
16 | #include <Standard_NegativeValue.hxx> |
17 | #include <Standard_NullValue.hxx> |
7fd59977 |
18 | #include <Standard_NumericError.hxx> |
42cf5bc1 |
19 | |
92efcf78 |
20 | IMPLEMENT_STANDARD_RTTIEXT(Aspect_CircularGrid,Aspect_Grid) |
21 | |
7fd59977 |
22 | Aspect_CircularGrid::Aspect_CircularGrid |
ee2be2a8 |
23 | (const Standard_Real aRadiusStep, |
7fd59977 |
24 | const Standard_Integer aDivisionNumber, |
ee2be2a8 |
25 | const Standard_Real anXOrigin, |
26 | const Standard_Real anYOrigin, |
27 | const Standard_Real aRotationAngle) |
7fd59977 |
28 | :Aspect_Grid(anXOrigin,anYOrigin,aRotationAngle),myRadiusStep(aRadiusStep), |
29 | myDivisionNumber(aDivisionNumber) { |
30 | } |
31 | |
32 | void Aspect_CircularGrid::SetRadiusStep(const Standard_Real aRadiusStep) { |
33 | Standard_NegativeValue_Raise_if(aRadiusStep < 0., "invalid radius step"); |
34 | Standard_NullValue_Raise_if(aRadiusStep == 0. , "invalid radius step"); |
35 | myRadiusStep= aRadiusStep; |
36 | Init(); |
37 | UpdateDisplay(); |
38 | } |
39 | void Aspect_CircularGrid::SetDivisionNumber(const Standard_Integer aNumber) { |
40 | Standard_NegativeValue_Raise_if(aNumber < 0., "invalid division number"); |
41 | Standard_NullValue_Raise_if(aNumber == 0. , "invalid division number"); |
42 | myDivisionNumber = aNumber; |
43 | Init(); |
44 | UpdateDisplay(); |
45 | } |
46 | void Aspect_CircularGrid::SetGridValues |
ee2be2a8 |
47 | (const Standard_Real theXOrigin, |
48 | const Standard_Real theYOrigin, |
49 | const Standard_Real theRadiusStep, |
7fd59977 |
50 | const Standard_Integer theDivisionNumber, |
ee2be2a8 |
51 | const Standard_Real theRotationAngle) { |
7fd59977 |
52 | myXOrigin = theXOrigin; |
53 | myYOrigin = theYOrigin; |
54 | Standard_NegativeValue_Raise_if(theRadiusStep < 0., "invalid radius step"); |
55 | Standard_NullValue_Raise_if(theRadiusStep == 0. , "invalid radius step"); |
56 | myRadiusStep= theRadiusStep; |
57 | Standard_NegativeValue_Raise_if(theDivisionNumber < 0., "invalid division number"); |
58 | Standard_NullValue_Raise_if(theDivisionNumber == 0. , "invalid division number"); |
59 | myDivisionNumber = theDivisionNumber; |
60 | myRotationAngle = theRotationAngle; |
61 | Init(); |
62 | UpdateDisplay(); |
63 | } |
ee2be2a8 |
64 | void Aspect_CircularGrid::Compute(const Standard_Real X, |
65 | const Standard_Real Y, |
66 | Standard_Real& gridX, |
67 | Standard_Real& gridY) const { |
7fd59977 |
68 | |
7fd59977 |
69 | Standard_Real xo = XOrigin(); |
70 | Standard_Real yo = YOrigin(); |
71 | Standard_Real d = Sqrt( (xo-X)*(xo-X) + (yo-Y)*(yo-Y) ); |
72 | Standard_Integer n = (Standard_Integer ) ( d/myRadiusStep + 0.5 ) ; |
73 | Standard_Real radius = Standard_Real(n) * myRadiusStep; |
74 | Standard_Real cosinus = (X-xo)/d; |
75 | Standard_Real a = ACos(cosinus); |
76 | Standard_Real ra = RotationAngle(); |
c6541a0c |
77 | if ( Y < yo ) a = 2 * M_PI - a; |
7fd59977 |
78 | n = (Standard_Integer ) ((a-ra)/myAlpha + Sign(0.5, a-ra)) ; |
7fd59977 |
79 | |
7fd59977 |
80 | Standard_Real cs=0,sn=0; |
81 | Standard_Boolean done = Standard_False; |
82 | Standard_Integer nmax = 2*myDivisionNumber; |
83 | Standard_Integer nquad,qmax; |
84 | |
85 | if( ra == 0. ) { |
86 | nquad = 4; qmax = nmax/nquad; |
87 | if( (n == 0) || (!(nmax % nquad) && !(n % qmax)) ) { |
88 | Standard_Integer q = n/qmax; |
89 | switch (q) { |
90 | default: |
91 | case 0: |
92 | cs = 1.; sn = 0.; |
93 | break; |
94 | case 1: |
95 | cs = 0.; sn = 1.; |
96 | break; |
97 | case 2: |
98 | cs = -1.; sn = 0.; |
99 | break; |
100 | case 3: |
101 | cs = 0.; sn = -1.; |
102 | break; |
103 | } |
104 | done = Standard_True; |
105 | } else { |
106 | nquad = 2; qmax = nmax/nquad; |
107 | if( !(nmax % nquad) && !(n % qmax) ) { |
108 | Standard_Integer q = n/qmax; |
109 | switch (q) { |
110 | default: |
111 | case 0: |
112 | cs = 1.; sn = 0.; |
113 | break; |
114 | case 1: |
115 | cs = -1.; sn = 0.; |
116 | break; |
117 | } |
118 | done = Standard_True; |
119 | } |
120 | } |
121 | } |
122 | |
123 | if( !done ) { |
124 | Standard_Real ang = ra + Standard_Real(n)*myAlpha; |
125 | cs = Cos(ang); sn = Sin(ang); |
126 | } |
7fd59977 |
127 | gridX = xo + cs * radius; |
128 | gridY = yo + sn * radius; |
7fd59977 |
129 | } |
130 | |
ee2be2a8 |
131 | Standard_Real Aspect_CircularGrid::RadiusStep() const { |
7fd59977 |
132 | return myRadiusStep; |
133 | } |
134 | |
135 | Standard_Integer Aspect_CircularGrid::DivisionNumber () const { |
136 | return myDivisionNumber; |
137 | } |
138 | |
139 | void Aspect_CircularGrid::Init () { |
c6541a0c |
140 | myAlpha = M_PI / Standard_Real(myDivisionNumber); |
7fd59977 |
141 | myA1 = Cos(myAlpha); myB1=Sin(myAlpha); |
142 | } |