added some meta stuff, simplified quadrilateral splitting and tested it

Mon, 09 Mar 2020 23:42:26 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 09 Mar 2020 23:42:26 +0200
changeset 77
028798a72591
parent 76
7c4a63a02632
child 78
97c3ce5aa498
child 90
e234edb5e613

added some meta stuff, simplified quadrilateral splitting and tested it

locale/fi.ts file | annotate | diff | comparison | revisions
src/gl/partrenderer.cpp file | annotate | diff | comparison | revisions
src/linetypes/conditionaledge.cpp file | annotate | diff | comparison | revisions
src/linetypes/conditionaledge.h file | annotate | diff | comparison | revisions
src/linetypes/edge.cpp file | annotate | diff | comparison | revisions
src/linetypes/edge.h file | annotate | diff | comparison | revisions
src/linetypes/errorline.h file | annotate | diff | comparison | revisions
src/linetypes/metacommand.h file | annotate | diff | comparison | revisions
src/linetypes/object.h file | annotate | diff | comparison | revisions
src/linetypes/quadrilateral.cpp file | annotate | diff | comparison | revisions
src/linetypes/quadrilateral.h file | annotate | diff | comparison | revisions
src/linetypes/subfilereference.h file | annotate | diff | comparison | revisions
src/linetypes/triangle.cpp file | annotate | diff | comparison | revisions
src/linetypes/triangle.h file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
src/modeleditcontext.cpp file | annotate | diff | comparison | revisions
src/parser.cpp file | annotate | diff | comparison | revisions
--- a/locale/fi.ts	Mon Mar 09 22:12:50 2020 +0200
+++ b/locale/fi.ts	Mon Mar 09 23:42:26 2020 +0200
@@ -209,17 +209,17 @@
 <context>
     <name>QObject</name>
     <message>
-        <location filename="../src/gl/partrenderer.cpp" line="239"/>
+        <location filename="../src/gl/partrenderer.cpp" line="244"/>
         <source>OpenGL error: %1</source>
         <translation>OpenGL-virhe: %1</translation>
     </message>
     <message>
-        <location filename="../src/gl/partrenderer.cpp" line="240"/>
+        <location filename="../src/gl/partrenderer.cpp" line="245"/>
         <source>OpenGL error</source>
         <translation>OpenGL-virhe</translation>
     </message>
     <message>
-        <location filename="../src/gl/partrenderer.cpp" line="242"/>
+        <location filename="../src/gl/partrenderer.cpp" line="247"/>
         <source>Damn it</source>
         <translation>Hemmetti</translation>
     </message>
--- a/src/gl/partrenderer.cpp	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/gl/partrenderer.cpp	Mon Mar 09 23:42:26 2020 +0200
@@ -24,6 +24,7 @@
 #include <QAbstractButton>
 #include "geometry.h"
 #include "partrenderer.h"
+#include "model.h"
 
 PartRenderer::PartRenderer(
 	Model* model,
@@ -61,6 +62,10 @@
 	}
 	this->compiler->initialize();
 	this->compiler->build(this->model, this->documents, this->renderPreferences);
+	connect(this->model, &Model::dataChanged, [&]()
+	{
+		this->compiler->build(this->model, this->documents, this->renderPreferences);
+	});
 	this->initialized = true;
 	this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0});
 	this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0});
--- a/src/linetypes/conditionaledge.cpp	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/conditionaledge.cpp	Mon Mar 09 23:42:26 2020 +0200
@@ -12,7 +12,7 @@
 {
 }
 
-ldraw::ConditionalEdge::ConditionalEdge(const QVector<glm::vec3>& vertices, const Color color) :
+ldraw::ConditionalEdge::ConditionalEdge(const std::array<glm::vec3, 4>& vertices, const Color color) :
 	Edge{vertices[0], vertices[1], color},
 	controlPoint_1{vertices[2]},
 	controlPoint_2{vertices[3]}
--- a/src/linetypes/conditionaledge.h	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/conditionaledge.h	Mon Mar 09 23:42:26 2020 +0200
@@ -16,13 +16,12 @@
 		const glm::vec3& controlPoint_1,
 		const glm::vec3& controlPoint_2,
 		const Color colorIndex = ldraw::edgeColor);
