Skip to content

Commit 806a4d5

Browse files
committed
FontUtils: Moved triangulate code to ShapeUtils.
1 parent 1e32e60 commit 806a4d5

File tree

2 files changed

+189
-194
lines changed

2 files changed

+189
-194
lines changed

src/extras/FontUtils.js

Lines changed: 0 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -270,198 +270,6 @@ THREE.FontUtils.generateShapes = function ( text, parameters ) {
270270

271271
};
272272

273-
274-
/**
275-
* This code is a quick port of code written in C++ which was submitted to
276-
* flipcode.com by John W. Ratcliff // July 22, 2000
277-
* See original code and more information here:
278-
* http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
279-
*
280-
* ported to actionscript by Zevan Rosser
281-
* www.actionsnippet.com
282-
*
283-
* ported to javascript by Joshua Koo
284-
* http://www.lab4games.net/zz85/blog
285-
*
286-
*/
287-
288-
289-
( function ( namespace ) {
290-
291-
// takes in an contour array and returns
292-
293-
function process( contour, indices ) {
294-
295-
var n = contour.length;
296-
297-
if ( n < 3 ) return null;
298-
299-
var result = [],
300-
verts = [],
301-
vertIndices = [];
302-
303-
/* we want a counter-clockwise polygon in verts */
304-
305-
var u, v, w;
306-
307-
if ( area( contour ) > 0.0 ) {
308-
309-
for ( v = 0; v < n; v ++ ) verts[ v ] = v;
310-
311-
} else {
312-
313-
for ( v = 0; v < n; v ++ ) verts[ v ] = ( n - 1 ) - v;
314-
315-
}
316-
317-
var nv = n;
318-
319-
/* remove nv - 2 vertices, creating 1 triangle every time */
320-
321-
var count = 2 * nv; /* error detection */
322-
323-
for ( v = nv - 1; nv > 2; ) {
324-
325-
/* if we loop, it is probably a non-simple polygon */
326-
327-
if ( ( count -- ) <= 0 ) {
328-
329-
//** Triangulate: ERROR - probable bad polygon!
330-
331-
//throw ( "Warning, unable to triangulate polygon!" );
332-
//return null;
333-
// Sometimes warning is fine, especially polygons are triangulated in reverse.
334-
console.warn( 'THREE.FontUtils: Warning, unable to triangulate polygon! in Triangulate.process()' );
335-
336-
if ( indices ) return vertIndices;
337-
return result;
338-
339-
}
340-
341-
/* three consecutive vertices in current polygon, <u,v,w> */
342-
343-
u = v; if ( nv <= u ) u = 0; /* previous */
344-
v = u + 1; if ( nv <= v ) v = 0; /* new v */
345-
w = v + 1; if ( nv <= w ) w = 0; /* next */
346-
347-
if ( snip( contour, u, v, w, nv, verts ) ) {
348-
349-
var a, b, c, s, t;
350-
351-
/* true names of the vertices */
352-
353-
a = verts[ u ];
354-
b = verts[ v ];
355-
c = verts[ w ];
356-
357-
/* output Triangle */
358-
359-
result.push( [ contour[ a ],
360-
contour[ b ],
361-
contour[ c ] ] );
362-
363-
364-
vertIndices.push( [ verts[ u ], verts[ v ], verts[ w ] ] );
365-
366-
/* remove v from the remaining polygon */
367-
368-
for ( s = v, t = v + 1; t < nv; s ++, t ++ ) {
369-
370-
verts[ s ] = verts[ t ];
371-
372-
}
373-
374-
nv --;
375-
376-
/* reset error detection counter */
377-
378-
count = 2 * nv;
379-
380-
}
381-
382-
}
383-
384-
if ( indices ) return vertIndices;
385-
return result;
386-
387-
}
388-
389-
// calculate area of the contour polygon
390-
391-
function area( contour ) {
392-
393-
var n = contour.length;
394-
var a = 0.0;
395-
396-
for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
397-
398-
a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
399-
400-
}
401-
402-
return a * 0.5;
403-
404-
}
405-
406-
function snip( contour, u, v, w, n, verts ) {
407-
408-
var p;
409-
var ax, ay, bx, by;
410-
var cx, cy, px, py;
411-
412-
ax = contour[ verts[ u ] ].x;
413-
ay = contour[ verts[ u ] ].y;
414-
415-
bx = contour[ verts[ v ] ].x;
416-
by = contour[ verts[ v ] ].y;
417-
418-
cx = contour[ verts[ w ] ].x;
419-
cy = contour[ verts[ w ] ].y;
420-
421-
if ( Number.EPSILON > ( ( ( bx - ax ) * ( cy - ay ) ) - ( ( by - ay ) * ( cx - ax ) ) ) ) return false;
422-
423-
var aX, aY, bX, bY, cX, cY;
424-
var apx, apy, bpx, bpy, cpx, cpy;
425-
var cCROSSap, bCROSScp, aCROSSbp;
426-
427-
aX = cx - bx; aY = cy - by;
428-
bX = ax - cx; bY = ay - cy;
429-
cX = bx - ax; cY = by - ay;
430-
431-
for ( p = 0; p < n; p ++ ) {
432-
433-
px = contour[ verts[ p ] ].x;
434-
py = contour[ verts[ p ] ].y;
435-
436-
if ( ( ( px === ax ) && ( py === ay ) ) ||
437-
( ( px === bx ) && ( py === by ) ) ||
438-
( ( px === cx ) && ( py === cy ) ) ) continue;
439-
440-
apx = px - ax; apy = py - ay;
441-
bpx = px - bx; bpy = py - by;
442-
cpx = px - cx; cpy = py - cy;
443-
444-
// see if p is inside triangle abc
445-
446-
aCROSSbp = aX * bpy - aY * bpx;
447-
cCROSSap = cX * apy - cY * apx;
448-
bCROSScp = bX * cpy - bY * cpx;
449-
450-
if ( ( aCROSSbp >= - Number.EPSILON ) && ( bCROSScp >= - Number.EPSILON ) && ( cCROSSap >= - Number.EPSILON ) ) return false;
451-
452-
}
453-
454-
return true;
455-
456-
}
457-
458-
namespace.Triangulate = process;
459-
namespace.Triangulate.area = area;
460-
461-
return namespace;
462-
463-
} )( THREE.FontUtils );
464-
465273
// To use the typeface.js face files, hook up the API
466274

467275
THREE.typeface_js = { faces: THREE.FontUtils.faces, loadFace: THREE.FontUtils.loadFace };

0 commit comments

Comments
 (0)