diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
commit | 5dbf1809c6e3e905b94b8764e99491e608122261 (patch) | |
tree | 5e5a5360db15d86d59ec8c6e4f7eb511388c5a9a /core/math/triangulate.cpp | |
parent | 45438e9918d421b244bfd7776a30e67dc7f2d3e3 (diff) |
A Whole New World (clang-format edition)
I can show you the code
Pretty, with proper whitespace
Tell me, coder, now when did
You last write readable code?
I can open your eyes
Make you see your bad indent
Force you to respect the style
The core devs agreed upon
A whole new world
A new fantastic code format
A de facto standard
With some sugar
Enforced with clang-format
A whole new world
A dazzling style we all dreamed of
And when we read it through
It's crystal clear
That now we're in a whole new world of code
Diffstat (limited to 'core/math/triangulate.cpp')
-rw-r--r-- | core/math/triangulate.cpp | 194 |
1 files changed, 99 insertions, 95 deletions
diff --git a/core/math/triangulate.cpp b/core/math/triangulate.cpp index 128b6ca331..8568a963ab 100644 --- a/core/math/triangulate.cpp +++ b/core/math/triangulate.cpp @@ -28,136 +28,140 @@ /*************************************************************************/ #include "triangulate.h" -real_t Triangulate::get_area(const Vector<Vector2> &contour) -{ +real_t Triangulate::get_area(const Vector<Vector2> &contour) { - int n = contour.size(); - const Vector2 *c=&contour[0]; + int n = contour.size(); + const Vector2 *c = &contour[0]; - real_t A=0.0; + real_t A = 0.0; - for(int p=n-1,q=0; q<n; p=q++) - { - A+= c[p].cross(c[q]); - } - return A*0.5; + for (int p = n - 1, q = 0; q < n; p = q++) { + A += c[p].cross(c[q]); + } + return A * 0.5; } - /* +/* is_inside_triangle decides if a point P is Inside of the triangle defined by A, B, C. */ bool Triangulate::is_inside_triangle(real_t Ax, real_t Ay, - real_t Bx, real_t By, - real_t Cx, real_t Cy, - real_t Px, real_t Py) + real_t Bx, real_t By, + real_t Cx, real_t Cy, + real_t Px, real_t Py) { - real_t ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy; - real_t cCROSSap, bCROSScp, aCROSSbp; - - ax = Cx - Bx; ay = Cy - By; - bx = Ax - Cx; by = Ay - Cy; - cx = Bx - Ax; cy = By - Ay; - apx= Px - Ax; apy= Py - Ay; - bpx= Px - Bx; bpy= Py - By; - cpx= Px - Cx; cpy= Py - Cy; - - aCROSSbp = ax*bpy - ay*bpx; - cCROSSap = cx*apy - cy*apx; - bCROSScp = bx*cpy - by*cpx; - - return ((aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0)); + real_t ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy; + real_t cCROSSap, bCROSScp, aCROSSbp; + + ax = Cx - Bx; + ay = Cy - By; + bx = Ax - Cx; + by = Ay - Cy; + cx = Bx - Ax; + cy = By - Ay; + apx = Px - Ax; + apy = Py - Ay; + bpx = Px - Bx; + bpy = Py - By; + cpx = Px - Cx; + cpy = Py - Cy; + + aCROSSbp = ax * bpy - ay * bpx; + cCROSSap = cx * apy - cy * apx; + bCROSScp = bx * cpy - by * cpx; + + return ((aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0)); }; -bool Triangulate::snip(const Vector<Vector2> &p_contour,int u,int v,int w,int n,const Vector<int>& V) -{ - int p; - real_t Ax, Ay, Bx, By, Cx, Cy, Px, Py; - const Vector2 *contour=&p_contour[0]; +bool Triangulate::snip(const Vector<Vector2> &p_contour, int u, int v, int w, int n, const Vector<int> &V) { + int p; + real_t Ax, Ay, Bx, By, Cx, Cy, Px, Py; + const Vector2 *contour = &p_contour[0]; - Ax = contour[V[u]].x; - Ay = contour[V[u]].y; + Ax = contour[V[u]].x; + Ay = contour[V[u]].y; - Bx = contour[V[v]].x; - By = contour[V[v]].y; + Bx = contour[V[v]].x; + By = contour[V[v]].y; - Cx = contour[V[w]].x; - Cy = contour[V[w]].y; + Cx = contour[V[w]].x; + Cy = contour[V[w]].y; - if ( CMP_EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false; + if (CMP_EPSILON > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax)))) return false; - for (p=0;p<n;p++) - { - if( (p == u) || (p == v) || (p == w) ) continue; - Px = contour[V[p]].x; - Py = contour[V[p]].y; - if (is_inside_triangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false; - } + for (p = 0; p < n; p++) { + if ((p == u) || (p == v) || (p == w)) continue; + Px = contour[V[p]].x; + Py = contour[V[p]].y; + if (is_inside_triangle(Ax, Ay, Bx, By, Cx, Cy, Px, Py)) return false; + } - return true; + return true; } -bool Triangulate::triangulate(const Vector<Vector2> &contour,Vector<int> &result) -{ - /* allocate and initialize list of Vertices in polygon */ - - int n = contour.size(); - if ( n < 3 ) return false; +bool Triangulate::triangulate(const Vector<Vector2> &contour, Vector<int> &result) { + /* allocate and initialize list of Vertices in polygon */ + int n = contour.size(); + if (n < 3) return false; - Vector<int> V; - V.resize(n); + Vector<int> V; + V.resize(n); - /* we want a counter-clockwise polygon in V */ + /* we want a counter-clockwise polygon in V */ - if ( 0.0 < get_area(contour) ) - for (int v=0; v<n; v++) V[v] = v; - else - for(int v=0; v<n; v++) V[v] = (n-1)-v; + if (0.0 < get_area(contour)) + for (int v = 0; v < n; v++) + V[v] = v; + else + for (int v = 0; v < n; v++) + V[v] = (n - 1) - v; - int nv = n; + int nv = n; - /* remove nv-2 Vertices, creating 1 triangle every time */ - int count = 2*nv; /* error detection */ + /* remove nv-2 Vertices, creating 1 triangle every time */ + int count = 2 * nv; /* error detection */ - for(int v=nv-1; nv>2; ) - { - /* if we loop, it is probably a non-simple polygon */ - if (0 >= (count--)) - { - //** Triangulate: ERROR - probable bad polygon! - return false; - } + for (int v = nv - 1; nv > 2;) { + /* if we loop, it is probably a non-simple polygon */ + if (0 >= (count--)) { + //** Triangulate: ERROR - probable bad polygon! + return false; + } - /* three consecutive vertices in current polygon, <u,v,w> */ - int u = v ; if (nv <= u) u = 0; /* previous */ - v = u+1; if (nv <= v) v = 0; /* new v */ - int w = v+1; if (nv <= w) w = 0; /* next */ + /* three consecutive vertices in current polygon, <u,v,w> */ + int u = v; + if (nv <= u) u = 0; /* previous */ + v = u + 1; + if (nv <= v) v = 0; /* new v */ + int w = v + 1; + if (nv <= w) w = 0; /* next */ - if ( snip(contour,u,v,w,nv,V) ) - { - int a,b,c,s,t; + if (snip(contour, u, v, w, nv, V)) { + int a, b, c, s, t; - /* true names of the vertices */ - a = V[u]; b = V[v]; c = V[w]; + /* true names of the vertices */ + a = V[u]; + b = V[v]; + c = V[w]; - /* output Triangle */ - result.push_back( a ); - result.push_back( b ); - result.push_back( c ); + /* output Triangle */ + result.push_back(a); + result.push_back(b); + result.push_back(c); - /* remove v from remaining polygon */ - for(s=v,t=v+1;t<nv;s++,t++) - V[s] = V[t]; + /* remove v from remaining polygon */ + for (s = v, t = v + 1; t < nv; s++, t++) + V[s] = V[t]; - nv--; + nv--; - /* resest error detection counter */ - count = 2*nv; - } - } + /* resest error detection counter */ + count = 2 * nv; + } + } - return true; + return true; } |