Skip to content

Commit

Permalink
Create Frustum Extrusions
Browse files Browse the repository at this point in the history
  • Loading branch information
phkahler committed Jun 12, 2020
1 parent b3f739f commit 6354cbd
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/drawentity.cpp
Expand Up @@ -86,6 +86,7 @@ void Entity::GetReferencePoints(std::vector<Vector> *refs) {
switch(type) {
case Type::POINT_N_COPY:
case Type::POINT_N_TRANS:
case Type::POINT_SCALE_TRANS:
case Type::POINT_N_ROT_TRANS:
case Type::POINT_N_ROT_AA:
case Type::POINT_IN_3D:
Expand Down Expand Up @@ -500,6 +501,7 @@ void Entity::Draw(DrawAs how, Canvas *canvas) {
switch(type) {
case Type::POINT_N_COPY:
case Type::POINT_N_TRANS:
case Type::POINT_SCALE_TRANS:
case Type::POINT_N_ROT_TRANS:
case Type::POINT_N_ROT_AA:
case Type::POINT_IN_3D:
Expand Down
37 changes: 37 additions & 0 deletions src/entity.cpp
Expand Up @@ -243,6 +243,7 @@ bool EntityBase::IsPoint() const {
case Type::POINT_IN_2D:
case Type::POINT_N_COPY:
case Type::POINT_N_TRANS:
case Type::POINT_SCALE_TRANS:
case Type::POINT_N_ROT_TRANS:
case Type::POINT_N_ROT_AA:
return true;
Expand Down Expand Up @@ -428,6 +429,17 @@ void EntityBase::PointForceTo(Vector p) {
break;
}

case Type::POINT_SCALE_TRANS: {
if(timesApplied == 0) break;
Vector center = Vector::From(param[4], param[5], param[6]);
Vector sp = numPoint.Minus(center).ScaledBy(SK.GetParam(param[3])->val).Plus(center);
Vector trans = (p.Minus(sp));
SK.GetParam(param[0])->val = trans.x;
SK.GetParam(param[1])->val = trans.y;
SK.GetParam(param[2])->val = trans.z;
break;
}

case Type::POINT_N_ROT_TRANS: {
// Force only the translation; leave the rotation unchanged. But
// remember that we're working with respect to the rotated
Expand Down Expand Up @@ -489,6 +501,18 @@ Vector EntityBase::PointGetNum() const {
break;
}

case Type::POINT_SCALE_TRANS: {
Vector trans = Vector::From(param[0], param[1], param[2]);
Vector center = Vector::From(param[4], param[5], param[6]);
if (timesApplied == 0) {
p = numPoint;
} else {
p = numPoint.Minus(center).ScaledBy(SK.GetParam(param[3])->val)
.Plus(center).Plus(trans);
}
break;
}

case Type::POINT_N_ROT_TRANS: {
Vector offset = Vector::From(param[0], param[1], param[2]);
Quaternion q = PointGetQuaternion();
Expand Down Expand Up @@ -537,6 +561,19 @@ ExprVector EntityBase::PointGetExprs() const {
r = orig.Plus(trans.ScaledBy(Expr::From(timesApplied)));
break;
}
case Type::POINT_SCALE_TRANS: {
ExprVector orig = ExprVector::From(numPoint);
ExprVector trans = ExprVector::From(param[0], param[1], param[2]);
ExprVector center = ExprVector::From(param[4], param[5], param[6]);
if (timesApplied != 0) {
r = orig.Minus(center).ScaledBy(Expr::From(param[3])).Plus(center).Plus(trans);
// r = orig.ScaledBy(Expr::From(param[3])).Plus(center.ScaledBy(Expr::From(1.0)->Minus(Expr::From(param[3])) ))
// .Plus(trans);
} else {
r = orig;
}
break;
}
case Type::POINT_N_ROT_TRANS: {
ExprVector orig = ExprVector::From(numPoint);
ExprVector trans = ExprVector::From(param[0], param[1], param[2]);
Expand Down
1 change: 1 addition & 0 deletions src/generate.cpp
Expand Up @@ -444,6 +444,7 @@ void SolveSpaceUI::MarkDraggedParams() {
if(pt) {
switch(pt->type) {
case Entity::Type::POINT_N_TRANS:
case Entity::Type::POINT_SCALE_TRANS:
case Entity::Type::POINT_IN_3D:
sys.dragged.Add(&(pt->param[0]));
sys.dragged.Add(&(pt->param[1]));
Expand Down
83 changes: 81 additions & 2 deletions src/group.cpp
Expand Up @@ -147,7 +147,7 @@ void Group::MenuGroup(Command id, Platform::Path linkFile) {
return;
}
break;

/*
case Command::GROUP_EXTRUDE:
if(!SS.GW.LockedInWorkplane()) {
Error(_("Activate a workplane (Sketch -> In Workplane) before "
Expand All @@ -161,6 +161,21 @@ void Group::MenuGroup(Command id, Platform::Path linkFile) {
g.subtype = Subtype::ONE_SIDED;
g.name = C_("group-name", "extrude");
break;
*/
case Command::GROUP_EXTRUDE: // FRUSTUM
if(!SS.GW.LockedInWorkplane() || (gs.points != 1)) {
Error(_("Activate a workplane (Sketch -> In Workplane) before "
"extruding. The sketch will be extruded normal to the "
"workplane."));
return;
}
g.predef.origin = gs.point[0];
g.type = Type::FRUSTUM;
g.opA = SS.GW.activeGroup;
g.predef.entityB = SS.GW.ActiveWorkplane();
g.subtype = Subtype::ONE_SIDED;
g.name = C_("group-name", "frustum");
break;

case Command::GROUP_LATHE:
if(gs.points == 1 && gs.vectors == 1 && gs.n == 2) {
Expand Down Expand Up @@ -474,6 +489,51 @@ void Group::Generate(IdList<Entity,hEntity> *entity,
return;
}

case Type::FRUSTUM: {
Vector center = SK.GetEntity(predef.origin)->PointGetNum();

AddParam(param, h.param(0), gn.x);
AddParam(param, h.param(1), gn.y);
AddParam(param, h.param(2), gn.z);

// The free parameter for relative size
AddParam(param, h.param(3), 0.5);

AddParam(param, h.param(4), center.x);
AddParam(param, h.param(5), center.y);
AddParam(param, h.param(6), center.z);

int ai, af;
ai = 0; af = 1;
// Get some arbitrary point in the sketch, that will be used
// as a reference when defining top and bottom faces.
hEntity pt = { 0 };
for(i = 0; i < entity->n; i++) {
Entity *e = &(entity->elem[i]);
if(e->group.v != opA.v) continue;

if(e->IsPoint()) pt = e->h;

e->CalculateNumerical(/*forExport=*/false);
hEntity he = e->h; e = NULL;
// As soon as I call CopyEntity, e may become invalid! That
// adds entities, which may cause a realloc.
CopyEntity(entity, SK.GetEntity(he), ai, REMAP_BOTTOM,
h.param(0), h.param(1), h.param(2),
h.param(3), h.param(4), h.param(5), h.param(6),
CopyAs::NUMERIC);
CopyEntity(entity, SK.GetEntity(he), af, REMAP_TOP,
h.param(0), h.param(1), h.param(2),
h.param(3), h.param(4), h.param(5), h.param(6),
CopyAs::SCALE_TRANS);
MakeExtrusionLines(entity, he);
}
// Remapped versions of that arbitrary point will be used to
// provide points on the plane faces.
MakeExtrusionTopBottomFaces(entity, pt);
return;
}

case Type::LATHE: {
Vector axis_pos = SK.GetEntity(predef.origin)->PointGetNum();
Vector axis_dir = SK.GetEntity(predef.entityB)->VectorGetNum();
Expand Down Expand Up @@ -701,6 +761,15 @@ void Group::GenerateEquations(IdList<Equation,hEquation> *l) {
AddEq(l, u.Dot(extruden), 0);
AddEq(l, v.Dot(extruden), 1);
}
} else if(type == Type::FRUSTUM) {
if(predef.entityB != Entity::FREE_IN_3D) {
// The center for scaling is locked in place
Vector center = SK.GetEntity(predef.origin)->PointGetNum();

AddEq(l, Expr::From(h.param(4))->Minus(Expr::From(center.x)), 0);
AddEq(l, Expr::From(h.param(5))->Minus(Expr::From(center.y)), 1);
AddEq(l, Expr::From(h.param(6))->Minus(Expr::From(center.z)), 2);
}
} else if(type == Type::TRANSLATE) {
if(predef.entityB != Entity::FREE_IN_3D) {
Entity *w = SK.GetEntity(predef.entityB);
Expand Down Expand Up @@ -893,11 +962,21 @@ void Group::CopyEntity(IdList<Entity,hEntity> *el,
case Entity::Type::POINT_N_ROT_AA:
case Entity::Type::POINT_IN_3D:
case Entity::Type::POINT_IN_2D:
case Entity::Type::POINT_SCALE_TRANS:
if(as == CopyAs::N_TRANS) {
en.type = Entity::Type::POINT_N_TRANS;
en.param[0] = dx;
en.param[1] = dy;
en.param[2] = dz;
} else if(as == CopyAs::SCALE_TRANS) {
en.type = Entity::Type::POINT_SCALE_TRANS;
en.param[0] = dx;
en.param[1] = dy;
en.param[2] = dz;
en.param[3] = qw;
en.param[4] = qvx;
en.param[5] = qvy;
en.param[6] = qvz;
} else if(as == CopyAs::NUMERIC) {
en.type = Entity::Type::POINT_N_COPY;
} else {
Expand All @@ -922,7 +1001,7 @@ void Group::CopyEntity(IdList<Entity,hEntity> *el,
case Entity::Type::NORMAL_N_ROT_AA:
case Entity::Type::NORMAL_IN_3D:
case Entity::Type::NORMAL_IN_2D:
if(as == CopyAs::N_TRANS || as == CopyAs::NUMERIC) {
if(as == CopyAs::N_TRANS || as == CopyAs::NUMERIC || as == CopyAs::SCALE_TRANS) {
en.type = Entity::Type::NORMAL_N_COPY;
} else {
if(as == CopyAs::N_ROT_AA) {
Expand Down
17 changes: 14 additions & 3 deletions src/groupmesh.cpp
Expand Up @@ -213,23 +213,34 @@ void Group::GenerateShellAndMesh() {
GenerateForStepAndRepeat<SMesh> (&prevm, &thisMesh, srcg->meshCombine);
}
}
} else if(type == Type::EXTRUDE && haveSrc) {
} else if((type == Type::EXTRUDE || type == Type::FRUSTUM) && haveSrc) {
Group *src = SK.GetGroup(opA);
Vector translate = Vector::From(h.param(0), h.param(1), h.param(2));

Vector origin = Vector::From(0, 0, 0);
Vector tbot, ttop;
if(subtype == Subtype::ONE_SIDED) {
tbot = Vector::From(0, 0, 0); ttop = translate.ScaledBy(2);
} else {
tbot = translate.ScaledBy(-1); ttop = translate.ScaledBy(1);
}
double scale = 0.0;
if (type == Type::FRUSTUM) {
ttop = translate;
//origin = SK.GetEntity(predef.origin)->PointGetNum();
origin = Vector::From(h.param(4), h.param(5), h.param(6));
scale = SK.GetParam(h.param(3))->val;
}

SBezierLoopSetSet *sblss = &(src->bezierLoops);
SBezierLoopSet *sbls;
for(sbls = sblss->l.First(); sbls; sbls = sblss->l.NextAfter(sbls)) {
int is = thisShell.surface.n;
// Extrude this outer contour (plus its inner contours, if present)
thisShell.MakeFromExtrusionOf(sbls, tbot, ttop, color);
if (type == Type::EXTRUDE) {
thisShell.MakeFromExtrusionOf(sbls, tbot, ttop, color);
} else {
thisShell.MakeFrustumOf(sbls, tbot, ttop, origin, 1.0, scale, color);
}

// And for any plane faces, annotate the model with the entity for
// that face, so that the user can select them with the mouse.
Expand Down
3 changes: 3 additions & 0 deletions src/sketch.h
Expand Up @@ -151,6 +151,7 @@ class Group {
N_TRANS,
N_ROT_AA,
N_ROT_TRANS,
SCALE_TRANS,
};

enum class Type : uint32_t {
Expand All @@ -159,6 +160,7 @@ class Group {
EXTRUDE = 5100,
LATHE = 5101,
REVOLVE = 5102,
FRUSTUM = 5110,
ROTATE = 5200,
TRANSLATE = 5201,
LINKED = 5300
Expand Down Expand Up @@ -387,6 +389,7 @@ class EntityBase {
POINT_N_ROT_TRANS = 2011,
POINT_N_COPY = 2012,
POINT_N_ROT_AA = 2013,
POINT_SCALE_TRANS = 2014,

NORMAL_IN_3D = 3000,
NORMAL_IN_2D = 3001,
Expand Down

0 comments on commit 6354cbd

Please sign in to comment.