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