refactored LDPolygon

Sun, 10 Jun 2018 21:48:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 10 Jun 2018 21:48:58 +0300
changeset 1400
ae83213bdd63
parent 1399
f52ea4078f5d
child 1401
59b578c77111

refactored LDPolygon

src/glShared.h file | annotate | diff | comparison | revisions
src/glcompiler.cpp file | annotate | diff | comparison | revisions
src/glcompiler.h file | annotate | diff | comparison | revisions
src/lddocument.cpp file | annotate | diff | comparison | revisions
src/linetypes/circularprimitive.cpp file | annotate | diff | comparison | revisions
src/linetypes/modelobject.cpp file | annotate | diff | comparison | revisions
src/linetypes/modelobject.h file | annotate | diff | comparison | revisions
--- a/src/glShared.h	Sun Jun 10 17:17:42 2018 +0300
+++ b/src/glShared.h	Sun Jun 10 21:48:58 2018 +0300
@@ -17,10 +17,11 @@
  */
 
 #pragma once
-
 #include <QOpenGLFunctions>
 #include <QGenericMatrix>
 #include "basics.h"
+#include "colors.h"
+#include "generics/enums.h"
 #include "types/vertex.h"
 
 inline void glMultMatrixf(const GLRotationMatrix& matrix)
@@ -42,18 +43,43 @@
 
 struct LDPolygon
 {
-	char		num;
-	Vertex		vertices[4];
-	int			color;
+	enum class Type : qint8 { InvalidPolygon, EdgeLine, Triangle, Quadrilateral, ConditionalEdge };
+	Type type = Type::InvalidPolygon;
+	Vertex vertices[4];
+	LDColor color;
 
 	inline int numPolygonVertices() const
 	{
-		return (num == 5) ? 2 : num;
+		if (type == Type::ConditionalEdge)
+			return 2;
+		else
+			return numVertices();
 	}
 
 	inline int numVertices() const
 	{
-		return (num == 5) ? 4 : num;
+		switch (type)
+		{
+		case Type::EdgeLine:
+			return 2;
+
+		case Type::Triangle:
+			return 3;
+
+		case Type::ConditionalEdge:
+		case Type::Quadrilateral:
+			return 4;
+
+		case Type::InvalidPolygon:
+			return 0;
+		}
+
+		return 0;
+	}
+
+	bool isValid() const
+	{
+		return type != Type::InvalidPolygon;
 	}
 };
 
--- a/src/glcompiler.cpp	Sun Jun 10 17:17:42 2018 +0300
+++ b/src/glcompiler.cpp	Sun Jun 10 21:48:58 2018 +0300
@@ -227,13 +227,13 @@
 	else
 	{
 		// The color was unknown. Use main color to make the polygon at least not appear pitch-black.
-		if (polygon.num != 2 and polygon.num != 5)
+		if (polygon.type != LDPolygon::Type::EdgeLine and polygon.type != LDPolygon::Type::ConditionalEdge)
 			color = guiUtilities()->mainColorRepresentation();
 		else
 			color = Qt::black;
 
 		// Warn about the unknown color, but only once.
-		static QSet<int> warnedColors;
+		static QSet<LDColor> warnedColors;
 		if (not warnedColors.contains(polygon.color))
 		{
 			print(tr("Unknown color %1!\n"), polygon.color);
@@ -367,11 +367,10 @@
 	case LDObjectType::EdgeLine:
 	case LDObjectType::ConditionalEdge:
 		{
-			LDPolygon* poly = object->getPolygon();
-			compilePolygon (*poly, index, info);
-			delete poly;
-			break;
+			LDPolygon polygon = object->getPolygon();
+			compilePolygon(polygon, index, info);
 		}
+		break;
 
 	case LDObjectType::BezierCurve:
 		{
@@ -409,21 +408,21 @@
 
 	VboClass surface;
 
-	switch (poly.num)
+	switch (poly.type)
 	{
-	case 2:
+	case LDPolygon::Type::EdgeLine:
 		surface = VboClass::Lines;
 		break;
 
-	case 3:
+	case LDPolygon::Type::Triangle:
 		surface = VboClass::Triangles;
 		break;
 
-	case 4:
+	case LDPolygon::Type::Quadrilateral:
 		surface = VboClass::Quads;
 		break;
 
-	case 5:
+	case LDPolygon::Type::ConditionalEdge:
 		surface = VboClass::ConditionalLines;
 		break;
 
--- a/src/glcompiler.h	Sun Jun 10 17:17:42 2018 +0300
+++ b/src/glcompiler.h	Sun Jun 10 21:48:58 2018 +0300
@@ -59,17 +59,9 @@
 	};
 
 	void compileStaged();
-	void compilePolygon(
-		LDPolygon& poly,
-		const QModelIndex& polygonOwnerIndex,
-		ObjectVboData& objectInfo
-	);
+	void compilePolygon(LDPolygon& poly, const QModelIndex& polygonOwnerIndex, ObjectVboData& objectInfo);
 	Q_SLOT void compileObject(const QModelIndex &index);
-	QColor getColorForPolygon(
-		LDPolygon& poly,
-		const QModelIndex& polygonOwnerIndex,
-		VboSubclass complement
-	);
+	QColor getColorForPolygon(LDPolygon& poly, const QModelIndex& polygonOwnerIndex, VboSubclass complement);
 	QColor indexColorForID (qint32 id) const;
 	void needMerge();
 	Q_SLOT void recompile();
--- a/src/lddocument.cpp	Sun Jun 10 17:17:42 2018 +0300
+++ b/src/lddocument.cpp	Sun Jun 10 21:48:58 2018 +0300
@@ -464,13 +464,10 @@
 				continue;
 			}
 
-			LDPolygon* data = obj->getPolygon();
+			LDPolygon data = obj->getPolygon();
 
-			if (data)
-			{
-				m_polygonData << *data;
-				delete data;
-			}
+			if (data.isValid())
+				m_polygonData << data;
 		}
 
 		m_needsRecache = false;
--- a/src/linetypes/circularprimitive.cpp	Sun Jun 10 17:17:42 2018 +0300
+++ b/src/linetypes/circularprimitive.cpp	Sun Jun 10 21:48:58 2018 +0300
@@ -110,17 +110,15 @@
 			object->setVertex(i, vertex);
 		}
 