-	ConditionalEdge(const QVector<glm::vec3>& vertices, const Color color);
+	ConditionalEdge(const std::array<glm::vec3, 4>& vertices, const Color color);
 	QVariant getProperty(Property property) const override;
 	SetPropertyResult setProperty(
 		Property property,
 		const QVariant& value) override;
 	QString textRepresentation() const override;
-private:
 	glm::vec3 controlPoint_1 = {};
 	glm::vec3 controlPoint_2 = {};
 };
--- a/src/linetypes/edge.cpp	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/edge.cpp	Mon Mar 09 23:42:26 2020 +0200
@@ -8,7 +8,7 @@
 	point_1{point_1},
 	point_2{point_2} {}
 
-ldraw::Edge::Edge(const QVector<glm::vec3>& vertices, const Color color) :
+ldraw::Edge::Edge(const std::array<glm::vec3, 2>& vertices, const Color color) :
 	ColoredObject{color},
 	point_1{vertices[0]},
 	point_2{vertices[1]}
--- a/src/linetypes/edge.h	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/edge.h	Mon Mar 09 23:42:26 2020 +0200
@@ -13,14 +13,13 @@
 	Edge() = default;
 	Edge(const glm::vec3& point_1, const glm::vec3& point_2,
 		 const Color colorIndex = ldraw::edgeColor);
-	Edge(const QVector<glm::vec3>& vertices, const Color color);
+	Edge(const std::array<glm::vec3, 2>& vertices, const Color color);
 	QVariant getProperty(Property property) const override;
 	SetPropertyResult setProperty(
 		Property property,
 		const QVariant& value) override;
 	QString textRepresentation() const override;
 	void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const override;
-private:
 	glm::vec3 point_1 = {};
 	glm::vec3 point_2 = {};
 };
--- a/src/linetypes/errorline.h	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/errorline.h	Mon Mar 09 23:42:26 2020 +0200
@@ -17,7 +17,6 @@
 	QString textRepresentation() const override;
 	QBrush textRepresentationForeground() const override;
 	QBrush textRepresentationBackground() const override;
-private:
 	QString text;
 	QString message;
 };
--- a/src/linetypes/metacommand.h	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/metacommand.h	Mon Mar 09 23:42:26 2020 +0200
@@ -15,6 +15,5 @@
 		Property property,
 		const QVariant& value) override;
 	QString textRepresentation() const override;
-private:
 	QString storedText = "";
 };
--- a/src/linetypes/object.h	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/object.h	Mon Mar 09 23:42:26 2020 +0200
@@ -13,6 +13,7 @@
 	class Object;
 	class ColoredObject;
 	class Empty;
+	class UnhandledProperty;
 }
 
 class DocumentManager;
@@ -41,6 +42,36 @@
 	ErrorMessage // For error lines, why parsing failed
 };
 
+// Mapping of properties to types
+#define LDFORGE_DEFINE_PROPERTY_TYPE(PROPERTY, TYPE) \
+	namespace ldraw { \
+		template<> struct PropertyType<ldraw::Property::PROPERTY> { using type = TYPE; }; \
+	}
+
+namespace ldraw
+{
+	template<ldraw::Property property>
+	struct PropertyType
+	{
+	};
+
+	template<ldraw::Property property>
+	using PropertyType_t = typename PropertyType<property>::type;
+}
+
+LDFORGE_DEFINE_PROPERTY_TYPE(Color, int)
+LDFORGE_DEFINE_PROPERTY_TYPE(Text, QString)
+LDFORGE_DEFINE_PROPERTY_TYPE(Point1, glm::vec3)
+LDFORGE_DEFINE_PROPERTY_TYPE(Point2, glm::vec3)
+LDFORGE_DEFINE_PROPERTY_TYPE(Point3, glm::vec3)
+LDFORGE_DEFINE_PROPERTY_TYPE(Point4, glm::vec3)
+LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint1, glm::vec3)
+LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint2, glm::vec3)
+LDFORGE_DEFINE_PROPERTY_TYPE(Transformation, glm::mat4)
+LDFORGE_DEFINE_PROPERTY_TYPE(ReferenceName, QString)
+LDFORGE_DEFINE_PROPERTY_TYPE(IsInverted, bool)
+LDFORGE_DEFINE_PROPERTY_TYPE(ErrorMessage, QString)
+
 class ldraw::Object
 {
 public:
@@ -73,7 +104,6 @@
 	bool hasColor() const override final;
 	QVariant getProperty(Property id) const override;
 	SetPropertyResult setProperty(Property id, const QVariant& value) override;
-protected:
 	Color colorIndex = ldraw::mainColor;
 };
 
