forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minimal version of GenTrap (Arb8) shape (celeritas-project#1171)
- Loading branch information
Showing
10 changed files
with
527 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
//----------------------------------*-C++-*----------------------------------// | ||
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers. | ||
// See the top-level COPYRIGHT file for details. | ||
// SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
//---------------------------------------------------------------------------// | ||
//! \file orange/orangeinp/detail/PolygonUtils.hh | ||
//! \brief Utility standalone functions for polygons in 2D or 3D space. | ||
//---------------------------------------------------------------------------// | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "corecel/cont/Array.hh" | ||
#include "corecel/cont/Range.hh" | ||
#include "corecel/cont/Span.hh" | ||
#include "corecel/math/ArrayOperators.hh" | ||
#include "corecel/math/ArrayUtils.hh" | ||
#include "orange/OrangeTypes.hh" | ||
|
||
namespace celeritas | ||
{ | ||
namespace orangeinp | ||
{ | ||
namespace detail | ||
{ | ||
//---------------------------------------------------------------------------// | ||
using Real2 = Array<real_type, 2>; | ||
|
||
//---------------------------------------------------------------------------// | ||
/*! | ||
* Polygon orientation based on ordering of vertices. | ||
*/ | ||
enum class Orientation | ||
{ | ||
clockwise = -1, | ||
collinear = 0, | ||
counterclockwise = 1, | ||
}; | ||
|
||
//---------------------------------------------------------------------------// | ||
// FREE FUNCTIONS | ||
//---------------------------------------------------------------------------// | ||
/*! | ||
* Find orientation of ordered vertices in 2D coordinates. | ||
*/ | ||
CELER_FORCEINLINE_FUNCTION Orientation orientation(Real2 const& a, | ||
Real2 const& b, | ||
Real2 const& c) | ||
{ | ||
auto crossp = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]); | ||
return crossp < 0 ? Orientation::clockwise | ||
: crossp > 0 ? Orientation::counterclockwise | ||
: Orientation::collinear; | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
/*! | ||
* Check if a 2D polygon is convex. | ||
* | ||
* \param corners the vertices of the polygon | ||
* \param degen_ok allow consecutive degenerate points | ||
*/ | ||
CELER_FUNCTION bool | ||
is_convex(Span<const Real2> const& corners, bool degen_ok = false) | ||
{ | ||
CELER_EXPECT(!corners.empty()); | ||
// The cross product of all vector pairs corresponding to ordered | ||
// consecutive segments has to be positive. | ||
auto crossp = [&](Real2 const& v1, Real2 const& v2) { | ||
return v1[0] * v2[1] - v1[1] * v2[0]; | ||
}; | ||
|
||
// Use the cross_product(last, first) as sign reference | ||
auto num_corners = corners.size(); | ||
Real2 vec1 = corners[0] - corners[num_corners - 1]; | ||
Real2 vec2 = corners[1] - corners[0]; | ||
real_type const ref = crossp(vec1, vec2); | ||
for (auto i : range(num_corners - 1)) | ||
{ | ||
vec1 = vec2; | ||
vec2 = corners[(i + 2) % num_corners] - corners[(i + 1) % num_corners]; | ||
auto val = crossp(vec1, vec2); | ||
// Make sure the sign is the same as the reference | ||
if (val * ref < 0 || (!degen_ok && val == 0)) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
/*! | ||
* Check for coplanarity of four 3D polygon vertices. | ||
*/ | ||
CELER_FUNCTION bool | ||
is_planar(Real3 const& a, Real3 const& b, Real3 const& c, Real3 const& d) | ||
{ | ||
// Use the cross_product(last, first) as sign reference | ||
auto norm = make_unit_vector(cross_product(b - a, c - a)); | ||
auto val = dot_product(norm, d - a); | ||
|
||
// FIXME: SoftEqual and SoftZero should have rel = abs | ||
return SoftZero{SoftEqual<>{}.rel()}(val); | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
} // namespace detail | ||
} // namespace orangeinp | ||
} // namespace celeritas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.