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 Quantity_Color aColor = Quantity_NOC_YELLOW;
228 Aspect_TypeOfMarker aType = Aspect_TOM_POINT;
229 Standard_Real aScale = 1.0;
230 if (myDrawer->HasLink())
232 myDrawer->Link()->PointAspect()->Aspect()->Values (aColor, aType, aScale);
234 myDrawer->PointAspect()->SetColor (aColor);
240 Graphic3d_MaterialAspect aDefaultMat (Graphic3d_NOM_BRASS);
241 Graphic3d_MaterialAspect aMat = aDefaultMat;
242 if (HasMaterial() || myDrawer->HasLink())
244 aMat = AIS_GraphicTool::GetMaterial (HasMaterial() ? myDrawer : myDrawer->Link());
248 Quantity_Color aColor = aDefaultMat.AmbientColor();
249 if (myDrawer->HasLink())
251 aColor = myDrawer->Link()->ShadingAspect()->Color (myCurrentFacingModel);
253 aMat.SetColor (aColor);
257 Standard_Real aTransp = myDrawer->ShadingAspect()->Transparency (myCurrentFacingModel);
258 aMat.SetTransparency (aTransp);
260 myDrawer->ShadingAspect()->SetMaterial (aMat, myCurrentFacingModel);
264 myDrawer->SetShadingAspect (Handle(Prs3d_ShadingAspect)());
266 myDrawer->SetPointAspect (Handle(Prs3d_PointAspect)());
268 // modify shading presentation without re-computation
269 const PrsMgr_Presentations& aPrsList = Presentations();
270 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->Link()->ShadingAspect()->Aspect();
271 Handle(Graphic3d_AspectMarker3d) aMarkerAsp = myDrawer->Link()->PointAspect()->Aspect();
272 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
274 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
275 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
280 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
281 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
283 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
285 // Check if aspect of given type is set for the group,
286 // because setting aspect for group with no already set aspect
287 // can lead to loss of presentation data
288 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
290 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
292 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_MARKER))
294 aGroup->SetGroupPrimitivesAspect (aMarkerAsp);
300 //=======================================================================
301 //function : SetMaterial
303 //=======================================================================
304 void AIS_PointCloud::SetMaterial (const Graphic3d_NameOfMaterial theMatName)
306 SetMaterial (Graphic3d_MaterialAspect (theMatName));
309 //=======================================================================
310 //function : SetMaterial
312 //=======================================================================
313 void AIS_PointCloud::SetMaterial (const Graphic3d_MaterialAspect& theMat)
315 if (!myDrawer->HasOwnShadingAspect())
317 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
318 if (myDrawer->HasLink())
320 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
323 hasOwnMaterial = Standard_True;
325 myDrawer->ShadingAspect()->SetMaterial (theMat, myCurrentFacingModel);
328 myDrawer->ShadingAspect()->SetColor (myOwnColor, myCurrentFacingModel);
330 myDrawer->ShadingAspect()->SetTransparency (myTransparency, myCurrentFacingModel);
332 // modify shading presentation without re-computation
333 const PrsMgr_Presentations& aPrsList = Presentations();
334 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
335 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
337 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
338 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
343 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
344 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
346 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
347 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
349 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
355 //=======================================================================
356 //function : UnsetMaterial
358 //=======================================================================
359 void AIS_PointCloud::UnsetMaterial()
369 Graphic3d_MaterialAspect aDefaultMat (Graphic3d_NOM_BRASS);
370 myDrawer->ShadingAspect()->SetMaterial (myDrawer->HasLink() ?
371 myDrawer->Link()->ShadingAspect()->Material (myCurrentFacingModel) :
373 myCurrentFacingModel);
376 myDrawer->ShadingAspect()->SetColor (myOwnColor, myCurrentFacingModel);
377 myDrawer->ShadingAspect()->SetTransparency (myTransparency, myCurrentFacingModel);
382 myDrawer->SetShadingAspect (Handle(Prs3d_ShadingAspect)());
384 hasOwnMaterial = Standard_False;
386 // modify shading presentation without re-computation
387 const PrsMgr_Presentations& aPrsList = Presentations();
388 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
389 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
391 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
392 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
397 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
398 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
400 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
401 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
403 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
409 //=======================================================================
412 //=======================================================================
413 void AIS_PointCloud::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
414 const Handle(Prs3d_Presentation)& thePrs,
415 const Standard_Integer theMode)
420 case AIS_PointCloud::DM_Points:
422 const Handle(Graphic3d_ArrayOfPoints) aPoints = GetPoints();
423 if (aPoints.IsNull())
428 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePrs);
429 aGroup->SetGroupPrimitivesAspect (myDrawer->PointAspect()->Aspect());
430 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
431 aGroup->AddPrimitiveArray (aPoints);
434 case AIS_PointCloud::DM_BndBox:
436 Bnd_Box aBndBox = GetBoundingBox();
437 if (aBndBox.IsVoid())
442 StdPrs_BndBox::Add (thePrs, aBndBox, myDrawer);
448 //=======================================================================
449 //function : ComputeSelection
451 //=======================================================================
452 void AIS_PointCloud::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
453 const Standard_Integer theMode)
455 Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
460 const Handle(Graphic3d_ArrayOfPoints) aPoints = GetPoints();
461 if (!aPoints.IsNull()
462 && !aPoints->Attributes().IsNull())
464 Handle(Select3D_SensitivePrimitiveArray) aSensitive = new Select3D_SensitivePrimitiveArray (anOwner);
465 aSensitive->SetSensitivityFactor (8);
466 aSensitive->InitPoints (aPoints->Attributes(), aPoints->Indices(), TopLoc_Location());
468 theSelection->Add (aSensitive);
483 Bnd_Box aBndBox = GetBoundingBox();
484 if (aBndBox.IsVoid())
488 Handle(Select3D_SensitiveBox) aSensBox = new Select3D_SensitiveBox (anOwner, aBndBox);
489 theSelection->Add (aSensBox);