--- a/src/linetypes/quadrilateral.cpp	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/quadrilateral.cpp	Mon Mar 09 23:42:26 2020 +0200
@@ -11,7 +11,7 @@
 {
 }
 
-ldraw::Quadrilateral::Quadrilateral(const QVector<glm::vec3>& vertices, const Color color) :
+ldraw::Quadrilateral::Quadrilateral(const std::array<glm::vec3, 4>& vertices, const Color color) :
 	ColoredObject{color},
 	points{vertices[0], vertices[1], vertices[2], vertices[3]}
 {
--- a/src/linetypes/quadrilateral.h	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/quadrilateral.h	Mon Mar 09 23:42:26 2020 +0200
@@ -16,12 +16,11 @@
 		const glm::vec3 &point_3,
 		const glm::vec3 &point_4,
 		Color colorIndex = ldraw::mainColor);
-	Quadrilateral(const QVector<glm::vec3>& vertices, const Color color);
+	Quadrilateral(const std::array<glm::vec3, 4>& vertices, const Color color);
 	QVariant getProperty(Property id) const override;
 	SetPropertyResult setProperty(Property id, const QVariant& value) override;
 	QString textRepresentation() const override;
 	void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const override;
 	void invert() override;
-private:
 	glm::vec3 points[4] = {{}};
 };
--- a/src/linetypes/subfilereference.h	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/subfilereference.h	Mon Mar 09 23:42:26 2020 +0200
@@ -22,7 +22,6 @@
 	void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const override;
 	glm::vec3 position() const;
 	void invert() override;
-private:
 	Model* resolve(DocumentManager* documents) const;
 	glm::mat4 transformation;
 	QString referenceName;
--- a/src/linetypes/triangle.cpp	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/triangle.cpp	Mon Mar 09 23:42:26 2020 +0200
@@ -10,7 +10,13 @@
 {
 }
 
-ldraw::Triangle::Triangle(const QVector<glm::vec3>& vertices, const Color color) :
+ldraw::Triangle::Triangle(const std::array<glm::vec3, 3>& vertices, const Color color) :
+	ColoredObject{color},
+	points{vertices[0], vertices[1], vertices[2]}
+{
+}
+
+ldraw::Triangle::Triangle(const glm::vec3 (&vertices)[3], const Color color) :
 	ColoredObject{color},
 	points{vertices[0], vertices[1], vertices[2]}
 {
--- a/src/linetypes/triangle.h	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/linetypes/triangle.h	Mon Mar 09 23:42:26 2020 +0200
@@ -15,13 +15,13 @@
 		const glm::vec3 &point_2,
 		const glm::vec3 &point_3,
 		Color colorIndex = ldraw::mainColor);
-	Triangle(const QVector<glm::vec3>& vertices, const Color color);
+	Triangle(const std::array<glm::vec3, 3>& vertices, const Color color);
+	Triangle(const glm::vec3 (&vertices)[3], const Color color);
 	QVariant getProperty(Property id) const override;
 	SetPropertyResult setProperty(Property id, const QVariant& value) override;
 	QString textRepresentation() const override;
 	void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const override;
 	void invert() override;
-private:
 	glm::vec3 points[3] = {{}};
 };
 
--- a/src/main.cpp	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/main.cpp	Mon Mar 09 23:42:26 2020 +0200
@@ -21,8 +21,6 @@
 #include "mainwindow.h"
 #include "version.h"
 
-#include "linetypes/quadrilateral.h"
-
 int main(int argc, char *argv[])
 {
 	::glutInit(&argc, argv);
--- a/src/modeleditcontext.cpp	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/modeleditcontext.cpp	Mon Mar 09 23:42:26 2020 +0200
@@ -63,27 +63,16 @@
 	return this->storedModel;
 }
 
