summaryrefslogtreecommitdiff
path: root/src/gui/ElementRenderer.cc
blob: 01b27a07ccaf38c896eaf9f2d51ecb63eb78befc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * ElementRenderer.cc
 *
 *  Created on: Apr 2, 2011
 *      Author: jakob
 */

#include <QtOpenGL>
#include <math.h>
#include "ElementRenderer.h"
#include "CurvedElement.h"
#include "StraightElement.h"
#include "Dipole.h"
#include "Quadrupole.h"
#include "util.h"
#include "Vector3D.h"

//using namespace vhc::util;

namespace vhc {

ElementRenderer::ElementRenderer() {

}

ElementRenderer::~ElementRenderer() {

}

void ElementRenderer::drawStraight(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(StraightElement* straight) {
	glColor4d(0.4, 0.4, 0.4, 0.9);
	drawStraight(straight);
}

void ElementRenderer::visit(Quadrupole* quadrupole) {
	if (quadrupole->getFocusingCoefficient() > 1) glColor4d(0.4, 0, 0, 0.9);
	else glColor4d(0, 0, 0.4, 0.9);
	drawStraight(quadrupole);
}


void ElementRenderer::visit(Dipole* dipole) {
	glColor4d(0.4, 0, 0.6, 0.9);
	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());

	//double fraction
	util::torus(d.norm(),
			dipole->getSectionRadius(),
			dipole->getAngle() / (2 * M_PI), SLICES, 200);// * dipole->getAngle() * d.norm());

	glPopMatrix();
}

}