From 199fd6d577b914d083db9dd845e0b7f94960eac3 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sun, 3 Apr 2011 20:50:45 +0000 Subject: Essais graphiques, modifications du VisitorPattern. --- src/gui/ElementRenderer.cc | 40 ++++++++++++++++++++++++++++ src/gui/ElementRenderer.h | 37 ++++++++++++++++++++++++++ src/gui/GElement.cc | 21 --------------- src/gui/GElement.h | 28 -------------------- src/gui/GLWidget.cc | 14 +++++++--- src/gui/Makefile | 19 +++++++++++--- src/gui/graphicals.h | 35 ------------------------- src/gui/gui.pro | 4 +-- src/gui/util.cc | 42 +++++++++++++++++++++++++++++ src/gui/util.h | 28 ++++++++++++++++++++ src/main/CompositeElement.h | 12 +++++++-- src/main/CurvedElement.h | 5 ++++ src/main/ElementVisitor.h | 2 -- src/test/ElementTest.cc | 6 +++++ src/test/VisitorTest.cc | 64 +++++++++++++++++++++++++++++++++++++++++++++ 15 files changed, 260 insertions(+), 97 deletions(-) create mode 100644 src/gui/ElementRenderer.cc create mode 100644 src/gui/ElementRenderer.h delete mode 100644 src/gui/GElement.cc delete mode 100644 src/gui/GElement.h delete mode 100644 src/gui/graphicals.h create mode 100644 src/gui/util.cc create mode 100644 src/gui/util.h create mode 100644 src/test/VisitorTest.cc diff --git a/src/gui/ElementRenderer.cc b/src/gui/ElementRenderer.cc new file mode 100644 index 0000000..6f28c13 --- /dev/null +++ b/src/gui/ElementRenderer.cc @@ -0,0 +1,40 @@ +/* + * ElementRenderer.cc + * + * Created on: Apr 2, 2011 + * Author: jakob + */ + +#include "ElementRenderer.h" +#include "Vector3D.h" + +namespace vhc { +using namespace util; + +ElementRenderer::ElementRenderer() { + // TODO Auto-generated constructor stub + +} + +ElementRenderer::~ElementRenderer() { + // TODO Auto-generated destructor stub +} + + +void ElementRenderer::visit(StraightElement* straight) { + +} + +void ElementRenderer::visit(Quadrupole* quadrupole) { + +} + +void ElementRenderer::visit(CurvedElement* curved) { + +} + +void ElementRenderer::visit(Dipole* dipole) { + +} + +} diff --git a/src/gui/ElementRenderer.h b/src/gui/ElementRenderer.h new file mode 100644 index 0000000..9d6ff75 --- /dev/null +++ b/src/gui/ElementRenderer.h @@ -0,0 +1,37 @@ +/* + * ElementRenderer.h + * + * Created on: Apr 2, 2011 + * Author: jakob + */ + +#ifndef ELEMENTRENDERER_H_ +#define ELEMENTRENDERER_H_ + +#include "ElementVisitor.h" + +namespace vhc { + + +class ElementRenderer: public ElementVisitor { + +public: + + ElementRenderer(); + + virtual ~ElementRenderer(); + + virtual void visit(StraightElement* straight); + + virtual void visit(Quadrupole* quadrupole); + + virtual void visit(CurvedElement* curved); + + virtual void visit(Dipole* dipole); + +}; + + +} + +#endif /* ELEMENTRENDERER_H_ */ diff --git a/src/gui/GElement.cc b/src/gui/GElement.cc deleted file mode 100644 index cbb3cae..0000000 --- a/src/gui/GElement.cc +++ /dev/null @@ -1,21 +0,0 @@ -/* - * GElement.cc - * - * Created on: Mar 31, 2011 - * Author: jakob - */ - -#include "GElement.h" - -namespace vhc { - -GElement::GElement() { - // TODO Auto-generated constructor stub - -} - -GElement::~GElement() { - // TODO Auto-generated destructor stub -} - -} diff --git a/src/gui/GElement.h b/src/gui/GElement.h deleted file mode 100644 index a62ff74..0000000 --- a/src/gui/GElement.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * GElement.h - * - * Created on: Mar 31, 2011 - * Author: jakob - */ - -#ifndef GELEMENT_H_ -#define GELEMENT_H_ -#include "Element.h" - -namespace vhc { - -class GElement { - - Element& element; - -public: - GElement(Element& element); - - - virtual ~GElement(); - -}; - -} - -#endif /* GELEMENT_H_ */ diff --git a/src/gui/GLWidget.cc b/src/gui/GLWidget.cc index ee2bb59..391c5f5 100644 --- a/src/gui/GLWidget.cc +++ b/src/gui/GLWidget.cc @@ -1,15 +1,16 @@ #include #include #include "GLWidget.h" +#include "util.h" -void torus(int numc, int numt, double R = 1, double r = 0.1) { +void torus(int numc, int numt, double fraction = 1 , double R = 1, double r = 0.1) { int i, j, k; double s, t, x, y, z, twopi; twopi = 2 * M_PI; for (i = 0; i < numc; i++) { glBegin(GL_QUAD_STRIP); - for (j = 0; j <= numt ; j++) { + for (j = 0; j <= numt / fraction ; j++) { for (k = 1; k >= 0; k--) { s = (i + k) % numc + 0.5; t = j % numt; @@ -25,6 +26,8 @@ void torus(int numc, int numt, double R = 1, double r = 0.1) { } } + + void axes() { glBegin(GL_LINES); glColor3d(1, 0, 0); @@ -101,9 +104,13 @@ void GLWidget::paintGL () { glPolygonMode(GL_FRONT, GL_LINE); glPolygonMode(GL_BACK, GL_LINE); - torus(12, 20, 1, 0.2); + torus(12, 20, 4, 1, 0.1); + + vhc::util::cylinder(0.1, 0, 2, 12, 20); + glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_FILL); + } void GLWidget::resizeGL (int width, int height) { @@ -149,4 +156,5 @@ void GLWidget::keyPressEvent (QKeyEvent* event) { } void GLWidget::keyReleaseEvent (QKeyEvent* event) { + } diff --git a/src/gui/Makefile b/src/gui/Makefile index e1f37a2..cd4970a 100644 --- a/src/gui/Makefile +++ b/src/gui/Makefile @@ -1,6 +1,6 @@ ############################################################################# # Makefile for building: $(BINDIR)/gui/gui -# Generated by qmake (2.01a) (Qt 4.7.0) on: Sat Apr 2 21:37:01 2011 +# Generated by qmake (2.01a) (Qt 4.7.0) on: Sun Apr 3 21:53:36 2011 # Project: gui.pro # Template: app # Command: /usr/bin/qmake -o Makefile gui.pro @@ -45,10 +45,14 @@ OBJECTS_DIR = $(BINDIR)/gui/ SOURCES = Main.cc \ GLWidget.cc \ - Camera.cc moc_GLWidget.cpp + Camera.cc \ + ElementRenderer.cc \ + util.cc moc_GLWidget.cpp OBJECTS = $(BINDIR)/gui/Main.o \ $(BINDIR)/gui/GLWidget.o \ $(BINDIR)/gui/Camera.o \ + $(BINDIR)/gui/ElementRenderer.o \ + $(BINDIR)/gui/util.o \ $(BINDIR)/gui/moc_GLWidget.o DIST = /usr/share/qt4/mkspecs/common/g++.conf \ /usr/share/qt4/mkspecs/common/unix.conf \ @@ -158,7 +162,7 @@ qmake: FORCE dist: @$(CHK_DIR_EXISTS) $(BINDIR)/gui/gui1.0.0 || $(MKDIR) $(BINDIR)/gui/gui1.0.0 - $(COPY_FILE) --parents $(SOURCES) $(DIST) $(BINDIR)/gui/gui1.0.0/ && $(COPY_FILE) --parents GLWidget.h Camera.h $(BINDIR)/gui/gui1.0.0/ && $(COPY_FILE) --parents Main.cc GLWidget.cc Camera.cc $(BINDIR)/gui/gui1.0.0/ && (cd `dirname $(BINDIR)/gui/gui1.0.0` && $(TAR) gui1.0.0.tar gui1.0.0 && $(COMPRESS) gui1.0.0.tar) && $(MOVE) `dirname $(BINDIR)/gui/gui1.0.0`/gui1.0.0.tar.gz . && $(DEL_FILE) -r $(BINDIR)/gui/gui1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) $(BINDIR)/gui/gui1.0.0/ && $(COPY_FILE) --parents GLWidget.h Camera.h ElementRenderer.h util.h $(BINDIR)/gui/gui1.0.0/ && $(COPY_FILE) --parents Main.cc GLWidget.cc Camera.cc ElementRenderer.cc util.cc $(BINDIR)/gui/gui1.0.0/ && (cd `dirname $(BINDIR)/gui/gui1.0.0` && $(TAR) gui1.0.0.tar gui1.0.0 && $(COMPRESS) gui1.0.0.tar) && $(MOVE) `dirname $(BINDIR)/gui/gui1.0.0`/gui1.0.0.tar.gz . && $(DEL_FILE) -r $(BINDIR)/gui/gui1.0.0 clean:compiler_clean @@ -207,12 +211,19 @@ compiler_clean: compiler_moc_header_clean $(BINDIR)/gui/Main.o: Main.cc GLWidget.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/Main.o Main.cc -$(BINDIR)/gui/GLWidget.o: GLWidget.cc GLWidget.h +$(BINDIR)/gui/GLWidget.o: GLWidget.cc GLWidget.h \ + util.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/GLWidget.o GLWidget.cc $(BINDIR)/gui/Camera.o: Camera.cc Camera.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/Camera.o Camera.cc +$(BINDIR)/gui/ElementRenderer.o: ElementRenderer.cc ElementRenderer.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/ElementRenderer.o ElementRenderer.cc + +$(BINDIR)/gui/util.o: util.cc + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/util.o util.cc + $(BINDIR)/gui/moc_GLWidget.o: moc_GLWidget.cpp $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/moc_GLWidget.o moc_GLWidget.cpp diff --git a/src/gui/graphicals.h b/src/gui/graphicals.h deleted file mode 100644 index c944e7e..0000000 --- a/src/gui/graphicals.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * graphicals.h - * - * Created on: Apr 2, 2011 - * Author: jakob - */ - -#ifndef GRAPHICALS_H_ -#define GRAPHICALS_H_ -#include "Element.h" - -namespace vhc { - -class Renderable { - -public: - - Renderable() {}; - - virtual ~Renderable() {}; - - virtual void render() const = 0; - -}; - -template ; -class Graphical: public Renderable { -private: - T* element -}; - - -} - -#endif /* GRAPHICALS_H_ */ diff --git a/src/gui/gui.pro b/src/gui/gui.pro index fc82d3f..ee15909 100644 --- a/src/gui/gui.pro +++ b/src/gui/gui.pro @@ -10,5 +10,5 @@ LIBS += -L$(BINDIR)/main -lvhc QT += opengl # Input -HEADERS += GLWidget.h Camera.h -SOURCES += Main.cc GLWidget.cc Camera.cc +HEADERS += GLWidget.h Camera.h ElementRenderer.h util.h +SOURCES += Main.cc GLWidget.cc Camera.cc ElementRenderer.cc util.cc diff --git a/src/gui/util.cc b/src/gui/util.cc new file mode 100644 index 0000000..a92d5be --- /dev/null +++ b/src/gui/util.cc @@ -0,0 +1,42 @@ +/* + * util.cc + * + * Created on: Apr 2, 2011 + * Author: jakob + */ +#include + +namespace vhc { +namespace util { + +void torus(double R, double r, double fraction, int slices, int stacks) { + int i, j, k; + double s, t, x, y, z, twopi; + + twopi = 2 * M_PI; + for (i = 0; i < slices; i++) { + glBegin(GL_QUAD_STRIP); + for (j = 0; j <= stacks * fraction ; j++) { + for (k = 1; k >= 0; k--) { + s = (i + k) % slices + 0.5; + t = j % stacks; + + x = (R+r*cos(s*twopi/slices))*cos(t*twopi/stacks); + y = (R+r*cos(s*twopi/slices))*sin(t*twopi/stacks); + z = r * sin(s * twopi / slices); + glColor3d(0, s/slices, t/slices); + glVertex3f(x, y, z); + } + } + glEnd(); + } +} + +void cylinder(double base, double top, double height, int slices, int stacks) { + GLUquadric* q = gluNewQuadric(); + gluCylinder(q, base, top, height, slices, stacks); + gluDeleteQuadric(q); +} + +} +} diff --git a/src/gui/util.h b/src/gui/util.h new file mode 100644 index 0000000..e1f7609 --- /dev/null +++ b/src/gui/util.h @@ -0,0 +1,28 @@ +/* + * util.h + * + * Created on: Apr 3, 2011 + * Author: jakob + */ + +#ifndef UTIL_H_ +#define UTIL_H_ + +namespace vhc { +namespace util { + +/** Dessine un tore autour de l'origine* + * @param R rayon du tore + * @param r rayon du "cylindre" du tore + * @param fraction fraction de tore a dessiner (1 = un tour) + * @param slices nombre de cotes du "cylindre" du tore + * @param stacks nombre de cotes du tore */ +void torus(double R, double r, double fraction, int slices = 12, int stacks = 20); + + +void cylinder(double base, double top, double height, int slices = 12, int stacks = 20); + +} +} + +#endif /* UTIL_H_ */ diff --git a/src/main/CompositeElement.h b/src/main/CompositeElement.h index a4d2cae..b2fbd5a 100644 --- a/src/main/CompositeElement.h +++ b/src/main/CompositeElement.h @@ -34,7 +34,7 @@ public: elements(0) {}; - virtual ~CompositeElement(); + virtual ~CompositeElement() {}; /*for (int i(0); i < elements.size(); ++i) { delete elements[i]; @@ -68,7 +68,7 @@ public: for (int i(0); i < elements.size(); i++) { e = e + elements[i]->electricFieldAt(position); } - return e; + return e; } virtual void accept(ElementVisitor& v) { @@ -77,6 +77,14 @@ public: } } + virtual std::string toString() { + std::stringstream s; + for (int i(0); i < elements.size(); ++i) { + s << elements[i]->toString() << "\n"; + } + return s.str(); + } + }; } diff --git a/src/main/CurvedElement.h b/src/main/CurvedElement.h index 8719a68..9c77feb 100644 --- a/src/main/CurvedElement.h +++ b/src/main/CurvedElement.h @@ -55,10 +55,15 @@ public: return (particle.getPosition() - exitPosition).dot(out) > 0; } + /** Retourne la courbure. */ double getCurvature() const {return curvature;} + /** Retourne le centre de courbure. */ Vector3D getCurvatureCenter() const {return curvatureCenter;} + /** Retourne l'angle entre la position d'entree, le centre de courbure et la position de sortie. */ + double getAngle() const {return acos((entryPosition - curvatureCenter).unit().dot((exitPosition - curvatureCenter).unit()));} + virtual std::string getType() const {return "Curved Element";} virtual std::string toString() const { std::stringstream s; diff --git a/src/main/ElementVisitor.h b/src/main/ElementVisitor.h index 0fda645..dbef4d7 100644 --- a/src/main/ElementVisitor.h +++ b/src/main/ElementVisitor.h @@ -37,8 +37,6 @@ public: virtual void visit(CurvedElement* curved) = 0; virtual void visit(Dipole* dipole) = 0; - - virtual void visit(CompositeElement* composite) = 0; }; } diff --git a/src/test/ElementTest.cc b/src/test/ElementTest.cc index e4e82c9..a17e0bb 100644 --- a/src/test/ElementTest.cc +++ b/src/test/ElementTest.cc @@ -8,6 +8,7 @@ #include #include #include "Dipole.h" +#include "FODO.h" #include "Vector3D.h" using namespace std; @@ -19,6 +20,11 @@ int main() { Dipole* d = new Dipole(Vector3D(0,0,0), Vector3D(1,2,0), 0.2, 0.2, Vector3D::Null, NULL); cout << *d << endl; delete d; d = NULL; + + FODO* fodo = new FODO(Vector3D(0,0,0), Vector3D(1, 0,0), 0.2, 0.2, 3); + cout << *fodo << endl; + delete fodo; fodo = NULL; + /* CurvedElement* c = new CurvedElement(Vector3D(0,1,0), Vector3D(1,0,0), 0.2, 1); cout << *c << endl; diff --git a/src/test/VisitorTest.cc b/src/test/VisitorTest.cc new file mode 100644 index 0000000..8dcf4c4 --- /dev/null +++ b/src/test/VisitorTest.cc @@ -0,0 +1,64 @@ +/* + * VisitorTest.cc + * + * Created on: Apr 3, 2011 + * Author: jakob + */ + +#include +#include +#include "Element.h" +#include "CompositeElement.h" +#include "StraightElement.h" +#include "CurvedElement.h" +#include "Quadrupole.h" +#include "Dipole.h" +#include "FODO.h" +#include "ElementVisitor.h" +#include "Vector3D.h" +using namespace std; +using namespace vhc; + +class PrintVisitor: public ElementVisitor { +public: + + virtual void visit(StraightElement* straight) { + cout << "A straight element." << endl; + } + + virtual void visit(Quadrupole* quadrupole) { + cout << "A quadrupole." << endl; + } + + virtual void visit(CurvedElement* curved) { + cout << "A curved element." << endl; + } + + virtual void visit(Dipole* dipole) { + cout << "A dipole." << endl; + } + + +}; + +int main() { + PrintVisitor pv; + Dipole* dp = new Dipole (Vector3D::Null, Vector3D::i, 0.2, 0.5); + Quadrupole* qp = new Quadrupole(Vector3D::Null, Vector3D::i, 0.2, 2); + FODO* fodo = new FODO(Vector3D::Null, Vector3D::i, 0.2, 0.25, 2.0); + vector elements; + elements.push_back(dp); + elements.push_back(qp); + elements.push_back(fodo); + + for (int i = 0; i < elements.size(); ++i) { + elements[i]->accept(pv); + } + + for (int i = 0; i < elements.size(); ++i) { + delete elements[i]; + elements[i] = NULL; + } + + return 0; +} -- cgit v1.2.3