1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <AIS_Triangulation.hxx>
16 #include <AIS_DisplayMode.hxx>
17 #include <AIS_InteractiveObject.hxx>
18 #include <Standard_Type.hxx>
19 #include <Poly_Array1OfTriangle.hxx>
20 #include <Poly_Triangulation.hxx>
21 #include <Prs3d_Drawer.hxx>
22 #include <Prs3d_ShadingAspect.hxx>
23 #include <TShort_Array1OfShortReal.hxx>
24 #include <TColgp_Array1OfPnt.hxx>
25 #include <TColStd_HArray1OfInteger.hxx>
26 #include <TShort_HArray1OfShortReal.hxx>
27 #include <Graphic3d_Group.hxx>
28 #include <Graphic3d_AspectFillArea3d.hxx>
29 #include <Graphic3d_ArrayOfTriangles.hxx>
32 IMPLEMENT_STANDARD_RTTIEXT(AIS_Triangulation,AIS_InteractiveObject)
34 AIS_Triangulation::AIS_Triangulation(const Handle(Poly_Triangulation)& Triangulation)
36 myTriangulation = Triangulation;
37 myNbNodes = Triangulation->NbNodes();
38 myNbTriangles = Triangulation->NbTriangles();
42 //=======================================================================
43 //function : SetTransparency
45 //=======================================================================
46 void AIS_Triangulation::SetTransparency (const Standard_Real theValue)
48 if (!myDrawer->HasOwnShadingAspect())
50 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
51 if (myDrawer->HasLink())
53 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
57 // override transparency
58 myDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel);
59 myDrawer->SetTransparency ((Standard_ShortReal )theValue);
64 //=======================================================================
65 //function : UnsetTransparency
67 //=======================================================================
68 void AIS_Triangulation::UnsetTransparency()
70 myDrawer->SetTransparency (0.0f);
71 if (!myDrawer->HasOwnShadingAspect())
75 else if (HasColor() || HasMaterial())
77 myDrawer->ShadingAspect()->SetTransparency (0.0, myCurrentFacingModel);
83 //=======================================================================
84 //function : updatePresentation
86 //=======================================================================
87 void AIS_Triangulation::updatePresentation()
89 if (HasVertexColors())
91 SetToUpdate (AIS_WireFrame);
95 // modify shading presentation without re-computation
96 const PrsMgr_Presentations& aPrsList = Presentations();
97 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
98 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
100 if (aPrsIter.Value()->Mode() != AIS_WireFrame)
105 const Handle(Prs3d_Presentation)& aPrs = aPrsIter.Value();
106 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
108 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
109 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
115 //=======================================================================
118 //=======================================================================
119 void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
120 const Handle(Prs3d_Presentation)& aPresentation,
121 const Standard_Integer aMode)
126 const TColgp_Array1OfPnt& nodes = myTriangulation->Nodes(); //Nodes
127 const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles(); //Triangle
129 Standard_Boolean hasVNormals = myTriangulation->HasNormals();
130 Standard_Boolean hasVColors = HasVertexColors();
132 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (myNbNodes, myNbTriangles * 3,
133 hasVNormals, hasVColors, Standard_False);
134 Handle(Graphic3d_Group) TheGroup = aPresentation->CurrentGroup();
135 Handle(Graphic3d_AspectFillArea3d) aspect = myDrawer->ShadingAspect()->Aspect();
140 const Standard_Real ambient = 0.2;
143 const TShort_Array1OfShortReal& normals = myTriangulation->Normals();
146 const TColStd_Array1OfInteger& colors = myColor->Array1();
147 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
149 j = (i - nodes.Lower()) * 3;
150 anArray->AddVertex(nodes(i), attenuateColor(colors(i), ambient));
151 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
156 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
158 j = (i - nodes.Lower()) * 3;
159 anArray->AddVertex(nodes(i));
160 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
168 const TColStd_Array1OfInteger& colors = myColor->Array1();
169 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
171 anArray->AddVertex(nodes(i), attenuateColor(colors(i), ambient));
176 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
178 anArray->AddVertex(nodes(i));
183 Standard_Integer indexTriangle[3] = {0,0,0};
184 for ( i = triangles.Lower(); i<= triangles.Upper(); i++ ) {
185 triangles(i).Get(indexTriangle[0], indexTriangle[1], indexTriangle[2]);
186 anArray->AddEdge(indexTriangle[0]);
187 anArray->AddEdge(indexTriangle[1]);
188 anArray->AddEdge(indexTriangle[2]);
190 TheGroup->SetPrimitivesAspect(aspect);
191 TheGroup->AddPrimitiveArray(anArray);
196 //=======================================================================
197 //function : ComputeSelection
199 //=======================================================================
200 void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/,
201 const Standard_Integer /*aMode*/)
206 //=======================================================================
207 //function : SetColor
208 //purpose : Set the color for each node.
209 // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
210 // Order of color components is essential for further usage by OpenGL
211 //=======================================================================
212 void AIS_Triangulation::SetColors(const Handle(TColStd_HArray1OfInteger)& aColor)
218 //=======================================================================
219 //function : GetColor
220 //purpose : Get the color for each node.
221 // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
222 // Order of color components is essential for further usage by OpenGL
223 //=======================================================================
225 Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const
231 //=======================================================================
232 //function : SetTriangulation
234 //=======================================================================
235 void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation)
237 myTriangulation = aTriangulation;
240 //=======================================================================
241 //function : GetTriangulation
243 //=======================================================================
244 Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{
245 return myTriangulation;
248 //=======================================================================
249 //function : AttenuateColor
251 //=======================================================================
252 Graphic3d_Vec4ub AIS_Triangulation::attenuateColor (const Standard_Integer theColor,
253 const Standard_Real theComposition)
255 const Standard_Byte* anRgbx = reinterpret_cast<const Standard_Byte*> (&theColor);
257 // If IsTranparent() is false alpha value will be ignored anyway.
258 Standard_Byte anAlpha = IsTransparent() ? static_cast<Standard_Byte> (255.0 - myDrawer->ShadingAspect()->Aspect()->FrontMaterial().Transparency() * 255.0)
261 return Graphic3d_Vec4ub ((Standard_Byte)(theComposition * anRgbx[0]),
262 (Standard_Byte)(theComposition * anRgbx[1]),
263 (Standard_Byte)(theComposition * anRgbx[2]),