0032308: Configuration - make Xlib dependency optional
[occt.git] / src / Aspect / Aspect_RectangularGrid.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 <Aspect_RectangularGrid.hxx>
15
16 #include <Standard_NegativeValue.hxx>
17 #include <Standard_NullValue.hxx>
18 #include <Standard_NumericError.hxx>
19
20 IMPLEMENT_STANDARD_RTTIEXT(Aspect_RectangularGrid,Aspect_Grid)
21
22 Aspect_RectangularGrid::Aspect_RectangularGrid(
23                                const Standard_Real aXStep,
24                                const Standard_Real aYStep,
25                                const Standard_Real anXOrigin,
26                                const Standard_Real anYOrigin,
27                                const Standard_Real aFirstAngle,
28                                const Standard_Real aSecondAngle,
29                                const Standard_Real aRotationAngle)
30 :Aspect_Grid(anXOrigin,anYOrigin,aRotationAngle),myXStep(aXStep),myYStep(aYStep),myFirstAngle(aFirstAngle),mySecondAngle(aSecondAngle)
31
32 {
33   Standard_NumericError_Raise_if(!CheckAngle (aFirstAngle,mySecondAngle),
34                                  "networks are parallel");
35
36   Standard_NegativeValue_Raise_if(aXStep < 0. , "invalid x step");
37   Standard_NegativeValue_Raise_if(aYStep < 0. , "invalid y step");
38   Standard_NullValue_Raise_if(aXStep == 0. , "invalid x step");
39   Standard_NullValue_Raise_if(aYStep == 0. , "invalid y step");
40 }
41
42
43
44 void Aspect_RectangularGrid::SetXStep(const Standard_Real aStep) {
45   Standard_NegativeValue_Raise_if(aStep < 0. , "invalid x step");
46   Standard_NullValue_Raise_if(aStep == 0. , "invalid y step");
47   myXStep = aStep;
48   Init();
49   UpdateDisplay();
50 }
51
52 void Aspect_RectangularGrid::SetYStep(const Standard_Real aStep) {
53   Standard_NegativeValue_Raise_if(aStep < 0. , "invalid x step");
54   Standard_NullValue_Raise_if(aStep == 0. , "invalid y step");
55   myYStep = aStep;
56   Init();
57   UpdateDisplay();
58 }
59
60 void Aspect_RectangularGrid::SetAngle (const Standard_Real anAngle1,
61                                        const Standard_Real anAngle2)
62 {
63   Standard_NumericError_Raise_if(!CheckAngle (anAngle1,anAngle2 ),
64                                  "axis are parallel");
65   myFirstAngle = anAngle1;
66   mySecondAngle = anAngle2;
67   Init();
68   UpdateDisplay();
69 }
70
71 void Aspect_RectangularGrid::SetGridValues(
72         const Standard_Real theXOrigin,
73         const Standard_Real theYOrigin,
74         const Standard_Real theXStep,
75         const Standard_Real theYStep,
76         const Standard_Real theRotationAngle) {
77
78   myXOrigin = theXOrigin;
79   myYOrigin = theYOrigin;
80   Standard_NegativeValue_Raise_if(theXStep < 0. , "invalid x step");
81   Standard_NullValue_Raise_if(theXStep == 0. , "invalid x step");
82   myXStep = theXStep;
83   Standard_NegativeValue_Raise_if(theYStep < 0. , "invalid y step");
84   Standard_NullValue_Raise_if(theYStep == 0. , "invalid y step");
85   myYStep = theYStep;
86   myRotationAngle = theRotationAngle;
87   Init();
88   UpdateDisplay();
89 }
90
91 void Aspect_RectangularGrid::Compute(const Standard_Real X,
92                          const Standard_Real Y,
93                          Standard_Real& gridX,
94                          Standard_Real& gridY) const {
95     Standard_Real D1 = b1 * X - a1 * Y - c1;
96     Standard_Real D2 = b2 * X - a2 * Y - c2;
97     Standard_Integer n1 = Standard_Integer ( Abs(D1)/myXStep + 0.5);
98     Standard_Integer n2 = Standard_Integer ( Abs(D2)/myYStep + 0.5);
99     Standard_Real offset1 = c1 + Standard_Real(n1) * Sign (myXStep , D1);
100     Standard_Real offset2 = c2 + Standard_Real(n2) * Sign (myYStep , D2);
101     Standard_Real Delta = a1*b2 - b1*a2;
102     gridX = ( offset2*a1 - offset1*a2) /Delta;
103     gridY = ( offset2*b1 - offset1*b2) /Delta;
104 }
105
106 Standard_Real Aspect_RectangularGrid::XStep() const {
107   return myXStep;
108 }
109
110 Standard_Real Aspect_RectangularGrid::YStep() const {
111   return myYStep;
112 }
113
114 Standard_Real Aspect_RectangularGrid::FirstAngle() const {
115   return myFirstAngle;
116 }
117
118 Standard_Real Aspect_RectangularGrid::SecondAngle() const {
119   return mySecondAngle;
120 }
121
122 void Aspect_RectangularGrid::Init () {
123
124 //+zov Fixing CTS17856
125 //  a1 = Cos (myFirstAngle + RotationAngle() ); 
126 //  b1 = Sin (myFirstAngle + RotationAngle() );
127 //  c1 = XOrigin() * b1 - YOrigin() * a1;
128 //
129 //  a2 = Cos (mySecondAngle + RotationAngle() + M_PI / 2.); 
130 //  b2 = Sin (mySecondAngle + RotationAngle() + M_PI / 2.);
131 //  c2 = XOrigin() * b2 - YOrigin() * a2;
132
133   Standard_Real angle1 = myFirstAngle + RotationAngle();
134   Standard_Real angle2 = mySecondAngle + RotationAngle();
135   if ( angle1 != 0. ) {
136     a1 = -Sin (angle1); 
137     b1 = Cos (angle1);
138     c1 = XOrigin() * b1 - YOrigin() * a1;
139   } else {
140     a1 = 0.; b1 = 1.; c1 = XOrigin();
141   }
142
143   if ( angle2 != 0. ) {
144     angle2 += M_PI / 2.;
145     a2 = -Sin (angle2); 
146     b2 = Cos (angle2);
147     c2 = XOrigin() * b2 - YOrigin() * a2;
148   } else {
149     a2 = -1.; b2 = 0.; c2 = YOrigin();
150   }
151 //-zov
152 }
153
154 Standard_Boolean Aspect_RectangularGrid::CheckAngle(const Standard_Real alpha,
155                                             const Standard_Real beta) const {
156   return (Abs( Sin(alpha) * Cos(beta + M_PI / 2.) - Cos(alpha) * Sin(beta + M_PI / 2.)) != 0) ;
157 }
158
159 //=======================================================================
160 //function : DumpJson
161 //purpose  : 
162 //=======================================================================
163 void Aspect_RectangularGrid::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
164 {
165   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
166
167   OCCT_DUMP_BASE_CLASS(theOStream, theDepth, Aspect_Grid)
168
169   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myXStep)
170   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myYStep)
171   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myFirstAngle)
172   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, mySecondAngle)
173   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, a1)
174   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, b1)
175   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, c1)
176   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, a2)
177   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, b2)
178   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, c2)
179 }