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