From 74be3fc6fccefae9df8b8b795aef6d2da137efaa Mon Sep 17 00:00:00 2001 From: jmespadero Date: Wed, 4 Oct 2023 15:44:37 +0200 Subject: [PATCH 1/2] bbox_tokens --- .../filter_func/filter_func.cpp | 118 ++++++++++++++---- src/meshlabplugins/filter_func/filter_func.h | 7 ++ 2 files changed, 104 insertions(+), 21 deletions(-) diff --git a/src/meshlabplugins/filter_func/filter_func.cpp b/src/meshlabplugins/filter_func/filter_func.cpp index 34c693fa62..e035357aef 100644 --- a/src/meshlabplugins/filter_func/filter_func.cpp +++ b/src/meshlabplugins/filter_func/filter_func.cpp @@ -30,6 +30,8 @@ #include "muParser.h" #include "string_conversion.h" +#include + using namespace mu; using namespace vcg; @@ -123,34 +125,45 @@ QString FilterFunctionPlugin::pythonFilterName(ActionIDType f) const } const QString PossibleOperators( - "
It's possible to use parenthesis (), and predefined " - "muparser built-in operators, like:
" + "
It's possible to use any of the predefined muparser built-in " + "operators and " + "functions, like: " "&& (logic and), || (logic or), <, <=, >, >=, " - "!= (not equal), == (equal), _?_:_ (c/c++ ternary operator).

"); + "!= (not equal), == (equal), _?_:_ (c/c++ ternary operator), and " + "rnd() (random value in [0..1])" ); const QString PerVertexAttributeString( - "It's possible to use muparser built-in functions" - "and the following per-vertex variables in the expression:
" + ""); const QString PerFaceAttributeString( - "It's possible to use muparser built-in functions" - "and the following per-face or per-vertex variables:
" - "x0,y0,z0 for the first vertex position, x1,y1,z1 for the second vertex " - "position, x2,y2,z2 for the third vertex position, " - "nx0,ny0,nz0 nx1,ny1,nz1 nx2,ny2,nz2 for vertex normals, r0,g0,b0,a0 r1,g1,b1,a1 " - "r2,g2,b2,a2 for vertex colors, vi0, vi1, vi2 for vertex indices, " - "q0,q1,q2 for vertex quality, wtu0,wtv0 wtu1,wtv1 wtu2,wtv2 for per-wedge " - "texture coords, ti for face texture index, vsel0,vsel1,vsel2 for vertex " - "selection (1 yes, 0 no) " - "fi for face index, fr,fg,fb,fa for face color, fq for face quality, " - "fnx,fny,fnz for face normal, fsel face selection (1 yes, 0 no)." - "All user defined face scalar attributes are available." - "Point3 attribute are available as three variables with _x, _y, _z appended to the attribute name.

