1 // LProps_Presentation.cpp: implementation of the LProps_Presentation class.
2 // Presentation class: Local properties of curves and surfaces
3 //////////////////////////////////////////////////////////////////////
6 #include "LProps_Presentation.h"
8 #include <Quantity_Color.hxx>
9 #include <Geom_BezierCurve.hxx>
10 #include <Geom_BSplineCurve.hxx>
11 #include <TColgp_Array1OfPnt.hxx>
12 #include <TColStd_Array1OfReal.hxx>
13 #include <TColStd_Array1OfInteger.hxx>
14 #include <GeomLProp_CLProps.hxx>
15 #include <Precision.hxx>
16 #include <Geom_Circle.hxx>
17 #include <TColgp_Array2OfPnt.hxx>
18 #include <Geom_BezierSurface.hxx>
19 #include <GeomLProp_SLProps.hxx>
22 // Initialization of global variable with an instance of this class
23 OCCDemo_Presentation* OCCDemo_Presentation::Current = new LProps_Presentation;
25 // Initialization of array of samples
26 const LProps_Presentation::PSampleFuncType LProps_Presentation::SampleFuncs[] =
28 &LProps_Presentation::sampleBezier,
29 &LProps_Presentation::samplePBSpline,
30 &LProps_Presentation::sampleBezierSurface
39 #define ASTR(_val) TCollection_AsciiString(_val)
40 #define PRINT_COORDS(_p) ASTR((_p).X()) + " " + ASTR((_p).Y()) + " " + ASTR((_p).Z())
42 #define D1Color Quantity_Color(Quantity_NOC_DARKOLIVEGREEN4)
43 #define D2Color Quantity_Color(Quantity_NOC_DARKOLIVEGREEN)
44 #define D3Color Quantity_Color(Quantity_NOC_DARKGREEN)
45 #define TanColor Quantity_Color(Quantity_NOC_GREEN)
46 #define NormColor Quantity_Color(Quantity_NOC_CYAN4)
47 #define CrvtColor Quantity_Color(Quantity_NOC_BLUE1)
49 //////////////////////////////////////////////////////////////////////
50 // Construction/Destruction
51 //////////////////////////////////////////////////////////////////////
53 LProps_Presentation::LProps_Presentation()
56 myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
57 setName ("Local Properties of Curves and Surfaces");
60 //////////////////////////////////////////////////////////////////////
62 //////////////////////////////////////////////////////////////////////
64 void LProps_Presentation::DoSample()
66 getAISContext()->EraseAll();
67 if (myIndex >=0 && myIndex < myNbSamples)
68 (this->*SampleFuncs[myIndex])();
71 //////////////////////////////////////////////////////////////////////
73 //////////////////////////////////////////////////////////////////////
75 void LProps_Presentation::sampleBezier()
77 Standard_CString aName = "BezierCurve";
78 // Create a BezierCurve
79 Standard_Real aCoords[][3] = {
80 {0,0,0},{0,1,0},{1,1,0.2},{1,2,0.2},{2,2,0.4},{2,1,0.4},{3,1,0.6},{3,0,0.6},
81 {2,0,0.8},{2,-1,0},{3,-1,0},{3,-2,-0.5},{4,-2,1},{4,-1,1.2},{5,-1,1.2},
82 {5,0,1.4},{6,0,1.4},{6,-1,1.6},{7,-1,1.6},{7,0,1.8},{8,0,1.8},{8,1,2}
84 Standard_Integer nPoles = sizeof(aCoords)/(sizeof(Standard_Real)*3);
85 TColgp_Array1OfPnt aPoles (1, nPoles);
86 for (Standard_Integer i=0; i < nPoles; i++)
87 aPoles(i+1) = gp_Pnt (aCoords[i][0]*100-500, aCoords[i][1]*100, aCoords[i][2]*100);
88 Handle(Geom_BezierCurve) aCurve = new Geom_BezierCurve (aPoles);
90 Standard_Real aPoints[] = {0, 0.1, 0.4, 0.6, 0.9};
91 showCurveLProps (aCurve, aName, sizeof(aPoints)/sizeof(Standard_Real), aPoints);
94 void LProps_Presentation::samplePBSpline()
96 Standard_CString aName = "BSplineCurve";
97 // Create a Periodic BSplineCurve
98 Standard_Real aCoords[][3] = {
99 {0,0,0},{0,1,0},{1,1,0},{1,0,0},{2,0,-0.3},{2,-1,-0.3},{1,-1,0},
100 {1,-2,0},{0,-2,0},{0,-1,0},{-1,-1,0.3},{-1,0,0.3}
102 Standard_Integer nPoles = sizeof(aCoords)/(sizeof(Standard_Real)*3);
103 TColgp_Array1OfPnt aPoles (1, nPoles);
104 TColStd_Array1OfReal aKnots (1, nPoles+1);
105 TColStd_Array1OfInteger aMults(1, nPoles+1);
106 for (Standard_Integer i=0; i < nPoles; i++)
107 aPoles(i+1) = gp_Pnt (aCoords[i][0]*200, aCoords[i][1]*200, aCoords[i][2]*200);
108 for (i=0; i < aKnots.Length(); i++)
109 aKnots(i+1) = Standard_Real(i)/aKnots.Length();
111 Standard_Integer aDegree = 3;
112 Standard_Boolean isPeriodic = Standard_True;
113 Handle(Geom_BSplineCurve) aCurve =
114 new Geom_BSplineCurve (aPoles, aKnots, aMults, aDegree, isPeriodic);
116 Standard_Real aPoints[] = {0, 0.1, 0.4, 0.6, 0.8};
117 showCurveLProps (aCurve, aName, sizeof(aPoints)/sizeof(Standard_Real), aPoints);
120 void LProps_Presentation::sampleBezierSurface()
122 Standard_CString aName = "BezierSurface";
123 // Create a BezierSurface
124 TColgp_Array2OfPnt aPoles(1,2,1,4); // 8 points
125 TColStd_Array2OfReal aWeights(1,2,1,4);
126 // initializing array of points
127 aPoles.SetValue(1,1,gp_Pnt(0,10,0)); aPoles.SetValue(1,2,gp_Pnt(3.3,6.6,3));
128 aPoles.SetValue(1,3,gp_Pnt(6.6,6.6,-3)); aPoles.SetValue(1,4,gp_Pnt(10,10,0));
129 aPoles.SetValue(2,1,gp_Pnt(0,0,0)); aPoles.SetValue(2,2,gp_Pnt(3.3,3.3,-3));
130 aPoles.SetValue(2,3,gp_Pnt(6.6,3.3,3)); aPoles.SetValue(2,4,gp_Pnt(10,0,0));
132 for (Standard_Integer i=1; i <= aPoles.ColLength(); i++)
133 for (Standard_Integer j=1; j <= aPoles.RowLength(); j++)
134 aPoles(i,j).ChangeCoord() = aPoles(i,j).Coord() * 100 + gp_XYZ(-500,-500,0);
135 //initializing array of weights
136 aWeights.SetValue(1,1,1); aWeights.SetValue(1,2,3);
137 aWeights.SetValue(1,3,9); aWeights.SetValue(1,4,1);
138 aWeights.SetValue(2,1,1); aWeights.SetValue(2,2,2);
139 aWeights.SetValue(2,3,5); aWeights.SetValue(2,4,1);
140 Handle(Geom_BezierSurface) aSurface =
141 new Geom_BezierSurface(aPoles, aWeights);
143 Standard_Real aPoints[][2] = {{0.1,0}, {0.8,0.1}, {0.4,0.3}, {0.6,0.9}, {0.9,0.98}};
144 showSurfaceLProps (aSurface, aName,
145 sizeof(aPoints)/(sizeof(Standard_Real)*2), aPoints);
148 void LProps_Presentation::showCurveLProps (Handle_Geom_Curve theCurve,
149 const Standard_CString theName,
150 const Standard_Integer theNbPoints,
151 const Standard_Real thePoints[])
153 TCollection_AsciiString aTitle ("Local properties of a ");
155 TCollection_AsciiString aText;
157 " // Create a " + theName + EOL
158 " Handle(Geom_" + theName + ") aCurve;" EOL
159 " // initialize aCurve" EOL
160 " // aCurve = ..." EOL EOL
162 " // define parameter at which properties should be computed" EOL
163 " Standard_Real aParam;" EOL
164 " // aParam = ..." EOL EOL
166 " // compute local properties" EOL
167 " Standard_Integer maxOrder = 3;" EOL
168 " Standard_Real aResol = gp::Resolution();" EOL
169 " GeomLProp_CLProps aLProps (theCurve, maxOrder, aResol);" EOL
170 " aLProps.SetParameter (aParam);" EOL
171 " gp_Pnt aPnt = aLProps.Value();" EOL
172 " gp_Vec aVecD1 = aLProps.D1();" EOL
173 " gp_Vec aVecD2 = aLProps.D2();" EOL
174 " gp_Vec aVecD3 = aLProps.D3();" EOL
175 " gp_Dir aTangent, aNormal;" EOL
176 " Standard_Real aCurvature;" EOL
177 " gp_Pnt aCentreOfCurvature;" EOL
178 " Standard_Boolean isCurvatureValid = Standard_False;" EOL
179 " Standard_Boolean isTan = aLProps.IsTangentDefined();" EOL
180 " // the tangent must be defined" EOL
181 " // to compute the curvature and the normal" EOL
184 " aLProps.Tangent (aTangent);" EOL
185 " aCurvature = aLProps.Curvature();" EOL
186 " // the curvature must be non-null and finite" EOL
187 " // to compute the centre of curvature and the normal" EOL
188 " if (aCurvature > aResol && !Precision::IsInfinite(aCurvature))" EOL
190 " isCurvatureValid = Standard_True;" EOL
191 " aLProps.CentreOfCurvature (aCentreOfCurvature);" EOL
192 " aLProps.Normal (aNormal);" EOL
195 "//======================================" EOL;
196 setResultTitle (aTitle.ToCString());
197 setResultText (aText.ToCString());
200 drawCurve (theCurve);
202 Standard_Integer maxOrder = 3;
203 Standard_Real aResol = gp::Resolution();
204 GeomLProp_CLProps aLProps (theCurve, maxOrder, aResol);
205 Handle(AIS_InteractiveObject) aObjs[7];
206 for (int i=0; i < theNbPoints; i++)
210 for (int j=0; j < sizeof(aObjs)/sizeof(Handle(AIS_InteractiveObject)); j++)
211 if (!aObjs[j].IsNull())
212 getAISContext()->Erase(aObjs[j]);
214 // define parameter at which properties should be computed
215 Standard_Real aParam;
216 aParam = thePoints[i];
218 // compute local properties
219 aLProps.SetParameter (aParam);
220 gp_Pnt aPnt = aLProps.Value();
221 gp_Vec aVecD1 = aLProps.D1();
222 gp_Vec aVecD2 = aLProps.D2();
223 gp_Vec aVecD3 = aLProps.D3();
224 gp_Dir aTangent, aNormal;
225 Standard_Real aCurvature;
226 gp_Pnt aCentreOfCurvature;
227 Standard_Boolean isCurvatureValid = Standard_False;
228 Standard_Boolean isTan = aLProps.IsTangentDefined();
229 // the tangent must be defined
230 // to compute the curvature and the normal
233 aLProps.Tangent (aTangent);
234 aCurvature = aLProps.Curvature();
235 // the curvature must be non-null and finite
236 // to compute the centre of curvature and the normal
237 if (aCurvature > aResol && !Precision::IsInfinite(aCurvature))
239 isCurvatureValid = Standard_True;
240 aLProps.CentreOfCurvature (aCentreOfCurvature);
241 aLProps.Normal (aNormal);
246 aText += EOL " Results with parameter ";
247 aText += ASTR(thePoints[i]) + " :" EOL
248 "aPnt = (" + PRINT_COORDS(aPnt) + ")" EOL
249 "aVecD1 = (" + PRINT_COORDS(aVecD1) + ")" EOL
250 "aVecD2 = (" + PRINT_COORDS(aVecD2) + ")" EOL
251 "aVecD3 = (" + PRINT_COORDS(aVecD3) + ")" EOL;
255 "aTangent = (" + PRINT_COORDS(aTangent) + ")" EOL
256 "aCurvature = " + ASTR(aCurvature) + EOL;
257 if (isCurvatureValid)
260 "aCentreOfCurvature = (" + PRINT_COORDS(aCentreOfCurvature) + ")" EOL
261 "aNormal = (" + PRINT_COORDS(aNormal) + ")" EOL;
265 aText += "Tangent is not defined" EOL;
266 setResultText (aText.ToCString());
269 aObjs[0] = drawPoint (aPnt);
270 aObjs[3] = drawVector (aPnt, aVecD3, D3Color);
271 aObjs[2] = drawVector (aPnt, aVecD2, D2Color);
272 aObjs[1] = drawVector (aPnt, aVecD1, D1Color);
274 aObjs[4] = drawVector (aPnt, gp_Vec(aTangent)*50, TanColor);
275 if (isCurvatureValid)
277 aObjs[5] = drawVector (aPnt, gp_Vec(aNormal)*50, NormColor);
278 Handle(Geom_Circle) aCircle =
279 new Geom_Circle (gp_Ax2 (aCentreOfCurvature, aNormal^aTangent),
280 aCentreOfCurvature.Distance(aPnt));
281 aObjs[6] = drawCurve (aCircle, CrvtColor);
286 void LProps_Presentation::showSurfaceLProps (Handle_Geom_Surface theSurface,
287 const Standard_CString theName,
288 const Standard_Integer theNbPoints,
289 const Standard_Real thePoints[][2])
291 TCollection_AsciiString aTitle ("Local properties of a ");
293 TCollection_AsciiString aText;
295 " // Create a " + theName + EOL
296 " Handle(Geom_" + theName + ") aSurface;" EOL
297 " // initialize aSurface" EOL
298 " // aSurface = ..." EOL EOL
300 " // define U and V parameters at which properties should be computed" EOL
301 " Standard_Real aUParam, aVParam;" EOL
302 " // aUParam = ..." EOL
303 " // aVParam = ..." EOL EOL
305 " // compute local properties" EOL
306 " Standard_Integer maxOrder = 2;" EOL
307 " Standard_Real aResol = gp::Resolution();" EOL
308 " GeomLProp_SLProps aLProps (theSurface, maxOrder, aResol);" EOL
309 " aLProps.SetParameters (aUParam, aVParam);" EOL
310 " gp_Pnt aPnt = aLProps.Value();" EOL
311 " gp_Vec aVecD1U = aLProps.D1U();" EOL
312 " gp_Vec aVecD1V = aLProps.D1V();" EOL
313 " gp_Vec aVecD2U = aLProps.D2U();" EOL
314 " gp_Vec aVecD2V = aLProps.D2V();" EOL
315 " gp_Vec aVecDUV = aLProps.DUV();" EOL
316 " gp_Dir aTangentU, aTangentV, aNormal, aMaxCurvD, aMinCurvD;" EOL
317 " Standard_Real aMaxCurvature, aMinCurvature, aMeanCurvature, aGausCurvature;" EOL
318 " // determine availability of properties" EOL
319 " Standard_Boolean isTanU = aLProps.IsTangentUDefined();" EOL
320 " Standard_Boolean isTanV = aLProps.IsTangentVDefined();" EOL
321 " Standard_Boolean isNormal = aLProps.IsNormalDefined();" EOL
322 " Standard_Boolean isCurvature = aLProps.IsCurvatureDefined();" EOL
324 " aLProps.TangentU (aTangentU);" EOL
326 " aLProps.TangentV (aTangentV);" EOL
328 " aNormal = aLProps.Normal();" EOL
329 " if (isCurvature)" EOL
331 " aMaxCurvature = aLProps.MaxCurvature();" EOL
332 " aMinCurvature = aLProps.MinCurvature();" EOL
333 " aGausCurvature = aLProps.GaussianCurvature();" EOL
334 " aMeanCurvature = aLProps.MeanCurvature();" EOL
335 " aLProps.CurvatureDirections (aMaxCurvD, aMinCurvD);" EOL
337 "//======================================" EOL;
338 setResultTitle (aTitle.ToCString());
339 setResultText (aText.ToCString());
341 // Display theSurface
342 drawSurface (theSurface);
344 Standard_Integer maxOrder = 2;
345 Standard_Real aResol = gp::Resolution();
346 GeomLProp_SLProps aLProps (theSurface, maxOrder, aResol);
347 Handle(AIS_InteractiveObject) aObjs[11];
348 for (int i=0; i < theNbPoints; i++)
352 for (int j=0; j < sizeof(aObjs)/sizeof(Handle(AIS_InteractiveObject)); j++)
353 if (!aObjs[j].IsNull())
354 getAISContext()->Erase(aObjs[j]);
356 // define U and V parameters at which properties should be computed
357 Standard_Real aUParam, aVParam;
358 aUParam = thePoints[i][0];
359 aVParam = thePoints[i][1];
361 // compute local properties
362 aLProps.SetParameters (aUParam, aVParam);
363 gp_Pnt aPnt = aLProps.Value();
364 gp_Vec aVecD1U = aLProps.D1U();
365 gp_Vec aVecD1V = aLProps.D1V();
366 gp_Vec aVecD2U = aLProps.D2U();
367 gp_Vec aVecD2V = aLProps.D2V();
368 gp_Vec aVecDUV = aLProps.DUV();
369 gp_Dir aTangentU, aTangentV, aNormal, aMaxCurvD, aMinCurvD;
370 Standard_Real aMaxCurvature, aMinCurvature, aMeanCurvature, aGausCurvature;
371 // determine availability of properties
372 Standard_Boolean isTanU = aLProps.IsTangentUDefined();
373 Standard_Boolean isTanV = aLProps.IsTangentVDefined();
374 Standard_Boolean isNormal = aLProps.IsNormalDefined();
375 Standard_Boolean isCurvature = aLProps.IsCurvatureDefined();
377 aLProps.TangentU (aTangentU);
379 aLProps.TangentV (aTangentV);
381 aNormal = aLProps.Normal();
384 aMaxCurvature = aLProps.MaxCurvature();
385 aMinCurvature = aLProps.MinCurvature();
386 aGausCurvature = aLProps.GaussianCurvature();
387 aMeanCurvature = aLProps.MeanCurvature();
388 aLProps.CurvatureDirections (aMaxCurvD, aMinCurvD);
392 aText = aText + EOL " Results with parameters "
393 "U=" + ASTR(thePoints[i][0]) + " V=" + ASTR(thePoints[i][1]) + " :" EOL
394 "aPnt = (" + PRINT_COORDS(aPnt) + ")" EOL
395 "aVecD1U = (" + PRINT_COORDS(aVecD1U) + ")" EOL
396 "aVecD1V = (" + PRINT_COORDS(aVecD1V) + ")" EOL
397 "aVecD2U = (" + PRINT_COORDS(aVecD2U) + ")" EOL
398 "aVecD2V = (" + PRINT_COORDS(aVecD2V) + ")" EOL
399 "aVecDUV = (" + PRINT_COORDS(aVecDUV) + ")" EOL;
402 "aTangentU = (" + PRINT_COORDS(aTangentU) + ")" EOL;
405 "aTangentV = (" + PRINT_COORDS(aTangentV) + ")" EOL;
408 "aNormal = (" + PRINT_COORDS(aNormal) + ")" EOL;
411 "aMaxCurvature = " + ASTR(aMaxCurvature) + EOL
412 "aMinCurvature = " + ASTR(aMinCurvature) + EOL
413 "aMeanCurvature = " + ASTR(aMeanCurvature) + EOL
414 "aGausCurvature = " + ASTR(aGausCurvature) + EOL
415 "aMaxCurvD = (" + PRINT_COORDS(aMaxCurvD) + ")" EOL
416 "aMinCurvD = (" + PRINT_COORDS(aMinCurvD) + ")" EOL;
417 setResultText (aText.ToCString());
420 aObjs[0] = drawPoint (aPnt);
421 aObjs[3] = drawVector (aPnt, aVecD2U, D2Color);
422 aObjs[4] = drawVector (aPnt, aVecD2V, D2Color);
423 aObjs[1] = drawVector (aPnt, aVecD1U, D1Color);
424 aObjs[2] = drawVector (aPnt, aVecD1V, D1Color);
425 aObjs[5] = drawVector (aPnt, aVecDUV, D3Color);
427 aObjs[6] = drawVector (aPnt, gp_Vec(aTangentU)*50, TanColor);
429 aObjs[7] = drawVector (aPnt, gp_Vec(aTangentV)*50, TanColor);
431 aObjs[8] = drawVector (aPnt, gp_Vec(aNormal)*50, NormColor);
434 aObjs[9] = drawVector (aPnt, gp_Vec(aMaxCurvD)*50, CrvtColor);
435 aObjs[10] = drawVector (aPnt, gp_Vec(aMinCurvD)*50, CrvtColor);