d33222c1 |
1 | // Created on: 2014-08-13 |
2 | // Created by: Maxim GLIBIN |
3 | // Copyright (c) 2014 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
16 | #include <AIS_PointCloud.hxx> |
17 | |
d33222c1 |
18 | #include <AIS_GraphicTool.hxx> |
19 | #include <Graphic3d_AspectFillArea3d.hxx> |
d33222c1 |
20 | #include <Graphic3d_Group.hxx> |
6262338c |
21 | #include <Prs3d_Drawer.hxx> |
d33222c1 |
22 | #include <Prs3d_PointAspect.hxx> |
23 | #include <Prs3d_Presentation.hxx> |
d33222c1 |
24 | #include <Prs3d_ShadingAspect.hxx> |
d33222c1 |
25 | #include <Select3D_SensitiveBox.hxx> |
8b9a309b |
26 | #include <Select3D_SensitivePrimitiveArray.hxx> |
d33222c1 |
27 | #include <SelectMgr_EntityOwner.hxx> |
28 | #include <SelectMgr_Selection.hxx> |
7f24b768 |
29 | #include <Prs3d_BndBox.hxx> |
d33222c1 |
30 | |
4a056d20 |
31 | IMPLEMENT_STANDARD_RTTIEXT(AIS_PointCloudOwner, SelectMgr_EntityOwner) |
32 | IMPLEMENT_STANDARD_RTTIEXT(AIS_PointCloud, AIS_InteractiveObject) |
d33222c1 |
33 | |
4a056d20 |
34 | //======================================================================= |
35 | //function : AIS_PointCloudOwner |
36 | //purpose : |
37 | //======================================================================= |
38 | AIS_PointCloudOwner::AIS_PointCloudOwner (const Handle(AIS_PointCloud)& theOrigin) |
39 | : SelectMgr_EntityOwner ((const Handle(SelectMgr_SelectableObject)& )theOrigin, 5), |
40 | myDetPoints (new TColStd_HPackedMapOfInteger()), |
41 | mySelPoints (new TColStd_HPackedMapOfInteger()) |
42 | { |
43 | // |
44 | } |
45 | |
46 | //======================================================================= |
47 | //function : ~AIS_PointCloudOwner |
48 | //purpose : |
49 | //======================================================================= |
50 | AIS_PointCloudOwner::~AIS_PointCloudOwner() |
51 | { |
52 | // |
53 | } |
54 | |
55 | //======================================================================= |
56 | //function : HilightWithColor |
57 | //purpose : |
58 | //======================================================================= |
59 | Standard_Boolean AIS_PointCloudOwner::IsForcedHilight() const |
60 | { |
61 | return true; |
62 | } |
63 | |
64 | //======================================================================= |
65 | //function : HilightWithColor |
66 | //purpose : |
67 | //======================================================================= |
decbff0d |
68 | void AIS_PointCloudOwner::HilightWithColor (const Handle(PrsMgr_PresentationManager)& thePrsMgr, |
69 | const Handle(Prs3d_Drawer)& theStyle, |
70 | const Standard_Integer ) |
4a056d20 |
71 | { |
72 | Handle(AIS_PointCloud) anObj = Handle(AIS_PointCloud)::DownCast (Selectable()); |
73 | if (anObj.IsNull()) |
74 | { |
75 | throw Standard_ProgramError ("Internal Error within AIS_PointCloud::PointsOwner!"); |
76 | } |
77 | |
78 | const Handle(TColStd_HPackedMapOfInteger)& aMap = thePrsMgr->IsImmediateModeOn() |
79 | ? myDetPoints |
80 | : mySelPoints; |
81 | Handle(Prs3d_Presentation) aPrs = thePrsMgr->IsImmediateModeOn() |
82 | ? anObj->GetHilightPresentation(thePrsMgr) |
83 | : anObj->GetSelectPresentation (thePrsMgr); |
84 | const Graphic3d_ZLayerId aZLayer = theStyle->ZLayer() != -1 |
85 | ? theStyle->ZLayer() |
86 | : (thePrsMgr->IsImmediateModeOn() ? Graphic3d_ZLayerId_Top : anObj->ZLayer()); |
87 | aMap->ChangeMap().Clear(); |
88 | for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next()) |
89 | { |
90 | const Handle(SelectMgr_Selection)& aSel = aSelIter.Value(); |
91 | for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next()) |
92 | { |
93 | const Handle(SelectMgr_SensitiveEntity)& aSelEnt = aSelEntIter.Value(); |
94 | if (aSelEnt->BaseSensitive()->OwnerId() == this) |
95 | { |
96 | if (Handle(Select3D_SensitivePrimitiveArray) aSensitive = Handle(Select3D_SensitivePrimitiveArray)::DownCast (aSelEnt->BaseSensitive())) |
97 | { |
98 | aMap->ChangeMap() = aSensitive->LastDetectedElementMap()->Map(); |
99 | if (aSensitive->LastDetectedElement() != -1) |
100 | { |
101 | aMap->ChangeMap().Add (aSensitive->LastDetectedElement()); |
102 | } |
103 | break; |
104 | } |
105 | } |
106 | } |
107 | } |
108 | |
109 | aPrs->Clear(); |
110 | if (aPrs->GetZLayer() != aZLayer) |
111 | { |
112 | aPrs->SetZLayer (aZLayer); |
113 | } |
114 | if (aMap->Map().IsEmpty()) |
115 | { |
116 | return; |
117 | } |
118 | |
119 | const Handle(Graphic3d_ArrayOfPoints) anAllPoints = anObj->GetPoints(); |
120 | if (anAllPoints.IsNull()) |
121 | { |
122 | return; |
123 | } |
124 | |
125 | Handle(Graphic3d_ArrayOfPoints) aPoints = new Graphic3d_ArrayOfPoints (aMap->Map().Extent()); |
126 | for (TColStd_PackedMapOfInteger::Iterator aPntIter (aMap->Map()); aPntIter.More(); aPntIter.Next()) |
127 | { |
128 | const gp_Pnt aPnt = anAllPoints->Vertice (aPntIter.Key() + 1); |
129 | aPoints->AddVertex (aPnt); |
130 | } |
131 | |
132 | Handle(Graphic3d_Group) aGroup = aPrs->NewGroup(); |
133 | aGroup->SetGroupPrimitivesAspect (theStyle->PointAspect()->Aspect()); |
134 | aGroup->AddPrimitiveArray (aPoints); |
135 | if (thePrsMgr->IsImmediateModeOn()) |
136 | { |
137 | thePrsMgr->AddToImmediateList (aPrs); |
138 | } |
139 | else |
140 | { |
141 | aPrs->Display(); |
142 | } |
143 | } |
144 | |
145 | //======================================================================= |
146 | //function : Unhilight |
147 | //purpose : |
148 | //======================================================================= |
149 | void AIS_PointCloudOwner::Unhilight (const Handle(PrsMgr_PresentationManager)& , const Standard_Integer ) |
150 | { |
decbff0d |
151 | if (Handle(Prs3d_Presentation) aPrs = Selectable()->GetSelectPresentation (Handle(PrsMgr_PresentationManager)())) |
4a056d20 |
152 | { |
153 | aPrs->Erase(); |
154 | } |
155 | } |
156 | |
157 | //======================================================================= |
158 | //function : Clear |
159 | //purpose : |
160 | //======================================================================= |
161 | void AIS_PointCloudOwner::Clear (const Handle(PrsMgr_PresentationManager)& thePrsMgr, const Standard_Integer theMode) |
162 | { |
163 | SelectMgr_EntityOwner::Clear (thePrsMgr, theMode); |
164 | } |
92efcf78 |
165 | |
d33222c1 |
166 | //================================================== |
167 | // Function: AIS_PointCloud |
168 | // Purpose : Constructor |
169 | //================================================== |
170 | AIS_PointCloud::AIS_PointCloud() |
171 | { |
bf5f0ca2 |
172 | myDrawer->SetupOwnShadingAspect(); |
173 | myDrawer->ShadingAspect()->Aspect()->SetMarkerType (Aspect_TOM_POINT); |
2831708b |
174 | |
d33222c1 |
175 | SetDisplayMode (AIS_PointCloud::DM_Points); |
176 | SetHilightMode (AIS_PointCloud::DM_BndBox); |
4a056d20 |
177 | |
178 | myDynHilightDrawer->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_PLUS, Quantity_NOC_CYAN1, 1.0)); |
d33222c1 |
179 | } |
180 | |
181 | //======================================================================= |
182 | //function : GetPoints |
183 | //purpose : |
184 | //======================================================================= |
185 | const Handle(Graphic3d_ArrayOfPoints) AIS_PointCloud::GetPoints() const |
186 | { |
187 | return myPoints; |
188 | } |
189 | |
190 | //======================================================================= |
191 | //function : GetBoundingBox |
192 | //purpose : |
193 | //======================================================================= |
194 | Bnd_Box AIS_PointCloud::GetBoundingBox() const |
195 | { |
196 | return myBndBox; |
197 | } |
198 | |
199 | //! Auxiliary method |
200 | static inline Bnd_Box getBoundingBox (const Handle(Graphic3d_ArrayOfPoints)& thePoints) |
201 | { |
202 | Bnd_Box aBndBox; |
203 | if (thePoints.IsNull()) |
204 | { |
205 | return aBndBox; |
206 | } |
207 | |
208 | const Standard_Integer aNbVertices = thePoints->VertexNumber(); |
209 | for (Standard_Integer aVertIter = 1; aVertIter <= aNbVertices; ++aVertIter) |
210 | { |
211 | aBndBox.Add (thePoints->Vertice (aVertIter)); |
212 | } |
213 | return aBndBox; |
214 | } |
215 | |
216 | //======================================================================= |
217 | //function : SetPoints |
218 | //purpose : |
219 | //======================================================================= |
220 | void AIS_PointCloud::SetPoints (const Handle(Graphic3d_ArrayOfPoints)& thePoints) |
221 | { |
222 | myPoints = thePoints; |
223 | myBndBox = getBoundingBox (thePoints); |
224 | } |
225 | |
226 | //======================================================================= |
227 | //function : SetPoints |
228 | //purpose : |
229 | //======================================================================= |
230 | void AIS_PointCloud::SetPoints (const Handle(TColgp_HArray1OfPnt)& theCoords, |
231 | const Handle(Quantity_HArray1OfColor)& theColors, |
232 | const Handle(TColgp_HArray1OfDir)& theNormals) |
233 | { |
234 | myPoints.Nullify(); |
235 | myBndBox.SetVoid(); |
236 | if (theCoords.IsNull()) |
237 | { |
238 | return; |
239 | } |
240 | |
241 | const Standard_Integer aNbPoints = theCoords->Length(); |
242 | if ((!theNormals.IsNull() && theNormals->Length() != aNbPoints) |
243 | || (!theColors.IsNull() && theColors->Length() != aNbPoints)) |
244 | { |
245 | // invalid input |
246 | return; |
247 | } |
248 | |
249 | const Standard_Boolean hasColors = !theColors.IsNull() && theColors->Length() == aNbPoints; |
250 | const Standard_Boolean hasNormals = !theNormals.IsNull() && theNormals->Length() == aNbPoints; |
251 | |
252 | const Standard_Integer aDiffColors = hasColors ? (theColors->Lower() - theCoords->Lower()) : 0; |
253 | const Standard_Integer aDiffNormals = hasNormals ? (theNormals->Lower() - theCoords->Lower()) : 0; |
254 | |
255 | myPoints = new Graphic3d_ArrayOfPoints (aNbPoints, hasColors, hasNormals); |
256 | for (Standard_Integer aPntIter = theCoords->Lower(); aPntIter <= theCoords->Upper(); ++aPntIter) |
257 | { |
258 | myPoints->AddVertex (theCoords->Value (aPntIter)); |
259 | if (hasColors) |
260 | { |
261 | myPoints->SetVertexColor (myPoints->VertexNumber(), |
262 | theColors->Value (aPntIter + aDiffColors)); |
263 | } |
264 | if (hasNormals) |
265 | { |
266 | myPoints->SetVertexNormal (myPoints->VertexNumber(), |
267 | theNormals->Value (aPntIter + aDiffNormals)); |
268 | } |
269 | } |
270 | myBndBox = getBoundingBox (myPoints); |
271 | } |
272 | |
d33222c1 |
273 | //======================================================================= |
274 | //function : SetColor |
275 | //purpose : |
276 | //======================================================================= |
277 | void AIS_PointCloud::SetColor (const Quantity_Color& theColor) |
278 | { |
279 | AIS_InteractiveObject::SetColor(theColor); |
280 | |
d33222c1 |
281 | myDrawer->ShadingAspect()->SetColor (theColor); |
bf5f0ca2 |
282 | SynchronizeAspects(); |
d33222c1 |
283 | } |
284 | |
285 | //======================================================================= |
286 | //function : UnsetColor |
287 | //purpose : |
288 | //======================================================================= |
289 | void AIS_PointCloud::UnsetColor() |
290 | { |
291 | if (!HasColor()) |
292 | { |
293 | return; |
294 | } |
295 | |
296 | AIS_InteractiveObject::UnsetColor(); |
d33222c1 |
297 | { |
a966542b |
298 | Graphic3d_MaterialAspect aDefaultMat (Graphic3d_NameOfMaterial_Brass); |
6262338c |
299 | Graphic3d_MaterialAspect aMat = aDefaultMat; |
bf5f0ca2 |
300 | Quantity_Color aColor = aDefaultMat.Color(); |
301 | if (myDrawer->HasLink()) |
302 | { |
303 | aColor = myDrawer->Link()->ShadingAspect()->Color (myCurrentFacingModel); |
304 | } |
6262338c |
305 | if (HasMaterial() || myDrawer->HasLink()) |
306 | { |
307 | aMat = AIS_GraphicTool::GetMaterial (HasMaterial() ? myDrawer : myDrawer->Link()); |
308 | } |
d33222c1 |
309 | if (HasMaterial()) |
310 | { |
d33222c1 |
311 | aMat.SetColor (aColor); |
312 | } |
313 | if (IsTransparent()) |
314 | { |
315 | Standard_Real aTransp = myDrawer->ShadingAspect()->Transparency (myCurrentFacingModel); |
4e1bc39a |
316 | aMat.SetTransparency (Standard_ShortReal(aTransp)); |
d33222c1 |
317 | } |
318 | myDrawer->ShadingAspect()->SetMaterial (aMat, myCurrentFacingModel); |
bf5f0ca2 |
319 | myDrawer->ShadingAspect()->Aspect()->SetInteriorColor (aColor); |
d33222c1 |
320 | } |
d33222c1 |
321 | |
bf5f0ca2 |
322 | SynchronizeAspects(); |
d33222c1 |
323 | } |
324 | |
d33222c1 |
325 | //======================================================================= |
326 | //function : SetMaterial |
327 | //purpose : |
328 | //======================================================================= |
329 | void AIS_PointCloud::SetMaterial (const Graphic3d_MaterialAspect& theMat) |
330 | { |
d33222c1 |
331 | hasOwnMaterial = Standard_True; |
332 | |
333 | myDrawer->ShadingAspect()->SetMaterial (theMat, myCurrentFacingModel); |
334 | if (HasColor()) |
335 | { |
f838dac4 |
336 | myDrawer->ShadingAspect()->SetColor (myDrawer->Color(), myCurrentFacingModel); |
d33222c1 |
337 | } |
f838dac4 |
338 | myDrawer->ShadingAspect()->SetTransparency (myDrawer->Transparency(), myCurrentFacingModel); |
bf5f0ca2 |
339 | SynchronizeAspects(); |
d33222c1 |
340 | } |
341 | |
342 | //======================================================================= |
343 | //function : UnsetMaterial |
344 | //purpose : |
345 | //======================================================================= |
346 | void AIS_PointCloud::UnsetMaterial() |
347 | { |
348 | if (!HasMaterial()) |
349 | { |
350 | return; |
351 | } |
352 | |
d33222c1 |
353 | { |
a966542b |
354 | Graphic3d_MaterialAspect aDefaultMat (Graphic3d_NameOfMaterial_Brass); |
6262338c |
355 | myDrawer->ShadingAspect()->SetMaterial (myDrawer->HasLink() ? |
356 | myDrawer->Link()->ShadingAspect()->Material (myCurrentFacingModel) : |
357 | aDefaultMat, |
d33222c1 |
358 | myCurrentFacingModel); |
359 | if (HasColor()) |
360 | { |
f838dac4 |
361 | myDrawer->ShadingAspect()->SetColor (myDrawer->Color(), myCurrentFacingModel); |
362 | myDrawer->ShadingAspect()->SetTransparency (myDrawer->Transparency(), myCurrentFacingModel); |
d33222c1 |
363 | } |
364 | } |
d33222c1 |
365 | hasOwnMaterial = Standard_False; |
bf5f0ca2 |
366 | SynchronizeAspects(); |
d33222c1 |
367 | } |
368 | |
369 | //======================================================================= |
370 | //function : Compute |
371 | //purpose : |
372 | //======================================================================= |
decbff0d |
373 | void AIS_PointCloud::Compute (const Handle(PrsMgr_PresentationManager)& , |
374 | const Handle(Prs3d_Presentation)& thePrs, |
375 | const Standard_Integer theMode) |
d33222c1 |
376 | { |
d33222c1 |
377 | switch (theMode) |
378 | { |
379 | case AIS_PointCloud::DM_Points: |
380 | { |
381 | const Handle(Graphic3d_ArrayOfPoints) aPoints = GetPoints(); |
382 | if (aPoints.IsNull()) |
383 | { |
384 | return; |
385 | } |
386 | |
bf5f0ca2 |
387 | Handle(Graphic3d_Group) aGroup = thePrs->NewGroup(); |
2831708b |
388 | aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect()); |
d33222c1 |
389 | aGroup->AddPrimitiveArray (aPoints); |
390 | break; |
391 | } |
392 | case AIS_PointCloud::DM_BndBox: |
393 | { |
394 | Bnd_Box aBndBox = GetBoundingBox(); |
395 | if (aBndBox.IsVoid()) |
396 | { |
397 | return; |
398 | } |
399 | |
7f24b768 |
400 | Prs3d_BndBox::Add (thePrs, aBndBox, myDrawer); |
d33222c1 |
401 | break; |
402 | } |
403 | } |
404 | } |
405 | |
406 | //======================================================================= |
407 | //function : ComputeSelection |
408 | //purpose : |
409 | //======================================================================= |
410 | void AIS_PointCloud::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, |
8b9a309b |
411 | const Standard_Integer theMode) |
d33222c1 |
412 | { |
8b9a309b |
413 | Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this); |
414 | switch (theMode) |
415 | { |
416 | case SM_Points: |
4a056d20 |
417 | case SM_SubsetOfPoints: |
8b9a309b |
418 | { |
419 | const Handle(Graphic3d_ArrayOfPoints) aPoints = GetPoints(); |
420 | if (!aPoints.IsNull() |
421 | && !aPoints->Attributes().IsNull()) |
422 | { |
4a056d20 |
423 | if (theMode == SM_SubsetOfPoints) |
424 | { |
425 | anOwner = new AIS_PointCloudOwner (this); |
426 | } |
427 | |
a228288f |
428 | // split large point clouds into several groups |
429 | const Standard_Integer aNbGroups = aPoints->Attributes()->NbElements > 500000 ? 8 : 1; |
8b9a309b |
430 | Handle(Select3D_SensitivePrimitiveArray) aSensitive = new Select3D_SensitivePrimitiveArray (anOwner); |
4a056d20 |
431 | aSensitive->SetDetectElements (true); |
432 | aSensitive->SetDetectElementMap (theMode == SM_SubsetOfPoints); |
8b9a309b |
433 | aSensitive->SetSensitivityFactor (8); |
a228288f |
434 | aSensitive->InitPoints (aPoints->Attributes(), aPoints->Indices(), TopLoc_Location(), true, aNbGroups); |
8b9a309b |
435 | aSensitive->BVH(); |
436 | theSelection->Add (aSensitive); |
437 | return; |
438 | } |
439 | break; |
440 | } |
441 | case SM_BndBox: |
442 | { |
443 | break; |
444 | } |
445 | default: |
446 | { |
447 | return; |
448 | } |
449 | } |
450 | |
d33222c1 |
451 | Bnd_Box aBndBox = GetBoundingBox(); |
452 | if (aBndBox.IsVoid()) |
453 | { |
454 | return; |
455 | } |
d33222c1 |
456 | Handle(Select3D_SensitiveBox) aSensBox = new Select3D_SensitiveBox (anOwner, aBndBox); |
457 | theSelection->Add (aSensBox); |
458 | } |