Make LDMatrixObject's transform and pos members properties

Sat, 01 Jun 2013 21:36:03 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 01 Jun 2013 21:36:03 +0300
changeset 268
778eed342ee4
parent 267
95fde37e1f00
child 269
2d71227f35cb

Make LDMatrixObject's transform and pos members properties

src/addObjectDialog.cpp file | annotate | diff | comparison | revisions
src/bbox.cpp file | annotate | diff | comparison | revisions
src/bbox.h file | annotate | diff | comparison | revisions
src/file.cpp file | annotate | diff | comparison | revisions
src/gldraw.cpp file | annotate | diff | comparison | revisions
src/gui.cpp file | annotate | diff | comparison | revisions
src/gui_editactions.cpp file | annotate | diff | comparison | revisions
src/ldtypes.cpp file | annotate | diff | comparison | revisions
src/ldtypes.h file | annotate | diff | comparison | revisions
src/misc.cpp file | annotate | diff | comparison | revisions
src/types.cpp file | annotate | diff | comparison | revisions
src/types.h file | annotate | diff | comparison | revisions
--- a/src/addObjectDialog.cpp	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/addObjectDialog.cpp	Sat Jun 01 21:36:03 2013 +0300
@@ -240,40 +240,34 @@
 		layout->addWidget (sb_radSegments, 2, 4);
 		layout->addWidget (lb_radRingNum, 3, 3);
 		layout->addWidget (sb_radRingNum, 3, 4);
-		
-		if (obj)
-			for (short i = 0; i < 3; ++i)
-				dsb_coords[i]->setValue (static_cast<LDRadial*> (obj)->pos.coord (i));
 		break;
 	
 	case LDObject::Subfile:
 		layout->addWidget (tw_subfileList, 1, 1, 1, 2);
 		layout->addWidget (lb_subfileName, 2, 1);
 		layout->addWidget (le_subfileName, 2, 2);
-		
-		if (obj)
-			for (short i = 0; i < 3; ++i)
-				dsb_coords[i]->setValue (static_cast<LDSubfile*> (obj)->pos.coord (i));
 		break;
 	
 	default:
 		break;
 	}
 	
-	if (type == LDObject::Subfile || type == LDObject::Radial) {
+	if (defaults->hasMatrix ()) {
+		LDMatrixObject* mo = obj ? dynamic_cast<LDMatrixObject*> (obj) : null;
+		
 		QLabel* lb_matrix = new QLabel ("Matrix:");
 		le_matrix = new QLineEdit;
 		// le_matrix->setValidator (new QDoubleValidator);
-		matrix defval = g_identity;
+		matrix defaultMatrix = g_identity;
 		
-		if (obj) {
-			if (obj->getType () == LDObject::Subfile)
-				defval = static_cast<LDSubfile*> (obj)->transform;
-			else
-				defval = static_cast<LDRadial*> (obj)->transform;
+		if (mo) {
+			for (const Axis ax : g_Axes)
+				dsb_coords[ax]->setValue (mo->position ()[ax]);
+			
+			defaultMatrix = mo->transform ();
 		}
 		
-		le_matrix->setText (defval.stringRep ());
+		le_matrix->setText (defaultMatrix.stringRep ());
 		layout->addWidget (lb_matrix, 4, 1);
 		layout->addWidget (le_matrix, 4, 2, 1, 3);
 	}
@@ -433,13 +427,13 @@
 			LDRadial* rad = initObj<LDRadial> (obj);
 			
 			for (const Axis ax : g_Axes)
-				rad->pos[ax] = dlg.dsb_coords[ax]->value ();
+				rad->setCoordinate (ax, dlg.dsb_coords[ax]->value ());
 			
 			rad->setDivisions (dlg.cb_radHiRes->isChecked () ? hires : lores);
 			rad->setSegments (min<short> (dlg.sb_radSegments->value (), rad->divisions ()));
 			rad->setType ((LDRadial::Type) dlg.rb_radType->value ());
 			rad->setNumber (dlg.sb_radRingNum->value ());
-			rad->transform = transform;
+			rad->setTransform (transform);
 		}
 		break;
 	
@@ -458,10 +452,10 @@
 			LDSubfile* ref = initObj<LDSubfile> (obj);
 			
 			for (const Axis ax : g_Axes)
-				ref->pos[ax] = dlg.dsb_coords[ax]->value ();
+				ref->setCoordinate (ax, dlg.dsb_coords[ax]->value ());
 			
 			ref->fileName = name;
-			ref->transform = transform;
+			ref->setTransform (transform);
 			ref->fileInfo = file;
 		}
 		break;
