Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dfx splines #370

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/dxf/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace DXF {
virtual ~Entity() {}

virtual type_t getType() const = 0;
virtual void addVertex(const cb::Vector3D &v) {THROW("Cannot add vertex");}
virtual void addVertex(const cb::Vector3D &v, double weight = 0.0) {THROW("Cannot add vertex");}
virtual void addKnot(double k) {THROW("Cannot add knot");}
};
}
22 changes: 19 additions & 3 deletions src/dxf/PolyLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,31 @@

namespace DXF {
class PolyLine : public Entity {
unsigned flags; ///< See flag_t
std::vector<cb::Vector3D> vertices;

public:
PolyLine() {}

typedef enum {
POLYLINE_FLAG_CLOSED = 1 << 0, // Closed polyline or M closed mesh
POLYLINE_FLAG_CURVE_FIT = 1 << 1, // Curve-fit vertices added
POLYLINE_FLAG_SPLINE_FIT = 1 << 2, // Spline-fit vertices added
POLYLINE_FLAG_3D_LINE = 1 << 3, // 3D polyline
POLYLINE_FLAG_3D_MESH = 1 << 4, // 3D polygon mesh
POLYLINE_FLAG_N_CLOSED_MESH = 1 << 5, // Mesh closed in N direction
POLYLINE_FLAG_FACE_MESH = 1 << 6, // Polyline is a polyface mesh
POLYLINE_FLAG_CONTINUOUS = 1 << 7, // Continuous linetype pattern
} flag_t;


PolyLine(unsigned flags) : flags(flags) {}

unsigned getFlags() const {return flags;}
bool isClosed() const {return flags & POLYLINE_FLAG_CLOSED;}
const std::vector<cb::Vector3D> &getVertices() const {return vertices;}

// From Entity
void addVertex(const cb::Vector3D &v) {vertices.push_back(v);}
void addVertex(const cb::Vector3D &v, double weight = 0)
{vertices.push_back(v);}
type_t getType() const {return DXF_POLYLINE;}
};
}
6 changes: 3 additions & 3 deletions src/dxf/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void Reader::addCircle(const DL_CircleData &circle) {

void Reader::addPolyline(const DL_PolylineData &polyline) {
if (!entity.isNull()) THROW("DXF Already in DXF entity");
addEntity(entity = new PolyLine);
addEntity(entity = new PolyLine(polyline.flags));
}


Expand All @@ -97,12 +97,12 @@ void Reader::addVertex(const DL_VertexData &vertex) {

void Reader::addSpline(const DL_SplineData &spline) {
if (!entity.isNull()) THROW("DXF Already in DXF entity");
addEntity(entity = new Spline(spline.degree));
addEntity(entity = new Spline(spline.degree, spline.flags));
}


void Reader::addControlPoint(const DL_ControlPointData &ctrlPt) {
entity->addVertex(cb::Vector3D(ctrlPt.x, ctrlPt.y, ctrlPt.z));
entity->addVertex(cb::Vector3D(ctrlPt.x, ctrlPt.y, ctrlPt.z), ctrlPt.w);
}


Expand Down
22 changes: 20 additions & 2 deletions src/dxf/Spline.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,37 @@
namespace DXF {
class Spline : public Entity {
unsigned degree;
unsigned flags; ///< See flag_t
std::vector<cb::Vector3D> ctrlPts;
std::vector<double> weights;
std::vector<double> knots;

public:
Spline(unsigned degree) : degree(degree) {}
typedef enum {
SPLINE_FLAG_CLOSED = 1 << 0, // Closed spline
SPLINE_FLAG_PERIODIC = 1 << 1, // Periodic spline
SPLINE_FLAG_RATIONAL = 1 << 2, // Rational spline
SPLINE_FLAG_PLANAR = 1 << 3, // Planar
SPLINE_FLAG_LINEAR = 1 << 4, // Linear (planar bit is also set)
} flag_t;

Spline(unsigned degree, unsigned flags) : degree(degree), flags(flags) {}

unsigned getDegree() const {return degree;}
unsigned getFlags() const {return flags;}
bool isClosed() const {return flags & SPLINE_FLAG_CLOSED;}
bool isPeriodic() const {return flags & SPLINE_FLAG_PERIODIC;}
bool isRational() const {return flags & SPLINE_FLAG_RATIONAL;}
bool isPlanar() const {return flags & SPLINE_FLAG_PLANAR;}
bool isLinear() const {return flags & SPLINE_FLAG_LINEAR;}
const std::vector<cb::Vector3D> &getControlPoints() const {return ctrlPts;}
const std::vector<double> &getWeights() const {return weights;}
const std::vector<double> &getKnots() const {return knots;}

// From Entity
void addKnot(double k) {knots.push_back(k);}
void addVertex(const cb::Vector3D &v) {ctrlPts.push_back(v);}
void addVertex(const cb::Vector3D &v, double weight = 1)
{ctrlPts.push_back(v); weights.push_back(weight);}
type_t getType() const {return DXF_SPLINE;}
};
}
9 changes: 9 additions & 0 deletions src/tplang/DXFModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ void DXFModule::openCB(const js::Value &args, js::Sink &sink) {
case DXF::Entity::DXF_POLYLINE: {
const DXF::PolyLine &polyLine =
dynamic_cast<const DXF::PolyLine &>(entity);
// TODO expose other flags
sink.insertBoolean("isClosed", polyLine.isClosed());
const vector<Vector3D> &vertices = polyLine.getVertices();
sink.insertList("vertices");

Expand All @@ -130,16 +132,23 @@ void DXFModule::openCB(const js::Value &args, js::Sink &sink) {
const DXF::Spline &spline = dynamic_cast<const DXF::Spline &>(entity);

sink.insert("degree", spline.getDegree());
sink.insertBoolean("isClosed", spline.isClosed());
sink.insertBoolean("isPeriodic", spline.isPeriodic());
sink.insertBoolean("isRational", spline.isRational());
sink.insertBoolean("isPlanar", spline.isPlanar());
sink.insertBoolean("isLinear", spline.isLinear());

// Control points
const vector<Vector3D> &ctrlPts = spline.getControlPoints();
const std::vector<double> &weights = spline.getWeights();
sink.insertList("ctrlPts");

for (unsigned k = 0; k < ctrlPts.size(); k++) {
sink.appendDict();
sink.insert("x", ctrlPts[k].x());
sink.insert("y", ctrlPts[k].y());
sink.insert("z", ctrlPts[k].z());
sink.insert("w", weights[k]);
sink.insert("type", DXF::Entity::DXF_POINT);
sink.endDict();
}
Expand Down
Loading