- subfile inverting now detects flatness and acts accordingly instead of naively applying invertnext to everything

Sun, 01 Jun 2014 03:15:36 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sun, 01 Jun 2014 03:15:36 +0300
changeset 783
1db20d88650f
parent 782
70526ee626a0
child 784
f82ab4d3c7b4

- subfile inverting now detects flatness and acts accordingly instead of naively applying invertnext to everything

changelog.txt file | annotate | diff | comparison | revisions
src/ldObject.cc file | annotate | diff | comparison | revisions
--- a/changelog.txt	Sun Jun 01 02:36:29 2014 +0300
+++ b/changelog.txt	Sun Jun 01 03:15:36 2014 +0300
@@ -34,6 +34,7 @@
 +	- Added a selection hover effect to the renderer, making selecting objects a lot more convenient.
 +	- Selecting an invertnext'd object now also selects the invertnext.
 +	- Made the coordinate rounding precision configurable.
++	- Inverting a subfile now detects whether the subfile is flat and flips it if it is instead of naively invertnexting everything.
 
 -	- The camera is now changed to the top one if switching to draw mode while using the free camera instead of disabling the draw mode.
 -	- The color selector now uses the color's edge color for the borders instead of black.
--- a/src/ldObject.cc	Sun Jun 01 02:36:29 2014 +0300
+++ b/src/ldObject.cc	Sun Jun 01 03:15:36 2014 +0300
@@ -654,12 +654,50 @@
 //
 void LDSubfile::invert()
 {
-	// Subfiles are inverted when they're prefixed with
-	// a BFC INVERTNEXT statement. Thus we need to toggle this status.
-	// For flat primitives it's sufficient that the determinant is
-	// flipped but I don't have a method for checking flatness yet.
-	// Food for thought...
+	// Check whether subfile is flat
+	int axisSet = (1 << X) | (1 << Y) | (1 << Z);
+	LDObjectList objs = inlineContents (true, false);
+
+	for (LDObjectPtr obj : objs)
+	{
+		for (int i = 0; i < obj->numVertices(); ++i)
+		{
+			Vertex const& vrt = obj->vertex (i);
+
+			if (axisSet & (1 << X) && vrt.x() != 0.0)
+				axisSet &= ~(1 << X);
+
+			if (axisSet & (1 << Y) && vrt.y() != 0.0)
+				axisSet &= ~(1 << Y);
+
+			if (axisSet & (1 << Z) && vrt.z() != 0.0)
+				axisSet &= ~(1 << Z);
+		}
 
+		if (axisSet == 0)
+			break;
+	}
+
+	if (axisSet != 0)
+	{
+		// Subfile has all vertices zero on one specific plane, so it is flat.
+		// Let's flip it.
+		Matrix matrixModifier = g_identity;
+
+		if (axisSet & (1 << X))
+			matrixModifier[0] = -1;
+
+		if (axisSet & (1 << Y))
+			matrixModifier[4] = -1;
+
+		if (axisSet & (1 << Z))
+			matrixModifier[8] = -1;
+
+		setTransform (transform() * matrixModifier);
+		return;
+	}
+
+	// Subfile is not flat. Resort to invertnext.
 	int idx = lineNumber();
 
 	if (idx > 0)

mercurial