Skip to content

Commit 7606864

Browse files
committed
fix: updating bounding box project/reproject to just translate to origin
1 parent 6aaa302 commit 7606864

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

LoopStructural/datatypes/_bounding_box.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def __init__(
1717
origin: Optional[np.ndarray] = None,
1818
maximum: Optional[np.ndarray] = None,
1919
global_origin: Optional[np.ndarray] = None,
20+
global_maximum: Optional[np.ndarray] = None,
2021
nsteps: Optional[np.ndarray] = None,
2122
step_vector: Optional[np.ndarray] = None,
2223
dimensions: Optional[int] = 3,
@@ -39,9 +40,12 @@ def __init__(
3940
# we want the local coordinates to start at 0
4041
# otherwise uses provided origin. This is useful for having multiple bounding boxes rela
4142
if global_origin is not None and origin is None:
42-
origin = np.zeros(global_origin.shape)
43+
origin = np.zeros(np.array(global_origin).shape)
44+
if global_maximum is not None and global_origin is not None:
45+
maximum = np.array(global_maximum) - np.array(global_origin)
46+
4347
if maximum is None and nsteps is not None and step_vector is not None:
44-
maximum = origin + nsteps * step_vector
48+
maximum = np.array(origin) + np.array(nsteps) * np.array(step_vector)
4549
if origin is not None and global_origin is None:
4650
global_origin = np.zeros(3)
4751
self._origin = np.array(origin)
@@ -517,11 +521,8 @@ def project(self, xyz, inplace=False):
517521
"""
518522
if inplace:
519523
xyz -= self.global_origin
520-
xyz = np.clip(xyz, self.origin, self.maximum)
521524
return xyz
522-
return (xyz - self.global_origin) / np.max(
523-
(self.global_maximum - self.global_origin)
524-
) # np.clip(xyz, self.origin, self.maximum)
525+
return (xyz - self.global_origin) # np.clip(xyz, self.origin, self.maximum)
525526

526527
def scale_by_projection_factor(self, value):
527528
return value / np.max((self.global_maximum - self.global_origin))
@@ -541,10 +542,9 @@ def reproject(self, xyz, inplace=False):
541542
reprojected point
542543
"""
543544
if inplace:
544-
xyz -= self.global_origin
545-
xyz /= np.max((self.global_maximum - self.global_origin))
545+
xyz += self.global_origin
546546
return xyz
547-
return xyz * np.max((self.global_maximum - self.global_origin)) + self.global_origin
547+
return xyz + self.global_origin
548548

549549
def __repr__(self):
550550
return f"BoundingBox({self.origin}, {self.maximum}, {self.nsteps})"

LoopStructural/modelling/core/geological_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ def __init__(
111111
raise ValueError("Must provide a bounding box")
112112
self.bounding_box = bounding_box
113113
if len(args) == 2:
114-
origin = args[0]
115-
maximum = args[1]
114+
origin = np.array(args[0])
115+
maximum = np.array(args[1])
116116
if not isinstance(origin, np.ndarray) or not isinstance(maximum, np.ndarray):
117117
raise ValueError("Must provide origin and maximum as numpy arrays")
118118
self.bounding_box = BoundingBox(
@@ -510,7 +510,7 @@ def data(self, data: pd.DataFrame):
510510

511511
def set_model_data(self, data):
512512
logger.warning("deprecated method. Model data can now be set using the data attribute")
513-
self.data = data
513+
self.data = data.copy()
514514

515515
def set_stratigraphic_column(self, stratigraphic_column, cmap="tab20"):
516516
"""

tests/integration/test_interpolator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def model_fit(model, data):
1414
def test_create_model():
1515
data, bb = load_claudius()
1616
model = GeologicalModel(bb[0, :], bb[1, :])
17-
assert np.all(np.isclose(model.origin, bb[0, :]))
18-
assert np.all(np.isclose(model.maximum, bb[1, :]))
17+
assert np.all(np.isclose(model.bounding_box.global_origin, bb[0, :]))
18+
assert np.all(np.isclose(model.bounding_box.global_maximum, bb[1, :]))
1919

2020

2121
def test_add_data():

tests/unit/datatypes/test_bounding_box.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ def test_regular_grid_2d():
9797
grid = bbox.regular_grid((10, 10))
9898
assert grid.shape == (10 * 10, 2)
9999

100-
100+
def test_project_to_local():
101+
bbox = BoundingBox(global_origin=[10,10,10], global_maximum=[20,20,20])
102+
point = np.array([15, 15, 15])
103+
local_point = bbox.project(point)
104+
assert np.all(local_point == np.array([5, 5, 5]))
101105
if __name__ == "__main__":
102106
test_create_bounding_box()
103107
test_create_bounding_box_from_points()
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
from LoopStructural import GeologicalModel
22
from LoopStructural.datasets import load_claudius
33
import numpy as np
4+
import pytest
45

6+
@pytest.mark.parametrize("origin, maximum", [([0,0,0],[5,5,5]), ([10,10,10],[15,15,15])])
7+
def test_create_geological_model(origin, maximum):
8+
model = GeologicalModel(origin, maximum)
9+
assert (model.bounding_box.global_origin - np.array(origin)).sum() == 0
10+
assert (model.bounding_box.global_maximum - np.array(maximum)).sum() == 0
11+
assert (model.bounding_box.origin - np.zeros(3)).sum() == 0
12+
assert (model.bounding_box.maximum - np.ones(3)*5).sum() == 0
513

6-
def test_create_geological_model():
7-
model = GeologicalModel([0, 0, 0], [5, 5, 5])
8-
assert (model.origin - np.array([0, 0, 0])).sum() == 0
9-
assert (model.maximum - np.array([5, 5, 5])).sum() == 0
10-
11-
14+
def test_rescale_model_data():
15+
data, bb = load_claudius()
16+
model = GeologicalModel(bb[0, :], bb[1, :])
17+
model.set_model_data(data)
18+
# Check that the model data is rescaled to local coordinates
19+
expected = data[['X', 'Y', 'Z']].values - bb[None, 0, :]
20+
actual = model.data[['X', 'Y', 'Z']].values
21+
assert np.allclose(actual, expected, atol=1e-6)
1222
def test_access_feature_model():
1323
data, bb = load_claudius()
1424
model = GeologicalModel(bb[0, :], bb[1, :])
1525
model.set_model_data(data)
1626
s0 = model.create_and_add_foliation("strati")
1727
assert s0 == model["strati"]
28+
29+
if __name__ == "__main__":
30+
test_rescale_model_data()

0 commit comments

Comments
 (0)