Skip to content
Open
10 changes: 10 additions & 0 deletions scripts/run_all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ geomPipeline.py ../data/test_geometry/test_geometry.stp -o test_geometry_result.
geomPipeline.py check ../data/test_geometry/test_geometry_manifest.json --verbosity WARNING
if [ ! $? -eq 0 ] ; then echo "Error during geometry test" ; exit 1 ; fi

# test scaling, output to a diff output folder
geomPipeline.py imprint ../data/test_geometry/test_geometry.stp -o /tmp/test_geometry_scaled.brep --output-unit M
geomPipeline.py imprint ../data/test_geometry/test_geometry.stp --working-dir /tmp/ -o test_geometry_scaled.stp --output-unit M
if [ ! $? -eq 0 ] ; then echo "Error during geometry output unit (scaling) test " ; exit 1 ; fi

# test single thread mode, only for non-coupled operations
geomPipeline.py check ../data/test_geometry/test_geometry.stp --thread-count 1 --verbosity WARNING
if [ ! $? -eq 0 ] ; then echo "Error during geometry test in single thread mode" ; exit 1 ; fi

# test_*.py has not been installed by package, so must be copied into this place
# todo: pytest to auto discover tests
cp ../../src/python/*.py ./
if [ $? -eq 0 ]
then
Expand All @@ -62,6 +68,10 @@ then
if [ $? -eq 0 ]; then
test_collision.py
fi

if [ $? -eq 0 ]; then
test_inscribedShape.py
fi
echo "test completed"
else
echo "geometry pipeline test failed"
Expand Down
1 change: 1 addition & 0 deletions src/Geom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include_directories("third-party/SGeom/src/GEOMAlgo")
set(MyGeom_SOURCES
"OccUtils.cpp"
#"GeometryFixer.cpp"
"InscribedShapeBuilder.cpp"
"CollisionDetector.cpp"
"Geom.cpp"
"OpenCascadeAll.cpp"
Expand Down
3 changes: 3 additions & 0 deletions src/Geom/Geom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "GeometryPropertyBuilder.h"
#include "GeometrySearchBuilder.h"
#include "GeometryShapeChecker.h"
#include "InscribedShapeBuilder.h"

#include "GeometryReader.h"
#include "GeometryWriter.h"
Expand All @@ -37,6 +38,7 @@ TYPESYSTEM_SOURCE(Geom::GeometryShapeChecker, Geom::GeometryProcessor);
TYPESYSTEM_SOURCE(Geom::GeometryPropertyBuilder, Geom::GeometryProcessor);
TYPESYSTEM_SOURCE(Geom::GeometrySearchBuilder, Geom::GeometryProcessor);
TYPESYSTEM_SOURCE(Geom::BoundBoxBuilder, Geom::GeometryProcessor);
TYPESYSTEM_SOURCE(Geom::InscribedShapeBuilder, Geom::GeometryProcessor);

TYPESYSTEM_SOURCE(Geom::CollisionDetector, Geom::GeometryProcessor);
TYPESYSTEM_SOURCE(Geom::GeometryImprinter, Geom::CollisionDetector);
Expand All @@ -60,6 +62,7 @@ namespace Geom
GeometryPropertyBuilder::init();
GeometrySearchBuilder::init();
BoundBoxBuilder::init();
InscribedShapeBuilder::init();
GeometryShapeChecker::init();
CollisionDetector::init();
GeometryImprinter::init();
Expand Down
4 changes: 2 additions & 2 deletions src/Geom/GeometryPropertyBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ namespace Geom
{
// max and min volume check!
double max_volume = 0;
double min_volume = 1e100;
double max_volume_threshold = 1e16; // todo: get from config
double min_volume = 1e100; // unit is mm^3
double max_volume_threshold = 1e16; // todo: get from config parameter
for (size_t i = 0; i < myInputData->itemCount(); i++)
{
const auto& p = myGeometryProperties[i];
Expand Down
26 changes: 24 additions & 2 deletions src/Geom/GeometryTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ inline void from_json(const nlohmann::json& j, Bnd_Box& b)
b.Update(v[0], v[1], v[2], v[3], v[4], v[5]);
}

inline void to_json(nlohmann::json& j, const Bnd_Sphere& b)
{
gp_XYZ c = b.Center();
j = nlohmann::json{c.X(), c.Y(), c.Z(), b.Radius()};
}

/// OpenCASCADE has T::DumpJson(Standard_OStream & theOStream, Standard_Integer theDepth = -1 )
/// T::InitFromJson(const Standard_SStream & theSStream, Standard_Integer & theStreamPos)

/// RGBA float array, conmponent value range [0, 1.0]
inline void to_json(nlohmann::json& j, const Quantity_Color& p)
{
Expand All @@ -48,11 +57,16 @@ namespace Geom
typedef Standard_Integer ItemHashType;
static const ItemHashType ItemHashMax = INT_MAX;

typedef std::uint64_t UniqueIdType; // also define in PPP/UniqueId.h
typedef std::uint64_t UniqueIdType; /// defined in PPP/UniqueId.h
/// map has order (non contiguous in memory), can increase capacity
typedef MapType<ItemHashType, TopoDS_Shape> ItemContainerType;
typedef std::shared_ptr<ItemContainerType> ItemContainerPType;

typedef gp_Pnt PointType;
/// conventional C enum starting from zero, can be used as array index
typedef GeomAbs_SurfaceType SurfaceType;
const size_t SurfacTypeCount = 11; /// total count of SurfaceType enum elements

/**
* from OCCT to FreeCAD style better enum name
* integer value: ShapeType == TopAbs_ShapeEnum; but NOT compatible
Expand Down Expand Up @@ -152,7 +166,7 @@ namespace Geom
return ShapeErrorType::NoError;
}

class CollisionInfo
struct CollisionInfo
{
public:
ItemIndexType first;
Expand All @@ -161,6 +175,14 @@ namespace Geom
CollisionType type;

CollisionInfo() = default;
// C++20 prevents conversion form <brace-enclosed initializer list> to this type
CollisionInfo(ItemIndexType _first, ItemIndexType _second, double _value, CollisionType _type)
: first(_first)
, second(_second)
, value(_value)
, type(_type)
{
}
};
inline void to_json(json& j, const CollisionInfo& p)
{
Expand Down
49 changes: 43 additions & 6 deletions src/Geom/GeometryWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,26 @@ namespace Geom
LOG_F(INFO, "%s", sout.str().c_str());
}

// TODO: query inputData() metadata for inputUnit
/// return 1 which means no scaling is needed
/// not quite useful for
double calcOutputScale(const std::string& outputUnit)
{
const std::string inputUnit = "MM";
if (outputUnit == inputUnit)
return 1;
else if ("MM" == inputUnit && outputUnit == "M")
return 0.001;
else
{
LOG_F(ERROR, "scaling for input length unit %s and output unit %s is not supported", inputUnit.c_str(),
outputUnit.c_str());
return 1;
}
}

/// can export floating shapes which can not be compoSolid
void exportCompound(const std::string& file_name)
void exportCompound(const std::string& file_name, double scale = 1.0)
{
summary();

Expand All @@ -122,6 +140,10 @@ namespace Geom
LOG_F(INFO, "result is not merged (duplicated face removed) for result brep file");
finalShape = OccUtils::createCompound(*mySolids, myShapeErrors);
}

if (scale != 1) // double type has integer value can be compared with integer by ==
finalShape = OccUtils::scaleShape(finalShape, scale);

/// NOTE: exportMetaData() is done in the second GeometryPropertyBuilder processor
BRepTools::Write(finalShape, file_name.c_str()); // progress reporter can be the last arg
}
Expand All @@ -131,15 +153,19 @@ namespace Geom
* */
bool exportGeometry(const std::string file_name)
{
std::string defaultLengthUnit = "MM";
auto outputUnit = parameterValue<std::string>("outputUnit", defaultLengthUnit);
double scale = calcOutputScale(outputUnit);

if (Utilities::hasFileExt(file_name, "brp") || Utilities::hasFileExt(file_name, "brep"))
{
exportCompound(file_name);
exportCompound(file_name, scale);
return true;
}
else if (Utilities::hasFileExt(file_name, "stp") || Utilities::hasFileExt(file_name, "step"))
{
// LOG_F(INFO, "export Dataset pointed by member hDoc");
Handle(TDocStd_Document) aDoc = createDocument();
Handle(TDocStd_Document) aDoc = createDocument(scale);

/// the user can work with an already prepared WorkSession or create a new one
Standard_Boolean scratch = Standard_False;
Expand All @@ -150,15 +176,23 @@ namespace Geom
// recommended value, others shape mode are available
// Interface_Static::SetCVal("write.step.schema", "AP214IS");
Interface_Static::SetIVal("write.step.assembly", 1); // global variable
// Interface_Static::SetIVal ("write.step.nonmanifold", 1);
// "write.precision.val" = 0.0001 is the default value

if (outputUnit != defaultLengthUnit)
{
Interface_Static::SetCVal("xstep.cascade.unit", outputUnit.c_str());
Interface_Static::SetCVal("write.step.unit", outputUnit.c_str());
// all vertex coordinate will not be scaled during writing out, change unit,
// but it causes scaling at reading back
}

STEPCAFControl_Writer writer(WS, scratch);
// this writer contains a STEPControl_Writer class, not by inheritance
// writer.SetColorMode(mode);
if (!writer.Transfer(aDoc, mode))
{
LOG_F(ERROR, "The Dataset cannot be translated or gives no result");
// abandon ..
}

IFSelect_ReturnStatus stat = writer.Write(file_name.c_str());
Expand All @@ -173,7 +207,7 @@ namespace Geom
return false;
}

Handle(TDocStd_Document) createDocument()
Handle(TDocStd_Document) createDocument(double scale = 1)
{

Handle(XCAFApp_Application) hApp = XCAFApp_Application::GetApplication();
Expand All @@ -194,7 +228,10 @@ namespace Geom
for (auto& item : *mySolids)
{
TDF_Label partLabel = shapeTool->NewShape();
shapeTool->SetShape(partLabel, item.second);
if (scale != 1)
shapeTool->SetShape(partLabel, OccUtils::scaleShape(item.second, scale));
else
shapeTool->SetShape(partLabel, item.second);
colorTool->SetColor(partLabel, (*myColorMap)[item.first], XCAFDoc_ColorGen);
TDataStd_Name::Set(partLabel, TCollection_ExtendedString((*myNameMap)[item.first].c_str(), true));
/// Material not yet supported
Expand Down
Loading