Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code

Sat, 16 Mar 2013 13:08:24 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 16 Mar 2013 13:08:24 +0200
changeset 18
a6732098fed8
parent 17
5606eebd0b90
child 19
6c5977e43e73

Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code

common.h file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
ldtypes.cpp file | annotate | diff | comparison | revisions
main.cpp file | annotate | diff | comparison | revisions
misc.cpp file | annotate | diff | comparison | revisions
misc.h file | annotate | diff | comparison | revisions
str.cpp file | annotate | diff | comparison | revisions
str.h file | annotate | diff | comparison | revisions
--- a/common.h	Sat Mar 16 03:42:04 2013 +0200
+++ b/common.h	Sat Mar 16 13:08:24 2013 +0200
@@ -52,7 +52,7 @@
 	// =========================================================================
 	// Midpoint between this vertex and another vertex.
 	vertex midpoint (vertex& other);
-	str getStringRep ();
+	str getStringRep (const bool bMangled);
 };
 
 // =============================================================================
--- a/gui.cpp	Sat Mar 16 03:42:04 2013 +0200
+++ b/gui.cpp	Sat Mar 16 13:08:24 2013 +0200
@@ -363,8 +363,8 @@
 			{
 				LDLine* line = static_cast<LDLine*> (obj);
 				zText.format ("%s, %s",
-					line->vaCoords[0].getStringRep ().chars(),
-					line->vaCoords[1].getStringRep ().chars());
+					line->vaCoords[0].getStringRep (true).chars(),
+					line->vaCoords[1].getStringRep (true).chars());
 			}
 			break;
 		
@@ -372,9 +372,9 @@
 			{
 				LDTriangle* triangle = static_cast<LDTriangle*> (obj);
 				zText.format ("%s, %s, %s",
-					triangle->vaCoords[0].getStringRep ().chars(),
-					triangle->vaCoords[1].getStringRep ().chars(),
-					triangle->vaCoords[2].getStringRep ().chars());
+					triangle->vaCoords[0].getStringRep (true).chars(),
+					triangle->vaCoords[1].getStringRep (true).chars(),
+					triangle->vaCoords[2].getStringRep (true).chars());
 			}
 			break;
 		
@@ -382,10 +382,10 @@
 			{
 				LDQuad* quad = static_cast<LDQuad*> (obj);
 				zText.format ("%s, %s, %s, %s",
-					quad->vaCoords[0].getStringRep ().chars(),
-					quad->vaCoords[1].getStringRep ().chars(),
-					quad->vaCoords[2].getStringRep ().chars(),
-					quad->vaCoords[3].getStringRep ().chars());
+					quad->vaCoords[0].getStringRep (true).chars(),
+					quad->vaCoords[1].getStringRep (true).chars(),
+					quad->vaCoords[2].getStringRep (true).chars(),
+					quad->vaCoords[3].getStringRep (true).chars());
 			}
 			break;
 		
@@ -395,11 +395,11 @@
 			break;
 		
 		case OBJ_Vector:
-			zText.format ("%s", static_cast<LDVector*> (obj)->vPos.getStringRep().chars());
+			zText.format ("%s", static_cast<LDVector*> (obj)->vPos.getStringRep(true).chars());
 			break;
 		
 		case OBJ_Vertex:
-			zText.format ("%s", static_cast<LDVertex*> (obj)->vPosition.getStringRep().chars());
+			zText.format ("%s", static_cast<LDVertex*> (obj)->vPosition.getStringRep(true).chars());
 			break;
 		
 		default:
--- a/ldtypes.cpp	Sat Mar 16 03:42:04 2013 +0200
+++ b/ldtypes.cpp	Sat Mar 16 13:08:24 2013 +0200
@@ -1,6 +1,7 @@
 #include "common.h"
 #include "ldtypes.h"
 #include "io.h"
+#include "misc.h"
 
 const char* g_saObjTypeNames[] = {
 	"unidentified",
@@ -91,49 +92,65 @@
 	return str::mkfmt ("0 %s", zText.chars ());
 }
 
