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