"); + ""); + // long string describing each filtering action QString FilterFunctionPlugin::filterInfo(ActionIDType filterId) const @@ -696,6 +709,24 @@ std::map FilterFunctionPlugin::applyFilter( md.addNewMesh("", this->filterName(ID(filter))); MeshModel& m = *(md.mm()); Q_UNUSED(cb); + + //Set values to parser variables related to BBox + vcg::Box3f bbox = m.cm.bbox; + xmin = bbox.min.X(); + ymin = bbox.min.Y(); + zmin = bbox.min.Z(); + xmax = bbox.max.X(); + ymax = bbox.max.Y(); + zmax = bbox.max.Z(); + xdim = bbox.DimX(); + ydim = bbox.DimY(); + zdim = bbox.DimZ(); + bbdiag = bbox.Diag(); + vcg::Point3f bbCenter = bbox.Center(); + xmid = bbCenter.X(); + ymid = bbCenter.Y(); + zmid = bbCenter.Z(); + switch (ID(filter)) { case FF_VERT_SELECTION: { std::string expr = par.getString("condSelect").toStdString(); @@ -1762,6 +1793,15 @@ void FilterFunctionPlugin::setAttributes(CMeshO::FaceIterator& fi, CMeshO& m) } + // Generate a random double in [0.0, 1.0] interval +double FilterFunctionPlugin::random() +{ + std::random_device rd; // Seed for the random number engine + std::mt19937 gen(rd()); // Mersenne Twister engine + std::uniform_real_distribution dis(0.0, 1.0); + return dis(gen); // Generate a random double in [0.0, 1.0] +} + // Function explicitly define parser variables to perform per-vertex filter action // x, y, z for vertex coord, nx, ny, nz for normal coord, r, g ,b for color // and q for quality @@ -1784,6 +1824,24 @@ void FilterFunctionPlugin::setPerVertexVariables(Parser& p, CMeshO& m) p.DefineVar(conversion::fromStringToWString("ti"), &ti); p.DefineVar(conversion::fromStringToWString("vsel"), &vsel); + //Add tokens related to mesh bounding box + p.DefineVar(conversion::fromStringToWString("xmin"), &xmin); + p.DefineVar(conversion::fromStringToWString("ymin"), &ymin); + p.DefineVar(conversion::fromStringToWString("zmin"), &zmin); + p.DefineVar(conversion::fromStringToWString("xmax"), &xmax); + p.DefineVar(conversion::fromStringToWString("ymax"), &ymax); + p.DefineVar(conversion::fromStringToWString("zmax"), &zmax); + p.DefineVar(conversion::fromStringToWString("bbdiag"), &bbdiag); + p.DefineVar(conversion::fromStringToWString("xdim"), &xdim); + p.DefineVar(conversion::fromStringToWString("ydim"), &ydim); + p.DefineVar(conversion::fromStringToWString("zdim"), &zdim); + p.DefineVar(conversion::fromStringToWString("xmid"), &xmid); + p.DefineVar(conversion::fromStringToWString("ymid"), &ymid); + p.DefineVar(conversion::fromStringToWString("zmid"), &zmid); + + //Add function rnd() + p.DefineFun("rnd", random); + // define var for user-defined attributes (if any exists) // if vector is empty, code won't be executed v_handlers.clear(); @@ -1912,6 +1970,24 @@ void FilterFunctionPlugin::setPerFaceVariables(Parser& p, CMeshO& m) p.DefineVar(conversion::fromStringToWString("vsel2"), &vsel2); p.DefineVar(conversion::fromStringToWString("fsel"), &fsel); + //Add tokens related to mesh bounding box + p.DefineVar(conversion::fromStringToWString("xmin"), &xmin); + p.DefineVar(conversion::fromStringToWString("ymin"), &ymin); + p.DefineVar(conversion::fromStringToWString("zmin"), &zmin); + p.DefineVar(conversion::fromStringToWString("xmax"), &xmax); + p.DefineVar(conversion::fromStringToWString("ymax"), &ymax); + p.DefineVar(conversion::fromStringToWString("zmax"), &zmax); + p.DefineVar(conversion::fromStringToWString("bbdiag"), &bbdiag); + p.DefineVar(conversion::fromStringToWString("xdim"), &xdim); + p.DefineVar(conversion::fromStringToWString("ydim"), &ydim); + p.DefineVar(conversion::fromStringToWString("zdim"), &zdim); + p.DefineVar(conversion::fromStringToWString("xmid"), &xmid); + p.DefineVar(conversion::fromStringToWString("ymid"), &ymid); + p.DefineVar(conversion::fromStringToWString("zmid"), &zmid); + + //Add function rnd() + p.DefineFun("rnd", random); + // define var for user-defined attributes (if any exists) // if vector is empty, code won't be executed f_handlers.clear(); diff --git a/src/meshlabplugins/filter_func/filter_func.h b/src/meshlabplugins/filter_func/filter_func.h index 4e6a0c53e3..c1ca56608d 100644 --- a/src/meshlabplugins/filter_func/filter_func.h +++ b/src/meshlabplugins/filter_func/filter_func.h @@ -44,6 +44,10 @@ class FilterFunctionPlugin : public QObject, public FilterPlugin vsel0, vsel1, vsel2; double fr, fg, fb, fa, fnx, fny, fnz, fq, fsel; double v, f, v0i, v1i, v2i, ti; + + //Bounding Box of the mesh + double xmin, ymin, zmin, xmax, ymax, zmax, xdim, ydim, zdim, bbdiag, xmid, ymid, zmid; + std::vector v_attrNames; // names of the per vertex attributes std::vector v_attrValue; // values of the per vertex attributes std::vector v3_attrNames; // names of the per vertex attributes There are @@ -62,6 +66,9 @@ class FilterFunctionPlugin : public QObject, public FilterPlugin QString errorMsg; + // Generate a random double in [0.0, 1.0] interval + static double random(); + public: enum { FF_VERT_SELECTION, From 0b44eb991c4b0bd49dff319a0cdfba61542440cc Mon Sep 17 00:00:00 2001 From: jmespadero Date: Wed, 4 Oct 2023 15:52:42 +0200 Subject: [PATCH 2/2] Add bbox tokens to muparse --- src/meshlabplugins/filter_func/filter_func.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/meshlabplugins/filter_func/filter_func.cpp b/src/meshlabplugins/filter_func/filter_func.cpp index e035357aef..31f1c278d7 100644 --- a/src/meshlabplugins/filter_func/filter_func.cpp +++ b/src/meshlabplugins/filter_func/filter_func.cpp @@ -130,7 +130,7 @@ const QString PossibleOperators( "functions, like: " "&& (logic and), || (logic or), <, <=, >, >=, " "!= (not equal), == (equal), _?_:_ (c/c++ ternary operator), and " - "rnd() (random value in [0..1])" ); + "rnd() (random value in [0..1]), and these values:" ); const QString PerVertexAttributeString( "
  • Per-vertex variables:
    " @@ -139,7 +139,8 @@ const QString PerVertexAttributeString( "index), vsel ( 1 if selected, 0 if not selected)." "
  • Bounding Box variables:
    " "xmin,ymin,zmin (min coordinates), xmax,ymax,zmax (max coordinates), " - "xmid,ymid,zmid (midpoint coordinates), xdim,ydim,zdim (dimensions), bbdiag (diagonal length)" + "xmid,ymid,zmid (midpoint coordinates), xdim,ydim,zdim (dimensions), " + "bbdiag (diagonal length)" "
  • User-defined attributes:
    " "All user defined custom vertex attributes are available. " "Point3 attribute are available as 3 variables with _x, _y, _z appended to the attribute name." @@ -148,17 +149,18 @@ const QString PerVertexAttributeString( const QString PerFaceAttributeString( "
    • Per-face variables:
      " "fi (face index), fr,fg,fb,fa (face color), fq (face quality), " - "fnx,fny,fnz (face normal), fsel ( 1 if selected, 0 if not selected)." + "fnx,fny,fnz (face normal), fsel ( 1 if face is selected, 0 if not selected)." "
    • Per-vertex variables:
      " "x0,y0,z0 (first vertex position), x1,y1,z1 (second vertex position)," "x2,y2,z2 (third vertex position), " "nx0,ny0,nz0 nx1,ny1,nz1 nx2,ny2,nz2 (vertex normals), r0,g0,b0,a0 r1,g1,b1,a1 " "r2,g2,b2,a2 (vertex colors), vi0, vi1, vi2 (vertex indices), " - "q0,q1,q2 (vertex quality), wtu0,wtv0 wtu1,wtv1 wtu2,wtv2 (per-wedge " - "texture coords), ti (face texture index), vsel0,vsel1,vsel2 (vertex selected (1 yes, 0 no))." + "q0,q1,q2 (vertex quality), wtu0,wtv0 wtu1,wtv1 wtu2,wtv2 (per-wedge texture coords), " + "ti (face texture index), vsel0,vsel1,vsel2 (1 if vertex is selected, 0 if not)." "
    • Bounding Box variables:
      " "xmin,ymin,zmin (min coordinates), xmax,ymax,zmax (max coordinates), " - "xmid,ymid,zmid (midpoint coordinates), xdim,ydim,zdim (dimensions), bbdiag (diagonal length)." + "xmid,ymid,zmid (midpoint coordinates), xdim,ydim,zdim (dimensions), " + "bbdiag (diagonal length)." "
    • User-defined attributes:
      " "All user defined custom face scalar attributes are available. " "Point3 attribute are available as 3 variables with _x, _y, _z appended to the attribute name." @@ -711,7 +713,7 @@ std::map FilterFunctionPlugin::applyFilter( Q_UNUSED(cb); //Set values to parser variables related to BBox - vcg::Box3f bbox = m.cm.bbox; + const auto &bbox = m.cm.bbox; xmin = bbox.min.X(); ymin = bbox.min.Y(); zmin = bbox.min.Z(); @@ -722,7 +724,7 @@ std::map FilterFunctionPlugin::applyFilter( ydim = bbox.DimY(); zdim = bbox.DimZ(); bbdiag = bbox.Diag(); - vcg::Point3f bbCenter = bbox.Center(); + auto bbCenter = bbox.Center(); xmid = bbCenter.X(); ymid = bbCenter.Y(); zmid = bbCenter.Z();