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 <SelectMgr_EntityOwner.hxx>
31 #include <SelectMgr_Selection.hxx>
32 #include <StdPrs_WFDeflectionRestrictedFace.hxx>
34 IMPLEMENT_STANDARD_HANDLE (AIS_PointCloud, AIS_InteractiveObject)
35 IMPLEMENT_STANDARD_RTTIEXT(AIS_PointCloud, AIS_InteractiveObject)
37 //==================================================
38 // Function: AIS_PointCloud
39 // Purpose : Constructor
40 //==================================================
41 AIS_PointCloud::AIS_PointCloud()
43 SetDisplayMode (AIS_PointCloud::DM_Points);
44 SetHilightMode (AIS_PointCloud::DM_BndBox);
47 //=======================================================================
48 //function : GetPoints
50 //=======================================================================
51 const Handle(Graphic3d_ArrayOfPoints) AIS_PointCloud::GetPoints() const
56 //=======================================================================
57 //function : GetBoundingBox
59 //=======================================================================
60 Bnd_Box AIS_PointCloud::GetBoundingBox() const
66 static inline Bnd_Box getBoundingBox (const Handle(Graphic3d_ArrayOfPoints)& thePoints)
69 if (thePoints.IsNull())
74 const Standard_Integer aNbVertices = thePoints->VertexNumber();
75 for (Standard_Integer aVertIter = 1; aVertIter <= aNbVertices; ++aVertIter)
77 aBndBox.Add (thePoints->Vertice (aVertIter));
82 //=======================================================================
83 //function : SetPoints
85 //=======================================================================
86 void AIS_PointCloud::SetPoints (const Handle(Graphic3d_ArrayOfPoints)& thePoints)
89 myBndBox = getBoundingBox (thePoints);
92 //=======================================================================
93 //function : SetPoints
95 //=======================================================================
96 void AIS_PointCloud::SetPoints (const Handle(TColgp_HArray1OfPnt)& theCoords,
97 const Handle(Quantity_HArray1OfColor)& theColors,
98 const Handle(TColgp_HArray1OfDir)& theNormals)
102 if (theCoords.IsNull())
107 const Standard_Integer aNbPoints = theCoords->Length();
108 if ((!theNormals.IsNull() && theNormals->Length() != aNbPoints)
109 || (!theColors.IsNull() && theColors->Length() != aNbPoints))
115 const Standard_Boolean hasColors = !theColors.IsNull() && theColors->Length() == aNbPoints;
116 const Standard_Boolean hasNormals = !theNormals.IsNull() && theNormals->Length() == aNbPoints;
118 const Standard_Integer aDiffColors = hasColors ? (theColors->Lower() - theCoords->Lower()) : 0;
119 const Standard_Integer aDiffNormals = hasNormals ? (theNormals->Lower() - theCoords->Lower()) : 0;
121 myPoints = new Graphic3d_ArrayOfPoints (aNbPoints, hasColors, hasNormals);
122 for (Standard_Integer aPntIter = theCoords->Lower(); aPntIter <= theCoords->Upper(); ++aPntIter)
124 myPoints->AddVertex (theCoords->Value (aPntIter));
127 myPoints->SetVertexColor (myPoints->VertexNumber(),
128 theColors->Value (aPntIter + aDiffColors));
132 myPoints->SetVertexNormal (myPoints->VertexNumber(),
133 theNormals->Value (aPntIter + aDiffNormals));
136 myBndBox = getBoundingBox (myPoints);
139 //=======================================================================
140 //function : SetColor
142 //=======================================================================
143 void AIS_PointCloud::SetColor (const Quantity_NameOfColor theColor)
145 SetColor (Quantity_Color (theColor));
148 //=======================================================================
149 //function : SetColor
151 //=======================================================================
152 void AIS_PointCloud::SetColor (const Quantity_Color& theColor)
154 AIS_InteractiveObject::SetColor(theColor);
156 if (!myDrawer->HasOwnPointAspect())
158 myDrawer->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_POINT, theColor, 1.0));
159 if (myDrawer->HasLink())
161 *myDrawer->PointAspect()->Aspect() = *myDrawer->Link()->PointAspect()->Aspect();
164 if (!myDrawer->HasOwnShadingAspect())
166 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
167 if (myDrawer->HasLink())
169 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
174 myDrawer->ShadingAspect()->SetColor (theColor);
175 myDrawer->PointAspect() ->SetColor (theColor);
177 const PrsMgr_Presentations& aPrsList = Presentations();
178 Handle(Graphic3d_AspectMarker3d) aPointAspect = myDrawer->PointAspect()->Aspect();
179 Handle(Graphic3d_AspectFillArea3d) anAreaAspect = myDrawer->ShadingAspect()->Aspect();
180 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
182 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
183 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
188 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
190 // Set aspects for presentation
191 aPrs->SetPrimitivesAspect (aPointAspect);
192 aPrs->SetPrimitivesAspect (anAreaAspect);
194 // Go through all groups to change color for all primitives
195 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
197 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
198 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_MARKER))
200 aGroup->SetGroupPrimitivesAspect (aPointAspect);
202 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
204 aGroup->SetGroupPrimitivesAspect (anAreaAspect);
210 //=======================================================================
211 //function : UnsetColor
213 //=======================================================================
214 void AIS_PointCloud::UnsetColor()
221 AIS_InteractiveObject::UnsetColor();
225 myDrawer->SetPointAspect (Handle(Prs3d_PointAspect)());
229 Quantity_Color aColor = Quantity_NOC_YELLOW;
230 Aspect_TypeOfMarker aType = Aspect_TOM_POINT;
231 Standard_Real aScale = 1.0;
232 if (myDrawer->HasLink())
234 myDrawer->Link()->PointAspect()->Aspect()->Values (aColor, aType, aScale);
236 myDrawer->PointAspect()->SetColor (aColor);
242 Graphic3d_MaterialAspect aDefaultMat (Graphic3d_NOM_BRASS);
243 Graphic3d_MaterialAspect aMat = aDefaultMat;
244 if (HasMaterial() || myDrawer->HasLink())
246 aMat = AIS_GraphicTool::GetMaterial (HasMaterial() ? myDrawer : myDrawer->Link());
250 Quantity_Color aColor = aDefaultMat.AmbientColor();
251 if (myDrawer->HasLink())
253 aColor = myDrawer->Link()->ShadingAspect()->Color (myCurrentFacingModel);
255 aMat.SetColor (aColor);
259 Standard_Real aTransp = myDrawer->ShadingAspect()->Transparency (myCurrentFacingModel);
260 aMat.SetTransparency (aTransp);
262 myDrawer->ShadingAspect()->SetMaterial (aMat, myCurrentFacingModel);
266 myDrawer->SetShadingAspect (Handle(Prs3d_ShadingAspect)());
268 myDrawer->SetPointAspect (Handle(Prs3d_PointAspect)());
270 // modify shading presentation without re-computation
271 const PrsMgr_Presentations& aPrsList = Presentations();
272 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->Link()->ShadingAspect()->Aspect();
273 Handle(Graphic3d_AspectMarker3d) aMarkerAsp = myDrawer->Link()->PointAspect()->Aspect();
274 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
276 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
277 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
282 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
283 aPrs->SetPrimitivesAspect (anAreaAsp);
284 aPrs->SetPrimitivesAspect (aMarkerAsp);
285 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
287 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
289 // Check if aspect of given type is set for the group,
290 // because setting aspect for group with no already set aspect
291 // can lead to loss of presentation data
292 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
294 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
296 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_MARKER))
298 aGroup->SetGroupPrimitivesAspect (aMarkerAsp);
304 //=======================================================================
305 //function : SetMaterial
307 //=======================================================================
308 void AIS_PointCloud::SetMaterial (const Graphic3d_NameOfMaterial theMatName)
310 SetMaterial (Graphic3d_MaterialAspect (theMatName));
313 //=======================================================================
314 //function : SetMaterial
316 //=======================================================================
317 void AIS_PointCloud::SetMaterial (const Graphic3d_MaterialAspect& theMat)
319 if (!myDrawer->HasOwnShadingAspect())
321 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
322 if (myDrawer->HasLink())
324 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
327 hasOwnMaterial = Standard_True;
329 myDrawer->ShadingAspect()->SetMaterial (theMat, myCurrentFacingModel);
332 myDrawer->ShadingAspect()->SetColor (myOwnColor, myCurrentFacingModel);
334 myDrawer->ShadingAspect()->SetTransparency (myTransparency, myCurrentFacingModel);
336 // modify shading presentation without re-computation
337 const PrsMgr_Presentations& aPrsList = Presentations();
338 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
339 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
341 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
342 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
347 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
348 aPrs->SetPrimitivesAspect (anAreaAsp);
349 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
351 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
352 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
354 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
360 //=======================================================================
361 //function : UnsetMaterial
363 //=======================================================================
364 void AIS_PointCloud::UnsetMaterial()
374 Graphic3d_MaterialAspect aDefaultMat (Graphic3d_NOM_BRASS);
375 myDrawer->ShadingAspect()->SetMaterial (myDrawer->HasLink() ?
376 myDrawer->Link()->ShadingAspect()->Material (myCurrentFacingModel) :
378 myCurrentFacingModel);
381 myDrawer->ShadingAspect()->SetColor (myOwnColor, myCurrentFacingModel);
382 myDrawer->ShadingAspect()->SetTransparency (myTransparency, myCurrentFacingModel);
387 myDrawer->SetShadingAspect (Handle(Prs3d_ShadingAspect)());
389 hasOwnMaterial = Standard_False;
391 // modify shading presentation without re-computation
392 const PrsMgr_Presentations& aPrsList = Presentations();
393 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
394 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
396 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
397 if (aPrsModed.Mode() != AIS_PointCloud::DM_Points)
402 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
403 aPrs->SetPrimitivesAspect (anAreaAsp);
404 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
406 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
407 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
409 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
415 //=======================================================================
418 //=======================================================================
419 void AIS_PointCloud::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
420 const Handle(Prs3d_Presentation)& thePrs,
421 const Standard_Integer theMode)
426 case AIS_PointCloud::DM_Points:
428 const Handle(Graphic3d_ArrayOfPoints) aPoints = GetPoints();
429 if (aPoints.IsNull())
434 Handle(Graphic3d_AspectMarker3d) aMarkerAspect = myDrawer->PointAspect()->Aspect();
435 if (!myDrawer->HasOwnPointAspect())
437 aMarkerAspect->SetType (Aspect_TOM_POINT);
440 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePrs);
441 aGroup->SetGroupPrimitivesAspect (aMarkerAspect);
442 aGroup->AddPrimitiveArray (aPoints);
445 case AIS_PointCloud::DM_BndBox:
447 Bnd_Box aBndBox = GetBoundingBox();
448 if (aBndBox.IsVoid())
453 StdPrs_WFDeflectionRestrictedFace::AddBox (thePrs, aBndBox, myDrawer);
459 //=======================================================================
460 //function : ComputeSelection
462 //=======================================================================
463 void AIS_PointCloud::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
464 const Standard_Integer /*theMode*/)
466 Bnd_Box aBndBox = GetBoundingBox();
467 if (aBndBox.IsVoid())
472 Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
473 Handle(Select3D_SensitiveBox) aSensBox = new Select3D_SensitiveBox (anOwner, aBndBox);
474 theSelection->Add (aSensBox);