From c0feb56521461596720cd5145b80fc0030a71710 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 8 Sep 2021 18:49:23 +0200
Subject: [PATCH] ENH: optional face area normals output for VTK surface format

- similar to additional flag for the raw surface writer (#2003)

  Example,
  ```
  formatOptions
  {
      vtk
      {
          normal      yes;
      }
  }
---
 src/surfMesh/writers/vtk/vtkSurfaceWriter.C | 28 ++++++++++++++++++++-
 src/surfMesh/writers/vtk/vtkSurfaceWriter.H |  8 ++++--
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/src/surfMesh/writers/vtk/vtkSurfaceWriter.C b/src/surfMesh/writers/vtk/vtkSurfaceWriter.C
index 016351e6212..ffbf0b4ff14 100644
--- a/src/surfMesh/writers/vtk/vtkSurfaceWriter.C
+++ b/src/surfMesh/writers/vtk/vtkSurfaceWriter.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2019-2020 OpenCFD Ltd.
+    Copyright (C) 2019-2021 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -66,6 +66,7 @@ Foam::surfaceWriters::vtkWriter::vtkWriter()
     surfaceWriter(),
     fmtType_(static_cast<unsigned>(vtk::formatType::INLINE_BASE64)),
     precision_(IOstream::defaultPrecision()),
+    writeNormal_(false),
     fieldScale_(),
     writer_(nullptr)
 {}
@@ -79,6 +80,7 @@ Foam::surfaceWriters::vtkWriter::vtkWriter
     surfaceWriter(),
     fmtType_(static_cast<unsigned>(opts.fmt())),
     precision_(opts.precision()),
+    writeNormal_(false),
     fieldScale_(),
     writer_(nullptr)
 {}
@@ -95,6 +97,7 @@ Foam::surfaceWriters::vtkWriter::vtkWriter
     (
         options.getOrDefault("precision", IOstream::defaultPrecision())
     ),
+    writeNormal_(options.getOrDefault("normal", false)),
     fieldScale_(options.subOrEmptyDict("fieldScale")),
     writer_(nullptr)
 {
@@ -241,6 +244,29 @@ Foam::fileName Foam::surfaceWriters::vtkWriter::write()
         }
 
         writer_->writeGeometry();
+
+        if (writeNormal_)
+        {
+            const faceList& fcs = surf.faces();
+            const pointField& pts = surf.points();
+
+            Field<vector> normals(fcs.size());
+            forAll(fcs, facei)
+            {
+                normals[facei] = fcs[facei].areaNormal(pts);
+            }
+
+            label nCellData = 1;
+
+            if (!this->isPointData())
+            {
+                // Ill-defined with legacy() if nFields_ not properly set...
+                nCellData += nFields_;
+            }
+
+            writer_->beginCellData(nCellData);
+            writer_->write("area-normal", normals);
+        }
     }
 
     wroteGeom_ = true;
diff --git a/src/surfMesh/writers/vtk/vtkSurfaceWriter.H b/src/surfMesh/writers/vtk/vtkSurfaceWriter.H
index 0ae0972fd22..36d763c7f27 100644
--- a/src/surfMesh/writers/vtk/vtkSurfaceWriter.H
+++ b/src/surfMesh/writers/vtk/vtkSurfaceWriter.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011 OpenFOAM Foundation
-    Copyright (C) 2015-2020 OpenCFD Ltd.
+    Copyright (C) 2015-2021 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -36,7 +36,8 @@ Description
         format      | ascii or binary format                | no  | binary
         legacy      | Legacy VTK output                     | no  | false
         precision   | Write precision in ascii         | no | same as IOstream
-        fieldScale  | output field scaling (dictionary)     | no    | empty
+        fieldScale  | Output field scaling (dictionary)     | no  | empty
+        normal      | Write face area-normal in output      | no  | false
     \endtable
 
     For example,
@@ -110,6 +111,9 @@ class vtkWriter
         //- ASCII write precision
         unsigned precision_;
 
+        //- Output face area normal
+        const bool writeNormal_;
+
         //- Output field scaling
         const dictionary fieldScale_;
 
-- 
GitLab