working hardcode line pair

master
howard 2021-03-21 14:39:59 -07:00
parent ec59e95b87
commit cf69fee366
8 changed files with 551 additions and 208 deletions

1
dist/CDemo.js vendored

File diff suppressed because one or more lines are too long

BIN
dist/CDemo.wasm vendored

Binary file not shown.

2
dist/bundle.js vendored

File diff suppressed because one or more lines are too long

137
dist/solver.js vendored
View File

@ -736,10 +736,6 @@ function cwrap(ident, returnType, argTypes, opts) {
// We used to include malloc/free by default in the past. Show a helpful error in // We used to include malloc/free by default in the past. Show a helpful error in
// builds with assertions. // builds with assertions.
function _free() {
// Show a helpful error since we used to include free by default in the past.
abort("free() called but not included in the build - add '_free' to EXPORTED_FUNCTIONS");
}
var ALLOC_NORMAL = 0; // Tries to use _malloc() var ALLOC_NORMAL = 0; // Tries to use _malloc()
var ALLOC_STACK = 1; // Lives for the duration of the current function call var ALLOC_STACK = 1; // Lives for the duration of the current function call
@ -1199,7 +1195,7 @@ function updateGlobalBufferAndViews(buf) {
var TOTAL_STACK = 5242880; var TOTAL_STACK = 5242880;
if (Module['TOTAL_STACK']) assert(TOTAL_STACK === Module['TOTAL_STACK'], 'the stack size can no longer be determined at runtime') if (Module['TOTAL_STACK']) assert(TOTAL_STACK === Module['TOTAL_STACK'], 'the stack size can no longer be determined at runtime')
var INITIAL_MEMORY = Module['INITIAL_MEMORY'] || 16777216; var INITIAL_MEMORY = Module['INITIAL_MEMORY'] || 134217728;
if (!Object.getOwnPropertyDescriptor(Module, 'INITIAL_MEMORY')) { if (!Object.getOwnPropertyDescriptor(Module, 'INITIAL_MEMORY')) {
Object.defineProperty(Module, 'INITIAL_MEMORY', { Object.defineProperty(Module, 'INITIAL_MEMORY', {
configurable: true, configurable: true,
@ -1217,7 +1213,7 @@ assert(typeof Int32Array !== 'undefined' && typeof Float64Array !== 'undefined'
// If memory is defined in wasm, the user can't provide it. // If memory is defined in wasm, the user can't provide it.
assert(!Module['wasmMemory'], 'Use of `wasmMemory` detected. Use -s IMPORTED_MEMORY to define wasmMemory externally'); assert(!Module['wasmMemory'], 'Use of `wasmMemory` detected. Use -s IMPORTED_MEMORY to define wasmMemory externally');
assert(INITIAL_MEMORY == 16777216, 'Detected runtime INITIAL_MEMORY setting. Use -s IMPORTED_MEMORY to define wasmMemory dynamically'); assert(INITIAL_MEMORY == 134217728, 'Detected runtime INITIAL_MEMORY setting. Use -s IMPORTED_MEMORY to define wasmMemory dynamically');
// include: runtime_init_table.js // include: runtime_init_table.js
// In regular non-RELOCATABLE mode the table is exported // In regular non-RELOCATABLE mode the table is exported
@ -1615,7 +1611,7 @@ function createWasm() {
// This assertion doesn't hold when emscripten is run in --post-link // This assertion doesn't hold when emscripten is run in --post-link
// mode. // mode.
// TODO(sbc): Read INITIAL_MEMORY out of the wasm file in post-link mode. // TODO(sbc): Read INITIAL_MEMORY out of the wasm file in post-link mode.
//assert(wasmMemory.buffer.byteLength === 16777216); //assert(wasmMemory.buffer.byteLength === 134217728);
updateGlobalBufferAndViews(wasmMemory.buffer); updateGlobalBufferAndViews(wasmMemory.buffer);
wasmTable = Module['asm']['__indirect_function_table']; wasmTable = Module['asm']['__indirect_function_table'];
@ -1767,6 +1763,100 @@ var ASM_CONSTS = {
return demangleAll(js); return demangleAll(js);
} }
var ExceptionInfoAttrs={DESTRUCTOR_OFFSET:0,REFCOUNT_OFFSET:4,TYPE_OFFSET:8,CAUGHT_OFFSET:12,RETHROWN_OFFSET:13,SIZE:16};
function ___cxa_allocate_exception(size) {
// Thrown object is prepended by exception metadata block
return _malloc(size + ExceptionInfoAttrs.SIZE) + ExceptionInfoAttrs.SIZE;
}
function _atexit(func, arg) {
}
function ___cxa_atexit(a0,a1
) {
return _atexit(a0,a1);
}
function ExceptionInfo(excPtr) {
this.excPtr = excPtr;
this.ptr = excPtr - ExceptionInfoAttrs.SIZE;
this.set_type = function(type) {
HEAP32[(((this.ptr)+(ExceptionInfoAttrs.TYPE_OFFSET))>>2)] = type;
};
this.get_type = function() {
return HEAP32[(((this.ptr)+(ExceptionInfoAttrs.TYPE_OFFSET))>>2)];
};
this.set_destructor = function(destructor) {
HEAP32[(((this.ptr)+(ExceptionInfoAttrs.DESTRUCTOR_OFFSET))>>2)] = destructor;
};
this.get_destructor = function() {
return HEAP32[(((this.ptr)+(ExceptionInfoAttrs.DESTRUCTOR_OFFSET))>>2)];
};
this.set_refcount = function(refcount) {
HEAP32[(((this.ptr)+(ExceptionInfoAttrs.REFCOUNT_OFFSET))>>2)] = refcount;
};
this.set_caught = function (caught) {
caught = caught ? 1 : 0;
HEAP8[(((this.ptr)+(ExceptionInfoAttrs.CAUGHT_OFFSET))>>0)] = caught;
};
this.get_caught = function () {
return HEAP8[(((this.ptr)+(ExceptionInfoAttrs.CAUGHT_OFFSET))>>0)] != 0;
};
this.set_rethrown = function (rethrown) {
rethrown = rethrown ? 1 : 0;
HEAP8[(((this.ptr)+(ExceptionInfoAttrs.RETHROWN_OFFSET))>>0)] = rethrown;
};
this.get_rethrown = function () {
return HEAP8[(((this.ptr)+(ExceptionInfoAttrs.RETHROWN_OFFSET))>>0)] != 0;
};
// Initialize native structure fields. Should be called once after allocated.
this.init = function(type, destructor) {
this.set_type(type);
this.set_destructor(destructor);
this.set_refcount(0);
this.set_caught(false);
this.set_rethrown(false);
}
this.add_ref = function() {
var value = HEAP32[(((this.ptr)+(ExceptionInfoAttrs.REFCOUNT_OFFSET))>>2)];
HEAP32[(((this.ptr)+(ExceptionInfoAttrs.REFCOUNT_OFFSET))>>2)] = value + 1;
};
// Returns true if last reference released.
this.release_ref = function() {
var prev = HEAP32[(((this.ptr)+(ExceptionInfoAttrs.REFCOUNT_OFFSET))>>2)];
HEAP32[(((this.ptr)+(ExceptionInfoAttrs.REFCOUNT_OFFSET))>>2)] = prev - 1;
assert(prev > 0);
return prev === 1;
};
}
var exceptionLast=0;
var uncaughtExceptionCount=0;
function ___cxa_throw(ptr, type, destructor) {
var info = new ExceptionInfo(ptr);
// Initialize ExceptionInfo content after it was allocated in __cxa_allocate_exception.
info.init(type, destructor);
exceptionLast = ptr;
uncaughtExceptionCount++;
throw ptr + " - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.";
}
function _abort() {
abort();
}
function _emscripten_memcpy_big(dest, src, num) { function _emscripten_memcpy_big(dest, src, num) {
HEAPU8.copyWithin(dest, src, src + num); HEAPU8.copyWithin(dest, src, src + num);
} }
@ -1788,14 +1878,6 @@ var ASM_CONSTS = {
exit(status); exit(status);
} }
function flush_NO_FILESYSTEM() {
// flush anything remaining in the buffers during shutdown
if (typeof _fflush !== 'undefined') _fflush(0);
var buffers = SYSCALLS.buffers;
if (buffers[1].length) SYSCALLS.printChar(1, 10);
if (buffers[2].length) SYSCALLS.printChar(2, 10);
}
var SYSCALLS={mappings:{},buffers:[null,[],[]],printChar:function(stream, curr) { var SYSCALLS={mappings:{},buffers:[null,[],[]],printChar:function(stream, curr) {
var buffer = SYSCALLS.buffers[stream]; var buffer = SYSCALLS.buffers[stream];
assert(buffer); assert(buffer);
@ -1818,6 +1900,22 @@ var ASM_CONSTS = {
else assert(high === -1); else assert(high === -1);
return low; return low;
}}; }};
function _fd_close(fd) {
abort('it should not be possible to operate on streams when !SYSCALLS_REQUIRE_FILESYSTEM');
return 0;
}
function _fd_seek(fd, offset_low, offset_high, whence, newOffset) {
abort('it should not be possible to operate on streams when !SYSCALLS_REQUIRE_FILESYSTEM');
}
function flush_NO_FILESYSTEM() {
// flush anything remaining in the buffers during shutdown
if (typeof _fflush !== 'undefined') _fflush(0);
var buffers = SYSCALLS.buffers;
if (buffers[1].length) SYSCALLS.printChar(1, 10);
if (buffers[2].length) SYSCALLS.printChar(2, 10);
}
function _fd_write(fd, iov, iovcnt, pnum) { function _fd_write(fd, iov, iovcnt, pnum) {
// hack to support printf in SYSCALLS_REQUIRE_FILESYSTEM=0 // hack to support printf in SYSCALLS_REQUIRE_FILESYSTEM=0
var num = 0; var num = 0;
@ -1866,9 +1964,15 @@ function intArrayToString(array) {
var asmLibraryArg = { var asmLibraryArg = {
"__cxa_allocate_exception": ___cxa_allocate_exception,
"__cxa_atexit": ___cxa_atexit,
"__cxa_throw": ___cxa_throw,
"abort": _abort,
"emscripten_memcpy_big": _emscripten_memcpy_big, "emscripten_memcpy_big": _emscripten_memcpy_big,
"emscripten_resize_heap": _emscripten_resize_heap, "emscripten_resize_heap": _emscripten_resize_heap,
"exit": _exit, "exit": _exit,
"fd_close": _fd_close,
"fd_seek": _fd_seek,
"fd_write": _fd_write, "fd_write": _fd_write,
"setTempRet0": _setTempRet0 "setTempRet0": _setTempRet0
}; };
@ -1885,6 +1989,9 @@ var _main = Module["_main"] = createExportWrapper("main");
/** @type {function(...*):?} */ /** @type {function(...*):?} */
var _malloc = Module["_malloc"] = createExportWrapper("malloc"); var _malloc = Module["_malloc"] = createExportWrapper("malloc");
/** @type {function(...*):?} */
var _free = Module["_free"] = createExportWrapper("free");
/** @type {function(...*):?} */ /** @type {function(...*):?} */
var ___errno_location = Module["___errno_location"] = createExportWrapper("__errno_location"); var ___errno_location = Module["___errno_location"] = createExportWrapper("__errno_location");

BIN
dist/solver.wasm vendored

Binary file not shown.

View File

@ -81,7 +81,7 @@ export class Sketcher extends THREE.Group {
this.mode = "line" this.mode = "line"
break; break;
case 'b': case 'b':
this.writeBuff() this.solve()
break; break;
case '=': case '=':
this.plane.applyMatrix4(new Matrix4().makeRotationY(0.1)) this.plane.applyMatrix4(new Matrix4().makeRotationY(0.1))
@ -258,7 +258,7 @@ export class Sketcher extends THREE.Group {
this.pointStart(e) this.pointStart(e)
} }
writeBuff() { solve() {
// const linesBuf = new Float32Array(this.linesArr.length * 4) // const linesBuf = new Float32Array(this.linesArr.length * 4)
// const xyOnly = [0,1,3,4]; // const xyOnly = [0,1,3,4];
// let p = 0 // let p = 0
@ -274,12 +274,13 @@ export class Sketcher extends THREE.Group {
ptsBuf[p++] = this.ptsArr[i].geometry.attributes.position.array[j] ptsBuf[p++] = this.ptsArr[i].geometry.attributes.position.array[j]
} }
} }
console.log(ptsBuf)
buffer = Module._malloc(ptsBuf.length * ptsBuf.BYTES_PER_ELEMENT) buffer = Module._malloc(ptsBuf.length * ptsBuf.BYTES_PER_ELEMENT)
Module.HEAPF32.set(ptsBuf, buffer >> 2) Module.HEAPF32.set(ptsBuf, buffer >> 2)
Module["_solver"](buffer) Module["_solver"](this.ptsArr.length/2, buffer)
Module._free(buffer)
} }
} }

View File

@ -1 +1,138 @@
emcc ./wasm/solver.c ./wasm/libslvs.a -L./wasm/ -lslvs -o ./dist/solver.js -s TOTAL_MEMORY=134217728 -s EXPORTED_FUNCTIONS='[_main, _solver]' emcc ./wasm/solver.c ./wasm/libslvs.a -L./wasm/ -lslvs -o ./dist/solver.js -s TOTAL_MEMORY=134217728 -s EXPORTED_FUNCTIONS='[_main, _solver, _free]'
int solver(int nLines, float *ptr)
{
// for (int i=0; i<nLines ;i++) {
// printf("%f\n",*ptr++);
// }
float *buf_pt_start = ptr;
Slvs_hGroup g;
double qw, qx, qy, qz;
g = 1;
/* First, we create our workplane. Its origin corresponds to the origin
* of our base frame (x y z) = (0 0 0) */
sys.param[sys.params++] = Slvs_MakeParam(1, g, 0.0);
sys.param[sys.params++] = Slvs_MakeParam(2, g, 0.0);
sys.param[sys.params++] = Slvs_MakeParam(3, g, 0.0);
sys.entity[sys.entities++] = Slvs_MakePoint3d(101, g, 1, 2, 3);
/* and it is parallel to the xy plane, so it has basis vectors (1 0 0)
* and (0 1 0). */
Slvs_MakeQuaternion(1, 0, 0,
0, 1, 0, &qw, &qx, &qy, &qz);
sys.param[sys.params++] = Slvs_MakeParam(4, g, qw);
sys.param[sys.params++] = Slvs_MakeParam(5, g, qx);
sys.param[sys.params++] = Slvs_MakeParam(6, g, qy);
sys.param[sys.params++] = Slvs_MakeParam(7, g, qz);
sys.entity[sys.entities++] = Slvs_MakeNormal3d(102, g, 4, 5, 6, 7);
sys.entity[sys.entities++] = Slvs_MakeWorkplane(200, g, 101, 102);
/* Now create a second group. We'll solve group 2, while leaving group 1
* constant; so the workplane that we've created will be locked down,
* and the solver can't move it. */
g = 2;
/* These points are represented by their coordinates (u v) within the
* workplane, so they need only two parameters each. */
Slvs_hParam h = 8;
Slvs_hParam pt_id = 301;
Slvs_hParam line_id = 400;
Slvs_hParam con_id = 0;
int ptStart = sys.params;
for (int i = 0; i < nLines; i++)
{
sys.param[sys.params++] = Slvs_MakeParam(h++, g, (float)*ptr++);
sys.param[sys.params++] = Slvs_MakeParam(h++, g, (float)*ptr++);
sys.entity[sys.entities++] = Slvs_MakePoint2d(pt_id++, g, 200, h - 2, h - 1);
if (i > 0)
{
sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
con_id++, g,
SLVS_C_POINTS_COINCIDENT,
200,
0.0,
pt_id - 1, pt_id - 2, 0, 0);
}
else
{
sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
con_id++, g,
SLVS_C_POINTS_COINCIDENT,
200,
0.0,
pt_id - 1, 101, 0, 0);
}
printf("h:%i\n", h);
sys.param[sys.params++] = Slvs_MakeParam(h++, g, (float)*ptr++);
sys.param[sys.params++] = Slvs_MakeParam(h++, g, (float)*ptr++);
sys.entity[sys.entities++] = Slvs_MakePoint2d(pt_id++, g, 200, h - 2, h - 1);
sys.entity[sys.entities++] = Slvs_MakeLineSegment(line_id++, g,
200, pt_id - 1, pt_id - 2);
sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
con_id++, g,
SLVS_C_PT_PT_DISTANCE,
200,
30.0,
pt_id - 1, pt_id - 2, 0, 0);
sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
con_id++, g,
SLVS_C_VERTICAL,
200,
0.0,
0, 0, line_id-1, 0);
}
/* If the solver fails, then ask it to report which constraints caused
* the problem. */
sys.calculateFaileds = 1;
sys.dragged[0] = 10;
sys.dragged[1] = 11;
sys.dragged[2] = 14;
sys.dragged[3] = 15;
/* And solve. */
Slvs_Solve(&sys, g);
// printf("%i,wtf\n", sys.result);
if (sys.result == SLVS_RESULT_OKAY)
{
printf("solved okay\n");
for (int i = 0; i < nLines * 4; i++)
{
// *buf_pt_start++ = (float)sys.param[ptStart++].val;
printf("%f\n", sys.param[ptStart++].val);
}
}
else
{
int i;
printf("solve failed: problematic constraints are:");
for (i = 0; i < sys.faileds; i++)
{
printf(" %d", sys.failed[i]);
}
printf("\n");
if (sys.result == SLVS_RESULT_INCONSISTENT)
{
printf("system inconsistent\n");
}
else
{
printf("system nonconvergent\n");
}
}
sys.params = sys.constraints = sys.entities = 0;
return 0;
}

View File

@ -6,7 +6,7 @@
* Copyright 2008-2013 Jonathan Westhues. * Copyright 2008-2013 Jonathan Westhues.
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
#ifdef WIN32 #ifdef WIN32
# include <windows.h> #include <windows.h>
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -19,12 +19,13 @@ static Slvs_System sys;
static void *CheckMalloc(size_t n) static void *CheckMalloc(size_t n)
{ {
void *r = malloc(n); void *r = malloc(n);
if(!r) { if (!r)
printf("out of memory!\n"); {
exit(-1); printf("out of memory!\n");
} exit(-1);
return r; }
return r;
} }
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
@ -33,49 +34,52 @@ static void *CheckMalloc(size_t n)
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
void Example3d() void Example3d()
{ {
/* This will contain a single group, which will arbitrarily number 1. */ /* This will contain a single group, which will arbitrarily number 1. */
Slvs_hGroup g = 1; Slvs_hGroup g = 1;
/* A point, initially at (x y z) = (10 10 10) */ /* A point, initially at (x y z) = (10 10 10) */
sys.param[sys.params++] = Slvs_MakeParam(1, g, 10.0); sys.param[sys.params++] = Slvs_MakeParam(1, g, 10.0);
sys.param[sys.params++] = Slvs_MakeParam(2, g, 10.0); sys.param[sys.params++] = Slvs_MakeParam(2, g, 10.0);
sys.param[sys.params++] = Slvs_MakeParam(3, g, 10.0); sys.param[sys.params++] = Slvs_MakeParam(3, g, 10.0);
sys.entity[sys.entities++] = Slvs_MakePoint3d(101, g, 1, 2, 3); sys.entity[sys.entities++] = Slvs_MakePoint3d(101, g, 1, 2, 3);
/* and a second point at (20 20 20) */ /* and a second point at (20 20 20) */
sys.param[sys.params++] = Slvs_MakeParam(4, g, 20.0); sys.param[sys.params++] = Slvs_MakeParam(4, g, 20.0);
sys.param[sys.params++] = Slvs_MakeParam(5, g, 20.0); sys.param[sys.params++] = Slvs_MakeParam(5, g, 20.0);
sys.param[sys.params++] = Slvs_MakeParam(6, g, 20.0); sys.param[sys.params++] = Slvs_MakeParam(6, g, 20.0);
sys.entity[sys.entities++] = Slvs_MakePoint3d(102, g, 4, 5, 6); sys.entity[sys.entities++] = Slvs_MakePoint3d(102, g, 4, 5, 6);
/* and a line segment connecting them. */ /* and a line segment connecting them. */
sys.entity[sys.entities++] = Slvs_MakeLineSegment(200, g, sys.entity[sys.entities++] = Slvs_MakeLineSegment(200, g,
SLVS_FREE_IN_3D, 101, 102); SLVS_FREE_IN_3D, 101, 102);
/* The distance between the points should be 30.0 units. */ /* The distance between the points should be 30.0 units. */
sys.constraint[sys.constraints++] = Slvs_MakeConstraint( sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
1, g, 1, g,
SLVS_C_PT_PT_DISTANCE, SLVS_C_PT_PT_DISTANCE,
SLVS_FREE_IN_3D, SLVS_FREE_IN_3D,
30.0, 30.0,
101, 102, 0, 0); 101, 102, 0, 0);
/* Let's tell the solver to keep the second point as close to constant /* Let's tell the solver to keep the second point as close to constant
* as possible, instead moving the first point. */ * as possible, instead moving the first point. */
sys.dragged[0] = 4; // sys.dragged[0] = 4;
sys.dragged[1] = 5; // sys.dragged[1] = 5;
sys.dragged[2] = 6; // sys.dragged[2] = 6;
/* Now that we have written our system, we solve. */ /* Now that we have written our system, we solve. */
Slvs_Solve(&sys, g); Slvs_Solve(&sys, g);
if(sys.result == SLVS_RESULT_OKAY) { if (sys.result == SLVS_RESULT_OKAY)
printf("okay; now at (%.3f %.3f %.3f)\n" {
" (%.3f %.3f %.3f)\n", printf("okay; now at (%.3f %.3f %.3f)\n"
sys.param[0].val, sys.param[1].val, sys.param[2].val, " (%.3f %.3f %.3f)\n",
sys.param[3].val, sys.param[4].val, sys.param[5].val); sys.param[0].val, sys.param[1].val, sys.param[2].val,
printf("%d DOF\n", sys.dof); sys.param[3].val, sys.param[4].val, sys.param[5].val);
} else { printf("%d DOF\n", sys.dof);
printf("solve failed"); }
} else
{
printf("solve failed");
}
} }
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
@ -85,108 +89,107 @@ void Example3d()
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
void Example2d(float xx) void Example2d(float xx)
{ {
Slvs_hGroup g; Slvs_hGroup g;
double qw, qx, qy, qz; double qw, qx, qy, qz;
g = 1; g = 1;
/* First, we create our workplane. Its origin corresponds to the origin /* First, we create our workplane. Its origin corresponds to the origin
* of our base frame (x y z) = (0 0 0) */ * of our base frame (x y z) = (0 0 0) */
sys.param[sys.params++] = Slvs_MakeParam(1, g, 0.0); sys.param[sys.params++] = Slvs_MakeParam(1, g, 0.0);
sys.param[sys.params++] = Slvs_MakeParam(2, g, 0.0); sys.param[sys.params++] = Slvs_MakeParam(2, g, 0.0);
sys.param[sys.params++] = Slvs_MakeParam(3, g, 0.0); sys.param[sys.params++] = Slvs_MakeParam(3, g, 0.0);
sys.entity[sys.entities++] = Slvs_MakePoint3d(101, g, 1, 2, 3); sys.entity[sys.entities++] = Slvs_MakePoint3d(101, g, 1, 2, 3);
/* and it is parallel to the xy plane, so it has basis vectors (1 0 0) /* and it is parallel to the xy plane, so it has basis vectors (1 0 0)
* and (0 1 0). */ * and (0 1 0). */
Slvs_MakeQuaternion(1, 0, 0, Slvs_MakeQuaternion(1, 0, 0,
0, 1, 0, &qw, &qx, &qy, &qz); 0, 1, 0, &qw, &qx, &qy, &qz);
sys.param[sys.params++] = Slvs_MakeParam(4, g, qw); sys.param[sys.params++] = Slvs_MakeParam(4, g, qw);
sys.param[sys.params++] = Slvs_MakeParam(5, g, qx); sys.param[sys.params++] = Slvs_MakeParam(5, g, qx);
sys.param[sys.params++] = Slvs_MakeParam(6, g, qy); sys.param[sys.params++] = Slvs_MakeParam(6, g, qy);
sys.param[sys.params++] = Slvs_MakeParam(7, g, qz); sys.param[sys.params++] = Slvs_MakeParam(7, g, qz);
sys.entity[sys.entities++] = Slvs_MakeNormal3d(102, g, 4, 5, 6, 7); sys.entity[sys.entities++] = Slvs_MakeNormal3d(102, g, 4, 5, 6, 7);
sys.entity[sys.entities++] = Slvs_MakeWorkplane(200, g, 101, 102); sys.entity[sys.entities++] = Slvs_MakeWorkplane(200, g, 101, 102);
/* Now create a second group. We'll solve group 2, while leaving group 1 /* Now create a second group. We'll solve group 2, while leaving group 1
* constant; so the workplane that we've created will be locked down, * constant; so the workplane that we've created will be locked down,
* and the solver can't move it. */ * and the solver can't move it. */
g = 2; g = 2;
/* These points are represented by their coordinates (u v) within the /* These points are represented by their coordinates (u v) within the
* workplane, so they need only two parameters each. */ * workplane, so they need only two parameters each. */
sys.param[sys.params++] = Slvs_MakeParam(11, g, 10.0); sys.param[sys.params++] = Slvs_MakeParam(11, g, 10.0);
sys.param[sys.params++] = Slvs_MakeParam(12, g, 20.0); sys.param[sys.params++] = Slvs_MakeParam(12, g, 20.0);
sys.entity[sys.entities++] = Slvs_MakePoint2d(301, g, 200, 11, 12); sys.entity[sys.entities++] = Slvs_MakePoint2d(301, g, 200, 11, 12);
sys.param[sys.params++] = Slvs_MakeParam(13, g, 20.0); sys.param[sys.params++] = Slvs_MakeParam(13, g, 20.0);
sys.param[sys.params++] = Slvs_MakeParam(14, g, 10.0); sys.param[sys.params++] = Slvs_MakeParam(14, g, 10.0);
sys.entity[sys.entities++] = Slvs_MakePoint2d(302, g, 200, 13, 14); sys.entity[sys.entities++] = Slvs_MakePoint2d(302, g, 200, 13, 14);
/* And we create a line segment with those endpoints. */ /* And we create a line segment with those endpoints. */
sys.entity[sys.entities++] = Slvs_MakeLineSegment(400, g, sys.entity[sys.entities++] = Slvs_MakeLineSegment(400, g,
200, 301, 302); 200, 301, 302);
/* Now three more points. */ /* Now three more points. */
sys.param[sys.params++] = Slvs_MakeParam(15, g, 110.0); sys.param[sys.params++] = Slvs_MakeParam(15, g, 110.0);
sys.param[sys.params++] = Slvs_MakeParam(16, g, 120.0); sys.param[sys.params++] = Slvs_MakeParam(16, g, 120.0);
sys.entity[sys.entities++] = Slvs_MakePoint2d(303, g, 200, 15, 16); sys.entity[sys.entities++] = Slvs_MakePoint2d(303, g, 200, 15, 16);
sys.param[sys.params++] = Slvs_MakeParam(17, g, 120.0); sys.param[sys.params++] = Slvs_MakeParam(17, g, 120.0);
sys.param[sys.params++] = Slvs_MakeParam(18, g, 110.0); sys.param[sys.params++] = Slvs_MakeParam(18, g, 110.0);
sys.entity[sys.entities++] = Slvs_MakePoint2d(304, g, 200, 17, 18); sys.entity[sys.entities++] = Slvs_MakePoint2d(304, g, 200, 17, 18);
sys.param[sys.params++] = Slvs_MakeParam(19, g, 115.0); sys.param[sys.params++] = Slvs_MakeParam(19, g, 115.0);
sys.param[sys.params++] = Slvs_MakeParam(20, g, xx); sys.param[sys.params++] = Slvs_MakeParam(20, g, xx);
sys.entity[sys.entities++] = Slvs_MakePoint2d(305, g, 200, 19, 20); sys.entity[sys.entities++] = Slvs_MakePoint2d(305, g, 200, 19, 20);
/* And arc, centered at point 303, starting at point 304, ending at /* And arc, centered at point 303, starting at point 304, ending at
* point 305. */ * point 305. */
sys.entity[sys.entities++] = Slvs_MakeArcOfCircle(401, g, 200, 102, sys.entity[sys.entities++] = Slvs_MakeArcOfCircle(401, g, 200, 102,
303, 304, 305); 303, 304, 305);
/* Now one more point, and a distance */ /* Now one more point, and a distance */
sys.param[sys.params++] = Slvs_MakeParam(21, g, 200.0); sys.param[sys.params++] = Slvs_MakeParam(21, g, 200.0);
sys.param[sys.params++] = Slvs_MakeParam(22, g, 200.0); sys.param[sys.params++] = Slvs_MakeParam(22, g, 200.0);
sys.entity[sys.entities++] = Slvs_MakePoint2d(306, g, 200, 21, 22); sys.entity[sys.entities++] = Slvs_MakePoint2d(306, g, 200, 21, 22);
sys.param[sys.params++] = Slvs_MakeParam(23, g, 30.0); sys.param[sys.params++] = Slvs_MakeParam(23, g, 30.0);
sys.entity[sys.entities++] = Slvs_MakeDistance(307, g, 200, 23); sys.entity[sys.entities++] = Slvs_MakeDistance(307, g, 200, 23);
/* And a complete circle, centered at point 306 with radius equal to /* And a complete circle, centered at point 306 with radius equal to
* distance 307. The normal is 102, the same as our workplane. */ * distance 307. The normal is 102, the same as our workplane. */
sys.entity[sys.entities++] = Slvs_MakeCircle(402, g, 200, sys.entity[sys.entities++] = Slvs_MakeCircle(402, g, 200,
306, 102, 307); 306, 102, 307);
/* The length of our line segment is 30.0 units. */
sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
1, g,
SLVS_C_PT_PT_DISTANCE,
200,
30.0,
301, 302, 0, 0);
/* The length of our line segment is 30.0 units. */ /* And the distance from our line segment to the origin is 10.0 units. */
sys.constraint[sys.constraints++] = Slvs_MakeConstraint( sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
1, g, 2, g,
SLVS_C_PT_PT_DISTANCE, SLVS_C_PT_LINE_DISTANCE,
200, 200,
30.0, 10.0,
301, 302, 0, 0); 101, 0, 400, 0);
/* And the line segment is vertical. */
/* And the distance from our line segment to the origin is 10.0 units. */ sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
sys.constraint[sys.constraints++] = Slvs_MakeConstraint( 3, g,
2, g, SLVS_C_VERTICAL,
SLVS_C_PT_LINE_DISTANCE, 200,
200, 0.0,
10.0, 0, 0, 400, 0);
101, 0, 400, 0); /* And the distance from one endpoint to the origin is 15.0 units. */
/* And the line segment is vertical. */ sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
sys.constraint[sys.constraints++] = Slvs_MakeConstraint( 4, g,
3, g, SLVS_C_PT_PT_DISTANCE,
SLVS_C_VERTICAL, 200,
200, 15.0,
0.0, 301, 101, 0, 0);
0, 0, 400, 0);
/* And the distance from one endpoint to the origin is 15.0 units. */
sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
4, g,
SLVS_C_PT_PT_DISTANCE,
200,
15.0,
301, 101, 0, 0);
#if 0 #if 0
/* And same for the other endpoint; so if you add this constraint then /* And same for the other endpoint; so if you add this constraint then
* the sketch is overconstrained and will signal an error. */ * the sketch is overconstrained and will signal an error. */
@ -198,84 +201,180 @@ void Example2d(float xx)
302, 101, 0, 0); 302, 101, 0, 0);
#endif /* 0 */ #endif /* 0 */
/* The arc and the circle have equal radius. */ /* The arc and the circle have equal radius. */
sys.constraint[sys.constraints++] = Slvs_MakeConstraint( sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
6, g, 6, g,
SLVS_C_EQUAL_RADIUS, SLVS_C_EQUAL_RADIUS,
200, 200,
0.0, 0.0,
0, 0, 401, 402); 0, 0, 401, 402);
/* The arc has radius 17.0 units. */ /* The arc has radius 17.0 units. */
sys.constraint[sys.constraints++] = Slvs_MakeConstraint( sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
7, g, 7, g,
SLVS_C_DIAMETER, SLVS_C_DIAMETER,
200, 200,
17.0*2, 17.0 * 2,
0, 0, 401, 0); 0, 0, 401, 0);
/* If the solver fails, then ask it to report which constraints caused /* If the solver fails, then ask it to report which constraints caused
* the problem. */ * the problem. */
sys.calculateFaileds = 1; sys.calculateFaileds = 1;
/* And solve. */ /* And solve. */
Slvs_Solve(&sys, g); Slvs_Solve(&sys, g);
if(sys.result == SLVS_RESULT_OKAY) { if (sys.result == SLVS_RESULT_OKAY)
printf("solved okay\n"); {
printf("line from (%.3f %.3f) to (%.3f %.3f)\n", printf("solved okay\n");
sys.param[7].val, sys.param[8].val, printf("line from (%.3f %.3f) to (%.3f %.3f)\n",
sys.param[9].val, sys.param[10].val); sys.param[7].val, sys.param[8].val,
sys.param[9].val, sys.param[10].val);
printf("arc center (%.3f %.3f) start (%.3f %.3f) finish (%.3f %.3f)\n", printf("arc center (%.3f %.3f) start (%.3f %.3f) finish (%.3f %.3f)\n",
sys.param[11].val, sys.param[12].val, sys.param[11].val, sys.param[12].val,
sys.param[13].val, sys.param[14].val, sys.param[13].val, sys.param[14].val,
sys.param[15].val, sys.param[16].val); sys.param[15].val, sys.param[16].val);
printf("circle center (%.3f %.3f) radius %.3f\n", printf("circle center (%.3f %.3f) radius %.3f\n",
sys.param[17].val, sys.param[18].val, sys.param[17].val, sys.param[18].val,
sys.param[19].val); sys.param[19].val);
printf("%d DOF\n", sys.dof); printf("%d DOF\n", sys.dof);
} else { }
int i; else
printf("solve failed: problematic constraints are:"); {
for(i = 0; i < sys.faileds; i++) { int i;
printf(" %d", sys.failed[i]); printf("solve failed: problematic constraints are:");
} for (i = 0; i < sys.faileds; i++)
printf("\n"); {
if(sys.result == SLVS_RESULT_INCONSISTENT) { printf(" %d", sys.failed[i]);
printf("system inconsistent\n");
} else {
printf("system nonconvergent\n");
}
} }
printf("\n");
if (sys.result == SLVS_RESULT_INCONSISTENT)
{
printf("system inconsistent\n");
}
else
{
printf("system nonconvergent\n");
}
}
} }
int solver(int nLines, float *ptr)
int solver(float *ptr)
{ {
// for (int i=0; i<nLines ;i++) {
// printf("%f\n",*ptr++);
// }
float *buf_pt_start = ptr;
// Example2d(150.0); Slvs_hGroup g;
// sys.params = sys.constraints = sys.entities = 0; double qw, qx, qy, qz;
for (int i=0; i<10 ;i++) {
printf("%f\n",*ptr++); g = 1;
/* First, we create our workplane. Its origin corresponds to the origin
* of our base frame (x y z) = (0 0 0) */
sys.param[sys.params++] = Slvs_MakeParam(1, g, 0.0);
sys.param[sys.params++] = Slvs_MakeParam(2, g, 0.0);
sys.param[sys.params++] = Slvs_MakeParam(3, g, 0.0);
sys.entity[sys.entities++] = Slvs_MakePoint3d(101, g, 1, 2, 3);
/* and it is parallel to the xy plane, so it has basis vectors (1 0 0)
* and (0 1 0). */
Slvs_MakeQuaternion(1, 0, 0,
0, 1, 0, &qw, &qx, &qy, &qz);
sys.param[sys.params++] = Slvs_MakeParam(4, g, qw);
sys.param[sys.params++] = Slvs_MakeParam(5, g, qx);
sys.param[sys.params++] = Slvs_MakeParam(6, g, qy);
sys.param[sys.params++] = Slvs_MakeParam(7, g, qz);
sys.entity[sys.entities++] = Slvs_MakeNormal3d(102, g, 4, 5, 6, 7);
sys.entity[sys.entities++] = Slvs_MakeWorkplane(200, g, 101, 102);
/* Now create a second group. We'll solve group 2, while leaving group 1
* constant; so the workplane that we've created will be locked down,
* and the solver can't move it. */
g = 2;
/* These points are represented by their coordinates (u v) within the
* workplane, so they need only two parameters each. */
int ptStart = sys.params;
sys.param[sys.params++] = Slvs_MakeParam(11, g, (float)*ptr++);
sys.param[sys.params++] = Slvs_MakeParam(12, g, (float)*ptr++);
sys.entity[sys.entities++] = Slvs_MakePoint2d(301, g, 200, 11, 12);
sys.param[sys.params++] = Slvs_MakeParam(13, g, (float)*ptr++);
sys.param[sys.params++] = Slvs_MakeParam(14, g, (float)*ptr++);
sys.entity[sys.entities++] = Slvs_MakePoint2d(302, g, 200, 13, 14);
/* And we create a line segment with those endpoints. */
sys.entity[sys.entities++] = Slvs_MakeLineSegment(400, g,
200, 301, 302);
sys.param[sys.params++] = Slvs_MakeParam(15, g, (float)*ptr++);
sys.param[sys.params++] = Slvs_MakeParam(16, g, (float)*ptr++);
sys.entity[sys.entities++] = Slvs_MakePoint2d(303, g, 200, 15, 16);
sys.param[sys.params++] = Slvs_MakeParam(17, g, (float)*ptr++);
sys.param[sys.params++] = Slvs_MakeParam(18, g, (float)*ptr++);
sys.entity[sys.entities++] = Slvs_MakePoint2d(304, g, 200, 17, 18);
sys.entity[sys.entities++] = Slvs_MakeLineSegment(401, g,
200, 303, 304);
sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
421, g,
SLVS_C_POINTS_COINCIDENT,
200,
0.0,
302, 303, 0, 0);
/* And solve. */
Slvs_Solve(&sys, g);
// printf("%i,wtf\n", sys.result);
if (sys.result == SLVS_RESULT_OKAY)
{
printf("solved okay\n");
for (int i = 0; i < nLines * 4; i++)
{
// *buf_pt_start++ = (float)sys.param[ptStart++].val;
printf("%f\n", sys.param[ptStart++].val);
} }
return 0; }
else
{
int i;
printf("solve failed: problematic constraints are:");
for (i = 0; i < sys.faileds; i++)
{
printf(" %d", sys.failed[i]);
}
printf("\n");
if (sys.result == SLVS_RESULT_INCONSISTENT)
{
printf("system inconsistent\n");
}
else
{
printf("system nonconvergent\n");
}
}
sys.params = sys.constraints = sys.entities = 0;
return 0;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
sys.param = CheckMalloc(50*sizeof(sys.param[0])); sys.param = CheckMalloc(50 * sizeof(sys.param[0]));
sys.entity = CheckMalloc(50*sizeof(sys.entity[0])); sys.entity = CheckMalloc(50 * sizeof(sys.entity[0]));
sys.constraint = CheckMalloc(50*sizeof(sys.constraint[0])); sys.constraint = CheckMalloc(50 * sizeof(sys.constraint[0]));
sys.failed = CheckMalloc(50*sizeof(sys.failed[0])); sys.failed = CheckMalloc(50 * sizeof(sys.failed[0]));
sys.faileds = 50; sys.faileds = 50;
// Example2d(150.0);
// Example2d(150.0); printf("hello\n");
return 0;
printf("hello\n");
return 0;
} }