2008-03-25 10:02:13 +00:00
|
|
|
#include "solvespace.h"
|
|
|
|
|
|
|
|
SolveSpace SS;
|
|
|
|
|
2008-04-24 06:22:16 +00:00
|
|
|
void SolveSpace::Init(char *cmdLine) {
|
|
|
|
if(strlen(cmdLine) == 0) {
|
|
|
|
NewFile();
|
|
|
|
} else {
|
|
|
|
LoadFromFile(cmdLine);
|
|
|
|
}
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-13 10:57:41 +00:00
|
|
|
TW.Init();
|
|
|
|
GW.Init();
|
|
|
|
|
2008-04-27 09:03:01 +00:00
|
|
|
GenerateAll(false);
|
2008-04-25 08:26:15 +00:00
|
|
|
|
|
|
|
TW.Show();
|
2008-04-08 12:54:53 +00:00
|
|
|
}
|
|
|
|
|
2008-04-27 09:03:01 +00:00
|
|
|
void SolveSpace::GenerateAll(bool andSolve) {
|
|
|
|
int i, j;
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-27 05:00:12 +00:00
|
|
|
// Don't lose our numerical guesses when we regenerate.
|
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-27 09:03:01 +00:00
|
|
|
|
|
|
|
for(i = 0; i < group.n; i++) {
|
|
|
|
group.elem[i].solved = false;
|
2008-03-26 09:18:12 +00:00
|
|
|
}
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-27 09:03:01 +00:00
|
|
|
// For now, solve the groups in given order; should discover the
|
|
|
|
// correct order later.
|
|
|
|
for(i = 0; i < group.n; i++) {
|
|
|
|
Group *g = &(group.elem[i]);
|
|
|
|
|
|
|
|
for(j = 0; j < request.n; j++) {
|
|
|
|
Request *r = &(request.elem[j]);
|
|
|
|
if(r->group.v != g->h.v) continue;
|
|
|
|
|
|
|
|
r->Generate(&entity, ¶m);
|
|
|
|
}
|
|
|
|
|
|
|
|
g->Generate(&entity, ¶m);
|
|
|
|
|
|
|
|
// Use the previous values for params that we've seen before, as
|
|
|
|
// initial guesses for the solver.
|
|
|
|
for(j = 0; j < param.n; j++) {
|
|
|
|
Param *newp = &(param.elem[j]);
|
|
|
|
if(newp->known) continue;
|
|
|
|
|
|
|
|
Param *prevp = prev.FindByIdNoOops(newp->h);
|
|
|
|
if(prevp) newp->val = prevp->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(g->h.v == Group::HGROUP_REFERENCES.v) {
|
|
|
|
ForceReferences();
|
|
|
|
group.elem[0].solved = true;
|
|
|
|
} else {
|
|
|
|
// Solve this group.
|
|
|
|
if(andSolve) SolveGroup(g->h);
|
2008-04-13 10:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-20 11:35:10 +00:00
|
|
|
prev.Clear();
|
2008-04-24 06:22:16 +00:00
|
|
|
InvalidateGraphics();
|
2008-04-12 16:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-27 09:03:01 +00:00
|
|
|
Entity *origin = GetEntity(hr.entity(1));
|
|
|
|
origin->PointForceTo(v);
|
|
|
|
GetParam(origin->param[0])->known = true;
|
|
|
|
GetParam(origin->param[1])->known = true;
|
|
|
|
GetParam(origin->param[2])->known = true;
|
2008-04-09 08:39:01 +00:00
|
|
|
// The quaternion that defines the rotation, from the table.
|
2008-04-27 09:03:01 +00:00
|
|
|
Param *p;
|
|
|
|
p = GetParam(hr.param(0)); p->val = Quat[i].a; p->known = true;
|
|
|
|
p = GetParam(hr.param(1)); p->val = Quat[i].b; p->known = true;
|
|
|
|
p = GetParam(hr.param(2)); p->val = Quat[i].c; p->known = true;
|
|
|
|
p = GetParam(hr.param(3)); p->val = Quat[i].d; p->known = true;
|
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;
|
|
|
|
// 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));
|
|
|
|
}
|
2008-04-27 09:03:01 +00:00
|
|
|
// And for the group itself
|
|
|
|
Group *g = SS.GetGroup(hg);
|
|
|
|
g->Generate(&(sys.entity), &(sys.param));
|
2008-04-20 11:35:10 +00:00
|
|
|
// 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();
|
2008-04-25 10:11:29 +00:00
|
|
|
FreeAllTemporary();
|
2008-04-21 08:16:38 +00:00
|
|
|
return r;
|
2008-04-20 11:35:10 +00:00
|
|
|
}
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
void SolveSpace::MenuFile(int id) {
|
|
|
|
switch(id) {
|
|
|
|
case GraphicsWindow::MNU_NEW:
|
2008-04-24 06:22:16 +00:00
|
|
|
SS.NewFile();
|
2008-04-27 09:03:01 +00:00
|
|
|
SS.GenerateAll(false);
|
2008-04-25 07:04:09 +00:00
|
|
|
SS.GW.Init();
|
|
|
|
SS.TW.Init();
|
2008-04-25 08:26:15 +00:00
|
|
|
SS.TW.Show();
|
2008-04-24 06:22:16 +00:00
|
|
|
break;
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
case GraphicsWindow::MNU_OPEN:
|
2008-04-24 06:22:16 +00:00
|
|
|
break;
|
2008-04-18 11:11:48 +00:00
|
|
|
|
|
|
|
case GraphicsWindow::MNU_SAVE:
|
2008-04-24 06:22:16 +00:00
|
|
|
SS.SaveToFile("t.slvs");
|
2008-04-18 11:11:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GraphicsWindow::MNU_SAVE_AS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GraphicsWindow::MNU_EXIT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
}
|