#12460 Python: Add triangulated surface created from Python

Add TriangulatedSurface with set_geometry, set_property, get_property
and available_properties, created by
SurfaceCollection.new_triangulated_surface. When the project is saved,
the surface is written as a GOCAD TSurf file next to the project file
and read back from that file when the project is opened.
This commit is contained in:
Stefan Crooijmans authored and Kristian Bendiksen committed 2026-11-09 19:48:06 +01:00
1 parent 70a47d2ac0
commit 31f79f426f
12 files changed
+920 -1

No files matched your search

@@ -38,6 +38,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RimcDataFilterCollection.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcCombinedFilter.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcRegularSurface.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcTriangulatedSurface.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcPerforationInterval.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcValveTemplateCollection.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcWellPathCompletionSettings.cpp
@@ -27,6 +27,7 @@
#include "RimRegularSurface.h"
#include "RimSurface.h"
#include "RimSurfaceCollection.h"
#include "RimTriangulatedSurface.h"
#include "cafPdmFieldScriptingCapability.h"
#include "cafPdmObjectScriptingCapability.h"
@@ -37,6 +38,7 @@
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimSurfaceCollection, RimcSurfaceCollection_importSurface, "ImportSurface" );
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimSurfaceCollection, RimcSurfaceCollection_newSurface, "NewSurface" );
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimSurfaceCollection, RimcSurfaceCollection_newRegularSurface, "NewRegularSurface" );
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimSurfaceCollection, RimcSurfaceCollection_newTriangulatedSurface, "NewTriangulatedSurface" );
//--------------------------------------------------------------------------------------------------
///
@@ -214,3 +216,58 @@ QString RimcSurfaceCollection_newRegularSurface::classKeywordReturnedType() cons
{
return RimRegularSurface::classKeywordStatic();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimcSurfaceCollection_newTriangulatedSurface::RimcSurfaceCollection_newTriangulatedSurface( caf::PdmObjectHandle* self )
: PdmObjectCreationMethod( self )
{
CAF_PDM_InitObject( "New Triangulated Surface", "", "", "Create a new triangulated surface" );
CAF_PDM_InitScriptableField( &m_name, "Name", QString( "" ), "Name" );
CAF_PDM_InitScriptableField( &m_onNameConflict,
"OnNameConflict",
caf::AppEnum<RiaDefines::NameConflictPolicy>( RiaDefines::NameConflictPolicy::FAIL ),
"",
"",
"",
"How to handle a surface name already used in this folder" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<caf::PdmObjectHandle*, QString> RimcSurfaceCollection_newTriangulatedSurface::execute()
{
RimSurfaceCollection* coll = self<RimSurfaceCollection>();
if ( !coll ) return std::unexpected( "No surface collection found" );
auto resolution = RiaNameUniquenessTools::applyConflictPolicy( &coll->itemsField(), m_name(), m_onNameConflict().value() );
if ( !resolution.errorMessage.isEmpty() ) return std::unexpected( resolution.errorMessage );
if ( auto* existingSurface = dynamic_cast<RimSurface*>( resolution.objectToReplace ) )
{
coll->deleteItem( existingSurface );
}
RimTriangulatedSurface* surface = new RimTriangulatedSurface;
surface->setUserDescription( resolution.nameToUse );
surface->setColor( cvf::Color3f::BLUE );
surface->setOpacity( true, 0.6f );
coll->addSurface( surface );
coll->updateViews();
coll->updateConnectedEditors();
return surface;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimcSurfaceCollection_newTriangulatedSurface::classKeywordReturnedType() const
{
return RimTriangulatedSurface::classKeywordStatic();
}
@@ -97,3 +97,21 @@ private:
caf::PdmField<double> m_incrementY;
caf::PdmField<double> m_rotation;
};
//==================================================================================================
///
//==================================================================================================
class RimcSurfaceCollection_newTriangulatedSurface : public caf::PdmObjectCreationMethod
{
CAF_PDM_HEADER_INIT;
public:
RimcSurfaceCollection_newTriangulatedSurface( caf::PdmObjectHandle* self );
std::expected<caf::PdmObjectHandle*, QString> execute() override;
QString classKeywordReturnedType() const override;
private:
caf::PdmField<QString> m_name;
caf::PdmField<caf::AppEnum<RiaDefines::NameConflictPolicy>> m_onNameConflict;
};
@@ -0,0 +1,206 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight 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 General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimcTriangulatedSurface.h"
#include "KeyValueStore/RiaKeyValueStoreUtil.h"
#include "RiaApplication.h"
#include "RimSurfaceCollection.h"
#include "RimTriangulatedSurface.h"
#include "RimcDataContainerString.h"
#include "cafPdmFieldScriptingCapability.h"
#include "cafPdmObjectScriptingCapability.h"
#include <cmath>
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimTriangulatedSurface, RimcTriangulatedSurface_setGeometryFromKeys, "SetGeometryFromKeys" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimcTriangulatedSurface_setGeometryFromKeys::RimcTriangulatedSurface_setGeometryFromKeys( caf::PdmObjectHandle* self )
: caf::PdmVoidObjectMethod( self )
{
CAF_PDM_InitObject( "Set geometry from keys", "", "", "Set geometry from keys." );
CAF_PDM_InitScriptableFieldNoDefault( &m_verticesKey, "VerticesKey", "", "", "", "Key for x, y and depth of each vertex" );
CAF_PDM_InitScriptableFieldNoDefault( &m_trianglesKey, "TrianglesKey", "", "", "", "Key for three vertex indices per triangle" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<caf::PdmObjectHandle*, QString> RimcTriangulatedSurface_setGeometryFromKeys::execute()
{
RimTriangulatedSurface* surface = self<RimTriangulatedSurface>();
if ( !surface ) return std::unexpected( "No surface found" );
auto keyValueStore = RiaApplication::instance()->keyValueStore();
std::vector<float> vertexValues = RiaKeyValueStoreUtil::convertToFloatVector( keyValueStore->get( m_verticesKey().toStdString() ) );
std::vector<float> triangleValues = RiaKeyValueStoreUtil::convertToFloatVector( keyValueStore->get( m_trianglesKey().toStdString() ) );
keyValueStore->remove( m_verticesKey().toStdString() );
keyValueStore->remove( m_trianglesKey().toStdString() );
if ( vertexValues.empty() || vertexValues.size() % 3 != 0 )
return std::unexpected( "Invalid vertices. Must be a non-empty list of x, y and depth values." );
if ( triangleValues.empty() || triangleValues.size() % 3 != 0 )
return std::unexpected( "Invalid triangles. Must be a non-empty list of three vertex indices per triangle." );
std::vector<cvf::Vec3d> vertices;
for ( size_t i = 0; i < vertexValues.size(); i += 3 )
{
// Depth is positive downwards
vertices.emplace_back( vertexValues[i], vertexValues[i + 1], -vertexValues[i + 2] );
}
// The key-value store holds floats, so the indices arrive as floats
std::vector<unsigned> triangleIndices;
for ( float value : triangleValues )
{
if ( value < 0.0f || value >= static_cast<float>( vertices.size() ) || std::floor( value ) != value )
return std::unexpected( QString( "Invalid triangle index %1. Valid range: [0-%2]" )
.arg( static_cast<double>( value ) )
.arg( static_cast<int>( vertices.size() ) - 1 ) );
triangleIndices.push_back( static_cast<unsigned>( value ) );
}
surface->setGeometry( vertices, triangleIndices );
surface->onLoadData();
if ( auto coll = surface->firstAncestorOrThisOfType<RimSurfaceCollection>() )
{
coll->updateUiIconFromToggleField();
coll->updateViews();
}
return nullptr;
}
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimTriangulatedSurface, RimcTriangulatedSurface_setPropertyFromKey, "SetPropertyFromKey" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimcTriangulatedSurface_setPropertyFromKey::RimcTriangulatedSurface_setPropertyFromKey( caf::PdmObjectHandle* self )
: caf::PdmVoidObjectMethod( self )
{
CAF_PDM_InitObject( "Set property from key", "", "", "Set property from key." );
CAF_PDM_InitScriptableFieldNoDefault( &m_name, "Name", "", "", "", "Name" );
CAF_PDM_InitScriptableFieldNoDefault( &m_valueKey, "ValueKey", "", "", "", "Key Value" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<caf::PdmObjectHandle*, QString> RimcTriangulatedSurface_setPropertyFromKey::execute()
{
RimTriangulatedSurface* surface = self<RimTriangulatedSurface>();
if ( !surface ) return std::unexpected( "No surface found" );
auto keyValueStore = RiaApplication::instance()->keyValueStore();
std::vector<float> values = RiaKeyValueStoreUtil::convertToFloatVector( keyValueStore->get( m_valueKey().toStdString() ) );
keyValueStore->remove( m_valueKey().toStdString() );
if ( values.empty() ) return std::unexpected( "Found unexpected empty property." );
if ( values.size() != surface->vertexCount() )
return std::unexpected( "Failed to set property: incorrect dimensions. Must be one value per vertex." );
surface->setProperty( m_name, values );
surface->onLoadData();
if ( auto coll = surface->firstAncestorOrThisOfType<RimSurfaceCollection>() )
{
coll->updateUiIconFromToggleField();
coll->updateViews();
}
return nullptr;
}
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimTriangulatedSurface, RimcTriangulatedSurface_getPropertyToKey, "GetPropertyToKey" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimcTriangulatedSurface_getPropertyToKey::RimcTriangulatedSurface_getPropertyToKey( caf::PdmObjectHandle* self )
: caf::PdmVoidObjectMethod( self )
{
CAF_PDM_InitObject( "Get property to key", "", "", "Get property to key." );
CAF_PDM_InitScriptableFieldNoDefault( &m_name, "Name", "", "", "", "Name" );
CAF_PDM_InitScriptableFieldNoDefault( &m_valueKey, "ValueKey", "", "", "", "Key Value" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<caf::PdmObjectHandle*, QString> RimcTriangulatedSurface_getPropertyToKey::execute()
{
RimTriangulatedSurface* surface = self<RimTriangulatedSurface>();
if ( !surface ) return std::unexpected( "No surface found" );
std::vector<float> values = surface->getProperty( m_name );
if ( values.empty() ) return std::unexpected( QString( "Property '%1' not found." ).arg( m_name() ) );
auto keyValueStore = RiaApplication::instance()->keyValueStore();
keyValueStore->set( m_valueKey().toStdString(), RiaKeyValueStoreUtil::convertToByteVector( values ) );
return nullptr;
}
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimTriangulatedSurface, RimcTriangulatedSurface_propertyNames, "PropertyNames" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimcTriangulatedSurface_propertyNames::RimcTriangulatedSurface_propertyNames( caf::PdmObjectHandle* self )
: PdmObjectMethod( self, PdmObjectMethod::NullPointerType::NULL_IS_INVALID, PdmObjectMethod::ResultType::PERSISTENT_FALSE )
{
CAF_PDM_InitObject( "Property Names", "", "", "Property Names." );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<caf::PdmObjectHandle*, QString> RimcTriangulatedSurface_propertyNames::execute()
{
RimTriangulatedSurface* surface = self<RimTriangulatedSurface>();
if ( !surface ) return std::unexpected( "No surface found" );
auto dataObject = new RimcDataContainerString();
dataObject->m_stringValues = surface->propertyNames();
return dataObject;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimcTriangulatedSurface_propertyNames::classKeywordReturnedType() const
{
return RimcDataContainerString::classKeywordStatic();
}
@@ -0,0 +1,90 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight 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 General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cafPdmField.h"
#include "cafPdmObjectHandle.h"
#include "cafPdmObjectMethod.h"
#include <QString>
//==================================================================================================
///
//==================================================================================================
class RimcTriangulatedSurface_setGeometryFromKeys : public caf::PdmVoidObjectMethod
{
CAF_PDM_HEADER_INIT;
public:
RimcTriangulatedSurface_setGeometryFromKeys( caf::PdmObjectHandle* self );
std::expected<caf::PdmObjectHandle*, QString> execute() override;
private:
caf::PdmField<QString> m_verticesKey;
caf::PdmField<QString> m_trianglesKey;
};
//==================================================================================================
///
//==================================================================================================
class RimcTriangulatedSurface_setPropertyFromKey : public caf::PdmVoidObjectMethod
{
CAF_PDM_HEADER_INIT;
public:
RimcTriangulatedSurface_setPropertyFromKey( caf::PdmObjectHandle* self );
std::expected<caf::PdmObjectHandle*, QString> execute() override;
private:
caf::PdmField<QString> m_name;
caf::PdmField<QString> m_valueKey;
};
//==================================================================================================
///
//==================================================================================================
class RimcTriangulatedSurface_getPropertyToKey : public caf::PdmVoidObjectMethod
{
CAF_PDM_HEADER_INIT;
public:
RimcTriangulatedSurface_getPropertyToKey( caf::PdmObjectHandle* self );
std::expected<caf::PdmObjectHandle*, QString> execute() override;
private:
caf::PdmField<QString> m_name;
caf::PdmField<QString> m_valueKey;
};
//==================================================================================================
///
//==================================================================================================
class RimcTriangulatedSurface_propertyNames : public caf::PdmObjectMethod
{
CAF_PDM_HEADER_INIT;
public:
RimcTriangulatedSurface_propertyNames( caf::PdmObjectHandle* self );
std::expected<caf::PdmObjectHandle*, QString> execute() override;
QString classKeywordReturnedType() const override;
};