summaryrefslogtreecommitdiff
path: root/core/math/plane.h
blob: 92ebcd80249b67e0805b7581a0a9d45df6228734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*************************************************************************/
/*  plane.h                                                              */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)    */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#ifndef PLANE_H
#define PLANE_H

#include "vector3.h"

class Plane {
public:
	Vector3 normal;
	real_t d;

	void set_normal(const Vector3 &p_normal);
	_FORCE_INLINE_ Vector3 get_normal() const { return normal; }; ///Point is coplanar, CMP_EPSILON for precision

	void normalize();
	Plane normalized() const;

	/* Plane-Point operations */

	_FORCE_INLINE_ Vector3 center() const { return normal * d; }
	Vector3 get_any_point() const;
	Vector3 get_any_perpendicular_normal() const;

	_FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
	_FORCE_INLINE_ real_t distance_to(const Vector3 &p_point) const;
	_FORCE_INLINE_ bool has_point(const Vector3 &p_point, real_t _epsilon = CMP_EPSILON) const;

	/* intersections */

	bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = 0) const;
	bool intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3 *p_intersection) const;
	bool intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3 *p_intersection) const;

	_FORCE_INLINE_ Vector3 project(const Vector3 &p_point) const {

		return p_point - normal * distance_to(p_point);
	}

	/* misc */

	Plane operator-() const { return Plane(-normal, -d); }
	bool is_almost_like(const Plane &p_plane) const;

	_FORCE_INLINE_ bool operator==(const Plane &p_plane) const;
	_FORCE_INLINE_ bool operator!=(const Plane &p_plane) const;
	operator String() const;

	_FORCE_INLINE_ Plane() { d = 0; }
	_FORCE_INLINE_ Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d)
		: normal(p_a, p_b, p_c),
		  d(p_d){};

	_FORCE_INLINE_ Plane(const Vector3 &p_normal, real_t p_d);
	_FORCE_INLINE_ Plane(const Vector3 &p_point, const Vector3 &p_normal);
	_FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
};

bool Plane::is_point_over(const Vector3 &p_point) const {

	return (normal.dot(p_point) > d);
}

real_t Plane::distance_to(const Vector3 &p_point) const {

	return (normal.dot(p_point) - d);
}

bool Plane::has_point(const Vector3 &p_point, real_t _epsilon) const {

	real_t dist = normal.dot(p_point) - d;
	dist = ABS(dist);
	return (dist <= _epsilon);
}

Plane::Plane(const Vector3 &p_normal, real_t p_d)
	: normal(p_normal),
	  d(p_d) {
}

Plane::Plane(const Vector3 &p_point, const Vector3 &p_normal)
	: normal(p_normal),
	  d(p_normal.dot(p_point)) {
}

Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) {

	if (p_dir == CLOCKWISE)
		normal = (p_point1 - p_point3).cross(p_point1 - p_point2);
	else
		normal = (p_point1 - p_point2).cross(p_point1 - p_point3);

	normal.normalize();
	d = normal.dot(p_point1);
}

bool Plane::operator==(const Plane &p_plane) const {

	return normal == p_plane.normal && d == p_plane.d;
}

bool Plane::operator!=(const Plane &p_plane) const {

	return normal != p_plane.normal || d != p_plane.d;
}

#endif // PLANE_H