-namespace
+static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const glm::vec3(&points)[4])
 {
-	using PropertyTriplet = std::tuple<ldraw::Property, ldraw::Property, ldraw::Property>;
-}
-
-static std::array<PropertyTriplet, 2> diagonalToPointProperties(ldraw::Diagonal diagonal)
-{
-	std::array<PropertyTriplet, 2> result;
+	std::array<geom::Triangle, 2> result;
 	switch (diagonal)
 	{
 	case ldraw::Diagonal::Diagonal_13:
-		result = {
-			std::make_tuple(ldraw::Property::Point1, ldraw::Property::Point2, ldraw::Property::Point3),
-			std::make_tuple(ldraw::Property::Point1, ldraw::Property::Point3, ldraw::Property::Point4)
-		};
+		result = {geom::Triangle{points[0], points[1], points[2]}, {points[0], points[2], points[3]}};
 		break;
 	case ldraw::Diagonal::Diagonal_24:
-		result = {
-			std::make_tuple(ldraw::Property::Point1, ldraw::Property::Point2, ldraw::Property::Point4),
-			std::make_tuple(ldraw::Property::Point2, ldraw::Property::Point3, ldraw::Property::Point4)
-		};
+		result = {geom::Triangle{points[0], points[1], points[3]}, {points[1], points[2], points[3]}};
 		break;
 	}
 	return result;
@@ -100,24 +89,13 @@
 	const ldraw::Quadrilateral* quadrilateral = editor.model().get(quadrilateral_id, &index);
 	if (quadrilateral != nullptr)
 	{
-		Q_ASSERT(index.isValid());
-		int position = index.row();
+		const ldraw::Color color = quadrilateral->colorIndex;
+		const std::array<geom::Triangle, 2> split = splitTriangles(splitType, quadrilateral->points);
+		const int position = index.row();
 		editor.remove(position);
-		std::vector<ldraw::triangleid_t> triangles;
-		triangles.reserve(2);
-		const std::array<PropertyTriplet, 2> split = diagonalToPointProperties(splitType);
-		for (const PropertyTriplet& properties : split)
-		{
-			const ldraw::triangleid_t triangle = editor.insert<ldraw::Triangle>(
-				position,
-				quadrilateral->getProperty(std::get<0>(properties)).value<glm::vec3>(),
-				quadrilateral->getProperty(std::get<1>(properties)).value<glm::vec3>(),
-				quadrilateral->getProperty(std::get<2>(properties)).value<glm::vec3>(),
-				ldraw::Color{quadrilateral->getProperty(ldraw::Property::Color).toInt()});
-			triangles.push_back(triangle);
-			position += 1;
-		}
-		result = {triangles[0], triangles[1]};
+		result = std::make_pair(
+			editor.insert<ldraw::Triangle>(position, split[0].points, color),
+			editor.insert<ldraw::Triangle>(position, split[1].points, color));
 	}
 	return result;
 }
--- a/src/parser.cpp	Mon Mar 09 22:12:50 2020 +0200
+++ b/src/parser.cpp	Mon Mar 09 23:42:26 2020 +0200
@@ -263,6 +263,21 @@
 		}
 		invertNext = false;
 	}
+	// Test quadrilateral splitting by splitting all the quadrilaterals
+	QVector<ldraw::quadrilateralid_t> quadrilateral_ids;
+	for (int i = 0; i < editor.model().size(); i += 1)
+	{
+		const ldraw::id_t id = editor.model().resolve(editor.model().index(i));
+		const ldraw::quadrilateralid_t quad_id = editor.model().checkType<ldraw::Quadrilateral>(id);
+		if (not(quad_id == ldraw::NULL_ID))
+		{
+			quadrilateral_ids.push_back(quad_id);
+		}
+	}
+	for (const ldraw::quadrilateralid_t id : quadrilateral_ids)
+	{
+		ldraw::splitQuadrilateral(editor, id);
+	}
 }
 
 static ldraw::Color colorFromString(const QString& colorString)
@@ -376,11 +391,10 @@
 		throw BodyParseError{"wrong amount of tokens"};
 	}
 	const ldraw::Color color = colorFromString(tokens[colorPosition]);
-	QVector<glm::vec3> vertices;
-	vertices.reserve(NumVertices);
+	std::array<glm::vec3, NumVertices> vertices;
 	for (int i = 0; i < NumVertices; i += 1)
 	{
-		vertices.append(vertexFromStrings(tokens, vertexPosition(i)));
+		vertices[unsigned_cast(i)] = vertexFromStrings(tokens, vertexPosition(i));
 	}
 	return std::make_unique<T>(vertices, color);
 }

mercurial