summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2017-03-05 16:44:50 +0100
committerRémi Verschelde <rverschelde@gmail.com>2017-03-05 16:44:50 +0100
commit5dbf1809c6e3e905b94b8764e99491e608122261 (patch)
tree5e5a5360db15d86d59ec8c6e4f7eb511388c5a9a /core/math
parent45438e9918d421b244bfd7776a30e67dc7f2d3e3 (diff)
A Whole New World (clang-format edition)
I can show you the code Pretty, with proper whitespace Tell me, coder, now when did You last write readable code? I can open your eyes Make you see your bad indent Force you to respect the style The core devs agreed upon A whole new world A new fantastic code format A de facto standard With some sugar Enforced with clang-format A whole new world A dazzling style we all dreamed of And when we read it through It's crystal clear That now we're in a whole new world of code
Diffstat (limited to 'core/math')
-rw-r--r--core/math/a_star.cpp308
-rw-r--r--core/math/a_star.h41
-rw-r--r--core/math/audio_frame.cpp1
-rw-r--r--core/math/audio_frame.h77
-rw-r--r--core/math/bsp_tree.cpp418
-rw-r--r--core/math/bsp_tree.h105
-rw-r--r--core/math/camera_matrix.cpp593
-rw-r--r--core/math/camera_matrix.h40
-rw-r--r--core/math/face3.cpp426
-rw-r--r--core/math/face3.h247
-rw-r--r--core/math/geometry.cpp836
-rw-r--r--core/math/geometry.h758
-rw-r--r--core/math/math_2d.cpp364
-rw-r--r--core/math/math_2d.h706
-rw-r--r--core/math/math_defs.h8
-rw-r--r--core/math/math_funcs.cpp76
-rw-r--r--core/math/math_funcs.h249
-rw-r--r--core/math/matrix3.cpp379
-rw-r--r--core/math/matrix3.h217
-rw-r--r--core/math/octree.h987
-rw-r--r--core/math/plane.cpp81
-rw-r--r--core/math/plane.h84
-rw-r--r--core/math/quat.cpp102
-rw-r--r--core/math/quat.h167
-rw-r--r--core/math/quick_hull.cpp299
-rw-r--r--core/math/quick_hull.h42
-rw-r--r--core/math/rect3.cpp355
-rw-r--r--core/math/rect3.h333
-rw-r--r--core/math/transform.cpp96
-rw-r--r--core/math/transform.h239
-rw-r--r--core/math/triangle_mesh.cpp416
-rw-r--r--core/math/triangle_mesh.h22
-rw-r--r--core/math/triangulate.cpp194
-rw-r--r--core/math/triangulate.h21
-rw-r--r--core/math/vector3.cpp79
-rw-r--r--core/math/vector3.h274
36 files changed, 4560 insertions, 5080 deletions
diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp
index 7e31957e3e..110185c2d2 100644
--- a/core/math/a_star.cpp
+++ b/core/math/a_star.cpp
@@ -29,55 +29,52 @@
#include "a_star.h"
#include "geometry.h"
-
int AStar::get_available_point_id() const {
if (points.empty()) {
return 1;
}
- return points.back()->key()+1;
+ return points.back()->key() + 1;
}
void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) {
- ERR_FAIL_COND(p_id<0);
+ ERR_FAIL_COND(p_id < 0);
if (!points.has(p_id)) {
- Point *pt = memnew( Point );
- pt->id=p_id;
- pt->pos=p_pos;
- pt->weight_scale=p_weight_scale;
- pt->prev_point=NULL;
- pt->last_pass=0;
- points[p_id]=pt;
+ Point *pt = memnew(Point);
+ pt->id = p_id;
+ pt->pos = p_pos;
+ pt->weight_scale = p_weight_scale;
+ pt->prev_point = NULL;
+ pt->last_pass = 0;
+ points[p_id] = pt;
} else {
- points[p_id]->pos=p_pos;
- points[p_id]->weight_scale=p_weight_scale;
+ points[p_id]->pos = p_pos;
+ points[p_id]->weight_scale = p_weight_scale;
}
}
-Vector3 AStar::get_point_pos(int p_id) const{
+Vector3 AStar::get_point_pos(int p_id) const {
- ERR_FAIL_COND_V(!points.has(p_id),Vector3());
+ ERR_FAIL_COND_V(!points.has(p_id), Vector3());
return points[p_id]->pos;
-
}
-real_t AStar::get_point_weight_scale(int p_id) const{
+real_t AStar::get_point_weight_scale(int p_id) const {
- ERR_FAIL_COND_V(!points.has(p_id),0);
+ ERR_FAIL_COND_V(!points.has(p_id), 0);
return points[p_id]->weight_scale;
-
}
-void AStar::remove_point(int p_id){
+void AStar::remove_point(int p_id) {
ERR_FAIL_COND(!points.has(p_id));
- Point* p = points[p_id];
+ Point *p = points[p_id];
- for(int i=0;i<p->neighbours.size();i++) {
+ for (int i = 0; i < p->neighbours.size(); i++) {
- Segment s(p_id,p->neighbours[i]->id);
+ Segment s(p_id, p->neighbours[i]->id);
segments.erase(s);
p->neighbours[i]->neighbours.erase(p);
}
@@ -86,54 +83,49 @@ void AStar::remove_point(int p_id){
points.erase(p_id);
}
-void AStar::connect_points(int p_id,int p_with_id){
+void AStar::connect_points(int p_id, int p_with_id) {
ERR_FAIL_COND(!points.has(p_id));
ERR_FAIL_COND(!points.has(p_with_id));
- ERR_FAIL_COND(p_id==p_with_id);
+ ERR_FAIL_COND(p_id == p_with_id);
-
- Point* a = points[p_id];
- Point* b = points[p_with_id];
+ Point *a = points[p_id];
+ Point *b = points[p_with_id];
a->neighbours.push_back(b);
b->neighbours.push_back(a);
- Segment s(p_id,p_with_id);
- if (s.from==p_id) {
- s.from_point=a;
- s.to_point=b;
+ Segment s(p_id, p_with_id);
+ if (s.from == p_id) {
+ s.from_point = a;
+ s.to_point = b;
} else {
- s.from_point=b;
- s.to_point=a;
+ s.from_point = b;
+ s.to_point = a;
}
segments.insert(s);
-
-
}
-void AStar::disconnect_points(int p_id,int p_with_id){
+void AStar::disconnect_points(int p_id, int p_with_id) {
- Segment s(p_id,p_with_id);
+ Segment s(p_id, p_with_id);
ERR_FAIL_COND(!segments.has(s));
-
segments.erase(s);
Point *a = points[p_id];
Point *b = points[p_with_id];
a->neighbours.erase(b);
b->neighbours.erase(a);
-
}
-bool AStar::are_points_connected(int p_id,int p_with_id) const{
+bool AStar::are_points_connected(int p_id, int p_with_id) const {
- Segment s(p_id,p_with_id);
+ Segment s(p_id, p_with_id);
return segments.has(s);
}
-void AStar::clear(){
+void AStar::clear() {
- for (const Map<int,Point*>::Element *E=points.front();E;E=E->next()) {
+ for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) {
memdelete(E->get());
}
@@ -141,142 +133,130 @@ void AStar::clear(){
points.clear();
}
+int AStar::get_closest_point(const Vector3 &p_point) const {
-int AStar::get_closest_point(const Vector3& p_point) const{
-
- int closest_id=-1;
- real_t closest_dist=1e20;
+ int closest_id = -1;
+ real_t closest_dist = 1e20;
- for (const Map<int,Point*>::Element *E=points.front();E;E=E->next()) {
+ for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) {
real_t d = p_point.distance_squared_to(E->get()->pos);
- if (closest_id<0 || d<closest_dist) {
- closest_dist=d;
- closest_id=E->key();
+ if (closest_id < 0 || d < closest_dist) {
+ closest_dist = d;
+ closest_id = E->key();
}
}
return closest_id;
-
-
}
-Vector3 AStar::get_closest_pos_in_segment(const Vector3& p_point) const {
+Vector3 AStar::get_closest_pos_in_segment(const Vector3 &p_point) const {
real_t closest_dist = 1e20;
- bool found=false;
+ bool found = false;
Vector3 closest_point;
+ for (const Set<Segment>::Element *E = segments.front(); E; E = E->next()) {
- for (const Set<Segment>::Element *E=segments.front();E;E=E->next()) {
-
- Vector3 segment[2]={
+ Vector3 segment[2] = {
E->get().from_point->pos,
E->get().to_point->pos,
};
- Vector3 p = Geometry::get_closest_point_to_segment(p_point,segment);
+ Vector3 p = Geometry::get_closest_point_to_segment(p_point, segment);
real_t d = p_point.distance_squared_to(p);
- if (!found || d<closest_dist) {
+ if (!found || d < closest_dist) {
- closest_point=p;
- closest_dist=d;
- found=true;
+ closest_point = p;
+ closest_dist = d;
+ found = true;
}
}
return closest_point;
}
-bool AStar::_solve(Point* begin_point, Point* end_point) {
+bool AStar::_solve(Point *begin_point, Point *end_point) {
pass++;
SelfList<Point>::List open_list;
- bool found_route=false;
-
+ bool found_route = false;
- for(int i=0;i<begin_point->neighbours.size();i++) {
+ for (int i = 0; i < begin_point->neighbours.size(); i++) {
Point *n = begin_point->neighbours[i];
- n->prev_point=begin_point;
- n->distance=n->pos.distance_to(begin_point->pos);
- n->distance*=n->weight_scale;
- n->last_pass=pass;
+ n->prev_point = begin_point;
+ n->distance = n->pos.distance_to(begin_point->pos);
+ n->distance *= n->weight_scale;
+ n->last_pass = pass;
open_list.add(&n->list);
- if (end_point==n) {
- found_route=true;
+ if (end_point == n) {
+ found_route = true;
break;
}
}
- while(!found_route) {
+ while (!found_route) {
- if (open_list.first()==NULL) {
+ if (open_list.first() == NULL) {
//could not find path sadly
break;
}
//check open list
- SelfList<Point> *least_cost_point=NULL;
- real_t least_cost=1e30;
+ SelfList<Point> *least_cost_point = NULL;
+ real_t least_cost = 1e30;
//this could be faster (cache previous results)
- for (SelfList<Point> *E=open_list.first();E;E=E->next()) {
+ for (SelfList<Point> *E = open_list.first(); E; E = E->next()) {
- Point *p=E->self();
+ Point *p = E->self();
- real_t cost=p->distance;
- cost+=p->pos.distance_to(end_point->pos);
- cost*=p->weight_scale;
+ real_t cost = p->distance;
+ cost += p->pos.distance_to(end_point->pos);
+ cost *= p->weight_scale;
- if (cost<least_cost) {
+ if (cost < least_cost) {
- least_cost_point=E;
- least_cost=cost;
+ least_cost_point = E;
+ least_cost = cost;
}
}
-
- Point *p=least_cost_point->self();
+ Point *p = least_cost_point->self();
//open the neighbours for search
int es = p->neighbours.size();
- for(int i=0;i<es;i++) {
-
-
- Point* e=p->neighbours[i];
+ for (int i = 0; i < es; i++) {
+ Point *e = p->neighbours[i];
real_t distance = p->pos.distance_to(e->pos) + p->distance;
- distance*=e->weight_scale;
-
-
+ distance *= e->weight_scale;
- if (e->last_pass==pass) {
+ if (e->last_pass == pass) {
//oh this was visited already, can we win the cost?
- if (e->distance>distance) {
+ if (e->distance > distance) {
- e->prev_point=p;
- e->distance=distance;
+ e->prev_point = p;
+ e->distance = distance;
}
} else {
//add to open neighbours
- e->prev_point=p;
- e->distance=distance;
- e->last_pass=pass; //mark as used
+ e->prev_point = p;
+ e->distance = distance;
+ e->last_pass = pass; //mark as used
open_list.add(&e->list);
- if (e==end_point) {
+ if (e == end_point) {
//oh my reached end! stop algorithm
- found_route=true;
+ found_route = true;
break;
-
}
-
}
}
@@ -287,46 +267,43 @@ bool AStar::_solve(Point* begin_point, Point* end_point) {
}
//clear the openf list
- while(open_list.first()) {
- open_list.remove( open_list.first() );
+ while (open_list.first()) {
+ open_list.remove(open_list.first());
}
return found_route;
-
}
PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
- ERR_FAIL_COND_V(!points.has(p_from_id),PoolVector<Vector3>());
- ERR_FAIL_COND_V(!points.has(p_to_id),PoolVector<Vector3>());
-
+ ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<Vector3>());
+ ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<Vector3>());
pass++;
- Point* a = points[p_from_id];
- Point* b = points[p_to_id];
+ Point *a = points[p_from_id];
+ Point *b = points[p_to_id];
- if (a==b) {
+ if (a == b) {
PoolVector<Vector3> ret;
ret.push_back(a->pos);
return ret;
}
+ Point *begin_point = a;
+ Point *end_point = b;
- Point *begin_point=a;
- Point *end_point=b;
-
- bool found_route=_solve(begin_point,end_point);
+ bool found_route = _solve(begin_point, end_point);
if (!found_route)
return PoolVector<Vector3>();
//midpoints
- Point *p=end_point;
- int pc=1; //begin point
- while(p!=begin_point) {
+ Point *p = end_point;
+ int pc = 1; //begin point
+ while (p != begin_point) {
pc++;
- p=p->prev_point;
+ p = p->prev_point;
}
PoolVector<Vector3> path;
@@ -335,54 +312,49 @@ PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
{
PoolVector<Vector3>::Write w = path.write();
- Point *p=end_point;
- int idx=pc-1;
- while(p!=begin_point) {
- w[idx--]=p->pos;
- p=p->prev_point;
+ Point *p = end_point;
+ int idx = pc - 1;
+ while (p != begin_point) {
+ w[idx--] = p->pos;
+ p = p->prev_point;
}
- w[0]=p->pos; //assign first
-
+ w[0] = p->pos; //assign first
}
return path;
-
}
-
PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
- ERR_FAIL_COND_V(!points.has(p_from_id),PoolVector<int>());
- ERR_FAIL_COND_V(!points.has(p_to_id),PoolVector<int>());
-
+ ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<int>());
+ ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<int>());
pass++;
- Point* a = points[p_from_id];
- Point* b = points[p_to_id];
+ Point *a = points[p_from_id];
+ Point *b = points[p_to_id];
- if (a==b) {
+ if (a == b) {
PoolVector<int> ret;
ret.push_back(a->id);
return ret;
}
+ Point *begin_point = a;
+ Point *end_point = b;
- Point *begin_point=a;
- Point *end_point=b;
-
- bool found_route=_solve(begin_point,end_point);
+ bool found_route = _solve(begin_point, end_point);
if (!found_route)
return PoolVector<int>();
//midpoints
- Point *p=end_point;
- int pc=1; //begin point
- while(p!=begin_point) {
+ Point *p = end_point;
+ int pc = 1; //begin point
+ while (p != begin_point) {
pc++;
- p=p->prev_point;
+ p = p->prev_point;
}
PoolVector<int> path;
@@ -391,15 +363,14 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
{
PoolVector<int>::Write w = path.write();
- p=end_point;
- int idx=pc-1;
- while(p!=begin_point) {
- w[idx--]=p->id;
- p=p->prev_point;
+ p = end_point;
+ int idx = pc - 1;
+ while (p != begin_point) {
+ w[idx--] = p->id;
+ p = p->prev_point;
}
- w[0]=p->id; //assign first
-
+ w[0] = p->id; //assign first
}
return path;
@@ -407,34 +378,31 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
void AStar::_bind_methods() {
- ClassDB::bind_method(D_METHOD("get_available_point_id"),&AStar::get_available_point_id);
- ClassDB::bind_method(D_METHOD("add_point","id","pos","weight_scale"),&AStar::add_point,DEFVAL(1.0));
- ClassDB::bind_method(D_METHOD("get_point_pos","id"),&AStar::get_point_pos);
- ClassDB::bind_method(D_METHOD("get_point_weight_scale","id"),&AStar::get_point_weight_scale);
- ClassDB::bind_method(D_METHOD("remove_point","id"),&AStar::remove_point);
-
- ClassDB::bind_method(D_METHOD("connect_points","id","to_id"),&AStar::connect_points);
- ClassDB::bind_method(D_METHOD("disconnect_points","id","to_id"),&AStar::disconnect_points);
- ClassDB::bind_method(D_METHOD("are_points_connected","id","to_id"),&AStar::are_points_connected);
+ ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id);
+ ClassDB::bind_method(D_METHOD("add_point", "id", "pos", "weight_scale"), &AStar::add_point, DEFVAL(1.0));
+ ClassDB::bind_method(D_METHOD("get_point_pos", "id"), &AStar::get_point_pos);
+ ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale);
+ ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar::remove_point);
- ClassDB::bind_method(D_METHOD("clear"),&AStar::clear);
+ ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id"), &AStar::connect_points);
+ ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar::disconnect_points);
+ ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar::are_points_connected);
- ClassDB::bind_method(D_METHOD("get_closest_point","to_pos"),&AStar::get_closest_point);
- ClassDB::bind_method(D_METHOD("get_closest_pos_in_segment","to_pos"),&AStar::get_closest_pos_in_segment);
+ ClassDB::bind_method(D_METHOD("clear"), &AStar::clear);
- ClassDB::bind_method(D_METHOD("get_point_path","from_id","to_id"),&AStar::get_point_path);
- ClassDB::bind_method(D_METHOD("get_id_path","from_id","to_id"),&AStar::get_id_path);
+ ClassDB::bind_method(D_METHOD("get_closest_point", "to_pos"), &AStar::get_closest_point);
+ ClassDB::bind_method(D_METHOD("get_closest_pos_in_segment", "to_pos"), &AStar::get_closest_pos_in_segment);
+ ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path);
+ ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path);
}
-
AStar::AStar() {
- pass=1;
+ pass = 1;
}
-
AStar::~AStar() {
- pass=1;
+ pass = 1;
}
diff --git a/core/math/a_star.h b/core/math/a_star.h
index c4c955ed2d..2ac855737c 100644
--- a/core/math/a_star.h
+++ b/core/math/a_star.h
@@ -35,10 +35,9 @@
@author Juan Linietsky <reduzio@gmail.com>
*/
-class AStar: public Reference {
-
- GDCLASS(AStar,Reference)
+class AStar : public Reference {
+ GDCLASS(AStar, Reference)
uint64_t pass;
@@ -51,16 +50,17 @@ class AStar: public Reference {
real_t weight_scale;
uint64_t last_pass;
- Vector<Point*> neighbours;
+ Vector<Point *> neighbours;
//used for pathfinding
Point *prev_point;
real_t distance;
- Point() : list(this) {}
+ Point()
+ : list(this) {}
};
- Map<int,Point*> points;
+ Map<int, Point *> points;
struct Segment {
union {
@@ -74,44 +74,41 @@ class AStar: public Reference {
Point *from_point;
Point *to_point;
- bool operator<(const Segment& p_s) const { return key<p_s.key; }
- Segment() { key=0; }
- Segment(int p_from,int p_to) {
+ bool operator<(const Segment &p_s) const { return key < p_s.key; }
+ Segment() { key = 0; }
+ Segment(int p_from, int p_to) {
if (p_from > p_to) {
- SWAP(p_from,p_to);
+ SWAP(p_from, p_to);
}
- from=p_from;
- to=p_to;
+ from = p_from;
+ to = p_to;
}
};
-
Set<Segment> segments;
bool _solve(Point *begin_point, Point *end_point);
protected:
-
static void _bind_methods();
-public:
+public:
int get_available_point_id() const;
- void add_point(int p_id,const Vector3& p_pos,real_t p_weight_scale=1);
+ void add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
Vector3 get_point_pos(int p_id) const;
real_t get_point_weight_scale(int p_id) const;
void remove_point(int p_id);
- void connect_points(int p_id,int p_with_id);
- void disconnect_points(int p_id,int p_with_id);
- bool are_points_connected(int p_id,int p_with_id) const;
+ void connect_points(int p_id, int p_with_id);
+ void disconnect_points(int p_id, int p_with_id);
+ bool are_points_connected(int p_id, int p_with_id) const;
void clear();
-
- int get_closest_point(const Vector3& p_point) const;
- Vector3 get_closest_pos_in_segment(const Vector3& p_point) const;
+ int get_closest_point(const Vector3 &p_point) const;
+ Vector3 get_closest_pos_in_segment(const Vector3 &p_point) const;
PoolVector<Vector3> get_point_path(int p_from_id, int p_to_id);
PoolVector<int> get_id_path(int p_from_id, int p_to_id);
diff --git a/core/math/audio_frame.cpp b/core/math/audio_frame.cpp
index aae1561a84..e56157ffef 100644
--- a/core/math/audio_frame.cpp
+++ b/core/math/audio_frame.cpp
@@ -27,4 +27,3 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "audio_frame.h"
-
diff --git a/core/math/audio_frame.h b/core/math/audio_frame.h
index e4e93f34fa..dd43f48df4 100644
--- a/core/math/audio_frame.h
+++ b/core/math/audio_frame.h
@@ -31,9 +31,7 @@
#include "typedefs.h"
-
-static inline float undenormalise(volatile float f)
-{
+static inline float undenormalise(volatile float f) {
union {
uint32_t i;
float f;
@@ -46,42 +44,71 @@ static inline float undenormalise(volatile float f)
return (v.i & 0x7f800000) < 0x08000000 ? 0.0f : f;
}
-
struct AudioFrame {
//left and right samples
- float l,r;
+ float l, r;
- _ALWAYS_INLINE_ const float& operator[](int idx) const { return idx==0?l:r; }
- _ALWAYS_INLINE_ float& operator[](int idx) { return idx==0?l:r; }
+ _ALWAYS_INLINE_ const float &operator[](int idx) const { return idx == 0 ? l : r; }
+ _ALWAYS_INLINE_ float &operator[](int idx) { return idx == 0 ? l : r; }
- _ALWAYS_INLINE_ AudioFrame operator+(const AudioFrame& p_frame) const { return AudioFrame(l+p_frame.l,r+p_frame.r); }
- _ALWAYS_INLINE_ AudioFrame operator-(const AudioFrame& p_frame) const { return AudioFrame(l-p_frame.l,r-p_frame.r); }
- _ALWAYS_INLINE_ AudioFrame operator*(const AudioFrame& p_frame) const { return AudioFrame(l*p_frame.l,r*p_frame.r); }
- _ALWAYS_INLINE_ AudioFrame operator/(const AudioFrame& p_frame) const { return AudioFrame(l/p_frame.l,r/p_frame.r); }
+ _ALWAYS_INLINE_ AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(l + p_frame.l, r + p_frame.r); }
+ _ALWAYS_INLINE_ AudioFrame operator-(const AudioFrame &p_frame) const { return AudioFrame(l - p_frame.l, r - p_frame.r); }
+ _ALWAYS_INLINE_ AudioFrame operator*(const AudioFrame &p_frame) const { return AudioFrame(l * p_frame.l, r * p_frame.r); }
+ _ALWAYS_INLINE_ AudioFrame operator/(const AudioFrame &p_frame) const { return AudioFrame(l / p_frame.l, r / p_frame.r); }
- _ALWAYS_INLINE_ AudioFrame operator+(float p_sample) const { return AudioFrame(l+p_sample,r+p_sample); }
- _ALWAYS_INLINE_ AudioFrame operator-(float p_sample) const { return AudioFrame(l-p_sample,r-p_sample); }
- _ALWAYS_INLINE_ AudioFrame operator*(float p_sample) const { return AudioFrame(l*p_sample,r*p_sample); }
- _ALWAYS_INLINE_ AudioFrame operator/(float p_sample) const { return AudioFrame(l/p_sample,r/p_sample); }
+ _ALWAYS_INLINE_ AudioFrame operator+(float p_sample) const { return AudioFrame(l + p_sample, r + p_sample); }
+ _ALWAYS_INLINE_ AudioFrame operator-(float p_sample) const { return AudioFrame(l - p_sample, r - p_sample); }
+ _ALWAYS_INLINE_ AudioFrame operator*(float p_sample) const { return AudioFrame(l * p_sample, r * p_sample); }
+ _ALWAYS_INLINE_ AudioFrame operator/(float p_sample) const { return AudioFrame(l / p_sample, r / p_sample); }
- _ALWAYS_INLINE_ void operator+=(const AudioFrame& p_frame) { l+=p_frame.l; r+=p_frame.r; }
- _ALWAYS_INLINE_ void operator-=(const AudioFrame& p_frame) { l-=p_frame.l; r-=p_frame.r; }
- _ALWAYS_INLINE_ void operator*=(const AudioFrame& p_frame) { l*=p_frame.l; r*=p_frame.r; }
- _ALWAYS_INLINE_ void operator/=(const AudioFrame& p_frame) { l/=p_frame.l; r/=p_frame.r; }
+ _ALWAYS_INLINE_ void operator+=(const AudioFrame &p_frame) {
+ l += p_frame.l;
+ r += p_frame.r;
+ }
+ _ALWAYS_INLINE_ void operator-=(const AudioFrame &p_frame) {
+ l -= p_frame.l;
+ r -= p_frame.r;
+ }
+ _ALWAYS_INLINE_ void operator*=(const AudioFrame &p_frame) {
+ l *= p_frame.l;
+ r *= p_frame.r;
+ }
+ _ALWAYS_INLINE_ void operator/=(const AudioFrame &p_frame) {
+ l /= p_frame.l;
+ r /= p_frame.r;
+ }
- _ALWAYS_INLINE_ void operator+=(float p_sample) { l+=p_sample; r+=p_sample; }
- _ALWAYS_INLINE_ void operator-=(float p_sample) { l-=p_sample; r-=p_sample; }
- _ALWAYS_INLINE_ void operator*=(float p_sample) { l*=p_sample; r*=p_sample; }
- _ALWAYS_INLINE_ void operator/=(float p_sample) { l/=p_sample; r/=p_sample; }
+ _ALWAYS_INLINE_ void operator+=(float p_sample) {
+ l += p_sample;
+ r += p_sample;
+ }
+ _ALWAYS_INLINE_ void operator-=(float p_sample) {
+ l -= p_sample;
+ r -= p_sample;
+ }
+ _ALWAYS_INLINE_ void operator*=(float p_sample) {
+ l *= p_sample;
+ r *= p_sample;
+ }
+ _ALWAYS_INLINE_ void operator/=(float p_sample) {
+ l /= p_sample;
+ r /= p_sample;
+ }
_ALWAYS_INLINE_ void undenormalise() {
l = ::undenormalise(l);
r = ::undenormalise(r);
}
- _ALWAYS_INLINE_ AudioFrame(float p_l, float p_r) {l=p_l; r=p_r;}
- _ALWAYS_INLINE_ AudioFrame(const AudioFrame& p_frame) {l=p_frame.l; r=p_frame.r;}
+ _ALWAYS_INLINE_ AudioFrame(float p_l, float p_r) {
+ l = p_l;
+ r = p_r;
+ }
+ _ALWAYS_INLINE_ AudioFrame(const AudioFrame &p_frame) {
+ l = p_frame.l;
+ r = p_frame.r;
+ }
_ALWAYS_INLINE_ AudioFrame() {}
};
diff --git a/core/math/bsp_tree.cpp b/core/math/bsp_tree.cpp
index 1ca6385032..ef229a0553 100644
--- a/core/math/bsp_tree.cpp
+++ b/core/math/bsp_tree.cpp
@@ -30,32 +30,31 @@
#include "error_macros.h"
#include "print_string.h"
-
-void BSP_Tree::from_aabb(const Rect3& p_aabb) {
+void BSP_Tree::from_aabb(const Rect3 &p_aabb) {
planes.clear();
- for(int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
Vector3 n;
- n[i]=1;
- planes.push_back(Plane(n,p_aabb.pos[i]+p_aabb.size[i]));
- planes.push_back(Plane(-n,-p_aabb.pos[i]));
+ n[i] = 1;
+ planes.push_back(Plane(n, p_aabb.pos[i] + p_aabb.size[i]));
+ planes.push_back(Plane(-n, -p_aabb.pos[i]));
}
nodes.clear();
- for(int i=0;i<6;i++) {
+ for (int i = 0; i < 6; i++) {
Node n;
- n.plane=i;
- n.under=(i==0)?UNDER_LEAF:i-1;
- n.over=OVER_LEAF;
+ n.plane = i;
+ n.under = (i == 0) ? UNDER_LEAF : i - 1;
+ n.over = OVER_LEAF;
nodes.push_back(n);
}
- aabb=p_aabb;
- error_radius=0;
+ aabb = p_aabb;
+ error_radius = 0;
}
Vector<BSP_Tree::Node> BSP_Tree::get_nodes() const {
@@ -72,143 +71,136 @@ Rect3 BSP_Tree::get_aabb() const {
return aabb;
}
-int BSP_Tree::_get_points_inside(int p_node,const Vector3* p_points,int *p_indices, const Vector3& p_center,const Vector3& p_half_extents,int p_indices_count) const {
-
+int BSP_Tree::_get_points_inside(int p_node, const Vector3 *p_points, int *p_indices, const Vector3 &p_center, const Vector3 &p_half_extents, int p_indices_count) const {
- const Node *node =&nodes[p_node];
+ const Node *node = &nodes[p_node];
const Plane &p = planes[node->plane];
Vector3 min(
- (p.normal.x>0) ? -p_half_extents.x : p_half_extents.x,
- (p.normal.y>0) ? -p_half_extents.y : p_half_extents.y,
- (p.normal.z>0) ? -p_half_extents.z : p_half_extents.z
- );
- Vector3 max=-min;
- max+=p_center;
- min+=p_center;
+ (p.normal.x > 0) ? -p_half_extents.x : p_half_extents.x,
+ (p.normal.y > 0) ? -p_half_extents.y : p_half_extents.y,
+ (p.normal.z > 0) ? -p_half_extents.z : p_half_extents.z);
+ Vector3 max = -min;
+ max += p_center;
+ min += p_center;
real_t dist_min = p.distance_to(min);
real_t dist_max = p.distance_to(max);
- if ((dist_min * dist_max) < CMP_EPSILON ) { //intersection, test point by point
-
+ if ((dist_min * dist_max) < CMP_EPSILON) { //intersection, test point by point
- int under_count=0;
+ int under_count = 0;
//sort points, so the are under first, over last
- for(int i=0;i<p_indices_count;i++) {
+ for (int i = 0; i < p_indices_count; i++) {
- int index=p_indices[i];
+ int index = p_indices[i];
if (p.is_point_over(p_points[index])) {
// kind of slow (but cache friendly), should try something else,
// but this is a corner case most of the time
- for(int j=index;j<p_indices_count-1;j++)
- p_indices[j]=p_indices[j+1];
+ for (int j = index; j < p_indices_count - 1; j++)
+ p_indices[j] = p_indices[j + 1];
- p_indices[p_indices_count-1]=index;
+ p_indices[p_indices_count - 1] = index;
} else {
under_count++;
}
-
}
- int total=0;
+ int total = 0;
- if (under_count>0) {
- if (node->under==UNDER_LEAF) {
- total+=under_count;
+ if (under_count > 0) {
+ if (node->under == UNDER_LEAF) {
+ total += under_count;
} else {
- total+=_get_points_inside(node->under,p_points,p_indices,p_center,p_half_extents,under_count);
+ total += _get_points_inside(node->under, p_points, p_indices, p_center, p_half_extents, under_count);
}
}
- if (under_count!=p_indices_count) {
- if (node->over==OVER_LEAF) {
+ if (under_count != p_indices_count) {
+ if (node->over == OVER_LEAF) {
//total+=0 //if they are over an OVER_LEAF, they are outside the model
} else {
- total+=_get_points_inside(node->over,p_points,&p_indices[under_count],p_center,p_half_extents,p_indices_count-under_count);
+ total += _get_points_inside(node->over, p_points, &p_indices[under_count], p_center, p_half_extents, p_indices_count - under_count);
}
}
return total;
- } else if (dist_min > 0 ) { //all points over plane
+ } else if (dist_min > 0) { //all points over plane
- if (node->over==OVER_LEAF) {
+ if (node->over == OVER_LEAF) {
return 0; // all these points are not visible
}
+ return _get_points_inside(node->over, p_points, p_indices, p_center, p_half_extents, p_indices_count);
+ } else if (dist_min <= 0) { //all points behind plane
- return _get_points_inside(node->over,p_points,p_indices,p_center,p_half_extents,p_indices_count);
- } else if (dist_min <= 0 ) { //all points behind plane
-
- if (node->under==UNDER_LEAF) {
+ if (node->under == UNDER_LEAF) {
return p_indices_count; // all these points are visible
}
- return _get_points_inside(node->under,p_points,p_indices,p_center,p_half_extents,p_indices_count);
+ return _get_points_inside(node->under, p_points, p_indices, p_center, p_half_extents, p_indices_count);
}
return 0;
}
-int BSP_Tree::get_points_inside(const Vector3* p_points,int p_point_count) const {
-
+int BSP_Tree::get_points_inside(const Vector3 *p_points, int p_point_count) const {
- if (nodes.size()==0)
+ if (nodes.size() == 0)
return 0;
#if 1
-//this version is easier to debug, and and MUCH faster in real world cases
+ //this version is easier to debug, and and MUCH faster in real world cases
int pass_count = 0;
- const Node *nodesptr=&nodes[0];
- const Plane *planesptr=&planes[0];
- int plane_count=planes.size();
- int node_count=nodes.size();
+ const Node *nodesptr = &nodes[0];
+ const Plane *planesptr = &planes[0];
+ int plane_count = planes.size();
+ int node_count = nodes.size();
- if (node_count==0) // no nodes!
+ if (node_count == 0) // no nodes!
return 0;
- for(int i=0;i<p_point_count;i++) {
+ for (int i = 0; i < p_point_count; i++) {
- const Vector3& point = p_points[i];
+ const Vector3 &point = p_points[i];
if (!aabb.has_point(point)) {
continue;
}
- int idx=node_count-1;
+ int idx = node_count - 1;
- bool pass=false;
+ bool pass = false;
- while(true) {
+ while (true) {
- if (idx==OVER_LEAF) {
- pass=false;
+ if (idx == OVER_LEAF) {
+ pass = false;
break;
- } else if (idx==UNDER_LEAF) {
- pass=true;
+ } else if (idx == UNDER_LEAF) {
+ pass = true;
break;
}
- uint16_t plane=nodesptr[ idx ].plane;
+ uint16_t plane = nodesptr[idx].plane;
#ifdef DEBUG_ENABLED
- ERR_FAIL_INDEX_V( plane, plane_count, false );
+ ERR_FAIL_INDEX_V(plane, plane_count, false);
#endif
- idx = planesptr[ nodesptr[ idx ].plane ].is_point_over(point) ? nodes[ idx ].over : nodes[ idx ].under;
+ idx = planesptr[nodesptr[idx].plane].is_point_over(point) ? nodes[idx].over : nodes[idx].under;
#ifdef DEBUG_ENABLED
- ERR_FAIL_COND_V( idx<MAX_NODES && idx>=node_count, false );
+ ERR_FAIL_COND_V(idx < MAX_NODES && idx >= node_count, false);
#endif
-
}
if (pass)
@@ -218,69 +210,65 @@ int BSP_Tree::get_points_inside(const Vector3* p_points,int p_point_count) const
return pass_count;
#else
-//this version scales better but it's slower for real world cases
+ //this version scales better but it's slower for real world cases
- int *indices = (int*)alloca(p_point_count*sizeof(int));
+ int *indices = (int *)alloca(p_point_count * sizeof(int));
AABB bounds;
- for(int i=0;i<p_point_count;i++) {
+ for (int i = 0; i < p_point_count; i++) {
- indices[i]=i;
- if (i==0)
- bounds.pos=p_points[i];
+ indices[i] = i;
+ if (i == 0)
+ bounds.pos = p_points[i];
else
bounds.expand_to(p_points[i]);
-
}
- Vector3 half_extents = bounds.size/2.0;
- return _get_points_inside(nodes.size()+1,p_points,indices,bounds.pos+half_extents,half_extents,p_point_count);
+ Vector3 half_extents = bounds.size / 2.0;
+ return _get_points_inside(nodes.size() + 1, p_points, indices, bounds.pos + half_extents, half_extents, p_point_count);
#endif
}
-
-
-bool BSP_Tree::point_is_inside(const Vector3& p_point) const {
+bool BSP_Tree::point_is_inside(const Vector3 &p_point) const {
if (!aabb.has_point(p_point)) {
return false;
}
- int node_count=nodes.size();
+ int node_count = nodes.size();
- if (node_count==0) // no nodes!
+ if (node_count == 0) // no nodes!
return false;
+ const Node *nodesptr = &nodes[0];
+ const Plane *planesptr = &planes[0];
+ int plane_count = planes.size();
- const Node *nodesptr=&nodes[0];
- const Plane *planesptr=&planes[0];
- int plane_count=planes.size();
-
- int idx=node_count-1;
- int steps=0;
+ int idx = node_count - 1;
+ int steps = 0;
- while(true) {
+ while (true) {
- if (idx==OVER_LEAF) {
+ if (idx == OVER_LEAF) {
return false;
}
- if (idx==UNDER_LEAF) {
+ if (idx == UNDER_LEAF) {
return true;
}
- uint16_t plane=nodesptr[ idx ].plane;
+ uint16_t plane = nodesptr[idx].plane;
#ifdef DEBUG_ENABLED
- ERR_FAIL_INDEX_V( plane, plane_count, false );
+ ERR_FAIL_INDEX_V(plane, plane_count, false);
#endif
- bool over = planesptr[ nodesptr[ idx ].plane ].is_point_over(p_point);
+ bool over = planesptr[nodesptr[idx].plane].is_point_over(p_point);
- idx = over ? nodes[ idx ].over : nodes[ idx ].under;
+ idx = over ? nodes[idx].over : nodes[idx].under;
#ifdef DEBUG_ENABLED
- ERR_FAIL_COND_V( idx<MAX_NODES && idx>=node_count, false );
+ ERR_FAIL_COND_V(idx < MAX_NODES && idx >= node_count, false);
#endif
steps++;
@@ -289,44 +277,42 @@ bool BSP_Tree::point_is_inside(const Vector3& p_point) const {
return false;
}
-
-static int _bsp_find_best_half_plane(const Face3* p_faces,const Vector<int>& p_indices,real_t p_tolerance) {
+static int _bsp_find_best_half_plane(const Face3 *p_faces, const Vector<int> &p_indices, real_t p_tolerance) {
int ic = p_indices.size();
- const int*indices=p_indices.ptr();
+ const int *indices = p_indices.ptr();
int best_plane = -1;
real_t best_plane_cost = 1e20;
// Loop to find the polygon that best divides the set.
- for (int i=0;i<ic;i++) {
+ for (int i = 0; i < ic; i++) {
- const Face3& f=p_faces[ indices[i] ];
+ const Face3 &f = p_faces[indices[i]];
Plane p = f.get_plane();
- int num_over=0,num_under=0,num_spanning=0;
+ int num_over = 0, num_under = 0, num_spanning = 0;
- for(int j=0;j<ic;j++) {
+ for (int j = 0; j < ic; j++) {
- if (i==j)
+ if (i == j)
continue;
- const Face3& g=p_faces[ indices[j] ];
- int over=0,under=0;
+ const Face3 &g = p_faces[indices[j]];
+ int over = 0, under = 0;
- for(int k=0;k<3;k++) {
+ for (int k = 0; k < 3; k++) {
real_t d = p.distance_to(g.vertex[j]);
- if (Math::abs(d)>p_tolerance) {
+ if (Math::abs(d) > p_tolerance) {
if (d > 0)
over++;
else
under++;
}
-
}
if (over && under)
@@ -335,13 +321,10 @@ static int _bsp_find_best_half_plane(const Face3* p_faces,const Vector<int>& p_i
num_over++;
else
num_under++;
-
}
-
-
//real_t split_cost = num_spanning / (real_t) face_count;
- real_t relation = Math::abs(num_over-num_under) / (real_t) ic;
+ real_t relation = Math::abs(num_over - num_under) / (real_t)ic;
// being honest, i never found a way to add split cost to the mix in a meaninguful way
// in this engine, also, will likely be ignored anyway
@@ -349,59 +332,55 @@ static int _bsp_find_best_half_plane(const Face3* p_faces,const Vector<int>& p_i
real_t plane_cost = /*split_cost +*/ relation;
//printf("plane %i, %i over, %i under, %i spanning, cost is %g\n",i,num_over,num_under,num_spanning,plane_cost);
- if (plane_cost<best_plane_cost) {
+ if (plane_cost < best_plane_cost) {
- best_plane=i;
- best_plane_cost=plane_cost;
+ best_plane = i;
+ best_plane_cost = plane_cost;
}
-
}
return best_plane;
-
}
+static int _bsp_create_node(const Face3 *p_faces, const Vector<int> &p_indices, Vector<Plane> &p_planes, Vector<BSP_Tree::Node> &p_nodes, real_t p_tolerance) {
-static int _bsp_create_node(const Face3 *p_faces,const Vector<int>& p_indices,Vector<Plane> &p_planes, Vector<BSP_Tree::Node> &p_nodes,real_t p_tolerance) {
-
- ERR_FAIL_COND_V( p_nodes.size() == BSP_Tree::MAX_NODES, -1 );
+ ERR_FAIL_COND_V(p_nodes.size() == BSP_Tree::MAX_NODES, -1);
// should not reach here
- ERR_FAIL_COND_V( p_indices.size() == 0, -1 )
+ ERR_FAIL_COND_V(p_indices.size() == 0, -1)
int ic = p_indices.size();
- const int*indices=p_indices.ptr();
+ const int *indices = p_indices.ptr();
- int divisor_idx = _bsp_find_best_half_plane(p_faces,p_indices,p_tolerance);
+ int divisor_idx = _bsp_find_best_half_plane(p_faces, p_indices, p_tolerance);
// returned error
- ERR_FAIL_COND_V( divisor_idx<0 , -1 );
-
+ ERR_FAIL_COND_V(divisor_idx < 0, -1);
Vector<int> faces_over;
Vector<int> faces_under;
- Plane divisor_plane=p_faces[ indices[divisor_idx] ].get_plane();
+ Plane divisor_plane = p_faces[indices[divisor_idx]].get_plane();
- for (int i=0;i<ic;i++) {
+ for (int i = 0; i < ic; i++) {
- if (i==divisor_idx)
+ if (i == divisor_idx)
continue;
- const Face3& f=p_faces[ indices[i] ];
+ const Face3 &f = p_faces[indices[i]];
/*
if (f.get_plane().is_almost_like(divisor_plane))
continue;
*/
- int over_count=0;
- int under_count=0;
+ int over_count = 0;
+ int under_count = 0;
- for(int j=0;j<3;j++) {
+ for (int j = 0; j < 3; j++) {
real_t d = divisor_plane.distance_to(f.vertex[j]);
- if (Math::abs(d)>p_tolerance) {
+ if (Math::abs(d) > p_tolerance) {
if (d > 0)
over_count++;
@@ -411,183 +390,169 @@ static int _bsp_create_node(const Face3 *p_faces,const Vector<int>& p_indices,Ve
}
if (over_count)
- faces_over.push_back( indices[i] );
+ faces_over.push_back(indices[i]);
if (under_count)
- faces_under.push_back( indices[i] );
-
+ faces_under.push_back(indices[i]);
}
+ uint16_t over_idx = BSP_Tree::OVER_LEAF, under_idx = BSP_Tree::UNDER_LEAF;
+ if (faces_over.size() > 0) { //have facess above?
- uint16_t over_idx=BSP_Tree::OVER_LEAF,under_idx=BSP_Tree::UNDER_LEAF;
-
- if (faces_over.size()>0) { //have facess above?
-
- int idx = _bsp_create_node( p_faces, faces_over, p_planes, p_nodes,p_tolerance );
- if (idx>=0)
- over_idx=idx;
+ int idx = _bsp_create_node(p_faces, faces_over, p_planes, p_nodes, p_tolerance);
+ if (idx >= 0)
+ over_idx = idx;
}
- if (faces_under.size()>0) { //have facess above?
+ if (faces_under.size() > 0) { //have facess above?
- int idx = _bsp_create_node( p_faces,faces_under, p_planes, p_nodes,p_tolerance );
- if (idx>=0)
- under_idx=idx;
+ int idx = _bsp_create_node(p_faces, faces_under, p_planes, p_nodes, p_tolerance);
+ if (idx >= 0)
+ under_idx = idx;
}
/* Create the node */
// find existing divisor plane
- int divisor_plane_idx=-1;
-
+ int divisor_plane_idx = -1;
- for (int i=0;i<p_planes.size();i++) {
+ for (int i = 0; i < p_planes.size(); i++) {
- if (p_planes[i].is_almost_like( divisor_plane )) {
- divisor_plane_idx=i;
+ if (p_planes[i].is_almost_like(divisor_plane)) {
+ divisor_plane_idx = i;
break;
}
}
- if (divisor_plane_idx==-1) {
+ if (divisor_plane_idx == -1) {
- ERR_FAIL_COND_V( p_planes.size() == BSP_Tree::MAX_PLANES, -1 );
- divisor_plane_idx=p_planes.size();
- p_planes.push_back( divisor_plane );
+ ERR_FAIL_COND_V(p_planes.size() == BSP_Tree::MAX_PLANES, -1);
+ divisor_plane_idx = p_planes.size();
+ p_planes.push_back(divisor_plane);
}
BSP_Tree::Node node;
- node.plane=divisor_plane_idx;
- node.under=under_idx;
- node.over=over_idx;
+ node.plane = divisor_plane_idx;
+ node.under = under_idx;
+ node.over = over_idx;
p_nodes.push_back(node);
- return p_nodes.size()-1;
+ return p_nodes.size() - 1;
}
-
BSP_Tree::operator Variant() const {
-
Dictionary d;
- d["error_radius"]=error_radius;
+ d["error_radius"] = error_radius;
Vector<real_t> plane_values;
- plane_values.resize(planes.size()*4);
+ plane_values.resize(planes.size() * 4);
- for(int i=0;i<planes.size();i++) {
+ for (int i = 0; i < planes.size(); i++) {
- plane_values[i*4+0]=planes[i].normal.x;
- plane_values[i*4+1]=planes[i].normal.y;
- plane_values[i*4+2]=planes[i].normal.z;
- plane_values[i*4+3]=planes[i].d;
+ plane_values[i * 4 + 0] = planes[i].normal.x;
+ plane_values[i * 4 + 1] = planes[i].normal.y;
+ plane_values[i * 4 + 2] = planes[i].normal.z;
+ plane_values[i * 4 + 3] = planes[i].d;
}
- d["planes"]=plane_values;
+ d["planes"] = plane_values;
PoolVector<int> dst_nodes;
- dst_nodes.resize(nodes.size()*3);
+ dst_nodes.resize(nodes.size() * 3);
- for(int i=0;i<nodes.size();i++) {
+ for (int i = 0; i < nodes.size(); i++) {
- dst_nodes.set(i*3+0,nodes[i].over);
- dst_nodes.set(i*3+1,nodes[i].under);
- dst_nodes.set(i*3+2,nodes[i].plane);
+ dst_nodes.set(i * 3 + 0, nodes[i].over);
+ dst_nodes.set(i * 3 + 1, nodes[i].under);
+ dst_nodes.set(i * 3 + 2, nodes[i].plane);
}
-
- d["nodes"]=dst_nodes;
+ d["nodes"] = dst_nodes;
d["aabb"] = aabb;
return Variant(d);
}
BSP_Tree::BSP_Tree() {
-
}
+BSP_Tree::BSP_Tree(const Variant &p_variant) {
-BSP_Tree::BSP_Tree(const Variant& p_variant) {
-
- Dictionary d=p_variant;
+ Dictionary d = p_variant;
ERR_FAIL_COND(!d.has("nodes"));
ERR_FAIL_COND(!d.has("planes"));
ERR_FAIL_COND(!d.has("aabb"));
ERR_FAIL_COND(!d.has("error_radius"));
PoolVector<int> src_nodes = d["nodes"];
- ERR_FAIL_COND(src_nodes.size()%3);
+ ERR_FAIL_COND(src_nodes.size() % 3);
+ if (d["planes"].get_type() == Variant::POOL_REAL_ARRAY) {
- if (d["planes"].get_type()==Variant::POOL_REAL_ARRAY) {
-
- PoolVector<real_t> src_planes=d["planes"];
- int plane_count=src_planes.size();
- ERR_FAIL_COND(plane_count%4);
- planes.resize(plane_count/4);
+ PoolVector<real_t> src_planes = d["planes"];
+ int plane_count = src_planes.size();
+ ERR_FAIL_COND(plane_count % 4);
+ planes.resize(plane_count / 4);
if (plane_count) {
PoolVector<real_t>::Read r = src_planes.read();
- for(int i=0;i<plane_count/4;i++) {
+ for (int i = 0; i < plane_count / 4; i++) {
- planes[i].normal.x=r[i*4+0];
- planes[i].normal.y=r[i*4+1];
- planes[i].normal.z=r[i*4+2];
- planes[i].d=r[i*4+3];
+ planes[i].normal.x = r[i * 4 + 0];
+ planes[i].normal.y = r[i * 4 + 1];
+ planes[i].normal.z = r[i * 4 + 2];
+ planes[i].d = r[i * 4 + 3];
}
}
-
} else {
planes = d["planes"];
}
-
error_radius = d["error"];
aabb = d["aabb"];
//int node_count = src_nodes.size();
- nodes.resize(src_nodes.size()/3);
+ nodes.resize(src_nodes.size() / 3);
PoolVector<int>::Read r = src_nodes.read();
- for(int i=0;i<nodes.size();i++) {
+ for (int i = 0; i < nodes.size(); i++) {
- nodes[i].over=r[i*3+0];
- nodes[i].under=r[i*3+1];
- nodes[i].plane=r[i*3+2];
+ nodes[i].over = r[i * 3 + 0];
+ nodes[i].under = r[i * 3 + 1];
+ nodes[i].plane = r[i * 3 + 2];
}
-
}
-BSP_Tree::BSP_Tree(const PoolVector<Face3>& p_faces,real_t p_error_radius) {
+BSP_Tree::BSP_Tree(const PoolVector<Face3> &p_faces, real_t p_error_radius) {
// compute aabb
- int face_count=p_faces.size();
- PoolVector<Face3>::Read faces_r=p_faces.read();
+ int face_count = p_faces.size();
+ PoolVector<Face3>::Read faces_r = p_faces.read();
const Face3 *facesptr = faces_r.ptr();
-
- bool first=true;
+ bool first = true;
Vector<int> indices;
- for (int i=0;i<face_count;i++) {
+ for (int i = 0; i < face_count; i++) {
- const Face3& f=facesptr[i];
+ const Face3 &f = facesptr[i];
if (f.is_degenerate())
continue;
- for (int j=0;j<3;j++) {
+ for (int j = 0; j < 3; j++) {
if (first) {
- aabb.pos=f.vertex[0];
- first=false;
+ aabb.pos = f.vertex[0];
+ first = false;
} else {
aabb.expand_to(f.vertex[j]);
@@ -595,36 +560,29 @@ BSP_Tree::BSP_Tree(const PoolVector<Face3>& p_faces,real_t p_error_radius) {
}
indices.push_back(i);
-
}
- ERR_FAIL_COND( aabb.has_no_area() );
+ ERR_FAIL_COND(aabb.has_no_area());
- int top = _bsp_create_node(faces_r.ptr(),indices,planes,nodes,aabb.get_longest_axis_size()*0.0001);
+ int top = _bsp_create_node(faces_r.ptr(), indices, planes, nodes, aabb.get_longest_axis_size() * 0.0001);
- if (top<0) {
+ if (top < 0) {
nodes.clear();
planes.clear();
- ERR_FAIL_COND( top < 0 );
+ ERR_FAIL_COND(top < 0);
}
-
-
-
- error_radius=p_error_radius;
+ error_radius = p_error_radius;
}
-BSP_Tree::BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const Rect3& p_aabb,real_t p_error_radius) {
-
- nodes=p_nodes;
- planes=p_planes;
- aabb=p_aabb;
- error_radius=p_error_radius;
+BSP_Tree::BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const Rect3 &p_aabb, real_t p_error_radius) {
+ nodes = p_nodes;
+ planes = p_planes;
+ aabb = p_aabb;
+ error_radius = p_error_radius;
}
BSP_Tree::~BSP_Tree() {
-
-
}
diff --git a/core/math/bsp_tree.h b/core/math/bsp_tree.h
index c0071438db..4cfac35a2c 100644
--- a/core/math/bsp_tree.h
+++ b/core/math/bsp_tree.h
@@ -29,25 +29,24 @@
#ifndef BSP_TREE_H
#define BSP_TREE_H
+#include "dvector.h"
+#include "face3.h"
+#include "method_ptrcall.h"
#include "plane.h"
#include "rect3.h"
-#include "face3.h"
-#include "vector.h"
-#include "dvector.h"
#include "variant.h"
-#include "method_ptrcall.h"
+#include "vector.h"
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
class BSP_Tree {
public:
-
enum {
- UNDER_LEAF=0xFFFF,
- OVER_LEAF=0xFFFE,
- MAX_NODES=0xFFFE,
- MAX_PLANES=(1<<16)
+ UNDER_LEAF = 0xFFFF,
+ OVER_LEAF = 0xFFFE,
+ MAX_NODES = 0xFFFE,
+ MAX_PLANES = (1 << 16)
};
struct Node {
@@ -57,7 +56,6 @@ public:
uint16_t over;
};
-
private:
// thanks to the properties of Vector,
// this class can be assigned and passed around between threads
@@ -68,95 +66,90 @@ private:
Rect3 aabb;
real_t error_radius;
- int _get_points_inside(int p_node,const Vector3* p_points,int *p_indices, const Vector3& p_center,const Vector3& p_half_extents,int p_indices_count) const;
+ int _get_points_inside(int p_node, const Vector3 *p_points, int *p_indices, const Vector3 &p_center, const Vector3 &p_half_extents, int p_indices_count) const;
- template<class T>
- bool _test_convex(const Node* p_nodes, const Plane* p_planes,int p_current, const T& p_convex) const;
+ template <class T>
+ bool _test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const;
public:
-
- bool is_empty() const { return nodes.size()==0; }
+ bool is_empty() const { return nodes.size() == 0; }
Vector<Node> get_nodes() const;
Vector<Plane> get_planes() const;
Rect3 get_aabb() const;
- bool point_is_inside(const Vector3& p_point) const;
- int get_points_inside(const Vector3* p_points, int p_point_count) const;
- template<class T>
- bool convex_is_inside(const T& p_convex) const;
+ bool point_is_inside(const Vector3 &p_point) const;
+ int get_points_inside(const Vector3 *p_points, int p_point_count) const;
+ template <class T>
+ bool convex_is_inside(const T &p_convex) const;
operator Variant() const;
- void from_aabb(const Rect3& p_aabb);
+ void from_aabb(const Rect3 &p_aabb);
BSP_Tree();
- BSP_Tree(const Variant& p_variant);
- BSP_Tree(const PoolVector<Face3>& p_faces,real_t p_error_radius=0);
- BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const Rect3& p_aabb,real_t p_error_radius=0);
+ BSP_Tree(const Variant &p_variant);
+ BSP_Tree(const PoolVector<Face3> &p_faces, real_t p_error_radius = 0);
+ BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const Rect3 &p_aabb, real_t p_error_radius = 0);
~BSP_Tree();
-
};
-template<class T>
-bool BSP_Tree::_test_convex(const Node* p_nodes, const Plane* p_planes,int p_current, const T& p_convex) const {
+template <class T>
+bool BSP_Tree::_test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const {
- if (p_current==UNDER_LEAF)
+ if (p_current == UNDER_LEAF)
return true;
- else if (p_current==OVER_LEAF)
+ else if (p_current == OVER_LEAF)
return false;
- bool collided=false;
- const Node&n=p_nodes[p_current];
+ bool collided = false;
+ const Node &n = p_nodes[p_current];
- const Plane& p=p_planes[n.plane];
+ const Plane &p = p_planes[n.plane];
- real_t min,max;
- p_convex.project_range(p.normal,min,max);
+ real_t min, max;
+ p_convex.project_range(p.normal, min, max);
bool go_under = min < p.d;
bool go_over = max >= p.d;
- if (go_under && _test_convex(p_nodes,p_planes,n.under,p_convex))
- collided=true;
- if (go_over && _test_convex(p_nodes,p_planes,n.over,p_convex))
- collided=true;
+ if (go_under && _test_convex(p_nodes, p_planes, n.under, p_convex))
+ collided = true;
+ if (go_over && _test_convex(p_nodes, p_planes, n.over, p_convex))
+ collided = true;
return collided;
-
}
-template<class T>
-bool BSP_Tree::convex_is_inside(const T& p_convex) const {
+template <class T>
+bool BSP_Tree::convex_is_inside(const T &p_convex) const {
int node_count = nodes.size();
- if (node_count==0)
+ if (node_count == 0)
return false;
- const Node* nodes=&this->nodes[0];
- const Plane* planes = &this->planes[0];
+ const Node *nodes = &this->nodes[0];
+ const Plane *planes = &this->planes[0];
- return _test_convex(nodes,planes,node_count-1,p_convex);
+ return _test_convex(nodes, planes, node_count - 1, p_convex);
}
-
#ifdef PTRCALL_ENABLED
-
-template<>
+template <>
struct PtrToArg<BSP_Tree> {
- _FORCE_INLINE_ static BSP_Tree convert(const void* p_ptr) {
- BSP_Tree s( Variant( *reinterpret_cast<const Dictionary*>(p_ptr) ) );
+ _FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) {
+ BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr)));
return s;
}
- _FORCE_INLINE_ static void encode(BSP_Tree p_val,void* p_ptr) {
- Dictionary *d = reinterpret_cast<Dictionary*>(p_ptr);
- *d=Variant(p_val);
+ _FORCE_INLINE_ static void encode(BSP_Tree p_val, void *p_ptr) {
+ Dictionary *d = reinterpret_cast<Dictionary *>(p_ptr);
+ *d = Variant(p_val);
}
};
-template<>
-struct PtrToArg<const BSP_Tree&> {
- _FORCE_INLINE_ static BSP_Tree convert(const void* p_ptr) {
- BSP_Tree s( Variant( *reinterpret_cast<const Dictionary*>(p_ptr) ) );
+template <>
+struct PtrToArg<const BSP_Tree &> {
+ _FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) {
+ BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr)));
return s;
}
};
diff --git a/core/math/camera_matrix.cpp b/core/math/camera_matrix.cpp
index 3b47a75c65..227f586c43 100644
--- a/core/math/camera_matrix.cpp
+++ b/core/math/camera_matrix.cpp
@@ -32,29 +32,27 @@
void CameraMatrix::set_identity() {
- for (int i=0;i<4;i++) {
+ for (int i = 0; i < 4; i++) {
- for (int j=0;j<4;j++) {
+ for (int j = 0; j < 4; j++) {
- matrix[i][j]=(i==j)?1:0;
+ matrix[i][j] = (i == j) ? 1 : 0;
}
}
}
-
void CameraMatrix::set_zero() {
- for (int i=0;i<4;i++) {
+ for (int i = 0; i < 4; i++) {
- for (int j=0;j<4;j++) {
+ for (int j = 0; j < 4; j++) {
- matrix[i][j]=0;
+ matrix[i][j] = 0;
}
}
}
-
-Plane CameraMatrix::xform4(const Plane& p_vec4) const {
+Plane CameraMatrix::xform4(const Plane &p_vec4) const {
Plane ret;
@@ -65,11 +63,10 @@ Plane CameraMatrix::xform4(const Plane& p_vec4) const {
return ret;
}
-void CameraMatrix::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far,bool p_flip_fov) {
+void CameraMatrix::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov) {
if (p_flip_fov) {
- p_fovy_degrees=get_fovy(p_fovy_degrees,1.0/p_aspect);
-
+ p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect);
}
real_t sine, cotangent, deltaZ;
@@ -78,8 +75,8 @@ void CameraMatrix::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_
deltaZ = p_z_far - p_z_near;
sine = Math::sin(radians);
- if ((deltaZ == 0) || (sine == 0) || (p_aspect == 0)) {
- return ;
+ if ((deltaZ == 0) || (sine == 0) || (p_aspect == 0)) {
+ return;
}
cotangent = Math::cos(radians) / sine;
@@ -91,35 +88,30 @@ void CameraMatrix::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_
matrix[2][3] = -1;
matrix[3][2] = -2 * p_z_near * p_z_far / deltaZ;
matrix[3][3] = 0;
-
}
-void CameraMatrix::set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar) {
-
+void CameraMatrix::set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar) {
set_identity();
- matrix[0][0] = 2.0/(p_right-p_left);
- matrix[3][0] = -((p_right+p_left)/(p_right-p_left));
- matrix[1][1] = 2.0/(p_top-p_bottom);
- matrix[3][1] = -((p_top+p_bottom)/(p_top-p_bottom));
- matrix[2][2] = -2.0/(p_zfar-p_znear);
- matrix[3][2] = -((p_zfar+p_znear)/(p_zfar-p_znear));
+ matrix[0][0] = 2.0 / (p_right - p_left);
+ matrix[3][0] = -((p_right + p_left) / (p_right - p_left));
+ matrix[1][1] = 2.0 / (p_top - p_bottom);
+ matrix[3][1] = -((p_top + p_bottom) / (p_top - p_bottom));
+ matrix[2][2] = -2.0 / (p_zfar - p_znear);
+ matrix[3][2] = -((p_zfar + p_znear) / (p_zfar - p_znear));
matrix[3][3] = 1.0;
-
}
-void CameraMatrix::set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar,bool p_flip_fov) {
+void CameraMatrix::set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov) {
if (!p_flip_fov) {
- p_size*=p_aspect;
+ p_size *= p_aspect;
}
- set_orthogonal(-p_size/2,+p_size/2,-p_size/p_aspect/2,+p_size/p_aspect/2,p_znear,p_zfar);
+ set_orthogonal(-p_size / 2, +p_size / 2, -p_size / p_aspect / 2, +p_size / p_aspect / 2, p_znear, p_zfar);
}
-
-
void CameraMatrix::set_frustum(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_near, real_t p_far) {
#if 0
///@TODO, give a check to this. I'm not sure if it's working.
@@ -135,13 +127,13 @@ void CameraMatrix::set_frustum(real_t p_left, real_t p_right, real_t p_bottom, r
matrix[3][3]=0;
#else
real_t *te = &matrix[0][0];
- real_t x = 2 * p_near / ( p_right - p_left );
- real_t y = 2 * p_near / ( p_top - p_bottom );
+ real_t x = 2 * p_near / (p_right - p_left);
+ real_t y = 2 * p_near / (p_top - p_bottom);
- real_t a = ( p_right + p_left ) / ( p_right - p_left );
- real_t b = ( p_top + p_bottom ) / ( p_top - p_bottom );
- real_t c = - ( p_far + p_near ) / ( p_far - p_near );
- real_t d = - 2 * p_far * p_near / ( p_far - p_near );
+ real_t a = (p_right + p_left) / (p_right - p_left);
+ real_t b = (p_top + p_bottom) / (p_top - p_bottom);
+ real_t c = -(p_far + p_near) / (p_far - p_near);
+ real_t d = -2 * p_far * p_near / (p_far - p_near);
te[0] = x;
te[1] = 0;
@@ -161,120 +153,117 @@ void CameraMatrix::set_frustum(real_t p_left, real_t p_right, real_t p_bottom, r
te[15] = 0;
#endif
-
}
-
-
real_t CameraMatrix::get_z_far() const {
- const real_t * matrix = (const real_t*)this->matrix;
- Plane new_plane=Plane(matrix[ 3] - matrix[ 2],
- matrix[ 7] - matrix[ 6],
- matrix[11] - matrix[10],
- matrix[15] - matrix[14]);
+ const real_t *matrix = (const real_t *)this->matrix;
+ Plane new_plane = Plane(matrix[3] - matrix[2],
+ matrix[7] - matrix[6],
+ matrix[11] - matrix[10],
+ matrix[15] - matrix[14]);
- new_plane.normal=-new_plane.normal;
+ new_plane.normal = -new_plane.normal;
new_plane.normalize();
return new_plane.d;
}
real_t CameraMatrix::get_z_near() const {
- const real_t * matrix = (const real_t*)this->matrix;
- Plane new_plane=Plane(matrix[ 3] + matrix[ 2],
- matrix[ 7] + matrix[ 6],
- matrix[11] + matrix[10],
- -matrix[15] - matrix[14]);
+ const real_t *matrix = (const real_t *)this->matrix;
+ Plane new_plane = Plane(matrix[3] + matrix[2],
+ matrix[7] + matrix[6],
+ matrix[11] + matrix[10],
+ -matrix[15] - matrix[14]);
new_plane.normalize();
return new_plane.d;
}
-void CameraMatrix::get_viewport_size(real_t& r_width, real_t& r_height) const {
+void CameraMatrix::get_viewport_size(real_t &r_width, real_t &r_height) const {
- const real_t * matrix = (const real_t*)this->matrix;
+ const real_t *matrix = (const real_t *)this->matrix;
///////--- Near Plane ---///////
- Plane near_plane=Plane(matrix[ 3] + matrix[ 2],
- matrix[ 7] + matrix[ 6],
- matrix[11] + matrix[10],
+ Plane near_plane = Plane(matrix[3] + matrix[2],
+ matrix[7] + matrix[6],
+ matrix[11] + matrix[10],
-matrix[15] - matrix[14]);
near_plane.normalize();
///////--- Right Plane ---///////
- Plane right_plane=Plane(matrix[ 3] - matrix[ 0],
- matrix[ 7] - matrix[ 4],
- matrix[11] - matrix[ 8],
- - matrix[15] + matrix[12]);
+ Plane right_plane = Plane(matrix[3] - matrix[0],
+ matrix[7] - matrix[4],
+ matrix[11] - matrix[8],
+ -matrix[15] + matrix[12]);
right_plane.normalize();
- Plane top_plane=Plane(matrix[ 3] - matrix[ 1],
- matrix[ 7] - matrix[ 5],
- matrix[11] - matrix[ 9],
+ Plane top_plane = Plane(matrix[3] - matrix[1],
+ matrix[7] - matrix[5],
+ matrix[11] - matrix[9],
-matrix[15] + matrix[13]);
top_plane.normalize();
Vector3 res;
- near_plane.intersect_3(right_plane,top_plane,&res);
+ near_plane.intersect_3(right_plane, top_plane, &res);
- r_width=res.x;
- r_height=res.y;
+ r_width = res.x;
+ r_height = res.y;
}
-bool CameraMatrix::get_endpoints(const Transform& p_transform, Vector3 *p_8points) const {
+bool CameraMatrix::get_endpoints(const Transform &p_transform, Vector3 *p_8points) const {
- const real_t * matrix = (const real_t*)this->matrix;
+ const real_t *matrix = (const real_t *)this->matrix;
///////--- Near Plane ---///////
- Plane near_plane=Plane(matrix[ 3] + matrix[ 2],
- matrix[ 7] + matrix[ 6],
- matrix[11] + matrix[10],
+ Plane near_plane = Plane(matrix[3] + matrix[2],
+ matrix[7] + matrix[6],
+ matrix[11] + matrix[10],
-matrix[15] - matrix[14]);
near_plane.normalize();
///////--- Far Plane ---///////
- Plane far_plane=Plane(matrix[ 2] - matrix[ 3],
- matrix[ 6] - matrix[ 7],
- matrix[10] - matrix[11],
- matrix[15] - matrix[14]);
+ Plane far_plane = Plane(matrix[2] - matrix[3],
+ matrix[6] - matrix[7],
+ matrix[10] - matrix[11],
+ matrix[15] - matrix[14]);
far_plane.normalize();
///////--- Right Plane ---///////
- Plane right_plane=Plane(matrix[ 0] - matrix[ 3],
- matrix[ 4] - matrix[ 7],
- matrix[8] - matrix[ 11],
- - matrix[15] + matrix[12]);
+ Plane right_plane = Plane(matrix[0] - matrix[3],
+ matrix[4] - matrix[7],
+ matrix[8] - matrix[11],
+ -matrix[15] + matrix[12]);
right_plane.normalize();
///////--- Top Plane ---///////
- Plane top_plane=Plane(matrix[ 1] - matrix[ 3],
- matrix[ 5] - matrix[ 7],
- matrix[9] - matrix[ 11],
+ Plane top_plane = Plane(matrix[1] - matrix[3],
+ matrix[5] - matrix[7],
+ matrix[9] - matrix[11],
-matrix[15] + matrix[13]);
top_plane.normalize();
Vector3 near_endpoint;
Vector3 far_endpoint;
- bool res=near_plane.intersect_3(right_plane,top_plane,&near_endpoint);
- ERR_FAIL_COND_V(!res,false);
+ bool res = near_plane.intersect_3(right_plane, top_plane, &near_endpoint);
+ ERR_FAIL_COND_V(!res, false);
- res=far_plane.intersect_3(right_plane,top_plane,&far_endpoint);
- ERR_FAIL_COND_V(!res,false);
+ res = far_plane.intersect_3(right_plane, top_plane, &far_endpoint);
+ ERR_FAIL_COND_V(!res, false);
- p_8points[0]=p_transform.xform( Vector3( near_endpoint.x, near_endpoint.y, near_endpoint.z ) );
- p_8points[1]=p_transform.xform( Vector3( near_endpoint.x,-near_endpoint.y, near_endpoint.z ) );
- p_8points[2]=p_transform.xform( Vector3(-near_endpoint.x, near_endpoint.y, near_endpoint.z ) );
- p_8points[3]=p_transform.xform( Vector3(-near_endpoint.x,-near_endpoint.y, near_endpoint.z ) );
- p_8points[4]=p_transform.xform( Vector3( far_endpoint.x, far_endpoint.y, far_endpoint.z ) );
- p_8points[5]=p_transform.xform( Vector3( far_endpoint.x,-far_endpoint.y, far_endpoint.z ) );
- p_8points[6]=p_transform.xform( Vector3(-far_endpoint.x, far_endpoint.y, far_endpoint.z ) );
- p_8points[7]=p_transform.xform( Vector3(-far_endpoint.x,-far_endpoint.y, far_endpoint.z ) );
+ p_8points[0] = p_transform.xform(Vector3(near_endpoint.x, near_endpoint.y, near_endpoint.z));
+ p_8points[1] = p_transform.xform(Vector3(near_endpoint.x, -near_endpoint.y, near_endpoint.z));
+ p_8points[2] = p_transform.xform(Vector3(-near_endpoint.x, near_endpoint.y, near_endpoint.z));
+ p_8points[3] = p_transform.xform(Vector3(-near_endpoint.x, -near_endpoint.y, near_endpoint.z));
+ p_8points[4] = p_transform.xform(Vector3(far_endpoint.x, far_endpoint.y, far_endpoint.z));
+ p_8points[5] = p_transform.xform(Vector3(far_endpoint.x, -far_endpoint.y, far_endpoint.z));
+ p_8points[6] = p_transform.xform(Vector3(-far_endpoint.x, far_endpoint.y, far_endpoint.z));
+ p_8points[7] = p_transform.xform(Vector3(-far_endpoint.x, -far_endpoint.y, far_endpoint.z));
return true;
}
-Vector<Plane> CameraMatrix::get_projection_planes(const Transform& p_transform) const {
+Vector<Plane> CameraMatrix::get_projection_planes(const Transform &p_transform) const {
/** Fast Plane Extraction from combined modelview/projection matrices.
* References:
@@ -284,88 +273,79 @@ Vector<Plane> CameraMatrix::get_projection_planes(const Transform& p_transform)
Vector<Plane> planes;
- const real_t * matrix = (const real_t*)this->matrix;
+ const real_t *matrix = (const real_t *)this->matrix;
Plane new_plane;
///////--- Near Plane ---///////
- new_plane=Plane(matrix[ 3] + matrix[ 2],
- matrix[ 7] + matrix[ 6],
- matrix[11] + matrix[10],
- matrix[15] + matrix[14]);
+ new_plane = Plane(matrix[3] + matrix[2],
+ matrix[7] + matrix[6],
+ matrix[11] + matrix[10],
+ matrix[15] + matrix[14]);
- new_plane.normal=-new_plane.normal;
+ new_plane.normal = -new_plane.normal;
new_plane.normalize();
- planes.push_back( p_transform.xform(new_plane) );
+ planes.push_back(p_transform.xform(new_plane));
///////--- Far Plane ---///////
- new_plane=Plane(matrix[ 3] - matrix[ 2],
- matrix[ 7] - matrix[ 6],
- matrix[11] - matrix[10],
- matrix[15] - matrix[14]);
+ new_plane = Plane(matrix[3] - matrix[2],
+ matrix[7] - matrix[6],
+ matrix[11] - matrix[10],
+ matrix[15] - matrix[14]);
- new_plane.normal=-new_plane.normal;
+ new_plane.normal = -new_plane.normal;
new_plane.normalize();
- planes.push_back( p_transform.xform(new_plane) );
-
+ planes.push_back(p_transform.xform(new_plane));
///////--- Left Plane ---///////
- new_plane=Plane(matrix[ 3] + matrix[ 0],
- matrix[ 7] + matrix[ 4],
- matrix[11] + matrix[ 8],
- matrix[15] + matrix[12]);
+ new_plane = Plane(matrix[3] + matrix[0],
+ matrix[7] + matrix[4],
+ matrix[11] + matrix[8],
+ matrix[15] + matrix[12]);
- new_plane.normal=-new_plane.normal;
+ new_plane.normal = -new_plane.normal;
new_plane.normalize();
- planes.push_back( p_transform.xform(new_plane) );
-
+ planes.push_back(p_transform.xform(new_plane));
///////--- Top Plane ---///////
- new_plane=Plane(matrix[ 3] - matrix[ 1],
- matrix[ 7] - matrix[ 5],
- matrix[11] - matrix[ 9],
- matrix[15] - matrix[13]);
+ new_plane = Plane(matrix[3] - matrix[1],
+ matrix[7] - matrix[5],
+ matrix[11] - matrix[9],
+ matrix[15] - matrix[13]);
-
- new_plane.normal=-new_plane.normal;
+ new_plane.normal = -new_plane.normal;
new_plane.normalize();
- planes.push_back( p_transform.xform(new_plane) );
-
+ planes.push_back(p_transform.xform(new_plane));
///////--- Right Plane ---///////
- new_plane=Plane(matrix[ 3] - matrix[ 0],
- matrix[ 7] - matrix[ 4],
- matrix[11] - matrix[ 8],
- matrix[15] - matrix[12]);
+ new_plane = Plane(matrix[3] - matrix[0],
+ matrix[7] - matrix[4],
+ matrix[11] - matrix[8],
+ matrix[15] - matrix[12]);
-
- new_plane.normal=-new_plane.normal;
+ new_plane.normal = -new_plane.normal;
new_plane.normalize();
- planes.push_back( p_transform.xform(new_plane) );
-
+ planes.push_back(p_transform.xform(new_plane));
///////--- Bottom Plane ---///////
- new_plane=Plane(matrix[ 3] + matrix[ 1],
- matrix[ 7] + matrix[ 5],
- matrix[11] + matrix[ 9],
- matrix[15] + matrix[13]);
+ new_plane = Plane(matrix[3] + matrix[1],
+ matrix[7] + matrix[5],
+ matrix[11] + matrix[9],
+ matrix[15] + matrix[13]);
-
- new_plane.normal=-new_plane.normal;
+ new_plane.normal = -new_plane.normal;
new_plane.normalize();
- planes.push_back( p_transform.xform(new_plane) );
+ planes.push_back(p_transform.xform(new_plane));
return planes;
}
-
-
CameraMatrix CameraMatrix::inverse() const {
CameraMatrix cm = *this;
@@ -375,98 +355,96 @@ CameraMatrix CameraMatrix::inverse() const {
void CameraMatrix::invert() {
- int i,j,k;
- int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
- real_t pvt_val; /* Value of current pivot element */
- real_t hold; /* Temporary storage */
- real_t determinat; /* Determinant */
+ int i, j, k;
+ int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
+ real_t pvt_val; /* Value of current pivot element */
+ real_t hold; /* Temporary storage */
+ real_t determinat; /* Determinant */
determinat = 1.0;
- for (k=0; k<4; k++) {
+ for (k = 0; k < 4; k++) {
/** Locate k'th pivot element **/
- pvt_val=matrix[k][k]; /** Initialize for search **/
- pvt_i[k]=k;
- pvt_j[k]=k;
- for (i=k; i<4; i++) {
- for (j=k; j<4; j++) {
+ pvt_val = matrix[k][k]; /** Initialize for search **/
+ pvt_i[k] = k;
+ pvt_j[k] = k;
+ for (i = k; i < 4; i++) {
+ for (j = k; j < 4; j++) {
if (Math::absd(matrix[i][j]) > Math::absd(pvt_val)) {
- pvt_i[k]=i;
- pvt_j[k]=j;
- pvt_val=matrix[i][j];
+ pvt_i[k] = i;
+ pvt_j[k] = j;
+ pvt_val = matrix[i][j];
}
}
}
/** Product of pivots, gives determinant when finished **/
- determinat*=pvt_val;
- if (Math::absd(determinat)<1e-7) {
+ determinat *= pvt_val;
+ if (Math::absd(determinat) < 1e-7) {
return; //(false); /** Matrix is singular (zero determinant). **/
}
/** "Interchange" rows (with sign change stuff) **/
- i=pvt_i[k];
- if (i!=k) { /** If rows are different **/
- for (j=0; j<4; j++) {
- hold=-matrix[k][j];
- matrix[k][j]=matrix[i][j];
- matrix[i][j]=hold;
+ i = pvt_i[k];
+ if (i != k) { /** If rows are different **/
+ for (j = 0; j < 4; j++) {
+ hold = -matrix[k][j];
+ matrix[k][j] = matrix[i][j];
+ matrix[i][j] = hold;
}
}
/** "Interchange" columns **/
- j=pvt_j[k];
- if (j!=k) { /** If columns are different **/
- for (i=0; i<4; i++) {
- hold=-matrix[i][k];
- matrix[i][k]=matrix[i][j];
- matrix[i][j]=hold;
+ j = pvt_j[k];
+ if (j != k) { /** If columns are different **/
+ for (i = 0; i < 4; i++) {
+ hold = -matrix[i][k];
+ matrix[i][k] = matrix[i][j];
+ matrix[i][j] = hold;
}
}
/** Divide column by minus pivot value **/
- for (i=0; i<4; i++) {
- if (i!=k) matrix[i][k]/=( -pvt_val) ;
+ for (i = 0; i < 4; i++) {
+ if (i != k) matrix[i][k] /= (-pvt_val);
}
/** Reduce the matrix **/
- for (i=0; i<4; i++) {
+ for (i = 0; i < 4; i++) {
hold = matrix[i][k];
- for (j=0; j<4; j++) {
- if (i!=k && j!=k) matrix[i][j]+=hold*matrix[k][j];
+ for (j = 0; j < 4; j++) {
+ if (i != k && j != k) matrix[i][j] += hold * matrix[k][j];
}
}
/** Divide row by pivot **/
- for (j=0; j<4; j++) {
- if (j!=k) matrix[k][j]/=pvt_val;
+ for (j = 0; j < 4; j++) {
+ if (j != k) matrix[k][j] /= pvt_val;
}
/** Replace pivot by reciprocal (at last we can touch it). **/
- matrix[k][k] = 1.0/pvt_val;
+ matrix[k][k] = 1.0 / pvt_val;
}
/* That was most of the work, one final pass of row/column interchange */
/* to finish */
- for (k=4-2; k>=0; k--) { /* Don't need to work with 1 by 1 corner*/
- i=pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
- if (i!=k) { /* If rows are different */
- for(j=0; j<4; j++) {
+ for (k = 4 - 2; k >= 0; k--) { /* Don't need to work with 1 by 1 corner*/
+ i = pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
+ if (i != k) { /* If rows are different */
+ for (j = 0; j < 4; j++) {
hold = matrix[k][j];
- matrix[k][j]=-matrix[i][j];
- matrix[i][j]=hold;
+ matrix[k][j] = -matrix[i][j];
+ matrix[i][j] = hold;
}
}
- j=pvt_i[k]; /* Columns to swap correspond to pivot ROW */
- if (j!=k) /* If columns are different */
- for (i=0; i<4; i++) {
- hold=matrix[i][k];
- matrix[i][k]=-matrix[i][j];
- matrix[i][j]=hold;
+ j = pvt_i[k]; /* Columns to swap correspond to pivot ROW */
+ if (j != k) /* If columns are different */
+ for (i = 0; i < 4; i++) {
+ hold = matrix[i][k];
+ matrix[i][k] = -matrix[i][j];
+ matrix[i][j] = hold;
}
}
-
-
}
CameraMatrix::CameraMatrix() {
@@ -474,15 +452,15 @@ CameraMatrix::CameraMatrix() {
set_identity();
}
-CameraMatrix CameraMatrix::operator*(const CameraMatrix& p_matrix) const {
+CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const {
CameraMatrix new_matrix;
- for( int j = 0; j < 4; j++ ) {
- for( int i = 0; i < 4; i++ ) {
+ for (int j = 0; j < 4; j++) {
+ for (int i = 0; i < 4; i++) {
real_t ab = 0;
- for( int k = 0; k < 4; k++ )
- ab += matrix[k][i] * p_matrix.matrix[j][k] ;
+ for (int k = 0; k < 4; k++)
+ ab += matrix[k][i] * p_matrix.matrix[j][k];
new_matrix.matrix[j][i] = ab;
}
}
@@ -492,173 +470,164 @@ CameraMatrix CameraMatrix::operator*(const CameraMatrix& p_matrix) const {
void CameraMatrix::set_light_bias() {
- real_t *m=&matrix[0][0];
-
- m[0]=0.5,
- m[1]=0.0,
- m[2]=0.0,
- m[3]=0.0,
- m[4]=0.0,
- m[5]=0.5,
- m[6]=0.0,
- m[7]=0.0,
- m[8]=0.0,
- m[9]=0.0,
- m[10]=0.5,
- m[11]=0.0,
- m[12]=0.5,
- m[13]=0.5,
- m[14]=0.5,
- m[15]=1.0;
-
+ real_t *m = &matrix[0][0];
+
+ m[0] = 0.5,
+ m[1] = 0.0,
+ m[2] = 0.0,
+ m[3] = 0.0,
+ m[4] = 0.0,
+ m[5] = 0.5,
+ m[6] = 0.0,
+ m[7] = 0.0,
+ m[8] = 0.0,
+ m[9] = 0.0,
+ m[10] = 0.5,
+ m[11] = 0.0,
+ m[12] = 0.5,
+ m[13] = 0.5,
+ m[14] = 0.5,
+ m[15] = 1.0;
}
-void CameraMatrix::set_light_atlas_rect(const Rect2& p_rect) {
-
- real_t *m=&matrix[0][0];
-
- m[0]=p_rect.size.width,
- m[1]=0.0,
- m[2]=0.0,
- m[3]=0.0,
- m[4]=0.0,
- m[5]=p_rect.size.height,
- m[6]=0.0,
- m[7]=0.0,
- m[8]=0.0,
- m[9]=0.0,
- m[10]=1.0,
- m[11]=0.0,
- m[12]=p_rect.pos.x,
- m[13]=p_rect.pos.y,
- m[14]=0.0,
- m[15]=1.0;
+void CameraMatrix::set_light_atlas_rect(const Rect2 &p_rect) {
+
+ real_t *m = &matrix[0][0];
+
+ m[0] = p_rect.size.width,
+ m[1] = 0.0,
+ m[2] = 0.0,
+ m[3] = 0.0,
+ m[4] = 0.0,
+ m[5] = p_rect.size.height,
+ m[6] = 0.0,
+ m[7] = 0.0,
+ m[8] = 0.0,
+ m[9] = 0.0,
+ m[10] = 1.0,
+ m[11] = 0.0,
+ m[12] = p_rect.pos.x,
+ m[13] = p_rect.pos.y,
+ m[14] = 0.0,
+ m[15] = 1.0;
}
CameraMatrix::operator String() const {
String str;
- for (int i=0;i<4;i++)
- for (int j=0;j<4;j++)
- str+=String((j>0)?", ":"\n")+rtos(matrix[i][j]);
+ for (int i = 0; i < 4; i++)
+ for (int j = 0; j < 4; j++)
+ str += String((j > 0) ? ", " : "\n") + rtos(matrix[i][j]);
return str;
}
real_t CameraMatrix::get_aspect() const {
- real_t w,h;
- get_viewport_size(w,h);
- return w/h;
+ real_t w, h;
+ get_viewport_size(w, h);
+ return w / h;
}
int CameraMatrix::get_pixels_per_meter(int p_for_pixel_width) const {
-
- Vector3 result = xform(Vector3(1,0,-1));
+ Vector3 result = xform(Vector3(1, 0, -1));
return int((result.x * 0.5 + 0.5) * p_for_pixel_width);
-
}
real_t CameraMatrix::get_fov() const {
- const real_t * matrix = (const real_t*)this->matrix;
+ const real_t *matrix = (const real_t *)this->matrix;
- Plane right_plane=Plane(matrix[ 3] - matrix[ 0],
- matrix[ 7] - matrix[ 4],
- matrix[11] - matrix[ 8],
- - matrix[15] + matrix[12]);
+ Plane right_plane = Plane(matrix[3] - matrix[0],
+ matrix[7] - matrix[4],
+ matrix[11] - matrix[8],
+ -matrix[15] + matrix[12]);
right_plane.normalize();
- return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x)))*2.0;
+ return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0;
}
-
void CameraMatrix::make_scale(const Vector3 &p_scale) {
set_identity();
- matrix[0][0]=p_scale.x;
- matrix[1][1]=p_scale.y;
- matrix[2][2]=p_scale.z;
-
+ matrix[0][0] = p_scale.x;
+ matrix[1][1] = p_scale.y;
+ matrix[2][2] = p_scale.z;
}
-void CameraMatrix::scale_translate_to_fit(const Rect3& p_aabb) {
+void CameraMatrix::scale_translate_to_fit(const Rect3 &p_aabb) {
Vector3 min = p_aabb.pos;
- Vector3 max = p_aabb.pos+p_aabb.size;
-
-
- matrix[0][0]=2/(max.x-min.x);
- matrix[1][0]=0;
- matrix[2][0]=0;
- matrix[3][0]=-(max.x+min.x)/(max.x-min.x);
-
- matrix[0][1]=0;
- matrix[1][1]=2/(max.y-min.y);
- matrix[2][1]=0;
- matrix[3][1]=-(max.y+min.y)/(max.y-min.y);
-
- matrix[0][2]=0;
- matrix[1][2]=0;
- matrix[2][2]=2/(max.z-min.z);
- matrix[3][2]=-(max.z+min.z)/(max.z-min.z);
-
- matrix[0][3]=0;
- matrix[1][3]=0;
- matrix[2][3]=0;
- matrix[3][3]=1;
+ Vector3 max = p_aabb.pos + p_aabb.size;
+
+ matrix[0][0] = 2 / (max.x - min.x);
+ matrix[1][0] = 0;
+ matrix[2][0] = 0;
+ matrix[3][0] = -(max.x + min.x) / (max.x - min.x);
+
+ matrix[0][1] = 0;
+ matrix[1][1] = 2 / (max.y - min.y);
+ matrix[2][1] = 0;
+ matrix[3][1] = -(max.y + min.y) / (max.y - min.y);
+
+ matrix[0][2] = 0;
+ matrix[1][2] = 0;
+ matrix[2][2] = 2 / (max.z - min.z);
+ matrix[3][2] = -(max.z + min.z) / (max.z - min.z);
+
+ matrix[0][3] = 0;
+ matrix[1][3] = 0;
+ matrix[2][3] = 0;
+ matrix[3][3] = 1;
}
CameraMatrix::operator Transform() const {
Transform tr;
- const real_t *m=&matrix[0][0];
+ const real_t *m = &matrix[0][0];
- tr.basis.elements[0][0]=m[0];
- tr.basis.elements[1][0]=m[1];
- tr.basis.elements[2][0]=m[2];
+ tr.basis.elements[0][0] = m[0];
+ tr.basis.elements[1][0] = m[1];
+ tr.basis.elements[2][0] = m[2];
- tr.basis.elements[0][1]=m[4];
- tr.basis.elements[1][1]=m[5];
- tr.basis.elements[2][1]=m[6];
+ tr.basis.elements[0][1] = m[4];
+ tr.basis.elements[1][1] = m[5];
+ tr.basis.elements[2][1] = m[6];
- tr.basis.elements[0][2]=m[8];
- tr.basis.elements[1][2]=m[9];
- tr.basis.elements[2][2]=m[10];
+ tr.basis.elements[0][2] = m[8];
+ tr.basis.elements[1][2] = m[9];
+ tr.basis.elements[2][2] = m[10];
- tr.origin.x=m[12];
- tr.origin.y=m[13];
- tr.origin.z=m[14];
+ tr.origin.x = m[12];
+ tr.origin.y = m[13];
+ tr.origin.z = m[14];
return tr;
}
-CameraMatrix::CameraMatrix(const Transform& p_transform) {
+CameraMatrix::CameraMatrix(const Transform &p_transform) {
const Transform &tr = p_transform;
- real_t *m=&matrix[0][0];
-
- m[0]=tr.basis.elements[0][0];
- m[1]=tr.basis.elements[1][0];
- m[2]=tr.basis.elements[2][0];
- m[3]=0.0;
- m[4]=tr.basis.elements[0][1];
- m[5]=tr.basis.elements[1][1];
- m[6]=tr.basis.elements[2][1];
- m[7]=0.0;
- m[8]=tr.basis.elements[0][2];
- m[9]=tr.basis.elements[1][2];
- m[10]=tr.basis.elements[2][2];
- m[11]=0.0;
- m[12]=tr.origin.x;
- m[13]=tr.origin.y;
- m[14]=tr.origin.z;
- m[15]=1.0;
+ real_t *m = &matrix[0][0];
+
+ m[0] = tr.basis.elements[0][0];
+ m[1] = tr.basis.elements[1][0];
+ m[2] = tr.basis.elements[2][0];
+ m[3] = 0.0;
+ m[4] = tr.basis.elements[0][1];
+ m[5] = tr.basis.elements[1][1];
+ m[6] = tr.basis.elements[2][1];
+ m[7] = 0.0;
+ m[8] = tr.basis.elements[0][2];
+ m[9] = tr.basis.elements[1][2];
+ m[10] = tr.basis.elements[2][2];
+ m[11] = 0.0;
+ m[12] = tr.origin.x;
+ m[13] = tr.origin.y;
+ m[14] = tr.origin.z;
+ m[15] = 1.0;
}
-CameraMatrix::~CameraMatrix()
-{
+CameraMatrix::~CameraMatrix() {
}
-
-
diff --git a/core/math/camera_matrix.h b/core/math/camera_matrix.h
index c96f8259b5..857628c703 100644
--- a/core/math/camera_matrix.h
+++ b/core/math/camera_matrix.h
@@ -29,14 +29,12 @@
#ifndef CAMERA_MATRIX_H
#define CAMERA_MATRIX_H
-#include "transform.h"
#include "math_2d.h"
+#include "transform.h"
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
-
-
struct CameraMatrix {
enum Planes {
@@ -48,21 +46,20 @@ struct CameraMatrix {
PLANE_BOTTOM
};
- real_t matrix[4][4];
-
+ real_t matrix[4][4];
void set_identity();
void set_zero();
void set_light_bias();
- void set_light_atlas_rect(const Rect2& p_rect);
- void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far,bool p_flip_fov=false);
- void set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar);
- void set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar,bool p_flip_fov=false);
+ void set_light_atlas_rect(const Rect2 &p_rect);
+ void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov = false);
+ void set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar);
+ void set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov = false);
void set_frustum(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_near, real_t p_far);
- static real_t get_fovy(real_t p_fovx,real_t p_aspect) {
+ static real_t get_fovy(real_t p_fovx, real_t p_aspect) {
- return Math::rad2deg(Math::atan(p_aspect * Math::tan(Math::deg2rad(p_fovx) * 0.5))*2.0);
+ return Math::rad2deg(Math::atan(p_aspect * Math::tan(Math::deg2rad(p_fovx) * 0.5)) * 2.0);
}
real_t get_z_far() const;
@@ -70,40 +67,39 @@ struct CameraMatrix {
real_t get_aspect() const;
real_t get_fov() const;
- Vector<Plane> get_projection_planes(const Transform& p_transform) const;
+ Vector<Plane> get_projection_planes(const Transform &p_transform) const;
- bool get_endpoints(const Transform& p_transform,Vector3 *p_8points) const;
- void get_viewport_size(real_t& r_width, real_t& r_height) const;
+ bool get_endpoints(const Transform &p_transform, Vector3 *p_8points) const;
+ void get_viewport_size(real_t &r_width, real_t &r_height) const;
void invert();
CameraMatrix inverse() const;
- CameraMatrix operator*(const CameraMatrix& p_matrix) const;
+ CameraMatrix operator*(const CameraMatrix &p_matrix) const;
- Plane xform4(const Plane& p_vec4) const;
- _FORCE_INLINE_ Vector3 xform(const Vector3& p_vec3) const;
+ Plane xform4(const Plane &p_vec4) const;
+ _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vec3) const;
operator String() const;
- void scale_translate_to_fit(const Rect3& p_aabb);
+ void scale_translate_to_fit(const Rect3 &p_aabb);
void make_scale(const Vector3 &p_scale);
int get_pixels_per_meter(int p_for_pixel_width) const;
operator Transform() const;
CameraMatrix();
- CameraMatrix(const Transform& p_transform);
+ CameraMatrix(const Transform &p_transform);
~CameraMatrix();
-
};
-Vector3 CameraMatrix::xform(const Vector3& p_vec3) const {
+Vector3 CameraMatrix::xform(const Vector3 &p_vec3) const {
Vector3 ret;
ret.x = matrix[0][0] * p_vec3.x + matrix[1][0] * p_vec3.y + matrix[2][0] * p_vec3.z + matrix[3][0];
ret.y = matrix[0][1] * p_vec3.x + matrix[1][1] * p_vec3.y + matrix[2][1] * p_vec3.z + matrix[3][1];
ret.z = matrix[0][2] * p_vec3.x + matrix[1][2] * p_vec3.y + matrix[2][2] * p_vec3.z + matrix[3][2];
real_t w = matrix[0][3] * p_vec3.x + matrix[1][3] * p_vec3.y + matrix[2][3] * p_vec3.z + matrix[3][3];
- return ret/w;
+ return ret / w;
}
#endif
diff --git a/core/math/face3.cpp b/core/math/face3.cpp
index 60fab6748a..d9d99b0384 100644
--- a/core/math/face3.cpp
+++ b/core/math/face3.cpp
@@ -29,121 +29,112 @@
#include "face3.h"
#include "geometry.h"
-int Face3::split_by_plane(const Plane& p_plane,Face3 p_res[3],bool p_is_point_over[3]) const {
+int Face3::split_by_plane(const Plane &p_plane, Face3 p_res[3], bool p_is_point_over[3]) const {
- ERR_FAIL_COND_V(is_degenerate(),0);
+ ERR_FAIL_COND_V(is_degenerate(), 0);
-
- Vector3 above[4];
- int above_count=0;
+ Vector3 above[4];
+ int above_count = 0;
Vector3 below[4];
- int below_count=0;
+ int below_count = 0;
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
- if (p_plane.has_point( vertex[i], CMP_EPSILON )) { // point is in plane
+ if (p_plane.has_point(vertex[i], CMP_EPSILON)) { // point is in plane
- ERR_FAIL_COND_V(above_count>=4,0);
- above[above_count++]=vertex[i];
- ERR_FAIL_COND_V(below_count>=4,0);
- below[below_count++]=vertex[i];
+ ERR_FAIL_COND_V(above_count >= 4, 0);
+ above[above_count++] = vertex[i];
+ ERR_FAIL_COND_V(below_count >= 4, 0);
+ below[below_count++] = vertex[i];
} else {
- if (p_plane.is_point_over( vertex[i])) {
+ if (p_plane.is_point_over(vertex[i])) {
//Point is over
- ERR_FAIL_COND_V(above_count>=4,0);
- above[above_count++]=vertex[i];
+ ERR_FAIL_COND_V(above_count >= 4, 0);
+ above[above_count++] = vertex[i];
} else {
//Point is under
- ERR_FAIL_COND_V(below_count>=4,0);
- below[below_count++]=vertex[i];
+ ERR_FAIL_COND_V(below_count >= 4, 0);
+ below[below_count++] = vertex[i];
}
/* Check for Intersection between this and the next vertex*/
Vector3 inters;
- if (!p_plane.intersects_segment( vertex[i],vertex[(i+1)%3],&inters))
+ if (!p_plane.intersects_segment(vertex[i], vertex[(i + 1) % 3], &inters))
continue;
/* Intersection goes to both */
- ERR_FAIL_COND_V(above_count>=4,0);
- above[above_count++]=inters;
- ERR_FAIL_COND_V(below_count>=4,0);
- below[below_count++]=inters;
+ ERR_FAIL_COND_V(above_count >= 4, 0);
+ above[above_count++] = inters;
+ ERR_FAIL_COND_V(below_count >= 4, 0);
+ below[below_count++] = inters;
}
}
- int polygons_created=0;
+ int polygons_created = 0;
- ERR_FAIL_COND_V( above_count>=4 && below_count>=4 , 0 ); //bug in the algo
+ ERR_FAIL_COND_V(above_count >= 4 && below_count >= 4, 0); //bug in the algo
- if (above_count>=3) {
+ if (above_count >= 3) {
- p_res[polygons_created]=Face3( above[0], above[1], above[2] );
- p_is_point_over[polygons_created]=true;
+ p_res[polygons_created] = Face3(above[0], above[1], above[2]);
+ p_is_point_over[polygons_created] = true;
polygons_created++;
- if (above_count==4) {
+ if (above_count == 4) {
- p_res[polygons_created]=Face3( above[2], above[3], above[0] );
- p_is_point_over[polygons_created]=true;
+ p_res[polygons_created] = Face3(above[2], above[3], above[0]);
+ p_is_point_over[polygons_created] = true;
polygons_created++;
-
}
}
- if (below_count>=3) {
+ if (below_count >= 3) {
- p_res[polygons_created]=Face3( below[0], below[1], below[2] );
- p_is_point_over[polygons_created]=false;
+ p_res[polygons_created] = Face3(below[0], below[1], below[2]);
+ p_is_point_over[polygons_created] = false;
polygons_created++;
- if (below_count==4) {
+ if (below_count == 4) {
- p_res[polygons_created]=Face3( below[2], below[3], below[0] );
- p_is_point_over[polygons_created]=false;
+ p_res[polygons_created] = Face3(below[2], below[3], below[0]);
+ p_is_point_over[polygons_created] = false;
polygons_created++;
-
}
}
return polygons_created;
}
+bool Face3::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const {
-
-bool Face3::intersects_ray(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection) const {
-
- return Geometry::ray_intersects_triangle(p_from,p_dir,vertex[0],vertex[1],vertex[2],p_intersection);
-
+ return Geometry::ray_intersects_triangle(p_from, p_dir, vertex[0], vertex[1], vertex[2], p_intersection);
}
-bool Face3::intersects_segment(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection) const {
-
- return Geometry::segment_intersects_triangle(p_from,p_dir,vertex[0],vertex[1],vertex[2],p_intersection);
+bool Face3::intersects_segment(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const {
+ return Geometry::segment_intersects_triangle(p_from, p_dir, vertex[0], vertex[1], vertex[2], p_intersection);
}
-
bool Face3::is_degenerate() const {
- Vector3 normal=vec3_cross(vertex[0]-vertex[1], vertex[0]-vertex[2]);
+ Vector3 normal = vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]);
return (normal.length_squared() < CMP_EPSILON2);
}
+Face3::Side Face3::get_side_of(const Face3 &p_face, ClockDirection p_clock_dir) const {
-Face3::Side Face3::get_side_of(const Face3& p_face,ClockDirection p_clock_dir) const {
+ int over = 0, under = 0;
- int over=0,under=0;
+ Plane plane = get_plane(p_clock_dir);
- Plane plane=get_plane(p_clock_dir);
+ for (int i = 0; i < 3; i++) {
- for (int i=0;i<3;i++) {
-
- const Vector3 &v=p_face.vertex[i];
+ const Vector3 &v = p_face.vertex[i];
if (plane.has_point(v)) //coplanar, dont bother
continue;
@@ -152,81 +143,73 @@ Face3::Side Face3::get_side_of(const Face3& p_face,ClockDirection p_clock_dir) c
over++;
else
under++;
-
}
- if ( over > 0 && under == 0 )
+ if (over > 0 && under == 0)
return SIDE_OVER;
- else if (under > 0 && over ==0 )
+ else if (under > 0 && over == 0)
return SIDE_UNDER;
- else if (under ==0 && over == 0)
+ else if (under == 0 && over == 0)
return SIDE_COPLANAR;
else
return SIDE_SPANNING;
-
}
Vector3 Face3::get_random_point_inside() const {
- real_t a=Math::random(0,1);
- real_t b=Math::random(0,1);
- if (a>b) {
- SWAP(a,b);
+ real_t a = Math::random(0, 1);
+ real_t b = Math::random(0, 1);
+ if (a > b) {
+ SWAP(a, b);
}
- return vertex[0]*a + vertex[1]*(b-a) + vertex[2]*(1.0-b);
-
+ return vertex[0] * a + vertex[1] * (b - a) + vertex[2] * (1.0 - b);
}
Plane Face3::get_plane(ClockDirection p_dir) const {
- return Plane( vertex[0], vertex[1], vertex[2] , p_dir );
-
+ return Plane(vertex[0], vertex[1], vertex[2], p_dir);
}
Vector3 Face3::get_median_point() const {
- return (vertex[0] + vertex[1] + vertex[2])/3.0;
+ return (vertex[0] + vertex[1] + vertex[2]) / 3.0;
}
-
real_t Face3::get_area() const {
- return vec3_cross(vertex[0]-vertex[1], vertex[0]-vertex[2]).length();
+ return vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]).length();
}
ClockDirection Face3::get_clock_dir() const {
-
- Vector3 normal=vec3_cross(vertex[0]-vertex[1], vertex[0]-vertex[2]);
+ Vector3 normal = vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]);
//printf("normal is %g,%g,%g x %g,%g,%g- wtfu is %g\n",tofloat(normal.x),tofloat(normal.y),tofloat(normal.z),tofloat(vertex[0].x),tofloat(vertex[0].y),tofloat(vertex[0].z),tofloat( normal.dot( vertex[0] ) ) );
- return ( normal.dot( vertex[0] ) >= 0 ) ? CLOCKWISE : COUNTERCLOCKWISE;
-
+ return (normal.dot(vertex[0]) >= 0) ? CLOCKWISE : COUNTERCLOCKWISE;
}
-
-bool Face3::intersects_aabb(const Rect3& p_aabb) const {
+bool Face3::intersects_aabb(const Rect3 &p_aabb) const {
/** TEST PLANE **/
- if (!p_aabb.intersects_plane( get_plane() ))
+ if (!p_aabb.intersects_plane(get_plane()))
return false;
- /** TEST FACE AXIS */
-
-#define TEST_AXIS(m_ax)\
- {\
- real_t aabb_min=p_aabb.pos.m_ax;\
- real_t aabb_max=p_aabb.pos.m_ax+p_aabb.size.m_ax;\
- real_t tri_min,tri_max;\
- for (int i=0;i<3;i++) {\
- if (i==0 || vertex[i].m_ax > tri_max)\
- tri_max=vertex[i].m_ax;\
- if (i==0 || vertex[i].m_ax < tri_min)\
- tri_min=vertex[i].m_ax;\
- }\
-\
- if (tri_max<aabb_min || aabb_max<tri_min)\
- return false;\
+/** TEST FACE AXIS */
+
+#define TEST_AXIS(m_ax) \
+ { \
+ real_t aabb_min = p_aabb.pos.m_ax; \
+ real_t aabb_max = p_aabb.pos.m_ax + p_aabb.size.m_ax; \
+ real_t tri_min, tri_max; \
+ for (int i = 0; i < 3; i++) { \
+ if (i == 0 || vertex[i].m_ax > tri_max) \
+ tri_max = vertex[i].m_ax; \
+ if (i == 0 || vertex[i].m_ax < tri_min) \
+ tri_min = vertex[i].m_ax; \
+ } \
+ \
+ if (tri_max < aabb_min || aabb_max < tri_min) \
+ return false; \
}
TEST_AXIS(x);
@@ -235,221 +218,188 @@ bool Face3::intersects_aabb(const Rect3& p_aabb) const {
/** TEST ALL EDGES **/
- Vector3 edge_norms[3]={
- vertex[0]-vertex[1],
- vertex[1]-vertex[2],
- vertex[2]-vertex[0],
+ Vector3 edge_norms[3] = {
+ vertex[0] - vertex[1],
+ vertex[1] - vertex[2],
+ vertex[2] - vertex[0],
};
- for (int i=0;i<12;i++) {
+ for (int i = 0; i < 12; i++) {
- Vector3 from,to;
- p_aabb.get_edge(i,from,to);
- Vector3 e1=from-to;
- for (int j=0;j<3;j++) {
- Vector3 e2=edge_norms[j];
+ Vector3 from, to;
+ p_aabb.get_edge(i, from, to);
+ Vector3 e1 = from - to;
+ for (int j = 0; j < 3; j++) {
+ Vector3 e2 = edge_norms[j];
- Vector3 axis=vec3_cross( e1, e2 );
+ Vector3 axis = vec3_cross(e1, e2);
- if (axis.length_squared()<0.0001)
+ if (axis.length_squared() < 0.0001)
continue; // coplanar
axis.normalize();
- real_t minA,maxA,minB,maxB;
- p_aabb.project_range_in_plane(Plane(axis,0),minA,maxA);
- project_range(axis,Transform(),minB,maxB);
+ real_t minA, maxA, minB, maxB;
+ p_aabb.project_range_in_plane(Plane(axis, 0), minA, maxA);
+ project_range(axis, Transform(), minB, maxB);
- if (maxA<minB || maxB<minA)
+ if (maxA < minB || maxB < minA)
return false;
}
}
return true;
-
}
Face3::operator String() const {
- return String()+vertex[0]+", "+vertex[1]+", "+vertex[2];
+ return String() + vertex[0] + ", " + vertex[1] + ", " + vertex[2];
}
-void Face3::project_range(const Vector3& p_normal,const Transform& p_transform,real_t& r_min, real_t& r_max) const {
+void Face3::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
- Vector3 v=p_transform.xform(vertex[i]);
- real_t d=p_normal.dot(v);
+ Vector3 v = p_transform.xform(vertex[i]);
+ real_t d = p_normal.dot(v);
- if (i==0 || d > r_max)
- r_max=d;
+ if (i == 0 || d > r_max)
+ r_max = d;
- if (i==0 || d < r_min)
- r_min=d;
+ if (i == 0 || d < r_min)
+ r_min = d;
}
}
-
-
-void Face3::get_support(const Vector3& p_normal,const Transform& p_transform,Vector3 *p_vertices,int* p_count,int p_max) const {
+void Face3::get_support(const Vector3 &p_normal, const Transform &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const {
#define _FACE_IS_VALID_SUPPORT_TRESHOLD 0.98
#define _EDGE_IS_VALID_SUPPORT_TRESHOLD 0.05
- if (p_max<=0)
+ if (p_max <= 0)
return;
- Vector3 n=p_transform.basis.xform_inv(p_normal);
+ Vector3 n = p_transform.basis.xform_inv(p_normal);
/** TEST FACE AS SUPPORT **/
if (get_plane().normal.dot(n) > _FACE_IS_VALID_SUPPORT_TRESHOLD) {
- *p_count=MIN(3,p_max);
+ *p_count = MIN(3, p_max);
- for (int i=0;i<*p_count;i++) {
+ for (int i = 0; i < *p_count; i++) {
- p_vertices[i]=p_transform.xform(vertex[i]);
+ p_vertices[i] = p_transform.xform(vertex[i]);
}
return;
-
}
/** FIND SUPPORT VERTEX **/
- int vert_support_idx=-1;
+ int vert_support_idx = -1;
real_t support_max;
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
- real_t d=n.dot(vertex[i]);
+ real_t d = n.dot(vertex[i]);
- if (i==0 || d > support_max) {
- support_max=d;
- vert_support_idx=i;
+ if (i == 0 || d > support_max) {
+ support_max = d;
+ vert_support_idx = i;
}
}
/** TEST EDGES AS SUPPORT **/
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
- if (i!=vert_support_idx && i+1!=vert_support_idx)
+ if (i != vert_support_idx && i + 1 != vert_support_idx)
continue;
- // check if edge is valid as a support
- real_t dot=(vertex[i]-vertex[(i+1)%3]).normalized().dot(n);
- dot=ABS(dot);
+ // check if edge is valid as a support
+ real_t dot = (vertex[i] - vertex[(i + 1) % 3]).normalized().dot(n);
+ dot = ABS(dot);
if (dot < _EDGE_IS_VALID_SUPPORT_TRESHOLD) {
- *p_count=MIN(2,p_max);
+ *p_count = MIN(2, p_max);
- for (int j=0;j<*p_count;j++)
- p_vertices[j]=p_transform.xform(vertex[(j+i)%3]);
+ for (int j = 0; j < *p_count; j++)
+ p_vertices[j] = p_transform.xform(vertex[(j + i) % 3]);
return;
}
}
-
- *p_count=1;
- p_vertices[0]=p_transform.xform(vertex[vert_support_idx]);
-
+ *p_count = 1;
+ p_vertices[0] = p_transform.xform(vertex[vert_support_idx]);
}
-
-Vector3 Face3::get_closest_point_to(const Vector3& p_point) const {
-
- Vector3 edge0 = vertex[1] - vertex[0];
- Vector3 edge1 = vertex[2] - vertex[0];
- Vector3 v0 = vertex[0] - p_point;
-
- real_t a = edge0.dot( edge0 );
- real_t b = edge0.dot( edge1 );
- real_t c = edge1.dot( edge1 );
- real_t d = edge0.dot( v0 );
- real_t e = edge1.dot( v0 );
-
- real_t det = a*c - b*b;
- real_t s = b*e - c*d;
- real_t t = b*d - a*e;
-
- if ( s + t < det )
- {
- if ( s < 0.f )
- {
- if ( t < 0.f )
- {
- if ( d < 0.f )
- {
- s = CLAMP( -d/a, 0.f, 1.f );
- t = 0.f;
- }
- else
- {
- s = 0.f;
- t = CLAMP( -e/c, 0.f, 1.f );
+Vector3 Face3::get_closest_point_to(const Vector3 &p_point) const {
+
+ Vector3 edge0 = vertex[1] - vertex[0];
+ Vector3 edge1 = vertex[2] - vertex[0];
+ Vector3 v0 = vertex[0] - p_point;
+
+ real_t a = edge0.dot(edge0);
+ real_t b = edge0.dot(edge1);
+ real_t c = edge1.dot(edge1);
+ real_t d = edge0.dot(v0);
+ real_t e = edge1.dot(v0);
+
+ real_t det = a * c - b * b;
+ real_t s = b * e - c * d;
+ real_t t = b * d - a * e;
+
+ if (s + t < det) {
+ if (s < 0.f) {
+ if (t < 0.f) {
+ if (d < 0.f) {
+ s = CLAMP(-d / a, 0.f, 1.f);
+ t = 0.f;
+ } else {
+ s = 0.f;
+ t = CLAMP(-e / c, 0.f, 1.f);
+ }
+ } else {
+ s = 0.f;
+ t = CLAMP(-e / c, 0.f, 1.f);
}
- }
- else
- {
- s = 0.f;
- t = CLAMP( -e/c, 0.f, 1.f );
- }
- }
- else if ( t < 0.f )
- {
- s = CLAMP( -d/a, 0.f, 1.f );
- t = 0.f;
- }
- else
- {
- real_t invDet = 1.f / det;
- s *= invDet;
- t *= invDet;
- }
- }
- else
- {
- if ( s < 0.f )
- {
- real_t tmp0 = b+d;
- real_t tmp1 = c+e;
- if ( tmp1 > tmp0 )
- {
- real_t numer = tmp1 - tmp0;
- real_t denom = a-2*b+c;
- s = CLAMP( numer/denom, 0.f, 1.f );
- t = 1-s;
- }
- else
- {
- t = CLAMP( -e/c, 0.f, 1.f );
- s = 0.f;
- }
- }
- else if ( t < 0.f )
- {
- if ( a+d > b+e )
- {
- real_t numer = c+e-b-d;
- real_t denom = a-2*b+c;
- s = CLAMP( numer/denom, 0.f, 1.f );
- t = 1-s;
- }
- else
- {
- s = CLAMP( -e/c, 0.f, 1.f );
+ } else if (t < 0.f) {
+ s = CLAMP(-d / a, 0.f, 1.f);
t = 0.f;
- }
+ } else {
+ real_t invDet = 1.f / det;
+ s *= invDet;
+ t *= invDet;
}
- else
- {
- real_t numer = c+e-b-d;
- real_t denom = a-2*b+c;
- s = CLAMP( numer/denom, 0.f, 1.f );
- t = 1.f - s;
+ } else {
+ if (s < 0.f) {
+ real_t tmp0 = b + d;
+ real_t tmp1 = c + e;
+ if (tmp1 > tmp0) {
+ real_t numer = tmp1 - tmp0;
+ real_t denom = a - 2 * b + c;
+ s = CLAMP(numer / denom, 0.f, 1.f);
+ t = 1 - s;
+ } else {
+ t = CLAMP(-e / c, 0.f, 1.f);
+ s = 0.f;
+ }
+ } else if (t < 0.f) {
+ if (a + d > b + e) {
+ real_t numer = c + e - b - d;
+ real_t denom = a - 2 * b + c;
+ s = CLAMP(numer / denom, 0.f, 1.f);
+ t = 1 - s;
+ } else {
+ s = CLAMP(-e / c, 0.f, 1.f);
+ t = 0.f;
+ }
+ } else {
+ real_t numer = c + e - b - d;
+ real_t denom = a - 2 * b + c;
+ s = CLAMP(numer / denom, 0.f, 1.f);
+ t = 1.f - s;
}
- }
-
- return vertex[0] + s * edge0 + t * edge1;
+ }
+ return vertex[0] + s * edge0 + t * edge1;
}
diff --git a/core/math/face3.h b/core/math/face3.h
index a0da588ea5..6d15c60e3b 100644
--- a/core/math/face3.h
+++ b/core/math/face3.h
@@ -29,25 +29,23 @@
#ifndef FACE3_H
#define FACE3_H
-#include "vector3.h"
#include "plane.h"
#include "rect3.h"
#include "transform.h"
+#include "vector3.h"
class Face3 {
public:
+ enum Side {
+ SIDE_OVER,
+ SIDE_UNDER,
+ SIDE_SPANNING,
+ SIDE_COPLANAR
+ };
- enum Side {
- SIDE_OVER,
- SIDE_UNDER,
- SIDE_SPANNING,
- SIDE_COPLANAR
- };
-
-
- Vector3 vertex[3];
+ Vector3 vertex[3];
- /**
+ /**
*
* @param p_plane plane used to split the face
* @param p_res array of at least 3 faces, amount used in functio return
@@ -56,81 +54,80 @@ public:
* @return amount of faces generated by the split, either 0 (means no split possible), 2 or 3
*/
- int split_by_plane(const Plane& p_plane,Face3 *p_res,bool *p_is_point_over) const;
+ int split_by_plane(const Plane &p_plane, Face3 *p_res, bool *p_is_point_over) const;
- Plane get_plane(ClockDirection p_dir=CLOCKWISE) const;
+ Plane get_plane(ClockDirection p_dir = CLOCKWISE) const;
Vector3 get_random_point_inside() const;
+ Side get_side_of(const Face3 &p_face, ClockDirection p_clock_dir = CLOCKWISE) const;
- Side get_side_of(const Face3& p_face,ClockDirection p_clock_dir=CLOCKWISE) const;
-
- bool is_degenerate() const;
+ bool is_degenerate() const;
real_t get_area() const;
- Vector3 get_median_point() const;
- Vector3 get_closest_point_to(const Vector3& p_point) const;
+ Vector3 get_median_point() const;
+ Vector3 get_closest_point_to(const Vector3 &p_point) const;
- bool intersects_ray(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection=0) const;
- bool intersects_segment(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection=0) const;
+ bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = 0) const;
+ bool intersects_segment(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = 0) const;
- ClockDirection get_clock_dir() const; ///< todo, test if this is returning the proper clockwisity
+ ClockDirection get_clock_dir() const; ///< todo, test if this is returning the proper clockwisity
- void get_support(const Vector3& p_normal,const Transform& p_transform,Vector3 *p_vertices,int* p_count,int p_max) const;
- void project_range(const Vector3& p_normal,const Transform& p_transform,real_t& r_min, real_t& r_max) const;
+ void get_support(const Vector3 &p_normal, const Transform &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const;
+ void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
- Rect3 get_aabb() const {
+ Rect3 get_aabb() const {
- Rect3 aabb( vertex[0], Vector3() );
- aabb.expand_to( vertex[1] );
- aabb.expand_to( vertex[2] );
- return aabb;
- }
+ Rect3 aabb(vertex[0], Vector3());
+ aabb.expand_to(vertex[1]);
+ aabb.expand_to(vertex[2]);
+ return aabb;
+ }
- bool intersects_aabb(const Rect3& p_aabb) const;
- _FORCE_INLINE_ bool intersects_aabb2(const Rect3& p_aabb) const;
+ bool intersects_aabb(const Rect3 &p_aabb) const;
+ _FORCE_INLINE_ bool intersects_aabb2(const Rect3 &p_aabb) const;
operator String() const;
- inline Face3() {}
- inline Face3(const Vector3 &p_v1,const Vector3 &p_v2,const Vector3 &p_v3) { vertex[0]=p_v1; vertex[1]=p_v2; vertex[2]=p_v3; }
-
+ inline Face3() {}
+ inline Face3(const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) {
+ vertex[0] = p_v1;
+ vertex[1] = p_v2;
+ vertex[2] = p_v3;
+ }
};
+bool Face3::intersects_aabb2(const Rect3 &p_aabb) const {
-bool Face3::intersects_aabb2(const Rect3& p_aabb) const {
-
- Vector3 perp = (vertex[0]-vertex[2]).cross(vertex[0]-vertex[1]);
+ Vector3 perp = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]);
Vector3 half_extents = p_aabb.size * 0.5;
Vector3 ofs = p_aabb.pos + half_extents;
- Vector3 sup =Vector3(
- (perp.x>0) ? -half_extents.x : half_extents.x,
- (perp.y>0) ? -half_extents.y : half_extents.y,
- (perp.z>0) ? -half_extents.z : half_extents.z
- );
+ Vector3 sup = Vector3(
+ (perp.x > 0) ? -half_extents.x : half_extents.x,
+ (perp.y > 0) ? -half_extents.y : half_extents.y,
+ (perp.z > 0) ? -half_extents.z : half_extents.z);
real_t d = perp.dot(vertex[0]);
- real_t dist_a = perp.dot(ofs+sup)-d;
- real_t dist_b = perp.dot(ofs-sup)-d;
+ real_t dist_a = perp.dot(ofs + sup) - d;
+ real_t dist_b = perp.dot(ofs - sup) - d;
- if (dist_a*dist_b > 0)
+ if (dist_a * dist_b > 0)
return false; //does not intersect the plane
-
-#define TEST_AXIS(m_ax)\
- {\
- real_t aabb_min=p_aabb.pos.m_ax;\
- real_t aabb_max=p_aabb.pos.m_ax+p_aabb.size.m_ax;\
- real_t tri_min,tri_max;\
- for (int i=0;i<3;i++) {\
- if (i==0 || vertex[i].m_ax > tri_max)\
- tri_max=vertex[i].m_ax;\
- if (i==0 || vertex[i].m_ax < tri_min)\
- tri_min=vertex[i].m_ax;\
- }\
-\
- if (tri_max<aabb_min || aabb_max<tri_min)\
- return false;\
+#define TEST_AXIS(m_ax) \
+ { \
+ real_t aabb_min = p_aabb.pos.m_ax; \
+ real_t aabb_max = p_aabb.pos.m_ax + p_aabb.size.m_ax; \
+ real_t tri_min, tri_max; \
+ for (int i = 0; i < 3; i++) { \
+ if (i == 0 || vertex[i].m_ax > tri_max) \
+ tri_max = vertex[i].m_ax; \
+ if (i == 0 || vertex[i].m_ax < tri_min) \
+ tri_min = vertex[i].m_ax; \
+ } \
+ \
+ if (tri_max < aabb_min || aabb_max < tri_min) \
+ return false; \
}
TEST_AXIS(x);
@@ -139,131 +136,125 @@ bool Face3::intersects_aabb2(const Rect3& p_aabb) const {
#undef TEST_AXIS
-
- Vector3 edge_norms[3]={
- vertex[0]-vertex[1],
- vertex[1]-vertex[2],
- vertex[2]-vertex[0],
+ Vector3 edge_norms[3] = {
+ vertex[0] - vertex[1],
+ vertex[1] - vertex[2],
+ vertex[2] - vertex[0],
};
- for (int i=0;i<12;i++) {
+ for (int i = 0; i < 12; i++) {
- Vector3 from,to;
- switch(i) {
+ Vector3 from, to;
+ switch (i) {
- case 0:{
+ case 0: {
- from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z );
- to=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z );
+ from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z);
+ to = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z);
} break;
- case 1:{
+ case 1: {
- from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
- to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z );
+ from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z);
+ to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z);
} break;
- case 2:{
- from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
- to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
+ case 2: {
+ from = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z);
+ to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z);
} break;
- case 3:{
+ case 3: {
- from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z );
- to=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
+ from = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z);
+ to = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z);
} break;
- case 4:{
+ case 4: {
- from=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
- to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
+ from = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z);
+ to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z);
} break;
- case 5:{
+ case 5: {
- from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
- to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
+ from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z);
+ to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z);
} break;
- case 6:{
- from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
- to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
+ case 6: {
+ from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z);
+ to = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z);
} break;
- case 7:{
+ case 7: {
- from=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
- to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
+ from = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z);
+ to = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z);
} break;
- case 8:{
+ case 8: {
- from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
- to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
+ from = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z);
+ to = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z);
} break;
- case 9:{
+ case 9: {
- from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z );
- to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
+ from = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z);
+ to = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z);
} break;
- case 10:{
+ case 10: {
- from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z );
- to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
+ from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z);
+ to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z);
} break;
- case 11:{
+ case 11: {
- from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
- to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
+ from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z);
+ to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z);
} break;
-
}
- Vector3 e1=from-to;
- for (int j=0;j<3;j++) {
- Vector3 e2=edge_norms[j];
+ Vector3 e1 = from - to;
+ for (int j = 0; j < 3; j++) {
+ Vector3 e2 = edge_norms[j];
- Vector3 axis=vec3_cross( e1, e2 );
+ Vector3 axis = vec3_cross(e1, e2);
- if (axis.length_squared()<0.0001)
+ if (axis.length_squared() < 0.0001)
continue; // coplanar
//axis.normalize();
- Vector3 sup2 =Vector3(
- (axis.x>0) ? -half_extents.x : half_extents.x,
- (axis.y>0) ? -half_extents.y : half_extents.y,
- (axis.z>0) ? -half_extents.z : half_extents.z
- );
+ Vector3 sup2 = Vector3(
+ (axis.x > 0) ? -half_extents.x : half_extents.x,
+ (axis.y > 0) ? -half_extents.y : half_extents.y,
+ (axis.z > 0) ? -half_extents.z : half_extents.z);
- real_t maxB = axis.dot(ofs+sup2);
- real_t minB = axis.dot(ofs-sup2);
- if (minB>maxB) {
- SWAP(maxB,minB);
+ real_t maxB = axis.dot(ofs + sup2);
+ real_t minB = axis.dot(ofs - sup2);
+ if (minB > maxB) {
+ SWAP(maxB, minB);
}
- real_t minT=1e20,maxT=-1e20;
- for (int k=0;k<3;k++) {
+ real_t minT = 1e20, maxT = -1e20;
+ for (int k = 0; k < 3; k++) {
- real_t d=axis.dot(vertex[k]);
+ real_t d = axis.dot(vertex[k]);
if (d > maxT)
- maxT=d;
+ maxT = d;
if (d < minT)
- minT=d;
+ minT = d;
}
- if (maxB<minT || maxT<minB)
+ if (maxB < minT || maxT < minB)
return false;
}
}
return true;
-
-
}
-
//this sucks...
#endif // FACE3_H
diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp
index 6570dfe672..ec4d352a8f 100644
--- a/core/math/geometry.cpp
+++ b/core/math/geometry.cpp
@@ -29,58 +29,54 @@
#include "geometry.h"
#include "print_string.h"
-
-
void Geometry::MeshData::optimize_vertices() {
- Map<int,int> vtx_remap;
+ Map<int, int> vtx_remap;
- for(int i=0;i<faces.size();i++) {
+ for (int i = 0; i < faces.size(); i++) {
- for(int j=0;j<faces[i].indices.size();j++) {
+ for (int j = 0; j < faces[i].indices.size(); j++) {
int idx = faces[i].indices[j];
if (!vtx_remap.has(idx)) {
int ni = vtx_remap.size();
- vtx_remap[idx]=ni;
-
-
+ vtx_remap[idx] = ni;
}
- faces[i].indices[j]=vtx_remap[idx];
+ faces[i].indices[j] = vtx_remap[idx];
}
}
- for(int i=0;i<edges.size();i++) {
+ for (int i = 0; i < edges.size(); i++) {
int a = edges[i].a;
int b = edges[i].b;
if (!vtx_remap.has(a)) {
int ni = vtx_remap.size();
- vtx_remap[a]=ni;
+ vtx_remap[a] = ni;
}
if (!vtx_remap.has(b)) {
int ni = vtx_remap.size();
- vtx_remap[b]=ni;
+ vtx_remap[b] = ni;
}
- edges[i].a=vtx_remap[a];
- edges[i].b=vtx_remap[b];
+ edges[i].a = vtx_remap[a];
+ edges[i].b = vtx_remap[b];
}
Vector<Vector3> new_vertices;
new_vertices.resize(vtx_remap.size());
- for(int i=0;i<vertices.size();i++) {
+ for (int i = 0; i < vertices.size(); i++) {
if (vtx_remap.has(i))
- new_vertices[vtx_remap[i]]=vertices[i];
+ new_vertices[vtx_remap[i]] = vertices[i];
}
- vertices=new_vertices;
+ vertices = new_vertices;
}
-Vector< Vector<Vector2> > (*Geometry::_decompose_func)(const Vector<Vector2>& p_polygon)=NULL;
+Vector<Vector<Vector2> > (*Geometry::_decompose_func)(const Vector<Vector2> &p_polygon) = NULL;
struct _FaceClassify {
@@ -88,16 +84,22 @@ struct _FaceClassify {
int face;
int edge;
- void clear() { face=-1; edge=-1; }
- _Link() { face=-1; edge=-1; }
+ void clear() {
+ face = -1;
+ edge = -1;
+ }
+ _Link() {
+ face = -1;
+ edge = -1;
+ }
};
bool valid;
int group;
_Link links[3];
Face3 face;
_FaceClassify() {
- group=-1;
- valid=false;
+ group = -1;
+ valid = false;
};
};
@@ -105,76 +107,73 @@ static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) {
/* connect faces, error will occur if an edge is shared between more than 2 faces */
/* clear connections */
- bool error=false;
+ bool error = false;
- for (int i=0;i<len;i++) {
+ for (int i = 0; i < len; i++) {
- for (int j=0;j<3;j++) {
+ for (int j = 0; j < 3; j++) {
p_faces[i].links[j].clear();
}
}
- for (int i=0;i<len;i++) {
+ for (int i = 0; i < len; i++) {
- if (p_faces[i].group!=p_group)
+ if (p_faces[i].group != p_group)
continue;
- for (int j=i+1;j<len;j++) {
+ for (int j = i + 1; j < len; j++) {
- if (p_faces[j].group!=p_group)
+ if (p_faces[j].group != p_group)
continue;
- for (int k=0;k<3;k++) {
+ for (int k = 0; k < 3; k++) {
- Vector3 vi1=p_faces[i].face.vertex[k];
- Vector3 vi2=p_faces[i].face.vertex[(k+1)%3];
+ Vector3 vi1 = p_faces[i].face.vertex[k];
+ Vector3 vi2 = p_faces[i].face.vertex[(k + 1) % 3];
- for (int l=0;l<3;l++) {
+ for (int l = 0; l < 3; l++) {
- Vector3 vj2=p_faces[j].face.vertex[l];
- Vector3 vj1=p_faces[j].face.vertex[(l+1)%3];
+ Vector3 vj2 = p_faces[j].face.vertex[l];
+ Vector3 vj1 = p_faces[j].face.vertex[(l + 1) % 3];
- if (vi1.distance_to(vj1)<0.00001 &&
- vi2.distance_to(vj2)<0.00001
- ) {
- if (p_faces[i].links[k].face!=-1) {
+ if (vi1.distance_to(vj1) < 0.00001 &&
+ vi2.distance_to(vj2) < 0.00001) {
+ if (p_faces[i].links[k].face != -1) {
ERR_PRINT("already linked\n");
- error=true;
+ error = true;
break;
}
- if (p_faces[j].links[l].face!=-1) {
+ if (p_faces[j].links[l].face != -1) {
ERR_PRINT("already linked\n");
- error=true;
+ error = true;
break;
}
- p_faces[i].links[k].face=j;
- p_faces[i].links[k].edge=l;
- p_faces[j].links[l].face=i;
- p_faces[j].links[l].edge=k;
- }
+ p_faces[i].links[k].face = j;
+ p_faces[i].links[k].edge = l;
+ p_faces[j].links[l].face = i;
+ p_faces[j].links[l].edge = k;
+ }
}
if (error)
break;
-
}
if (error)
break;
-
}
if (error)
break;
}
- for (int i=0;i<len;i++) {
+ for (int i = 0; i < len; i++) {
- p_faces[i].valid=true;
- for (int j=0;j<3;j++) {
+ p_faces[i].valid = true;
+ for (int j = 0; j < 3; j++) {
- if (p_faces[i].links[j].face==-1)
- p_faces[i].valid=false;
+ if (p_faces[i].links[j].face == -1)
+ p_faces[i].valid = false;
}
/*printf("face %i is valid: %i, group %i. connected to %i:%i,%i:%i,%i:%i\n",i,p_faces[i].valid,p_faces[i].group,
p_faces[i].links[0].face,
@@ -187,152 +186,146 @@ static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) {
return error;
}
-static bool _group_face(_FaceClassify *p_faces, int len, int p_index,int p_group) {
+static bool _group_face(_FaceClassify *p_faces, int len, int p_index, int p_group) {
- if (p_faces[p_index].group>=0)
+ if (p_faces[p_index].group >= 0)
return false;
- p_faces[p_index].group=p_group;
+ p_faces[p_index].group = p_group;
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
- ERR_FAIL_INDEX_V(p_faces[p_index].links[i].face,len,true);
- _group_face(p_faces,len,p_faces[p_index].links[i].face,p_group);
+ ERR_FAIL_INDEX_V(p_faces[p_index].links[i].face, len, true);
+ _group_face(p_faces, len, p_faces[p_index].links[i].face, p_group);
}
return true;
}
+PoolVector<PoolVector<Face3> > Geometry::separate_objects(PoolVector<Face3> p_array) {
-PoolVector< PoolVector< Face3 > > Geometry::separate_objects( PoolVector< Face3 > p_array ) {
-
- PoolVector< PoolVector< Face3 > > objects;
+ PoolVector<PoolVector<Face3> > objects;
int len = p_array.size();
- PoolVector<Face3>::Read r=p_array.read();
+ PoolVector<Face3>::Read r = p_array.read();
- const Face3* arrayptr = r.ptr();
+ const Face3 *arrayptr = r.ptr();
- PoolVector< _FaceClassify> fc;
+ PoolVector<_FaceClassify> fc;
- fc.resize( len );
+ fc.resize(len);
- PoolVector< _FaceClassify >::Write fcw=fc.write();
+ PoolVector<_FaceClassify>::Write fcw = fc.write();
- _FaceClassify * _fcptr = fcw.ptr();
+ _FaceClassify *_fcptr = fcw.ptr();
- for (int i=0;i<len;i++) {
+ for (int i = 0; i < len; i++) {
- _fcptr[i].face=arrayptr[i];
+ _fcptr[i].face = arrayptr[i];
}
- bool error=_connect_faces(_fcptr,len,-1);
+ bool error = _connect_faces(_fcptr, len, -1);
if (error) {
- ERR_FAIL_COND_V(error, PoolVector< PoolVector< Face3 > >() ); // invalid geometry
+ ERR_FAIL_COND_V(error, PoolVector<PoolVector<Face3> >()); // invalid geometry
}
/* group connected faces in separate objects */
- int group=0;
- for (int i=0;i<len;i++) {
+ int group = 0;
+ for (int i = 0; i < len; i++) {
if (!_fcptr[i].valid)
continue;
- if (_group_face(_fcptr,len,i,group)) {
+ if (_group_face(_fcptr, len, i, group)) {
group++;
}
}
/* group connected faces in separate objects */
+ for (int i = 0; i < len; i++) {
- for (int i=0;i<len;i++) {
-
- _fcptr[i].face=arrayptr[i];
+ _fcptr[i].face = arrayptr[i];
}
- if (group>=0) {
+ if (group >= 0) {
objects.resize(group);
- PoolVector< PoolVector<Face3> >::Write obw=objects.write();
- PoolVector< Face3 > *group_faces = obw.ptr();
+ PoolVector<PoolVector<Face3> >::Write obw = objects.write();
+ PoolVector<Face3> *group_faces = obw.ptr();
- for (int i=0;i<len;i++) {
+ for (int i = 0; i < len; i++) {
if (!_fcptr[i].valid)
continue;
- if (_fcptr[i].group>=0 && _fcptr[i].group<group) {
+ if (_fcptr[i].group >= 0 && _fcptr[i].group < group) {
- group_faces[_fcptr[i].group].push_back( _fcptr[i].face );
+ group_faces[_fcptr[i].group].push_back(_fcptr[i].face);
}
}
}
-
return objects;
-
}
/*** GEOMETRY WRAPPER ***/
enum _CellFlags {
- _CELL_SOLID=1,
- _CELL_EXTERIOR=2,
- _CELL_STEP_MASK=0x1C,
- _CELL_STEP_NONE=0<<2,
- _CELL_STEP_Y_POS=1<<2,
- _CELL_STEP_Y_NEG=2<<2,
- _CELL_STEP_X_POS=3<<2,
- _CELL_STEP_X_NEG=4<<2,
- _CELL_STEP_Z_POS=5<<2,
- _CELL_STEP_Z_NEG=6<<2,
- _CELL_STEP_DONE=7<<2,
- _CELL_PREV_MASK=0xE0,
- _CELL_PREV_NONE=0<<5,
- _CELL_PREV_Y_POS=1<<5,
- _CELL_PREV_Y_NEG=2<<5,
- _CELL_PREV_X_POS=3<<5,
- _CELL_PREV_X_NEG=4<<5,
- _CELL_PREV_Z_POS=5<<5,
- _CELL_PREV_Z_NEG=6<<5,
- _CELL_PREV_FIRST=7<<5,
+ _CELL_SOLID = 1,
+ _CELL_EXTERIOR = 2,
+ _CELL_STEP_MASK = 0x1C,
+ _CELL_STEP_NONE = 0 << 2,
+ _CELL_STEP_Y_POS = 1 << 2,
+ _CELL_STEP_Y_NEG = 2 << 2,
+ _CELL_STEP_X_POS = 3 << 2,
+ _CELL_STEP_X_NEG = 4 << 2,
+ _CELL_STEP_Z_POS = 5 << 2,
+ _CELL_STEP_Z_NEG = 6 << 2,
+ _CELL_STEP_DONE = 7 << 2,
+ _CELL_PREV_MASK = 0xE0,
+ _CELL_PREV_NONE = 0 << 5,
+ _CELL_PREV_Y_POS = 1 << 5,
+ _CELL_PREV_Y_NEG = 2 << 5,
+ _CELL_PREV_X_POS = 3 << 5,
+ _CELL_PREV_X_NEG = 4 << 5,
+ _CELL_PREV_Z_POS = 5 << 5,
+ _CELL_PREV_Z_NEG = 6 << 5,
+ _CELL_PREV_FIRST = 7 << 5,
};
-static inline void _plot_face(uint8_t*** p_cell_status,int x,int y,int z,int len_x,int len_y,int len_z,const Vector3& voxelsize,const Face3& p_face) {
+static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, const Vector3 &voxelsize, const Face3 &p_face) {
- Rect3 aabb( Vector3(x,y,z),Vector3(len_x,len_y,len_z));
- aabb.pos=aabb.pos*voxelsize;
- aabb.size=aabb.size*voxelsize;
+ Rect3 aabb(Vector3(x, y, z), Vector3(len_x, len_y, len_z));
+ aabb.pos = aabb.pos * voxelsize;
+ aabb.size = aabb.size * voxelsize;
if (!p_face.intersects_aabb(aabb))
return;
- if (len_x==1 && len_y==1 && len_z==1) {
+ if (len_x == 1 && len_y == 1 && len_z == 1) {
- p_cell_status[x][y][z]=_CELL_SOLID;
+ p_cell_status[x][y][z] = _CELL_SOLID;
return;
}
-
-
- int div_x=len_x>1?2:1;
- int div_y=len_y>1?2:1;
- int div_z=len_z>1?2:1;
-
-#define _SPLIT(m_i,m_div,m_v,m_len_v,m_new_v,m_new_len_v)\
- if (m_div==1) {\
- m_new_v=m_v;\
- m_new_len_v=1; \
- } else if (m_i==0) {\
- m_new_v=m_v;\
- m_new_len_v=m_len_v/2;\
- } else {\
- m_new_v=m_v+m_len_v/2;\
- m_new_len_v=m_len_v-m_len_v/2; \
+ int div_x = len_x > 1 ? 2 : 1;
+ int div_y = len_y > 1 ? 2 : 1;
+ int div_z = len_z > 1 ? 2 : 1;
+
+#define _SPLIT(m_i, m_div, m_v, m_len_v, m_new_v, m_new_len_v) \
+ if (m_div == 1) { \
+ m_new_v = m_v; \
+ m_new_len_v = 1; \
+ } else if (m_i == 0) { \
+ m_new_v = m_v; \
+ m_new_len_v = m_len_v / 2; \
+ } else { \
+ m_new_v = m_v + m_len_v / 2; \
+ m_new_len_v = m_len_v - m_len_v / 2; \
}
int new_x;
@@ -342,84 +335,83 @@ static inline void _plot_face(uint8_t*** p_cell_status,int x,int y,int z,int len
int new_z;
int new_len_z;
- for (int i=0;i<div_x;i++) {
-
+ for (int i = 0; i < div_x; i++) {
- _SPLIT(i,div_x,x,len_x,new_x,new_len_x);
+ _SPLIT(i, div_x, x, len_x, new_x, new_len_x);
- for (int j=0;j<div_y;j++) {
+ for (int j = 0; j < div_y; j++) {
- _SPLIT(j,div_y,y,len_y,new_y,new_len_y);
+ _SPLIT(j, div_y, y, len_y, new_y, new_len_y);
- for (int k=0;k<div_z;k++) {
+ for (int k = 0; k < div_z; k++) {
- _SPLIT(k,div_z,z,len_z,new_z,new_len_z);
+ _SPLIT(k, div_z, z, len_z, new_z, new_len_z);
- _plot_face(p_cell_status,new_x,new_y,new_z,new_len_x,new_len_y,new_len_z,voxelsize,p_face);
+ _plot_face(p_cell_status, new_x, new_y, new_z, new_len_x, new_len_y, new_len_z, voxelsize, p_face);
}
}
}
}
-static inline void _mark_outside(uint8_t*** p_cell_status,int x,int y,int z,int len_x,int len_y,int len_z) {
+static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z) {
- if (p_cell_status[x][y][z]&3)
+ if (p_cell_status[x][y][z] & 3)
return; // nothing to do, already used and/or visited
- p_cell_status[x][y][z]=_CELL_PREV_FIRST;
+ p_cell_status[x][y][z] = _CELL_PREV_FIRST;
- while(true) {
+ while (true) {
uint8_t &c = p_cell_status[x][y][z];
//printf("at %i,%i,%i\n",x,y,z);
- if ( (c&_CELL_STEP_MASK)==_CELL_STEP_NONE) {
+ if ((c & _CELL_STEP_MASK) == _CELL_STEP_NONE) {
/* Haven't been in here, mark as outside */
- p_cell_status[x][y][z]|=_CELL_EXTERIOR;
+ p_cell_status[x][y][z] |= _CELL_EXTERIOR;
//printf("not marked as anything, marking exterior\n");
}
//printf("cell step is %i\n",(c&_CELL_STEP_MASK));
- if ( (c&_CELL_STEP_MASK)!=_CELL_STEP_DONE) {
+ if ((c & _CELL_STEP_MASK) != _CELL_STEP_DONE) {
/* if not done, increase step */
- c+=1<<2;
+ c += 1 << 2;
//printf("incrementing cell step\n");
}
- if ( (c&_CELL_STEP_MASK)==_CELL_STEP_DONE) {
+ if ((c & _CELL_STEP_MASK) == _CELL_STEP_DONE) {
/* Go back */
//printf("done, going back a cell\n");
- switch(c&_CELL_PREV_MASK) {
+ switch (c & _CELL_PREV_MASK) {
case _CELL_PREV_FIRST: {
//printf("at end, finished marking\n");
return;
} break;
case _CELL_PREV_Y_POS: {
y++;
- ERR_FAIL_COND(y>=len_y);
+ ERR_FAIL_COND(y >= len_y);
} break;
case _CELL_PREV_Y_NEG: {
y--;
- ERR_FAIL_COND(y<0);
+ ERR_FAIL_COND(y < 0);
} break;
case _CELL_PREV_X_POS: {
x++;
- ERR_FAIL_COND(x>=len_x);
+ ERR_FAIL_COND(x >= len_x);
} break;
case _CELL_PREV_X_NEG: {
x--;
- ERR_FAIL_COND(x<0);
+ ERR_FAIL_COND(x < 0);
} break;
case _CELL_PREV_Z_POS: {
z++;
- ERR_FAIL_COND(z>=len_z);
+ ERR_FAIL_COND(z >= len_z);
} break;
case _CELL_PREV_Z_NEG: {
z--;
- ERR_FAIL_COND(z<0);
+ ERR_FAIL_COND(z < 0);
} break;
default: {
ERR_FAIL();
@@ -430,70 +422,69 @@ static inline void _mark_outside(uint8_t*** p_cell_status,int x,int y,int z,int
//printf("attempting new cell!\n");
- int next_x=x,next_y=y,next_z=z;
- uint8_t prev=0;
+ int next_x = x, next_y = y, next_z = z;
+ uint8_t prev = 0;
- switch(c&_CELL_STEP_MASK) {
+ switch (c & _CELL_STEP_MASK) {
case _CELL_STEP_Y_POS: {
next_y++;
- prev=_CELL_PREV_Y_NEG;
+ prev = _CELL_PREV_Y_NEG;
} break;
case _CELL_STEP_Y_NEG: {
next_y--;
- prev=_CELL_PREV_Y_POS;
+ prev = _CELL_PREV_Y_POS;
} break;
case _CELL_STEP_X_POS: {
next_x++;
- prev=_CELL_PREV_X_NEG;
+ prev = _CELL_PREV_X_NEG;
} break;
case _CELL_STEP_X_NEG: {
next_x--;
- prev=_CELL_PREV_X_POS;
+ prev = _CELL_PREV_X_POS;
} break;
case _CELL_STEP_Z_POS: {
next_z++;
- prev=_CELL_PREV_Z_NEG;
+ prev = _CELL_PREV_Z_NEG;
} break;
case _CELL_STEP_Z_NEG: {
next_z--;
- prev=_CELL_PREV_Z_POS;
+ prev = _CELL_PREV_Z_POS;
} break;
default: ERR_FAIL();
-
}
//printf("testing if new cell will be ok...!\n");
- if (next_x<0 || next_x>=len_x)
+ if (next_x < 0 || next_x >= len_x)
continue;
- if (next_y<0 || next_y>=len_y)
+ if (next_y < 0 || next_y >= len_y)
continue;
- if (next_z<0 || next_z>=len_z)
+ if (next_z < 0 || next_z >= len_z)
continue;
//printf("testing if new cell is traversable\n");
- if (p_cell_status[next_x][next_y][next_z]&3)
+ if (p_cell_status[next_x][next_y][next_z] & 3)
continue;
//printf("move to it\n");
- x=next_x;
- y=next_y;
- z=next_z;
- p_cell_status[x][y][z]|=prev;
+ x = next_x;
+ y = next_y;
+ z = next_z;
+ p_cell_status[x][y][z] |= prev;
}
}
-static inline void _build_faces(uint8_t*** p_cell_status,int x,int y,int z,int len_x,int len_y,int len_z,PoolVector<Face3>& p_faces) {
+static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, PoolVector<Face3> &p_faces) {
- ERR_FAIL_INDEX(x,len_x);
- ERR_FAIL_INDEX(y,len_y);
- ERR_FAIL_INDEX(z,len_z);
+ ERR_FAIL_INDEX(x, len_x);
+ ERR_FAIL_INDEX(y, len_y);
+ ERR_FAIL_INDEX(z, len_z);
- if (p_cell_status[x][y][z]&_CELL_EXTERIOR)
+ if (p_cell_status[x][y][z] & _CELL_EXTERIOR)
return;
/* static const Vector3 vertices[8]={
@@ -507,18 +498,18 @@ static inline void _build_faces(uint8_t*** p_cell_status,int x,int y,int z,int l
Vector3(1,1,1),
};
*/
-#define vert(m_idx) Vector3( (m_idx&4)>>2, (m_idx&2)>>1, m_idx&1 )
+#define vert(m_idx) Vector3((m_idx & 4) >> 2, (m_idx & 2) >> 1, m_idx & 1)
- static const uint8_t indices[6][4]={
- {7,6,4,5},
- {7,3,2,6},
- {7,5,1,3},
- {0,2,3,1},
- {0,1,5,4},
- {0,4,6,2},
+ static const uint8_t indices[6][4] = {
+ { 7, 6, 4, 5 },
+ { 7, 3, 2, 6 },
+ { 7, 5, 1, 3 },
+ { 0, 2, 3, 1 },
+ { 0, 1, 5, 4 },
+ { 0, 4, 6, 2 },
};
-/*
+ /*
{0,1,2,3},
{0,1,4,5},
@@ -535,114 +526,107 @@ static inline void _build_faces(uint8_t*** p_cell_status,int x,int y,int z,int l
{7,5,1,3},
*/
- for (int i=0;i<6;i++) {
+ for (int i = 0; i < 6; i++) {
Vector3 face_points[4];
- int disp_x=x+((i%3)==0?((i<3)?1:-1):0);
- int disp_y=y+(((i-1)%3)==0?((i<3)?1:-1):0);
- int disp_z=z+(((i-2)%3)==0?((i<3)?1:-1):0);
+ int disp_x = x + ((i % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
+ int disp_y = y + (((i - 1) % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
+ int disp_z = z + (((i - 2) % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
- bool plot=false;
+ bool plot = false;
- if (disp_x<0 || disp_x>=len_x)
- plot=true;
- if (disp_y<0 || disp_y>=len_y)
- plot=true;
- if (disp_z<0 || disp_z>=len_z)
- plot=true;
+ if (disp_x < 0 || disp_x >= len_x)
+ plot = true;
+ if (disp_y < 0 || disp_y >= len_y)
+ plot = true;
+ if (disp_z < 0 || disp_z >= len_z)
+ plot = true;
- if (!plot && (p_cell_status[disp_x][disp_y][disp_z]&_CELL_EXTERIOR))
- plot=true;
+ if (!plot && (p_cell_status[disp_x][disp_y][disp_z] & _CELL_EXTERIOR))
+ plot = true;
if (!plot)
continue;
- for (int j=0;j<4;j++)
- face_points[j]=vert( indices[i][j] ) + Vector3(x,y,z);
+ for (int j = 0; j < 4; j++)
+ face_points[j] = vert(indices[i][j]) + Vector3(x, y, z);
p_faces.push_back(
- Face3(
- face_points[0],
- face_points[1],
- face_points[2]
- )
- );
+ Face3(
+ face_points[0],
+ face_points[1],
+ face_points[2]));
p_faces.push_back(
- Face3(
- face_points[2],
- face_points[3],
- face_points[0]
- )
- );
-
+ Face3(
+ face_points[2],
+ face_points[3],
+ face_points[0]));
}
-
}
-PoolVector< Face3 > Geometry::wrap_geometry( PoolVector< Face3 > p_array,real_t *p_error ) {
+PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_error) {
#define _MIN_SIZE 1.0
#define _MAX_LENGTH 20
- int face_count=p_array.size();
- PoolVector<Face3>::Read facesr=p_array.read();
+ int face_count = p_array.size();
+ PoolVector<Face3>::Read facesr = p_array.read();
const Face3 *faces = facesr.ptr();
Rect3 global_aabb;
- for(int i=0;i<face_count;i++) {
+ for (int i = 0; i < face_count; i++) {
- if (i==0) {
+ if (i == 0) {
- global_aabb=faces[i].get_aabb();
+ global_aabb = faces[i].get_aabb();
} else {
- global_aabb.merge_with( faces[i].get_aabb() );
+ global_aabb.merge_with(faces[i].get_aabb());
}
}
global_aabb.grow_by(0.01); // avoid numerical error
// determine amount of cells in grid axis
- int div_x,div_y,div_z;
+ int div_x, div_y, div_z;
- if (global_aabb.size.x/_MIN_SIZE<_MAX_LENGTH)
- div_x=(int)(global_aabb.size.x/_MIN_SIZE)+1;
+ if (global_aabb.size.x / _MIN_SIZE < _MAX_LENGTH)
+ div_x = (int)(global_aabb.size.x / _MIN_SIZE) + 1;
else
- div_x=_MAX_LENGTH;
+ div_x = _MAX_LENGTH;
- if (global_aabb.size.y/_MIN_SIZE<_MAX_LENGTH)
- div_y=(int)(global_aabb.size.y/_MIN_SIZE)+1;
+ if (global_aabb.size.y / _MIN_SIZE < _MAX_LENGTH)
+ div_y = (int)(global_aabb.size.y / _MIN_SIZE) + 1;
else
- div_y=_MAX_LENGTH;
+ div_y = _MAX_LENGTH;
- if (global_aabb.size.z/_MIN_SIZE<_MAX_LENGTH)
- div_z=(int)(global_aabb.size.z/_MIN_SIZE)+1;
+ if (global_aabb.size.z / _MIN_SIZE < _MAX_LENGTH)
+ div_z = (int)(global_aabb.size.z / _MIN_SIZE) + 1;
else
- div_z=_MAX_LENGTH;
-
- Vector3 voxelsize=global_aabb.size;
- voxelsize.x/=div_x;
- voxelsize.y/=div_y;
- voxelsize.z/=div_z;
+ div_z = _MAX_LENGTH;
+ Vector3 voxelsize = global_aabb.size;
+ voxelsize.x /= div_x;
+ voxelsize.y /= div_y;
+ voxelsize.z /= div_z;
// create and initialize cells to zero
//print_line("Wrapper: Initializing Cells");
- uint8_t ***cell_status=memnew_arr(uint8_t**,div_x);
- for(int i=0;i<div_x;i++) {
+ uint8_t ***cell_status = memnew_arr(uint8_t **, div_x);
+ for (int i = 0; i < div_x; i++) {
- cell_status[i]=memnew_arr(uint8_t*,div_y);
+ cell_status[i] = memnew_arr(uint8_t *, div_y);
- for(int j=0;j<div_y;j++) {
+ for (int j = 0; j < div_y; j++) {
- cell_status[i][j]=memnew_arr(uint8_t,div_z);
+ cell_status[i][j] = memnew_arr(uint8_t, div_z);
- for(int k=0;k<div_z;k++) {
+ for (int k = 0; k < div_z; k++) {
- cell_status[i][j][k]=0;
+ cell_status[i][j][k] = 0;
}
}
}
@@ -650,45 +634,44 @@ PoolVector< Face3 > Geometry::wrap_geometry( PoolVector< Face3 > p_array,real_t
// plot faces into cells
//print_line("Wrapper (1/6): Plotting Faces");
- for (int i=0;i<face_count;i++) {
+ for (int i = 0; i < face_count; i++) {
- Face3 f=faces[i];
- for (int j=0;j<3;j++) {
+ Face3 f = faces[i];
+ for (int j = 0; j < 3; j++) {
- f.vertex[j]-=global_aabb.pos;
+ f.vertex[j] -= global_aabb.pos;
}
- _plot_face(cell_status,0,0,0,div_x,div_y,div_z,voxelsize,f);
+ _plot_face(cell_status, 0, 0, 0, div_x, div_y, div_z, voxelsize, f);
}
-
// determine which cells connect to the outside by traversing the outside and recursively flood-fill marking
//print_line("Wrapper (2/6): Flood Filling");
- for (int i=0;i<div_x;i++) {
+ for (int i = 0; i < div_x; i++) {
- for (int j=0;j<div_y;j++) {
+ for (int j = 0; j < div_y; j++) {
- _mark_outside(cell_status,i,j,0,div_x,div_y,div_z);
- _mark_outside(cell_status,i,j,div_z-1,div_x,div_y,div_z);
+ _mark_outside(cell_status, i, j, 0, div_x, div_y, div_z);
+ _mark_outside(cell_status, i, j, div_z - 1, div_x, div_y, div_z);
}
}
- for (int i=0;i<div_z;i++) {
+ for (int i = 0; i < div_z; i++) {
- for (int j=0;j<div_y;j++) {
+ for (int j = 0; j < div_y; j++) {
- _mark_outside(cell_status,0,j,i,div_x,div_y,div_z);
- _mark_outside(cell_status,div_x-1,j,i,div_x,div_y,div_z);
+ _mark_outside(cell_status, 0, j, i, div_x, div_y, div_z);
+ _mark_outside(cell_status, div_x - 1, j, i, div_x, div_y, div_z);
}
}
- for (int i=0;i<div_x;i++) {
+ for (int i = 0; i < div_x; i++) {
- for (int j=0;j<div_z;j++) {
+ for (int j = 0; j < div_z; j++) {
- _mark_outside(cell_status,i,0,j,div_x,div_y,div_z);
- _mark_outside(cell_status,i,div_y-1,j,div_x,div_y,div_z);
+ _mark_outside(cell_status, i, 0, j, div_x, div_y, div_z);
+ _mark_outside(cell_status, i, div_y - 1, j, div_x, div_y, div_z);
}
}
@@ -698,13 +681,13 @@ PoolVector< Face3 > Geometry::wrap_geometry( PoolVector< Face3 > p_array,real_t
PoolVector<Face3> wrapped_faces;
- for (int i=0;i<div_x;i++) {
+ for (int i = 0; i < div_x; i++) {
- for (int j=0;j<div_y;j++) {
+ for (int j = 0; j < div_y; j++) {
- for (int k=0;k<div_z;k++) {
+ for (int k = 0; k < div_z; k++) {
- _build_faces(cell_status,i,j,k,div_x,div_y,div_z,wrapped_faces);
+ _build_faces(cell_status, i, j, k, div_x, div_y, div_z, wrapped_faces);
}
}
}
@@ -713,36 +696,36 @@ PoolVector< Face3 > Geometry::wrap_geometry( PoolVector< Face3 > p_array,real_t
// transform face vertices to global coords
- int wrapped_faces_count=wrapped_faces.size();
- PoolVector<Face3>::Write wrapped_facesw=wrapped_faces.write();
- Face3* wrapped_faces_ptr=wrapped_facesw.ptr();
+ int wrapped_faces_count = wrapped_faces.size();
+ PoolVector<Face3>::Write wrapped_facesw = wrapped_faces.write();
+ Face3 *wrapped_faces_ptr = wrapped_facesw.ptr();
- for(int i=0;i<wrapped_faces_count;i++) {
+ for (int i = 0; i < wrapped_faces_count; i++) {
- for(int j=0;j<3;j++) {
+ for (int j = 0; j < 3; j++) {
- Vector3& v = wrapped_faces_ptr[i].vertex[j];
- v=v*voxelsize;
- v+=global_aabb.pos;
+ Vector3 &v = wrapped_faces_ptr[i].vertex[j];
+ v = v * voxelsize;
+ v += global_aabb.pos;
}
}
// clean up grid
//print_line("Wrapper (5/6): Grid Cleanup");
- for(int i=0;i<div_x;i++) {
+ for (int i = 0; i < div_x; i++) {
- for(int j=0;j<div_y;j++) {
+ for (int j = 0; j < div_y; j++) {
- memdelete_arr( cell_status[i][j] );
+ memdelete_arr(cell_status[i][j]);
}
- memdelete_arr( cell_status[i] );
+ memdelete_arr(cell_status[i]);
}
memdelete_arr(cell_status);
if (p_error)
- *p_error=voxelsize.length();
+ *p_error = voxelsize.length();
//print_line("Wrapper (6/6): Finished.");
return wrapped_faces;
@@ -752,131 +735,125 @@ Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes
MeshData mesh;
-
#define SUBPLANE_SIZE 1024.0
real_t subplane_size = 1024.0; // should compute this from the actual plane
- for (int i=0;i<p_planes.size();i++) {
+ for (int i = 0; i < p_planes.size(); i++) {
- Plane p =p_planes[i];
+ Plane p = p_planes[i];
- Vector3 ref=Vector3(0.0,1.0,0.0);
+ Vector3 ref = Vector3(0.0, 1.0, 0.0);
- if (ABS(p.normal.dot(ref))>0.95)
- ref=Vector3(0.0,0.0,1.0); // change axis
+ if (ABS(p.normal.dot(ref)) > 0.95)
+ ref = Vector3(0.0, 0.0, 1.0); // change axis
Vector3 right = p.normal.cross(ref).normalized();
- Vector3 up = p.normal.cross( right ).normalized();
+ Vector3 up = p.normal.cross(right).normalized();
- Vector< Vector3 > vertices;
+ Vector<Vector3> vertices;
Vector3 center = p.get_any_point();
// make a quad clockwise
- vertices.push_back( center - up * subplane_size + right * subplane_size );
- vertices.push_back( center - up * subplane_size - right * subplane_size );
- vertices.push_back( center + up * subplane_size - right * subplane_size );
- vertices.push_back( center + up * subplane_size + right * subplane_size );
+ vertices.push_back(center - up * subplane_size + right * subplane_size);
+ vertices.push_back(center - up * subplane_size - right * subplane_size);
+ vertices.push_back(center + up * subplane_size - right * subplane_size);
+ vertices.push_back(center + up * subplane_size + right * subplane_size);
- for (int j=0;j<p_planes.size();j++) {
+ for (int j = 0; j < p_planes.size(); j++) {
- if (j==i)
+ if (j == i)
continue;
+ Vector<Vector3> new_vertices;
+ Plane clip = p_planes[j];
- Vector< Vector3 > new_vertices;
- Plane clip=p_planes[j];
-
- if (clip.normal.dot(p.normal)>0.95)
+ if (clip.normal.dot(p.normal) > 0.95)
continue;
- if (vertices.size()<3)
+ if (vertices.size() < 3)
break;
- for(int k=0;k<vertices.size();k++) {
+ for (int k = 0; k < vertices.size(); k++) {
- int k_n=(k+1)%vertices.size();
+ int k_n = (k + 1) % vertices.size();
- Vector3 edge0_A=vertices[k];
- Vector3 edge1_A=vertices[k_n];
+ Vector3 edge0_A = vertices[k];
+ Vector3 edge1_A = vertices[k_n];
real_t dist0 = clip.distance_to(edge0_A);
real_t dist1 = clip.distance_to(edge1_A);
-
- if ( dist0 <= 0 ) { // behind plane
+ if (dist0 <= 0) { // behind plane
new_vertices.push_back(vertices[k]);
}
-
// check for different sides and non coplanar
- if ( (dist0*dist1) < 0) {
+ if ((dist0 * dist1) < 0) {
// calculate intersection
Vector3 rel = edge1_A - edge0_A;
- real_t den=clip.normal.dot( rel );
- if (Math::abs(den)<CMP_EPSILON)
+ real_t den = clip.normal.dot(rel);
+ if (Math::abs(den) < CMP_EPSILON)
continue; // point too short
- real_t dist=-(clip.normal.dot( edge0_A )-clip.d)/den;
- Vector3 inters = edge0_A+rel*dist;
+ real_t dist = -(clip.normal.dot(edge0_A) - clip.d) / den;
+ Vector3 inters = edge0_A + rel * dist;
new_vertices.push_back(inters);
}
}
- vertices=new_vertices;
+ vertices = new_vertices;
}
- if (vertices.size()<3)
+ if (vertices.size() < 3)
continue;
-
//result is a clockwise face
MeshData::Face face;
// add face indices
- for (int j=0;j<vertices.size();j++) {
-
+ for (int j = 0; j < vertices.size(); j++) {
- int idx=-1;
- for (int k=0;k<mesh.vertices.size();k++) {
+ int idx = -1;
+ for (int k = 0; k < mesh.vertices.size(); k++) {
- if (mesh.vertices[k].distance_to(vertices[j])<0.001) {
+ if (mesh.vertices[k].distance_to(vertices[j]) < 0.001) {
- idx=k;
+ idx = k;
break;
}
}
- if (idx==-1) {
+ if (idx == -1) {
- idx=mesh.vertices.size();
+ idx = mesh.vertices.size();
mesh.vertices.push_back(vertices[j]);
}
face.indices.push_back(idx);
}
- face.plane=p;
+ face.plane = p;
mesh.faces.push_back(face);
//add edge
- for(int j=0;j<face.indices.size();j++) {
+ for (int j = 0; j < face.indices.size(); j++) {
- int a=face.indices[j];
- int b=face.indices[(j+1)%face.indices.size()];
+ int a = face.indices[j];
+ int b = face.indices[(j + 1) % face.indices.size()];
- bool found=false;
- for(int k=0;k<mesh.edges.size();k++) {
+ bool found = false;
+ for (int k = 0; k < mesh.edges.size(); k++) {
- if (mesh.edges[k].a==a && mesh.edges[k].b==b) {
- found=true;
+ if (mesh.edges[k].a == a && mesh.edges[k].b == b) {
+ found = true;
break;
}
- if (mesh.edges[k].b==a && mesh.edges[k].a==b) {
- found=true;
+ if (mesh.edges[k].b == a && mesh.edges[k].a == b) {
+ found = true;
break;
}
}
@@ -884,28 +861,25 @@ Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes
if (found)
continue;
MeshData::Edge edge;
- edge.a=a;
- edge.b=b;
+ edge.a = a;
+ edge.b = b;
mesh.edges.push_back(edge);
}
-
-
}
return mesh;
}
-
-PoolVector<Plane> Geometry::build_box_planes(const Vector3& p_extents) {
+PoolVector<Plane> Geometry::build_box_planes(const Vector3 &p_extents) {
PoolVector<Plane> planes;
- planes.push_back( Plane( Vector3(1,0,0), p_extents.x ) );
- planes.push_back( Plane( Vector3(-1,0,0), p_extents.x ) );
- planes.push_back( Plane( Vector3(0,1,0), p_extents.y ) );
- planes.push_back( Plane( Vector3(0,-1,0), p_extents.y ) );
- planes.push_back( Plane( Vector3(0,0,1), p_extents.z ) );
- planes.push_back( Plane( Vector3(0,0,-1), p_extents.z ) );
+ planes.push_back(Plane(Vector3(1, 0, 0), p_extents.x));
+ planes.push_back(Plane(Vector3(-1, 0, 0), p_extents.x));
+ planes.push_back(Plane(Vector3(0, 1, 0), p_extents.y));
+ planes.push_back(Plane(Vector3(0, -1, 0), p_extents.y));
+ planes.push_back(Plane(Vector3(0, 0, 1), p_extents.z));
+ planes.push_back(Plane(Vector3(0, 0, -1), p_extents.z));
return planes;
}
@@ -914,103 +888,95 @@ PoolVector<Plane> Geometry::build_cylinder_planes(real_t p_radius, real_t p_heig
PoolVector<Plane> planes;
- for (int i=0;i<p_sides;i++) {
+ for (int i = 0; i < p_sides; i++) {
Vector3 normal;
- normal[(p_axis+1)%3]=Math::cos(i*(2.0*Math_PI)/p_sides);
- normal[(p_axis+2)%3]=Math::sin(i*(2.0*Math_PI)/p_sides);
+ normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_sides);
+ normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_sides);
- planes.push_back( Plane( normal, p_radius ) );
+ planes.push_back(Plane(normal, p_radius));
}
Vector3 axis;
- axis[p_axis]=1.0;
+ axis[p_axis] = 1.0;
- planes.push_back( Plane( axis, p_height*0.5 ) );
- planes.push_back( Plane( -axis, p_height*0.5 ) );
+ planes.push_back(Plane(axis, p_height * 0.5));
+ planes.push_back(Plane(-axis, p_height * 0.5));
return planes;
-
}
-PoolVector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats,int p_lons, Vector3::Axis p_axis) {
-
+PoolVector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis) {
PoolVector<Plane> planes;
Vector3 axis;
- axis[p_axis]=1.0;
+ axis[p_axis] = 1.0;
Vector3 axis_neg;
- axis_neg[(p_axis+1)%3]=1.0;
- axis_neg[(p_axis+2)%3]=1.0;
- axis_neg[p_axis]=-1.0;
+ axis_neg[(p_axis + 1) % 3] = 1.0;
+ axis_neg[(p_axis + 2) % 3] = 1.0;
+ axis_neg[p_axis] = -1.0;
- for (int i=0;i<p_lons;i++) {
+ for (int i = 0; i < p_lons; i++) {
Vector3 normal;
- normal[(p_axis+1)%3]=Math::cos(i*(2.0*Math_PI)/p_lons);
- normal[(p_axis+2)%3]=Math::sin(i*(2.0*Math_PI)/p_lons);
+ normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_lons);
+ normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_lons);
- planes.push_back( Plane( normal, p_radius ) );
+ planes.push_back(Plane(normal, p_radius));
- for (int j=1;j<=p_lats;j++) {
+ for (int j = 1; j <= p_lats; j++) {
//todo this is stupid, fix
- Vector3 angle = normal.linear_interpolate(axis,j/(real_t)p_lats).normalized();
- Vector3 pos = angle*p_radius;
- planes.push_back( Plane( pos, angle ) );
- planes.push_back( Plane( pos * axis_neg, angle * axis_neg) );
-
+ Vector3 angle = normal.linear_interpolate(axis, j / (real_t)p_lats).normalized();
+ Vector3 pos = angle * p_radius;
+ planes.push_back(Plane(pos, angle));
+ planes.push_back(Plane(pos * axis_neg, angle * axis_neg));
}
}
return planes;
-
}
PoolVector<Plane> Geometry::build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
PoolVector<Plane> planes;
- Vector3 axis;
- axis[p_axis]=1.0;
+ Vector3 axis;
+ axis[p_axis] = 1.0;
Vector3 axis_neg;
- axis_neg[(p_axis+1)%3]=1.0;
- axis_neg[(p_axis+2)%3]=1.0;
- axis_neg[p_axis]=-1.0;
+ axis_neg[(p_axis + 1) % 3] = 1.0;
+ axis_neg[(p_axis + 2) % 3] = 1.0;
+ axis_neg[p_axis] = -1.0;
- for (int i=0;i<p_sides;i++) {
+ for (int i = 0; i < p_sides; i++) {
Vector3 normal;
- normal[(p_axis+1)%3]=Math::cos(i*(2.0*Math_PI)/p_sides);
- normal[(p_axis+2)%3]=Math::sin(i*(2.0*Math_PI)/p_sides);
-
- planes.push_back( Plane( normal, p_radius ) );
+ normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_sides);
+ normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_sides);
- for (int j=1;j<=p_lats;j++) {
+ planes.push_back(Plane(normal, p_radius));
- Vector3 angle = normal.linear_interpolate(axis,j/(real_t)p_lats).normalized();
- Vector3 pos = axis*p_height*0.5 + angle*p_radius;
- planes.push_back( Plane( pos, angle ) );
- planes.push_back( Plane( pos * axis_neg, angle * axis_neg) );
+ for (int j = 1; j <= p_lats; j++) {
+ Vector3 angle = normal.linear_interpolate(axis, j / (real_t)p_lats).normalized();
+ Vector3 pos = axis * p_height * 0.5 + angle * p_radius;
+ planes.push_back(Plane(pos, angle));
+ planes.push_back(Plane(pos * axis_neg, angle * axis_neg));
}
}
-
return planes;
-
}
-
struct _AtlasWorkRect {
Size2i s;
Point2i p;
int idx;
- _FORCE_INLINE_ bool operator<(const _AtlasWorkRect& p_r) const { return s.width > p_r.s.width; };
+ _FORCE_INLINE_ bool operator<(const _AtlasWorkRect &p_r) const { return s.width > p_r.s.width; };
};
struct _AtlasWorkRectResult {
@@ -1020,7 +986,7 @@ struct _AtlasWorkRectResult {
int max_h;
};
-void Geometry::make_atlas(const Vector<Size2i>& p_rects,Vector<Point2i>& r_result, Size2i& r_size) {
+void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size) {
//super simple, almost brute force scanline stacking fitter
//it's pretty basic for now, but it tries to make sure that the aspect ratio of the
@@ -1030,108 +996,100 @@ void Geometry::make_atlas(const Vector<Size2i>& p_rects,Vector<Point2i>& r_resul
// for example, it will prioritize a 1024x1024 atlas (works everywhere) instead of a
// 256x8192 atlas (won't work anywhere).
- ERR_FAIL_COND(p_rects.size()==0);
+ ERR_FAIL_COND(p_rects.size() == 0);
Vector<_AtlasWorkRect> wrects;
wrects.resize(p_rects.size());
- for(int i=0;i<p_rects.size();i++) {
- wrects[i].s=p_rects[i];
- wrects[i].idx=i;
+ for (int i = 0; i < p_rects.size(); i++) {
+ wrects[i].s = p_rects[i];
+ wrects[i].idx = i;
}
wrects.sort();
int widest = wrects[0].s.width;
Vector<_AtlasWorkRectResult> results;
- for(int i=0;i<=12;i++) {
+ for (int i = 0; i <= 12; i++) {
- int w = 1<<i;
- int max_h=0;
- int max_w=0;
- if ( w < widest )
+ int w = 1 << i;
+ int max_h = 0;
+ int max_w = 0;
+ if (w < widest)
continue;
Vector<int> hmax;
hmax.resize(w);
- for(int j=0;j<w;j++)
- hmax[j]=0;
+ for (int j = 0; j < w; j++)
+ hmax[j] = 0;
//place them
- int ofs=0;
- int limit_h=0;
- for(int j=0;j<wrects.size();j++) {
-
+ int ofs = 0;
+ int limit_h = 0;
+ for (int j = 0; j < wrects.size(); j++) {
- if (ofs+wrects[j].s.width > w) {
+ if (ofs + wrects[j].s.width > w) {
- ofs=0;
+ ofs = 0;
}
- int from_y=0;
- for(int k=0;k<wrects[j].s.width;k++) {
+ int from_y = 0;
+ for (int k = 0; k < wrects[j].s.width; k++) {
- if (hmax[ofs+k] > from_y)
- from_y=hmax[ofs+k];
+ if (hmax[ofs + k] > from_y)
+ from_y = hmax[ofs + k];
}
- wrects[j].p.x=ofs;
- wrects[j].p.y=from_y;
- int end_h = from_y+wrects[j].s.height;
- int end_w = ofs+wrects[j].s.width;
- if (ofs==0)
- limit_h=end_h;
+ wrects[j].p.x = ofs;
+ wrects[j].p.y = from_y;
+ int end_h = from_y + wrects[j].s.height;
+ int end_w = ofs + wrects[j].s.width;
+ if (ofs == 0)
+ limit_h = end_h;
- for(int k=0;k<wrects[j].s.width;k++) {
+ for (int k = 0; k < wrects[j].s.width; k++) {
- hmax[ofs+k]=end_h;
+ hmax[ofs + k] = end_h;
}
if (end_h > max_h)
- max_h=end_h;
+ max_h = end_h;
if (end_w > max_w)
- max_w=end_w;
-
- if (ofs==0 || end_h>limit_h ) //while h limit not reched, keep stacking
- ofs+=wrects[j].s.width;
+ max_w = end_w;
+ if (ofs == 0 || end_h > limit_h) //while h limit not reched, keep stacking
+ ofs += wrects[j].s.width;
}
_AtlasWorkRectResult result;
- result.result=wrects;
- result.max_h=max_h;
- result.max_w=max_w;
+ result.result = wrects;
+ result.max_h = max_h;
+ result.max_w = max_w;
results.push_back(result);
-
}
//find the result with the best aspect ratio
- int best=-1;
- real_t best_aspect=1e20;
+ int best = -1;
+ real_t best_aspect = 1e20;
- for(int i=0;i<results.size();i++) {
+ for (int i = 0; i < results.size(); i++) {
real_t h = nearest_power_of_2(results[i].max_h);
real_t w = nearest_power_of_2(results[i].max_w);
- real_t aspect = h>w ? h/w : w/h;
+ real_t aspect = h > w ? h / w : w / h;
if (aspect < best_aspect) {
- best=i;
- best_aspect=aspect;
+ best = i;
+ best_aspect = aspect;
}
}
r_result.resize(p_rects.size());
- for(int i=0;i<p_rects.size();i++) {
+ for (int i = 0; i < p_rects.size(); i++) {
- r_result[ results[best].result[i].idx ]=results[best].result[i].p;
+ r_result[results[best].result[i].idx] = results[best].result[i].p;
}
- r_size=Size2(results[best].max_w,results[best].max_h );
-
+ r_size = Size2(results[best].max_w, results[best].max_h);
}
-
-
-
-
diff --git a/core/math/geometry.h b/core/math/geometry.h
index 1dd7df038d..93ab0be2e0 100644
--- a/core/math/geometry.h
+++ b/core/math/geometry.h
@@ -29,26 +29,23 @@
#ifndef GEOMETRY_H
#define GEOMETRY_H
-#include "vector3.h"
-#include "face3.h"
#include "dvector.h"
+#include "face3.h"
#include "math_2d.h"
-#include "vector.h"
-#include "print_string.h"
#include "object.h"
+#include "print_string.h"
#include "triangulate.h"
+#include "vector.h"
+#include "vector3.h"
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
class Geometry {
Geometry();
-public:
-
-
-
- static real_t get_closest_points_between_segments( const Vector2& p1,const Vector2& q1, const Vector2& p2,const Vector2& q2, Vector2& c1, Vector2& c2) {
+public:
+ static real_t get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2, Vector2 &c1, Vector2 &c2) {
Vector2 d1 = q1 - p1; // Direction vector of segment S1
Vector2 d2 = q2 - p2; // Direction vector of segment S2
@@ -56,7 +53,7 @@ public:
real_t a = d1.dot(d1); // Squared length of segment S1, always nonnegative
real_t e = d2.dot(d2); // Squared length of segment S2, always nonnegative
real_t f = d2.dot(r);
- real_t s,t;
+ real_t s, t;
// Check if either or both segments degenerate into points
if (a <= CMP_EPSILON && e <= CMP_EPSILON) {
// Both segments degenerate into points
@@ -78,16 +75,16 @@ public:
} else {
// The general nondegenerate case starts here
real_t b = d1.dot(d2);
- real_t denom = a*e-b*b; // Always nonnegative
+ real_t denom = a * e - b * b; // Always nonnegative
// If segments not parallel, compute closest point on L1 to L2 and
// clamp to segment S1. Else pick arbitrary s (here 0)
if (denom != 0.0) {
- s = CLAMP((b*f - c*e) / denom, 0.0, 1.0);
+ s = CLAMP((b * f - c * e) / denom, 0.0, 1.0);
} else
s = 0.0;
// Compute point on L2 closest to S1(s) using
// t = Dot((P1 + D1*s) - P2,D2) / Dot(D2,D2) = (b*s + f) / e
- t = (b*s + f) / e;
+ t = (b * s + f) / e;
//If t in [0,1] done. Else clamp t, recompute s for the new value
// of t using s = Dot((P2 + D2*t) - P1,D1) / Dot(D1,D1)= (t*b - c) / a
@@ -106,12 +103,10 @@ public:
return Math::sqrt((c1 - c2).dot(c1 - c2));
}
-
- static void get_closest_points_between_segments(const Vector3& p1,const Vector3& p2,const Vector3& q1,const Vector3& q2,Vector3& c1, Vector3& c2)
- {
+ static void get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2, Vector3 &c1, Vector3 &c2) {
#if 0
//do the function 'd' as defined by pb. I think is is dot product of some sort
-#define d_of(m,n,o,p) ( (m.x - n.x) * (o.x - p.x) + (m.y - n.y) * (o.y - p.y) + (m.z - n.z) * (o.z - p.z) )
+#define d_of(m, n, o, p) ((m.x - n.x) * (o.x - p.x) + (m.y - n.y) * (o.y - p.y) + (m.z - n.z) * (o.z - p.z))
//caluclate the parpametric position on the 2 curves, mua and mub
real_t mua = ( d_of(p1,q1,q2,q1) * d_of(q2,q1,p2,p1) - d_of(p1,q1,p2,p1) * d_of(q2,q1,q2,q1) ) / ( d_of(p2,p1,p2,p1) * d_of(q2,q1,q2,q1) - d_of(q2,q1,p2,p1) * d_of(q2,q1,p2,p1) );
@@ -126,25 +121,24 @@ public:
c2 = q1.linear_interpolate(q2,mub);
#endif
- Vector3 u = p2 - p1;
- Vector3 v = q2 - q1;
- Vector3 w = p1 - q1;
- float a = u.dot(u);
- float b = u.dot(v);
- float c = v.dot(v); // always >= 0
- float d = u.dot(w);
- float e = v.dot(w);
- float D = a*c - b*b; // always >= 0
- float sc, tc;
+ Vector3 u = p2 - p1;
+ Vector3 v = q2 - q1;
+ Vector3 w = p1 - q1;
+ float a = u.dot(u);
+ float b = u.dot(v);
+ float c = v.dot(v); // always >= 0
+ float d = u.dot(w);
+ float e = v.dot(w);
+ float D = a * c - b * b; // always >= 0
+ float sc, tc;
// compute the line parameters of the two closest points
- if (D < CMP_EPSILON) { // the lines are almost parallel
+ if (D < CMP_EPSILON) { // the lines are almost parallel
sc = 0.0;
- tc = (b>c ? d/b : e/c); // use the largest denominator
- }
- else {
- sc = (b*e - c*d) / D;
- tc = (a*e - b*d) / D;
+ tc = (b > c ? d / b : e / c); // use the largest denominator
+ } else {
+ sc = (b * e - c * d) / D;
+ tc = (a * e - b * d) / D;
}
c1 = w + sc * u;
@@ -153,92 +147,89 @@ public:
//Vector dP = w + (sc * u) - (tc * v); // = L1(sc) - L2(tc)
}
- static real_t get_closest_distance_between_segments( const Vector3& p_from_a,const Vector3& p_to_a, const Vector3& p_from_b,const Vector3& p_to_b) {
- Vector3 u = p_to_a - p_from_a;
- Vector3 v = p_to_b - p_from_b;
- Vector3 w = p_from_a - p_to_a;
- real_t a = u.dot(u); // always >= 0
- real_t b = u.dot(v);
- real_t c = v.dot(v); // always >= 0
- real_t d = u.dot(w);
- real_t e = v.dot(w);
- real_t D = a*c - b*b; // always >= 0
- real_t sc, sN, sD = D; // sc = sN / sD, default sD = D >= 0
- real_t tc, tN, tD = D; // tc = tN / tD, default tD = D >= 0
-
- // compute the line parameters of the two closest points
- if (D < CMP_EPSILON) { // the lines are almost parallel
- sN = 0.0; // force using point P0 on segment S1
- sD = 1.0; // to prevent possible division by 0.0 later
- tN = e;
- tD = c;
- }
- else { // get the closest points on the infinite lines
- sN = (b*e - c*d);
- tN = (a*e - b*d);
- if (sN < 0.0) { // sc < 0 => the s=0 edge is visible
- sN = 0.0;
- tN = e;
- tD = c;
- }
- else if (sN > sD) { // sc > 1 => the s=1 edge is visible
- sN = sD;
- tN = e + b;
- tD = c;
- }
- }
-
- if (tN < 0.0) { // tc < 0 => the t=0 edge is visible
- tN = 0.0;
- // recompute sc for this edge
- if (-d < 0.0)
- sN = 0.0;
- else if (-d > a)
- sN = sD;
- else {
- sN = -d;
- sD = a;
+ static real_t get_closest_distance_between_segments(const Vector3 &p_from_a, const Vector3 &p_to_a, const Vector3 &p_from_b, const Vector3 &p_to_b) {
+ Vector3 u = p_to_a - p_from_a;
+ Vector3 v = p_to_b - p_from_b;
+ Vector3 w = p_from_a - p_to_a;
+ real_t a = u.dot(u); // always >= 0
+ real_t b = u.dot(v);
+ real_t c = v.dot(v); // always >= 0
+ real_t d = u.dot(w);
+ real_t e = v.dot(w);
+ real_t D = a * c - b * b; // always >= 0
+ real_t sc, sN, sD = D; // sc = sN / sD, default sD = D >= 0
+ real_t tc, tN, tD = D; // tc = tN / tD, default tD = D >= 0
+
+ // compute the line parameters of the two closest points
+ if (D < CMP_EPSILON) { // the lines are almost parallel
+ sN = 0.0; // force using point P0 on segment S1
+ sD = 1.0; // to prevent possible division by 0.0 later
+ tN = e;
+ tD = c;
+ } else { // get the closest points on the infinite lines
+ sN = (b * e - c * d);
+ tN = (a * e - b * d);
+ if (sN < 0.0) { // sc < 0 => the s=0 edge is visible
+ sN = 0.0;
+ tN = e;
+ tD = c;
+ } else if (sN > sD) { // sc > 1 => the s=1 edge is visible
+ sN = sD;
+ tN = e + b;
+ tD = c;
+ }
}
- }
- else if (tN > tD) { // tc > 1 => the t=1 edge is visible
- tN = tD;
- // recompute sc for this edge
- if ((-d + b) < 0.0)
- sN = 0;
- else if ((-d + b) > a)
- sN = sD;
- else {
- sN = (-d + b);
- sD = a;
+
+ if (tN < 0.0) { // tc < 0 => the t=0 edge is visible
+ tN = 0.0;
+ // recompute sc for this edge
+ if (-d < 0.0)
+ sN = 0.0;
+ else if (-d > a)
+ sN = sD;
+ else {
+ sN = -d;
+ sD = a;
+ }
+ } else if (tN > tD) { // tc > 1 => the t=1 edge is visible
+ tN = tD;
+ // recompute sc for this edge
+ if ((-d + b) < 0.0)
+ sN = 0;
+ else if ((-d + b) > a)
+ sN = sD;
+ else {
+ sN = (-d + b);
+ sD = a;
+ }
}
- }
- // finally do the division to get sc and tc
- sc = (Math::abs(sN) < CMP_EPSILON ? 0.0 : sN / sD);
- tc = (Math::abs(tN) < CMP_EPSILON ? 0.0 : tN / tD);
+ // finally do the division to get sc and tc
+ sc = (Math::abs(sN) < CMP_EPSILON ? 0.0 : sN / sD);
+ tc = (Math::abs(tN) < CMP_EPSILON ? 0.0 : tN / tD);
- // get the difference of the two closest points
- Vector3 dP = w + (sc * u) - (tc * v); // = S1(sc) - S2(tc)
+ // get the difference of the two closest points
+ Vector3 dP = w + (sc * u) - (tc * v); // = S1(sc) - S2(tc)
- return dP.length(); // return the closest distance
+ return dP.length(); // return the closest distance
}
- static inline bool ray_intersects_triangle( const Vector3& p_from, const Vector3& p_dir, const Vector3& p_v0,const Vector3& p_v1,const Vector3& p_v2,Vector3* r_res=0) {
- Vector3 e1=p_v1-p_v0;
- Vector3 e2=p_v2-p_v0;
+ static inline bool ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2, Vector3 *r_res = 0) {
+ Vector3 e1 = p_v1 - p_v0;
+ Vector3 e2 = p_v2 - p_v0;
Vector3 h = p_dir.cross(e2);
- real_t a =e1.dot(h);
- if (a>-CMP_EPSILON && a < CMP_EPSILON) // parallel test
+ real_t a = e1.dot(h);
+ if (a > -CMP_EPSILON && a < CMP_EPSILON) // parallel test
return false;
- real_t f=1.0/a;
+ real_t f = 1.0 / a;
- Vector3 s=p_from-p_v0;
+ Vector3 s = p_from - p_v0;
real_t u = f * s.dot(h);
- if ( u< 0.0 || u > 1.0)
+ if (u < 0.0 || u > 1.0)
return false;
- Vector3 q=s.cross(e1);
+ Vector3 q = s.cross(e1);
real_t v = f * p_dir.dot(q);
@@ -249,34 +240,34 @@ public:
// the intersection point is on the line
real_t t = f * e2.dot(q);
- if (t > 0.00001) {// ray intersection
+ if (t > 0.00001) { // ray intersection
if (r_res)
- *r_res=p_from+p_dir*t;
+ *r_res = p_from + p_dir * t;
return true;
} else // this means that there is a line intersection
// but not a ray intersection
return false;
}
- static inline bool segment_intersects_triangle( const Vector3& p_from, const Vector3& p_to, const Vector3& p_v0,const Vector3& p_v1,const Vector3& p_v2,Vector3* r_res=0) {
+ static inline bool segment_intersects_triangle(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2, Vector3 *r_res = 0) {
- Vector3 rel=p_to-p_from;
- Vector3 e1=p_v1-p_v0;
- Vector3 e2=p_v2-p_v0;
+ Vector3 rel = p_to - p_from;
+ Vector3 e1 = p_v1 - p_v0;
+ Vector3 e2 = p_v2 - p_v0;
Vector3 h = rel.cross(e2);
- real_t a =e1.dot(h);
- if (a>-CMP_EPSILON && a < CMP_EPSILON) // parallel test
+ real_t a = e1.dot(h);
+ if (a > -CMP_EPSILON && a < CMP_EPSILON) // parallel test
return false;
- real_t f=1.0/a;
+ real_t f = 1.0 / a;
- Vector3 s=p_from-p_v0;
+ Vector3 s = p_from - p_v0;
real_t u = f * s.dot(h);
- if ( u< 0.0 || u > 1.0)
+ if (u < 0.0 || u > 1.0)
return false;
- Vector3 q=s.cross(e1);
+ Vector3 q = s.cross(e1);
real_t v = f * rel.dot(q);
@@ -287,124 +278,122 @@ public:
// the intersection point is on the line
real_t t = f * e2.dot(q);
- if (t > CMP_EPSILON && t<=1.0) {// ray intersection
+ if (t > CMP_EPSILON && t <= 1.0) { // ray intersection
if (r_res)
- *r_res=p_from+rel*t;
+ *r_res = p_from + rel * t;
return true;
} else // this means that there is a line intersection
// but not a ray intersection
return false;
}
- static inline bool segment_intersects_sphere( const Vector3& p_from, const Vector3& p_to, const Vector3& p_sphere_pos,real_t p_sphere_radius,Vector3* r_res=0,Vector3 *r_norm=0) {
-
+ static inline bool segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 *r_res = 0, Vector3 *r_norm = 0) {
- Vector3 sphere_pos=p_sphere_pos-p_from;
- Vector3 rel=(p_to-p_from);
- real_t rel_l=rel.length();
- if (rel_l<CMP_EPSILON)
+ Vector3 sphere_pos = p_sphere_pos - p_from;
+ Vector3 rel = (p_to - p_from);
+ real_t rel_l = rel.length();
+ if (rel_l < CMP_EPSILON)
return false; // both points are the same
- Vector3 normal=rel/rel_l;
+ Vector3 normal = rel / rel_l;
- real_t sphere_d=normal.dot(sphere_pos);
+ real_t sphere_d = normal.dot(sphere_pos);
//Vector3 ray_closest=normal*sphere_d;
- real_t ray_distance=sphere_pos.distance_to(normal*sphere_d);
+ real_t ray_distance = sphere_pos.distance_to(normal * sphere_d);
- if (ray_distance>=p_sphere_radius)
+ if (ray_distance >= p_sphere_radius)
return false;
- real_t inters_d2=p_sphere_radius*p_sphere_radius - ray_distance*ray_distance;
- real_t inters_d=sphere_d;
+ real_t inters_d2 = p_sphere_radius * p_sphere_radius - ray_distance * ray_distance;
+ real_t inters_d = sphere_d;
- if (inters_d2>=CMP_EPSILON)
- inters_d-=Math::sqrt(inters_d2);
+ if (inters_d2 >= CMP_EPSILON)
+ inters_d -= Math::sqrt(inters_d2);
// check in segment
- if (inters_d<0 || inters_d>rel_l)
+ if (inters_d < 0 || inters_d > rel_l)
return false;
- Vector3 result=p_from+normal*inters_d;
+ Vector3 result = p_from + normal * inters_d;
if (r_res)
- *r_res=result;
+ *r_res = result;
if (r_norm)
- *r_norm=(result-p_sphere_pos).normalized();
+ *r_norm = (result - p_sphere_pos).normalized();
return true;
}
- static inline bool segment_intersects_cylinder( const Vector3& p_from, const Vector3& p_to, real_t p_height,real_t p_radius,Vector3* r_res=0,Vector3 *r_norm=0) {
+ static inline bool segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, real_t p_height, real_t p_radius, Vector3 *r_res = 0, Vector3 *r_norm = 0) {
- Vector3 rel=(p_to-p_from);
- real_t rel_l=rel.length();
- if (rel_l<CMP_EPSILON)
+ Vector3 rel = (p_to - p_from);
+ real_t rel_l = rel.length();
+ if (rel_l < CMP_EPSILON)
return false; // both points are the same
// first check if they are parallel
- Vector3 normal=(rel/rel_l);
- Vector3 crs = normal.cross(Vector3(0,0,1));
- real_t crs_l=crs.length();
+ Vector3 normal = (rel / rel_l);
+ Vector3 crs = normal.cross(Vector3(0, 0, 1));
+ real_t crs_l = crs.length();
Vector3 z_dir;
- if(crs_l<CMP_EPSILON) {
+ if (crs_l < CMP_EPSILON) {
//blahblah parallel
- z_dir=Vector3(1,0,0); //any x/y vector ok
+ z_dir = Vector3(1, 0, 0); //any x/y vector ok
} else {
- z_dir=crs/crs_l;
+ z_dir = crs / crs_l;
}
- real_t dist=z_dir.dot(p_from);
+ real_t dist = z_dir.dot(p_from);
- if (dist>=p_radius)
+ if (dist >= p_radius)
return false; // too far away
// convert to 2D
- real_t w2=p_radius*p_radius-dist*dist;
- if (w2<CMP_EPSILON)
+ real_t w2 = p_radius * p_radius - dist * dist;
+ if (w2 < CMP_EPSILON)
return false; //avoid numerical error
- Size2 size(Math::sqrt(w2),p_height*0.5);
-
- Vector3 x_dir=z_dir.cross(Vector3(0,0,1)).normalized();
+ Size2 size(Math::sqrt(w2), p_height * 0.5);
- Vector2 from2D(x_dir.dot(p_from),p_from.z);
- Vector2 to2D(x_dir.dot(p_to),p_to.z);
+ Vector3 x_dir = z_dir.cross(Vector3(0, 0, 1)).normalized();
- real_t min=0,max=1;
+ Vector2 from2D(x_dir.dot(p_from), p_from.z);
+ Vector2 to2D(x_dir.dot(p_to), p_to.z);
- int axis=-1;
+ real_t min = 0, max = 1;
- for(int i=0;i<2;i++) {
+ int axis = -1;
- real_t seg_from=from2D[i];
- real_t seg_to=to2D[i];
- real_t box_begin=-size[i];
- real_t box_end=size[i];
- real_t cmin,cmax;
+ for (int i = 0; i < 2; i++) {
+ real_t seg_from = from2D[i];
+ real_t seg_to = to2D[i];
+ real_t box_begin = -size[i];
+ real_t box_end = size[i];
+ real_t cmin, cmax;
if (seg_from < seg_to) {
if (seg_from > box_end || seg_to < box_begin)
return false;
- real_t length=seg_to-seg_from;
- cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
- cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
+ real_t length = seg_to - seg_from;
+ cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
+ cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
} else {
if (seg_to > box_end || seg_from < box_begin)
return false;
- real_t length=seg_to-seg_from;
- cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
- cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
+ real_t length = seg_to - seg_from;
+ cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
+ cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
}
if (cmin > min) {
min = cmin;
- axis=i;
+ axis = i;
}
if (cmax < max)
max = cmax;
@@ -412,254 +401,245 @@ public:
return false;
}
-
// convert to 3D again
- Vector3 result = p_from + (rel*min);
+ Vector3 result = p_from + (rel * min);
Vector3 res_normal = result;
- if (axis==0) {
- res_normal.z=0;
+ if (axis == 0) {
+ res_normal.z = 0;
} else {
- res_normal.x=0;
- res_normal.y=0;
+ res_normal.x = 0;
+ res_normal.y = 0;
}
-
res_normal.normalize();
if (r_res)
- *r_res=result;
+ *r_res = result;
if (r_norm)
- *r_norm=res_normal;
+ *r_norm = res_normal;
return true;
}
+ static bool segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Plane *p_planes, int p_plane_count, Vector3 *p_res, Vector3 *p_norm) {
- static bool segment_intersects_convex(const Vector3& p_from, const Vector3& p_to,const Plane* p_planes, int p_plane_count,Vector3 *p_res, Vector3 *p_norm) {
+ real_t min = -1e20, max = 1e20;
- real_t min=-1e20,max=1e20;
+ Vector3 rel = p_to - p_from;
+ real_t rel_l = rel.length();
- Vector3 rel=p_to-p_from;
- real_t rel_l=rel.length();
-
- if (rel_l<CMP_EPSILON)
+ if (rel_l < CMP_EPSILON)
return false;
- Vector3 dir=rel/rel_l;
+ Vector3 dir = rel / rel_l;
- int min_index=-1;
+ int min_index = -1;
- for (int i=0;i<p_plane_count;i++) {
+ for (int i = 0; i < p_plane_count; i++) {
- const Plane&p=p_planes[i];
+ const Plane &p = p_planes[i];
- real_t den=p.normal.dot( dir );
+ real_t den = p.normal.dot(dir);
//printf("den is %i\n",den);
- if (Math::abs(den)<=CMP_EPSILON)
+ if (Math::abs(den) <= CMP_EPSILON)
continue; // ignore parallel plane
+ real_t dist = -p.distance_to(p_from) / den;
- real_t dist=-p.distance_to(p_from ) / den;
-
- if (den>0) {
+ if (den > 0) {
//backwards facing plane
- if (dist<max)
- max=dist;
+ if (dist < max)
+ max = dist;
} else {
//front facing plane
- if (dist>min) {
- min=dist;
- min_index=i;
+ if (dist > min) {
+ min = dist;
+ min_index = i;
}
}
}
- if (max<=min || min<0 || min>rel_l || min_index==-1) // exit conditions
+ if (max <= min || min < 0 || min > rel_l || min_index == -1) // exit conditions
return false; // no intersection
if (p_res)
- *p_res=p_from+dir*min;
+ *p_res = p_from + dir * min;
if (p_norm)
- *p_norm=p_planes[min_index].normal;
+ *p_norm = p_planes[min_index].normal;
return true;
}
- static Vector3 get_closest_point_to_segment(const Vector3& p_point, const Vector3 *p_segment) {
+ static Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 *p_segment) {
- Vector3 p=p_point-p_segment[0];
- Vector3 n=p_segment[1]-p_segment[0];
- real_t l =n.length();
- if (l<1e-10)
+ Vector3 p = p_point - p_segment[0];
+ Vector3 n = p_segment[1] - p_segment[0];
+ real_t l = n.length();
+ if (l < 1e-10)
return p_segment[0]; // both points are the same, just give any
- n/=l;
+ n /= l;
- real_t d=n.dot(p);
+ real_t d = n.dot(p);
- if (d<=0.0)
+ if (d <= 0.0)
return p_segment[0]; // before first point
- else if (d>=l)
+ else if (d >= l)
return p_segment[1]; // after first point
else
- return p_segment[0]+n*d; // inside
+ return p_segment[0] + n * d; // inside
}
- static Vector3 get_closest_point_to_segment_uncapped(const Vector3& p_point, const Vector3 *p_segment) {
+ static Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 *p_segment) {
- Vector3 p=p_point-p_segment[0];
- Vector3 n=p_segment[1]-p_segment[0];
- real_t l =n.length();
- if (l<1e-10)
+ Vector3 p = p_point - p_segment[0];
+ Vector3 n = p_segment[1] - p_segment[0];
+ real_t l = n.length();
+ if (l < 1e-10)
return p_segment[0]; // both points are the same, just give any
- n/=l;
+ n /= l;
- real_t d=n.dot(p);
+ real_t d = n.dot(p);
- return p_segment[0]+n*d; // inside
+ return p_segment[0] + n * d; // inside
}
- static Vector2 get_closest_point_to_segment_2d(const Vector2& p_point, const Vector2 *p_segment) {
+ static Vector2 get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 *p_segment) {
- Vector2 p=p_point-p_segment[0];
- Vector2 n=p_segment[1]-p_segment[0];
- real_t l =n.length();
- if (l<1e-10)
+ Vector2 p = p_point - p_segment[0];
+ Vector2 n = p_segment[1] - p_segment[0];
+ real_t l = n.length();
+ if (l < 1e-10)
return p_segment[0]; // both points are the same, just give any
- n/=l;
+ n /= l;
- real_t d=n.dot(p);
+ real_t d = n.dot(p);
- if (d<=0.0)
+ if (d <= 0.0)
return p_segment[0]; // before first point
- else if (d>=l)
+ else if (d >= l)
return p_segment[1]; // after first point
else
- return p_segment[0]+n*d; // inside
+ return p_segment[0] + n * d; // inside
}
- static bool is_point_in_triangle(const Vector2& s, const Vector2& a, const Vector2& b, const Vector2& c)
- {
- int as_x = s.x-a.x;
- int as_y = s.y-a.y;
+ static bool is_point_in_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) {
+ int as_x = s.x - a.x;
+ int as_y = s.y - a.y;
- bool s_ab = (b.x-a.x)*as_y-(b.y-a.y)*as_x > 0;
+ bool s_ab = (b.x - a.x) * as_y - (b.y - a.y) * as_x > 0;
- if(((c.x-a.x)*as_y-(c.y-a.y)*as_x > 0) == s_ab) return false;
+ if (((c.x - a.x) * as_y - (c.y - a.y) * as_x > 0) == s_ab) return false;
- if(((c.x-b.x)*(s.y-b.y)-(c.y-b.y)*(s.x-b.x) > 0) != s_ab) return false;
+ if (((c.x - b.x) * (s.y - b.y) - (c.y - b.y) * (s.x - b.x) > 0) != s_ab) return false;
- return true;
+ return true;
}
- static Vector2 get_closest_point_to_segment_uncapped_2d(const Vector2& p_point, const Vector2 *p_segment) {
+ static Vector2 get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 *p_segment) {
- Vector2 p=p_point-p_segment[0];
- Vector2 n=p_segment[1]-p_segment[0];
- real_t l =n.length();
- if (l<1e-10)
+ Vector2 p = p_point - p_segment[0];
+ Vector2 n = p_segment[1] - p_segment[0];
+ real_t l = n.length();
+ if (l < 1e-10)
return p_segment[0]; // both points are the same, just give any
- n/=l;
+ n /= l;
- real_t d=n.dot(p);
+ real_t d = n.dot(p);
- return p_segment[0]+n*d; // inside
+ return p_segment[0] + n * d; // inside
}
- static bool segment_intersects_segment_2d(const Vector2& p_from_a,const Vector2& p_to_a,const Vector2& p_from_b,const Vector2& p_to_b,Vector2* r_result) {
+ static bool segment_intersects_segment_2d(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b, Vector2 *r_result) {
- Vector2 B = p_to_a-p_from_a;
- Vector2 C = p_from_b-p_from_a;
- Vector2 D = p_to_b-p_from_a;
+ Vector2 B = p_to_a - p_from_a;
+ Vector2 C = p_from_b - p_from_a;
+ Vector2 D = p_to_b - p_from_a;
real_t ABlen = B.dot(B);
- if (ABlen<=0)
+ if (ABlen <= 0)
return false;
- Vector2 Bn = B/ABlen;
- C = Vector2( C.x*Bn.x + C.y*Bn.y, C.y*Bn.x - C.x*Bn.y );
- D = Vector2( D.x*Bn.x + D.y*Bn.y, D.y*Bn.x - D.x*Bn.y );
+ Vector2 Bn = B / ABlen;
+ C = Vector2(C.x * Bn.x + C.y * Bn.y, C.y * Bn.x - C.x * Bn.y);
+ D = Vector2(D.x * Bn.x + D.y * Bn.y, D.y * Bn.x - D.x * Bn.y);
- if ((C.y<0 && D.y<0) || (C.y>=0 && D.y>=0))
+ if ((C.y < 0 && D.y < 0) || (C.y >= 0 && D.y >= 0))
return false;
- real_t ABpos=D.x+(C.x-D.x)*D.y/(D.y-C.y);
+ real_t ABpos = D.x + (C.x - D.x) * D.y / (D.y - C.y);
// Fail if segment C-D crosses line A-B outside of segment A-B.
- if (ABpos<0 || ABpos>1.0)
+ if (ABpos < 0 || ABpos > 1.0)
return false;
// (4) Apply the discovered position to line A-B in the original coordinate system.
if (r_result)
- *r_result=p_from_a+B*ABpos;
+ *r_result = p_from_a + B * ABpos;
return true;
}
+ static inline bool point_in_projected_triangle(const Vector3 &p_point, const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) {
- static inline bool point_in_projected_triangle(const Vector3& p_point,const Vector3& p_v1,const Vector3& p_v2,const Vector3& p_v3) {
-
-
- Vector3 face_n = (p_v1-p_v3).cross(p_v1-p_v2);
+ Vector3 face_n = (p_v1 - p_v3).cross(p_v1 - p_v2);
- Vector3 n1 = (p_point-p_v3).cross(p_point-p_v2);
+ Vector3 n1 = (p_point - p_v3).cross(p_point - p_v2);
- if (face_n.dot(n1)<0)
+ if (face_n.dot(n1) < 0)
return false;
- Vector3 n2 = (p_v1-p_v3).cross(p_v1-p_point);
+ Vector3 n2 = (p_v1 - p_v3).cross(p_v1 - p_point);
- if (face_n.dot(n2)<0)
+ if (face_n.dot(n2) < 0)
return false;
- Vector3 n3 = (p_v1-p_point).cross(p_v1-p_v2);
+ Vector3 n3 = (p_v1 - p_point).cross(p_v1 - p_v2);
- if (face_n.dot(n3)<0)
+ if (face_n.dot(n3) < 0)
return false;
return true;
-
}
- static inline bool triangle_sphere_intersection_test(const Vector3 *p_triangle,const Vector3& p_normal,const Vector3& p_sphere_pos, real_t p_sphere_radius,Vector3& r_triangle_contact,Vector3& r_sphere_contact) {
+ static inline bool triangle_sphere_intersection_test(const Vector3 *p_triangle, const Vector3 &p_normal, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 &r_triangle_contact, Vector3 &r_sphere_contact) {
- real_t d=p_normal.dot(p_sphere_pos)-p_normal.dot(p_triangle[0]);
+ real_t d = p_normal.dot(p_sphere_pos) - p_normal.dot(p_triangle[0]);
if (d > p_sphere_radius || d < -p_sphere_radius) // not touching the plane of the face, return
return false;
- Vector3 contact=p_sphere_pos - (p_normal*d);
+ Vector3 contact = p_sphere_pos - (p_normal * d);
/** 2nd) TEST INSIDE TRIANGLE **/
-
- if (Geometry::point_in_projected_triangle(contact,p_triangle[0],p_triangle[1],p_triangle[2])) {
- r_triangle_contact=contact;
- r_sphere_contact=p_sphere_pos-p_normal*p_sphere_radius;
+ if (Geometry::point_in_projected_triangle(contact, p_triangle[0], p_triangle[1], p_triangle[2])) {
+ r_triangle_contact = contact;
+ r_sphere_contact = p_sphere_pos - p_normal * p_sphere_radius;
//printf("solved inside triangle\n");
return true;
}
/** 3rd TEST INSIDE EDGE CYLINDERS **/
- const Vector3 verts[4]={p_triangle[0],p_triangle[1],p_triangle[2],p_triangle[0]}; // for() friendly
+ const Vector3 verts[4] = { p_triangle[0], p_triangle[1], p_triangle[2], p_triangle[0] }; // for() friendly
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
// check edge cylinder
- Vector3 n1=verts[i]-verts[i+1];
- Vector3 n2=p_sphere_pos-verts[i+1];
+ Vector3 n1 = verts[i] - verts[i + 1];
+ Vector3 n2 = p_sphere_pos - verts[i + 1];
///@TODO i could discard by range here to make the algorithm quicker? dunno..
// check point within cylinder radius
- Vector3 axis =n1.cross(n2).cross(n1);
+ Vector3 axis = n1.cross(n2).cross(n1);
axis.normalize(); // ugh
- real_t ad=axis.dot(n2);
+ real_t ad = axis.dot(n2);
- if (ABS(ad)>p_sphere_radius) {
+ if (ABS(ad) > p_sphere_radius) {
// no chance with this edge, too far away
continue;
}
@@ -669,34 +649,34 @@ public:
real_t sphere_at = n1.dot(n2);
- if (sphere_at>=0 && sphere_at<n1.dot(n1)) {
+ if (sphere_at >= 0 && sphere_at < n1.dot(n1)) {
- r_triangle_contact=p_sphere_pos-axis*(axis.dot(n2));
- r_sphere_contact=p_sphere_pos-axis*p_sphere_radius;
+ r_triangle_contact = p_sphere_pos - axis * (axis.dot(n2));
+ r_sphere_contact = p_sphere_pos - axis * p_sphere_radius;
// point inside here
//printf("solved inside edge\n");
return true;
}
- real_t r2=p_sphere_radius*p_sphere_radius;
+ real_t r2 = p_sphere_radius * p_sphere_radius;
- if (n2.length_squared()<r2) {
+ if (n2.length_squared() < r2) {
- Vector3 n=(p_sphere_pos-verts[i+1]).normalized();
+ Vector3 n = (p_sphere_pos - verts[i + 1]).normalized();
//r_triangle_contact=verts[i+1]+n*p_sphere_radius;p_sphere_pos+axis*(p_sphere_radius-axis.dot(n2));
- r_triangle_contact=verts[i+1];
- r_sphere_contact=p_sphere_pos-n*p_sphere_radius;
+ r_triangle_contact = verts[i + 1];
+ r_sphere_contact = p_sphere_pos - n * p_sphere_radius;
//printf("solved inside point segment 1\n");
return true;
}
- if (n2.distance_squared_to(n1)<r2) {
- Vector3 n=(p_sphere_pos-verts[i]).normalized();
+ if (n2.distance_squared_to(n1) < r2) {
+ Vector3 n = (p_sphere_pos - verts[i]).normalized();
//r_triangle_contact=verts[i]+n*p_sphere_radius;p_sphere_pos+axis*(p_sphere_radius-axis.dot(n2));
- r_triangle_contact=verts[i];
- r_sphere_contact=p_sphere_pos-n*p_sphere_radius;
+ r_triangle_contact = verts[i];
+ r_sphere_contact = p_sphere_pos - n * p_sphere_radius;
//printf("solved inside point segment 1\n");
return true;
}
@@ -707,56 +687,51 @@ public:
return false;
}
+ static real_t segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
+ Vector2 line_vec = p_to - p_from;
+ Vector2 vec_to_line = p_from - p_circle_pos;
- static real_t segment_intersects_circle(const Vector2& p_from, const Vector2& p_to, const Vector2& p_circle_pos, real_t p_circle_radius) {
-
- Vector2 line_vec = p_to - p_from;
- Vector2 vec_to_line = p_from - p_circle_pos;
-
- /* create a quadratic formula of the form ax^2 + bx + c = 0 */
- real_t a, b, c;
+ /* create a quadratic formula of the form ax^2 + bx + c = 0 */
+ real_t a, b, c;
- a = line_vec.dot(line_vec);
- b = 2 * vec_to_line.dot(line_vec);
- c = vec_to_line.dot(vec_to_line) - p_circle_radius * p_circle_radius;
+ a = line_vec.dot(line_vec);
+ b = 2 * vec_to_line.dot(line_vec);
+ c = vec_to_line.dot(vec_to_line) - p_circle_radius * p_circle_radius;
- /* solve for t */
- real_t sqrtterm = b*b - 4*a*c;
+ /* solve for t */
+ real_t sqrtterm = b * b - 4 * a * c;
- /* if the term we intend to square root is less than 0 then the answer won't be real, so it definitely won't be t in the range 0 to 1 */
- if(sqrtterm < 0) return -1;
+ /* if the term we intend to square root is less than 0 then the answer won't be real, so it definitely won't be t in the range 0 to 1 */
+ if (sqrtterm < 0) return -1;
- /* if we can assume that the line segment starts outside the circle (e.g. for continuous time collision detection) then the following can be skipped and we can just return the equivalent of res1 */
- sqrtterm = Math::sqrt(sqrtterm);
- real_t res1 = ( -b - sqrtterm ) / (2 * a);
- //real_t res2 = ( -b + sqrtterm ) / (2 * a);
-
- return (res1 >= 0 && res1 <= 1) ? res1 : -1;
+ /* if we can assume that the line segment starts outside the circle (e.g. for continuous time collision detection) then the following can be skipped and we can just return the equivalent of res1 */
+ sqrtterm = Math::sqrt(sqrtterm);
+ real_t res1 = (-b - sqrtterm) / (2 * a);
+ //real_t res2 = ( -b + sqrtterm ) / (2 * a);
+ return (res1 >= 0 && res1 <= 1) ? res1 : -1;
}
-
-
- static inline Vector<Vector3> clip_polygon(const Vector<Vector3>& polygon,const Plane& p_plane) {
+ static inline Vector<Vector3> clip_polygon(const Vector<Vector3> &polygon, const Plane &p_plane) {
enum LocationCache {
- LOC_INSIDE=1,
- LOC_BOUNDARY=0,
- LOC_OUTSIDE=-1
+ LOC_INSIDE = 1,
+ LOC_BOUNDARY = 0,
+ LOC_OUTSIDE = -1
};
- if (polygon.size()==0)
+ if (polygon.size() == 0)
return polygon;
- int *location_cache = (int*)alloca(sizeof(int)*polygon.size());
+ int *location_cache = (int *)alloca(sizeof(int) * polygon.size());
int inside_count = 0;
int outside_count = 0;
for (int a = 0; a < polygon.size(); a++) {
//real_t p_plane.d = (*this) * polygon[a];
real_t dist = p_plane.distance_to(polygon[a]);
- if (dist <-CMP_POINT_IN_PLANE_EPSILON) {
+ if (dist < -CMP_POINT_IN_PLANE_EPSILON) {
location_cache[a] = LOC_INSIDE;
inside_count++;
} else {
@@ -785,24 +760,24 @@ public:
int loc = location_cache[index];
if (loc == LOC_OUTSIDE) {
if (location_cache[previous] == LOC_INSIDE) {
- const Vector3& v1 = polygon[previous];
- const Vector3& v2 = polygon[index];
-
- Vector3 segment= v1 - v2;
- real_t den=p_plane.normal.dot( segment );
- real_t dist=p_plane.distance_to( v1 ) / den;
- dist=-dist;
- clipped.push_back( v1 + segment * dist );
+ const Vector3 &v1 = polygon[previous];
+ const Vector3 &v2 = polygon[index];
+
+ Vector3 segment = v1 - v2;
+ real_t den = p_plane.normal.dot(segment);
+ real_t dist = p_plane.distance_to(v1) / den;
+ dist = -dist;
+ clipped.push_back(v1 + segment * dist);
}
} else {
- const Vector3& v1 = polygon[index];
+ const Vector3 &v1 = polygon[index];
if ((loc == LOC_INSIDE) && (location_cache[previous] == LOC_OUTSIDE)) {
- const Vector3& v2 = polygon[previous];
- Vector3 segment= v1 - v2;
- real_t den=p_plane.normal.dot( segment );
- real_t dist=p_plane.distance_to( v1 ) / den;
- dist=-dist;
- clipped.push_back( v1 + segment * dist );
+ const Vector3 &v2 = polygon[previous];
+ Vector3 segment = v1 - v2;
+ real_t den = p_plane.normal.dot(segment);
+ real_t dist = p_plane.distance_to(v1) / den;
+ dist = -dist;
+ clipped.push_back(v1 + segment * dist);
}
clipped.push_back(v1);
@@ -814,30 +789,26 @@ public:
return clipped;
}
-
- static Vector<int> triangulate_polygon(const Vector<Vector2>& p_polygon) {
+ static Vector<int> triangulate_polygon(const Vector<Vector2> &p_polygon) {
Vector<int> triangles;
- if (!Triangulate::triangulate(p_polygon,triangles))
+ if (!Triangulate::triangulate(p_polygon, triangles))
return Vector<int>(); //fail
return triangles;
}
- static Vector< Vector<Vector2> > (*_decompose_func)(const Vector<Vector2>& p_polygon);
- static Vector< Vector<Vector2> > decompose_polygon(const Vector<Vector2>& p_polygon) {
+ static Vector<Vector<Vector2> > (*_decompose_func)(const Vector<Vector2> &p_polygon);
+ static Vector<Vector<Vector2> > decompose_polygon(const Vector<Vector2> &p_polygon) {
if (_decompose_func)
return _decompose_func(p_polygon);
- return Vector< Vector<Vector2> >();
-
+ return Vector<Vector<Vector2> >();
}
+ static PoolVector<PoolVector<Face3> > separate_objects(PoolVector<Face3> p_array);
- static PoolVector< PoolVector< Face3 > > separate_objects( PoolVector< Face3 > p_array );
-
- static PoolVector< Face3 > wrap_geometry( PoolVector< Face3 > p_array, real_t *p_error=NULL ); ///< create a "wrap" that encloses the given geometry
-
+ static PoolVector<Face3> wrap_geometry(PoolVector<Face3> p_array, real_t *p_error = NULL); ///< create a "wrap" that encloses the given geometry
struct MeshData {
@@ -850,94 +821,89 @@ public:
struct Edge {
- int a,b;
+ int a, b;
};
Vector<Edge> edges;
- Vector< Vector3 > vertices;
+ Vector<Vector3> vertices;
void optimize_vertices();
};
+ _FORCE_INLINE_ static int get_uv84_normal_bit(const Vector3 &p_vector) {
+ int lat = Math::fast_ftoi(Math::floor(Math::acos(p_vector.dot(Vector3(0, 1, 0))) * 4.0 / Math_PI + 0.5));
- _FORCE_INLINE_ static int get_uv84_normal_bit(const Vector3& p_vector) {
-
- int lat = Math::fast_ftoi(Math::floor(Math::acos(p_vector.dot(Vector3(0,1,0)))*4.0/Math_PI+0.5));
-
- if (lat==0) {
+ if (lat == 0) {
return 24;
- } else if (lat==4) {
+ } else if (lat == 4) {
return 25;
}
- int lon = Math::fast_ftoi(Math::floor( (Math_PI+Math::atan2(p_vector.x,p_vector.z))*8.0/(Math_PI*2.0) + 0.5))%8;
+ int lon = Math::fast_ftoi(Math::floor((Math_PI + Math::atan2(p_vector.x, p_vector.z)) * 8.0 / (Math_PI * 2.0) + 0.5)) % 8;
- return lon+(lat-1)*8;
+ return lon + (lat - 1) * 8;
}
_FORCE_INLINE_ static int get_uv84_normal_bit_neighbors(int p_idx) {
- if (p_idx==24) {
- return 1|2|4|8;
- } else if (p_idx==25) {
- return (1<<23)|(1<<22)|(1<<21)|(1<<20);
+ if (p_idx == 24) {
+ return 1 | 2 | 4 | 8;
+ } else if (p_idx == 25) {
+ return (1 << 23) | (1 << 22) | (1 << 21) | (1 << 20);
} else {
int ret = 0;
- if ((p_idx%8) == 0)
- ret|=(1<<(p_idx+7));
+ if ((p_idx % 8) == 0)
+ ret |= (1 << (p_idx + 7));
else
- ret|=(1<<(p_idx-1));
- if ((p_idx%8) == 7)
- ret|=(1<<(p_idx-7));
+ ret |= (1 << (p_idx - 1));
+ if ((p_idx % 8) == 7)
+ ret |= (1 << (p_idx - 7));
else
- ret|=(1<<(p_idx+1));
+ ret |= (1 << (p_idx + 1));
- int mask = ret|(1<<p_idx);
- if (p_idx<8)
- ret|=24;
+ int mask = ret | (1 << p_idx);
+ if (p_idx < 8)
+ ret |= 24;
else
- ret|=mask>>8;
+ ret |= mask >> 8;
- if (p_idx>=16)
- ret|=25;
+ if (p_idx >= 16)
+ ret |= 25;
else
- ret|=mask<<8;
+ ret |= mask << 8;
return ret;
}
-
}
-
- static real_t vec2_cross(const Point2 &O, const Point2 &A, const Point2 &B)
- {
+ static real_t vec2_cross(const Point2 &O, const Point2 &A, const Point2 &B) {
return (real_t)(A.x - O.x) * (B.y - O.y) - (real_t)(A.y - O.y) * (B.x - O.x);
}
// Returns a list of points on the convex hull in counter-clockwise order.
// Note: the last point in the returned list is the same as the first one.
- static Vector<Point2> convex_hull_2d(Vector<Point2> P)
- {
+ static Vector<Point2> convex_hull_2d(Vector<Point2> P) {
int n = P.size(), k = 0;
Vector<Point2> H;
- H.resize(2*n);
+ H.resize(2 * n);
// Sort points lexicographically
P.sort();
-
// Build lower hull
for (int i = 0; i < n; ++i) {
- while (k >= 2 && vec2_cross(H[k-2], H[k-1], P[i]) <= 0) k--;
+ while (k >= 2 && vec2_cross(H[k - 2], H[k - 1], P[i]) <= 0)
+ k--;
H[k++] = P[i];
}
// Build upper hull
- for (int i = n-2, t = k+1; i >= 0; i--) {
- while (k >= t && vec2_cross(H[k-2], H[k-1], P[i]) <= 0) k--;
+ for (int i = n - 2, t = k + 1; i >= 0; i--) {
+ while (k >= t && vec2_cross(H[k - 2], H[k - 1], P[i]) <= 0)
+ k--;
H[k++] = P[i];
}
@@ -946,16 +912,12 @@ public:
}
static MeshData build_convex_mesh(const PoolVector<Plane> &p_planes);
- static PoolVector<Plane> build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis=Vector3::AXIS_Z);
- static PoolVector<Plane> build_box_planes(const Vector3& p_extents);
- static PoolVector<Plane> build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis=Vector3::AXIS_Z);
- static PoolVector<Plane> build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis=Vector3::AXIS_Z);
-
- static void make_atlas(const Vector<Size2i>& p_rects,Vector<Point2i>& r_result, Size2i& r_size);
-
+ static PoolVector<Plane> build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis = Vector3::AXIS_Z);
+ static PoolVector<Plane> build_box_planes(const Vector3 &p_extents);
+ static PoolVector<Plane> build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z);
+ static PoolVector<Plane> build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis = Vector3::AXIS_Z);
+ static void make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size);
};
-
-
#endif
diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp
index 76eeece688..021b1fbf55 100644
--- a/core/math/math_2d.cpp
+++ b/core/math/math_2d.cpp
@@ -28,91 +28,91 @@
/*************************************************************************/
#include "math_2d.h"
-
real_t Vector2::angle() const {
- return Math::atan2(y,x);
+ return Math::atan2(y, x);
}
real_t Vector2::length() const {
- return Math::sqrt( x*x + y*y );
+ return Math::sqrt(x * x + y * y);
}
real_t Vector2::length_squared() const {
- return x*x + y*y;
+ return x * x + y * y;
}
void Vector2::normalize() {
- real_t l = x*x + y*y;
- if (l!=0) {
+ real_t l = x * x + y * y;
+ if (l != 0) {
- l=Math::sqrt(l);
- x/=l;
- y/=l;
+ l = Math::sqrt(l);
+ x /= l;
+ y /= l;
}
}
Vector2 Vector2::normalized() const {
- Vector2 v=*this;
+ Vector2 v = *this;
v.normalize();
return v;
}
-real_t Vector2::distance_to(const Vector2& p_vector2) const {
+real_t Vector2::distance_to(const Vector2 &p_vector2) const {
- return Math::sqrt( (x-p_vector2.x)*(x-p_vector2.x) + (y-p_vector2.y)*(y-p_vector2.y));
+ return Math::sqrt((x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y));
}
-real_t Vector2::distance_squared_to(const Vector2& p_vector2) const {
+real_t Vector2::distance_squared_to(const Vector2 &p_vector2) const {
- return (x-p_vector2.x)*(x-p_vector2.x) + (y-p_vector2.y)*(y-p_vector2.y);
+ return (x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y);
}
-real_t Vector2::angle_to(const Vector2& p_vector2) const {
+real_t Vector2::angle_to(const Vector2 &p_vector2) const {
- return Math::atan2( cross(p_vector2), dot(p_vector2) );
+ return Math::atan2(cross(p_vector2), dot(p_vector2));
}
-real_t Vector2::angle_to_point(const Vector2& p_vector2) const {
+real_t Vector2::angle_to_point(const Vector2 &p_vector2) const {
- return Math::atan2( y - p_vector2.y, x-p_vector2.x );
+ return Math::atan2(y - p_vector2.y, x - p_vector2.x);
}
-real_t Vector2::dot(const Vector2& p_other) const {
+real_t Vector2::dot(const Vector2 &p_other) const {
- return x*p_other.x + y*p_other.y;
+ return x * p_other.x + y * p_other.y;
}
-real_t Vector2::cross(const Vector2& p_other) const {
+real_t Vector2::cross(const Vector2 &p_other) const {
- return x*p_other.y - y*p_other.x;
+ return x * p_other.y - y * p_other.x;
}
Vector2 Vector2::cross(real_t p_other) const {
- return Vector2(p_other*y,-p_other*x);
+ return Vector2(p_other * y, -p_other * x);
}
+Vector2 Vector2::operator+(const Vector2 &p_v) const {
-Vector2 Vector2::operator+(const Vector2& p_v) const {
-
- return Vector2(x+p_v.x,y+p_v.y);
+ return Vector2(x + p_v.x, y + p_v.y);
}
-void Vector2::operator+=(const Vector2& p_v) {
+void Vector2::operator+=(const Vector2 &p_v) {
- x+=p_v.x; y+=p_v.y;
+ x += p_v.x;
+ y += p_v.y;
}
-Vector2 Vector2::operator-(const Vector2& p_v) const {
+Vector2 Vector2::operator-(const Vector2 &p_v) const {
- return Vector2(x-p_v.x,y-p_v.y);
+ return Vector2(x - p_v.x, y - p_v.y);
}
-void Vector2::operator-=(const Vector2& p_v) {
+void Vector2::operator-=(const Vector2 &p_v) {
- x-=p_v.x; y-=p_v.y;
+ x -= p_v.x;
+ y -= p_v.y;
}
Vector2 Vector2::operator*(const Vector2 &p_v1) const {
@@ -126,7 +126,8 @@ Vector2 Vector2::operator*(const real_t &rvalue) const {
};
void Vector2::operator*=(const real_t &rvalue) {
- x *= rvalue; y *= rvalue;
+ x *= rvalue;
+ y *= rvalue;
};
Vector2 Vector2::operator/(const Vector2 &p_v1) const {
@@ -141,64 +142,64 @@ Vector2 Vector2::operator/(const real_t &rvalue) const {
void Vector2::operator/=(const real_t &rvalue) {
- x /= rvalue; y /= rvalue;
+ x /= rvalue;
+ y /= rvalue;
};
Vector2 Vector2::operator-() const {
- return Vector2(-x,-y);
+ return Vector2(-x, -y);
}
-bool Vector2::operator==(const Vector2& p_vec2) const {
+bool Vector2::operator==(const Vector2 &p_vec2) const {
- return x==p_vec2.x && y==p_vec2.y;
+ return x == p_vec2.x && y == p_vec2.y;
}
-bool Vector2::operator!=(const Vector2& p_vec2) const {
+bool Vector2::operator!=(const Vector2 &p_vec2) const {
- return x!=p_vec2.x || y!=p_vec2.y;
+ return x != p_vec2.x || y != p_vec2.y;
}
Vector2 Vector2::floor() const {
- return Vector2( Math::floor(x), Math::floor(y) );
+ return Vector2(Math::floor(x), Math::floor(y));
}
Vector2 Vector2::rotated(real_t p_by) const {
Vector2 v;
- v.set_rotation(angle()+p_by);
- v*=length();
+ v.set_rotation(angle() + p_by);
+ v *= length();
return v;
}
-Vector2 Vector2::project(const Vector2& p_vec) const {
+Vector2 Vector2::project(const Vector2 &p_vec) const {
- Vector2 v1=p_vec;
- Vector2 v2=*this;
- return v2 * ( v1.dot(v2) / v2.dot(v2));
+ Vector2 v1 = p_vec;
+ Vector2 v2 = *this;
+ return v2 * (v1.dot(v2) / v2.dot(v2));
}
-Vector2 Vector2::snapped(const Vector2& p_by) const {
+Vector2 Vector2::snapped(const Vector2 &p_by) const {
return Vector2(
- Math::stepify(x,p_by.x),
- Math::stepify(y,p_by.y)
- );
+ Math::stepify(x, p_by.x),
+ Math::stepify(y, p_by.y));
}
Vector2 Vector2::clamped(real_t p_len) const {
real_t l = length();
Vector2 v = *this;
- if (l>0 && p_len<l) {
+ if (l > 0 && p_len < l) {
- v/=l;
- v*=p_len;
+ v /= l;
+ v *= p_len;
}
return v;
}
-Vector2 Vector2::cubic_interpolate_soft(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,real_t p_t) const {
+Vector2 Vector2::cubic_interpolate_soft(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const {
#if 0
k[0] = ((*this) (vi[0] + 1, vi[1], vi[2])) - ((*this) (vi[0],
vi[1],vi[2])); //fk = a0
@@ -225,27 +226,25 @@ Vector2 Vector2::cubic_interpolate_soft(const Vector2& p_b,const Vector2& p_pre_
return Vector2();
}
-Vector2 Vector2::cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,real_t p_t) const {
-
-
+Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const {
- Vector2 p0=p_pre_a;
- Vector2 p1=*this;
- Vector2 p2=p_b;
- Vector2 p3=p_post_b;
+ Vector2 p0 = p_pre_a;
+ Vector2 p1 = *this;
+ Vector2 p2 = p_b;
+ Vector2 p3 = p_post_b;
real_t t = p_t;
real_t t2 = t * t;
real_t t3 = t2 * t;
Vector2 out;
- out = 0.5 * ( ( p1 * 2.0) +
- ( -p0 + p2 ) * t +
- ( 2.0 * p0 - 5.0 * p1 + 4 * p2 - p3 ) * t2 +
- ( -p0 + 3.0 * p1 - 3.0 * p2 + p3 ) * t3 );
+ out = 0.5 * ((p1 * 2.0) +
+ (-p0 + p2) * t +
+ (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
+ (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
return out;
-/*
+ /*
real_t mu = p_t;
real_t mu2 = mu*mu;
@@ -273,57 +272,54 @@ Vector2 Vector2::cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, co
(a * p_a.y) + (b *p_b.y) + (c * p_pre_a.y) + (d * p_post_b.y)
);
*/
-
}
-Vector2 Vector2::slide(const Vector2& p_vec) const {
+Vector2 Vector2::slide(const Vector2 &p_vec) const {
return p_vec - *this * this->dot(p_vec);
}
-Vector2 Vector2::reflect(const Vector2& p_vec) const {
+Vector2 Vector2::reflect(const Vector2 &p_vec) const {
return p_vec - *this * this->dot(p_vec) * 2.0;
-
}
+bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
-bool Rect2::intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos,Point2* r_normal) const {
+ real_t min = 0, max = 1;
+ int axis = 0;
+ real_t sign = 0;
- real_t min=0,max=1;
- int axis=0;
- real_t sign=0;
-
- for(int i=0;i<2;i++) {
- real_t seg_from=p_from[i];
- real_t seg_to=p_to[i];
- real_t box_begin=pos[i];
- real_t box_end=box_begin+size[i];
- real_t cmin,cmax;
+ for (int i = 0; i < 2; i++) {
+ real_t seg_from = p_from[i];
+ real_t seg_to = p_to[i];
+ real_t box_begin = pos[i];
+ real_t box_end = box_begin + size[i];
+ real_t cmin, cmax;
real_t csign;
if (seg_from < seg_to) {
if (seg_from > box_end || seg_to < box_begin)
return false;
- real_t length=seg_to-seg_from;
- cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
- cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
- csign=-1.0;
+ real_t length = seg_to - seg_from;
+ cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
+ cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
+ csign = -1.0;
} else {
if (seg_to > box_end || seg_from < box_begin)
return false;
- real_t length=seg_to-seg_from;
- cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
- cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
- csign=1.0;
+ real_t length = seg_to - seg_from;
+ cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
+ cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
+ csign = 1.0;
}
if (cmin > min) {
min = cmin;
- axis=i;
- sign=csign;
+ axis = i;
+ sign = csign;
}
if (cmax < max)
max = cmax;
@@ -331,38 +327,39 @@ bool Rect2::intersects_segment(const Point2& p_from, const Point2& p_to, Point2*
return false;
}
-
- Vector2 rel=p_to-p_from;
+ Vector2 rel = p_to - p_from;
if (r_normal) {
Vector2 normal;
- normal[axis]=sign;
- *r_normal=normal;
+ normal[axis] = sign;
+ *r_normal = normal;
}
if (r_pos)
- *r_pos=p_from+rel*min;
+ *r_pos = p_from + rel * min;
return true;
}
/* Point2i */
-Point2i Point2i::operator+(const Point2i& p_v) const {
+Point2i Point2i::operator+(const Point2i &p_v) const {
- return Point2i(x+p_v.x,y+p_v.y);
+ return Point2i(x + p_v.x, y + p_v.y);
}
-void Point2i::operator+=(const Point2i& p_v) {
+void Point2i::operator+=(const Point2i &p_v) {
- x+=p_v.x; y+=p_v.y;
+ x += p_v.x;
+ y += p_v.y;
}
-Point2i Point2i::operator-(const Point2i& p_v) const {
+Point2i Point2i::operator-(const Point2i &p_v) const {
- return Point2i(x-p_v.x,y-p_v.y);
+ return Point2i(x - p_v.x, y - p_v.y);
}
-void Point2i::operator-=(const Point2i& p_v) {
+void Point2i::operator-=(const Point2i &p_v) {
- x-=p_v.x; y-=p_v.y;
+ x -= p_v.x;
+ y -= p_v.y;
}
Point2i Point2i::operator*(const Point2i &p_v1) const {
@@ -376,7 +373,8 @@ Point2i Point2i::operator*(const int &rvalue) const {
};
void Point2i::operator*=(const int &rvalue) {
- x *= rvalue; y *= rvalue;
+ x *= rvalue;
+ y *= rvalue;
};
Point2i Point2i::operator/(const Point2i &p_v1) const {
@@ -391,225 +389,215 @@ Point2i Point2i::operator/(const int &rvalue) const {
void Point2i::operator/=(const int &rvalue) {
- x /= rvalue; y /= rvalue;
+ x /= rvalue;
+ y /= rvalue;
};
Point2i Point2i::operator-() const {
- return Point2i(-x,-y);
+ return Point2i(-x, -y);
}
-bool Point2i::operator==(const Point2i& p_vec2) const {
+bool Point2i::operator==(const Point2i &p_vec2) const {
- return x==p_vec2.x && y==p_vec2.y;
+ return x == p_vec2.x && y == p_vec2.y;
}
-bool Point2i::operator!=(const Point2i& p_vec2) const {
+bool Point2i::operator!=(const Point2i &p_vec2) const {
- return x!=p_vec2.x || y!=p_vec2.y;
+ return x != p_vec2.x || y != p_vec2.y;
}
void Transform2D::invert() {
// FIXME: this function assumes the basis is a rotation matrix, with no scaling.
// Transform2D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
- SWAP(elements[0][1],elements[1][0]);
+ SWAP(elements[0][1], elements[1][0]);
elements[2] = basis_xform(-elements[2]);
}
Transform2D Transform2D::inverse() const {
- Transform2D inv=*this;
+ Transform2D inv = *this;
inv.invert();
return inv;
-
}
void Transform2D::affine_invert() {
real_t det = basis_determinant();
- ERR_FAIL_COND(det==0);
+ ERR_FAIL_COND(det == 0);
real_t idet = 1.0 / det;
- SWAP( elements[0][0],elements[1][1] );
- elements[0]*=Vector2(idet,-idet);
- elements[1]*=Vector2(-idet,idet);
+ SWAP(elements[0][0], elements[1][1]);
+ elements[0] *= Vector2(idet, -idet);
+ elements[1] *= Vector2(-idet, idet);
elements[2] = basis_xform(-elements[2]);
-
}
Transform2D Transform2D::affine_inverse() const {
- Transform2D inv=*this;
+ Transform2D inv = *this;
inv.affine_invert();
return inv;
}
void Transform2D::rotate(real_t p_phi) {
- *this = Transform2D(p_phi,Vector2()) * (*this);
+ *this = Transform2D(p_phi, Vector2()) * (*this);
}
real_t Transform2D::get_rotation() const {
real_t det = basis_determinant();
Transform2D m = orthonormalized();
if (det < 0) {
- m.scale_basis(Size2(-1,-1));
+ m.scale_basis(Size2(-1, -1));
}
- return Math::atan2(m[0].y,m[0].x);
+ return Math::atan2(m[0].y, m[0].x);
}
void Transform2D::set_rotation(real_t p_rot) {
real_t cr = Math::cos(p_rot);
real_t sr = Math::sin(p_rot);
- elements[0][0]=cr;
- elements[0][1]=sr;
- elements[1][0]=-sr;
- elements[1][1]=cr;
+ elements[0][0] = cr;
+ elements[0][1] = sr;
+ elements[1][0] = -sr;
+ elements[1][1] = cr;
}
-Transform2D::Transform2D(real_t p_rot, const Vector2& p_pos) {
+Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) {
real_t cr = Math::cos(p_rot);
real_t sr = Math::sin(p_rot);
- elements[0][0]=cr;
- elements[0][1]=sr;
- elements[1][0]=-sr;
- elements[1][1]=cr;
- elements[2]=p_pos;
+ elements[0][0] = cr;
+ elements[0][1] = sr;
+ elements[1][0] = -sr;
+ elements[1][1] = cr;
+ elements[2] = p_pos;
}
Size2 Transform2D::get_scale() const {
real_t det_sign = basis_determinant() > 0 ? 1 : -1;
- return det_sign * Size2( elements[0].length(), elements[1].length() );
+ return det_sign * Size2(elements[0].length(), elements[1].length());
}
-void Transform2D::scale(const Size2& p_scale) {
+void Transform2D::scale(const Size2 &p_scale) {
scale_basis(p_scale);
- elements[2]*=p_scale;
+ elements[2] *= p_scale;
}
-void Transform2D::scale_basis(const Size2& p_scale) {
-
- elements[0][0]*=p_scale.x;
- elements[0][1]*=p_scale.y;
- elements[1][0]*=p_scale.x;
- elements[1][1]*=p_scale.y;
+void Transform2D::scale_basis(const Size2 &p_scale) {
+ elements[0][0] *= p_scale.x;
+ elements[0][1] *= p_scale.y;
+ elements[1][0] *= p_scale.x;
+ elements[1][1] *= p_scale.y;
}
-void Transform2D::translate( real_t p_tx, real_t p_ty) {
+void Transform2D::translate(real_t p_tx, real_t p_ty) {
- translate(Vector2(p_tx,p_ty));
+ translate(Vector2(p_tx, p_ty));
}
-void Transform2D::translate( const Vector2& p_translation ) {
+void Transform2D::translate(const Vector2 &p_translation) {
- elements[2]+=basis_xform(p_translation);
+ elements[2] += basis_xform(p_translation);
}
void Transform2D::orthonormalize() {
// Gram-Schmidt Process
- Vector2 x=elements[0];
- Vector2 y=elements[1];
+ Vector2 x = elements[0];
+ Vector2 y = elements[1];
x.normalize();
- y = (y-x*(x.dot(y)));
+ y = (y - x * (x.dot(y)));
y.normalize();
- elements[0]=x;
- elements[1]=y;
+ elements[0] = x;
+ elements[1] = y;
}
Transform2D Transform2D::orthonormalized() const {
- Transform2D on=*this;
+ Transform2D on = *this;
on.orthonormalize();
return on;
-
}
-bool Transform2D::operator==(const Transform2D& p_transform) const {
+bool Transform2D::operator==(const Transform2D &p_transform) const {
- for(int i=0;i<3;i++) {
- if (elements[i]!=p_transform.elements[i])
+ for (int i = 0; i < 3; i++) {
+ if (elements[i] != p_transform.elements[i])
return false;
}
return true;
}
-bool Transform2D::operator!=(const Transform2D& p_transform) const {
+bool Transform2D::operator!=(const Transform2D &p_transform) const {
- for(int i=0;i<3;i++) {
- if (elements[i]!=p_transform.elements[i])
+ for (int i = 0; i < 3; i++) {
+ if (elements[i] != p_transform.elements[i])
return true;
}
return false;
-
}
-void Transform2D::operator*=(const Transform2D& p_transform) {
+void Transform2D::operator*=(const Transform2D &p_transform) {
elements[2] = xform(p_transform.elements[2]);
- real_t x0,x1,y0,y1;
+ real_t x0, x1, y0, y1;
x0 = tdotx(p_transform.elements[0]);
x1 = tdoty(p_transform.elements[0]);
y0 = tdotx(p_transform.elements[1]);
y1 = tdoty(p_transform.elements[1]);
- elements[0][0]=x0;
- elements[0][1]=x1;
- elements[1][0]=y0;
- elements[1][1]=y1;
+ elements[0][0] = x0;
+ elements[0][1] = x1;
+ elements[1][0] = y0;
+ elements[1][1] = y1;
}
-
-Transform2D Transform2D::operator*(const Transform2D& p_transform) const {
+Transform2D Transform2D::operator*(const Transform2D &p_transform) const {
Transform2D t = *this;
- t*=p_transform;
+ t *= p_transform;
return t;
-
}
-Transform2D Transform2D::scaled(const Size2& p_scale) const {
+Transform2D Transform2D::scaled(const Size2 &p_scale) const {
- Transform2D copy=*this;
+ Transform2D copy = *this;
copy.scale(p_scale);
return copy;
-
}
-Transform2D Transform2D::basis_scaled(const Size2& p_scale) const {
+Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const {
- Transform2D copy=*this;
+ Transform2D copy = *this;
copy.scale_basis(p_scale);
return copy;
-
}
Transform2D Transform2D::untranslated() const {
- Transform2D copy=*this;
- copy.elements[2]=Vector2();
+ Transform2D copy = *this;
+ copy.elements[2] = Vector2();
return copy;
}
-Transform2D Transform2D::translated(const Vector2& p_offset) const {
+Transform2D Transform2D::translated(const Vector2 &p_offset) const {
- Transform2D copy=*this;
+ Transform2D copy = *this;
copy.translate(p_offset);
return copy;
-
}
Transform2D Transform2D::rotated(real_t p_phi) const {
- Transform2D copy=*this;
+ Transform2D copy = *this;
copy.rotate(p_phi);
return copy;
-
}
real_t Transform2D::basis_determinant() const {
@@ -617,7 +605,7 @@ real_t Transform2D::basis_determinant() const {
return elements[0].x * elements[1].y - elements[0].y * elements[1].x;
}
-Transform2D Transform2D::interpolate_with(const Transform2D& p_transform, real_t p_c) const {
+Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t p_c) const {
//extract parameters
Vector2 p1 = get_origin();
@@ -642,9 +630,9 @@ Transform2D Transform2D::interpolate_with(const Transform2D& p_transform, real_t
if (dot > 0.9995) {
v = Vector2::linear_interpolate(v1, v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
} else {
- real_t angle = p_c*Math::acos(dot);
- Vector2 v3 = (v2 - v1*dot).normalized();
- v = v1*Math::cos(angle) + v3*Math::sin(angle);
+ real_t angle = p_c * Math::acos(dot);
+ Vector2 v3 = (v2 - v1 * dot).normalized();
+ v = v1 * Math::cos(angle) + v3 * Math::sin(angle);
}
//construct matrix
@@ -655,5 +643,5 @@ Transform2D Transform2D::interpolate_with(const Transform2D& p_transform, real_t
Transform2D::operator String() const {
- return String(String()+elements[0]+", "+elements[1]+", "+elements[2]);
+ return String(String() + elements[0] + ", " + elements[1] + ", " + elements[2]);
}
diff --git a/core/math/math_2d.h b/core/math/math_2d.h
index a24c4266ee..af6437d7f1 100644
--- a/core/math/math_2d.h
+++ b/core/math/math_2d.h
@@ -73,12 +73,11 @@ struct Vector2 {
real_t height;
};
-
- _FORCE_INLINE_ real_t& operator[](int p_idx) {
- return p_idx?y:x;
+ _FORCE_INLINE_ real_t &operator[](int p_idx) {
+ return p_idx ? y : x;
}
- _FORCE_INLINE_ const real_t& operator[](int p_idx) const {
- return p_idx?y:x;
+ _FORCE_INLINE_ const real_t &operator[](int p_idx) const {
+ return p_idx ? y : x;
}
void normalize();
@@ -87,32 +86,32 @@ struct Vector2 {
real_t length() const;
real_t length_squared() const;
- real_t distance_to(const Vector2& p_vector2) const;
- real_t distance_squared_to(const Vector2& p_vector2) const;
- real_t angle_to(const Vector2& p_vector2) const;
- real_t angle_to_point(const Vector2& p_vector2) const;
+ real_t distance_to(const Vector2 &p_vector2) const;
+ real_t distance_squared_to(const Vector2 &p_vector2) const;
+ real_t angle_to(const Vector2 &p_vector2) const;
+ real_t angle_to_point(const Vector2 &p_vector2) const;
- real_t dot(const Vector2& p_other) const;
- real_t cross(const Vector2& p_other) const;
+ real_t dot(const Vector2 &p_other) const;
+ real_t cross(const Vector2 &p_other) const;
Vector2 cross(real_t p_other) const;
- Vector2 project(const Vector2& p_vec) const;
+ Vector2 project(const Vector2 &p_vec) const;
- Vector2 plane_project(real_t p_d, const Vector2& p_vec) const;
+ Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
Vector2 clamped(real_t p_len) const;
- _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2& p_a, const Vector2& p_b,real_t p_t);
- _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2& p_b,real_t p_t) const;
- Vector2 cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,real_t p_t) const;
- Vector2 cubic_interpolate_soft(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,real_t p_t) const;
+ _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t);
+ _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_b, real_t p_t) const;
+ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
+ Vector2 cubic_interpolate_soft(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
- Vector2 slide(const Vector2& p_vec) const;
- Vector2 reflect(const Vector2& p_vec) const;
+ Vector2 slide(const Vector2 &p_vec) const;
+ Vector2 reflect(const Vector2 &p_vec) const;
- Vector2 operator+(const Vector2& p_v) const;
- void operator+=(const Vector2& p_v);
- Vector2 operator-(const Vector2& p_v) const;
- void operator-=(const Vector2& p_v);
+ Vector2 operator+(const Vector2 &p_v) const;
+ void operator+=(const Vector2 &p_v);
+ Vector2 operator-(const Vector2 &p_v) const;
+ void operator-=(const Vector2 &p_v);
Vector2 operator*(const Vector2 &p_v1) const;
Vector2 operator*(const real_t &rvalue) const;
@@ -127,70 +126,73 @@ struct Vector2 {
Vector2 operator-() const;
- bool operator==(const Vector2& p_vec2) const;
- bool operator!=(const Vector2& p_vec2) const;
+ bool operator==(const Vector2 &p_vec2) const;
+ bool operator!=(const Vector2 &p_vec2) const;
- bool operator<(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<p_vec2.y):(x<p_vec2.x); }
- bool operator<=(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<=p_vec2.y):(x<=p_vec2.x); }
+ bool operator<(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
+ bool operator<=(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y <= p_vec2.y) : (x <= p_vec2.x); }
real_t angle() const;
void set_rotation(real_t p_radians) {
- x=Math::cos(p_radians);
- y=Math::sin(p_radians);
+ x = Math::cos(p_radians);
+ y = Math::sin(p_radians);
}
_FORCE_INLINE_ Vector2 abs() const {
- return Vector2( Math::abs(x), Math::abs(y) );
+ return Vector2(Math::abs(x), Math::abs(y));
}
Vector2 rotated(real_t p_by) const;
Vector2 tangent() const {
- return Vector2(y,-x);
+ return Vector2(y, -x);
}
Vector2 floor() const;
- Vector2 snapped(const Vector2& p_by) const;
- real_t aspect() const { return width/height; }
-
+ Vector2 snapped(const Vector2 &p_by) const;
+ real_t aspect() const { return width / height; }
- operator String() const { return String::num(x)+", "+String::num(y); }
+ operator String() const { return String::num(x) + ", " + String::num(y); }
- _FORCE_INLINE_ Vector2(real_t p_x,real_t p_y) { x=p_x; y=p_y; }
- _FORCE_INLINE_ Vector2() { x=0; y=0; }
+ _FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
+ x = p_x;
+ y = p_y;
+ }
+ _FORCE_INLINE_ Vector2() {
+ x = 0;
+ y = 0;
+ }
};
-_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2& p_vec) const {
+_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
- return p_vec - *this * ( dot(p_vec) -p_d);
+ return p_vec - *this * (dot(p_vec) - p_d);
}
+_FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
-_FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2& p_vec) {
-
- return p_vec*p_scalar;
+ return p_vec * p_scalar;
}
-Vector2 Vector2::linear_interpolate(const Vector2& p_b,real_t p_t) const {
+Vector2 Vector2::linear_interpolate(const Vector2 &p_b, real_t p_t) const {
- Vector2 res=*this;
+ Vector2 res = *this;
- res.x+= (p_t * (p_b.x-x));
- res.y+= (p_t * (p_b.y-y));
+ res.x += (p_t * (p_b.x - x));
+ res.y += (p_t * (p_b.y - y));
return res;
-
}
-Vector2 Vector2::linear_interpolate(const Vector2& p_a, const Vector2& p_b,real_t p_t) {
+Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) {
- Vector2 res=p_a;
+ Vector2 res = p_a;
- res.x+= (p_t * (p_b.x-p_a.x));
- res.y+= (p_t * (p_b.y-p_a.y));
+ res.x += (p_t * (p_b.x - p_a.x));
+ res.y += (p_t * (p_b.y - p_a.y));
return res;
}
@@ -200,170 +202,170 @@ typedef Vector2 Point2;
struct Transform2D;
-
struct Rect2 {
Point2 pos;
Size2 size;
- const Vector2& get_pos() const { return pos; }
- void set_pos(const Vector2& p_pos) { pos=p_pos; }
- const Vector2& get_size() const { return size; }
- void set_size(const Vector2& p_size) { size=p_size; }
+ const Vector2 &get_pos() const { return pos; }
+ void set_pos(const Vector2 &p_pos) { pos = p_pos; }
+ const Vector2 &get_size() const { return size; }
+ void set_size(const Vector2 &p_size) { size = p_size; }
- real_t get_area() const { return size.width*size.height; }
+ real_t get_area() const { return size.width * size.height; }
- inline bool intersects(const Rect2& p_rect) const {
- if ( pos.x >= (p_rect.pos.x + p_rect.size.width) )
+ inline bool intersects(const Rect2 &p_rect) const {
+ if (pos.x >= (p_rect.pos.x + p_rect.size.width))
return false;
- if ( (pos.x+size.width) <= p_rect.pos.x )
+ if ((pos.x + size.width) <= p_rect.pos.x)
return false;
- if ( pos.y >= (p_rect.pos.y + p_rect.size.height) )
+ if (pos.y >= (p_rect.pos.y + p_rect.size.height))
return false;
- if ( (pos.y+size.height) <= p_rect.pos.y )
+ if ((pos.y + size.height) <= p_rect.pos.y)
return false;
return true;
}
- inline real_t distance_to(const Vector2& p_point) const {
+ inline real_t distance_to(const Vector2 &p_point) const {
real_t dist = 1e20;
if (p_point.x < pos.x) {
- dist=MIN(dist,pos.x-p_point.x);
+ dist = MIN(dist, pos.x - p_point.x);
}
if (p_point.y < pos.y) {
- dist=MIN(dist,pos.y-p_point.y);
+ dist = MIN(dist, pos.y - p_point.y);
}
- if (p_point.x >= (pos.x+size.x) ) {
- dist=MIN(p_point.x-(pos.x+size.x),dist);
+ if (p_point.x >= (pos.x + size.x)) {
+ dist = MIN(p_point.x - (pos.x + size.x), dist);
}
- if (p_point.y >= (pos.y+size.y) ) {
- dist=MIN(p_point.y-(pos.y+size.y),dist);
+ if (p_point.y >= (pos.y + size.y)) {
+ dist = MIN(p_point.y - (pos.y + size.y), dist);
}
- if (dist==1e20)
+ if (dist == 1e20)
return 0;
else
return dist;
}
- _FORCE_INLINE_ bool intersects_transformed(const Transform2D& p_xform, const Rect2& p_rect) const;
-
- bool intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos=NULL, Point2* r_normal=NULL) const;
+ _FORCE_INLINE_ bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
- inline bool encloses(const Rect2& p_rect) const {
+ bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = NULL, Point2 *r_normal = NULL) const;
- return (p_rect.pos.x>=pos.x) && (p_rect.pos.y>=pos.y) &&
- ((p_rect.pos.x+p_rect.size.x)<(pos.x+size.x)) &&
- ((p_rect.pos.y+p_rect.size.y)<(pos.y+size.y));
+ inline bool encloses(const Rect2 &p_rect) const {
+ return (p_rect.pos.x >= pos.x) && (p_rect.pos.y >= pos.y) &&
+ ((p_rect.pos.x + p_rect.size.x) < (pos.x + size.x)) &&
+ ((p_rect.pos.y + p_rect.size.y) < (pos.y + size.y));
}
inline bool has_no_area() const {
- return (size.x<=0 || size.y<=0);
-
+ return (size.x <= 0 || size.y <= 0);
}
- inline Rect2 clip(const Rect2& p_rect) const { /// return a clipped rect
+ inline Rect2 clip(const Rect2 &p_rect) const { /// return a clipped rect
- Rect2 new_rect=p_rect;
+ Rect2 new_rect = p_rect;
- if (!intersects( new_rect ))
+ if (!intersects(new_rect))
return Rect2();
- new_rect.pos.x = MAX( p_rect.pos.x , pos.x );
- new_rect.pos.y = MAX( p_rect.pos.y , pos.y );
+ new_rect.pos.x = MAX(p_rect.pos.x, pos.x);
+ new_rect.pos.y = MAX(p_rect.pos.y, pos.y);
- Point2 p_rect_end=p_rect.pos+p_rect.size;
- Point2 end=pos+size;
+ Point2 p_rect_end = p_rect.pos + p_rect.size;
+ Point2 end = pos + size;
- new_rect.size.x=MIN(p_rect_end.x,end.x) - new_rect.pos.x;
- new_rect.size.y=MIN(p_rect_end.y,end.y) - new_rect.pos.y;
+ new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.pos.x;
+ new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.pos.y;
return new_rect;
}
- inline Rect2 merge(const Rect2& p_rect) const { ///< return a merged rect
+ inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
Rect2 new_rect;
- new_rect.pos.x=MIN( p_rect.pos.x , pos.x );
- new_rect.pos.y=MIN( p_rect.pos.y , pos.y );
-
+ new_rect.pos.x = MIN(p_rect.pos.x, pos.x);
+ new_rect.pos.y = MIN(p_rect.pos.y, pos.y);
- new_rect.size.x = MAX( p_rect.pos.x+p_rect.size.x , pos.x+size.x );
- new_rect.size.y = MAX( p_rect.pos.y+p_rect.size.y , pos.y+size.y );
+ new_rect.size.x = MAX(p_rect.pos.x + p_rect.size.x, pos.x + size.x);
+ new_rect.size.y = MAX(p_rect.pos.y + p_rect.size.y, pos.y + size.y);
new_rect.size = new_rect.size - new_rect.pos; //make relative again
return new_rect;
};
- inline bool has_point(const Point2& p_point) const {
+ inline bool has_point(const Point2 &p_point) const {
if (p_point.x < pos.x)
return false;
if (p_point.y < pos.y)
return false;
- if (p_point.x >= (pos.x+size.x) )
+ if (p_point.x >= (pos.x + size.x))
return false;
- if (p_point.y >= (pos.y+size.y) )
+ if (p_point.y >= (pos.y + size.y))
return false;
return true;
}
- inline bool no_area() const { return (size.width<=0 || size.height<=0 ); }
+ inline bool no_area() const { return (size.width <= 0 || size.height <= 0); }
- bool operator==(const Rect2& p_rect) const { return pos==p_rect.pos && size==p_rect.size; }
- bool operator!=(const Rect2& p_rect) const { return pos!=p_rect.pos || size!=p_rect.size; }
+ bool operator==(const Rect2 &p_rect) const { return pos == p_rect.pos && size == p_rect.size; }
+ bool operator!=(const Rect2 &p_rect) const { return pos != p_rect.pos || size != p_rect.size; }
inline Rect2 grow(real_t p_by) const {
- Rect2 g=*this;
- g.pos.x-=p_by;
- g.pos.y-=p_by;
- g.size.width+=p_by*2;
- g.size.height+=p_by*2;
+ Rect2 g = *this;
+ g.pos.x -= p_by;
+ g.pos.y -= p_by;
+ g.size.width += p_by * 2;
+ g.size.height += p_by * 2;
return g;
}
- inline Rect2 expand(const Vector2& p_vector) const {
+ inline Rect2 expand(const Vector2 &p_vector) const {
Rect2 r = *this;
r.expand_to(p_vector);
return r;
}
- inline void expand_to(const Vector2& p_vector) { //in place function for speed
+ inline void expand_to(const Vector2 &p_vector) { //in place function for speed
- Vector2 begin=pos;
- Vector2 end=pos+size;
+ Vector2 begin = pos;
+ Vector2 end = pos + size;
- if (p_vector.x<begin.x)
- begin.x=p_vector.x;
- if (p_vector.y<begin.y)
- begin.y=p_vector.y;
+ if (p_vector.x < begin.x)
+ begin.x = p_vector.x;
+ if (p_vector.y < begin.y)
+ begin.y = p_vector.y;
- if (p_vector.x>end.x)
- end.x=p_vector.x;
- if (p_vector.y>end.y)
- end.y=p_vector.y;
+ if (p_vector.x > end.x)
+ end.x = p_vector.x;
+ if (p_vector.y > end.y)
+ end.y = p_vector.y;
- pos=begin;
- size=end-begin;
+ pos = begin;
+ size = end - begin;
}
-
- operator String() const { return String(pos)+", "+String(size); }
+ operator String() const { return String(pos) + ", " + String(size); }
Rect2() {}
- Rect2( real_t p_x, real_t p_y, real_t p_width, real_t p_height) { pos=Point2(p_x,p_y); size=Size2( p_width, p_height ); }
- Rect2( const Point2& p_pos, const Size2& p_size ) { pos=p_pos; size=p_size; }
+ Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) {
+ pos = Point2(p_x, p_y);
+ size = Size2(p_width, p_height);
+ }
+ Rect2(const Point2 &p_pos, const Size2 &p_size) {
+ pos = p_pos;
+ size = p_size;
+ }
};
-
/* INTEGER STUFF */
struct Point2i {
@@ -377,18 +379,17 @@ struct Point2i {
int height;
};
-
- _FORCE_INLINE_ int& operator[](int p_idx) {
- return p_idx?y:x;
+ _FORCE_INLINE_ int &operator[](int p_idx) {
+ return p_idx ? y : x;
}
- _FORCE_INLINE_ const int& operator[](int p_idx) const {
- return p_idx?y:x;
+ _FORCE_INLINE_ const int &operator[](int p_idx) const {
+ return p_idx ? y : x;
}
- Point2i operator+(const Point2i& p_v) const;
- void operator+=(const Point2i& p_v);
- Point2i operator-(const Point2i& p_v) const;
- void operator-=(const Point2i& p_v);
+ Point2i operator+(const Point2i &p_v) const;
+ void operator+=(const Point2i &p_v);
+ Point2i operator-(const Point2i &p_v) const;
+ void operator-=(const Point2i &p_v);
Point2i operator*(const Point2i &p_v1) const;
Point2i operator*(const int &rvalue) const;
@@ -401,20 +402,29 @@ struct Point2i {
void operator/=(const int &rvalue);
Point2i operator-() const;
- bool operator<(const Point2i& p_vec2) const { return (x==p_vec2.x)?(y<p_vec2.y):(x<p_vec2.x); }
- bool operator>(const Point2i& p_vec2) const { return (x==p_vec2.x)?(y>p_vec2.y):(x>p_vec2.x); }
+ bool operator<(const Point2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
+ bool operator>(const Point2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
- bool operator==(const Point2i& p_vec2) const;
- bool operator!=(const Point2i& p_vec2) const;
+ bool operator==(const Point2i &p_vec2) const;
+ bool operator!=(const Point2i &p_vec2) const;
- real_t get_aspect() const { return width/(real_t)height; }
+ real_t get_aspect() const { return width / (real_t)height; }
- operator String() const { return String::num(x)+", "+String::num(y); }
+ operator String() const { return String::num(x) + ", " + String::num(y); }
- operator Vector2() const { return Vector2(x,y); }
- inline Point2i(const Vector2& p_vec2) { x=(int)p_vec2.x; y=(int)p_vec2.y; }
- inline Point2i(int p_x,int p_y) { x=p_x; y=p_y; }
- inline Point2i() { x=0; y=0; }
+ operator Vector2() const { return Vector2(x, y); }
+ inline Point2i(const Vector2 &p_vec2) {
+ x = (int)p_vec2.x;
+ y = (int)p_vec2.y;
+ }
+ inline Point2i(int p_x, int p_y) {
+ x = p_x;
+ y = p_y;
+ }
+ inline Point2i() {
+ x = 0;
+ y = 0;
+ }
};
typedef Point2i Size2i;
@@ -424,133 +434,136 @@ struct Rect2i {
Point2i pos;
Size2i size;
- const Point2i& get_pos() const { return pos; }
- void set_pos(const Point2i& p_pos) { pos=p_pos; }
- const Point2i& get_size() const { return size; }
- void set_size(const Point2i& p_size) { size=p_size; }
+ const Point2i &get_pos() const { return pos; }
+ void set_pos(const Point2i &p_pos) { pos = p_pos; }
+ const Point2i &get_size() const { return size; }
+ void set_size(const Point2i &p_size) { size = p_size; }
- int get_area() const { return size.width*size.height; }
+ int get_area() const { return size.width * size.height; }
- inline bool intersects(const Rect2i& p_rect) const {
- if ( pos.x > (p_rect.pos.x + p_rect.size.width) )
+ inline bool intersects(const Rect2i &p_rect) const {
+ if (pos.x > (p_rect.pos.x + p_rect.size.width))
return false;
- if ( (pos.x+size.width) < p_rect.pos.x )
+ if ((pos.x + size.width) < p_rect.pos.x)
return false;
- if ( pos.y > (p_rect.pos.y + p_rect.size.height) )
+ if (pos.y > (p_rect.pos.y + p_rect.size.height))
return false;
- if ( (pos.y+size.height) < p_rect.pos.y )
+ if ((pos.y + size.height) < p_rect.pos.y)
return false;
return true;
}
- inline bool encloses(const Rect2i& p_rect) const {
-
- return (p_rect.pos.x>=pos.x) && (p_rect.pos.y>=pos.y) &&
- ((p_rect.pos.x+p_rect.size.x)<(pos.x+size.x)) &&
- ((p_rect.pos.y+p_rect.size.y)<(pos.y+size.y));
+ inline bool encloses(const Rect2i &p_rect) const {
+ return (p_rect.pos.x >= pos.x) && (p_rect.pos.y >= pos.y) &&
+ ((p_rect.pos.x + p_rect.size.x) < (pos.x + size.x)) &&
+ ((p_rect.pos.y + p_rect.size.y) < (pos.y + size.y));
}
inline bool has_no_area() const {
- return (size.x<=0 || size.y<=0);
-
+ return (size.x <= 0 || size.y <= 0);
}
- inline Rect2i clip(const Rect2i& p_rect) const { /// return a clipped rect
+ inline Rect2i clip(const Rect2i &p_rect) const { /// return a clipped rect
- Rect2i new_rect=p_rect;
+ Rect2i new_rect = p_rect;
- if (!intersects( new_rect ))
+ if (!intersects(new_rect))
return Rect2i();
- new_rect.pos.x = MAX( p_rect.pos.x , pos.x );
- new_rect.pos.y = MAX( p_rect.pos.y , pos.y );
+ new_rect.pos.x = MAX(p_rect.pos.x, pos.x);
+ new_rect.pos.y = MAX(p_rect.pos.y, pos.y);
- Point2 p_rect_end=p_rect.pos+p_rect.size;
- Point2 end=pos+size;
+ Point2 p_rect_end = p_rect.pos + p_rect.size;
+ Point2 end = pos + size;
- new_rect.size.x=(int)(MIN(p_rect_end.x,end.x) - new_rect.pos.x);
- new_rect.size.y=(int)(MIN(p_rect_end.y,end.y) - new_rect.pos.y);
+ new_rect.size.x = (int)(MIN(p_rect_end.x, end.x) - new_rect.pos.x);
+ new_rect.size.y = (int)(MIN(p_rect_end.y, end.y) - new_rect.pos.y);
return new_rect;
}
- inline Rect2i merge(const Rect2i& p_rect) const { ///< return a merged rect
+ inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
Rect2i new_rect;
- new_rect.pos.x=MIN( p_rect.pos.x , pos.x );
- new_rect.pos.y=MIN( p_rect.pos.y , pos.y );
-
+ new_rect.pos.x = MIN(p_rect.pos.x, pos.x);
+ new_rect.pos.y = MIN(p_rect.pos.y, pos.y);
- new_rect.size.x = MAX( p_rect.pos.x+p_rect.size.x , pos.x+size.x );
- new_rect.size.y = MAX( p_rect.pos.y+p_rect.size.y , pos.y+size.y );
+ new_rect.size.x = MAX(p_rect.pos.x + p_rect.size.x, pos.x + size.x);
+ new_rect.size.y = MAX(p_rect.pos.y + p_rect.size.y, pos.y + size.y);
new_rect.size = new_rect.size - new_rect.pos; //make relative again
return new_rect;
};
- bool has_point(const Point2& p_point) const {
+ bool has_point(const Point2 &p_point) const {
if (p_point.x < pos.x)
return false;
if (p_point.y < pos.y)
return false;
- if (p_point.x >= (pos.x+size.x) )
+ if (p_point.x >= (pos.x + size.x))
return false;
- if (p_point.y >= (pos.y+size.y) )
+ if (p_point.y >= (pos.y + size.y))
return false;
return true;
}
- bool no_area() { return (size.width<=0 || size.height<=0 ); }
+ bool no_area() { return (size.width <= 0 || size.height <= 0); }
- bool operator==(const Rect2i& p_rect) const { return pos==p_rect.pos && size==p_rect.size; }
- bool operator!=(const Rect2i& p_rect) const { return pos!=p_rect.pos || size!=p_rect.size; }
+ bool operator==(const Rect2i &p_rect) const { return pos == p_rect.pos && size == p_rect.size; }
+ bool operator!=(const Rect2i &p_rect) const { return pos != p_rect.pos || size != p_rect.size; }
Rect2i grow(int p_by) const {
- Rect2i g=*this;
- g.pos.x-=p_by;
- g.pos.y-=p_by;
- g.size.width+=p_by*2;
- g.size.height+=p_by*2;
+ Rect2i g = *this;
+ g.pos.x -= p_by;
+ g.pos.y -= p_by;
+ g.size.width += p_by * 2;
+ g.size.height += p_by * 2;
return g;
}
- inline void expand_to(const Point2i& p_vector) {
+ inline void expand_to(const Point2i &p_vector) {
- Point2i begin=pos;
- Point2i end=pos+size;
+ Point2i begin = pos;
+ Point2i end = pos + size;
- if (p_vector.x<begin.x)
- begin.x=p_vector.x;
- if (p_vector.y<begin.y)
- begin.y=p_vector.y;
+ if (p_vector.x < begin.x)
+ begin.x = p_vector.x;
+ if (p_vector.y < begin.y)
+ begin.y = p_vector.y;
- if (p_vector.x>end.x)
- end.x=p_vector.x;
- if (p_vector.y>end.y)
- end.y=p_vector.y;
+ if (p_vector.x > end.x)
+ end.x = p_vector.x;
+ if (p_vector.y > end.y)
+ end.y = p_vector.y;
- pos=begin;
- size=end-begin;
+ pos = begin;
+ size = end - begin;
}
+ operator String() const { return String(pos) + ", " + String(size); }
- operator String() const { return String(pos)+", "+String(size); }
-
- operator Rect2() const { return Rect2(pos,size); }
- Rect2i(const Rect2& p_r2) { pos=p_r2.pos; size=p_r2.size; }
+ operator Rect2() const { return Rect2(pos, size); }
+ Rect2i(const Rect2 &p_r2) {
+ pos = p_r2.pos;
+ size = p_r2.size;
+ }
Rect2i() {}
- Rect2i( int p_x, int p_y, int p_width, int p_height) { pos=Point2(p_x,p_y); size=Size2( p_width, p_height ); }
- Rect2i( const Point2& p_pos, const Size2& p_size ) { pos=p_pos; size=p_size; }
+ Rect2i(int p_x, int p_y, int p_width, int p_height) {
+ pos = Point2(p_x, p_y);
+ size = Size2(p_width, p_height);
+ }
+ Rect2i(const Point2 &p_pos, const Size2 &p_size) {
+ pos = p_pos;
+ size = p_size;
+ }
};
-
-
struct Transform2D {
// Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
// M = (elements[0][0] elements[1][0])
@@ -558,21 +571,27 @@ struct Transform2D {
// This is such that the columns, which can be interpreted as basis vectors of the coordinate system "painted" on the object, can be accessed as elements[i].
// Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to elements[1][0] here.
// This requires additional care when working with explicit indices.
- // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
+ // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
// Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
// and angle is measure from +X to +Y in a clockwise-fashion.
Vector2 elements[3];
- _FORCE_INLINE_ real_t tdotx(const Vector2& v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
- _FORCE_INLINE_ real_t tdoty(const Vector2& v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
+ _FORCE_INLINE_ real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
+ _FORCE_INLINE_ real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
- const Vector2& operator[](int p_idx) const { return elements[p_idx]; }
- Vector2& operator[](int p_idx) { return elements[p_idx]; }
+ const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
+ Vector2 &operator[](int p_idx) { return elements[p_idx]; }
- _FORCE_INLINE_ Vector2 get_axis(int p_axis) const { ERR_FAIL_INDEX_V(p_axis,3,Vector2()); return elements[p_axis]; }
- _FORCE_INLINE_ void set_axis(int p_axis,const Vector2& p_vec) { ERR_FAIL_INDEX(p_axis,3); elements[p_axis]=p_vec; }
+ _FORCE_INLINE_ Vector2 get_axis(int p_axis) const {
+ ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
+ return elements[p_axis];
+ }
+ _FORCE_INLINE_ void set_axis(int p_axis, const Vector2 &p_vec) {
+ ERR_FAIL_INDEX(p_axis, 3);
+ elements[p_axis] = p_vec;
+ }
void invert();
Transform2D inverse() const;
@@ -582,24 +601,24 @@ struct Transform2D {
void set_rotation(real_t p_phi);
real_t get_rotation() const;
- _FORCE_INLINE_ void set_rotation_and_scale(real_t p_phi,const Size2& p_scale);
+ _FORCE_INLINE_ void set_rotation_and_scale(real_t p_phi, const Size2 &p_scale);
void rotate(real_t p_phi);
- void scale(const Size2& p_scale);
- void scale_basis(const Size2& p_scale);
- void translate( real_t p_tx, real_t p_ty);
- void translate( const Vector2& p_translation );
+ void scale(const Size2 &p_scale);
+ void scale_basis(const Size2 &p_scale);
+ void translate(real_t p_tx, real_t p_ty);
+ void translate(const Vector2 &p_translation);
real_t basis_determinant() const;
Size2 get_scale() const;
- _FORCE_INLINE_ const Vector2& get_origin() const { return elements[2]; }
- _FORCE_INLINE_ void set_origin(const Vector2& p_origin) { elements[2]=p_origin; }
+ _FORCE_INLINE_ const Vector2 &get_origin() const { return elements[2]; }
+ _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
- Transform2D scaled(const Size2& p_scale) const;
- Transform2D basis_scaled(const Size2& p_scale) const;
- Transform2D translated(const Vector2& p_offset) const;
+ Transform2D scaled(const Size2 &p_scale) const;
+ Transform2D basis_scaled(const Size2 &p_scale) const;
+ Transform2D translated(const Vector2 &p_offset) const;
Transform2D rotated(real_t p_phi) const;
Transform2D untranslated() const;
@@ -607,20 +626,20 @@ struct Transform2D {
void orthonormalize();
Transform2D orthonormalized() const;
- bool operator==(const Transform2D& p_transform) const;
- bool operator!=(const Transform2D& p_transform) const;
+ bool operator==(const Transform2D &p_transform) const;
+ bool operator!=(const Transform2D &p_transform) const;
- void operator*=(const Transform2D& p_transform);
- Transform2D operator*(const Transform2D& p_transform) const;
+ void operator*=(const Transform2D &p_transform);
+ Transform2D operator*(const Transform2D &p_transform) const;
- Transform2D interpolate_with(const Transform2D& p_transform, real_t p_c) const;
+ Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
- _FORCE_INLINE_ Vector2 basis_xform(const Vector2& p_vec) const;
- _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2& p_vec) const;
- _FORCE_INLINE_ Vector2 xform(const Vector2& p_vec) const;
- _FORCE_INLINE_ Vector2 xform_inv(const Vector2& p_vec) const;
- _FORCE_INLINE_ Rect2 xform(const Rect2& p_vec) const;
- _FORCE_INLINE_ Rect2 xform_inv(const Rect2& p_vec) const;
+ _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
+ _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
+ _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
+ _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
+ _FORCE_INLINE_ Rect2 xform(const Rect2 &p_vec) const;
+ _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_vec) const;
operator String() const;
@@ -634,232 +653,226 @@ struct Transform2D {
elements[2][1] = oy;
}
- Transform2D(real_t p_rot, const Vector2& p_pos);
- Transform2D() { elements[0][0]=1.0; elements[1][1]=1.0; }
+ Transform2D(real_t p_rot, const Vector2 &p_pos);
+ Transform2D() {
+ elements[0][0] = 1.0;
+ elements[1][1] = 1.0;
+ }
};
-bool Rect2::intersects_transformed(const Transform2D& p_xform, const Rect2& p_rect) const {
+bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
//SAT intersection between local and transformed rect2
- Vector2 xf_points[4]={
+ Vector2 xf_points[4] = {
p_xform.xform(p_rect.pos),
- p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y)),
- p_xform.xform(Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y)),
- p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y)),
+ p_xform.xform(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y)),
+ p_xform.xform(Vector2(p_rect.pos.x, p_rect.pos.y + p_rect.size.y)),
+ p_xform.xform(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y + p_rect.size.y)),
};
real_t low_limit;
//base rect2 first (faster)
- if (xf_points[0].y>pos.y)
+ if (xf_points[0].y > pos.y)
goto next1;
- if (xf_points[1].y>pos.y)
+ if (xf_points[1].y > pos.y)
goto next1;
- if (xf_points[2].y>pos.y)
+ if (xf_points[2].y > pos.y)
goto next1;
- if (xf_points[3].y>pos.y)
+ if (xf_points[3].y > pos.y)
goto next1;
return false;
- next1:
+next1:
- low_limit=pos.y+size.y;
+ low_limit = pos.y + size.y;
- if (xf_points[0].y<low_limit)
+ if (xf_points[0].y < low_limit)
goto next2;
- if (xf_points[1].y<low_limit)
+ if (xf_points[1].y < low_limit)
goto next2;
- if (xf_points[2].y<low_limit)
+ if (xf_points[2].y < low_limit)
goto next2;
- if (xf_points[3].y<low_limit)
+ if (xf_points[3].y < low_limit)
goto next2;
return false;
- next2:
+next2:
- if (xf_points[0].x>pos.x)
+ if (xf_points[0].x > pos.x)
goto next3;
- if (xf_points[1].x>pos.x)
+ if (xf_points[1].x > pos.x)
goto next3;
- if (xf_points[2].x>pos.x)
+ if (xf_points[2].x > pos.x)
goto next3;
- if (xf_points[3].x>pos.x)
+ if (xf_points[3].x > pos.x)
goto next3;
return false;
- next3:
+next3:
- low_limit=pos.x+size.x;
+ low_limit = pos.x + size.x;
- if (xf_points[0].x<low_limit)
+ if (xf_points[0].x < low_limit)
goto next4;
- if (xf_points[1].x<low_limit)
+ if (xf_points[1].x < low_limit)
goto next4;
- if (xf_points[2].x<low_limit)
+ if (xf_points[2].x < low_limit)
goto next4;
- if (xf_points[3].x<low_limit)
+ if (xf_points[3].x < low_limit)
goto next4;
return false;
- next4:
+next4:
- Vector2 xf_points2[4]={
+ Vector2 xf_points2[4] = {
pos,
- Vector2(pos.x+size.x,pos.y),
- Vector2(pos.x,pos.y+size.y),
- Vector2(pos.x+size.x,pos.y+size.y),
+ Vector2(pos.x + size.x, pos.y),
+ Vector2(pos.x, pos.y + size.y),
+ Vector2(pos.x + size.x, pos.y + size.y),
};
- real_t maxa=p_xform.elements[0].dot(xf_points2[0]);
- real_t mina=maxa;
+ real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
+ real_t mina = maxa;
real_t dp = p_xform.elements[0].dot(xf_points2[1]);
- maxa=MAX(dp,maxa);
- mina=MIN(dp,mina);
+ maxa = MAX(dp, maxa);
+ mina = MIN(dp, mina);
dp = p_xform.elements[0].dot(xf_points2[2]);
- maxa=MAX(dp,maxa);
- mina=MIN(dp,mina);
+ maxa = MAX(dp, maxa);
+ mina = MIN(dp, mina);
dp = p_xform.elements[0].dot(xf_points2[3]);
- maxa=MAX(dp,maxa);
- mina=MIN(dp,mina);
+ maxa = MAX(dp, maxa);
+ mina = MIN(dp, mina);
- real_t maxb=p_xform.elements[0].dot(xf_points[0]);
- real_t minb=maxb;
+ real_t maxb = p_xform.elements[0].dot(xf_points[0]);
+ real_t minb = maxb;
dp = p_xform.elements[0].dot(xf_points[1]);
- maxb=MAX(dp,maxb);
- minb=MIN(dp,minb);
+ maxb = MAX(dp, maxb);
+ minb = MIN(dp, minb);
dp = p_xform.elements[0].dot(xf_points[2]);
- maxb=MAX(dp,maxb);
- minb=MIN(dp,minb);
+ maxb = MAX(dp, maxb);
+ minb = MIN(dp, minb);
dp = p_xform.elements[0].dot(xf_points[3]);
- maxb=MAX(dp,maxb);
- minb=MIN(dp,minb);
-
+ maxb = MAX(dp, maxb);
+ minb = MIN(dp, minb);
- if ( mina > maxb )
+ if (mina > maxb)
return false;
- if ( minb > maxa )
+ if (minb > maxa)
return false;
- maxa=p_xform.elements[1].dot(xf_points2[0]);
- mina=maxa;
+ maxa = p_xform.elements[1].dot(xf_points2[0]);
+ mina = maxa;
dp = p_xform.elements[1].dot(xf_points2[1]);
- maxa=MAX(dp,maxa);
- mina=MIN(dp,mina);
+ maxa = MAX(dp, maxa);
+ mina = MIN(dp, mina);
dp = p_xform.elements[1].dot(xf_points2[2]);
- maxa=MAX(dp,maxa);
- mina=MIN(dp,mina);
+ maxa = MAX(dp, maxa);
+ mina = MIN(dp, mina);
dp = p_xform.elements[1].dot(xf_points2[3]);
- maxa=MAX(dp,maxa);
- mina=MIN(dp,mina);
+ maxa = MAX(dp, maxa);
+ mina = MIN(dp, mina);
- maxb=p_xform.elements[1].dot(xf_points[0]);
- minb=maxb;
+ maxb = p_xform.elements[1].dot(xf_points[0]);
+ minb = maxb;
dp = p_xform.elements[1].dot(xf_points[1]);
- maxb=MAX(dp,maxb);
- minb=MIN(dp,minb);
+ maxb = MAX(dp, maxb);
+ minb = MIN(dp, minb);
dp = p_xform.elements[1].dot(xf_points[2]);
- maxb=MAX(dp,maxb);
- minb=MIN(dp,minb);
+ maxb = MAX(dp, maxb);
+ minb = MIN(dp, minb);
dp = p_xform.elements[1].dot(xf_points[3]);
- maxb=MAX(dp,maxb);
- minb=MIN(dp,minb);
+ maxb = MAX(dp, maxb);
+ minb = MIN(dp, minb);
-
- if ( mina > maxb )
+ if (mina > maxb)
return false;
- if ( minb > maxa )
+ if (minb > maxa)
return false;
-
return true;
-
}
-Vector2 Transform2D::basis_xform(const Vector2& v) const {
+Vector2 Transform2D::basis_xform(const Vector2 &v) const {
return Vector2(
- tdotx(v),
- tdoty(v)
- );
+ tdotx(v),
+ tdoty(v));
}
-Vector2 Transform2D::basis_xform_inv(const Vector2& v) const{
+Vector2 Transform2D::basis_xform_inv(const Vector2 &v) const {
return Vector2(
- elements[0].dot(v),
- elements[1].dot(v)
- );
+ elements[0].dot(v),
+ elements[1].dot(v));
}
-Vector2 Transform2D::xform(const Vector2& v) const {
+Vector2 Transform2D::xform(const Vector2 &v) const {
return Vector2(
- tdotx(v),
- tdoty(v)
- ) + elements[2];
+ tdotx(v),
+ tdoty(v)) +
+ elements[2];
}
-Vector2 Transform2D::xform_inv(const Vector2& p_vec) const {
+Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
Vector2 v = p_vec - elements[2];
return Vector2(
- elements[0].dot(v),
- elements[1].dot(v)
- );
-
+ elements[0].dot(v),
+ elements[1].dot(v));
}
-Rect2 Transform2D::xform(const Rect2& p_rect) const {
+Rect2 Transform2D::xform(const Rect2 &p_rect) const {
- Vector2 x=elements[0]*p_rect.size.x;
- Vector2 y=elements[1]*p_rect.size.y;
- Vector2 pos = xform( p_rect.pos );
+ Vector2 x = elements[0] * p_rect.size.x;
+ Vector2 y = elements[1] * p_rect.size.y;
+ Vector2 pos = xform(p_rect.pos);
Rect2 new_rect;
- new_rect.pos=pos;
- new_rect.expand_to( pos+x );
- new_rect.expand_to( pos+y );
- new_rect.expand_to( pos+x+y );
+ new_rect.pos = pos;
+ new_rect.expand_to(pos + x);
+ new_rect.expand_to(pos + y);
+ new_rect.expand_to(pos + x + y);
return new_rect;
}
-void Transform2D::set_rotation_and_scale(real_t p_rot,const Size2& p_scale) {
-
- elements[0][0]=Math::cos(p_rot)*p_scale.x;
- elements[1][1]=Math::cos(p_rot)*p_scale.y;
- elements[1][0]=-Math::sin(p_rot)*p_scale.y;
- elements[0][1]=Math::sin(p_rot)*p_scale.x;
+void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
+ elements[0][0] = Math::cos(p_rot) * p_scale.x;
+ elements[1][1] = Math::cos(p_rot) * p_scale.y;
+ elements[1][0] = -Math::sin(p_rot) * p_scale.y;
+ elements[0][1] = Math::sin(p_rot) * p_scale.x;
}
-Rect2 Transform2D::xform_inv(const Rect2& p_rect) const {
+Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
- Vector2 ends[4]={
- xform_inv( p_rect.pos ),
- xform_inv( Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y ) ),
- xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y ) ),
- xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y ) )
+ Vector2 ends[4] = {
+ xform_inv(p_rect.pos),
+ xform_inv(Vector2(p_rect.pos.x, p_rect.pos.y + p_rect.size.y)),
+ xform_inv(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y + p_rect.size.y)),
+ xform_inv(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y))
};
Rect2 new_rect;
- new_rect.pos=ends[0];
+ new_rect.pos = ends[0];
new_rect.expand_to(ends[1]);
new_rect.expand_to(ends[2]);
new_rect.expand_to(ends[3]);
@@ -867,5 +880,4 @@ Rect2 Transform2D::xform_inv(const Rect2& p_rect) const {
return new_rect;
}
-
#endif
diff --git a/core/math/math_defs.h b/core/math/math_defs.h
index feaff38a44..08f4e27e64 100644
--- a/core/math/math_defs.h
+++ b/core/math/math_defs.h
@@ -30,11 +30,11 @@
#define MATH_DEFS_H
#define CMP_EPSILON 0.00001
-#define CMP_EPSILON2 (CMP_EPSILON*CMP_EPSILON)
+#define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
#define CMP_NORMALIZE_TOLERANCE 0.000001
#define CMP_POINT_IN_PLANE_EPSILON 0.00001
-#define USEC_TO_SEC(m_usec) ((m_usec)/1000000.0)
+#define USEC_TO_SEC(m_usec) ((m_usec) / 1000000.0)
/**
* "Real" is a type that will be translated to either floats or fixed depending
* on the compilation setting
@@ -42,11 +42,10 @@
enum ClockDirection {
- CLOCKWISE,
+ CLOCKWISE,
COUNTERCLOCKWISE
};
-
#ifdef REAL_T_IS_DOUBLE
typedef double real_t;
@@ -57,5 +56,4 @@ typedef float real_t;
#endif
-
#endif // MATH_DEFS_H
diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp
index c730b4fa30..ccc463c114 100644
--- a/core/math/math_funcs.cpp
+++ b/core/math/math_funcs.cpp
@@ -29,7 +29,7 @@
#include "math_funcs.h"
#include "core/os/os.h"
-pcg32_random_t Math::default_pcg = {1, PCG_DEFAULT_INC_64};
+pcg32_random_t Math::default_pcg = { 1, PCG_DEFAULT_INC_64 };
#define PHI 0x9e3779b9
@@ -39,30 +39,29 @@ static uint32_t Q[4096];
// TODO: we should eventually expose pcg.inc too
uint32_t Math::rand_from_seed(uint64_t *seed) {
- pcg32_random_t pcg = {*seed, PCG_DEFAULT_INC_64};
+ pcg32_random_t pcg = { *seed, PCG_DEFAULT_INC_64 };
uint32_t r = pcg32_random_r(&pcg);
*seed = pcg.state;
return r;
}
void Math::seed(uint64_t x) {
- default_pcg.state=x;
+ default_pcg.state = x;
}
void Math::randomize() {
OS::Time time = OS::get_singleton()->get_time();
- seed(OS::get_singleton()->get_ticks_usec()*(time.hour+1)*(time.min+1)*(time.sec+1)*rand()); // TODO: can be simplified.
+ seed(OS::get_singleton()->get_ticks_usec() * (time.hour + 1) * (time.min + 1) * (time.sec + 1) * rand()); // TODO: can be simplified.
}
uint32_t Math::rand() {
return pcg32_random_r(&default_pcg);
}
-
int Math::step_decimals(double p_step) {
- static const int maxn=9;
- static const double sd[maxn]={
+ static const int maxn = 9;
+ static const double sd[maxn] = {
0.9999, // somehow compensate for floating point error
0.09999,
0.009999,
@@ -74,9 +73,9 @@ int Math::step_decimals(double p_step) {
0.000000009999
};
- double as=Math::abs(p_step);
- for(int i=0;i<maxn;i++) {
- if (as>=sd[i]) {
+ double as = Math::abs(p_step);
+ for (int i = 0; i < maxn; i++) {
+ if (as >= sd[i]) {
return i;
}
}
@@ -84,46 +83,45 @@ int Math::step_decimals(double p_step) {
return maxn;
}
-double Math::dectime(double p_value,double p_amount, double p_step) {
+double Math::dectime(double p_value, double p_amount, double p_step) {
double sgn = p_value < 0 ? -1.0 : 1.0;
double val = Math::abs(p_value);
- val-=p_amount*p_step;
- if (val<0.0)
- val=0.0;
- return val*sgn;
+ val -= p_amount * p_step;
+ if (val < 0.0)
+ val = 0.0;
+ return val * sgn;
}
double Math::ease(double p_x, double p_c) {
- if (p_x<0)
- p_x=0;
- else if (p_x>1.0)
- p_x=1.0;
- if (p_c>0) {
- if (p_c<1.0) {
- return 1.0-Math::pow(1.0-p_x,1.0/p_c);
+ if (p_x < 0)
+ p_x = 0;
+ else if (p_x > 1.0)
+ p_x = 1.0;
+ if (p_c > 0) {
+ if (p_c < 1.0) {
+ return 1.0 - Math::pow(1.0 - p_x, 1.0 / p_c);
} else {
- return Math::pow(p_x,p_c);
+ return Math::pow(p_x, p_c);
}
- } else if (p_c<0) {
+ } else if (p_c < 0) {
//inout ease
- if (p_x<0.5) {
- return Math::pow(p_x*2.0,-p_c)*0.5;
+ if (p_x < 0.5) {
+ return Math::pow(p_x * 2.0, -p_c) * 0.5;
} else {
- return (1.0-Math::pow(1.0-(p_x-0.5)*2.0,-p_c))*0.5+0.5;
+ return (1.0 - Math::pow(1.0 - (p_x - 0.5) * 2.0, -p_c)) * 0.5 + 0.5;
}
} else
return 0; // no ease (raw)
}
-double Math::stepify(double p_value,double p_step) {
- if (p_step!=0) {
- p_value=Math::floor( p_value / p_step + 0.5 ) * p_step;
+double Math::stepify(double p_value, double p_step) {
+ if (p_step != 0) {
+ p_value = Math::floor(p_value / p_step + 0.5) * p_step;
}
return p_value;
}
-
uint32_t Math::larger_prime(uint32_t p_val) {
static const uint32_t primes[] = {
@@ -159,11 +157,11 @@ uint32_t Math::larger_prime(uint32_t p_val) {
0,
};
- int idx=0;
+ int idx = 0;
while (true) {
- ERR_FAIL_COND_V(primes[idx]==0,0);
- if (primes[idx]>p_val)
+ ERR_FAIL_COND_V(primes[idx] == 0, 0);
+ if (primes[idx] > p_val)
return primes[idx];
idx++;
}
@@ -173,14 +171,12 @@ uint32_t Math::larger_prime(uint32_t p_val) {
double Math::random(double from, double to) {
unsigned int r = Math::rand();
- double ret = (double)r/(double)RANDOM_MAX;
- return (ret)*(to-from) + from;
+ double ret = (double)r / (double)RANDOM_MAX;
+ return (ret) * (to - from) + from;
}
float Math::random(float from, float to) {
unsigned int r = Math::rand();
- float ret = (float)r/(float)RANDOM_MAX;
- return (ret)*(to-from) + from;
+ float ret = (float)r / (float)RANDOM_MAX;
+ return (ret) * (to - from) + from;
}
-
-
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h
index ae461eda2e..3e02ac0bb8 100644
--- a/core/math/math_funcs.h
+++ b/core/math/math_funcs.h
@@ -29,13 +29,13 @@
#ifndef MATH_FUNCS_H
#define MATH_FUNCS_H
-#include "typedefs.h"
#include "math_defs.h"
#include "pcg.h"
+#include "typedefs.h"
-#include <math.h>
#include <float.h>
-
+#include <math.h>
+
#define Math_PI 3.14159265358979323846
#define Math_SQRT12 0.7071067811865475244008443621048490
#define Math_LN2 0.693147180559945309417
@@ -50,10 +50,9 @@ public:
Math() {} // useless to instance
enum {
- RANDOM_MAX=4294967295L
+ RANDOM_MAX = 4294967295L
};
-
static _ALWAYS_INLINE_ double sin(double p_x) { return ::sin(p_x); }
static _ALWAYS_INLINE_ float sin(float p_x) { return ::sinf(p_x); }
@@ -81,14 +80,14 @@ public:
static _ALWAYS_INLINE_ double atan(double p_x) { return ::atan(p_x); }
static _ALWAYS_INLINE_ float atan(float p_x) { return ::atanf(p_x); }
- static _ALWAYS_INLINE_ double atan2(double p_y, double p_x) { return ::atan2(p_y,p_x); }
- static _ALWAYS_INLINE_ float atan2(float p_y, float p_x) { return ::atan2f(p_y,p_x); }
+ static _ALWAYS_INLINE_ double atan2(double p_y, double p_x) { return ::atan2(p_y, p_x); }
+ static _ALWAYS_INLINE_ float atan2(float p_y, float p_x) { return ::atan2f(p_y, p_x); }
static _ALWAYS_INLINE_ double sqrt(double p_x) { return ::sqrt(p_x); }
static _ALWAYS_INLINE_ float sqrt(float p_x) { return ::sqrtf(p_x); }
- static _ALWAYS_INLINE_ double fmod(double p_x,double p_y) { return ::fmod(p_x,p_y); }
- static _ALWAYS_INLINE_ float fmod(float p_x,float p_y) { return ::fmodf(p_x,p_y); }
+ static _ALWAYS_INLINE_ double fmod(double p_x, double p_y) { return ::fmod(p_x, p_y); }
+ static _ALWAYS_INLINE_ float fmod(float p_x, float p_y) { return ::fmodf(p_x, p_y); }
static _ALWAYS_INLINE_ double floor(double p_x) { return ::floor(p_x); }
static _ALWAYS_INLINE_ float floor(float p_x) { return ::floorf(p_x); }
@@ -96,8 +95,8 @@ public:
static _ALWAYS_INLINE_ double ceil(double p_x) { return ::ceil(p_x); }
static _ALWAYS_INLINE_ float ceil(float p_x) { return ::ceilf(p_x); }
- static _ALWAYS_INLINE_ double pow(double p_x, double p_y) { return ::pow(p_x,p_y); }
- static _ALWAYS_INLINE_ float pow(float p_x, float p_y) { return ::powf(p_x,p_y); }
+ static _ALWAYS_INLINE_ double pow(double p_x, double p_y) { return ::pow(p_x, p_y); }
+ static _ALWAYS_INLINE_ float pow(float p_x, float p_y) { return ::powf(p_x, p_y); }
static _ALWAYS_INLINE_ double log(double p_x) { return ::log(p_x); }
static _ALWAYS_INLINE_ float log(float p_x) { return ::logf(p_x); }
@@ -105,59 +104,59 @@ public:
static _ALWAYS_INLINE_ double exp(double p_x) { return ::exp(p_x); }
static _ALWAYS_INLINE_ float exp(float p_x) { return ::expf(p_x); }
- static _ALWAYS_INLINE_ bool is_nan(double p_val) { return (p_val!=p_val); }
- static _ALWAYS_INLINE_ bool is_nan(float p_val) { return (p_val!=p_val); }
+ static _ALWAYS_INLINE_ bool is_nan(double p_val) { return (p_val != p_val); }
+ static _ALWAYS_INLINE_ bool is_nan(float p_val) { return (p_val != p_val); }
static _ALWAYS_INLINE_ bool is_inf(double p_val) {
- #ifdef _MSC_VER
+#ifdef _MSC_VER
return !_finite(p_val);
- #else
+#else
return isinf(p_val);
- #endif
+#endif
}
-
+
static _ALWAYS_INLINE_ bool is_inf(float p_val) {
- #ifdef _MSC_VER
+#ifdef _MSC_VER
return !_finite(p_val);
- #else
+#else
return isinf(p_val);
- #endif
+#endif
}
-
+
static _ALWAYS_INLINE_ double abs(double g) { return absd(g); }
static _ALWAYS_INLINE_ float abs(float g) { return absf(g); }
static _ALWAYS_INLINE_ int abs(int g) { return g > 0 ? g : -g; }
- static _ALWAYS_INLINE_ double fposmod(double p_x,double p_y) { return (p_x>=0) ? Math::fmod(p_x,p_y) : p_y-Math::fmod(-p_x,p_y); }
- static _ALWAYS_INLINE_ float fposmod(float p_x,float p_y) { return (p_x>=0) ? Math::fmod(p_x,p_y) : p_y-Math::fmod(-p_x,p_y); }
+ static _ALWAYS_INLINE_ double fposmod(double p_x, double p_y) { return (p_x >= 0) ? Math::fmod(p_x, p_y) : p_y - Math::fmod(-p_x, p_y); }
+ static _ALWAYS_INLINE_ float fposmod(float p_x, float p_y) { return (p_x >= 0) ? Math::fmod(p_x, p_y) : p_y - Math::fmod(-p_x, p_y); }
- static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y*Math_PI/180.0; }
- static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y*Math_PI/180.0; }
+ static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * Math_PI / 180.0; }
+ static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * Math_PI / 180.0; }
- static _ALWAYS_INLINE_ double rad2deg(double p_y) { return p_y*180.0/Math_PI; }
- static _ALWAYS_INLINE_ float rad2deg(float p_y) { return p_y*180.0/Math_PI; }
+ static _ALWAYS_INLINE_ double rad2deg(double p_y) { return p_y * 180.0 / Math_PI; }
+ static _ALWAYS_INLINE_ float rad2deg(float p_y) { return p_y * 180.0 / Math_PI; }
- static _ALWAYS_INLINE_ double lerp(double a, double b, double c) { return a+(b-a)*c; }
- static _ALWAYS_INLINE_ float lerp(float a, float b, float c) { return a+(b-a)*c; }
+ static _ALWAYS_INLINE_ double lerp(double a, double b, double c) { return a + (b - a) * c; }
+ static _ALWAYS_INLINE_ float lerp(float a, float b, float c) { return a + (b - a) * c; }
- static _ALWAYS_INLINE_ double linear2db(double p_linear) { return Math::log( p_linear ) * 8.6858896380650365530225783783321; }
- static _ALWAYS_INLINE_ float linear2db(float p_linear) { return Math::log( p_linear ) * 8.6858896380650365530225783783321; }
+ static _ALWAYS_INLINE_ double linear2db(double p_linear) { return Math::log(p_linear) * 8.6858896380650365530225783783321; }
+ static _ALWAYS_INLINE_ float linear2db(float p_linear) { return Math::log(p_linear) * 8.6858896380650365530225783783321; }
- static _ALWAYS_INLINE_ double db2linear(double p_db) { return Math::exp( p_db * 0.11512925464970228420089957273422 ); }
- static _ALWAYS_INLINE_ float db2linear(float p_db) { return Math::exp( p_db * 0.11512925464970228420089957273422 ); }
+ static _ALWAYS_INLINE_ double db2linear(double p_db) { return Math::exp(p_db * 0.11512925464970228420089957273422); }
+ static _ALWAYS_INLINE_ float db2linear(float p_db) { return Math::exp(p_db * 0.11512925464970228420089957273422); }
- static _ALWAYS_INLINE_ double round(double p_val) { return (p_val>=0) ? Math::floor(p_val+0.5) : -Math::floor(-p_val+0.5); }
- static _ALWAYS_INLINE_ float round(float p_val) { return (p_val>=0) ? Math::floor(p_val+0.5) : -Math::floor(-p_val+0.5); }
+ static _ALWAYS_INLINE_ double round(double p_val) { return (p_val >= 0) ? Math::floor(p_val + 0.5) : -Math::floor(-p_val + 0.5); }
+ static _ALWAYS_INLINE_ float round(float p_val) { return (p_val >= 0) ? Math::floor(p_val + 0.5) : -Math::floor(-p_val + 0.5); }
// double only, as these functions are mainly used by the editor and not performance-critical,
static double ease(double p_x, double p_c);
static int step_decimals(double p_step);
- static double stepify(double p_value,double p_step);
- static double dectime(double p_value,double p_amount, double p_step);
+ static double stepify(double p_value, double p_step);
+ static double dectime(double p_value, double p_amount, double p_step);
static uint32_t larger_prime(uint32_t p_val);
- static void seed(uint64_t x=0);
+ static void seed(uint64_t x = 0);
static void randomize();
static uint32_t rand_from_seed(uint64_t *seed);
static uint32_t rand();
@@ -168,17 +167,15 @@ public:
static float random(float from, float to);
static real_t random(int from, int to) { return (real_t)random((real_t)from, (real_t)to); }
-
static _ALWAYS_INLINE_ bool isequal_approx(real_t a, real_t b) {
// TODO: Comparing floats for approximate-equality is non-trivial.
// Using epsilon should cover the typical cases in Godot (where a == b is used to compare two reals), such as matrix and vector comparison operators.
// A proper implementation in terms of ULPs should eventually replace the contents of this function.
// See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ for details.
- return abs(a-b) < CMP_EPSILON;
+ return abs(a - b) < CMP_EPSILON;
}
-
static _ALWAYS_INLINE_ float absf(float g) {
union {
@@ -186,8 +183,8 @@ public:
uint32_t i;
} u;
- u.f=g;
- u.i&=2147483647u;
+ u.f = g;
+ u.i &= 2147483647u;
return u.f;
}
@@ -197,8 +194,8 @@ public:
double d;
uint64_t i;
} u;
- u.d=g;
- u.i&=(uint64_t)9223372036854775807ll;
+ u.d = g;
+ u.i &= (uint64_t)9223372036854775807ll;
return u.d;
}
@@ -208,11 +205,10 @@ public:
static int b;
#if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
- b = (int)((a>0.0) ? (a + 0.5):(a -0.5));
+ b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
#elif defined(_MSC_VER) && _MSC_VER < 1800
- __asm fld a
- __asm fistp b
+ __asm fld a __asm fistp b
/*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
// use AT&T inline assembly style, document that
// we use memory as output (=m) and input (m)
@@ -223,12 +219,11 @@ public:
: "m" (a));*/
#else
- b=lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
+ b = lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
#endif
- return b;
+ return b;
}
-
#if defined(__GNUC__)
static _ALWAYS_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
@@ -239,37 +234,35 @@ public:
static _ALWAYS_INLINE_ int64_t dtoll(float p_float) { return (int64_t)p_float; } ///@TODO OPTIMIZE and rename
#endif
-
- static _ALWAYS_INLINE_ uint32_t halfbits_to_floatbits(uint16_t h)
- {
- uint16_t h_exp, h_sig;
- uint32_t f_sgn, f_exp, f_sig;
-
- h_exp = (h&0x7c00u);
- f_sgn = ((uint32_t)h&0x8000u) << 16;
- switch (h_exp) {
- case 0x0000u: /* 0 or subnormal */
- h_sig = (h&0x03ffu);
- /* Signed zero */
- if (h_sig == 0) {
- return f_sgn;
- }
- /* Subnormal */
- h_sig <<= 1;
- while ((h_sig&0x0400u) == 0) {
- h_sig <<= 1;
- h_exp++;
- }
- f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
- f_sig = ((uint32_t)(h_sig&0x03ffu)) << 13;
- return f_sgn + f_exp + f_sig;
- case 0x7c00u: /* inf or NaN */
- /* All-ones exponent and a copy of the significand */
- return f_sgn + 0x7f800000u + (((uint32_t)(h&0x03ffu)) << 13);
- default: /* normalized */
- /* Just need to adjust the exponent and shift */
- return f_sgn + (((uint32_t)(h&0x7fffu) + 0x1c000u) << 13);
- }
+ static _ALWAYS_INLINE_ uint32_t halfbits_to_floatbits(uint16_t h) {
+ uint16_t h_exp, h_sig;
+ uint32_t f_sgn, f_exp, f_sig;
+
+ h_exp = (h & 0x7c00u);
+ f_sgn = ((uint32_t)h & 0x8000u) << 16;
+ switch (h_exp) {
+ case 0x0000u: /* 0 or subnormal */
+ h_sig = (h & 0x03ffu);
+ /* Signed zero */
+ if (h_sig == 0) {
+ return f_sgn;
+ }
+ /* Subnormal */
+ h_sig <<= 1;
+ while ((h_sig & 0x0400u) == 0) {
+ h_sig <<= 1;
+ h_exp++;
+ }
+ f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
+ f_sig = ((uint32_t)(h_sig & 0x03ffu)) << 13;
+ return f_sgn + f_exp + f_sig;
+ case 0x7c00u: /* inf or NaN */
+ /* All-ones exponent and a copy of the significand */
+ return f_sgn + 0x7f800000u + (((uint32_t)(h & 0x03ffu)) << 13);
+ default: /* normalized */
+ /* Just need to adjust the exponent and shift */
+ return f_sgn + (((uint32_t)(h & 0x7fffu) + 0x1c000u) << 13);
+ }
}
static _ALWAYS_INLINE_ float halfptr_to_float(const uint16_t *h) {
@@ -279,70 +272,58 @@ public:
float f32;
} u;
- u.u32=halfbits_to_floatbits(*h);
+ u.u32 = halfbits_to_floatbits(*h);
return u.f32;
}
static _ALWAYS_INLINE_ uint16_t make_half_float(float f) {
- union {
- float fv;
- uint32_t ui;
- } ci;
- ci.fv=f;
-
- uint32_t x = ci.ui;
- uint32_t sign = (unsigned short)(x >> 31);
- uint32_t mantissa;
- uint32_t exp;
- uint16_t hf;
-
- // get mantissa
- mantissa = x & ((1 << 23) - 1);
- // get exponent bits
- exp = x & (0xFF << 23);
- if (exp >= 0x47800000)
- {
- // check if the original single precision float number is a NaN
- if (mantissa && (exp == (0xFF << 23)))
- {
- // we have a single precision NaN
- mantissa = (1 << 23) - 1;
- }
- else
- {
- // 16-bit half-float representation stores number as Inf
- mantissa = 0;
+ union {
+ float fv;
+ uint32_t ui;
+ } ci;
+ ci.fv = f;
+
+ uint32_t x = ci.ui;
+ uint32_t sign = (unsigned short)(x >> 31);
+ uint32_t mantissa;
+ uint32_t exp;
+ uint16_t hf;
+
+ // get mantissa
+ mantissa = x & ((1 << 23) - 1);
+ // get exponent bits
+ exp = x & (0xFF << 23);
+ if (exp >= 0x47800000) {
+ // check if the original single precision float number is a NaN
+ if (mantissa && (exp == (0xFF << 23))) {
+ // we have a single precision NaN
+ mantissa = (1 << 23) - 1;
+ } else {
+ // 16-bit half-float representation stores number as Inf
+ mantissa = 0;
+ }
+ hf = (((uint16_t)sign) << 15) | (uint16_t)((0x1F << 10)) |
+ (uint16_t)(mantissa >> 13);
}
- hf = (((uint16_t)sign) << 15) | (uint16_t)((0x1F << 10)) |
- (uint16_t)(mantissa >> 13);
- }
- // check if exponent is <= -15
- else if (exp <= 0x38000000)
- {
-
- /*// store a denorm half-float value or zero
+ // check if exponent is <= -15
+ else if (exp <= 0x38000000) {
+
+ /*// store a denorm half-float value or zero
exp = (0x38000000 - exp) >> 23;
mantissa >>= (14 + exp);
hf = (((uint16_t)sign) << 15) | (uint16_t)(mantissa);
*/
- hf=0; //denormals do not work for 3D, convert to zero
- }
- else
- {
- hf = (((uint16_t)sign) << 15) |
- (uint16_t)((exp - 0x38000000) >> 13) |
- (uint16_t)(mantissa >> 13);
- }
-
- return hf;
- }
-
-
+ hf = 0; //denormals do not work for 3D, convert to zero
+ } else {
+ hf = (((uint16_t)sign) << 15) |
+ (uint16_t)((exp - 0x38000000) >> 13) |
+ (uint16_t)(mantissa >> 13);
+ }
+ return hf;
+ }
};
-
-
#endif // MATH_FUNCS_H
diff --git a/core/math/matrix3.cpp b/core/math/matrix3.cpp
index 1fabfbbd4c..5f73d91ef3 100644
--- a/core/math/matrix3.cpp
+++ b/core/math/matrix3.cpp
@@ -30,46 +30,44 @@
#include "math_funcs.h"
#include "os/copymem.h"
-#define cofac(row1,col1, row2, col2)\
+#define cofac(row1, col1, row2, col2) \
(elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1])
-void Basis::from_z(const Vector3& p_z) {
+void Basis::from_z(const Vector3 &p_z) {
- if (Math::abs(p_z.z) > Math_SQRT12 ) {
+ if (Math::abs(p_z.z) > Math_SQRT12) {
// choose p in y-z plane
- real_t a = p_z[1]*p_z[1] + p_z[2]*p_z[2];
- real_t k = 1.0/Math::sqrt(a);
- elements[0]=Vector3(0,-p_z[2]*k,p_z[1]*k);
- elements[1]=Vector3(a*k,-p_z[0]*elements[0][2],p_z[0]*elements[0][1]);
+ real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
+ real_t k = 1.0 / Math::sqrt(a);
+ elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
+ elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[0][1]);
} else {
// choose p in x-y plane
- real_t a = p_z.x*p_z.x + p_z.y*p_z.y;
- real_t k = 1.0/Math::sqrt(a);
- elements[0]=Vector3(-p_z.y*k,p_z.x*k,0);
- elements[1]=Vector3(-p_z.z*elements[0].y,p_z.z*elements[0].x,a*k);
+ real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
+ real_t k = 1.0 / Math::sqrt(a);
+ elements[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
+ elements[1] = Vector3(-p_z.z * elements[0].y, p_z.z * elements[0].x, a * k);
}
- elements[2]=p_z;
+ elements[2] = p_z;
}
void Basis::invert() {
-
- real_t co[3]={
+ real_t co[3] = {
cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
};
- real_t det = elements[0][0] * co[0]+
- elements[0][1] * co[1]+
- elements[0][2] * co[2];
-
- ERR_FAIL_COND( det == 0 );
- real_t s = 1.0/det;
+ real_t det = elements[0][0] * co[0] +
+ elements[0][1] * co[1] +
+ elements[0][2] * co[2];
- set( co[0]*s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
- co[1]*s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
- co[2]*s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s );
+ ERR_FAIL_COND(det == 0);
+ real_t s = 1.0 / det;
+ set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
+ co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
+ co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s);
}
void Basis::orthonormalize() {
@@ -77,20 +75,19 @@ void Basis::orthonormalize() {
// Gram-Schmidt Process
- Vector3 x=get_axis(0);
- Vector3 y=get_axis(1);
- Vector3 z=get_axis(2);
+ Vector3 x = get_axis(0);
+ Vector3 y = get_axis(1);
+ Vector3 z = get_axis(2);
x.normalize();
- y = (y-x*(x.dot(y)));
+ y = (y - x * (x.dot(y)));
y.normalize();
- z = (z-x*(x.dot(z))-y*(y.dot(z)));
+ z = (z - x * (x.dot(z)) - y * (y.dot(z)));
z.normalize();
- set_axis(0,x);
- set_axis(1,y);
- set_axis(2,z);
-
+ set_axis(0, x);
+ set_axis(1, y);
+ set_axis(2, z);
}
Basis Basis::orthonormalized() const {
@@ -102,16 +99,15 @@ Basis Basis::orthonormalized() const {
bool Basis::is_orthogonal() const {
Basis id;
- Basis m = (*this)*transposed();
+ Basis m = (*this) * transposed();
- return isequal_approx(id,m);
+ return isequal_approx(id, m);
}
bool Basis::is_rotation() const {
return Math::isequal_approx(determinant(), 1) && is_orthogonal();
}
-
bool Basis::is_symmetric() const {
if (Math::abs(elements[0][1] - elements[1][0]) > CMP_EPSILON)
@@ -124,21 +120,20 @@ bool Basis::is_symmetric() const {
return true;
}
-
Basis Basis::diagonalize() {
//NOTE: only implemented for symmetric matrices
//with the Jacobi iterative method method
-
+
ERR_FAIL_COND_V(!is_symmetric(), Basis());
const int ite_max = 1024;
- real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2];
+ real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2];
int ite = 0;
Basis acc_rot;
- while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max ) {
+ while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max) {
real_t el01_2 = elements[0][1] * elements[0][1];
real_t el02_2 = elements[0][2] * elements[0][2];
real_t el12_2 = elements[1][2] * elements[1][2];
@@ -151,7 +146,7 @@ Basis Basis::diagonalize() {
} else {
i = 0;
j = 1;
- }
+ }
} else {
if (el12_2 > el02_2) {
i = 1;
@@ -163,17 +158,17 @@ Basis Basis::diagonalize() {
}
// Compute the rotation angle
- real_t angle;
+ real_t angle;
if (Math::abs(elements[j][j] - elements[i][i]) < CMP_EPSILON) {
angle = Math_PI / 4;
} else {
- angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
+ angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
}
// Compute the rotation matrix
Basis rot;
rot.elements[i][i] = rot.elements[j][j] = Math::cos(angle);
- rot.elements[i][j] = - (rot.elements[j][i] = Math::sin(angle));
+ rot.elements[i][j] = -(rot.elements[j][i] = Math::sin(angle));
// Update the off matrix norm
off_matrix_norm_2 -= elements[i][j] * elements[i][j];
@@ -188,41 +183,41 @@ Basis Basis::diagonalize() {
Basis Basis::inverse() const {
- Basis inv=*this;
+ Basis inv = *this;
inv.invert();
return inv;
}
void Basis::transpose() {
- SWAP(elements[0][1],elements[1][0]);
- SWAP(elements[0][2],elements[2][0]);
- SWAP(elements[1][2],elements[2][1]);
+ SWAP(elements[0][1], elements[1][0]);
+ SWAP(elements[0][2], elements[2][0]);
+ SWAP(elements[1][2], elements[2][1]);
}
Basis Basis::transposed() const {
- Basis tr=*this;
+ Basis tr = *this;
tr.transpose();
return tr;
}
// Multiplies the matrix from left by the scaling matrix: M -> S.M
// See the comment for Basis::rotated for further explanation.
-void Basis::scale(const Vector3& p_scale) {
+void Basis::scale(const Vector3 &p_scale) {
- elements[0][0]*=p_scale.x;
- elements[0][1]*=p_scale.x;
- elements[0][2]*=p_scale.x;
- elements[1][0]*=p_scale.y;
- elements[1][1]*=p_scale.y;
- elements[1][2]*=p_scale.y;
- elements[2][0]*=p_scale.z;
- elements[2][1]*=p_scale.z;
- elements[2][2]*=p_scale.z;
+ elements[0][0] *= p_scale.x;
+ elements[0][1] *= p_scale.x;
+ elements[0][2] *= p_scale.x;
+ elements[1][0] *= p_scale.y;
+ elements[1][1] *= p_scale.y;
+ elements[1][2] *= p_scale.y;
+ elements[2][0] *= p_scale.z;
+ elements[2][1] *= p_scale.z;
+ elements[2][2] *= p_scale.z;
}
-Basis Basis::scaled( const Vector3& p_scale ) const {
+Basis Basis::scaled(const Vector3 &p_scale) const {
Basis m = *this;
m.scale(p_scale);
@@ -236,12 +231,10 @@ Vector3 Basis::get_scale() const {
// (such that it can be represented by a Quat or Euler angles), we absorb the sign flip into the scaling matrix.
// As such, it works in conjuction with get_rotation().
real_t det_sign = determinant() > 0 ? 1 : -1;
- return det_sign*Vector3(
- Vector3(elements[0][0],elements[1][0],elements[2][0]).length(),
- Vector3(elements[0][1],elements[1][1],elements[2][1]).length(),
- Vector3(elements[0][2],elements[1][2],elements[2][2]).length()
- );
-
+ return det_sign * Vector3(
+ Vector3(elements[0][0], elements[1][0], elements[2][0]).length(),
+ Vector3(elements[0][1], elements[1][1], elements[2][1]).length(),
+ Vector3(elements[0][2], elements[1][2], elements[2][2]).length());
}
// Multiplies the matrix from left by the rotation matrix: M -> R.M
@@ -250,19 +243,19 @@ Vector3 Basis::get_scale() const {
// The main use of Basis is as Transform.basis, which is used a the transformation matrix
// of 3D object. Rotate here refers to rotation of the object (which is R * (*this)),
// not the matrix itself (which is R * (*this) * R.transposed()).
-Basis Basis::rotated(const Vector3& p_axis, real_t p_phi) const {
+Basis Basis::rotated(const Vector3 &p_axis, real_t p_phi) const {
return Basis(p_axis, p_phi) * (*this);
}
-void Basis::rotate(const Vector3& p_axis, real_t p_phi) {
+void Basis::rotate(const Vector3 &p_axis, real_t p_phi) {
*this = rotated(p_axis, p_phi);
}
-Basis Basis::rotated(const Vector3& p_euler) const {
+Basis Basis::rotated(const Vector3 &p_euler) const {
return Basis(p_euler) * (*this);
}
-void Basis::rotate(const Vector3& p_euler) {
+void Basis::rotate(const Vector3 &p_euler) {
*this = rotated(p_euler);
}
@@ -274,7 +267,7 @@ Vector3 Basis::get_rotation() const {
real_t det = m.determinant();
if (det < 0) {
// Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
- m.scale(Vector3(-1,-1,-1));
+ m.scale(Vector3(-1, -1, -1));
}
return m.get_euler();
@@ -304,67 +297,64 @@ Vector3 Basis::get_euler() const {
ERR_FAIL_COND_V(is_rotation() == false, euler);
euler.y = Math::asin(elements[0][2]);
- if ( euler.y < Math_PI*0.5) {
- if ( euler.y > -Math_PI*0.5) {
- euler.x = Math::atan2(-elements[1][2],elements[2][2]);
- euler.z = Math::atan2(-elements[0][1],elements[0][0]);
+ if (euler.y < Math_PI * 0.5) {
+ if (euler.y > -Math_PI * 0.5) {
+ euler.x = Math::atan2(-elements[1][2], elements[2][2]);
+ euler.z = Math::atan2(-elements[0][1], elements[0][0]);
} else {
- real_t r = Math::atan2(elements[1][0],elements[1][1]);
+ real_t r = Math::atan2(elements[1][0], elements[1][1]);
euler.z = 0.0;
euler.x = euler.z - r;
-
}
} else {
- real_t r = Math::atan2(elements[0][1],elements[1][1]);
+ real_t r = Math::atan2(elements[0][1], elements[1][1]);
euler.z = 0;
euler.x = r - euler.z;
}
return euler;
-
-
}
// set_euler expects a vector containing the Euler angles in the format
// (c,b,a), where a is the angle of the first rotation, and c is the last.
// The current implementation uses XYZ convention (Z is the first rotation).
-void Basis::set_euler(const Vector3& p_euler) {
+void Basis::set_euler(const Vector3 &p_euler) {
real_t c, s;
c = Math::cos(p_euler.x);
s = Math::sin(p_euler.x);
- Basis xmat(1.0,0.0,0.0,0.0,c,-s,0.0,s,c);
+ Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
c = Math::cos(p_euler.y);
s = Math::sin(p_euler.y);
- Basis ymat(c,0.0,s,0.0,1.0,0.0,-s,0.0,c);
+ Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
c = Math::cos(p_euler.z);
s = Math::sin(p_euler.z);
- Basis zmat(c,-s,0.0,s,c,0.0,0.0,0.0,1.0);
+ Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
//optimizer will optimize away all this anyway
- *this = xmat*(ymat*zmat);
+ *this = xmat * (ymat * zmat);
}
-bool Basis::isequal_approx(const Basis& a, const Basis& b) const {
+bool Basis::isequal_approx(const Basis &a, const Basis &b) const {
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- if (Math::isequal_approx(a.elements[i][j],b.elements[i][j]) == false)
- return false;
- }
- }
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
+ if (Math::isequal_approx(a.elements[i][j], b.elements[i][j]) == false)
+ return false;
+ }
+ }
- return true;
+ return true;
}
-bool Basis::operator==(const Basis& p_matrix) const {
+bool Basis::operator==(const Basis &p_matrix) const {
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
if (elements[i][j] != p_matrix.elements[i][j])
return false;
}
@@ -373,22 +363,22 @@ bool Basis::operator==(const Basis& p_matrix) const {
return true;
}
-bool Basis::operator!=(const Basis& p_matrix) const {
+bool Basis::operator!=(const Basis &p_matrix) const {
- return (!(*this==p_matrix));
+ return (!(*this == p_matrix));
}
Basis::operator String() const {
String mtx;
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
- for (int j=0;j<3;j++) {
+ for (int j = 0; j < 3; j++) {
- if (i!=0 || j!=0)
- mtx+=", ";
+ if (i != 0 || j != 0)
+ mtx += ", ";
- mtx+=rtos( elements[i][j] );
+ mtx += rtos(elements[i][j]);
}
}
@@ -401,21 +391,18 @@ Basis::operator Quat() const {
real_t trace = elements[0][0] + elements[1][1] + elements[2][2];
real_t temp[4];
- if (trace > 0.0)
- {
+ if (trace > 0.0) {
real_t s = Math::sqrt(trace + 1.0);
- temp[3]=(s * 0.5);
+ temp[3] = (s * 0.5);
s = 0.5 / s;
- temp[0]=((elements[2][1] - elements[1][2]) * s);
- temp[1]=((elements[0][2] - elements[2][0]) * s);
- temp[2]=((elements[1][0] - elements[0][1]) * s);
- }
- else
- {
+ temp[0] = ((elements[2][1] - elements[1][2]) * s);
+ temp[1] = ((elements[0][2] - elements[2][0]) * s);
+ temp[2] = ((elements[1][0] - elements[0][1]) * s);
+ } else {
int i = elements[0][0] < elements[1][1] ?
- (elements[1][1] < elements[2][2] ? 2 : 1) :
- (elements[0][0] < elements[2][2] ? 2 : 0);
+ (elements[1][1] < elements[2][2] ? 2 : 1) :
+ (elements[0][0] < elements[2][2] ? 2 : 0);
int j = (i + 1) % 3;
int k = (i + 2) % 3;
@@ -428,11 +415,10 @@ Basis::operator Quat() const {
temp[k] = (elements[k][i] + elements[i][k]) * s;
}
- return Quat(temp[0],temp[1],temp[2],temp[3]);
-
+ return Quat(temp[0], temp[1], temp[2], temp[3]);
}
-static const Basis _ortho_bases[24]={
+static const Basis _ortho_bases[24] = {
Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
@@ -462,164 +448,147 @@ static const Basis _ortho_bases[24]={
int Basis::get_orthogonal_index() const {
//could be sped up if i come up with a way
- Basis orth=*this;
- for(int i=0;i<3;i++) {
- for(int j=0;j<3;j++) {
+ Basis orth = *this;
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
real_t v = orth[i][j];
- if (v>0.5)
- v=1.0;
- else if (v<-0.5)
- v=-1.0;
+ if (v > 0.5)
+ v = 1.0;
+ else if (v < -0.5)
+ v = -1.0;
else
- v=0;
+ v = 0;
- orth[i][j]=v;
+ orth[i][j] = v;
}
}
- for(int i=0;i<24;i++) {
+ for (int i = 0; i < 24; i++) {
- if (_ortho_bases[i]==orth)
+ if (_ortho_bases[i] == orth)
return i;
-
-
}
return 0;
}
-void Basis::set_orthogonal_index(int p_index){
+void Basis::set_orthogonal_index(int p_index) {
//there only exist 24 orthogonal bases in r3
- ERR_FAIL_INDEX(p_index,24);
-
-
- *this=_ortho_bases[p_index];
+ ERR_FAIL_INDEX(p_index, 24);
+ *this = _ortho_bases[p_index];
}
-
-void Basis::get_axis_and_angle(Vector3 &r_axis,real_t& r_angle) const {
+void Basis::get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const {
ERR_FAIL_COND(is_rotation() == false);
+ real_t angle, x, y, z; // variables for result
+ real_t epsilon = 0.01; // margin to allow for rounding errors
+ real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
- real_t angle,x,y,z; // variables for result
- real_t epsilon = 0.01; // margin to allow for rounding errors
- real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
-
- if ( (Math::abs(elements[1][0]-elements[0][1])< epsilon)
- && (Math::abs(elements[2][0]-elements[0][2])< epsilon)
- && (Math::abs(elements[2][1]-elements[1][2])< epsilon)) {
- // singularity found
- // first check for identity matrix which must have +1 for all terms
- // in leading diagonaland zero in other terms
- if ((Math::abs(elements[1][0]+elements[0][1]) < epsilon2)
- && (Math::abs(elements[2][0]+elements[0][2]) < epsilon2)
- && (Math::abs(elements[2][1]+elements[1][2]) < epsilon2)
- && (Math::abs(elements[0][0]+elements[1][1]+elements[2][2]-3) < epsilon2)) {
+ if ((Math::abs(elements[1][0] - elements[0][1]) < epsilon) && (Math::abs(elements[2][0] - elements[0][2]) < epsilon) && (Math::abs(elements[2][1] - elements[1][2]) < epsilon)) {
+ // singularity found
+ // first check for identity matrix which must have +1 for all terms
+ // in leading diagonaland zero in other terms
+ if ((Math::abs(elements[1][0] + elements[0][1]) < epsilon2) && (Math::abs(elements[2][0] + elements[0][2]) < epsilon2) && (Math::abs(elements[2][1] + elements[1][2]) < epsilon2) && (Math::abs(elements[0][0] + elements[1][1] + elements[2][2] - 3) < epsilon2)) {
// this singularity is identity matrix so angle = 0
- r_axis=Vector3(0,1,0);
- r_angle=0;
+ r_axis = Vector3(0, 1, 0);
+ r_angle = 0;
return;
}
// otherwise this singularity is angle = 180
angle = Math_PI;
- real_t xx = (elements[0][0]+1)/2;
- real_t yy = (elements[1][1]+1)/2;
- real_t zz = (elements[2][2]+1)/2;
- real_t xy = (elements[1][0]+elements[0][1])/4;
- real_t xz = (elements[2][0]+elements[0][2])/4;
- real_t yz = (elements[2][1]+elements[1][2])/4;
+ real_t xx = (elements[0][0] + 1) / 2;
+ real_t yy = (elements[1][1] + 1) / 2;
+ real_t zz = (elements[2][2] + 1) / 2;
+ real_t xy = (elements[1][0] + elements[0][1]) / 4;
+ real_t xz = (elements[2][0] + elements[0][2]) / 4;
+ real_t yz = (elements[2][1] + elements[1][2]) / 4;
if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term
- if (xx< epsilon) {
+ if (xx < epsilon) {
x = 0;
y = 0.7071;
z = 0.7071;
} else {
x = Math::sqrt(xx);
- y = xy/x;
- z = xz/x;
+ y = xy / x;
+ z = xz / x;
}
} else if (yy > zz) { // elements[1][1] is the largest diagonal term
- if (yy< epsilon) {
+ if (yy < epsilon) {
x = 0.7071;
y = 0;
z = 0.7071;
} else {
y = Math::sqrt(yy);
- x = xy/y;
- z = yz/y;
+ x = xy / y;
+ z = yz / y;
}
} else { // elements[2][2] is the largest diagonal term so base result on this
- if (zz< epsilon) {
+ if (zz < epsilon) {
x = 0.7071;
y = 0.7071;
z = 0;
} else {
z = Math::sqrt(zz);
- x = xz/z;
- y = yz/z;
+ x = xz / z;
+ y = yz / z;
}
}
- r_axis=Vector3(x,y,z);
- r_angle=angle;
+ r_axis = Vector3(x, y, z);
+ r_angle = angle;
return;
}
// as we have reached here there are no singularities so we can handle normally
- real_t s = Math::sqrt((elements[1][2] - elements[2][1])*(elements[1][2] - elements[2][1])
- +(elements[2][0] - elements[0][2])*(elements[2][0] - elements[0][2])
- +(elements[0][1] - elements[1][0])*(elements[0][1] - elements[1][0])); // s=|axis||sin(angle)|, used to normalise
+ real_t s = Math::sqrt((elements[1][2] - elements[2][1]) * (elements[1][2] - elements[2][1]) + (elements[2][0] - elements[0][2]) * (elements[2][0] - elements[0][2]) + (elements[0][1] - elements[1][0]) * (elements[0][1] - elements[1][0])); // s=|axis||sin(angle)|, used to normalise
- angle = Math::acos(( elements[0][0] + elements[1][1] + elements[2][2] - 1)/2);
+ angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2);
if (angle < 0) s = -s;
- x = (elements[2][1] - elements[1][2])/s;
- y = (elements[0][2] - elements[2][0])/s;
- z = (elements[1][0] - elements[0][1])/s;
+ x = (elements[2][1] - elements[1][2]) / s;
+ y = (elements[0][2] - elements[2][0]) / s;
+ z = (elements[1][0] - elements[0][1]) / s;
- r_axis=Vector3(x,y,z);
- r_angle=angle;
+ r_axis = Vector3(x, y, z);
+ r_angle = angle;
}
-Basis::Basis(const Vector3& p_euler) {
-
- set_euler( p_euler );
+Basis::Basis(const Vector3 &p_euler) {
+ set_euler(p_euler);
}
-Basis::Basis(const Quat& p_quat) {
+Basis::Basis(const Quat &p_quat) {
real_t d = p_quat.length_squared();
real_t s = 2.0 / d;
- real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
- real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
- real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
- real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
- set( 1.0 - (yy + zz), xy - wz, xz + wy,
- xy + wz, 1.0 - (xx + zz), yz - wx,
- xz - wy, yz + wx, 1.0 - (xx + yy)) ;
-
+ real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
+ real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
+ real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
+ real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
+ set(1.0 - (yy + zz), xy - wz, xz + wy,
+ xy + wz, 1.0 - (xx + zz), yz - wx,
+ xz - wy, yz + wx, 1.0 - (xx + yy));
}
-Basis::Basis(const Vector3& p_axis, real_t p_phi) {
+Basis::Basis(const Vector3 &p_axis, real_t p_phi) {
// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
- Vector3 axis_sq(p_axis.x*p_axis.x,p_axis.y*p_axis.y,p_axis.z*p_axis.z);
-
- real_t cosine= Math::cos(p_phi);
- real_t sine= Math::sin(p_phi);
+ Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
- elements[0][0] = axis_sq.x + cosine * ( 1.0 - axis_sq.x );
- elements[0][1] = p_axis.x * p_axis.y * ( 1.0 - cosine ) - p_axis.z * sine;
- elements[0][2] = p_axis.z * p_axis.x * ( 1.0 - cosine ) + p_axis.y * sine;
+ real_t cosine = Math::cos(p_phi);
+ real_t sine = Math::sin(p_phi);
- elements[1][0] = p_axis.x * p_axis.y * ( 1.0 - cosine ) + p_axis.z * sine;
- elements[1][1] = axis_sq.y + cosine * ( 1.0 - axis_sq.y );
- elements[1][2] = p_axis.y * p_axis.z * ( 1.0 - cosine ) - p_axis.x * sine;
+ elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x);
+ elements[0][1] = p_axis.x * p_axis.y * (1.0 - cosine) - p_axis.z * sine;
+ elements[0][2] = p_axis.z * p_axis.x * (1.0 - cosine) + p_axis.y * sine;
- elements[2][0] = p_axis.z * p_axis.x * ( 1.0 - cosine ) - p_axis.y * sine;
- elements[2][1] = p_axis.y * p_axis.z * ( 1.0 - cosine ) + p_axis.x * sine;
- elements[2][2] = axis_sq.z + cosine * ( 1.0 - axis_sq.z );
+ elements[1][0] = p_axis.x * p_axis.y * (1.0 - cosine) + p_axis.z * sine;
+ elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y);
+ elements[1][2] = p_axis.y * p_axis.z * (1.0 - cosine) - p_axis.x * sine;
+ elements[2][0] = p_axis.z * p_axis.x * (1.0 - cosine) - p_axis.y * sine;
+ elements[2][1] = p_axis.y * p_axis.z * (1.0 - cosine) + p_axis.x * sine;
+ elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z);
}
-
diff --git a/core/math/matrix3.h b/core/math/matrix3.h
index abce1ee45d..0240bc8610 100644
--- a/core/math/matrix3.h
+++ b/core/math/matrix3.h
@@ -39,14 +39,13 @@
*/
class Basis {
public:
-
Vector3 elements[3];
- _FORCE_INLINE_ const Vector3& operator[](int axis) const {
+ _FORCE_INLINE_ const Vector3 &operator[](int axis) const {
return elements[axis];
}
- _FORCE_INLINE_ Vector3& operator[](int axis) {
+ _FORCE_INLINE_ Vector3 &operator[](int axis) {
return elements[axis];
}
@@ -59,57 +58,57 @@ public:
_FORCE_INLINE_ real_t determinant() const;
- void from_z(const Vector3& p_z);
+ void from_z(const Vector3 &p_z);
_FORCE_INLINE_ Vector3 get_axis(int p_axis) const {
// get actual basis axis (elements is transposed for performance)
- return Vector3( elements[0][p_axis], elements[1][p_axis], elements[2][p_axis] );
+ return Vector3(elements[0][p_axis], elements[1][p_axis], elements[2][p_axis]);
}
- _FORCE_INLINE_ void set_axis(int p_axis, const Vector3& p_value) {
+ _FORCE_INLINE_ void set_axis(int p_axis, const Vector3 &p_value) {
// get actual basis axis (elements is transposed for performance)
- elements[0][p_axis]=p_value.x;
- elements[1][p_axis]=p_value.y;
- elements[2][p_axis]=p_value.z;
+ elements[0][p_axis] = p_value.x;
+ elements[1][p_axis] = p_value.y;
+ elements[2][p_axis] = p_value.z;
}
- void rotate(const Vector3& p_axis, real_t p_phi);
- Basis rotated(const Vector3& p_axis, real_t p_phi) const;
+ void rotate(const Vector3 &p_axis, real_t p_phi);
+ Basis rotated(const Vector3 &p_axis, real_t p_phi) const;
- void rotate(const Vector3& p_euler);
- Basis rotated(const Vector3& p_euler) const;
+ void rotate(const Vector3 &p_euler);
+ Basis rotated(const Vector3 &p_euler) const;
Vector3 get_rotation() const;
- void scale( const Vector3& p_scale );
- Basis scaled( const Vector3& p_scale ) const;
+ void scale(const Vector3 &p_scale);
+ Basis scaled(const Vector3 &p_scale) const;
Vector3 get_scale() const;
Vector3 get_euler() const;
- void set_euler(const Vector3& p_euler);
+ void set_euler(const Vector3 &p_euler);
// transposed dot products
- _FORCE_INLINE_ real_t tdotx(const Vector3& v) const {
+ _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const {
return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
}
- _FORCE_INLINE_ real_t tdoty(const Vector3& v) const {
+ _FORCE_INLINE_ real_t tdoty(const Vector3 &v) const {
return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
}
- _FORCE_INLINE_ real_t tdotz(const Vector3& v) const {
+ _FORCE_INLINE_ real_t tdotz(const Vector3 &v) const {
return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
}
- bool isequal_approx(const Basis& a, const Basis& b) const;
+ bool isequal_approx(const Basis &a, const Basis &b) const;
- bool operator==(const Basis& p_matrix) const;
- bool operator!=(const Basis& p_matrix) const;
+ bool operator==(const Basis &p_matrix) const;
+ bool operator!=(const Basis &p_matrix) const;
- _FORCE_INLINE_ Vector3 xform(const Vector3& p_vector) const;
- _FORCE_INLINE_ Vector3 xform_inv(const Vector3& p_vector) const;
- _FORCE_INLINE_ void operator*=(const Basis& p_matrix);
- _FORCE_INLINE_ Basis operator*(const Basis& p_matrix) const;
- _FORCE_INLINE_ void operator+=(const Basis& p_matrix);
- _FORCE_INLINE_ Basis operator+(const Basis& p_matrix) const;
- _FORCE_INLINE_ void operator-=(const Basis& p_matrix);
- _FORCE_INLINE_ Basis operator-(const Basis& p_matrix) const;
+ _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
+ _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
+ _FORCE_INLINE_ void operator*=(const Basis &p_matrix);
+ _FORCE_INLINE_ Basis operator*(const Basis &p_matrix) const;
+ _FORCE_INLINE_ void operator+=(const Basis &p_matrix);
+ _FORCE_INLINE_ Basis operator+(const Basis &p_matrix) const;
+ _FORCE_INLINE_ void operator-=(const Basis &p_matrix);
+ _FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const;
_FORCE_INLINE_ void operator*=(real_t p_val);
_FORCE_INLINE_ Basis operator*(real_t p_val) const;
@@ -121,40 +120,39 @@ public:
operator String() const;
- void get_axis_and_angle(Vector3 &r_axis,real_t& r_angle) const;
+ void get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const;
/* create / set */
-
_FORCE_INLINE_ void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
- elements[0][0]=xx;
- elements[0][1]=xy;
- elements[0][2]=xz;
- elements[1][0]=yx;
- elements[1][1]=yy;
- elements[1][2]=yz;
- elements[2][0]=zx;
- elements[2][1]=zy;
- elements[2][2]=zz;
+ elements[0][0] = xx;
+ elements[0][1] = xy;
+ elements[0][2] = xz;
+ elements[1][0] = yx;
+ elements[1][1] = yy;
+ elements[1][2] = yz;
+ elements[2][0] = zx;
+ elements[2][1] = zy;
+ elements[2][2] = zz;
}
_FORCE_INLINE_ Vector3 get_column(int i) const {
- return Vector3(elements[0][i],elements[1][i],elements[2][i]);
+ return Vector3(elements[0][i], elements[1][i], elements[2][i]);
}
_FORCE_INLINE_ Vector3 get_row(int i) const {
- return Vector3(elements[i][0],elements[i][1],elements[i][2]);
+ return Vector3(elements[i][0], elements[i][1], elements[i][2]);
}
_FORCE_INLINE_ Vector3 get_main_diagonal() const {
- return Vector3(elements[0][0],elements[1][1],elements[2][2]);
+ return Vector3(elements[0][0], elements[1][1], elements[2][2]);
}
- _FORCE_INLINE_ void set_row(int i, const Vector3& p_row) {
- elements[i][0]=p_row.x;
- elements[i][1]=p_row.y;
- elements[i][2]=p_row.z;
+ _FORCE_INLINE_ void set_row(int i, const Vector3 &p_row) {
+ elements[i][0] = p_row.x;
+ elements[i][1] = p_row.y;
+ elements[i][2] = p_row.z;
}
_FORCE_INLINE_ void set_zero() {
@@ -163,18 +161,17 @@ public:
elements[2].zero();
}
- _FORCE_INLINE_ Basis transpose_xform(const Basis& m) const
- {
+ _FORCE_INLINE_ Basis transpose_xform(const Basis &m) const {
return Basis(
- elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
- elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
- elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
- elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
- elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
- elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
- elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
- elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
- elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z);
+ elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
+ elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
+ elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
+ elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
+ elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
+ elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
+ elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
+ elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
+ elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z);
}
Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
@@ -189,75 +186,69 @@ public:
operator Quat() const;
- Basis(const Quat& p_quat); // euler
- Basis(const Vector3& p_euler); // euler
- Basis(const Vector3& p_axis, real_t p_phi);
+ Basis(const Quat &p_quat); // euler
+ Basis(const Vector3 &p_euler); // euler
+ Basis(const Vector3 &p_axis, real_t p_phi);
- _FORCE_INLINE_ Basis(const Vector3& row0, const Vector3& row1, const Vector3& row2)
- {
- elements[0]=row0;
- elements[1]=row1;
- elements[2]=row2;
+ _FORCE_INLINE_ Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) {
+ elements[0] = row0;
+ elements[1] = row1;
+ elements[2] = row2;
}
_FORCE_INLINE_ Basis() {
- elements[0][0]=1;
- elements[0][1]=0;
- elements[0][2]=0;
- elements[1][0]=0;
- elements[1][1]=1;
- elements[1][2]=0;
- elements[2][0]=0;
- elements[2][1]=0;
- elements[2][2]=1;
+ elements[0][0] = 1;
+ elements[0][1] = 0;
+ elements[0][2] = 0;
+ elements[1][0] = 0;
+ elements[1][1] = 1;
+ elements[1][2] = 0;
+ elements[2][0] = 0;
+ elements[2][1] = 0;
+ elements[2][2] = 1;
}
-
-
};
-_FORCE_INLINE_ void Basis::operator*=(const Basis& p_matrix) {
+_FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) {
set(
- p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
- p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
- p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
-
+ p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
+ p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
+ p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
}
-_FORCE_INLINE_ Basis Basis::operator*(const Basis& p_matrix) const {
+_FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const {
return Basis(
- p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
- p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
- p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]) );
-
+ p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
+ p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
+ p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
}
-
-_FORCE_INLINE_ void Basis::operator+=(const Basis& p_matrix) {
+_FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) {
elements[0] += p_matrix.elements[0];
elements[1] += p_matrix.elements[1];
elements[2] += p_matrix.elements[2];
}
-_FORCE_INLINE_ Basis Basis::operator+(const Basis& p_matrix) const {
-
+_FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const {
+
Basis ret(*this);
ret += p_matrix;
return ret;
}
-_FORCE_INLINE_ void Basis::operator-=(const Basis& p_matrix) {
+_FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) {
elements[0] -= p_matrix.elements[0];
elements[1] -= p_matrix.elements[1];
elements[2] -= p_matrix.elements[2];
}
-_FORCE_INLINE_ Basis Basis::operator-(const Basis& p_matrix) const {
-
+_FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const {
+
Basis ret(*this);
ret -= p_matrix;
return ret;
@@ -265,40 +256,38 @@ _FORCE_INLINE_ Basis Basis::operator-(const Basis& p_matrix) const {
_FORCE_INLINE_ void Basis::operator*=(real_t p_val) {
- elements[0]*=p_val;
- elements[1]*=p_val;
- elements[2]*=p_val;
+ elements[0] *= p_val;
+ elements[1] *= p_val;
+ elements[2] *= p_val;
}
_FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const {
- Basis ret(*this);
- ret *= p_val;
- return ret;
+ Basis ret(*this);
+ ret *= p_val;
+ return ret;
}
-Vector3 Basis::xform(const Vector3& p_vector) const {
+Vector3 Basis::xform(const Vector3 &p_vector) const {
return Vector3(
- elements[0].dot(p_vector),
- elements[1].dot(p_vector),
- elements[2].dot(p_vector)
- );
+ elements[0].dot(p_vector),
+ elements[1].dot(p_vector),
+ elements[2].dot(p_vector));
}
-Vector3 Basis::xform_inv(const Vector3& p_vector) const {
+Vector3 Basis::xform_inv(const Vector3 &p_vector) const {
return Vector3(
- (elements[0][0]*p_vector.x ) + ( elements[1][0]*p_vector.y ) + ( elements[2][0]*p_vector.z ),
- (elements[0][1]*p_vector.x ) + ( elements[1][1]*p_vector.y ) + ( elements[2][1]*p_vector.z ),
- (elements[0][2]*p_vector.x ) + ( elements[1][2]*p_vector.y ) + ( elements[2][2]*p_vector.z )
- );
+ (elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z),
+ (elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z),
+ (elements[0][2] * p_vector.x) + (elements[1][2] * p_vector.y) + (elements[2][2] * p_vector.z));
}
real_t Basis::determinant() const {
- return elements[0][0]*(elements[1][1]*elements[2][2] - elements[2][1]*elements[1][2]) -
- elements[1][0]*(elements[0][1]*elements[2][2] - elements[2][1]*elements[0][2]) +
- elements[2][0]*(elements[0][1]*elements[1][2] - elements[1][1]*elements[0][2]);
+ return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) -
+ elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) +
+ elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]);
}
#endif
diff --git a/core/math/octree.h b/core/math/octree.h
index e566df6a4f..06c5791b11 100644
--- a/core/math/octree.h
+++ b/core/math/octree.h
@@ -29,12 +29,12 @@
#ifndef OCTREE_H
#define OCTREE_H
-#include "vector3.h"
-#include "rect3.h"
#include "list.h"
-#include "variant.h"
#include "map.h"
#include "print_string.h"
+#include "rect3.h"
+#include "variant.h"
+#include "vector3.h"
/**
@author Juan Linietsky <reduzio@gmail.com>
@@ -45,18 +45,17 @@ typedef uint32_t OctreeElementID;
#define OCTREE_ELEMENT_INVALID_ID 0
#define OCTREE_SIZE_LIMIT 1e15
-template<class T,bool use_pairs=false,class AL=DefaultAllocator>
+template <class T, bool use_pairs = false, class AL = DefaultAllocator>
class Octree {
public:
-
- typedef void* (*PairCallback)(void*,OctreeElementID, T*,int,OctreeElementID, T*,int);
- typedef void (*UnpairCallback)(void*,OctreeElementID, T*,int,OctreeElementID, T*,int,void*);
+ typedef void *(*PairCallback)(void *, OctreeElementID, T *, int, OctreeElementID, T *, int);
+ typedef void (*UnpairCallback)(void *, OctreeElementID, T *, int, OctreeElementID, T *, int, void *);
private:
enum {
- NEG=0,
- POS=1,
+ NEG = 0,
+ POS = 1,
};
enum {
@@ -70,7 +69,6 @@ private:
OCTANT_PX_PY_PZ
};
-
struct PairKey {
union {
@@ -81,21 +79,21 @@ private:
uint64_t key;
};
- _FORCE_INLINE_ bool operator<(const PairKey& p_pair) const {
+ _FORCE_INLINE_ bool operator<(const PairKey &p_pair) const {
- return key<p_pair.key;
+ return key < p_pair.key;
}
- _FORCE_INLINE_ PairKey( OctreeElementID p_A, OctreeElementID p_B) {
+ _FORCE_INLINE_ PairKey(OctreeElementID p_A, OctreeElementID p_B) {
- if (p_A<p_B) {
+ if (p_A < p_B) {
- A=p_A;
- B=p_B;
+ A = p_A;
+ B = p_B;
} else {
- B=p_A;
- A=p_B;
+ B = p_A;
+ A = p_B;
}
}
@@ -116,16 +114,16 @@ private:
int children_count; // cache for amount of childrens (fast check for removal)
int parent_index; // cache for parent index (fast check for removal)
- List<Element*,AL> pairable_elements;
- List<Element*,AL> elements;
+ List<Element *, AL> pairable_elements;
+ List<Element *, AL> elements;
Octant() {
- children_count=0;
- parent_index=-1;
- last_pass=0;
- parent=NULL;
- for (int i=0;i<8;i++)
- children[i]=NULL;
+ children_count = 0;
+ parent_index = -1;
+ last_pass = 0;
+ parent = NULL;
+ for (int i = 0; i < 8; i++)
+ children[i] = NULL;
}
~Octant() {
@@ -137,7 +135,6 @@ private:
}
};
-
struct PairData;
struct Element {
@@ -157,28 +154,36 @@ private:
Rect3 aabb;
Rect3 container_aabb;
- List<PairData*,AL> pair_list;
+ List<PairData *, AL> pair_list;
struct OctantOwner {
Octant *octant;
- typename List<Element*,AL>::Element *E;
+ typename List<Element *, AL>::Element *E;
}; // an element can be in max 8 octants
- List<OctantOwner,AL> octant_owners;
-
-
- Element() { last_pass=0; _id=0; pairable=false; subindex=0; userdata=0; octree=0; pairable_mask=0; pairable_type=0; common_parent=NULL; }
+ List<OctantOwner, AL> octant_owners;
+
+ Element() {
+ last_pass = 0;
+ _id = 0;
+ pairable = false;
+ subindex = 0;
+ userdata = 0;
+ octree = 0;
+ pairable_mask = 0;
+ pairable_type = 0;
+ common_parent = NULL;
+ }
};
-
struct PairData {
int refcount;
bool intersect;
- Element *A,*B;
+ Element *A, *B;
void *ud;
- typename List<PairData*,AL>::Element *eA,*eB;
+ typename List<PairData *, AL>::Element *eA, *eB;
};
typedef Map<OctreeElementID, Element, Comparator<OctreeElementID>, AL> ElementMap;
@@ -199,58 +204,52 @@ private:
int octant_count;
int pair_count;
-
-
_FORCE_INLINE_ void _pair_check(PairData *p_pair) {
- bool intersect=p_pair->A->aabb.intersects_inclusive( p_pair->B->aabb );
+ bool intersect = p_pair->A->aabb.intersects_inclusive(p_pair->B->aabb);
- if (intersect!=p_pair->intersect) {
+ if (intersect != p_pair->intersect) {
if (intersect) {
if (pair_callback) {
- p_pair->ud=pair_callback(pair_callback_userdata,p_pair->A->_id, p_pair->A->userdata,p_pair->A->subindex,p_pair->B->_id, p_pair->B->userdata,p_pair->B->subindex);
-
+ p_pair->ud = pair_callback(pair_callback_userdata, p_pair->A->_id, p_pair->A->userdata, p_pair->A->subindex, p_pair->B->_id, p_pair->B->userdata, p_pair->B->subindex);
}
pair_count++;
} else {
-
if (unpair_callback) {
- unpair_callback(pair_callback_userdata,p_pair->A->_id, p_pair->A->userdata,p_pair->A->subindex,p_pair->B->_id, p_pair->B->userdata,p_pair->B->subindex,p_pair->ud);
+ unpair_callback(pair_callback_userdata, p_pair->A->_id, p_pair->A->userdata, p_pair->A->subindex, p_pair->B->_id, p_pair->B->userdata, p_pair->B->subindex, p_pair->ud);
}
pair_count--;
-
}
- p_pair->intersect=intersect;
-
+ p_pair->intersect = intersect;
}
}
- _FORCE_INLINE_ void _pair_reference(Element* p_A,Element* p_B) {
+ _FORCE_INLINE_ void _pair_reference(Element *p_A, Element *p_B) {
- if (p_A==p_B || (p_A->userdata==p_B->userdata && p_A->userdata))
+ if (p_A == p_B || (p_A->userdata == p_B->userdata && p_A->userdata))
return;
- if ( !(p_A->pairable_type&p_B->pairable_mask) &&
- !(p_B->pairable_type&p_A->pairable_mask) )
+ if (!(p_A->pairable_type & p_B->pairable_mask) &&
+ !(p_B->pairable_type & p_A->pairable_mask))
return; // none can pair with none
PairKey key(p_A->_id, p_B->_id);
- typename PairMap::Element *E=pair_map.find(key);
+ typename PairMap::Element *E = pair_map.find(key);
if (!E) {
PairData pdata;
- pdata.refcount=1;
- pdata.A=p_A;
- pdata.B=p_B;
- pdata.intersect=false;
- E=pair_map.insert(key,pdata);
- E->get().eA=p_A->pair_list.push_back(&E->get());
- E->get().eB=p_B->pair_list.push_back(&E->get());
+ pdata.refcount = 1;
+ pdata.A = p_A;
+ pdata.B = p_B;
+ pdata.intersect = false;
+ E = pair_map.insert(key, pdata);
+ E->get().eA = p_A->pair_list.push_back(&E->get());
+ E->get().eB = p_B->pair_list.push_back(&E->get());
/*
if (pair_callback)
@@ -260,189 +259,178 @@ private:
E->get().refcount++;
}
-
}
- _FORCE_INLINE_ void _pair_unreference(Element* p_A,Element* p_B) {
+ _FORCE_INLINE_ void _pair_unreference(Element *p_A, Element *p_B) {
- if (p_A==p_B)
+ if (p_A == p_B)
return;
PairKey key(p_A->_id, p_B->_id);
- typename PairMap::Element *E=pair_map.find(key);
+ typename PairMap::Element *E = pair_map.find(key);
if (!E) {
return; // no pair
}
E->get().refcount--;
-
- if (E->get().refcount==0) {
+ if (E->get().refcount == 0) {
// bye pair
if (E->get().intersect) {
if (unpair_callback) {
- unpair_callback(pair_callback_userdata,p_A->_id, p_A->userdata,p_A->subindex,p_B->_id, p_B->userdata,p_B->subindex,E->get().ud);
+ unpair_callback(pair_callback_userdata, p_A->_id, p_A->userdata, p_A->subindex, p_B->_id, p_B->userdata, p_B->subindex, E->get().ud);
}
pair_count--;
}
- if (p_A==E->get().B) {
+ if (p_A == E->get().B) {
//may be reaching inverted
- SWAP(p_A,p_B);
+ SWAP(p_A, p_B);
}
- p_A->pair_list.erase( E->get().eA );
- p_B->pair_list.erase( E->get().eB );
+ p_A->pair_list.erase(E->get().eA);
+ p_B->pair_list.erase(E->get().eB);
pair_map.erase(E);
}
-
}
_FORCE_INLINE_ void _element_check_pairs(Element *p_element) {
- typename List<PairData*,AL>::Element *E=p_element->pair_list.front();
- while(E) {
+ typename List<PairData *, AL>::Element *E = p_element->pair_list.front();
+ while (E) {
- _pair_check( E->get() );
- E=E->next();
+ _pair_check(E->get());
+ E = E->next();
}
-
}
_FORCE_INLINE_ void _optimize() {
+ while (root && root->children_count < 2 && !root->elements.size() && !(use_pairs && root->pairable_elements.size())) {
- while(root && root->children_count<2 && !root->elements.size() && !(use_pairs && root->pairable_elements.size())) {
-
-
- Octant *new_root=NULL;
- if (root->children_count==1) {
+ Octant *new_root = NULL;
+ if (root->children_count == 1) {
- for(int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
if (root->children[i]) {
- new_root=root->children[i];
- root->children[i]=NULL;
+ new_root = root->children[i];
+ root->children[i] = NULL;
break;
}
}
ERR_FAIL_COND(!new_root);
- new_root->parent=NULL;
- new_root->parent_index=-1;
+ new_root->parent = NULL;
+ new_root->parent_index = -1;
}
- memdelete_allocator<Octant,AL>( root );
+ memdelete_allocator<Octant, AL>(root);
octant_count--;
- root=new_root;
-
+ root = new_root;
}
}
-
- void _insert_element(Element *p_element,Octant *p_octant);
- void _ensure_valid_root(const Rect3& p_aabb);
- bool _remove_element_from_octant(Element *p_element,Octant *p_octant,Octant *p_limit=NULL);
+ void _insert_element(Element *p_element, Octant *p_octant);
+ void _ensure_valid_root(const Rect3 &p_aabb);
+ bool _remove_element_from_octant(Element *p_element, Octant *p_octant, Octant *p_limit = NULL);
void _remove_element(Element *p_element);
- void _pair_element(Element *p_element,Octant *p_octant);
- void _unpair_element(Element *p_element,Octant *p_octant);
-
+ void _pair_element(Element *p_element, Octant *p_octant);
+ void _unpair_element(Element *p_element, Octant *p_octant);
struct _CullConvexData {
- const Plane* planes;
+ const Plane *planes;
int plane_count;
- T** result_array;
+ T **result_array;
int *result_idx;
int result_max;
uint32_t mask;
};
- void _cull_convex(Octant *p_octant,_CullConvexData *p_cull);
- void _cull_AABB(Octant *p_octant,const Rect3& p_aabb, T** p_result_array,int *p_result_idx,int p_result_max,int *p_subindex_array,uint32_t p_mask);
- void _cull_segment(Octant *p_octant,const Vector3& p_from, const Vector3& p_to,T** p_result_array,int *p_result_idx,int p_result_max,int *p_subindex_array,uint32_t p_mask);
- void _cull_point(Octant *p_octant,const Vector3& p_point,T** p_result_array,int *p_result_idx,int p_result_max,int *p_subindex_array,uint32_t p_mask);
+ void _cull_convex(Octant *p_octant, _CullConvexData *p_cull);
+ void _cull_AABB(Octant *p_octant, const Rect3 &p_aabb, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask);
+ void _cull_segment(Octant *p_octant, const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask);
+ void _cull_point(Octant *p_octant, const Vector3 &p_point, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask);
void _remove_tree(Octant *p_octant) {
if (!p_octant)
return;
- for(int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
if (p_octant->children[i])
_remove_tree(p_octant->children[i]);
}
- memdelete_allocator<Octant,AL>(p_octant);
+ memdelete_allocator<Octant, AL>(p_octant);
}
-public:
- OctreeElementID create(T* p_userdata, const Rect3& p_aabb=Rect3(), int p_subindex=0, bool p_pairable=false,uint32_t p_pairable_type=0,uint32_t pairable_mask=1);
- void move(OctreeElementID p_id, const Rect3& p_aabb);
- void set_pairable(OctreeElementID p_id,bool p_pairable=false,uint32_t p_pairable_type=0,uint32_t pairable_mask=1);
+public:
+ OctreeElementID create(T *p_userdata, const Rect3 &p_aabb = Rect3(), int p_subindex = 0, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1);
+ void move(OctreeElementID p_id, const Rect3 &p_aabb);
+ void set_pairable(OctreeElementID p_id, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1);
void erase(OctreeElementID p_id);
bool is_pairable(OctreeElementID p_id) const;
T *get(OctreeElementID p_id) const;
int get_subindex(OctreeElementID p_id) const;
- int cull_convex(const Vector<Plane>& p_convex,T** p_result_array,int p_result_max,uint32_t p_mask=0xFFFFFFFF);
- int cull_AABB(const Rect3& p_aabb,T** p_result_array,int p_result_max,int *p_subindex_array=NULL,uint32_t p_mask=0xFFFFFFFF);
- int cull_segment(const Vector3& p_from, const Vector3& p_to,T** p_result_array,int p_result_max,int *p_subindex_array=NULL,uint32_t p_mask=0xFFFFFFFF);
+ int cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
+ int cull_AABB(const Rect3 &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array = NULL, uint32_t p_mask = 0xFFFFFFFF);
+ int cull_segment(const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int p_result_max, int *p_subindex_array = NULL, uint32_t p_mask = 0xFFFFFFFF);
- int cull_point(const Vector3& p_point,T** p_result_array,int p_result_max,int *p_subindex_array=NULL,uint32_t p_mask=0xFFFFFFFF);
+ int cull_point(const Vector3 &p_point, T **p_result_array, int p_result_max, int *p_subindex_array = NULL, uint32_t p_mask = 0xFFFFFFFF);
- void set_pair_callback( PairCallback p_callback, void *p_userdata );
- void set_unpair_callback( UnpairCallback p_callback, void *p_userdata );
+ void set_pair_callback(PairCallback p_callback, void *p_userdata);
+ void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
int get_octant_count() const { return octant_count; }
int get_pair_count() const { return pair_count; }
- Octree(real_t p_unit_size=1.0);
+ Octree(real_t p_unit_size = 1.0);
~Octree() { _remove_tree(root); }
};
-
/* PRIVATE FUNCTIONS */
-template<class T,bool use_pairs,class AL>
-T *Octree<T,use_pairs,AL>::get(OctreeElementID p_id) const {
+template <class T, bool use_pairs, class AL>
+T *Octree<T, use_pairs, AL>::get(OctreeElementID p_id) const {
const typename ElementMap::Element *E = element_map.find(p_id);
- ERR_FAIL_COND_V(!E,NULL);
+ ERR_FAIL_COND_V(!E, NULL);
return E->get().userdata;
}
-
-template<class T,bool use_pairs,class AL>
-bool Octree<T,use_pairs,AL>::is_pairable(OctreeElementID p_id) const {
+template <class T, bool use_pairs, class AL>
+bool Octree<T, use_pairs, AL>::is_pairable(OctreeElementID p_id) const {
const typename ElementMap::Element *E = element_map.find(p_id);
- ERR_FAIL_COND_V(!E,false);
+ ERR_FAIL_COND_V(!E, false);
return E->get().pairable;
}
-template<class T,bool use_pairs,class AL>
-int Octree<T,use_pairs,AL>::get_subindex(OctreeElementID p_id) const {
+template <class T, bool use_pairs, class AL>
+int Octree<T, use_pairs, AL>::get_subindex(OctreeElementID p_id) const {
const typename ElementMap::Element *E = element_map.find(p_id);
- ERR_FAIL_COND_V(!E,-1);
+ ERR_FAIL_COND_V(!E, -1);
return E->get().subindex;
}
#define OCTREE_DIVISOR 4
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::_insert_element(Element *p_element,Octant *p_octant) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_insert_element(Element *p_element, Octant *p_octant) {
real_t element_size = p_element->aabb.get_longest_axis_size() * 1.01; // avoid precision issues
- if (p_octant->aabb.size.x/OCTREE_DIVISOR < element_size) {
- //if (p_octant->aabb.size.x*0.5 < element_size) {
+ if (p_octant->aabb.size.x / OCTREE_DIVISOR < element_size) {
+ //if (p_octant->aabb.size.x*0.5 < element_size) {
/* at smallest possible size for the element */
typename Element::OctantOwner owner;
- owner.octant=p_octant;
+ owner.octant = p_octant;
if (use_pairs && p_element->pairable) {
@@ -454,426 +442,409 @@ void Octree<T,use_pairs,AL>::_insert_element(Element *p_element,Octant *p_octant
owner.E = p_octant->elements.back();
}
- p_element->octant_owners.push_back( owner );
+ p_element->octant_owners.push_back(owner);
- if (p_element->common_parent==NULL) {
- p_element->common_parent=p_octant;
- p_element->container_aabb=p_octant->aabb;
+ if (p_element->common_parent == NULL) {
+ p_element->common_parent = p_octant;
+ p_element->container_aabb = p_octant->aabb;
} else {
p_element->container_aabb.merge_with(p_octant->aabb);
}
-
- if (use_pairs && p_octant->children_count>0) {
+ if (use_pairs && p_octant->children_count > 0) {
pass++; //elements below this only get ONE reference added
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
if (p_octant->children[i]) {
- _pair_element(p_element,p_octant->children[i]);
+ _pair_element(p_element, p_octant->children[i]);
}
}
}
} else {
/* not big enough, send it to subitems */
- int splits=0;
- bool candidate=p_element->common_parent==NULL;
+ int splits = 0;
+ bool candidate = p_element->common_parent == NULL;
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
if (p_octant->children[i]) {
/* element exists, go straight to it */
- if (p_octant->children[i]->aabb.intersects_inclusive( p_element->aabb ) ) {
- _insert_element( p_element, p_octant->children[i] );
+ if (p_octant->children[i]->aabb.intersects_inclusive(p_element->aabb)) {
+ _insert_element(p_element, p_octant->children[i]);
splits++;
}
} else {
/* check againt AABB where child should be */
- Rect3 aabb=p_octant->aabb;
- aabb.size*=0.5;
+ Rect3 aabb = p_octant->aabb;
+ aabb.size *= 0.5;
- if (i&1)
- aabb.pos.x+=aabb.size.x;
- if (i&2)
- aabb.pos.y+=aabb.size.y;
- if (i&4)
- aabb.pos.z+=aabb.size.z;
+ if (i & 1)
+ aabb.pos.x += aabb.size.x;
+ if (i & 2)
+ aabb.pos.y += aabb.size.y;
+ if (i & 4)
+ aabb.pos.z += aabb.size.z;
- if (aabb.intersects_inclusive( p_element->aabb) ) {
+ if (aabb.intersects_inclusive(p_element->aabb)) {
/* if actually intersects, create the child */
- Octant *child = memnew_allocator( Octant, AL );
- p_octant->children[i]=child;
- child->parent=p_octant;
- child->parent_index=i;
+ Octant *child = memnew_allocator(Octant, AL);
+ p_octant->children[i] = child;
+ child->parent = p_octant;
+ child->parent_index = i;
- child->aabb=aabb;
+ child->aabb = aabb;
p_octant->children_count++;
- _insert_element( p_element, child );
+ _insert_element(p_element, child);
octant_count++;
splits++;
-
}
}
-
}
- if (candidate && splits>1) {
+ if (candidate && splits > 1) {
- p_element->common_parent=p_octant;
+ p_element->common_parent = p_octant;
}
-
}
if (use_pairs) {
- typename List<Element*,AL>::Element *E=p_octant->pairable_elements.front();
+ typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
- while(E) {
- _pair_reference( p_element,E->get() );
- E=E->next();
+ while (E) {
+ _pair_reference(p_element, E->get());
+ E = E->next();
}
if (p_element->pairable) {
// and always test non-pairable if element is pairable
- E=p_octant->elements.front();
- while(E) {
- _pair_reference( p_element,E->get() );
- E=E->next();
+ E = p_octant->elements.front();
+ while (E) {
+ _pair_reference(p_element, E->get());
+ E = E->next();
}
}
}
-
-
}
-
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::_ensure_valid_root(const Rect3& p_aabb) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_ensure_valid_root(const Rect3 &p_aabb) {
if (!root) {
// octre is empty
- Rect3 base( Vector3(), Vector3(1.0,1.0,1.0) * unit_size);
+ Rect3 base(Vector3(), Vector3(1.0, 1.0, 1.0) * unit_size);
- while ( !base.encloses(p_aabb) ) {
+ while (!base.encloses(p_aabb)) {
- if ( ABS(base.pos.x+base.size.x) <= ABS(base.pos.x) ) {
+ if (ABS(base.pos.x + base.size.x) <= ABS(base.pos.x)) {
/* grow towards positive */
- base.size*=2.0;
+ base.size *= 2.0;
} else {
- base.pos-=base.size;
- base.size*=2.0;
+ base.pos -= base.size;
+ base.size *= 2.0;
}
}
- root = memnew_allocator( Octant, AL );
+ root = memnew_allocator(Octant, AL);
- root->parent=NULL;
- root->parent_index=-1;
- root->aabb=base;
+ root->parent = NULL;
+ root->parent_index = -1;
+ root->aabb = base;
octant_count++;
-
} else {
- Rect3 base=root->aabb;
+ Rect3 base = root->aabb;
- while( !base.encloses( p_aabb ) ) {
+ while (!base.encloses(p_aabb)) {
if (base.size.x > OCTREE_SIZE_LIMIT) {
ERR_EXPLAIN("Octree upper size limit reeached, does the AABB supplied contain NAN?");
ERR_FAIL();
}
- Octant * gp = memnew_allocator( Octant, AL );
+ Octant *gp = memnew_allocator(Octant, AL);
octant_count++;
- root->parent=gp;
+ root->parent = gp;
- if ( ABS(base.pos.x+base.size.x) <= ABS(base.pos.x) ) {
+ if (ABS(base.pos.x + base.size.x) <= ABS(base.pos.x)) {
/* grow towards positive */
- base.size*=2.0;
- gp->aabb=base;
- gp->children[0]=root;
- root->parent_index=0;
+ base.size *= 2.0;
+ gp->aabb = base;
+ gp->children[0] = root;
+ root->parent_index = 0;
} else {
- base.pos-=base.size;
- base.size*=2.0;
- gp->aabb=base;
- gp->children[(1<<0)|(1<<1)|(1<<2)]=root; // add at all-positive
- root->parent_index=(1<<0)|(1<<1)|(1<<2);
+ base.pos -= base.size;
+ base.size *= 2.0;
+ gp->aabb = base;
+ gp->children[(1 << 0) | (1 << 1) | (1 << 2)] = root; // add at all-positive
+ root->parent_index = (1 << 0) | (1 << 1) | (1 << 2);
}
- gp->children_count=1;
- root=gp;
+ gp->children_count = 1;
+ root = gp;
}
}
}
-template<class T,bool use_pairs,class AL>
-bool Octree<T,use_pairs,AL>::_remove_element_from_octant(Element *p_element,Octant *p_octant,Octant *p_limit) {
+template <class T, bool use_pairs, class AL>
+bool Octree<T, use_pairs, AL>::_remove_element_from_octant(Element *p_element, Octant *p_octant, Octant *p_limit) {
- bool octant_removed=false;
+ bool octant_removed = false;
- while(true) {
+ while (true) {
// check all exit conditions
- if (p_octant==p_limit) // reached limit, nothing to erase, exit
+ if (p_octant == p_limit) // reached limit, nothing to erase, exit
return octant_removed;
- bool unpaired=false;
+ bool unpaired = false;
- if (use_pairs && p_octant->last_pass!=pass) {
+ if (use_pairs && p_octant->last_pass != pass) {
// check wether we should unpair stuff
// always test pairable
- typename List<Element*,AL>::Element *E=p_octant->pairable_elements.front();
- while(E) {
- _pair_unreference( p_element,E->get() );
- E=E->next();
+ typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
+ while (E) {
+ _pair_unreference(p_element, E->get());
+ E = E->next();
}
if (p_element->pairable) {
// and always test non-pairable if element is pairable
- E=p_octant->elements.front();
- while(E) {
- _pair_unreference( p_element,E->get() );
- E=E->next();
+ E = p_octant->elements.front();
+ while (E) {
+ _pair_unreference(p_element, E->get());
+ E = E->next();
}
}
- p_octant->last_pass=pass;
- unpaired=true;
+ p_octant->last_pass = pass;
+ unpaired = true;
}
- bool removed=false;
+ bool removed = false;
- Octant *parent=p_octant->parent;
+ Octant *parent = p_octant->parent;
- if (p_octant->children_count==0 && p_octant->elements.empty() && p_octant->pairable_elements.empty()) {
+ if (p_octant->children_count == 0 && p_octant->elements.empty() && p_octant->pairable_elements.empty()) {
// erase octant
- if (p_octant==root) { // won't have a parent, just erase
+ if (p_octant == root) { // won't have a parent, just erase
- root=NULL;
+ root = NULL;
} else {
- ERR_FAIL_INDEX_V(p_octant->parent_index,8,octant_removed);
+ ERR_FAIL_INDEX_V(p_octant->parent_index, 8, octant_removed);
- parent->children[ p_octant->parent_index ]=NULL;
+ parent->children[p_octant->parent_index] = NULL;
parent->children_count--;
}
- memdelete_allocator<Octant,AL>(p_octant);
+ memdelete_allocator<Octant, AL>(p_octant);
octant_count--;
- removed=true;
- octant_removed=true;
+ removed = true;
+ octant_removed = true;
}
if (!removed && !unpaired)
return octant_removed; // no reason to keep going up anymore! was already visited and was not removed
- p_octant=parent;
-
+ p_octant = parent;
}
return octant_removed;
}
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::_unpair_element(Element *p_element,Octant *p_octant) {
-
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_unpair_element(Element *p_element, Octant *p_octant) {
// always test pairable
- typename List<Element*,AL>::Element *E=p_octant->pairable_elements.front();
- while(E) {
- if (E->get()->last_pass!=pass) { // only remove ONE reference
- _pair_unreference( p_element,E->get() );
- E->get()->last_pass=pass;
+ typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
+ while (E) {
+ if (E->get()->last_pass != pass) { // only remove ONE reference
+ _pair_unreference(p_element, E->get());
+ E->get()->last_pass = pass;
}
- E=E->next();
+ E = E->next();
}
if (p_element->pairable) {
// and always test non-pairable if element is pairable
- E=p_octant->elements.front();
- while(E) {
- if (E->get()->last_pass!=pass) { // only remove ONE reference
- _pair_unreference( p_element,E->get() );
- E->get()->last_pass=pass;
+ E = p_octant->elements.front();
+ while (E) {
+ if (E->get()->last_pass != pass) { // only remove ONE reference
+ _pair_unreference(p_element, E->get());
+ E->get()->last_pass = pass;
}
- E=E->next();
+ E = E->next();
}
}
- p_octant->last_pass=pass;
+ p_octant->last_pass = pass;
- if (p_octant->children_count==0)
+ if (p_octant->children_count == 0)
return; // small optimization for leafs
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
if (p_octant->children[i])
- _unpair_element(p_element,p_octant->children[i]);
+ _unpair_element(p_element, p_octant->children[i]);
}
}
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::_pair_element(Element *p_element,Octant *p_octant) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_pair_element(Element *p_element, Octant *p_octant) {
// always test pairable
- typename List<Element*,AL>::Element *E=p_octant->pairable_elements.front();
+ typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
- while(E) {
+ while (E) {
- if (E->get()->last_pass!=pass) { // only get ONE reference
- _pair_reference( p_element,E->get() );
- E->get()->last_pass=pass;
+ if (E->get()->last_pass != pass) { // only get ONE reference
+ _pair_reference(p_element, E->get());
+ E->get()->last_pass = pass;
}
- E=E->next();
+ E = E->next();
}
if (p_element->pairable) {
// and always test non-pairable if element is pairable
- E=p_octant->elements.front();
- while(E) {
- if (E->get()->last_pass!=pass) { // only get ONE reference
- _pair_reference( p_element,E->get() );
- E->get()->last_pass=pass;
+ E = p_octant->elements.front();
+ while (E) {
+ if (E->get()->last_pass != pass) { // only get ONE reference
+ _pair_reference(p_element, E->get());
+ E->get()->last_pass = pass;
}
- E=E->next();
+ E = E->next();
}
}
- p_octant->last_pass=pass;
+ p_octant->last_pass = pass;
- if (p_octant->children_count==0)
+ if (p_octant->children_count == 0)
return; // small optimization for leafs
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
if (p_octant->children[i])
- _pair_element(p_element,p_octant->children[i]);
+ _pair_element(p_element, p_octant->children[i]);
}
}
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::_remove_element(Element *p_element) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_remove_element(Element *p_element) {
pass++; // will do a new pass for this
- typename List< typename Element::OctantOwner,AL >::Element *I=p_element->octant_owners.front();
-
+ typename List<typename Element::OctantOwner, AL>::Element *I = p_element->octant_owners.front();
/* FIRST remove going up normally */
- for(;I;I=I->next()) {
+ for (; I; I = I->next()) {
- Octant *o=I->get().octant;
+ Octant *o = I->get().octant;
if (!use_pairs) // small speedup
- o->elements.erase( I->get().E );
-
- _remove_element_from_octant( p_element, o );
+ o->elements.erase(I->get().E);
+ _remove_element_from_octant(p_element, o);
}
/* THEN remove going down */
- I=p_element->octant_owners.front();
+ I = p_element->octant_owners.front();
if (use_pairs) {
- for(;I;I=I->next()) {
+ for (; I; I = I->next()) {
- Octant *o=I->get().octant;
+ Octant *o = I->get().octant;
// erase children pairs, they are erased ONCE even if repeated
pass++;
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
if (o->children[i])
- _unpair_element(p_element,o->children[i]);
+ _unpair_element(p_element, o->children[i]);
}
if (p_element->pairable)
- o->pairable_elements.erase( I->get().E );
+ o->pairable_elements.erase(I->get().E);
else
- o->elements.erase( I->get().E );
-
+ o->elements.erase(I->get().E);
}
}
p_element->octant_owners.clear();
- if(use_pairs) {
+ if (use_pairs) {
- int remaining=p_element->pair_list.size();
+ int remaining = p_element->pair_list.size();
//p_element->pair_list.clear();
- ERR_FAIL_COND( remaining );
+ ERR_FAIL_COND(remaining);
}
-
}
-template<class T,bool use_pairs,class AL>
-OctreeElementID Octree<T,use_pairs,AL>::create(T* p_userdata, const Rect3& p_aabb, int p_subindex,bool p_pairable,uint32_t p_pairable_type,uint32_t p_pairable_mask) {
+template <class T, bool use_pairs, class AL>
+OctreeElementID Octree<T, use_pairs, AL>::create(T *p_userdata, const Rect3 &p_aabb, int p_subindex, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) {
- // check for AABB validity
+// check for AABB validity
#ifdef DEBUG_ENABLED
- ERR_FAIL_COND_V( p_aabb.pos.x > 1e15 || p_aabb.pos.x < -1e15, 0 );
- ERR_FAIL_COND_V( p_aabb.pos.y > 1e15 || p_aabb.pos.y < -1e15, 0 );
- ERR_FAIL_COND_V( p_aabb.pos.z > 1e15 || p_aabb.pos.z < -1e15, 0 );
- ERR_FAIL_COND_V( p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0, 0 );
- ERR_FAIL_COND_V( p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0, 0 );
- ERR_FAIL_COND_V( p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0, 0 );
- ERR_FAIL_COND_V( Math::is_nan(p_aabb.size.x) , 0 );
- ERR_FAIL_COND_V( Math::is_nan(p_aabb.size.y) , 0 );
- ERR_FAIL_COND_V( Math::is_nan(p_aabb.size.z) , 0 );
-
+ ERR_FAIL_COND_V(p_aabb.pos.x > 1e15 || p_aabb.pos.x < -1e15, 0);
+ ERR_FAIL_COND_V(p_aabb.pos.y > 1e15 || p_aabb.pos.y < -1e15, 0);
+ ERR_FAIL_COND_V(p_aabb.pos.z > 1e15 || p_aabb.pos.z < -1e15, 0);
+ ERR_FAIL_COND_V(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0, 0);
+ ERR_FAIL_COND_V(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0, 0);
+ ERR_FAIL_COND_V(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0, 0);
+ ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.x), 0);
+ ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.y), 0);
+ ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.z), 0);
#endif
typename ElementMap::Element *E = element_map.insert(last_element_id++,
- Element());
+ Element());
Element &e = E->get();
- e.aabb=p_aabb;
- e.userdata=p_userdata;
- e.subindex=p_subindex;
- e.last_pass=0;
- e.octree=this;
- e.pairable=p_pairable;
- e.pairable_type=p_pairable_type;
- e.pairable_mask=p_pairable_mask;
- e._id=last_element_id-1;
+ e.aabb = p_aabb;
+ e.userdata = p_userdata;
+ e.subindex = p_subindex;
+ e.last_pass = 0;
+ e.octree = this;
+ e.pairable = p_pairable;
+ e.pairable_type = p_pairable_type;
+ e.pairable_mask = p_pairable_mask;
+ e._id = last_element_id - 1;
if (!e.aabb.has_no_surface()) {
_ensure_valid_root(p_aabb);
- _insert_element(&e,root);
+ _insert_element(&e, root);
if (use_pairs)
_element_check_pairs(&e);
}
- return last_element_id-1;
+ return last_element_id - 1;
}
-
-
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::move(OctreeElementID p_id, const Rect3& p_aabb) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const Rect3 &p_aabb) {
#ifdef DEBUG_ENABLED
// check for AABB validity
- ERR_FAIL_COND( p_aabb.pos.x > 1e15 || p_aabb.pos.x < -1e15 );
- ERR_FAIL_COND( p_aabb.pos.y > 1e15 || p_aabb.pos.y < -1e15 );
- ERR_FAIL_COND( p_aabb.pos.z > 1e15 || p_aabb.pos.z < -1e15 );
- ERR_FAIL_COND( p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0 );
- ERR_FAIL_COND( p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0 );
- ERR_FAIL_COND( p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0 );
- ERR_FAIL_COND( Math::is_nan(p_aabb.size.x) );
- ERR_FAIL_COND( Math::is_nan(p_aabb.size.y) );
- ERR_FAIL_COND( Math::is_nan(p_aabb.size.z) );
+ ERR_FAIL_COND(p_aabb.pos.x > 1e15 || p_aabb.pos.x < -1e15);
+ ERR_FAIL_COND(p_aabb.pos.y > 1e15 || p_aabb.pos.y < -1e15);
+ ERR_FAIL_COND(p_aabb.pos.z > 1e15 || p_aabb.pos.z < -1e15);
+ ERR_FAIL_COND(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0);
+ ERR_FAIL_COND(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0);
+ ERR_FAIL_COND(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0);
+ ERR_FAIL_COND(Math::is_nan(p_aabb.size.x));
+ ERR_FAIL_COND(Math::is_nan(p_aabb.size.y));
+ ERR_FAIL_COND(Math::is_nan(p_aabb.size.z));
#endif
typename ElementMap::Element *E = element_map.find(p_id);
ERR_FAIL_COND(!E);
@@ -901,25 +872,23 @@ void Octree<T,use_pairs,AL>::move(OctreeElementID p_id, const Rect3& p_aabb) {
#else
- bool old_has_surf=!e.aabb.has_no_surface();
- bool new_has_surf=!p_aabb.has_no_surface();
-
- if (old_has_surf!=new_has_surf) {
+ bool old_has_surf = !e.aabb.has_no_surface();
+ bool new_has_surf = !p_aabb.has_no_surface();
+ if (old_has_surf != new_has_surf) {
if (old_has_surf) {
_remove_element(&e); // removing
- e.common_parent=NULL;
- e.aabb=Rect3();
+ e.common_parent = NULL;
+ e.aabb = Rect3();
_optimize();
} else {
_ensure_valid_root(p_aabb); // inserting
- e.common_parent=NULL;
- e.aabb=p_aabb;
- _insert_element(&e,root);
+ e.common_parent = NULL;
+ e.aabb = p_aabb;
+ _insert_element(&e, root);
if (use_pairs)
_element_check_pairs(&e);
-
}
return;
@@ -931,48 +900,46 @@ void Octree<T,use_pairs,AL>::move(OctreeElementID p_id, const Rect3& p_aabb) {
// it still is enclosed in the same AABB it was assigned to
if (e.container_aabb.encloses(p_aabb)) {
- e.aabb=p_aabb;
+ e.aabb = p_aabb;
if (use_pairs)
_element_check_pairs(&e); // must check pairs anyway
-
return;
}
- Rect3 combined=e.aabb;
+ Rect3 combined = e.aabb;
combined.merge_with(p_aabb);
_ensure_valid_root(combined);
- ERR_FAIL_COND( e.octant_owners.front()==NULL );
+ ERR_FAIL_COND(e.octant_owners.front() == NULL);
/* FIND COMMON PARENT */
- List<typename Element::OctantOwner,AL> owners = e.octant_owners; // save the octant owners
- Octant *common_parent=e.common_parent;
+ List<typename Element::OctantOwner, AL> owners = e.octant_owners; // save the octant owners
+ Octant *common_parent = e.common_parent;
ERR_FAIL_COND(!common_parent);
-
//src is now the place towards where insertion is going to happen
pass++;
- while(common_parent && !common_parent->aabb.encloses(p_aabb))
- common_parent=common_parent->parent;
+ while (common_parent && !common_parent->aabb.encloses(p_aabb))
+ common_parent = common_parent->parent;
ERR_FAIL_COND(!common_parent);
//prepare for reinsert
e.octant_owners.clear();
- e.common_parent=NULL;
- e.aabb=p_aabb;
+ e.common_parent = NULL;
+ e.aabb = p_aabb;
- _insert_element(&e,common_parent); // reinsert from this point
+ _insert_element(&e, common_parent); // reinsert from this point
pass++;
- for(typename List<typename Element::OctantOwner,AL>::Element *E=owners.front();E;) {
+ for (typename List<typename Element::OctantOwner, AL>::Element *E = owners.front(); E;) {
- Octant *o=E->get().octant;
- typename List<typename Element::OctantOwner,AL>::Element *N=E->next();
+ Octant *o = E->get().octant;
+ typename List<typename Element::OctantOwner, AL>::Element *N = E->next();
/*
if (!use_pairs)
@@ -980,77 +947,70 @@ void Octree<T,use_pairs,AL>::move(OctreeElementID p_id, const Rect3& p_aabb) {
*/
if (use_pairs && e.pairable)
- o->pairable_elements.erase( E->get().E );
+ o->pairable_elements.erase(E->get().E);
else
- o->elements.erase( E->get().E );
+ o->elements.erase(E->get().E);
- if (_remove_element_from_octant( &e, o, common_parent->parent )) {
+ if (_remove_element_from_octant(&e, o, common_parent->parent)) {
owners.erase(E);
}
- E=N;
+ E = N;
}
-
if (use_pairs) {
//unpair child elements in anything that survived
- for(typename List<typename Element::OctantOwner,AL>::Element *E=owners.front();E;E=E->next()) {
+ for (typename List<typename Element::OctantOwner, AL>::Element *E = owners.front(); E; E = E->next()) {
- Octant *o=E->get().octant;
+ Octant *o = E->get().octant;
// erase children pairs, unref ONCE
pass++;
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
if (o->children[i])
- _unpair_element(&e,o->children[i]);
+ _unpair_element(&e, o->children[i]);
}
-
}
_element_check_pairs(&e);
}
-
_optimize();
#endif
-
-
}
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::set_pairable(OctreeElementID p_id,bool p_pairable,uint32_t p_pairable_type,uint32_t p_pairable_mask) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::set_pairable(OctreeElementID p_id, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) {
typename ElementMap::Element *E = element_map.find(p_id);
ERR_FAIL_COND(!E);
Element &e = E->get();
- if (p_pairable == e.pairable && e.pairable_type==p_pairable_type && e.pairable_mask==p_pairable_mask)
+ if (p_pairable == e.pairable && e.pairable_type == p_pairable_type && e.pairable_mask == p_pairable_mask)
return; // no changes, return
if (!e.aabb.has_no_surface()) {
_remove_element(&e);
}
- e.pairable=p_pairable;
- e.pairable_type=p_pairable_type;
- e.pairable_mask=p_pairable_mask;
- e.common_parent=NULL;
+ e.pairable = p_pairable;
+ e.pairable_type = p_pairable_type;
+ e.pairable_mask = p_pairable_mask;
+ e.common_parent = NULL;
if (!e.aabb.has_no_surface()) {
_ensure_valid_root(e.aabb);
- _insert_element(&e,root);
+ _insert_element(&e, root);
if (use_pairs)
_element_check_pairs(&e);
-
}
}
-
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::erase(OctreeElementID p_id) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::erase(OctreeElementID p_id) {
typename ElementMap::Element *E = element_map.find(p_id);
ERR_FAIL_COND(!E);
@@ -1066,28 +1026,28 @@ void Octree<T,use_pairs,AL>::erase(OctreeElementID p_id) {
_optimize();
}
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::_cull_convex(Octant *p_octant,_CullConvexData *p_cull) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p_cull) {
- if (*p_cull->result_idx==p_cull->result_max)
+ if (*p_cull->result_idx == p_cull->result_max)
return; //pointless
if (!p_octant->elements.empty()) {
- typename List< Element*,AL >::Element *I;
- I=p_octant->elements.front();
+ typename List<Element *, AL>::Element *I;
+ I = p_octant->elements.front();
- for(;I;I=I->next()) {
+ for (; I; I = I->next()) {
- Element *e=I->get();
+ Element *e = I->get();
- if (e->last_pass==pass || (use_pairs && !(e->pairable_type&p_cull->mask)))
+ if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask)))
continue;
- e->last_pass=pass;
+ e->last_pass = pass;
- if (e->aabb.intersects_convex_shape(p_cull->planes,p_cull->plane_count)) {
+ if (e->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count)) {
- if (*p_cull->result_idx<p_cull->result_max) {
+ if (*p_cull->result_idx < p_cull->result_max) {
p_cull->result_array[*p_cull->result_idx] = e->userdata;
(*p_cull->result_idx)++;
} else {
@@ -1100,20 +1060,20 @@ void Octree<T,use_pairs,AL>::_cull_convex(Octant *p_octant,_CullConvexData *p_cu
if (use_pairs && !p_octant->pairable_elements.empty()) {
- typename List< Element*,AL >::Element *I;
- I=p_octant->pairable_elements.front();
+ typename List<Element *, AL>::Element *I;
+ I = p_octant->pairable_elements.front();
- for(;I;I=I->next()) {
+ for (; I; I = I->next()) {
- Element *e=I->get();
+ Element *e = I->get();
- if (e->last_pass==pass || (use_pairs && !(e->pairable_type&p_cull->mask)))
+ if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask)))
continue;
- e->last_pass=pass;
+ e->last_pass = pass;
- if (e->aabb.intersects_convex_shape(p_cull->planes,p_cull->plane_count)) {
+ if (e->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count)) {
- if (*p_cull->result_idx<p_cull->result_max) {
+ if (*p_cull->result_idx < p_cull->result_max) {
p_cull->result_array[*p_cull->result_idx] = e->userdata;
(*p_cull->result_idx)++;
@@ -1125,36 +1085,35 @@ void Octree<T,use_pairs,AL>::_cull_convex(Octant *p_octant,_CullConvexData *p_cu
}
}
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
- if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_convex_shape(p_cull->planes,p_cull->plane_count)) {
- _cull_convex(p_octant->children[i],p_cull);
+ if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count)) {
+ _cull_convex(p_octant->children[i], p_cull);
}
}
}
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_cull_AABB(Octant *p_octant, const Rect3 &p_aabb, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::_cull_AABB(Octant *p_octant,const Rect3& p_aabb, T** p_result_array,int *p_result_idx,int p_result_max,int *p_subindex_array,uint32_t p_mask) {
-
- if (*p_result_idx==p_result_max)
+ if (*p_result_idx == p_result_max)
return; //pointless
if (!p_octant->elements.empty()) {
- typename List< Element*,AL >::Element *I;
- I=p_octant->elements.front();
- for(;I;I=I->next()) {
+ typename List<Element *, AL>::Element *I;
+ I = p_octant->elements.front();
+ for (; I; I = I->next()) {
- Element *e=I->get();
+ Element *e = I->get();
- if (e->last_pass==pass || (use_pairs && !(e->pairable_type&p_mask)))
+ if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
continue;
- e->last_pass=pass;
+ e->last_pass = pass;
if (p_aabb.intersects_inclusive(e->aabb)) {
- if (*p_result_idx<p_result_max) {
+ if (*p_result_idx < p_result_max) {
p_result_array[*p_result_idx] = e->userdata;
if (p_subindex_array)
@@ -1171,19 +1130,19 @@ void Octree<T,use_pairs,AL>::_cull_AABB(Octant *p_octant,const Rect3& p_aabb, T*
if (use_pairs && !p_octant->pairable_elements.empty()) {
- typename List< Element*,AL >::Element *I;
- I=p_octant->pairable_elements.front();
- for(;I;I=I->next()) {
+ typename List<Element *, AL>::Element *I;
+ I = p_octant->pairable_elements.front();
+ for (; I; I = I->next()) {
- Element *e=I->get();
+ Element *e = I->get();
- if (e->last_pass==pass || (use_pairs && !(e->pairable_type&p_mask)))
+ if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
continue;
- e->last_pass=pass;
+ e->last_pass = pass;
if (p_aabb.intersects_inclusive(e->aabb)) {
- if (*p_result_idx<p_result_max) {
+ if (*p_result_idx < p_result_max) {
p_result_array[*p_result_idx] = e->userdata;
if (p_subindex_array)
@@ -1197,36 +1156,35 @@ void Octree<T,use_pairs,AL>::_cull_AABB(Octant *p_octant,const Rect3& p_aabb, T*
}
}
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_inclusive(p_aabb)) {
- _cull_AABB(p_octant->children[i],p_aabb, p_result_array,p_result_idx,p_result_max,p_subindex_array,p_mask);
+ _cull_AABB(p_octant->children[i], p_aabb, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
}
}
-
}
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::_cull_segment(Octant *p_octant,const Vector3& p_from, const Vector3& p_to,T** p_result_array,int *p_result_idx,int p_result_max,int *p_subindex_array,uint32_t p_mask) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
- if (*p_result_idx==p_result_max)
+ if (*p_result_idx == p_result_max)
return; //pointless
if (!p_octant->elements.empty()) {
- typename List< Element*,AL >::Element *I;
- I=p_octant->elements.front();
- for(;I;I=I->next()) {
+ typename List<Element *, AL>::Element *I;
+ I = p_octant->elements.front();
+ for (; I; I = I->next()) {
- Element *e=I->get();
+ Element *e = I->get();
- if (e->last_pass==pass || (use_pairs && !(e->pairable_type&p_mask)))
+ if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
continue;
- e->last_pass=pass;
+ e->last_pass = pass;
- if (e->aabb.intersects_segment(p_from,p_to)) {
+ if (e->aabb.intersects_segment(p_from, p_to)) {
- if (*p_result_idx<p_result_max) {
+ if (*p_result_idx < p_result_max) {
p_result_array[*p_result_idx] = e->userdata;
if (p_subindex_array)
@@ -1243,20 +1201,20 @@ void Octree<T,use_pairs,AL>::_cull_segment(Octant *p_octant,const Vector3& p_fro
if (use_pairs && !p_octant->pairable_elements.empty()) {
- typename List< Element*,AL >::Element *I;
- I=p_octant->pairable_elements.front();
- for(;I;I=I->next()) {
+ typename List<Element *, AL>::Element *I;
+ I = p_octant->pairable_elements.front();
+ for (; I; I = I->next()) {
- Element *e=I->get();
+ Element *e = I->get();
- if (e->last_pass==pass || (use_pairs && !(e->pairable_type&p_mask)))
+ if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
continue;
- e->last_pass=pass;
+ e->last_pass = pass;
- if (e->aabb.intersects_segment(p_from,p_to)) {
+ if (e->aabb.intersects_segment(p_from, p_to)) {
- if (*p_result_idx<p_result_max) {
+ if (*p_result_idx < p_result_max) {
p_result_array[*p_result_idx] = e->userdata;
if (p_subindex_array)
@@ -1272,37 +1230,35 @@ void Octree<T,use_pairs,AL>::_cull_segment(Octant *p_octant,const Vector3& p_fro
}
}
+ for (int i = 0; i < 8; i++) {
- for (int i=0;i<8;i++) {
-
- if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_segment(p_from,p_to)) {
- _cull_segment(p_octant->children[i],p_from,p_to, p_result_array,p_result_idx,p_result_max,p_subindex_array,p_mask);
+ if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_segment(p_from, p_to)) {
+ _cull_segment(p_octant->children[i], p_from, p_to, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
}
}
}
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_point, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::_cull_point(Octant *p_octant,const Vector3& p_point,T** p_result_array,int *p_result_idx,int p_result_max,int *p_subindex_array,uint32_t p_mask) {
-
- if (*p_result_idx==p_result_max)
+ if (*p_result_idx == p_result_max)
return; //pointless
if (!p_octant->elements.empty()) {
- typename List< Element*,AL >::Element *I;
- I=p_octant->elements.front();
- for(;I;I=I->next()) {
+ typename List<Element *, AL>::Element *I;
+ I = p_octant->elements.front();
+ for (; I; I = I->next()) {
- Element *e=I->get();
+ Element *e = I->get();
- if (e->last_pass==pass || (use_pairs && !(e->pairable_type&p_mask)))
+ if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
continue;
- e->last_pass=pass;
+ e->last_pass = pass;
if (e->aabb.has_point(p_point)) {
- if (*p_result_idx<p_result_max) {
+ if (*p_result_idx < p_result_max) {
p_result_array[*p_result_idx] = e->userdata;
if (p_subindex_array)
@@ -1319,20 +1275,20 @@ void Octree<T,use_pairs,AL>::_cull_point(Octant *p_octant,const Vector3& p_point
if (use_pairs && !p_octant->pairable_elements.empty()) {
- typename List< Element*,AL >::Element *I;
- I=p_octant->pairable_elements.front();
- for(;I;I=I->next()) {
+ typename List<Element *, AL>::Element *I;
+ I = p_octant->pairable_elements.front();
+ for (; I; I = I->next()) {
- Element *e=I->get();
+ Element *e = I->get();
- if (e->last_pass==pass || (use_pairs && !(e->pairable_type&p_mask)))
+ if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
continue;
- e->last_pass=pass;
+ e->last_pass = pass;
if (e->aabb.has_point(p_point)) {
- if (*p_result_idx<p_result_max) {
+ if (*p_result_idx < p_result_max) {
p_result_array[*p_result_idx] = e->userdata;
if (p_subindex_array)
@@ -1348,120 +1304,103 @@ void Octree<T,use_pairs,AL>::_cull_point(Octant *p_octant,const Vector3& p_point
}
}
-
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
//could be optimized..
if (p_octant->children[i] && p_octant->children[i]->aabb.has_point(p_point)) {
- _cull_point(p_octant->children[i],p_point, p_result_array,p_result_idx,p_result_max,p_subindex_array,p_mask);
+ _cull_point(p_octant->children[i], p_point, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
}
}
}
-template<class T,bool use_pairs,class AL>
-int Octree<T,use_pairs,AL>::cull_convex(const Vector<Plane>& p_convex,T** p_result_array,int p_result_max,uint32_t p_mask) {
+template <class T, bool use_pairs, class AL>
+int Octree<T, use_pairs, AL>::cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask) {
if (!root)
return 0;
- int result_count=0;
+ int result_count = 0;
pass++;
_CullConvexData cdata;
- cdata.planes=&p_convex[0];
- cdata.plane_count=p_convex.size();
- cdata.result_array=p_result_array;
- cdata.result_max=p_result_max;
- cdata.result_idx=&result_count;
- cdata.mask=p_mask;
+ cdata.planes = &p_convex[0];
+ cdata.plane_count = p_convex.size();
+ cdata.result_array = p_result_array;
+ cdata.result_max = p_result_max;
+ cdata.result_idx = &result_count;
+ cdata.mask = p_mask;
- _cull_convex(root,&cdata);
+ _cull_convex(root, &cdata);
return result_count;
}
-
-
-template<class T,bool use_pairs,class AL>
-int Octree<T,use_pairs,AL>::cull_AABB(const Rect3& p_aabb,T** p_result_array,int p_result_max,int *p_subindex_array,uint32_t p_mask) {
-
+template <class T, bool use_pairs, class AL>
+int Octree<T, use_pairs, AL>::cull_AABB(const Rect3 &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
if (!root)
return 0;
- int result_count=0;
+ int result_count = 0;
pass++;
- _cull_AABB(root,p_aabb,p_result_array,&result_count,p_result_max,p_subindex_array,p_mask);
+ _cull_AABB(root, p_aabb, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
return result_count;
}
-
-template<class T,bool use_pairs,class AL>
-int Octree<T,use_pairs,AL>::cull_segment(const Vector3& p_from, const Vector3& p_to,T** p_result_array,int p_result_max,int *p_subindex_array,uint32_t p_mask) {
+template <class T, bool use_pairs, class AL>
+int Octree<T, use_pairs, AL>::cull_segment(const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
if (!root)
return 0;
- int result_count=0;
+ int result_count = 0;
pass++;
- _cull_segment(root,p_from,p_to,p_result_array,&result_count,p_result_max,p_subindex_array,p_mask);
+ _cull_segment(root, p_from, p_to, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
return result_count;
-
}
-template<class T,bool use_pairs,class AL>
-int Octree<T,use_pairs,AL>::cull_point(const Vector3& p_point,T** p_result_array,int p_result_max,int *p_subindex_array,uint32_t p_mask) {
+template <class T, bool use_pairs, class AL>
+int Octree<T, use_pairs, AL>::cull_point(const Vector3 &p_point, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
if (!root)
return 0;
- int result_count=0;
+ int result_count = 0;
pass++;
- _cull_point(root,p_point,p_result_array,&result_count,p_result_max,p_subindex_array,p_mask);
+ _cull_point(root, p_point, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
return result_count;
-
}
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::set_pair_callback(PairCallback p_callback, void *p_userdata) {
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::set_pair_callback( PairCallback p_callback, void *p_userdata ) {
-
- pair_callback=p_callback;
- pair_callback_userdata=p_userdata;
+ pair_callback = p_callback;
+ pair_callback_userdata = p_userdata;
}
-template<class T,bool use_pairs,class AL>
-void Octree<T,use_pairs,AL>::set_unpair_callback( UnpairCallback p_callback, void *p_userdata ) {
-
- unpair_callback=p_callback;
- unpair_callback_userdata=p_userdata;
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::set_unpair_callback(UnpairCallback p_callback, void *p_userdata) {
+ unpair_callback = p_callback;
+ unpair_callback_userdata = p_userdata;
}
+template <class T, bool use_pairs, class AL>
+Octree<T, use_pairs, AL>::Octree(real_t p_unit_size) {
-template<class T,bool use_pairs,class AL>
-Octree<T,use_pairs,AL>::Octree(real_t p_unit_size) {
-
- last_element_id=1;
- pass=1;
- unit_size=p_unit_size;
- root=NULL;
-
- octant_count=0;
- pair_count=0;
-
- pair_callback=NULL;
- unpair_callback=NULL;
- pair_callback_userdata=NULL;
- unpair_callback_userdata=NULL;
-
-
+ last_element_id = 1;
+ pass = 1;
+ unit_size = p_unit_size;
+ root = NULL;
+ octant_count = 0;
+ pair_count = 0;
+ pair_callback = NULL;
+ unpair_callback = NULL;
+ pair_callback_userdata = NULL;
+ unpair_callback_userdata = NULL;
}
-
-
-
#endif
diff --git a/core/math/plane.cpp b/core/math/plane.cpp
index 2a97932049..29e7f2e75c 100644
--- a/core/math/plane.cpp
+++ b/core/math/plane.cpp
@@ -33,20 +33,20 @@
#define _PLANE_EQ_DOT_EPSILON 0.999
#define _PLANE_EQ_D_EPSILON 0.0001
-void Plane::set_normal(const Vector3& p_normal) {
+void Plane::set_normal(const Vector3 &p_normal) {
- normal=p_normal;
+ normal = p_normal;
}
void Plane::normalize() {
real_t l = normal.length();
- if (l==0) {
- *this=Plane(0,0,0,0);
+ if (l == 0) {
+ *this = Plane(0, 0, 0, 0);
return;
}
- normal/=l;
- d/=l;
+ normal /= l;
+ d /= l;
}
Plane Plane::normalized() const {
@@ -58,21 +58,21 @@ Plane Plane::normalized() const {
Vector3 Plane::get_any_point() const {
- return get_normal()*d;
+ return get_normal() * d;
}
Vector3 Plane::get_any_perpendicular_normal() const {
- static const Vector3 p1 = Vector3(1,0,0);
- static const Vector3 p2 = Vector3(0,1,0);
+ static const Vector3 p1 = Vector3(1, 0, 0);
+ static const Vector3 p2 = Vector3(0, 1, 0);
Vector3 p;
if (ABS(normal.dot(p1)) > 0.99) // if too similar to p1
- p=p2; // use p2
+ p = p2; // use p2
else
- p=p1; // use p1
+ p = p1; // use p1
- p-=normal * normal.dot(p);
+ p -= normal * normal.dot(p);
p.normalize();
return p;
@@ -82,71 +82,71 @@ Vector3 Plane::get_any_perpendicular_normal() const {
bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const {
- const Plane &p_plane0=*this;
- Vector3 normal0=p_plane0.normal;
- Vector3 normal1=p_plane1.normal;
- Vector3 normal2=p_plane2.normal;
+ const Plane &p_plane0 = *this;
+ Vector3 normal0 = p_plane0.normal;
+ Vector3 normal1 = p_plane1.normal;
+ Vector3 normal2 = p_plane2.normal;
- real_t denom=vec3_cross(normal0,normal1).dot(normal2);
+ real_t denom = vec3_cross(normal0, normal1).dot(normal2);
- if (ABS(denom)<=CMP_EPSILON)
+ if (ABS(denom) <= CMP_EPSILON)
return false;
- if (r_result) {
- *r_result = ( (vec3_cross(normal1, normal2) * p_plane0.d) +
- (vec3_cross(normal2, normal0) * p_plane1.d) +
- (vec3_cross(normal0, normal1) * p_plane2.d) )/denom;
- }
+ if (r_result) {
+ *r_result = ((vec3_cross(normal1, normal2) * p_plane0.d) +
+ (vec3_cross(normal2, normal0) * p_plane1.d) +
+ (vec3_cross(normal0, normal1) * p_plane2.d)) /
+ denom;
+ }
return true;
}
+bool Plane::intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3 *p_intersection) const {
-bool Plane::intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3* p_intersection) const {
-
- Vector3 segment=p_dir;
- real_t den=normal.dot( segment );
+ Vector3 segment = p_dir;
+ real_t den = normal.dot(segment);
//printf("den is %i\n",den);
- if (Math::abs(den)<=CMP_EPSILON) {
+ if (Math::abs(den) <= CMP_EPSILON) {
return false;
}
- real_t dist=(normal.dot( p_from ) - d) / den;
+ real_t dist = (normal.dot(p_from) - d) / den;
//printf("dist is %i\n",dist);
- if (dist>CMP_EPSILON) { //this is a ray, before the emiting pos (p_from) doesnt exist
+ if (dist > CMP_EPSILON) { //this is a ray, before the emiting pos (p_from) doesnt exist
return false;
}
- dist=-dist;
+ dist = -dist;
*p_intersection = p_from + segment * dist;
return true;
}
-bool Plane::intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3* p_intersection) const {
+bool Plane::intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3 *p_intersection) const {
- Vector3 segment= p_begin - p_end;
- real_t den=normal.dot( segment );
+ Vector3 segment = p_begin - p_end;
+ real_t den = normal.dot(segment);
//printf("den is %i\n",den);
- if (Math::abs(den)<=CMP_EPSILON) {
+ if (Math::abs(den) <= CMP_EPSILON) {
return false;
}
- real_t dist=(normal.dot( p_begin ) - d) / den;
+ real_t dist = (normal.dot(p_begin) - d) / den;
//printf("dist is %i\n",dist);
- if (dist<-CMP_EPSILON || dist > (1.0 +CMP_EPSILON)) {
+ if (dist < -CMP_EPSILON || dist > (1.0 + CMP_EPSILON)) {
return false;
}
- dist=-dist;
+ dist = -dist;
*p_intersection = p_begin + segment * dist;
return true;
@@ -154,12 +154,11 @@ bool Plane::intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3* p_inters
/* misc */
-bool Plane::is_almost_like(const Plane& p_plane) const {
+bool Plane::is_almost_like(const Plane &p_plane) const {
- return (normal.dot( p_plane.normal ) > _PLANE_EQ_DOT_EPSILON && Math::absd(d-p_plane.d) < _PLANE_EQ_D_EPSILON);
+ return (normal.dot(p_plane.normal) > _PLANE_EQ_DOT_EPSILON && Math::absd(d - p_plane.d) < _PLANE_EQ_D_EPSILON);
}
-
Plane::operator String() const {
return normal.operator String() + ", " + rtos(d);
diff --git a/core/math/plane.h b/core/math/plane.h
index 8235c59135..380452f6d2 100644
--- a/core/math/plane.h
+++ b/core/math/plane.h
@@ -29,64 +29,58 @@
#ifndef PLANE_H
#define PLANE_H
-
#include "vector3.h"
class Plane {
public:
-
Vector3 normal;
real_t d;
-
- void set_normal(const Vector3& p_normal);
+ 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 */
+ /* Plane-Point operations */
- _FORCE_INLINE_ Vector3 center() const { return normal*d; }
+ _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;
+ _FORCE_INLINE_ bool has_point(const Vector3 &p_point, real_t _epsilon = CMP_EPSILON) const;
- /* intersections */
+ /* 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;
+ 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 {
+ _FORCE_INLINE_ Vector3 project(const Vector3 &p_point) const {
return p_point - normal * distance_to(p_point);
}
- /* misc */
+ /* misc */
- Plane operator-() const { return Plane(-normal,-d); }
- bool is_almost_like(const Plane& p_plane) const;
+ 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;
+ _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() { 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);
-
-
+ _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);
@@ -94,53 +88,47 @@ bool Plane::is_point_over(const Vector3 &p_point) const {
real_t Plane::distance_to(const Vector3 &p_point) const {
- return (normal.dot(p_point)-d);
+ 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);
+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;
+ normal = p_normal;
+ d = p_d;
}
-Plane::Plane(const Vector3 &p_point, const Vector3& p_normal) {
+Plane::Plane(const Vector3 &p_point, const Vector3 &p_normal) {
- normal=p_normal;
- d=p_normal.dot(p_point);
+ 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) {
+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);
+ normal = (p_point1 - p_point3).cross(p_point1 - p_point2);
else
- normal=(p_point1-p_point2).cross(p_point1-p_point3);
-
+ 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 {
+bool Plane::operator==(const Plane &p_plane) const {
- return normal==p_plane.normal && d == p_plane.d;
+ 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;
+bool Plane::operator!=(const Plane &p_plane) const {
+ return normal != p_plane.normal || d != p_plane.d;
}
#endif // PLANE_H
-
diff --git a/core/math/quat.cpp b/core/math/quat.cpp
index 4085f9b84a..b990e9184f 100644
--- a/core/math/quat.cpp
+++ b/core/math/quat.cpp
@@ -33,7 +33,7 @@
// set_euler expects a vector containing the Euler angles in the format
// (c,b,a), where a is the angle of the first rotation, and c is the last.
// The current implementation uses XYZ convention (Z is the first rotation).
-void Quat::set_euler(const Vector3& p_euler) {
+void Quat::set_euler(const Vector3 &p_euler) {
real_t half_a1 = p_euler.x * 0.5;
real_t half_a2 = p_euler.y * 0.5;
real_t half_a3 = p_euler.z * 0.5;
@@ -49,10 +49,10 @@ void Quat::set_euler(const Vector3& p_euler) {
real_t cos_a3 = Math::cos(half_a3);
real_t sin_a3 = Math::sin(half_a3);
- set(sin_a1*cos_a2*cos_a3 + sin_a2*sin_a3*cos_a1,
- -sin_a1*sin_a3*cos_a2 + sin_a2*cos_a1*cos_a3,
- sin_a1*sin_a2*cos_a3 + sin_a3*cos_a1*cos_a2,
- -sin_a1*sin_a2*sin_a3 + cos_a1*cos_a2*cos_a3);
+ set(sin_a1 * cos_a2 * cos_a3 + sin_a2 * sin_a3 * cos_a1,
+ -sin_a1 * sin_a3 * cos_a2 + sin_a2 * cos_a1 * cos_a3,
+ sin_a1 * sin_a2 * cos_a3 + sin_a3 * cos_a1 * cos_a2,
+ -sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3);
}
// get_euler returns a vector containing the Euler angles in the format
@@ -63,24 +63,21 @@ Vector3 Quat::get_euler() const {
return m.get_euler();
}
-void Quat::operator*=(const Quat& q) {
+void Quat::operator*=(const Quat &q) {
- set(w * q.x+x * q.w+y * q.z - z * q.y,
- w * q.y+y * q.w+z * q.x - x * q.z,
- w * q.z+z * q.w+x * q.y - y * q.x,
- w * q.w - x * q.x - y * q.y - z * q.z);
+ set(w * q.x + x * q.w + y * q.z - z * q.y,
+ w * q.y + y * q.w + z * q.x - x * q.z,
+ w * q.z + z * q.w + x * q.y - y * q.x,
+ w * q.w - x * q.x - y * q.y - z * q.z);
}
-Quat Quat::operator*(const Quat& q) const {
+Quat Quat::operator*(const Quat &q) const {
- Quat r=*this;
- r*=q;
+ Quat r = *this;
+ r *= q;
return r;
}
-
-
-
real_t Quat::length() const {
return Math::sqrt(length_squared());
@@ -90,17 +87,15 @@ void Quat::normalize() {
*this /= length();
}
-
Quat Quat::normalized() const {
return *this / length();
}
Quat Quat::inverse() const {
- return Quat( -x, -y, -z, w );
+ return Quat(-x, -y, -z, w);
}
-
-Quat Quat::slerp(const Quat& q, const real_t& t) const {
+Quat Quat::slerp(const Quat &q, const real_t &t) const {
#if 0
@@ -144,31 +139,29 @@ Quat Quat::slerp(const Quat& q, const real_t& t) const {
}
#else
- Quat to1;
- real_t omega, cosom, sinom, scale0, scale1;
-
+ Quat to1;
+ real_t omega, cosom, sinom, scale0, scale1;
// calc cosine
cosom = dot(q);
// adjust signs (if necessary)
- if ( cosom <0.0 ) {
+ if (cosom < 0.0) {
cosom = -cosom;
- to1.x = - q.x;
- to1.y = - q.y;
- to1.z = - q.z;
- to1.w = - q.w;
- } else {
+ to1.x = -q.x;
+ to1.y = -q.y;
+ to1.z = -q.z;
+ to1.w = -q.w;
+ } else {
to1.x = q.x;
to1.y = q.y;
to1.z = q.z;
to1.w = q.w;
}
-
// calculate coefficients
- if ( (1.0 - cosom) > CMP_EPSILON ) {
+ if ((1.0 - cosom) > CMP_EPSILON) {
// standard case (slerp)
omega = Math::acos(cosom);
sinom = Math::sin(omega);
@@ -182,15 +175,14 @@ Quat Quat::slerp(const Quat& q, const real_t& t) const {
}
// calculate final values
return Quat(
- scale0 * x + scale1 * to1.x,
- scale0 * y + scale1 * to1.y,
- scale0 * z + scale1 * to1.z,
- scale0 * w + scale1 * to1.w
- );
+ scale0 * x + scale1 * to1.x,
+ scale0 * y + scale1 * to1.y,
+ scale0 * z + scale1 * to1.z,
+ scale0 * w + scale1 * to1.w);
#endif
}
-Quat Quat::slerpni(const Quat& q, const real_t& t) const {
+Quat Quat::slerpni(const Quat &q, const real_t &t) const {
const Quat &from = *this;
@@ -198,15 +190,15 @@ Quat Quat::slerpni(const Quat& q, const real_t& t) const {
if (Math::absf(dot) > 0.9999) return from;
- real_t theta = Math::acos(dot),
- sinT = 1.0 / Math::sin(theta),
- newFactor = Math::sin(t * theta) * sinT,
- invFactor = Math::sin((1.0 - t) * theta) * sinT;
+ real_t theta = Math::acos(dot),
+ sinT = 1.0 / Math::sin(theta),
+ newFactor = Math::sin(t * theta) * sinT,
+ invFactor = Math::sin((1.0 - t) * theta) * sinT;
return Quat(invFactor * from.x + newFactor * q.x,
- invFactor * from.y + newFactor * q.y,
- invFactor * from.z + newFactor * q.z,
- invFactor * from.w + newFactor * q.w);
+ invFactor * from.y + newFactor * q.y,
+ invFactor * from.z + newFactor * q.z,
+ invFactor * from.w + newFactor * q.w);
#if 0
real_t to1[4];
@@ -256,31 +248,29 @@ Quat Quat::slerpni(const Quat& q, const real_t& t) const {
#endif
}
-Quat Quat::cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const {
+Quat Quat::cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const {
//the only way to do slerp :|
- real_t t2 = (1.0-t)*t*2;
- Quat sp = this->slerp(q,t);
- Quat sq = prep.slerpni(postq,t);
- return sp.slerpni(sq,t2);
-
+ real_t t2 = (1.0 - t) * t * 2;
+ Quat sp = this->slerp(q, t);
+ Quat sq = prep.slerpni(postq, t);
+ return sp.slerpni(sq, t2);
}
-
Quat::operator String() const {
- return String::num(x)+", "+String::num(y)+", "+ String::num(z)+", "+ String::num(w);
+ return String::num(x) + ", " + String::num(y) + ", " + String::num(z) + ", " + String::num(w);
}
-Quat::Quat(const Vector3& axis, const real_t& angle) {
+Quat::Quat(const Vector3 &axis, const real_t &angle) {
real_t d = axis.length();
- if (d==0)
- set(0,0,0,0);
+ if (d == 0)
+ set(0, 0, 0, 0);
else {
real_t sin_angle = Math::sin(angle * 0.5);
real_t cos_angle = Math::cos(angle * 0.5);
real_t s = sin_angle / d;
set(axis.x * s, axis.y * s, axis.z * s,
- cos_angle);
+ cos_angle);
}
}
diff --git a/core/math/quat.h b/core/math/quat.h
index d3a50343a3..3fc843b83a 100644
--- a/core/math/quat.h
+++ b/core/math/quat.h
@@ -39,159 +39,166 @@
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
-class Quat{
+class Quat {
public:
-
- real_t x,y,z,w;
+ real_t x, y, z, w;
_FORCE_INLINE_ real_t length_squared() const;
real_t length() const;
void normalize();
Quat normalized() const;
Quat inverse() const;
- _FORCE_INLINE_ real_t dot(const Quat& q) const;
- void set_euler(const Vector3& p_euler);
+ _FORCE_INLINE_ real_t dot(const Quat &q) const;
+ void set_euler(const Vector3 &p_euler);
Vector3 get_euler() const;
- Quat slerp(const Quat& q, const real_t& t) const;
- Quat slerpni(const Quat& q, const real_t& t) const;
- Quat cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const;
+ Quat slerp(const Quat &q, const real_t &t) const;
+ Quat slerpni(const Quat &q, const real_t &t) const;
+ Quat cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const;
- _FORCE_INLINE_ void get_axis_and_angle(Vector3& r_axis, real_t &r_angle) const {
+ _FORCE_INLINE_ void get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const {
r_angle = 2 * Math::acos(w);
- r_axis.x = x / Math::sqrt(1-w*w);
- r_axis.y = y / Math::sqrt(1-w*w);
- r_axis.z = z / Math::sqrt(1-w*w);
+ r_axis.x = x / Math::sqrt(1 - w * w);
+ r_axis.y = y / Math::sqrt(1 - w * w);
+ r_axis.z = z / Math::sqrt(1 - w * w);
}
- void operator*=(const Quat& q);
- Quat operator*(const Quat& q) const;
-
-
+ void operator*=(const Quat &q);
+ Quat operator*(const Quat &q) const;
- Quat operator*(const Vector3& v) const
- {
- return Quat( w * v.x + y * v.z - z * v.y,
- w * v.y + z * v.x - x * v.z,
- w * v.z + x * v.y - y * v.x,
- -x * v.x - y * v.y - z * v.z);
+ Quat operator*(const Vector3 &v) const {
+ return Quat(w * v.x + y * v.z - z * v.y,
+ w * v.y + z * v.x - x * v.z,
+ w * v.z + x * v.y - y * v.x,
+ -x * v.x - y * v.y - z * v.z);
}
- _FORCE_INLINE_ Vector3 xform(const Vector3& v) const {
+ _FORCE_INLINE_ Vector3 xform(const Vector3 &v) const {
Quat q = *this * v;
q *= this->inverse();
- return Vector3(q.x,q.y,q.z);
+ return Vector3(q.x, q.y, q.z);
}
- _FORCE_INLINE_ void operator+=(const Quat& q);
- _FORCE_INLINE_ void operator-=(const Quat& q);
- _FORCE_INLINE_ void operator*=(const real_t& s);
- _FORCE_INLINE_ void operator/=(const real_t& s);
- _FORCE_INLINE_ Quat operator+(const Quat& q2) const;
- _FORCE_INLINE_ Quat operator-(const Quat& q2) const;
+ _FORCE_INLINE_ void operator+=(const Quat &q);
+ _FORCE_INLINE_ void operator-=(const Quat &q);
+ _FORCE_INLINE_ void operator*=(const real_t &s);
+ _FORCE_INLINE_ void operator/=(const real_t &s);
+ _FORCE_INLINE_ Quat operator+(const Quat &q2) const;
+ _FORCE_INLINE_ Quat operator-(const Quat &q2) const;
_FORCE_INLINE_ Quat operator-() const;
- _FORCE_INLINE_ Quat operator*(const real_t& s) const;
- _FORCE_INLINE_ Quat operator/(const real_t& s) const;
-
+ _FORCE_INLINE_ Quat operator*(const real_t &s) const;
+ _FORCE_INLINE_ Quat operator/(const real_t &s) const;
- _FORCE_INLINE_ bool operator==(const Quat& p_quat) const;
- _FORCE_INLINE_ bool operator!=(const Quat& p_quat) const;
+ _FORCE_INLINE_ bool operator==(const Quat &p_quat) const;
+ _FORCE_INLINE_ bool operator!=(const Quat &p_quat) const;
operator String() const;
- inline void set( real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
- x=p_x; y=p_y; z=p_z; w=p_w;
+ inline void set(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
+ x = p_x;
+ y = p_y;
+ z = p_z;
+ w = p_w;
}
inline Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
- x=p_x; y=p_y; z=p_z; w=p_w;
+ x = p_x;
+ y = p_y;
+ z = p_z;
+ w = p_w;
}
- Quat(const Vector3& axis, const real_t& angle);
+ Quat(const Vector3 &axis, const real_t &angle);
- Quat(const Vector3& v0, const Vector3& v1) // shortest arc
+ Quat(const Vector3 &v0, const Vector3 &v1) // shortest arc
{
Vector3 c = v0.cross(v1);
- real_t d = v0.dot(v1);
+ real_t d = v0.dot(v1);
if (d < -1.0 + CMP_EPSILON) {
- x=0;
- y=1;
- z=0;
- w=0;
+ x = 0;
+ y = 1;
+ z = 0;
+ w = 0;
} else {
- real_t s = Math::sqrt((1.0 + d) * 2.0);
+ real_t s = Math::sqrt((1.0 + d) * 2.0);
real_t rs = 1.0 / s;
- x=c.x*rs;
- y=c.y*rs;
- z=c.z*rs;
- w=s * 0.5;
+ x = c.x * rs;
+ y = c.y * rs;
+ z = c.z * rs;
+ w = s * 0.5;
}
}
- inline Quat() {x=y=z=0; w=1; }
-
-
+ inline Quat() {
+ x = y = z = 0;
+ w = 1;
+ }
};
-
-real_t Quat::dot(const Quat& q) const {
- return x * q.x+y * q.y+z * q.z+w * q.w;
+real_t Quat::dot(const Quat &q) const {
+ return x * q.x + y * q.y + z * q.z + w * q.w;
}
real_t Quat::length_squared() const {
return dot(*this);
}
-void Quat::operator+=(const Quat& q) {
- x += q.x; y += q.y; z += q.z; w += q.w;
+void Quat::operator+=(const Quat &q) {
+ x += q.x;
+ y += q.y;
+ z += q.z;
+ w += q.w;
}
-void Quat::operator-=(const Quat& q) {
- x -= q.x; y -= q.y; z -= q.z; w -= q.w;
+void Quat::operator-=(const Quat &q) {
+ x -= q.x;
+ y -= q.y;
+ z -= q.z;
+ w -= q.w;
}
-void Quat::operator*=(const real_t& s) {
- x *= s; y *= s; z *= s; w *= s;
+void Quat::operator*=(const real_t &s) {
+ x *= s;
+ y *= s;
+ z *= s;
+ w *= s;
}
-
-void Quat::operator/=(const real_t& s) {
+void Quat::operator/=(const real_t &s) {
*this *= 1.0 / s;
}
-Quat Quat::operator+(const Quat& q2) const {
- const Quat& q1 = *this;
- return Quat( q1.x+q2.x, q1.y+q2.y, q1.z+q2.z, q1.w+q2.w );
+Quat Quat::operator+(const Quat &q2) const {
+ const Quat &q1 = *this;
+ return Quat(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w);
}
-Quat Quat::operator-(const Quat& q2) const {
- const Quat& q1 = *this;
- return Quat( q1.x-q2.x, q1.y-q2.y, q1.z-q2.z, q1.w-q2.w);
+Quat Quat::operator-(const Quat &q2) const {
+ const Quat &q1 = *this;
+ return Quat(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
}
Quat Quat::operator-() const {
- const Quat& q2 = *this;
- return Quat( -q2.x, -q2.y, -q2.z, -q2.w);
+ const Quat &q2 = *this;
+ return Quat(-q2.x, -q2.y, -q2.z, -q2.w);
}
-Quat Quat::operator*(const real_t& s) const {
+Quat Quat::operator*(const real_t &s) const {
return Quat(x * s, y * s, z * s, w * s);
}
-Quat Quat::operator/(const real_t& s) const {
+Quat Quat::operator/(const real_t &s) const {
return *this * (1.0 / s);
}
-
-bool Quat::operator==(const Quat& p_quat) const {
- return x==p_quat.x && y==p_quat.y && z==p_quat.z && w==p_quat.w;
+bool Quat::operator==(const Quat &p_quat) const {
+ return x == p_quat.x && y == p_quat.y && z == p_quat.z && w == p_quat.w;
}
-bool Quat::operator!=(const Quat& p_quat) const {
- return x!=p_quat.x || y!=p_quat.y || z!=p_quat.z || w!=p_quat.w;
+bool Quat::operator!=(const Quat &p_quat) const {
+ return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w;
}
-
#endif
diff --git a/core/math/quick_hull.cpp b/core/math/quick_hull.cpp
index 32fc0e01e8..a235d1cf32 100644
--- a/core/math/quick_hull.cpp
+++ b/core/math/quick_hull.cpp
@@ -29,49 +29,44 @@
#include "quick_hull.h"
#include "map.h"
-uint32_t QuickHull::debug_stop_after=0xFFFFFFFF;
-
-Error QuickHull::build(const Vector<Vector3>& p_points, Geometry::MeshData &r_mesh) {
+uint32_t QuickHull::debug_stop_after = 0xFFFFFFFF;
+Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_mesh) {
static const real_t over_tolerance = 0.0001;
/* CREATE AABB VOLUME */
Rect3 aabb;
- for(int i=0;i<p_points.size();i++) {
+ for (int i = 0; i < p_points.size(); i++) {
- if (i==0) {
- aabb.pos=p_points[i];
+ if (i == 0) {
+ aabb.pos = p_points[i];
} else {
aabb.expand_to(p_points[i]);
}
}
-
- if (aabb.size==Vector3()) {
+ if (aabb.size == Vector3()) {
return ERR_CANT_CREATE;
}
-
Vector<bool> valid_points;
valid_points.resize(p_points.size());
Set<Vector3> valid_cache;
- for(int i=0;i<p_points.size();i++) {
+ for (int i = 0; i < p_points.size(); i++) {
Vector3 sp = p_points[i].snapped(0.0001);
if (valid_cache.has(sp)) {
- valid_points[i]=false;
+ valid_points[i] = false;
//print_line("INVALIDATED: "+itos(i));
- }else {
- valid_points[i]=true;
+ } else {
+ valid_points[i] = true;
valid_cache.insert(sp);
}
}
-
-
/* CREATE INITIAL SIMPLEX */
int longest_axis = aabb.get_longest_axis_index();
@@ -80,46 +75,44 @@ Error QuickHull::build(const Vector<Vector3>& p_points, Geometry::MeshData &r_me
int simplex[4];
{
- real_t max,min;
+ real_t max, min;
- for(int i=0;i<p_points.size();i++) {
+ for (int i = 0; i < p_points.size(); i++) {
if (!valid_points[i])
continue;
real_t d = p_points[i][longest_axis];
- if (i==0 || d < min) {
+ if (i == 0 || d < min) {
- simplex[0]=i;
- min=d;
+ simplex[0] = i;
+ min = d;
}
- if (i==0 || d > max) {
- simplex[1]=i;
- max=d;
+ if (i == 0 || d > max) {
+ simplex[1] = i;
+ max = d;
}
-
}
}
//third vertex is one most further away from the line
-
{
real_t maxd;
- Vector3 rel12 = p_points[simplex[0]] - p_points[simplex[1]];
+ Vector3 rel12 = p_points[simplex[0]] - p_points[simplex[1]];
- for(int i=0;i<p_points.size();i++) {
+ for (int i = 0; i < p_points.size(); i++) {
if (!valid_points[i])
continue;
- Vector3 n = rel12.cross(p_points[simplex[0]]-p_points[i]).cross(rel12).normalized();
- real_t d = Math::abs(n.dot(p_points[simplex[0]])-n.dot(p_points[i]));
+ Vector3 n = rel12.cross(p_points[simplex[0]] - p_points[i]).cross(rel12).normalized();
+ real_t d = Math::abs(n.dot(p_points[simplex[0]]) - n.dot(p_points[i]));
- if (i==0 || d>maxd) {
+ if (i == 0 || d > maxd) {
- maxd=d;
- simplex[2]=i;
+ maxd = d;
+ simplex[2] = i;
}
}
}
@@ -128,102 +121,92 @@ Error QuickHull::build(const Vector<Vector3>& p_points, Geometry::MeshData &r_me
{
real_t maxd;
- Plane p(p_points[simplex[0]],p_points[simplex[1]],p_points[simplex[2]]);
+ Plane p(p_points[simplex[0]], p_points[simplex[1]], p_points[simplex[2]]);
- for(int i=0;i<p_points.size();i++) {
+ for (int i = 0; i < p_points.size(); i++) {
if (!valid_points[i])
continue;
real_t d = Math::abs(p.distance_to(p_points[i]));
- if (i==0 || d>maxd) {
+ if (i == 0 || d > maxd) {
- maxd=d;
- simplex[3]=i;
+ maxd = d;
+ simplex[3] = i;
}
}
}
-
//compute center of simplex, this is a point always warranted to be inside
Vector3 center;
- for(int i=0;i<4;i++) {
- center+=p_points[simplex[i]];
+ for (int i = 0; i < 4; i++) {
+ center += p_points[simplex[i]];
}
- center/=4.0;
+ center /= 4.0;
//add faces
List<Face> faces;
- for(int i=0;i<4;i++) {
+ for (int i = 0; i < 4; i++) {
- static const int face_order[4][3]={
- {0,1,2},
- {0,1,3},
- {0,2,3},
- {1,2,3}
+ static const int face_order[4][3] = {
+ { 0, 1, 2 },
+ { 0, 1, 3 },
+ { 0, 2, 3 },
+ { 1, 2, 3 }
};
Face f;
- for(int j=0;j<3;j++) {
- f.vertices[j]=simplex[face_order[i][j]];
+ for (int j = 0; j < 3; j++) {
+ f.vertices[j] = simplex[face_order[i][j]];
}
-
- Plane p(p_points[f.vertices[0]],p_points[f.vertices[1]],p_points[f.vertices[2]]);
+ Plane p(p_points[f.vertices[0]], p_points[f.vertices[1]], p_points[f.vertices[2]]);
if (p.is_point_over(center)) {
//flip face to clockwise if facing inwards
- SWAP( f.vertices[0], f.vertices[1] );
- p=-p;
+ SWAP(f.vertices[0], f.vertices[1]);
+ p = -p;
}
-
f.plane = p;
faces.push_back(f);
-
}
-
/* COMPUTE AVAILABLE VERTICES */
- for(int i=0;i<p_points.size();i++) {
+ for (int i = 0; i < p_points.size(); i++) {
- if (i==simplex[0])
+ if (i == simplex[0])
continue;
- if (i==simplex[1])
+ if (i == simplex[1])
continue;
- if (i==simplex[2])
+ if (i == simplex[2])
continue;
- if (i==simplex[3])
+ if (i == simplex[3])
continue;
if (!valid_points[i])
continue;
- for(List<Face>::Element *E=faces.front();E;E=E->next()) {
+ for (List<Face>::Element *E = faces.front(); E; E = E->next()) {
- if (E->get().plane.distance_to(p_points[i]) > over_tolerance ) {
+ if (E->get().plane.distance_to(p_points[i]) > over_tolerance) {
E->get().points_over.push_back(i);
break;
}
}
-
-
-
}
faces.sort(); // sort them, so the ones with points are in the back
-
/* BUILD HULL */
-
//poop face (while still remain)
//find further away point
//find lit faces
@@ -231,72 +214,68 @@ Error QuickHull::build(const Vector<Vector3>& p_points, Geometry::MeshData &r_me
//build new faces with horizon edges, them assign points side from all lit faces
//remove lit faces
-
uint32_t debug_stop = debug_stop_after;
- while(debug_stop>0 && faces.back()->get().points_over.size()) {
+ while (debug_stop > 0 && faces.back()->get().points_over.size()) {
debug_stop--;
- Face& f = faces.back()->get();
+ Face &f = faces.back()->get();
//find vertex most outside
- int next=-1;
- real_t next_d=0;
+ int next = -1;
+ real_t next_d = 0;
- for(int i=0;i<f.points_over.size();i++) {
+ for (int i = 0; i < f.points_over.size(); i++) {
real_t d = f.plane.distance_to(p_points[f.points_over[i]]);
if (d > next_d) {
- next_d=d;
- next=i;
+ next_d = d;
+ next = i;
}
}
- ERR_FAIL_COND_V(next==-1,ERR_BUG);
-
-
+ ERR_FAIL_COND_V(next == -1, ERR_BUG);
Vector3 v = p_points[f.points_over[next]];
//find lit faces and lit edges
- List< List<Face>::Element* > lit_faces; //lit face is a death sentence
+ List<List<Face>::Element *> lit_faces; //lit face is a death sentence
- Map<Edge,FaceConnect> lit_edges; //create this on the flight, should not be that bad for performance and simplifies code a lot
+ Map<Edge, FaceConnect> lit_edges; //create this on the flight, should not be that bad for performance and simplifies code a lot
- for(List<Face>::Element *E=faces.front();E;E=E->next()) {
+ for (List<Face>::Element *E = faces.front(); E; E = E->next()) {
- if (E->get().plane.distance_to(v) >0 ) {
+ if (E->get().plane.distance_to(v) > 0) {
lit_faces.push_back(E);
- for(int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
uint32_t a = E->get().vertices[i];
- uint32_t b = E->get().vertices[(i+1)%3];
- Edge e(a,b);
+ uint32_t b = E->get().vertices[(i + 1) % 3];
+ Edge e(a, b);
- Map<Edge,FaceConnect>::Element *F=lit_edges.find(e);
+ Map<Edge, FaceConnect>::Element *F = lit_edges.find(e);
if (!F) {
- F=lit_edges.insert(e,FaceConnect());
+ F = lit_edges.insert(e, FaceConnect());
}
- if (e.vertices[0]==a) {
+ if (e.vertices[0] == a) {
//left
- F->get().left=E;
+ F->get().left = E;
} else {
- F->get().right=E;
+ F->get().right = E;
}
}
}
}
-
//create new faces from horizon edges
- List< List<Face>::Element* > new_faces; //new faces
+ List<List<Face>::Element *> new_faces; //new faces
- for(Map<Edge,FaceConnect>::Element *E=lit_edges.front();E;E=E->next()) {
+ for (Map<Edge, FaceConnect>::Element *E = lit_edges.front(); E; E = E->next()) {
- FaceConnect& fc = E->get();
+ FaceConnect &fc = E->get();
if (fc.left && fc.right) {
continue; //edge is uninteresting, not on horizont
}
@@ -304,50 +283,48 @@ Error QuickHull::build(const Vector<Vector3>& p_points, Geometry::MeshData &r_me
//create new face!
Face face;
- face.vertices[0]=f.points_over[next];
- face.vertices[1]=E->key().vertices[0];
- face.vertices[2]=E->key().vertices[1];
+ face.vertices[0] = f.points_over[next];
+ face.vertices[1] = E->key().vertices[0];
+ face.vertices[2] = E->key().vertices[1];
- Plane p(p_points[face.vertices[0]],p_points[face.vertices[1]],p_points[face.vertices[2]]);
+ Plane p(p_points[face.vertices[0]], p_points[face.vertices[1]], p_points[face.vertices[2]]);
if (p.is_point_over(center)) {
//flip face to clockwise if facing inwards
- SWAP( face.vertices[0], face.vertices[1] );
+ SWAP(face.vertices[0], face.vertices[1]);
p = -p;
}
face.plane = p;
- new_faces.push_back( faces.push_back(face) );
+ new_faces.push_back(faces.push_back(face));
}
//distribute points into new faces
- for(List< List<Face>::Element* >::Element *F=lit_faces.front();F;F=F->next()) {
+ for (List<List<Face>::Element *>::Element *F = lit_faces.front(); F; F = F->next()) {
Face &lf = F->get()->get();
- for(int i=0;i<lf.points_over.size();i++) {
+ for (int i = 0; i < lf.points_over.size(); i++) {
- if (lf.points_over[i]==f.points_over[next]) //do not add current one
+ if (lf.points_over[i] == f.points_over[next]) //do not add current one
continue;
Vector3 p = p_points[lf.points_over[i]];
- for (List< List<Face>::Element* >::Element *E=new_faces.front();E;E=E->next()) {
+ for (List<List<Face>::Element *>::Element *E = new_faces.front(); E; E = E->next()) {
Face &f2 = E->get()->get();
- if (f2.plane.distance_to(p)>over_tolerance) {
+ if (f2.plane.distance_to(p) > over_tolerance) {
f2.points_over.push_back(lf.points_over[i]);
break;
}
}
-
-
}
}
//erase lit faces
- while(lit_faces.size()) {
+ while (lit_faces.size()) {
faces.erase(lit_faces.front()->get());
lit_faces.pop_front();
@@ -355,129 +332,115 @@ Error QuickHull::build(const Vector<Vector3>& p_points, Geometry::MeshData &r_me
//put faces that contain no points on the front
- for (List< List<Face>::Element* >::Element *E=new_faces.front();E;E=E->next()) {
+ for (List<List<Face>::Element *>::Element *E = new_faces.front(); E; E = E->next()) {
Face &f2 = E->get()->get();
- if (f2.points_over.size()==0) {
+ if (f2.points_over.size() == 0) {
faces.move_to_front(E->get());
}
}
//whew, done with iteration, go next
-
-
-
}
/* CREATE MESHDATA */
-
//make a map of edges again
- Map<Edge,RetFaceConnect> ret_edges;
+ Map<Edge, RetFaceConnect> ret_edges;
List<Geometry::MeshData::Face> ret_faces;
-
- for(List<Face>::Element *E=faces.front();E;E=E->next()) {
+ for (List<Face>::Element *E = faces.front(); E; E = E->next()) {
Geometry::MeshData::Face f;
f.plane = E->get().plane;
-
-
- for(int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
f.indices.push_back(E->get().vertices[i]);
}
List<Geometry::MeshData::Face>::Element *F = ret_faces.push_back(f);
- for(int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
uint32_t a = E->get().vertices[i];
- uint32_t b = E->get().vertices[(i+1)%3];
- Edge e(a,b);
+ uint32_t b = E->get().vertices[(i + 1) % 3];
+ Edge e(a, b);
- Map<Edge,RetFaceConnect>::Element *G=ret_edges.find(e);
+ Map<Edge, RetFaceConnect>::Element *G = ret_edges.find(e);
if (!G) {
- G=ret_edges.insert(e,RetFaceConnect());
+ G = ret_edges.insert(e, RetFaceConnect());
}
- if (e.vertices[0]==a) {
+ if (e.vertices[0] == a) {
//left
- G->get().left=F;
+ G->get().left = F;
} else {
- G->get().right=F;
+ G->get().right = F;
}
}
}
//fill faces
- for (List<Geometry::MeshData::Face>::Element *E=ret_faces.front();E;E=E->next()) {
+ for (List<Geometry::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
- Geometry::MeshData::Face& f = E->get();
+ Geometry::MeshData::Face &f = E->get();
- for(int i=0;i<f.indices.size();i++) {
+ for (int i = 0; i < f.indices.size(); i++) {
uint32_t a = E->get().indices[i];
- uint32_t b = E->get().indices[(i+1)%f.indices.size()];
- Edge e(a,b);
+ uint32_t b = E->get().indices[(i + 1) % f.indices.size()];
+ Edge e(a, b);
- Map<Edge,RetFaceConnect>::Element *F=ret_edges.find(e);
+ Map<Edge, RetFaceConnect>::Element *F = ret_edges.find(e);
ERR_CONTINUE(!F);
List<Geometry::MeshData::Face>::Element *O = F->get().left == E ? F->get().right : F->get().left;
- ERR_CONTINUE(O==E);
- ERR_CONTINUE(O==NULL);
+ ERR_CONTINUE(O == E);
+ ERR_CONTINUE(O == NULL);
if (O->get().plane.is_almost_like(f.plane)) {
//merge and delete edge and contiguous face, while repointing edges (uuugh!)
int ois = O->get().indices.size();
- int merged=0;
-
+ int merged = 0;
- for(int j=0;j<ois;j++) {
+ for (int j = 0; j < ois; j++) {
//search a
- if (O->get().indices[j]==a) {
+ if (O->get().indices[j] == a) {
//append the rest
- for(int k=0;k<ois;k++) {
+ for (int k = 0; k < ois; k++) {
- int idx = O->get().indices[(k+j)%ois];
- int idxn = O->get().indices[(k+j+1)%ois];
- if (idx==b && idxn==a) {//already have b!
+ int idx = O->get().indices[(k + j) % ois];
+ int idxn = O->get().indices[(k + j + 1) % ois];
+ if (idx == b && idxn == a) { //already have b!
break;
}
- if (idx!=a) {
- f.indices.insert(i+1,idx);
+ if (idx != a) {
+ f.indices.insert(i + 1, idx);
i++;
merged++;
}
- Edge e2(idx,idxn);
+ Edge e2(idx, idxn);
- Map<Edge,RetFaceConnect>::Element *F2=ret_edges.find(e2);
+ Map<Edge, RetFaceConnect>::Element *F2 = ret_edges.find(e2);
ERR_CONTINUE(!F2);
//change faceconnect, point to this face instead
if (F2->get().left == O)
- F2->get().left=E;
+ F2->get().left = E;
else if (F2->get().right == O)
- F2->get().right=E;
-
+ F2->get().right = E;
}
break;
}
}
-
ret_edges.erase(F); //remove the edge
ret_faces.erase(O); //remove the face
-
-
}
-
}
-
}
//fill mesh
@@ -485,26 +448,24 @@ Error QuickHull::build(const Vector<Vector3>& p_points, Geometry::MeshData &r_me
r_mesh.faces.resize(ret_faces.size());
//print_line("FACECOUNT: "+itos(r_mesh.faces.size()));
- int idx=0;
- for (List<Geometry::MeshData::Face>::Element *E=ret_faces.front();E;E=E->next()) {
- r_mesh.faces[idx++]=E->get();
-
-
+ int idx = 0;
+ for (List<Geometry::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
+ r_mesh.faces[idx++] = E->get();
}
r_mesh.edges.resize(ret_edges.size());
- idx=0;
- for(Map<Edge,RetFaceConnect>::Element *E=ret_edges.front();E;E=E->next()) {
+ idx = 0;
+ for (Map<Edge, RetFaceConnect>::Element *E = ret_edges.front(); E; E = E->next()) {
Geometry::MeshData::Edge e;
- e.a=E->key().vertices[0];
- e.b=E->key().vertices[1];
- r_mesh.edges[idx++]=e;
+ e.a = E->key().vertices[0];
+ e.b = E->key().vertices[1];
+ r_mesh.edges[idx++] = e;
}
- r_mesh.vertices=p_points;
+ r_mesh.vertices = p_points;
//r_mesh.optimize_vertices();
-/*
+ /*
print_line("FACES: "+itos(r_mesh.faces.size()));
print_line("EDGES: "+itos(r_mesh.edges.size()));
print_line("VERTICES: "+itos(r_mesh.vertices.size()));
diff --git a/core/math/quick_hull.h b/core/math/quick_hull.h
index 7bd23d31f2..43a802e6bd 100644
--- a/core/math/quick_hull.h
+++ b/core/math/quick_hull.h
@@ -29,16 +29,14 @@
#ifndef QUICK_HULL_H
#define QUICK_HULL_H
+#include "geometry.h"
+#include "list.h"
#include "rect3.h"
#include "set.h"
-#include "list.h"
-#include "geometry.h"
class QuickHull {
public:
-
-
struct Edge {
union {
@@ -46,19 +44,18 @@ public:
uint64_t id;
};
-
- bool operator<(const Edge& p_edge) const {
+ bool operator<(const Edge &p_edge) const {
return id < p_edge.id;
}
- Edge(int p_vtx_a=0,int p_vtx_b=0) {
+ Edge(int p_vtx_a = 0, int p_vtx_b = 0) {
- if (p_vtx_a>p_vtx_b) {
- SWAP(p_vtx_a,p_vtx_b);
+ if (p_vtx_a > p_vtx_b) {
+ SWAP(p_vtx_a, p_vtx_b);
}
- vertices[0]=p_vtx_a;
- vertices[1]=p_vtx_b;
+ vertices[0] = p_vtx_a;
+ vertices[1] = p_vtx_b;
}
};
@@ -68,28 +65,31 @@ public:
int vertices[3];
Vector<int> points_over;
- bool operator<(const Face& p_face) const {
+ bool operator<(const Face &p_face) const {
return points_over.size() < p_face.points_over.size();
}
-
};
-private:
+private:
struct FaceConnect {
- List<Face>::Element *left,*right;
- FaceConnect() { left=NULL; right=NULL; }
+ List<Face>::Element *left, *right;
+ FaceConnect() {
+ left = NULL;
+ right = NULL;
+ }
};
struct RetFaceConnect {
- List<Geometry::MeshData::Face>::Element *left,*right;
- RetFaceConnect() { left=NULL; right=NULL; }
+ List<Geometry::MeshData::Face>::Element *left, *right;
+ RetFaceConnect() {
+ left = NULL;
+ right = NULL;
+ }
};
public:
-
static uint32_t debug_stop_after;
- static Error build(const Vector<Vector3>& p_points,Geometry::MeshData& r_mesh);
-
+ static Error build(const Vector<Vector3> &p_points, Geometry::MeshData &r_mesh);
};
#endif // QUICK_HULL_H
diff --git a/core/math/rect3.cpp b/core/math/rect3.cpp
index d3f95b89e8..c0cd66d9bb 100644
--- a/core/math/rect3.cpp
+++ b/core/math/rect3.cpp
@@ -32,94 +32,87 @@
real_t Rect3::get_area() const {
- return size.x*size.y*size.z;
-
+ return size.x * size.y * size.z;
}
-bool Rect3::operator==(const Rect3& p_rval) const {
-
- return ((pos==p_rval.pos) && (size==p_rval.size));
+bool Rect3::operator==(const Rect3 &p_rval) const {
+ return ((pos == p_rval.pos) && (size == p_rval.size));
}
-bool Rect3::operator!=(const Rect3& p_rval) const {
-
- return ((pos!=p_rval.pos) || (size!=p_rval.size));
+bool Rect3::operator!=(const Rect3 &p_rval) const {
+ return ((pos != p_rval.pos) || (size != p_rval.size));
}
-void Rect3::merge_with(const Rect3& p_aabb) {
+void Rect3::merge_with(const Rect3 &p_aabb) {
- Vector3 beg_1,beg_2;
- Vector3 end_1,end_2;
- Vector3 min,max;
+ Vector3 beg_1, beg_2;
+ Vector3 end_1, end_2;
+ Vector3 min, max;
- beg_1=pos;
- beg_2=p_aabb.pos;
- end_1=Vector3(size.x,size.y,size.z)+beg_1;
- end_2=Vector3(p_aabb.size.x,p_aabb.size.y,p_aabb.size.z)+beg_2;
+ beg_1 = pos;
+ beg_2 = p_aabb.pos;
+ end_1 = Vector3(size.x, size.y, size.z) + beg_1;
+ end_2 = Vector3(p_aabb.size.x, p_aabb.size.y, p_aabb.size.z) + beg_2;
- min.x=(beg_1.x<beg_2.x)?beg_1.x:beg_2.x;
- min.y=(beg_1.y<beg_2.y)?beg_1.y:beg_2.y;
- min.z=(beg_1.z<beg_2.z)?beg_1.z:beg_2.z;
+ min.x = (beg_1.x < beg_2.x) ? beg_1.x : beg_2.x;
+ min.y = (beg_1.y < beg_2.y) ? beg_1.y : beg_2.y;
+ min.z = (beg_1.z < beg_2.z) ? beg_1.z : beg_2.z;
- max.x=(end_1.x>end_2.x)?end_1.x:end_2.x;
- max.y=(end_1.y>end_2.y)?end_1.y:end_2.y;
- max.z=(end_1.z>end_2.z)?end_1.z:end_2.z;
+ max.x = (end_1.x > end_2.x) ? end_1.x : end_2.x;
+ max.y = (end_1.y > end_2.y) ? end_1.y : end_2.y;
+ max.z = (end_1.z > end_2.z) ? end_1.z : end_2.z;
- pos=min;
- size=max-min;
+ pos = min;
+ size = max - min;
}
-Rect3 Rect3::intersection(const Rect3& p_aabb) const {
+Rect3 Rect3::intersection(const Rect3 &p_aabb) const {
- Vector3 src_min=pos;
- Vector3 src_max=pos+size;
- Vector3 dst_min=p_aabb.pos;
- Vector3 dst_max=p_aabb.pos+p_aabb.size;
+ Vector3 src_min = pos;
+ Vector3 src_max = pos + size;
+ Vector3 dst_min = p_aabb.pos;
+ Vector3 dst_max = p_aabb.pos + p_aabb.size;
- Vector3 min,max;
+ Vector3 min, max;
- if (src_min.x > dst_max.x || src_max.x < dst_min.x )
+ if (src_min.x > dst_max.x || src_max.x < dst_min.x)
return Rect3();
else {
- min.x= ( src_min.x > dst_min.x ) ? src_min.x :dst_min.x;
- max.x= ( src_max.x < dst_max.x ) ? src_max.x :dst_max.x;
-
+ min.x = (src_min.x > dst_min.x) ? src_min.x : dst_min.x;
+ max.x = (src_max.x < dst_max.x) ? src_max.x : dst_max.x;
}
- if (src_min.y > dst_max.y || src_max.y < dst_min.y )
+ if (src_min.y > dst_max.y || src_max.y < dst_min.y)
return Rect3();
else {
- min.y= ( src_min.y > dst_min.y ) ? src_min.y :dst_min.y;
- max.y= ( src_max.y < dst_max.y ) ? src_max.y :dst_max.y;
-
+ min.y = (src_min.y > dst_min.y) ? src_min.y : dst_min.y;
+ max.y = (src_max.y < dst_max.y) ? src_max.y : dst_max.y;
}
- if (src_min.z > dst_max.z || src_max.z < dst_min.z )
+ if (src_min.z > dst_max.z || src_max.z < dst_min.z)
return Rect3();
else {
- min.z= ( src_min.z > dst_min.z ) ? src_min.z :dst_min.z;
- max.z= ( src_max.z < dst_max.z ) ? src_max.z :dst_max.z;
-
+ min.z = (src_min.z > dst_min.z) ? src_min.z : dst_min.z;
+ max.z = (src_max.z < dst_max.z) ? src_max.z : dst_max.z;
}
-
- return Rect3( min, max-min );
+ return Rect3(min, max - min);
}
-bool Rect3::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip,Vector3* r_normal) const {
+bool Rect3::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const {
Vector3 c1, c2;
- Vector3 end = pos+size;
- real_t near=-1e20;
- real_t far=1e20;
- int axis=0;
+ Vector3 end = pos + size;
+ real_t near = -1e20;
+ real_t far = 1e20;
+ int axis = 0;
- for (int i=0;i<3;i++){
- if (p_dir[i] == 0){
+ for (int i = 0; i < 3; i++) {
+ if (p_dir[i] == 0) {
if ((p_from[i] < pos[i]) || (p_from[i] > end[i])) {
return false;
}
@@ -127,71 +120,69 @@ bool Rect3::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3*
c1[i] = (pos[i] - p_from[i]) / p_dir[i];
c2[i] = (end[i] - p_from[i]) / p_dir[i];
- if(c1[i] > c2[i]){
- SWAP(c1,c2);
+ if (c1[i] > c2[i]) {
+ SWAP(c1, c2);
}
- if (c1[i] > near){
+ if (c1[i] > near) {
near = c1[i];
- axis=i;
+ axis = i;
}
- if (c2[i] < far){
+ if (c2[i] < far) {
far = c2[i];
}
- if( (near > far) || (far < 0) ){
+ if ((near > far) || (far < 0)) {
return false;
}
}
}
if (r_clip)
- *r_clip=c1;
+ *r_clip = c1;
if (r_normal) {
- *r_normal=Vector3();
- (*r_normal)[axis]=p_dir[axis]?-1:1;
+ *r_normal = Vector3();
+ (*r_normal)[axis] = p_dir[axis] ? -1 : 1;
}
return true;
-
}
+bool Rect3::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const {
-bool Rect3::intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip,Vector3* r_normal) const {
+ real_t min = 0, max = 1;
+ int axis = 0;
+ real_t sign = 0;
- real_t min=0,max=1;
- int axis=0;
- real_t sign=0;
-
- for(int i=0;i<3;i++) {
- real_t seg_from=p_from[i];
- real_t seg_to=p_to[i];
- real_t box_begin=pos[i];
- real_t box_end=box_begin+size[i];
- real_t cmin,cmax;
+ for (int i = 0; i < 3; i++) {
+ real_t seg_from = p_from[i];
+ real_t seg_to = p_to[i];
+ real_t box_begin = pos[i];
+ real_t box_end = box_begin + size[i];
+ real_t cmin, cmax;
real_t csign;
if (seg_from < seg_to) {
if (seg_from > box_end || seg_to < box_begin)
return false;
- real_t length=seg_to-seg_from;
- cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
- cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
- csign=-1.0;
+ real_t length = seg_to - seg_from;
+ cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
+ cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
+ csign = -1.0;
} else {
if (seg_to > box_end || seg_from < box_begin)
return false;
- real_t length=seg_to-seg_from;
- cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
- cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
- csign=1.0;
+ real_t length = seg_to - seg_from;
+ cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
+ cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
+ csign = 1.0;
}
if (cmin > min) {
min = cmin;
- axis=i;
- sign=csign;
+ axis = i;
+ sign = csign;
}
if (cmax < max)
max = cmax;
@@ -199,220 +190,210 @@ bool Rect3::intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector
return false;
}
-
- Vector3 rel=p_to-p_from;
+ Vector3 rel = p_to - p_from;
if (r_normal) {
Vector3 normal;
- normal[axis]=sign;
- *r_normal=normal;
+ normal[axis] = sign;
+ *r_normal = normal;
}
if (r_clip)
- *r_clip=p_from+rel*min;
+ *r_clip = p_from + rel * min;
return true;
-
}
-
bool Rect3::intersects_plane(const Plane &p_plane) const {
Vector3 points[8] = {
- Vector3( pos.x , pos.y , pos.z ),
- Vector3( pos.x , pos.y , pos.z+size.z ),
- Vector3( pos.x , pos.y+size.y , pos.z ),
- Vector3( pos.x , pos.y+size.y , pos.z+size.z ),
- Vector3( pos.x+size.x , pos.y , pos.z ),
- Vector3( pos.x+size.x , pos.y , pos.z+size.z ),
- Vector3( pos.x+size.x , pos.y+size.y , pos.z ),
- Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ),
+ Vector3(pos.x, pos.y, pos.z),
+ Vector3(pos.x, pos.y, pos.z + size.z),
+ Vector3(pos.x, pos.y + size.y, pos.z),
+ Vector3(pos.x, pos.y + size.y, pos.z + size.z),
+ Vector3(pos.x + size.x, pos.y, pos.z),
+ Vector3(pos.x + size.x, pos.y, pos.z + size.z),
+ Vector3(pos.x + size.x, pos.y + size.y, pos.z),
+ Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z),
};
- bool over=false;
- bool under=false;
+ bool over = false;
+ bool under = false;
- for (int i=0;i<8;i++) {
+ for (int i = 0; i < 8; i++) {
- if (p_plane.distance_to(points[i])>0)
- over=true;
+ if (p_plane.distance_to(points[i]) > 0)
+ over = true;
else
- under=true;
-
+ under = true;
}
return under && over;
}
-
-
Vector3 Rect3::get_longest_axis() const {
- Vector3 axis(1,0,0);
- real_t max_size=size.x;
+ Vector3 axis(1, 0, 0);
+ real_t max_size = size.x;
- if (size.y > max_size ) {
- axis=Vector3(0,1,0);
- max_size=size.y;
+ if (size.y > max_size) {
+ axis = Vector3(0, 1, 0);
+ max_size = size.y;
}
- if (size.z > max_size ) {
- axis=Vector3(0,0,1);
- max_size=size.z;
+ if (size.z > max_size) {
+ axis = Vector3(0, 0, 1);
+ max_size = size.z;
}
return axis;
}
int Rect3::get_longest_axis_index() const {
- int axis=0;
- real_t max_size=size.x;
+ int axis = 0;
+ real_t max_size = size.x;
- if (size.y > max_size ) {
- axis=1;
- max_size=size.y;
+ if (size.y > max_size) {
+ axis = 1;
+ max_size = size.y;
}
- if (size.z > max_size ) {
- axis=2;
- max_size=size.z;
+ if (size.z > max_size) {
+ axis = 2;
+ max_size = size.z;
}
return axis;
}
-
Vector3 Rect3::get_shortest_axis() const {
- Vector3 axis(1,0,0);
- real_t max_size=size.x;
+ Vector3 axis(1, 0, 0);
+ real_t max_size = size.x;
- if (size.y < max_size ) {
- axis=Vector3(0,1,0);
- max_size=size.y;
+ if (size.y < max_size) {
+ axis = Vector3(0, 1, 0);
+ max_size = size.y;
}
- if (size.z < max_size ) {
- axis=Vector3(0,0,1);
- max_size=size.z;
+ if (size.z < max_size) {
+ axis = Vector3(0, 0, 1);
+ max_size = size.z;
}
return axis;
}
int Rect3::get_shortest_axis_index() const {
- int axis=0;
- real_t max_size=size.x;
+ int axis = 0;
+ real_t max_size = size.x;
- if (size.y < max_size ) {
- axis=1;
- max_size=size.y;
+ if (size.y < max_size) {
+ axis = 1;
+ max_size = size.y;
}
- if (size.z < max_size ) {
- axis=2;
- max_size=size.z;
+ if (size.z < max_size) {
+ axis = 2;
+ max_size = size.z;
}
return axis;
}
-Rect3 Rect3::merge(const Rect3& p_with) const {
+Rect3 Rect3::merge(const Rect3 &p_with) const {
- Rect3 aabb=*this;
+ Rect3 aabb = *this;
aabb.merge_with(p_with);
return aabb;
}
-Rect3 Rect3::expand(const Vector3& p_vector) const {
- Rect3 aabb=*this;
+Rect3 Rect3::expand(const Vector3 &p_vector) const {
+ Rect3 aabb = *this;
aabb.expand_to(p_vector);
return aabb;
-
}
Rect3 Rect3::grow(real_t p_by) const {
- Rect3 aabb=*this;
+ Rect3 aabb = *this;
aabb.grow_by(p_by);
return aabb;
}
-void Rect3::get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const {
+void Rect3::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const {
- ERR_FAIL_INDEX(p_edge,12);
- switch(p_edge) {
+ ERR_FAIL_INDEX(p_edge, 12);
+ switch (p_edge) {
- case 0:{
+ case 0: {
- r_from=Vector3( pos.x+size.x , pos.y , pos.z );
- r_to=Vector3( pos.x , pos.y , pos.z );
+ r_from = Vector3(pos.x + size.x, pos.y, pos.z);
+ r_to = Vector3(pos.x, pos.y, pos.z);
} break;
- case 1:{
+ case 1: {
- r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
- r_to=Vector3( pos.x+size.x , pos.y , pos.z );
+ r_from = Vector3(pos.x + size.x, pos.y, pos.z + size.z);
+ r_to = Vector3(pos.x + size.x, pos.y, pos.z);
} break;
- case 2:{
- r_from=Vector3( pos.x , pos.y , pos.z+size.z );
- r_to=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
+ case 2: {
+ r_from = Vector3(pos.x, pos.y, pos.z + size.z);
+ r_to = Vector3(pos.x + size.x, pos.y, pos.z + size.z);
} break;
- case 3:{
+ case 3: {
- r_from=Vector3( pos.x , pos.y , pos.z );
- r_to=Vector3( pos.x , pos.y , pos.z+size.z );
+ r_from = Vector3(pos.x, pos.y, pos.z);
+ r_to = Vector3(pos.x, pos.y, pos.z + size.z);
} break;
- case 4:{
+ case 4: {
- r_from=Vector3( pos.x , pos.y+size.y , pos.z );
- r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
+ r_from = Vector3(pos.x, pos.y + size.y, pos.z);
+ r_to = Vector3(pos.x + size.x, pos.y + size.y, pos.z);
} break;
- case 5:{
+ case 5: {
- r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
- r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
+ r_from = Vector3(pos.x + size.x, pos.y + size.y, pos.z);
+ r_to = Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z);
} break;
- case 6:{
- r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
- r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
+ case 6: {
+ r_from = Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z);
+ r_to = Vector3(pos.x, pos.y + size.y, pos.z + size.z);
} break;
- case 7:{
+ case 7: {
- r_from=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
- r_to=Vector3( pos.x , pos.y+size.y , pos.z );
+ r_from = Vector3(pos.x, pos.y + size.y, pos.z + size.z);
+ r_to = Vector3(pos.x, pos.y + size.y, pos.z);
} break;
- case 8:{
+ case 8: {
- r_from=Vector3( pos.x , pos.y , pos.z+size.z );
- r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
+ r_from = Vector3(pos.x, pos.y, pos.z + size.z);
+ r_to = Vector3(pos.x, pos.y + size.y, pos.z + size.z);
} break;
- case 9:{
+ case 9: {
- r_from=Vector3( pos.x , pos.y , pos.z );
- r_to=Vector3( pos.x , pos.y+size.y , pos.z );
+ r_from = Vector3(pos.x, pos.y, pos.z);
+ r_to = Vector3(pos.x, pos.y + size.y, pos.z);
} break;
- case 10:{
+ case 10: {
- r_from=Vector3( pos.x+size.x , pos.y , pos.z );
- r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
+ r_from = Vector3(pos.x + size.x, pos.y, pos.z);
+ r_to = Vector3(pos.x + size.x, pos.y + size.y, pos.z);
} break;
- case 11:{
+ case 11: {
- r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
- r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
+ r_from = Vector3(pos.x + size.x, pos.y, pos.z + size.z);
+ r_to = Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z);
} break;
-
}
-
}
Rect3::operator String() const {
- return String()+pos +" - "+ size;
+ return String() + pos + " - " + size;
}
diff --git a/core/math/rect3.h b/core/math/rect3.h
index 902592b02c..26198537c2 100644
--- a/core/math/rect3.h
+++ b/core/math/rect3.h
@@ -29,19 +29,15 @@
#ifndef AABB_H
#define AABB_H
-
-
-#include "vector3.h"
-#include "plane.h"
#include "math_defs.h"
+#include "plane.h"
+#include "vector3.h"
/**
* AABB / AABB (Axis Aligned Bounding Box)
* This is implemented by a point (pos) and the box size
*/
-
-
class Rect3 {
public:
Vector3 pos;
@@ -50,40 +46,38 @@ public:
real_t get_area() const; /// get area
_FORCE_INLINE_ bool has_no_area() const {
- return (size.x<=CMP_EPSILON || size.y<=CMP_EPSILON || size.z<=CMP_EPSILON);
+ return (size.x <= CMP_EPSILON || size.y <= CMP_EPSILON || size.z <= CMP_EPSILON);
}
_FORCE_INLINE_ bool has_no_surface() const {
- return (size.x<=CMP_EPSILON && size.y<=CMP_EPSILON && size.z<=CMP_EPSILON);
+ return (size.x <= CMP_EPSILON && size.y <= CMP_EPSILON && size.z <= CMP_EPSILON);
}
- const Vector3& get_pos() const { return pos; }
- void set_pos(const Vector3& p_pos) { pos=p_pos; }
- const Vector3& get_size() const { return size; }
- void set_size(const Vector3& p_size) { size=p_size; }
+ const Vector3 &get_pos() const { return pos; }
+ void set_pos(const Vector3 &p_pos) { pos = p_pos; }
+ const Vector3 &get_size() const { return size; }
+ void set_size(const Vector3 &p_size) { size = p_size; }
+ bool operator==(const Rect3 &p_rval) const;
+ bool operator!=(const Rect3 &p_rval) const;
- bool operator==(const Rect3& p_rval) const;
- bool operator!=(const Rect3& p_rval) const;
+ _FORCE_INLINE_ bool intersects(const Rect3 &p_aabb) const; /// Both AABBs overlap
+ _FORCE_INLINE_ bool intersects_inclusive(const Rect3 &p_aabb) const; /// Both AABBs (or their faces) overlap
+ _FORCE_INLINE_ bool encloses(const Rect3 &p_aabb) const; /// p_aabb is completely inside this
- _FORCE_INLINE_ bool intersects(const Rect3& p_aabb) const; /// Both AABBs overlap
- _FORCE_INLINE_ bool intersects_inclusive(const Rect3& p_aabb) const; /// Both AABBs (or their faces) overlap
- _FORCE_INLINE_ bool encloses(const Rect3 & p_aabb) const; /// p_aabb is completely inside this
-
- Rect3 merge(const Rect3& p_with) const;
- void merge_with(const Rect3& p_aabb); ///merge with another AABB
- Rect3 intersection(const Rect3& p_aabb) const; ///get box where two intersect, empty if no intersection occurs
- bool intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
- bool intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
- _FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &from,const Vector3& p_dir, real_t t0, real_t t1) const;
+ Rect3 merge(const Rect3 &p_with) const;
+ void merge_with(const Rect3 &p_aabb); ///merge with another AABB
+ Rect3 intersection(const Rect3 &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
+ bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip = NULL, Vector3 *r_normal = NULL) const;
+ bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = NULL, Vector3 *r_normal = NULL) const;
+ _FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &from, const Vector3 &p_dir, real_t t0, real_t t1) const;
_FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_plane, int p_plane_count) const;
bool intersects_plane(const Plane &p_plane) const;
- _FORCE_INLINE_ bool has_point(const Vector3& p_point) const;
- _FORCE_INLINE_ Vector3 get_support(const Vector3& p_normal) const;
-
+ _FORCE_INLINE_ bool has_point(const Vector3 &p_point) const;
+ _FORCE_INLINE_ Vector3 get_support(const Vector3 &p_normal) const;
Vector3 get_longest_axis() const;
int get_longest_axis_index() const;
@@ -96,98 +90,97 @@ public:
Rect3 grow(real_t p_by) const;
_FORCE_INLINE_ void grow_by(real_t p_amount);
- void get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const;
+ void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const;
_FORCE_INLINE_ Vector3 get_endpoint(int p_point) const;
- Rect3 expand(const Vector3& p_vector) const;
- _FORCE_INLINE_ void project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const;
- _FORCE_INLINE_ void expand_to(const Vector3& p_vector); /** expand to contain a point if necesary */
+ Rect3 expand(const Vector3 &p_vector) const;
+ _FORCE_INLINE_ void project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const;
+ _FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necesary */
operator String() const;
_FORCE_INLINE_ Rect3() {}
- inline Rect3(const Vector3 &p_pos,const Vector3& p_size) { pos=p_pos; size=p_size; }
+ inline Rect3(const Vector3 &p_pos, const Vector3 &p_size) {
+ pos = p_pos;
+ size = p_size;
+ }
+};
+inline bool Rect3::intersects(const Rect3 &p_aabb) const {
-};
+ if (pos.x >= (p_aabb.pos.x + p_aabb.size.x))
+ return false;
+ if ((pos.x + size.x) <= p_aabb.pos.x)
+ return false;
+ if (pos.y >= (p_aabb.pos.y + p_aabb.size.y))
+ return false;
+ if ((pos.y + size.y) <= p_aabb.pos.y)
+ return false;
+ if (pos.z >= (p_aabb.pos.z + p_aabb.size.z))
+ return false;
+ if ((pos.z + size.z) <= p_aabb.pos.z)
+ return false;
-inline bool Rect3::intersects(const Rect3& p_aabb) const {
-
- if ( pos.x >= (p_aabb.pos.x + p_aabb.size.x) )
- return false;
- if ( (pos.x+size.x) <= p_aabb.pos.x )
- return false;
- if ( pos.y >= (p_aabb.pos.y + p_aabb.size.y) )
- return false;
- if ( (pos.y+size.y) <= p_aabb.pos.y )
- return false;
- if ( pos.z >= (p_aabb.pos.z + p_aabb.size.z) )
- return false;
- if ( (pos.z+size.z) <= p_aabb.pos.z )
- return false;
-
- return true;
+ return true;
}
-inline bool Rect3::intersects_inclusive(const Rect3& p_aabb) const {
-
- if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) )
- return false;
- if ( (pos.x+size.x) < p_aabb.pos.x )
- return false;
- if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) )
- return false;
- if ( (pos.y+size.y) < p_aabb.pos.y )
- return false;
- if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) )
- return false;
- if ( (pos.z+size.z) < p_aabb.pos.z )
- return false;
-
- return true;
+inline bool Rect3::intersects_inclusive(const Rect3 &p_aabb) const {
+
+ if (pos.x > (p_aabb.pos.x + p_aabb.size.x))
+ return false;
+ if ((pos.x + size.x) < p_aabb.pos.x)
+ return false;
+ if (pos.y > (p_aabb.pos.y + p_aabb.size.y))
+ return false;
+ if ((pos.y + size.y) < p_aabb.pos.y)
+ return false;
+ if (pos.z > (p_aabb.pos.z + p_aabb.size.z))
+ return false;
+ if ((pos.z + size.z) < p_aabb.pos.z)
+ return false;
+
+ return true;
}
-inline bool Rect3::encloses(const Rect3 & p_aabb) const {
+inline bool Rect3::encloses(const Rect3 &p_aabb) const {
- Vector3 src_min=pos;
- Vector3 src_max=pos+size;
- Vector3 dst_min=p_aabb.pos;
- Vector3 dst_max=p_aabb.pos+p_aabb.size;
+ Vector3 src_min = pos;
+ Vector3 src_max = pos + size;
+ Vector3 dst_min = p_aabb.pos;
+ Vector3 dst_max = p_aabb.pos + p_aabb.size;
- return (
- (src_min.x <= dst_min.x) &&
+ return (
+ (src_min.x <= dst_min.x) &&
(src_max.x > dst_max.x) &&
(src_min.y <= dst_min.y) &&
(src_max.y > dst_max.y) &&
(src_min.z <= dst_min.z) &&
- (src_max.z > dst_max.z) );
-
+ (src_max.z > dst_max.z));
}
-Vector3 Rect3::get_support(const Vector3& p_normal) const {
+Vector3 Rect3::get_support(const Vector3 &p_normal) const {
Vector3 half_extents = size * 0.5;
Vector3 ofs = pos + half_extents;
return Vector3(
- (p_normal.x>0) ? -half_extents.x : half_extents.x,
- (p_normal.y>0) ? -half_extents.y : half_extents.y,
- (p_normal.z>0) ? -half_extents.z : half_extents.z
- )+ofs;
+ (p_normal.x > 0) ? -half_extents.x : half_extents.x,
+ (p_normal.y > 0) ? -half_extents.y : half_extents.y,
+ (p_normal.z > 0) ? -half_extents.z : half_extents.z) +
+ ofs;
}
-
Vector3 Rect3::get_endpoint(int p_point) const {
- switch(p_point) {
- case 0: return Vector3( pos.x , pos.y , pos.z );
- case 1: return Vector3( pos.x , pos.y , pos.z+size.z );
- case 2: return Vector3( pos.x , pos.y+size.y , pos.z );
- case 3: return Vector3( pos.x , pos.y+size.y , pos.z+size.z );
- case 4: return Vector3( pos.x+size.x , pos.y , pos.z );
- case 5: return Vector3( pos.x+size.x , pos.y , pos.z+size.z );
- case 6: return Vector3( pos.x+size.x , pos.y+size.y , pos.z );
- case 7: return Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
+ switch (p_point) {
+ case 0: return Vector3(pos.x, pos.y, pos.z);
+ case 1: return Vector3(pos.x, pos.y, pos.z + size.z);
+ case 2: return Vector3(pos.x, pos.y + size.y, pos.z);
+ case 3: return Vector3(pos.x, pos.y + size.y, pos.z + size.z);
+ case 4: return Vector3(pos.x + size.x, pos.y, pos.z);
+ case 5: return Vector3(pos.x + size.x, pos.y, pos.z + size.z);
+ case 6: return Vector3(pos.x + size.x, pos.y + size.y, pos.z);
+ case 7: return Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z);
};
ERR_FAIL_V(Vector3());
@@ -200,14 +193,13 @@ bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) co
Vector3 half_extents = size * 0.5;
Vector3 ofs = pos + half_extents;
- for(int i=0;i<p_plane_count;i++) {
- const Plane &p=p_planes[i];
+ for (int i = 0; i < p_plane_count; i++) {
+ const Plane &p = p_planes[i];
Vector3 point(
- (p.normal.x>0) ? -half_extents.x : half_extents.x,
- (p.normal.y>0) ? -half_extents.y : half_extents.y,
- (p.normal.z>0) ? -half_extents.z : half_extents.z
- );
- point+=ofs;
+ (p.normal.x > 0) ? -half_extents.x : half_extents.x,
+ (p.normal.y > 0) ? -half_extents.y : half_extents.y,
+ (p.normal.z > 0) ? -half_extents.z : half_extents.z);
+ point += ofs;
if (p.is_point_over(point))
return false;
}
@@ -215,33 +207,31 @@ bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) co
return true;
#else
//cache all points to check against!
-// #warning should be easy to optimize, just use the same as when taking the support and use only that point
+ // #warning should be easy to optimize, just use the same as when taking the support and use only that point
Vector3 points[8] = {
- Vector3( pos.x , pos.y , pos.z ),
- Vector3( pos.x , pos.y , pos.z+size.z ),
- Vector3( pos.x , pos.y+size.y , pos.z ),
- Vector3( pos.x , pos.y+size.y , pos.z+size.z ),
- Vector3( pos.x+size.x , pos.y , pos.z ),
- Vector3( pos.x+size.x , pos.y , pos.z+size.z ),
- Vector3( pos.x+size.x , pos.y+size.y , pos.z ),
- Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ),
+ Vector3(pos.x, pos.y, pos.z),
+ Vector3(pos.x, pos.y, pos.z + size.z),
+ Vector3(pos.x, pos.y + size.y, pos.z),
+ Vector3(pos.x, pos.y + size.y, pos.z + size.z),
+ Vector3(pos.x + size.x, pos.y, pos.z),
+ Vector3(pos.x + size.x, pos.y, pos.z + size.z),
+ Vector3(pos.x + size.x, pos.y + size.y, pos.z),
+ Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z),
};
- for (int i=0;i<p_plane_count;i++) { //for each plane
+ for (int i = 0; i < p_plane_count; i++) { //for each plane
- const Plane & plane=p_planes[i];
- bool all_points_over=true;
+ const Plane &plane = p_planes[i];
+ bool all_points_over = true;
//test if it has all points over!
- for (int j=0;j<8;j++) {
-
+ for (int j = 0; j < 8; j++) {
- if (!plane.is_point_over( points[j] )) {
+ if (!plane.is_point_over(points[j])) {
- all_points_over=false;
+ all_points_over = false;
break;
}
-
}
if (all_points_over) {
@@ -253,69 +243,68 @@ bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) co
#endif
}
-bool Rect3::has_point(const Vector3& p_point) const {
+bool Rect3::has_point(const Vector3 &p_point) const {
- if (p_point.x<pos.x)
+ if (p_point.x < pos.x)
return false;
- if (p_point.y<pos.y)
+ if (p_point.y < pos.y)
return false;
- if (p_point.z<pos.z)
+ if (p_point.z < pos.z)
return false;
- if (p_point.x>pos.x+size.x)
+ if (p_point.x > pos.x + size.x)
return false;
- if (p_point.y>pos.y+size.y)
+ if (p_point.y > pos.y + size.y)
return false;
- if (p_point.z>pos.z+size.z)
+ if (p_point.z > pos.z + size.z)
return false;
return true;
}
+inline void Rect3::expand_to(const Vector3 &p_vector) {
-inline void Rect3::expand_to(const Vector3& p_vector) {
-
- Vector3 begin=pos;
- Vector3 end=pos+size;
+ Vector3 begin = pos;
+ Vector3 end = pos + size;
- if (p_vector.x<begin.x)
- begin.x=p_vector.x;
- if (p_vector.y<begin.y)
- begin.y=p_vector.y;
- if (p_vector.z<begin.z)
- begin.z=p_vector.z;
+ if (p_vector.x < begin.x)
+ begin.x = p_vector.x;
+ if (p_vector.y < begin.y)
+ begin.y = p_vector.y;
+ if (p_vector.z < begin.z)
+ begin.z = p_vector.z;
- if (p_vector.x>end.x)
- end.x=p_vector.x;
- if (p_vector.y>end.y)
- end.y=p_vector.y;
- if (p_vector.z>end.z)
- end.z=p_vector.z;
+ if (p_vector.x > end.x)
+ end.x = p_vector.x;
+ if (p_vector.y > end.y)
+ end.y = p_vector.y;
+ if (p_vector.z > end.z)
+ end.z = p_vector.z;
- pos=begin;
- size=end-begin;
+ pos = begin;
+ size = end - begin;
}
-void Rect3::project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const {
+void Rect3::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const {
- Vector3 half_extents( size.x * 0.5, size.y * 0.5, size.z * 0.5 );
- Vector3 center( pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z );
+ Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5);
+ Vector3 center(pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z);
real_t length = p_plane.normal.abs().dot(half_extents);
- real_t distance = p_plane.distance_to( center );
+ real_t distance = p_plane.distance_to(center);
r_min = distance - length;
r_max = distance + length;
}
inline real_t Rect3::get_longest_axis_size() const {
- real_t max_size=size.x;
+ real_t max_size = size.x;
- if (size.y > max_size ) {
- max_size=size.y;
+ if (size.y > max_size) {
+ max_size = size.y;
}
- if (size.z > max_size ) {
- max_size=size.z;
+ if (size.z > max_size) {
+ max_size = size.z;
}
return max_size;
@@ -323,75 +312,71 @@ inline real_t Rect3::get_longest_axis_size() const {
inline real_t Rect3::get_shortest_axis_size() const {
- real_t max_size=size.x;
+ real_t max_size = size.x;
- if (size.y < max_size ) {
- max_size=size.y;
+ if (size.y < max_size) {
+ max_size = size.y;
}
- if (size.z < max_size ) {
- max_size=size.z;
+ if (size.z < max_size) {
+ max_size = size.z;
}
return max_size;
}
-bool Rect3::smits_intersect_ray(const Vector3 &from,const Vector3& dir, real_t t0, real_t t1) const {
+bool Rect3::smits_intersect_ray(const Vector3 &from, const Vector3 &dir, real_t t0, real_t t1) const {
- real_t divx=1.0/dir.x;
- real_t divy=1.0/dir.y;
- real_t divz=1.0/dir.z;
+ real_t divx = 1.0 / dir.x;
+ real_t divy = 1.0 / dir.y;
+ real_t divz = 1.0 / dir.z;
- Vector3 upbound=pos+size;
+ Vector3 upbound = pos + size;
real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
if (dir.x >= 0) {
tmin = (pos.x - from.x) * divx;
tmax = (upbound.x - from.x) * divx;
- }
- else {
+ } else {
tmin = (upbound.x - from.x) * divx;
tmax = (pos.x - from.x) * divx;
}
if (dir.y >= 0) {
tymin = (pos.y - from.y) * divy;
tymax = (upbound.y - from.y) * divy;
- }
- else {
+ } else {
tymin = (upbound.y - from.y) * divy;
tymax = (pos.y - from.y) * divy;
}
- if ( (tmin > tymax) || (tymin > tmax) )
+ if ((tmin > tymax) || (tymin > tmax))
return false;
if (tymin > tmin)
- tmin = tymin;
+ tmin = tymin;
if (tymax < tmax)
tmax = tymax;
if (dir.z >= 0) {
tzmin = (pos.z - from.z) * divz;
tzmax = (upbound.z - from.z) * divz;
- }
- else {
+ } else {
tzmin = (upbound.z - from.z) * divz;
tzmax = (pos.z - from.z) * divz;
}
- if ( (tmin > tzmax) || (tzmin > tmax) )
+ if ((tmin > tzmax) || (tzmin > tmax))
return false;
if (tzmin > tmin)
tmin = tzmin;
if (tzmax < tmax)
tmax = tzmax;
- return ( (tmin < t1) && (tmax > t0) );
+ return ((tmin < t1) && (tmax > t0));
}
void Rect3::grow_by(real_t p_amount) {
- pos.x-=p_amount;
- pos.y-=p_amount;
- pos.z-=p_amount;
- size.x+=2.0*p_amount;
- size.y+=2.0*p_amount;
- size.z+=2.0*p_amount;
+ pos.x -= p_amount;
+ pos.y -= p_amount;
+ pos.z -= p_amount;
+ size.x += 2.0 * p_amount;
+ size.y += 2.0 * p_amount;
+ size.z += 2.0 * p_amount;
}
-
#endif // AABB_H
diff --git a/core/math/transform.cpp b/core/math/transform.cpp
index 6d9324c176..d35938e559 100644
--- a/core/math/transform.cpp
+++ b/core/math/transform.cpp
@@ -31,7 +31,6 @@
#include "os/copymem.h"
#include "print_string.h"
-
void Transform::affine_invert() {
basis.invert();
@@ -40,13 +39,11 @@ void Transform::affine_invert() {
Transform Transform::affine_inverse() const {
- Transform ret=*this;
+ Transform ret = *this;
ret.affine_invert();
return ret;
-
}
-
void Transform::invert() {
basis.transpose();
@@ -56,35 +53,34 @@ void Transform::invert() {
Transform Transform::inverse() const {
// FIXME: this function assumes the basis is a rotation matrix, with no scaling.
// Transform::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
- Transform ret=*this;
+ Transform ret = *this;
ret.invert();
return ret;
}
-
-void Transform::rotate(const Vector3& p_axis,real_t p_phi) {
+void Transform::rotate(const Vector3 &p_axis, real_t p_phi) {
*this = rotated(p_axis, p_phi);
}
-Transform Transform::rotated(const Vector3& p_axis,real_t p_phi) const{
+Transform Transform::rotated(const Vector3 &p_axis, real_t p_phi) const {
- return Transform(Basis( p_axis, p_phi ), Vector3()) * (*this);
+ return Transform(Basis(p_axis, p_phi), Vector3()) * (*this);
}
-void Transform::rotate_basis(const Vector3& p_axis,real_t p_phi) {
+void Transform::rotate_basis(const Vector3 &p_axis, real_t p_phi) {
- basis.rotate(p_axis,p_phi);
+ basis.rotate(p_axis, p_phi);
}
-Transform Transform::looking_at( const Vector3& p_target, const Vector3& p_up ) const {
+Transform Transform::looking_at(const Vector3 &p_target, const Vector3 &p_up) const {
Transform t = *this;
- t.set_look_at(origin,p_target,p_up);
+ t.set_look_at(origin, p_target, p_up);
return t;
}
-void Transform::set_look_at( const Vector3& p_eye, const Vector3& p_target, const Vector3& p_up ) {
+void Transform::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up) {
// Reference: MESA source code
Vector3 v_x, v_y, v_z;
@@ -98,23 +94,21 @@ void Transform::set_look_at( const Vector3& p_eye, const Vector3& p_target, cons
v_y = p_up;
-
- v_x=v_y.cross(v_z);
+ v_x = v_y.cross(v_z);
/* Recompute Y = Z cross X */
- v_y=v_z.cross(v_x);
+ v_y = v_z.cross(v_x);
v_x.normalize();
v_y.normalize();
- basis.set_axis(0,v_x);
- basis.set_axis(1,v_y);
- basis.set_axis(2,v_z);
- origin=p_eye;
-
+ basis.set_axis(0, v_x);
+ basis.set_axis(1, v_y);
+ basis.set_axis(2, v_z);
+ origin = p_eye;
}
-Transform Transform::interpolate_with(const Transform& p_transform, real_t p_c) const {
+Transform Transform::interpolate_with(const Transform &p_transform, real_t p_c) const {
/* not sure if very "efficient" but good enough? */
@@ -127,45 +121,44 @@ Transform Transform::interpolate_with(const Transform& p_transform, real_t p_c)
Vector3 dst_loc = p_transform.origin;
Transform dst;
- dst.basis=src_rot.slerp(dst_rot,p_c);
- dst.basis.scale(src_scale.linear_interpolate(dst_scale,p_c));
- dst.origin=src_loc.linear_interpolate(dst_loc,p_c);
+ dst.basis = src_rot.slerp(dst_rot, p_c);
+ dst.basis.scale(src_scale.linear_interpolate(dst_scale, p_c));
+ dst.origin = src_loc.linear_interpolate(dst_loc, p_c);
return dst;
}
-void Transform::scale(const Vector3& p_scale) {
+void Transform::scale(const Vector3 &p_scale) {
basis.scale(p_scale);
- origin*=p_scale;
+ origin *= p_scale;
}
-Transform Transform::scaled(const Vector3& p_scale) const {
+Transform Transform::scaled(const Vector3 &p_scale) const {
Transform t = *this;
t.scale(p_scale);
return t;
}
-void Transform::scale_basis(const Vector3& p_scale) {
+void Transform::scale_basis(const Vector3 &p_scale) {
basis.scale(p_scale);
}
-void Transform::translate( real_t p_tx, real_t p_ty, real_t p_tz) {
- translate( Vector3(p_tx,p_ty,p_tz) );
-
+void Transform::translate(real_t p_tx, real_t p_ty, real_t p_tz) {
+ translate(Vector3(p_tx, p_ty, p_tz));
}
-void Transform::translate( const Vector3& p_translation ) {
+void Transform::translate(const Vector3 &p_translation) {
- for( int i = 0; i < 3; i++ ) {
+ for (int i = 0; i < 3; i++) {
origin[i] += basis[i].dot(p_translation);
}
}
-Transform Transform::translated( const Vector3& p_translation ) const {
+Transform Transform::translated(const Vector3 &p_translation) const {
- Transform t=*this;
+ Transform t = *this;
t.translate(p_translation);
return t;
}
@@ -182,25 +175,25 @@ Transform Transform::orthonormalized() const {
return _copy;
}
-bool Transform::operator==(const Transform& p_transform) const {
+bool Transform::operator==(const Transform &p_transform) const {
- return (basis==p_transform.basis && origin==p_transform.origin);
+ return (basis == p_transform.basis && origin == p_transform.origin);
}
-bool Transform::operator!=(const Transform& p_transform) const {
+bool Transform::operator!=(const Transform &p_transform) const {
- return (basis!=p_transform.basis || origin!=p_transform.origin);
+ return (basis != p_transform.basis || origin != p_transform.origin);
}
-void Transform::operator*=(const Transform& p_transform) {
+void Transform::operator*=(const Transform &p_transform) {
- origin=xform(p_transform.origin);
- basis*=p_transform.basis;
+ origin = xform(p_transform.origin);
+ basis *= p_transform.basis;
}
-Transform Transform::operator*(const Transform& p_transform) const {
+Transform Transform::operator*(const Transform &p_transform) const {
- Transform t=*this;
- t*=p_transform;
+ Transform t = *this;
+ t *= p_transform;
return t;
}
@@ -209,11 +202,8 @@ Transform::operator String() const {
return basis.operator String() + " - " + origin.operator String();
}
+Transform::Transform(const Basis &p_basis, const Vector3 &p_origin) {
-Transform::Transform(const Basis& p_basis, const Vector3& p_origin) {
-
- basis=p_basis;
- origin=p_origin;
+ basis = p_basis;
+ origin = p_origin;
}
-
-
diff --git a/core/math/transform.h b/core/math/transform.h
index 45d7b2a12c..e307aba129 100644
--- a/core/math/transform.h
+++ b/core/math/transform.h
@@ -37,7 +37,6 @@
*/
class Transform {
public:
-
Basis basis;
Vector3 origin;
@@ -47,199 +46,187 @@ public:
void affine_invert();
Transform affine_inverse() const;
- Transform rotated(const Vector3& p_axis,real_t p_phi) const;
+ Transform rotated(const Vector3 &p_axis, real_t p_phi) const;
- void rotate(const Vector3& p_axis,real_t p_phi);
- void rotate_basis(const Vector3& p_axis,real_t p_phi);
+ void rotate(const Vector3 &p_axis, real_t p_phi);
+ void rotate_basis(const Vector3 &p_axis, real_t p_phi);
- void set_look_at( const Vector3& p_eye, const Vector3& p_target, const Vector3& p_up );
- Transform looking_at( const Vector3& p_target, const Vector3& p_up ) const;
+ void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up);
+ Transform looking_at(const Vector3 &p_target, const Vector3 &p_up) const;
- void scale(const Vector3& p_scale);
- Transform scaled(const Vector3& p_scale) const;
- void scale_basis(const Vector3& p_scale);
- void translate( real_t p_tx, real_t p_ty, real_t p_tz );
- void translate( const Vector3& p_translation );
- Transform translated( const Vector3& p_translation ) const;
+ void scale(const Vector3 &p_scale);
+ Transform scaled(const Vector3 &p_scale) const;
+ void scale_basis(const Vector3 &p_scale);
+ void translate(real_t p_tx, real_t p_ty, real_t p_tz);
+ void translate(const Vector3 &p_translation);
+ Transform translated(const Vector3 &p_translation) const;
- const Basis& get_basis() const { return basis; }
- void set_basis(const Basis& p_basis) { basis=p_basis; }
+ const Basis &get_basis() const { return basis; }
+ void set_basis(const Basis &p_basis) { basis = p_basis; }
- const Vector3& get_origin() const { return origin; }
- void set_origin(const Vector3& p_origin) { origin=p_origin; }
+ const Vector3 &get_origin() const { return origin; }
+ void set_origin(const Vector3 &p_origin) { origin = p_origin; }
void orthonormalize();
Transform orthonormalized() const;
- bool operator==(const Transform& p_transform) const;
- bool operator!=(const Transform& p_transform) const;
+ bool operator==(const Transform &p_transform) const;
+ bool operator!=(const Transform &p_transform) const;
- _FORCE_INLINE_ Vector3 xform(const Vector3& p_vector) const;
- _FORCE_INLINE_ Vector3 xform_inv(const Vector3& p_vector) const;
+ _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
+ _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
- _FORCE_INLINE_ Plane xform(const Plane& p_plane) const;
- _FORCE_INLINE_ Plane xform_inv(const Plane& p_plane) const;
+ _FORCE_INLINE_ Plane xform(const Plane &p_plane) const;
+ _FORCE_INLINE_ Plane xform_inv(const Plane &p_plane) const;
- _FORCE_INLINE_ Rect3 xform(const Rect3& p_aabb) const;
- _FORCE_INLINE_ Rect3 xform_inv(const Rect3& p_aabb) const;
+ _FORCE_INLINE_ Rect3 xform(const Rect3 &p_aabb) const;
+ _FORCE_INLINE_ Rect3 xform_inv(const Rect3 &p_aabb) const;
- void operator*=(const Transform& p_transform);
- Transform operator*(const Transform& p_transform) const;
+ void operator*=(const Transform &p_transform);
+ Transform operator*(const Transform &p_transform) const;
- Transform interpolate_with(const Transform& p_transform, real_t p_c) const;
+ Transform interpolate_with(const Transform &p_transform, real_t p_c) const;
- _FORCE_INLINE_ Transform inverse_xform(const Transform& t) const {
+ _FORCE_INLINE_ Transform inverse_xform(const Transform &t) const {
Vector3 v = t.origin - origin;
return Transform(basis.transpose_xform(t.basis),
- basis.xform(v));
+ basis.xform(v));
}
- void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz,real_t tx, real_t ty, real_t tz) {
-
- basis.elements[0][0]=xx;
- basis.elements[0][1]=xy;
- basis.elements[0][2]=xz;
- basis.elements[1][0]=yx;
- basis.elements[1][1]=yy;
- basis.elements[1][2]=yz;
- basis.elements[2][0]=zx;
- basis.elements[2][1]=zy;
- basis.elements[2][2]=zz;
- origin.x=tx;
- origin.y=ty;
- origin.z=tz;
+ void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t tx, real_t ty, real_t tz) {
+
+ basis.elements[0][0] = xx;
+ basis.elements[0][1] = xy;
+ basis.elements[0][2] = xz;
+ basis.elements[1][0] = yx;
+ basis.elements[1][1] = yy;
+ basis.elements[1][2] = yz;
+ basis.elements[2][0] = zx;
+ basis.elements[2][1] = zy;
+ basis.elements[2][2] = zz;
+ origin.x = tx;
+ origin.y = ty;
+ origin.z = tz;
}
operator String() const;
- Transform(const Basis& p_basis, const Vector3& p_origin=Vector3());
+ Transform(const Basis &p_basis, const Vector3 &p_origin = Vector3());
Transform() {}
-
};
-
-_FORCE_INLINE_ Vector3 Transform::xform(const Vector3& p_vector) const {
+_FORCE_INLINE_ Vector3 Transform::xform(const Vector3 &p_vector) const {
return Vector3(
- basis[0].dot(p_vector)+origin.x,
- basis[1].dot(p_vector)+origin.y,
- basis[2].dot(p_vector)+origin.z
- );
+ basis[0].dot(p_vector) + origin.x,
+ basis[1].dot(p_vector) + origin.y,
+ basis[2].dot(p_vector) + origin.z);
}
-_FORCE_INLINE_ Vector3 Transform::xform_inv(const Vector3& p_vector) const {
+_FORCE_INLINE_ Vector3 Transform::xform_inv(const Vector3 &p_vector) const {
Vector3 v = p_vector - origin;
return Vector3(
- (basis.elements[0][0]*v.x ) + ( basis.elements[1][0]*v.y ) + ( basis.elements[2][0]*v.z ),
- (basis.elements[0][1]*v.x ) + ( basis.elements[1][1]*v.y ) + ( basis.elements[2][1]*v.z ),
- (basis.elements[0][2]*v.x ) + ( basis.elements[1][2]*v.y ) + ( basis.elements[2][2]*v.z )
- );
+ (basis.elements[0][0] * v.x) + (basis.elements[1][0] * v.y) + (basis.elements[2][0] * v.z),
+ (basis.elements[0][1] * v.x) + (basis.elements[1][1] * v.y) + (basis.elements[2][1] * v.z),
+ (basis.elements[0][2] * v.x) + (basis.elements[1][2] * v.y) + (basis.elements[2][2] * v.z));
}
-_FORCE_INLINE_ Plane Transform::xform(const Plane& p_plane) const {
+_FORCE_INLINE_ Plane Transform::xform(const Plane &p_plane) const {
+ Vector3 point = p_plane.normal * p_plane.d;
+ Vector3 point_dir = point + p_plane.normal;
+ point = xform(point);
+ point_dir = xform(point_dir);
- Vector3 point=p_plane.normal*p_plane.d;
- Vector3 point_dir=point+p_plane.normal;
- point=xform(point);
- point_dir=xform(point_dir);
-
- Vector3 normal=point_dir-point;
+ Vector3 normal = point_dir - point;
normal.normalize();
- real_t d=normal.dot(point);
-
- return Plane(normal,d);
+ real_t d = normal.dot(point);
+ return Plane(normal, d);
}
-_FORCE_INLINE_ Plane Transform::xform_inv(const Plane& p_plane) const {
+_FORCE_INLINE_ Plane Transform::xform_inv(const Plane &p_plane) const {
- Vector3 point=p_plane.normal*p_plane.d;
- Vector3 point_dir=point+p_plane.normal;
+ Vector3 point = p_plane.normal * p_plane.d;
+ Vector3 point_dir = point + p_plane.normal;
xform_inv(point);
xform_inv(point_dir);
- Vector3 normal=point_dir-point;
+ Vector3 normal = point_dir - point;
normal.normalize();
- real_t d=normal.dot(point);
-
- return Plane(normal,d);
+ real_t d = normal.dot(point);
+ return Plane(normal, d);
}
-_FORCE_INLINE_ Rect3 Transform::xform(const Rect3& p_aabb) const {
- /* define vertices */
+_FORCE_INLINE_ Rect3 Transform::xform(const Rect3 &p_aabb) const {
+/* define vertices */
#if 1
- Vector3 x=basis.get_axis(0)*p_aabb.size.x;
- Vector3 y=basis.get_axis(1)*p_aabb.size.y;
- Vector3 z=basis.get_axis(2)*p_aabb.size.z;
- Vector3 pos = xform( p_aabb.pos );
-//could be even further optimized
+ Vector3 x = basis.get_axis(0) * p_aabb.size.x;
+ Vector3 y = basis.get_axis(1) * p_aabb.size.y;
+ Vector3 z = basis.get_axis(2) * p_aabb.size.z;
+ Vector3 pos = xform(p_aabb.pos);
+ //could be even further optimized
Rect3 new_aabb;
- new_aabb.pos=pos;
- new_aabb.expand_to( pos+x );
- new_aabb.expand_to( pos+y );
- new_aabb.expand_to( pos+z );
- new_aabb.expand_to( pos+x+y );
- new_aabb.expand_to( pos+x+z );
- new_aabb.expand_to( pos+y+z );
- new_aabb.expand_to( pos+x+y+z );
+ new_aabb.pos = pos;
+ new_aabb.expand_to(pos + x);
+ new_aabb.expand_to(pos + y);
+ new_aabb.expand_to(pos + z);
+ new_aabb.expand_to(pos + x + y);
+ new_aabb.expand_to(pos + x + z);
+ new_aabb.expand_to(pos + y + z);
+ new_aabb.expand_to(pos + x + y + z);
return new_aabb;
#else
-
- Vector3 vertices[8]={
- Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z+p_aabb.size.z),
- Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z),
- Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z+p_aabb.size.z),
- Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z),
- Vector3(p_aabb.pos.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z+p_aabb.size.z),
- Vector3(p_aabb.pos.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z),
- Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z+p_aabb.size.z),
- Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z)
+ Vector3 vertices[8] = {
+ Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z),
+ Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z),
+ Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z),
+ Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z),
+ Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z),
+ Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z),
+ Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z),
+ Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z)
};
-
AABB ret;
- ret.pos=xform(vertices[0]);
+ ret.pos = xform(vertices[0]);
- for (int i=1;i<8;i++) {
+ for (int i = 1; i < 8; i++) {
- ret.expand_to( xform(vertices[i]) );
+ ret.expand_to(xform(vertices[i]));
}
return ret;
#endif
-
}
-_FORCE_INLINE_ Rect3 Transform::xform_inv(const Rect3& p_aabb) const {
+_FORCE_INLINE_ Rect3 Transform::xform_inv(const Rect3 &p_aabb) const {
/* define vertices */
- Vector3 vertices[8]={
- Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z+p_aabb.size.z),
- Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z),
- Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z+p_aabb.size.z),
- Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z),
- Vector3(p_aabb.pos.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z+p_aabb.size.z),
- Vector3(p_aabb.pos.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z),
- Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z+p_aabb.size.z),
- Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z)
+ Vector3 vertices[8] = {
+ Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z),
+ Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z),
+ Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z),
+ Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z),
+ Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z),
+ Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z),
+ Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z),
+ Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z)
};
-
Rect3 ret;
- ret.pos=xform_inv(vertices[0]);
+ ret.pos = xform_inv(vertices[0]);
- for (int i=1;i<8;i++) {
+ for (int i = 1; i < 8; i++) {
- ret.expand_to( xform_inv(vertices[i]) );
+ ret.expand_to(xform_inv(vertices[i]));
}
return ret;
-
}
#ifdef OPTIMIZED_TRANSFORM_IMPL_OVERRIDE
@@ -250,16 +237,16 @@ struct OptimizedTransform {
Transform transform;
- _FORCE_INLINE_ void invert() {transform.invert(); }
- _FORCE_INLINE_ void affine_invert() {transform.affine_invert(); }
- _FORCE_INLINE_ Vector3 xform(const Vector3& p_vec) const { return transform.xform(p_vec); };
- _FORCE_INLINE_ Vector3 xform_inv(const Vector3& p_vec) const { return transform.xform_inv(p_vec); };
- _FORCE_INLINE_ OptimizedTransform operator*(const OptimizedTransform& p_ot ) const { return OptimizedTransform( transform * p_ot.transform ) ; }
+ _FORCE_INLINE_ void invert() { transform.invert(); }
+ _FORCE_INLINE_ void affine_invert() { transform.affine_invert(); }
+ _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vec) const { return transform.xform(p_vec); };
+ _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vec) const { return transform.xform_inv(p_vec); };
+ _FORCE_INLINE_ OptimizedTransform operator*(const OptimizedTransform &p_ot) const { return OptimizedTransform(transform * p_ot.transform); }
_FORCE_INLINE_ Transform get_transform() const { return transform; }
- _FORCE_INLINE_ void set_transform(const Transform& p_transform) { transform=p_transform; }
+ _FORCE_INLINE_ void set_transform(const Transform &p_transform) { transform = p_transform; }
- OptimizedTransform(const Transform& p_transform) {
- transform=p_transform;
+ OptimizedTransform(const Transform &p_transform) {
+ transform = p_transform;
}
};
diff --git a/core/math/triangle_mesh.cpp b/core/math/triangle_mesh.cpp
index 247cb90a48..93c6b2786e 100644
--- a/core/math/triangle_mesh.cpp
+++ b/core/math/triangle_mesh.cpp
@@ -29,81 +29,73 @@
#include "triangle_mesh.h"
#include "sort.h"
+int TriangleMesh::_create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc) {
-
-int TriangleMesh::_create_bvh(BVH*p_bvh,BVH** p_bb,int p_from,int p_size,int p_depth,int&max_depth,int&max_alloc) {
-
-
- if (p_depth>max_depth) {
- max_depth=p_depth;
+ if (p_depth > max_depth) {
+ max_depth = p_depth;
}
- if (p_size==1) {
+ if (p_size == 1) {
-
- return p_bb[p_from]-p_bvh;
- } else if (p_size==0) {
+ return p_bb[p_from] - p_bvh;
+ } else if (p_size == 0) {
return -1;
}
-
Rect3 aabb;
- aabb=p_bb[p_from]->aabb;
- for(int i=1;i<p_size;i++) {
+ aabb = p_bb[p_from]->aabb;
+ for (int i = 1; i < p_size; i++) {
- aabb.merge_with(p_bb[p_from+i]->aabb);
+ aabb.merge_with(p_bb[p_from + i]->aabb);
}
- int li=aabb.get_longest_axis_index();
+ int li = aabb.get_longest_axis_index();
- switch(li) {
+ switch (li) {
case Vector3::AXIS_X: {
- SortArray<BVH*,BVHCmpX> sort_x;
- sort_x.nth_element(0,p_size,p_size/2,&p_bb[p_from]);
+ SortArray<BVH *, BVHCmpX> sort_x;
+ sort_x.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
//sort_x.sort(&p_bb[p_from],p_size);
} break;
case Vector3::AXIS_Y: {
- SortArray<BVH*,BVHCmpY> sort_y;
- sort_y.nth_element(0,p_size,p_size/2,&p_bb[p_from]);
+ SortArray<BVH *, BVHCmpY> sort_y;
+ sort_y.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
//sort_y.sort(&p_bb[p_from],p_size);
} break;
case Vector3::AXIS_Z: {
- SortArray<BVH*,BVHCmpZ> sort_z;
- sort_z.nth_element(0,p_size,p_size/2,&p_bb[p_from]);
+ SortArray<BVH *, BVHCmpZ> sort_z;
+ sort_z.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
//sort_z.sort(&p_bb[p_from],p_size);
} break;
}
+ int left = _create_bvh(p_bvh, p_bb, p_from, p_size / 2, p_depth + 1, max_depth, max_alloc);
+ int right = _create_bvh(p_bvh, p_bb, p_from + p_size / 2, p_size - p_size / 2, p_depth + 1, max_depth, max_alloc);
- int left = _create_bvh(p_bvh,p_bb,p_from,p_size/2,p_depth+1,max_depth,max_alloc);
- int right = _create_bvh(p_bvh,p_bb,p_from+p_size/2,p_size-p_size/2,p_depth+1,max_depth,max_alloc);
-
- int index=max_alloc++;
+ int index = max_alloc++;
BVH *_new = &p_bvh[index];
- _new->aabb=aabb;
- _new->center=aabb.pos+aabb.size*0.5;
- _new->face_index=-1;
- _new->left=left;
- _new->right=right;
+ _new->aabb = aabb;
+ _new->center = aabb.pos + aabb.size * 0.5;
+ _new->face_index = -1;
+ _new->left = left;
+ _new->right = right;
return index;
-
}
+void TriangleMesh::create(const PoolVector<Vector3> &p_faces) {
-void TriangleMesh::create(const PoolVector<Vector3>& p_faces) {
+ valid = false;
- valid=false;
-
- int fc=p_faces.size();
- ERR_FAIL_COND(!fc || ((fc%3) != 0));
- fc/=3;
+ int fc = p_faces.size();
+ ERR_FAIL_COND(!fc || ((fc % 3) != 0));
+ fc /= 3;
triangles.resize(fc);
- bvh.resize(fc*3); //will never be larger than this (todo make better)
+ bvh.resize(fc * 3); //will never be larger than this (todo make better)
PoolVector<BVH>::Write bw = bvh.write();
{
@@ -114,148 +106,143 @@ void TriangleMesh::create(const PoolVector<Vector3>& p_faces) {
PoolVector<Vector3>::Read r = p_faces.read();
PoolVector<Triangle>::Write w = triangles.write();
- Map<Vector3,int> db;
+ Map<Vector3, int> db;
- for(int i=0;i<fc;i++) {
+ for (int i = 0; i < fc; i++) {
- Triangle&f=w[i];
- const Vector3 *v=&r[i*3];
+ Triangle &f = w[i];
+ const Vector3 *v = &r[i * 3];
- for(int j=0;j<3;j++) {
+ for (int j = 0; j < 3; j++) {
- int vidx=-1;
- Vector3 vs=v[j].snapped(0.0001);
- Map<Vector3,int>::Element *E=db.find(vs);
+ int vidx = -1;
+ Vector3 vs = v[j].snapped(0.0001);
+ Map<Vector3, int>::Element *E = db.find(vs);
if (E) {
- vidx=E->get();
+ vidx = E->get();
} else {
- vidx=db.size();
- db[vs]=vidx;
+ vidx = db.size();
+ db[vs] = vidx;
}
- f.indices[j]=vidx;
- if (j==0)
- bw[i].aabb.pos=vs;
+ f.indices[j] = vidx;
+ if (j == 0)
+ bw[i].aabb.pos = vs;
else
bw[i].aabb.expand_to(vs);
}
- f.normal=Face3(r[i*3+0],r[i*3+1],r[i*3+2]).get_plane().get_normal();
+ f.normal = Face3(r[i * 3 + 0], r[i * 3 + 1], r[i * 3 + 2]).get_plane().get_normal();
- bw[i].left=-1;
- bw[i].right=-1;
- bw[i].face_index=i;
- bw[i].center=bw[i].aabb.pos+bw[i].aabb.size*0.5;
+ bw[i].left = -1;
+ bw[i].right = -1;
+ bw[i].face_index = i;
+ bw[i].center = bw[i].aabb.pos + bw[i].aabb.size * 0.5;
}
vertices.resize(db.size());
PoolVector<Vector3>::Write vw = vertices.write();
- for (Map<Vector3,int>::Element *E=db.front();E;E=E->next()) {
- vw[E->get()]=E->key();
+ for (Map<Vector3, int>::Element *E = db.front(); E; E = E->next()) {
+ vw[E->get()] = E->key();
}
-
}
- PoolVector<BVH*> bwptrs;
+ PoolVector<BVH *> bwptrs;
bwptrs.resize(fc);
- PoolVector<BVH*>::Write bwp = bwptrs.write();
- for(int i=0;i<fc;i++) {
+ PoolVector<BVH *>::Write bwp = bwptrs.write();
+ for (int i = 0; i < fc; i++) {
- bwp[i]=&bw[i];
+ bwp[i] = &bw[i];
}
- max_depth=0;
- int max_alloc=fc;
- int max=_create_bvh(bw.ptr(),bwp.ptr(),0,fc,1,max_depth,max_alloc);
+ max_depth = 0;
+ int max_alloc = fc;
+ int max = _create_bvh(bw.ptr(), bwp.ptr(), 0, fc, 1, max_depth, max_alloc);
- bw=PoolVector<BVH>::Write(); //clearup
+ bw = PoolVector<BVH>::Write(); //clearup
bvh.resize(max_alloc); //resize back
- valid=true;
-
+ valid = true;
}
+Vector3 TriangleMesh::get_area_normal(const Rect3 &p_aabb) const {
-Vector3 TriangleMesh::get_area_normal(const Rect3& p_aabb) const {
-
- uint32_t* stack = (uint32_t*)alloca(sizeof(int)*max_depth);
+ uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
enum {
- TEST_AABB_BIT=0,
- VISIT_LEFT_BIT=1,
- VISIT_RIGHT_BIT=2,
- VISIT_DONE_BIT=3,
- VISITED_BIT_SHIFT=29,
- NODE_IDX_MASK=(1<<VISITED_BIT_SHIFT)-1,
- VISITED_BIT_MASK=~NODE_IDX_MASK,
-
+ TEST_AABB_BIT = 0,
+ VISIT_LEFT_BIT = 1,
+ VISIT_RIGHT_BIT = 2,
+ VISIT_DONE_BIT = 3,
+ VISITED_BIT_SHIFT = 29,
+ NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
+ VISITED_BIT_MASK = ~NODE_IDX_MASK,
};
- int n_count=0;
+ int n_count = 0;
Vector3 n;
- int level=0;
+ int level = 0;
PoolVector<Triangle>::Read trianglesr = triangles.read();
- PoolVector<Vector3>::Read verticesr=vertices.read();
- PoolVector<BVH>::Read bvhr=bvh.read();
+ PoolVector<Vector3>::Read verticesr = vertices.read();
+ PoolVector<BVH>::Read bvhr = bvh.read();
- const Triangle *triangleptr=trianglesr.ptr();
- int pos=bvh.size()-1;
+ const Triangle *triangleptr = trianglesr.ptr();
+ int pos = bvh.size() - 1;
const BVH *bvhptr = bvhr.ptr();
- stack[0]=pos;
- while(true) {
+ stack[0] = pos;
+ while (true) {
- uint32_t node = stack[level]&NODE_IDX_MASK;
- const BVH &b = bvhptr[ node ];
- bool done=false;
+ uint32_t node = stack[level] & NODE_IDX_MASK;
+ const BVH &b = bvhptr[node];
+ bool done = false;
- switch(stack[level]>>VISITED_BIT_SHIFT) {
+ switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
-
bool valid = b.aabb.intersects(p_aabb);
if (!valid) {
- stack[level]=(VISIT_DONE_BIT<<VISITED_BIT_SHIFT)|node;
+ stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
- if (b.face_index>=0) {
+ if (b.face_index >= 0) {
- const Triangle &s=triangleptr[ b.face_index ];
- n+=s.normal;
+ const Triangle &s = triangleptr[b.face_index];
+ n += s.normal;
n_count++;
- stack[level]=(VISIT_DONE_BIT<<VISITED_BIT_SHIFT)|node;
+ stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
- stack[level]=(VISIT_LEFT_BIT<<VISITED_BIT_SHIFT)|node;
+ stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
}
}
continue;
}
case VISIT_LEFT_BIT: {
- stack[level]=(VISIT_RIGHT_BIT<<VISITED_BIT_SHIFT)|node;
- stack[level+1]=b.left|TEST_AABB_BIT;
+ stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
+ stack[level + 1] = b.left | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_RIGHT_BIT: {
- stack[level]=(VISIT_DONE_BIT<<VISITED_BIT_SHIFT)|node;
- stack[level+1]=b.right|TEST_AABB_BIT;
+ stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
+ stack[level + 1] = b.right | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_DONE_BIT: {
- if (level==0) {
- done=true;
+ if (level == 0) {
+ done = true;
break;
} else
level--;
@@ -263,121 +250,111 @@ Vector3 TriangleMesh::get_area_normal(const Rect3& p_aabb) const {
}
}
-
if (done)
break;
}
-
- if (n_count>0)
- n/=n_count;
+ if (n_count > 0)
+ n /= n_count;
return n;
-
}
+bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const {
-bool TriangleMesh::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_point, Vector3 &r_normal) const {
-
-
- uint32_t* stack = (uint32_t*)alloca(sizeof(int)*max_depth);
+ uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
enum {
- TEST_AABB_BIT=0,
- VISIT_LEFT_BIT=1,
- VISIT_RIGHT_BIT=2,
- VISIT_DONE_BIT=3,
- VISITED_BIT_SHIFT=29,
- NODE_IDX_MASK=(1<<VISITED_BIT_SHIFT)-1,
- VISITED_BIT_MASK=~NODE_IDX_MASK,
-
+ TEST_AABB_BIT = 0,
+ VISIT_LEFT_BIT = 1,
+ VISIT_RIGHT_BIT = 2,
+ VISIT_DONE_BIT = 3,
+ VISITED_BIT_SHIFT = 29,
+ NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
+ VISITED_BIT_MASK = ~NODE_IDX_MASK,
};
- Vector3 n = (p_end-p_begin).normalized();
- real_t d=1e10;
- bool inters=false;
+ Vector3 n = (p_end - p_begin).normalized();
+ real_t d = 1e10;
+ bool inters = false;
- int level=0;
+ int level = 0;
PoolVector<Triangle>::Read trianglesr = triangles.read();
- PoolVector<Vector3>::Read verticesr=vertices.read();
- PoolVector<BVH>::Read bvhr=bvh.read();
+ PoolVector<Vector3>::Read verticesr = vertices.read();
+ PoolVector<BVH>::Read bvhr = bvh.read();
- const Triangle *triangleptr=trianglesr.ptr();
- const Vector3 *vertexptr=verticesr.ptr();
- int pos=bvh.size()-1;
+ const Triangle *triangleptr = trianglesr.ptr();
+ const Vector3 *vertexptr = verticesr.ptr();
+ int pos = bvh.size() - 1;
const BVH *bvhptr = bvhr.ptr();
- stack[0]=pos;
- while(true) {
+ stack[0] = pos;
+ while (true) {
- uint32_t node = stack[level]&NODE_IDX_MASK;
- const BVH &b = bvhptr[ node ];
- bool done=false;
+ uint32_t node = stack[level] & NODE_IDX_MASK;
+ const BVH &b = bvhptr[node];
+ bool done = false;
- switch(stack[level]>>VISITED_BIT_SHIFT) {
+ switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
-
- bool valid = b.aabb.intersects_segment(p_begin,p_end);
+ bool valid = b.aabb.intersects_segment(p_begin, p_end);
//bool valid = b.aabb.intersects(ray_aabb);
if (!valid) {
- stack[level]=(VISIT_DONE_BIT<<VISITED_BIT_SHIFT)|node;
+ stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
- if (b.face_index>=0) {
-
- const Triangle &s=triangleptr[ b.face_index ];
- Face3 f3(vertexptr[ s.indices[0] ],vertexptr[ s.indices[1] ],vertexptr[ s.indices[2] ]);
+ if (b.face_index >= 0) {
+ const Triangle &s = triangleptr[b.face_index];
+ Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
Vector3 res;
- if (f3.intersects_segment(p_begin,p_end,&res)) {
-
+ if (f3.intersects_segment(p_begin, p_end, &res)) {
real_t nd = n.dot(res);
- if (nd<d) {
+ if (nd < d) {
- d=nd;
- r_point=res;
- r_normal=f3.get_plane().get_normal();
- inters=true;
+ d = nd;
+ r_point = res;
+ r_normal = f3.get_plane().get_normal();
+ inters = true;
}
-
}
- stack[level]=(VISIT_DONE_BIT<<VISITED_BIT_SHIFT)|node;
+ stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
- stack[level]=(VISIT_LEFT_BIT<<VISITED_BIT_SHIFT)|node;
+ stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
}
}
continue;
}
case VISIT_LEFT_BIT: {
- stack[level]=(VISIT_RIGHT_BIT<<VISITED_BIT_SHIFT)|node;
- stack[level+1]=b.left|TEST_AABB_BIT;
+ stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
+ stack[level + 1] = b.left | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_RIGHT_BIT: {
- stack[level]=(VISIT_DONE_BIT<<VISITED_BIT_SHIFT)|node;
- stack[level+1]=b.right|TEST_AABB_BIT;
+ stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
+ stack[level + 1] = b.right | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_DONE_BIT: {
- if (level==0) {
- done=true;
+ if (level == 0) {
+ done = true;
break;
} else
level--;
@@ -385,121 +362,112 @@ bool TriangleMesh::intersect_segment(const Vector3& p_begin,const Vector3& p_end
}
}
-
if (done)
break;
}
-
if (inters) {
- if (n.dot(r_normal)>0)
- r_normal=-r_normal;
+ if (n.dot(r_normal) > 0)
+ r_normal = -r_normal;
}
return inters;
}
+bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const {
-bool TriangleMesh::intersect_ray(const Vector3& p_begin,const Vector3& p_dir,Vector3 &r_point, Vector3 &r_normal) const {
-
-
- uint32_t* stack = (uint32_t*)alloca(sizeof(int)*max_depth);
+ uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
enum {
- TEST_AABB_BIT=0,
- VISIT_LEFT_BIT=1,
- VISIT_RIGHT_BIT=2,
- VISIT_DONE_BIT=3,
- VISITED_BIT_SHIFT=29,
- NODE_IDX_MASK=(1<<VISITED_BIT_SHIFT)-1,
- VISITED_BIT_MASK=~NODE_IDX_MASK,
-
+ TEST_AABB_BIT = 0,
+ VISIT_LEFT_BIT = 1,
+ VISIT_RIGHT_BIT = 2,
+ VISIT_DONE_BIT = 3,
+ VISITED_BIT_SHIFT = 29,
+ NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
+ VISITED_BIT_MASK = ~NODE_IDX_MASK,
};
Vector3 n = p_dir;
- real_t d=1e20;
- bool inters=false;
+ real_t d = 1e20;
+ bool inters = false;
- int level=0;
+ int level = 0;
PoolVector<Triangle>::Read trianglesr = triangles.read();
- PoolVector<Vector3>::Read verticesr=vertices.read();
- PoolVector<BVH>::Read bvhr=bvh.read();
+ PoolVector<Vector3>::Read verticesr = vertices.read();
+ PoolVector<BVH>::Read bvhr = bvh.read();
- const Triangle *triangleptr=trianglesr.ptr();
- const Vector3 *vertexptr=verticesr.ptr();
- int pos=bvh.size()-1;
+ const Triangle *triangleptr = trianglesr.ptr();
+ const Vector3 *vertexptr = verticesr.ptr();
+ int pos = bvh.size() - 1;
const BVH *bvhptr = bvhr.ptr();
- stack[0]=pos;
- while(true) {
+ stack[0] = pos;
+ while (true) {
- uint32_t node = stack[level]&NODE_IDX_MASK;
- const BVH &b = bvhptr[ node ];
- bool done=false;
+ uint32_t node = stack[level] & NODE_IDX_MASK;
+ const BVH &b = bvhptr[node];
+ bool done = false;
- switch(stack[level]>>VISITED_BIT_SHIFT) {
+ switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
-
- bool valid = b.aabb.intersects_ray(p_begin,p_dir);
+ bool valid = b.aabb.intersects_ray(p_begin, p_dir);
if (!valid) {
- stack[level]=(VISIT_DONE_BIT<<VISITED_BIT_SHIFT)|node;
+ stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
- if (b.face_index>=0) {
-
- const Triangle &s=triangleptr[ b.face_index ];
- Face3 f3(vertexptr[ s.indices[0] ],vertexptr[ s.indices[1] ],vertexptr[ s.indices[2] ]);
+ if (b.face_index >= 0) {
+ const Triangle &s = triangleptr[b.face_index];
+ Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
Vector3 res;
- if (f3.intersects_ray(p_begin,p_dir,&res)) {
-
+ if (f3.intersects_ray(p_begin, p_dir, &res)) {
real_t nd = n.dot(res);
- if (nd<d) {
+ if (nd < d) {
- d=nd;
- r_point=res;
- r_normal=f3.get_plane().get_normal();
- inters=true;
+ d = nd;
+ r_point = res;
+ r_normal = f3.get_plane().get_normal();
+ inters = true;
}
-
}
- stack[level]=(VISIT_DONE_BIT<<VISITED_BIT_SHIFT)|node;
+ stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
- stack[level]=(VISIT_LEFT_BIT<<VISITED_BIT_SHIFT)|node;
+ stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
}
}
continue;
}
case VISIT_LEFT_BIT: {
- stack[level]=(VISIT_RIGHT_BIT<<VISITED_BIT_SHIFT)|node;
- stack[level+1]=b.left|TEST_AABB_BIT;
+ stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
+ stack[level + 1] = b.left | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_RIGHT_BIT: {
- stack[level]=(VISIT_DONE_BIT<<VISITED_BIT_SHIFT)|node;
- stack[level+1]=b.right|TEST_AABB_BIT;
+ stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
+ stack[level + 1] = b.right | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_DONE_BIT: {
- if (level==0) {
- done=true;
+ if (level == 0) {
+ done = true;
break;
} else
level--;
@@ -507,16 +475,14 @@ bool TriangleMesh::intersect_ray(const Vector3& p_begin,const Vector3& p_dir,Vec
}
}
-
if (done)
break;
}
-
if (inters) {
- if (n.dot(r_normal)>0)
- r_normal=-r_normal;
+ if (n.dot(r_normal) > 0)
+ r_normal = -r_normal;
}
return inters;
@@ -536,13 +502,13 @@ PoolVector<Face3> TriangleMesh::get_faces() const {
int ts = triangles.size();
faces.resize(triangles.size());
- PoolVector<Face3>::Write w=faces.write();
+ PoolVector<Face3>::Write w = faces.write();
PoolVector<Triangle>::Read r = triangles.read();
PoolVector<Vector3>::Read rv = vertices.read();
- for(int i=0;i<ts;i++) {
- for(int j=0;j<3;j++) {
- w[i].vertex[j]=rv[r[i].indices[j]];
+ for (int i = 0; i < ts; i++) {
+ for (int j = 0; j < 3; j++) {
+ w[i].vertex[j] = rv[r[i].indices[j]];
}
}
@@ -552,6 +518,6 @@ PoolVector<Face3> TriangleMesh::get_faces() const {
TriangleMesh::TriangleMesh() {
- valid=false;
- max_depth=0;
+ valid = false;
+ max_depth = 0;
}
diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h
index 65250c023d..7f81e54613 100644
--- a/core/math/triangle_mesh.h
+++ b/core/math/triangle_mesh.h
@@ -29,11 +29,11 @@
#ifndef TRIANGLE_MESH_H
#define TRIANGLE_MESH_H
-#include "reference.h"
#include "face3.h"
+#include "reference.h"
class TriangleMesh : public Reference {
- GDCLASS( TriangleMesh, Reference);
+ GDCLASS(TriangleMesh, Reference);
struct Triangle {
@@ -56,7 +56,7 @@ class TriangleMesh : public Reference {
struct BVHCmpX {
- bool operator()(const BVH* p_left, const BVH* p_right) const {
+ bool operator()(const BVH *p_left, const BVH *p_right) const {
return p_left->center.x < p_right->center.x;
}
@@ -64,35 +64,33 @@ class TriangleMesh : public Reference {
struct BVHCmpY {
- bool operator()(const BVH* p_left, const BVH* p_right) const {
+ bool operator()(const BVH *p_left, const BVH *p_right) const {
return p_left->center.y < p_right->center.y;
}
};
struct BVHCmpZ {
- bool operator()(const BVH* p_left, const BVH* p_right) const {
+ bool operator()(const BVH *p_left, const BVH *p_right) const {
return p_left->center.z < p_right->center.z;
}
};
- int _create_bvh(BVH*p_bvh,BVH** p_bb,int p_from,int p_size,int p_depth,int&max_depth,int&max_alloc);
+ int _create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc);
PoolVector<BVH> bvh;
int max_depth;
bool valid;
public:
-
bool is_valid() const;
- bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_point, Vector3 &r_normal) const;
- bool intersect_ray(const Vector3& p_begin,const Vector3& p_dir,Vector3 &r_point, Vector3 &r_normal) const;
- Vector3 get_area_normal(const Rect3& p_aabb) const;
+ bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const;
+ bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const;
+ Vector3 get_area_normal(const Rect3 &p_aabb) const;
PoolVector<Face3> get_faces() const;
-
- void create(const PoolVector<Vector3>& p_faces);
+ void create(const PoolVector<Vector3> &p_faces);
TriangleMesh();
};
diff --git a/core/math/triangulate.cpp b/core/math/triangulate.cpp
index 128b6ca331..8568a963ab 100644
--- a/core/math/triangulate.cpp
+++ b/core/math/triangulate.cpp
@@ -28,136 +28,140 @@
/*************************************************************************/
#include "triangulate.h"
-real_t Triangulate::get_area(const Vector<Vector2> &contour)
-{
+real_t Triangulate::get_area(const Vector<Vector2> &contour) {
- int n = contour.size();
- const Vector2 *c=&contour[0];
+ int n = contour.size();
+ const Vector2 *c = &contour[0];
- real_t A=0.0;
+ real_t A = 0.0;
- for(int p=n-1,q=0; q<n; p=q++)
- {
- A+= c[p].cross(c[q]);
- }
- return A*0.5;
+ for (int p = n - 1, q = 0; q < n; p = q++) {
+ A += c[p].cross(c[q]);
+ }
+ return A * 0.5;
}
- /*
+/*
is_inside_triangle decides if a point P is Inside of the triangle
defined by A, B, C.
*/
bool Triangulate::is_inside_triangle(real_t Ax, real_t Ay,
- real_t Bx, real_t By,
- real_t Cx, real_t Cy,
- real_t Px, real_t Py)
+ real_t Bx, real_t By,
+ real_t Cx, real_t Cy,
+ real_t Px, real_t Py)
{
- real_t ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
- real_t cCROSSap, bCROSScp, aCROSSbp;
-
- ax = Cx - Bx; ay = Cy - By;
- bx = Ax - Cx; by = Ay - Cy;
- cx = Bx - Ax; cy = By - Ay;
- apx= Px - Ax; apy= Py - Ay;
- bpx= Px - Bx; bpy= Py - By;
- cpx= Px - Cx; cpy= Py - Cy;
-
- aCROSSbp = ax*bpy - ay*bpx;
- cCROSSap = cx*apy - cy*apx;
- bCROSScp = bx*cpy - by*cpx;
-
- return ((aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0));
+ real_t ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
+ real_t cCROSSap, bCROSScp, aCROSSbp;
+
+ ax = Cx - Bx;
+ ay = Cy - By;
+ bx = Ax - Cx;
+ by = Ay - Cy;
+ cx = Bx - Ax;
+ cy = By - Ay;
+ apx = Px - Ax;
+ apy = Py - Ay;
+ bpx = Px - Bx;
+ bpy = Py - By;
+ cpx = Px - Cx;
+ cpy = Py - Cy;
+
+ aCROSSbp = ax * bpy - ay * bpx;
+ cCROSSap = cx * apy - cy * apx;
+ bCROSScp = bx * cpy - by * cpx;
+
+ return ((aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0));
};
-bool Triangulate::snip(const Vector<Vector2> &p_contour,int u,int v,int w,int n,const Vector<int>& V)
-{
- int p;
- real_t Ax, Ay, Bx, By, Cx, Cy, Px, Py;
- const Vector2 *contour=&p_contour[0];
+bool Triangulate::snip(const Vector<Vector2> &p_contour, int u, int v, int w, int n, const Vector<int> &V) {
+ int p;
+ real_t Ax, Ay, Bx, By, Cx, Cy, Px, Py;
+ const Vector2 *contour = &p_contour[0];
- Ax = contour[V[u]].x;
- Ay = contour[V[u]].y;
+ Ax = contour[V[u]].x;
+ Ay = contour[V[u]].y;
- Bx = contour[V[v]].x;
- By = contour[V[v]].y;
+ Bx = contour[V[v]].x;
+ By = contour[V[v]].y;
- Cx = contour[V[w]].x;
- Cy = contour[V[w]].y;
+ Cx = contour[V[w]].x;
+ Cy = contour[V[w]].y;
- if ( CMP_EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false;
+ if (CMP_EPSILON > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax)))) return false;
- for (p=0;p<n;p++)
- {
- if( (p == u) || (p == v) || (p == w) ) continue;
- Px = contour[V[p]].x;
- Py = contour[V[p]].y;
- if (is_inside_triangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false;
- }
+ for (p = 0; p < n; p++) {
+ if ((p == u) || (p == v) || (p == w)) continue;
+ Px = contour[V[p]].x;
+ Py = contour[V[p]].y;
+ if (is_inside_triangle(Ax, Ay, Bx, By, Cx, Cy, Px, Py)) return false;
+ }
- return true;
+ return true;
}
-bool Triangulate::triangulate(const Vector<Vector2> &contour,Vector<int> &result)
-{
- /* allocate and initialize list of Vertices in polygon */
-
- int n = contour.size();
- if ( n < 3 ) return false;
+bool Triangulate::triangulate(const Vector<Vector2> &contour, Vector<int> &result) {
+ /* allocate and initialize list of Vertices in polygon */
+ int n = contour.size();
+ if (n < 3) return false;
- Vector<int> V;
- V.resize(n);
+ Vector<int> V;
+ V.resize(n);
- /* we want a counter-clockwise polygon in V */
+ /* we want a counter-clockwise polygon in V */
- if ( 0.0 < get_area(contour) )
- for (int v=0; v<n; v++) V[v] = v;
- else
- for(int v=0; v<n; v++) V[v] = (n-1)-v;
+ if (0.0 < get_area(contour))
+ for (int v = 0; v < n; v++)
+ V[v] = v;
+ else
+ for (int v = 0; v < n; v++)
+ V[v] = (n - 1) - v;
- int nv = n;
+ int nv = n;
- /* remove nv-2 Vertices, creating 1 triangle every time */
- int count = 2*nv; /* error detection */
+ /* remove nv-2 Vertices, creating 1 triangle every time */
+ int count = 2 * nv; /* error detection */
- for(int v=nv-1; nv>2; )
- {
- /* if we loop, it is probably a non-simple polygon */
- if (0 >= (count--))
- {
- //** Triangulate: ERROR - probable bad polygon!
- return false;
- }
+ for (int v = nv - 1; nv > 2;) {
+ /* if we loop, it is probably a non-simple polygon */
+ if (0 >= (count--)) {
+ //** Triangulate: ERROR - probable bad polygon!
+ return false;
+ }
- /* three consecutive vertices in current polygon, <u,v,w> */
- int u = v ; if (nv <= u) u = 0; /* previous */
- v = u+1; if (nv <= v) v = 0; /* new v */
- int w = v+1; if (nv <= w) w = 0; /* next */
+ /* three consecutive vertices in current polygon, <u,v,w> */
+ int u = v;
+ if (nv <= u) u = 0; /* previous */
+ v = u + 1;
+ if (nv <= v) v = 0; /* new v */
+ int w = v + 1;
+ if (nv <= w) w = 0; /* next */
- if ( snip(contour,u,v,w,nv,V) )
- {
- int a,b,c,s,t;
+ if (snip(contour, u, v, w, nv, V)) {
+ int a, b, c, s, t;
- /* true names of the vertices */
- a = V[u]; b = V[v]; c = V[w];
+ /* true names of the vertices */
+ a = V[u];
+ b = V[v];
+ c = V[w];
- /* output Triangle */
- result.push_back( a );
- result.push_back( b );
- result.push_back( c );
+ /* output Triangle */
+ result.push_back(a);
+ result.push_back(b);
+ result.push_back(c);
- /* remove v from remaining polygon */
- for(s=v,t=v+1;t<nv;s++,t++)
- V[s] = V[t];
+ /* remove v from remaining polygon */
+ for (s = v, t = v + 1; t < nv; s++, t++)
+ V[s] = V[t];
- nv--;
+ nv--;
- /* resest error detection counter */
- count = 2*nv;
- }
- }
+ /* resest error detection counter */
+ count = 2 * nv;
+ }
+ }
- return true;
+ return true;
}
diff --git a/core/math/triangulate.h b/core/math/triangulate.h
index ce77334519..c23d3ba27d 100644
--- a/core/math/triangulate.h
+++ b/core/math/triangulate.h
@@ -31,35 +31,28 @@
#include "math_2d.h"
-
/*
http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
*/
-class Triangulate
-{
+class Triangulate {
public:
-
// triangulate a contour/polygon, places results in STL vector
// as series of triangles.
- static bool triangulate(const Vector< Vector2 > &contour, Vector<int> &result);
+ static bool triangulate(const Vector<Vector2> &contour, Vector<int> &result);
// compute area of a contour/polygon
- static real_t get_area(const Vector< Vector2 > &contour);
+ static real_t get_area(const Vector<Vector2> &contour);
// decide if point Px/Py is inside triangle defined by
// (Ax,Ay) (Bx,By) (Cx,Cy)
static bool is_inside_triangle(real_t Ax, real_t Ay,
- real_t Bx, real_t By,
- real_t Cx, real_t Cy,
- real_t Px, real_t Py);
-
+ real_t Bx, real_t By,
+ real_t Cx, real_t Cy,
+ real_t Px, real_t Py);
private:
- static bool snip(const Vector<Vector2> &p_contour,int u,int v,int w,int n,const Vector<int>& V);
-
+ static bool snip(const Vector<Vector2> &p_contour, int u, int v, int w, int n, const Vector<int> &V);
};
-
-
#endif
diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp
index 2ab5fa0465..235840e06a 100644
--- a/core/math/vector3.cpp
+++ b/core/math/vector3.cpp
@@ -29,27 +29,25 @@
#include "vector3.h"
#include "matrix3.h"
+void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) {
-void Vector3::rotate(const Vector3& p_axis,real_t p_phi) {
-
- *this=Basis(p_axis,p_phi).xform(*this);
+ *this = Basis(p_axis, p_phi).xform(*this);
}
-Vector3 Vector3::rotated(const Vector3& p_axis,real_t p_phi) const {
+Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_phi) const {
Vector3 r = *this;
- r.rotate(p_axis,p_phi);
+ r.rotate(p_axis, p_phi);
return r;
}
-void Vector3::set_axis(int p_axis,real_t p_value) {
- ERR_FAIL_INDEX(p_axis,3);
- coord[p_axis]=p_value;
-
+void Vector3::set_axis(int p_axis, real_t p_value) {
+ ERR_FAIL_INDEX(p_axis, 3);
+ coord[p_axis] = p_value;
}
real_t Vector3::get_axis(int p_axis) const {
- ERR_FAIL_INDEX_V(p_axis,3,0);
+ ERR_FAIL_INDEX_V(p_axis, 3, 0);
return operator[](p_axis);
}
@@ -62,27 +60,25 @@ int Vector3::max_axis() const {
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
}
-
void Vector3::snap(real_t p_val) {
- x=Math::stepify(x,p_val);
- y=Math::stepify(y,p_val);
- z=Math::stepify(z,p_val);
+ x = Math::stepify(x, p_val);
+ y = Math::stepify(y, p_val);
+ z = Math::stepify(z, p_val);
}
Vector3 Vector3::snapped(real_t p_val) const {
- Vector3 v=*this;
+ Vector3 v = *this;
v.snap(p_val);
return v;
}
+Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const {
-Vector3 Vector3::cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,real_t p_t) const {
-
- Vector3 p0=p_pre_a;
- Vector3 p1=*this;
- Vector3 p2=p_b;
- Vector3 p3=p_post_b;
+ Vector3 p0 = p_pre_a;
+ Vector3 p1 = *this;
+ Vector3 p2 = p_b;
+ Vector3 p3 = p_post_b;
{
//normalize
@@ -91,44 +87,41 @@ Vector3 Vector3::cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, c
real_t bc = p1.distance_to(p2);
real_t cd = p2.distance_to(p3);
- if (ab>0)
- p0 = p1+(p0-p1)*(bc/ab);
- if (cd>0)
- p3 = p2+(p3-p2)*(bc/cd);
+ if (ab > 0)
+ p0 = p1 + (p0 - p1) * (bc / ab);
+ if (cd > 0)
+ p3 = p2 + (p3 - p2) * (bc / cd);
}
-
real_t t = p_t;
real_t t2 = t * t;
real_t t3 = t2 * t;
Vector3 out;
- out = 0.5 * ( ( p1 * 2.0) +
- ( -p0 + p2 ) * t +
- ( 2.0 * p0 - 5.0 * p1 + 4 * p2 - p3 ) * t2 +
- ( -p0 + 3.0 * p1 - 3.0 * p2 + p3 ) * t3 );
+ out = 0.5 * ((p1 * 2.0) +
+ (-p0 + p2) * t +
+ (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
+ (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
return out;
-
}
-Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,real_t p_t) const {
+Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const {
- Vector3 p0=p_pre_a;
- Vector3 p1=*this;
- Vector3 p2=p_b;
- Vector3 p3=p_post_b;
+ Vector3 p0 = p_pre_a;
+ Vector3 p1 = *this;
+ Vector3 p2 = p_b;
+ Vector3 p3 = p_post_b;
real_t t = p_t;
real_t t2 = t * t;
real_t t3 = t2 * t;
Vector3 out;
- out = 0.5 * ( ( p1 * 2.0) +
- ( -p0 + p2 ) * t +
- ( 2.0 * p0 - 5.0 * p1 + 4 * p2 - p3 ) * t2 +
- ( -p0 + 3.0 * p1 - 3.0 * p2 + p3 ) * t3 );
+ out = 0.5 * ((p1 * 2.0) +
+ (-p0 + p2) * t +
+ (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
+ (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
return out;
-
}
#if 0
@@ -175,8 +168,8 @@ Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, co
( -p0.z + 3.0 * p1.z - 3.0 * p2.z + p3.z ) * t3 );
return out;
}
-# endif
+#endif
Vector3::operator String() const {
- return (rtos(x)+", "+rtos(y)+", "+rtos(z));
+ return (rtos(x) + ", " + rtos(y) + ", " + rtos(z));
}
diff --git a/core/math/vector3.h b/core/math/vector3.h
index a289f9bf4c..fc02e66c33 100644
--- a/core/math/vector3.h
+++ b/core/math/vector3.h
@@ -29,9 +29,9 @@
#ifndef VECTOR3_H
#define VECTOR3_H
-#include "typedefs.h"
#include "math_defs.h"
#include "math_funcs.h"
+#include "typedefs.h"
#include "ustring.h"
class Basis;
@@ -54,17 +54,17 @@ struct Vector3 {
real_t coord[3];
};
- _FORCE_INLINE_ const real_t& operator[](int p_axis) const {
+ _FORCE_INLINE_ const real_t &operator[](int p_axis) const {
return coord[p_axis];
}
- _FORCE_INLINE_ real_t& operator[](int p_axis) {
+ _FORCE_INLINE_ real_t &operator[](int p_axis) {
return coord[p_axis];
}
- void set_axis(int p_axis,real_t p_value);
+ void set_axis(int p_axis, real_t p_value);
real_t get_axis(int p_axis) const;
int min_axis() const;
@@ -82,63 +82,63 @@ struct Vector3 {
void snap(real_t p_val);
Vector3 snapped(real_t p_val) const;
- void rotate(const Vector3& p_axis,real_t p_phi);
- Vector3 rotated(const Vector3& p_axis,real_t p_phi) const;
+ void rotate(const Vector3 &p_axis, real_t p_phi);
+ Vector3 rotated(const Vector3 &p_axis, real_t p_phi) const;
/* Static Methods between 2 vector3s */
- _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3& p_b,real_t p_t) const;
- Vector3 cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,real_t p_t) const;
- Vector3 cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,real_t p_t) const;
+ _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const;
+ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const;
+ Vector3 cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const;
- _FORCE_INLINE_ Vector3 cross(const Vector3& p_b) const;
- _FORCE_INLINE_ real_t dot(const Vector3& p_b) const;
- _FORCE_INLINE_ Basis outer(const Vector3& p_b) const;
+ _FORCE_INLINE_ Vector3 cross(const Vector3 &p_b) const;
+ _FORCE_INLINE_ real_t dot(const Vector3 &p_b) const;
+ _FORCE_INLINE_ Basis outer(const Vector3 &p_b) const;
_FORCE_INLINE_ Basis to_diagonal_matrix() const;
_FORCE_INLINE_ Vector3 abs() const;
_FORCE_INLINE_ Vector3 floor() const;
_FORCE_INLINE_ Vector3 ceil() const;
- _FORCE_INLINE_ real_t distance_to(const Vector3& p_b) const;
- _FORCE_INLINE_ real_t distance_squared_to(const Vector3& p_b) const;
-
- _FORCE_INLINE_ real_t angle_to(const Vector3& p_b) const;
-
+ _FORCE_INLINE_ real_t distance_to(const Vector3 &p_b) const;
+ _FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_b) const;
- _FORCE_INLINE_ Vector3 slide(const Vector3& p_vec) const;
- _FORCE_INLINE_ Vector3 reflect(const Vector3& p_vec) const;
+ _FORCE_INLINE_ real_t angle_to(const Vector3 &p_b) const;
+ _FORCE_INLINE_ Vector3 slide(const Vector3 &p_vec) const;
+ _FORCE_INLINE_ Vector3 reflect(const Vector3 &p_vec) const;
/* Operators */
- _FORCE_INLINE_ Vector3& operator+=(const Vector3& p_v);
- _FORCE_INLINE_ Vector3 operator+(const Vector3& p_v) const;
- _FORCE_INLINE_ Vector3& operator-=(const Vector3& p_v);
- _FORCE_INLINE_ Vector3 operator-(const Vector3& p_v) const;
- _FORCE_INLINE_ Vector3& operator*=(const Vector3& p_v);
- _FORCE_INLINE_ Vector3 operator*(const Vector3& p_v) const;
- _FORCE_INLINE_ Vector3& operator/=(const Vector3& p_v);
- _FORCE_INLINE_ Vector3 operator/(const Vector3& p_v) const;
+ _FORCE_INLINE_ Vector3 &operator+=(const Vector3 &p_v);
+ _FORCE_INLINE_ Vector3 operator+(const Vector3 &p_v) const;
+ _FORCE_INLINE_ Vector3 &operator-=(const Vector3 &p_v);
+ _FORCE_INLINE_ Vector3 operator-(const Vector3 &p_v) const;
+ _FORCE_INLINE_ Vector3 &operator*=(const Vector3 &p_v);
+ _FORCE_INLINE_ Vector3 operator*(const Vector3 &p_v) const;
+ _FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v);
+ _FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const;
-
- _FORCE_INLINE_ Vector3& operator*=(real_t p_scalar);
+ _FORCE_INLINE_ Vector3 &operator*=(real_t p_scalar);
_FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
- _FORCE_INLINE_ Vector3& operator/=(real_t p_scalar);
+ _FORCE_INLINE_ Vector3 &operator/=(real_t p_scalar);
_FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
_FORCE_INLINE_ Vector3 operator-() const;
- _FORCE_INLINE_ bool operator==(const Vector3& p_v) const;
- _FORCE_INLINE_ bool operator!=(const Vector3& p_v) const;
- _FORCE_INLINE_ bool operator<(const Vector3& p_v) const;
- _FORCE_INLINE_ bool operator<=(const Vector3& p_v) const;
+ _FORCE_INLINE_ bool operator==(const Vector3 &p_v) const;
+ _FORCE_INLINE_ bool operator!=(const Vector3 &p_v) const;
+ _FORCE_INLINE_ bool operator<(const Vector3 &p_v) const;
+ _FORCE_INLINE_ bool operator<=(const Vector3 &p_v) const;
operator String() const;
- _FORCE_INLINE_ Vector3() { x=y=z=0; }
- _FORCE_INLINE_ Vector3(real_t p_x,real_t p_y,real_t p_z) { x=p_x; y=p_y; z=p_z; }
-
+ _FORCE_INLINE_ Vector3() { x = y = z = 0; }
+ _FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) {
+ x = p_x;
+ y = p_y;
+ z = p_z;
+ }
};
#ifdef VECTOR3_IMPL_OVERRIDE
@@ -149,260 +149,258 @@ struct Vector3 {
#include "matrix3.h"
-Vector3 Vector3::cross(const Vector3& p_b) const {
+Vector3 Vector3::cross(const Vector3 &p_b) const {
- Vector3 ret (
- (y * p_b.z) - (z * p_b.y),
- (z * p_b.x) - (x * p_b.z),
- (x * p_b.y) - (y * p_b.x)
- );
+ Vector3 ret(
+ (y * p_b.z) - (z * p_b.y),
+ (z * p_b.x) - (x * p_b.z),
+ (x * p_b.y) - (y * p_b.x));
return ret;
}
-real_t Vector3::dot(const Vector3& p_b) const {
+real_t Vector3::dot(const Vector3 &p_b) const {
- return x*p_b.x + y*p_b.y + z*p_b.z;
+ return x * p_b.x + y * p_b.y + z * p_b.z;
}
-Basis Vector3::outer(const Vector3& p_b) const {
-
- Vector3 row0(x*p_b.x, x*p_b.y, x*p_b.z);
- Vector3 row1(y*p_b.x, y*p_b.y, y*p_b.z);
- Vector3 row2(z*p_b.x, z*p_b.y, z*p_b.z);
+Basis Vector3::outer(const Vector3 &p_b) const {
+
+ Vector3 row0(x * p_b.x, x * p_b.y, x * p_b.z);
+ Vector3 row1(y * p_b.x, y * p_b.y, y * p_b.z);
+ Vector3 row2(z * p_b.x, z * p_b.y, z * p_b.z);
return Basis(row0, row1, row2);
}
Basis Vector3::to_diagonal_matrix() const {
return Basis(x, 0, 0,
- 0, y, 0,
- 0, 0, z);
+ 0, y, 0,
+ 0, 0, z);
}
Vector3 Vector3::abs() const {
- return Vector3( Math::abs(x), Math::abs(y), Math::abs(z) );
+ return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
}
Vector3 Vector3::floor() const {
- return Vector3( Math::floor(x), Math::floor(y), Math::floor(z) );
+ return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
}
Vector3 Vector3::ceil() const {
- return Vector3( Math::ceil(x), Math::ceil(y), Math::ceil(z) );
+ return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
}
-Vector3 Vector3::linear_interpolate(const Vector3& p_b,real_t p_t) const {
+Vector3 Vector3::linear_interpolate(const Vector3 &p_b, real_t p_t) const {
return Vector3(
- x+(p_t * (p_b.x-x)),
- y+(p_t * (p_b.y-y)),
- z+(p_t * (p_b.z-z))
- );
+ x + (p_t * (p_b.x - x)),
+ y + (p_t * (p_b.y - y)),
+ z + (p_t * (p_b.z - z)));
}
-real_t Vector3::distance_to(const Vector3& p_b) const {
+real_t Vector3::distance_to(const Vector3 &p_b) const {
- return (p_b-*this).length();
+ return (p_b - *this).length();
}
-real_t Vector3::distance_squared_to(const Vector3& p_b) const {
+real_t Vector3::distance_squared_to(const Vector3 &p_b) const {
- return (p_b-*this).length_squared();
+ return (p_b - *this).length_squared();
}
-real_t Vector3::angle_to(const Vector3& p_b) const {
+real_t Vector3::angle_to(const Vector3 &p_b) const {
return Math::acos(this->dot(p_b) / Math::sqrt(this->length_squared() * p_b.length_squared()));
}
/* Operators */
-Vector3& Vector3::operator+=(const Vector3& p_v) {
+Vector3 &Vector3::operator+=(const Vector3 &p_v) {
- x+=p_v.x;
- y+=p_v.y;
- z+=p_v.z;
+ x += p_v.x;
+ y += p_v.y;
+ z += p_v.z;
return *this;
}
-Vector3 Vector3::operator+(const Vector3& p_v) const {
+Vector3 Vector3::operator+(const Vector3 &p_v) const {
- return Vector3(x+p_v.x, y+p_v.y, z+ p_v.z);
+ return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
}
-Vector3& Vector3::operator-=(const Vector3& p_v) {
+Vector3 &Vector3::operator-=(const Vector3 &p_v) {
- x-=p_v.x;
- y-=p_v.y;
- z-=p_v.z;
+ x -= p_v.x;
+ y -= p_v.y;
+ z -= p_v.z;
return *this;
}
-Vector3 Vector3::operator-(const Vector3& p_v) const {
+Vector3 Vector3::operator-(const Vector3 &p_v) const {
- return Vector3(x-p_v.x, y-p_v.y, z- p_v.z);
+ return Vector3(x - p_v.x, y - p_v.y, z - p_v.z);
}
-Vector3& Vector3::operator*=(const Vector3& p_v) {
+Vector3 &Vector3::operator*=(const Vector3 &p_v) {
- x*=p_v.x;
- y*=p_v.y;
- z*=p_v.z;
+ x *= p_v.x;
+ y *= p_v.y;
+ z *= p_v.z;
return *this;
}
-Vector3 Vector3::operator*(const Vector3& p_v) const {
+Vector3 Vector3::operator*(const Vector3 &p_v) const {
- return Vector3(x*p_v.x, y*p_v.y, z* p_v.z);
+ return Vector3(x * p_v.x, y * p_v.y, z * p_v.z);
}
-Vector3& Vector3::operator/=(const Vector3& p_v) {
+Vector3 &Vector3::operator/=(const Vector3 &p_v) {
- x/=p_v.x;
- y/=p_v.y;
- z/=p_v.z;
+ x /= p_v.x;
+ y /= p_v.y;
+ z /= p_v.z;
return *this;
}
-Vector3 Vector3::operator/(const Vector3& p_v) const {
+Vector3 Vector3::operator/(const Vector3 &p_v) const {
- return Vector3(x/p_v.x, y/p_v.y, z/ p_v.z);
+ return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
}
-Vector3& Vector3::operator*=(real_t p_scalar) {
+Vector3 &Vector3::operator*=(real_t p_scalar) {
- x*=p_scalar;
- y*=p_scalar;
- z*=p_scalar;
+ x *= p_scalar;
+ y *= p_scalar;
+ z *= p_scalar;
return *this;
}
-_FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3& p_vec) {
+_FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
return p_vec * p_scalar;
}
Vector3 Vector3::operator*(real_t p_scalar) const {
- return Vector3( x*p_scalar, y*p_scalar, z*p_scalar);
+ return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
}
-Vector3& Vector3::operator/=(real_t p_scalar) {
+Vector3 &Vector3::operator/=(real_t p_scalar) {
- x/=p_scalar;
- y/=p_scalar;
- z/=p_scalar;
+ x /= p_scalar;
+ y /= p_scalar;
+ z /= p_scalar;
return *this;
}
Vector3 Vector3::operator/(real_t p_scalar) const {
- return Vector3( x/p_scalar, y/p_scalar, z/p_scalar);
+ return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
}
Vector3 Vector3::operator-() const {
- return Vector3( -x, -y, -z );
+ return Vector3(-x, -y, -z);
}
-bool Vector3::operator==(const Vector3& p_v) const {
+bool Vector3::operator==(const Vector3 &p_v) const {
- return (x==p_v.x && y==p_v.y && z==p_v.z);
+ return (x == p_v.x && y == p_v.y && z == p_v.z);
}
-bool Vector3::operator!=(const Vector3& p_v) const {
- return (x!=p_v.x || y!=p_v.y || z!=p_v.z);
+bool Vector3::operator!=(const Vector3 &p_v) const {
+ return (x != p_v.x || y != p_v.y || z != p_v.z);
}
-bool Vector3::operator<(const Vector3& p_v) const {
+bool Vector3::operator<(const Vector3 &p_v) const {
- if (x==p_v.x) {
- if (y==p_v.y)
- return z<p_v.z;
+ if (x == p_v.x) {
+ if (y == p_v.y)
+ return z < p_v.z;
else
- return y<p_v.y;
+ return y < p_v.y;
} else {
- return x<p_v.x;
+ return x < p_v.x;
}
}
-bool Vector3::operator<=(const Vector3& p_v) const {
+bool Vector3::operator<=(const Vector3 &p_v) const {
- if (x==p_v.x) {
- if (y==p_v.y)
- return z<=p_v.z;
+ if (x == p_v.x) {
+ if (y == p_v.y)
+ return z <= p_v.z;
else
- return y<p_v.y;
+ return y < p_v.y;
} else {
- return x<p_v.x;
+ return x < p_v.x;
}
}
-_FORCE_INLINE_ Vector3 vec3_cross(const Vector3& p_a, const Vector3& p_b) {
+_FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
return p_a.cross(p_b);
}
-_FORCE_INLINE_ real_t vec3_dot(const Vector3& p_a, const Vector3& p_b) {
+_FORCE_INLINE_ real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
return p_a.dot(p_b);
}
real_t Vector3::length() const {
- real_t x2=x*x;
- real_t y2=y*y;
- real_t z2=z*z;
+ real_t x2 = x * x;
+ real_t y2 = y * y;
+ real_t z2 = z * z;
- return Math::sqrt(x2+y2+z2);
+ return Math::sqrt(x2 + y2 + z2);
}
real_t Vector3::length_squared() const {
- real_t x2=x*x;
- real_t y2=y*y;
- real_t z2=z*z;
+ real_t x2 = x * x;
+ real_t y2 = y * y;
+ real_t z2 = z * z;
- return x2+y2+z2;
+ return x2 + y2 + z2;
}
void Vector3::normalize() {
- real_t l=length();
- if (l==0) {
- x=y=z=0;
+ real_t l = length();
+ if (l == 0) {
+ x = y = z = 0;
} else {
- x/=l;
- y/=l;
- z/=l;
+ x /= l;
+ y /= l;
+ z /= l;
}
}
Vector3 Vector3::normalized() const {
- Vector3 v=*this;
+ Vector3 v = *this;
v.normalize();
return v;
}
Vector3 Vector3::inverse() const {
- return Vector3( 1.0/x, 1.0/y, 1.0/z );
+ return Vector3(1.0 / x, 1.0 / y, 1.0 / z);
}
void Vector3::zero() {
- x=y=z=0;
+ x = y = z = 0;
}
-Vector3 Vector3::slide(const Vector3& p_vec) const {
+Vector3 Vector3::slide(const Vector3 &p_vec) const {
return p_vec - *this * this->dot(p_vec);
}
-Vector3 Vector3::reflect(const Vector3& p_vec) const {
+Vector3 Vector3::reflect(const Vector3 &p_vec) const {
return p_vec - *this * this->dot(p_vec) * 2.0;
}