From 2a61606251b86f4fa5ba615bf585fcbc104ac26d Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 27 Jan 2022 18:07:06 +0100 Subject: [PATCH] ENH: improve gltf handling - scene - write with fileName, additional getMesh accessor - addColourToMesh accepts an alpha field size 1 as a constant alpha value - sceneWriter wrapper ENH: improve gltf handling of colour and alpha specification - accept plain input directly. Eg, colour (1 0 1); vs colour uniform; colourValue (1 0 1); - use field magnitude for colouring of non-scalar fields. Eg, having three different colour maps for a vector field simply does not help much with visualisation. --- src/fileFormats/Make/files | 1 + src/fileFormats/colours/colourTable.H | 2 +- src/fileFormats/gltf/foamGltfFwd.H | 54 ++ .../gltf/foamGltfObjectTemplates.C | 25 +- src/fileFormats/gltf/foamGltfScene.C | 79 ++- src/fileFormats/gltf/foamGltfScene.H | 40 +- src/fileFormats/gltf/foamGltfSceneTemplates.C | 18 +- src/fileFormats/gltf/foamGltfSceneWriter.C | 106 ++++ src/fileFormats/gltf/foamGltfSceneWriter.H | 126 +++++ .../writers/gltf/gltfCoordSetWriter.C | 462 +++++++++++------- .../coordSet/writers/gltf/gltfSetWriter.H | 42 +- 11 files changed, 685 insertions(+), 270 deletions(-) create mode 100644 src/fileFormats/gltf/foamGltfFwd.H create mode 100644 src/fileFormats/gltf/foamGltfSceneWriter.C create mode 100644 src/fileFormats/gltf/foamGltfSceneWriter.H diff --git a/src/fileFormats/Make/files b/src/fileFormats/Make/files index e997a6cefb..6b1d5018c2 100644 --- a/src/fileFormats/Make/files +++ b/src/fileFormats/Make/files @@ -36,6 +36,7 @@ gltf/foamGltfBufferView.C gltf/foamGltfMesh.C gltf/foamGltfObject.C gltf/foamGltfScene.C +gltf/foamGltfSceneWriter.C starcd/STARCDCore.C stl/STLCore.C diff --git a/src/fileFormats/colours/colourTable.H b/src/fileFormats/colours/colourTable.H index 0225e2c433..9356f4c95d 100644 --- a/src/fileFormats/colours/colourTable.H +++ b/src/fileFormats/colours/colourTable.H @@ -178,7 +178,7 @@ public: //- Predefined tables static const HashPtrTable& tables(); - //- Return the colour at x (within 0-1 range) + //- Return the colour at x. The input is clipped to 0-1 range. vector value(const scalar x) const; //- Return a discrete lookup table of colours diff --git a/src/fileFormats/gltf/foamGltfFwd.H b/src/fileFormats/gltf/foamGltfFwd.H new file mode 100644 index 0000000000..4b2d17b7fd --- /dev/null +++ b/src/fileFormats/gltf/foamGltfFwd.H @@ -0,0 +1,54 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2022 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM 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. + + OpenFOAM 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 + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + Forward declarations for exposed glTF interfaces + +\*---------------------------------------------------------------------------*/ + +#ifndef Foam_gltf_Forward_Declarations_H +#define Foam_gltf_Forward_Declarations_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace glTF +{ + +// Forward Declarations +class scene; +class sceneWriter; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace glTF +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/fileFormats/gltf/foamGltfObjectTemplates.C b/src/fileFormats/gltf/foamGltfObjectTemplates.C index df8247e5ca..14c3e5d74e 100644 --- a/src/fileFormats/gltf/foamGltfObjectTemplates.C +++ b/src/fileFormats/gltf/foamGltfObjectTemplates.C @@ -28,15 +28,14 @@ License template void Foam::glTF::object::addData(const Type& fld) { - const label nComponents = - pTraits::nComponents; + const direction nCmpts = pTraits::nComponents; label count = data_.size(); - data_.setSize(data_.size() + fld.size()*nComponents); + data_.resize(data_.size() + fld.size()*nCmpts); forAll(fld, fieldi) { - for (direction d = 0; d < nComponents; ++d) + for (direction d = 0; d < nCmpts; ++d) { data_[count++] = component(fld[fieldi], d); } @@ -45,7 +44,7 @@ void Foam::glTF::object::addData(const Type& fld) template -void Foam::glTF::object::addData(const Type1& fld1, const Type2&fld2) +void Foam::glTF::object::addData(const Type1& fld1, const Type2& fld2) { if (fld1.size() != fld2.size()) { @@ -55,26 +54,20 @@ void Foam::glTF::object::addData(const Type1& fld1, const Type2&fld2) << abort(FatalError); } - const label nComponents1 = - pTraits::nComponents; - - const label nComponents2 = - pTraits::nComponents; + const direction nCmpts1 = pTraits::nComponents; + const direction nCmpts2 = pTraits::nComponents; label count = data_.size(); - data_.setSize - ( - data_.size() + fld1.size()*(nComponents1 + nComponents2) - ); + data_.resize(data_.size() + fld1.size()*(nCmpts1 + nCmpts2)); forAll(fld1, fieldi) { - for (direction d = 0; d < nComponents1; ++d) + for (direction d = 0; d < nCmpts1; ++d) { data_[count++] = component(fld1[fieldi], d); } - for (direction d = 0; d < nComponents2; ++d) + for (direction d = 0; d < nCmpts2; ++d) { data_[count++] = component(fld2[fieldi], d); } diff --git a/src/fileFormats/gltf/foamGltfScene.C b/src/fileFormats/gltf/foamGltfScene.C index ba003ce3a3..ad4cae8d8f 100644 --- a/src/fileFormats/gltf/foamGltfScene.C +++ b/src/fileFormats/gltf/foamGltfScene.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2021 OpenCFD Ltd. + Copyright (C) 2021-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,7 +26,8 @@ License \*---------------------------------------------------------------------------*/ #include "foamGltfScene.H" -#include "fileName.H" +#include "OFstream.H" +#include "OSspecific.H" // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -43,6 +44,26 @@ Foam::glTF::scene::scene() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +Foam::glTF::mesh& Foam::glTF::scene::getMesh(label meshi) +{ + const label lastMeshi = (meshes_.size() - 1); + + if (meshi < 0) + { + meshi = (lastMeshi < 0 ? static_cast