Skip to content

Commit

Permalink
Refactored Malo's code to avoid DRY
Browse files Browse the repository at this point in the history
I also added unit tests for the TiglWingHelperFunctions.

Addresses #827
  • Loading branch information
rainman110 committed Nov 12, 2021
1 parent 7b01138 commit b3c5ab4
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/wing/CCPACSWing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ double CCPACSWing::GetReferenceArea(TiglSymmetryAxis symPlane) const

double CCPACSWing::GetReferenceArea() const
{
TiglAxis spanDir = winghelper::GetMajorDirection(*this);
TiglAxis deepDir = winghelper::GetDeepDirection(*this);
TiglAxis spanDir = winghelper::GetWingSpanAxis(*this);
TiglAxis deepDir = winghelper::GetWingDepthAxis(*this);

if (spanDir == TIGL_Y_AXIS && deepDir == TIGL_X_AXIS) {
return GetReferenceArea(TIGL_X_Y_PLANE);
Expand Down
65 changes: 36 additions & 29 deletions src/wing/TiglWingHelperFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,35 @@
#include "tigl.h"
#include "CCPACSWingSegment.h"

namespace
{
struct CumulatedLength
{
gp_XYZ depth{0., 0., 0.};
gp_XYZ span{0., 0., 0.};
};


// Helper function to compute accumulated depth and span of the wing
CumulatedLength GetCumulatedLength(const tigl::CCPACSWing& wing)
{
CumulatedLength length;
for (int i = 1; i <= wing.GetSegmentCount(); ++i) {
const auto& segment = wing.GetSegment(i);
gp_XYZ dirSpan = segment.GetChordPoint(1, 0).XYZ() - segment.GetChordPoint(0, 0).XYZ();
gp_XYZ dirDepth = segment.GetChordPoint(0, 1).XYZ() - segment.GetChordPoint(0, 0).XYZ();
length.span += gp_XYZ(fabs(dirSpan.X()), fabs(dirSpan.Y()), fabs(dirSpan.Z()));
length.depth += gp_XYZ(fabs(dirDepth.X()), fabs(dirDepth.Y()), fabs(dirDepth.Z()));

}
const auto& outerSegment = wing.GetSegment(wing.GetSegmentCount());
gp_XYZ dirDepth = outerSegment.GetChordPoint(1, 1).XYZ() - outerSegment.GetChordPoint(1, 0).XYZ();
length.depth += gp_XYZ(fabs(dirDepth.X()), fabs(dirDepth.Y()), fabs(dirDepth.Z()));

return length;
}
}

namespace tigl
{

Expand All @@ -35,27 +64,17 @@ bool HasShape(const tigl::CCPACSWing& wing)

}

TiglAxis GetDeepDirection(const tigl::CCPACSWing& wing)
TiglAxis GetWingDepthAxis(const tigl::CCPACSWing& wing)
{
if (!HasShape(wing)) {
LOG(WARNING) << "CTiglWingHelper::GetDeepDirection: This wing has no shape -> impossible to determine the "
"direction properly. The default direction will be returned";
return TIGL_X_AXIS;
}

gp_XYZ cumulatedDepthDirection(0, 0, 0);
for (int i = 1; i <= wing.GetSegmentCount(); ++i) {
const CCPACSWingSegment& segment = wing.GetSegment(i);
gp_XYZ dirDepth = segment.GetChordPoint(0, 1).XYZ() - segment.GetChordPoint(0, 0).XYZ();
dirDepth = gp_XYZ(fabs(dirDepth.X()), fabs(dirDepth.Y()), fabs(dirDepth.Z()));
cumulatedDepthDirection += dirDepth;
}
const CCPACSWingSegment& outerSegment = wing.GetSegment(wing.GetSegmentCount());
gp_XYZ dirDepth = outerSegment.GetChordPoint(1, 1).XYZ() - outerSegment.GetChordPoint(1, 0).XYZ();
dirDepth = gp_XYZ(fabs(dirDepth.X()), fabs(dirDepth.Y()), fabs(dirDepth.Z()));
cumulatedDepthDirection += dirDepth;
gp_XYZ cumulatedDepthDirection = GetCumulatedLength(wing).depth;

switch (GetMajorDirection(wing)) {
switch (GetWingSpanAxis(wing)) {
case TIGL_Y_AXIS:
return cumulatedDepthDirection.X() >= cumulatedDepthDirection.Z() ? TiglAxis::TIGL_X_AXIS
: TiglAxis::TIGL_Z_AXIS;
Expand All @@ -70,7 +89,7 @@ TiglAxis GetDeepDirection(const tigl::CCPACSWing& wing)
}
}

TiglAxis GetMajorDirection(const CCPACSWing& wing)
TiglAxis GetWingSpanAxis(const CCPACSWing& wing)
{
if (!HasShape(wing)) {
LOG(WARNING) << "CTiglWingHelper::GetMajorDirection: This wing has no shape -> impossible to determine the "
Expand All @@ -88,21 +107,9 @@ TiglAxis GetMajorDirection(const CCPACSWing& wing)
default:
// heuristic to find the best major axis
// first find the deep axis , then chose the major axis between the two left axis
gp_XYZ cumulatedSpanDirection(0, 0, 0);
gp_XYZ cumulatedDepthDirection(0, 0, 0);
for (int i = 1; i <= wing.GetSegmentCount(); ++i) {
const CCPACSWingSegment& segment = wing.GetSegment(i);
gp_XYZ dirSpan = segment.GetChordPoint(1, 0).XYZ() - segment.GetChordPoint(0, 0).XYZ();
gp_XYZ dirDepth = segment.GetChordPoint(0, 1).XYZ() - segment.GetChordPoint(0, 0).XYZ();
dirSpan = gp_XYZ(fabs(dirSpan.X()), fabs(dirSpan.Y()), fabs(dirSpan.Z())); // why we use abs value?
dirDepth = gp_XYZ(fabs(dirDepth.X()), fabs(dirDepth.Y()), fabs(dirDepth.Z()));
cumulatedSpanDirection += dirSpan;
cumulatedDepthDirection += dirDepth;
}
const CCPACSWingSegment& outerSegment = wing.GetSegment(wing.GetSegmentCount());
gp_XYZ dirDepth = outerSegment.GetChordPoint(1, 1).XYZ() - outerSegment.GetChordPoint(1, 0).XYZ();
dirDepth = gp_XYZ(fabs(dirDepth.X()), fabs(dirDepth.Y()), fabs(dirDepth.Z()));
cumulatedDepthDirection += dirDepth;
auto length = GetCumulatedLength(wing);
gp_XYZ cumulatedSpanDirection = length.span;
gp_XYZ cumulatedDepthDirection = length.depth;

int depthIndex = 0;
if (cumulatedDepthDirection.X() >= cumulatedDepthDirection.Y() &&
Expand Down
29 changes: 23 additions & 6 deletions src/wing/TiglWingHelperFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,30 @@ namespace tigl
namespace winghelper
{

// Returns the deep direction of the wing
TiglAxis GetDeepDirection(const tigl::CCPACSWing& wing);
/**
* Returns the depth direction of the wing
*
* Note: this is a heuristic that is determined by the wing shape
* and its position in 3D space.
*
* TODO: In future we need do define on the cpacs basis the projection
* plane of the wing
*/
TiglAxis GetWingDepthAxis(const tigl::CCPACSWing& wing);

// Returns the major direction of the wing (correspond to the span direction)
// @Details: If a symmetry plan is set, the major direction is normal to the symmetry plan,
// otherwise, an heuristic is used to find out the best span axis candidate.
TiglAxis GetMajorDirection(const tigl::CCPACSWing& wing);
/**
* Returns the major direction of the wing (correspond to the span direction)
*
* @Details: If a symmetry plan is set, the major direction is normal to the symmetry plan,
* otherwise, an heuristic is used to find out the best span axis candidate.
*
* Note: this is a heuristic that is determined by the wing shape
* and its position in 3D space.
*
* TODO: In future we need do define on the cpacs basis the projection
* plane of the wing
*/
TiglAxis GetWingSpanAxis(const tigl::CCPACSWing& wing);

} // namespace winghelper

Expand Down
51 changes: 51 additions & 0 deletions tests/unittests/tiglWing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "tigl.h"
#include <string.h>
#include <CCPACSConfigurationManager.h>
#include <TiglWingHelperFunctions.h>

/******************************************************************************/

Expand Down Expand Up @@ -342,6 +343,56 @@ TEST_F(TiglWing, tiglGetAspectRatio)

}

TEST_F(TiglWing, spanDirection)
{
tigl::CCPACSConfigurationManager& manager = tigl::CCPACSConfigurationManager::GetInstance();
tigl::CCPACSConfiguration& config = manager.GetConfiguration(tiglHandle);

auto& wing = config.GetWing(1);
auto& vtp = config.GetWing(3);

auto axis = tigl::winghelper::GetWingSpanAxis(wing);
EXPECT_EQ(TIGL_Y_AXIS, axis);


axis = tigl::winghelper::GetWingSpanAxis(vtp);
EXPECT_EQ(TIGL_Z_AXIS, axis);

wing.SetSymmetryAxis(TIGL_NO_SYMMETRY);
vtp.SetSymmetryAxis(TIGL_X_Y_PLANE);

axis = tigl::winghelper::GetWingSpanAxis(wing);
EXPECT_EQ(TIGL_Y_AXIS, axis);

axis = tigl::winghelper::GetWingSpanAxis(vtp);
EXPECT_EQ(TIGL_Z_AXIS, axis);
}

TEST_F(TiglWing, depthDirection)
{
tigl::CCPACSConfigurationManager& manager = tigl::CCPACSConfigurationManager::GetInstance();
tigl::CCPACSConfiguration& config = manager.GetConfiguration(tiglHandle);

auto& wing = config.GetWing(1);
auto& vtp = config.GetWing(3);

auto axis = tigl::winghelper::GetWingDepthAxis(wing);
EXPECT_EQ(TIGL_X_AXIS, axis);


axis = tigl::winghelper::GetWingDepthAxis(vtp);
EXPECT_EQ(TIGL_X_AXIS, axis);

wing.SetSymmetryAxis(TIGL_NO_SYMMETRY);
vtp.SetSymmetryAxis(TIGL_X_Y_PLANE);

axis = tigl::winghelper::GetWingDepthAxis(wing);
EXPECT_EQ(TIGL_X_AXIS, axis);

axis = tigl::winghelper::GetWingDepthAxis(vtp);
EXPECT_EQ(TIGL_X_AXIS, axis);
}

TEST_F(WingSimple, wingGetMAC_success)
{
double c_1, x_1, y_1, z_1;
Expand Down

0 comments on commit b3c5ab4

Please sign in to comment.