Library of Assembled Shared Sources
vector_3d.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @struct lass::prim::Vector3D
46 * @brief 3D Vector
47 * @author BdG
48 * @date 2003
49 */
50
51
52
53#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_3D_H
54#define LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_3D_H
55
56
57
58
59#include "prim_common.h"
60#include "../io/xml_o_stream.h"
61#include "xyz.h"
62
63
64
65namespace lass
66{
67
68namespace prim
69{
70
71template<typename T>
72struct LASS_SIMD_ALIGN Vector3D
73{
74 typedef Vector3D<T> TSelf;
75
76 typedef typename util::CallTraits<T>::TValue TValue;
77 typedef typename util::CallTraits<T>::TParam TParam;
78 typedef typename util::CallTraits<T>::TReference TReference;
79 typedef typename util::CallTraits<T>::TConstReference TConstReference;
80 typedef num::NumTraits<T> TNumTraits;
81
82 enum { dimension = 3 }; /**< number of dimensions of vector */
83
84 template <typename U> struct Rebind
85 {
86 typedef Vector3D<U> Type;
87 };
88
89
90 // public data
91
92 TValue x;
93 TValue y;
94 TValue z;
95
96
97 // methods
98
99 Vector3D();
100 Vector3D(TParam x, TParam y, TParam z);
101 template <typename U> explicit Vector3D(const Vector3D<U>& other);
102 template <typename U> Vector3D(const U& x, const U& y, const U& z);
103
104 typename Vector3D::TConstReference operator[](size_t index) const;
105 typename Vector3D::TReference operator[](size_t index) ;
106 typename Vector3D::TConstReference at(signed index) const;
107 typename Vector3D::TReference at(signed index);
108
109 const Vector3D<T>& operator+() const;
110 const Vector3D<T> operator-() const;
111 Vector3D<T>& operator+=(const Vector3D<T>& other);
112 Vector3D<T>& operator-=(const Vector3D<T>& other);
113 Vector3D<T>& operator*=(const Vector3D<T>& other);
114 Vector3D<T>& operator/=(const Vector3D<T>& other);
115 Vector3D<T>& operator+=(TParam other);
116 Vector3D<T>& operator-=(TParam other);
117 Vector3D<T>& operator*=(TParam other);
118 Vector3D<T>& operator/=(TParam other);
119
120 bool isZero() const;
121 bool isNaN() const;
122 const TValue squaredNorm() const;
123 const TValue norm() const;
124 XYZ majorAxis() const;
125 const Vector3D<T> normal() const;
126 const Vector3D<T> reciprocal() const;
127 const Vector3D<T> project(const Vector3D<T>& other) const;
128 const Vector3D<T> reject(const Vector3D<T>& other) const;
129 const Vector3D<T> reflect(const Vector3D<T>& other) const;
130 const Vector3D<T> transform(T (*op)(T)) const;
131
132 void normalize();
133
134 template <class RandomGenerator> static Vector3D<T> random(RandomGenerator& generator);
135};
136
137template<typename T> typename Vector3D<T>::TValue dot(const Vector3D<T>& a, const Vector3D<T>& b);
138template<typename T> Vector3D<T> cross(const Vector3D<T>& a, const Vector3D<T>& b);
139
140template<typename T> bool operator==(const Vector3D<T>& a, const Vector3D<T>& b);
141template<typename T> bool operator!=(const Vector3D<T>& a, const Vector3D<T>& b);
142
143template<typename T> Vector3D<T> operator+(const Vector3D<T>& a, const Vector3D<T>& b);
144template<typename T> Vector3D<T> operator-(const Vector3D<T>& a, const Vector3D<T>& b);
145template<typename T> Vector3D<T> operator*(const Vector3D<T>& a, const Vector3D<T>& b);
146template<typename T> Vector3D<T> operator/(const Vector3D<T>& a, const Vector3D<T>& b);
147
148template<typename T> Vector3D<T> operator+(const Vector3D<T>& a, typename Vector3D<T>::TParam b);
149template<typename T> Vector3D<T> operator-(const Vector3D<T>& a, typename Vector3D<T>::TParam b);
150template<typename T> Vector3D<T> operator*(const Vector3D<T>& a, typename Vector3D<T>::TParam b);
151template<typename T> Vector3D<T> operator/(const Vector3D<T>& a, typename Vector3D<T>::TParam b);
152
153template<typename T> Vector3D<T> operator+(typename Vector3D<T>::TParam a, const Vector3D<T>& b);
154template<typename T> Vector3D<T> operator-(typename Vector3D<T>::TParam a, const Vector3D<T>& b);
155template<typename T> Vector3D<T> operator*(typename Vector3D<T>::TParam a, const Vector3D<T>& b);
156// we don't allow scalar / vector. you can do scalar * vector.reciprocal() for that.
157
158template<typename T> Vector3D<T> pointwiseMin(const Vector3D<T>& a, const Vector3D<T>& b);
159template<typename T> Vector3D<T> pointwiseMax(const Vector3D<T>& a, const Vector3D<T>& b);
160
161template <typename T> Vector3D<T> lerp(const Vector3D<T>& a, const Vector3D<T>& b, typename Vector3D<T>::TParam t);
162
163template<typename T, typename Char, typename Traits>
164std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream, const Vector3D<T>& b);
165template<typename T, typename Char, typename Traits>
166std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& stream, Vector3D<T>& b);
167
168template<typename T> io::XmlOStream& operator<<(io::XmlOStream& stream, const Vector3D<T>& b);
169
170
171
172}
173
174}
175
176#include "vector_3d.inl"
177
178#define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_VECTOR_3D
179#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYOBJECT_PLUS_H
181#endif
182
183#endif
Output stream for writing a selection of geometric primitives to XML files.
cyclic iterator over xyz indices
Definition xyz.h:62
#define LASS_SIMD_ALIGN
if LASS_SIMD_ALIGNMENT is set, use LASS_SIMD_ALIGN to align some structures on SIMD alignment boundar...
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53
const Vector3D< T > project(const Vector3D< T > &other) const
Project vector on this one.
Vector3D::TConstReference at(signed index) const
Wrap index around range.
const TValue squaredNorm() const
Return squared norm of vector.
void normalize()
Normalize vector.
Vector3D< T > & operator-=(TParam other)
subtract other of each component of this.
Vector3D< T > & operator*=(TParam other)
multiply each component of this with other.
const Vector3D< T > reject(const Vector3D< T > &other) const
Reject vector from this one.
const Vector3D< T > transform(T(*op)(T)) const
apply a function to every component
Vector3D< T > & operator/=(TParam other)
divide each component of this by other.
XYZ majorAxis() const
Return axis with the greatest absolute value.
const TValue norm() const
Return norm of vector.
Vector3D< T > & operator/=(const Vector3D< T > &other)
Componentwise division.
Vector3D< T > & operator-=(const Vector3D< T > &other)
componentwise subtraction
Vector3D< T > & operator+=(TParam other)
add other to each component of this.
static Vector3D< T > random(RandomGenerator &generator)
Random unit vector.
bool isNaN() const
Return true if at least one of the components is NaN.
Vector3D< T > & operator+=(const Vector3D< T > &other)
componentwise addition
bool isZero() const
Return true if all the components are (exactly!) zero.
const Vector3D< T > & operator+() const
A weird way to get back the same object.
const Vector3D< T > normal() const
return a unit vector with same direction/sense as this vector.
const Vector3D< T > reciprocal() const
return the reciprocal version of this vector
Vector3D::TReference at(signed index)
Wrap index around range.
Vector3D< T > & operator*=(const Vector3D< T > &other)
Componentwise multiplication.