--- a/src/bbox.cpp	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/bbox.cpp	Sat Jun 01 21:36:03 2013 +0300
@@ -110,7 +110,7 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void bbox::calcVertex (vertex v) {
+void bbox::calcVertex (const vertex& v) {
 	for (const Axis ax : g_Axes) {
 		if (v[ax] < m_v0[ax])
 			m_v0[ax] = v[ax];
--- a/src/bbox.h	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/bbox.h	Sat Jun 01 21:36:03 2013 +0300
@@ -40,7 +40,7 @@
 	void calculate ();
 	double size () const;
 	void calcObject (LDObject* obj);
-	void calcVertex (vertex v);
+	void calcVertex (const vertex& v);
 	vertex center () const;
 	
 	bbox& operator<< (LDObject* obj) {
@@ -48,7 +48,7 @@
 		return *this;
 	}
 	
-	bbox& operator<< (vertex& v) {
+	bbox& operator<< (const vertex& v) {
 		calcVertex (v);
 		return *this;
 	}
--- a/src/file.cpp	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/file.cpp	Sat Jun 01 21:36:03 2013 +0300
@@ -658,11 +658,13 @@
 					obj->setSegments (atol (tokens[5]));
 					obj->setDivisions (atol (tokens[6]));
 					obj->setNumber (atol (tokens[7]));
-					obj->pos = parseVertex (tokens, 8); // 8 - 10
+					obj->setPosition (parseVertex (tokens, 8)); // 8 - 10
 					
+					matrix transform;
 					for (short i = 0; i < 9; ++i)
-						obj->transform[i] = atof (tokens[i + 11]); // 11 - 19
+						transform[i] = atof (tokens[i + 11]); // 11 - 19
 					
+					obj->setTransform (transform);
 					return obj;
 				}
 			}
@@ -691,11 +693,13 @@
 			
 			LDSubfile* obj = new LDSubfile;
 			obj->setColor (atol (tokens[1]));
-			obj->pos = parseVertex (tokens, 2); // 2 - 4
+			obj->setPosition (parseVertex (tokens, 2)); // 2 - 4
 			
+			matrix transform;
 			for (short i = 0; i < 9; ++i)
-				obj->transform[i] = atof (tokens[i + 5]); // 5 - 13
+				transform[i] = atof (tokens[i + 5]); // 5 - 13
 			
+			obj->setTransform (transform);
 			obj->fileName = tokens[14];
 			obj->fileInfo = load;
 			return obj;
--- a/src/gldraw.cpp	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/gldraw.cpp	Sat Jun 01 21:36:03 2013 +0300
@@ -746,7 +746,7 @@
 			
 			bool oldinvert = g_glInvert;
 			
-			if (ref->transform.determinant () < 0)
+			if (ref->transform ().determinant () < 0)
 				g_glInvert = !g_glInvert;
 			
 			LDObject* prev = ref->prev ();
@@ -768,7 +768,7 @@
 			vector<LDObject*> objs = rad->decompose (true);
 			
 			bool oldinvert = g_glInvert;
-			if (rad->transform.determinant () < 0)
+			if (rad->transform ().determinant () < 0)
 				g_glInvert = !g_glInvert;
 			
 			LDObject* prev = rad->prev ();
--- a/src/gui.cpp	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/gui.cpp	Sat Jun 01 21:36:03 2013 +0300
@@ -633,12 +633,11 @@
 			{
 				LDSubfile* ref = static_cast<LDSubfile*> (obj);
 				
-				descr.format ("%s %s, (",
-					ref->fileName.chars(), ref->pos.stringRep (true).chars());
+				descr.format ("%s %s, (", ref->fileName.chars (),
+					ref->position ().stringRep (true).chars ());
 				
 				for (short i = 0; i < 9; ++i)
-					descr += fmt ("%s%s",
-						ftoa (ref->transform[i]).chars(),
+					descr += fmt ("%s%s", ftoa (ref->transform ()[i]).chars (),
 						(i != 8) ? " " : "");
 				
 				descr += ')';
@@ -657,7 +656,7 @@
 				if (rad->type () == LDRadial::Ring || rad->type () == LDRadial::Cone)
 					descr += fmt (" %d", rad->number ());
 				
-				descr += fmt (" %s", rad->pos.stringRep (true).chars ());
+				descr += fmt (" %s", rad->position ().stringRep (true).chars ());
 			}
 			break;
 		
--- a/src/gui_editactions.cpp	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/gui_editactions.cpp	Sat Jun 01 21:36:03 2013 +0300
@@ -171,8 +171,8 @@
 		
 		// Create the replacement primitive.
 		LDSubfile* prim = new LDSubfile;
-		memcpy (&prim->pos, &rad->pos, sizeof rad->pos); // inherit position
-		memcpy (&prim->transform, &rad->transform, sizeof rad->transform); // inherit matrix
+		prim->setPosition (rad->position ()); // inherit position
+		prim->setTransform (rad->transform ()); // inherit matrix
 		prim->setColor (rad->color ()); // inherit color
 		prim->fileName = name;
 		prim->fileInfo = file;
@@ -435,8 +435,8 @@
 // =============================================================================
 static void doRotate (const short l, const short m, const short n) {
 	vector<LDObject*> sel = g_win->sel ();
-	vertex origin;
 	vector<vertex*> queue;
+	const vertex rotpoint = rotPoint (sel);
 	const double angle = (pi * currentGrid ().confs[Grid::Angle]->value) / 180;
 	
 	// ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2
@@ -457,28 +457,27 @@
 		(n * n * (1 - cosangle)) + cosangle
 	});
 	
-	origin = rotPoint (sel);
-	
-	// Apply the above matrix to everything
+	// Apply the above matrix to everything - first, mark down
+	// which vertices to transform
 	for (LDObject* obj : sel) {
 		if (obj->vertices ())
 			for (short i = 0; i < obj->vertices (); ++i)
 				queue << &obj->coords[i];
 		else if (obj->hasMatrix ()) {
-			LDMatrixObject* mobj = static_cast<LDSubfile*> (obj);
-			
-			queue << &mobj->pos;
-			mobj->transform = mobj->transform * transform;
+			LDMatrixObject* mo = static_cast<LDSubfile*> (obj);
+			queue << const_cast<vertex*> (&mo->position ()); // TEMPORARY HACK
+			mo->setTransform (mo->transform () * transform);
 		} else if (obj->getType () == LDObject::Vertex)
 			queue << &static_cast<LDVertex*> (obj)->pos;
 		
 		g_win->R ()->compileObject (obj);
 	}
 	
+	// Now do the actual transformations
 	for (vertex* v : queue) {
-		v->move (-origin);
+		v->move (-rotpoint);
 		v->transform (transform, g_origin);
-		v->move (origin);
+		v->move (rotpoint);
 	}
 	
 	g_win->refresh ();
--- a/src/ldtypes.cpp	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/ldtypes.cpp	Sat Jun 01 21:36:03 2013 +0300
@@ -78,8 +78,8 @@
 }
 
 str LDSubfile::getContents () {
-	str val = fmt ("1 %d %s ", color (), pos.stringRep (false).chars ());
-	val += transform.stringRep ();
+	str val = fmt ("1 %d %s ", color (), position ().stringRep (false).chars ());
+	val += transform ().stringRep ();
 	val += ' ';
 	val += fileName;
 	return val;
@@ -238,10 +238,12 @@
 	case LDObject::Subfile:
 		{
 			LDSubfile* ref = static_cast<LDSubfile*> (obj);
+			matrix newMatrix = transform * ref->transform ();
+			vertex newpos = ref->position ();
 			
-			matrix newMatrix = transform * ref->transform;
-			ref->pos.transform (transform, pos);
-			ref->transform = newMatrix;
+			newpos.transform (transform, pos);
+			ref->setPosition (newpos);
+			ref->setTransform (newMatrix);
 		}
 		break;
 	
@@ -319,7 +321,7 @@
 		// Set the parent now so we know what inlined this.
 		obj->setParent (this);
 		
-		transformObject (obj, transform, pos, color ());
+		transformObject (obj, transform (), position (), color ());
 	}
 	
 	return objs;
@@ -461,11 +463,11 @@
 }
 
 void LDSubfile::move (vertex vect) {
-	pos += vect;
+	setPosition (position () + vect);
 }
 
 void LDRadial::move (vertex vect) {
-	pos += vect;
+	setPosition (position () + vect);
 }
 
 void LDLine::move (vertex vect) {
@@ -521,24 +523,21 @@
 			z0 = sin ((i * 2 * pi) / divisions ()),
 			z1 = sin (((i + 1) * 2 * pi) / divisions ());
 		
+		LDObject* obj = null;
+		
 		switch (type ()) {
 		case LDRadial::Circle:
 			{
 				vertex v0 (x0, 0.0f, z0),
 					v1 (x1, 0.0f, z1);
 				
-				if (applyTransform) {
-					v0.transform (transform, pos);
-					v1.transform (transform, pos);
-				}
-				
 				LDLine* line = new LDLine;
 				line->coords[0] = v0;
 				line->coords[1] = v1;
 				line->setColor (edgecolor);
 				line->setParent (this);
 				
-				objs << line;
+				obj = line;
 			}
 			break;
 		
@@ -581,22 +580,16 @@
 					v2 (x2, y2, z2),
 					v3 (x3, y3, z3);
 				
-				if (applyTransform) {
-					v0.transform (transform, pos);
-					v1.transform (transform, pos);
-					v2.transform (transform, pos);
-					v3.transform (transform, pos);
-				}
-				
 				LDQuad* quad = new LDQuad;
 				quad->coords[0] = v0;
 				quad->coords[1] = v1;
 				quad->coords[2] = v2;
 				quad->coords[3] = v3;
+				
 				quad->setColor (color ());
 				quad->setParent (this);
 				
-				objs << quad;
+				obj = quad;
 			}
 			break;
 		
@@ -616,12 +609,6 @@
 					v1 (x1, 0.0f, z1),
 					v2 (x2, 0.0f, z2);
 				
-				if (applyTransform) {
-					v0.transform (transform, pos);
-					v1.transform (transform, pos);
-					v2.transform (transform, pos);
-				}
-				
 				LDTriangle* seg = new LDTriangle;
 				seg->coords[0] = v0;
 				seg->coords[1] = v1;
@@ -629,13 +616,25 @@
 				seg->setColor (color ());
 				seg->setParent (this);
 				
-				objs << seg;
+				if (applyTransform)
+					for (int i = 0; i < 3; ++i)
+						seg->coords[i].transform (transform (), position ());
+				
+				obj = seg;
 			}
 			break;
 		
 		default:
 			break;
 		}
