0023097: MFC Samples do not compile after redesigning the TKOpenGl driver
[occt.git] / samples / mfc / standard / Common / User_Cylinder.cxx
CommitLineData
7fd59977 1#include <stdafx.h>
2
3#include <User_Cylinder.hxx>
4
5// Implementation of Handle and type mgt
6//
7IMPLEMENT_STANDARD_HANDLE(User_Cylinder,AIS_InteractiveObject)
8IMPLEMENT_STANDARD_RTTIEXT(User_Cylinder,AIS_InteractiveObject)
9
10#include "ColoredMeshDlg.h"
11
1a4b0f54 12#include <Graphic3d_ArrayOfTriangles.hxx>
7fd59977 13#include <Graphic3d_StructureManager.hxx>
14#include <PrsMgr_PresentationManager3d.hxx>
15#include <BRepMesh.hxx>
16#include <StdPrs_ToolShadedShape.hxx>
17#include <Poly_Connect.hxx>
18#include <TColgp_Array1OfPnt.hxx>
19#include <Poly_Triangulation.hxx>
20#include <TColgp_Array1OfDir.hxx>
21#include <GProp_PGProps.hxx>
22#include <Graphic3d_Array1OfVertexNC.hxx>
23#include <Aspect_Array1OfEdge.hxx>
24#include <Quantity_Color.hxx>
25
26#include <AIS_GraphicTool.hxx>
27
28// Constructors implementation
29//
30
31User_Cylinder::User_Cylinder(const Standard_Real R, const Standard_Real H) :
32AIS_InteractiveObject(PrsMgr_TOP_ProjectorDependant)
33{
34 BRepPrimAPI_MakeCylinder S(R,H);
35 myShape = S.Shape();
36 SetHilightMode(0);
37 SetSelectionMode(0);
38 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
39 myPlanarFaceColor = Quantity_NOC_FIREBRICK3;
40 myCylindricalFaceColor = Quantity_NOC_GRAY;
41}
42
43User_Cylinder::User_Cylinder(const gp_Ax2 CylAx2, const Standard_Real R, const Standard_Real H) :
44AIS_InteractiveObject(PrsMgr_TOP_ProjectorDependant)
45
46{
47 BRepPrimAPI_MakeCylinder S(CylAx2,R,H);
48 BRepBuilderAPI_NurbsConvert aNurbsConvert(S.Shape());
49 myShape = aNurbsConvert.Shape();
50 SetHilightMode(0);
51 SetSelectionMode(0);
52 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
53 myPlanarFaceColor = Quantity_NOC_FIREBRICK3;
54 myCylindricalFaceColor = Quantity_NOC_KHAKI4;
55}
56
57void User_Cylinder::Compute(const Handle_PrsMgr_PresentationManager3d& aPresentationManager,
58 const Handle_Prs3d_Presentation& aPresentation,
59 const Standard_Integer aMode )
60{
61// Quantity_NameOfColor aColor;
62 switch (aMode) {
63 case 0:
64// aColor = AIS_InteractiveObject::Color();
65// SetColor(aColor);
66// myDrawer->WireAspect()->SetColor (aColor);
67 StdPrs_WFDeflectionShape::Add(aPresentation,myShape, myDrawer );
68 break;
69 case 1:
70 {
71/*
72 myDrawer->ShadingAspect()->SetMaterial(Graphic3d_NOM_BRASS);
73 myDrawer->SetShadingAspectGlobal(Standard_False);
74 TopExp_Explorer Ex;
75 Handle(Geom_Surface) Surface;
76 for (Ex.Init(myShape,TopAbs_FACE); Ex.More(); Ex.Next())
77 {
78 Surface = BRep_Tool::Surface(TopoDS::Face(Ex.Current()));
79 if (Surface->IsKind(STANDARD_TYPE(Geom_Plane)))
80 myDrawer->ShadingAspect()->SetColor(myPlanarFaceColor);
81 else
82 myDrawer->ShadingAspect()->SetColor(myCylindricalFaceColor);
83 StdPrs_ShadedShape::Add(aPresentation,Ex.Current(), myDrawer);
84 }
85
86 break;
87*/
88
89 Standard_Real aTransparency = Transparency();
90 Graphic3d_NameOfMaterial aMaterial = Material();
91// aColor = AIS_InteractiveObject::Color();
92
93
94 myDrawer->SetShadingAspectGlobal(Standard_False);
95 TopExp_Explorer Ex;
96 Handle(Geom_Surface) Surface;
97
98 for (Ex.Init(myShape,TopAbs_FACE); Ex.More(); Ex.Next())
99 {
100
101 Surface = BRep_Tool::Surface(TopoDS::Face(Ex.Current()));
102 myDrawer->ShadingAspect()->SetMaterial(aMaterial);
103 if (Surface->IsKind(STANDARD_TYPE(Geom_Plane)))
104 myDrawer->ShadingAspect()->SetColor(myPlanarFaceColor);
105 else
106 myDrawer->ShadingAspect()->SetColor(myCylindricalFaceColor);
107
108
109 myDrawer->ShadingAspect()->SetTransparency (aTransparency);
110 StdPrs_ShadedShape::Add(aPresentation,Ex.Current(), myDrawer);
111 }
112// myDrawer->ShadingAspect()->SetMaterial(aMaterial);
113 //myDrawer->ShadingAspect()->SetTransparency (aTransparency);
114// if(HasColor())
115// myDrawer->ShadingAspect()->SetColor (aColor);
116// StdPrs_ShadedShape::Add(aPresentation,myShape, myDrawer);
117
118 break;
119
120 }
121 case 6: //color
122 {
123 BRepTools::Clean(myShape);
124 BRepTools::Update(myShape);
125
126 Handle(Graphic3d_StructureManager) aStrucMana = GetContext()->MainPrsMgr()->StructureManager();
127
128 Handle(Graphic3d_Group) mygroup = Prs3d_Root::CurrentGroup(aPresentation);
129 myAspect = (new Prs3d_ShadingAspect())->Aspect();
130 Graphic3d_MaterialAspect material = myAspect->FrontMaterial();
131 material.SetReflectionModeOff(Graphic3d_TOR_AMBIENT);
132 material.SetReflectionModeOff(Graphic3d_TOR_DIFFUSE);
133 material.SetReflectionModeOff(Graphic3d_TOR_SPECULAR);
134 material.SetReflectionModeOff(Graphic3d_TOR_EMISSION);
135 myAspect->SetFrontMaterial(material);
136
137 mygroup->SetPrimitivesAspect(myAspect);
138 myAspect->SetEdgeOn();
139
140 myDeflection = AIS_Shape::GetDeflection(myShape,myDrawer);
141 BRepMesh::Mesh(myShape,myDeflection);
142
143 myX1OnOff = Standard_False;
144 myXBlueOnOff = Standard_False;
145 myXGreenOnOff =Standard_False;
146 myXRedOnOff = Standard_False;
147 myY1OnOff = Standard_False;
148 myYBlueOnOff = Standard_False;
149 myYGreenOnOff = Standard_False;
150 myYRedOnOff = Standard_False;
151 myZ1OnOff = Standard_False;
152 myZBlueOnOff =Standard_False;
153 myZGreenOnOff = Standard_False;
154 myZRedOnOff = Standard_False;
155
156 CColoredMeshDlg Dlg(NULL);
157 Dlg.DoModal();
158
159 myX1OnOff = Dlg.X1OnOff;
160
161 myXBlueOnOff = Dlg.m_CheckXBlueOnOff;
162 myXGreenOnOff = Dlg.m_CheckXGreenOnOff;
163 myXRedOnOff = Dlg.m_CheckXRedOnOff;
164
165 myY1OnOff = Dlg.Y1OnOff;
166
167 myYBlueOnOff = Dlg.m_CheckYBlueOnOff;
168 myYGreenOnOff = Dlg.m_CheckYGreenOnOff;
169 myYRedOnOff = Dlg.m_CheckYRedOnOff;
170
171 myZ1OnOff = Dlg.Z1OnOff;
172
173 myZBlueOnOff = Dlg.m_CheckZBlueOnOff;
174 myZGreenOnOff = Dlg.m_CheckZGreenOnOff;
175 myZRedOnOff = Dlg.m_CheckZRedOnOff;
176
177 // Adds a triangulation of the shape myShape to its topological data structure.
178 // This triangulation is computed with the deflection myDeflection.
179
180#ifdef DEBUG
181 cout <<"Deflection = " << myDeflection << "\n" << endl;
182#endif
183
184 StdPrs_ToolShadedShape SST;
185
186 Standard_Integer NumFace;
187 TopExp_Explorer ExpFace;
188
189//khr -->
190
191 gp_Pnt H (0,0,0);
192 gp_Pnt B (0,0,1000000000);
193 for( NumFace=0,ExpFace.Init(myShape,TopAbs_FACE); ExpFace.More(); ExpFace.Next(),NumFace++ )
194 {
195 TopoDS_Face myFace = TopoDS::Face(ExpFace.Current());
196 TopLoc_Location myLocation = myFace.Location();
197
198#ifdef DEBUG
199 cout << "J\'explore actuellement la face " << NumFace << "\n" << endl;
200#endif
201 Handle(Poly_Triangulation) myT = BRep_Tool::Triangulation(myFace, myLocation);
202 // Returns the Triangulation of the face. It is a null handle if there is no triangulation.
203
204 if (myT.IsNull())
205 {
206#ifdef DEBUG
207// cout << "Triangulation of the face "<< i <<" is null \n"<< endl;
208#endif
209 return;
210 }
211
212 const TColgp_Array1OfPnt& Nodes= myT->Nodes();
213
214 const Poly_Array1OfTriangle& triangles = myT->Triangles();
215
216 Standard_Integer nnn = myT->NbTriangles(); // nnn : nombre de triangles
217 Standard_Integer nt, n1, n2, n3 = 0; // nt : triangle courant
218 // ni : sommet i du triangle courant
219
220
221//recherche du pt "haut" et du pt "bas
222
223 for (nt = 1; nt <= nnn; nt++)
224 {
225// triangles(nt).Get(n1,n2,n3); // le triangle est n1,n2,n3
226
227 if (SST.Orientation(myFace) == TopAbs_REVERSED) // si la face est "reversed"
228 triangles(nt).Get(n1,n3,n2); // le triangle est n1,n3,n2
229 else
230 triangles(nt).Get(n1,n2,n3); // le triangle est n1,n2,n3
231
232 if (TriangleIsValid (Nodes(n1),Nodes(n2),Nodes(n3)) )
233 { // Associates a vertexNT to each node
234 gp_Pnt p = Nodes(n1).Transformed(myLocation.Transformation());
235 gp_Pnt q = Nodes(n2).Transformed(myLocation.Transformation());
236 gp_Pnt r = Nodes(n3).Transformed(myLocation.Transformation());
237
238 if (p.Z() > H.Z()) H=p;
239 if (q.Z() > H.Z()) H=q;
240 if (r.Z() > H.Z()) H=r;
241 if (p.Z() < B.Z()) B=p;
242 if (q.Z() < B.Z()) B=q;
243 if (r.Z() < B.Z()) B=r;
244 }
245 }
246 }
247
248//khr <--
249
250
251 for( NumFace=0,ExpFace.Init(myShape,TopAbs_FACE); ExpFace.More(); ExpFace.Next(),NumFace++ )
252 {
253 TopoDS_Face myFace = TopoDS::Face(ExpFace.Current());
254 TopLoc_Location myLocation = myFace.Location();
255
256#ifdef DEBUG
257 cout << "J\'explore actuellement la face " << NumFace << "\n" << endl;
258#endif
259 Handle(Poly_Triangulation) myT = BRep_Tool::Triangulation(myFace, myLocation);
260 // Returns the Triangulation of the face. It is a null handle if there is no triangulation.
261
262 if (myT.IsNull())
263 {
264#ifdef DEBUG
265 //cout << "Triangulation of the face "<< i <<" is null \n"<< endl;
266#endif
267 return;
268 }
269 Poly_Connect pc(myT);
270 const TColgp_Array1OfPnt& Nodes= myT->Nodes();
271 BAR = GProp_PGProps::Barycentre(Nodes);
272
273
274 const TColgp_Array1OfPnt2d& UVNodes = myT->UVNodes();
275 const Poly_Array1OfTriangle& triangles = myT->Triangles();
276 TColgp_Array1OfDir myNormal(Nodes.Lower(), Nodes.Upper());
277
278 SST.Normal(myFace, pc, myNormal);
279 BRepTools::UVBounds(myFace,Umin, Umax, Vmin, Vmax);
280 dUmax = (Umax - Umin);
281 dVmax = (Vmax - Vmin);
282
283 Standard_Integer nnn = myT->NbTriangles(); // nnn : nombre de triangles
284 Standard_Integer nt, n1, n2, n3 = 0; // nt : triangle courant
285 // ni : sommet i du triangle courant
286
287
288//recherche du pt "haut" et du pt "bas
289// gp_Pnt H (0,0,0);
290// gp_Pnt B (0,0,1000000000);
291
292 for (nt = 1; nt <= nnn; nt++)
293 {
294// triangles(nt).Get(n1,n2,n3); // le triangle est n1,n2,n3
295
296 if (SST.Orientation(myFace) == TopAbs_REVERSED) // si la face est "reversed"
297 triangles(nt).Get(n1,n3,n2); // le triangle est n1,n3,n2
298 else
299 triangles(nt).Get(n1,n2,n3); // le triangle est n1,n2,n3
300
301 if (TriangleIsValid (Nodes(n1),Nodes(n2),Nodes(n3)) )
302 { // Associates a vertexNT to each node
303 gp_Pnt p = Nodes(n1).Transformed(myLocation.Transformation());
304 gp_Pnt q = Nodes(n2).Transformed(myLocation.Transformation());
305 gp_Pnt r = Nodes(n3).Transformed(myLocation.Transformation());
306/*
307 if (p.Z() > H.Z()) H=p;
308 if (q.Z() > H.Z()) H=q;
309 if (r.Z() > H.Z()) H=r;
310 if (p.Z() < B.Z()) B=p;
311 if (q.Z() < B.Z()) B=q;
312 if (r.Z() < B.Z()) B=r;
313*/
314 }
315 }
316
1a4b0f54
P
317 Handle(Graphic3d_ArrayOfTriangles) aOP = new Graphic3d_ArrayOfTriangles(3 * nnn, 0, Standard_True, Standard_True);
318
7fd59977 319 for (nt = 1; nt <= nnn; nt++)
320 {
321#ifdef DEBUG
322 cout << "On traite actuellement le triangle : "<< nt <<"\n";
323#endif
324 if (SST.Orientation(myFace) == TopAbs_REVERSED) // si la face est "reversed"
325 triangles(nt).Get(n1,n3,n2); // le triangle est n1,n3,n2
326 else
327 triangles(nt).Get(n1,n2,n3); // le triangle est n1,n2,n3
328
329 if (TriangleIsValid (Nodes(n1),Nodes(n2),Nodes(n3)) )
330 { // Associates a vertexNT to each node
1a4b0f54
P
331
332 TColgp_Array1OfPnt Points(1,3);
7fd59977 333
1a4b0f54
P
334 gp_Pnt p = Nodes(n1).Transformed(myLocation.Transformation());
335 gp_Pnt q = Nodes(n2).Transformed(myLocation.Transformation());
336 gp_Pnt r = Nodes(n3).Transformed(myLocation.Transformation());
7fd59977 337
1a4b0f54
P
338 Points(1).SetCoord(p.X(), p.Y(), p.Z());
339 Points(2).SetCoord(q.X(), q.Y(), q.Z());
340 Points(3).SetCoord(r.X(), r.Y(), r.Z());
7fd59977 341
1a4b0f54
P
342 aOP->AddVertex(Points(1), myNormal(n1), Color(p,B.Z(),H.Z(),Dlg.Colorization));
343 aOP->AddVertex(Points(2), myNormal(n2), Color(q,B.Z(),H.Z(),Dlg.Colorization));
344 aOP->AddVertex(Points(3), myNormal(n3), Color(r,B.Z(),H.Z(),Dlg.Colorization));
7fd59977 345 } // end of "if the triangle is valid
346 } // end of the "parcours" of the triangles
1a4b0f54
P
347
348 mygroup->BeginPrimitives();
349 mygroup->AddPrimitiveArray(aOP);
350 mygroup->EndPrimitives();
351
7fd59977 352 mygroup->SetGroupPrimitivesAspect(myAspect);
353 }// end of the exploration of the shape in faces
354
355 break;
356 }
357 }
358}
359
360void User_Cylinder::Compute(const Handle_Prs3d_Projector& aProjector,
361 const Handle_Prs3d_Presentation& aPresentation)
362{
363 myDrawer->EnableDrawHiddenLine();
364 StdPrs_HLRPolyShape::Add(aPresentation,myShape,myDrawer,aProjector);
365}
366
367void User_Cylinder::ComputeSelection(const Handle_SelectMgr_Selection& aSelection,
368 const Standard_Integer aMode)
369{
370 switch(aMode){
371 case 0:
d5c67ba0 372 StdSelect_BRepSelectionTool::Load(aSelection,this,myShape,TopAbs_SHAPE, 0.01, 0.1);
7fd59977 373 break;
374 case 4:
d5c67ba0 375 StdSelect_BRepSelectionTool::Load(aSelection,this,myShape,TopAbs_FACE, 0.01, 0.1);
7fd59977 376 break;
377 }
378}
379
380Standard_Integer User_Cylinder::NbPossibleSelection() const
381{
382 return 2;
383}
384
385Standard_Boolean User_Cylinder::AcceptShapeDecomposition() const
386{
387 return Standard_True;
388}
389
390void User_Cylinder::SetPlanarFaceColor(const Quantity_Color acolor)
391{
392 myPlanarFaceColor = acolor;
393}
394
395void User_Cylinder::SetCylindricalFaceColor(const Quantity_Color acolor)
396{
397 myCylindricalFaceColor = acolor;
398}
399
400Standard_Boolean User_Cylinder::TriangleIsValid(const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3) const
401{
402 gp_Vec V1(P1,P2); // V1=(P1,P2)
403 gp_Vec V2(P2,P3); // V2=(P2,P3)
404 gp_Vec V3(P3,P1); // V3=(P3,P1)
405
406 if ((V1.SquareMagnitude() > 1.e-10) && (V2.SquareMagnitude() > 1.e-10) && (V3.SquareMagnitude() > 1.e-10))
407 {
408 V1.Cross(V2); // V1 = Normal
409 if (V1.SquareMagnitude() > 1.e-10)
410 return Standard_True;
411 else
412 return Standard_False;
413 }
414 else
415 return Standard_False;
416
417}
418
419Quantity_Color User_Cylinder::Color(gp_Pnt& thePoint,Standard_Real AltMin,Standard_Real AltMax,
420 const Standard_Integer ColorizationMode)
421{
422 red =1; //initializing colors parameters
423 green=1;
424 blue =1;
425 switch ( ColorizationMode)
426 {
427 case 0 : //normal, vert/maron
428 {
429 Standard_Real Alt= thePoint.Z();
430
431 Standard_Real AltDelta;
432
433 AltDelta = AltMax-AltMin;
434
435 red = 0.5- ((0.5*(AltMax-Alt))/(AltDelta));
436 // red = 1/(((1000/(AltMax-AltMin))*Alt)+1000*(1-(AltMin/(AltMax-AltMin))));
437
438 // green = (3*AltMax-AltMin)/(3*AltMax-8*AltMin + 7*Alt);
439 // green = 1/(((7/(3*AltMax-AltMin))*Alt) + 1-(7*AltMin/(3*AltMax-AltMin)));
440 Standard_Real A = 7*Alt-7*AltMin;
441 green = (3*AltMax-AltMin)/(3*AltMax-AltMin+(7*Alt-7*AltMin));
442// AfxMessageBox(green);
443 // green =(0.30- (((0.3-1)*(AltMax-Alt))/(AltMax-AltMin)));
444
445 blue = 0 ;
446/*
447 red = 0.5;
448 green = 0.2222;
449 blue = 0;
450*/
451 Quantity_Color color;
452 color.SetValues(red,green,blue, Quantity_TOC_RGB);
453 return color;
454 break;
455 }//end case 0
456
457 case 1 : //mer-neige
458 {
459 Standard_Real Alt= thePoint.Z();
460
461 Standard_Real b =AltMax-AltMin;
462 Standard_Real a= AltMax-thePoint.Z();
463
464 red =1;
465 green=1;
466 blue =1;
467 if (0<a && a <= (b/5))
468 {
469 red = 1;
470 green = 1;
471 blue = 1;
472 }
473 else if ((b/5)<a && a <= (2*b/5))
474 {
475 red = .55;
476 green = 0.3;
477 blue = 0;
478 }
479 else if ((2*b/5)<a && a <= (18*b/20))
480 {
481 green =1/(((7/(3*AltMax-AltMin))*Alt)+(1-(7*AltMin/(3*AltMax-AltMin))));
482 red = 1/(((1000/(AltMax-AltMin))*Alt)+1000*(1-(AltMin/(AltMax-AltMin))));
483 blue = 0;
484 }
485 else if ((18*b/20)<a && a <= (18.5*b/20))
486 {
487 red = 0.75;
488 green = 0.66;
489 blue = 0;
490 }
491
492 else if ((18.5*b/20)<a && a <= b)
493 {
494 red = 0.25;
495 green = .66;
496 blue = 1;
497 }
498 Quantity_Color color;
499 color.SetValues(red,green,blue, Quantity_TOC_RGB);
500 return color;
501 break;
502 }//end case 1
503
504 case 2 :
505 {
506 gp_Pnt P (85.,0.,-105.);
507 gp_Vec TheVect ( P, thePoint);
508 Standard_Real CoordX;
509 Standard_Real CoordY;
510 Standard_Real CoordZ;
511
512 CoordX = TheVect.X();
513 CoordY = TheVect.Y();
514 CoordZ = TheVect.Z();
515/*
516 Standard_Real maxixy = Max(fabs(CoordX),fabs(CoordY));
517 Standard_Real maxiyz = Max(fabs(CoordY),fabs(CoordZ));
518 Standard_Real Maxi = Max(maxixy,maxiyz);
519
520*/
521 Standard_Real Distance = BAR.Distance ( P);
522
523 //red = (abs(CoordX))/(1*Distance) ;
524 //green =(abs(CoordY))/(1*Distance) ;
525 //blue = (abs(CoordZ))/(1*Distance) ;
526
527 Standard_Real a =fabs(CoordX);
528 Standard_Real b =fabs(CoordY);
529 Standard_Real c =fabs(CoordZ);
530
531Standard_Real xx = a / Max(Distance,a); //(Max (Distance, Maxi));
532Standard_Real yy = b / Max(Distance,b); //(Max (Distance, Maxi));
533Standard_Real zz = c / Max(Distance,c); //(Max (Distance, Maxi));
534
535
536 if (myXRedOnOff)
537 red = xx;
538 else if (myXGreenOnOff)
539 green =xx;
540 else if (myXBlueOnOff)
541 blue=xx;
542
543 if (myYRedOnOff)
544 red = yy;
545 else if (myYGreenOnOff)
546 green = yy;
547 else if (myYBlueOnOff)
548 blue = yy;
549
550 if (myZRedOnOff)
551 red = zz;
552 else if (myZGreenOnOff)
553 green = zz;
554 else if (myZBlueOnOff)
555 blue = zz;
556
557 /* if (myX1OnOff)
558 if (myY1OnOff)
559 if (myZ1OnOff)*/
560
561
562 Quantity_Color color;
563 color.SetValues(red,green,blue, Quantity_TOC_RGB);
564 return color;
565 break;
566 }//end case 2
567 }//end switch
568
569 Quantity_Color c;
570 return c;
571}
572
573void User_Cylinder::SetColor(const Quantity_Color &aColor)
574{
575 AIS_InteractiveObject::SetColor(aColor);
576 SetPlanarFaceColor(aColor);
577 SetCylindricalFaceColor(aColor);
578}