1/*
2---------------------------------------------------------------------------
3Open Asset Import Library (assimp)
4---------------------------------------------------------------------------
5
6Copyright (c) 2006-2017, assimp team
7
8
9All rights reserved.
10
11Redistribution and use of this software in source and binary forms,
12with or without modification, are permitted provided that the following
13conditions are met:
14
15* Redistributions of source code must retain the above
16 copyright notice, this list of conditions and the
17 following disclaimer.
18
19* Redistributions in binary form must reproduce the above
20 copyright notice, this list of conditions and the
21 following disclaimer in the documentation and/or other
22 materials provided with the distribution.
23
24* Neither the name of the assimp team, nor the names of its
25 contributors may be used to endorse or promote products
26 derived from this software without specific prior
27 written permission of the assimp team.
28
29THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40---------------------------------------------------------------------------
41*/
42
43/** @file vector2.inl
44 * @brief Inline implementation of aiVector2t<TReal> operators
45 */
46#pragma once
47#ifndef AI_VECTOR2D_INL_INC
48#define AI_VECTOR2D_INL_INC
49
50#ifdef __cplusplus
51#include "vector2.h"
52
53#include <cmath>
54
55// ------------------------------------------------------------------------------------------------
56template <typename TReal>
57template <typename TOther>
58aiVector2t<TReal>::operator aiVector2t<TOther> () const {
59 return aiVector2t<TOther>(static_cast<TOther>(x),static_cast<TOther>(y));
60}
61// ------------------------------------------------------------------------------------------------
62template <typename TReal>
63void aiVector2t<TReal>::Set( TReal pX, TReal pY) {
64 x = pX; y = pY;
65}
66
67// ------------------------------------------------------------------------------------------------
68template <typename TReal>
69TReal aiVector2t<TReal>::SquareLength() const {
70 return x*x + y*y;
71}
72
73// ------------------------------------------------------------------------------------------------
74template <typename TReal>
75TReal aiVector2t<TReal>::Length() const {
76 return std::sqrt( SquareLength());
77}
78
79// ------------------------------------------------------------------------------------------------
80template <typename TReal>
81aiVector2t<TReal>& aiVector2t<TReal>::Normalize() {
82 *this /= Length();
83 return *this;
84}
85
86// ------------------------------------------------------------------------------------------------
87template <typename TReal>
88const aiVector2t<TReal>& aiVector2t<TReal>::operator += (const aiVector2t& o) {
89 x += o.x; y += o.y;
90 return *this;
91}
92
93// ------------------------------------------------------------------------------------------------
94template <typename TReal>
95const aiVector2t<TReal>& aiVector2t<TReal>::operator -= (const aiVector2t& o) {
96 x -= o.x; y -= o.y;
97 return *this;
98}
99
100// ------------------------------------------------------------------------------------------------
101template <typename TReal>
102const aiVector2t<TReal>& aiVector2t<TReal>::operator *= (TReal f) {
103 x *= f; y *= f;
104 return *this;
105}
106
107// ------------------------------------------------------------------------------------------------
108template <typename TReal>
109const aiVector2t<TReal>& aiVector2t<TReal>::operator /= (TReal f) {
110 x /= f; y /= f;
111 return *this;
112}
113
114// ------------------------------------------------------------------------------------------------
115template <typename TReal>
116TReal aiVector2t<TReal>::operator[](unsigned int i) const {
117 return *(&x + i);
118}
119
120// ------------------------------------------------------------------------------------------------
121template <typename TReal>
122TReal& aiVector2t<TReal>::operator[](unsigned int i) {
123 return *(&x + i);
124}
125
126// ------------------------------------------------------------------------------------------------
127template <typename TReal>
128bool aiVector2t<TReal>::operator== (const aiVector2t& other) const {
129 return x == other.x && y == other.y;
130}
131
132// ------------------------------------------------------------------------------------------------
133template <typename TReal>
134bool aiVector2t<TReal>::operator!= (const aiVector2t& other) const {
135 return x != other.x || y != other.y;
136}
137
138// ---------------------------------------------------------------------------
139template<typename TReal>
140bool aiVector2t<TReal>::Equal(const aiVector2t& other, TReal epsilon) const {
141 return
142 std::abs(x - other.x) <= epsilon &&
143 std::abs(y - other.y) <= epsilon;
144}
145
146// ------------------------------------------------------------------------------------------------
147template <typename TReal>
148aiVector2t<TReal>& aiVector2t<TReal>::operator= (TReal f) {
149 x = y = f;
150 return *this;
151}
152
153// ------------------------------------------------------------------------------------------------
154template <typename TReal>
155const aiVector2t<TReal> aiVector2t<TReal>::SymMul(const aiVector2t& o) {
156 return aiVector2t(x*o.x,y*o.y);
157}
158
159
160// ------------------------------------------------------------------------------------------------
161// symmetric addition
162template <typename TReal>
163inline aiVector2t<TReal> operator + (const aiVector2t<TReal>& v1, const aiVector2t<TReal>& v2)
164{
165 return aiVector2t<TReal>( v1.x + v2.x, v1.y + v2.y);
166}
167
168// ------------------------------------------------------------------------------------------------
169// symmetric subtraction
170template <typename TReal>
171inline aiVector2t<TReal> operator - (const aiVector2t<TReal>& v1, const aiVector2t<TReal>& v2)
172{
173 return aiVector2t<TReal>( v1.x - v2.x, v1.y - v2.y);
174}
175
176// ------------------------------------------------------------------------------------------------
177// scalar product
178template <typename TReal>
179inline TReal operator * (const aiVector2t<TReal>& v1, const aiVector2t<TReal>& v2)
180{
181 return v1.x*v2.x + v1.y*v2.y;
182}
183
184// ------------------------------------------------------------------------------------------------
185// scalar multiplication
186template <typename TReal>
187inline aiVector2t<TReal> operator * ( TReal f, const aiVector2t<TReal>& v)
188{
189 return aiVector2t<TReal>( f*v.x, f*v.y);
190}
191
192// ------------------------------------------------------------------------------------------------
193// and the other way around
194template <typename TReal>
195inline aiVector2t<TReal> operator * ( const aiVector2t<TReal>& v, TReal f)
196{
197 return aiVector2t<TReal>( f*v.x, f*v.y);
198}
199
200// ------------------------------------------------------------------------------------------------
201// scalar division
202template <typename TReal>
203inline aiVector2t<TReal> operator / ( const aiVector2t<TReal>& v, TReal f)
204{
205
206 return v * (1/f);
207}
208
209// ------------------------------------------------------------------------------------------------
210// vector division
211template <typename TReal>
212inline aiVector2t<TReal> operator / ( const aiVector2t<TReal>& v, const aiVector2t<TReal>& v2)
213{
214 return aiVector2t<TReal>(v.x / v2.x,v.y / v2.y);
215}
216
217// ------------------------------------------------------------------------------------------------
218// vector negation
219template <typename TReal>
220inline aiVector2t<TReal> operator - ( const aiVector2t<TReal>& v)
221{
222 return aiVector2t<TReal>( -v.x, -v.y);
223}
224
225#endif
226
227#endif // AI_VECTOR2D_INL_INC
228