+		
+		if (obj) {
+			if (applyTransform)
+				for (int i = 0; i < obj->vertices (); ++i)
+					obj->coords[i].transform (transform (), position ());
+			
+			objs << obj;
+		}
 	}
 	
 	return objs;
@@ -648,7 +647,7 @@
 	return fmt ("0 !LDFORGE RADIAL %s %d %d %d %d %s %s",
 		str (radialTypeName ()).upper ().strip (' ').c (),
 		color (), segments (), divisions (), number (),
-		pos.stringRep (false).chars(), transform.stringRep().chars());
+		position ().stringRep (false).chars (), transform ().stringRep().chars ());
 }
 
 char const* g_radialNameRoots[] = {
--- a/src/ldtypes.h	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/ldtypes.h	Sat Jun 01 21:36:03 2013 +0300
@@ -57,13 +57,21 @@
 // Common code for objects with matrices
 // =============================================================================
 class LDMatrixObject {
+	PROPERTY (matrix, transform, setTransform)
+	PROPERTY (vertex, position, setPosition)
+	
 public:
 	LDMatrixObject () {}
 	LDMatrixObject (const matrix& transform, const vertex& pos) :
-		transform (transform), pos (pos) {}
+		PROP_NAME (transform) (transform), PROP_NAME (position) (pos) {}
 	
-	matrix transform;
-	vertex pos;
+	const double& setCoordinate (const Axis ax, double value) {
+		vertex v = position ();
+		v[ax] = value;
+		setPosition (v);
+		
+		return position ()[ax];
+	}
 };
 
 // =============================================================================
--- a/src/misc.cpp	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/misc.cpp	Sat Jun 01 21:36:03 2013 +0300
@@ -182,27 +182,26 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void simplify (short& dNum, short& dDenom) {
-	bool bRepeat;
+void simplify (short& numer, short& denom) {
+	bool repeat;
 	
 	do {
-		bRepeat = false;
+		repeat = false;
 		
-		for (ulong x = 0; x < NUM_PRIMES; x++) {
-			ulong i = NUM_PRIMES - x - 1;
-			ushort uPrime = g_primes[i];
+		for (ushort x = 0; x < NUM_PRIMES; x++) {
+			const ushort prime = g_primes[NUM_PRIMES - x - 1];
 			
-			if (dNum <= uPrime || dDenom <= uPrime)
+			if (numer <= prime || denom <= prime)
 				continue;
 			
-			if ((dNum % uPrime == 0) && (dDenom % uPrime == 0)) {
-				dNum /= uPrime;
-				dDenom /= uPrime;
-				bRepeat = true;
+			if ((numer % prime == 0) && (denom % prime == 0)) {
+				numer /= prime;
+				denom /= prime;
+				repeat = true;
 				break;
 			}
 		}
-	} while (bRepeat);
+	} while (repeat);
 }
 
 // =============================================================================
@@ -214,10 +213,8 @@
 	
 	// Calculate center vertex
 	for (LDObject* obj : objs) {
-		if (obj->getType () == LDObject::Subfile)
-			box << static_cast<LDSubfile*> (obj)->pos;
-		else if (obj->getType () == LDObject::Radial)
-			box << static_cast<LDRadial*> (obj)->pos;
+		if (obj->hasMatrix ())
+			box << dynamic_cast<LDMatrixObject*> (obj)->position ();
 		else
 			box << obj;
 	}
--- a/src/types.cpp	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/types.cpp	Sat Jun 01 21:36:03 2013 +0300
@@ -28,13 +28,13 @@
 }
 
 // =============================================================================
-void vertex::move (vertex other) {
+void vertex::move (const vertex& other) {
 	for (const Axis ax : g_Axes)
 		m_coords[ax] += other[ax];
 }
 
 // =============================================================================
-vertex vertex::midpoint (vertex& other) {
+vertex vertex::midpoint (const vertex& other) {
 	vertex mid;
 	
 	for (const Axis ax : g_Axes)
@@ -44,7 +44,7 @@
 }
 
 // =============================================================================
-str vertex::stringRep (const bool mangled) {
+str vertex::stringRep (bool mangled) const {
 	return fmt (mangled ? "(%s, %s, %s)" : "%s %s %s",
 		ftoa (coord (X)).chars(),
 		ftoa (coord (Y)).chars(),
@@ -104,11 +104,17 @@
 	return other /= d;
 }
 
-vertex& vertex::operator+= (vertex other) {
+vertex& vertex::operator+= (const vertex& other) {
 	move (other);
 	return *this;
 }
 
+vertex& vertex::operator+ (const vertex& other) const {
+	vertex newvert = *this;
+	newvert.move (other);
+	return newvert;
+}
+
 int vertex::operator< (const vertex& other) const {
 	if (operator== (other))
 		return false;
@@ -171,7 +177,7 @@
 }
 
 // =============================================================================
-matrix matrix::mult (matrix other) {
+matrix matrix::mult (matrix other) const {
 	matrix val;
 	val.zero ();
 	
--- a/src/types.h	Sat Jun 01 20:31:33 2013 +0300
+++ b/src/types.h	Sat Jun 01 21:36:03 2013 +0300
@@ -58,7 +58,7 @@
 	matrix (double vals[]);
 	
 	double			determinant	() const;
-	matrix			mult			(matrix other);
+	matrix			mult			(matrix other) const;
 	void			puts			() const;
 	str				stringRep		() const;
 	void			zero			();
@@ -66,7 +66,7 @@
 	const double&	val				(const uint idx) const { return m_vals[idx]; }
 	
 	matrix&			operator=		(matrix other);
-	matrix			operator*		(matrix other) { return mult (other); }
+	matrix			operator*		(matrix other) const { return mult (other); }
 	double&			operator[]		(const uint idx) { return m_vals[idx]; }
 	const double&	operator[]		(const uint idx) const { return m_vals[idx]; }
 
@@ -89,9 +89,9 @@
 	
 	double&			coord			(const ushort n) { return m_coords[n]; }
 	const double&	coord			(const ushort n) const { return m_coords[n]; }
-	vertex			midpoint		(vertex& other);
-	void			move			(vertex other);
-	str				stringRep		(const bool mangled);
+	vertex			midpoint		(const vertex& other);
+	void			move			(const vertex& other);
+	str				stringRep		(bool mangled) const;
 	void			transform		(matrix matr, vertex pos);
 	double&			x				() { return m_coords[X]; }
 	const double&	x				() const { return m_coords[X]; }
@@ -100,7 +100,8 @@
 	double&			z				() { return m_coords[Z]; }
 	const double&	z				() const { return m_coords[Z]; }
 	
-	vertex&			operator+=		(vertex other);
+	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;

mercurial