0028838: Configuration - undefine macros coming from X11 headers in place of collision
[occt.git] / src / BRepOffset / BRepOffset.cxx
1 // Created on: 1995-10-25
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License 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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <BRepOffset.hxx>
19 #include <Geom_BSplineSurface.hxx>
20 #include <Geom_ConicalSurface.hxx>
21 #include <Geom_CylindricalSurface.hxx>
22 #include <Geom_OffsetSurface.hxx>
23 #include <Geom_Plane.hxx>
24 #include <Geom_RectangularTrimmedSurface.hxx>
25 #include <Geom_SphericalSurface.hxx>
26 #include <Geom_Surface.hxx>
27 #include <Geom_SurfaceOfLinearExtrusion.hxx>
28 #include <Geom_SurfaceOfRevolution.hxx>
29 #include <Geom_ToroidalSurface.hxx>
30 #include <gp_Ax1.hxx>
31 #include <gp_Ax3.hxx>
32 #include <gp_Dir.hxx>
33 #include <gp_Vec.hxx>
34 #include <Precision.hxx>
35
36 //=======================================================================
37 //function : Surface
38 //purpose  : 
39 //=======================================================================
40 Handle(Geom_Surface) BRepOffset::Surface(const Handle(Geom_Surface)& Surface,
41                                          const Standard_Real Offset,
42                                                BRepOffset_Status& theStatus)
43 {
44   Standard_Real Tol = Precision::Confusion();
45
46   theStatus = BRepOffset_Good;
47   Handle(Geom_Surface) Result;
48   
49   Handle(Standard_Type) TheType = Surface->DynamicType();
50
51   if (TheType == STANDARD_TYPE(Geom_Plane)) {
52     Handle(Geom_Plane) P =
53       Handle(Geom_Plane)::DownCast(Surface);
54     gp_Vec T = P->Position().XDirection()^P->Position().YDirection();
55     T *= Offset;
56     Result = Handle(Geom_Plane)::DownCast(P->Translated(T));
57   }
58   else if (TheType == STANDARD_TYPE(Geom_CylindricalSurface)) {
59     Handle(Geom_CylindricalSurface) C =
60       Handle(Geom_CylindricalSurface)::DownCast(Surface);
61     Standard_Real Radius = C->Radius();
62     gp_Ax3 Axis = C->Position();
63     if (Axis.Direct()) 
64       Radius += Offset;
65     else 
66       Radius -= Offset;
67     if ( Radius >= Tol ) {
68       Result = new Geom_CylindricalSurface( Axis, Radius);
69     }
70     else if ( Radius <= -Tol ){
71       Axis.Rotate(gp_Ax1(Axis.Location(),Axis.Direction()),M_PI);
72       Result = new Geom_CylindricalSurface( Axis, Abs(Radius));
73       theStatus = BRepOffset_Reversed;
74     }
75     else {
76       theStatus = BRepOffset_Degenerated;
77     }
78   }
79   else if (TheType == STANDARD_TYPE(Geom_ConicalSurface)) {
80     Handle(Geom_ConicalSurface) C =
81       Handle(Geom_ConicalSurface)::DownCast(Surface);
82     Standard_Real Alpha = C->SemiAngle();
83     Standard_Real Radius = C->RefRadius() + Offset * Cos(Alpha);
84     gp_Ax3 Axis = C->Position();
85     if ( Radius >= 0.) {
86       gp_Vec Z( Axis.Direction());
87       Z *= - Offset * Sin(Alpha);
88       Axis.Translate(Z);
89     }
90     else {
91       Radius = -Radius; 
92       gp_Vec Z( Axis.Direction());
93       Z *= - Offset * Sin(Alpha);
94       Axis.Translate(Z);
95       Axis.Rotate(gp_Ax1(Axis.Location(),Axis.Direction()),M_PI);
96       Alpha = -Alpha; 
97     }
98     Result = new Geom_ConicalSurface(Axis, Alpha, Radius);
99   }
100   else if (TheType == STANDARD_TYPE(Geom_SphericalSurface)) {
101     Handle(Geom_SphericalSurface) S = 
102       Handle(Geom_SphericalSurface)::DownCast(Surface);
103     Standard_Real Radius = S->Radius();
104     gp_Ax3 Axis = S->Position();
105     if (Axis.Direct()) 
106       Radius += Offset;
107     else 
108       Radius -= Offset;
109     if ( Radius >= Tol) {
110       Result = new Geom_SphericalSurface(Axis, Radius);
111     }
112     else if ( Radius <= -Tol ) {
113       Axis.Rotate(gp_Ax1(Axis.Location(),Axis.Direction()),M_PI);
114       Axis.ZReverse();
115       Result = new Geom_SphericalSurface(Axis, -Radius);
116       theStatus = BRepOffset_Reversed;
117     }
118     else {
119       theStatus = BRepOffset_Degenerated;
120     }
121   }
122   else if (TheType == STANDARD_TYPE(Geom_ToroidalSurface)) {
123     Handle(Geom_ToroidalSurface) S = 
124       Handle(Geom_ToroidalSurface)::DownCast(Surface);
125     Standard_Real MajorRadius = S->MajorRadius();
126     Standard_Real MinorRadius = S->MinorRadius();
127     gp_Ax3 Axis = S->Position();
128     if (MinorRadius < MajorRadius) {  // A FINIR
129       if (Axis.Direct())
130         MinorRadius += Offset;
131       else 
132         MinorRadius -= Offset;
133       if (MinorRadius >= Tol) {
134         Result = new Geom_ToroidalSurface(Axis,MajorRadius,MinorRadius);
135       }
136       else if (MinorRadius <= -Tol) {
137         theStatus = BRepOffset_Reversed;
138       }
139       else {
140         theStatus = BRepOffset_Degenerated;
141       }
142     }
143   }
144   else if (TheType == STANDARD_TYPE(Geom_SurfaceOfRevolution)) {
145   }
146   else if (TheType == STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) {
147   }
148   else if (TheType == STANDARD_TYPE(Geom_BSplineSurface)) {
149   }
150   else if (TheType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
151     Handle(Geom_RectangularTrimmedSurface) S = 
152       Handle(Geom_RectangularTrimmedSurface)::DownCast(Surface);
153     Standard_Real U1,U2,V1,V2;
154     S->Bounds(U1,U2,V1,V2);
155     Handle(Geom_Surface) Off = BRepOffset::Surface (S->BasisSurface(), Offset, theStatus);
156     Result = new Geom_RectangularTrimmedSurface (Off,U1,U2,V1,V2);
157   }
158   else if (TheType == STANDARD_TYPE(Geom_OffsetSurface)) {
159   }
160
161   if ( Result.IsNull()) {
162     Result = new Geom_OffsetSurface( Surface, Offset);
163   }
164   
165   return Result;
166 }
167
168