973c2be1 |
1 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
2 | // |
973c2be1 |
3 | // This file is part of Open CASCADE Technology software library. |
b311480e |
4 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
10 | // |
973c2be1 |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
b311480e |
13 | |
7fd59977 |
14 | #include <AIS_Triangulation.hxx> |
d6c48921 |
15 | |
16 | #include <AIS_DisplayMode.hxx> |
7fd59977 |
17 | #include <AIS_InteractiveObject.hxx> |
ec357c5c |
18 | #include <Standard_Type.hxx> |
7fd59977 |
19 | #include <Poly_Array1OfTriangle.hxx> |
20 | #include <Poly_Triangulation.hxx> |
6262338c |
21 | #include <Prs3d_Drawer.hxx> |
7fd59977 |
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> |
7fd59977 |
30 | |
31 | |
92efcf78 |
32 | IMPLEMENT_STANDARD_RTTIEXT(AIS_Triangulation,AIS_InteractiveObject) |
33 | |
7fd59977 |
34 | AIS_Triangulation::AIS_Triangulation(const Handle(Poly_Triangulation)& Triangulation) |
35 | { |
36 | myTriangulation = Triangulation; |
37 | myNbNodes = Triangulation->NbNodes(); |
38 | myNbTriangles = Triangulation->NbTriangles(); |
39 | myFlagColor = 0; |
40 | } |
41 | |
dcc17419 |
42 | //======================================================================= |
43 | //function : SetTransparency |
44 | //purpose : |
45 | //======================================================================= |
46 | void AIS_Triangulation::SetTransparency (const Standard_Real theValue) |
47 | { |
48 | if (!myDrawer->HasOwnShadingAspect()) |
49 | { |
50 | myDrawer->SetShadingAspect (new Prs3d_ShadingAspect()); |
51 | if (myDrawer->HasLink()) |
52 | { |
53 | *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect(); |
54 | } |
55 | } |
56 | |
57 | // override transparency |
58 | myDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel); |
f838dac4 |
59 | myDrawer->SetTransparency ((Standard_ShortReal )theValue); |
dcc17419 |
60 | |
61 | updatePresentation(); |
62 | } |
63 | |
64 | //======================================================================= |
65 | //function : UnsetTransparency |
66 | //purpose : |
67 | //======================================================================= |
68 | void AIS_Triangulation::UnsetTransparency() |
69 | { |
f838dac4 |
70 | myDrawer->SetTransparency (0.0f); |
dcc17419 |
71 | if (!myDrawer->HasOwnShadingAspect()) |
72 | { |
73 | return; |
74 | } |
75 | else if (HasColor() || HasMaterial()) |
76 | { |
77 | myDrawer->ShadingAspect()->SetTransparency (0.0, myCurrentFacingModel); |
78 | } |
79 | |
80 | updatePresentation(); |
81 | } |
82 | |
83 | //======================================================================= |
84 | //function : updatePresentation |
85 | //purpose : |
86 | //======================================================================= |
87 | void AIS_Triangulation::updatePresentation() |
88 | { |
89 | if (HasVertexColors()) |
90 | { |
91 | SetToUpdate (AIS_WireFrame); |
92 | } |
93 | else |
94 | { |
95 | // modify shading presentation without re-computation |
96 | const PrsMgr_Presentations& aPrsList = Presentations(); |
97 | Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect(); |
7dd7c146 |
98 | for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next()) |
dcc17419 |
99 | { |
7dd7c146 |
100 | if (aPrsIter.Value()->Mode() != AIS_WireFrame) |
dcc17419 |
101 | { |
102 | continue; |
103 | } |
104 | |
7dd7c146 |
105 | const Handle(Prs3d_Presentation)& aPrs = aPrsIter.Value(); |
dcc17419 |
106 | for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next()) |
107 | { |
108 | const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value(); |
bf5f0ca2 |
109 | aGroup->SetGroupPrimitivesAspect (anAreaAsp); |
dcc17419 |
110 | } |
dcc17419 |
111 | } |
dcc17419 |
112 | } |
113 | } |
114 | |
7fd59977 |
115 | //======================================================================= |
116 | //function : Compute |
117 | //purpose : |
118 | //======================================================================= |
decbff0d |
119 | void AIS_Triangulation::Compute (const Handle(PrsMgr_PresentationManager)& , |
120 | const Handle(Prs3d_Presentation)& thePrs, |
121 | const Standard_Integer theMode) |
7fd59977 |
122 | { |
decbff0d |
123 | if (theMode != 0) |
7fd59977 |
124 | { |
decbff0d |
125 | return; |
126 | } |
7fd59977 |
127 | |
decbff0d |
128 | Standard_Boolean hasVNormals = myTriangulation->HasNormals(); |
129 | Standard_Boolean hasVColors = HasVertexColors(); |
7fd59977 |
130 | |
decbff0d |
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(); |
7fd59977 |
135 | |
61a05a36 |
136 | const Standard_Real anAmbient = 1.0; |
decbff0d |
137 | if (hasVNormals) |
138 | { |
139 | gp_Vec3f aNormal; |
140 | if (hasVColors) |
141 | { |
142 | const TColStd_Array1OfInteger& colors = myColor->Array1(); |
143 | for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter) |
16e65a03 |
144 | { |
decbff0d |
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()); |
16e65a03 |
148 | } |
decbff0d |
149 | } |
150 | else // !hasVColors |
151 | { |
152 | for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter) |
16e65a03 |
153 | { |
decbff0d |
154 | anArray->AddVertex (myTriangulation->Node (aNodeIter)); |
155 | myTriangulation->Normal (aNodeIter, aNormal); |
156 | anArray->SetVertexNormal(aNodeIter, aNormal.x(), aNormal.y(), aNormal.z()); |
7fd59977 |
157 | } |
decbff0d |
158 | } |
159 | } |
160 | else // !hasVNormals |
161 | { |
162 | if (hasVColors) |
163 | { |
164 | const TColStd_Array1OfInteger& colors = myColor->Array1(); |
165 | for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter) |
166 | { |
167 | anArray->AddVertex (myTriangulation->Node (aNodeIter), attenuateColor (colors[aNodeIter], anAmbient)); |
168 | } |
169 | } |
170 | else // !hasVColors |
171 | { |
172 | for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter) |
a8b605eb |
173 | { |
decbff0d |
174 | anArray->AddVertex (myTriangulation->Node (aNodeIter)); |
7fd59977 |
175 | } |
decbff0d |
176 | } |
177 | } |
178 | |
179 | Standard_Integer aTriIndices[3] = {0,0,0}; |
180 | for (Standard_Integer aTriIter = 1; aTriIter <= myTriangulation->NbTriangles(); ++aTriIter) |
181 | { |
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]); |
7fd59977 |
186 | } |
decbff0d |
187 | aGroup->SetPrimitivesAspect (anAspect); |
188 | aGroup->AddPrimitiveArray (anArray); |
7fd59977 |
189 | } |
190 | |
191 | //======================================================================= |
192 | //function : ComputeSelection |
7379f442 |
193 | //purpose : |
7fd59977 |
194 | //======================================================================= |
35e08fe8 |
195 | void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/, |
7fd59977 |
196 | const Standard_Integer /*aMode*/) |
197 | { |
198 | |
199 | } |
200 | |
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) |
208 | { |
209 | myFlagColor = 1; |
210 | myColor = aColor; |
211 | } |
212 | |
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 | //======================================================================= |
219 | |
220 | Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const |
221 | { |
222 | return myColor; |
223 | } |
224 | |
225 | |
226 | //======================================================================= |
227 | //function : SetTriangulation |
7379f442 |
228 | //purpose : |
7fd59977 |
229 | //======================================================================= |
230 | void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation) |
231 | { |
232 | myTriangulation = aTriangulation; |
233 | } |
234 | |
235 | //======================================================================= |
236 | //function : GetTriangulation |
7379f442 |
237 | //purpose : |
7fd59977 |
238 | //======================================================================= |
239 | Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{ |
240 | return myTriangulation; |
241 | } |
242 | |
243 | //======================================================================= |
244 | //function : AttenuateColor |
7379f442 |
245 | //purpose : |
7fd59977 |
246 | //======================================================================= |
dcc17419 |
247 | Graphic3d_Vec4ub AIS_Triangulation::attenuateColor (const Standard_Integer theColor, |
248 | const Standard_Real theComposition) |
7fd59977 |
249 | { |
7379f442 |
250 | const Standard_Byte* anRgbx = reinterpret_cast<const Standard_Byte*> (&theColor); |
7fd59977 |
251 | |
dcc17419 |
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) |
254 | : 255; |
7fd59977 |
255 | |
7379f442 |
256 | return Graphic3d_Vec4ub ((Standard_Byte)(theComposition * anRgbx[0]), |
257 | (Standard_Byte)(theComposition * anRgbx[1]), |
258 | (Standard_Byte)(theComposition * anRgbx[2]), |
dcc17419 |
259 | anAlpha); |
7fd59977 |
260 | } |