Files
solvecraft/harness/cpp/shim/nlib_srf_common.cpp
T
bherbruck 4ad38d036b port: nlib.srf_common (NL_SrfCommon: corner, extrude, cyl/cone, ruled, revolved surfaces)
Rust port in nurbs::srf_common, C++ shim, ffi wrapper, difftest runner and
fixtures; 1550/1550 cases bit-identical.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-12 18:08:05 -04:00

205 lines
5.9 KiB
C++

// SPDX-License-Identifier: Apache-2.0
// difftest shim for unit `nlib.srf_common`.
// Wraps the common surface constructions of upstream source/SMLib/src/NL_SrfCommon.cpp
// owned by the unit: N_CreateSrfCornerPts, N_CreateSrfExtrudeCrv, N_CreateCylCone,
// N_CreateRuledSrf, N_CreateRevolvedSrf.
//
// One entry point, sc_nlib_srf_common_eval(op, in, n_in, out, cap, out_len): `op`
// selects the routine, `in` holds its curve(s) and parameters (layouts in
// crates/solidcore-ffi/src/units/nlib_srf_common.rs), `out` receives a flat record of
// doubles: the routine's status (0 = ok, else NL_ERROR.eno, cleared before the call;
// 1000 if the flag was set without a number), then every input curve as it is after
// the call (the routines change them in place), then on success the surface:
// n m p q r s, (n+1)*(m+1) control points x y z w (row i = u index), r+1 u knots,
// s+1 v knots. A curve is n p m, n+1 control points x y z w, m+1 knots. The output
// surface is a new NULL surface, as most upstream callers pass.
#include "sc_shim.h"
#include <nurbs.h>
#include <NL_Globals.h>
#include <vector>
namespace
{
double nl_status(NL_FLAG err)
{
if (err == NL_NO)
return 0.0;
return NL_ERROR.eno != 0 ? (double)NL_ERROR.eno : 1000.0;
}
NL_POINT pt(const double* d)
{
NL_POINT p;
p.x = d[0];
p.y = d[1];
p.z = d[2];
return p;
}
// Reads one curve at in[*at], allocating it on S; advances *at.
NL_CURVE* read_crv(const double* in, int64_t* at, NL_STACKS* S)
{
const double* d = in + *at;
NL_INDEX n = (NL_INDEX)d[0];
NL_DEGREE p = (NL_DEGREE)d[1];
NL_INDEX m = (NL_INDEX)d[2];
NL_CURVE* cur = N_AllocCrvAndArrays(n, p, m, S);
if (cur == NULL)
return NULL;
const double* c = d + 3;
NL_CPOINT* Pw;
NL_REAL* U;
N_CrvGetCPtsAndKnots(cur, &Pw, &U);
for (NL_INDEX i = 0; i <= n; i++)
{
Pw[i].x = c[0];
Pw[i].y = c[1];
Pw[i].z = c[2];
Pw[i].w = c[3];
c += 4;
}
for (NL_INDEX i = 0; i <= m; i++)
U[i] = *c++;
*at = (int64_t)(c - in);
return cur;
}
void dump_crv(std::vector<double>& o, NL_CURVE* cur)
{
NL_INDEX n, m;
NL_DEGREE p;
NL_CPOINT* Pw;
NL_REAL* U;
N_CrvGetCPtsDegreeAndKnots(cur, &n, &Pw, &p, &m, &U);
o.push_back((double)n);
o.push_back((double)p);
o.push_back((double)m);
for (NL_INDEX i = 0; i <= n; i++)
{
o.push_back(Pw[i].x);
o.push_back(Pw[i].y);
o.push_back(Pw[i].z);
o.push_back(Pw[i].w);
}
for (NL_INDEX i = 0; i <= m; i++)
o.push_back(U[i]);
}
void dump_srf(std::vector<double>& o, NL_SURFACE* sur)
{
NL_INDEX n, m, r, s;
NL_DEGREE p, q;
NL_CPOINT** Pw;
NL_REAL *U, *V;
N_SrfGetCPtsDegreesAndKnots(sur, &n, &m, &Pw, &p, &q, &r, &s, &U, &V);
o.push_back((double)n);
o.push_back((double)m);
o.push_back((double)p);
o.push_back((double)q);
o.push_back((double)r);
o.push_back((double)s);
for (NL_INDEX i = 0; i <= n; i++)
for (NL_INDEX j = 0; j <= m; j++)
{
o.push_back(Pw[i][j].x);
o.push_back(Pw[i][j].y);
o.push_back(Pw[i][j].z);
o.push_back(Pw[i][j].w);
}
for (NL_INDEX i = 0; i <= r; i++)
o.push_back(U[i]);
for (NL_INDEX j = 0; j <= s; j++)
o.push_back(V[j]);
}
} // namespace
SC_EXPORT int32_t sc_nlib_srf_common_eval(int32_t op, const double* in, int64_t n_in, double* out,
int64_t cap, int64_t* out_len)
{
(void)n_in;
SC_TRY
NL_STACKS S;
N_InitNurbs(&S);
std::vector<double> o;
int32_t rc = SC_OK;
NL_SURFACE* sur = N_AllocSrf(&S);
if (sur == NULL)
{
N_EndNurbs(&S);
return SC_ERR_ARG;
}
N_SrfInitArrays(sur);
N_ErrClear();
NL_FLAG err = NL_NO;
NL_CURVE* crv[2] = {NULL, NULL};
int ncrv = 0;
int64_t at = 0;
switch (op)
{
case 0: // N_CreateSrfCornerPts: P00 P10 P01 P11
err = N_CreateSrfCornerPts(pt(in), pt(in + 3), pt(in + 6), pt(in + 9), sur, &S);
break;
case 1: // N_CreateSrfExtrudeCrv: curve, W d dir
{
crv[0] = read_crv(in, &at, &S);
ncrv = 1;
if (crv[0] == NULL)
{
rc = SC_ERR_ARG;
break;
}
const double* d = in + at;
err = N_CreateSrfExtrudeCrv(crv[0], pt(d), d[3], (NL_FLAG)d[4], sur, &S);
break;
}
case 2: // N_CreateCylCone: C X Y rb rt as ae h ctp dir
err = N_CreateCylCone(pt(in), pt(in + 3), pt(in + 6), in[9], in[10], in[11], in[12],
in[13], (NL_FLAG)in[14], (NL_FLAG)in[15], sur, &S);
break;
case 3: // N_CreateRuledSrf: curveA, curveB, dir
{
crv[0] = read_crv(in, &at, &S);
crv[1] = crv[0] == NULL ? NULL : read_crv(in, &at, &S);
ncrv = 2;
if (crv[0] == NULL || crv[1] == NULL)
{
rc = SC_ERR_ARG;
break;
}
err = N_CreateRuledSrf(crv[0], crv[1], (NL_FLAG)in[at], sur, &S, &S);
break;
}
case 4: // N_CreateRevolvedSrf: curve, S T al ctp
{
crv[0] = read_crv(in, &at, &S);
ncrv = 1;
if (crv[0] == NULL)
{
rc = SC_ERR_ARG;
break;
}
const double* d = in + at;
err = N_CreateRevolvedSrf(crv[0], pt(d), pt(d + 3), d[6], (NL_FLAG)d[7], sur, &S);
break;
}
default:
rc = SC_ERR_ARG;
break;
}
if (rc == SC_OK)
{
o.push_back(nl_status(err));
for (int k = 0; k < ncrv; k++)
dump_crv(o, crv[k]);
if (err == NL_NO)
dump_srf(o, sur);
}
N_EndNurbs(&S);
if (rc != SC_OK)
return rc;
return sc_copy_out(o.data(), (int64_t)o.size(), out, cap, out_len);
SC_CATCH
}