summaryrefslogtreecommitdiff
path: root/src/HLRBRep/HLRBRep_Surface.cxx
blob: 706a47a4b7b6d72ac9b9d22d0ac53bcbe2e57889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// File:	HLRBRep_Surface.cxx
// Created:	Fri Mar 13 11:08:32 1992
// Author:	Christophe MARION
//		<cma@sdsun2>

#include <HLRBRep_Surface.ixx>
#include <HLRBRep_BSurfaceTool.hxx>
#include <gp_Pln.hxx>
#include <GProp_PEquation.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <BRepClass_FaceClassifier.hxx>
#include <HLRAlgo_Projector.hxx>
#include <HLRBRep_Curve.hxx>
#include <TColStd_Array2OfReal.hxx>
#include <Geom_BSplineSurface.hxx>
#include <Geom_BezierSurface.hxx>

//=======================================================================
//function : HLRBRep_Surface
//purpose  : 
//=======================================================================

HLRBRep_Surface::HLRBRep_Surface ()
{
}

//=======================================================================
//function : Surface
//purpose  : 
//=======================================================================

void HLRBRep_Surface::Surface (const TopoDS_Face& F)
{
  mySurf.Initialize(F,Standard_False);
  GeomAbs_SurfaceType typ = HLRBRep_BSurfaceTool::GetType(mySurf);
  switch (typ) {

  case GeomAbs_Plane :
  case GeomAbs_Cylinder :
  case GeomAbs_Cone :
  case GeomAbs_Sphere :
  case GeomAbs_Torus :
    // unchanged type
    myType = typ;
    break;
    
  case GeomAbs_BezierSurface :
    if (HLRBRep_BSurfaceTool::UDegree(mySurf) == 1 &&
	HLRBRep_BSurfaceTool::VDegree(mySurf) == 1) {
      myType = GeomAbs_Plane;
    }
    else
      myType = typ;
    break;
    
    default :
    myType = GeomAbs_OtherSurface;
    break;
  }
}

//=======================================================================
//function : SideRowsOfPoles
//purpose  : 
//=======================================================================

Standard_Boolean 
HLRBRep_Surface::SideRowsOfPoles (const Standard_Real tol,
				  const Standard_Integer nbuPoles,
				  const Standard_Integer nbvPoles,
				  TColgp_Array2OfPnt& Pnt) const
{
  Standard_Integer iu,iv;
  Standard_Real x0,y0,x,y,z;
  Standard_Boolean result;
  Standard_Real tole = (Standard_Real)tol;
  const gp_Trsf& T = ((HLRAlgo_Projector*) myProj)->Transformation();

  for (iu = 1; iu <= nbuPoles; iu++) {
    
    for (iv = 1; iv <= nbvPoles; iv++)
      Pnt(iu,iv).Transform(T);
  }
  result = Standard_True;

  for (iu = 1; iu <= nbuPoles && result; iu++) {         // Side iso u ?
    Pnt(iu,1).Coord(x0,y0,z);
    
    for (iv = 2; iv <= nbvPoles && result; iv++) {
      Pnt(iu,iv).Coord(x,y,z);
      result = Abs(x-x0) < tole && Abs(y-y0) < tole;
    }
  }
  if (result) return result;
  result = Standard_True;
  
  for (iv = 1; iv <= nbvPoles && result; iv++) {         // Side iso v ?
    Pnt(1,iv).Coord(x0,y0,z);
    
    for (iu = 2; iu <= nbuPoles && result; iu++) {
      Pnt(iu,iv).Coord(x,y,z);
      result = Abs(x-x0) < tole && Abs(y-y0) < tole;
    }
  }
  if (result) return result;

  // Are the Poles in a Side Plane ?
  TColgp_Array1OfPnt p(1,nbuPoles*nbvPoles);
  Standard_Integer i = 0;

  for (iu = 1; iu <= nbuPoles; iu++) {
    
    for (iv = 1; iv <= nbvPoles; iv++) { 
      i++;
      p(i) = Pnt(iu,iv);
    }
  }

  GProp_PEquation Pl(p,(Standard_Real)tol);
  if (Pl.IsPlanar())
    result = Abs(Pl.Plane().Axis().Direction().Z()) < 0.0001;

  return result;
}

//=======================================================================
//function : IsSide
//purpose  : 
//=======================================================================

