Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## [Unreleased]
### Fixed
- The C API's flat B-rep arrays (`smlib_brep_export` / `smlib_brep_import`) carry a Ball corner's
`SmBallPatch` as itself. It used to be written as a bare NURBS and read back as an
`SmBSplineSurface`, losing its sphere, so a box rounded all round and then shelled failed after a
round trip (status 1034). Such a face is now surface kind `SMLIB_FLAT_SURFACE_BALL_PATCH` (6), with
its NURBS block as before and its sphere's centre and radius in the new arrays
`SMLIB_FLAT_FACE_DEFINITION_REAL_COUNT` (36) and `SMLIB_FLAT_FACE_DEFINITION_REALS` (166). Import
makes it an `SmBallPatch` only after certifying that the net lies on that sphere (`IsOnSphere`), and
refuses the B-rep otherwise. A reader that doesn't know kind 6 can take it as NURBS, but must write
arrays 36 and 166 back unchanged. ([smlib #102](https://github.com/tangency-labs/smlib/issues/102))

## [1.0.1] - 2026-10-21
### Added
- `SmApiUsdGeometryValidate.h`: run `SmBrep::AssertValid` on one BrepArray prim without changing the stage; `brep_geometry_validator` uses it.
Expand Down
13 changes: 13 additions & 0 deletions capi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ units and length rule. The conversion is `src/smlib_flat.cpp`, a USD-free port o
their circles as plain B-splines, so those export as NURBS. So do booleans' intersection curves.
- A cone with a negative semi-angle is rebuilt in the equivalent positive-angle form on import.
Its second export therefore differs from its first, and from then on the arrays are stable.
- Definitions (an addition to SmuConvert): a face whose surface is defined beyond its net carries
that definition. Today that means the Ball corner's `SmBallPatch`: surface kind
`SMLIB_FLAT_SURFACE_BALL_PATCH`, its NURBS block, range and UV trims as for a NURBS face, and its
sphere's centre and radius in `FACE_DEFINITION_REAL_COUNT` / `FACE_DEFINITION_REALS`, so its
offsets stay exact through a round trip. Import builds the `SmBallPatch` only once
`SmBallPatch::IsOnSphere` certifies the net on that sphere (the exact Bernstein identity per
Bezier piece, to rounding); otherwise it refuses the B-rep. USD's BrepArray does not carry
definitions yet.
- Attributes, materials and colours are not carried.
- Shell order (an addition to SmuConvert): SMLib takes a region's first shell as the one that
encloses it, and a body whose first shell is an inner one fails its check (test 12, "inner
Expand Down Expand Up @@ -413,6 +421,11 @@ a static build no longer instantiates every `SmTArray<T>` in every file (see bel
## Changes from upstream

This fork changes upstream SMLib (NVIDIA usd-brep, Apache-2.0) as follows:
- **Ball patches through the flat arrays** (`smlib_flat.cpp`, `SmBallPatch::IsOnSphere`;
smlib#102): export wrote a Ball corner's `SmBallPatch` as a bare NURBS and import read it back as
an `SmBSplineSurface`, losing the sphere. A box rounded all round then failed to shell after a
round trip. It is now its own surface kind with its sphere as its definition, and import certifies
the net on the sphere before taking it.
- **`SM_RAND_SEED`** (`SmConfig.h`, `SmFace.cpp`, `SmShell.cpp`): upstream seeds `rand()` from
the clock before its random sampling, so results could differ from run to run. This build
fixes the seed (`-DSMLIB_RAND_SEED=`; leave it empty for upstream's behaviour).
Expand Down
16 changes: 16 additions & 0 deletions capi/include/smlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ typedef struct smlib_flat smlib_flat;
* clockwise about its axis, or the circle made has another NURBS. */
#define SMLIB_FLAT_EDGE_NURBS_FORM 34
#define SMLIB_FLAT_WIRE_EDGE_NURBS_FORM 35
/* Per face whose kind carries a definition (SMLIB_FLAT_SURFACE_BALL_PATCH; later kinds of a
* surface defined beyond its net will too), in face order: how many reals of
* FACE_DEFINITION_REALS are its definition (a ball patch: 4). Length n_definition_faces, or 0
* (no such face). */
#define SMLIB_FLAT_FACE_DEFINITION_REAL_COUNT 36

/* --- double arrays --- */
/* The B-rep's intersection tolerance (twice its largest face/edge/vertex tolerance). Length 1. */
Expand Down Expand Up @@ -482,6 +487,9 @@ typedef struct smlib_flat smlib_flat;
* only it then carries more. */
#define SMLIB_FLAT_EDGE_TOLERANCE 164
#define SMLIB_FLAT_VERTEX_TOLERANCE 165
/* The definitions of the faces counted in FACE_DEFINITION_REAL_COUNT, packed in face order
* (sum(FACE_DEFINITION_REAL_COUNT)). A ball patch: its sphere's center x, y, z and radius. */
#define SMLIB_FLAT_FACE_DEFINITION_REALS 166

/* FACE_SURFACE_TYPE values. */
#define SMLIB_FLAT_SURFACE_NURBS 0
Expand All @@ -490,6 +498,14 @@ typedef struct smlib_flat smlib_flat;
#define SMLIB_FLAT_SURFACE_CONE 3
#define SMLIB_FLAT_SURFACE_SPHERE 4
#define SMLIB_FLAT_SURFACE_TORUS 5
/* A rational patch on a sphere, given by its net and its sphere (SMLib's SmBallPatch, the corner
* of three equal rounds): its NURBS block, range and UV trims as a NURBS face's (a reader that
* does not know the kind may take it as one); its definition (FACE_DEFINITION_REAL_COUNT 4,
* FACE_DEFINITION_REALS the sphere's center and radius) keeps the sphere, whose offsets are exact
* and collapse when they pass its center. Import makes the face that patch only when every point
* of its net is on that sphere (SmBallPatch::IsOnSphere, decided on the net to rounding), and
* refuses the B-rep (status 1007, SM_ERR_INVALID_INPUT) otherwise. */
#define SMLIB_FLAT_SURFACE_BALL_PATCH 6

/* EDGE_CURVE_TYPE and WIRE_EDGE_CURVE_TYPE values. Export writes LINE, CIRCLE and ELLIPSE for
* SMLib's SmLine, SmCircle and SmEllipse curves and NURBS for everything else. */
Expand Down
107 changes: 97 additions & 10 deletions capi/src/smlib_flat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <SmRegion.h>
#include <SmShell.h>
#include <SmSphere.h>
#include <SmBallPatch.h>
#include <SmSurface.h>
#include <SmTol.h>
#include <SmTorus.h>
Expand Down Expand Up @@ -97,6 +98,7 @@ enum : int64_t
kSurfaceCone = SMLIB_FLAT_SURFACE_CONE,
kSurfaceSphere = SMLIB_FLAT_SURFACE_SPHERE,
kSurfaceTorus = SMLIB_FLAT_SURFACE_TORUS,
kSurfaceBallPatch = SMLIB_FLAT_SURFACE_BALL_PATCH,
kCurveNurbs = SMLIB_FLAT_CURVE_NURBS,
kCurveLine = SMLIB_FLAT_CURVE_LINE,
kCurveCircle = SMLIB_FLAT_CURVE_CIRCLE,
Expand All @@ -112,7 +114,19 @@ bool EdgeHasForm(const std::vector<int64_t> &form, size_t iEdge)

bool FaceHasNurbs(const FlatBrep &F, size_t iFace)
{
return F.face_surface_type[iFace] == kSurfaceNurbs || (!F.face_nurbs_form.empty() && F.face_nurbs_form[iFace] == 1);
return F.face_surface_type[iFace] == kSurfaceNurbs || F.face_surface_type[iFace] == kSurfaceBallPatch ||
(!F.face_nurbs_form.empty() && F.face_nurbs_form[iFace] == 1);
}

// The kinds that carry a definition (FACE_DEFINITION_REAL_COUNT, FACE_DEFINITION_REALS), and how
// many reals each has: a ball patch its sphere's center and radius.
// A ball patch's net is on its sphere when |X - C W|^2 - R^2 W^2 is zero to this much of the
// patch's size squared (SmBallPatch::IsOnSphere): rounding at the part's scale.
const double kBallPatchRelTol = 1e-12;

int64_t DefinitionReals(int64_t tKind)
{
return tKind == kSurfaceBallPatch ? 4 : 0;
}

// USD writes USDBREP_NO_OBJECT_INDEX (9999999) for "no vertex"; the flat arrays use -1.
Expand Down Expand Up @@ -1531,6 +1545,10 @@ SmStatus FlatFromBrep(const SmBrep &crBrep, FlatBrep &F)
{
tFaceSurfaceType = kSurfaceSphere;
}
else if (pFace->GetSurface()->IsKindOf(SmBallPatch_TYPE))
{
tFaceSurfaceType = kSurfaceBallPatch;
}
else if (pFace->GetSurface()->IsKindOf(SmPlane_TYPE))
{
tFaceSurfaceType = kSurfacePlane;
Expand All @@ -1551,7 +1569,9 @@ SmStatus FlatFromBrep(const SmBrep &crBrep, FlatBrep &F)
// A curved analytic face is also given with its NURBS form (FACE_NURBS_FORM), the NURBS
// SMLib works on: its range and UV trims are in that NURBS's parameters, as SMLib holds
// them. A plane's are in its own (u, v), which carries no trims.
const bool bNurbsForm = tFaceSurfaceType != kSurfaceNurbs && tFaceSurfaceType != kSurfacePlane;
const bool bNurbsForm = tFaceSurfaceType != kSurfaceNurbs && tFaceSurfaceType != kSurfacePlane && tFaceSurfaceType != kSurfaceBallPatch;
// A ball patch's NURBS block is its own, as a NURBS face's.
const bool bOwnNurbs = tFaceSurfaceType == kSurfaceNurbs || tFaceSurfaceType == kSurfaceBallPatch;
// A plane's trims are in its NURBS (u, v); the arrays' plane (u, v) are distances along
// its unit axes (its STEP parameters scaled by GetUVScale), an affine image of them.
std::array<double, 6> sPlaneMap{};
Expand All @@ -1578,7 +1598,7 @@ SmStatus FlatFromBrep(const SmBrep &crBrep, FlatBrep &F)
{
return SM_ERR; // "face loop index is out of bounds"
}
sLoopExportsUVCurves[lLoopIndex] = tFaceSurfaceType == kSurfaceNurbs || bNurbsForm || bPlaneMap;
sLoopExportsUVCurves[lLoopIndex] = bOwnNurbs || bNurbsForm || bPlaneMap;
sLoopPlaneMap[lLoopIndex] = sPlaneMap;
sLoopHasPlaneMap[lLoopIndex] = bPlaneMap;
}
Expand All @@ -1597,7 +1617,7 @@ SmStatus FlatFromBrep(const SmBrep &crBrep, FlatBrep &F)
const SmExtent2d &sFaceRangeDomain = rFaceData.m_vUVDomain;
double sFaceUVMin[2] = {sFaceRangeDomain.GetUMin(), sFaceRangeDomain.GetVMin()};
double sFaceUVMax[2] = {sFaceRangeDomain.GetUMax(), sFaceRangeDomain.GetVMax()};
if (tFaceSurfaceType != kSurfaceNurbs && !bNurbsForm)
if (!bOwnNurbs && !bNurbsForm)
{
SmExtent2d sStepDom = pFace->GetSurface()->GetSTEPUVDomain();
double dUMin = sStepDom.GetUMin();
Expand Down Expand Up @@ -1752,6 +1772,19 @@ SmStatus FlatFromBrep(const SmBrep &crBrep, FlatBrep &F)
return s;
}
}
else if (tFaceSurfaceType == kSurfaceBallPatch)
{
// its net, then its definition: the sphere it is on
const SmBallPatch *pBall = static_cast<const SmBallPatch *>(pSurface);
SmStatus s = smu_AddSmBSplineSurface(*pBall, F);
if (s != SM_SUCCESS)
{
return s;
}
const SmPoint3d &sCenter = pBall->GetCentre();
F.face_definition_real_count.push_back(DefinitionReals(kSurfaceBallPatch));
F.face_definition_reals.insert(F.face_definition_reals.end(), {sCenter.x, sCenter.y, sCenter.z, pBall->GetRadius()});
}
else if (tFaceSurfaceType == kSurfaceNurbs)
{
// NURB or approximation path
Expand Down Expand Up @@ -2313,7 +2346,7 @@ bool Validate(const FlatBrep &F, Shape &S)

// Faces
if (F.face_loop_count.size() != S.faces || F.face_surface_type.size() != S.faces || F.face_trim_type.size() != S.faces ||
F.face_range.size() != 4 * S.faces || !Bits(F.face_trim_type) || !InRange(F.face_surface_type, kSurfaceNurbs, kSurfaceTorus) ||
F.face_range.size() != 4 * S.faces || !Bits(F.face_trim_type) || !InRange(F.face_surface_type, kSurfaceNurbs, kSurfaceBallPatch) ||
!InRange(F.face_loop_count, 1, kMaxTopology))
{
return false;
Expand All @@ -2326,7 +2359,8 @@ bool Validate(const FlatBrep &F, Shape &S)
}
for (size_t i = 0; i < S.faces; ++i)
{
if (F.face_nurbs_form[i] == 1 && (F.face_surface_type[i] == kSurfaceNurbs || F.face_surface_type[i] == kSurfacePlane))
if (F.face_nurbs_form[i] == 1 &&
(F.face_surface_type[i] == kSurfaceNurbs || F.face_surface_type[i] == kSurfacePlane || F.face_surface_type[i] == kSurfaceBallPatch))
{
return false;
}
Expand Down Expand Up @@ -2529,7 +2563,7 @@ bool Validate(const FlatBrep &F, Shape &S)
}

// Surfaces
size_t nT[6] = {0, 0, 0, 0, 0, 0};
size_t nT[7] = {0, 0, 0, 0, 0, 0, 0};
for (int64_t t : F.face_surface_type)
{
nT[static_cast<size_t>(t)]++;
Expand All @@ -2541,6 +2575,41 @@ bool Validate(const FlatBrep &F, Shape &S)
}
const size_t nP = nT[kSurfacePlane], nCy = nT[kSurfaceCylinder], nCo = nT[kSurfaceCone], nS = nT[kSurfaceSphere],
nTo = nT[kSurfaceTorus];
// Definitions: one count per face whose kind carries one, its kind's; then its reals, finite
// (a ball patch's radius > 0).
{
size_t nD = 0, at = 0;
for (size_t i = 0; i < S.faces; ++i)
{
const int64_t want = DefinitionReals(F.face_surface_type[i]);
if (want == 0)
{
continue;
}
if (nD >= F.face_definition_real_count.size() || F.face_definition_real_count[nD] != want ||
at + static_cast<size_t>(want) > F.face_definition_reals.size())
{
return false;
}
for (int64_t k = 0; k < want; ++k)
{
if (!std::isfinite(F.face_definition_reals[at + static_cast<size_t>(k)]))
{
return false;
}
}
if (F.face_surface_type[i] == kSurfaceBallPatch && !(F.face_definition_reals[at + 3] > 0.0))
{
return false;
}
nD++;
at += static_cast<size_t>(want);
}
if (F.face_definition_real_count.size() != nD || F.face_definition_reals.size() != at)
{
return false;
}
}
if (F.face_nurbs_u_vertex_count.size() != nN || F.face_nurbs_v_vertex_count.size() != nN || F.face_nurbs_u_order.size() != nN ||
F.face_nurbs_v_order.size() != nN)
{
Expand Down Expand Up @@ -3083,13 +3152,13 @@ SmStatus FlatToBrepData(const FlatBrep &F, const Shape &S, const SmContext &crSm
pData->m_vFaces.SetSize(static_cast<ULONG>(S.faces));
ULONG lLoopIndex = 0;
Cursor sNurbsSurfaces;
size_t iSphere = 0, iPlane = 0, iCylinder = 0, iCone = 0, iTorus = 0;
size_t iSphere = 0, iPlane = 0, iCylinder = 0, iCone = 0, iTorus = 0, iBallPatch = 0;
for (ULONG iLocal = 0; iLocal < S.faces; iLocal++)
{
SmFaceData &rFaceData = pData->m_vFaces[iLocal];
rFaceData.ReSet();
const int64_t tSurfaceType = F.face_surface_type[iLocal];
const bool bNurbsForm = tSurfaceType != kSurfaceNurbs && FaceHasNurbs(F, iLocal);
const bool bNurbsForm = tSurfaceType != kSurfaceNurbs && tSurfaceType != kSurfaceBallPatch && FaceHasNurbs(F, iLocal);

// Mapping face:trimType -> SMLib m_bRectangularTrim (tri-state):
// "rectangular" -> TRUE (definitively naturally/rectangularly trimmed)
Expand Down Expand Up @@ -3277,6 +3346,24 @@ SmStatus FlatToBrepData(const FlatBrep &F, const Shape &S, const SmContext &crSm
stat = SmTorus::CreateCanonical(ctx, sPlacement, F.face_torus_major_radius[i], F.face_torus_minor_radius[i], pTorus);
pNewSurface = pTorus;
}
else if (tSurfaceType == kSurfaceBallPatch)
{
// its net, taken as the patch on its definition's sphere only when the net is on it
const size_t at = 4 * iBallPatch++;
const SmPoint3d sCenter(F.face_definition_reals[at], F.face_definition_reals[at + 1], F.face_definition_reals[at + 2]);
const double dRadius = F.face_definition_reals[at + 3];
SmBSplineSurface *pNet = smu_CreateSmBSplineSurface3d(ctx, F, sNurbsSurfaces);
SmObjDelete sNetCleanup(pNet);
SmBoolean bOn = FALSE;
SmBallPatch *pBall = NULL;
stat = SM_ERR_INVALID_INPUT;
if (pNet != NULL && SmBallPatch::IsOnSphere(*pNet, sCenter, dRadius, kBallPatchRelTol, bOn) == SM_SUCCESS && bOn == TRUE &&
SmBallPatch::Create(ctx, *pNet, sCenter, dRadius, pBall) == SM_SUCCESS)
{
stat = SM_SUCCESS;
}
pNewSurface = pBall;
}
else // NURBS
{
pNewSurface = smu_CreateSmBSplineSurface3d(ctx, F, sNurbsSurfaces);
Expand All @@ -3290,7 +3377,7 @@ SmStatus FlatToBrepData(const FlatBrep &F, const Shape &S, const SmContext &crSm
return stat != SM_SUCCESS ? stat : SM_ERR;
}

const bool bAnalyticSurface = tSurfaceType != kSurfaceNurbs && !bNurbsForm;
const bool bAnalyticSurface = tSurfaceType != kSurfaceNurbs && tSurfaceType != kSurfaceBallPatch && !bNurbsForm;
if (bAnalyticSurface && sDomain.IsBounded())
{
SmStatus adjStat = pNewSurface->AdjustSTEPUVDomain(sDomain);
Expand Down
4 changes: 3 additions & 1 deletion capi/src/smlib_flat.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
I(SMLIB_FLAT_FACE_NURBS_FORM, face_nurbs_form) \
I(SMLIB_FLAT_EDGE_NURBS_FORM, edge_nurbs_form) \
I(SMLIB_FLAT_WIRE_EDGE_NURBS_FORM, wire_edge_nurbs_form) \
I(SMLIB_FLAT_FACE_DEFINITION_REAL_COUNT, face_definition_real_count) \
F(SMLIB_FLAT_TOLERANCE, tolerance) \
F(SMLIB_FLAT_EXTENT, extent) \
F(SMLIB_FLAT_FACE_RANGE, face_range) \
Expand Down Expand Up @@ -121,7 +122,8 @@
F(SMLIB_FLAT_FACE_TORUS_MAJOR_RADIUS, face_torus_major_radius) \
F(SMLIB_FLAT_FACE_TORUS_MINOR_RADIUS, face_torus_minor_radius) \
F(SMLIB_FLAT_EDGE_TOLERANCE, edge_tolerance) \
F(SMLIB_FLAT_VERTEX_TOLERANCE, vertex_tolerance)
F(SMLIB_FLAT_VERTEX_TOLERANCE, vertex_tolerance) \
F(SMLIB_FLAT_FACE_DEFINITION_REALS, face_definition_reals)

namespace smlib_flat_impl
{
Expand Down
8 changes: 4 additions & 4 deletions capi/tests/flat_roundtrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ std::string dir = ".";
} \
} while (0)

// Every id: int64 ids 1..35, double ids 101..165.
// Every id: int64 ids 1..36, double ids 101..166.
std::vector<int32_t> IntIds()
{
std::vector<int32_t> v;
for (int32_t id = SMLIB_FLAT_REGION_SHELL_COUNT; id <= SMLIB_FLAT_WIRE_EDGE_NURBS_FORM; ++id)
for (int32_t id = SMLIB_FLAT_REGION_SHELL_COUNT; id <= SMLIB_FLAT_FACE_DEFINITION_REAL_COUNT; ++id)
{
v.push_back(id);
}
Expand All @@ -63,7 +63,7 @@ std::vector<int32_t> IntIds()
std::vector<int32_t> RealIds()
{
std::vector<int32_t> v;
for (int32_t id = SMLIB_FLAT_TOLERANCE; id <= SMLIB_FLAT_VERTEX_TOLERANCE; ++id)
for (int32_t id = SMLIB_FLAT_TOLERANCE; id <= SMLIB_FLAT_FACE_DEFINITION_REALS; ++id)
{
v.push_back(id);
}
Expand Down Expand Up @@ -1981,7 +1981,7 @@ int main(int argc, char **argv)
int64_t n = 0;
const int64_t one = 1;
const double half = 0.5;
for (int32_t id : {0, 36, 100, 166, -1, 1000})
for (int32_t id : {0, 37, 100, 167, -1, 1000})
{
CHECK(smlib_flat_get_i64(f, id, &pi, &n) == SMLIB_ERR_ARG);
CHECK(smlib_flat_get_f64(f, id, &pf, &n) == SMLIB_ERR_ARG);
Expand Down
Loading
Loading