- line lengths are now displayed when drawing

Fri, 20 Dec 2013 02:05:19 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 20 Dec 2013 02:05:19 +0200
changeset 574
10874674fe30
parent 573
74d71c6fdc20
child 575
59c0b57e843b

- line lengths are now displayed when drawing

changelog.txt file | annotate | diff | comparison | revisions
src/configDialog.cc file | annotate | diff | comparison | revisions
src/gldraw.cc file | annotate | diff | comparison | revisions
src/gldraw.h file | annotate | diff | comparison | revisions
src/types.cc file | annotate | diff | comparison | revisions
src/types.h file | annotate | diff | comparison | revisions
ui/config.ui file | annotate | diff | comparison | revisions
--- a/changelog.txt	Thu Dec 19 16:08:05 2013 +0200
+++ b/changelog.txt	Fri Dec 20 02:05:19 2013 +0200
@@ -22,6 +22,7 @@
 	usage somewhat.
 - LDraw code parser no longer complains about scientific notation.
 - Changing to draw mode while in free camera now causes the camera to be changed to top.
+- When drawing polygons, line lengths are now displayed. Added a configuration option to toggle this behavior.
 - Added config fields for default name/username/license. This data will be automatically filled
 	into forms that require such information.
 - Upon first start the configuration prompt pops up on its own, defaulting on the profile tab. This
--- a/src/configDialog.cc	Thu Dec 19 16:08:05 2013 +0200
+++ b/src/configDialog.cc	Fri Dec 20 02:05:19 2013 +0200
@@ -55,6 +55,7 @@
 extern_cfg (Bool, net_guesspaths);
 extern_cfg (Bool, net_autoclose);
 extern_cfg (Bool, gl_logostuds);
+extern_cfg (Bool,	gl_linelengths);
 extern_cfg (String, ld_defaultname);
 extern_cfg (String, ld_defaultuser);
 extern_cfg (Int, ld_defaultlicense);
@@ -107,6 +108,7 @@
 	ui->m_aa->setChecked (gl_aa);
 	ui->implicitFiles->setChecked (gui_implicitfiles);
 	ui->m_logostuds->setChecked (gl_logostuds);
+	ui->linelengths->setChecked (gl_linelengths);
 
 	int i = 0;
 #define act(N) addShortcut (key_##N, ACTION(N), i);
@@ -305,6 +307,7 @@
 	net_guesspaths = ui->guessNetPaths->isChecked();
 	net_autoclose = ui->autoCloseNetPrompt->isChecked();
 	gl_logostuds = ui->m_logostuds->isChecked();
+	gl_linelengths = ui->linelengths->isChecked();
 	ld_defaultuser = ui->m_profileUsername->text();
 	ld_defaultname = ui->m_profileName->text();
 	ld_defaultlicense = ui->m_profileLicense->currentIndex();
--- a/src/gldraw.cc	Thu Dec 19 16:08:05 2013 +0200
+++ b/src/gldraw.cc	Fri Dec 20 02:05:19 2013 +0200
@@ -48,23 +48,26 @@
 	{{  0, -1, 0 }, Z, Y, false,  true },
 };
 
+// Matrix templates for circle drawing. 2 is substituted with
+// the scale value, 1 is inverted to -1 if needed.
 static const matrix g_circleDrawTransforms[3] =
 {	{ 2, 0, 0, 0, 1, 0, 0, 0, 2 },
 	{ 2, 0, 0, 0, 0, 2, 0, 1, 0 },
 	{ 0, 1, 0, 2, 0, 0, 0, 0, 2 },
 };
 
-cfg (String, gl_bgcolor, "#CCCCD9");
-cfg (String, gl_maincolor, "#707078");
-cfg (Float, gl_maincolor_alpha, 1.0);
-cfg (Int, gl_linethickness, 2);
-cfg (Bool, gl_colorbfc, false);
-cfg (Int, gl_camera, GLRenderer::EFreeCamera);
-cfg (Bool, gl_blackedges, false);
-cfg (Bool, gl_axes, false);
-cfg (Bool, gl_wireframe, false);
-cfg (Bool, gl_logostuds, false);
-cfg (Bool, gl_aa, true);
+cfg (String,	gl_bgcolor,				"#CCCCD9");
+cfg (String,	gl_maincolor,			"#707078");
+cfg (Float,		gl_maincolor_alpha,	1.0);
+cfg (Int,		gl_linethickness,		2);
+cfg (Bool,		gl_colorbfc,			false);
+cfg (Int,		gl_camera,				GLRenderer::EFreeCamera);
+cfg (Bool,		gl_blackedges,			false);
+cfg (Bool,		gl_axes,					false);
+cfg (Bool,		gl_wireframe,			false);
+cfg (Bool,		gl_logostuds,			false);
+cfg (Bool,		gl_aa,					true);
+cfg (Bool,		gl_linelengths,		true);
 
 // argh
 const char* g_CameraNames[7] =
@@ -87,13 +90,14 @@
 	GL::EFreeCamera
 };
 
+// Definitions for visual axes, drawn on the screen
 const struct LDGLAxis
 {	const QColor col;
 	const vertex vert;
 } g_GLAxes[3] =
