Loading files
3d097a032This file is pinned to the selected release. Download the original to open it in your design tool.
// Copyright (c) 2025 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <gtest/gtest.h>
#include <Geom_Ellipse.hxx>
#include <GeomGridEval_Ellipse.hxx>
#include <gp_Ax2.hxx>
#include <gp_Pnt.hxx>
#include <NCollection_Array1.hxx>
#include <cmath>
namespace
{
const double THE_TOLERANCE = 1e-10;
NCollection_Array1<double> CreateUniformParams(double theFirst, double theLast, int theNbPoints)
{
NCollection_Array1<double> aParams(1, theNbPoints);
const double aStep = (theLast - theFirst) / (theNbPoints - 1);
for (int i = 1; i <= theNbPoints; ++i)
{
aParams.SetValue(i, theFirst + (i - 1) * aStep);
}
return aParams;
}
} // namespace
TEST(GeomGridEval_EllipseTest, BasicEvaluation)
{
// Ellipse in XY plane, Major=3, Minor=2, center at origin
occ::handle<Geom_Ellipse> anEllipse =
new Geom_Ellipse(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 3.0, 2.0);
GeomGridEval_Ellipse anEval(anEllipse);
EXPECT_FALSE(anEval.Geometry().IsNull());
// Test at 0, PI/2, PI, 3PI/2, 2PI
NCollection_Array1<double> aParams(1, 5);
aParams.SetValue(1, 0.0);
aParams.SetValue(2, M_PI / 2);
aParams.SetValue(3, M_PI);
aParams.SetValue(4, 3 * M_PI / 2);
aParams.SetValue(5, 2 * M_PI);
NCollection_Array1<gp_Pnt> aGrid = anEval.EvaluateGrid(aParams);
// u=0: (3, 0, 0)
EXPECT_NEAR(aGrid.Value(1).X(), 3.0, THE_TOLERANCE);
EXPECT_NEAR(aGrid.Value(1).Y(), 0.0, THE_TOLERANCE);
// u=PI/2: (0, 2, 0)
EXPECT_NEAR(aGrid.Value(2).X(), 0.0, THE_TOLERANCE);
EXPECT_NEAR(aGrid.Value(2).Y(), 2.0, THE_TOLERANCE);
// u=PI: (-3, 0, 0)
EXPECT_NEAR(aGrid.Value(3).X(), -3.0, THE_TOLERANCE);
EXPECT_NEAR(aGrid.Value(3).Y(), 0.0, THE_TOLERANCE);
// u=3PI/2: (0, -2, 0)
EXPECT_NEAR(aGrid.Value(4).X(), 0.0, THE_TOLERANCE);
EXPECT_NEAR(aGrid.Value(4).Y(), -2.0, THE_TOLERANCE);
// u=2PI: back to (3, 0, 0)
EXPECT_NEAR(aGrid.Value(5).X(), 3.0, THE_TOLERANCE);
EXPECT_NEAR(aGrid.Value(5).Y(), 0.0, THE_TOLERANCE);
}
TEST(GeomGridEval_EllipseTest, DerivativeD1)
{
occ::handle<Geom_Ellipse> anEllipse =
new Geom_Ellipse(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 3.0, 2.0);
GeomGridEval_Ellipse anEval(anEllipse);
NCollection_Array1<double> aParams = CreateUniformParams(0.0, 2 * M_PI, 9);
NCollection_Array1<GeomGridEval::CurveD1> aGrid = anEval.EvaluateGridD1(aParams);
for (int i = 1; i <= 9; ++i)
{
gp_Pnt aPnt;
gp_Vec aD1;
anEllipse->D1(aParams.Value(i), aPnt, aD1);
EXPECT_NEAR(aGrid.Value(i).Point.Distance(aPnt), 0.0, THE_TOLERANCE);
EXPECT_NEAR((aGrid.Value(i).D1 - aD1).Magnitude(), 0.0, THE_TOLERANCE);
}
}
TEST(GeomGridEval_EllipseTest, DerivativeD2)
{
occ::handle<Geom_Ellipse> anEllipse =
new Geom_Ellipse(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 3.0, 2.0);
GeomGridEval_Ellipse anEval(anEllipse);
NCollection_Array1<double> aParams = CreateUniformParams(0.0, 2 * M_PI, 9);
NCollection_Array1<GeomGridEval::CurveD2> aGrid = anEval.EvaluateGridD2(aParams);
for (int i = 1; i <= 9; ++i)
{
gp_Pnt aPnt;
gp_Vec aD1, aD2;
anEllipse->D2(aParams.Value(i), aPnt, aD1, aD2);
EXPECT_NEAR(aGrid.Value(i).Point.Distance(aPnt), 0.0, THE_TOLERANCE);
EXPECT_NEAR((aGrid.Value(i).D1 - aD1).Magnitude(), 0.0, THE_TOLERANCE);
EXPECT_NEAR((aGrid.Value(i).D2 - aD2).Magnitude(), 0.0, THE_TOLERANCE);
}
}
TEST(GeomGridEval_EllipseTest, DerivativeD3)
{
occ::handle<Geom_Ellipse> anEllipse =
new Geom_Ellipse(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 3.0, 2.0);
GeomGridEval_Ellipse anEval(anEllipse);
NCollection_Array1<double> aParams = CreateUniformParams(0.0, 2 * M_PI, 9);
NCollection_Array1<GeomGridEval::CurveD3> aGrid = anEval.EvaluateGridD3(aParams);
for (int i = 1; i <= 9; ++i)
{
gp_Pnt aPnt;
gp_Vec aD1, aD2, aD3;
anEllipse->D3(aParams.Value(i), aPnt, aD1, aD2, aD3);
EXPECT_NEAR(aGrid.Value(i).Point.Distance(aPnt), 0.0, THE_TOLERANCE);
EXPECT_NEAR((aGrid.Value(i).D1 - aD1).Magnitude(), 0.0, THE_TOLERANCE);
EXPECT_NEAR((aGrid.Value(i).D2 - aD2).Magnitude(), 0.0, THE_TOLERANCE);
EXPECT_NEAR((aGrid.Value(i).D3 - aD3).Magnitude(), 0.0, THE_TOLERANCE);
}
}