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
76
77
78
79
80
81
82
83
|
/*
* ElementRenderer.cc
*
* Created on: Apr 2, 2011
* Author: jakob
*/
#include <QtOpenGL>
#include <math.h>
#include "ElementRenderer.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::render(const Element& element) const {
element.accept(*this);
}
void ElementRenderer::drawStraight(const StraightElement* straight) const {
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(const StraightElement* straight) const {
glColor4d(0.4, 0.4, 0.4, 0.9);
drawStraight(straight);
}
void ElementRenderer::visit(const Quadrupole* quadrupole) const {
if (quadrupole->getFocalizingCoefficient() > 1)
glColor4d(0.4, 0, 0, 0.9);
else
glColor4d(0.4, 0.2, 0.2, 0.9);
drawStraight(quadrupole);
}
void ElementRenderer::visit(const Dipole* dipole) const {
glColor4d(0.2, 0, 0.4, 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());
if (d != -Vector3D::i)
glRotated(angle * 180 / M_PI, axis.getX(), axis.getY(), axis.getZ());
else
glRotated(180, 0, 0, 1); //handle degenerate case
//double fraction
util::torus(d.norm(),
dipole->getSectionRadius(),
dipole->getAngle() / (2 * M_PI), SLICES, 200); // * dipole->getAngle() * d.norm());
glPopMatrix();
}
}
|