-
Notifications
You must be signed in to change notification settings - Fork 2
/
box.cc
105 lines (93 loc) · 3.04 KB
/
box.cc
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
#include "box.h"
#include <iostream>
#include "vector3d.h"
#include <tuple>
*/
#include "box.h"
ostream& operator<<(ostream& os, const box& b)
{
os << b.center << ", " << vector3d(b.halfRatio[0], b.halfRatio[1], b.halfRatio[2]) << ", " << b.base[0] << ", " << b.base[1] << ", " << b.base[2] << endl;
//os << acos(b.base[0]*vector3d(1,0,0)) * 180/M_PI << "°, " << acos(b.base[1]*vector3d(0,1,0)) * 180/M_PI << "°, " << acos(b.base[2]*vector3d(0,0,1)) * 180/M_PI << "°" << endl;
return os;
}
void box::updateEdges()
{
edges[0] = center + halfRatio[0]*base[0] + halfRatio[1]*base[1] + halfRatio[2]*base[2];
edges[1] = center + halfRatio[0]*base[0] + halfRatio[1]*base[1] - halfRatio[2]*base[2];
edges[2] = center + halfRatio[0]*base[0] - halfRatio[1]*base[1] + halfRatio[2]*base[2];
edges[3] = center + halfRatio[0]*base[0] - halfRatio[1]*base[1] - halfRatio[2]*base[2];
edges[4] = center - halfRatio[0]*base[0] + halfRatio[1]*base[1] + halfRatio[2]*base[2];
edges[5] = center - halfRatio[0]*base[0] + halfRatio[1]*base[1] - halfRatio[2]*base[2];
edges[6] = center - halfRatio[0]*base[0] - halfRatio[1]*base[1] + halfRatio[2]*base[2];
edges[7] = center - halfRatio[0]*base[0] - halfRatio[1]*base[1] - halfRatio[2]*base[2];
}
void box::checkBase()
{
//throw invalid_argument( "Base is not orthogonal" );
if ((base[0].length() != 1) || (base[1].length() != 1) || (base[2].length() != 1))
{
base[0] = base[0].normalize();
base[1] = base[1].normalize();
base[2] = base[2].normalize();
}
if ((base[0] * base[1] != 0) || (base[1] * base[2] != 0) || (base[2] * base[0] != 0))
{
//cout << "BASE IS NOT ORTHOGONAL: " << (base[0] * base[1]) << ", " << (base[1] * base[2]) << ", " << (base[2] * base[0])<<endl;
base[0] = base[0].normalize();
base[1] = base[1] - (base[0]*base[1])*base[0];
base[1] = base[1].normalize();
base[2] = base[2] - (base[0]*base[2])*base[0] - (base[1]*base[2])*base[1];
base[2] = base[2].normalize();
}
//if (baseVectors[0]%baseVectors[1] != baseVectors[2])
// throw invalid_argument( "Base is not right handed" );
}
box::box(vector3d c, double r1, double r2, double r3, vector3d b1, vector3d b2, vector3d b3)
{
center = c;
halfRatio[0] = r1;
halfRatio[1] = r2;
halfRatio[2] = r3;
base[0] = b1.normalize();
base[1] = b2.normalize();
base[2] = b3.normalize();
checkBase();
updateEdges();
}
box::box()
{
center = vector3d();
halfRatio[0] = standardHalfRatio;
halfRatio[1] = standardHalfRatio;
halfRatio[2] = standardHalfRatio;
base[0] = vector3d(1,0,0);
base[1] = vector3d(0,1,0);
base[2] = vector3d(0,0,1);
checkBase();
updateEdges();
}
box::box(vector3d c, vector3d b1, vector3d b2, vector3d b3)
{
center = c;
halfRatio[0] = standardHalfRatio;
halfRatio[1] = standardHalfRatio;
halfRatio[2] = standardHalfRatio;
base[0] = b1.normalize();
base[1] = b2.normalize();
base[2] = b3.normalize();
checkBase();
updateEdges();
}
void box::translate(vector3d translation)
{
center = center + translation;
updateEdges();
}
void box::printEdges()
{
for(int i = 0; i <= 7; i++) {
cout << edges[i];
if (i != 7) cout << ", ";
}
}