-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfunctions.cpp
87 lines (74 loc) · 3.27 KB
/
functions.cpp
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
/*
* This file is a part of KNOSSOS.
*
* (C) Copyright 2007-2018
* Max-Planck-Gesellschaft zur Foerderung der Wissenschaften e.V.
*
* KNOSSOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 of
* the License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* For further information, visit https://knossos.app
* or contact knossosteam@gmail.com
*/
#include "dataset.h"
#include "functions.h"
#include <boost/math/constants/constants.hpp>
#include <cmath>
/** this file contains function which are not dependent from any state */
constexpr bool inRange(const int value, const int min, const int max) {
return value >= min && value < max;
}
bool insideCurrentSupercube(const Coordinate & coord, const Coordinate & center, const Coordinate & cubesPerDimension, const Coordinate & cubeSizeGlobal) {
const auto halfSupercube = cubeSizeGlobal.componentMul(cubesPerDimension-1) / 2.;
const auto c = coord.cube(cubeSizeGlobal);
const auto tl = (center - halfSupercube).cube(cubeSizeGlobal);
const auto br = (center + halfSupercube).cube(cubeSizeGlobal) + 1;
bool valid = true;
valid &= inRange(c.x, tl.x, br.x);
valid &= inRange(c.y, tl.y, br.y);
valid &= inRange(c.z, tl.z, br.z);
return valid;
}
bool currentlyVisible(const Coordinate & coord, const Coordinate & center, const Coordinate & cubesPerDimension, const Coordinate & cubeSizeGlobal) {
const bool valid = insideCurrentSupercube(coord, center, cubesPerDimension, cubeSizeGlobal);
const auto c = center.cube(cubeSizeGlobal).cube2Global(cubeSizeGlobal);
const bool xvalid = valid & inRange(coord.x, c.x, c.x + cubeSizeGlobal.x);
const bool yvalid = valid & inRange(coord.y, c.y, c.y + cubeSizeGlobal.y);
const bool zvalid = valid & inRange(coord.z, c.z, c.z + cubeSizeGlobal.z);
return xvalid || yvalid || zvalid;
}
int roundFloat(float number) {
if(number >= 0) return (int)(number + 0.5);
else return (int)(number - 0.5);
}
int sgn(float number) {
if(number > 0.) return 1;
else if(number == 0.) return 0;
else return -1;
}
//Some math helper functions
float radToDeg(float rad) {
return ((180. * rad) / boost::math::constants::pi<float>());
}
float degToRad(float deg) {
return ((deg / 180.) * boost::math::constants::pi<float>());
}
bool intersectLineAndPlane(const floatCoordinate planeNormal, const floatCoordinate planeUpVec,
const floatCoordinate lineUpVec, const floatCoordinate lineDirectionVec,
floatCoordinate & intersectionPoint) {
if (std::abs(lineDirectionVec.dot(planeNormal)) > 0.0001) {
const float lambda = (planeNormal.dot(planeUpVec) - planeNormal.dot(lineUpVec)) / lineDirectionVec.dot(planeNormal);
intersectionPoint = lineUpVec + lineDirectionVec * lambda;
return true;
}
return false;
}