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_PresentationManager)& ,
120 const Handle(Prs3d_Presentation)& thePrs,
121 const Standard_Integer theMode)
128 Standard_Boolean hasVNormals = myTriangulation->HasNormals();
129 Standard_Boolean hasVColors = HasVertexColors();
131 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (myNbNodes, myNbTriangles * 3,
132 hasVNormals, hasVColors, Standard_False);
133 Handle(Graphic3d_Group) aGroup = thePrs->CurrentGroup();
134 Handle(Graphic3d_AspectFillArea3d) anAspect = myDrawer->ShadingAspect()->Aspect();
136 const Standard_Real anAmbient = 0.2;
142 const TColStd_Array1OfInteger& colors = myColor->Array1();
143 for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter)
145 anArray->AddVertex (myTriangulation->Node (aNodeIter), attenuateColor (colors[aNodeIter], anAmbient));
146 myTriangulation->Normal (aNodeIter, aNormal);
147 anArray->SetVertexNormal (aNodeIter, aNormal.x(), aNormal.y(), aNormal.z());
152 for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter)
154 anArray->AddVertex (myTriangulation->Node (aNodeIter));
155 myTriangulation->Normal (aNodeIter, aNormal);
156 anArray->SetVertexNormal(aNodeIter, aNormal.x(), aNormal.y(), aNormal.z());
164 const TColStd_Array1OfInteger& colors = myColor->Array1();
165 for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter)
167 anArray->AddVertex (myTriangulation->Node (aNodeIter), attenuateColor (colors[aNodeIter], anAmbient));
172 for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter)
174 anArray->AddVertex (myTriangulation->Node (aNodeIter));
179 Standard_Integer aTriIndices[3] = {0,0,0};
180 for (Standard_Integer aTriIter = 1; aTriIter <= myTriangulation->NbTriangles(); ++aTriIter)
182 myTriangulation->Triangle (aTriIter).Get (aTriIndices[0], aTriIndices[1], aTriIndices[2]);
183 anArray->AddEdge (aTriIndices[0]);
184 anArray->AddEdge (aTriIndices[1]);
185 anArray->AddEdge (aTriIndices[2]);
187 aGroup->SetPrimitivesAspect (anAspect);
188 aGroup->AddPrimitiveArray (anArray);
191 //=======================================================================
192 //function : ComputeSelection
194 //=======================================================================
195 void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/,
196 const Standard_Integer /*aMode*/)
201 //=======================================================================
202 //function : SetColor
203 //purpose : Set the color for each node.
204 // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
205 // Order of color components is essential for further usage by OpenGL
206 //=======================================================================
207 void AIS_Triangulation::SetColors(const Handle(TColStd_HArray1OfInteger)& aColor)
213 //=======================================================================
214 //function : GetColor
215 //purpose : Get the color for each node.
216 // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
217 // Order of color components is essential for further usage by OpenGL
218 //=======================================================================
220 Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const
226 //=======================================================================
227 //function : SetTriangulation
229 //=======================================================================
230 void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation)
232 myTriangulation = aTriangulation;
235 //=======================================================================
236 //function : GetTriangulation
238 //=======================================================================
239 Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{
240 return myTriangulation;
243 //=======================================================================
244 //function : AttenuateColor
246 //=======================================================================
247 Graphic3d_Vec4ub AIS_Triangulation::attenuateColor (const Standard_Integer theColor,
248 const Standard_Real theComposition)
250 const Standard_Byte* anRgbx = reinterpret_cast<const Standard_Byte*> (&theColor);
252 // If IsTranparent() is false alpha value will be ignored anyway.
253 Standard_Byte anAlpha = IsTransparent() ? static_cast<Standard_Byte> (255.0 - myDrawer->ShadingAspect()->Aspect()->FrontMaterial().Transparency() * 255.0)
256 return Graphic3d_Vec4ub ((Standard_Byte)(theComposition * anRgbx[0]),
257 (Standard_Byte)(theComposition * anRgbx[1]),
258 (Standard_Byte)(theComposition * anRgbx[2]),