2009-06-05 05:38:41 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Routines to merge multiple coincident surfaces (each with their own trim
|
|
|
|
// curves) into a single surface, with all of the trim curves.
|
2013-07-28 22:08:34 +00:00
|
|
|
//
|
|
|
|
// Copyright 2008-2013 Jonathan Westhues.
|
2009-06-05 05:38:41 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#include "../solvespace.h"
|
|
|
|
|
2016-05-05 05:54:05 +00:00
|
|
|
void SShell::MergeCoincidentSurfaces() {
|
2009-06-05 05:38:41 +00:00
|
|
|
surface.ClearTags();
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
SSurface *si, *sj;
|
|
|
|
|
|
|
|
for(i = 0; i < surface.n; i++) {
|
|
|
|
si = &(surface.elem[i]);
|
|
|
|
if(si->tag) continue;
|
2009-06-10 08:26:09 +00:00
|
|
|
// Let someone else clean up the empty surfaces; we can certainly merge
|
|
|
|
// them, but we don't know how to calculate a reasonable bounding box.
|
|
|
|
if(si->trim.n == 0) continue;
|
2009-06-30 04:38:40 +00:00
|
|
|
// And for now we handle only coincident planes, so no sense wasting
|
|
|
|
// time on other surfaces.
|
|
|
|
if(si->degm != 1 || si->degn != 1) continue;
|
2009-06-05 05:38:41 +00:00
|
|
|
|
2015-03-27 15:31:23 +00:00
|
|
|
SEdgeList sel = {};
|
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 08:31:20 +00:00
|
|
|
si->MakeEdgesInto(this, &sel, SSurface::MakeAs::XYZ);
|
2009-06-05 05:38:41 +00:00
|
|
|
|
2009-06-30 04:38:40 +00:00
|
|
|
bool mergedThisTime, merged = false;
|
|
|
|
do {
|
|
|
|
mergedThisTime = false;
|
|
|
|
|
|
|
|
for(j = i + 1; j < surface.n; j++) {
|
|
|
|
sj = &(surface.elem[j]);
|
|
|
|
if(sj->tag) continue;
|
|
|
|
if(!sj->CoincidentWith(si, true)) continue;
|
Replaced RGB-color integers with dedicated data structure
RGB colors were represented using a uint32_t with the red, green and blue
values stuffed into the lower three octets (i.e. 0x00BBGGRR), like
Microsoft's COLORREF. This approach did not lend itself to type safety,
however, so this change replaces it with an RgbColor class that provides
the same infomation plus a handful of useful methods to work with it. (Note
that sizeof(RgbColor) == sizeof(uint32_t), so this change should not lead
to memory bloat.)
Some of the new methods/fields replace what were previously macro calls;
e.g. RED(c) is now c.red, REDf(c) is now c.redF(). The .Equals() method is
now used instead of == to compare colors.
RGB colors still need to be represented as packed integers in file I/O and
preferences, so the methods .FromPackedInt() and .ToPackedInt() are
provided. Also implemented are Cnf{Freeze,Thaw}Color(), type-safe wrappers
around Cnf{Freeze,Thaw}Int() that facilitate I/O with preferences.
(Cnf{Freeze,Thaw}Color() are defined outside of the system-dependent code
to minimize the footprint of the latter; because the same can be done with
Cnf{Freeze,Thaw}Bool(), those are also moved out of the system code with
this commit.)
Color integers were being OR'ed with 0x80000000 in some places for two
distinct purposes: One, to indicate use of a default color in
glxFillMesh(); this has been replaced by use of the .UseDefault() method.
Two, to indicate to TextWindow::Printf() that the format argument of a
"%Bp"/"%Fp" specifier is an RGB color rather than a color "code" from
TextWindow::bgColors[] or TextWindow::fgColors[] (as the specifier can
accept either); instead, we define a new flag "z" (as in "%Bz" or "%Fz") to
indicate an RGBcolor pointer, leaving "%Bp"/"%Fp" to indicate a color code
exclusively.
(This also allows TextWindow::meta[][].bg to be a char instead of an int,
partly compensating for the new .bgRgb field added immediately after.)
In array declarations, RGB colors could previously be specified as 0 (often
in a terminating element). As that no longer works, we define NULL_COLOR,
which serves much the same purpose for RgbColor variables as NULL serves
for pointers.
2013-10-16 20:00:58 +00:00
|
|
|
if(!sj->color.Equals(si->color)) continue;
|
2009-06-30 04:38:40 +00:00
|
|
|
// But we do merge surfaces with different face entities, since
|
|
|
|
// otherwise we'd hardly ever merge anything.
|
|
|
|
|
|
|
|
// This surface is coincident. But let's not merge coincident
|
|
|
|
// surfaces if they contain disjoint contours; that just makes
|
|
|
|
// the bounding box tests less effective, and possibly things
|
|
|
|
// less robust.
|
2015-03-27 15:31:23 +00:00
|
|
|
SEdgeList tel = {};
|
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 08:31:20 +00:00
|
|
|
sj->MakeEdgesInto(this, &tel, SSurface::MakeAs::XYZ);
|
2009-06-30 04:38:40 +00:00
|
|
|
if(!sel.ContainsEdgeFrom(&tel)) {
|
|
|
|
tel.Clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tel.Clear();
|
|
|
|
|
|
|
|
sj->tag = 1;
|
|
|
|
merged = true;
|
|
|
|
mergedThisTime = true;
|
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 08:31:20 +00:00
|
|
|
sj->MakeEdgesInto(this, &sel, SSurface::MakeAs::XYZ);
|
2009-06-30 04:38:40 +00:00
|
|
|
sj->trim.Clear();
|
|
|
|
|
|
|
|
// All the references to this surface get replaced with the
|
|
|
|
// new srf
|
|
|
|
SCurve *sc;
|
|
|
|
for(sc = curve.First(); sc; sc = curve.NextAfter(sc)) {
|
|
|
|
if(sc->surfA.v == sj->h.v) sc->surfA = si->h;
|
|
|
|
if(sc->surfB.v == sj->h.v) sc->surfB = si->h;
|
|
|
|
}
|
2009-06-05 05:38:41 +00:00
|
|
|
}
|
2009-06-30 04:38:40 +00:00
|
|
|
|
|
|
|
// If this iteration merged a contour onto ours, then we have to
|
|
|
|
// go through the surfaces again; that might have made a new
|
|
|
|
// surface touch us.
|
|
|
|
} while(mergedThisTime);
|
2009-06-05 05:38:41 +00:00
|
|
|
|
|
|
|
if(merged) {
|
|
|
|
sel.CullExtraneousEdges();
|
|
|
|
si->trim.Clear();
|
|
|
|
si->TrimFromEdgeList(&sel, false);
|
|
|
|
|
|
|
|
// And we must choose control points such that all the trims lie
|
|
|
|
// with u and v in [0, 1], so that the bbox tests work.
|
|
|
|
Vector u, v, n;
|
|
|
|
si->TangentsAt(0.5, 0.5, &u, &v);
|
|
|
|
u = u.WithMagnitude(1);
|
|
|
|
v = v.WithMagnitude(1);
|
|
|
|
n = si->NormalAt(0.5, 0.5).WithMagnitude(1);
|
|
|
|
v = (n.Cross(u)).WithMagnitude(1);
|
|
|
|
|
|
|
|
double umax = VERY_NEGATIVE, umin = VERY_POSITIVE,
|
|
|
|
vmax = VERY_NEGATIVE, vmin = VERY_POSITIVE;
|
|
|
|
SEdge *se;
|
|
|
|
for(se = sel.l.First(); se; se = sel.l.NextAfter(se)) {
|
|
|
|
double ut = (se->a).Dot(u), vt = (se->a).Dot(v);
|
|
|
|
umax = max(umax, ut);
|
|
|
|
vmax = max(vmax, vt);
|
|
|
|
umin = min(umin, ut);
|
|
|
|
vmin = min(vmin, vt);
|
|
|
|
}
|
|
|
|
|
2009-06-25 11:58:39 +00:00
|
|
|
// An interesting problem here; the real curve could extend
|
|
|
|
// slightly beyond the bounding box of the piecewise linear
|
|
|
|
// bits. Not a problem for us, but some apps won't import STEP
|
|
|
|
// in that case. So give a bit of extra room; in theory just
|
|
|
|
// a chord tolerance, but more can't hurt.
|
|
|
|
double muv = max((umax - umin), (vmax - vmin));
|
|
|
|
double tol = muv/50 + 3*SS.ChordTolMm();
|
|
|
|
umax += tol;
|
|
|
|
vmax += tol;
|
|
|
|
umin -= tol;
|
|
|
|
vmin -= tol;
|
|
|
|
|
2009-06-05 05:38:41 +00:00
|
|
|
// We move in the +v direction as v goes from 0 to 1, and in the
|
|
|
|
// +u direction as u goes from 0 to 1. So our normal ends up
|
|
|
|
// pointed the same direction.
|
|
|
|
double nt = (si->ctrl[0][0]).Dot(n);
|
|
|
|
si->ctrl[0][0] =
|
|
|
|
Vector::From(umin, vmin, nt).ScaleOutOfCsys(u, v, n);
|
|
|
|
si->ctrl[0][1] =
|
|
|
|
Vector::From(umin, vmax, nt).ScaleOutOfCsys(u, v, n);
|
|
|
|
si->ctrl[1][1] =
|
|
|
|
Vector::From(umax, vmax, nt).ScaleOutOfCsys(u, v, n);
|
|
|
|
si->ctrl[1][0] =
|
|
|
|
Vector::From(umax, vmin, nt).ScaleOutOfCsys(u, v, n);
|
|
|
|
}
|
2009-06-30 04:38:40 +00:00
|
|
|
sel.Clear();
|
2009-06-05 05:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
surface.RemoveTagged();
|
|
|
|
}
|
|
|
|
|