0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / GeomLib / GeomLib_Tool.cxx
CommitLineData
b311480e 1// Created on: 2003-03-18
2// Created by: Oleg FEDYAEV
973c2be1 3// Copyright (c) 2003-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
7fd59977 16
42cf5bc1 17#include <ElCLib.hxx>
18#include <ElSLib.hxx>
19#include <Extrema_ExtPC.hxx>
20#include <Extrema_ExtPC2d.hxx>
21#include <Extrema_ExtPS.hxx>
22#include <Geom2d_BezierCurve.hxx>
23#include <Geom2d_BSplineCurve.hxx>
24#include <Geom2d_Circle.hxx>
25#include <Geom2d_Curve.hxx>
26#include <Geom2d_Ellipse.hxx>
27#include <Geom2d_Hyperbola.hxx>
28#include <Geom2d_Line.hxx>
29#include <Geom2d_OffsetCurve.hxx>
30#include <Geom2d_Parabola.hxx>
31#include <Geom2d_TrimmedCurve.hxx>
32#include <Geom2dAdaptor_Curve.hxx>
33#include <Geom_BezierCurve.hxx>
34#include <Geom_BezierSurface.hxx>
35#include <Geom_BSplineCurve.hxx>
36#include <Geom_BSplineSurface.hxx>
7fd59977 37#include <Geom_Circle.hxx>
42cf5bc1 38#include <Geom_ConicalSurface.hxx>
39#include <Geom_Curve.hxx>
40#include <Geom_CylindricalSurface.hxx>
7fd59977 41#include <Geom_Ellipse.hxx>
7fd59977 42#include <Geom_Hyperbola.hxx>
42cf5bc1 43#include <Geom_Line.hxx>
7fd59977 44#include <Geom_OffsetCurve.hxx>
42cf5bc1 45#include <Geom_OffsetSurface.hxx>
46#include <Geom_Parabola.hxx>
7fd59977 47#include <Geom_Plane.hxx>
7fd59977 48#include <Geom_RectangularTrimmedSurface.hxx>
42cf5bc1 49#include <Geom_SphericalSurface.hxx>
50#include <Geom_Surface.hxx>
7fd59977 51#include <Geom_SurfaceOfLinearExtrusion.hxx>
52#include <Geom_SurfaceOfRevolution.hxx>
42cf5bc1 53#include <Geom_ToroidalSurface.hxx>
54#include <Geom_TrimmedCurve.hxx>
7fd59977 55#include <GeomAdaptor_Curve.hxx>
56#include <GeomAdaptor_Surface.hxx>
42cf5bc1 57#include <GeomLib_Tool.hxx>
58#include <gp_Circ.hxx>
7fd59977 59#include <gp_Circ2d.hxx>
42cf5bc1 60#include <gp_Cone.hxx>
61#include <gp_Cylinder.hxx>
62#include <gp_Elips.hxx>
7fd59977 63#include <gp_Elips2d.hxx>
42cf5bc1 64#include <gp_Hypr.hxx>
7fd59977 65#include <gp_Hypr2d.hxx>
42cf5bc1 66#include <gp_Lin.hxx>
67#include <gp_Lin2d.hxx>
68#include <gp_Parab.hxx>
69#include <gp_Parab2d.hxx>
70#include <gp_Pln.hxx>
71#include <gp_Pnt.hxx>
72#include <gp_Pnt2d.hxx>
73#include <gp_Sphere.hxx>
74#include <gp_Torus.hxx>
75#include <gp_Vec.hxx>
7fd59977 76
77// The functions Parameter(s) are used to compute parameter(s) of point
78// on curves and surfaces. The main rule is that tested point must lied
79// on curves or surfaces otherwise the resulted parameter(s) may be wrong.
e2ca538a 80// To make search process more common the MaxDist value is used to define
81// the proximity of point to curve or surface. It is clear that this MaxDist
7fd59977 82// value can't be too high to be not in conflict with previous rule.
7fd59977 83static const Standard_Real PARTOLERANCE = 1.e-9;
84
85//=======================================================================
ce77f999 86//function : Parameter
87//purpose : Get parameter on curve of given point
e2ca538a 88// return FALSE if point is far from curve than MaxDist
ce77f999 89// or computation fails
7fd59977 90//=======================================================================
91
ce77f999 92Standard_Boolean GeomLib_Tool::Parameter(const Handle(Geom_Curve)& Curve,
93 const gp_Pnt& Point,
e2ca538a 94 const Standard_Real MaxDist,
ce77f999 95 Standard_Real& U)
7fd59977 96{
ce77f999 97 if( Curve.IsNull() ) return Standard_False;
98 //
99 U = 0.;
e2ca538a 100 Standard_Real aTol = MaxDist * MaxDist;
ce77f999 101 //
e2ca538a 102 GeomAdaptor_Curve aGAC(Curve);
103 Extrema_ExtPC extrema(Point,aGAC);
104 //
105 if( !extrema.IsDone() ) return Standard_False;
106 //
107 Standard_Integer n = extrema.NbExt();
108 if( n <= 0 ) return Standard_False;
109 //
110 Standard_Integer i = 0, iMin = 0;
111 Standard_Real Dist2Min = RealLast();
112 for( i = 1; i <= n; i++ )
ce77f999 113 {
e2ca538a 114 if (extrema.SquareDistance(i) < Dist2Min)
7fd59977 115 {
e2ca538a 116 iMin = i;
117 Dist2Min = extrema.SquareDistance(i);
7fd59977 118 }
ce77f999 119 }
e2ca538a 120 if( iMin != 0 && Dist2Min <= aTol )
ce77f999 121 {
e2ca538a 122 U = (extrema.Point(iMin)).Parameter();
ce77f999 123 }
e2ca538a 124 else
125 {
126 return Standard_False;
127 }
128
7fd59977 129 return Standard_True;
130
131}
132
133//=======================================================================
134//function : Parameters
135//purpose : Get parameters on surface of given point
e2ca538a 136// return FALSE if point is far from surface than MaxDist
7fd59977 137// or computation fails
138//=======================================================================
139
140Standard_Boolean GeomLib_Tool::Parameters(const Handle(Geom_Surface)& Surface,
ce77f999 141 const gp_Pnt& Point,
e2ca538a 142 const Standard_Real MaxDist,
ce77f999 143 Standard_Real& U,
144 Standard_Real& V)
7fd59977 145{
ce77f999 146 if( Surface.IsNull() ) return Standard_False;
147 //
7fd59977 148 U = 0.;
149 V = 0.;
e2ca538a 150 Standard_Real aTol = MaxDist * MaxDist;
ce77f999 151 //
e2ca538a 152 GeomAdaptor_Surface aGAS(Surface);
153 Standard_Real aTolU = PARTOLERANCE, aTolV = PARTOLERANCE;
154 //
155 Extrema_ExtPS extrema(Point,aGAS,aTolU,aTolV);
156 //
157 if( !extrema.IsDone() ) return Standard_False;
158 //
159 Standard_Integer n = extrema.NbExt();
160 if( n <= 0 ) return Standard_False;
161 //
162 Standard_Real Dist2Min = RealLast();
163 Standard_Integer i = 0, iMin = 0;
164 for( i = 1; i <= n; i++ )
ce77f999 165 {
e2ca538a 166 if( extrema.SquareDistance(i) < Dist2Min )
ce77f999 167 {
e2ca538a 168 Dist2Min = extrema.SquareDistance(i);
169 iMin = i;
ce77f999 170 }
ce77f999 171 }
e2ca538a 172 if( iMin != 0 && Dist2Min <= aTol)
ce77f999 173 {
e2ca538a 174 extrema.Point(iMin).Parameter(U,V);
175 }
176 else
177 {
178 return Standard_False;
ce77f999 179 }
ce77f999 180
7fd59977 181 return Standard_True;
182
183}
184
185//=======================================================================
ce77f999 186//function : Parameter
187//purpose : Get parameter on curve of given point
e2ca538a 188// return FALSE if point is far from curve than MaxDist
ce77f999 189// or computation fails
7fd59977 190//=======================================================================
191
ce77f999 192Standard_Boolean GeomLib_Tool::Parameter(const Handle(Geom2d_Curve)& Curve,
193 const gp_Pnt2d& Point,
e2ca538a 194 const Standard_Real MaxDist,
ce77f999 195 Standard_Real& U)
7fd59977 196{
ce77f999 197 if( Curve.IsNull() ) return Standard_False;
198 //
199 U = 0.;
e2ca538a 200 Standard_Real aTol = MaxDist * MaxDist;
201 //
202 Geom2dAdaptor_Curve aGAC(Curve);
203 Extrema_ExtPC2d extrema(Point,aGAC);
204 if( !extrema.IsDone() ) return Standard_False;
205 Standard_Integer n = extrema.NbExt();
206 if( n <= 0 ) return Standard_False;
207 Standard_Integer i = 0, iMin = 0;
208 Standard_Real Dist2Min = RealLast();
209 for ( i = 1; i <= n; i++ )
ce77f999 210 {
e2ca538a 211 if( extrema.SquareDistance(i) < Dist2Min )
ce77f999 212 {
e2ca538a 213 Dist2Min = extrema.SquareDistance(i);
214 iMin = i;
7fd59977 215 }
ce77f999 216 }
e2ca538a 217 if( iMin != 0 && Dist2Min <= aTol )
ce77f999 218 {
e2ca538a 219 U = (extrema.Point(iMin)).Parameter();
220 }
221 else
222 {
223 return Standard_False;
ce77f999 224 }
ce77f999 225
7fd59977 226 return Standard_True;
227}