| 
									
										
										
										
											2020-04-21 16:55:00 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 09:50:34 +02:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2020-04-21 16:55:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_float3.hh"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-09 10:27:24 +02:00
										 |  |  | namespace blender { | 
					
						
							| 
									
										
										
										
											2020-04-21 16:55:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct float2 { | 
					
						
							|  |  |  |   float x, y; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   float2() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   float2(const float *ptr) : x{ptr[0]}, y{ptr[1]} | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 18:02:59 +01:00
										 |  |  |   explicit float2(float value) : x(value), y(value) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   explicit float2(int value) : x(value), y(value) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 16:55:00 +02:00
										 |  |  |   float2(float x, float y) : x(x), y(y) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   float2(const float3 &other) : x(other.x), y(other.y) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   operator float *() | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return &x; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   operator const float *() const | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return &x; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 10:56:44 -04:00
										 |  |  |   float length() const | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return len_v2(*this); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 18:02:59 +01:00
										 |  |  |   float length_squared() const | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return len_squared_v2(*this); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 22:36:35 -04:00
										 |  |  |   bool is_zero() const | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return this->x == 0.0f && this->y == 0.0f; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-09 19:03:50 +02:00
										 |  |  |   float2 &operator+=(const float2 &other) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     x += other.x; | 
					
						
							|  |  |  |     y += other.y; | 
					
						
							|  |  |  |     return *this; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   float2 &operator-=(const float2 &other) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     x -= other.x; | 
					
						
							|  |  |  |     y -= other.y; | 
					
						
							|  |  |  |     return *this; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   float2 &operator*=(float factor) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     x *= factor; | 
					
						
							|  |  |  |     y *= factor; | 
					
						
							|  |  |  |     return *this; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   float2 &operator/=(float divisor) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     x /= divisor; | 
					
						
							|  |  |  |     y /= divisor; | 
					
						
							|  |  |  |     return *this; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 12:00:45 +01:00
										 |  |  |   uint64_t hash() const | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     uint64_t x1 = *reinterpret_cast<const uint32_t *>(&x); | 
					
						
							|  |  |  |     uint64_t x2 = *reinterpret_cast<const uint32_t *>(&y); | 
					
						
							|  |  |  |     return (x1 * 812519) ^ (x2 * 707951); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 16:55:00 +02:00
										 |  |  |   friend float2 operator+(const float2 &a, const float2 &b) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return {a.x + b.x, a.y + b.y}; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   friend float2 operator-(const float2 &a, const float2 &b) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return {a.x - b.x, a.y - b.y}; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   friend float2 operator*(const float2 &a, float b) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return {a.x * b, a.y * b}; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   friend float2 operator/(const float2 &a, float b) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     BLI_assert(b != 0.0f); | 
					
						
							|  |  |  |     return {a.x / b, a.y / b}; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   friend float2 operator*(float a, const float2 &b) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return b * a; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   friend std::ostream &operator<<(std::ostream &stream, const float2 &v) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     stream << "(" << v.x << ", " << v.y << ")"; | 
					
						
							|  |  |  |     return stream; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-08 14:40:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 10:56:44 -04:00
										 |  |  |   static float dot(const float2 &a, const float2 &b) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return a.x * b.x + a.y * b.y; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static float2 interpolate(const float2 &a, const float2 &b, float t) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return a * (1 - t) + b * t; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static float2 abs(const float2 &a) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return float2(fabsf(a.x), fabsf(a.y)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static float distance(const float2 &a, const float2 &b) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return (a - b).length(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static float distance_squared(const float2 &a, const float2 &b) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2021-01-11 14:58:29 +01:00
										 |  |  |     float2 diff = a - b; | 
					
						
							|  |  |  |     return float2::dot(diff, diff); | 
					
						
							| 
									
										
										
										
											2020-08-28 10:56:44 -04:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   struct isect_result { | 
					
						
							|  |  |  |     enum { | 
					
						
							|  |  |  |       LINE_LINE_COLINEAR = -1, | 
					
						
							|  |  |  |       LINE_LINE_NONE = 0, | 
					
						
							|  |  |  |       LINE_LINE_EXACT = 1, | 
					
						
							|  |  |  |       LINE_LINE_CROSS = 2, | 
					
						
							|  |  |  |     } kind; | 
					
						
							|  |  |  |     float lambda; | 
					
						
							|  |  |  |     float mu; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static isect_result isect_seg_seg(const float2 &v1, | 
					
						
							|  |  |  |                                     const float2 &v2, | 
					
						
							|  |  |  |                                     const float2 &v3, | 
					
						
							|  |  |  |                                     const float2 &v4); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-08 14:40:34 +02:00
										 |  |  |   friend bool operator==(const float2 &a, const float2 &b) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return a.x == b.x && a.y == b.y; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   friend bool operator!=(const float2 &a, const float2 &b) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return !(a == b); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-04-21 16:55:00 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-09 10:27:24 +02:00
										 |  |  | }  // namespace blender
 |