diff --git a/src/meshlabplugins/filter_func/filter_func.cpp b/src/meshlabplugins/filter_func/filter_func.cpp index 0acca0fed..530f15e60 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; @@ -137,34 +139,47 @@ 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]), and these values:" ); 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 @@ -710,6 +725,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 + const auto &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(); + auto 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(); @@ -1814,6 +1847,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 @@ -1836,6 +1878,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(); @@ -1964,6 +2024,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 4e6a0c53e..c1ca56608 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,