Made the quad→triangles use emplacement. However, now it crashes because of problems in the underlying system (the LDObject constructor shouldn't do anything in regard to the model!)

Sun, 29 Jan 2017 15:18:40 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 29 Jan 2017 15:18:40 +0200
changeset 1074
a62f810ca26f
parent 1073
a0a0d581309b
child 1075
711c5fff384d

Made the quad→triangles use emplacement. However, now it crashes because of problems in the underlying system (the LDObject constructor shouldn't do anything in regard to the model!)

src/ldDocument.cpp file | annotate | diff | comparison | revisions
src/ldObject.cpp file | annotate | diff | comparison | revisions
src/ldObject.h file | annotate | diff | comparison | revisions
src/model.h file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/basictoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/ldDocument.cpp	Sun Jan 29 15:05:14 2017 +0200
+++ b/src/ldDocument.cpp	Sun Jan 29 15:18:40 2017 +0200
@@ -578,9 +578,9 @@
 //
 void LDDocument::insertObject (int pos, LDObject* obj)
 {
-	history()->add (new AddHistoryEntry (pos, obj));
 	Model::insertObject(pos, obj);
-	m_window->renderer()->compileObject (obj);
+	history()->add(new AddHistoryEntry {pos, obj});
+	m_window->renderer()->compileObject(obj);
 	connect(obj, SIGNAL(codeChanged(int,QString,QString)), this, SLOT(objectChanged(int,QString,QString)));
 
 #ifdef DEBUG
@@ -729,18 +729,18 @@
 	if (m_manager->preInline(this, model, deep, renderinline))
 		return; // Manager dealt with this inline
 
-	for (LDObject* obj : objects())
+	for (LDObject* object : objects())
 	{
 		// Skip those without scemantic meaning
-		if (not obj->isScemantic())
+		if (not object->isScemantic())
 			continue;
 
 		// Got another sub-file reference, inline it if we're deep-inlining. If not,
 		// just add it into the objects normally. Yay, recursion!
-		if (deep and obj->type() == OBJ_SubfileReference)
-			static_cast<LDSubfileReference*>(obj)->inlineContents(model, deep, renderinline);
+		if (deep and object->type() == OBJ_SubfileReference)
+			static_cast<LDSubfileReference*>(object)->inlineContents(model, deep, renderinline);
 		else
-			model.addObject(obj->createCopy());
+			model.addObject(object->createCopy());
 	}
 }
 
--- a/src/ldObject.cpp	Sun Jan 29 15:05:14 2017 +0200
+++ b/src/ldObject.cpp	Sun Jan 29 15:18:40 2017 +0200
@@ -191,26 +191,6 @@
 
 // =============================================================================
 //
-QList<LDTriangle*> LDQuad::splitToTriangles()
-{
-	// Create the two triangles based on this quadrilateral:
-	// 0───3       0───3    3
-	// │   │  --→  │  ╱    ╱│
-	// │   │  --→  │ ╱    ╱ │
-	// │   │  --→  │╱    ╱  │
-	// 1───2       1    1───2
-	LDTriangle* tri1 (new LDTriangle (vertex (0), vertex (1), vertex (3)));
-	LDTriangle* tri2 (new LDTriangle (vertex (1), vertex (2), vertex (3)));
-
-	// The triangles also inherit the quad's color
-	tri1->setColor (color());
-	tri2->setColor (color());
-
-	return {tri1, tri2};
-}
-
-// =============================================================================
-//
 // Replace this LDObject with another LDObject. Object is deleted in the process.
 //
 void LDObject::replace (LDObject* other)
@@ -278,7 +258,7 @@
 
 // =============================================================================
 //
-LDTriangle::LDTriangle (const Vertex& v1, const Vertex& v2, const Vertex& v3, LDDocument* document) :
+LDTriangle::LDTriangle (const Vertex& v1, const Vertex& v2, const Vertex& v3, Model* document) :
 	LDObject (document)
 {
 	setVertex (0, v1);
--- a/src/ldObject.h	Sun Jan 29 15:05:14 2017 +0200
+++ b/src/ldObject.h	Sun Jan 29 15:18:40 2017 +0200
@@ -389,7 +389,7 @@
 	LDOBJ_NO_MATRIX
 
 public:
-	LDTriangle (Vertex const& v1, Vertex const& v2, Vertex const& v3, LDDocument* model = nullptr);
+	LDTriangle (Vertex const& v1, Vertex const& v2, Vertex const& v3, Model* model = nullptr);
 	int triangleCount() const override;
 };
 
@@ -412,8 +412,6 @@
 public:
 	LDQuad (const Vertex& v1, const Vertex& v2, const Vertex& v3, const Vertex& v4, LDDocument* model = nullptr);
 
-	// Split this quad into two triangles
-	QList<LDTriangle*> splitToTriangles();
 	int triangleCount() const override;
 };
 
--- a/src/model.h	Sun Jan 29 15:05:14 2017 +0200
+++ b/src/model.h	Sun Jan 29 15:18:40 2017 +0200
@@ -57,6 +57,14 @@
 			return nullptr;
 	}
 
