summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarius Kintel <marius@kintel.net>2013-04-12 04:13:09 (GMT)
committerMarius Kintel <marius@kintel.net>2013-04-12 04:13:09 (GMT)
commit60a351a5ee593aee68716dfa76e9ee4f4b767e9f (patch)
tree374e7104bec631f64187010ba8c2521ba95a3fb7 /src
parent6de88962556c5343f6b8db7fdfe4233c50ba2953 (diff)
parentf242a7a6934effdb292c0fedafa954bafc615cc3 (diff)
Merge branch 'noqt'
Diffstat (limited to 'src')
-rw-r--r--src/boost-utils.cc168
-rw-r--r--src/boost-utils.h13
-rw-r--r--src/boosty.h68
-rw-r--r--src/dxfdata.cc7
-rw-r--r--src/dxftess-glu.cc77
-rw-r--r--src/parsersettings.cc3
-rw-r--r--src/value.cc8
7 files changed, 301 insertions, 43 deletions
diff --git a/src/boost-utils.cc b/src/boost-utils.cc
new file mode 100644
index 0000000..534cbaa
--- /dev/null
+++ b/src/boost-utils.cc
@@ -0,0 +1,168 @@
+#include "boosty.h"
+#include "boost-utils.h"
+#include <stdio.h>
+#include <iostream>
+
+// If the given (absolute) path is relative to the relative_to path, return a new
+// relative path. Will normalize the given path first
+fs::path boostfs_relative_path(const fs::path &path, const fs::path &relative_to)
+{
+ // create absolute paths
+ fs::path p = boosty::absolute(boostfs_normalize(path));
+ fs::path r = boosty::absolute(relative_to);
+
+ // if root paths are different, return absolute path
+ if (p.root_path() != r.root_path())
+ return p;
+
+ // initialize relative path
+ fs::path result;
+
+ // find out where the two paths diverge
+ fs::path::const_iterator itr_path = p.begin();
+ fs::path::const_iterator itr_relative_to = r.begin();
+ while (*itr_path == *itr_relative_to && itr_path != p.end() && itr_relative_to != r.end()) {
+ ++itr_path;
+ ++itr_relative_to;
+ }
+
+ // add "../" for each remaining token in relative_to
+ if (itr_relative_to != r.end()) {
+ ++itr_relative_to;
+ while (itr_relative_to != r.end()) {
+ result /= "..";
+ ++itr_relative_to;
+ }
+ }
+
+ // add remaining path
+ while (itr_path != p.end()) {
+ result /= *itr_path;
+ ++itr_path;
+ }
+
+ return result;
+}
+
+// Will normalize the given path, i.e. remove any redundant ".." path elements.
+fs::path boostfs_normalize(const fs::path &path)
+{
+ fs::path absPath = boosty::absolute(path);
+ fs::path::iterator it = absPath.begin();
+ fs::path result = *it;
+ if (it!=absPath.end()) it++;
+
+ // Get canonical version of the existing part
+ for(;exists(result) && it != absPath.end(); ++it) {
+ result /= *it;
+ }
+ result = boosty::canonical(result.parent_path());
+ if (it!=absPath.begin()) it--;
+
+ // For the rest remove ".." and "." in a path with no symlinks
+ for (; it != absPath.end(); ++it) {
+ // Just move back on ../
+ if (*it == "..") {
+ result = result.parent_path();
+ }
+ // Ignore "."
+ else if (*it != ".") {
+ // Just cat other path entries
+ result /= *it;
+ }
+ }
+
+ return result;
+}
+
+/**
+ * https://svn.boost.org/trac/boost/ticket/1976#comment:2
+ *
+ * "The idea: uncomplete(/foo/new, /foo/bar) => ../new
+ * The use case for this is any time you get a full path (from an open dialog, perhaps)
+ * and want to store a relative path so that the group of files can be moved to a different
+ * directory without breaking the paths. An IDE would be a simple example, so that the
+ * project file could be safely checked out of subversion."
+ *
+ * ALGORITHM:
+ * iterate path and base
+ * compare all elements so far of path and base
+ * whilst they are the same, no write to output
+x2 * when they change, or one runs out:
+ * write to output, ../ times the number of remaining elements in base
+ * write to output, the remaining elements in path
+ */
+fs::path
+boostfs_uncomplete(fs::path const p, fs::path const base)
+{
+ if (p == base) return "./";
+ /*!! this breaks stuff if path is a filename rather than a directory,
+ which it most likely is... but then base shouldn't be a filename so... */
+
+ // create absolute paths
+ fs::path abs_p = boosty::absolute(boostfs_normalize(p));
+ fs::path abs_base = boosty::absolute(base);
+
+ fs::path from_path, from_base, output;
+
+ fs::path::iterator path_it = abs_p.begin(), path_end = abs_p.end();
+ fs::path::iterator base_it = abs_base.begin(), base_end = abs_base.end();
+
+ // check for emptiness
+ if ((path_it == path_end) || (base_it == base_end)) {
+ throw std::runtime_error("path or base was empty; couldn't generate relative path");
+ }
+
+#ifdef WIN32
+ // drive letters are different; don't generate a relative path
+ if (*path_it != *base_it) return p;
+
+ // now advance past drive letters; relative paths should only go up
+ // to the root of the drive and not past it
+ ++path_it, ++base_it;
+#endif
+
+ // Cache system-dependent dot, double-dot and slash strings
+ const std::string _dot = ".";
+ const std::string _dots = "..";
+ const std::string _sep = "/";
+
+ // iterate over path and base
+ while (true) {
+
+ // compare all elements so far of path and base to find greatest common root;
+ // when elements of path and base differ, or run out:
+ if ((path_it == path_end) || (base_it == base_end) || (*path_it != *base_it)) {
+
+ // write to output, ../ times the number of remaining elements in base;
+ // this is how far we've had to come down the tree from base to get to the common root
+ for (; base_it != base_end; ++base_it) {
+ if (*base_it == _dot)
+ continue;
+ else if (*base_it == _sep)
+ continue;
+
+ output /= "../";
+ }
+
+ // write to output, the remaining elements in path;
+ // this is the path relative from the common root
+ fs::path::iterator path_it_start = path_it;
+ for (; path_it != path_end; ++path_it) {
+ if (path_it != path_it_start) output /= "/";
+ if (*path_it == _dot) continue;
+ if (*path_it == _sep) continue;
+ output /= *path_it;
+ }
+ break;
+ }
+
+ // add directory level to both paths and continue iteration
+ from_path /= fs::path(*path_it);
+ from_base /= fs::path(*base_it);
+
+ ++path_it, ++base_it;
+ }
+
+ return output;
+}
diff --git a/src/boost-utils.h b/src/boost-utils.h
new file mode 100644
index 0000000..3678ecc
--- /dev/null
+++ b/src/boost-utils.h
@@ -0,0 +1,13 @@
+#ifndef BOOST_UTILS_H_
+#define BOOST_UTILS_H_
+
+#include <boost/filesystem.hpp>
+namespace fs = boost::filesystem;
+
+// FIXME: boostfs_relative_path() has been replaced by
+// boostfs_uncomplete(), but kept around for now.
+fs::path boostfs_relative_path(const fs::path &path, const fs::path &relative_to);
+fs::path boostfs_normalize(const fs::path &path);
+fs::path boostfs_uncomplete(fs::path const p, fs::path const base);
+
+#endif
diff --git a/src/boosty.h b/src/boosty.h
index 6ec417a..82c765b 100644
--- a/src/boosty.h
+++ b/src/boosty.h
@@ -10,9 +10,8 @@
versions of boost found on popular versions of linux, circa early 2012.
design
- hope that the user is compiling with boost>1.46 + filesystem v3
- if not, fall back to older deprecated functions, and rely on
- testing to find bugs. implement the minimum needed by OpenSCAD and no more.
+ the boost filsystem changed around 1.46-1.48. we do a large #ifdef
+ based on boost version that wraps various functions appropriately.
in a few years, this file should be deleted as unnecessary.
see also
@@ -27,7 +26,9 @@
#include <string>
#include <boost/version.hpp>
#include <boost/filesystem.hpp>
+#include <boost/algorithm/string.hpp>
namespace fs = boost::filesystem;
+#include "printutils.h"
namespace boosty {
@@ -77,6 +78,67 @@ inline std::string extension_str( fs::path p)
#endif
+
+
+
+
+#if BOOST_VERSION >= 104800
+
+inline fs::path canonical( fs::path p, fs::path p2 )
+{
+ return fs::canonical( p, p2 );
+}
+
+inline fs::path canonical( fs::path p )
+{
+ return fs::canonical( p );
+}
+
+#else
+
+inline fs::path canonical( fs::path p, fs::path p2 )
+{
+#if defined (__WIN32__) || defined(__APPLE__)
+#error you should be using a newer version of boost on win/mac
+#endif
+ // based on the code in boost
+ fs::path result;
+ if (p=="") p=p2;
+ std::string result_s;
+ std::vector<std::string> resultv, pieces;
+ std::vector<std::string>::iterator pi;
+ std::string tmps = boosty::stringy( p );
+ boost::split( pieces, tmps, boost::is_any_of("/") );
+ for ( pi = pieces.begin(); pi != pieces.end(); ++pi )
+ {
+ if (*pi == "..")
+ resultv.erase( resultv.end() );
+ else
+ resultv.push_back( *pi );
+ }
+ for ( pi = resultv.begin(); pi != resultv.end(); ++pi )
+ {
+ if ((*pi).length()>0) result_s = result_s + "/" + *pi;
+ }
+ result = fs::path( result_s );
+ if (fs::is_symlink(result))
+ {
+ PRINT("WARNING: canonical() wrapper can't do symlinks. rebuild openscad with boost >=1.48");
+ PRINT("WARNING: or don't use symbolic links");
+ }
+ return result;
+}
+
+inline fs::path canonical( fs::path p )
+{
+ return canonical( p, fs::current_path() );
+}
+
+#endif
+
+
+
+
} // namespace
#endif
diff --git a/src/dxfdata.cc b/src/dxfdata.cc
index f34af51..8415228 100644
--- a/src/dxfdata.cc
+++ b/src/dxfdata.cc
@@ -41,8 +41,9 @@
#include <sstream>
#include <map>
-#include <QDir>
#include "value.h"
+#include "boost-utils.h"
+#include "boosty.h"
/*! \class DxfData
@@ -389,10 +390,10 @@ DxfData::DxfData(double fn, double fs, double fa,
BOOST_FOREACH(const EntityList::value_type &i, unsupported_entities_list) {
if (layername.empty()) {
PRINTB("WARNING: Unsupported DXF Entity '%s' (%x) in %s.",
- i.first % i.second % QuotedString(QDir::current().relativeFilePath(QString::fromLocal8Bit(filename.c_str())).toLocal8Bit().constData()));
+ i.first % i.second % QuotedString(boosty::stringy(boostfs_uncomplete(filename, fs::current_path()))));
} else {
PRINTB("WARNING: Unsupported DXF Entity '%s' (%x) in layer '%s' of %s.",
- i.first % i.second % layername % QuotedString(QDir::current().relativeFilePath(QString::fromLocal8Bit(filename.c_str())).toLocal8Bit().constData()));
+ i.first % i.second % layername % QuotedString(boosty::stringy(boostfs_uncomplete(filename, fs::current_path()))));
}
}
diff --git a/src/dxftess-glu.cc b/src/dxftess-glu.cc
index 1a8bfa5..3f87729 100644
--- a/src/dxftess-glu.cc
+++ b/src/dxftess-glu.cc
@@ -3,14 +3,11 @@
#include "polyset.h"
#include "grid.h"
#include <stdio.h>
+#include <boost/foreach.hpp>
#include "system-gl.h"
#include "mathc99.h"
-#include <QVector>
-#include <QPair>
-#include <QHash>
-
#ifdef WIN32
# define STDCALL __stdcall
#else
@@ -31,7 +28,7 @@ struct tess_triangle {
static GLenum tess_type;
static int tess_count;
-static QVector<tess_triangle> tess_tri;
+static std::vector<tess_triangle> tess_tri;
static GLdouble *tess_p1, *tess_p2;
static void STDCALL tess_vertex(void *vertex_data)
@@ -48,7 +45,7 @@ static void STDCALL tess_vertex(void *vertex_data)
tess_p2 = p;
}
if (tess_count > 1) {
- tess_tri.append(tess_triangle(tess_p1, tess_p2, p));
+ tess_tri.push_back(tess_triangle(tess_p1, tess_p2, p));
tess_p2 = p;
}
}
@@ -61,9 +58,9 @@ static void STDCALL tess_vertex(void *vertex_data)
}
if (tess_count > 1) {
if (tess_count % 2 == 1) {
- tess_tri.append(tess_triangle(tess_p2, tess_p1, p));
+ tess_tri.push_back(tess_triangle(tess_p2, tess_p1, p));
} else {
- tess_tri.append(tess_triangle(tess_p1, tess_p2, p));
+ tess_tri.push_back(tess_triangle(tess_p1, tess_p2, p));
}
tess_p1 = tess_p2;
tess_p2 = p;
@@ -77,7 +74,7 @@ static void STDCALL tess_vertex(void *vertex_data)
tess_p2 = p;
}
if (tess_count == 2) {
- tess_tri.append(tess_triangle(tess_p1, tess_p2, p));
+ tess_tri.push_back(tess_triangle(tess_p1, tess_p2, p));
tess_count = -1;
}
}
@@ -188,6 +185,17 @@ static bool point_on_line(double *p1, double *p2, double *p3)
return true;
}
+typedef std::pair<int,int> pair_ii;
+inline void do_emplace( boost::unordered_multimap<int, pair_ii> &tri_by_atan2, int ai, const pair_ii &indexes)
+{
+#if BOOST_VERSION >= 104800
+ tri_by_atan2.emplace(ai, indexes);
+#else
+ std::pair< int, pair_ii > tmp( ai, indexes );
+ tri_by_atan2.insert( tmp );
+#endif
+}
+
/*!
up: true if the polygon is facing in the normal direction (i.e. normal = [0,0,1])
rot: CLOCKWISE rotation around positive Z axis
@@ -214,7 +222,7 @@ void dxf_tesselate(PolySet *ps, DxfData &dxf, double rot, bool up, bool do_trian
tess_tri.clear();
- QList<tess_vdata> vl;
+ std::list<tess_vdata> vl;
gluTessBeginPolygon(tobj, NULL);
@@ -225,7 +233,7 @@ void dxf_tesselate(PolySet *ps, DxfData &dxf, double rot, bool up, bool do_trian
gluTessNormal(tobj, 0, 0, +1);
}
- Grid3d< QPair<int,int> > point_to_path(GRID_COARSE);
+ Grid3d< std::pair<int,int> > point_to_path(GRID_COARSE);
for (int i = 0; i < dxf.paths.size(); i++) {
if (!dxf.paths[i].is_closed)
@@ -234,12 +242,12 @@ void dxf_tesselate(PolySet *ps, DxfData &dxf, double rot, bool up, bool do_trian
for (int j = 1; j < dxf.paths[i].indices.size(); j++) {
point_to_path.data(dxf.points[dxf.paths[i].indices[j]][0],
dxf.points[dxf.paths[i].indices[j]][1],
- h) = QPair<int,int>(i, j);
- vl.append(tess_vdata());
- vl.last().v[0] = dxf.points[dxf.paths[i].indices[j]][0];
- vl.last().v[1] = dxf.points[dxf.paths[i].indices[j]][1];
- vl.last().v[2] = h;
- gluTessVertex(tobj, vl.last().v, vl.last().v);
+ h) = std::pair<int,int>(i, j);
+ vl.push_back(tess_vdata());
+ vl.back().v[0] = dxf.points[dxf.paths[i].indices[j]][0];
+ vl.back().v[1] = dxf.points[dxf.paths[i].indices[j]][1];
+ vl.back().v[2] = h;
+ gluTessVertex(tobj, vl.back().v, vl.back().v);
}
gluTessEndContour(tobj);
}
@@ -263,7 +271,7 @@ void dxf_tesselate(PolySet *ps, DxfData &dxf, double rot, bool up, bool do_trian
point_on_line(tess_tri[i].p[1], tess_tri[i].p[2], tess_tri[i].p[0]) ||
point_on_line(tess_tri[i].p[2], tess_tri[i].p[0], tess_tri[i].p[1])) {
// printf("DEBUG: Removed triangle\n");
- tess_tri.remove(i--);
+ tess_tri.erase(tess_tri.begin() + i--);
}
}
@@ -277,13 +285,13 @@ void dxf_tesselate(PolySet *ps, DxfData &dxf, double rot, bool up, bool do_trian
if (do_triangle_splitting)
{
bool added_triangles = true;
- typedef QPair<int,int> QPair_ii;
- QHash<int, QPair_ii> tri_by_atan2;
+ typedef std::pair<int,int> pair_ii;
+ boost::unordered_multimap<int, pair_ii> tri_by_atan2;
for (int i = 0; i < tess_tri.size(); i++)
for (int j = 0; j < 3; j++) {
int ai = (int)round(atan2(fabs(tess_tri[i].p[(j+1)%3][0] - tess_tri[i].p[j][0]),
fabs(tess_tri[i].p[(j+1)%3][1] - tess_tri[i].p[j][1])) / 0.001);
- tri_by_atan2.insertMulti(ai, QPair<int,int>(i, j));
+ do_emplace( tri_by_atan2, ai, std::pair<int,int>(i, j) );
}
while (added_triangles)
{
@@ -294,20 +302,23 @@ void dxf_tesselate(PolySet *ps, DxfData &dxf, double rot, bool up, bool do_trian
for (int i = 0; i < tess_tri.size(); i++)
for (int k = 0; k < 3; k++)
{
- QHash<QPair_ii, QPair_ii> possible_neigh;
+ boost::unordered_map<pair_ii, pair_ii> possible_neigh;
int ai = (int)floor(atan2(fabs(tess_tri[i].p[(k+1)%3][0] - tess_tri[i].p[k][0]),
fabs(tess_tri[i].p[(k+1)%3][1] - tess_tri[i].p[k][1])) / 0.001 - 0.5);
for (int j = 0; j < 2; j++) {
- foreach (const QPair_ii &jl, tri_by_atan2.values(ai+j))
- if (i != jl.first)
- possible_neigh[jl] = jl;
+ for (boost::unordered_multimap<int, pair_ii>::iterator it = tri_by_atan2.find(ai+j);
+ it != tri_by_atan2.end();
+ it++) {
+ if (i != it->first) possible_neigh[it->second] = it->second;
+ }
}
#ifdef DEBUG_TRIANGLE_SPLITTING
printf("%d/%d: %d\n", i, k, possible_neigh.size());
#endif
- foreach (const QPair_ii &jl, possible_neigh) {
- int j = jl.first;
- for (int l = jl.second; l != (jl.second + 2) % 3; l = (l + 1) % 3)
+ typedef std::pair<pair_ii,pair_ii> ElemPair;
+ BOOST_FOREACH (const ElemPair &elem, possible_neigh) {
+ int j = elem.first.first;
+ for (int l = elem.first.second; l != (elem.first.second + 2) % 3; l = (l + 1) % 3)
if (point_on_line(tess_tri[i].p[k], tess_tri[j].p[l], tess_tri[i].p[(k+1)%3])) {
#ifdef DEBUG_TRIANGLE_SPLITTING
printf("%% %f %f %f %f %f %f [%d %d]\n",
@@ -316,18 +327,18 @@ void dxf_tesselate(PolySet *ps, DxfData &dxf, double rot, bool up, bool do_trian
tess_tri[i].p[(k+1)%3][0], tess_tri[i].p[(k+1)%3][1],
i, j);
#endif
- tess_tri.append(tess_triangle(tess_tri[j].p[l],
+ tess_tri.push_back(tess_triangle(tess_tri[j].p[l],
tess_tri[i].p[(k+1)%3], tess_tri[i].p[(k+2)%3]));
for (int m = 0; m < 2; m++) {
- int ai = (int)round(atan2(fabs(tess_tri.last().p[(m+1)%3][0] - tess_tri.last().p[m][0]),
- fabs(tess_tri.last().p[(m+1)%3][1] - tess_tri.last().p[m][1])) / 0.001 );
- tri_by_atan2.insertMulti(ai, QPair<int,int>(tess_tri.size()-1, m));
+ int ai = (int)round(atan2(fabs(tess_tri.back().p[(m+1)%3][0] - tess_tri.back().p[m][0]),
+ fabs(tess_tri.back().p[(m+1)%3][1] - tess_tri.back().p[m][1])) / 0.001 );
+ do_emplace(tri_by_atan2, ai, std::pair<int,int>(tess_tri.size()-1, m));
}
tess_tri[i].p[(k+1)%3] = tess_tri[j].p[l];
for (int m = 0; m < 2; m++) {
int ai = (int)round(atan2(fabs(tess_tri[i].p[(m+1)%3][0] - tess_tri[i].p[m][0]),
fabs(tess_tri[i].p[(m+1)%3][1] - tess_tri[i].p[m][1])) / 0.001 );
- tri_by_atan2.insertMulti(ai, QPair<int,int>(i, m));
+ do_emplace(tri_by_atan2, ai, std::pair<int,int>(i, m));
}
added_triangles = true;
}
diff --git a/src/parsersettings.cc b/src/parsersettings.cc
index a757ba1..8d82744 100644
--- a/src/parsersettings.cc
+++ b/src/parsersettings.cc
@@ -3,8 +3,9 @@
#include <boost/foreach.hpp>
#include "boosty.h"
#include <boost/algorithm/string.hpp>
-#include <qglobal.h> // Needed for Q_ defines - move the offending code somewhere else
+#ifdef __APPLE__
#include "CocoaUtils.h"
+#endif
namespace fs = boost::filesystem;
diff --git a/src/value.cc b/src/value.cc
index f14f826..ebb825d 100644
--- a/src/value.cc
+++ b/src/value.cc
@@ -34,12 +34,14 @@
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/format.hpp>
-
-#include <QtCore/QDir>
+#include "boost-utils.h"
+#include "boosty.h"
std::ostream &operator<<(std::ostream &stream, const Filename &filename)
{
- stream << QuotedString(QDir::current().relativeFilePath(QString::fromLocal8Bit(filename.c_str())).toLocal8Bit().constData());
+ fs::path fnpath = fs::path( (std::string)filename );
+ fs::path fpath = boostfs_uncomplete(fnpath, fs::current_path());
+ stream << QuotedString(boosty::stringy( fpath ));
return stream;
}
contact: Jan Huwald // Impressum