- added LDSharedVertex class for holding copy-on-write vertices. Will need this for future features..

Fri, 18 Oct 2013 23:49:55 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 18 Oct 2013 23:49:55 +0300
changeset 516
d3373bc7ca3b
parent 515
a0ad72800b96
child 517
2b5adb45aaa5

- added LDSharedVertex class for holding copy-on-write vertices. Will need this for future features..
- fixed zoomToFit() crashes for real this time

src/addObjectDialog.cpp file | annotate | diff | comparison | revisions
src/gldraw.cpp file | annotate | diff | comparison | revisions
src/gldraw.h file | annotate | diff | comparison | revisions
src/ldtypes.cpp file | annotate | diff | comparison | revisions
src/ldtypes.h file | annotate | diff | comparison | revisions
--- a/src/addObjectDialog.cpp	Fri Oct 18 22:30:08 2013 +0300
+++ b/src/addObjectDialog.cpp	Fri Oct 18 23:49:55 2013 +0300
@@ -320,13 +320,13 @@
 		return;
 
 	if (type == LDObject::Subfile)
-	{	QStringList matrixstrvals = dlg.le_matrix->text().split (" ");
+	{	QStringList matrixstrvals = dlg.le_matrix->text().split (" ", QString::SkipEmptyParts);
 
 		if (matrixstrvals.size() == 9)
 		{	double matrixvals[9];
 			int i = 0;
 
-		for (str val : matrixstrvals)
+			for (str val : matrixstrvals)
 				matrixvals[i++] = val.toFloat();
 
 			transform = matrix (matrixvals);
--- a/src/gldraw.cpp	Fri Oct 18 22:30:08 2013 +0300
+++ b/src/gldraw.cpp	Fri Oct 18 23:49:55 2013 +0300
@@ -107,10 +107,10 @@
 	m_editMode = Select;
 	m_rectdraw = false;
 	m_panning = false;
-	m_firstResize = true;
 	setFile (null);
 	setDrawOnly (false);
 	setMessageLog (null);
+	m_width = m_height = -1;
 
 	m_toolTipTimer = new QTimer (this);
 	m_toolTipTimer->setSingleShot (true);
@@ -351,14 +351,6 @@
 {	m_width = w;
 	m_height = h;
 
-	// If this is the first call to resizeGL, reset the angles. We cannot call
-	// resetAngles() in the initializer because it does not know m_width or m_height,
-	// which zoomToFit() must know.
-	if (m_firstResize)
-	{	m_firstResize = false;
-		resetAngles();
-	}
-
 	calcCameraIcons();
 
 	glViewport (0, 0, w, h);
@@ -801,7 +793,7 @@
 
 	m_knownVerts.clear();
 
-for (LDObject * obj : file()->objects())
+	for (LDObject* obj : file()->objects())
 		compileObject (obj);
 
 	// Compile axes
@@ -810,7 +802,7 @@
 	glNewList (m_axeslist, GL_COMPILE);
 	glBegin (GL_LINES);
 
-for (const LDGLAxis & ax : g_GLAxes)
+	for (const LDGLAxis& ax : g_GLAxes)
 	{	qglColor (ax.col);
 		compileVertex (ax.vert);
 		compileVertex (-ax.vert);
@@ -827,14 +819,14 @@
 void GLRenderer::compileSubObject (LDObject* obj, const GLenum gltype)
 {	glBegin (gltype);
 
-	const short numverts = (obj->getType() != LDObject::CndLine) ? obj->vertices() : 2;
+	const int numverts = (obj->getType() != LDObject::CndLine) ? obj->vertices() : 2;
 
 	if (g_glInvert == false)
-		for (short i = 0; i < numverts; ++i)
-			compileVertex (obj->m_coords[i]);
+		for (int i = 0; i < numverts; ++i)
+			compileVertex (obj->getVertex (i));
 	else
-		for (short i = numverts - 1; i >= 0; --i)
-			compileVertex (obj->m_coords[i]);
+		for (int i = numverts - 1; i >= 0; --i)
+			compileVertex (obj->getVertex (i));
 
 	glEnd();
 }
@@ -1755,7 +1747,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void GLRenderer::zoomToFit()
-{	if (file() == null)
+{	if (file() == null || m_width == -1 || m_height == -1)
 	{	setZoom (30.0f);
 		return;
 	}
--- a/src/gldraw.h	Fri Oct 18 22:30:08 2013 +0300
+++ b/src/gldraw.h	Fri Oct 18 23:49:55 2013 +0300
@@ -167,7 +167,6 @@
 		LDGLOverlay           m_overlays[6];
 		QList<vertex>         m_knownVerts;
 		bool                  m_panning;
-		bool                  m_firstResize;
 
 		void           addDrawnVertex (vertex m_hoverpos);
 		void           calcCameraIcons();                                      // Compute geometry for camera icons
--- a/src/ldtypes.cpp	Fri Oct 18 22:30:08 2013 +0300
+++ b/src/ldtypes.cpp	Fri Oct 18 23:49:55 2013 +0300
@@ -42,10 +42,13 @@
 	m_file (null),
 	qObjListEntry (null),
 	m_glinit (false)
-{	// Determine ID
+{
+	memset (m_coords, 0, sizeof m_coords);
+
+	// Determine ID
 	int32 id = 1; // 0 is invalid
 
-for (LDObject * obj : g_LDObjects)
+	for (LDObject* obj : g_LDObjects)
 		if (obj->id() >= id)
 			id = obj->id() + 1;
 
@@ -661,7 +664,7 @@
 // It takes care of history management so we can capture low-level changes, this
 // makes history stuff work out of the box.
 // -----------------------------------------------------------------------------
-template<class T> void changeProperty (LDObject* obj, T* ptr, const T& val)
+template<class T> static void changeProperty (LDObject* obj, T* ptr, const T& val)
 {	long idx;
 
 	if (obj->file() && (idx = obj->getIndex()) != -1)
@@ -688,21 +691,17 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 const vertex& LDObject::getVertex (int i) const
-{	return m_coords[i];
+{	return m_coords[i]->data();
 }
 
 void LDObject::setVertex (int i, const vertex& vert)
-{	changeProperty (this, &m_coords[i], vert);
+{	changeProperty (this, &m_coords[i], LDSharedVertex::getSharedVertex (vert));
 }
 
 // =============================================================================
 // -----------------------------------------------------------------------------
-READ_ACCESSOR (vertex, LDMatrixObject::position)
-{	return m_position;
-}
-
-SET_ACCESSOR (vertex, LDMatrixObject::setPosition)
-{	changeProperty (linkPointer(), &m_position, val);
+void LDMatrixObject::setPosition (const vertex& a)
+{	changeProperty (linkPointer(), &m_position, LDSharedVertex::getSharedVertex (a));
 }
 
 // =============================================================================
@@ -714,3 +713,36 @@
 SET_ACCESSOR (matrix, LDMatrixObject::setTransform)
 {	changeProperty (linkPointer(), &m_transform, val);
 }
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+static QMap<vertex, LDSharedVertex*> g_sharedVerts;
+
+LDSharedVertex* LDSharedVertex::getSharedVertex (const vertex& a)
+{	auto it = g_sharedVerts.find (a);
+
+	if (it == g_sharedVerts.end())
+	{	LDSharedVertex* v = new LDSharedVertex (a);
+		g_sharedVerts[a] = v;
+		return v;
+	}
+
+	return *it;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void LDSharedVertex::addRef(LDObject* a)
+{	m_refs << a;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void LDSharedVertex::delRef (LDObject* a)
+{	m_refs.removeOne (a);
+
+	if (m_refs.empty())
+	{	g_sharedVerts.remove (m_data);
+		delete this;
+	}
+}
\ No newline at end of file
--- a/src/ldtypes.h	Fri Oct 18 22:30:08 2013 +0300
+++ b/src/ldtypes.h	Fri Oct 18 23:49:55 2013 +0300
@@ -52,6 +52,7 @@
 class QListWidgetItem;
 class LDSubfile;
 class LDFile;
+class LDSharedVertex;
 
 // =============================================================================
 // LDObject
@@ -95,7 +96,7 @@
 		}
 		long getIndex() const;                      // Index (i.e. line number) of this object
 		virtual LDObject::Type getType() const;     // Type enumerator of this object
-		const vertex& getVertex (int i) const;      // Get a vertex by index
+		const vertex& getVertex (int i) const;    // Get a vertex by index
 		virtual bool hasMatrix() const;             // Does this object have a matrix and position? (see LDMatrixObject)
 		virtual void invert();                      // Inverts this object (winding is reversed)
 		virtual bool isColored() const;             // Is this object colored?
@@ -132,7 +133,35 @@
 		friend class GLRenderer;
 
 	private:
-		vertex m_coords[4];
+		LDSharedVertex* m_coords[4];
+};
+
+// =============================================================================
+// LDSharedVertex
+//
+// For use as coordinates of LDObjects. Keeps count of references.
+// -----------------------------------------------------------------------------
+class LDSharedVertex
+{	public:
+		inline const vertex& data() const
+		{	return m_data;
+		}
+
+		inline operator const vertex&() const
+		{	return m_data;
+		}
+
+		void addRef (LDObject* a);
+		void delRef (LDObject* a);
+
+		static LDSharedVertex* getSharedVertex (const vertex& a);
+
+	protected:
+		LDSharedVertex (const vertex& a) : m_data (a) {}
+
+	private:
+		QList<LDObject*> m_refs;
+		vertex m_data;
 };
 
 // =============================================================================
@@ -152,13 +181,18 @@
 // =============================================================================
 class LDMatrixObject
 {	DECLARE_PROPERTY (matrix, transform, setTransform)
-	DECLARE_PROPERTY (vertex, position, setPosition)
 	PROPERTY (LDObject*, linkPointer, setLinkPointer)
 
 	public:
 		LDMatrixObject() {}
 		LDMatrixObject (const matrix& transform, const vertex& pos) :
-			PROP_NAME (transform) (transform), PROP_NAME (position) (pos) {}
+			m_transform (transform), m_position (LDSharedVertex::getSharedVertex (pos)) {}
+
+		const vertex& position() const
+		{	return m_position->data();
+		}
+
+		void setPosition (const vertex& a);
 
 		const double& setCoordinate (const Axis ax, double value)
 		{	vertex v = position();
@@ -167,6 +201,9 @@
 
 			return position() [ax];
 		}
+
+	private:
+		LDSharedVertex* m_position;
 };
 
 // =============================================================================

mercurial