Rotation and rounding. Rotation needs work to get proper origin stuff

Sun, 21 Apr 2013 04:17:05 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sun, 21 Apr 2013 04:17:05 +0300
changeset 120
607301744394
parent 119
b93a64f5ee67
child 121
7e87c85ad092

Rotation and rounding. Rotation needs work to get proper origin stuff

gldraw.cpp file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
gui_editactions.cpp file | annotate | diff | comparison | revisions
ldtypes.h file | annotate | diff | comparison | revisions
types.h file | annotate | diff | comparison | revisions
zz_configDialog.cpp file | annotate | diff | comparison | revisions
--- a/gldraw.cpp	Sat Apr 20 04:26:36 2013 +0300
+++ b/gldraw.cpp	Sun Apr 21 04:17:05 2013 +0300
@@ -109,9 +109,9 @@
 		1.0f);
 }
 
-// ========================================================================= //
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-// ========================================================================= //
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
 static vector<short> g_daWarnedColors;
 void GLRenderer::setObjectColor (LDObject* obj) {
 	QColor qCol;
--- a/gui.cpp	Sat Apr 20 04:26:36 2013 +0300
+++ b/gui.cpp	Sun Apr 21 04:17:05 2013 +0300
@@ -70,6 +70,13 @@
 EXTERN_ACTION (moveYPos)
 EXTERN_ACTION (moveZPos)
 EXTERN_ACTION (invert)
+EXTERN_ACTION (rotateXNeg)
+EXTERN_ACTION (rotateYNeg)
+EXTERN_ACTION (rotateZNeg)
+EXTERN_ACTION (rotateXPos)
+EXTERN_ACTION (rotateYPos)
+EXTERN_ACTION (rotateZPos)
+EXTERN_ACTION (roundCoords)
 
 #ifndef RELEASE
 EXTERN_ACTION (addTestQuad)
@@ -208,6 +215,7 @@
 	qEditMenu->addAction (ACTION_NAME (setContents));		// Set Contents
 	qEditMenu->addAction (ACTION_NAME (makeBorders));		// Make Borders
 	qEditMenu->addAction (ACTION_NAME (makeCornerVerts));	// Make Corner Vertices
+	qEditMenu->addAction (ACTION_NAME (roundCoords));		// Round Coordinates
 	
 	// Move menu
 	qMoveMenu = menuBar ()->addMenu (tr ("&Move"));
@@ -220,6 +228,13 @@
 	qMoveMenu->addAction (ACTION_NAME (moveYNeg));			// Move -Y
 	qMoveMenu->addAction (ACTION_NAME (moveZPos));			// Move +Z
 	qMoveMenu->addAction (ACTION_NAME (moveZNeg));			// Move -Z
+	qMoveMenu->addSeparator ();								// -----
+	qMoveMenu->addAction (ACTION_NAME (rotateXPos));		// Rotate +X
+	qMoveMenu->addAction (ACTION_NAME (rotateXNeg));		// Rotate -X
+	qMoveMenu->addAction (ACTION_NAME (rotateYPos));		// Rotate +Y
+	qMoveMenu->addAction (ACTION_NAME (rotateYNeg));		// Rotate -Y
+	qMoveMenu->addAction (ACTION_NAME (rotateZPos));		// Rotate +Z
+	qMoveMenu->addAction (ACTION_NAME (rotateZNeg));		// Rotate -Z
 	
 	// Control menu
 	qControlMenu = menuBar ()->addMenu (tr ("&Control"));
@@ -301,6 +316,12 @@
 	ADD_TOOLBAR_ITEM (paste)
 	ADD_TOOLBAR_ITEM (del)
 	
+	initSingleToolBar ("Select");
+	ADD_TOOLBAR_ITEM (selectByColor)
+	ADD_TOOLBAR_ITEM (selectByType)
+	
+	addToolBarBreak (Qt::TopToolBarArea);
+	
 	initSingleToolBar ("Move");
 	ADD_TOOLBAR_ITEM (moveUp)
 	ADD_TOOLBAR_ITEM (moveDown)
@@ -311,11 +332,13 @@
 	ADD_TOOLBAR_ITEM (moveZPos)
 	ADD_TOOLBAR_ITEM (moveZNeg)
 	
-	addToolBarBreak (Qt::TopToolBarArea);
-	
-	initSingleToolBar ("Select");
-	ADD_TOOLBAR_ITEM (selectByColor)
-	ADD_TOOLBAR_ITEM (selectByType)
+	initSingleToolBar ("Rotate");
+	ADD_TOOLBAR_ITEM (rotateXPos)
+	ADD_TOOLBAR_ITEM (rotateXNeg)
+	ADD_TOOLBAR_ITEM (rotateYPos)
+	ADD_TOOLBAR_ITEM (rotateYNeg)
+	ADD_TOOLBAR_ITEM (rotateZPos)
+	ADD_TOOLBAR_ITEM (rotateZNeg)
 	
 	// ==========================================
 	// Color toolbar
@@ -334,6 +357,7 @@
 	ADD_TOOLBAR_ITEM (setContents)
 	ADD_TOOLBAR_ITEM (makeBorders)
 	ADD_TOOLBAR_ITEM (makeCornerVerts)
+	ADD_TOOLBAR_ITEM (roundCoords)
 	
 	updateToolBars ();
 }