+	template<typename T, typename... Args>
+	T* emplaceReplacementAt(int position, Args&& ...args)
+	{
+		T* replacement = constructObject<T>(args...);
+		setObjectAt(position, replacement);
+		return replacement;
+	}
+
 protected:
 	template<typename T, typename... Args>
 	T* constructObject(Args&& ...args)
--- a/src/toolsets/algorithmtoolset.cpp	Sun Jan 29 15:05:14 2017 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Sun Jan 29 15:18:40 2017 +0200
@@ -50,27 +50,40 @@
 
 void AlgorithmToolset::splitQuads()
 {
-	int num = 0;
-	QVector<LDObject*> selected = selectedObjects().toList().toVector();
+	int count = 0;
+
+	for (LDObject* object : selectedObjects().toList())
+	{
+		if (object->numVertices() != 4)
+			continue;
 
-	for (LDObjectIterator<LDQuad> it (selected); it.isValid(); ++it)
-	{
+		Vertex v0 = object->vertex(0);
+		Vertex v1 = object->vertex(1);
+		Vertex v2 = object->vertex(2);
+		Vertex v3 = object->vertex(3);
+		LDColor color = object->color();
+
 		// Find the index of this quad
-		int index = it->lineNumber();
-
+		int index = object->lineNumber();
 		if (index == -1)
-			return;
-
-		QList<LDTriangle*> triangles = it->splitToTriangles();
+			continue;
 
-		// Replace the quad with the first triangle and add the second triangle
-		// after the first one.
-		currentDocument()->setObjectAt (index, triangles[0]);
-		currentDocument()->insertObject (index + 1, triangles[1]);
-		num++;
+		// Create the two triangles based on this quadrilateral:
+		// 0───3       0───3    3
+		// │   │  --→  │  ╱    ╱│
+		// │   │  --→  │ ╱    ╱ │
+		// │   │  --→  │╱    ╱  │
+		// 1───2       1    1───2
+		LDTriangle* triangle1 = currentDocument()->emplaceReplacementAt<LDTriangle>(index, v0, v1, v3);
+		LDTriangle* triangle2 = currentDocument()->emplaceAt<LDTriangle>(index + 1, v1, v2, v3);
+
+		// The triangles also inherit the quad's color
+		triangle1->setColor(color);
+		triangle2->setColor(color);
+		count += 1;
 	}
 
-	print ("%1 quadrilaterals split", num);
+	print ("%1 quadrilaterals split", count);
 }
 
 void AlgorithmToolset::editRaw()
--- a/src/toolsets/basictoolset.cpp	Sun Jan 29 15:05:14 2017 +0200
+++ b/src/toolsets/basictoolset.cpp	Sun Jan 29 15:18:40 2017 +0200
@@ -108,7 +108,6 @@
 			reference->inlineContents(inlined, deep, false);
 
 			// Merge in the inlined objects
-			print("Inlined %1 objects.\n", countof(inlined.objects()));
 			for (LDObject* inlinedObject : inlined.objects())
 			{
 				currentDocument()->insertObject (idx++, inlinedObject);

mercurial