/
spivag
/
command_occ
Обзор
Документация
Войти
/
spivag
/
command_occ
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
initial
commands/opengl_mesh_factory.cpp
200 строк
6 KB
Степанов Никита
Запрос на слияние 'topology_iterators' (
#3
) из topology_iterators в initial
27 апр 2026, 21:55
Верифицирован
27 апр 2026, 21:55
c047a95
Код
Авторство
О чём код?
/** * @file opengl_mesh_factory.cpp * @brief Реализация фабрики для создания OpenGL сеток на основе OpenCASCADE * @copyright (c) Alexander Spivakov 2025 */ #include "opengl_mesh_factory.h" #include <shape_command.h> #include <shape_iterator.h> #include <topology.h> #include <cmath> static constexpr double LinearDeflection = 0.1; static constexpr double AngularDeflection = 0.5; static void AppendFaceMesh( FaceMeshData const& mesh, AttributeType attributeType, GL1Grid& grid ) { for ( auto const& tri : mesh.triangles ) { std::array< GLPoint, 3 > triangle; for ( int j = 0; j < 3; ++j ) { triangle[j].xyz = tri[j].xyz; triangle[j].normal = tri[j].normal; if ( attributeType == AttributeType::GaussCurvature && tri[j].gaussCurvature.has_value() ) { triangle[j].attribute = tri[j].gaussCurvature; grid.attributeInfo.Update( *tri[j].gaussCurvature ); } } grid.triangles.push_back( triangle ); } if ( attributeType == AttributeType::GaussCurvature ) { grid.attributeInfo.type = attributeType; grid.attributeInfo.name = "Gauss Curvature"; } } static void AppendFaceIsolines( FaceIsolineData const& isolines, GL1Grid& grid ) { for ( auto const& line : isolines.lines ) { GL1Polygon poly; for ( auto const& pt : line ) { GLPoint glpt; glpt.xyz = pt; poly.points.push_back( glpt ); } grid.gridIsolines.push_back( std::move( poly ) ); } } static void AppendEdgePolyline( EdgePolylineData const& poly, GL1RawCache& cache ) { GL1Polygon glpoly; for ( auto const& pt : poly.points ) { GLPoint glpt; glpt.xyz = pt; glpoly.points.push_back( glpt ); } cache.polygons.push_back( std::move( glpoly ) ); } //======================================================================================== // OpenGLMeshFactory //======================================================================================== bool OpenGLMeshFactory::Create( std::shared_ptr< IBrepFaceHandle > const& faceHandle, GridRenderStyle renderStyle, GL1Grid& gridAppendTo, AttributeType attributeType ) const { if ( !faceHandle ) return false; auto face = faceHandle->GetFace(); if ( !face || face->IsNull() ) return false; if ( renderStyle == GridRenderStyle::triangles ) { face->Tessellate( LinearDeflection, AngularDeflection ); FaceMeshData mesh = face->GetTriangulation( attributeType == AttributeType::GaussCurvature ); if ( mesh.triangles.empty() ) return false; AppendFaceMesh( mesh, attributeType, gridAppendTo ); return !gridAppendTo.triangles.empty(); } if ( renderStyle == GridRenderStyle::isolines ) { FaceIsolineData isolines = face->GetIsolines( 5, 20 ); AppendFaceIsolines( isolines, gridAppendTo ); return !gridAppendTo.gridIsolines.empty(); } return false; } //---------------------------------------------------------------------------------------- bool OpenGLMeshFactory::Create( std::shared_ptr< IBrepEdgeHandle > const& edgeHandle, GridRenderStyle renderStyle, GL1RawCache& cacheAppendTo ) const { if ( !edgeHandle ) return false; auto edge = edgeHandle->GetEdge(); if ( !edge || edge->IsNull() ) return false; EdgePolylineData poly = edge->Discretize( LinearDeflection, AngularDeflection ); if ( poly.points.empty() ) return false; AppendEdgePolyline( poly, cacheAppendTo ); return true; } //---------------------------------------------------------------------------------------- bool OpenGLMeshFactory::Create( std::shared_ptr< IShapeBuilder > const& shapeBuilder, GridRenderStyle renderStyle, GL1RawCache& cacheAppendTo, AttributeType attributeType ) const { if ( !shapeBuilder ) return false; shapeBuilder->Execute(); auto shape = shapeBuilder->GetShape(); if ( !shape || shape->IsNull() ) return false; if ( renderStyle == GridRenderStyle::triangles ) { shape->Tessellate( LinearDeflection, AngularDeflection ); bool hasAny = false; auto faceIt = CreateFaceIterator( shapeBuilder ); while ( faceIt->HasNext() ) { faceIt->Next(); auto faceHandle = faceIt->FaceHandle(); FaceMeshData mesh = faceHandle->GetFace()->GetTriangulation( attributeType == AttributeType::GaussCurvature ); if ( mesh.triangles.empty() ) continue; GL1Grid grid; AppendFaceMesh( mesh, attributeType, grid ); cacheAppendTo.grids.push_back( std::move( grid ) ); hasAny = true; } return hasAny; } if ( renderStyle == GridRenderStyle::isolines ) { shape->Tessellate( LinearDeflection, AngularDeflection ); bool hasAny = false; auto faceIt = CreateFaceIterator( shapeBuilder ); while ( faceIt->HasNext() ) { faceIt->Next(); auto faceHandle = faceIt->FaceHandle(); FaceIsolineData isolines = faceHandle->GetFace()->GetIsolines( 5, 20 ); if ( isolines.lines.empty() ) continue; GL1Grid grid; AppendFaceIsolines( isolines, grid ); cacheAppendTo.grids.push_back( std::move( grid ) ); hasAny = true; } return hasAny; } if ( renderStyle == GridRenderStyle::edgePolygons ) { auto edgeIt = CreateEdgeIterator( shapeBuilder ); while ( edgeIt->HasNext() ) { edgeIt->Next(); auto edgeHandle = edgeIt->EdgeHandle(); EdgePolylineData poly = edgeHandle->GetEdge()->Discretize( LinearDeflection, AngularDeflection ); if ( poly.points.empty() ) continue; AppendEdgePolyline( poly, cacheAppendTo ); } return !cacheAppendTo.polygons.empty(); } return false; }