From dea735ad5b9c9ad06d172e48df52fc989e174dda Mon Sep 17 00:00:00 2001 From: Pablo Garzon Date: Thu, 16 Mar 2023 13:43:50 +0100 Subject: [PATCH] #3501 new box GlyphPolyDataNormals (Pablo) --- .../src/bbcreaVtkCreateMeshFromPoints.cxx | 128 +++++++++++++- .../src/bbcreaVtkCreateMeshFromPoints.h | 7 + .../src/bbcreaVtkGlyphPolyDataNormals.cxx | 160 ++++++++++++++++++ .../src/bbcreaVtkGlyphPolyDataNormals.h | 77 +++++++++ 4 files changed, 370 insertions(+), 2 deletions(-) create mode 100644 bbtk_creaVtk_PKG/src/bbcreaVtkGlyphPolyDataNormals.cxx create mode 100644 bbtk_creaVtk_PKG/src/bbcreaVtkGlyphPolyDataNormals.h diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx b/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx index 671b264..2c17fb7 100644 --- a/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx @@ -5,6 +5,7 @@ #include "bbcreaVtkPackage.h" #include "vtkTriangleStrip.h" +#include namespace bbcreaVtk { @@ -38,7 +39,7 @@ void CreateMeshFromPoints::Process() std::vector lstIndexs = bbGetInputLstIndexs(); if ( (lstIndexs.size()<=1) || (lstX.size()==0) || (lstX.size()!=lstY.size()) || (lstY.size()!=lstZ.size()) ) { - printf("Warnning! CreateMeshFromPoints::Process: List of points X Y Z and LstIndexes is not correct\n"); + printf("PG CreateMeshFromPoints::Process: List of points X Y Z and LstIndexes is not correct\n"); bbSetOutputOut(NULL); } else { int ii,sizeSegment1,sizeSegment2; @@ -82,7 +83,17 @@ void CreateMeshFromPoints::Process() } // for ii iGeneral=iGeneral+sizeSegment1; cells->InsertNextCell(triangleStrip); - } //for LstIndexs + } //for LstIndexs + + //false = Open + //true = Close + if(bbGetInputOpenClose()){ + CloseContourSides(lstIndexs, true); + } + else{ + CloseOpenContourSurface(lstIndexs); + } + // vtkPolyData *polydata = vtkPolyData::New(); if (polydata!=NULL) polydata->Delete(); polydata = vtkPolyData::New(); @@ -100,6 +111,118 @@ void CreateMeshFromPoints::Process() triangle->Update(); bbSetOutputOut( clean->GetOutput() ); }// if listXYZ size + printf("CreateMeshFromPoints::Process: End\n"); +} + +void CreateMeshFromPoints::CloseContourSides(std::vector lstIndexs, bool uPointOrder){ + int sizeLstIdexes = lstIndexs.size(); + int sizePoints = bbGetInputLstX().size(); + + int firstIndex, end, centroidId, numPointsFace; + int increment = uPointOrder?1:sizeLstIdexes; + double centroid[3], currentPoint[3] = {}; + + for(int facesIdx = 0; facesIdx < 2; facesIdx++){ + std::fill(std::begin(centroid), std::end(centroid), 0); + if(facesIdx == 0){ + firstIndex = 0; + numPointsFace = uPointOrder?lstIndexs[0]: sizeLstIdexes; + end = uPointOrder?firstIndex + numPointsFace:sizePoints - lstIndexs[sizeLstIdexes - 1] + 1; + } + else{ + firstIndex = uPointOrder?sizePoints - lstIndexs[sizeLstIdexes-1]:lstIndexs[0] - 1; + numPointsFace = uPointOrder?lstIndexs[sizeLstIdexes-1]:sizeLstIdexes; + end = uPointOrder?firstIndex + numPointsFace:sizePoints; + } + if(numPointsFace > 1){ + double lastPoint[3] = {}; + CalcCentroid(centroid, firstIndex, end, increment, numPointsFace); + centroidId = points->InsertNextPoint(centroid[0], centroid[1], centroid[2]); + vtkSmartPointer triangleStrip1 = vtkSmartPointer::New(); + triangleStrip1->GetPointIds()->SetNumberOfIds(numPointsFace*2+1); + if(facesIdx == 0){ + int initial = firstIndex; + int triangleIndex = 0; + for(int index = initial; index < end; index+=increment){ + triangleStrip1->GetPointIds()->SetId(triangleIndex,index); + if(index+increment >= end){ + triangleStrip1->GetPointIds()->SetId(triangleIndex+1,initial); + triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId); + } + else{ + triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId); + } + triangleIndex+=2; + } + cells->InsertNextCell(triangleStrip1); + } + else{ + int initial = firstIndex-1; + int triangleIndex = 0; + int triangleStripStart = end-1; + for(int index = triangleStripStart; index > initial ; index-=increment){ + triangleStrip1->GetPointIds()->SetId(triangleIndex,index); + if(index-increment <= initial){ + triangleStrip1->GetPointIds()->SetId(triangleIndex+1,triangleStripStart); + triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId); + } + else{ + triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId); + } + triangleIndex+=2; + } + cells->InsertNextCell(triangleStrip1); + } + } + } + +} + +void CreateMeshFromPoints::CloseOpenContourSurface(std::vector lstIndexs){ + int sizeLstIdexes = lstIndexs.size(); + int sizeLstX = bbGetInputLstX().size(); + bool linePointOrder = false; + + if(linePointOrder){ + CloseContourSides(lstIndexs, false); + CloseContourBottom(false); + } + else{ + CloseContourSides(lstIndexs, true); + CloseContourBottom(true); + } + +} + +void CreateMeshFromPoints::CalcCentroid(double(¢roid)[3], int start, int end, int increment, int numPoints){ + double currPoint[3] = {}; + for(int i = start; i < end; i+=increment){ + points->GetPoint(i, currPoint); + centroid[0] += currPoint[0]; + centroid[1] += currPoint[1]; + centroid[2] += currPoint[2]; + } + centroid[0] /= numPoints; + centroid[1] /= numPoints; + centroid[2] /= numPoints; +} + +void CreateMeshFromPoints::CloseContourBottom(bool uPointOrder){ + std::vector lstIndexs = bbGetInputLstIndexs(); + int sizeLstIdexes = lstIndexs.size(); + int sizeLstX = bbGetInputLstX().size(); + + vtkSmartPointer triangleStripBottom = vtkSmartPointer::New(); + triangleStripBottom->GetPointIds()->SetNumberOfIds(sizeLstIdexes*2); + int triangleIndex = 0, currentId = 0, nextId = 0; + for(int splineIndex = 0; splineIndex < sizeLstIdexes;splineIndex++){ + triangleStripBottom->GetPointIds()->SetId(triangleIndex, currentId); + nextId = uPointOrder?currentId + lstIndexs[splineIndex] - 1:sizeLstX - sizeLstIdexes + splineIndex; + triangleStripBottom->GetPointIds()->SetId(triangleIndex+1, nextId); + triangleIndex+=2; + currentId = uPointOrder?nextId + 1: splineIndex+1; + } + cells->InsertNextCell(triangleStripBottom); } //===== @@ -111,6 +234,7 @@ void CreateMeshFromPoints::bbUserSetDefaultValues() // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX // Here we initialize the input 'In' to 0 // bbSetInputIn(0); + bbSetInputOpenClose(false); points = NULL; cells = NULL; polydata = NULL; diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.h b/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.h index 94eda53..b103d3e 100644 --- a/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.h +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.h @@ -30,6 +30,7 @@ class bbcreaVtk_EXPORT CreateMeshFromPoints BBTK_DECLARE_INPUT(LstY,std::vector); BBTK_DECLARE_INPUT(LstZ,std::vector); BBTK_DECLARE_INPUT(LstIndexs,std::vector); + BBTK_DECLARE_INPUT(OpenClose, bool); BBTK_DECLARE_OUTPUT(Out,vtkPolyData*); BBTK_PROCESS(Process); void Process(); @@ -39,6 +40,11 @@ class bbcreaVtk_EXPORT CreateMeshFromPoints vtkPolyData *polydata; vtkCleanPolyData *clean; vtkTriangleFilter *triangle; + + void CalcCentroid(double(¢roid)[3], int start, int end, int increment, int numPoints); + void CloseContourBottom(bool uPointOrder); + void CloseContourSides(std::vector lstIndexs, bool uPointOrder); + void CloseOpenContourSurface(std::vector lstIndexs); //===== // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) @@ -55,6 +61,7 @@ BBTK_BEGIN_DESCRIBE_BLACK_BOX(CreateMeshFromPoints,bbtk::AtomicBlackBox); BBTK_INPUT(CreateMeshFromPoints,LstY,"List Y point",std::vector,""); BBTK_INPUT(CreateMeshFromPoints,LstZ,"List Z point",std::vector,""); BBTK_INPUT(CreateMeshFromPoints,LstIndexs,"Number of points by segment",std::vector,""); + BBTK_INPUT(CreateMeshFromPoints,OpenClose,"(default false) Type of Contour: false=Open, true=Close",bool,""); BBTK_OUTPUT(CreateMeshFromPoints,Out,"vtkPolyData",vtkPolyData*,""); diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkGlyphPolyDataNormals.cxx b/bbtk_creaVtk_PKG/src/bbcreaVtkGlyphPolyDataNormals.cxx new file mode 100644 index 0000000..1234236 --- /dev/null +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkGlyphPolyDataNormals.cxx @@ -0,0 +1,160 @@ +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +#include "bbcreaVtkGlyphPolyDataNormals.h" +#include "bbcreaVtkPackage.h" + +#include "vtkProperty.h" +#include + +namespace bbcreaVtk +{ + +BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,GlyphPolyDataNormals) +BBTK_BLACK_BOX_IMPLEMENTATION(GlyphPolyDataNormals,bbtk::AtomicBlackBox); +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void GlyphPolyDataNormals::Process() +{ + +// THE MAIN PROCESSING METHOD BODY +// Here we simply set the input 'In' value to the output 'Out' +// And print out the output value +// INPUT/OUTPUT ACCESSORS ARE OF THE FORM : +// void bbSet{Input|Output}NAME(const TYPE&) +// const TYPE& bbGet{Input|Output}NAME() const +// Where : +// * NAME is the name of the input/output +// (the one provided in the attribute 'name' of the tag 'input') +// * TYPE is the C++ type of the input/output +// (the one provided in the attribute 'type' of the tag 'input') + + + //Active bool Done + //Color vector Done + //Opacity 0-1 Done + //Mask Ratio 1-100 Done + //Type of Glyph 1-Arrow 2-Line + //size of glyph Done + + if(bbGetInputIn() == NULL){ + printf("Warnning! GlyphPolyDataNormals::Process: Missing input PolyData\n"); + bbSetOutputOut( NULL ); + } + else if(bbGetInputRenderer() == NULL){ + printf("Warnning! GlyphPolyDataNormals::Process: Missing Renderer\n"); + bbSetOutputOut( NULL ); + } + else{ + //Arrow Source + arrow->SetTipResolution(16); + arrow->SetTipLength(0.5); + arrow->SetTipRadius(0.2 * bbGetInputGlyphType()); + arrow->Update(); + + //Mask Points + ptMask->SetInputData(bbGetInputIn()); + ptMask->SetOnRatio(bbGetInputMaskRatio()); + ptMask->RandomModeOn(); + ptMask->Update(); + //Glyph + glyph->SetSourceData(arrow->GetOutput()); + glyph->SetInputData(ptMask->GetOutput()); + glyph->Update(); + //glyph->SetInputData(bbGetInputIn()); + glyph->SetVectorModeToUseNormal(); + glyph->SetScaleFactor(bbGetInputSize()); + glyph->SetColorModeToColorByVector(); + glyph->SetScaleModeToScaleByVector(); + glyph->OrientOn(); + + //Mapper y Actor + normalsMapper->SetInputData(glyph->GetOutput()); + normalsActor->SetMapper(normalsMapper); + normalsActor->GetProperty()->SetOpacity(bbGetInputOpacity()); + normalsActor->GetProperty()->SetColor(bbGetInputColor()[0], + bbGetInputColor()[1], + bbGetInputColor()[2]); + + if(bbGetInputRenderer() != NULL){ + if(actorAdded && !bbGetInputActive()){ + bbGetInputRenderer()->RemoveActor(normalsActor); + actorAdded = false; + } + else if(bbGetInputActive() && !actorAdded){ + bbGetInputRenderer()->AddActor(normalsActor); + actorAdded = true; + } + } + + bbSetOutputOut( normalsActor ); + } + +} +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void GlyphPolyDataNormals::bbUserSetDefaultValues() +{ + +// SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX +// Here we initialize the input 'In' to 0 + bbSetInputIn(NULL); + bbSetInputOpacity(1); + bbSetInputActive(false); + bbSetInputMaskRatio(10); + std::vector color; + color.push_back(0.0); + color.push_back(0.0); + color.push_back(0.5); + bbSetInputColor(color); + bbSetInputSize(10); + bbSetInputGlyphType(1); + bbSetInputRenderer(NULL); + bbSetOutputOut(NULL); + actorAdded = false; +} +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void GlyphPolyDataNormals::bbUserInitializeProcessing() +{ + +// THE INITIALIZATION METHOD BODY : +// Here does nothing +// but this is where you should allocate the internal/output pointers +// if any + arrow = vtkArrowSource::New(); + ptMask = vtkMaskPoints::New(); + glyph = vtkGlyph3D::New(); + normalsMapper = vtkPolyDataMapper::New(); + normalsActor = vtkActor::New(); + +} +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void GlyphPolyDataNormals::bbUserFinalizeProcessing() +{ + +// THE FINALIZATION METHOD BODY : +// Here does nothing +// but this is where you should desallocate the internal/output pointers +// if any + arrow->Delete(); + ptMask->Delete(); + glyph->Delete(); + normalsMapper->Delete(); + normalsActor->Delete(); + + arrow = NULL; + ptMask = NULL; + glyph = NULL; + normalsMapper = NULL; + normalsActor = NULL; +} +} +// EO namespace bbcreaVtk + + diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkGlyphPolyDataNormals.h b/bbtk_creaVtk_PKG/src/bbcreaVtkGlyphPolyDataNormals.h new file mode 100644 index 0000000..bdeecfd --- /dev/null +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkGlyphPolyDataNormals.h @@ -0,0 +1,77 @@ +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +#ifndef __bbcreaVtkGlyphPolyDataNormals_h_INCLUDED__ +#define __bbcreaVtkGlyphPolyDataNormals_h_INCLUDED__ + +#include "bbcreaVtk_EXPORT.h" +#include "bbtkAtomicBlackBox.h" +#include "iostream" + +#include "vtkPolyData.h" +#include "vtkRenderer.h" +#include "vtkProp3D.h" +#include "vtkGlyph3D.h" +#include "vtkArrowSource.h" +#include "vtkPolyDataMapper.h" +#include "vtkActor.h" +#include "vtkMaskPoints.h" + +namespace bbcreaVtk +{ + +class bbcreaVtk_EXPORT GlyphPolyDataNormals + : + public bbtk::AtomicBlackBox +{ + BBTK_BLACK_BOX_INTERFACE(GlyphPolyDataNormals,bbtk::AtomicBlackBox); +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== + BBTK_DECLARE_INPUT(In,vtkPolyData*); + BBTK_DECLARE_INPUT(Opacity, double); + BBTK_DECLARE_INPUT(MaskRatio, int); + BBTK_DECLARE_INPUT(Active, bool); + BBTK_DECLARE_INPUT(Color, std::vector); + BBTK_DECLARE_INPUT(Size, double); + BBTK_DECLARE_INPUT(GlyphType, int); + BBTK_DECLARE_INPUT(Renderer,vtkRenderer*); + BBTK_DECLARE_OUTPUT(Out,vtkProp3D*); + BBTK_PROCESS(Process); + void Process(); + + bool actorAdded; + vtkArrowSource *arrow; + vtkMaskPoints *ptMask; + vtkGlyph3D *glyph; + vtkPolyDataMapper *normalsMapper; + vtkActor *normalsActor; + +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +}; + +BBTK_BEGIN_DESCRIBE_BLACK_BOX(GlyphPolyDataNormals,bbtk::AtomicBlackBox); +BBTK_NAME("GlyphPolyDataNormals"); +BBTK_AUTHOR("InfoDev"); +BBTK_DESCRIPTION("No Description."); +BBTK_CATEGORY("empty"); +BBTK_INPUT(GlyphPolyDataNormals,In,"in PolyData, must set normals beforehand",vtkPolyData*,""); +BBTK_INPUT(GlyphPolyDataNormals,Opacity,"Opacity",double,""); +BBTK_INPUT(GlyphPolyDataNormals,MaskRatio,"Mask Ratio: select every nth point",int,""); +BBTK_INPUT(GlyphPolyDataNormals,Size,"Size",double,""); +BBTK_INPUT(GlyphPolyDataNormals,Active,"Active",bool,""); +BBTK_INPUT(GlyphPolyDataNormals,GlyphType,"Glyph type 0: Line | 1: Arrow",int,""); +BBTK_INPUT(GlyphPolyDataNormals,Color,"Color",std::vector,""); +BBTK_INPUT(GlyphPolyDataNormals,Renderer,"Renderer",vtkRenderer*,""); +BBTK_OUTPUT(GlyphPolyDataNormals,Out,"out vtkProp3D",vtkProp3D*,""); +BBTK_END_DESCRIBE_BLACK_BOX(GlyphPolyDataNormals); +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +} +// EO namespace bbcreaVtk + +#endif // __bbcreaVtkGlyphPolyDataNormals_h_INCLUDED__ + -- 2.47.1