Standard_Boolean 
HLRBRep_Surface::IsSide (const Standard_Real tolF,
			 const Standard_Real toler) const
{
  gp_Pnt Pt;
  gp_Vec D;
  Standard_Real r;

  if (myType == GeomAbs_Plane) {
    gp_Pln Pl = Plane();
    gp_Ax1 A  = Pl.Axis();
    Pt = A.Location();
    D  = A.Direction();
    Pt.Transform(((HLRAlgo_Projector*) myProj)->Transformation());
    D .Transform(((HLRAlgo_Projector*) myProj)->Transformation());
    if (((HLRAlgo_Projector*) myProj)->Perspective()) {
      r = D.Z() * ((HLRAlgo_Projector*) myProj)->Focus() - 
	( D.X() * Pt.X() + D.Y() * Pt.Y() + D.Z() * Pt.Z() );
    }
    else r= D.Z();
    return Abs(r) < toler;
  }
  else if (myType == GeomAbs_Cylinder) {
    if (((HLRAlgo_Projector*) myProj)->Perspective()) return Standard_False;
    gp_Cylinder Cyl = HLRBRep_BSurfaceTool::Cylinder(mySurf);
    gp_Ax1 A = Cyl.Axis();
    D  = A.Direction();
    D .Transform(((HLRAlgo_Projector*) myProj)->Transformation());
    r = Sqrt(D.X() * D.X() + D.Y() * D.Y());
    return r < toler;
  }
  else if (myType == GeomAbs_Cone) {
    if (!((HLRAlgo_Projector*) myProj)->Perspective()) return Standard_False;
    gp_Cone Con = HLRBRep_BSurfaceTool::Cone(mySurf);
    Pt = Con.Apex();
    Pt.Transform(((HLRAlgo_Projector*) myProj)->Transformation());
    Standard_Real tol = 0.001;
    return Pt.IsEqual(gp_Pnt(0,0,((HLRAlgo_Projector*) myProj)->Focus()),tol);
  }
  else if (myType == GeomAbs_BezierSurface) {
    if (((HLRAlgo_Projector*) myProj)->Perspective()) return Standard_False;
    Standard_Integer nu = HLRBRep_BSurfaceTool::NbUPoles(mySurf);
    Standard_Integer nv = HLRBRep_BSurfaceTool::NbVPoles(mySurf);
    TColgp_Array2OfPnt Pnt(1,nu,1,nv);
    HLRBRep_BSurfaceTool::Bezier(mySurf)->Poles(Pnt);
    return SideRowsOfPoles (tolF,nu,nv,Pnt);
  }
  else if (myType == GeomAbs_BSplineSurface) {
    if (((HLRAlgo_Projector*) myProj)->Perspective()) return Standard_False;
    Standard_Integer nu = HLRBRep_BSurfaceTool::NbUPoles(mySurf);
    Standard_Integer nv = HLRBRep_BSurfaceTool::NbVPoles(mySurf);
    TColgp_Array2OfPnt Pnt(1,nu,1,nv);
    TColStd_Array2OfReal W(1,nu,1,nv);
    HLRBRep_BSurfaceTool::BSpline(mySurf)->Poles(Pnt);
    HLRBRep_BSurfaceTool::BSpline(mySurf)->Weights(W);
    return SideRowsOfPoles (tolF,nu,nv,Pnt);
  }
  else return Standard_False;
}

//=======================================================================
//function : IsAbove
//purpose  : 
//=======================================================================

Standard_Boolean  
HLRBRep_Surface::IsAbove (const Standard_Boolean back,
			  const Standard_Address A,
			  const Standard_Real tol) const
{ 
  Standard_Boolean planar = (myType == GeomAbs_Plane);
  if (planar) {
    gp_Pln Pl = Plane();
    Standard_Real a,b,c,d;
    Pl.Coefficients(a,b,c,d);
    Standard_Real u,u1,u2,dd,x,y,z;
    gp_Pnt P;
    u1 = ((HLRBRep_Curve*)A)->Parameter3d
      (((HLRBRep_Curve*)A)->FirstParameter());
    u2 = ((HLRBRep_Curve*)A)->Parameter3d
      (((HLRBRep_Curve*)A)->LastParameter());
    u=u1;
    ((HLRBRep_Curve*)A)->D0(u,P);
    P.Coord(x,y,z);
    dd = a*x + b*y + c*z + d;
    if (back) dd = -dd;
    if (dd < -tol) return Standard_False;
    if (((HLRBRep_Curve*)A)->GetType() != GeomAbs_Line) {
      Standard_Integer nbPnt = 30;
      Standard_Real step = (u2-u1)/(nbPnt+1);
      for (Standard_Integer i = 1; i <= nbPnt; i++) {
	u += step;
	((HLRBRep_Curve*)A)->D0(u,P);
	P.Coord(x,y,z);
	dd = a*x + b*y + c*z + d;
	if (back) dd = -dd;
	if (dd < -tol) return Standard_False;
      }
    }
    u = u2;
    ((HLRBRep_Curve*)A)->D0(u,P);
    P.Coord(x,y,z);
    dd = a*x + b*y + c*z + d;
    if (back) dd = -dd;
    if (dd < -tol) return Standard_False;
    return Standard_True;
  }
  else return Standard_False; 
}

//=======================================================================
//function : Value
//purpose  : 
//=======================================================================

gp_Pnt HLRBRep_Surface::Value (const Standard_Real U,
			       const Standard_Real V) const
{ 
  gp_Pnt P;
  D0(U,V,P);
  return P;
}

//=======================================================================
//function : Plane
//purpose  : 
//=======================================================================

gp_Pln  HLRBRep_Surface::Plane () const 
{
  GeomAbs_SurfaceType typ = HLRBRep_BSurfaceTool::GetType(mySurf);
  switch (typ) {
  case GeomAbs_BezierSurface :
    {
      gp_Pnt P;
      gp_Vec D1U;
      gp_Vec D1V;
      D1(0.5,0.5,P,D1U,D1V);
      return gp_Pln(P,gp_Dir(D1U.Crossed(D1V)));
    }
    
    default :
    return HLRBRep_BSurfaceTool::Plane(mySurf);
  }
}