Library of Assembled Shared Sources
transformation_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-2023 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/** @class lass::prim::Transformation3D
46 * @brief a linear 3D transformation
47 * @author Bram de Greve [BdG]
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_TRANSFORMATION_3D_H
51#define LASS_GUARDIAN_OF_INCLUSION_PRIM_TRANSFORMATION_3D_H
52
53#include "prim_common.h"
54#include "point_3d.h"
55#include "xyz.h"
56#include "../io/xml_o_stream.h"
57#include "../num/num_traits.h"
58#include "../util/allocator.h"
59#include "../util/shared_ptr.h"
60#include "../util/thread.h"
61
62namespace lass
63{
64namespace prim
65{
66namespace impl
67{
68
69template <typename T, size_t N>
70struct LASS_SIMD_ALIGN TransformationImpl
71{
72 enum { matrixSize = N };
73 LASS_SIMD_ALIGN T forward[matrixSize];
74 LASS_SIMD_ALIGN T inverse[matrixSize];
75 util::Semaphore sync;
76 std::atomic<size_t> referenceCount;
77 std::atomic<bool> hasInverse;
78 bool isTranslation;
79 TransformationImpl(): referenceCount(0), hasInverse(false), isTranslation(false) {}
80};
81
82template <typename U, typename Cascade>
83class TransformationImplStorage: public util::ObjectStorage<U, Cascade>
84{
85public:
86 TransformationImplStorage(): util::ObjectStorage<U, Cascade>() {}
87 TransformationImplStorage(U* p): util::ObjectStorage<U, Cascade>(p) {}
88 static U* allocate() { return allocator().allocate(); }
89protected:
90 void dispose()
91 {
92 allocator().deallocate(this->pointer());
93 }
94private:
95 typedef util::AllocatorObject< U , util::AllocatorConcurrentFreeList<> > TAllocator;
96 static TAllocator& allocator()
97 {
98 // this is _not_ thread safe, but it should be OK as it's called once
99 // before main, to init the identity matrix.
100 static TAllocator alloc;
101 return alloc;
102 }
103};
104
105}
106
107template <typename T>
109{
110public:
111
112 typedef Transformation3D<T> TSelf;
113
114 typedef Point3D<T> TPoint;
115 typedef typename TPoint::TVector TVector;
116 typedef typename util::CallTraits<T>::TValue TValue;
117 typedef typename util::CallTraits<T>::TParam TParam;
118 typedef typename util::CallTraits<T>::TReference TReference;
119 typedef typename util::CallTraits<T>::TConstReference TConstReference;
120 typedef num::NumTraits<T> TNumTraits;
121 typedef size_t TSize;
122
123 enum { dimension = TPoint::dimension }; /**< number of dimensions of vector */
124
125 template <typename U> struct Rebind
126 {
127 typedef Transformation3D<U> Type;
128 };
129
131 Transformation3D(const TPoint& origin, const TVector& baseX, const TVector& baseY,
132 const TVector& baseZ);
133 template <typename InputIterator> Transformation3D(InputIterator first, InputIterator last);
134 Transformation3D(std::initializer_list<TValue> list);
135
136 const TSelf inverse() const;
137
138 const TValue* matrix() const;
139 const TValue* inverseMatrix() const;
140
141 bool isIdentity() const;
142 bool isTranslation() const;
143
144 void swap(TSelf& other);
145
146 static const TSelf identity();
147 static const TSelf translation(const TVector& offset);
148 static const TSelf scaler(TParam scale);
149 static const TSelf scaler(const TVector& scale);
150 static const TSelf rotation(XYZ axis, TParam radians);
151 static const TSelf rotation(const TVector& axis, TParam radians);
152 static const TSelf lookAt(const TPoint& eye, const TPoint& target, const TVector& sky);
153
154private:
155
156 enum { matrixSize_ = 16 };
157
158 typedef T TMatrix[matrixSize_];
159 typedef const T TConstMatrix[matrixSize_];
160 typedef impl::TransformationImpl<T, matrixSize_> TImpl;
161 typedef util::SharedPtr<TImpl, impl::TransformationImplStorage, util::IntrusiveCounter<TImpl, std::atomic<size_t>, &TImpl::referenceCount> > TImplPtr;
162
163 Transformation3D(const TImplPtr& pimpl, bool isInversed = false);
164 void computeInverse() const;
165
166 static void translate(const TConstMatrix &source, TMatrix& dest);
167 static void identity(TMatrix& dest);
168 static TImplPtr makeIdentity();
169
170 TImplPtr pimpl_;
171 bool isInversed_;
172 static TImplPtr identity_;
173};
174
175template <typename T> Transformation3D<T> concatenate(const Transformation3D<T>& first, const Transformation3D<T>& second);
176
177template <typename T> Vector3D<T> transform(const Vector3D<T>& subject, const Transformation3D<T>& transformation);
178template <typename T> Point3D<T> transform(const Point3D<T>& subject, const Transformation3D<T>& transformation);
179template <typename T> Vector3D<T> normalTransform(const Vector3D<T>& subject, const Transformation3D<T>& transformation);
180template <typename T> std::pair<Vector3D<T>, T> normalTransform(const std::pair<Vector3D<T>, T>& subject, const Transformation3D<T>& transformation);
181
182template<typename T, typename Char, typename Traits>
183std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream, const Transformation3D<T>& transformation);
184
185template<typename T> io::XmlOStream& operator<<(io::XmlOStream& stream, const Transformation3D<T>& transformation);
186
187}
188
189}
190
191#include "transformation_3d.inl"
192
193#define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_TRANSFORMATION_3D
194#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYOBJECT_PLUS_H
196#endif
197
198#ifdef LASS_GUARDIAN_OF_INCLUSION_PRIM_AABB_3D_H
200#endif
201
202#ifdef LASS_GUARDIAN_OF_INCLUSION_PRIM_PLANE_3D_H
204#endif
205
206#ifdef LASS_GUARDIAN_OF_INCLUSION_PRIM_RAY_3D_H
208#endif
209
210#ifdef LASS_GUARDIAN_OF_INCLUSION_PRIM_COLOR_RGBA_H
212#endif
213
214#endif
215
216// EOF
Output stream for writing a selection of geometric primitives to XML files.
static const TSelf scaler(TParam scale)
make a 3D transformation representing a uniform scaling
Transformation3D(const TPoint &origin, const TVector &baseX, const TVector &baseY, const TVector &baseZ)
construct an matrix from an origin and three base vectors
const TValue * inverseMatrix() const
Return pointer to row major matrix representation of inverse transformation.
static const TSelf rotation(const TVector &axis, TParam radians)
make a 3D transformation representing a rotation around an arbitrary axis
static const TSelf scaler(const TVector &scale)
make a 3D transformation representing a scaling with different factors per axis
static const TSelf translation(const TVector &offset)
make a 3D transformation representing a translation
const TSelf inverse() const
return the inverse transformation.
static const TSelf identity()
make a 3D identity transformation
const TValue * matrix() const
Return pointer to row major matrix representation of transformation.
static const TSelf rotation(XYZ axis, TParam radians)
make a 3D transformation representing a rotation around a primary axis
Transformation3D()
construct an identity transformation.
Transformation3D(InputIterator first, InputIterator last)
construct a transformation from a 4x4 tranformation matrix.
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...
implementation details of lass::prim
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53