-{	{ QColor (255,   0,   0), vertex (10000, 0, 0) },
-	{ QColor (80,  192,   0), vertex (0, 10000, 0) },
-	{ QColor (0,   160, 192), vertex (0, 0, 10000) },
+{	{ QColor (255,   0,   0), vertex (10000, 0, 0) }, // X
+	{ QColor (80,  192,   0), vertex (0, 10000, 0) }, // Y
+	{ QColor (0,   160, 192), vertex (0, 0, 10000) }, // Z
 };
 
 static bool g_glInvert = false;
@@ -489,10 +493,12 @@
 	cx *= negXFac;
 	cy *= negYFac;
 
-	str tmp;
+	roundToDecimals (cx, 4);
+	roundToDecimals (cy, 4);
+
 	// Create the vertex from the coordinates
-	pos3d[axisX] = tmp.sprintf ("%.3f", cx).toDouble();
-	pos3d[axisY] = tmp.sprintf ("%.3f", cy).toDouble();
+	pos3d[axisX] = cx;
+	pos3d[axisY] = cy;
 	pos3d[3 - axisX - axisY] = getDepthValue();
 	return pos3d;
 }
@@ -500,7 +506,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 // Inverse operation for the above - convert a 3D position to a 2D screen
-// position
+// position. Don't ask me how this code manages to work, I don't even know.
 // -----------------------------------------------------------------------------
 QPoint GLRenderer::coordconv3_2 (const vertex& pos3d) const
 {	GLfloat m[16];
@@ -539,7 +545,7 @@
 	initGLData();
 	drawGLScene();
 
-	const QPen textpen = getTextPen();
+	const QPen textpen (m_darkbg ? Qt::white : Qt::black);
 	const QBrush polybrush (QColor (64, 192, 0, 128));
 	QPainter paint (this);
 	QFontMetrics metrics = QFontMetrics (QFont());
@@ -568,7 +574,7 @@
 		QFontMetrics metrics = QFontMetrics (font());
 		QRect textSize = metrics.boundingRect (0, 0, m_width, m_height, Qt::AlignCenter, text);
 
-		paint.setPen (getTextPen());
+		paint.setPen (textpen);
 		paint.drawText (m_width - textSize.width(), m_height - 16, textSize.width(),
 			textSize.height(), Qt::AlignCenter, text);
 
@@ -585,40 +591,33 @@
 
 			if (numverts > 0)
 			{	QPoint poly[4];
-				vertex polyverts[4];
+				vertex poly3d[4];
 
 				if (!m_rectdraw)
-				{	uchar i = 0;
+				{	int i = 0;
 
 					for (vertex& vert : m_drawedVerts)
-					{	poly[i] = coordconv3_2 (vert);
-						polyverts[i] = vert;
-						++i;
-					}
+						poly3d[i++] = vert;
 
 					// Draw the cursor vertex as the last one in the list.
 					if (numverts <= 4)
-					{	poly[i] = coordconv3_2 (m_hoverpos);
-						polyverts[i] = m_hoverpos;
-					}
+						poly3d[i] = m_hoverpos;
 					else
-					{	numverts = 4;
-					}
+						numverts = 4;
 				}
 				else
-				{	if (m_drawedVerts.size() > 0)
-					{	// Get vertex information from m_rectverts
+				{	// Get vertex information from m_rectverts
+					if (m_drawedVerts.size() > 0)
 						for (int i = 0; i < numverts; ++i)
-						{	polyverts[i] = m_rectverts[i];
-							poly[i] = coordconv3_2 (polyverts[i]);
-						}
-					}
+							poly3d[i] = m_rectverts[i];
 					else
-					{	poly[0] = coordconv3_2 (m_hoverpos);
-						polyverts[0] = m_hoverpos;
-					}
+						poly3d[0] = m_hoverpos;
 				}
 
+				// convert to 2d
+				for (int i = 0; i < numverts; ++i)
+					poly[i] = coordconv3_2 (poly3d[i]);
+
 				// Draw the polygon-to-be
 				paint.setPen (linepen);
 				paint.setBrush (polybrush);
@@ -630,7 +629,19 @@
 					drawBlip (paint, blip);
 
 					// Draw their coordinates
-					paint.drawText (blip.x(), blip.y() - 8, polyverts[i].stringRep (true));
+					paint.drawText (blip.x(), blip.y() - 8, poly3d[i].stringRep (true));
+				}
+
+				// Draw line lengths
+				if (numverts >= 2 && gl_linelengths)
+				{	int numlines = (m_drawedVerts.size() == 1) ? 1 : m_drawedVerts.size() + 1;
+
+					for (int i = 0; i < numlines; ++i)
+					{	const int j = (i + 1 < numverts) ? i + 1 : 0;
+						const str label = str::number (poly3d[i].distanceTo (poly3d[j]));
+						paint.setPen (textpen);
+						paint.drawText (QLineF (poly[i], poly[j]).pointAt (0.5), label);
+					}
 				}
 			}
 		}
@@ -761,7 +772,7 @@
 	if (getMessageLog())
 	{	int y = 0;
 		const int margin = 2;
-		QColor penColor = getTextPen();
+		QColor penColor = textpen.color();
 
 		for (const MessageManager::Line& line : getMessageLog()->getLines())
 		{	penColor.setAlphaF (line.alpha);
@@ -801,12 +812,6 @@
 
 // =============================================================================
 // -----------------------------------------------------------------------------
-QColor GLRenderer::getTextPen () const
-{	return m_darkbg ? Qt::white : Qt::black;
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
 void GLRenderer::compileAllObjects()
 {	if (!getFile())
 		return;
--- a/src/gldraw.h	Thu Dec 19 16:08:05 2013 +0200
+++ b/src/gldraw.h	Fri Dec 20 02:05:19 2013 +0200
@@ -221,9 +221,6 @@
 		// Convert a 3D point to a 2D point
 		QPoint         coordconv3_2 (const vertex& pos3d) const;
 
-		// Determine which color to draw text with
-		QColor         getTextPen() const;
-
 		// Perform object selection
 		void           pick (int mouseX, int mouseY);
 
--- a/src/types.cc	Thu Dec 19 16:08:05 2013 +0200
+++ b/src/types.cc	Fri Dec 20 02:05:19 2013 +0200
@@ -56,6 +56,15 @@
 
 // =============================================================================
 // -----------------------------------------------------------------------------
+double vertex::distanceTo (const vertex& other) const
+{	double dx = abs (x() - other.x());
+	double dy = abs (y() - other.y());
+	double dz = abs (z() - other.z());
+	return sqrt ((dx * dx) + (dy * dy) + (dz * dz));
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
 vertex vertex::midpoint (const vertex& other)
 {	vertex mid;
 
--- a/src/types.h	Thu Dec 19 16:08:05 2013 +0200
+++ b/src/types.h	Fri Dec 20 02:05:19 2013 +0200
@@ -115,10 +115,24 @@
 		vertex() {}
 		vertex (double x, double y, double z);
 
-		vertex          midpoint    (const vertex& other);
-		void            move        (const vertex& other);
-		str             stringRep   (bool mangled) const;
-		void            transform   (matrix matr, vertex pos);
+		double			distanceTo (const vertex& other) const;
+		vertex			midpoint (const vertex& other);
+		void				move (const vertex& other);
+		str				stringRep (bool mangled) const;
+		void				transform (matrix matr, vertex pos);
+
+		vertex&			operator+= (const vertex& other);
+		vertex			operator+ (const vertex& other) const;
+		vertex			operator/ (const double d) const;
+		vertex&			operator/= (const double d);
+		bool				operator== (const vertex& other) const;
+		bool				operator!= (const vertex& other) const;
+		vertex			operator-() const;
+		int				operator< (const vertex& other) const;
+		double&			operator[] (const Axis ax);
+		const double&	operator[] (const Axis ax) const;
+		double&			operator[] (const int ax);
+		const double&	operator[] (const int ax) const;
 
 		inline double& coord (int n)
 		{	return m_coords[n];
@@ -128,42 +142,29 @@
 		{	return m_coords[n];
 		}
 
-		inline double& x ()
+		inline double& x()
 		{	return m_coords[X];
 		}
 
-		inline const double& x () const
+		inline const double& x() const
 		{	return m_coords[X];
 		}
 
-		inline double& y ()
-		{	return m_coords[Y];
-		}
-
-		inline const double& y () const
+		inline double& y()
 		{	return m_coords[Y];
 		}
 
-		inline double& z ()
-		{	return m_coords[Z];
+		inline const double& y() const
+		{	return m_coords[Y];
 		}
 
-		inline const double& z () const
+		inline double& z()
 		{	return m_coords[Z];
 		}
 
-		vertex&			operator+=	(const vertex& other);
-		vertex			operator+	(const vertex& other) const;
-		vertex			operator/	(const double d) const;
-		vertex&			operator/=	(const double d);
-		bool			operator==	(const vertex& other) const;
-		bool			operator!=	(const vertex& other) const;
-		vertex			operator-	() const;
-		int				operator<	(const vertex& other) const;
-		double&			operator[]	(const Axis ax);
-		const double&	operator[]	(const Axis ax) const;
-		double&			operator[]	(const int ax);
-		const double&	operator[]	(const int ax) const;
+		inline const double& z() const
+		{	return m_coords[Z];
+		}
 
 	private:
 		double m_coords[3];
--- a/ui/config.ui	Thu Dec 19 16:08:05 2013 +0200
+++ b/ui/config.ui	Fri Dec 20 02:05:19 2013 +0200
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>717</width>
-    <height>400</height>
+    <height>328</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -267,6 +267,13 @@
                  </widget>
                 </item>
                 <item>
+                 <widget class="QCheckBox" name="linelengths">
+                  <property name="text">
+                   <string>Display line lenghts when drawing</string>
+                  </property>
+                 </widget>
+                </item>
+                <item>
                  <widget class="QCheckBox" name="implicitFiles">
                   <property name="text">
                    <string>List implicitly loaded files</string>

mercurial