Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brings BoundingRegion's related stuff from maliput_object. #518

Merged
merged 1 commit into from Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 102 additions & 0 deletions include/maliput/math/bounding_box.h
@@ -0,0 +1,102 @@
// BSD 3-Clause License
//
// Copyright (c) 2022, Woven Planet.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once

#include <vector>

#include "maliput/common/maliput_copyable.h"
#include "maliput/math/bounding_region.h"
#include "maliput/math/overlapping_type.h"
#include "maliput/math/roll_pitch_yaw.h"
#include "maliput/math/vector.h"

namespace maliput {
namespace math {

/// Implements BoundingRegion abstract class for non-axis-aligned-box-shaped bounding regions.
class BoundingBox : public BoundingRegion<Vector3> {
public:
MALIPUT_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(BoundingBox)

/// Constructs a BoundingBox object.
/// The box is defined by a position, dimensions(length, width and height) and orientation.
/// @param position Position of the bounding box in the Inertial-frame. The position matches with the centroid of the
/// box.
/// @param box_size The size of the bounding box on XYZ (length/width,height)
/// @param orientation Orientation of the box in the Inertial-frame.
/// @param tolerance Used to compute IsBoxContained() and IsBoxIntersected() against other BoundingBoxes.
/// @throws maliput::common::assertion_error When tolerance or any box_size's component are negative.
BoundingBox(const Vector3& position, const Vector3& box_size, const RollPitchYaw& orientation, double tolerance);

~BoundingBox() = default;

/// @returns The vertices of the bounding box in the Inertial-frame.
std::vector<Vector3> get_vertices() const;

/// @returns The orientation of the box in the Inertial-frame.
const RollPitchYaw& get_orientation() const;

/// @returns The size of the box in length, width and height.
const Vector3& box_size() const;

/// @returns True when this region contains @p other .
bool IsBoxContained(const BoundingBox& other) const;

/// @returns True when this region intersects @p other .
bool IsBoxIntersected(const BoundingBox& other) const;

private:
/// Implements BoundingRegion::do_position() method.
/// @returns Position of the box.
const Vector3& do_position() const override;

/// Implements BoundingRegion::DoContains() method.
/// @param position Inertial-frame's coordinate.
/// @returns True when @p position is contained in this bounding region.
bool DoContains(const Vector3& position) const override;

/// Implements BoundingRegion::DoOverlaps() method.
/// Valid @p other 's implementations:
/// - BoundingBox
/// @param other Another bounding region.
/// @returns The overlapping type.
OverlappingType DoOverlaps(const BoundingRegion<Vector3>& other) const override;

Vector3 position_;
Vector3 box_size_;
RollPitchYaw orientation_;
double tolerance_{};

// Half sized box dimensions.
Vector3 xyz_2_;
};

} // namespace math
} // namespace maliput
75 changes: 75 additions & 0 deletions include/maliput/math/bounding_region.h
@@ -0,0 +1,75 @@
// BSD 3-Clause License
//
// Copyright (c) 2022, Woven Planet.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once

#include "maliput/common/maliput_copyable.h"
#include "maliput/math/overlapping_type.h"

namespace maliput {
namespace math {

/// Abstract API for bounding description.
/// @tparam Coordinate Coordinate in a given coordinate system.
template <typename Coordinate>
class BoundingRegion {
public:
MALIPUT_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(BoundingRegion)

virtual ~BoundingRegion() = default;

/// Obtains the bounding region's position in the Inertial-frame.
/// The position is expected to match the centroid of the bounding region.
/// @returns The position coordinate.
const Coordinate& position() const { return do_position(); }

/// Determines whether a @p position in the Inertial-frame is contained in this bounding region.
/// @param position Inertial-frame's coordinate.
/// @returns True when @p position is contained in this bounding region.
bool Contains(const Coordinate& position) const { return DoContains(position); }

/// Determines the overlapping type with @p other BoundingRegion instance.
/// - OverlappingType::kDisjointed is returned when there is no overlapping with @p other .
/// - OverlappingType::kIntersected is returned when @p other intersects with this region.
/// - OverlappingType::kContained is returned when @p other is contained within this region.
/// @param other Another BoundingRegion.
/// @returns The overlapping type.
OverlappingType Overlaps(const BoundingRegion<Coordinate>& other) const { return DoOverlaps(other); }

protected:
BoundingRegion() = default;

private:
virtual const Coordinate& do_position() const = 0;
virtual bool DoContains(const Coordinate& position) const = 0;
virtual OverlappingType DoOverlaps(const BoundingRegion<Coordinate>& other) const = 0;
};

} // namespace math
} // namespace maliput
63 changes: 63 additions & 0 deletions include/maliput/math/overlapping_type.h
@@ -0,0 +1,63 @@
// BSD 3-Clause License
//
// Copyright (c) 2022, Woven Planet.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once

namespace maliput {
namespace math {

/// Holds the possible overlapping types between regions.
///
/// Given two sets `A` and `B` :
/// - `A` intersects `B` iff `A` and `B` have at least one point in common.
/// - `A` contains `B` iff `A` contains all the points of `B`.
/// - `A` disjoints `B` iff `A` and `B` have no points in common.
///
/// - Example of use:
/// @code {.cpp}
/// OverlappingType MyMethod();
/// ...
/// if(OverlappingType::kIntersected & MyMethod() == OverlappingType::kIntersected) {
/// // Do something.
/// }
/// @endcode
enum class OverlappingType : unsigned int {
kDisjointed = 0, ///< No overlapping between bounding regions
kIntersected = 1, ///< Bounding regions are intersected.
kContained = 3, ///< Bounding regions are contained.
};

// Union operator.
OverlappingType operator|(const OverlappingType& first, const OverlappingType& second);

// Intersection operator.
OverlappingType operator&(const OverlappingType& first, const OverlappingType& second);

} // namespace math
} // namespace maliput
53 changes: 53 additions & 0 deletions include/maliput/test_utilities/mock_math.h
@@ -0,0 +1,53 @@
// BSD 3-Clause License
//
// Copyright (c) 2022, Woven Planet.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <memory>
#include <unordered_map>
#include <vector>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "maliput/math/bounding_box.h"
#include "maliput/math/overlapping_type.h"

namespace maliput {
namespace math {
namespace test {

class MockBoundingRegion : public BoundingRegion<Vector3> {
public:
MOCK_METHOD((const Vector3&), do_position, (), (const, override));
MOCK_METHOD((bool), DoContains, (const Vector3&), (const, override));
MOCK_METHOD((OverlappingType), DoOverlaps, (const BoundingRegion<Vector3>&), (const, override));
};

} // namespace test
} // namespace math
} // namespace maliput
2 changes: 2 additions & 0 deletions src/math/CMakeLists.txt
Expand Up @@ -3,8 +3,10 @@
##############################################################################

set(MATH_SOURCES
bounding_box.cc
matrix.cc
quaternion.cc
overlapping_type.cc
roll_pitch_yaw.cc
saturate.cc
vector.cc
Expand Down