Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfVector2.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_TFVECTOR2_H_
21#define _SOURCE_TYPES_TFVECTOR2_H_
22
23#include "tfVector.h"
24
25#include <Magnum/Math/Vector2.h>
26#include <Magnum/Math/Distance.h>
27
28
29namespace TissueForge::types {
30
31
32 template<class T> using Vector2Base = Magnum::Math::Vector2<T>;
33
34 template<typename T>
35 class TVector2 : public Vector2Base<T> {
36 public:
38 constexpr static TVector2<T> xAxis(T length = T(1)) { return (TVector2<T>)Vector2Base<T>::xAxis(length); }
39
41 constexpr static TVector2<T> yAxis(T length = T(1)) { return (TVector2<T>)Vector2Base<T>::yAxis(length); }
42
44 constexpr static TVector2<T> xScale(T scale) { return (TVector2<T>)Vector2Base<T>::xScale(scale); }
45
47 constexpr static TVector2<T> yScale(T scale) { return (TVector2<T>)Vector2Base<T>::yScale(scale); }
48
49 constexpr TVector2() noexcept: Vector2Base<T>() {}
50
51 constexpr explicit TVector2(T value) noexcept: Vector2Base<T>(value) {}
52
53 constexpr TVector2(T x, T y) noexcept: Vector2Base<T>(x, y) {}
54
55 template<class U> constexpr explicit TVector2(const TVector2<U>& other) noexcept: Vector2Base<T>(other) {}
56
58 T& x() { return Vector2Base<T>::x(); }
59
61 T& y() { return Vector2Base<T>::y(); }
62
64 constexpr T x() const { return Vector2Base<T>::x(); }
65
67 constexpr T y() const { return Vector2Base<T>::y(); }
68
70 template<class U = T, typename std::enable_if<std::is_floating_point<U>::value, bool>::type = true>
71 T distance(const TVector2<T> &lineStartPt, const TVector2<T> &lineEndPt) {
72 return Magnum::Math::Distance::lineSegmentPoint(lineStartPt, lineEndPt, *this);
73 }
74
75 template<class U = T, typename std::enable_if<std::is_floating_point<U>::value, bool>::type = true>
76 TVector2<T> lineShortestDisplacementTo(const TVector2<T> &lineStartPt, const TVector2<T> &lineEndPt) const {
77 const TVector2<T> lineDir = (lineEndPt - lineStartPt).normalized();
78 return lineStartPt + (*this - lineStartPt).dot(lineDir) * lineDir - *this;
79 }
80
82 T cross(const TVector2<T> &other) {
83 return Magnum::Math::cross(*this, other);
84 }
85
86 MAGNUM_BASE_VECTOR_CAST_METHODS(2, TVector2, Vector2Base)
87
88 REVISED_MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(2, TVector2, Vector2Base)
89
90 #ifdef SWIGPYTHON
91 SWIGPYTHON_MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(2, TVector2)
92 #endif
93 };
94
95}
96
97TF_VECTOR_IMPL_OSTREAM(TissueForge::types::TVector2)
98
99#endif // _SOURCE_TYPES_TFVECTOR2_H_
Definition tfVector2.h:35
static constexpr TVector2< T > yScale(T scale)
Definition tfVector2.h:47
constexpr T x() const
Definition tfVector2.h:64
std::enable_if< std::is_floating_point< U >::value, double >::type length() const
Definition tfVector2.h:88
T & y()
Definition tfVector2.h:61
std::enable_if< std::is_floating_point< U >::value, TVector2< T > >::type normalized() const
Definition tfVector2.h:88
static constexpr TVector2< T > xScale(T scale)
Definition tfVector2.h:44
T dot() const
Definition tfVector2.h:88
static constexpr TVector2< T > xAxis(T length=T(1))
Definition tfVector2.h:38
T cross(const TVector2< T > &other)
Definition tfVector2.h:82
constexpr T y() const
Definition tfVector2.h:67
static constexpr TVector2< T > yAxis(T length=T(1))
Definition tfVector2.h:41
T distance(const TVector2< T > &lineStartPt, const TVector2< T > &lineEndPt)
Definition tfVector2.h:71
T & x()
Definition tfVector2.h:58
Native Tissue Forge type definitions.
Definition tfMatrix.h:33