Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.

Sun, 04 Oct 2015 06:31:36 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 04 Oct 2015 06:31:36 +0300
changeset 1002
a3ee23db8393
parent 1001
024cbc902e75
child 1003
31873c3cbdbc

Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.

src/basics.cpp file | annotate | diff | comparison | revisions
src/basics.h file | annotate | diff | comparison | revisions
src/editmodes/curvemode.cpp file | annotate | diff | comparison | revisions
src/ldObject.cpp file | annotate | diff | comparison | revisions
src/ldObject.h file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/basics.cpp	Sun Oct 04 04:27:38 2015 +0300
+++ b/src/basics.cpp	Sun Oct 04 06:31:36 2015 +0300
@@ -93,6 +93,34 @@
 	return format ("%1 %2 %3", x(), y(), z());
 }
 
+Vertex Vertex::operator* (qreal scalar) const
+{
+	return Vertex (x() * scalar, y() * scalar, z() * scalar);
+}
+
+Vertex& Vertex::operator+= (const Vertex& other)
+{
+	setX (x() + other.x());
+	setY (y() + other.y());
+	setZ (z() + other.z());
+	return *this;
+}
+
+Vertex Vertex::operator+ (const Vertex& other) const
+{
+	Vertex result (*this);
+	result += other;
+	return result;
+}
+
+Vertex& Vertex::operator*= (qreal scalar)
+{
+	setX (x() * scalar);
+	setY (y() * scalar);
+	setZ (z() * scalar);
+	return *this;
+}
+
 bool Vertex::operator< (const Vertex& other) const
 {
 	if (x() != other.x()) return x() < other.x();
--- a/src/basics.h	Sun Oct 04 04:27:38 2015 +0300
+++ b/src/basics.h	Sun Oct 04 06:31:36 2015 +0300
@@ -70,10 +70,19 @@
 	void	transform (const Matrix& matr, const Vertex& pos);
 	void	setCoordinate (Axis ax, qreal value);
 
+	Vertex&	operator+= (const Vertex& other);
+	Vertex	operator+ (const Vertex& other) const;
+	Vertex&	operator*= (qreal scalar);
+	Vertex	operator* (qreal scalar) const;
 	bool	operator< (const Vertex& other) const;
 	double	operator[] (Axis ax) const;
 };
 
+inline Vertex operator* (qreal scalar, const Vertex& vertex)
+{
+	return vertex * scalar;
+}
+
 Q_DECLARE_METATYPE (Vertex)
 
 //
--- a/src/editmodes/curvemode.cpp	Sun Oct 04 04:27:38 2015 +0300
+++ b/src/editmodes/curvemode.cpp	Sun Oct 04 06:31:36 2015 +0300
@@ -37,12 +37,12 @@
 		if (m_drawedVerts.size() < 4)
 			curve[m_drawedVerts.size()] = getCursorVertex();
 
-		// Default the control points to their vertex positions
+		// Default the control points to the first vertex position
 		if (m_drawedVerts.size() < 2)
 			curve[2] = curve[0];
 
 		if (m_drawedVerts.size() < 3)
-			curve[3] = curve[1];
+			curve[3] = curve[2];
 
 		for (int i = 0; i < countof(curve); ++i)
 			curve2d[i] = renderer()->convert3dTo2d (curve[i]);
--- a/src/ldObject.cpp	Sun Oct 04 04:27:38 2015 +0300
+++ b/src/ldObject.cpp	Sun Oct 04 06:31:36 2015 +0300
@@ -224,6 +224,20 @@
 	}
 }
 
+void LDObject::replace (const LDObjectList& others)
+{
+	int idx = lineNumber();
+
+	if (idx != -1 and not others.isEmpty())
+	{
+		for (int i = 1; i < others.size(); ++i)
+			document()->insertObj (idx + i, others[i]);
+
+		document()->setObject (idx, others[0]);
+		destroy();
+	}
+}
+
 // =============================================================================
 //
 // Swap this object with another.
@@ -1056,6 +1070,38 @@
 	m_fileName = value;
 }
 
+Vertex LDBezierCurve::pointAt (qreal t) const
+{
+	if (t >= 0.0 and t <= 1.0)
+	{
+		Vertex result;
+		result += pow (1.0 - t, 3) * vertex (0);
+		result += (3 * pow (1.0 - t, 2) * t) * vertex (2);
+		result += (3 * (1.0 - t) * pow (t, 2)) * vertex (3);
+		result += pow (t, 3) * vertex (1);
+		return result;
+	}
+	else
+		return Vertex();
+}
+
+LDObjectList LDBezierCurve::rasterize (int segments)
+{
+	LDObjectList result;
+	QVector<Vertex> parms;
+	parms.append (pointAt (0.0));
+
+	for (int i = 1; i < segments; ++i)
+		parms.append (pointAt (double (i) / segments));
+
+	parms.append (pointAt (1.0));
+
+	for (int i = 0; i < segments; ++i)
+		result << new LDLine (parms[i], parms[i + 1]);
+
+	return result;
+}
+
 // =============================================================================
 //
 // Selects this object.
--- a/src/ldObject.h	Sun Oct 04 04:27:38 2015 +0300
+++ b/src/ldObject.h	Sun Oct 04 06:31:36 2015 +0300
@@ -118,6 +118,7 @@
 	bool previousIsInvertnext (LDBfc*& ptr);
 	QColor randomColor() const;
 	void replace (LDObject* other);
+	void replace (const LDObjectList& others);
 	void select();
 	void setColor (LDColor color);
 	void setDocument (LDDocument* document);
@@ -461,7 +462,8 @@
 public:
 	LDBezierCurve (const Vertex& v0, const Vertex& v1,
 		const Vertex& v2, const Vertex& v3, LDDocument* document = nullptr);
-	LDLine* toEdgeLine();
+	Vertex pointAt (qreal t) const;
+	LDObjectList rasterize (int segments);
 };
 
 // Other common LDraw stuff
--- a/src/toolsets/algorithmtoolset.cpp	Sun Oct 04 04:27:38 2015 +0300
+++ b/src/toolsets/algorithmtoolset.cpp	Sun Oct 04 06:31:36 2015 +0300
@@ -283,7 +283,13 @@
 		++num;
 	}
 
-	print (tr ("Converted %1 conditional lines"), num);
+	for (LDObjectIterator<LDBezierCurve> it (selectedObjects()); it.isValid(); ++it)
+	{
+		it->replace (it->rasterize (10));
+		++num;
+	}
+
+	print (tr ("Converted %1 conditional lines and Bézier curves"), num);
 }
 
 bool AlgorithmToolset::isColorUsed (LDColor color)

mercurial