2013-07-29 06:08:34 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// User-initiated (not parametric) operations to modify our sketch, by
|
|
|
|
// changing the requests, like to round a corner or split curves where they
|
|
|
|
// intersect.
|
|
|
|
//
|
|
|
|
// Copyright 2008-2013 Jonathan Westhues.
|
|
|
|
//-----------------------------------------------------------------------------
|
2009-01-03 20:27:33 +08:00
|
|
|
#include "solvespace.h"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-04-07 22:44:56 +08:00
|
|
|
// Replace constraints on oldpt with the same constraints on newpt.
|
|
|
|
// Useful when splitting, tangent arcing, or removing bezier points.
|
2009-01-03 20:27:33 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void GraphicsWindow::ReplacePointInConstraints(hEntity oldpt, hEntity newpt) {
|
2018-01-04 09:42:38 +08:00
|
|
|
for(auto &c : SK.constraint) {
|
|
|
|
if(c.ptA == oldpt)
|
|
|
|
c.ptA = newpt;
|
|
|
|
if(c.ptB == oldpt)
|
|
|
|
c.ptB = newpt;
|
2009-01-03 20:27:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 22:44:56 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Remove constraints on hpt. Useful when removing bezier points.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void GraphicsWindow::RemoveConstraintsForPointBeingDeleted(hEntity hpt) {
|
|
|
|
SK.constraint.ClearTags();
|
2018-01-04 09:42:38 +08:00
|
|
|
for(auto &c : SK.constraint) {
|
|
|
|
if(c.ptA == hpt || c.ptB == hpt) {
|
|
|
|
c.tag = 1;
|
2016-04-07 22:44:56 +08:00
|
|
|
(SS.deleted.constraints)++;
|
2018-01-04 09:42:38 +08:00
|
|
|
if(c.type != Constraint::Type::POINTS_COINCIDENT &&
|
|
|
|
c.type != Constraint::Type::HORIZONTAL &&
|
|
|
|
c.type != Constraint::Type::VERTICAL)
|
2016-04-07 22:44:56 +08:00
|
|
|
{
|
|
|
|
(SS.deleted.nonTrivialConstraints)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SK.constraint.RemoveTagged();
|
|
|
|
}
|
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Let's say that A is coincident with B, and B is coincident with C. This
|
|
|
|
// implies that A is coincident with C; but if we delete B, then both
|
|
|
|
// constraints must be deleted too (since they reference B), and A is no
|
|
|
|
// longer constrained to C. This routine adds back that constraint.
|
|
|
|
//-----------------------------------------------------------------------------
|
2009-07-08 17:36:18 +08:00
|
|
|
void GraphicsWindow::FixConstraintsForRequestBeingDeleted(hRequest hr) {
|
|
|
|
Request *r = SK.GetRequest(hr);
|
2019-07-09 22:44:57 +08:00
|
|
|
if(r->group != SS.GW.activeGroup) return;
|
2009-07-08 17:36:18 +08:00
|
|
|
|
|
|
|
Entity *e;
|
|
|
|
for(e = SK.entity.First(); e; e = SK.entity.NextAfter(e)) {
|
|
|
|
if(!(e->h.isFromRequest())) continue;
|
2019-07-09 22:44:57 +08:00
|
|
|
if(e->h.request() != hr) continue;
|
2009-07-08 17:36:18 +08:00
|
|
|
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
if(e->type != Entity::Type::POINT_IN_2D &&
|
|
|
|
e->type != Entity::Type::POINT_IN_3D)
|
2009-07-08 17:36:18 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a point generated by the request being deleted; so fix
|
|
|
|
// the constraints for that.
|
|
|
|
FixConstraintsForPointBeingDeleted(e->h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void GraphicsWindow::FixConstraintsForPointBeingDeleted(hEntity hpt) {
|
2015-03-27 23:31:23 +08:00
|
|
|
List<hEntity> ld = {};
|
2009-07-08 17:36:18 +08:00
|
|
|
|
|
|
|
Constraint *c;
|
|
|
|
SK.constraint.ClearTags();
|
|
|
|
for(c = SK.constraint.First(); c; c = SK.constraint.NextAfter(c)) {
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
if(c->type != Constraint::Type::POINTS_COINCIDENT) continue;
|
2019-07-09 22:44:57 +08:00
|
|
|
if(c->group != SS.GW.activeGroup) continue;
|
2009-07-08 17:36:18 +08:00
|
|
|
|
2019-07-09 22:44:57 +08:00
|
|
|
if(c->ptA == hpt) {
|
2009-07-08 17:36:18 +08:00
|
|
|
ld.Add(&(c->ptB));
|
|
|
|
c->tag = 1;
|
|
|
|
}
|
2019-07-09 22:44:57 +08:00
|
|
|
if(c->ptB == hpt) {
|
2009-07-08 17:36:18 +08:00
|
|
|
ld.Add(&(c->ptA));
|
|
|
|
c->tag = 1;
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 22:44:56 +08:00
|
|
|
// Remove constraints without waiting for regeneration; this way
|
|
|
|
// if another point takes the place of the deleted one (e.g. when
|
|
|
|
// removing control points of a bezier) the constraint doesn't
|
|
|
|
// spuriously move. Similarly, subsequent calls of this function
|
|
|
|
// (if multiple coincident points are getting deleted) will work
|
|
|
|
// correctly.
|
2009-07-08 17:36:18 +08:00
|
|
|
SK.constraint.RemoveTagged();
|
|
|
|
|
|
|
|
// If more than one point was constrained coincident with hpt, then
|
|
|
|
// those two points were implicitly coincident with each other. By
|
|
|
|
// deleting hpt (and all constraints that mention it), we will delete
|
|
|
|
// that relationship. So put it back here now.
|
2019-08-21 00:22:10 +08:00
|
|
|
for(int i = 1; i < ld.n; i++) {
|
2019-05-24 01:09:47 +08:00
|
|
|
Constraint::ConstrainCoincident(ld[i-1], ld[i]);
|
2009-07-08 17:36:18 +08:00
|
|
|
}
|
|
|
|
ld.Clear();
|
|
|
|
}
|
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// A curve by its parametric equation, helper functions for computing tangent
|
|
|
|
// arcs by a numerical method.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void GraphicsWindow::ParametricCurve::MakeFromEntity(hEntity he, bool reverse) {
|
2015-03-27 23:31:23 +08:00
|
|
|
*this = {};
|
2010-05-17 00:36:23 +08:00
|
|
|
Entity *e = SK.GetEntity(he);
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
if(e->type == Entity::Type::LINE_SEGMENT) {
|
2010-05-17 00:36:23 +08:00
|
|
|
isLine = true;
|
|
|
|
p0 = e->EndpointStart(),
|
|
|
|
p1 = e->EndpointFinish();
|
|
|
|
if(reverse) {
|
2015-03-27 23:43:28 +08:00
|
|
|
swap(p0, p1);
|
2010-05-17 00:36:23 +08:00
|
|
|
}
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
} else if(e->type == Entity::Type::ARC_OF_CIRCLE) {
|
2010-05-17 00:36:23 +08:00
|
|
|
isLine = false;
|
|
|
|
p0 = SK.GetEntity(e->point[0])->PointGetNum();
|
|
|
|
Vector pe = SK.GetEntity(e->point[1])->PointGetNum();
|
|
|
|
r = (pe.Minus(p0)).Magnitude();
|
|
|
|
e->ArcGetAngles(&theta0, &theta1, &dtheta);
|
|
|
|
if(reverse) {
|
2015-03-27 23:43:28 +08:00
|
|
|
swap(theta0, theta1);
|
2010-05-17 00:36:23 +08:00
|
|
|
dtheta = -dtheta;
|
|
|
|
}
|
|
|
|
EntityBase *wrkpln = SK.GetEntity(e->workplane)->Normal();
|
|
|
|
u = wrkpln->NormalU();
|
|
|
|
v = wrkpln->NormalV();
|
2016-05-19 06:51:36 +08:00
|
|
|
} else ssassert(false, "Unexpected entity type");
|
2010-05-17 00:36:23 +08:00
|
|
|
}
|
2016-05-05 13:54:05 +08:00
|
|
|
double GraphicsWindow::ParametricCurve::LengthForAuto() {
|
2010-05-17 00:36:23 +08:00
|
|
|
if(isLine) {
|
|
|
|
// Allow a third of the line to disappear with auto radius
|
|
|
|
return (p1.Minus(p0)).Magnitude() / 3;
|
|
|
|
} else {
|
|
|
|
// But only a twentieth of the arc; shorter means fewer numerical
|
|
|
|
// problems since the curve is more linear over shorter sections.
|
|
|
|
return (fabs(dtheta)*r)/20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Vector GraphicsWindow::ParametricCurve::PointAt(double t) {
|
|
|
|
if(isLine) {
|
|
|
|
return p0.Plus((p1.Minus(p0)).ScaledBy(t));
|
|
|
|
} else {
|
|
|
|
double theta = theta0 + dtheta*t;
|
|
|
|
return p0.Plus(u.ScaledBy(r*cos(theta)).Plus(v.ScaledBy(r*sin(theta))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Vector GraphicsWindow::ParametricCurve::TangentAt(double t) {
|
|
|
|
if(isLine) {
|
|
|
|
return p1.Minus(p0);
|
|
|
|
} else {
|
|
|
|
double theta = theta0 + dtheta*t;
|
|
|
|
Vector t = u.ScaledBy(-r*sin(theta)).Plus(v.ScaledBy(r*cos(theta)));
|
|
|
|
t = t.ScaledBy(dtheta);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 02:56:28 +08:00
|
|
|
/** Changes or copies the given entity and connects it to the arc.
|
|
|
|
* \param t Where on this parametric curve does it connect to the arc.
|
|
|
|
* \param reuseOrig Should the original entity be modified
|
|
|
|
* \param orig The original entity.
|
|
|
|
* \param arc The arc that will be connected to.
|
|
|
|
* \param arcFinish Whether to connect to the end point of the arc.
|
|
|
|
* \param pointf When changing the original entity, whether the end point should be modified.
|
|
|
|
*/
|
|
|
|
void GraphicsWindow::ParametricCurve::CreateRequestTrimmedTo(double t,
|
|
|
|
bool reuseOrig, hEntity orig, hEntity arc, bool arcFinish, bool pointf)
|
2010-05-17 00:36:23 +08:00
|
|
|
{
|
|
|
|
hRequest hr;
|
|
|
|
Entity *e;
|
|
|
|
if(isLine) {
|
2018-07-23 02:56:28 +08:00
|
|
|
if (reuseOrig) {
|
|
|
|
e = SK.GetEntity(orig);
|
|
|
|
int i = pointf ? 1 : 0;
|
|
|
|
SK.GetEntity(e->point[i])->PointForceTo(PointAt(t));
|
|
|
|
ConstrainPointIfCoincident(e->point[i]);
|
|
|
|
} else {
|
|
|
|
hr = SS.GW.AddRequest(Request::Type::LINE_SEGMENT, /*rememberForUndo=*/false),
|
|
|
|
e = SK.GetEntity(hr.entity(0));
|
|
|
|
SK.GetEntity(e->point[0])->PointForceTo(PointAt(t));
|
|
|
|
SK.GetEntity(e->point[1])->PointForceTo(PointAt(1));
|
|
|
|
ConstrainPointIfCoincident(e->point[0]);
|
|
|
|
ConstrainPointIfCoincident(e->point[1]);
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
Constraint::Constrain(Constraint::Type::PT_ON_LINE,
|
2010-05-17 00:36:23 +08:00
|
|
|
hr.entity(1), Entity::NO_ENTITY, orig);
|
|
|
|
}
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
Constraint::Constrain(Constraint::Type::ARC_LINE_TANGENT,
|
2010-05-17 00:36:23 +08:00
|
|
|
Entity::NO_ENTITY, Entity::NO_ENTITY,
|
2016-05-25 20:08:19 +08:00
|
|
|
arc, e->h, /*other=*/arcFinish, /*other2=*/false);
|
2010-05-17 00:36:23 +08:00
|
|
|
} else {
|
2018-07-23 02:56:28 +08:00
|
|
|
if (reuseOrig) {
|
|
|
|
e = SK.GetEntity(orig);
|
|
|
|
int i = pointf ? 2 : 1;
|
|
|
|
SK.GetEntity(e->point[i])->PointForceTo(PointAt(t));
|
|
|
|
ConstrainPointIfCoincident(e->point[i]);
|
2010-05-17 00:36:23 +08:00
|
|
|
} else {
|
2018-07-23 02:56:28 +08:00
|
|
|
hr = SS.GW.AddRequest(Request::Type::ARC_OF_CIRCLE, /*rememberForUndo=*/false),
|
|
|
|
e = SK.GetEntity(hr.entity(0));
|
|
|
|
SK.GetEntity(e->point[0])->PointForceTo(p0);
|
|
|
|
if(dtheta > 0) {
|
|
|
|
SK.GetEntity(e->point[1])->PointForceTo(PointAt(t));
|
|
|
|
SK.GetEntity(e->point[2])->PointForceTo(PointAt(1));
|
|
|
|
} else {
|
|
|
|
SK.GetEntity(e->point[2])->PointForceTo(PointAt(t));
|
|
|
|
SK.GetEntity(e->point[1])->PointForceTo(PointAt(1));
|
|
|
|
}
|
|
|
|
ConstrainPointIfCoincident(e->point[0]);
|
|
|
|
ConstrainPointIfCoincident(e->point[1]);
|
|
|
|
ConstrainPointIfCoincident(e->point[2]);
|
2010-05-17 00:36:23 +08:00
|
|
|
}
|
|
|
|
// The tangency constraint alone is enough to fully constrain it,
|
|
|
|
// so there's no need for more.
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
Constraint::Constrain(Constraint::Type::CURVE_CURVE_TANGENT,
|
2010-05-17 00:36:23 +08:00
|
|
|
Entity::NO_ENTITY, Entity::NO_ENTITY,
|
2016-05-25 20:08:19 +08:00
|
|
|
arc, e->h, /*other=*/arcFinish, /*other2=*/(dtheta < 0));
|
2010-05-17 00:36:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// If a point in the same group as hpt, and numerically coincident with hpt,
|
|
|
|
// happens to exist, then constrain that point coincident to hpt.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void GraphicsWindow::ParametricCurve::ConstrainPointIfCoincident(hEntity hpt) {
|
|
|
|
Entity *e, *pt;
|
|
|
|
pt = SK.GetEntity(hpt);
|
|
|
|
Vector ev, ptv;
|
|
|
|
ptv = pt->PointGetNum();
|
|
|
|
|
|
|
|
for(e = SK.entity.First(); e; e = SK.entity.NextAfter(e)) {
|
2019-07-09 22:44:57 +08:00
|
|
|
if(e->h == pt->h) continue;
|
2010-05-17 00:36:23 +08:00
|
|
|
if(!e->IsPoint()) continue;
|
2019-07-09 22:44:57 +08:00
|
|
|
if(e->group != pt->group) continue;
|
|
|
|
if(e->workplane != pt->workplane) continue;
|
2015-03-29 08:30:52 +08:00
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
ev = e->PointGetNum();
|
|
|
|
if(!ev.Equals(ptv)) continue;
|
|
|
|
|
|
|
|
Constraint::ConstrainCoincident(hpt, e->h);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-03 20:27:33 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// A single point must be selected when this function is called. We find two
|
|
|
|
// non-construction line segments that join at this point, and create a
|
|
|
|
// tangent arc joining them.
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-05 13:54:05 +08:00
|
|
|
void GraphicsWindow::MakeTangentArc() {
|
2009-01-03 20:27:33 +08:00
|
|
|
if(!LockedInWorkplane()) {
|
2017-01-07 14:41:13 +08:00
|
|
|
Error(_("Must be sketching in workplane to create tangent arc."));
|
2009-01-03 20:27:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
// The point corresponding to the vertex to be rounded.
|
2009-04-19 13:53:16 +08:00
|
|
|
Vector pshared = SK.GetEntity(gs.point[0])->PointGetNum();
|
2009-01-03 20:27:33 +08:00
|
|
|
ClearSelection();
|
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
// First, find two requests (that are not construction, and that are
|
|
|
|
// in our group and workplane) that generate entities that have an
|
|
|
|
// endpoint at our vertex to be rounded.
|
2009-01-03 20:27:33 +08:00
|
|
|
int i, c = 0;
|
2010-05-17 00:36:23 +08:00
|
|
|
Entity *ent[2];
|
|
|
|
Request *req[2];
|
|
|
|
hRequest hreq[2];
|
|
|
|
hEntity hent[2];
|
|
|
|
bool pointf[2];
|
2018-01-04 09:42:38 +08:00
|
|
|
for(auto &r : SK.request) {
|
|
|
|
if(r.group != activeGroup)
|
|
|
|
continue;
|
|
|
|
if(r.workplane != ActiveWorkplane())
|
|
|
|
continue;
|
|
|
|
if(r.construction)
|
|
|
|
continue;
|
|
|
|
if(r.type != Request::Type::LINE_SEGMENT && r.type != Request::Type::ARC_OF_CIRCLE) {
|
2010-05-17 00:36:23 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2018-01-04 09:42:38 +08:00
|
|
|
Entity *e = SK.GetEntity(r.h.entity(0));
|
2010-05-17 00:36:23 +08:00
|
|
|
Vector ps = e->EndpointStart(),
|
|
|
|
pf = e->EndpointFinish();
|
2015-03-29 08:30:52 +08:00
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
if(ps.Equals(pshared) || pf.Equals(pshared)) {
|
2009-01-03 20:27:33 +08:00
|
|
|
if(c < 2) {
|
2010-05-17 00:36:23 +08:00
|
|
|
// We record the entity and request and their handles,
|
|
|
|
// and whether the vertex to be rounded is the start or
|
|
|
|
// finish of this entity.
|
|
|
|
ent[c] = e;
|
|
|
|
hent[c] = e->h;
|
2018-01-04 09:42:38 +08:00
|
|
|
req[c] = &r;
|
|
|
|
hreq[c] = r.h;
|
2010-05-17 00:36:23 +08:00
|
|
|
pointf[c] = (pf.Equals(pshared));
|
2009-01-03 20:27:33 +08:00
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(c != 2) {
|
2017-01-07 14:41:13 +08:00
|
|
|
Error(_("To create a tangent arc, select a point where two "
|
|
|
|
"non-construction lines or circles in this group and "
|
|
|
|
"workplane join."));
|
2009-01-03 20:27:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
Entity *wrkpl = SK.GetEntity(ActiveWorkplane());
|
2009-01-03 20:27:33 +08:00
|
|
|
Vector wn = wrkpl->Normal()->NormalN();
|
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
// Based on these two entities, we make the objects that we'll use to
|
|
|
|
// numerically find the tangent arc.
|
|
|
|
ParametricCurve pc[2];
|
|
|
|
pc[0].MakeFromEntity(ent[0]->h, pointf[0]);
|
|
|
|
pc[1].MakeFromEntity(ent[1]->h, pointf[1]);
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
// And thereafter we mustn't touch the entity or req ptrs,
|
|
|
|
// because the new requests/entities we add might force a
|
|
|
|
// realloc.
|
|
|
|
memset(ent, 0, sizeof(ent));
|
|
|
|
memset(req, 0, sizeof(req));
|
|
|
|
|
|
|
|
Vector pinter;
|
2013-08-27 03:36:00 +08:00
|
|
|
double r = 0.0, vv = 0.0;
|
2010-05-17 00:36:23 +08:00
|
|
|
// We now do Newton iterations to find the tangent arc, and its positions
|
|
|
|
// t back along the two curves, starting from shared point of the curves
|
|
|
|
// at t = 0. Lots of iterations helps convergence, and this is still
|
|
|
|
// ~10 ms for everything.
|
|
|
|
int iters = 1000;
|
|
|
|
double t[2] = { 0, 0 }, tp[2];
|
|
|
|
for(i = 0; i < iters + 20; i++) {
|
|
|
|
Vector p0 = pc[0].PointAt(t[0]),
|
|
|
|
p1 = pc[1].PointAt(t[1]),
|
|
|
|
t0 = pc[0].TangentAt(t[0]),
|
|
|
|
t1 = pc[1].TangentAt(t[1]);
|
|
|
|
|
|
|
|
pinter = Vector::AtIntersectionOfLines(p0, p0.Plus(t0),
|
|
|
|
p1, p1.Plus(t1),
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
|
|
|
|
// The sign of vv determines whether shortest distance is
|
|
|
|
// clockwise or anti-clockwise.
|
|
|
|
Vector v = (wn.Cross(t0)).WithMagnitude(1);
|
|
|
|
vv = t1.Dot(v);
|
|
|
|
|
|
|
|
double dot = (t0.WithMagnitude(1)).Dot(t1.WithMagnitude(1));
|
|
|
|
double theta = acos(dot);
|
|
|
|
|
|
|
|
if(SS.tangentArcManual) {
|
|
|
|
r = SS.tangentArcRadius;
|
|
|
|
} else {
|
|
|
|
r = 200/scale;
|
2015-03-29 08:30:52 +08:00
|
|
|
// Set the radius so that no more than one third of the
|
2010-05-17 00:36:23 +08:00
|
|
|
// line segment disappears.
|
|
|
|
r = min(r, pc[0].LengthForAuto()*tan(theta/2));
|
|
|
|
r = min(r, pc[1].LengthForAuto()*tan(theta/2));;
|
|
|
|
}
|
|
|
|
// We are source-stepping the radius, to improve convergence. So
|
|
|
|
// ramp that for most of the iterations, and then do a few at
|
|
|
|
// the end with that constant for polishing.
|
|
|
|
if(i < iters) {
|
|
|
|
r *= 0.1 + 0.9*i/((double)iters);
|
|
|
|
}
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
// The distance from the intersection of the lines to the endpoint
|
|
|
|
// of the arc, along each line.
|
|
|
|
double el = r/tan(theta/2);
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
// Compute the endpoints of the arc, for each curve
|
|
|
|
Vector pa0 = pinter.Plus(t0.WithMagnitude(el)),
|
|
|
|
pa1 = pinter.Plus(t1.WithMagnitude(el));
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
tp[0] = t[0];
|
|
|
|
tp[1] = t[1];
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
// And convert those points to parameter values along the curve.
|
2019-09-20 09:09:25 +08:00
|
|
|
t[0] += (pa0.Minus(p0)).DivProjected(t0);
|
|
|
|
t[1] += (pa1.Minus(p1)).DivProjected(t1);
|
2010-05-17 00:36:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stupid check for convergence, and for an out of range result (as
|
|
|
|
// we would get, for example, if the line is too short to fit the
|
|
|
|
// rounding arc).
|
|
|
|
if(fabs(tp[0] - t[0]) > 1e-3 || fabs(tp[1] - t[1]) > 1e-3 ||
|
|
|
|
t[0] < 0.01 || t[1] < 0.01 ||
|
|
|
|
t[0] > 0.99 || t[1] > 0.99 ||
|
|
|
|
isnan(t[0]) || isnan(t[1]))
|
|
|
|
{
|
2017-01-07 14:41:13 +08:00
|
|
|
Error(_("Couldn't round this corner. Try a smaller radius, or try "
|
|
|
|
"creating the desired geometry by hand with tangency "
|
|
|
|
"constraints."));
|
2010-05-17 00:36:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the location of the center of the arc
|
|
|
|
Vector center = pc[0].PointAt(t[0]),
|
|
|
|
v0inter = pinter.Minus(center);
|
2009-01-03 20:27:33 +08:00
|
|
|
int a, b;
|
|
|
|
if(vv < 0) {
|
|
|
|
a = 1; b = 2;
|
2010-05-17 00:36:23 +08:00
|
|
|
center = center.Minus(v0inter.Cross(wn).WithMagnitude(r));
|
2009-01-03 20:27:33 +08:00
|
|
|
} else {
|
|
|
|
a = 2; b = 1;
|
2010-05-17 00:36:23 +08:00
|
|
|
center = center.Plus(v0inter.Cross(wn).WithMagnitude(r));
|
2009-01-03 20:27:33 +08:00
|
|
|
}
|
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
SS.UndoRemember();
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2018-07-23 02:56:28 +08:00
|
|
|
if (SS.tangentArcModify) {
|
|
|
|
// Delete the coincident constraint for the removed point.
|
|
|
|
SK.constraint.ClearTags();
|
|
|
|
for(i = 0; i < SK.constraint.n; i++) {
|
2019-05-24 01:09:47 +08:00
|
|
|
Constraint *cs = &(SK.constraint[i]);
|
2019-07-09 22:44:57 +08:00
|
|
|
if(cs->group != activeGroup) continue;
|
|
|
|
if(cs->workplane != ActiveWorkplane()) continue;
|
2018-07-23 02:56:28 +08:00
|
|
|
if(cs->type != Constraint::Type::POINTS_COINCIDENT) continue;
|
|
|
|
if (SK.GetEntity(cs->ptA)->PointGetNum().Equals(pshared)) {
|
|
|
|
cs->tag = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SK.constraint.RemoveTagged();
|
|
|
|
} else {
|
|
|
|
// Make the original entities construction, or delete them
|
|
|
|
// entirely, according to user preference.
|
|
|
|
SK.GetRequest(hreq[0])->construction = true;
|
|
|
|
SK.GetRequest(hreq[1])->construction = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and position the new tangent arc.
|
2016-05-25 20:08:19 +08:00
|
|
|
hRequest harc = AddRequest(Request::Type::ARC_OF_CIRCLE, /*rememberForUndo=*/false);
|
2010-05-17 00:36:23 +08:00
|
|
|
Entity *earc = SK.GetEntity(harc.entity(0));
|
|
|
|
hEntity hearc = earc->h;
|
|
|
|
|
|
|
|
SK.GetEntity(earc->point[0])->PointForceTo(center);
|
|
|
|
SK.GetEntity(earc->point[a])->PointForceTo(pc[0].PointAt(t[0]));
|
|
|
|
SK.GetEntity(earc->point[b])->PointForceTo(pc[1].PointAt(t[1]));
|
2015-03-29 08:30:52 +08:00
|
|
|
|
2010-05-17 00:36:23 +08:00
|
|
|
earc = NULL;
|
|
|
|
|
2018-07-23 02:56:28 +08:00
|
|
|
// Modify or duplicate the original entities and connect them to the tangent arc.
|
|
|
|
pc[0].CreateRequestTrimmedTo(t[0], SS.tangentArcModify,
|
|
|
|
hent[0], hearc, /*arcFinish=*/(b == 1), pointf[0]);
|
|
|
|
pc[1].CreateRequestTrimmedTo(t[1], SS.tangentArcModify,
|
|
|
|
hent[1], hearc, /*arcFinish=*/(a == 1), pointf[1]);
|
2009-01-03 20:27:33 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 16:21:59 +08:00
|
|
|
hEntity GraphicsWindow::SplitLine(hEntity he, Vector pinter) {
|
2009-01-03 20:27:33 +08:00
|
|
|
// Save the original endpoints, since we're about to delete this entity.
|
2009-04-19 13:53:16 +08:00
|
|
|
Entity *e01 = SK.GetEntity(he);
|
2009-01-03 20:27:33 +08:00
|
|
|
hEntity hep0 = e01->point[0], hep1 = e01->point[1];
|
2009-04-19 13:53:16 +08:00
|
|
|
Vector p0 = SK.GetEntity(hep0)->PointGetNum(),
|
|
|
|
p1 = SK.GetEntity(hep1)->PointGetNum();
|
2009-01-03 20:27:33 +08:00
|
|
|
|
|
|
|
// Add the two line segments this one gets split into.
|
2016-05-25 20:08:19 +08:00
|
|
|
hRequest r0i = AddRequest(Request::Type::LINE_SEGMENT, /*rememberForUndo=*/false),
|
|
|
|
ri1 = AddRequest(Request::Type::LINE_SEGMENT, /*rememberForUndo=*/false);
|
2009-01-03 20:27:33 +08:00
|
|
|
// Don't get entities till after adding, realloc issues
|
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
Entity *e0i = SK.GetEntity(r0i.entity(0)),
|
|
|
|
*ei1 = SK.GetEntity(ri1.entity(0));
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.GetEntity(e0i->point[0])->PointForceTo(p0);
|
|
|
|
SK.GetEntity(e0i->point[1])->PointForceTo(pinter);
|
|
|
|
SK.GetEntity(ei1->point[0])->PointForceTo(pinter);
|
|
|
|
SK.GetEntity(ei1->point[1])->PointForceTo(p1);
|
2009-01-03 20:27:33 +08:00
|
|
|
|
|
|
|
ReplacePointInConstraints(hep0, e0i->point[0]);
|
|
|
|
ReplacePointInConstraints(hep1, ei1->point[1]);
|
2009-07-07 16:21:59 +08:00
|
|
|
Constraint::ConstrainCoincident(e0i->point[1], ei1->point[0]);
|
|
|
|
return e0i->point[1];
|
2009-01-03 20:27:33 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 16:21:59 +08:00
|
|
|
hEntity GraphicsWindow::SplitCircle(hEntity he, Vector pinter) {
|
2009-04-19 13:53:16 +08:00
|
|
|
Entity *circle = SK.GetEntity(he);
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
if(circle->type == Entity::Type::CIRCLE) {
|
2009-01-03 20:27:33 +08:00
|
|
|
// Start with an unbroken circle, split it into a 360 degree arc.
|
2009-04-19 13:53:16 +08:00
|
|
|
Vector center = SK.GetEntity(circle->point[0])->PointGetNum();
|
2009-01-03 20:27:33 +08:00
|
|
|
|
|
|
|
circle = NULL; // shortly invalid!
|
2016-05-25 20:08:19 +08:00
|
|
|
hRequest hr = AddRequest(Request::Type::ARC_OF_CIRCLE, /*rememberForUndo=*/false);
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
Entity *arc = SK.GetEntity(hr.entity(0));
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.GetEntity(arc->point[0])->PointForceTo(center);
|
|
|
|
SK.GetEntity(arc->point[1])->PointForceTo(pinter);
|
|
|
|
SK.GetEntity(arc->point[2])->PointForceTo(pinter);
|
2009-07-07 16:21:59 +08:00
|
|
|
|
|
|
|
Constraint::ConstrainCoincident(arc->point[1], arc->point[2]);
|
|
|
|
return arc->point[1];
|
2009-01-03 20:27:33 +08:00
|
|
|
} else {
|
|
|
|
// Start with an arc, break it in to two arcs
|
2009-07-07 16:21:59 +08:00
|
|
|
hEntity hc = circle->point[0],
|
|
|
|
hs = circle->point[1],
|
|
|
|
hf = circle->point[2];
|
|
|
|
Vector center = SK.GetEntity(hc)->PointGetNum(),
|
|
|
|
start = SK.GetEntity(hs)->PointGetNum(),
|
|
|
|
finish = SK.GetEntity(hf)->PointGetNum();
|
2009-01-03 20:27:33 +08:00
|
|
|
|
|
|
|
circle = NULL; // shortly invalid!
|
2016-05-25 20:08:19 +08:00
|
|
|
hRequest hr0 = AddRequest(Request::Type::ARC_OF_CIRCLE, /*rememberForUndo=*/false),
|
|
|
|
hr1 = AddRequest(Request::Type::ARC_OF_CIRCLE, /*rememberForUndo=*/false);
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
Entity *arc0 = SK.GetEntity(hr0.entity(0)),
|
|
|
|
*arc1 = SK.GetEntity(hr1.entity(0));
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.GetEntity(arc0->point[0])->PointForceTo(center);
|
|
|
|
SK.GetEntity(arc0->point[1])->PointForceTo(start);
|
|
|
|
SK.GetEntity(arc0->point[2])->PointForceTo(pinter);
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.GetEntity(arc1->point[0])->PointForceTo(center);
|
|
|
|
SK.GetEntity(arc1->point[1])->PointForceTo(pinter);
|
|
|
|
SK.GetEntity(arc1->point[2])->PointForceTo(finish);
|
2009-07-07 16:21:59 +08:00
|
|
|
|
|
|
|
ReplacePointInConstraints(hs, arc0->point[1]);
|
|
|
|
ReplacePointInConstraints(hf, arc1->point[2]);
|
|
|
|
Constraint::ConstrainCoincident(arc0->point[2], arc1->point[1]);
|
|
|
|
return arc0->point[2];
|
2009-01-03 20:27:33 +08:00
|
|
|
}
|
2009-07-07 16:21:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hEntity GraphicsWindow::SplitCubic(hEntity he, Vector pinter) {
|
|
|
|
// Save the original endpoints, since we're about to delete this entity.
|
|
|
|
Entity *e01 = SK.GetEntity(he);
|
2015-03-27 23:31:23 +08:00
|
|
|
SBezierList sbl = {};
|
2009-11-10 11:57:24 +08:00
|
|
|
e01->GenerateBezierCurves(&sbl);
|
2009-07-07 16:21:59 +08:00
|
|
|
|
2009-11-10 11:57:24 +08:00
|
|
|
hEntity hep0 = e01->point[0],
|
|
|
|
hep1 = e01->point[3+e01->extraPoints],
|
|
|
|
hep0n = Entity::NO_ENTITY, // the new start point
|
|
|
|
hep1n = Entity::NO_ENTITY, // the new finish point
|
|
|
|
hepin = Entity::NO_ENTITY; // the intersection point
|
2009-07-07 16:21:59 +08:00
|
|
|
|
2009-11-10 11:57:24 +08:00
|
|
|
// The curve may consist of multiple cubic segments. So find which one
|
|
|
|
// contains the intersection point.
|
2009-07-07 16:21:59 +08:00
|
|
|
double t;
|
2009-11-10 11:57:24 +08:00
|
|
|
int i, j;
|
|
|
|
for(i = 0; i < sbl.l.n; i++) {
|
2019-05-24 01:09:47 +08:00
|
|
|
SBezier *sb = &(sbl.l[i]);
|
2016-05-19 06:51:36 +08:00
|
|
|
ssassert(sb->deg == 3, "Expected a cubic bezier");
|
2009-11-10 11:57:24 +08:00
|
|
|
|
2016-05-25 20:08:19 +08:00
|
|
|
sb->ClosestPointTo(pinter, &t, /*mustConverge=*/false);
|
2009-11-10 11:57:24 +08:00
|
|
|
if(pinter.Equals(sb->PointAt(t))) {
|
|
|
|
// Split that segment at the intersection.
|
|
|
|
SBezier b0i, bi1, b01 = *sb;
|
|
|
|
b01.SplitAt(t, &b0i, &bi1);
|
|
|
|
|
|
|
|
// Add the two cubic segments this one gets split into.
|
2016-05-25 20:08:19 +08:00
|
|
|
hRequest r0i = AddRequest(Request::Type::CUBIC, /*rememberForUndo=*/false),
|
|
|
|
ri1 = AddRequest(Request::Type::CUBIC, /*rememberForUndo=*/false);
|
2009-11-10 11:57:24 +08:00
|
|
|
// Don't get entities till after adding, realloc issues
|
|
|
|
|
|
|
|
Entity *e0i = SK.GetEntity(r0i.entity(0)),
|
|
|
|
*ei1 = SK.GetEntity(ri1.entity(0));
|
|
|
|
|
|
|
|
for(j = 0; j <= 3; j++) {
|
|
|
|
SK.GetEntity(e0i->point[j])->PointForceTo(b0i.ctrl[j]);
|
|
|
|
}
|
|
|
|
for(j = 0; j <= 3; j++) {
|
|
|
|
SK.GetEntity(ei1->point[j])->PointForceTo(bi1.ctrl[j]);
|
|
|
|
}
|
2009-07-07 16:21:59 +08:00
|
|
|
|
2009-11-10 11:57:24 +08:00
|
|
|
Constraint::ConstrainCoincident(e0i->point[3], ei1->point[0]);
|
|
|
|
if(i == 0) hep0n = e0i->point[0];
|
|
|
|
hep1n = ei1->point[3];
|
|
|
|
hepin = e0i->point[3];
|
|
|
|
} else {
|
2016-05-25 20:08:19 +08:00
|
|
|
hRequest r = AddRequest(Request::Type::CUBIC, /*rememberForUndo=*/false);
|
2009-11-10 11:57:24 +08:00
|
|
|
Entity *e = SK.GetEntity(r.entity(0));
|
2009-07-07 16:21:59 +08:00
|
|
|
|
2009-11-10 11:57:24 +08:00
|
|
|
for(j = 0; j <= 3; j++) {
|
|
|
|
SK.GetEntity(e->point[j])->PointForceTo(sb->ctrl[j]);
|
|
|
|
}
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2009-11-10 11:57:24 +08:00
|
|
|
if(i == 0) hep0n = e->point[0];
|
|
|
|
hep1n = e->point[3];
|
|
|
|
}
|
|
|
|
}
|
2009-07-07 16:21:59 +08:00
|
|
|
|
2009-11-10 11:57:24 +08:00
|
|
|
sbl.Clear();
|
2009-07-07 16:21:59 +08:00
|
|
|
|
2009-11-10 11:57:24 +08:00
|
|
|
ReplacePointInConstraints(hep0, hep0n);
|
|
|
|
ReplacePointInConstraints(hep1, hep1n);
|
|
|
|
return hepin;
|
2009-07-07 16:21:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hEntity GraphicsWindow::SplitEntity(hEntity he, Vector pinter) {
|
|
|
|
Entity *e = SK.GetEntity(he);
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
Entity::Type entityType = e->type;
|
2009-07-07 16:21:59 +08:00
|
|
|
|
|
|
|
hEntity ret;
|
|
|
|
if(e->IsCircle()) {
|
|
|
|
ret = SplitCircle(he, pinter);
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
} else if(e->type == Entity::Type::LINE_SEGMENT) {
|
2009-07-07 16:21:59 +08:00
|
|
|
ret = SplitLine(he, pinter);
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
} else if(e->type == Entity::Type::CUBIC || e->type == Entity::Type::CUBIC_PERIODIC) {
|
2009-07-07 16:21:59 +08:00
|
|
|
ret = SplitCubic(he, pinter);
|
|
|
|
} else {
|
2017-01-07 14:41:13 +08:00
|
|
|
Error(_("Couldn't split this entity; lines, circles, or cubics only."));
|
2009-07-07 16:21:59 +08:00
|
|
|
return Entity::NO_ENTITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, delete the request that generated the original entity.
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
Request::Type reqType = EntReqTable::GetRequestForEntity(entityType);
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.request.ClearTags();
|
2018-01-04 09:42:38 +08:00
|
|
|
for(auto &r : SK.request) {
|
|
|
|
if(r.group != activeGroup)
|
|
|
|
continue;
|
|
|
|
if(r.type != reqType)
|
|
|
|
continue;
|
2015-03-29 08:30:52 +08:00
|
|
|
|
2009-07-07 16:21:59 +08:00
|
|
|
// If the user wants to keep the old entities around, they can just
|
|
|
|
// mark them construction first.
|
2018-01-04 09:42:38 +08:00
|
|
|
if(he == r.h.entity(0) && !r.construction) {
|
|
|
|
r.tag = 1;
|
2009-01-03 20:27:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DeleteTaggedRequests();
|
2009-07-07 16:21:59 +08:00
|
|
|
|
|
|
|
return ret;
|
2009-01-03 20:27:33 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 13:54:05 +08:00
|
|
|
void GraphicsWindow::SplitLinesOrCurves() {
|
2009-01-03 20:27:33 +08:00
|
|
|
if(!LockedInWorkplane()) {
|
2017-01-07 14:41:13 +08:00
|
|
|
Error(_("Must be sketching in workplane to split."));
|
2009-01-03 20:27:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupSelection();
|
2018-04-07 16:34:36 +08:00
|
|
|
int n = gs.lineSegments + gs.circlesOrArcs + gs.cubics + gs.periodicCubics;
|
|
|
|
if(!((n == 2 && gs.points == 0) || (n == 1 && gs.points == 1))) {
|
|
|
|
Error(_("Select two entities that intersect each other "
|
|
|
|
"(e.g. two lines/circles/arcs or a line/circle/arc and a point)."));
|
2009-07-07 16:21:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-01-03 20:27:33 +08:00
|
|
|
|
2018-04-07 16:34:36 +08:00
|
|
|
bool splitAtPoint = (gs.points == 1);
|
2009-07-07 16:21:59 +08:00
|
|
|
hEntity ha = gs.entity[0],
|
2018-04-07 16:34:36 +08:00
|
|
|
hb = splitAtPoint ? gs.point[0] : gs.entity[1];
|
|
|
|
|
2009-07-07 16:21:59 +08:00
|
|
|
Entity *ea = SK.GetEntity(ha),
|
|
|
|
*eb = SK.GetEntity(hb);
|
2015-03-27 23:31:23 +08:00
|
|
|
SPointList inters = {};
|
2018-04-07 16:34:36 +08:00
|
|
|
SBezierList sbla = {},
|
|
|
|
sblb = {};
|
|
|
|
Vector pi = Vector::From(0, 0, 0);
|
|
|
|
|
|
|
|
SK.constraint.ClearTags();
|
|
|
|
|
|
|
|
// First, decide the point where we're going to make the split.
|
|
|
|
bool foundInters = false;
|
|
|
|
if(splitAtPoint) {
|
|
|
|
// One of the entities is a point, and this point must be on the other entity.
|
|
|
|
// Verify that a corresponding point-coincident constraint exists for the point/entity.
|
|
|
|
Vector p0, p1;
|
|
|
|
if(ea->type == Entity::Type::LINE_SEGMENT) {
|
|
|
|
p0 = ea->EndpointStart();
|
|
|
|
p1 = ea->EndpointFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(Constraint &c : SK.constraint) {
|
2019-07-09 22:44:57 +08:00
|
|
|
if(c.ptA.request() == hb.request() &&
|
|
|
|
c.entityA.request() == ha.request()) {
|
2018-04-07 16:34:36 +08:00
|
|
|
pi = SK.GetEntity(c.ptA)->PointGetNum();
|
|
|
|
|
|
|
|
if(ea->type == Entity::Type::LINE_SEGMENT && !pi.OnLineSegment(p0, p1)) {
|
|
|
|
// The point isn't between line endpoints, so there isn't an actual
|
|
|
|
// intersection.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
c.tag = 1;
|
|
|
|
foundInters = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Compute the possibly-rational Bezier curves for each of these non-point entities...
|
|
|
|
ea->GenerateBezierCurves(&sbla);
|
|
|
|
eb->GenerateBezierCurves(&sblb);
|
|
|
|
// ... and then compute the points where they intersect, based on those curves.
|
|
|
|
sbla.AllIntersectionsWith(&sblb, &inters);
|
|
|
|
|
|
|
|
// If there's multiple points, then take the one closest to the mouse pointer.
|
2018-01-04 05:10:18 +08:00
|
|
|
if(!inters.l.IsEmpty()) {
|
2018-04-07 16:34:36 +08:00
|
|
|
double dmin = VERY_POSITIVE;
|
|
|
|
SPoint *sp;
|
|
|
|
for(sp = inters.l.First(); sp; sp = inters.l.NextAfter(sp)) {
|
|
|
|
double d = ProjectPoint(sp->p).DistanceTo(currentMousePosition);
|
|
|
|
if(d < dmin) {
|
|
|
|
dmin = d;
|
|
|
|
pi = sp->p;
|
|
|
|
}
|
2010-07-12 14:47:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 16:34:36 +08:00
|
|
|
foundInters = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, actually split the entities.
|
|
|
|
if(foundInters) {
|
2009-11-10 11:57:24 +08:00
|
|
|
SS.UndoRemember();
|
2018-04-07 16:34:36 +08:00
|
|
|
|
|
|
|
// Remove any constraints we're going to replace.
|
|
|
|
SK.constraint.RemoveTagged();
|
|
|
|
|
2009-07-07 16:21:59 +08:00
|
|
|
hEntity hia = SplitEntity(ha, pi),
|
2018-04-07 16:34:36 +08:00
|
|
|
hib = {};
|
2009-07-07 16:21:59 +08:00
|
|
|
// SplitEntity adds the coincident constraints to join the split halves
|
|
|
|
// of each original entity; and then we add the constraint to join
|
|
|
|
// the two entities together at the split point.
|
2018-04-07 16:34:36 +08:00
|
|
|
if(splitAtPoint) {
|
|
|
|
// Remove datum point, as it has now been superseded by the split point.
|
|
|
|
SK.request.ClearTags();
|
|
|
|
for(Request &r : SK.request) {
|
2019-07-09 22:44:57 +08:00
|
|
|
if(r.h == hb.request()) {
|
2018-04-07 16:34:36 +08:00
|
|
|
if(r.type == Request::Type::DATUM_POINT) {
|
|
|
|
// Delete datum point.
|
|
|
|
r.tag = 1;
|
|
|
|
FixConstraintsForRequestBeingDeleted(r.h);
|
|
|
|
} else {
|
|
|
|
// Add constraint if not datum point, but endpoint of line/arc etc.
|
|
|
|
Constraint::ConstrainCoincident(hia, hb);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SK.request.RemoveTagged();
|
|
|
|
} else {
|
|
|
|
// Split second non-point entity and add constraint.
|
|
|
|
hib = SplitEntity(hb, pi);
|
|
|
|
if(hia.v && hib.v) {
|
|
|
|
Constraint::ConstrainCoincident(hia, hib);
|
|
|
|
}
|
2009-01-03 20:27:33 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-07 14:41:13 +08:00
|
|
|
Error(_("Can't split; no intersection found."));
|
Eliminate blocking in Error() and Message() calls.
This serves two purposes.
First, we want to (some day) convert these messages into a less
obtrustive form, something like toaster notifications, such that they
don't interrupt workflow as harshly. That would, of course, be
nonblocking.
Second, some platforms, like Emscripten, do not support nested event
loops, and it's not possible to display a modal dialog on them
synchronously.
When making this commit, I've reviewed all Error() and Message()
calls to ensure that only some of the following is true for all
of them:
* The call is followed a break or return statement that exits
an UI entry point (e.g. an MenuX function);
* The call is followed by cleanup (in fact, in this case the new
behavior is better, since even with a synchronous modal dialog
we have to be reentrant);
* The message is an informational message only and nothing
unexpected will happen if the operation proceeds in background.
In general, all Error() calls already satisfied the above conditions,
although in some cases I changed control flow aroudn them to more
clearly show that. The Message() calls that didn't satisfy these
conditions were reworked into an asynchronous form.
There are three explicit RunModal() calls left that need to be
reworked into an async form.
2018-07-20 05:54:05 +08:00
|
|
|
return;
|
2009-01-03 20:27:33 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 16:21:59 +08:00
|
|
|
// All done, clean up and regenerate.
|
|
|
|
inters.Clear();
|
|
|
|
sbla.Clear();
|
|
|
|
sblb.Clear();
|
2009-01-03 20:27:33 +08:00
|
|
|
ClearSelection();
|
|
|
|
}
|