aboutsummaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorRémi Verschelde2017-03-19 00:36:26 +0100
committerRémi Verschelde2017-03-19 00:36:26 +0100
commitf8db8a3faa30b71dca33ced38be16d3f93f43e8a (patch)
tree3b798318132cca7eccfbca5818ab55656a2896d7 /core/math
parent1d418afe863c9e553b69174ce63aef203c46d2f0 (diff)
downloadgodot-f8db8a3faa30b71dca33ced38be16d3f93f43e8a.tar.gz
godot-f8db8a3faa30b71dca33ced38be16d3f93f43e8a.tar.zst
godot-f8db8a3faa30b71dca33ced38be16d3f93f43e8a.zip
Bring that Whole New World to the Old Continent too
Applies the clang-format style to the 2.1 branch as done for master in 5dbf1809c6e3e905b94b8764e99491e608122261.
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/aabb.cpp355
-rw-r--r--core/math/aabb.h324
-rw-r--r--core/math/bsp_tree.cpp420
-rw-r--r--core/math/bsp_tree.h105
-rw-r--r--core/math/camera_matrix.cpp537
-rw-r--r--core/math/camera_matrix.h36
-rw-r--r--core/math/face3.cpp416
-rw-r--r--core/math/face3.h249
-rw-r--r--core/math/geometry.cpp834
-rw-r--r--core/math/geometry.h733
-rw-r--r--core/math/math_2d.cpp362
-rw-r--r--core/math/math_2d.h704
-rw-r--r--core/math/math_defs.h8
-rw-r--r--core/math/math_funcs.cpp114
-rw-r--r--core/math/math_funcs.h48
-rw-r--r--core/math/matrix3.cpp351
-rw-r--r--core/math/matrix3.h169
-rw-r--r--core/math/octree.h991
-rw-r--r--core/math/plane.cpp81
-rw-r--r--core/math/plane.h84
-rw-r--r--core/math/quat.cpp109
-rw-r--r--core/math/quat.h167
-rw-r--r--core/math/quick_hull.cpp301
-rw-r--r--core/math/quick_hull.h42
-rw-r--r--core/math/transform.cpp98
-rw-r--r--core/math/transform.h239
-rw-r--r--core/math/triangle_mesh.cpp418
-rw-r--r--core/math/triangle_mesh.h22
-rw-r--r--core/math/triangulate.cpp186
-rw-r--r--core/math/triangulate.h21
-rw-r--r--core/math/vector3.cpp86
-rw-r--r--core/math/vector3.h259
34 files changed, 4341 insertions, 4877 deletions
diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp
index 0b45577b5..401a33da8 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, float 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;
-
}
-float AStar::get_point_weight_scale(int p_id) const{
+float 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;
- float closest_dist=1e20;
+ int closest_id = -1;
+ float 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()) {
float 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 {
float 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);
float 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;
- float least_cost=1e30;
+ SelfList<Point> *least_cost_point = NULL;
+ float 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();
- float cost=p->distance;
- cost+=p->pos.distance_to(end_point->pos);
- cost*=p->weight_scale;
+ float 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];
float 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;
-
}
DVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
- ERR_FAIL_COND_V(!points.has(p_from_id),DVector<Vector3>());
- ERR_FAIL_COND_V(!points.has(p_to_id),DVector<Vector3>());
-
+ ERR_FAIL_COND_V(!points.has(p_from_id), DVector<Vector3>());
+ ERR_FAIL_COND_V(!points.has(p_to_id), DVector<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) {
DVector<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 DVector<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;
}
DVector<Vector3> path;
@@ -335,54 +312,49 @@ DVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
{
DVector<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;
-
}
-
DVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
- ERR_FAIL_COND_V(!points.has(p_from_id),DVector<int>());
- ERR_FAIL_COND_V(!points.has(p_to_id),DVector<int>());
-
+ ERR_FAIL_COND_V(!points.has(p_from_id), DVector<int>());
+ ERR_FAIL_COND_V(!points.has(p_to_id), DVector<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) {
DVector<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 DVector<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;
}
DVector<int> path;
@@ -391,15 +363,14 @@ DVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
{
DVector<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 @@ DVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
void AStar::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("get_available_point_id"),&AStar::get_available_point_id);
- ObjectTypeDB::bind_method(_MD("add_point","id","pos","weight_scale"),&AStar::add_point,DEFVAL(1.0));
- ObjectTypeDB::bind_method(_MD("get_point_pos","id"),&AStar::get_point_pos);
- ObjectTypeDB::bind_method(_MD("get_point_weight_scale","id"),&AStar::get_point_weight_scale);
- ObjectTypeDB::bind_method(_MD("remove_point","id"),&AStar::remove_point);
-
- ObjectTypeDB::bind_method(_MD("connect_points","id","to_id"),&AStar::connect_points);
- ObjectTypeDB::bind_method(_MD("disconnect_points","id","to_id"),&AStar::disconnect_points);
- ObjectTypeDB::bind_method(_MD("are_points_connected","id","to_id"),&AStar::are_points_connected);
+ ObjectTypeDB::bind_method(_MD("get_available_point_id"), &AStar::get_available_point_id);
+ ObjectTypeDB::bind_method(_MD("add_point", "id", "pos", "weight_scale"), &AStar::add_point, DEFVAL(1.0));
+ ObjectTypeDB::bind_method(_MD("get_point_pos", "id"), &AStar::get_point_pos);
+ ObjectTypeDB::bind_method(_MD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale);
+ ObjectTypeDB::bind_method(_MD("remove_point", "id"), &AStar::remove_point);
- ObjectTypeDB::bind_method(_MD("clear"),&AStar::clear);
+ ObjectTypeDB::bind_method(_MD("connect_points", "id", "to_id"), &AStar::connect_points);
+ ObjectTypeDB::bind_method(_MD("disconnect_points", "id", "to_id"), &AStar::disconnect_points);
+ ObjectTypeDB::bind_method(_MD("are_points_connected", "id", "to_id"), &AStar::are_points_connected);
- ObjectTypeDB::bind_method(_MD("get_closest_point","to_pos"),&AStar::get_closest_point);
- ObjectTypeDB::bind_method(_MD("get_closest_pos_in_segment","to_pos"),&AStar::get_closest_pos_in_segment);
+ ObjectTypeDB::bind_method(_MD("clear"), &AStar::clear);
- ObjectTypeDB::bind_method(_MD("get_point_path","from_id","to_id"),&AStar::get_point_path);
- ObjectTypeDB::bind_method(_MD("get_id_path","from_id","to_id"),&AStar::get_id_path);
+ ObjectTypeDB::bind_method(_MD("get_closest_point", "to_pos"), &AStar::get_closest_point);
+ ObjectTypeDB::bind_method(_MD("get_closest_pos_in_segment", "to_pos"), &AStar::get_closest_pos_in_segment);
+ ObjectTypeDB::bind_method(_MD("get_point_path", "from_id", "to_id"), &AStar::get_point_path);
+ ObjectTypeDB::bind_method(_MD("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 f9ffc477f..c88591cf9 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 {
-
- OBJ_TYPE(AStar,Reference)
+class AStar : public Reference {
+ OBJ_TYPE(AStar, Reference)
uint64_t pass;
@@ -51,16 +50,17 @@ class AStar: public Reference {
float weight_scale;
uint64_t last_pass;
- Vector<Point*> neighbours;
+ Vector<Point *> neighbours;
//used for pathfinding
Point *prev_point;
float 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,float p_weight_scale=1);
+ void add_point(int p_id, const Vector3 &p_pos, float p_weight_scale = 1);
Vector3 get_point_pos(int p_id) const;
float 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;
DVector<Vector3> get_point_path(int p_from_id, int p_to_id);
DVector<int> get_id_path(int p_from_id, int p_to_id);
diff --git a/core/math/aabb.cpp b/core/math/aabb.cpp
index d304d3e09..b9f7e8272 100644
--- a/core/math/aabb.cpp
+++ b/core/math/aabb.cpp
@@ -32,94 +32,87 @@
float AABB::get_area() const {
- return size.x*size.y*size.z;
-
+ return size.x * size.y * size.z;
}
-bool AABB::operator==(const AABB& p_rval) const {
-
- return ((pos==p_rval.pos) && (size==p_rval.size));
+bool AABB::operator==(const AABB &p_rval) const {
+ return ((pos == p_rval.pos) && (size == p_rval.size));
}
-bool AABB::operator!=(const AABB& p_rval) const {
-
- return ((pos!=p_rval.pos) || (size!=p_rval.size));
+bool AABB::operator!=(const AABB &p_rval) const {
+ return ((pos != p_rval.pos) || (size != p_rval.size));
}
-void AABB::merge_with(const AABB& p_aabb) {
+void AABB::merge_with(const AABB &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;
}
-AABB AABB::intersection(const AABB& p_aabb) const {
+AABB AABB::intersection(const AABB &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 AABB();
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 AABB();
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 AABB();
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 AABB( min, max-min );
+ return AABB(min, max - min);
}
-bool AABB::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip,Vector3* r_normal) const {
+bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const {
Vector3 c1, c2;
- Vector3 end = pos+size;
- float near=-1e20;
- float far=1e20;
- int axis=0;
+ Vector3 end = pos + size;
+ float near = -1e20;
+ float 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 AABB::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r
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 AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const {
-bool AABB::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;
+ float sign = 0;
- real_t min=0,max=1;
- int axis=0;
- float 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;
float 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 AABB::intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3
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 AABB::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 AABB::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 AABB::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 AABB::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 AABB::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;
}
-AABB AABB::merge(const AABB& p_with) const {
+AABB AABB::merge(const AABB &p_with) const {
- AABB aabb=*this;
+ AABB aabb = *this;
aabb.merge_with(p_with);
return aabb;
}
-AABB AABB::expand(const Vector3& p_vector) const {
- AABB aabb=*this;
+AABB AABB::expand(const Vector3 &p_vector) const {
+ AABB aabb = *this;
aabb.expand_to(p_vector);
return aabb;
-
}
AABB AABB::grow(real_t p_by) const {
- AABB aabb=*this;
+ AABB aabb = *this;
aabb.grow_by(p_by);
return aabb;
}
-void AABB::get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const {
+void AABB::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;
-
}
-
}
AABB::operator String() const {
- return String()+pos +" - "+ size;
+ return String() + pos + " - " + size;
}
diff --git a/core/math/aabb.h b/core/math/aabb.h
index 1c0adf901..63f822fbb 100644
--- a/core/math/aabb.h
+++ b/core/math/aabb.h
@@ -29,17 +29,13 @@
#ifndef AABB_H
#define AABB_H
-
-
-#include "vector3.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 AABB {
public:
Vector3 pos;
@@ -48,40 +44,38 @@ public:
float 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 AABB& p_rval) const;
- bool operator!=(const AABB& p_rval) const;
+ bool operator==(const AABB &p_rval) const;
+ bool operator!=(const AABB &p_rval) const;
- _FORCE_INLINE_ bool intersects(const AABB& p_aabb) const; /// Both AABBs overlap
- _FORCE_INLINE_ bool intersects_inclusive(const AABB& p_aabb) const; /// Both AABBs (or their faces) overlap
- _FORCE_INLINE_ bool encloses(const AABB & p_aabb) const; /// p_aabb is completely inside this
+ _FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
+ _FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
+ _FORCE_INLINE_ bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this
- AABB merge(const AABB& p_with) const;
- void merge_with(const AABB& p_aabb); ///merge with another AABB
- AABB intersection(const AABB& 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, float t0, float t1) const;
+ AABB merge(const AABB &p_with) const;
+ void merge_with(const AABB &p_aabb); ///merge with another AABB
+ AABB intersection(const AABB &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, float t0, float 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;
@@ -94,98 +88,97 @@ public:
AABB 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;
- AABB expand(const Vector3& p_vector) const;
- _FORCE_INLINE_ void project_range_in_plane(const Plane& p_plane,float &r_min,float& r_max) const;
- _FORCE_INLINE_ void expand_to(const Vector3& p_vector); /** expand to contain a point if necesary */
+ AABB expand(const Vector3 &p_vector) const;
+ _FORCE_INLINE_ void project_range_in_plane(const Plane &p_plane, float &r_min, float &r_max) const;
+ _FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necesary */
operator String() const;
_FORCE_INLINE_ AABB() {}
- inline AABB(const Vector3 &p_pos,const Vector3& p_size) { pos=p_pos; size=p_size; }
-
-
+ inline AABB(const Vector3 &p_pos, const Vector3 &p_size) {
+ pos = p_pos;
+ size = p_size;
+ }
};
-inline bool AABB::intersects(const AABB& p_aabb) const {
+inline bool AABB::intersects(const AABB &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;
+ 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 AABB::intersects_inclusive(const AABB& p_aabb) const {
+inline bool AABB::intersects_inclusive(const AABB &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;
+ 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 AABB::encloses(const AABB & p_aabb) const {
+inline bool AABB::encloses(const AABB &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 AABB::get_support(const Vector3& p_normal) const {
+Vector3 AABB::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 AABB::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());
@@ -198,14 +191,13 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) con
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;
}
@@ -213,33 +205,31 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) con
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) {
@@ -251,69 +241,68 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) con
#endif
}
-bool AABB::has_point(const Vector3& p_point) const {
+bool AABB::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 AABB::expand_to(const Vector3 &p_vector) {
-inline void AABB::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 AABB::project_range_in_plane(const Plane& p_plane,float &r_min,float& r_max) const {
+void AABB::project_range_in_plane(const Plane &p_plane, float &r_min, float &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);
float length = p_plane.normal.abs().dot(half_extents);
- float distance = p_plane.distance_to( center );
+ float distance = p_plane.distance_to(center);
r_min = distance - length;
r_max = distance + length;
}
inline real_t AABB::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;
@@ -321,74 +310,71 @@ inline real_t AABB::get_longest_axis_size() const {
inline real_t AABB::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 AABB::smits_intersect_ray(const Vector3 &from,const Vector3& dir, float t0, float t1) const {
+bool AABB::smits_intersect_ray(const Vector3 &from, const Vector3 &dir, float t0, float t1) const {
- float divx=1.0/dir.x;
- float divy=1.0/dir.y;
- float divz=1.0/dir.z;
+ float divx = 1.0 / dir.x;
+ float divy = 1.0 / dir.y;
+ float divz = 1.0 / dir.z;
- Vector3 upbound=pos+size;
+ Vector3 upbound = pos + size;
float 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 AABB::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;
}
typedef AABB Rect3;
diff --git a/core/math/bsp_tree.cpp b/core/math/bsp_tree.cpp
index 5242abfa3..6f1343d28 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 AABB& p_aabb) {
+void BSP_Tree::from_aabb(const AABB &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 @@ AABB 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;
float dist_min = p.distance_to(min);
float 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,float p_tolerance) {
+static int _bsp_find_best_half_plane(const Face3 *p_faces, const Vector<int> &p_indices, float p_tolerance) {
int ic = p_indices.size();
- const int*indices=p_indices.ptr();
+ const int *indices = p_indices.ptr();
int best_plane = -1;
float 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++) {
float 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++;
-
}
-
-
//double split_cost = num_spanning / (double) face_count;
- double relation = Math::abs(num_over-num_under) / (double) ic;
+ double relation = Math::abs(num_over - num_under) / (double)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,57 +332,53 @@ static int _bsp_find_best_half_plane(const Face3* p_faces,const Vector<int>& p_i
double 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, float 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,float 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++) {
float 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++;
@@ -409,183 +388,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<float> 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;
DVector<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"));
DVector<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::REAL_ARRAY) {
- if (d["planes"].get_type()==Variant::REAL_ARRAY) {
-
- DVector<float> src_planes=d["planes"];
- int plane_count=src_planes.size();
- ERR_FAIL_COND(plane_count%4);
- planes.resize(plane_count/4);
+ DVector<float> src_planes = d["planes"];
+ int plane_count = src_planes.size();
+ ERR_FAIL_COND(plane_count % 4);
+ planes.resize(plane_count / 4);
if (plane_count) {
DVector<float>::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);
+ // int node_count = src_nodes.size();
+ nodes.resize(src_nodes.size() / 3);
DVector<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 DVector<Face3>& p_faces,float p_error_radius) {
+BSP_Tree::BSP_Tree(const DVector<Face3> &p_faces, float p_error_radius) {
// compute aabb
- int face_count=p_faces.size();
- DVector<Face3>::Read faces_r=p_faces.read();
+ int face_count = p_faces.size();
+ DVector<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]);
@@ -593,36 +558,29 @@ BSP_Tree::BSP_Tree(const DVector<Face3>& p_faces,float 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 AABB& p_aabb,float 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 AABB &p_aabb, float 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 3913e3d34..9cf9d2fdd 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 "plane.h"
#include "aabb.h"
-#include "face3.h"
-#include "vector.h"
#include "dvector.h"
-#include "variant.h"
+#include "face3.h"
#include "method_ptrcall.h"
+#include "plane.h"
+#include "variant.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:
AABB aabb;
float 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;
AABB 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 AABB& p_aabb);
+ void from_aabb(const AABB &p_aabb);
BSP_Tree();
- BSP_Tree(const Variant& p_variant);
- BSP_Tree(const DVector<Face3>& p_faces,float p_error_radius=0);
- BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const AABB& p_aabb,float p_error_radius=0);
+ BSP_Tree(const Variant &p_variant);
+ BSP_Tree(const DVector<Face3> &p_faces, float p_error_radius = 0);
+ BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const AABB &p_aabb, float 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];
- float min,max;
- p_convex.project_range(p.normal,min,max);
+ float 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 a62409ac0..212ad9aea 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) {
+Plane CameraMatrix::xform4(const Plane &p_vec4) {
Plane ret;
@@ -65,11 +63,10 @@ Plane CameraMatrix::xform4(const Plane& p_vec4) {
return ret;
}
-void CameraMatrix::set_perspective(float p_fovy_degrees, float p_aspect, float p_z_near, float p_z_far,bool p_flip_fov) {
+void CameraMatrix::set_perspective(float p_fovy_degrees, float p_aspect, float p_z_near, float 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);
}
float sine, cotangent, deltaZ;
@@ -78,8 +75,8 @@ void CameraMatrix::set_perspective(float p_fovy_degrees, float p_aspect, float p
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(float p_fovy_degrees, float p_aspect, float p
matrix[2][3] = -1;
matrix[3][2] = -2 * p_z_near * p_z_far / deltaZ;
matrix[3][3] = 0;
-
}
-void CameraMatrix::set_orthogonal(float p_left, float p_right, float p_bottom, float p_top, float p_znear, float p_zfar) {
-
+void CameraMatrix::set_orthogonal(float p_left, float p_right, float p_bottom, float p_top, float p_znear, float 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(float p_size, float p_aspect, float p_znear, float p_zfar,bool p_flip_fov) {
+void CameraMatrix::set_orthogonal(float p_size, float p_aspect, float p_znear, float 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(float p_left, float p_right, float p_bottom, float p_top, float p_near, float 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(float p_left, float p_right, float p_bottom, floa
matrix[3][3]=0;
#else
float *te = &matrix[0][0];
- float x = 2 * p_near / ( p_right - p_left );
- float y = 2 * p_near / ( p_top - p_bottom );
+ float x = 2 * p_near / (p_right - p_left);
+ float y = 2 * p_near / (p_top - p_bottom);
- float a = ( p_right + p_left ) / ( p_right - p_left );
- float b = ( p_top + p_bottom ) / ( p_top - p_bottom );
- float c = - ( p_far + p_near ) / ( p_far - p_near );
- float d = - 2 * p_far * p_near / ( p_far - p_near );
+ float a = (p_right + p_left) / (p_right - p_left);
+ float b = (p_top + p_bottom) / (p_top - p_bottom);
+ float c = -(p_far + p_near) / (p_far - p_near);
+ float d = -2 * p_far * p_near / (p_far - p_near);
te[0] = x;
te[1] = 0;
@@ -161,120 +153,117 @@ void CameraMatrix::set_frustum(float p_left, float p_right, float p_bottom, floa
te[15] = 0;
#endif
-
}
-
-
float CameraMatrix::get_z_far() const {
- const float * matrix = (const float*)this->matrix;
- Plane new_plane=Plane(matrix[ 3] - matrix[ 2],
- matrix[ 7] - matrix[ 6],
- matrix[11] - matrix[10],
- matrix[15] - matrix[14]);
+ const float *matrix = (const float *)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;
}
float CameraMatrix::get_z_near() const {
- const float * matrix = (const float*)this->matrix;
- Plane new_plane=Plane(matrix[ 3] + matrix[ 2],
- matrix[ 7] + matrix[ 6],
- matrix[11] + matrix[10],
- -matrix[15] - matrix[14]);
+ const float *matrix = (const float *)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(float& r_width, float& r_height) const {
+void CameraMatrix::get_viewport_size(float &r_width, float &r_height) const {
- const float * matrix = (const float*)this->matrix;
+ const float *matrix = (const float *)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 float * matrix = (const float*)this->matrix;
+ const float *matrix = (const float *)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 float * matrix = (const float*)this->matrix;
+ const float *matrix = (const float *)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 */
- float pvt_val; /* Value of current pivot element */
- float hold; /* Temporary storage */
- float determinat; /* Determinant */
+ int i, j, k;
+ int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
+ float pvt_val; /* Value of current pivot element */
+ float hold; /* Temporary storage */
+ float 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,142 +470,135 @@ CameraMatrix CameraMatrix::operator*(const CameraMatrix& p_matrix) const {
void CameraMatrix::set_light_bias() {
- float *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;
+ float *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;
}
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;
}
float CameraMatrix::get_aspect() const {
- float w,h;
- get_viewport_size(w,h);
- return w/h;
+ float w, h;
+ get_viewport_size(w, h);
+ return w / h;
}
float CameraMatrix::get_fov() const {
- const float * matrix = (const float*)this->matrix;
+ const float *matrix = (const float *)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 AABB& p_aabb) {
+void CameraMatrix::scale_translate_to_fit(const AABB &p_aabb) {
Vector3 min = p_aabb.pos;
- Vector3 max = p_aabb.pos+p_aabb.size;
+ 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][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][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][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;
+ matrix[0][3] = 0;
+ matrix[1][3] = 0;
+ matrix[2][3] = 0;
+ matrix[3][3] = 1;
}
CameraMatrix::operator Transform() const {
Transform tr;
- const float *m=&matrix[0][0];
+ const float *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;
- float *m=&matrix[0][0];
+ float *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;
+ 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 246a5faba..e989278e4 100644
--- a/core/math/camera_matrix.h
+++ b/core/math/camera_matrix.h
@@ -34,8 +34,6 @@
@author Juan Linietsky <reduzio@gmail.com>
*/
-
-
struct CameraMatrix {
enum Planes {
@@ -47,20 +45,19 @@ struct CameraMatrix {
PLANE_BOTTOM
};
- float matrix[4][4];
-
+ float matrix[4][4];
void set_identity();
void set_zero();
void set_light_bias();
- void set_perspective(float p_fovy_degrees, float p_aspect, float p_z_near, float p_z_far,bool p_flip_fov=false);
- void set_orthogonal(float p_left, float p_right, float p_bottom, float p_top, float p_znear, float p_zfar);
- void set_orthogonal(float p_size, float p_aspect, float p_znear, float p_zfar,bool p_flip_fov=false);
+ void set_perspective(float p_fovy_degrees, float p_aspect, float p_z_near, float p_z_far, bool p_flip_fov = false);
+ void set_orthogonal(float p_left, float p_right, float p_bottom, float p_top, float p_znear, float p_zfar);
+ void set_orthogonal(float p_size, float p_aspect, float p_znear, float p_zfar, bool p_flip_fov = false);
void set_frustum(float p_left, float p_right, float p_bottom, float p_top, float p_near, float p_far);
- static float get_fovy(float p_fovx,float p_aspect) {
+ static float get_fovy(float p_fovx, float 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);
}
float get_z_far() const;
@@ -68,39 +65,38 @@ struct CameraMatrix {
float get_aspect() const;
float 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(float& r_width, float& r_height) const;
+ bool get_endpoints(const Transform &p_transform, Vector3 *p_8points) const;
+ void get_viewport_size(float &r_width, float &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);
- _FORCE_INLINE_ Vector3 xform(const Vector3& p_vec3) const;
+ Plane xform4(const Plane &p_vec4);
+ _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vec3) const;
operator String() const;
- void scale_translate_to_fit(const AABB& p_aabb);
+ void scale_translate_to_fit(const AABB &p_aabb);
void make_scale(const Vector3 &p_scale);
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];
float 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 1024143ba..f41c6c92e 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 {
- float a=Math::random(0,1);
- float b=Math::random(0,1);
- if (a>b) {
- SWAP(a,b);
+ float a = Math::random(0, 1);
+ float 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 AABB& p_aabb) const {
+bool Face3::intersects_aabb(const AABB &p_aabb) const {
/** TEST PLANE **/
- if (!p_aabb.intersects_plane( get_plane() ))
+ if (!p_aabb.intersects_plane(get_plane()))
return false;
- /** TEST FACE AXIS */
+/** TEST FACE AXIS */
-#define TEST_AXIS(m_ax)\
- {\
- float aabb_min=p_aabb.pos.m_ax;\
- float aabb_max=p_aabb.pos.m_ax+p_aabb.size.m_ax;\
- float 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) \
+ { \
+ float aabb_min = p_aabb.pos.m_ax; \
+ float aabb_max = p_aabb.pos.m_ax + p_aabb.size.m_ax; \
+ float 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 AABB& 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();
- float minA,maxA,minB,maxB;
- p_aabb.project_range_in_plane(Plane(axis,0),minA,maxA);
- project_range(axis,Transform(),minB,maxB);
+ float 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,float& r_min, float& r_max) const {
+void Face3::project_range(const Vector3 &p_normal, const Transform &p_transform, float &r_min, float &r_max) const {
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
- Vector3 v=p_transform.xform(vertex[i]);
- float d=p_normal.dot(v);
+ Vector3 v = p_transform.xform(vertex[i]);
+ float 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;
float support_max;
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
- float d=n.dot(vertex[i]);
+ float 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
- float dot=(vertex[i]-vertex[(i+1)%3]).normalized().dot(n);
- dot=ABS(dot);
+ // check if edge is valid as a support
+ float 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 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;
- Vector3 edge0 = vertex[1] - vertex[0];
- Vector3 edge1 = vertex[2] - vertex[0];
- Vector3 v0 = vertex[0] - p_point;
+ float a = edge0.dot(edge0);
+ float b = edge0.dot(edge1);
+ float c = edge1.dot(edge1);
+ float d = edge0.dot(v0);
+ float e = edge1.dot(v0);
- float a = edge0.dot( edge0 );
- float b = edge0.dot( edge1 );
- float c = edge1.dot( edge1 );
- float d = edge0.dot( v0 );
- float e = edge1.dot( v0 );
+ float det = a * c - b * b;
+ float s = b * e - c * d;
+ float t = b * d - a * e;
- float det = a*c - b*b;
- float s = b*e - c*d;
- float 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 );
+ 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
- {
- float invDet = 1.f / det;
- s *= invDet;
- t *= invDet;
- }
- }
- else
- {
- if ( s < 0.f )
- {
- float tmp0 = b+d;
- float tmp1 = c+e;
- if ( tmp1 > tmp0 )
- {
- float numer = tmp1 - tmp0;
- float 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 )
- {
- float numer = c+e-b-d;
- float 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 {
+ float invDet = 1.f / det;
+ s *= invDet;
+ t *= invDet;
}
- else
- {
- float numer = c+e-b-d;
- float denom = a-2*b+c;
- s = CLAMP( numer/denom, 0.f, 1.f );
- t = 1.f - s;
+ } else {
+ if (s < 0.f) {
+ float tmp0 = b + d;
+ float tmp1 = c + e;
+ if (tmp1 > tmp0) {
+ float numer = tmp1 - tmp0;
+ float 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) {
+ float numer = c + e - b - d;
+ float 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 {
+ float numer = c + e - b - d;
+ float 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 4eade1217..3346fcca6 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 "aabb.h"
+#include "plane.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,float& r_min, float& 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, float &r_min, float &r_max) const;
- AABB get_aabb() const {
+ AABB get_aabb() const {
- AABB aabb( vertex[0], Vector3() );
- aabb.expand_to( vertex[1] );
- aabb.expand_to( vertex[2] );
- return aabb;
- }
+ AABB aabb(vertex[0], Vector3());
+ aabb.expand_to(vertex[1]);
+ aabb.expand_to(vertex[2]);
+ return aabb;
+ }
- bool intersects_aabb(const AABB& p_aabb) const;
- _FORCE_INLINE_ bool intersects_aabb2(const AABB& p_aabb) const;
+ bool intersects_aabb(const AABB &p_aabb) const;
+ _FORCE_INLINE_ bool intersects_aabb2(const AABB &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 AABB &p_aabb) const {
-bool Face3::intersects_aabb2(const AABB& 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);
float d = perp.dot(vertex[0]);
- float dist_a = perp.dot(ofs+sup)-d;
- float dist_b = perp.dot(ofs-sup)-d;
+ float dist_a = perp.dot(ofs + sup) - d;
+ float 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)\
- {\
- float aabb_min=p_aabb.pos.m_ax;\
- float aabb_max=p_aabb.pos.m_ax+p_aabb.size.m_ax;\
- float 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) \
+ { \
+ float aabb_min = p_aabb.pos.m_ax; \
+ float aabb_max = p_aabb.pos.m_ax + p_aabb.size.m_ax; \
+ float 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 AABB& 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);
- float maxB = axis.dot(ofs+sup2);
- float minB = axis.dot(ofs-sup2);
- if (minB>maxB) {
- SWAP(maxB,minB);
+ float maxB = axis.dot(ofs + sup2);
+ float minB = axis.dot(ofs - sup2);
+ if (minB > maxB) {
+ SWAP(maxB, minB);
}
- float minT=1e20,maxT=-1e20;
- for (int k=0;k<3;k++) {
+ float minT = 1e20, maxT = -1e20;
+ for (int k = 0; k < 3; k++) {
- float d=axis.dot(vertex[k]);
+ float 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 91f7cf179..7fbd1922b 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;
}
+DVector<DVector<Face3> > Geometry::separate_objects(DVector<Face3> p_array) {
-DVector< DVector< Face3 > > Geometry::separate_objects( DVector< Face3 > p_array ) {
-
- DVector< DVector< Face3 > > objects;
+ DVector<DVector<Face3> > objects;
int len = p_array.size();
- DVector<Face3>::Read r=p_array.read();
+ DVector<Face3>::Read r = p_array.read();
- const Face3* arrayptr = r.ptr();
+ const Face3 *arrayptr = r.ptr();
- DVector< _FaceClassify> fc;
+ DVector<_FaceClassify> fc;
- fc.resize( len );
+ fc.resize(len);
- DVector< _FaceClassify >::Write fcw=fc.write();
+ DVector<_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, DVector< DVector< Face3 > >() ); // invalid geometry
+ ERR_FAIL_COND_V(error, DVector<DVector<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);
- DVector< DVector<Face3> >::Write obw=objects.write();
- DVector< Face3 > *group_faces = obw.ptr();
+ DVector<DVector<Face3> >::Write obw = objects.write();
+ DVector<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) {
- AABB aabb( Vector3(x,y,z),Vector3(len_x,len_y,len_z));
- aabb.pos=aabb.pos*voxelsize;
- aabb.size=aabb.size*voxelsize;
+ AABB 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;
-
- 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; \
+#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,DVector<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, DVector<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]));
}
-
}
-DVector< Face3 > Geometry::wrap_geometry( DVector< Face3 > p_array,float *p_error ) {
+DVector<Face3> Geometry::wrap_geometry(DVector<Face3> p_array, float *p_error) {
#define _MIN_SIZE 1.0
#define _MAX_LENGTH 20
- int face_count=p_array.size();
- DVector<Face3>::Read facesr=p_array.read();
+ int face_count = p_array.size();
+ DVector<Face3>::Read facesr = p_array.read();
const Face3 *faces = facesr.ptr();
AABB 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 @@ DVector< Face3 > Geometry::wrap_geometry( DVector< Face3 > p_array,float *p_erro
// 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 @@ DVector< Face3 > Geometry::wrap_geometry( DVector< Face3 > p_array,float *p_erro
DVector<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 @@ DVector< Face3 > Geometry::wrap_geometry( DVector< Face3 > p_array,float *p_erro
// transform face vertices to global coords
- int wrapped_faces_count=wrapped_faces.size();
- DVector<Face3>::Write wrapped_facesw=wrapped_faces.write();
- Face3* wrapped_faces_ptr=wrapped_facesw.ptr();
+ int wrapped_faces_count = wrapped_faces.size();
+ DVector<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 DVector<Plane> &p_planes) {
MeshData mesh;
-
#define SUBPLANE_SIZE 1024.0
float 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 DVector<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;
}
-
-DVector<Plane> Geometry::build_box_planes(const Vector3& p_extents) {
+DVector<Plane> Geometry::build_box_planes(const Vector3 &p_extents) {
DVector<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 @@ DVector<Plane> Geometry::build_cylinder_planes(float p_radius, float p_height, i
DVector<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;
-
}
-DVector<Plane> Geometry::build_sphere_planes(float p_radius, int p_lats,int p_lons, Vector3::Axis p_axis) {
-
+DVector<Plane> Geometry::build_sphere_planes(float p_radius, int p_lats, int p_lons, Vector3::Axis p_axis) {
DVector<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/(float)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 / (float)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;
-
}
DVector<Plane> Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
DVector<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/(float)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 / (float)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;
- float best_aspect=1e20;
+ int best = -1;
+ float best_aspect = 1e20;
- for(int i=0;i<results.size();i++) {
+ for (int i = 0; i < results.size(); i++) {
float h = nearest_power_of_2(results[i].max_h);
float w = nearest_power_of_2(results[i].max_w);
- float aspect = h>w ? h/w : w/h;
+ float 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 c8398d063..053802085 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 float get_closest_points_between_segments( const Vector2& p1,const Vector2& q1, const Vector2& p2,const Vector2& q2, Vector2& c1, Vector2& c2) {
+public:
+ static float 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:
float a = d1.dot(d1); // Squared length of segment S1, always nonnegative
float e = d2.dot(d2); // Squared length of segment S2, always nonnegative
float f = d2.dot(r);
- float s,t;
+ float 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
float b = d1.dot(d2);
- float denom = a*e-b*b; // Always nonnegative
+ float 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.0f) {
- s = CLAMP((b*f - c*e) / denom, 0.0f, 1.0f);
+ s = CLAMP((b * f - c * e) / denom, 0.0f, 1.0f);
} else
s = 0.0f;
// 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,111 +103,106 @@ 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)
- {
- //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) )
+ static void get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2, Vector3 &c1, Vector3 &c2) {
+//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))
//caluclate the parpametric position on the 2 curves, mua and mub
- float 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) );
- float mub = ( d_of(p1,q1,q2,q1) + mua * d_of(q2,q1,p2,p1) ) / d_of(q2,q1,q2,q1);
+ float 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));
+ float mub = (d_of(p1, q1, q2, q1) + mua * d_of(q2, q1, p2, p1)) / d_of(q2, q1, q2, q1);
//clip the value between [0..1] constraining the solution to lie on the original curves
if (mua < 0) mua = 0;
if (mub < 0) mub = 0;
if (mua > 1) mua = 1;
if (mub > 1) mub = 1;
- c1 = p1.linear_interpolate(p2,mua);
- c2 = q1.linear_interpolate(q2,mub);
+ c1 = p1.linear_interpolate(p2, mua);
+ c2 = q1.linear_interpolate(q2, mub);
}
- static float 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
+ static float 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;
+ // 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;
- }
- }
- 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);
@@ -221,34 +213,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);
@@ -259,124 +251,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);
- float rel_l=rel.length();
- if (rel_l<CMP_EPSILON)
+ Vector3 sphere_pos = p_sphere_pos - p_from;
+ Vector3 rel = (p_to - p_from);
+ float 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;
- float sphere_d=normal.dot(sphere_pos);
+ float sphere_d = normal.dot(sphere_pos);
//Vector3 ray_closest=normal*sphere_d;
- float ray_distance=sphere_pos.distance_to(normal*sphere_d);
+ float ray_distance = sphere_pos.distance_to(normal * sphere_d);
- if (ray_distance>=p_sphere_radius)
+ if (ray_distance >= p_sphere_radius)
return false;
- float inters_d2=p_sphere_radius*p_sphere_radius - ray_distance*ray_distance;
- float inters_d=sphere_d;
+ float inters_d2 = p_sphere_radius * p_sphere_radius - ray_distance * ray_distance;
+ float 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, float p_height,float p_radius,Vector3* r_res=0,Vector3 *r_norm=0) {
+ static inline bool segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius, Vector3 *r_res = 0, Vector3 *r_norm = 0) {
- Vector3 rel=(p_to-p_from);
- float rel_l=rel.length();
- if (rel_l<CMP_EPSILON)
+ Vector3 rel = (p_to - p_from);
+ float 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));
- float crs_l=crs.length();
+ Vector3 normal = (rel / rel_l);
+ Vector3 crs = normal.cross(Vector3(0, 0, 1));
+ float 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;
}
- float dist=z_dir.dot(p_from);
+ float dist = z_dir.dot(p_from);
- if (dist>=p_radius)
+ if (dist >= p_radius)
return false; // too far away
// convert to 2D
- float w2=p_radius*p_radius-dist*dist;
- if (w2<CMP_EPSILON)
+ float 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();
- float 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;
+ float 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;
@@ -384,254 +374,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];
- float l =n.length();
- if (l<1e-10)
+ Vector3 p = p_point - p_segment[0];
+ Vector3 n = p_segment[1] - p_segment[0];
+ float l = n.length();
+ if (l < 1e-10)
return p_segment[0]; // both points are the same, just give any
- n/=l;
+ n /= l;
- float d=n.dot(p);
+ float 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];
- float l =n.length();
- if (l<1e-10)
+ Vector3 p = p_point - p_segment[0];
+ Vector3 n = p_segment[1] - p_segment[0];
+ float l = n.length();
+ if (l < 1e-10)
return p_segment[0]; // both points are the same, just give any
- n/=l;
+ n /= l;
- float d=n.dot(p);
+ float 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];
- float l =n.length();
- if (l<1e-10)
+ Vector2 p = p_point - p_segment[0];
+ Vector2 n = p_segment[1] - p_segment[0];
+ float l = n.length();
+ if (l < 1e-10)
return p_segment[0]; // both points are the same, just give any
- n/=l;
+ n /= l;
- float d=n.dot(p);
+ float 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];
- float l =n.length();
- if (l<1e-10)
+ Vector2 p = p_point - p_segment[0];
+ Vector2 n = p_segment[1] - p_segment[0];
+ float l = n.length();
+ if (l < 1e-10)
return p_segment[0]; // both points are the same, just give any
- n/=l;
+ n /= l;
- float d=n.dot(p);
+ float 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;
- float ABpos=D.x+(C.x-D.x)*D.y/(D.y-C.y);
+ float 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) {
- float d=p_normal.dot(p_sphere_pos)-p_normal.dot(p_triangle[0]);
+ float 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
- float ad=axis.dot(n2);
+ float 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;
}
@@ -641,34 +622,34 @@ public:
float 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;
}
- float r2=p_sphere_radius*p_sphere_radius;
+ float 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;
}
@@ -679,56 +660,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) {
+ /* create a quadratic formula of the form ax^2 + bx + c = 0 */
+ real_t a, b, c;
- Vector2 line_vec = p_to - p_from;
- Vector2 vec_to_line = p_from - p_circle_pos;
+ 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;
- /* create a quadratic formula of the form ax^2 + bx + c = 0 */
- real_t a, b, c;
+ /* solve for t */
+ real_t sqrtterm = b * b - 4 * a * 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;
+ /* 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;
- /* 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 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++) {
//float p_plane.d = (*this) * polygon[a];
float 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 {
@@ -750,7 +726,7 @@ public:
return Vector<Vector3>(); //empty
}
-// long count = 0;
+ // long count = 0;
long previous = polygon.size() - 1;
Vector<Vector3> clipped;
@@ -759,24 +735,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];
+ const Vector3 &v1 = polygon[previous];
+ const Vector3 &v2 = polygon[index];
- Vector3 segment= v1 - v2;
- double den=p_plane.normal.dot( segment );
- double dist=p_plane.distance_to( v1 ) / den;
- dist=-dist;
- clipped.push_back( v1 + segment * dist );
+ Vector3 segment = v1 - v2;
+ double den = p_plane.normal.dot(segment);
+ double 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;
- double den=p_plane.normal.dot( segment );
- double dist=p_plane.distance_to( v1 ) / den;
- dist=-dist;
- clipped.push_back( v1 + segment * dist );
+ const Vector3 &v2 = polygon[previous];
+ Vector3 segment = v1 - v2;
+ double den = p_plane.normal.dot(segment);
+ double dist = p_plane.distance_to(v1) / den;
+ dist = -dist;
+ clipped.push_back(v1 + segment * dist);
}
clipped.push_back(v1);
@@ -788,30 +764,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 DVector<DVector<Face3> > separate_objects(DVector<Face3> p_array);
- static DVector< DVector< Face3 > > separate_objects( DVector< Face3 > p_array );
-
- static DVector< Face3 > wrap_geometry( DVector< Face3 > p_array, float *p_error=NULL ); ///< create a "wrap" that encloses the given geometry
-
+ static DVector<Face3> wrap_geometry(DVector<Face3> p_array, float *p_error = NULL); ///< create a "wrap" that encloses the given geometry
struct MeshData {
@@ -824,94 +796,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 double vec2_cross(const Point2 &O, const Point2 &A, const Point2 &B)
- {
+ static double vec2_cross(const Point2 &O, const Point2 &A, const Point2 &B) {
return (double)(A.x - O.x) * (B.y - O.y) - (double)(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];
}
@@ -920,16 +887,12 @@ public:
}
static MeshData build_convex_mesh(const DVector<Plane> &p_planes);
- static DVector<Plane> build_sphere_planes(float p_radius, int p_lats, int p_lons, Vector3::Axis p_axis=Vector3::AXIS_Z);
- static DVector<Plane> build_box_planes(const Vector3& p_extents);
- static DVector<Plane> build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis=Vector3::AXIS_Z);
- static DVector<Plane> build_capsule_planes(float p_radius, float 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 DVector<Plane> build_sphere_planes(float p_radius, int p_lats, int p_lons, Vector3::Axis p_axis = Vector3::AXIS_Z);
+ static DVector<Plane> build_box_planes(const Vector3 &p_extents);
+ static DVector<Plane> build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z);
+ static DVector<Plane> build_capsule_planes(float p_radius, float 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 2ced18e42..b2b99c8e5 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(x,y);
+ return Math::atan2(x, y);
}
float Vector2::length() const {
- return Math::sqrt( x*x + y*y );
+ return Math::sqrt(x * x + y * y);
}
float Vector2::length_squared() const {
- return x*x + y*y;
+ return x * x + y * y;
}
void Vector2::normalize() {
- float l = x*x + y*y;
- if (l!=0) {
+ float 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;
}
-float Vector2::distance_to(const Vector2& p_vector2) const {
+float 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));
}
-float Vector2::distance_squared_to(const Vector2& p_vector2) const {
+float 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);
}
-float Vector2::angle_to(const Vector2& p_vector2) const {
+float Vector2::angle_to(const Vector2 &p_vector2) const {
- return Math::atan2( tangent().dot(p_vector2), dot(p_vector2) );
+ return Math::atan2(tangent().dot(p_vector2), dot(p_vector2));
}
-float Vector2::angle_to_point(const Vector2& p_vector2) const {
+float Vector2::angle_to_point(const Vector2 &p_vector2) const {
- return Math::atan2( x-p_vector2.x, y - p_vector2.y );
+ return Math::atan2(x - p_vector2.x, y - p_vector2.y);
}
-float Vector2::dot(const Vector2& p_other) const {
+float 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;
}
-float Vector2::cross(const Vector2& p_other) const {
+float 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 float &rvalue) const {
};
void Vector2::operator*=(const float &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 float &rvalue) const {
void Vector2::operator/=(const float &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(float 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,float p_t) const {
+Vector2 Vector2::cubic_interpolate_soft(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, float 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,float p_t) const {
-
-
+Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, float 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;
float t = p_t;
float t2 = t * t;
float t3 = t2 * t;
Vector2 out;
- out = 0.5f * ( ( p1 * 2.0f) +
- ( -p0 + p2 ) * t +
- ( 2.0f * p0 - 5.0f * p1 + 4 * p2 - p3 ) * t2 +
- ( -p0 + 3.0f * p1 - 3.0f * p2 + p3 ) * t3 );
+ out = 0.5f * ((p1 * 2.0f) +
+ (-p0 + p2) * t +
+ (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
+ (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
return out;
-/*
+ /*
float mu = p_t;
float 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;
+ float sign = 0;
- real_t min=0,max=1;
- int axis=0;
- float 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;
float 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,222 +389,212 @@ 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 Matrix32::invert() {
- SWAP(elements[0][1],elements[1][0]);
+ SWAP(elements[0][1], elements[1][0]);
elements[2] = basis_xform(-elements[2]);
}
Matrix32 Matrix32::inverse() const {
- Matrix32 inv=*this;
+ Matrix32 inv = *this;
inv.invert();
return inv;
-
}
void Matrix32::affine_invert() {
float det = basis_determinant();
- ERR_FAIL_COND(det==0);
+ ERR_FAIL_COND(det == 0);
float 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]);
-
}
Matrix32 Matrix32::affine_inverse() const {
- Matrix32 inv=*this;
+ Matrix32 inv = *this;
inv.affine_invert();
return inv;
}
void Matrix32::rotate(real_t p_phi) {
- Matrix32 rot(p_phi,Vector2());
+ Matrix32 rot(p_phi, Vector2());
*this *= rot;
}
real_t Matrix32::get_rotation() const {
- return Math::atan2(elements[1].x,elements[1].y);
+ return Math::atan2(elements[1].x, elements[1].y);
}
void Matrix32::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[1][1]=cr;
- elements[0][1]=-sr;
- elements[1][0]=sr;
+ elements[0][0] = cr;
+ elements[1][1] = cr;
+ elements[0][1] = -sr;
+ elements[1][0] = sr;
}
-Matrix32::Matrix32(real_t p_rot, const Vector2& p_pos) {
+Matrix32::Matrix32(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[1][1]=cr;
- elements[0][1]=-sr;
- elements[1][0]=sr;
- elements[2]=p_pos;
+ elements[0][0] = cr;
+ elements[1][1] = cr;
+ elements[0][1] = -sr;
+ elements[1][0] = sr;
+ elements[2] = p_pos;
}
Size2 Matrix32::get_scale() const {
- return Size2( elements[0].length(), elements[1].length() );
+ return Size2(elements[0].length(), elements[1].length());
}
-void Matrix32::scale(const Size2& p_scale) {
+void Matrix32::scale(const Size2 &p_scale) {
- elements[0]*=p_scale;
- elements[1]*=p_scale;
- elements[2]*=p_scale;
+ elements[0] *= p_scale;
+ elements[1] *= p_scale;
+ elements[2] *= p_scale;
}
-void Matrix32::scale_basis(const Size2& p_scale) {
-
- elements[0]*=p_scale;
- elements[1]*=p_scale;
+void Matrix32::scale_basis(const Size2 &p_scale) {
+ elements[0] *= p_scale;
+ elements[1] *= p_scale;
}
-void Matrix32::translate( real_t p_tx, real_t p_ty) {
+void Matrix32::translate(real_t p_tx, real_t p_ty) {
- translate(Vector2(p_tx,p_ty));
+ translate(Vector2(p_tx, p_ty));
}
-void Matrix32::translate( const Vector2& p_translation ) {
+void Matrix32::translate(const Vector2 &p_translation) {
- elements[2]+=basis_xform(p_translation);
+ elements[2] += basis_xform(p_translation);
}
void Matrix32::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;
}
Matrix32 Matrix32::orthonormalized() const {
- Matrix32 on=*this;
+ Matrix32 on = *this;
on.orthonormalize();
return on;
-
}
-bool Matrix32::operator==(const Matrix32& p_transform) const {
+bool Matrix32::operator==(const Matrix32 &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 Matrix32::operator!=(const Matrix32& p_transform) const {
+bool Matrix32::operator!=(const Matrix32 &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 Matrix32::operator*=(const Matrix32& p_transform) {
+void Matrix32::operator*=(const Matrix32 &p_transform) {
elements[2] = xform(p_transform.elements[2]);
- float x0,x1,y0,y1;
+ float 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;
}
-
-Matrix32 Matrix32::operator*(const Matrix32& p_transform) const {
+Matrix32 Matrix32::operator*(const Matrix32 &p_transform) const {
Matrix32 t = *this;
- t*=p_transform;
+ t *= p_transform;
return t;
-
}
-Matrix32 Matrix32::scaled(const Size2& p_scale) const {
+Matrix32 Matrix32::scaled(const Size2 &p_scale) const {
- Matrix32 copy=*this;
+ Matrix32 copy = *this;
copy.scale(p_scale);
return copy;
-
}
-Matrix32 Matrix32::basis_scaled(const Size2& p_scale) const {
+Matrix32 Matrix32::basis_scaled(const Size2 &p_scale) const {
- Matrix32 copy=*this;
+ Matrix32 copy = *this;
copy.scale_basis(p_scale);
return copy;
-
}
Matrix32 Matrix32::untranslated() const {
- Matrix32 copy=*this;
- copy.elements[2]=Vector2();
+ Matrix32 copy = *this;
+ copy.elements[2] = Vector2();
return copy;
}
-Matrix32 Matrix32::translated(const Vector2& p_offset) const {
+Matrix32 Matrix32::translated(const Vector2 &p_offset) const {
- Matrix32 copy=*this;
+ Matrix32 copy = *this;
copy.translate(p_offset);
return copy;
-
}
Matrix32 Matrix32::rotated(float p_phi) const {
- Matrix32 copy=*this;
+ Matrix32 copy = *this;
copy.rotate(p_phi);
return copy;
-
}
float Matrix32::basis_determinant() const {
@@ -614,7 +602,7 @@ float Matrix32::basis_determinant() const {
return elements[0].x * elements[1].y - elements[0].y * elements[1].x;
}
-Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) const {
+Matrix32 Matrix32::interpolate_with(const Matrix32 &p_transform, float p_c) const {
//extract parameters
Vector2 p1 = get_origin();
@@ -639,9 +627,9 @@ Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) cons
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
@@ -652,5 +640,5 @@ Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) cons
Matrix32::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 2ec0dc39c..35e9274ac 100644
--- a/core/math/math_2d.h
+++ b/core/math/math_2d.h
@@ -73,12 +73,11 @@ struct Vector2 {
float height;
};
-
- _FORCE_INLINE_ float& operator[](int p_idx) {
- return p_idx?y:x;
+ _FORCE_INLINE_ float &operator[](int p_idx) {
+ return p_idx ? y : x;
}
- _FORCE_INLINE_ const float& operator[](int p_idx) const {
- return p_idx?y:x;
+ _FORCE_INLINE_ const float &operator[](int p_idx) const {
+ return p_idx ? y : x;
}
void normalize();
@@ -87,32 +86,32 @@ struct Vector2 {
float length() const;
float length_squared() const;
- float distance_to(const Vector2& p_vector2) const;
- float distance_squared_to(const Vector2& p_vector2) const;
- float angle_to(const Vector2& p_vector2) const;
- float angle_to_point(const Vector2& p_vector2) const;
+ float distance_to(const Vector2 &p_vector2) const;
+ float distance_squared_to(const Vector2 &p_vector2) const;
+ float angle_to(const Vector2 &p_vector2) const;
+ float angle_to_point(const Vector2 &p_vector2) const;
- float dot(const Vector2& p_other) const;
- float cross(const Vector2& p_other) const;
+ float dot(const Vector2 &p_other) const;
+ float 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,float p_t);
- _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2& p_b,float p_t) const;
- Vector2 cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,float p_t) const;
- Vector2 cubic_interpolate_soft(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,float p_t) const;
+ _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, float p_t);
+ _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_b, float p_t) const;
+ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, float p_t) const;
+ Vector2 cubic_interpolate_soft(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, float 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 float &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(float p_radians) {
- x=Math::sin(p_radians);
- y=Math::cos(p_radians);
+ x = Math::sin(p_radians);
+ y = Math::cos(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(float p_by) const;
Vector2 tangent() const {
- return Vector2(y,-x);
+ return Vector2(y, -x);
}
Vector2 floor() const;
- Vector2 snapped(const Vector2& p_by) const;
- float get_aspect() const { return width/height; }
-
+ Vector2 snapped(const Vector2 &p_by) const;
+ float get_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(float p_x,float p_y) { x=p_x; y=p_y; }
- _FORCE_INLINE_ Vector2() { x=0; y=0; }
+ _FORCE_INLINE_ Vector2(float p_x, float 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*(float p_scalar, const Vector2 &p_vec) {
-_FORCE_INLINE_ Vector2 operator*(float p_scalar, const Vector2& p_vec) {
-
- return p_vec*p_scalar;
+ return p_vec * p_scalar;
}
-Vector2 Vector2::linear_interpolate(const Vector2& p_b,float p_t) const {
+Vector2 Vector2::linear_interpolate(const Vector2 &p_b, float 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,float p_t) {
+Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, float 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 Matrix32;
-
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; }
- float get_area() const { return size.width*size.height; }
+ float 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 float distance_to(const Vector2& p_point) const {
+ inline float distance_to(const Vector2 &p_point) const {
float 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 Matrix32& 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 Matrix32 &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( float p_x, float p_y, float p_width, float 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(float p_x, float p_y, float p_width, float 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;
- float get_aspect() const { return width/(float)height; }
+ float get_aspect() const { return width / (float)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,145 +434,154 @@ 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 Matrix32 {
Vector2 elements[3];
- _FORCE_INLINE_ float tdotx(const Vector2& v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
- _FORCE_INLINE_ float tdoty(const Vector2& v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
+ _FORCE_INLINE_ float tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
+ _FORCE_INLINE_ float 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();
Matrix32 inverse() const;
@@ -572,24 +591,24 @@ struct Matrix32 {
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);
float 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; }
- Matrix32 scaled(const Size2& p_scale) const;
- Matrix32 basis_scaled(const Size2& p_scale) const;
- Matrix32 translated(const Vector2& p_offset) const;
+ Matrix32 scaled(const Size2 &p_scale) const;
+ Matrix32 basis_scaled(const Size2 &p_scale) const;
+ Matrix32 translated(const Vector2 &p_offset) const;
Matrix32 rotated(float p_phi) const;
Matrix32 untranslated() const;
@@ -597,20 +616,20 @@ struct Matrix32 {
void orthonormalize();
Matrix32 orthonormalized() const;
- bool operator==(const Matrix32& p_transform) const;
- bool operator!=(const Matrix32& p_transform) const;
+ bool operator==(const Matrix32 &p_transform) const;
+ bool operator!=(const Matrix32 &p_transform) const;
- void operator*=(const Matrix32& p_transform);
- Matrix32 operator*(const Matrix32& p_transform) const;
+ void operator*=(const Matrix32 &p_transform);
+ Matrix32 operator*(const Matrix32 &p_transform) const;
- Matrix32 interpolate_with(const Matrix32& p_transform, float p_c) const;
+ Matrix32 interpolate_with(const Matrix32 &p_transform, float 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;
@@ -624,232 +643,226 @@ struct Matrix32 {
elements[2][1] = oy;
}
- Matrix32(real_t p_rot, const Vector2& p_pos);
- Matrix32() { elements[0][0]=1.0; elements[1][1]=1.0; }
+ Matrix32(real_t p_rot, const Vector2 &p_pos);
+ Matrix32() {
+ elements[0][0] = 1.0;
+ elements[1][1] = 1.0;
+ }
};
-bool Rect2::intersects_transformed(const Matrix32& p_xform, const Rect2& p_rect) const {
+bool Rect2::intersects_transformed(const Matrix32 &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 Matrix32::basis_xform(const Vector2& v) const {
+Vector2 Matrix32::basis_xform(const Vector2 &v) const {
return Vector2(
- tdotx(v),
- tdoty(v)
- );
+ tdotx(v),
+ tdoty(v));
}
-Vector2 Matrix32::basis_xform_inv(const Vector2& v) const{
+Vector2 Matrix32::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 Matrix32::xform(const Vector2& v) const {
+Vector2 Matrix32::xform(const Vector2 &v) const {
return Vector2(
- tdotx(v),
- tdoty(v)
- ) + elements[2];
+ tdotx(v),
+ tdoty(v)) +
+ elements[2];
}
-Vector2 Matrix32::xform_inv(const Vector2& p_vec) const {
+Vector2 Matrix32::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 Matrix32::xform(const Rect2& p_rect) const {
+Rect2 Matrix32::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 Matrix32::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[0][1]=-Math::sin(p_rot)*p_scale.x;
- elements[1][0]=Math::sin(p_rot)*p_scale.y;
+void Matrix32::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[0][1] = -Math::sin(p_rot) * p_scale.x;
+ elements[1][0] = Math::sin(p_rot) * p_scale.y;
}
-Rect2 Matrix32::xform_inv(const Rect2& p_rect) const {
+Rect2 Matrix32::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]);
@@ -857,5 +870,4 @@ Rect2 Matrix32::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 feaff38a4..08f4e27e6 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 38f45e331..a8a063384 100644
--- a/core/math/math_funcs.cpp
+++ b/core/math/math_funcs.cpp
@@ -29,10 +29,9 @@
#include "math_funcs.h"
#include "core/os/os.h"
-#include <math.h>
#include "float.h"
-uint32_t Math::default_seed=1;
-
+#include <math.h>
+uint32_t Math::default_seed = 1;
#define PHI 0x9e3779b9
@@ -49,8 +48,8 @@ uint32_t Math::rand_from_seed(uint32_t *seed) {
s = 0x12345987;
k = s / 127773;
s = 16807 * (s - k * 127773) - 2836 * k;
-// if (s < 0)
-// s += 2147483647;
+ // if (s < 0)
+ // s += 2147483647;
(*seed) = s;
return (s & Math::RANDOM_MAX);
#else
@@ -70,19 +69,19 @@ void Math::seed(uint32_t x) {
for (i = 3; i < 4096; i++)
Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
#else
- default_seed=x;
+ default_seed = x;
#endif
}
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()); /* *OS::get_singleton()->get_time().sec); // windows doesn't have get_time(), returns always 0 */
+ seed(OS::get_singleton()->get_ticks_usec() * (time.hour + 1) * (time.min + 1) * (time.sec + 1) * rand()); /* *OS::get_singleton()->get_time().sec); // windows doesn't have get_time(), returns always 0 */
}
uint32_t Math::rand() {
- return rand_from_seed(&default_seed)&0x7FFFFFFF;
+ return rand_from_seed(&default_seed) & 0x7FFFFFFF;
}
double Math::randf() {
@@ -93,19 +92,16 @@ double Math::randf() {
double Math::sin(double p_x) {
return ::sin(p_x);
-
}
double Math::cos(double p_x) {
return ::cos(p_x);
-
}
double Math::tan(double p_x) {
return ::tan(p_x);
-
}
double Math::sinh(double p_x) {
@@ -122,31 +118,29 @@ double Math::tanh(double p_x) {
return ::tanh(p_x);
}
-
double Math::deg2rad(double p_y) {
- return p_y*Math_PI/180.0;
+ return p_y * Math_PI / 180.0;
}
double Math::rad2deg(double p_y) {
- return p_y*180.0/Math_PI;
+ return p_y * 180.0 / Math_PI;
}
double Math::round(double p_val) {
- if (p_val>=0) {
- return ::floor(p_val+0.5);
+ if (p_val >= 0) {
+ return ::floor(p_val + 0.5);
} else {
- p_val=-p_val;
- return -::floor(p_val+0.5);
+ p_val = -p_val;
+ return -::floor(p_val + 0.5);
}
}
double Math::asin(double p_x) {
return ::asin(p_x);
-
}
double Math::acos(double p_x) {
@@ -159,42 +153,40 @@ double Math::atan(double p_x) {
return ::atan(p_x);
}
-double Math::dectime(double p_value,double p_amount, double p_step) {
+double Math::dectime(double p_value, double p_amount, double p_step) {
float sgn = p_value < 0 ? -1.0 : 1.0;
float val = absf(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::atan2(double p_y, double p_x) {
- return ::atan2(p_y,p_x);
-
+ return ::atan2(p_y, p_x);
}
double Math::sqrt(double p_x) {
return ::sqrt(p_x);
}
-double Math::fmod(double p_x,double p_y) {
+double Math::fmod(double p_x, double p_y) {
- return ::fmod(p_x,p_y);
+ return ::fmod(p_x, p_y);
}
-double Math::fposmod(double p_x,double p_y) {
+double Math::fposmod(double p_x, double p_y) {
- if (p_x>=0) {
+ if (p_x >= 0) {
- return Math::fmod(p_x,p_y);
+ return Math::fmod(p_x, p_y);
} else {
- return p_y-Math::fmod(-p_x,p_y);
+ return p_y - Math::fmod(-p_x, p_y);
}
-
}
double Math::floor(double p_x) {
@@ -208,8 +200,8 @@ double Math::ceil(double p_x) {
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,
@@ -221,9 +213,9 @@ int Math::step_decimals(double p_step) {
0.000000009999
};
- double as=absf(p_step);
- for(int i=0;i<maxn;i++) {
- if (as>=sd[i]) {
+ double as = absf(p_step);
+ for (int i = 0; i < maxn; i++) {
+ if (as >= sd[i]) {
return i;
}
}
@@ -233,41 +225,40 @@ int Math::step_decimals(double p_step) {
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) {
+double Math::stepify(double p_value, double p_step) {
- if (p_step!=0) {
+ if (p_step != 0) {
- p_value=floor( p_value / p_step + 0.5 ) * p_step;
+ p_value = floor(p_value / p_step + 0.5) * p_step;
}
return p_value;
}
bool Math::is_nan(double p_val) {
- return (p_val!=p_val);
+ return (p_val != p_val);
}
bool Math::is_inf(double p_val) {
@@ -277,7 +268,6 @@ bool Math::is_inf(double p_val) {
#else
return isinf(p_val);
#endif
-
}
uint32_t Math::larger_prime(uint32_t p_val) {
@@ -315,11 +305,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++;
}
@@ -330,13 +320,13 @@ 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;
}
double Math::pow(double x, double y) {
- return ::pow(x,y);
+ return ::pow(x, y);
}
double Math::log(double x) {
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h
index a5195e554..294b81500 100644
--- a/core/math/math_funcs.h
+++ b/core/math/math_funcs.h
@@ -29,8 +29,8 @@
#ifndef MATH_FUNCS_H
#define MATH_FUNCS_H
-#include "typedefs.h"
#include "math_defs.h"
+#include "typedefs.h"
#ifndef NO_MATH_H
#include "math.h"
@@ -38,13 +38,13 @@
class Math {
-
static uint32_t default_seed;
+
public:
- Math() {}; // useless to instance
+ Math(){}; // useless to instance
enum {
- RANDOM_MAX=2147483647L
+ RANDOM_MAX = 2147483647L
};
static double sin(double p_x);
@@ -60,35 +60,32 @@ public:
static double deg2rad(double p_y);
static double rad2deg(double p_y);
static double sqrt(double p_x);
- static double fmod(double p_x,double p_y);
- static double fposmod(double p_x,double p_y);
+ static double fmod(double p_x, double p_y);
+ static double fposmod(double p_x, double p_y);
static uint32_t rand_from_seed(uint32_t *seed);
static double floor(double p_x);
static double ceil(double p_x);
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 void seed(uint32_t x=0);
+ static double stepify(double p_value, double p_step);
+ static void seed(uint32_t x = 0);
static void randomize();
static uint32_t larger_prime(uint32_t p_val);
- static double dectime(double p_value,double p_amount, double p_step);
-
+ static double dectime(double p_value, double p_amount, double p_step);
static inline double linear2db(double p_linear) {
- return Math::log( p_linear ) * 8.6858896380650365530225783783321;
+ return Math::log(p_linear) * 8.6858896380650365530225783783321;
}
static inline double db2linear(double p_db) {
- return Math::exp( p_db * 0.11512925464970228420089957273422 );
+ return Math::exp(p_db * 0.11512925464970228420089957273422);
}
static bool is_nan(double p_val);
static bool is_inf(double p_val);
-
-
static uint32_t rand();
static double randf();
@@ -96,7 +93,6 @@ public:
static double random(double from, double to);
-
static _FORCE_INLINE_ real_t abs(real_t g) {
#ifdef REAL_T_IS_DOUBLE
@@ -115,8 +111,8 @@ public:
uint32_t i;
} u;
- u.f=g;
- u.i&=2147483647u;
+ u.f = g;
+ u.i &= 2147483647u;
return u.f;
}
@@ -126,8 +122,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;
}
@@ -137,11 +133,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.0f) ? (a + 0.5f):(a -0.5f));
+ b = (int)((a > 0.0f) ? (a + 0.5f) : (a - 0.5f));
#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)
@@ -152,12 +147,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 _FORCE_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
@@ -168,16 +162,14 @@ public:
static _FORCE_INLINE_ float lerp(float a, float b, float c) {
- return a+(b-a)*c;
+ return a + (b - a) * c;
}
static double pow(double x, double y);
static double log(double x);
static double exp(double x);
-
};
-
#define Math_PI 3.14159265358979323846
#define Math_SQRT12 0.7071067811865475244008443621048490
diff --git a/core/math/matrix3.cpp b/core/math/matrix3.cpp
index c30401cc2..243e5fdf6 100644
--- a/core/math/matrix3.cpp
+++ b/core/math/matrix3.cpp
@@ -30,66 +30,63 @@
#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 Matrix3::from_z(const Vector3& p_z) {
+void Matrix3::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 Matrix3::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 Matrix3::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);
}
Matrix3 Matrix3::orthonormalized() const {
@@ -99,42 +96,41 @@ Matrix3 Matrix3::orthonormalized() const {
return c;
}
-
Matrix3 Matrix3::inverse() const {
- Matrix3 inv=*this;
+ Matrix3 inv = *this;
inv.invert();
return inv;
}
void Matrix3::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]);
}
Matrix3 Matrix3::transposed() const {
- Matrix3 tr=*this;
+ Matrix3 tr = *this;
tr.transpose();
return tr;
}
-void Matrix3::scale(const Vector3& p_scale) {
+void Matrix3::scale(const Vector3 &p_scale) {
- elements[0][0]*=p_scale.x;
- elements[1][0]*=p_scale.x;
- elements[2][0]*=p_scale.x;
- elements[0][1]*=p_scale.y;
- elements[1][1]*=p_scale.y;
- elements[2][1]*=p_scale.y;
- elements[0][2]*=p_scale.z;
- elements[1][2]*=p_scale.z;
- elements[2][2]*=p_scale.z;
+ elements[0][0] *= p_scale.x;
+ elements[1][0] *= p_scale.x;
+ elements[2][0] *= p_scale.x;
+ elements[0][1] *= p_scale.y;
+ elements[1][1] *= p_scale.y;
+ elements[2][1] *= p_scale.y;
+ elements[0][2] *= p_scale.z;
+ elements[1][2] *= p_scale.z;
+ elements[2][2] *= p_scale.z;
}
-Matrix3 Matrix3::scaled( const Vector3& p_scale ) const {
+Matrix3 Matrix3::scaled(const Vector3 &p_scale) const {
Matrix3 m = *this;
m.scale(p_scale);
@@ -144,28 +140,25 @@ Matrix3 Matrix3::scaled( const Vector3& p_scale ) const {
Vector3 Matrix3::get_scale() const {
return 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()
- );
-
+ 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());
}
-void Matrix3::rotate(const Vector3& p_axis, real_t p_phi) {
+void Matrix3::rotate(const Vector3 &p_axis, real_t p_phi) {
*this = *this * Matrix3(p_axis, p_phi);
}
-Matrix3 Matrix3::rotated(const Vector3& p_axis, real_t p_phi) const {
+Matrix3 Matrix3::rotated(const Vector3 &p_axis, real_t p_phi) const {
return *this * Matrix3(p_axis, p_phi);
-
}
Vector3 Matrix3::get_euler() const {
// rot = cy*cz -cy*sz sy
- // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
- // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
+ // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
+ // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
Matrix3 m = *this;
m.orthonormalize();
@@ -173,75 +166,72 @@ Vector3 Matrix3::get_euler() const {
Vector3 euler;
euler.y = Math::asin(m[0][2]);
- if ( euler.y < Math_PI*0.5) {
- if ( euler.y > -Math_PI*0.5) {
- euler.x = Math::atan2(-m[1][2],m[2][2]);
- euler.z = Math::atan2(-m[0][1],m[0][0]);
+ if (euler.y < Math_PI * 0.5) {
+ if (euler.y > -Math_PI * 0.5) {
+ euler.x = Math::atan2(-m[1][2], m[2][2]);
+ euler.z = Math::atan2(-m[0][1], m[0][0]);
} else {
- real_t r = Math::atan2(m[1][0],m[1][1]);
+ real_t r = Math::atan2(m[1][0], m[1][1]);
euler.z = 0.0;
euler.x = euler.z - r;
-
}
} else {
- real_t r = Math::atan2(m[0][1],m[1][1]);
+ real_t r = Math::atan2(m[0][1], m[1][1]);
euler.z = 0;
euler.x = r - euler.z;
}
return euler;
-
-
}
-void Matrix3::set_euler(const Vector3& p_euler) {
+void Matrix3::set_euler(const Vector3 &p_euler) {
real_t c, s;
c = Math::cos(p_euler.x);
s = Math::sin(p_euler.x);
- Matrix3 xmat(1.0,0.0,0.0,0.0,c,-s,0.0,s,c);
+ Matrix3 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);
- Matrix3 ymat(c,0.0,s,0.0,1.0,0.0,-s,0.0,c);
+ Matrix3 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);
- Matrix3 zmat(c,-s,0.0,s,c,0.0,0.0,0.0,1.0);
+ Matrix3 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 Matrix3::operator==(const Matrix3& p_matrix) const {
+bool Matrix3::operator==(const Matrix3 &p_matrix) const {
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- if (elements[i][j]!=p_matrix.elements[i][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;
}
}
return true;
}
-bool Matrix3::operator!=(const Matrix3& p_matrix) const {
+bool Matrix3::operator!=(const Matrix3 &p_matrix) const {
- return (!(*this==p_matrix));
+ return (!(*this == p_matrix));
}
Matrix3::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]);
}
}
@@ -250,27 +240,24 @@ Matrix3::operator String() const {
Matrix3::operator Quat() const {
- Matrix3 m=*this;
+ Matrix3 m = *this;
m.orthonormalize();
real_t trace = m.elements[0][0] + m.elements[1][1] + m.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]=((m.elements[2][1] - m.elements[1][2]) * s);
- temp[1]=((m.elements[0][2] - m.elements[2][0]) * s);
- temp[2]=((m.elements[1][0] - m.elements[0][1]) * s);
- }
- else
- {
+ temp[0] = ((m.elements[2][1] - m.elements[1][2]) * s);
+ temp[1] = ((m.elements[0][2] - m.elements[2][0]) * s);
+ temp[2] = ((m.elements[1][0] - m.elements[0][1]) * s);
+ } else {
int i = m.elements[0][0] < m.elements[1][1] ?
- (m.elements[1][1] < m.elements[2][2] ? 2 : 1) :
- (m.elements[0][0] < m.elements[2][2] ? 2 : 0);
+ (m.elements[1][1] < m.elements[2][2] ? 2 : 1) :
+ (m.elements[0][0] < m.elements[2][2] ? 2 : 0);
int j = (i + 1) % 3;
int k = (i + 2) % 3;
@@ -283,11 +270,10 @@ Matrix3::operator Quat() const {
temp[k] = (m.elements[k][i] + m.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 Matrix3 _ortho_bases[24]={
+static const Matrix3 _ortho_bases[24] = {
Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1),
Matrix3(0, -1, 0, 1, 0, 0, 0, 0, 1),
Matrix3(-1, 0, 0, 0, -1, 0, 0, 0, 1),
@@ -317,163 +303,146 @@ static const Matrix3 _ortho_bases[24]={
int Matrix3::get_orthogonal_index() const {
//could be sped up if i come up with a way
- Matrix3 orth=*this;
- for(int i=0;i<3;i++) {
- for(int j=0;j<3;j++) {
+ Matrix3 orth = *this;
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
float 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 Matrix3::set_orthogonal_index(int p_index){
+void Matrix3::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 Matrix3::get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const {
-void Matrix3::get_axis_and_angle(Vector3 &r_axis,real_t& r_angle) const {
-
+ double angle, x, y, z; // variables for result
+ double epsilon = 0.01; // margin to allow for rounding errors
+ double epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
- double angle,x,y,z; // variables for result
- double epsilon = 0.01; // margin to allow for rounding errors
- double 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;
- double xx = (elements[0][0]+1)/2;
- double yy = (elements[1][1]+1)/2;
- double zz = (elements[2][2]+1)/2;
- double xy = (elements[1][0]+elements[0][1])/4;
- double xz = (elements[2][0]+elements[0][2])/4;
- double yz = (elements[2][1]+elements[1][2])/4;
+ double xx = (elements[0][0] + 1) / 2;
+ double yy = (elements[1][1] + 1) / 2;
+ double zz = (elements[2][2] + 1) / 2;
+ double xy = (elements[1][0] + elements[0][1]) / 4;
+ double xz = (elements[2][0] + elements[0][2]) / 4;
+ double 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
- double 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])); // used to normalise
- if (Math::abs(s) < 0.001) s=1;
- // prevent divide by zero, should not happen if matrix is orthogonal and should be
- // caught by singularity test above, but I've left it in just in case
- angle = Math::acos(( elements[0][0] + elements[1][1] + elements[2][2] - 1)/2);
- x = (elements[1][2] - elements[2][1])/s;
- y = (elements[2][0] - elements[0][2])/s;
- z = (elements[0][1] - elements[1][0])/s;
+ double 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])); // used to normalise
+ if (Math::abs(s) < 0.001) s = 1;
+ // prevent divide by zero, should not happen if matrix is orthogonal and should be
+ // caught by singularity test above, but I've left it in just in case
+ angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2);
+ x = (elements[1][2] - elements[2][1]) / s;
+ y = (elements[2][0] - elements[0][2]) / s;
+ z = (elements[0][1] - elements[1][0]) / s;
- r_axis=Vector3(x,y,z);
- r_angle=angle;
+ r_axis = Vector3(x, y, z);
+ r_angle = angle;
}
-Matrix3::Matrix3(const Vector3& p_euler) {
-
- set_euler( p_euler );
+Matrix3::Matrix3(const Vector3 &p_euler) {
+ set_euler(p_euler);
}
-Matrix3::Matrix3(const Quat& p_quat) {
+Matrix3::Matrix3(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));
}
-Matrix3::Matrix3(const Vector3& p_axis, real_t p_phi) {
+Matrix3::Matrix3(const Vector3 &p_axis, real_t p_phi) {
- Vector3 axis_sq(p_axis.x*p_axis.x,p_axis.y*p_axis.y,p_axis.z*p_axis.z);
+ 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);
+ real_t cosine = Math::cos(p_phi);
+ real_t sine = Math::sin(p_phi);
- 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[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[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 );
+ 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 2792200b7..2f6af1592 100644
--- a/core/math/matrix3.h
+++ b/core/math/matrix3.h
@@ -29,22 +29,21 @@
#ifndef MATRIX3_H
#define MATRIX3_H
-#include "vector3.h"
#include "quat.h"
+#include "vector3.h"
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
class Matrix3 {
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];
}
@@ -57,83 +56,82 @@ public:
_FORCE_INLINE_ float 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);
- Matrix3 rotated(const Vector3& p_axis, real_t p_phi) const;
+ void rotate(const Vector3 &p_axis, real_t p_phi);
+ Matrix3 rotated(const Vector3 &p_axis, real_t p_phi) const;
- void scale( const Vector3& p_scale );
- Matrix3 scaled( const Vector3& p_scale ) const;
+ void scale(const Vector3 &p_scale);
+ Matrix3 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 operator==(const Matrix3& p_matrix) const;
- bool operator!=(const Matrix3& p_matrix) const;
+ bool operator==(const Matrix3 &p_matrix) const;
+ bool operator!=(const Matrix3 &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 Matrix3& p_matrix);
- _FORCE_INLINE_ Matrix3 operator*(const Matrix3& 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 Matrix3 &p_matrix);
+ _FORCE_INLINE_ Matrix3 operator*(const Matrix3 &p_matrix) const;
int get_orthogonal_index() const;
void set_orthogonal_index(int p_index);
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_ 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() {
@@ -142,18 +140,17 @@ public:
elements[2].zero();
}
- _FORCE_INLINE_ Matrix3 transpose_xform(const Matrix3& m) const
- {
+ _FORCE_INLINE_ Matrix3 transpose_xform(const Matrix3 &m) const {
return Matrix3(
- 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);
}
Matrix3(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) {
@@ -165,66 +162,60 @@ public:
operator Quat() const;
- Matrix3(const Quat& p_quat); // euler
- Matrix3(const Vector3& p_euler); // euler
- Matrix3(const Vector3& p_axis, real_t p_phi);
+ Matrix3(const Quat &p_quat); // euler
+ Matrix3(const Vector3 &p_euler); // euler
+ Matrix3(const Vector3 &p_axis, real_t p_phi);
_FORCE_INLINE_ Matrix3() {
- 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 Matrix3::operator*=(const Matrix3& p_matrix) {
+_FORCE_INLINE_ void Matrix3::operator*=(const Matrix3 &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_ Matrix3 Matrix3::operator*(const Matrix3& p_matrix) const {
+_FORCE_INLINE_ Matrix3 Matrix3::operator*(const Matrix3 &p_matrix) const {
return Matrix3(
- 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]));
}
-Vector3 Matrix3::xform(const Vector3& p_vector) const {
+Vector3 Matrix3::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 Matrix3::xform_inv(const Vector3& p_vector) const {
+Vector3 Matrix3::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));
}
float Matrix3::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 189041cdc..39ac2f0ab 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 "aabb.h"
#include "list.h"
-#include "variant.h"
#include "map.h"
#include "print_string.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() {
@@ -135,7 +133,6 @@ private:
}
};
-
struct PairData;
struct Element {
@@ -155,28 +152,36 @@ private:
AABB aabb;
AABB 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;
-
+ 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; }
+ 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;
@@ -197,248 +202,231 @@ 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)
-// pair_callback(pair_callback_userdata,p_A->userdata,p_B->userdata);
+ // if (pair_callback)
+ // pair_callback(pair_callback_userdata,p_A->userdata,p_B->userdata);
} else {
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 AABB& 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 AABB &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 AABB& 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 AABB &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 AABB& p_aabb=AABB(), 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 AABB& 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 AABB &p_aabb = AABB(), 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 AABB &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 AABB& 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 AABB &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) {
float 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) {
@@ -450,426 +438,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 */
- AABB aabb=p_octant->aabb;
- aabb.size*=0.5;
+ AABB 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 AABB& p_aabb) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::_ensure_valid_root(const AABB &p_aabb) {
if (!root) {
// octre is empty
- AABB base( Vector3(), Vector3(1.0,1.0,1.0) * unit_size);
+ AABB 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 {
- AABB base=root->aabb;
+ AABB 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 AABB& 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 AABB &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 AABB& p_aabb) {
+template <class T, bool use_pairs, class AL>
+void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const AABB &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);
@@ -897,25 +868,23 @@ void Octree<T,use_pairs,AL>::move(OctreeElementID p_id, const AABB& 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=AABB();
+ e.common_parent = NULL;
+ e.aabb = AABB();
_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;
@@ -927,124 +896,115 @@ void Octree<T,use_pairs,AL>::move(OctreeElementID p_id, const AABB& 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;
}
- AABB combined=e.aabb;
+ AABB 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)
-// o->elements.erase( E->get().E );
+ // if (!use_pairs)
+ // o->elements.erase( E->get().E );
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);
@@ -1060,28 +1020,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 {
@@ -1094,20 +1054,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)++;
@@ -1119,36 +1079,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 AABB &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 AABB& 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)
@@ -1165,19 +1124,19 @@ void Octree<T,use_pairs,AL>::_cull_AABB(Octant *p_octant,const AABB& 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)
@@ -1191,36 +1150,35 @@ void Octree<T,use_pairs,AL>::_cull_AABB(Octant *p_octant,const AABB& 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)
@@ -1237,20 +1195,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)
@@ -1266,37 +1224,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)
@@ -1313,20 +1269,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)
@@ -1342,120 +1298,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 AABB& 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 AABB &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 2a9793204..29e7f2e75 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 f746ea206..4ed510713 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 {
-
- float dist=normal.dot(p_point) - d;
- dist=ABS(dist);
- return ( dist <= _epsilon);
+bool Plane::has_point(const Vector3 &p_point, real_t _epsilon) const {
+ float 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 8aa06a204..2e9cfadc4 100644
--- a/core/math/quat.cpp
+++ b/core/math/quat.cpp
@@ -29,7 +29,7 @@
#include "quat.h"
#include "print_string.h"
-void Quat::set_euler(const Vector3& p_euler) {
+void Quat::set_euler(const Vector3 &p_euler) {
real_t half_yaw = p_euler.x * 0.5;
real_t half_pitch = p_euler.y * 0.5;
real_t half_roll = p_euler.z * 0.5;
@@ -39,30 +39,27 @@ void Quat::set_euler(const Vector3& p_euler) {
real_t sin_pitch = Math::sin(half_pitch);
real_t cos_roll = Math::cos(half_roll);
real_t sin_roll = Math::sin(half_roll);
- set(cos_roll * sin_pitch * cos_yaw+sin_roll * cos_pitch * sin_yaw,
- cos_roll * cos_pitch * sin_yaw - sin_roll * sin_pitch * cos_yaw,
- sin_roll * cos_pitch * cos_yaw - cos_roll * sin_pitch * sin_yaw,
- cos_roll * cos_pitch * cos_yaw+sin_roll * sin_pitch * sin_yaw);
+ set(cos_roll * sin_pitch * cos_yaw + sin_roll * cos_pitch * sin_yaw,
+ cos_roll * cos_pitch * sin_yaw - sin_roll * sin_pitch * cos_yaw,
+ sin_roll * cos_pitch * cos_yaw - cos_roll * sin_pitch * sin_yaw,
+ cos_roll * cos_pitch * cos_yaw + sin_roll * sin_pitch * sin_yaw);
}
-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());
@@ -72,17 +69,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
@@ -126,32 +121,29 @@ Quat Quat::slerp(const Quat& q, const real_t& t) const {
}
#else
- real_t to1[4];
- real_t omega, cosom, sinom, scale0, scale1;
-
+ real_t to1[4];
+ real_t omega, cosom, sinom, scale0, scale1;
// calc cosine
- cosom = x * q.x + y * q.y + z * q.z
- + w * q.w;
-
+ cosom = x * q.x + y * q.y + z * q.z + w * q.w;
// adjust signs (if necessary)
- if ( cosom <0.0 ) {
- cosom = -cosom; to1[0] = - q.x;
- to1[1] = - q.y;
- to1[2] = - q.z;
- to1[3] = - q.w;
- } else {
+ if (cosom < 0.0) {
+ cosom = -cosom;
+ to1[0] = -q.x;
+ to1[1] = -q.y;
+ to1[2] = -q.z;
+ to1[3] = -q.w;
+ } else {
to1[0] = q.x;
to1[1] = q.y;
to1[2] = q.z;
to1[3] = 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);
@@ -165,15 +157,14 @@ Quat Quat::slerp(const Quat& q, const real_t& t) const {
}
// calculate final values
return Quat(
- scale0 * x + scale1 * to1[0],
- scale0 * y + scale1 * to1[1],
- scale0 * z + scale1 * to1[2],
- scale0 * w + scale1 * to1[3]
- );
+ scale0 * x + scale1 * to1[0],
+ scale0 * y + scale1 * to1[1],
+ scale0 * z + scale1 * to1[2],
+ scale0 * w + scale1 * to1[3]);
#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;
@@ -181,15 +172,15 @@ Quat Quat::slerpni(const Quat& q, const real_t& t) const {
if (Math::absf(dot) > 0.9999f) return from;
- float theta = Math::acos(dot),
- sinT = 1.0f / Math::sin(theta),
- newFactor = Math::sin(t * theta) * sinT,
- invFactor = Math::sin((1.0f - t) * theta) * sinT;
+ float theta = Math::acos(dot),
+ sinT = 1.0f / Math::sin(theta),
+ newFactor = Math::sin(t * theta) * sinT,
+ invFactor = Math::sin((1.0f - 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 );
+ 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);
#if 0
real_t to1[4];
@@ -239,29 +230,27 @@ 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 :|
- float t2 = (1.0-t)*t*2;
- Quat sp = this->slerp(q,t);
- Quat sq = prep.slerpni(postq,t);
- return sp.slerpni(sq,t2);
-
+ float 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 s = Math::sin(-angle * 0.5) / d;
set(axis.x * s, axis.y * s, axis.z * s,
- Math::cos(-angle * 0.5));
+ Math::cos(-angle * 0.5));
}
}
diff --git a/core/math/quat.h b/core/math/quat.h
index 9f4145cdd..4234095a2 100644
--- a/core/math/quat.h
+++ b/core/math/quat.h
@@ -37,160 +37,167 @@
/**
@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);
- 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_ real_t dot(const Quat &q) const;
+ void set_euler(const Vector3 &p_euler);
+ 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.0f + d) * 2.0f);
+ real_t s = Math::sqrt((1.0f + d) * 2.0f);
real_t rs = 1.0f / 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 {
-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;
+ 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 {
+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;
+ 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 ce6f72641..5e4cf60e3 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 */
AABB 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;
float 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
-
{
float 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
{
float 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,156 +332,140 @@ 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
r_mesh.faces.clear();
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();
-
+ // 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();
}
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 04d25fef1..d620d1899 100644
--- a/core/math/quick_hull.h
+++ b/core/math/quick_hull.h
@@ -30,15 +30,13 @@
#define QUICK_HULL_H
#include "aabb.h"
-#include "set.h"
-#include "list.h"
#include "geometry.h"
+#include "list.h"
+#include "set.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/transform.cpp b/core/math/transform.cpp
index 8516e4afc..8e9cd39b1 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();
@@ -55,35 +52,34 @@ void Transform::invert() {
Transform Transform::inverse() const {
- 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 = *this * Transform( Matrix3( p_axis, p_phi ), Vector3() );
+ *this = *this * Transform(Matrix3(p_axis, p_phi), Vector3());
}
-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 *this * Transform( Matrix3( p_axis, p_phi ), Vector3() );
+ return *this * Transform(Matrix3(p_axis, p_phi), Vector3());
}
-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;
@@ -97,23 +93,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, float p_c) const {
+Transform Transform::interpolate_with(const Transform &p_transform, float p_c) const {
/* not sure if very "efficient" but good enough? */
@@ -126,45 +120,44 @@ Transform Transform::interpolate_with(const Transform& p_transform, float p_c) 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;
}
@@ -181,25 +174,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;
}
@@ -208,11 +201,8 @@ Transform::operator String() const {
return basis.operator String() + " - " + origin.operator String();
}
+Transform::Transform(const Matrix3 &p_basis, const Vector3 &p_origin) {
-Transform::Transform(const Matrix3& 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 7999f0b34..f7be19ebe 100644
--- a/core/math/transform.h
+++ b/core/math/transform.h
@@ -29,15 +29,14 @@
#ifndef TRANSFORM_H
#define TRANSFORM_H
+#include "aabb.h"
#include "matrix3.h"
#include "plane.h"
-#include "aabb.h"
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
class Transform {
public:
-
Matrix3 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 Matrix3& get_basis() const { return basis; }
- void set_basis(const Matrix3& p_basis) { basis=p_basis; }
+ const Matrix3 &get_basis() const { return basis; }
+ void set_basis(const Matrix3 &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_ AABB xform(const AABB& p_aabb) const;
- _FORCE_INLINE_ AABB xform_inv(const AABB& p_aabb) const;
+ _FORCE_INLINE_ AABB xform(const AABB &p_aabb) const;
+ _FORCE_INLINE_ AABB xform_inv(const AABB &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, float p_c) const;
+ Transform interpolate_with(const Transform &p_transform, float 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) {
+ 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;
+ 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 Matrix3& p_basis, const Vector3& p_origin=Vector3());
+ Transform(const Matrix3 &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_ AABB Transform::xform(const AABB& p_aabb) const {
- /* define vertices */
+_FORCE_INLINE_ AABB Transform::xform(const AABB &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
AABB 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_ AABB Transform::xform_inv(const AABB& p_aabb) const {
+_FORCE_INLINE_ AABB Transform::xform_inv(const AABB &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)
};
-
AABB 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 125727aa7..9046396f1 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;
}
-
AABB 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 DVector<Vector3> &p_faces) {
-void TriangleMesh::create(const DVector<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)
DVector<BVH>::Write bw = bvh.write();
{
@@ -114,148 +106,143 @@ void TriangleMesh::create(const DVector<Vector3>& p_faces) {
DVector<Vector3>::Read r = p_faces.read();
DVector<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());
DVector<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();
}
-
}
- DVector<BVH*> bwptrs;
+ DVector<BVH *> bwptrs;
bwptrs.resize(fc);
- DVector<BVH*>::Write bwp = bwptrs.write();
- for(int i=0;i<fc;i++) {
+ DVector<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=DVector<BVH>::Write(); //clearup
+ bw = DVector<BVH>::Write(); //clearup
bvh.resize(max_alloc); //resize back
- valid=true;
-
+ valid = true;
}
+Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const {
-Vector3 TriangleMesh::get_area_normal(const AABB& 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;
DVector<Triangle>::Read trianglesr = triangles.read();
- DVector<Vector3>::Read verticesr=vertices.read();
- DVector<BVH>::Read bvhr=bvh.read();
+ DVector<Vector3>::Read verticesr = vertices.read();
+ DVector<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 AABB& 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;
DVector<Triangle>::Read trianglesr = triangles.read();
- DVector<Vector3>::Read verticesr=vertices.read();
- DVector<BVH>::Read bvhr=bvh.read();
+ DVector<Vector3>::Read verticesr = vertices.read();
+ DVector<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(ray_aabb);
+ 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)) {
float 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;
DVector<Triangle>::Read trianglesr = triangles.read();
- DVector<Vector3>::Read verticesr=vertices.read();
- DVector<BVH>::Read bvhr=bvh.read();
+ DVector<Vector3>::Read verticesr = vertices.read();
+ DVector<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)) {
float 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 @@ DVector<Face3> TriangleMesh::get_faces() const {
int ts = triangles.size();
faces.resize(triangles.size());
- DVector<Face3>::Write w=faces.write();
+ DVector<Face3>::Write w = faces.write();
DVector<Triangle>::Read r = triangles.read();
DVector<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 @@ DVector<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 a310f7e32..17417b4ae 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 {
- OBJ_TYPE( TriangleMesh, Reference);
+ OBJ_TYPE(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);
DVector<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 AABB& 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 AABB &p_aabb) const;
DVector<Face3> get_faces() const;
-
- void create(const DVector<Vector3>& p_faces);
+ void create(const DVector<Vector3> &p_faces);
TriangleMesh();
};
diff --git a/core/math/triangulate.cpp b/core/math/triangulate.cpp
index 5112077bc..8dadf9c63 100644
--- a/core/math/triangulate.cpp
+++ b/core/math/triangulate.cpp
@@ -28,133 +28,139 @@
/*************************************************************************/
#include "triangulate.h"
-float Triangulate::get_area(const Vector<Vector2> &contour)
-{
+float 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];
- float A=0.0f;
+ float A = 0.0f;
- for(int p=n-1,q=0; q<n; p=q++)
- {
- A+= c[p].cross(c[q]);
- }
- return A*0.5f;
+ for (int p = n - 1, q = 0; q < n; p = q++) {
+ A += c[p].cross(c[q]);
+ }
+ return A * 0.5f;
}
- /*
+/*
is_inside_triangle decides if a point P is Inside of the triangle
defined by A, B, C.
*/
bool Triangulate::is_inside_triangle(float Ax, float Ay,
- float Bx, float By,
- float Cx, float Cy,
- float Px, float Py)
+ float Bx, float By,
+ float Cx, float Cy,
+ float Px, float Py)
{
- float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
- float cCROSSap, bCROSScp, aCROSSbp;
+ float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
+ float 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;
+ 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;
+ aCROSSbp = ax * bpy - ay * bpx;
+ cCROSSap = cx * apy - cy * apx;
+ bCROSScp = bx * cpy - by * cpx;
- return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
+ return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
};
-bool Triangulate::snip(const Vector<Vector2> &p_contour,int u,int v,int w,int n,const Vector<int>& V)
-{
- int p;
- float 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;
+ float 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.0f < 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.0f < 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]; nv--;
+ /* remove v from remaining polygon */
+ for (s = v, t = v + 1; t < nv; s++, t++)
+ V[s] = V[t];
+ 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 d22677a8b..bfe9e1e7c 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 float get_area(const Vector< Vector2 > &contour);
+ static float 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(float Ax, float Ay,
- float Bx, float By,
- float Cx, float Cy,
- float Px, float Py);
-
+ float Bx, float By,
+ float Cx, float Cy,
+ float Px, float 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 db96dda46..56034ab49 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, float p_phi) {
-void Vector3::rotate(const Vector3& p_axis,float p_phi) {
-
- *this=Matrix3(p_axis,p_phi).xform(*this);
+ *this = Matrix3(p_axis, p_phi).xform(*this);
}
-Vector3 Vector3::rotated(const Vector3& p_axis,float p_phi) const {
+Vector3 Vector3::rotated(const Vector3 &p_axis, float 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,31 +60,28 @@ int Vector3::max_axis() const {
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
}
-
void Vector3::snap(float p_val) {
- x+=p_val/2.0;
- x-=Math::fmod(x,p_val);
- y+=p_val/2.0;
- y-=Math::fmod(y,p_val);
- z+=p_val/2.0;
- z-=Math::fmod(z,p_val);
-
+ x += p_val / 2.0;
+ x -= Math::fmod(x, p_val);
+ y += p_val / 2.0;
+ y -= Math::fmod(y, p_val);
+ z += p_val / 2.0;
+ z -= Math::fmod(z, p_val);
}
Vector3 Vector3::snapped(float 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, float p_t) const {
-Vector3 Vector3::cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float 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
@@ -95,44 +90,41 @@ Vector3 Vector3::cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, c
float bc = p1.distance_to(p2);
float 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);
}
-
float t = p_t;
float t2 = t * t;
float t3 = t2 * t;
Vector3 out;
- out = 0.5f * ( ( p1 * 2.0f) +
- ( -p0 + p2 ) * t +
- ( 2.0f * p0 - 5.0f * p1 + 4 * p2 - p3 ) * t2 +
- ( -p0 + 3.0f * p1 - 3.0f * p2 + p3 ) * t3 );
+ out = 0.5f * ((p1 * 2.0f) +
+ (-p0 + p2) * t +
+ (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
+ (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
return out;
-
}
-Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const {
+Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, float 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;
float t = p_t;
float t2 = t * t;
float t3 = t2 * t;
Vector3 out;
- out = 0.5f * ( ( p1 * 2.0f) +
- ( -p0 + p2 ) * t +
- ( 2.0f * p0 - 5.0f * p1 + 4 * p2 - p3 ) * t2 +
- ( -p0 + 3.0f * p1 - 3.0f * p2 + p3 ) * t3 );
+ out = 0.5f * ((p1 * 2.0f) +
+ (-p0 + p2) * t +
+ (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
+ (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
return out;
-
}
#if 0
@@ -179,8 +171,8 @@ Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, co
( -p0.z + 3.0f * p1.z - 3.0f * 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 3f451b0ab..b02fa5991 100644
--- a/core/math/vector3.h
+++ b/core/math/vector3.h
@@ -29,12 +29,11 @@
#ifndef VECTOR3_H
#define VECTOR3_H
-#include "typedefs.h"
#include "math_defs.h"
#include "math_funcs.h"
+#include "typedefs.h"
#include "ustring.h"
-
struct Vector3 {
enum Axis {
@@ -53,17 +52,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;
@@ -81,61 +80,61 @@ struct Vector3 {
void snap(float p_val);
Vector3 snapped(float p_val) const;
- void rotate(const Vector3& p_axis,float p_phi);
- Vector3 rotated(const Vector3& p_axis,float p_phi) const;
+ void rotate(const Vector3 &p_axis, float p_phi);
+ Vector3 rotated(const Vector3 &p_axis, float p_phi) const;
/* Static Methods between 2 vector3s */
- _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3& p_b,float p_t) const;
- Vector3 cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const;
- Vector3 cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const;
+ _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3 &p_b, float p_t) const;
+ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, float p_t) const;
+ Vector3 cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, float p_t) const;
- _FORCE_INLINE_ Vector3 cross(const Vector3& p_b) const;
- _FORCE_INLINE_ real_t dot(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_ 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_ 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;
-
+ _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
@@ -144,246 +143,244 @@ struct Vector3 {
#else
-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;
}
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,float p_t) const {
+Vector3 Vector3::linear_interpolate(const Vector3 &p_b, float 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 {
+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 {
+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;
}