-		LDPolygon* polygon = object->getPolygon();
+		LDPolygon polygon = object->getPolygon();
 
-		if (polygon)
+		if (polygon.isValid())
 		{
 			if (cachedShouldInvert)
-				invertPolygon(*polygon);
+				invertPolygon(polygon);
 
-			result.append(*polygon);
+			result.append(polygon);
 		}
-
-		delete polygon;
 	}
 
 	return result;
--- a/src/linetypes/modelobject.cpp	Sun Jun 10 17:17:42 2018 +0300
+++ b/src/linetypes/modelobject.cpp	Sun Jun 10 21:48:58 2018 +0300
@@ -210,24 +210,39 @@
 
 // =============================================================================
 //
-LDPolygon* LDObject::getPolygon()
+LDPolygon LDObject::getPolygon()
 {
 	LDObjectType ot = type();
-	int num = (ot == LDObjectType::EdgeLine)		? 2
-			: (ot == LDObjectType::Triangle)	? 3
-			: (ot == LDObjectType::Quadrilateral)		? 4
-			: (ot == LDObjectType::ConditionalEdge)	? 5
-			: 0;
+	LDPolygon::Type polygonType;
+
+	switch (ot)
+	{
+	case LDObjectType::EdgeLine:
+		polygonType = LDPolygon::Type::EdgeLine;
+		break;
+
+	case LDObjectType::Triangle:
+		polygonType = LDPolygon::Type::Triangle;
+		break;
 
-	if (num == 0)
-		return nullptr;
+	case LDObjectType::Quadrilateral:
+		polygonType = LDPolygon::Type::Quadrilateral;
+		break;
+
+	case LDObjectType::ConditionalEdge:
+		polygonType = LDPolygon::Type::ConditionalEdge;
+		break;
 
-	LDPolygon* data = new LDPolygon;
-	data->num = num;
-	data->color = color().index();
+	default:
+		return {};
+	}
 
-	for (int i = 0; i < data->numVertices(); ++i)
-		data->vertices[i] = vertex (i);
+	LDPolygon data;
+	data.type = polygonType;
+	data.color = color();
+
+	for (int i = 0; i < data.numVertices(); ++i)
+		data.vertices[i] = vertex(i);
 
 	return data;
 }
@@ -480,7 +495,7 @@
 	parms.append (pointAt (1.0));
 	LDPolygon poly;
 	poly.color = color().index();
-	poly.num = 2;
+	poly.type = LDPolygon::Type::EdgeLine;
 
 	for (int i = 0; i < segments; ++i)
 	{
--- a/src/linetypes/modelobject.h	Sun Jun 10 17:17:42 2018 +0300
+++ b/src/linetypes/modelobject.h	Sun Jun 10 21:48:58 2018 +0300
@@ -65,7 +65,7 @@
 	virtual QString asText() const = 0; // This object as LDraw code
 	LDColor color() const;
 	virtual LDColor defaultColor() const; // What color does the object default to?
-	LDPolygon* getPolygon();
+	LDPolygon getPolygon();
 	virtual void getVertices (DocumentManager *context, QSet<Vertex>& verts) const;
 	virtual bool hasMatrix() const; // Does this object have a matrix and position? (see LDMatrixObject)
 	virtual bool isColored() const;

mercurial