2008-04-01 10:48:44 +00:00
|
|
|
#include "solvespace.h"
|
|
|
|
|
2008-04-18 07:06:37 +00:00
|
|
|
char *Entity::DescriptionString(void) {
|
2008-04-19 11:09:47 +00:00
|
|
|
Request *r = SS.GetRequest(h.request());
|
2008-04-18 07:06:37 +00:00
|
|
|
return r->DescriptionString();
|
|
|
|
}
|
|
|
|
|
2008-04-19 11:44:44 +00:00
|
|
|
void Entity::Csys2dGetBasisVectors(Vector *u, Vector *v) {
|
2008-04-13 14:28:35 +00:00
|
|
|
double q[4];
|
|
|
|
for(int i = 0; i < 4; i++) {
|
2008-04-19 11:09:47 +00:00
|
|
|
q[i] = SS.GetParam(param.h[i])->val;
|
2008-04-13 14:28:35 +00:00
|
|
|
}
|
2008-04-18 11:11:48 +00:00
|
|
|
Quaternion quat = Quaternion::MakeFrom(q[0], q[1], q[2], q[3]);
|
2008-04-13 14:28:35 +00:00
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
*u = quat.RotationU();
|
|
|
|
*v = quat.RotationV();
|
2008-04-13 14:28:35 +00:00
|
|
|
}
|
|
|
|
|
2008-04-22 13:14:15 +00:00
|
|
|
void Entity::Csys2dGetBasisExprs(ExprVector *u, ExprVector *v) {
|
2008-04-20 11:35:10 +00:00
|
|
|
Expr *a = Expr::FromParam(param.h[0]);
|
|
|
|
Expr *b = Expr::FromParam(param.h[1]);
|
|
|
|
Expr *c = Expr::FromParam(param.h[2]);
|
|
|
|
Expr *d = Expr::FromParam(param.h[3]);
|
|
|
|
|
|
|
|
Expr *two = Expr::FromConstant(2);
|
|
|
|
|
2008-04-22 13:14:15 +00:00
|
|
|
u->x = a->Square();
|
|
|
|
u->x = (u->x)->Plus(b->Square());
|
|
|
|
u->x = (u->x)->Minus(c->Square());
|
|
|
|
u->x = (u->x)->Minus(d->Square());
|
|
|
|
|
|
|
|
u->y = two->Times(a->Times(d));
|
|
|
|
u->y = (u->y)->Plus(two->Times(b->Times(c)));
|
|
|
|
|
|
|
|
u->z = two->Times(b->Times(d));
|
|
|
|
u->z = (u->z)->Minus(two->Times(a->Times(c)));
|
|
|
|
|
|
|
|
v->x = two->Times(b->Times(c));
|
|
|
|
v->x = (v->x)->Minus(two->Times(a->Times(d)));
|
|
|
|
|
|
|
|
v->y = a->Square();
|
|
|
|
v->y = (v->y)->Minus(b->Square());
|
|
|
|
v->y = (v->y)->Plus(c->Square());
|
|
|
|
v->y = (v->y)->Minus(d->Square());
|
|
|
|
|
|
|
|
v->z = two->Times(a->Times(b));
|
|
|
|
v->z = (v->z)->Plus(two->Times(c->Times(d)));
|
2008-04-20 11:35:10 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 08:16:38 +00:00
|
|
|
bool Entity::HasPlane(void) {
|
|
|
|
switch(type) {
|
|
|
|
case CSYS_2D:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-22 13:14:15 +00:00
|
|
|
void Entity::PlaneGetExprs(ExprVector *n, Expr **dn) {
|
|
|
|
if(type == CSYS_2D) {
|
|
|
|
Expr *a = Expr::FromParam(param.h[0]);
|
|
|
|
Expr *b = Expr::FromParam(param.h[1]);
|
|
|
|
Expr *c = Expr::FromParam(param.h[2]);
|
|
|
|
Expr *d = Expr::FromParam(param.h[3]);
|
|
|
|
|
|
|
|
Expr *two = Expr::FromConstant(2);
|
|
|
|
|
|
|
|
// Convert the quaternion to our plane's normal vector.
|
|
|
|
n->x = two->Times(a->Times(c));
|
|
|
|
n->x = (n->x)->Plus (two->Times(b->Times(d)));
|
|
|
|
n->y = two->Times(c->Times(d));
|
|
|
|
n->y = (n->y)->Minus(two->Times(a->Times(b)));
|
|
|
|
n->z = a->Square();
|
|
|
|
n->z = (n->z)->Minus(b->Square());
|
|
|
|
n->z = (n->z)->Minus(c->Square());
|
|
|
|
n->z = (n->z)->Plus (d->Square());
|
|
|
|
|
|
|
|
ExprVector p0 = SS.GetEntity(assoc[0])->PointGetExprs();
|
|
|
|
// The plane is n dot (p - p0) = 0, or
|
|
|
|
// n dot p - n dot p0 = 0
|
|
|
|
// so dn = n dot p0
|
|
|
|
*dn = p0.Dot(*n);
|
|
|
|
} else {
|
|
|
|
oops();
|
|
|
|
}
|
2008-04-21 08:16:38 +00:00
|
|
|
}
|
|
|
|
|
2008-04-19 11:09:47 +00:00
|
|
|
bool Entity::IsPoint(void) {
|
|
|
|
switch(type) {
|
|
|
|
case POINT_IN_3D:
|
|
|
|
case POINT_IN_2D:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-22 13:14:15 +00:00
|
|
|
bool Entity::IsPointIn3d(void) {
|
|
|
|
switch(type) {
|
|
|
|
case POINT_IN_3D:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-21 08:16:38 +00:00
|
|
|
bool Entity::PointIsKnown(void) {
|
|
|
|
switch(type) {
|
|
|
|
case POINT_IN_3D:
|
|
|
|
return SS.GetParam(param.h[0])->known &&
|
|
|
|
SS.GetParam(param.h[1])->known &&
|
|
|
|
SS.GetParam(param.h[2])->known;
|
|
|
|
case POINT_IN_2D:
|
|
|
|
return SS.GetParam(param.h[0])->known &&
|
|
|
|
SS.GetParam(param.h[1])->known;
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 11:44:44 +00:00
|
|
|
bool Entity::PointIsFromReferences(void) {
|
2008-04-21 08:16:38 +00:00
|
|
|
return h.request().IsFromReferences();
|
2008-04-19 11:09:47 +00:00
|
|
|
}
|
|
|
|
|
2008-04-19 11:44:44 +00:00
|
|
|
void Entity::PointForceTo(Vector p) {
|
2008-04-19 11:09:47 +00:00
|
|
|
switch(type) {
|
|
|
|
case POINT_IN_3D:
|
2008-04-21 08:16:38 +00:00
|
|
|
SS.GetParam(param.h[0])->val = p.x;
|
|
|
|
SS.GetParam(param.h[1])->val = p.y;
|
|
|
|
SS.GetParam(param.h[2])->val = p.z;
|
2008-04-19 11:09:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POINT_IN_2D: {
|
|
|
|
Entity *c = SS.GetEntity(csys);
|
|
|
|
Vector u, v;
|
2008-04-19 11:44:44 +00:00
|
|
|
c->Csys2dGetBasisVectors(&u, &v);
|
2008-04-21 08:16:38 +00:00
|
|
|
SS.GetParam(param.h[0])->val = p.Dot(u);
|
|
|
|
SS.GetParam(param.h[1])->val = p.Dot(v);
|
2008-04-19 11:09:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 11:44:44 +00:00
|
|
|
Vector Entity::PointGetCoords(void) {
|
2008-04-19 11:09:47 +00:00
|
|
|
Vector p;
|
|
|
|
switch(type) {
|
|
|
|
case POINT_IN_3D:
|
|
|
|
p.x = SS.GetParam(param.h[0])->val;
|
|
|
|
p.y = SS.GetParam(param.h[1])->val;
|
|
|
|
p.z = SS.GetParam(param.h[2])->val;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case POINT_IN_2D: {
|
|
|
|
Entity *c = SS.GetEntity(csys);
|
|
|
|
Vector u, v;
|
2008-04-19 11:44:44 +00:00
|
|
|
c->Csys2dGetBasisVectors(&u, &v);
|
2008-04-19 11:09:47 +00:00
|
|
|
p = u.ScaledBy(SS.GetParam(param.h[0])->val);
|
|
|
|
p = p.Plus(v.ScaledBy(SS.GetParam(param.h[1])->val));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-04-22 13:14:15 +00:00
|
|
|
ExprVector Entity::PointGetExprs(void) {
|
|
|
|
ExprVector r;
|
2008-04-20 11:35:10 +00:00
|
|
|
switch(type) {
|
|
|
|
case POINT_IN_3D:
|
2008-04-22 13:14:15 +00:00
|
|
|
r.x = Expr::FromParam(param.h[0]);
|
|
|
|
r.y = Expr::FromParam(param.h[1]);
|
|
|
|
r.z = Expr::FromParam(param.h[2]);
|
2008-04-20 11:35:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POINT_IN_2D: {
|
|
|
|
Entity *c = SS.GetEntity(csys);
|
2008-04-22 13:14:15 +00:00
|
|
|
ExprVector u, v;
|
|
|
|
c->Csys2dGetBasisExprs(&u, &v);
|
2008-04-20 11:35:10 +00:00
|
|
|
|
2008-04-22 13:14:15 +00:00
|
|
|
r = u.ScaledBy(Expr::FromParam(param.h[0]));
|
|
|
|
r = r.Plus(v.ScaledBy(Expr::FromParam(param.h[1])));
|
2008-04-20 11:35:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: oops();
|
|
|
|
}
|
2008-04-22 13:14:15 +00:00
|
|
|
return r;
|
2008-04-20 11:35:10 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 14:12:26 +00:00
|
|
|
void Entity::LineDrawOrGetDistance(Vector a, Vector b) {
|
|
|
|
if(dogd.drawing) {
|
|
|
|
glBegin(GL_LINE_STRIP);
|
|
|
|
glxVertex3v(a);
|
|
|
|
glxVertex3v(b);
|
|
|
|
glEnd();
|
|
|
|
} else {
|
|
|
|
Point2d ap = SS.GW.ProjectPoint(a);
|
|
|
|
Point2d bp = SS.GW.ProjectPoint(b);
|
|
|
|
|
|
|
|
double d = dogd.mp.DistanceToLine(ap, bp.Minus(ap), true);
|
|
|
|
dogd.dmin = min(dogd.dmin, d);
|
|
|
|
}
|
2008-04-11 11:13:47 +00:00
|
|
|
}
|
|
|
|
|
2008-04-22 10:53:42 +00:00
|
|
|
void Entity::Draw(int order) {
|
2008-04-12 14:12:26 +00:00
|
|
|
dogd.drawing = true;
|
2008-04-22 10:53:42 +00:00
|
|
|
DrawOrGetDistance(order);
|
2008-04-12 14:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double Entity::GetDistance(Point2d mp) {
|
|
|
|
dogd.drawing = false;
|
|
|
|
dogd.mp = mp;
|
|
|
|
dogd.dmin = 1e12;
|
|
|
|
|
2008-04-22 10:53:42 +00:00
|
|
|
DrawOrGetDistance(-1);
|
2008-04-12 14:12:26 +00:00
|
|
|
|
|
|
|
return dogd.dmin;
|
|
|
|
}
|
|
|
|
|
2008-04-22 10:53:42 +00:00
|
|
|
void Entity::DrawOrGetDistance(int order) {
|
2008-04-19 11:09:47 +00:00
|
|
|
glxColor(1, 1, 1);
|
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
switch(type) {
|
2008-04-19 11:09:47 +00:00
|
|
|
case POINT_IN_3D:
|
|
|
|
case POINT_IN_2D: {
|
2008-04-22 10:53:42 +00:00
|
|
|
if(order >= 0 && order != 2) break;
|
2008-04-21 08:16:38 +00:00
|
|
|
if(!SS.GW.showPoints) break;
|
|
|
|
|
2008-04-19 11:09:47 +00:00
|
|
|
Entity *isfor = SS.GetEntity(h.request().entity(0));
|
|
|
|
if(!SS.GW.show2dCsyss && isfor->type == Entity::CSYS_2D) break;
|
|
|
|
|
2008-04-19 11:44:44 +00:00
|
|
|
Vector v = PointGetCoords();
|
2008-04-19 11:09:47 +00:00
|
|
|
|
|
|
|
if(dogd.drawing) {
|
2008-04-22 10:53:42 +00:00
|
|
|
double s = 3;
|
2008-04-19 11:09:47 +00:00
|
|
|
Vector r = SS.GW.projRight.ScaledBy(s/SS.GW.scale);
|
|
|
|
Vector d = SS.GW.projUp.ScaledBy(s/SS.GW.scale);
|
|
|
|
|
|
|
|
glxColor(0, 0.8, 0);
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glxVertex3v(v.Plus (r).Plus (d));
|
|
|
|
glxVertex3v(v.Plus (r).Minus(d));
|
|
|
|
glxVertex3v(v.Minus(r).Minus(d));
|
|
|
|
glxVertex3v(v.Minus(r).Plus (d));
|
|
|
|
glEnd();
|
|
|
|
} else {
|
|
|
|
Point2d pp = SS.GW.ProjectPoint(v);
|
2008-04-22 13:14:15 +00:00
|
|
|
dogd.dmin = pp.DistanceTo(dogd.mp) - 5;
|
2008-04-19 11:09:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
case CSYS_2D: {
|
2008-04-22 10:53:42 +00:00
|
|
|
if(order >= 0 && order != 0) break;
|
2008-04-19 11:09:47 +00:00
|
|
|
if(!SS.GW.show2dCsyss) break;
|
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
Vector p;
|
2008-04-19 11:44:44 +00:00
|
|
|
p = SS.GetEntity(assoc[0])->PointGetCoords();
|
2008-04-01 10:48:44 +00:00
|
|
|
|
2008-04-13 14:28:35 +00:00
|
|
|
Vector u, v;
|
2008-04-19 11:44:44 +00:00
|
|
|
Csys2dGetBasisVectors(&u, &v);
|
2008-04-01 10:48:44 +00:00
|
|
|
|
2008-04-13 10:57:41 +00:00
|
|
|
double s = (min(SS.GW.width, SS.GW.height))*0.4/SS.GW.scale;
|
2008-04-01 10:48:44 +00:00
|
|
|
|
2008-04-13 10:57:41 +00:00
|
|
|
Vector us = u.ScaledBy(s);
|
|
|
|
Vector vs = v.ScaledBy(s);
|
|
|
|
|
|
|
|
Vector pp = p.Plus (us).Plus (vs);
|
|
|
|
Vector pm = p.Plus (us).Minus(vs);
|
|
|
|
Vector mm = p.Minus(us).Minus(vs);
|
|
|
|
Vector mp = p.Minus(us).Plus (vs);
|
2008-04-11 11:13:47 +00:00
|
|
|
|
2008-04-19 11:09:47 +00:00
|
|
|
glxColor(0, 0.4, 0.4);
|
2008-04-12 14:12:26 +00:00
|
|
|
LineDrawOrGetDistance(pp, pm);
|
|
|
|
LineDrawOrGetDistance(pm, mm);
|
|
|
|
LineDrawOrGetDistance(mm, mp);
|
|
|
|
LineDrawOrGetDistance(mp, pp);
|
2008-04-01 10:48:44 +00:00
|
|
|
|
2008-04-12 14:12:26 +00:00
|
|
|
if(dogd.drawing) {
|
|
|
|
glPushMatrix();
|
|
|
|
glxTranslatev(mm);
|
|
|
|
glxOntoCsys(u, v);
|
2008-04-19 11:09:47 +00:00
|
|
|
glxWriteText(DescriptionString());
|
2008-04-12 14:12:26 +00:00
|
|
|
glPopMatrix();
|
|
|
|
}
|
2008-04-09 08:39:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-04-13 10:57:41 +00:00
|
|
|
|
|
|
|
case LINE_SEGMENT: {
|
2008-04-22 10:53:42 +00:00
|
|
|
if(order >= 0 && order != 1) break;
|
2008-04-19 11:44:44 +00:00
|
|
|
Vector a = SS.GetEntity(assoc[0])->PointGetCoords();
|
|
|
|
Vector b = SS.GetEntity(assoc[1])->PointGetCoords();
|
2008-04-13 10:57:41 +00:00
|
|
|
LineDrawOrGetDistance(a, b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
default:
|
|
|
|
oops();
|
2008-04-01 10:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-12 15:17:58 +00:00
|
|
|
|