marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
2.05k stars 266 forks source link

TetMesh threshold possible bugs #385

Closed XushanLu closed 3 years ago

XushanLu commented 3 years ago

I think there is possibly a bug in vedo.tetmesh.

In the code, you have

     if above is not None and below is not None:
            if above<below:
                th.ThresholdBetween(above, below)
            elif above==below:
                return self

which simply means above cannot be equal to below because otherwise the entire tetmesh would be returned which is not really what should happen. Sometimes, we are purely interested in thresholding the mesh to get just those cells equal to a single value. Also, it seems that vtk does allow above to be equal to below (https://vtk.org/doc/release/5.0/html/a03868.html):

00158   int Lower(double s) {return ( s <= this->LowerThreshold ? 1 : 0 );};
00159   int Upper(double s) {return ( s >= this->UpperThreshold ? 1 : 0 );};
00160   int Between(double s) {return ( s >= this->LowerThreshold ? 
00161                                ( s <= this->UpperThreshold ? 1 : 0 ) : 0 );};

I tried to bypass the current vedo code which does not allow above == below using the following code:

        if above is not None and below is not None:
            if above<below:
                th.ThresholdBetween(above, below)
            elif above==below:
                th.ThresholdBetween(above, below)
            elif above>below:
                printc("threshold(): above cannot be larger than below. Skip.", c='r')
                return self

and I think I got what I wanted, i.e., getting cells with the attribute that is the same as above and below.

Please take a look at this and see what you think. Thanks!

marcomusy commented 3 years ago

Oh you're right Thanks for reporting it, what if we do:

        if above is not None and below is not None:
            if above > below:
                th.SetInvert(True)
                th.ThresholdBetween(below, above)
            else:
                th.ThresholdBetween(above, below)

        elif above is not None:
            th.ThresholdByUpper(above)

        elif below is not None:
            th.ThresholdByLower(below)

that would even allow for above > below by flipping the selection, what do you think of it? does it work?

XushanLu commented 3 years ago

I think that surely works. It is probably the best just to flip above and below when above < below rather than throwing out an error. I will update the code from the repository to see what happens.

XushanLu commented 3 years ago

Hi @marcomusy , I don't think the code is in the repository, is it? I tried to update the code with pip install -U git+https://github.com/marcomusy/vedo.git but nothing seems to have been changed.