+str LDSubfile::getContents () {
+	str val = str::mkfmt ("1 %d", dColor);
+	val += vPosition;
+	
+	for (short i = 0; i < 9; ++i) {
+		val += ' ';
+		val += ftoa (faMatrix[i]);
+	}
+	
+	val += ' ';
+	val += zFileName;
+	return val;
+}
+
 str LDLine::getContents () {
-	return str::mkfmt ("2 %d %f %f %f %f %f %f", dColor,
-		vaCoords[0].x, vaCoords[0].y, vaCoords[0].z,
-		vaCoords[1].x, vaCoords[1].y, vaCoords[1].z);
+	str val = str::mkfmt ("2 %d", dColor);
+	
+	for (ushort i = 0; i < 2; ++i)
+		val.appendformat (" %s", vaCoords[i].getStringRep (false).chars ());
+	
+	return val;
 }
 
 str LDTriangle::getContents () {
-	return str::mkfmt ("3 %d %f %f %f %f %f %f %f %f %f", dColor,
-		vaCoords[0].x, vaCoords[0].y, vaCoords[0].z,
-		vaCoords[1].x, vaCoords[1].y, vaCoords[1].z,
-		vaCoords[2].x, vaCoords[2].y, vaCoords[2].z);
+	str val = str::mkfmt ("3 %d", dColor);
+	
+	for (ushort i = 0; i < 3; ++i)
+		val.appendformat (" %s", vaCoords[i].getStringRep (false).chars ());
+	
+	return val;
 }
 
 str LDQuad::getContents () {
-	// Oh, Jesus.
-	return str::mkfmt ("4 %d %f %f %f %f %f %f %f %f %f %f %f %f", dColor,
-		vaCoords[0].x, vaCoords[0].y, vaCoords[0].z,
-		vaCoords[1].x, vaCoords[1].y, vaCoords[1].z,
-		vaCoords[2].x, vaCoords[2].y, vaCoords[2].z,
-		vaCoords[3].x, vaCoords[3].y, vaCoords[3].z);
+	str val = str::mkfmt ("4 %d", dColor);
+	
+	for (ushort i = 0; i < 4; ++i)
+		val.appendformat (" %s", vaCoords[i].getStringRep (false).chars ());
+	
+	return val;
 }
 
 str LDCondLine::getContents () {
-	return str::mkfmt ("5 %d %f %f %f %f %f %f %f %f %f %f %f %f", dColor,
-		vaCoords[0].x, vaCoords[0].y, vaCoords[0].z,
-		vaCoords[1].x, vaCoords[1].y, vaCoords[1].z,
-		vaControl[0].x, vaControl[0].y, vaControl[0].z,
-		vaControl[1].x, vaControl[1].y, vaControl[1].z);
+	str val = str::mkfmt ("5 %d", dColor);
+	
+	// Add the coordinates of end points
+	for (ushort i = 0; i < 2; ++i)
+		val.appendformat (" %s", vaCoords[i].getStringRep (false).chars ());
+	
+	// Add the control points
+	for (ushort i = 0; i < 2; ++i)
+		val.appendformat (" %s", vaControl[i].getStringRep (false).chars ());
+	
+	return val;
 }
 
 str LDGibberish::getContents () {
 	return zContents;
 }
 
-str LDSubfile::getContents () {
-	return str::mkfmt ("1 %d %f %f %f %f %f %f %f %f %f %f %f %f %s", dColor,
-		vPosition.x, vPosition.y, vPosition.y,
-		faMatrix[0], faMatrix[1], faMatrix[2],
-		faMatrix[3], faMatrix[4], faMatrix[5],
-		faMatrix[6], faMatrix[7], faMatrix[8],
-		zFileName.chars());
-}
-
 str LDVector::getContents () {
 	return str::mkfmt ("0 !LDFORGE VECTOR"); // TODO
 }
--- a/main.cpp	Sat Mar 16 03:42:04 2013 +0200
+++ b/main.cpp	Sat Mar 16 13:08:24 2013 +0200
@@ -2,6 +2,7 @@
 #include "gui.h"
 #include "io.h"
 #include "bbox.h"
+#include "misc.h"
 
 vector<OpenFile*> g_LoadedFiles;
 OpenFile* g_CurrentFile = NULL;
