Skip to content

Commit a163dfa

Browse files
committed
Make range-based bounding_aabb_of return std::optional<AABB> when it makes sense (#1132)
1 parent 4383e9c commit a163dfa

31 files changed

+187
-109
lines changed

libopensimcreator/Documents/MeshImporter/Body.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace osc::mi
7878
m_Xform.scale = {1.0f, 1.0f, 1.0f};
7979
}
8080

81-
AABB implCalcBounds(const IObjectFinder&) const final
81+
std::optional<AABB> implCalcBounds(const IObjectFinder&) const final
8282
{
8383
return bounding_aabb_of(m_Xform.translation);
8484
}

libopensimcreator/Documents/MeshImporter/Ground.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ namespace osc::mi
3434
return identity<Transform>();
3535
}
3636

37-
AABB implCalcBounds(const IObjectFinder&) const final
37+
std::optional<AABB> implCalcBounds(const IObjectFinder&) const final
3838
{
39-
return AABB{};
39+
return std::nullopt;
4040
}
4141
};
4242
}

libopensimcreator/Documents/MeshImporter/Joint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ namespace osc::mi
116116
m_Xform.scale = {1.0f, 1.0f, 1.0f};
117117
}
118118

119-
AABB implCalcBounds(const IObjectFinder&) const final
119+
std::optional<AABB> implCalcBounds(const IObjectFinder&) const final
120120
{
121121
return bounding_aabb_of(m_Xform.translation);
122122
}

libopensimcreator/Documents/MeshImporter/MIObject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ namespace osc::mi
137137
setXform(lookup, getXForm(lookup).with_rotation(newRotation));
138138
}
139139

140-
AABB calcBounds(const IObjectFinder& lookup) const
140+
std::optional<AABB> calcBounds(const IObjectFinder& lookup) const
141141
{
142142
return implCalcBounds(lookup);
143143
}
@@ -218,6 +218,6 @@ namespace osc::mi
218218
virtual Transform implGetXform(const IObjectFinder&) const = 0;
219219
virtual void implSetXform(const IObjectFinder&, const Transform&) {}
220220

221-
virtual AABB implCalcBounds(const IObjectFinder&) const = 0;
221+
virtual std::optional<AABB> implCalcBounds(const IObjectFinder&) const = 0;
222222
};
223223
}

libopensimcreator/Documents/MeshImporter/Mesh.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ osc::mi::Mesh::Mesh(
3434
m_Name{SanitizeToOpenSimComponentName(m_Path.filename().replace_extension().string())}
3535
{}
3636

37-
AABB osc::mi::Mesh::calcBounds() const
37+
std::optional<AABB> osc::mi::Mesh::calcBounds() const
3838
{
39-
return transform_aabb(m_Transform, m_MeshData.bounds());
39+
return m_MeshData.bounds().transform([this](const AABB& localBounds)
40+
{
41+
return transform_aabb(m_Transform, localBounds);
42+
});
4043
}
4144

4245
void osc::mi::Mesh::reloadMeshDataFromDisk()

libopensimcreator/Documents/MeshImporter/Mesh.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace osc::mi
5858
m_Transform = t;
5959
}
6060

61-
AABB calcBounds() const;
61+
std::optional<AABB> calcBounds() const;
6262

6363
void reloadMeshDataFromDisk();
6464

@@ -113,7 +113,7 @@ namespace osc::mi
113113
setXform(t);
114114
}
115115

116-
AABB implCalcBounds(const IObjectFinder&) const final
116+
std::optional<AABB> implCalcBounds(const IObjectFinder&) const final
117117
{
118118
return calcBounds();
119119
}

libopensimcreator/Documents/MeshImporter/Station.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace osc::mi
9191
m_Position = t.translation;
9292
}
9393

94-
AABB implCalcBounds(const IObjectFinder&) const final
94+
std::optional<AABB> implCalcBounds(const IObjectFinder&) const final
9595
{
9696
return bounding_aabb_of(m_Position);
9797
}

libopensimcreator/Documents/MeshImporter/UndoableActions.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,13 @@ bool osc::mi::TryTranslateToMeshBoundsCenter(
258258
return false;
259259
}
260260

261-
const Vector3 boundsMidpoint = centroid_of(mesh->calcBounds());
261+
const std::optional<AABB> bounds = mesh->calcBounds();
262+
if (!bounds)
263+
{
264+
return false;
265+
}
266+
267+
const Vector3 boundsMidpoint = centroid_of(*bounds);
262268

263269
obj->setPos(doc, boundsMidpoint);
264270
udoc.commit_scratch("moved " + obj->getLabel());

libopensimcreator/Graphics/OpenSimDecorationGenerator.tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ TEST(GenerateModelDecorations, RadiusOfContactSphereIsCorrectlyUpdated)
577577
// Before changing radius: it should be as-set
578578
{
579579
const auto decorations = GenerateModelDecorations(cache, model, state);
580-
const float volume = volume_of(bounding_aabb_of(decorations, &SceneDecoration::world_space_bounds));
580+
const float volume = volume_of(bounding_aabb_of(decorations, &SceneDecoration::world_space_bounds).value());
581581
ASSERT_NEAR(volume, 0.2f*0.2f*0.2f, 0.001f);
582582
}
583583

@@ -588,7 +588,7 @@ TEST(GenerateModelDecorations, RadiusOfContactSphereIsCorrectlyUpdated)
588588
// After changing radius: should update it
589589
{
590590
const auto decorations = GenerateModelDecorations(cache, model, state);
591-
const float volume = volume_of(bounding_aabb_of(decorations, &SceneDecoration::world_space_bounds));
591+
const float volume = volume_of(bounding_aabb_of(decorations, &SceneDecoration::world_space_bounds).value());
592592
ASSERT_NEAR(volume, 1.0f*1.0f*1.0f, 0.001f);
593593
}
594594
}

libopensimcreator/UI/MeshImporter/DrawableThing.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ namespace osc::mi
2323
SceneDecorationFlags flags = SceneDecorationFlag::Default;
2424
};
2525

26-
inline AABB calcBounds(const DrawableThing& dt)
26+
inline std::optional<AABB> calcBounds(const DrawableThing& dt)
2727
{
28-
return transform_aabb(dt.transform, dt.mesh.bounds());
28+
return dt.mesh.bounds().transform([&dt](const AABB& localBounds)
29+
{
30+
return transform_aabb(dt.transform, localBounds);
31+
});
2932
}
3033
}

0 commit comments

Comments
 (0)