Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfVector3.h
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
20#ifndef _SOURCE_TYPES_TFVECTOR3_H_
21#define _SOURCE_TYPES_TFVECTOR3_H_
22
23#include "tfVector2.h"
24
25#include <Magnum/Math/Vector3.h>
26#include <Magnum/Math/Distance.h>
27
28
29namespace TissueForge::types {
30
31
32 template<class T> using Vector3Base = Magnum::Math::Vector3<T>;
33
34 template<typename T>
35 class TVector3 : public Vector3Base<T> {
36 public:
37 constexpr static TVector3<T> xAxis(T length = T(1)) { return (TVector3<T>)Vector3Base<T>::xAxis(length); }
38
39 constexpr static TVector3<T> yAxis(T length = T(1)) { return (TVector3<T>)Vector3Base<T>::yAxis(length); }
40
41 constexpr static TVector3<T> zAxis(T length = T(1)) { return (TVector3<T>)Vector3Base<T>::zAxis(length); }
42
43 constexpr static TVector3<T> xScale(T scale) { return (TVector3<T>)Vector3Base<T>::xScale(scale); }
44
45 constexpr static TVector3<T> yScale(T scale) { return (TVector3<T>)Vector3Base<T>::yScale(scale); }
46
47 constexpr static TVector3<T> zScale(T scale) { return (TVector3<T>)Vector3Base<T>::zScale(scale); }
48
49 constexpr TVector3() noexcept: Vector3Base<T>() {}
50
51 constexpr explicit TVector3(T value) noexcept: Vector3Base<T>(value) {}
52
53 constexpr TVector3(T x, T y, T z) noexcept: Vector3Base<T>(x, y, z) {}
54
55 constexpr TVector3(const TVector2<T>& xy, T z) noexcept: Vector3Base<T>(xy[0], xy[1], z) {}
56
57 template<class U> constexpr explicit TVector3(const TVector3<U>& other) noexcept: Vector3Base<T>(other) {}
58
60 T& x() { return Vector3Base<T>::x(); }
61
63 T& y() { return Vector3Base<T>::y(); }
64
66 T& z() { return Vector3Base<T>::z(); }
67
69 constexpr T x() const { return Vector3Base<T>::x(); }
70
72 constexpr T y() const { return Vector3Base<T>::y(); }
73
75 constexpr T z() const { return Vector3Base<T>::z(); }
76
78 T& r() { return Vector3Base<T>::r(); }
79
81 T& g() { return Vector3Base<T>::g(); }
82
84 T& b() { return Vector3Base<T>::b(); }
85
87 constexpr T r() const { return Vector3Base<T>::r(); }
88
90 constexpr T g() const { return Vector3Base<T>::g(); }
91
93 constexpr T b() const { return Vector3Base<T>::b(); }
94
96 TVector2<T>& xy() { return TVector2<T>::from(Vector3Base<T>::data()); }
97
99 constexpr const TVector2<T> xy() const {
100 return {Vector3Base<T>::_data[0], Vector3Base<T>::_data[1]};
101 }
102
104 template<class U = T, typename std::enable_if<std::is_floating_point<U>::value, bool>::type = true>
105 T distance(const TVector3<T> &lineStartPt, const TVector3<T> &lineEndPt) {
106 return Magnum::Math::Distance::lineSegmentPoint(lineStartPt, lineEndPt, *this);
107 }
108
118 template<class U = T, typename std::enable_if<std::is_floating_point<U>::value, bool>::type = true>
119 TVector3<T> lineShortestDisplacementTo(const TVector3<T> &lineStartPt, const TVector3<T> &lineEndPt) const {
120 const TVector3<T> lineDir = (lineEndPt - lineStartPt).normalized();
121 return lineStartPt + (*this - lineStartPt).dot(lineDir) * lineDir - *this;
122 }
123
124 template<class U = T, typename std::enable_if<std::is_floating_point<U>::value, bool>::type = true>
125 TVector3<T> relativeTo(const TVector3<T> &origin, const TVector3<T> &dim, const bool &periodic_x, const bool &periodic_y, const bool &periodic_z) {
126 TVector3<T> result = *this - origin;
127 TVector3<T> crit = dim / 2.0;
128 if(periodic_x) {
129 if(result[0] < -crit[0]) result[0] += dim[0];
130 else if(result[0] > crit[0]) result[0] -= dim[0];
131 }
132 if(periodic_y) {
133 if(result[1] < -crit[1]) result[1] += dim[1];
134 else if(result[1] > crit[1]) result[1] -= dim[1];
135 }
136 if(periodic_z) {
137 if(result[2] < -crit[2]) result[2] += dim[2];
138 else if(result[2] > crit[2]) result[2] -= dim[2];
139 }
140 return result;
141 }
142
145 return Magnum::Math::cross(*this, other);
146 }
147
148 MAGNUM_BASE_VECTOR_CAST_METHODS(3, TVector3, Vector3Base)
149
150 REVISED_MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(3, TVector3, Vector3Base)
151
152 #ifdef SWIGPYTHON
153 SWIGPYTHON_MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(3, TVector3)
154 #endif
155 };
156
157}
158
159TF_VECTOR_IMPL_OSTREAM(TissueForge::types::TVector3)
160
161#endif // _SOURCE_TYPES_TFVECTOR3_H_
Definition tfVector2.h:35
static TVector2< T > & from(T *data)
Definition tfVector2.h:88
Definition tfVector3.h:35
constexpr T g() const
Definition tfVector3.h:90
std::enable_if< std::is_floating_point< U >::value, T >::type length() const
Definition tfVector3.h:150
TVector3< T > lineShortestDisplacementTo(const TVector3< T > &lineStartPt, const TVector3< T > &lineEndPt) const
Get the position relative to a point.
Definition tfVector3.h:119
T & r()
Definition tfVector3.h:78
TVector3< T > cross(const TVector3< T > &other)
Definition tfVector3.h:144
constexpr T b() const
Definition tfVector3.h:93
T & g()
Definition tfVector3.h:81
TVector2< T > & xy()
Definition tfVector3.h:96
T & x()
Definition tfVector3.h:60
T & z()
Definition tfVector3.h:66
constexpr T x() const
Definition tfVector3.h:69
T & y()
Definition tfVector3.h:63
constexpr T y() const
Definition tfVector3.h:72
constexpr const TVector2< T > xy() const
Definition tfVector3.h:99
T dot() const
Definition tfVector3.h:150
constexpr T z() const
Definition tfVector3.h:75
std::enable_if< std::is_floating_point< U >::value, TVector3< T > >::type normalized() const
Definition tfVector3.h:150
T distance(const TVector3< T > &lineStartPt, const TVector3< T > &lineEndPt)
Definition tfVector3.h:105
T & b()
Definition tfVector3.h:84
constexpr T r() const
Definition tfVector3.h:87
Native Tissue Forge type definitions.
Definition tfMatrix.h:33