2009-07-08 09:44:13 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// The 2d vector output stuff that isn't specific to any particular file
|
|
|
|
// format: getting the appropriate lines and curves, performing hidden line
|
|
|
|
// removal, calculating bounding boxes, and so on. Also raster and triangle
|
|
|
|
// mesh output.
|
2013-07-28 22:08:34 +00:00
|
|
|
//
|
|
|
|
// Copyright 2008-2013 Jonathan Westhues.
|
2009-07-08 09:44:13 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-07-08 07:45:47 +00:00
|
|
|
#include "solvespace.h"
|
2019-02-25 00:46:57 +00:00
|
|
|
#include "config.h"
|
2008-07-08 07:45:47 +00:00
|
|
|
|
2017-03-11 14:43:21 +00:00
|
|
|
void SolveSpaceUI::ExportSectionTo(const Platform::Path &filename) {
|
2008-07-08 07:45:47 +00:00
|
|
|
Vector gn = (SS.GW.projRight).Cross(SS.GW.projUp);
|
|
|
|
gn = gn.WithMagnitude(1);
|
|
|
|
|
2009-04-19 05:53:16 +00:00
|
|
|
Group *g = SK.GetGroup(SS.GW.activeGroup);
|
2009-05-21 09:06:26 +00:00
|
|
|
g->GenerateDisplayItems();
|
2009-05-24 11:37:07 +00:00
|
|
|
if(g->displayMesh.IsEmpty()) {
|
2017-01-07 06:41:13 +00:00
|
|
|
Error(_("No solid model present; draw one with extrudes and revolves, "
|
|
|
|
"or use Export 2d View to export bare lines and curves."));
|
2009-01-14 05:10:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-29 00:30:52 +00:00
|
|
|
|
2008-07-08 07:45:47 +00:00
|
|
|
// The plane in which the exported section lies; need this because we'll
|
|
|
|
// reorient from that plane into the xy plane before exporting.
|
2009-01-14 05:10:42 +00:00
|
|
|
Vector origin, u, v, n;
|
2008-07-08 07:45:47 +00:00
|
|
|
double d;
|
|
|
|
|
2009-01-14 05:10:42 +00:00
|
|
|
SS.GW.GroupSelection();
|
2016-10-10 12:34:10 +00:00
|
|
|
auto const &gs = SS.GW.gs;
|
2019-07-09 14:44:57 +00:00
|
|
|
if((gs.n == 0 && g->activeWorkplane != Entity::FREE_IN_3D)) {
|
2009-04-19 05:53:16 +00:00
|
|
|
Entity *wrkpl = SK.GetEntity(g->activeWorkplane);
|
2009-01-14 05:10:42 +00:00
|
|
|
origin = wrkpl->WorkplaneGetOffset();
|
|
|
|
n = wrkpl->Normal()->NormalN();
|
|
|
|
u = wrkpl->Normal()->NormalU();
|
|
|
|
v = wrkpl->Normal()->NormalV();
|
|
|
|
} else if(gs.n == 1 && gs.faces == 1) {
|
2009-04-19 05:53:16 +00:00
|
|
|
Entity *face = SK.GetEntity(gs.entity[0]);
|
2009-01-14 05:10:42 +00:00
|
|
|
origin = face->FaceGetPointNum();
|
|
|
|
n = face->FaceGetNormalNum();
|
2008-07-08 07:45:47 +00:00
|
|
|
if(n.Dot(gn) < 0) n = n.ScaledBy(-1);
|
|
|
|
u = n.Normal(0);
|
|
|
|
v = n.Normal(1);
|
2009-01-14 05:10:42 +00:00
|
|
|
} else if(gs.n == 3 && gs.vectors == 2 && gs.points == 1) {
|
2009-04-19 05:53:16 +00:00
|
|
|
Vector ut = SK.GetEntity(gs.entity[0])->VectorGetNum(),
|
|
|
|
vt = SK.GetEntity(gs.entity[1])->VectorGetNum();
|
2009-01-14 05:10:42 +00:00
|
|
|
ut = ut.WithMagnitude(1);
|
|
|
|
vt = vt.WithMagnitude(1);
|
|
|
|
|
|
|
|
if(fabs(SS.GW.projUp.Dot(vt)) < fabs(SS.GW.projUp.Dot(ut))) {
|
2015-03-27 15:43:28 +00:00
|
|
|
swap(ut, vt);
|
2009-01-14 05:10:42 +00:00
|
|
|
}
|
|
|
|
if(SS.GW.projRight.Dot(ut) < 0) ut = ut.ScaledBy(-1);
|
|
|
|
if(SS.GW.projUp. Dot(vt) < 0) vt = vt.ScaledBy(-1);
|
|
|
|
|
2009-04-19 05:53:16 +00:00
|
|
|
origin = SK.GetEntity(gs.point[0])->PointGetNum();
|
2009-01-14 05:10:42 +00:00
|
|
|
n = ut.Cross(vt);
|
|
|
|
u = ut.WithMagnitude(1);
|
|
|
|
v = (n.Cross(u)).WithMagnitude(1);
|
|
|
|
} else {
|
2017-01-07 06:41:13 +00:00
|
|
|
Error(_("Bad selection for export section. Please select:\n\n"
|
|
|
|
" * nothing, with an active workplane "
|
|
|
|
"(workplane is section plane)\n"
|
|
|
|
" * a face (section plane through face)\n"
|
|
|
|
" * a point and two line segments "
|
|
|
|
"(plane through point and parallel to lines)\n"));
|
2009-01-14 05:10:42 +00:00
|
|
|
return;
|
2008-07-08 07:45:47 +00:00
|
|
|
}
|
2009-01-14 05:10:42 +00:00
|
|
|
SS.GW.ClearSelection();
|
2008-07-08 07:45:47 +00:00
|
|
|
|
2009-01-14 05:10:42 +00:00
|
|
|
n = n.WithMagnitude(1);
|
|
|
|
d = origin.Dot(n);
|
2008-07-08 07:45:47 +00:00
|
|
|
|
2015-03-27 15:31:23 +00:00
|
|
|
SEdgeList el = {};
|
|
|
|
SBezierList bl = {};
|
2009-04-14 04:19:23 +00:00
|
|
|
|
2009-05-28 07:07:54 +00:00
|
|
|
// If there's a mesh, then grab the edges from it.
|
|
|
|
g->runningMesh.MakeEdgesInPlaneInto(&el, n, d);
|
|
|
|
|
|
|
|
// If there's a shell, then grab the edges and possibly Beziers.
|
2019-05-13 14:34:22 +00:00
|
|
|
bool export_as_pwl = SS.exportPwlCurves || fabs(SS.exportOffset) > LENGTH_EPS;
|
|
|
|
g->runningShell.MakeSectionEdgesInto(n, d, &el, export_as_pwl ? NULL : &bl);
|
2009-04-14 04:19:23 +00:00
|
|
|
|
2009-09-22 05:46:30 +00:00
|
|
|
// All of these are solid model edges, so use the appropriate style.
|
|
|
|
SEdge *se;
|
|
|
|
for(se = el.l.First(); se; se = el.l.NextAfter(se)) {
|
|
|
|
se->auxA = Style::SOLID_EDGE;
|
|
|
|
}
|
|
|
|
SBezier *sb;
|
|
|
|
for(sb = bl.l.First(); sb; sb = bl.l.NextAfter(sb)) {
|
|
|
|
sb->auxA = Style::SOLID_EDGE;
|
|
|
|
}
|
|
|
|
|
2019-05-13 14:34:22 +00:00
|
|
|
// Remove all overlapping edges/beziers to merge the areas they describe.
|
|
|
|
el.CullExtraneousEdges(/*both=*/true);
|
|
|
|
bl.CullIdenticalBeziers(/*both=*/true);
|
2019-05-31 23:06:59 +00:00
|
|
|
|
2019-05-13 14:34:22 +00:00
|
|
|
// Collect lines and beziers with custom style & export.
|
2018-01-04 01:42:38 +00:00
|
|
|
for(auto &ent : SK.entity) {
|
|
|
|
Entity *e = &ent;
|
2019-05-13 14:34:22 +00:00
|
|
|
if (!e->IsVisible()) continue;
|
|
|
|
if (e->style.v < Style::FIRST_CUSTOM) continue;
|
|
|
|
if (!Style::Exportable(e->style.v)) continue;
|
|
|
|
if (!e->IsInPlane(n,d)) continue;
|
|
|
|
if (export_as_pwl) {
|
|
|
|
e->GenerateEdges(&el);
|
|
|
|
} else {
|
|
|
|
e->GenerateBezierCurves(&bl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only remove half of the overlapping edges/beziers to support TTF Stick Fonts.
|
|
|
|
el.CullExtraneousEdges(/*both=*/false);
|
|
|
|
bl.CullIdenticalBeziers(/*both=*/false);
|
2009-01-14 05:10:42 +00:00
|
|
|
|
2009-03-17 16:33:46 +00:00
|
|
|
// And write the edges.
|
2009-01-14 05:10:42 +00:00
|
|
|
VectorFileWriter *out = VectorFileWriter::ForFile(filename);
|
|
|
|
if(out) {
|
2009-03-17 16:33:46 +00:00
|
|
|
// parallel projection (no perspective), and no mesh
|
2009-04-14 04:19:23 +00:00
|
|
|
ExportLinesAndMesh(&el, &bl, NULL,
|
2009-03-17 16:33:46 +00:00
|
|
|
u, v, n, origin, 0,
|
|
|
|
out);
|
2009-01-14 05:10:42 +00:00
|
|
|
}
|
2009-03-17 16:33:46 +00:00
|
|
|
el.Clear();
|
2009-04-14 04:19:23 +00:00
|
|
|
bl.Clear();
|
2009-01-14 05:10:42 +00:00
|
|
|
}
|
2008-07-08 07:45:47 +00:00
|
|
|
|
Abstract all (ex-OpenGL) drawing operations into a Canvas interface.
This has several desirable consequences:
* It is now possible to port SolveSpace to a later version of
OpenGL, such as OpenGLES 2, so that it runs on platforms that
only have that OpenGL version;
* The majority of geometry is now rendered without references to
the camera in C++ code, so a renderer can now submit it to
the video card once and re-rasterize with a different projection
matrix every time the projection is changed, avoiding expensive
reuploads;
* The DOGD (draw or get distance) interface is now
a straightforward Canvas implementation;
* There are no more direct references to SS.GW.(projection)
in sketch rendering code, which allows rendering to multiple
viewports;
* There are no more unnecessary framebuffer flips on CPU on Cocoa
and GTK;
* The platform-dependent GL code is now confined to rendergl1.cpp.
* The Microsoft and Apple headers required by it that are prone to
identifier conflicts are no longer included globally;
* The rendergl1.cpp implementation can now be omitted from
compilation to run SolveSpace headless or with a different
OpenGL version.
Note these implementation details of Canvas:
* GetCamera currently always returns a reference to the field
`Camera camera;`. This is so that a future renderer that caches
geometry in the video memory can define it as asserting, which
would provide assurance against code that could accidentally
put something projection-dependent in the cache;
* Line and triangle rendering is specified through a level of
indirection, hStroke and hFill. This is so that a future renderer
that batches geometry could cheaply group identical styles.
* DrawPixmap and DrawVectorText accept a (o,u,v) and not a matrix.
This is so that a future renderer into an output format that
uses 2d transforms (e.g. SVG) could easily derive those.
Some additional internal changes were required to enable this:
* Pixmap is now always passed as std::shared_ptr<{const ,}Pixmap>.
This is so that the renderer could cache uploaded textures
between API calls, which requires it to capture a (weak)
reference.
* The PlatformPathEqual function was properly extracted into
platform-specific code. This is so that the <windows.h> header
could be included only where needed (in platform/w32* as well
as rendergl1.cpp).
* The SBsp{2,3}::DebugDraw functions were removed. They can be
rewritten using the Canvas API if they are ever needed.
While no visual changes were originally intended, some minor fixes
happened anyway:
* The "emphasis" yellow line from top-left corner is now correctly
rendered much wider.
* The marquee rectangle is now pixel grid aligned.
* The hidden entities now do not clobber the depth buffer, removing
some minor artifacts.
* The workplane "tab" now scales with the font used to render
the workplane name.
* The workplane name font is now taken from the normals style.
* Workplane and constraint line stipple is insignificantly
different. This is so that it can reuse the existing stipple
codepaths; rendering of workplanes and constraints predates
those.
Some debug functionality was added:
* In graphics window, an fps counter that becomes red when
rendering under 60fps is drawn.
2016-05-31 00:55:13 +00:00
|
|
|
// This is an awful temporary hack to replace Constraint::GetEdges until we have proper
|
|
|
|
// export through Canvas.
|
|
|
|
class GetEdgesCanvas : public Canvas {
|
|
|
|
public:
|
|
|
|
Camera camera;
|
|
|
|
SEdgeList *edges;
|
|
|
|
|
|
|
|
const Camera &GetCamera() const override {
|
|
|
|
return camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawLine(const Vector &a, const Vector &b, hStroke hcs) override {
|
|
|
|
edges->AddEdge(a, b, Style::CONSTRAINT);
|
|
|
|
}
|
|
|
|
void DrawEdges(const SEdgeList &el, hStroke hcs) override {
|
|
|
|
for(const SEdge &e : el.l) {
|
|
|
|
edges->AddEdge(e.a, e.b, Style::CONSTRAINT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DrawVectorText(const std::string &text, double height,
|
|
|
|
const Vector &o, const Vector &u, const Vector &v,
|
|
|
|
hStroke hcs) override {
|
|
|
|
auto traceEdge = [&](Vector a, Vector b) { edges->AddEdge(a, b, Style::CONSTRAINT); };
|
|
|
|
VectorFont::Builtin()->Trace(height, o, u, v, text, traceEdge, camera);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawQuad(const Vector &a, const Vector &b, const Vector &c, const Vector &d,
|
|
|
|
hFill hcf) override {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DrawBeziers(const SBezierList &bl, hStroke hcs) override {
|
|
|
|
ssassert(false, "Not implemented");
|
|
|
|
}
|
2016-06-26 06:39:27 +00:00
|
|
|
void DrawOutlines(const SOutlineList &ol, hStroke hcs, DrawOutlinesAs drawAs) override {
|
Abstract all (ex-OpenGL) drawing operations into a Canvas interface.
This has several desirable consequences:
* It is now possible to port SolveSpace to a later version of
OpenGL, such as OpenGLES 2, so that it runs on platforms that
only have that OpenGL version;
* The majority of geometry is now rendered without references to
the camera in C++ code, so a renderer can now submit it to
the video card once and re-rasterize with a different projection
matrix every time the projection is changed, avoiding expensive
reuploads;
* The DOGD (draw or get distance) interface is now
a straightforward Canvas implementation;
* There are no more direct references to SS.GW.(projection)
in sketch rendering code, which allows rendering to multiple
viewports;
* There are no more unnecessary framebuffer flips on CPU on Cocoa
and GTK;
* The platform-dependent GL code is now confined to rendergl1.cpp.
* The Microsoft and Apple headers required by it that are prone to
identifier conflicts are no longer included globally;
* The rendergl1.cpp implementation can now be omitted from
compilation to run SolveSpace headless or with a different
OpenGL version.
Note these implementation details of Canvas:
* GetCamera currently always returns a reference to the field
`Camera camera;`. This is so that a future renderer that caches
geometry in the video memory can define it as asserting, which
would provide assurance against code that could accidentally
put something projection-dependent in the cache;
* Line and triangle rendering is specified through a level of
indirection, hStroke and hFill. This is so that a future renderer
that batches geometry could cheaply group identical styles.
* DrawPixmap and DrawVectorText accept a (o,u,v) and not a matrix.
This is so that a future renderer into an output format that
uses 2d transforms (e.g. SVG) could easily derive those.
Some additional internal changes were required to enable this:
* Pixmap is now always passed as std::shared_ptr<{const ,}Pixmap>.
This is so that the renderer could cache uploaded textures
between API calls, which requires it to capture a (weak)
reference.
* The PlatformPathEqual function was properly extracted into
platform-specific code. This is so that the <windows.h> header
could be included only where needed (in platform/w32* as well
as rendergl1.cpp).
* The SBsp{2,3}::DebugDraw functions were removed. They can be
rewritten using the Canvas API if they are ever needed.
While no visual changes were originally intended, some minor fixes
happened anyway:
* The "emphasis" yellow line from top-left corner is now correctly
rendered much wider.
* The marquee rectangle is now pixel grid aligned.
* The hidden entities now do not clobber the depth buffer, removing
some minor artifacts.
* The workplane "tab" now scales with the font used to render
the workplane name.
* The workplane name font is now taken from the normals style.
* Workplane and constraint line stipple is insignificantly
different. This is so that it can reuse the existing stipple
codepaths; rendering of workplanes and constraints predates
those.
Some debug functionality was added:
* In graphics window, an fps counter that becomes red when
rendering under 60fps is drawn.
2016-05-31 00:55:13 +00:00
|
|
|
ssassert(false, "Not implemented");
|
|
|
|
}
|
2016-07-17 14:46:38 +00:00
|
|
|
void DrawPoint(const Vector &o, hStroke hcs) override {
|
Abstract all (ex-OpenGL) drawing operations into a Canvas interface.
This has several desirable consequences:
* It is now possible to port SolveSpace to a later version of
OpenGL, such as OpenGLES 2, so that it runs on platforms that
only have that OpenGL version;
* The majority of geometry is now rendered without references to
the camera in C++ code, so a renderer can now submit it to
the video card once and re-rasterize with a different projection
matrix every time the projection is changed, avoiding expensive
reuploads;
* The DOGD (draw or get distance) interface is now
a straightforward Canvas implementation;
* There are no more direct references to SS.GW.(projection)
in sketch rendering code, which allows rendering to multiple
viewports;
* There are no more unnecessary framebuffer flips on CPU on Cocoa
and GTK;
* The platform-dependent GL code is now confined to rendergl1.cpp.
* The Microsoft and Apple headers required by it that are prone to
identifier conflicts are no longer included globally;
* The rendergl1.cpp implementation can now be omitted from
compilation to run SolveSpace headless or with a different
OpenGL version.
Note these implementation details of Canvas:
* GetCamera currently always returns a reference to the field
`Camera camera;`. This is so that a future renderer that caches
geometry in the video memory can define it as asserting, which
would provide assurance against code that could accidentally
put something projection-dependent in the cache;
* Line and triangle rendering is specified through a level of
indirection, hStroke and hFill. This is so that a future renderer
that batches geometry could cheaply group identical styles.
* DrawPixmap and DrawVectorText accept a (o,u,v) and not a matrix.
This is so that a future renderer into an output format that
uses 2d transforms (e.g. SVG) could easily derive those.
Some additional internal changes were required to enable this:
* Pixmap is now always passed as std::shared_ptr<{const ,}Pixmap>.
This is so that the renderer could cache uploaded textures
between API calls, which requires it to capture a (weak)
reference.
* The PlatformPathEqual function was properly extracted into
platform-specific code. This is so that the <windows.h> header
could be included only where needed (in platform/w32* as well
as rendergl1.cpp).
* The SBsp{2,3}::DebugDraw functions were removed. They can be
rewritten using the Canvas API if they are ever needed.
While no visual changes were originally intended, some minor fixes
happened anyway:
* The "emphasis" yellow line from top-left corner is now correctly
rendered much wider.
* The marquee rectangle is now pixel grid aligned.
* The hidden entities now do not clobber the depth buffer, removing
some minor artifacts.
* The workplane "tab" now scales with the font used to render
the workplane name.
* The workplane name font is now taken from the normals style.
* Workplane and constraint line stipple is insignificantly
different. This is so that it can reuse the existing stipple
codepaths; rendering of workplanes and constraints predates
those.
Some debug functionality was added:
* In graphics window, an fps counter that becomes red when
rendering under 60fps is drawn.
2016-05-31 00:55:13 +00:00
|
|
|
ssassert(false, "Not implemented");
|
|
|
|
}
|
|
|
|
void DrawPolygon(const SPolygon &p, hFill hcf) override {
|
|
|
|
ssassert(false, "Not implemented");
|
|
|
|
}
|
2016-11-17 16:47:45 +00:00
|
|
|
void DrawMesh(const SMesh &m, hFill hcfFront, hFill hcfBack = {}) override {
|
Abstract all (ex-OpenGL) drawing operations into a Canvas interface.
This has several desirable consequences:
* It is now possible to port SolveSpace to a later version of
OpenGL, such as OpenGLES 2, so that it runs on platforms that
only have that OpenGL version;
* The majority of geometry is now rendered without references to
the camera in C++ code, so a renderer can now submit it to
the video card once and re-rasterize with a different projection
matrix every time the projection is changed, avoiding expensive
reuploads;
* The DOGD (draw or get distance) interface is now
a straightforward Canvas implementation;
* There are no more direct references to SS.GW.(projection)
in sketch rendering code, which allows rendering to multiple
viewports;
* There are no more unnecessary framebuffer flips on CPU on Cocoa
and GTK;
* The platform-dependent GL code is now confined to rendergl1.cpp.
* The Microsoft and Apple headers required by it that are prone to
identifier conflicts are no longer included globally;
* The rendergl1.cpp implementation can now be omitted from
compilation to run SolveSpace headless or with a different
OpenGL version.
Note these implementation details of Canvas:
* GetCamera currently always returns a reference to the field
`Camera camera;`. This is so that a future renderer that caches
geometry in the video memory can define it as asserting, which
would provide assurance against code that could accidentally
put something projection-dependent in the cache;
* Line and triangle rendering is specified through a level of
indirection, hStroke and hFill. This is so that a future renderer
that batches geometry could cheaply group identical styles.
* DrawPixmap and DrawVectorText accept a (o,u,v) and not a matrix.
This is so that a future renderer into an output format that
uses 2d transforms (e.g. SVG) could easily derive those.
Some additional internal changes were required to enable this:
* Pixmap is now always passed as std::shared_ptr<{const ,}Pixmap>.
This is so that the renderer could cache uploaded textures
between API calls, which requires it to capture a (weak)
reference.
* The PlatformPathEqual function was properly extracted into
platform-specific code. This is so that the <windows.h> header
could be included only where needed (in platform/w32* as well
as rendergl1.cpp).
* The SBsp{2,3}::DebugDraw functions were removed. They can be
rewritten using the Canvas API if they are ever needed.
While no visual changes were originally intended, some minor fixes
happened anyway:
* The "emphasis" yellow line from top-left corner is now correctly
rendered much wider.
* The marquee rectangle is now pixel grid aligned.
* The hidden entities now do not clobber the depth buffer, removing
some minor artifacts.
* The workplane "tab" now scales with the font used to render
the workplane name.
* The workplane name font is now taken from the normals style.
* Workplane and constraint line stipple is insignificantly
different. This is so that it can reuse the existing stipple
codepaths; rendering of workplanes and constraints predates
those.
Some debug functionality was added:
* In graphics window, an fps counter that becomes red when
rendering under 60fps is drawn.
2016-05-31 00:55:13 +00:00
|
|
|
ssassert(false, "Not implemented");
|
|
|
|
}
|
|
|
|
void DrawFaces(const SMesh &m, const std::vector<uint32_t> &faces, hFill hcf) override {
|
|
|
|
ssassert(false, "Not implemented");
|
|
|
|
}
|
|
|
|
void DrawPixmap(std::shared_ptr<const Pixmap> pm,
|
|
|
|
const Vector &o, const Vector &u, const Vector &v,
|
|
|
|
const Point2d &ta, const Point2d &tb, hFill hcf) override {
|
|
|
|
ssassert(false, "Not implemented");
|
|
|
|
}
|
|
|
|
void InvalidatePixmap(std::shared_ptr<const Pixmap> pm) override {
|
|
|
|
ssassert(false, "Not implemented");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-11 14:43:21 +00:00
|
|
|
void SolveSpaceUI::ExportViewOrWireframeTo(const Platform::Path &filename, bool exportWireframe) {
|
2015-03-27 15:31:23 +00:00
|
|
|
SEdgeList edges = {};
|
|
|
|
SBezierList beziers = {};
|
2008-07-08 07:45:47 +00:00
|
|
|
|
2016-01-06 12:40:17 +00:00
|
|
|
VectorFileWriter *out = VectorFileWriter::ForFile(filename);
|
|
|
|
if(!out) return;
|
|
|
|
|
2016-01-27 04:07:54 +00:00
|
|
|
SS.exportMode = 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
|
|
|
GenerateAll(Generate::ALL);
|
2016-01-27 04:07:54 +00:00
|
|
|
|
2009-03-17 16:33:46 +00:00
|
|
|
SMesh *sm = NULL;
|
2016-08-13 09:02:12 +00:00
|
|
|
if(SS.GW.showShaded || SS.GW.drawOccludedAs != GraphicsWindow::DrawOccludedAs::VISIBLE) {
|
2009-05-21 09:06:26 +00:00
|
|
|
Group *g = SK.GetGroup(SS.GW.activeGroup);
|
|
|
|
g->GenerateDisplayItems();
|
|
|
|
sm = &(g->displayMesh);
|
2009-03-17 16:33:46 +00:00
|
|
|
}
|
2009-09-22 06:47:11 +00:00
|
|
|
if(sm && sm->IsEmpty()) {
|
2009-04-15 02:55:18 +00:00
|
|
|
sm = NULL;
|
|
|
|
}
|
2008-07-08 07:45:47 +00:00
|
|
|
|
2018-01-04 01:42:38 +00:00
|
|
|
for(auto &entity : SK.entity) {
|
|
|
|
Entity *e = &entity;
|
2009-04-14 04:19:23 +00:00
|
|
|
if(!e->IsVisible()) continue;
|
|
|
|
|
2016-03-09 04:53:46 +00:00
|
|
|
if(SS.exportPwlCurves || sm || fabs(SS.exportOffset) > LENGTH_EPS)
|
2009-04-16 04:42:51 +00:00
|
|
|
{
|
2009-04-14 04:19:23 +00:00
|
|
|
// We will be doing hidden line removal, which we can't do on
|
2009-04-16 04:42:51 +00:00
|
|
|
// exact curves; so we need things broken down to pwls. Same
|
|
|
|
// problem with cutter radius compensation.
|
2009-04-14 04:19:23 +00:00
|
|
|
e->GenerateEdges(&edges);
|
|
|
|
} else {
|
|
|
|
e->GenerateBezierCurves(&beziers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 00:55:27 +00:00
|
|
|
if(SS.GW.showEdges || SS.GW.showOutlines) {
|
2009-05-21 09:06:26 +00:00
|
|
|
Group *g = SK.GetGroup(SS.GW.activeGroup);
|
|
|
|
g->GenerateDisplayItems();
|
2016-08-14 00:55:27 +00:00
|
|
|
if(SS.GW.showEdges) {
|
|
|
|
g->displayOutlines.ListTaggedInto(&edges, Style::SOLID_EDGE);
|
|
|
|
}
|
2009-03-18 04:26:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 10:17:14 +00:00
|
|
|
if(SS.GW.showConstraints != GraphicsWindow::ShowConstraintMode::SCM_NOSHOW ) {
|
2016-01-06 12:40:17 +00:00
|
|
|
if(!out->OutputConstraints(&SK.constraint)) {
|
Abstract all (ex-OpenGL) drawing operations into a Canvas interface.
This has several desirable consequences:
* It is now possible to port SolveSpace to a later version of
OpenGL, such as OpenGLES 2, so that it runs on platforms that
only have that OpenGL version;
* The majority of geometry is now rendered without references to
the camera in C++ code, so a renderer can now submit it to
the video card once and re-rasterize with a different projection
matrix every time the projection is changed, avoiding expensive
reuploads;
* The DOGD (draw or get distance) interface is now
a straightforward Canvas implementation;
* There are no more direct references to SS.GW.(projection)
in sketch rendering code, which allows rendering to multiple
viewports;
* There are no more unnecessary framebuffer flips on CPU on Cocoa
and GTK;
* The platform-dependent GL code is now confined to rendergl1.cpp.
* The Microsoft and Apple headers required by it that are prone to
identifier conflicts are no longer included globally;
* The rendergl1.cpp implementation can now be omitted from
compilation to run SolveSpace headless or with a different
OpenGL version.
Note these implementation details of Canvas:
* GetCamera currently always returns a reference to the field
`Camera camera;`. This is so that a future renderer that caches
geometry in the video memory can define it as asserting, which
would provide assurance against code that could accidentally
put something projection-dependent in the cache;
* Line and triangle rendering is specified through a level of
indirection, hStroke and hFill. This is so that a future renderer
that batches geometry could cheaply group identical styles.
* DrawPixmap and DrawVectorText accept a (o,u,v) and not a matrix.
This is so that a future renderer into an output format that
uses 2d transforms (e.g. SVG) could easily derive those.
Some additional internal changes were required to enable this:
* Pixmap is now always passed as std::shared_ptr<{const ,}Pixmap>.
This is so that the renderer could cache uploaded textures
between API calls, which requires it to capture a (weak)
reference.
* The PlatformPathEqual function was properly extracted into
platform-specific code. This is so that the <windows.h> header
could be included only where needed (in platform/w32* as well
as rendergl1.cpp).
* The SBsp{2,3}::DebugDraw functions were removed. They can be
rewritten using the Canvas API if they are ever needed.
While no visual changes were originally intended, some minor fixes
happened anyway:
* The "emphasis" yellow line from top-left corner is now correctly
rendered much wider.
* The marquee rectangle is now pixel grid aligned.
* The hidden entities now do not clobber the depth buffer, removing
some minor artifacts.
* The workplane "tab" now scales with the font used to render
the workplane name.
* The workplane name font is now taken from the normals style.
* Workplane and constraint line stipple is insignificantly
different. This is so that it can reuse the existing stipple
codepaths; rendering of workplanes and constraints predates
those.
Some debug functionality was added:
* In graphics window, an fps counter that becomes red when
rendering under 60fps is drawn.
2016-05-31 00:55:13 +00:00
|
|
|
GetEdgesCanvas canvas = {};
|
|
|
|
canvas.camera = SS.GW.GetCamera();
|
|
|
|
canvas.edges = &edges;
|
|
|
|
|
2016-01-06 12:40:17 +00:00
|
|
|
// The output format cannot represent constraints directly,
|
|
|
|
// so convert them to edges.
|
Abstract all (ex-OpenGL) drawing operations into a Canvas interface.
This has several desirable consequences:
* It is now possible to port SolveSpace to a later version of
OpenGL, such as OpenGLES 2, so that it runs on platforms that
only have that OpenGL version;
* The majority of geometry is now rendered without references to
the camera in C++ code, so a renderer can now submit it to
the video card once and re-rasterize with a different projection
matrix every time the projection is changed, avoiding expensive
reuploads;
* The DOGD (draw or get distance) interface is now
a straightforward Canvas implementation;
* There are no more direct references to SS.GW.(projection)
in sketch rendering code, which allows rendering to multiple
viewports;
* There are no more unnecessary framebuffer flips on CPU on Cocoa
and GTK;
* The platform-dependent GL code is now confined to rendergl1.cpp.
* The Microsoft and Apple headers required by it that are prone to
identifier conflicts are no longer included globally;
* The rendergl1.cpp implementation can now be omitted from
compilation to run SolveSpace headless or with a different
OpenGL version.
Note these implementation details of Canvas:
* GetCamera currently always returns a reference to the field
`Camera camera;`. This is so that a future renderer that caches
geometry in the video memory can define it as asserting, which
would provide assurance against code that could accidentally
put something projection-dependent in the cache;
* Line and triangle rendering is specified through a level of
indirection, hStroke and hFill. This is so that a future renderer
that batches geometry could cheaply group identical styles.
* DrawPixmap and DrawVectorText accept a (o,u,v) and not a matrix.
This is so that a future renderer into an output format that
uses 2d transforms (e.g. SVG) could easily derive those.
Some additional internal changes were required to enable this:
* Pixmap is now always passed as std::shared_ptr<{const ,}Pixmap>.
This is so that the renderer could cache uploaded textures
between API calls, which requires it to capture a (weak)
reference.
* The PlatformPathEqual function was properly extracted into
platform-specific code. This is so that the <windows.h> header
could be included only where needed (in platform/w32* as well
as rendergl1.cpp).
* The SBsp{2,3}::DebugDraw functions were removed. They can be
rewritten using the Canvas API if they are ever needed.
While no visual changes were originally intended, some minor fixes
happened anyway:
* The "emphasis" yellow line from top-left corner is now correctly
rendered much wider.
* The marquee rectangle is now pixel grid aligned.
* The hidden entities now do not clobber the depth buffer, removing
some minor artifacts.
* The workplane "tab" now scales with the font used to render
the workplane name.
* The workplane name font is now taken from the normals style.
* Workplane and constraint line stipple is insignificantly
different. This is so that it can reuse the existing stipple
codepaths; rendering of workplanes and constraints predates
those.
Some debug functionality was added:
* In graphics window, an fps counter that becomes red when
rendering under 60fps is drawn.
2016-05-31 00:55:13 +00:00
|
|
|
for(Constraint &c : SK.constraint) {
|
|
|
|
c.Draw(Constraint::DrawAs::DEFAULT, &canvas);
|
2016-01-06 12:40:17 +00:00
|
|
|
}
|
2016-08-07 19:33:42 +00:00
|
|
|
|
|
|
|
canvas.Clear();
|
2009-07-03 20:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 12:08:19 +00:00
|
|
|
if(exportWireframe) {
|
2016-01-06 12:40:17 +00:00
|
|
|
Vector u = Vector::From(1.0, 0.0, 0.0),
|
|
|
|
v = Vector::From(0.0, 1.0, 0.0),
|
|
|
|
n = Vector::From(0.0, 0.0, 1.0),
|
|
|
|
origin = Vector::From(0.0, 0.0, 0.0);
|
|
|
|
double cameraTan = 0.0,
|
|
|
|
scale = 1.0;
|
|
|
|
|
|
|
|
out->SetModelviewProjection(u, v, n, origin, cameraTan, scale);
|
|
|
|
|
|
|
|
ExportWireframeCurves(&edges, &beziers, out);
|
2009-10-12 10:40:48 +00:00
|
|
|
} else {
|
|
|
|
Vector u = SS.GW.projRight,
|
|
|
|
v = SS.GW.projUp,
|
|
|
|
n = u.Cross(v),
|
|
|
|
origin = SS.GW.offset.ScaledBy(-1);
|
|
|
|
|
2016-01-06 12:40:17 +00:00
|
|
|
out->SetModelviewProjection(u, v, n, origin,
|
|
|
|
SS.CameraTangent()*SS.GW.scale, SS.exportScale);
|
2010-01-14 05:24:32 +00:00
|
|
|
|
2016-01-06 12:40:17 +00:00
|
|
|
ExportLinesAndMesh(&edges, &beziers, sm,
|
|
|
|
u, v, n, origin, SS.CameraTangent()*SS.GW.scale,
|
|
|
|
out);
|
|
|
|
|
|
|
|
if(!out->HasCanvasSize()) {
|
2010-01-14 05:24:32 +00:00
|
|
|
// These file formats don't have a canvas size, so they just
|
|
|
|
// get exported in the raw coordinate system. So indicate what
|
|
|
|
// that was on-screen.
|
2016-01-27 04:07:54 +00:00
|
|
|
SS.justExportedInfo.showOrigin = true;
|
2010-01-14 05:24:32 +00:00
|
|
|
SS.justExportedInfo.pt = origin;
|
|
|
|
SS.justExportedInfo.u = u;
|
|
|
|
SS.justExportedInfo.v = v;
|
2016-01-27 04:07:54 +00:00
|
|
|
} else {
|
|
|
|
SS.justExportedInfo.showOrigin = false;
|
2010-01-14 05:24:32 +00:00
|
|
|
}
|
2016-01-27 04:07:54 +00:00
|
|
|
|
|
|
|
SS.justExportedInfo.draw = true;
|
2018-07-12 19:29:44 +00:00
|
|
|
GW.Invalidate();
|
2009-01-14 05:10:42 +00:00
|
|
|
}
|
2009-10-12 10:40:48 +00:00
|
|
|
|
2009-01-14 05:10:42 +00:00
|
|
|
edges.Clear();
|
2009-04-14 04:19:23 +00:00
|
|
|
beziers.Clear();
|
2009-01-14 05:10:42 +00:00
|
|
|
}
|
2008-07-08 07:45:47 +00:00
|
|
|
|
2015-03-23 17:49:04 +00:00
|
|
|
void SolveSpaceUI::ExportWireframeCurves(SEdgeList *sel, SBezierList *sbl,
|
2009-10-12 10:40:48 +00:00
|
|
|
VectorFileWriter *out)
|
|
|
|
{
|
2015-03-27 15:31:23 +00:00
|
|
|
SBezierLoopSetSet sblss = {};
|
2009-10-12 10:40:48 +00:00
|
|
|
SEdge *se;
|
|
|
|
for(se = sel->l.First(); se; se = sel->l.NextAfter(se)) {
|
2009-10-30 10:38:34 +00:00
|
|
|
SBezier sb = SBezier::From(
|
|
|
|
(se->a).ScaledBy(1.0 / SS.exportScale),
|
|
|
|
(se->b).ScaledBy(1.0 / SS.exportScale));
|
|
|
|
sblss.AddOpenPath(&sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
sbl->ScaleSelfBy(1.0/SS.exportScale);
|
|
|
|
SBezier *sb;
|
|
|
|
for(sb = sbl->l.First(); sb; sb = sbl->l.NextAfter(sb)) {
|
|
|
|
sblss.AddOpenPath(sb);
|
2009-10-12 10:40:48 +00:00
|
|
|
}
|
2009-10-30 10:38:34 +00:00
|
|
|
|
2016-01-06 12:40:17 +00:00
|
|
|
out->OutputLinesAndMesh(&sblss, NULL);
|
2009-10-30 10:38:34 +00:00
|
|
|
sblss.Clear();
|
2009-10-12 10:40:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-23 17:49:04 +00:00
|
|
|
void SolveSpaceUI::ExportLinesAndMesh(SEdgeList *sel, SBezierList *sbl, SMesh *sm,
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector u, Vector v, Vector n,
|
|
|
|
Vector origin, double cameraTan,
|
|
|
|
VectorFileWriter *out)
|
2009-01-14 05:10:42 +00:00
|
|
|
{
|
2009-03-17 16:33:46 +00:00
|
|
|
double s = 1.0 / SS.exportScale;
|
|
|
|
|
2008-08-14 08:28:25 +00:00
|
|
|
// Project into the export plane; so when we're done, z doesn't matter,
|
|
|
|
// and x and y are what goes in the DXF.
|
2019-05-15 21:30:49 +00:00
|
|
|
for(SEdge *e = sel->l.First(); e; e = sel->l.NextAfter(e)) {
|
2009-03-17 16:33:46 +00:00
|
|
|
// project into the specified csys, and apply export scale
|
|
|
|
(e->a) = e->a.InPerspective(u, v, n, origin, cameraTan).ScaledBy(s);
|
|
|
|
(e->b) = e->b.InPerspective(u, v, n, origin, cameraTan).ScaledBy(s);
|
2008-08-14 08:28:25 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 04:19:23 +00:00
|
|
|
if(sbl) {
|
2019-05-15 21:30:49 +00:00
|
|
|
for(SBezier *b = sbl->l.First(); b; b = sbl->l.NextAfter(b)) {
|
2009-04-14 04:19:23 +00:00
|
|
|
*b = b->InPerspective(u, v, n, origin, cameraTan);
|
|
|
|
int i;
|
|
|
|
for(i = 0; i <= b->deg; i++) {
|
|
|
|
b->ctrl[i] = (b->ctrl[i]).ScaledBy(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-17 16:33:46 +00:00
|
|
|
// If cutter radius compensation is requested, then perform it now
|
2008-08-14 08:28:25 +00:00
|
|
|
if(fabs(SS.exportOffset) > LENGTH_EPS) {
|
2009-03-17 16:33:46 +00:00
|
|
|
// assemble those edges into a polygon, and clear the edge list
|
2015-03-27 15:31:23 +00:00
|
|
|
SPolygon sp = {};
|
2009-03-17 16:33:46 +00:00
|
|
|
sel->AssemblePolygon(&sp, NULL);
|
|
|
|
sel->Clear();
|
|
|
|
|
2015-03-27 15:31:23 +00:00
|
|
|
SPolygon compd = {};
|
2009-03-17 16:33:46 +00:00
|
|
|
sp.normal = Vector::From(0, 0, -1);
|
|
|
|
sp.FixContourDirections();
|
2009-09-03 08:13:09 +00:00
|
|
|
sp.OffsetInto(&compd, SS.exportOffset*s);
|
2009-03-17 16:33:46 +00:00
|
|
|
sp.Clear();
|
|
|
|
|
|
|
|
compd.MakeEdgesInto(sel);
|
|
|
|
compd.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now the triangle mesh; project, then build a BSP to perform
|
|
|
|
// occlusion testing and generated the shaded surfaces.
|
2015-03-27 15:31:23 +00:00
|
|
|
SMesh smp = {};
|
2009-03-17 16:33:46 +00:00
|
|
|
if(sm) {
|
|
|
|
Vector l0 = (SS.lightDir[0]).WithMagnitude(1),
|
|
|
|
l1 = (SS.lightDir[1]).WithMagnitude(1);
|
|
|
|
STriangle *tr;
|
|
|
|
for(tr = sm->l.First(); tr; tr = sm->l.NextAfter(tr)) {
|
|
|
|
STriangle tt = *tr;
|
|
|
|
tt.a = (tt.a).InPerspective(u, v, n, origin, cameraTan).ScaledBy(s);
|
|
|
|
tt.b = (tt.b).InPerspective(u, v, n, origin, cameraTan).ScaledBy(s);
|
|
|
|
tt.c = (tt.c).InPerspective(u, v, n, origin, cameraTan).ScaledBy(s);
|
|
|
|
|
|
|
|
// And calculate lighting for the triangle
|
|
|
|
Vector n = tt.Normal().WithMagnitude(1);
|
2021-04-02 00:52:03 +00:00
|
|
|
double lighting = min(1.0, SS.ambientIntensity +
|
2015-03-27 15:43:28 +00:00
|
|
|
max(0.0, (SS.lightIntensity[0])*(n.Dot(l0))) +
|
2021-04-02 00:52:03 +00:00
|
|
|
max(0.0, (SS.lightIntensity[1])*(n.Dot(l1))));
|
2015-03-27 15:43:28 +00:00
|
|
|
double r = min(1.0, tt.meta.color.redF() * lighting),
|
|
|
|
g = min(1.0, tt.meta.color.greenF() * lighting),
|
|
|
|
b = min(1.0, tt.meta.color.blueF() * lighting);
|
2009-03-17 16:33:46 +00:00
|
|
|
tt.meta.color = RGBf(r, g, b);
|
|
|
|
smp.AddTriangle(&tt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 15:31:23 +00:00
|
|
|
SMesh sms = {};
|
2016-04-28 11:10:36 +00:00
|
|
|
|
|
|
|
// We need the mesh for occlusion testing, but if we don't/can't export it,
|
|
|
|
// don't generate it.
|
|
|
|
if(SS.GW.showShaded && out->CanOutputMesh()) {
|
|
|
|
// Use the BSP routines to generate the split triangles in paint order.
|
|
|
|
SBsp3 *bsp = SBsp3::FromMesh(&smp);
|
|
|
|
if(bsp) bsp->GenerateInPaintOrder(&sms);
|
|
|
|
// And cull the back-facing triangles
|
|
|
|
STriangle *tr;
|
|
|
|
sms.l.ClearTags();
|
|
|
|
for(tr = sms.l.First(); tr; tr = sms.l.NextAfter(tr)) {
|
|
|
|
Vector n = tr->Normal();
|
|
|
|
if(n.z < 0) {
|
|
|
|
tr->tag = 1;
|
|
|
|
}
|
2009-03-17 16:33:46 +00:00
|
|
|
}
|
2016-04-28 11:10:36 +00:00
|
|
|
sms.l.RemoveTagged();
|
2009-03-17 16:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// And now we perform hidden line removal if requested
|
2015-03-27 15:31:23 +00:00
|
|
|
SEdgeList hlrd = {};
|
2016-03-09 04:53:46 +00:00
|
|
|
if(sm) {
|
2009-03-17 16:33:46 +00:00
|
|
|
SKdNode *root = SKdNode::From(&smp);
|
2009-03-18 04:26:04 +00:00
|
|
|
|
|
|
|
// Generate the edges where a curved surface turns from front-facing
|
|
|
|
// to back-facing.
|
2016-08-14 00:55:27 +00:00
|
|
|
if(SS.GW.showEdges || SS.GW.showOutlines) {
|
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
|
|
|
root->MakeCertainEdgesInto(sel, EdgeKind::TURNING,
|
2016-03-14 16:14:24 +00:00
|
|
|
/*coplanarIsInter=*/false, NULL, NULL,
|
|
|
|
GW.showOutlines ? Style::OUTLINE : Style::SOLID_EDGE);
|
2009-03-18 04:26:04 +00:00
|
|
|
}
|
|
|
|
|
2009-03-17 16:33:46 +00:00
|
|
|
root->ClearTags();
|
|
|
|
int cnt = 1234;
|
|
|
|
|
|
|
|
SEdge *se;
|
|
|
|
for(se = sel->l.First(); se; se = sel->l.NextAfter(se)) {
|
2009-09-22 06:47:11 +00:00
|
|
|
if(se->auxA == Style::CONSTRAINT) {
|
|
|
|
// Constraints should not get hidden line removed; they're
|
|
|
|
// always on top.
|
|
|
|
hlrd.AddEdge(se->a, se->b, se->auxA);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-09 04:53:46 +00:00
|
|
|
SEdgeList edges = {};
|
2009-03-17 16:33:46 +00:00
|
|
|
// Split the original edge against the mesh
|
2016-03-09 04:53:46 +00:00
|
|
|
edges.AddEdge(se->a, se->b, se->auxA);
|
2016-07-21 20:27:53 +00:00
|
|
|
root->OcclusionTestLine(*se, &edges, cnt);
|
2016-08-13 09:02:12 +00:00
|
|
|
if(SS.GW.drawOccludedAs == GraphicsWindow::DrawOccludedAs::STIPPLED) {
|
2016-07-21 20:27:53 +00:00
|
|
|
for(SEdge &se : edges.l) {
|
|
|
|
if(se.tag == 1) {
|
|
|
|
se.auxA = Style::HIDDEN_EDGE;
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 09:02:12 +00:00
|
|
|
} else if(SS.GW.drawOccludedAs == GraphicsWindow::DrawOccludedAs::INVISIBLE) {
|
2016-07-21 20:27:53 +00:00
|
|
|
edges.l.RemoveTagged();
|
|
|
|
}
|
|
|
|
|
2009-04-08 04:54:07 +00:00
|
|
|
// the occlusion test splits unnecessarily; so fix those
|
2016-03-09 04:53:46 +00:00
|
|
|
edges.MergeCollinearSegments(se->a, se->b);
|
2009-03-17 16:33:46 +00:00
|
|
|
cnt++;
|
|
|
|
// And add the results to our output
|
|
|
|
SEdge *sen;
|
2016-03-09 04:53:46 +00:00
|
|
|
for(sen = edges.l.First(); sen; sen = edges.l.NextAfter(sen)) {
|
2009-09-22 06:47:11 +00:00
|
|
|
hlrd.AddEdge(sen->a, sen->b, sen->auxA);
|
2009-03-17 16:33:46 +00:00
|
|
|
}
|
2016-03-09 04:53:46 +00:00
|
|
|
edges.Clear();
|
2009-03-17 16:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sel = &hlrd;
|
2009-01-14 05:10:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 09:27:59 +00:00
|
|
|
// Clean up: remove overlapping line segments and
|
|
|
|
// segments with zero-length projections.
|
|
|
|
sel->l.ClearTags();
|
|
|
|
for(int i = 0; i < sel->l.n; ++i) {
|
2019-05-23 17:09:47 +00:00
|
|
|
SEdge *sei = &sel->l[i];
|
2016-03-13 09:27:59 +00:00
|
|
|
hStyle hsi = { (uint32_t)sei->auxA };
|
|
|
|
Style *si = Style::Get(hsi);
|
|
|
|
if(sei->tag != 0) continue;
|
|
|
|
|
|
|
|
// Remove segments with zero length projections.
|
|
|
|
Vector ai = sei->a;
|
|
|
|
ai.z = 0.0;
|
|
|
|
Vector bi = sei->b;
|
|
|
|
bi.z = 0.0;
|
|
|
|
Vector di = bi.Minus(ai);
|
|
|
|
if(fabs(di.x) < LENGTH_EPS && fabs(di.y) < LENGTH_EPS) {
|
|
|
|
sei->tag = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int j = i + 1; j < sel->l.n; ++j) {
|
2019-05-23 17:09:47 +00:00
|
|
|
SEdge *sej = &sel->l[j];
|
2016-03-13 09:27:59 +00:00
|
|
|
if(sej->tag != 0) continue;
|
|
|
|
|
|
|
|
Vector *pAj = &sej->a;
|
|
|
|
Vector *pBj = &sej->b;
|
|
|
|
|
|
|
|
// Remove segments with zero length projections.
|
|
|
|
Vector aj = sej->a;
|
|
|
|
aj.z = 0.0;
|
|
|
|
Vector bj = sej->b;
|
|
|
|
bj.z = 0.0;
|
|
|
|
Vector dj = bj.Minus(aj);
|
|
|
|
if(fabs(dj.x) < LENGTH_EPS && fabs(dj.y) < LENGTH_EPS) {
|
|
|
|
sej->tag = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip non-collinear segments.
|
|
|
|
const double eps = 1e-6;
|
|
|
|
if(aj.DistanceToLine(ai, di) > eps) continue;
|
|
|
|
if(bj.DistanceToLine(ai, di) > eps) continue;
|
|
|
|
|
|
|
|
double ta = aj.Minus(ai).Dot(di) / di.Dot(di);
|
|
|
|
double tb = bj.Minus(ai).Dot(di) / di.Dot(di);
|
|
|
|
if(ta > tb) {
|
|
|
|
std::swap(pAj, pBj);
|
|
|
|
std::swap(ta, tb);
|
|
|
|
}
|
|
|
|
|
|
|
|
hStyle hsj = { (uint32_t)sej->auxA };
|
|
|
|
Style *sj = Style::Get(hsj);
|
|
|
|
|
|
|
|
bool canRemoveI = sej->auxA == sei->auxA || si->zIndex < sj->zIndex;
|
|
|
|
bool canRemoveJ = sej->auxA == sei->auxA || sj->zIndex < si->zIndex;
|
|
|
|
|
|
|
|
if(canRemoveJ) {
|
|
|
|
// j-segment inside i-segment
|
|
|
|
if(ta > 0.0 - eps && tb < 1.0 + eps) {
|
|
|
|
sej->tag = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cut segment
|
|
|
|
bool aInside = ta > 0.0 - eps && ta < 1.0 + eps;
|
|
|
|
if(tb > 1.0 - eps && aInside) {
|
|
|
|
*pAj = sei->b;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cut segment
|
|
|
|
bool bInside = tb > 0.0 - eps && tb < 1.0 + eps;
|
|
|
|
if(ta < 0.0 - eps && bInside) {
|
|
|
|
*pBj = sei->a;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// split segment
|
|
|
|
if(ta < 0.0 - eps && tb > 1.0 + eps) {
|
|
|
|
sel->AddEdge(sei->b, *pBj, sej->auxA, sej->auxB);
|
|
|
|
*pBj = sei->a;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(canRemoveI) {
|
|
|
|
// j-segment inside i-segment
|
|
|
|
if(ta < 0.0 + eps && tb > 1.0 - eps) {
|
|
|
|
sei->tag = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cut segment
|
|
|
|
bool aInside = ta > 0.0 + eps && ta < 1.0 - eps;
|
|
|
|
if(tb > 1.0 - eps && aInside) {
|
|
|
|
sei->b = *pAj;
|
|
|
|
i--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cut segment
|
|
|
|
bool bInside = tb > 0.0 + eps && tb < 1.0 - eps;
|
|
|
|
if(ta < 0.0 + eps && bInside) {
|
|
|
|
sei->a = *pBj;
|
|
|
|
i--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// split segment
|
|
|
|
if(ta > 0.0 + eps && tb < 1.0 - eps) {
|
|
|
|
sel->AddEdge(*pBj, sei->b, sei->auxA, sei->auxB);
|
|
|
|
sei->b = *pAj;
|
|
|
|
i--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sel->l.RemoveTagged();
|
|
|
|
|
2009-10-30 10:38:34 +00:00
|
|
|
// We kept the line segments and Beziers separate until now; but put them
|
|
|
|
// all together, and also project everything into the xy plane, since not
|
|
|
|
// all export targets ignore the z component of the points.
|
2019-05-15 21:30:49 +00:00
|
|
|
ssassert(sbl != nullptr, "Adding line segments to beziers assumes bezier list is non-null.");
|
|
|
|
for(SEdge *e = sel->l.First(); e; e = sel->l.NextAfter(e)) {
|
2009-10-30 10:38:34 +00:00
|
|
|
SBezier sb = SBezier::From(e->a, e->b);
|
|
|
|
sb.auxA = e->auxA;
|
|
|
|
sbl->l.Add(&sb);
|
|
|
|
}
|
2019-05-15 21:30:49 +00:00
|
|
|
for(SBezier *b = sbl->l.First(); b; b = sbl->l.NextAfter(b)) {
|
2009-10-30 10:38:34 +00:00
|
|
|
for(int i = 0; i <= b->deg; i++) {
|
|
|
|
b->ctrl[i].z = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If possible, then we will assemble these output curves into loops. They
|
|
|
|
// will then get exported as closed paths.
|
2015-03-27 15:31:23 +00:00
|
|
|
SBezierLoopSetSet sblss = {};
|
2019-05-13 14:34:22 +00:00
|
|
|
SBezierLoopSet leftovers = {};
|
2009-10-30 10:38:34 +00:00
|
|
|
SSurface srf = SSurface::FromPlane(Vector::From(0, 0, 0),
|
|
|
|
Vector::From(1, 0, 0),
|
|
|
|
Vector::From(0, 1, 0));
|
2015-03-27 15:31:23 +00:00
|
|
|
SPolygon spxyz = {};
|
2009-10-30 10:38:34 +00:00
|
|
|
bool allClosed;
|
|
|
|
SEdge notClosedAt;
|
|
|
|
sbl->l.ClearTags();
|
|
|
|
sblss.FindOuterFacesFrom(sbl, &spxyz, &srf,
|
2016-01-27 04:07:54 +00:00
|
|
|
SS.ExportChordTolMm(),
|
2009-10-30 10:38:34 +00:00
|
|
|
&allClosed, ¬ClosedAt,
|
|
|
|
NULL, NULL,
|
|
|
|
&leftovers);
|
2019-05-13 14:34:22 +00:00
|
|
|
sblss.l.Add(&leftovers);
|
2009-10-30 10:38:34 +00:00
|
|
|
|
2009-03-17 16:33:46 +00:00
|
|
|
// Now write the lines and triangles to the output file
|
2016-01-06 12:40:17 +00:00
|
|
|
out->OutputLinesAndMesh(&sblss, &sms);
|
2009-03-17 16:33:46 +00:00
|
|
|
|
2009-10-30 10:38:34 +00:00
|
|
|
spxyz.Clear();
|
|
|
|
sblss.Clear();
|
2009-03-17 16:33:46 +00:00
|
|
|
smp.Clear();
|
|
|
|
sms.Clear();
|
|
|
|
hlrd.Clear();
|
2009-01-14 05:10:42 +00:00
|
|
|
}
|
|
|
|
|
2009-04-15 06:50:06 +00:00
|
|
|
double VectorFileWriter::MmToPts(double mm) {
|
|
|
|
// 72 points in an inch
|
|
|
|
return (mm/25.4)*72;
|
|
|
|
}
|
|
|
|
|
2017-03-11 14:43:21 +00:00
|
|
|
VectorFileWriter *VectorFileWriter::ForFile(const Platform::Path &filename) {
|
2009-01-14 05:10:42 +00:00
|
|
|
VectorFileWriter *ret;
|
2015-12-25 08:29:08 +00:00
|
|
|
bool needOpen = true;
|
2017-03-11 14:43:21 +00:00
|
|
|
if(filename.HasExtension("dxf")) {
|
2009-01-14 05:10:42 +00:00
|
|
|
static DxfFileWriter DxfWriter;
|
|
|
|
ret = &DxfWriter;
|
2015-12-25 08:29:08 +00:00
|
|
|
needOpen = false;
|
2017-03-11 14:43:21 +00:00
|
|
|
} else if(filename.HasExtension("ps") || filename.HasExtension("eps")) {
|
2009-01-27 05:48:40 +00:00
|
|
|
static EpsFileWriter EpsWriter;
|
|
|
|
ret = &EpsWriter;
|
2017-03-11 14:43:21 +00:00
|
|
|
} else if(filename.HasExtension("pdf")) {
|
2009-04-15 06:50:06 +00:00
|
|
|
static PdfFileWriter PdfWriter;
|
|
|
|
ret = &PdfWriter;
|
2017-03-11 14:43:21 +00:00
|
|
|
} else if(filename.HasExtension("svg")) {
|
2009-01-27 05:48:40 +00:00
|
|
|
static SvgFileWriter SvgWriter;
|
|
|
|
ret = &SvgWriter;
|
2017-03-11 14:43:21 +00:00
|
|
|
} else if(filename.HasExtension("plt") || filename.HasExtension("hpgl")) {
|
2009-01-27 05:48:40 +00:00
|
|
|
static HpglFileWriter HpglWriter;
|
|
|
|
ret = &HpglWriter;
|
2017-03-11 14:43:21 +00:00
|
|
|
} else if(filename.HasExtension("step") || filename.HasExtension("stp")) {
|
2009-06-11 05:57:23 +00:00
|
|
|
static Step2dFileWriter Step2dWriter;
|
|
|
|
ret = &Step2dWriter;
|
2017-03-11 14:43:21 +00:00
|
|
|
} else if(filename.HasExtension("txt") || filename.HasExtension("ngc")) {
|
2010-01-14 04:47:17 +00:00
|
|
|
static GCodeFileWriter GCodeWriter;
|
|
|
|
ret = &GCodeWriter;
|
2009-01-14 05:10:42 +00:00
|
|
|
} else {
|
2009-01-27 05:48:40 +00:00
|
|
|
Error("Can't identify output file type from file extension of "
|
2010-01-14 04:47:17 +00:00
|
|
|
"filename '%s'; try "
|
2016-08-25 02:57:29 +00:00
|
|
|
".step, .stp, .dxf, .svg, .plt, .hpgl, .pdf, .txt, .ngc, "
|
2009-06-11 05:57:23 +00:00
|
|
|
".eps, or .ps.",
|
2017-03-11 14:43:21 +00:00
|
|
|
filename.raw.c_str());
|
2009-01-14 05:10:42 +00:00
|
|
|
return NULL;
|
2008-08-14 08:28:25 +00:00
|
|
|
}
|
2015-12-25 08:29:08 +00:00
|
|
|
ret->filename = filename;
|
|
|
|
if(!needOpen) return ret;
|
2008-08-14 08:28:25 +00:00
|
|
|
|
2017-03-11 14:43:21 +00:00
|
|
|
FILE *f = OpenFile(filename, "wb");
|
2008-07-08 07:45:47 +00:00
|
|
|
if(!f) {
|
2017-03-11 14:43:21 +00:00
|
|
|
Error("Couldn't write to '%s'", filename.raw.c_str());
|
2009-01-14 05:10:42 +00:00
|
|
|
return NULL;
|
2008-07-08 07:45:47 +00:00
|
|
|
}
|
2009-01-14 05:10:42 +00:00
|
|
|
ret->f = f;
|
|
|
|
return ret;
|
|
|
|
}
|
2008-07-08 07:45:47 +00:00
|
|
|
|
2016-01-06 12:40:17 +00:00
|
|
|
void VectorFileWriter::SetModelviewProjection(const Vector &u, const Vector &v, const Vector &n,
|
|
|
|
const Vector &origin, double cameraTan,
|
|
|
|
double scale) {
|
|
|
|
this->u = u;
|
|
|
|
this->v = v;
|
|
|
|
this->n = n;
|
|
|
|
this->origin = origin;
|
|
|
|
this->cameraTan = cameraTan;
|
|
|
|
this->scale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector VectorFileWriter::Transform(Vector &pos) const {
|
|
|
|
return pos.InPerspective(u, v, n, origin, cameraTan).ScaledBy(1.0 / scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VectorFileWriter::OutputLinesAndMesh(SBezierLoopSetSet *sblss, SMesh *sm) {
|
2009-03-17 16:33:46 +00:00
|
|
|
STriangle *tr;
|
2009-04-14 04:19:23 +00:00
|
|
|
SBezier *b;
|
2009-01-27 05:48:40 +00:00
|
|
|
|
|
|
|
// First calculate the bounding box.
|
|
|
|
ptMin = Vector::From(VERY_POSITIVE, VERY_POSITIVE, VERY_POSITIVE);
|
|
|
|
ptMax = Vector::From(VERY_NEGATIVE, VERY_NEGATIVE, VERY_NEGATIVE);
|
2009-03-17 16:33:46 +00:00
|
|
|
if(sm) {
|
|
|
|
for(tr = sm->l.First(); tr; tr = sm->l.NextAfter(tr)) {
|
|
|
|
(tr->a).MakeMaxMin(&ptMax, &ptMin);
|
|
|
|
(tr->b).MakeMaxMin(&ptMax, &ptMin);
|
|
|
|
(tr->c).MakeMaxMin(&ptMax, &ptMin);
|
2009-01-27 05:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-30 10:38:34 +00:00
|
|
|
if(sblss) {
|
|
|
|
SBezierLoopSet *sbls;
|
|
|
|
for(sbls = sblss->l.First(); sbls; sbls = sblss->l.NextAfter(sbls)) {
|
|
|
|
SBezierLoop *sbl;
|
|
|
|
for(sbl = sbls->l.First(); sbl; sbl = sbls->l.NextAfter(sbl)) {
|
|
|
|
for(b = sbl->l.First(); b; b = sbl->l.NextAfter(b)) {
|
|
|
|
for(int i = 0; i <= b->deg; i++) {
|
|
|
|
(b->ctrl[i]).MakeMaxMin(&ptMax, &ptMin);
|
|
|
|
}
|
|
|
|
}
|
2009-04-14 04:19:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-27 05:48:40 +00:00
|
|
|
|
2009-09-03 08:13:09 +00:00
|
|
|
// And now we compute the canvas size.
|
|
|
|
double s = 1.0 / SS.exportScale;
|
|
|
|
if(SS.exportCanvasSizeAuto) {
|
|
|
|
// It's based on the calculated bounding box; we grow it along each
|
|
|
|
// boundary by the specified amount.
|
|
|
|
ptMin.x -= s*SS.exportMargin.left;
|
|
|
|
ptMax.x += s*SS.exportMargin.right;
|
|
|
|
ptMin.y -= s*SS.exportMargin.bottom;
|
|
|
|
ptMax.y += s*SS.exportMargin.top;
|
|
|
|
} else {
|
2020-09-16 21:22:50 +00:00
|
|
|
ptMin.x = (s*SS.exportCanvas.dx);
|
|
|
|
ptMin.y = (s*SS.exportCanvas.dy);
|
2009-09-03 08:13:09 +00:00
|
|
|
ptMax.x = ptMin.x + (s*SS.exportCanvas.width);
|
|
|
|
ptMax.y = ptMin.y + (s*SS.exportCanvas.height);
|
|
|
|
}
|
|
|
|
|
2009-01-27 05:48:40 +00:00
|
|
|
StartFile();
|
2020-03-25 20:40:14 +00:00
|
|
|
if(SS.exportBackgroundColor) {
|
|
|
|
Background(SS.backgroundColor);
|
|
|
|
}
|
2009-03-18 04:26:04 +00:00
|
|
|
if(sm && SS.exportShadedTriangles) {
|
2009-03-17 16:33:46 +00:00
|
|
|
for(tr = sm->l.First(); tr; tr = sm->l.NextAfter(tr)) {
|
|
|
|
Triangle(tr);
|
|
|
|
}
|
|
|
|
}
|
2009-10-30 10:38:34 +00:00
|
|
|
if(sblss) {
|
|
|
|
SBezierLoopSet *sbls;
|
|
|
|
for(sbls = sblss->l.First(); sbls; sbls = sblss->l.NextAfter(sbls)) {
|
2021-02-28 00:57:58 +00:00
|
|
|
for(SBezierLoop *sbl = sbls->l.First(); sbl; sbl = sbls->l.NextAfter(sbl)) {
|
|
|
|
b = sbl->l.First();
|
|
|
|
if(!b || !Style::Exportable(b->auxA)) continue;
|
|
|
|
|
|
|
|
hStyle hs = { (uint32_t)b->auxA };
|
|
|
|
Style *stl = Style::Get(hs);
|
|
|
|
double lineWidth = Style::WidthMm(b->auxA)*s;
|
|
|
|
RgbaColor strokeRgb = Style::Color(hs, /*forExport=*/true);
|
|
|
|
RgbaColor fillRgb = Style::FillColor(hs, /*forExport=*/true);
|
|
|
|
|
|
|
|
StartPath(strokeRgb, lineWidth, stl->filled, fillRgb, hs);
|
2009-10-30 10:38:34 +00:00
|
|
|
for(b = sbl->l.First(); b; b = sbl->l.NextAfter(b)) {
|
|
|
|
Bezier(b);
|
|
|
|
}
|
2021-02-28 00:57:58 +00:00
|
|
|
FinishPath(strokeRgb, lineWidth, stl->filled, fillRgb, hs);
|
2009-10-30 10:38:34 +00:00
|
|
|
}
|
2009-04-14 04:19:23 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-27 05:48:40 +00:00
|
|
|
FinishAndCloseFile();
|
|
|
|
}
|
|
|
|
|
2009-10-30 10:38:34 +00:00
|
|
|
void VectorFileWriter::BezierAsPwl(SBezier *sb) {
|
2015-03-27 15:31:23 +00:00
|
|
|
List<Vector> lv = {};
|
2016-01-27 04:07:54 +00:00
|
|
|
sb->MakePwlInto(&lv, SS.ExportChordTolMm());
|
2019-08-20 16:22:10 +00:00
|
|
|
|
|
|
|
for(int i = 1; i < lv.n; i++) {
|
2019-05-23 17:09:47 +00:00
|
|
|
SBezier sb = SBezier::From(lv[i-1], lv[i]);
|
2009-10-30 10:38:34 +00:00
|
|
|
Bezier(&sb);
|
2009-04-14 04:19:23 +00:00
|
|
|
}
|
|
|
|
lv.Clear();
|
|
|
|
}
|
|
|
|
|
2009-10-30 10:38:34 +00:00
|
|
|
void VectorFileWriter::BezierAsNonrationalCubic(SBezier *sb, int depth) {
|
2009-04-15 06:50:06 +00:00
|
|
|
Vector t0 = sb->TangentAt(0), t1 = sb->TangentAt(1);
|
|
|
|
// The curve is correct, and the first derivatives are correct, at the
|
|
|
|
// endpoints.
|
|
|
|
SBezier bnr = SBezier::From(
|
|
|
|
sb->Start(),
|
|
|
|
sb->Start().Plus(t0.ScaledBy(1.0/3)),
|
|
|
|
sb->Finish().Minus(t1.ScaledBy(1.0/3)),
|
|
|
|
sb->Finish());
|
|
|
|
|
2016-01-27 04:07:54 +00:00
|
|
|
double tol = SS.ExportChordTolMm();
|
2009-04-15 06:50:06 +00:00
|
|
|
// Arbitrary choice, but make it a little finer than pwl tolerance since
|
|
|
|
// it should be easier to achieve that with the smooth curves.
|
|
|
|
tol /= 2;
|
|
|
|
|
|
|
|
bool closeEnough = true;
|
|
|
|
int i;
|
|
|
|
for(i = 1; i <= 3; i++) {
|
|
|
|
double t = i/4.0;
|
|
|
|
Vector p0 = sb->PointAt(t),
|
|
|
|
pn = bnr.PointAt(t);
|
|
|
|
double d = (p0.Minus(pn)).Magnitude();
|
|
|
|
if(d > tol) {
|
|
|
|
closeEnough = false;
|
|
|
|
}
|
|
|
|
}
|
2015-03-29 00:30:52 +00:00
|
|
|
|
2009-04-15 06:50:06 +00:00
|
|
|
if(closeEnough || depth > 3) {
|
2009-10-30 10:38:34 +00:00
|
|
|
Bezier(&bnr);
|
2009-04-15 06:50:06 +00:00
|
|
|
} else {
|
|
|
|
SBezier bef, aft;
|
|
|
|
sb->SplitAt(0.5, &bef, &aft);
|
2009-10-30 10:38:34 +00:00
|
|
|
BezierAsNonrationalCubic(&bef, depth+1);
|
|
|
|
BezierAsNonrationalCubic(&aft, depth+1);
|
2009-04-15 06:50:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-27 05:48:40 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2009-10-12 11:34:43 +00:00
|
|
|
// Export a triangle mesh, in the requested format.
|
2009-01-27 05:48:40 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-03-11 14:43:21 +00:00
|
|
|
void SolveSpaceUI::ExportMeshTo(const Platform::Path &filename) {
|
2016-01-27 04:07:54 +00:00
|
|
|
SS.exportMode = 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
|
|
|
GenerateAll(Generate::ALL);
|
2016-01-27 04:07:54 +00:00
|
|
|
|
|
|
|
Group *g = SK.GetGroup(SS.GW.activeGroup);
|
|
|
|
g->GenerateDisplayItems();
|
|
|
|
|
2009-05-21 09:06:26 +00:00
|
|
|
SMesh *m = &(SK.GetGroup(SS.GW.activeGroup)->displayMesh);
|
2009-05-24 11:37:07 +00:00
|
|
|
if(m->IsEmpty()) {
|
2017-01-07 06:41:13 +00:00
|
|
|
Error(_("Active group mesh is empty; nothing to export."));
|
2008-07-08 07:45:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-11 14:43:21 +00:00
|
|
|
FILE *f = OpenFile(filename, "wb");
|
2008-07-08 07:45:47 +00:00
|
|
|
if(!f) {
|
2017-03-11 14:43:21 +00:00
|
|
|
Error("Couldn't write to '%s'", filename.raw.c_str());
|
2008-07-08 07:45:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-14 15:16:50 +00:00
|
|
|
ShowNakedEdges(/*reportOnlyWhenNotOkay=*/true);
|
2017-03-11 14:43:21 +00:00
|
|
|
if(filename.HasExtension("stl")) {
|
2009-10-12 11:34:43 +00:00
|
|
|
ExportMeshAsStlTo(f, m);
|
2017-03-11 14:43:21 +00:00
|
|
|
} else if(filename.HasExtension("obj")) {
|
|
|
|
Platform::Path mtlFilename = filename.WithExtension("mtl");
|
|
|
|
FILE *fMtl = OpenFile(mtlFilename, "wb");
|
2016-07-04 06:02:30 +00:00
|
|
|
if(!fMtl) {
|
2017-03-11 14:43:21 +00:00
|
|
|
Error("Couldn't write to '%s'", filename.raw.c_str());
|
2016-07-04 06:02:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-11 14:43:21 +00:00
|
|
|
fprintf(f, "mtllib %s\n", mtlFilename.FileName().c_str());
|
2016-07-04 06:02:30 +00:00
|
|
|
ExportMeshAsObjTo(f, fMtl, m);
|
|
|
|
|
|
|
|
fclose(fMtl);
|
2017-03-11 14:43:21 +00:00
|
|
|
} else if(filename.HasExtension("js") ||
|
|
|
|
filename.HasExtension("html")) {
|
2016-06-26 06:39:27 +00:00
|
|
|
SOutlineList *e = &(SK.GetGroup(SS.GW.activeGroup)->displayOutlines);
|
2015-08-31 19:33:57 +00:00
|
|
|
ExportMeshAsThreeJsTo(f, filename, m, e);
|
2019-08-09 19:08:54 +00:00
|
|
|
} else if(filename.HasExtension("wrl")) {
|
|
|
|
ExportMeshAsVrmlTo(f, filename, m);
|
2009-10-12 11:34:43 +00:00
|
|
|
} else {
|
|
|
|
Error("Can't identify output file type from file extension of "
|
2017-03-11 14:43:21 +00:00
|
|
|
"filename '%s'; try .stl, .obj, .js, .html.", filename.raw.c_str());
|
2009-10-12 11:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
2016-01-27 04:07:54 +00:00
|
|
|
|
|
|
|
SS.justExportedInfo.showOrigin = false;
|
|
|
|
SS.justExportedInfo.draw = true;
|
2018-07-12 19:29:44 +00:00
|
|
|
GW.Invalidate();
|
2009-10-12 11:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Export the mesh as an STL file; it should always be vertex-to-vertex and
|
|
|
|
// not self-intersecting, so not much to do.
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-23 17:49:04 +00:00
|
|
|
void SolveSpaceUI::ExportMeshAsStlTo(FILE *f, SMesh *sm) {
|
2015-03-27 15:31:23 +00:00
|
|
|
char str[80] = {};
|
2008-07-08 07:45:47 +00:00
|
|
|
strcpy(str, "STL exported mesh");
|
|
|
|
fwrite(str, 1, 80, f);
|
|
|
|
|
Use C99 integer types and C++ boolean types/values
This change comprehensively replaces the use of Microsoft-standard integer
and boolean types with their C99/C++ standard equivalents, as the latter is
more appropriate for a cross-platform application. With matter-of-course
exceptions in the Win32-specific code, the types/values have been converted
as follows:
QWORD --> uint64_t
SQWORD --> int64_t
DWORD --> uint32_t
SDWORD --> int32_t
WORD --> uint16_t
SWORD --> int16_t
BYTE --> uint8_t
BOOL --> bool
TRUE --> true
FALSE --> false
The following related changes are also included:
* Added C99 integer type definitions for Windows, as stdint.h is not
available prior to Visual Studio 2010
* Changed types of some variables in the SolveSpace class from 'int' to
'bool', as they actually represent boolean settings
* Implemented new Cnf{Freeze,Thaw}Bool() functions to support boolean
variables in the Registry
* Cnf{Freeze,Thaw}DWORD() are now Cnf{Freeze,Thaw}Int()
* TtfFont::Get{WORD,DWORD}() are now TtfFont::Get{USHORT,ULONG}() (names
inspired by the OpenType spec)
* RGB colors are packed into an integer of type uint32_t (nee DWORD), but
in a few places, these were represented by an int; these have been
corrected to uint32_t
2013-10-02 05:45:13 +00:00
|
|
|
uint32_t n = sm->l.n;
|
2008-07-08 07:45:47 +00:00
|
|
|
fwrite(&n, 4, 1, f);
|
|
|
|
|
|
|
|
double s = SS.exportScale;
|
|
|
|
int i;
|
2009-10-12 11:34:43 +00:00
|
|
|
for(i = 0; i < sm->l.n; i++) {
|
2019-05-23 17:09:47 +00:00
|
|
|
STriangle *tr = &(sm->l[i]);
|
2008-07-08 07:45:47 +00:00
|
|
|
Vector n = tr->Normal().WithMagnitude(1);
|
|
|
|
float w;
|
|
|
|
w = (float)n.x; fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)n.y; fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)n.z; fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)((tr->a.x)/s); fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)((tr->a.y)/s); fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)((tr->a.z)/s); fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)((tr->b.x)/s); fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)((tr->b.y)/s); fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)((tr->b.z)/s); fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)((tr->c.x)/s); fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)((tr->c.y)/s); fwrite(&w, 4, 1, f);
|
|
|
|
w = (float)((tr->c.z)/s); fwrite(&w, 4, 1, f);
|
|
|
|
fputc(0, f);
|
|
|
|
fputc(0, f);
|
|
|
|
}
|
2009-10-12 11:34:43 +00:00
|
|
|
}
|
2008-07-08 07:45:47 +00:00
|
|
|
|
2009-10-12 11:34:43 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Export the mesh as Wavefront OBJ format. This requires us to reduce all the
|
|
|
|
// identical vertices to the same identifier, so do that first.
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-07-04 06:02:30 +00:00
|
|
|
void SolveSpaceUI::ExportMeshAsObjTo(FILE *fObj, FILE *fMtl, SMesh *sm) {
|
|
|
|
std::map<RgbaColor, std::string, RgbaColorCompare> colors;
|
|
|
|
for(const STriangle &t : sm->l) {
|
|
|
|
RgbaColor color = t.meta.color;
|
|
|
|
if(colors.find(color) == colors.end()) {
|
|
|
|
std::string id = ssprintf("h%02x%02x%02x",
|
|
|
|
color.red,
|
|
|
|
color.green,
|
|
|
|
color.blue);
|
|
|
|
colors.emplace(color, id);
|
|
|
|
}
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
fprintf(fObj, "v %.10f %.10f %.10f\n",
|
|
|
|
CO(t.vertices[i].ScaledBy(1 / SS.exportScale)));
|
|
|
|
}
|
2009-10-12 11:34:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 06:02:30 +00:00
|
|
|
for(auto &it : colors) {
|
|
|
|
fprintf(fMtl, "newmtl %s\n",
|
|
|
|
it.second.c_str());
|
|
|
|
fprintf(fMtl, "Kd %.3f %.3f %.3f\n",
|
|
|
|
it.first.redF(), it.first.greenF(), it.first.blueF());
|
2009-10-12 11:34:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 06:02:30 +00:00
|
|
|
for(const STriangle &t : sm->l) {
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
Vector n = t.normals[i].WithMagnitude(1.0);
|
|
|
|
fprintf(fObj, "vn %.10f %.10f %.10f\n",
|
|
|
|
CO(n));
|
|
|
|
}
|
2009-10-12 11:34:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 06:02:30 +00:00
|
|
|
RgbaColor currentColor = {};
|
|
|
|
for(int i = 0; i < sm->l.n; i++) {
|
2019-05-23 17:09:47 +00:00
|
|
|
const STriangle &t = sm->l[i];
|
2016-07-04 06:02:30 +00:00
|
|
|
if(!currentColor.Equals(t.meta.color)) {
|
|
|
|
currentColor = t.meta.color;
|
|
|
|
fprintf(fObj, "usemtl %s\n", colors[currentColor].c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fObj, "f %d//%d %d//%d %d//%d\n",
|
|
|
|
i * 3 + 1, i * 3 + 1,
|
|
|
|
i * 3 + 2, i * 3 + 2,
|
|
|
|
i * 3 + 3, i * 3 + 3);
|
|
|
|
}
|
2008-07-08 07:45:47 +00:00
|
|
|
}
|
|
|
|
|
2009-01-27 05:48:40 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-08-31 19:33:57 +00:00
|
|
|
// Export the mesh as a JavaScript script, which is compatible with Three.js.
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-03-11 14:43:21 +00:00
|
|
|
void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const Platform::Path &filename,
|
2016-06-26 06:39:27 +00:00
|
|
|
SMesh *sm, SOutlineList *sol)
|
2015-08-31 19:33:57 +00:00
|
|
|
{
|
2015-03-27 15:31:23 +00:00
|
|
|
SPointList spl = {};
|
2015-08-31 19:33:57 +00:00
|
|
|
STriangle *tr;
|
|
|
|
Vector bndl, bndh;
|
2021-01-21 21:25:09 +00:00
|
|
|
|
|
|
|
const std::string THREE_FN("three-r111.min.js");
|
2021-01-21 21:26:39 +00:00
|
|
|
const std::string HAMMER_FN("hammer-2.0.8.js");
|
|
|
|
const std::string CONTROLS_FN("SolveSpaceControls.js");
|
2021-01-21 21:25:09 +00:00
|
|
|
|
2016-05-18 11:44:32 +00:00
|
|
|
const char htmlbegin[] = R"(
|
2016-05-06 17:24:18 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8"></meta>
|
|
|
|
<title>Three.js Solvespace Mesh</title>
|
2021-01-21 21:25:09 +00:00
|
|
|
<script id="%s">%s</script>
|
2021-01-21 21:26:39 +00:00
|
|
|
<script id="%s">%s</script>
|
|
|
|
<script id="%s">%s</script>
|
2016-05-06 17:24:18 +00:00
|
|
|
<style type="text/css">
|
|
|
|
body { margin: 0; overflow: hidden; }
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
)";
|
|
|
|
const char htmlend[] = R"(
|
2016-06-11 23:08:12 +00:00
|
|
|
document.body.appendChild(solvespace(solvespace_model_%s, {
|
|
|
|
scale: %g,
|
|
|
|
offset: new THREE.Vector3(%g, %g, %g),
|
|
|
|
projUp: new THREE.Vector3(%g, %g, %g),
|
|
|
|
projRight: new THREE.Vector3(%g, %g, %g)
|
|
|
|
}));
|
2016-05-06 17:24:18 +00:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
)";
|
2015-08-31 19:33:57 +00:00
|
|
|
|
|
|
|
// A default three.js viewer with OrthographicTrackballControls is
|
|
|
|
// generated as a comment preceding the data.
|
|
|
|
|
|
|
|
// x bounds should be the range of x or y, whichever
|
|
|
|
// is larger, before aspect ratio correction is applied.
|
|
|
|
// y bounds should be the range of x or y, whichever is
|
|
|
|
// larger. No aspect ratio correction is applied.
|
|
|
|
// Near plane should be 1.
|
|
|
|
// Camera's z-position should be the range of z + 1 or the larger of
|
|
|
|
// the x or y bounds, whichever is larger.
|
|
|
|
// Far plane should be at least twice as much as the camera's
|
|
|
|
// z-position.
|
|
|
|
// Edge projection bias should be about 1/500 of the far plane's distance.
|
|
|
|
// Further corrections will be applied to the z-position and far plane in
|
|
|
|
// the default viewer, but the defaults are fine for a model which
|
|
|
|
// only rotates about the world origin.
|
|
|
|
|
|
|
|
sm->GetBounding(&bndh, &bndl);
|
|
|
|
double largerBoundXY = max((bndh.x - bndl.x), (bndh.y - bndl.y));
|
|
|
|
double largerBoundZ = max(largerBoundXY, (bndh.z - bndl.z + 1));
|
|
|
|
|
2017-03-11 14:43:21 +00:00
|
|
|
std::string basename = filename.FileStem();
|
2017-03-08 19:25:45 +00:00
|
|
|
for(size_t i = 0; i < basename.length(); i++) {
|
|
|
|
if(!(isalnum(basename[i]) || ((unsigned)basename[i] >= 0x80))) {
|
|
|
|
basename[i] = '_';
|
|
|
|
}
|
2015-08-31 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 14:43:21 +00:00
|
|
|
if(filename.HasExtension("html")) {
|
2016-05-18 11:44:32 +00:00
|
|
|
fprintf(f, htmlbegin,
|
2021-01-21 21:25:09 +00:00
|
|
|
THREE_FN.c_str(),
|
|
|
|
LoadStringFromGzip("threejs/" + THREE_FN + ".gz").c_str(),
|
2021-01-21 21:26:39 +00:00
|
|
|
HAMMER_FN.c_str(),
|
|
|
|
LoadStringFromGzip("threejs/" + HAMMER_FN + ".gz").c_str(),
|
|
|
|
CONTROLS_FN.c_str(),
|
|
|
|
LoadString("threejs/" + CONTROLS_FN).c_str());
|
2016-05-06 17:24:18 +00:00
|
|
|
}
|
2016-01-11 10:53:04 +00:00
|
|
|
|
|
|
|
fprintf(f, "var solvespace_model_%s = {\n"
|
2015-08-31 19:33:57 +00:00
|
|
|
" bounds: {\n"
|
|
|
|
" x: %f, y: %f, near: %f, far: %f, z: %f, edgeBias: %f\n"
|
|
|
|
" },\n",
|
2017-03-08 19:25:45 +00:00
|
|
|
basename.c_str(),
|
2016-01-11 07:52:46 +00:00
|
|
|
largerBoundXY,
|
|
|
|
largerBoundXY,
|
|
|
|
1.0,
|
|
|
|
largerBoundZ * 2,
|
|
|
|
largerBoundZ,
|
|
|
|
largerBoundZ / 250);
|
2015-08-31 19:33:57 +00:00
|
|
|
|
|
|
|
// Output lighting information.
|
|
|
|
fputs(" lights: {\n"
|
|
|
|
" d: [\n", f);
|
|
|
|
|
|
|
|
// Directional.
|
|
|
|
int lightCount;
|
2017-03-08 19:25:45 +00:00
|
|
|
for(lightCount = 0; lightCount < 2; lightCount++) {
|
2015-08-31 19:33:57 +00:00
|
|
|
fprintf(f, " {\n"
|
|
|
|
" intensity: %f, direction: [%f, %f, %f]\n"
|
|
|
|
" },\n",
|
2016-01-11 07:52:46 +00:00
|
|
|
SS.lightIntensity[lightCount],
|
|
|
|
CO(SS.lightDir[lightCount]));
|
2015-08-31 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Global Ambience.
|
|
|
|
fprintf(f, " ],\n"
|
|
|
|
" a: %f\n", SS.ambientIntensity);
|
|
|
|
|
|
|
|
for(tr = sm->l.First(); tr; tr = sm->l.NextAfter(tr)) {
|
|
|
|
spl.IncrementTagFor(tr->a);
|
|
|
|
spl.IncrementTagFor(tr->b);
|
|
|
|
spl.IncrementTagFor(tr->c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output all the vertices.
|
|
|
|
SPoint *sp;
|
|
|
|
fputs(" },\n"
|
|
|
|
" points: [\n", f);
|
|
|
|
for(sp = spl.l.First(); sp; sp = spl.l.NextAfter(sp)) {
|
|
|
|
fprintf(f, " [%f, %f, %f],\n",
|
2016-01-11 07:52:46 +00:00
|
|
|
sp->p.x / SS.exportScale,
|
|
|
|
sp->p.y / SS.exportScale,
|
|
|
|
sp->p.z / SS.exportScale);
|
2015-08-31 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs(" ],\n"
|
|
|
|
" faces: [\n", f);
|
|
|
|
// And now all the triangular faces, in terms of those vertices.
|
|
|
|
// This time we count from zero.
|
|
|
|
for(tr = sm->l.First(); tr; tr = sm->l.NextAfter(tr)) {
|
|
|
|
fprintf(f, " [%d, %d, %d],\n",
|
2016-01-11 07:52:46 +00:00
|
|
|
spl.IndexForPoint(tr->a),
|
|
|
|
spl.IndexForPoint(tr->b),
|
|
|
|
spl.IndexForPoint(tr->c));
|
2015-08-31 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 07:52:46 +00:00
|
|
|
// Output face normals.
|
2015-08-31 19:33:57 +00:00
|
|
|
fputs(" ],\n"
|
2016-01-11 07:52:46 +00:00
|
|
|
" normals: [\n", f);
|
2015-08-31 19:33:57 +00:00
|
|
|
for(tr = sm->l.First(); tr; tr = sm->l.NextAfter(tr)) {
|
2016-01-11 07:52:46 +00:00
|
|
|
fprintf(f, " [[%f, %f, %f], [%f, %f, %f], [%f, %f, %f]],\n",
|
|
|
|
CO(tr->an), CO(tr->bn), CO(tr->cn));
|
2015-08-31 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs(" ],\n"
|
2016-01-11 07:52:46 +00:00
|
|
|
" colors: [\n", f);
|
|
|
|
// Output triangle colors.
|
2015-08-31 19:33:57 +00:00
|
|
|
for(tr = sm->l.First(); tr; tr = sm->l.NextAfter(tr)) {
|
2016-01-11 07:52:46 +00:00
|
|
|
fprintf(f, " 0x%x,\n", tr->meta.color.ToARGB32());
|
2015-08-31 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs(" ],\n"
|
|
|
|
" edges: [\n", f);
|
|
|
|
// Output edges. Assume user's model colors do not obscure white edges.
|
2016-06-26 06:39:27 +00:00
|
|
|
for(const SOutline &so : sol->l) {
|
|
|
|
if(so.tag == 0) continue;
|
2015-08-31 19:33:57 +00:00
|
|
|
fprintf(f, " [[%f, %f, %f], [%f, %f, %f]],\n",
|
2016-06-26 06:39:27 +00:00
|
|
|
so.a.x / SS.exportScale,
|
|
|
|
so.a.y / SS.exportScale,
|
|
|
|
so.a.z / SS.exportScale,
|
|
|
|
so.b.x / SS.exportScale,
|
|
|
|
so.b.y / SS.exportScale,
|
|
|
|
so.b.z / SS.exportScale);
|
2015-08-31 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs(" ]\n};\n", f);
|
2016-01-11 10:53:04 +00:00
|
|
|
|
2017-03-11 14:43:21 +00:00
|
|
|
if(filename.HasExtension("html")) {
|
2016-06-11 23:08:12 +00:00
|
|
|
fprintf(f, htmlend,
|
2017-03-08 19:25:45 +00:00
|
|
|
basename.c_str(),
|
2016-06-11 23:08:12 +00:00
|
|
|
SS.GW.scale,
|
|
|
|
CO(SS.GW.offset),
|
|
|
|
CO(SS.GW.projUp),
|
|
|
|
CO(SS.GW.projRight));
|
2017-03-08 19:25:45 +00:00
|
|
|
}
|
2016-01-11 10:53:04 +00:00
|
|
|
|
2015-08-31 19:33:57 +00:00
|
|
|
spl.Clear();
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:08:54 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Export the mesh as a VRML text file / WRL.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void SolveSpaceUI::ExportMeshAsVrmlTo(FILE *f, const Platform::Path &filename, SMesh *sm) {
|
2019-08-09 23:32:24 +00:00
|
|
|
struct STriangleSpan {
|
|
|
|
STriangle *first, *past_last;
|
|
|
|
|
|
|
|
STriangle *begin() const { return first; }
|
|
|
|
STriangle *end() const { return past_last; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-08-09 19:08:54 +00:00
|
|
|
std::string basename = filename.FileStem();
|
|
|
|
for(auto & c : basename) {
|
|
|
|
if(!(isalnum(c) || ((unsigned)c >= 0x80))) {
|
|
|
|
c = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "#VRML V2.0 utf8\n"
|
|
|
|
"#Exported from SolveSpace %s\n"
|
|
|
|
"\n"
|
|
|
|
"DEF %s Transform {\n"
|
2019-08-09 23:32:24 +00:00
|
|
|
" children [",
|
2019-08-09 19:08:54 +00:00
|
|
|
PACKAGE_VERSION,
|
2019-08-09 23:32:24 +00:00
|
|
|
basename.c_str());
|
2019-08-09 19:08:54 +00:00
|
|
|
|
|
|
|
|
2019-08-09 23:32:24 +00:00
|
|
|
std::map<std::uint8_t, std::vector<STriangleSpan>> opacities;
|
|
|
|
STriangle *start = sm->l.begin();
|
|
|
|
std::uint8_t last_opacity = start->meta.color.alpha;
|
|
|
|
for(auto & tr : sm->l) {
|
|
|
|
if(tr.meta.color.alpha != last_opacity) {
|
|
|
|
opacities[last_opacity].push_back(STriangleSpan{start, &tr});
|
|
|
|
start = &tr;
|
|
|
|
last_opacity = start->meta.color.alpha;
|
|
|
|
}
|
2019-08-09 19:08:54 +00:00
|
|
|
}
|
2019-08-09 23:32:24 +00:00
|
|
|
opacities[last_opacity].push_back(STriangleSpan{start, sm->l.end()});
|
|
|
|
|
|
|
|
for(auto && op : opacities) {
|
|
|
|
fprintf(f, "\n"
|
|
|
|
" Shape {\n"
|
|
|
|
" appearance Appearance {\n"
|
|
|
|
" material DEF %s_material_%u Material {\n"
|
|
|
|
" diffuseColor %f %f %f\n"
|
|
|
|
" ambientIntensity %f\n"
|
|
|
|
" transparency %f\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" geometry IndexedFaceSet {\n"
|
|
|
|
" colorPerVertex TRUE\n"
|
|
|
|
" coord Coordinate { point [\n",
|
|
|
|
basename.c_str(),
|
|
|
|
(unsigned)op.first,
|
|
|
|
SS.ambientIntensity,
|
|
|
|
SS.ambientIntensity,
|
|
|
|
SS.ambientIntensity,
|
|
|
|
SS.ambientIntensity,
|
|
|
|
1.f - ((float)op.first / 255.0f));
|
|
|
|
|
|
|
|
SPointList spl = {};
|
|
|
|
|
|
|
|
for(const auto & sp : op.second) {
|
|
|
|
for(const auto & tr : sp) {
|
|
|
|
spl.IncrementTagFor(tr.a);
|
|
|
|
spl.IncrementTagFor(tr.b);
|
|
|
|
spl.IncrementTagFor(tr.c);
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 19:08:54 +00:00
|
|
|
|
2019-08-09 23:32:24 +00:00
|
|
|
// Output all the vertices.
|
|
|
|
for(auto sp : spl.l) {
|
|
|
|
fprintf(f, " %f %f %f,\n",
|
|
|
|
sp.p.x / SS.exportScale,
|
|
|
|
sp.p.y / SS.exportScale,
|
|
|
|
sp.p.z / SS.exportScale);
|
|
|
|
}
|
2019-08-09 19:08:54 +00:00
|
|
|
|
2019-08-09 23:32:24 +00:00
|
|
|
fputs(" ] }\n"
|
|
|
|
" coordIndex [\n", f);
|
|
|
|
// And now all the triangular faces, in terms of those vertices.
|
|
|
|
for(const auto & sp : op.second) {
|
|
|
|
for(const auto & tr : sp) {
|
|
|
|
fprintf(f, " %d, %d, %d, -1,\n",
|
|
|
|
spl.IndexForPoint(tr.a),
|
|
|
|
spl.IndexForPoint(tr.b),
|
|
|
|
spl.IndexForPoint(tr.c));
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 19:08:54 +00:00
|
|
|
|
2019-08-09 23:32:24 +00:00
|
|
|
fputs(" ]\n"
|
|
|
|
" color Color { color [\n", f);
|
|
|
|
// Output triangle colors.
|
|
|
|
std::vector<int> triangle_colour_ids;
|
|
|
|
std::vector<RgbaColor> colours_present;
|
|
|
|
for(const auto & sp : op.second) {
|
|
|
|
for(const auto & tr : sp) {
|
|
|
|
const auto colour_itr = std::find_if(colours_present.begin(), colours_present.end(),
|
|
|
|
[&](const RgbaColor & c) {
|
|
|
|
return c.Equals(tr.meta.color);
|
|
|
|
});
|
|
|
|
if(colour_itr == colours_present.end()) {
|
|
|
|
fprintf(f, " %.10f %.10f %.10f,\n",
|
|
|
|
tr.meta.color.redF(),
|
|
|
|
tr.meta.color.greenF(),
|
|
|
|
tr.meta.color.blueF());
|
|
|
|
triangle_colour_ids.push_back(colours_present.size());
|
|
|
|
colours_present.insert(colours_present.end(), tr.meta.color);
|
|
|
|
} else {
|
|
|
|
triangle_colour_ids.push_back(colour_itr - colours_present.begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs(" ] }\n"
|
|
|
|
" colorIndex [\n", f);
|
|
|
|
|
|
|
|
for(auto colour_idx : triangle_colour_ids) {
|
|
|
|
fprintf(f, " %d, %d, %d, -1,\n", colour_idx, colour_idx, colour_idx);
|
2019-08-09 19:08:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 23:32:24 +00:00
|
|
|
fputs(" ]\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n", f);
|
2019-08-09 19:08:54 +00:00
|
|
|
|
2019-08-09 23:32:24 +00:00
|
|
|
spl.Clear();
|
2019-08-09 19:08:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 23:32:24 +00:00
|
|
|
fputs(" ]\n"
|
2019-08-09 19:08:54 +00:00
|
|
|
"}\n", f);
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:33:57 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2009-01-27 05:48:40 +00:00
|
|
|
// Export a view of the model as an image; we just take a screenshot, by
|
|
|
|
// rendering the view in the usual way and then copying the pixels.
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-03-11 14:43:21 +00:00
|
|
|
void SolveSpaceUI::ExportAsPngTo(const Platform::Path &filename) {
|
2017-01-23 00:24:18 +00:00
|
|
|
screenshotFile = filename;
|
|
|
|
// The rest of the work is done in the next redraw.
|
2018-07-12 19:29:44 +00:00
|
|
|
GW.Invalidate();
|
2008-07-08 07:45:47 +00:00
|
|
|
}
|