AcademySoftwareFoundation / AcademySoftwareFoundation/openvdb

[BUG] Python API background value different from what's expected

Open
#1,096 4 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
3.4k
Forks
774
Avg merge
3d 9h
Merged PRs (30d)
34

Description

### Environment

**Operating System:** Linux
**Version / Commit SHA:** master

### Describe the bug

Something it's wrong with the python wrappers for `FloatGrid`. I'm trying to debug this issue myself but so far I haven't reach anywhere. Looks like the precision of floating point numbers is messing around, or sometihng similar.

I manage to narrow down the error to a minimalistic example.

**NOTE** This problem only appears in the Python wrappers in `pyopenvdb.so`, not a C++ API issue.

### To Reproduce

#### C++ Example

**Code:**

```cpp
#include

#include

using std::cout;
using std::endl;

int main() {
openvdb::initialize();
const float background_value = 0.3;
openvdb::FloatGrid::Ptr grid = openvdb::FloatGrid::create(background_value);

grid->print();
cout << "grid->background() = " << grid->background() << endl;
cout << std::boolalpha << (grid->background() == background_value) << endl;
return 0;
}
```

**Output:**

```sh
Information about Tree:
Type: Tree_float_5_4_3
Configuration:
Root(0), Internal(32^3), Internal(16^3), Leaf(8^3)
Background value: 0.3
Transform:
voxel size: 1
index to world:
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]

grid->background() = 0.3
true
```

#### Python Example

**Code:**

```python
import pyopenvdb as vdb

background_value = float(0.3)
grid = vdb.FloatGrid(background_value)
print(grid.info())
print("grid.background =", grid.background)
print(grid.background == background_value)
```

**Output:**

```sh
Information about Tree:
Type: Tree_float_5_4_3
Configuration:
Root(0), Internal(32^3), Internal(16^3), Leaf(8^3)
Background value: 0.3
Transform:
voxel size: 1
index to world:
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]

grid.background = 0.30000001192092896
False
```

### Expected behavior

As shown in the example, the value `0.3` gets interpreted as `0.30000001192092896` when using the Python API. Which seems strange...

Moreover, the comparission `grid.background == background_value` return `False`, **only** on the Python example, which confirms that something is "wrong".

### Additional context

Any ideas on where to look for this?
The problem with this error is that when working with python vdb grids, the background value doesn't get propperly interpreted, leading to a denser vdb representation(since `0.3` is not a background value anymore, it will be considered an active value on the grid)

Contributor guide

Open the contributing guide

Research direction

Start with the pyopenvdb.so FloatGrid constructor and background property, comparing their behavior with the C++ FloatGrid example in the issue. Confirm the conversion of 0.3 and the equality result, then verify that Python-created grids retain the intended background value without causing equivalent values to become active.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.