1 // Created on: 2014-08-13
2 // Created by: Maxim GLIBIN
3 // Copyright (c) 2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <AIS_PointCloud.hxx>
18 #include <AIS_GraphicTool.hxx>
19 #include <Graphic3d_AspectFillArea3d.hxx>
20 #include <Graphic3d_AspectMarker3d.hxx>
21 #include <Graphic3d_Group.hxx>
22 #include <Prs3d_Drawer.hxx>
23 #include <Prs3d_PointAspect.hxx>
24 #include <Prs3d_Presentation.hxx>
25 #include <Prs3d_Root.hxx>
26 #include <Prs3d_ShadingAspect.hxx>
27 #include <PrsMgr_ModedPresentation.hxx>
28 #include <PrsMgr_Presentations.hxx>
29 #include <Select3D_SensitiveBox.hxx>
30 #include <Select3D_SensitivePrimitiveArray.hxx>
31 #include <SelectMgr_EntityOwner.hxx>
32 #include <SelectMgr_Selection.hxx>
33 #include <StdPrs_BndBox.hxx>
36 IMPLEMENT_STANDARD_RTTIEXT(AIS_PointCloud,AIS_InteractiveObject)
38 //==================================================
39 // Function: AIS_PointCloud
40 // Purpose : Constructor
41 //==================================================
42 AIS_PointCloud::AIS_PointCloud()
44 // override default point style to Aspect_TOM_POINT
45 myDrawer->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_POINT, Quantity_NOC_YELLOW, 1.0));
47 SetDisplayMode (AIS_PointCloud::DM_Points);
48 SetHilightMode (AIS_PointCloud::DM_BndBox);
51 //=======================================================================
52 //function : GetPoints
54 //=======================================================================
55 const Handle(Graphic3d_ArrayOfPoints) AIS_PointCloud::GetPoints() const
60 //=======================================================================
61 //function : GetBoundingBox
63 //=======================================================================
64 Bnd_Box AIS_PointCloud::GetBoundingBox() const
70 static inline Bnd_Box getBoundingBox (const Handle(Graphic3d_ArrayOfPoints)& thePoints)
73 if (thePoints.IsNull())
78 const Standard_Integer aNbVertices = thePoints->VertexNumber();
79 for (Standard_Integer aVertIter = 1; aVertIter <= aNbVertices; ++aVertIter)
81 aBndBox.Add (thePoints->Vertice (aVertIter));
86 //=======================================================================
87 //function : SetPoints
89 //=======================================================================
90 void AIS_PointCloud::SetPoints (const Handle(Graphic3d_ArrayOfPoints)& thePoints)
93 myBndBox = getBoundingBox (thePoints);
96 //=======================================================================
97 //function : SetPoints
99 //=======================================================================
100 void AIS_PointCloud::SetPoints (const Handle(TColgp_HArray1OfPnt)& theCoords,
101 const Handle(Quantity_HArray1OfColor)& theColors,
102 const Handle(TColgp_HArray1OfDir)& theNormals)
106 if (theCoords.IsNull())
111 const Standard_Integer aNbPoints = theCoords->Length();
112 if ((!theNormals.IsNull() && theNormals->Length() != aNbPoints)
113 || (!theColors.IsNull() && theColors->Length() != aNbPoints))
119 const Standard_Boolean hasColors = !theColors.IsNull() && theColors->Length() == aNbPoints;
120 const Standard_Boolean hasNormals = !theNormals.IsNull() && theNormals->Length() == aNbPoints;
122 const Standard_Integer aDiffColors = hasColors ? (theColors->Lower() - theCoords->Lower()) : 0;
123 const Standard_Integer aDiffNormals = hasNormals ? (theNormals->Lower() - theCoords->Lower()) : 0;
125 myPoints = new Graphic3d_ArrayOfPoints (aNbPoints, hasColors, hasNormals);
126 for (Standard_Integer aPntIter = theCoords->Lower(); aPntIter <= theCoords->Upper(); ++aPntIter)
128 myPoints->AddVertex (theCoords->Value (aPntIter));
131 myPoints->SetVertexColor (myPoints->VertexNumber(),
132 theColors->Value (aPntIter + aDiffColors));
136 myPoints->SetVertexNormal (myPoints->VertexNumber(),
137 theNormals->Value (aPntIter + aDiffNormals));
140 myBndBox = getBoundingBox (myPoints);
143 //=======================================================================
144 //function : SetColor
146 //=======================================================================
147 void AIS_PointCloud::SetColor (const Quantity_NameOfColor theColor)
149 SetColor (Quantity_Color (theColor));
152 //=======================================================================
153 //function : SetColor
155 //=======================================================================
156 void AIS_PointCloud::SetColor (const Quantity_Color& theColor)
158 AIS_InteractiveObject::SetColor(theColor);
160 if (!myDrawer->HasOwnPointAspect())
162 myDrawer->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_POINT, theColor, 1.0));
163 if (myDrawer->HasLink())
165 *myDrawer->PointAspect()->Aspect() = *myDrawer->Link()->PointAspect()->Aspect();
168 if (!myDrawer->HasOwnShadingAspect())
170 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
171 if (myDrawer->HasLink())
173 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
178 myDrawer->ShadingAspect()->SetColor (theColor);
179 myDrawer->PointAspect() ->SetColor (theColor);
181 const PrsMgr_Presentations& aPrsList = Presentations();
182 Handle(Graphic3d_AspectMarker3d) aPointAspect = myDrawer->PointAspect()->Aspect();
183 Handle(Graphic3d_AspectFillArea3d) anAreaAspect = myDrawer->ShadingAspect()->Aspect();
184 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
186 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
187 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
192 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
193 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
195 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
196 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_MARKER))
198 aGroup->SetGroupPrimitivesAspect (aPointAspect);
200 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
202 aGroup->SetGroupPrimitivesAspect (anAreaAspect);
208 //=======================================================================
209 //function : UnsetColor
211 //=======================================================================
212 void AIS_PointCloud::UnsetColor()
219 AIS_InteractiveObject::UnsetColor();
223 myDrawer->SetPointAspect (Handle(Prs3d_PointAspect)());
227 myDrawer->PointAspect()->SetColor (myDrawer->HasLink()
228 ? myDrawer->Link()->PointAspect()->Aspect()->Color()
229 : Quantity_Color (Quantity_NOC_YELLOW));
235 Graphic3d_MaterialAspect aDefaultMat (Graphic3d_NOM_BRASS);
236 Graphic3d_MaterialAspect aMat = aDefaultMat;
237 if (HasMaterial() || myDrawer->HasLink())
239 aMat = AIS_GraphicTool::GetMaterial (HasMaterial() ? myDrawer : myDrawer->Link());
243 Quantity_Color aColor = aDefaultMat.AmbientColor();
244 if (myDrawer->HasLink())
246 aColor = myDrawer->Link()->ShadingAspect()->Color (myCurrentFacingModel);
248 aMat.SetColor (aColor);
252 Standard_Real aTransp = myDrawer->ShadingAspect()->Transparency (myCurrentFacingModel);
253 aMat.SetTransparency (aTransp);
255 myDrawer->ShadingAspect()->SetMaterial (aMat, myCurrentFacingModel);
259 myDrawer->SetShadingAspect (Handle(Prs3d_ShadingAspect)());
261 myDrawer->SetPointAspect (Handle(Prs3d_PointAspect)());
263 // modify shading presentation without re-computation
264 const PrsMgr_Presentations& aPrsList = Presentations();
265 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->Link()->ShadingAspect()->Aspect();
266 Handle(Graphic3d_AspectMarker3d) aMarkerAsp = myDrawer->Link()->PointAspect()->Aspect();
267 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
269 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
270 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
275 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
276 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
278 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
280 // Check if aspect of given type is set for the group,
281 // because setting aspect for group with no already set aspect
282 // can lead to loss of presentation data
283 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
285 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
287 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_MARKER))
289 aGroup->SetGroupPrimitivesAspect (aMarkerAsp);
295 //=======================================================================
296 //function : SetMaterial
298 //=======================================================================
299 void AIS_PointCloud::SetMaterial (const Graphic3d_NameOfMaterial theMatName)
301 SetMaterial (Graphic3d_MaterialAspect (theMatName));
304 //=======================================================================
305 //function : SetMaterial
307 //=======================================================================
308 void AIS_PointCloud::SetMaterial (const Graphic3d_MaterialAspect& theMat)
310 if (!myDrawer->HasOwnShadingAspect())
312 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
313 if (myDrawer->HasLink())
315 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
318 hasOwnMaterial = Standard_True;
320 myDrawer->ShadingAspect()->SetMaterial (theMat, myCurrentFacingModel);
323 myDrawer->ShadingAspect()->SetColor (myDrawer->Color(), myCurrentFacingModel);
325 myDrawer->ShadingAspect()->SetTransparency (myDrawer->Transparency(), myCurrentFacingModel);
327 // modify shading presentation without re-computation
328 const PrsMgr_Presentations& aPrsList = Presentations();
329 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
330 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
332 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
333 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
338 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
339 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
341 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
342 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
344 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
350 //=======================================================================
351 //function : UnsetMaterial
353 //=======================================================================
354 void AIS_PointCloud::UnsetMaterial()
364 Graphic3d_MaterialAspect aDefaultMat (Graphic3d_NOM_BRASS);
365 myDrawer->ShadingAspect()->SetMaterial (myDrawer->HasLink() ?
366 myDrawer->Link()->ShadingAspect()->Material (myCurrentFacingModel) :
368 myCurrentFacingModel);
371 myDrawer->ShadingAspect()->SetColor (myDrawer->Color(), myCurrentFacingModel);
372 myDrawer->ShadingAspect()->SetTransparency (myDrawer->Transparency(), myCurrentFacingModel);
377 myDrawer->SetShadingAspect (Handle(Prs3d_ShadingAspect)());
379 hasOwnMaterial = Standard_False;
381 // modify shading presentation without re-computation
382 const PrsMgr_Presentations& aPrsList = Presentations();
383 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
384 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
386 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
387 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
392 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
393 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
395 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
396 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
398 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
404 //=======================================================================
407 //=======================================================================
408 void AIS_PointCloud::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
409 const Handle(Prs3d_Presentation)& thePrs,
410 const Standard_Integer theMode)
414 case AIS_PointCloud::DM_Points:
416 const Handle(Graphic3d_ArrayOfPoints) aPoints = GetPoints();
417 if (aPoints.IsNull())
422 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePrs);
423 aGroup->SetGroupPrimitivesAspect (myDrawer->PointAspect()->Aspect());
424 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
425 aGroup->AddPrimitiveArray (aPoints);
428 case AIS_PointCloud::DM_BndBox:
430 Bnd_Box aBndBox = GetBoundingBox();
431 if (aBndBox.IsVoid())
436 StdPrs_BndBox::Add (thePrs, aBndBox, myDrawer);
442 //=======================================================================
443 //function : ComputeSelection
445 //=======================================================================
446 void AIS_PointCloud::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
447 const Standard_Integer theMode)
449 Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
454 const Handle(Graphic3d_ArrayOfPoints) aPoints = GetPoints();
455 if (!aPoints.IsNull()
456 && !aPoints->Attributes().IsNull())
458 Handle(Select3D_SensitivePrimitiveArray) aSensitive = new Select3D_SensitivePrimitiveArray (anOwner);
459 aSensitive->SetSensitivityFactor (8);
460 aSensitive->InitPoints (aPoints->Attributes(), aPoints->Indices(), TopLoc_Location());
462 theSelection->Add (aSensitive);
477 Bnd_Box aBndBox = GetBoundingBox();
478 if (aBndBox.IsVoid())
482 Handle(Select3D_SensitiveBox) aSensBox = new Select3D_SensitiveBox (anOwner, aBndBox);
483 theSelection->Add (aSensBox);