From fe8bd8ca09325439342fc3038b261ac439489288 Mon Sep 17 00:00:00 2001 From: mgn Date: Mon, 25 Aug 2014 18:42:28 +0400 Subject: [PATCH] Refactor code. Add AIS_PointCloud.lxx file for inline method. Add GetPoints virtual method. Change the type of arguments of SetPoints function. Adapt test function. --- src/AIS/AIS.cdl | 2 +- src/AIS/AIS_PointCloud.cxx | 95 +++++++++------- src/AIS/AIS_PointCloud.hxx | 49 ++++---- src/AIS/AIS_PointCloud.lxx | 19 ++++ src/AIS/FILES | 1 + src/ViewerTest/ViewerTest_ObjectCommands.cxx | 111 ++++++++++--------- 6 files changed, 162 insertions(+), 115 deletions(-) create mode 100644 src/AIS/AIS_PointCloud.lxx diff --git a/src/AIS/AIS.cdl b/src/AIS/AIS.cdl index 50b17bb717..3756366f4e 100644 --- a/src/AIS/AIS.cdl +++ b/src/AIS/AIS.cdl @@ -368,7 +368,7 @@ is ---Category: General Objects class ConnectedInteractive; --signature 0 class MultipleConnectedInteractive; --signature 1 - imported PointCloud; --signature 2 + imported PointCloud; ---Category: DIMENSIONS AND RELATIONS diff --git a/src/AIS/AIS_PointCloud.cxx b/src/AIS/AIS_PointCloud.cxx index 6527786cea..0b63b95bfb 100644 --- a/src/AIS/AIS_PointCloud.cxx +++ b/src/AIS/AIS_PointCloud.cxx @@ -13,24 +13,23 @@ // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. -#include #include +#include +#include + +#include +#include -#include -#include #include +#include +#include -#include #include - -#include -#include +#include IMPLEMENT_STANDARD_HANDLE(AIS_PointCloud, AIS_InteractiveObject) IMPLEMENT_STANDARD_RTTIEXT(AIS_PointCloud, AIS_InteractiveObject) -IMPLEMENT_HARRAY1(AIS_ArrayOfPnt) - //================================================== // Function: AIS_PointCloud // Purpose : Constructor @@ -44,38 +43,50 @@ AIS_PointCloud::AIS_PointCloud() //function : SetPoints //purpose : //======================================================================= -bool AIS_PointCloud::SetPoints (const Handle(Graphic3d_ArrayOfPoints)& thePoints) +void AIS_PointCloud::SetPoints (const Handle(Graphic3d_ArrayOfPoints)& thePoints) { - if (thePoints.IsNull() || !thePoints->IsValid()) return false; + myPoints.Nullify(); + + if (thePoints.IsNull()) + return; + myPoints = thePoints; - return true; } //======================================================================= //function : SetPoints //purpose : //======================================================================= -bool AIS_PointCloud::SetPoints (const Handle(AIS_ArrayOfPnt)& theCoords, - const Handle(AIS_ArrayOfPnt)& theColors, - const Standard_Boolean hasColors) +void AIS_PointCloud::SetPoints (const Handle(TColgp_HArray1OfPnt)& theCoords, + const Handle(Quantity_HArray1OfColor)& theColors) { - if (theCoords.IsNull() || theColors.IsNull()) return false; - if (theCoords->Size() != theColors->Size()) return false; + myPoints.Nullify(); + + if (theCoords.IsNull()) + return; + + Standard_Integer aNumPoints = theCoords->Length(); - Standard_Integer aNumPoints = theCoords->Size(); + Standard_Boolean aHasColors = Standard_False; + if (!theColors.IsNull() && aNumPoints == theColors->Length()) + aHasColors = Standard_True; - Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (aNumPoints, hasColors); - Standard_Integer iter = 1; - for (; iter <= aNumPoints; iter++) + myPoints = new Graphic3d_ArrayOfPoints (aNumPoints, aHasColors); + + for (Standard_Integer aPntIter = theCoords->Lower(); aPntIter <= theCoords->Upper(); aPntIter++) { - anArrayOfPoints->AddVertex (theCoords->Value(iter)); + myPoints->AddVertex (theCoords->Value (aPntIter)); + } - gp_Pnt aColor = theColors->Value(iter); - anArrayOfPoints->SetVertexColor (anArrayOfPoints->VertexNumber(), - aColor.X(), aColor.Y(), aColor.Z()); + if (aHasColors) + { + Standard_Integer aNumVertex = 1; + for(Standard_Integer aColorIter = theColors->Lower(); aColorIter <= theColors->Upper(); aColorIter++) + { + myPoints->SetVertexColor (aNumVertex, theColors->Value (aColorIter)); + aNumVertex++; + } } - myPoints = anArrayOfPoints; - return true; } //======================================================================= @@ -84,14 +95,14 @@ bool AIS_PointCloud::SetPoints (const Handle(AIS_ArrayOfPnt)& theCoords, //======================================================================= void AIS_PointCloud::SetColor (const Quantity_NameOfColor theColor) { - SetColor (Quantity_Color(theColor)); + SetColor (Quantity_Color (theColor)); } //======================================================================= //function : SetColor -//purpose : +//purpose : //======================================================================= -void AIS_PointCloud::SetColor (const Quantity_Color &theColor) +void AIS_PointCloud::SetColor (const Quantity_Color& theColor) { if (!myDrawer->HasPointAspect()) { @@ -108,26 +119,28 @@ void AIS_PointCloud::SetColor (const Quantity_Color &theColor) //function : Compute //purpose : //======================================================================= -void AIS_PointCloud::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/, - const Handle(Prs3d_Presentation)& aPresentation, - const Standard_Integer /*aMode*/) +void AIS_PointCloud::Compute(const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/, + const Handle(Prs3d_Presentation)& thePresentation, + const Standard_Integer /*theMode*/) { - aPresentation->Clear(); - if (myPoints.IsNull() || !myPoints->IsValid()) return; + thePresentation->Clear(); + + if (GetPoints().IsNull() || !GetPoints()->IsValid()) + return; - Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (aPresentation); - if (Attributes()->HasPointAspect()) + Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation); + if (myDrawer->HasPointAspect()) { - aGroup->SetPrimitivesAspect (Attributes()->PointAspect()->Aspect()); + aGroup->SetPrimitivesAspect (myDrawer->PointAspect()->Aspect()); } - aGroup->AddPrimitiveArray (myPoints); + aGroup->AddPrimitiveArray (GetPoints()); } //======================================================================= //function : ComputeSelection //purpose : //======================================================================= -void AIS_PointCloud::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/, - const Standard_Integer /*aMode*/) +void AIS_PointCloud::ComputeSelection(const Handle(SelectMgr_Selection)& /*theSelection*/, + const Standard_Integer /*theMode*/) { } diff --git a/src/AIS/AIS_PointCloud.hxx b/src/AIS/AIS_PointCloud.hxx index 5bacffedcc..6a875d8b4c 100644 --- a/src/AIS/AIS_PointCloud.hxx +++ b/src/AIS/AIS_PointCloud.hxx @@ -18,16 +18,19 @@ #include #include -#include + +#include + +#include + #include +#include #include -#include -#include -#include -DEFINE_STANDARD_HANDLE (AIS_PointCloud, AIS_InteractiveObject) +#include +#include -NCOLLECTION_HARRAY1(AIS_ArrayOfPnt, gp_Pnt) +DEFINE_STANDARD_HANDLE (AIS_PointCloud, AIS_InteractiveObject) //! Interactive object for set of points. class AIS_PointCloud : public AIS_InteractiveObject @@ -35,23 +38,29 @@ class AIS_PointCloud : public AIS_InteractiveObject public: - //! Create point cloud. + //! Constructor Standard_EXPORT AIS_PointCloud (); - //! Initializes construction of the point cloud from points. - //! @param thePoints [in] the points. - Standard_EXPORT bool SetPoints (const Handle(Graphic3d_ArrayOfPoints)& thePoints); + //! Sets the points from array of points. + //! @detailed This function allows to avoid data duplication. + //! @param thePoints [in] the array of points. + Standard_EXPORT virtual void SetPoints (const Handle(Graphic3d_ArrayOfPoints)& thePoints); - //! Sets the points. + //! Sets the points with optional colors. //! @param theCoords [in] the array of coordinates. - //! @param theColors [in] the array of RGB colors. - Standard_EXPORT bool SetPoints (const Handle(AIS_ArrayOfPnt)& theCoords, - const Handle(AIS_ArrayOfPnt)& theColors, - const Standard_Boolean hasColors = true); + //! @param theColors [in] optional array of colors. + Standard_EXPORT virtual void SetPoints (const Handle(TColgp_HArray1OfPnt)& theCoords, + const Handle(Quantity_HArray1OfColor)& theColors = NULL); + + //! Get the points. + //! @return the array of points. + Standard_EXPORT virtual const Handle(Graphic3d_ArrayOfPoints)& GetPoints () const; + + //! Redefined method implemets the standard behavior. + Standard_EXPORT virtual void SetColor (const Quantity_NameOfColor theColor); - //! Sets the color theColor in the reconstructed point cloud - Standard_EXPORT void SetColor (const Quantity_NameOfColor theColor); - Standard_EXPORT void SetColor (const Quantity_Color &theColor); + //! Redefined method implemets the standard behavior. + Standard_EXPORT virtual void SetColor (const Quantity_Color& theColor); public: @@ -63,8 +72,8 @@ protected: const Handle(Prs3d_Presentation)& thePresentation, const Standard_Integer theMode = 0); - Standard_EXPORT virtual void ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, - const Standard_Integer aMode); + Standard_EXPORT virtual void ComputeSelection(const Handle(SelectMgr_Selection)& theSelection, + const Standard_Integer theMode); private: diff --git a/src/AIS/AIS_PointCloud.lxx b/src/AIS/AIS_PointCloud.lxx new file mode 100644 index 0000000000..5858036efc --- /dev/null +++ b/src/AIS/AIS_PointCloud.lxx @@ -0,0 +1,19 @@ +// Created on: 2014-08-25 +// Created by: Maxim GLIBIN +// Copyright (c) 2014 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +inline const Handle(Graphic3d_ArrayOfPoints)& AIS_PointCloud::GetPoints() const +{ + return myPoints; +} diff --git a/src/AIS/FILES b/src/AIS/FILES index 2f3c30c8e9..b335e6b64a 100755 --- a/src/AIS/FILES +++ b/src/AIS/FILES @@ -22,4 +22,5 @@ AIS_DiameterDimension.cxx AIS_RadiusDimension.hxx AIS_RadiusDimension.cxx AIS_PointCloud.hxx +AIS_PointCloud.lxx AIS_PointCloud.cxx diff --git a/src/ViewerTest/ViewerTest_ObjectCommands.cxx b/src/ViewerTest/ViewerTest_ObjectCommands.cxx index c715a0f9ab..83a479889d 100644 --- a/src/ViewerTest/ViewerTest_ObjectCommands.cxx +++ b/src/ViewerTest/ViewerTest_ObjectCommands.cxx @@ -5031,7 +5031,7 @@ static int VFont (Draw_Interpretor& theDI, //! Auxilarily function. //! Create random float number in range from theMin to theMax. -static Standard_Real RandomReal (const Standard_Real theMin, const Standard_Real theMax) +static Standard_Real randomReal (const Standard_Real theMin, const Standard_Real theMax) { return theMin + (theMax - theMin) * Standard_Real(rand()) / RAND_MAX; } @@ -5060,22 +5060,16 @@ static Standard_Integer VPointCloud (Draw_Interpretor& /*theDI*/, Standard_Integer anArgIter = 1; - // Get and check point cloud name in AIS context + // Get point cloud object name TCollection_AsciiString aName (theArgs[anArgIter++]); - if (GetMapOfAIS().IsBound2 (aName)) - { - std::cout << theArgs[0] << " error: object name exists." << std::endl; - return 1; - } // Sets default value - Standard_Integer aMode = 0; - Standard_Integer aMarkerType = -1; - Quantity_NameOfColor aColorName = Quantity_NOC_YELLOW; - Standard_Real aScale = 1.; - Standard_Boolean aHasColor = true; - Standard_Integer aNumberOfPoints = 1000; - TCollection_AsciiString aFileName = NULL; + Standard_Integer aMode = 0; + Standard_Integer aMarkerType = -1; + Quantity_NameOfColor aColorName = Quantity_NOC_YELLOW; + Standard_Real aScale = 1.0; + Standard_Boolean aHasColor = Standard_True; + Standard_Integer aNumberOfPoints = 100; // Parses arguments for (; anArgIter < theArgNum; ++anArgIter) @@ -5084,7 +5078,7 @@ static Standard_Integer VPointCloud (Draw_Interpretor& /*theDI*/, if (anArg.Search ("Mode=") > -1) { aMode = anArg.Token ("=", 2).IntegerValue(); - if (aMode != 0 && aMode != 1) + if (aMode < 0 && aMode > 1) { std::cerr << "Wrong argument : " << anArg << std::endl; return 1; @@ -5097,7 +5091,7 @@ static Standard_Integer VPointCloud (Draw_Interpretor& /*theDI*/, else if (anArg.Search ("ColorName=") > -1) { aColorName = ViewerTest::GetColorFromName (anArg.Token ("=", 2).ToCString()); - aHasColor = false; + aHasColor = Standard_False; } else if (anArg.Search ("Scale=") > -1) { @@ -5107,37 +5101,30 @@ static Standard_Integer VPointCloud (Draw_Interpretor& /*theDI*/, { aNumberOfPoints = anArg.Token ("=", 2).IntegerValue(); } - else if (anArg.Search ("FileName=") > -1) - { - aFileName = anArg.Token ("=", 2); - } else { - std::cerr << "Wrong argument: " << anArg << "\n"; + std::cerr << "Wrong argument: " << anArg << std::endl; return 1; } } - srand (static_cast(time(NULL))); - // Point cloud initialization Handle(AIS_PointCloud) aPointCloud = new AIS_PointCloud(); - Standard_Integer anIter = 1; if (aMode == 0) { Handle(Graphic3d_ArrayOfPoints) anArrayPoints = new Graphic3d_ArrayOfPoints (aNumberOfPoints, aHasColor); - for (; anIter < aNumberOfPoints; anIter++) + for (Standard_Integer anIter = 1; anIter < aNumberOfPoints; anIter++) { - // Create random points - gp_Pnt aPoint (RandomReal (0., 10000.), - RandomReal (0., 10000.), - RandomReal (0., 10000.)); - - // Create random colors - Quantity_Color aColor (RandomReal (0., 1.), - RandomReal (0., 1.), - RandomReal (0., 1.), + // Create random point + gp_Pnt aPoint (randomReal (0., 5000.), + randomReal (0., 5000.), + randomReal (0., 5000.)); + + // Create random color + Quantity_Color aColor (randomReal (0., 1.), + randomReal (0., 1.), + randomReal (0., 1.), Quantity_TOC_RGB); // Add point with color in array @@ -5148,30 +5135,49 @@ static Standard_Integer VPointCloud (Draw_Interpretor& /*theDI*/, } else if (aMode == 1) { - Handle(AIS_ArrayOfPnt) aCoords = new AIS_ArrayOfPnt (1, aNumberOfPoints); - Handle(AIS_ArrayOfPnt) aColors = new AIS_ArrayOfPnt (1, aNumberOfPoints); - for (; anIter <= aNumberOfPoints; anIter++) + Handle(TColgp_HArray1OfPnt) aCoords = new TColgp_HArray1OfPnt (1, aNumberOfPoints); + Handle(Quantity_HArray1OfColor) aColors = new Quantity_HArray1OfColor (1, aNumberOfPoints); + + if (aCoords->Length() != aColors->Length()) + { + std::cerr << "Wrong length of arrays" << std::endl; + return 1; + } + + for (Standard_Integer aPntIt = aCoords->Lower(); aPntIt <= aCoords->Upper(); aPntIt++) + { + gp_Pnt aPoint (randomReal (0., 5000.), + randomReal (0., 5000.), + randomReal (0., 5000.)); + aCoords->SetValue (aPntIt, aPoint); + } + + if (aHasColor) + { + for (Standard_Integer aColorIt = aColors->Lower(); aColorIt <= aColors->Upper(); aColorIt++) + { + Quantity_Color aColor (randomReal (0., 1.), + randomReal (0., 1.), + randomReal (0., 1.), + Quantity_TOC_RGB); + aColors->SetValue (aColorIt, aColor); + } + } + else { - gp_Pnt aPoint (RandomReal (0., 5000.), - RandomReal (0., 5000.), - RandomReal (0., 5000.)); - aCoords->SetValue (anIter, aPoint); - - gp_Pnt aColor (RandomReal (0., 1.), - RandomReal (0., 1.), - RandomReal (0., 1.)); - aColors->SetValue (anIter, aColor); + aColors.Nullify(); } // Set coordinates and colors - aPointCloud->SetPoints (aCoords, aColors, aHasColor); + aPointCloud->SetPoints (aCoords, aColors); } // Set point aspect for attributes of interactive object - Aspect_TypeOfMarker anAMarkerType = aMarkerType >= 0 ? (Aspect_TypeOfMarker )aMarkerType : Aspect_TOM_POINT; - aPointCloud->Attributes()->SetPointAspect (new Prs3d_PointAspect (anAMarkerType, aColorName, aScale)); + aPointCloud->Attributes()->SetPointAspect ( + new Prs3d_PointAspect (aMarkerType >= 0 ? (Aspect_TypeOfMarker )aMarkerType : Aspect_TOM_POINT, + aColorName, + aScale)); - anAISContext->Display (aPointCloud); - GetMapOfAIS().Bind (aPointCloud, theArgs[1]); + VDisplayAISObject (aName, aPointCloud); return 0; } @@ -5338,8 +5344,7 @@ void ViewerTest::ObjectCommands(Draw_Interpretor& theCommands) "vpointcloud usage:\n" "vpointcloud ObjectName [Mode=1]\n" " [NumPoints=100]\n" - " [MarkerType=0] [ColorName=GREEN] [Scale=1.0]\n" - " [FileName=PointCloudFile]" + " [MarkerType=0] [ColorName=GREEN] [Scale=1.0]" "\n\t\t: Create an interactive object for arbitary set of points.", __FILE__, VPointCloud, group); } -- 2.39.5