summaryrefslogtreecommitdiff
path: root/src/main/CurvedElement.cc
blob: 834484e1966549f14668cb595dc1ab244ff70126 (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
76
77
78
79
80
/*
 * CurvedElement.cc
 *
 *  Created on: Mar 22, 2011
 *      Author: jakob
 */

#include <math.h>
#include <sstream>
#include "exceptions.h"
#include "CurvedElement.h"

using namespace std;
namespace vhc {

CurvedElement::CurvedElement(const Vector3D& entry, const Vector3D& exit, double sectionRadius, double curvature, Element* next):
		Element(entry, exit, sectionRadius, next),
		curvature(curvature),
		curvatureCenter(Vector3D::Null) {


		double k = curvature;

		//pas possible que le rayon de courbure soit plus petit la moitie
		//de la distance entre les points d'entree et de sortie
		if (fabs(1.0 / k) < (exit - entry).norm() / 2) throw IllegalArgumentException("Invalid curvature radius: too small.");

		Vector3D outDirection = (entryPosition - curvatureCenter).cross(exitPosition - curvatureCenter).cross(exitPosition - curvatureCenter);
		if ((entryPosition - exitPosition).dot(outDirection) > 0) throw IllegalArgumentException("A curved element cannot be more than half a turn.");


		Vector3D midpoint = (getEntryPosition() + getExitPosition())/ 2;
		curvatureCenter = midpoint + 1 / k * sqrt(1.0 - (k * k) / 4 * getDiagonal().normSquare()) * (getDiagonal().unit().cross(Vector3D::k));
}

CurvedElement::~CurvedElement() {};

double CurvedElement::getCurvature() const {return curvature;}

Vector3D CurvedElement::getCurvatureCenter() const {return curvatureCenter;}

double CurvedElement::getAngle() const {
	return acos((entryPosition - curvatureCenter).unit().dot((exitPosition - curvatureCenter).unit()));
}

bool CurvedElement::isBefore(const Vector3D& position) const {
	Vector3D out = (exitPosition - curvatureCenter).cross(entryPosition - curvatureCenter).cross(entryPosition - curvatureCenter);
	return (position - entryPosition).dot(out) > 0;
}

bool CurvedElement::isBeside(const Vector3D& position) const {
	Vector3D X(position - curvatureCenter);
	Vector3D u = (X - position.getZ() * Vector3D::k).unit();
	return (X - fabs(1.0 / curvature) * u).norm() > sectionRadius;
}

bool CurvedElement::isAfter(const Vector3D& position) const {
	Vector3D out = (entryPosition - curvatureCenter).cross(exitPosition - curvatureCenter).cross(exitPosition - curvatureCenter);
	return (position - exitPosition).dot(out) > 0;
}

double CurvedElement::getLength() const {
	return getAngle() / getCurvature();
}

Vector3D CurvedElement::getHorizontalAt(const Vector3D& position) const {
	Vector3D X(position - curvatureCenter);
	return (X - position.getZ() * Vector3D::k).unit();
}

std::string CurvedElement::getType() const {return "Curved Element";}
std::string CurvedElement::toString() const {
	std::stringstream s;
	s << Element::toString() << " "
	  << "k = "	<< getCurvature() << " ; "
	  << "Cc =  " << getCurvatureCenter();
	return s.str();
}

} //vhc