diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/Camera.h | 22 | ||||
-rw-r--r-- | src/gui/ElementRenderer.cc | 34 | ||||
-rw-r--r-- | src/gui/ElementRenderer.h | 7 | ||||
-rw-r--r-- | src/gui/GLWidget.cc | 72 | ||||
-rw-r--r-- | src/gui/GLWidget.h | 6 | ||||
-rw-r--r-- | src/gui/Makefile | 13 | ||||
-rw-r--r-- | src/gui/moc_GLWidget.cpp | 2 | ||||
-rw-r--r-- | src/gui/util.cc | 1 | ||||
-rw-r--r-- | src/gui/util.h | 2 | ||||
-rw-r--r-- | src/main/CurvedElement.h | 2 | ||||
-rw-r--r-- | src/main/ElementVisitor.h | 2 | ||||
-rw-r--r-- | src/main/Vector3D.h | 2 | ||||
-rw-r--r-- | src/test/Vector3DTest.cc | 6 |
13 files changed, 136 insertions, 35 deletions
diff --git a/src/gui/Camera.h b/src/gui/Camera.h index fa17c58..75e07b2 100644 --- a/src/gui/Camera.h +++ b/src/gui/Camera.h @@ -8,20 +8,30 @@ #ifndef CAMERA_H_ #define CAMERA_H_ #include <math.h> +#include <QtOpenGL> #include "exception.h" class Camera { private: - double _x; - double _y; - double _z; - public: - Camera(double x, double y, double z): _x(x), _y(y), _z(z) {}; - virtual ~Camera(); + double r; + double theta; + double phi; + + Camera(double r, double theta, double phi): r(r), theta(theta), phi(phi) {}; + virtual ~Camera() {}; + + + void setView() { + double eyeX, eyeY, eyeZ; + toCarthesian(r, theta, phi, eyeX, eyeY, eyeZ); + gluLookAt(eyeX,eyeY,eyeZ, + 0,0,0, + 0,1,0); + } static double sign(double value) {return (value > 0) - (value < 0);} diff --git a/src/gui/ElementRenderer.cc b/src/gui/ElementRenderer.cc index 6f28c13..afce65c 100644 --- a/src/gui/ElementRenderer.cc +++ b/src/gui/ElementRenderer.cc @@ -5,11 +5,18 @@ * Author: jakob */ +#include <QtOpenGL> +#include <math.h> #include "ElementRenderer.h" +#include "CurvedElement.h" +#include "StraightElement.h" +#include "Dipole.h" +#include "util.h" #include "Vector3D.h" +//using namespace vhc::util; + namespace vhc { -using namespace util; ElementRenderer::ElementRenderer() { // TODO Auto-generated constructor stub @@ -22,19 +29,38 @@ ElementRenderer::~ElementRenderer() { void ElementRenderer::visit(StraightElement* straight) { + glPushMatrix(); + glTranslated(straight->getEntryPosition().getX(), straight->getEntryPosition().getY(), straight->getEntryPosition().getZ()); + Vector3D axis = Vector3D::k.cross(straight->getDiagonal()); + double angle = asin(axis.norm() / straight->getDiagonal().norm()); + glRotated(angle * 180 / M_PI, axis.getX(), axis.getY(), axis.getZ()); + + util::cylinder(straight->getSectionRadius(), + straight->getSectionRadius(), + straight->getDiagonal().norm(), + SLICES, STACKS_PER_LENGTH * straight->getDiagonal().norm()); + glPopMatrix(); } void ElementRenderer::visit(Quadrupole* quadrupole) { } -void ElementRenderer::visit(CurvedElement* curved) { - -} void ElementRenderer::visit(Dipole* dipole) { + glPushMatrix(); + glTranslated(dipole->getCurvatureCenter().getX(), dipole->getCurvatureCenter().getY(), dipole->getCurvatureCenter().getZ()); + Vector3D d = dipole->getExitPosition() - dipole->getCurvatureCenter(); + Vector3D axis = Vector3D::i.cross(d); + double angle = asin(axis.norm() / d.norm()); + glRotated(angle * 180 / M_PI, axis.getX(), axis.getY(), axis.getZ()); + + util::torus(d.norm(), + dipole->getSectionRadius(), + dipole->getAngle() / (2 * M_PI),12, 200); + glPopMatrix(); } } diff --git a/src/gui/ElementRenderer.h b/src/gui/ElementRenderer.h index 9d6ff75..dd55471 100644 --- a/src/gui/ElementRenderer.h +++ b/src/gui/ElementRenderer.h @@ -8,6 +8,7 @@ #ifndef ELEMENTRENDERER_H_ #define ELEMENTRENDERER_H_ +#include <stdlib.h> #include "ElementVisitor.h" namespace vhc { @@ -15,6 +16,10 @@ namespace vhc { class ElementRenderer: public ElementVisitor { +private: + static const int SLICES = 12; + static const int STACKS_PER_LENGTH = 20; + public: ElementRenderer(); @@ -25,8 +30,6 @@ public: virtual void visit(Quadrupole* quadrupole); - virtual void visit(CurvedElement* curved); - virtual void visit(Dipole* dipole); }; diff --git a/src/gui/GLWidget.cc b/src/gui/GLWidget.cc index 391c5f5..b169c97 100644 --- a/src/gui/GLWidget.cc +++ b/src/gui/GLWidget.cc @@ -1,7 +1,13 @@ #include <QtOpenGL> #include <math.h> +#include <iostream> #include "GLWidget.h" +#include "StraightElement.h" +#include "ElementRenderer.h" +#include "CurvedElement.h" +#include "Dipole.h" #include "util.h" +#include "Vector3D.h" void torus(int numc, int numt, double fraction = 1 , double R = 1, double r = 0.1) { int i, j, k; @@ -49,7 +55,7 @@ void axes() { GLWidget::GLWidget (QWidget* parent) - : QGLWidget (parent) + : QGLWidget (parent), camera(10,0,0), wireframe(false) { eyeX = 0; eyeY = 0; @@ -71,18 +77,48 @@ void GLWidget::initializeGL () { glClearColor (0, 0, 0, 1.0); glEnable (GL_DEPTH_TEST); gluPerspective(65.0, 4.0/3, 1.0, 1000.0); + glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); + glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); } void GLWidget::paintGL () { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity (); - gluLookAt(eyeX,eyeY,eyeZ, - 0,0,0, - 0,1000,0); + //gluLookAt(eyeX,eyeY,eyeZ, + //0,0,0, + //0,1,0); //glTranslated (0.0, 0.0, -10.0); //glColor3d (0.0, 0.0, 1.0); - //glScaled (300.0, 300.0, 300.0); + camera.setView(); + axes(); + + glScaled (300.0, 300.0, 300.0); + + if (wireframe) { + glPolygonMode(GL_FRONT, GL_LINE); + glPolygonMode(GL_BACK, GL_LINE); + } else { + glPolygonMode(GL_FRONT, GL_FILL); + glPolygonMode(GL_BACK, GL_FILL); + } + + vhc::ElementRenderer* er = new vhc::ElementRenderer; + vhc::StraightElement* se = new vhc::StraightElement(vhc::Vector3D::j, vhc::Vector3D::j + vhc::Vector3D::i, 0.2); + vhc::StraightElement* se2 = new vhc::StraightElement(vhc::Vector3D(2,0,0), vhc::Vector3D(2,-1,0), 0.2); + se->accept(*er); + se2->accept(*er); + delete se; se = NULL; + delete se2; se2 = NULL; + + vhc::CurvedElement* ce = new vhc::Dipole(vhc::Vector3D(1,1,0), vhc::Vector3D::i * 2, 0.2, 1, vhc::Vector3D::Null); + std::cout <<*ce << "\n"; + ce->accept(*er); + delete er; er = NULL; + + delete ce; ce = NULL; + +/* glBegin (GL_POLYGON); glColor3d(1,0,0); glVertex3d (-300, -300, 0.0); @@ -109,7 +145,7 @@ void GLWidget::paintGL () { vhc::util::cylinder(0.1, 0, 2, 12, 20); glPolygonMode(GL_FRONT, GL_FILL); - glPolygonMode(GL_BACK, GL_FILL); + glPolygonMode(GL_BACK, GL_FILL);*/ } @@ -133,21 +169,31 @@ void GLWidget::keyPressEvent (QKeyEvent* event) { qApp->quit(); break; case Qt::Key_Up: - eyeZ += 10; + //eyeZ += 10; + camera.theta += 2 * M_PI / 100; break; case Qt::Key_Down: - eyeZ -= 10; + //eyeZ -= 10; + camera.theta -= 2 * M_PI / 100; break; case Qt::Key_Right: - eyeX += 10; + //eyeX += 10; + camera.phi += 2 * M_PI / 100; break; case Qt::Key_Left: - eyeX -= 10; + //eyeX -= 10; + camera.phi -= 2 * M_PI / 100; break; case Qt::Key_R: - eyeX = 0; - eyeY = 0; - eyeZ = 10; + //eyeX = 0; + //eyeY = 0; + //eyeZ = 10; + camera.r = 10; + camera.phi = 0; + camera.theta = 0; + break; + case Qt::Key_Space: + wireframe = !wireframe; break; default: break; diff --git a/src/gui/GLWidget.h b/src/gui/GLWidget.h index 73124f5..2cbbb68 100644 --- a/src/gui/GLWidget.h +++ b/src/gui/GLWidget.h @@ -2,6 +2,8 @@ #define GLWIDGET_H #include <QGLWidget> +#include "Camera.h" + class GLWidget : public QGLWidget { Q_OBJECT @@ -17,6 +19,10 @@ public: double eyeY; double eyeZ; + Camera camera; + bool wireframe; + + protected: void initializeGL (); diff --git a/src/gui/Makefile b/src/gui/Makefile index cd4970a..6e33fef 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: Sun Apr 3 21:53:36 2011 +# Generated by qmake (2.01a) (Qt 4.7.0) on: Tue Apr 5 00:12:18 2011 # Project: gui.pro # Template: app # Command: /usr/bin/qmake -o Makefile gui.pro @@ -186,7 +186,8 @@ mocables: compiler_moc_header_make_all compiler_moc_source_make_all compiler_moc_header_make_all: moc_GLWidget.cpp compiler_moc_header_clean: -$(DEL_FILE) moc_GLWidget.cpp -moc_GLWidget.cpp: GLWidget.h +moc_GLWidget.cpp: Camera.h \ + GLWidget.h /usr/bin/moc-qt4 $(DEFINES) $(INCPATH) GLWidget.h -o moc_GLWidget.cpp compiler_rcc_make_all: @@ -208,17 +209,21 @@ compiler_clean: compiler_moc_header_clean ####### Compile -$(BINDIR)/gui/Main.o: Main.cc GLWidget.h +$(BINDIR)/gui/Main.o: Main.cc GLWidget.h \ + Camera.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/Main.o Main.cc $(BINDIR)/gui/GLWidget.o: GLWidget.cc GLWidget.h \ + Camera.h \ + ElementRenderer.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 +$(BINDIR)/gui/ElementRenderer.o: ElementRenderer.cc ElementRenderer.h \ + util.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/ElementRenderer.o ElementRenderer.cc $(BINDIR)/gui/util.o: util.cc diff --git a/src/gui/moc_GLWidget.cpp b/src/gui/moc_GLWidget.cpp index b6be50e..17b611d 100644 --- a/src/gui/moc_GLWidget.cpp +++ b/src/gui/moc_GLWidget.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** Meta object code from reading C++ file 'GLWidget.h' ** -** Created: Tue Mar 29 22:54:38 2011 +** Created: Mon Apr 4 23:55:10 2011 ** by: The Qt Meta Object Compiler version 62 (Qt 4.7.0) ** ** WARNING! All changes made in this file will be lost! diff --git a/src/gui/util.cc b/src/gui/util.cc index a92d5be..155df32 100644 --- a/src/gui/util.cc +++ b/src/gui/util.cc @@ -24,7 +24,6 @@ void torus(double R, double r, double fraction, int slices, int 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); } } diff --git a/src/gui/util.h b/src/gui/util.h index e1f7609..4f8133a 100644 --- a/src/gui/util.h +++ b/src/gui/util.h @@ -11,7 +11,7 @@ namespace vhc { namespace util { -/** Dessine un tore autour de l'origine* +/** 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) diff --git a/src/main/CurvedElement.h b/src/main/CurvedElement.h index 9c77feb..3bde1da 100644 --- a/src/main/CurvedElement.h +++ b/src/main/CurvedElement.h @@ -12,6 +12,7 @@ #include "Element.h" #include "Particle.h" #include "Vector3D.h" +#include "ElementVisitor.h" namespace vhc { @@ -74,7 +75,6 @@ public: return s.str(); } - }; } diff --git a/src/main/ElementVisitor.h b/src/main/ElementVisitor.h index dbef4d7..cf14aca 100644 --- a/src/main/ElementVisitor.h +++ b/src/main/ElementVisitor.h @@ -34,8 +34,6 @@ public: virtual void visit(Quadrupole* quadrupole) = 0; - virtual void visit(CurvedElement* curved) = 0; - virtual void visit(Dipole* dipole) = 0; }; diff --git a/src/main/Vector3D.h b/src/main/Vector3D.h index 979c9a2..5a836fd 100644 --- a/src/main/Vector3D.h +++ b/src/main/Vector3D.h @@ -107,6 +107,8 @@ public: return s.str(); }; + double angle(const Vector3D& v) const {return acos(dot(v) / norm() / v.norm());} + /** Produit mixte de 3 vecteurs. Retourne le produit scalaire de ce vecteur * avec le produit vectoriel de deux vecteurs <code>v</code> et <code>w</code>). */ double tripleProduct(const Vector3D& v, const Vector3D& w) const { return dot(v.cross(w)); } diff --git a/src/test/Vector3DTest.cc b/src/test/Vector3DTest.cc index 0065f00..d71f0d1 100644 --- a/src/test/Vector3DTest.cc +++ b/src/test/Vector3DTest.cc @@ -40,6 +40,12 @@ int main() { }; assert(caught == true); + //vector product test + assert(Vector3D::i.cross(Vector3D::j)==Vector3D::k); + assert(Vector3D::j.cross(Vector3D::k)==Vector3D::i); + assert(Vector3D::k.cross(Vector3D::i)==Vector3D::j); + assert(Vector3D::j.cross(Vector3D::i)==-Vector3D::k); + cout << "Vector3D: tests completed successfully" << endl; return 0; |