@@ -30,26 +31,13 @@
 	return mid;
 }
 
-static str getCoordinateRep (double fCoord) {
-	str zRep = str::mkfmt ("%.3f", fCoord);
-	
-	// Remove trailing zeroes
-	while (zRep[~zRep - 1] == '0')
-		zRep -= 1;
+str vertex::getStringRep (const bool bMangled) {
+	const char* sFormat = (bMangled) ? "(%s, %s, %s)" : "%s %s %s";
 	
-	// If there was only zeroes in the decimal place, remove
-	// the decimal point now.
-	if (zRep[~zRep - 1] == '.')
-		zRep -= 1;
-	
-	return zRep;
-}
-
-str vertex::getStringRep () {
-	return str::mkfmt ("(%s, %s, %s)",
-		getCoordinateRep (x).chars(),
-		getCoordinateRep (y).chars(),
-		getCoordinateRep (z).chars());
+	return str::mkfmt (sFormat,
+		ftoa (x).chars(),
+		ftoa (y).chars(),
+		ftoa (z).chars());
 }
 
 // =============================================================================
--- a/misc.cpp	Sat Mar 16 03:42:04 2013 +0200
+++ b/misc.cpp	Sat Mar 16 13:08:24 2013 +0200
@@ -34,4 +34,24 @@
 	vDest.x += (cos (fPitch) * ulLength);
 	vDest.z += (sin (fPitch) * ulLength);
 	return vDest;
+}
+
+// =============================================================================
+// str ftoa (double)
+//
+// Converts a double-precision float to a string value.
+// =============================================================================
+str ftoa (double fCoord) {
+	str zRep = str::mkfmt ("%.3f", fCoord);
+	
+	// Remove trailing zeroes
+	while (zRep[~zRep - 1] == '0')
+		zRep -= 1;
+	
+	// If there was only zeroes in the decimal place, remove
+	// the decimal point now.
+	if (zRep[~zRep - 1] == '.')
+		zRep -= 1;
+	
+	return zRep;
 }
\ No newline at end of file
--- a/misc.h	Sat Mar 16 03:42:04 2013 +0200
+++ b/misc.h	Sat Mar 16 13:08:24 2013 +0200
@@ -13,4 +13,7 @@
 vertex ParseVertex (str& s, const ushort n);
 void StripWhitespace (str& s);
 
+// Float to string
+str ftoa (double fCoord);
+
 #endif // __MISC_H__
\ No newline at end of file
--- a/str.cpp	Sat Mar 16 03:42:04 2013 +0200
+++ b/str.cpp	Sat Mar 16 13:08:24 2013 +0200
@@ -5,6 +5,8 @@
 #include <assert.h>
 // #include <initializer_list>
 #include "str.h"
+#include "common.h"
+#include "misc.h"
 
 #define ITERATE_STRING(u) \
 	for (unsigned int u = 0; u < strlen (text); u++)
@@ -463,4 +465,9 @@
 
 std::vector<str> str::operator/ (str splitstring) {return split(splitstring);}
 std::vector<str> str::operator/ (char* splitstring) {return split(splitstring);}
-std::vector<str> str::operator/ (const char* splitstring) {return split(splitstring);}
\ No newline at end of file
+std::vector<str> str::operator/ (const char* splitstring) {return split(splitstring);}
+
+str& str::operator+= (vertex vrt) {
+	appendformat ("%s", vrt.getStringRep (false).chars());
+	return *this;
+}
\ No newline at end of file
--- a/str.h	Sat Mar 16 03:42:04 2013 +0200
+++ b/str.h	Sat Mar 16 13:08:24 2013 +0200
@@ -13,6 +13,8 @@
 
 char* vdynformat (const char* csFormat, va_list vArgs, long int lSize);
 
+class vertex;
+
 // Dynamic string object, allocates memory when needed and
 // features a good bunch of manipulation methods
 class str {
@@ -164,6 +166,8 @@
 		return *this;
 	}
 	
+	str& operator+= (vertex vrt);
+	
 	str operator* (const int repcount) {
 		repeat (repcount);
 		return *this;

mercurial