--- a/gui_editactions.cpp	Sat Apr 20 04:26:36 2013 +0300
+++ b/gui_editactions.cpp	Sun Apr 21 04:17:05 2013 +0300
@@ -23,6 +23,7 @@
 #include "zz_colorSelectDialog.h"
 #include "zz_historyDialog.h"
 #include "zz_setContentsDialog.h"
+#include "misc.h"
 
 vector<LDObject*> g_Clipboard;
 
@@ -374,11 +375,11 @@
 	g_ForgeWindow->buildObjList ();
 }
 
-ACTION (moveUp, "Move Up", "arrow-up", "Move the current selection up.", CTRL (Up)) {
+ACTION (moveUp, "Move Up", "arrow-up", "Move the current selection up.", SHIFT (Up)) {
 	doMoveSelection (true);
 }
 
-ACTION (moveDown, "Move Down", "arrow-down", "Move the current selection down.", CTRL (Down)) {
+ACTION (moveDown, "Move Down", "arrow-down", "Move the current selection down.", SHIFT (Down)) {
 	doMoveSelection (false);
 }
 
@@ -560,4 +561,111 @@
 		History::addEntry (new ComboHistory (paHistory));
 		g_ForgeWindow->refresh ();
 	}
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+static void doRotate (const short l, const short m, const short n) {
+	std::vector<LDObject*> sel = g_ForgeWindow->selection ();
+	const double angle = (pi * 22.5f) / 360; // TODO
+	
+	// ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2
+	matrix transform (
+		(l * l * (1 - cos (angle))) + cos (angle),
+		(m * l * (1 - cos (angle))) - (n * sin (angle)),
+		(n * l * (1 - cos (angle))) + (m * sin (angle)),
+		
+		(l * m * (1 - cos (angle))) + (n * sin (angle)),
+		(m * m * (1 - cos (angle))) + cos (angle),
+		(n * m * (1 - cos (angle))) - (l * sin (angle)),
+		
+		(l * n * (1 - cos (angle))) - (m * sin (angle)),
+		(m * n * (1 - cos (angle))) + (l * sin (angle)),
+		(n * n * (1 - cos (angle))) + cos (angle)
+	);
+	
+	// Calculate center vertex
+	vertex origin;
+	short numverts = 0;
+	
+	for (LDObject* obj : sel) {
+		for (short i = 0; i < obj->vertices (); ++i) {
+			origin.x += obj->vaCoords[i].x;
+			origin.y += obj->vaCoords[i].y;
+			origin.z += obj->vaCoords[i].z;
+			numverts++;
+		}
+	}
+	
+	origin.x /= numverts;
+	origin.y /= numverts;
+	origin.z /= numverts;
+	
+	std::vector<vertex*> verticesToTransform;
+	
+	// Apply the above matrix to everything
+	for (LDObject* obj : sel) {
+		if (obj->vertices ())
+			for (short i = 0; i < obj->vertices (); ++i)
+				verticesToTransform.push_back (&obj->vaCoords[i]);
+		else if (obj->getType () == OBJ_Subfile)
+			verticesToTransform.push_back (&(static_cast<LDSubfile*> (obj)->vPosition));
+		else if (obj->getType () == OBJ_Radial)
+			verticesToTransform.push_back (&(static_cast<LDRadial*> (obj)->vPosition));
+		else if (obj->getType () == OBJ_Vertex)
+			verticesToTransform.push_back (&(static_cast<LDVertex*> (obj)->vPosition));
+	}
+	
+	for (vertex* v : verticesToTransform) {
+		v->transform (transform, origin);
+	}
+	
+	g_ForgeWindow->refresh ();
+}
+
+ACTION (rotateXPos, "Rotate +X", "rotate-x-pos", "Rotate objects around X axis", CTRL (Right)) {
+	doRotate (1, 0, 0);
+}
+
+ACTION (rotateYPos, "Rotate +Y", "rotate-y-pos", "Rotate objects around Y axis", CTRL (PageDown)) {
+	doRotate (0, 1, 0);
+}
+
+ACTION (rotateZPos, "Rotate +Z", "rotate-z-pos", "Rotate objects around Z axis", CTRL (Up)) {
+	doRotate (0, 0, 1);
+}
+
+ACTION (rotateXNeg, "Rotate -X", "rotate-x-neg", "Rotate objects around X axis", CTRL (Left)) {
+	doRotate (-1, 0, 0);
+}
+
+ACTION (rotateYNeg, "Rotate -Y", "rotate-y-neg", "Rotate objects around Y axis", CTRL (PageUp)) {
+	doRotate (0, -1, 0);
+}
+
+ACTION (rotateZNeg, "Rotate -Z", "rotate-z-neg", "Rotate objects around Z axis", CTRL (Down)) {
+	doRotate (0, 0, -1);
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+ACTION (roundCoords, "Round Coordinates", "round-coords", "Round coordinates down to 3/4 decimals", (0)) {
+	setlocale (LC_ALL, "C");
+	
+	for (LDObject* obj : g_ForgeWindow->selection ()) {
+		for (short i = 0; i < obj->vertices (); ++i) {
+			double* coords[3] = {
+				&obj->vaCoords[i].x,
+				&obj->vaCoords[i].y,
+				&obj->vaCoords[i].z,
+			};
+			
+			for (double* coord : coords)
+				*coord = atof (format ("%.3f", *coord));
+		}
+	}
+	
+	g_ForgeWindow->refresh ();
 }
\ No newline at end of file
--- a/ldtypes.h	Sat Apr 20 04:26:36 2013 +0300
+++ b/ldtypes.h	Sun Apr 21 04:17:05 2013 +0300
@@ -22,17 +22,18 @@
 #include "common.h"
 #include "types.h"
 
-#define IMPLEMENT_LDTYPE(N) \
-	LD##N (); \
-	virtual ~LD##N (); \
+#define IMPLEMENT_LDTYPE(T, NUMVERTS) \
+	LD##T (); \
+	virtual ~LD##T (); \
 	virtual LDObjectType_e getType () const { \
-		return OBJ_##N; \
+		return OBJ_##T; \
 	} \
 	virtual str getContents (); \
-	virtual LD##N* clone () { \
-		return new LD##N (*this); \
+	virtual LD##T* clone () { \
+		return new LD##T (*this); \
 	} \
-	virtual void move (vertex vVector);
+	virtual void move (vertex vVector); \
+	virtual short vertices () const { return NUMVERTS; }
 
 class QTreeWidgetItem;
 class LDSubfile;
@@ -81,6 +82,9 @@
 	// OpenGL list for this object
 	uint uGLList, uGLPickList;
 	
+	// Vertices of this object
+	vertex vaCoords[4];
+	
 	// Object this object was referenced from, if any
 	LDObject* parent;
 	
@@ -112,6 +116,9 @@
 	// What object in the current file ultimately references this?
 	LDObject* topLevelParent ();
 	
+	// Number of vertices this object has
+	virtual short vertices () const { return 0; }
+	
 	static void moveObjects (std::vector<LDObject*> objs, const bool bUp);
 	static str objectListContents (const std::vector<LDObject*>& objs);
 	
@@ -128,7 +135,7 @@
 // =============================================================================
 class LDGibberish : public LDObject {
 public:
-	IMPLEMENT_LDTYPE (Gibberish)
+	IMPLEMENT_LDTYPE (Gibberish, 0)
 	
 	LDGibberish (str _zContent, str _zReason);
 	
@@ -146,7 +153,7 @@
 // =============================================================================
 class LDEmpty : public LDObject {
 public:
-	IMPLEMENT_LDTYPE (Empty)
+	IMPLEMENT_LDTYPE (Empty, 0)
 };
 
 // =============================================================================
@@ -157,7 +164,7 @@
 // =============================================================================
 class LDComment : public LDObject {
 public:
-	IMPLEMENT_LDTYPE (Comment)
+	IMPLEMENT_LDTYPE (Comment, 0)
 	LDComment (str zText) : zText (zText) {}
 	
 	str zText; // The text of this comment
@@ -181,7 +188,7 @@
 		NumStatements
 	};
 	
-	IMPLEMENT_LDTYPE (BFC)
+	IMPLEMENT_LDTYPE (BFC, 0)
 	LDBFC (const LDBFC::Type eType) : eStatement (eType) {}
 	
 	// Statement strings
@@ -197,7 +204,7 @@
 // =============================================================================
 class LDSubfile : public LDObject {
 public:
-	IMPLEMENT_LDTYPE (Subfile)
+	IMPLEMENT_LDTYPE (Subfile, 0)
 	
 	vertex vPosition; // Position of the subpart
 	matrix mMatrix; // Transformation matrix for the subpart
@@ -217,10 +224,8 @@
 // =============================================================================
 class LDLine : public LDObject {
 public:
-	IMPLEMENT_LDTYPE (Line)
+	IMPLEMENT_LDTYPE (Line, 2)
 	LDLine (vertex v1, vertex v2);
-	
-	vertex vaCoords[2]; // End points of this line
 };
 
 // =============================================================================
@@ -231,9 +236,7 @@
 // =============================================================================
 class LDCondLine : public LDLine {
 public:
-	IMPLEMENT_LDTYPE (CondLine)
-	
-	vertex vaCoords[4]; // End points + control points of this line
+	IMPLEMENT_LDTYPE (CondLine, 4)
 };
 
 // =============================================================================
@@ -245,9 +248,7 @@
 // =============================================================================
 class LDTriangle : public LDObject {
 public:
-	IMPLEMENT_LDTYPE (Triangle)
-	
-	vertex vaCoords[3];
+	IMPLEMENT_LDTYPE (Triangle, 3)
 	
 	LDTriangle (vertex _v0, vertex _v1, vertex _v2) {
 		vaCoords[0] = _v0;
@@ -264,9 +265,7 @@
 // =============================================================================
 class LDQuad : public LDObject {
 public:
-	IMPLEMENT_LDTYPE (Quad)
-	
-	vertex vaCoords[4];
+	IMPLEMENT_LDTYPE (Quad, 4)
 	
 	// Split this quad into two triangles
 	vector<LDTriangle*> splitToTriangles ();
@@ -282,7 +281,7 @@
 // =============================================================================
 class LDVertex : public LDObject {
 public:
-	IMPLEMENT_LDTYPE (Vertex)
+	IMPLEMENT_LDTYPE (Vertex, 0) // TODO: move vPosition to vaCoords[0]
 	
 	vertex vPosition;
 };
@@ -308,7 +307,7 @@
 		NumTypes
 	};
 	
-	IMPLEMENT_LDTYPE (Radial)
+	IMPLEMENT_LDTYPE (Radial, 0)
 	
 	LDRadial::Type eRadialType;
 	vertex vPosition;
--- a/types.h	Sat Apr 20 04:26:36 2013 +0300
+++ b/types.h	Sun Apr 21 04:17:05 2013 +0300
@@ -25,11 +25,15 @@
 		z = fZ;
 	}
 	
-	// =========================================================================
-	vertex& operator+= (vertex& other) {
+	void move (vertex other) {
 		x += other.x;
 		y += other.y;
 		z += other.z;
+	}
+	
+	// =========================================================================
+	vertex& operator+= (vertex other) {
+		move (other);
 		return *this;
 	}
 	
--- a/zz_configDialog.cpp	Sat Apr 20 04:26:36 2013 +0300
+++ b/zz_configDialog.cpp	Sun Apr 21 04:17:05 2013 +0300
@@ -171,9 +171,6 @@
 	for (actionmeta meta : g_ActionMeta) {
 		QAction* const qAct = *meta.qAct;
 		
-		if (qAct->isEnabled() == false)
-			continue;
-		
 		QListWidgetItem* qItem = new QListWidgetItem;
 		setShortcutText (qItem, meta);
 		qItem->setIcon (qAct->icon ());

mercurial