Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

levelset: allow to evaluate the complement of an object #406

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/levelset/bitpit_levelset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "levelSetSegmentationObject.hpp"
#include "levelSetSignedObject.hpp"
#include "levelSetBooleanObject.hpp"
#include "levelSetComplementObject.hpp"
#include "levelSetMaskObject.hpp"

#include "levelSet.hpp"
Expand Down
22 changes: 20 additions & 2 deletions src/levelset/levelSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ void LevelSet::setMesh( VolumeKernel* mesh ) {

}

/*!
* Adds the complement of the specified object.
* Objects can be added to the levelset only after setting the mesh.
* @param[in] sourceId id of source object
* @param[in] id id to be assigned to object. In case default value is passed the insertion order will be used as identifier
* @return identifier of new object
*/
int LevelSet::addObjectComplement( int sourceId, int id ) {

assert(m_kernel && " levelset: setMesh must be called before adding a mask object ");

LevelSetObject *sourceObject = m_objects.at(sourceId).get() ;

std::unique_ptr<LevelSetObject> object = LevelSetObjectFactory::createComplementObject( m_kernel.get(), m_storageType, id, sourceObject ) ;

return registerObject(std::move(object));
};

/*!
* Adds a segmentation object
* Objects can be added to the levelset only after setting the mesh.
Expand Down Expand Up @@ -259,7 +277,7 @@ int LevelSet::addObject( LevelSetBooleanOperation operation, const std::vector<i
*/
int LevelSet::addObject( const std::unordered_set<long> &list, int id ) {

assert(m_kernel && " levelset: setMesh must be called befor adding a mask object ");
assert(m_kernel && " levelset: setMesh must be called before adding a mask object ");

std::unique_ptr<LevelSetObject> object = LevelSetObjectFactory::createMaskObject<LevelSetMaskNarrowBandCache, int &, const std::unordered_set<long> &, const VolumeKernel &>( m_kernel.get(), m_storageType, id, list, *m_kernel->getMesh() ) ;

Expand All @@ -277,7 +295,7 @@ int LevelSet::addObject( const std::unordered_set<long> &list, int id ) {
*/
int LevelSet::addObject( const std::vector<long> &list, long refInterface, bool invert, int id ) {

assert(m_kernel && " levelset: setMesh must be called befor adding a mask object ");
assert(m_kernel && " levelset: setMesh must be called before adding a mask object ");

std::unique_ptr<LevelSetObject> object = LevelSetObjectFactory::createMaskObject<LevelSetMaskNarrowBandCache, int &, const std::vector<long> &, long &, bool &, const VolumeKernel &>( m_kernel.get(), m_storageType, id, list, refInterface, invert, *m_kernel->getMesh() ) ;

Expand Down
2 changes: 2 additions & 0 deletions src/levelset/levelSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class LevelSet{

void setMesh( VolumeKernel* ) ;

int addObjectComplement( int, int id=levelSetDefaults::OBJECT ) ;

int addObject( std::unique_ptr<SurfaceKernel> &&, double, int id = levelSetDefaults::OBJECT ) ;
int addObject( SurfaceKernel *, double, int id = levelSetDefaults::OBJECT ) ;
int addObject( std::unique_ptr<SurfUnstructured> &&, double, int id = levelSetDefaults::OBJECT ) ;
Expand Down
229 changes: 229 additions & 0 deletions src/levelset/levelSetComplementObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*---------------------------------------------------------------------------*\
*
* bitpit
*
* Copyright (C) 2015-2021 OPTIMAD engineering Srl
*
* -------------------------------------------------------------------------
* License
* This file is part of bitpit.
*
* bitpit is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License v3 (LGPL)
* as published by the Free Software Foundation.
*
* bitpit is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with bitpit. If not, see <http://www.gnu.org/licenses/>.
*
\*---------------------------------------------------------------------------*/

# include <cassert>

# include "bitpit_IO.hpp"
# include "bitpit_common.hpp"

# include "levelSetObject.hpp"
# include "levelSetProxyObject.hpp"
# include "levelSetComplementObject.hpp"

namespace bitpit {

/*!
* \class LevelSetComplementObject
* \ingroup levelset
*
* \brief Class that allows to evaluate the complement of a LevelSetObjects.
*/

/*!
* Constructor.
*
* \param[in] id identifier of object
* \param[in] source pointer to source object
*/
LevelSetComplementObject::LevelSetComplementObject(int id, const LevelSetObject *source)
: LevelSetProxyObject(id),
m_sourceObject(source)
{
}

/*!
* Copy constructor.
*
* Assigns same id to new object;
* \param[in] other object to be copied
*/
LevelSetComplementObject::LevelSetComplementObject(const LevelSetComplementObject &other)
: LevelSetProxyObject(other),
m_sourceObject(other.m_sourceObject)
{
}

/*!
* Returns LevelSetInfo.
*
* \param[in] id cell id
* \return LevelSetInfo
*/
LevelSetInfo LevelSetComplementObject::getLevelSetInfo(long id) const
{
return LevelSetInfo(getValue(id), getGradient(id));
}

/*!
* Get the levelset value.
*
* \param[in] id cell id
* \return levelset value in cell
*/
double LevelSetComplementObject::getLS(long id) const
{
return getValue(id);
}

/*!
* Get the levelset value.
*
* \param[in] id cell id
* \return levelset value in cell
*/
double LevelSetComplementObject::getValue(long id) const
{
return (- m_sourceObject->getValue(id));
}

/*!
* Get the levelset gradient.
*
* \param[in] id cell id
* \return levelset gradient in cell
*/
std::array<double,3> LevelSetComplementObject::getGradient(long id) const
{
return (- 1. * m_sourceObject->getGradient(id));
}

/*!
* Computes the LevelSetInfo in a point.
*
* \param[in] coords point coordinates
* \return LevelSetInfo
*/
LevelSetInfo LevelSetComplementObject::computeLevelSetInfo(const std::array<double,3> &coords) const
{
LevelSetInfo levelSetInfo = m_sourceObject->computeLevelSetInfo(coords);
levelSetInfo.value *= -1.;
levelSetInfo.gradient *= -1.;

return levelSetInfo;
}

/*!
* Replace a source object.
*
* \param[in] current current source object
* \param[in] updated updated source object
*/
void LevelSetComplementObject::replaceSourceObject(const LevelSetObject *current, const LevelSetObject *updated)
{
if (current != m_sourceObject) {
throw std::runtime_error("Unable to find the source that should be replaced.");
}

m_sourceObject = updated;
}

/*!
* Clones the object.
*
* \return pointer to cloned object
*/
LevelSetComplementObject* LevelSetComplementObject::clone() const
{
return new LevelSetComplementObject(*this);
}

/*!
* Gets the surface normal at the projection point.
*
* \param[in] id cell index
* \return closest part
*/
std::array<double,3> LevelSetComplementObject::getNormal(long id) const
{
return (-1. * m_sourceObject->getNormal(id));
}

/*!
* Gets the closest part index.
*
* \param[in] id cell index
* \return closest part
*/
int LevelSetComplementObject::getPart(long id) const
{
return m_sourceObject->getPart(id);
}

/*!
* Get surface feature size.
*
* \param[in] id cell index
* \return characteristic size
*/
double LevelSetComplementObject::getSurfaceFeatureSize(long id) const
{
return m_sourceObject->getSurfaceFeatureSize(id);
}

/*!
* Get the smallest surface feature size.
*
* \return characteristic size
*/
double LevelSetComplementObject::getMinSurfaceFeatureSize() const
{
return m_sourceObject->getMinSurfaceFeatureSize();
}

/*!
* Get the largest surface feature size.
*
* \return characteristic size
*/
double LevelSetComplementObject::getMaxSurfaceFeatureSize() const
{
return m_sourceObject->getMaxSurfaceFeatureSize();
}

/*!
* Get the object that defines the levelset information for the specified cell.
*
* \param[in] id cell index
* \return The object that defines the levelset information for the specified
* cell.
*/
const LevelSetObject * LevelSetComplementObject::getReferenceObject(long id) const
{
BITPIT_UNUSED(id);

return m_sourceObject;
}

/*!
* Get all objects that compose the boolean object.
*
* \return pointers to all primary objects involved in the definition of the
* boolean object levelset information.
*/
std::vector<const LevelSetObject *> LevelSetComplementObject::getSourceObjects() const
{
return std::vector<const LevelSetObject *>(1, m_sourceObject);
}

}
74 changes: 74 additions & 0 deletions src/levelset/levelSetComplementObject.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*---------------------------------------------------------------------------*\
*
* bitpit
*
* Copyright (C) 2015-2021 OPTIMAD engineering Srl
*
* -------------------------------------------------------------------------
* License
* This file is part of bitpit.
*
* bitpit is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License v3 (LGPL)
* as published by the Free Software Foundation.
*
* bitpit is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with bitpit. If not, see <http://www.gnu.org/licenses/>.
*
\*---------------------------------------------------------------------------*/

# ifndef __BITPIT_LEVELSET_COMPLEMENT_OBJECT_HPP__
# define __BITPIT_LEVELSET_COMPLEMENT_OBJECT_HPP__

# include <array>

# include "levelSetCommon.hpp"
# include "levelSetProxyObject.hpp"

namespace bitpit{

class LevelSetObject ;
class LevelSetProxyObject ;

class LevelSetComplementObject: public LevelSetProxyObject {

private:
const LevelSetObject* m_sourceObject; /**< Pointers to source object */

protected:
void replaceSourceObject(const LevelSetObject *current, const LevelSetObject *updated) override ;

public:
LevelSetComplementObject(int id, const LevelSetObject*);
LevelSetComplementObject(const LevelSetComplementObject &);

LevelSetComplementObject* clone() const override;

LevelSetInfo getLevelSetInfo(long ) const override;
double getLS(long ) const override;
double getValue(long ) const override;
std::array<double,3> getGradient(long ) const override;

std::array<double,3> getNormal(long ) const override;
int getPart(long ) const override;
double getSurfaceFeatureSize(long ) const override;

LevelSetInfo computeLevelSetInfo(const std::array<double,3> &) const override;

double getMinSurfaceFeatureSize() const override;
double getMaxSurfaceFeatureSize() const override;

const LevelSetObject * getReferenceObject( long ) const override;

std::vector<const LevelSetObject *> getSourceObjects() const override;

};

}

#endif
4 changes: 2 additions & 2 deletions src/levelset/levelSetImmutableObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#define __BITPIT_LEVELSET_IMMUTABLE_OBJECT_HPP__

#include "levelSetObject.hpp"
#include "levelSetBooleanObject.hpp"
#include "levelSetCachedObject.hpp"
#include "levelSetSignedObject.hpp"

Expand All @@ -47,8 +46,9 @@ class LevelSetImmutableObject : public LevelSetObject, public LevelSetImmutableO
{

public:
template<typename object_t>
LevelSetImmutableObject(object_t *object);
LevelSetImmutableObject(LevelSetCachedObject<narrow_band_cache_t> *object);
LevelSetImmutableObject(LevelSetBooleanObject *object);

LevelSetImmutableObject * clone() const override;

Expand Down
Loading