2008-03-25 10:02:13 +00:00
|
|
|
#include "solvespace.h"
|
|
|
|
|
|
|
|
SolveSpace SS;
|
|
|
|
|
|
|
|
void SolveSpace::Init(void) {
|
2008-04-14 10:28:32 +00:00
|
|
|
constraint.Clear();
|
2008-04-08 12:54:53 +00:00
|
|
|
request.Clear();
|
2008-04-14 10:28:32 +00:00
|
|
|
group.Clear();
|
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
entity.Clear();
|
|
|
|
param.Clear();
|
|
|
|
|
2008-04-08 12:54:53 +00:00
|
|
|
// Our initial group, that contains the references.
|
|
|
|
Group g;
|
|
|
|
memset(&g, 0, sizeof(g));
|
2008-04-11 12:47:14 +00:00
|
|
|
g.name.strcpy("#references");
|
2008-04-09 08:39:01 +00:00
|
|
|
g.h = Group::HGROUP_REFERENCES;
|
|
|
|
group.Add(&g);
|
2008-04-01 10:48:44 +00:00
|
|
|
|
2008-04-18 07:06:37 +00:00
|
|
|
// And an empty group, for the first stuff the user draws.
|
2008-04-08 12:54:53 +00:00
|
|
|
g.name.strcpy("");
|
|
|
|
group.AddAndAssignId(&g);
|
|
|
|
|
|
|
|
|
|
|
|
// Let's create three two-d coordinate systems, for the coordinate
|
|
|
|
// planes; these are our references, present in every sketch.
|
|
|
|
Request r;
|
|
|
|
memset(&r, 0, sizeof(r));
|
2008-04-09 08:39:01 +00:00
|
|
|
r.type = Request::CSYS_2D;
|
2008-04-08 12:54:53 +00:00
|
|
|
r.group = Group::HGROUP_REFERENCES;
|
2008-04-18 07:06:37 +00:00
|
|
|
r.csys = Entity::NO_CSYS;
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-11 12:47:14 +00:00
|
|
|
r.name.strcpy("#XY-csys");
|
2008-04-09 08:39:01 +00:00
|
|
|
r.h = Request::HREQUEST_REFERENCE_XY;
|
|
|
|
request.Add(&r);
|
|
|
|
|
2008-04-11 12:47:14 +00:00
|
|
|
r.name.strcpy("#YZ-csys");
|
2008-04-09 08:39:01 +00:00
|
|
|
r.h = Request::HREQUEST_REFERENCE_YZ;
|
|
|
|
request.Add(&r);
|
|
|
|
|
2008-04-11 12:47:14 +00:00
|
|
|
r.name.strcpy("#ZX-csys");
|
2008-04-09 08:39:01 +00:00
|
|
|
r.h = Request::HREQUEST_REFERENCE_ZX;
|
|
|
|
request.Add(&r);
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-13 10:57:41 +00:00
|
|
|
TW.Init();
|
|
|
|
GW.Init();
|
|
|
|
|
2008-04-11 12:47:14 +00:00
|
|
|
TW.Show();
|
2008-04-13 10:57:41 +00:00
|
|
|
GenerateAll();
|
2008-04-08 12:54:53 +00:00
|
|
|
}
|
|
|
|
|
2008-04-13 10:57:41 +00:00
|
|
|
void SolveSpace::GenerateAll(void) {
|
2008-03-26 09:18:12 +00:00
|
|
|
int i;
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-13 10:57:41 +00:00
|
|
|
IdList<Param,hParam> prev;
|
|
|
|
param.MoveSelfInto(&prev);
|
|
|
|
|
2008-04-08 12:54:53 +00:00
|
|
|
entity.Clear();
|
2008-04-18 11:11:48 +00:00
|
|
|
for(i = 0; i < request.n; i++) {
|
2008-04-19 11:09:47 +00:00
|
|
|
request.elem[i].Generate(&entity, ¶m);
|
2008-03-26 09:18:12 +00:00
|
|
|
}
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
for(i = 0; i < param.n; i++) {
|
2008-04-18 07:21:17 +00:00
|
|
|
Param *p = prev.FindByIdNoOops(param.elem[i].h);
|
2008-04-13 10:57:41 +00:00
|
|
|
if(p) {
|
2008-04-18 07:21:17 +00:00
|
|
|
param.elem[i].val = p->val;
|
2008-04-13 10:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-20 11:35:10 +00:00
|
|
|
prev.Clear();
|
2008-04-12 16:28:48 +00:00
|
|
|
ForceReferences();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SolveSpace::ForceReferences(void) {
|
2008-04-09 08:39:01 +00:00
|
|
|
// Force the values of the paramters that define the three reference
|
|
|
|
// coordinate systems.
|
|
|
|
static const struct {
|
|
|
|
hRequest hr;
|
|
|
|
double a, b, c, d;
|
|
|
|
} Quat[] = {
|
|
|
|
{ Request::HREQUEST_REFERENCE_XY, 1, 0, 0, 0, },
|
2008-04-18 11:11:48 +00:00
|
|
|
{ Request::HREQUEST_REFERENCE_YZ, 0.5, 0.5, 0.5, 0.5, },
|
|
|
|
{ Request::HREQUEST_REFERENCE_ZX, 0.5, -0.5, -0.5, -0.5, },
|
2008-04-09 08:39:01 +00:00
|
|
|
};
|
2008-04-12 16:28:48 +00:00
|
|
|
for(int i = 0; i < 3; i++) {
|
2008-04-19 11:09:47 +00:00
|
|
|
hRequest hr = Quat[i].hr;
|
2008-04-09 08:39:01 +00:00
|
|
|
// The origin for our coordinate system, always zero
|
|
|
|
Vector v = Vector::MakeFrom(0, 0, 0);
|
2008-04-19 11:44:44 +00:00
|
|
|
GetEntity(hr.entity(1))->PointForceTo(v);
|
2008-04-09 08:39:01 +00:00
|
|
|
// The quaternion that defines the rotation, from the table.
|
2008-04-21 08:16:38 +00:00
|
|
|
GetParam(hr.param(0))->val = Quat[i].a;
|
|
|
|
GetParam(hr.param(1))->val = Quat[i].b;
|
|
|
|
GetParam(hr.param(2))->val = Quat[i].c;
|
|
|
|
GetParam(hr.param(3))->val = Quat[i].d;
|
2008-04-08 12:54:53 +00:00
|
|
|
}
|
2008-03-25 10:02:13 +00:00
|
|
|
}
|
|
|
|
|
2008-04-20 11:35:10 +00:00
|
|
|
bool SolveSpace::SolveGroup(hGroup hg) {
|
|
|
|
int i;
|
|
|
|
if(hg.v == Group::HGROUP_REFERENCES.v) {
|
|
|
|
// Special case; mark everything in the references known.
|
|
|
|
for(i = 0; i < param.n; i++) {
|
|
|
|
Param *p = &(param.elem[i]);
|
|
|
|
Request *r = GetRequest(p->h.request());
|
|
|
|
if(r->group.v == hg.v) p->known = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear out the system to be solved.
|
|
|
|
sys.entity.Clear();
|
|
|
|
sys.param.Clear();
|
|
|
|
sys.eq.Clear();
|
|
|
|
// And generate all the params for requests in this group
|
|
|
|
for(i = 0; i < request.n; i++) {
|
|
|
|
Request *r = &(request.elem[i]);
|
|
|
|
if(r->group.v != hg.v) continue;
|
|
|
|
|
|
|
|
r->Generate(&(sys.entity), &(sys.param));
|
|
|
|
}
|
|
|
|
// Set the initial guesses for all the params
|
|
|
|
for(i = 0; i < sys.param.n; i++) {
|
|
|
|
Param *p = &(sys.param.elem[i]);
|
|
|
|
p->known = false;
|
|
|
|
p->val = GetParam(p->h)->val;
|
|
|
|
}
|
|
|
|
// And generate all the equations from constraints in this group
|
|
|
|
for(i = 0; i < constraint.n; i++) {
|
|
|
|
Constraint *c = &(constraint.elem[i]);
|
|
|
|
if(c->group.v != hg.v) continue;
|
|
|
|
|
|
|
|
c->Generate(&(sys.eq));
|
|
|
|
}
|
|
|
|
|
2008-04-21 08:16:38 +00:00
|
|
|
bool r = sys.Solve();
|
|
|
|
FreeAllExprs();
|
|
|
|
return r;
|
2008-04-20 11:35:10 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 08:16:38 +00:00
|
|
|
bool SolveSpace::SolveWorker(int order) {
|
2008-04-20 11:35:10 +00:00
|
|
|
bool allSolved = true;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < group.n; i++) {
|
|
|
|
Group *g = &(group.elem[i]);
|
|
|
|
if(g->solved) continue;
|
|
|
|
|
|
|
|
allSolved = false;
|
|
|
|
dbp("try solve group %s", g->DescriptionString());
|
2008-04-21 08:16:38 +00:00
|
|
|
|
|
|
|
// Save the parameter table; a failed solve attempt will mess that
|
|
|
|
// up a little bit.
|
|
|
|
IdList<Param,hParam> savedParam;
|
|
|
|
param.DeepCopyInto(&savedParam);
|
|
|
|
|
2008-04-20 11:35:10 +00:00
|
|
|
if(SolveGroup(g->h)) {
|
|
|
|
g->solved = true;
|
2008-04-21 08:16:38 +00:00
|
|
|
g->solveOrder = order;
|
2008-04-20 11:35:10 +00:00
|
|
|
// So this one worked; let's see if we can go any further.
|
2008-04-21 08:16:38 +00:00
|
|
|
if(SolveWorker(order+1)) {
|
2008-04-20 11:35:10 +00:00
|
|
|
// So everything worked; we're done.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-04-21 08:16:38 +00:00
|
|
|
// Didn't work, so undo this choice and give up
|
|
|
|
g->solved = false;
|
|
|
|
param.Clear();
|
|
|
|
savedParam.MoveSelfInto(¶m);
|
2008-04-20 11:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we got here, then either everything failed, so we're stuck, or
|
|
|
|
// everything was already solved, so we're done.
|
|
|
|
return allSolved;
|
|
|
|
}
|
|
|
|
|
2008-04-12 16:28:48 +00:00
|
|
|
void SolveSpace::Solve(void) {
|
2008-04-20 11:35:10 +00:00
|
|
|
int i;
|
|
|
|
for(i = 0; i < group.n; i++) {
|
|
|
|
group.elem[i].solved = false;
|
|
|
|
}
|
2008-04-21 08:16:38 +00:00
|
|
|
SolveWorker(0);
|
2008-04-20 11:35:10 +00:00
|
|
|
|
|
|
|
InvalidateGraphics();
|
2008-04-12 16:28:48 +00:00
|
|
|
}
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
void SolveSpace::MenuFile(int id) {
|
|
|
|
switch(id) {
|
|
|
|
case GraphicsWindow::MNU_NEW:
|
|
|
|
case GraphicsWindow::MNU_OPEN:
|
|
|
|
|
|
|
|
case GraphicsWindow::MNU_SAVE:
|
|
|
|
SS.SaveToFile("t.slv");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GraphicsWindow::MNU_SAVE_AS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GraphicsWindow::MNU_EXIT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
}
|