1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
"""
Intercircle is a collection of utilities for intersecting circles, used to get smooth loops around a collection of points and inset & outset loops.
"""
from __future__ import absolute_import
try:
import psyco
psyco.full()
except:
pass
#Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
import __init__
from skeinforge_tools.skeinforge_utilities.vector3 import Vector3
from skeinforge_tools.skeinforge_utilities import euclidean
import math
__author__ = "Enrique Perez (perez_enrique@yahoo.com)"
__date__ = "$Date: 2008/21/04 $"
__license__ = "GPL 3.0"
def addCircleIntersectionLoop( circleIntersectionPaths, circleIntersections ):
"Add a circle intersection loop."
firstCircleIntersection = circleIntersectionPaths[ 0 ]
circleIntersectionAhead = firstCircleIntersection
for circleIntersectionIndex in xrange( len( circleIntersections ) + 1 ):
circleIntersectionAhead = circleIntersectionAhead.getCircleIntersectionAhead()
if circleIntersectionAhead.index == firstCircleIntersection.index:
firstCircleIntersection.steppedOn = True
return
if circleIntersectionAhead.steppedOn == True:
print( 'circleIntersectionAhead.steppedOn == True in intercircle.' )
print( circleIntersectionAhead )
circleIntersectionAhead.addToList( circleIntersectionPaths )
firstCircleIntersection.steppedOn = True
print( "addCircleIntersectionLoop would have gone into an endless loop, this should never happen." )
print( "circleIntersectionPaths" )
for circleIntersection in circleIntersectionPaths:
print( circleIntersection )
print( circleIntersection.circleNodeAhead )
print( circleIntersection.circleNodeBehind )
print( "firstCircleIntersection" )
print( firstCircleIntersection )
print( "circleIntersections" )
for circleIntersection in circleIntersections:
print( circleIntersection )
def addOrbits( distanceFeedRate, loop, orbitalFeedRatePerSecond, temperatureChangeTime, z ):
"Add orbits with the extruder off."
timeInOrbit = 0.0
while timeInOrbit < temperatureChangeTime:
for point in loop:
distanceFeedRate.addGcodeMovementZWithFeedRate( 60.0 * orbitalFeedRatePerSecond, point, z )
timeInOrbit += euclidean.getPolygonLength( loop ) / orbitalFeedRatePerSecond
def addOrbitsIfLarge( distanceFeedRate, loop, orbitalFeedRatePerSecond, temperatureChangeTime, z ):
"Add orbits with the extruder off if the orbits are large enough."
if orbitsAreLarge( loop, temperatureChangeTime ):
addOrbits( distanceFeedRate, loop, orbitalFeedRatePerSecond, temperatureChangeTime, z )
def addPointsFromSegment( pointBegin, pointEnd, points, radius, thresholdRatio = 0.9 ):
"Add point complexes between the endpoints of a segment."
if radius <= 0.0:
print( 'This should never happen, radius should never be zero or less in addPointsFromSegment in intercircle.' )
thresholdRadius = radius * thresholdRatio # a higher number would be faster but would leave bigger dangling loops and extra dangling loops.
thresholdDiameter = thresholdRadius + thresholdRadius
segment = pointEnd - pointBegin
segmentLength = abs( segment )
extraCircles = int( math.floor( segmentLength / thresholdDiameter ) )
if extraCircles < 1:
return
if segmentLength == 0.0:
print( 'This should never happen, segmentLength = 0.0 in intercircle.' )
print( 'pointBegin' )
print( pointBegin )
print( pointEnd )
return
if extraCircles < 2:
lengthIncrement = segmentLength / ( float( extraCircles ) + 1.0 )
segment *= lengthIncrement / segmentLength
pointBegin += segment
else:
pointBegin += segment * thresholdDiameter / segmentLength
remainingLength = segmentLength - thresholdDiameter - thresholdDiameter
lengthIncrement = remainingLength / ( float( extraCircles ) - 1.0 )
segment *= lengthIncrement / segmentLength
for circleIndex in xrange( extraCircles ):
points.append( pointBegin )
pointBegin += segment
def getAroundsFromLoop( loop, radius, thresholdRatio = 0.9 ):
"Get the arounds from the loop, later combine with get arounds."
slightlyGreaterThanRadius = 1.01 * abs( radius )
points = getPointsFromLoop( loop, slightlyGreaterThanRadius, thresholdRatio )
return getAroundsFromPoints( points, radius )
def getAroundsFromLoops( loops, radius, thresholdRatio = 0.9 ):
"Get the arounds from the loops."
slightlyGreaterThanRadius = 1.01 * abs( radius )
points = getPointsFromLoops( loops, slightlyGreaterThanRadius, thresholdRatio )
return getAroundsFromPoints( points, radius )
def getAroundsFromPoints( points, radius ):
"Get the arounds from the points."
arounds = []
radius = abs( radius )
centers = getCentersFromPoints( points, radius )
for center in centers:
inset = getSimplifiedInsetFromClockwiseLoop( center, radius )
if isLargeSameDirection( inset, center, radius ):
arounds.append( inset )
return arounds
def getCentersFromCircleNodes( circleNodes, radius ):
"Get the complex centers of the circle intersection loops from circle nodes."
if len( circleNodes ) < 2:
return []
circleIntersections = getCircleIntersectionsFromCircleNodes( circleNodes )
circleIntersectionLoops = getCircleIntersectionLoops( circleIntersections )
return getCentersFromIntersectionLoops( circleIntersectionLoops, radius )
def getCentersFromIntersectionLoop( circleIntersectionLoop, radius ):
"Get the centers from the intersection loop."
loop = []
for circleIntersection in circleIntersectionLoop:
loop.append( circleIntersection.circleNodeAhead.circle * radius )
return loop
def getCentersFromIntersectionLoops( circleIntersectionLoops, radius ):
"Get the centers from the intersection loops."
centers = []
for circleIntersectionLoop in circleIntersectionLoops:
centers.append( getCentersFromIntersectionLoop( circleIntersectionLoop, radius ) )
return centers
def getCentersFromLoop( loop, radius ):
"Get the centers of the loop."
circleNodes = getCircleNodesFromLoop( loop, radius )
return getCentersFromCircleNodes( circleNodes, radius )
def getCentersFromLoopDirection( isWiddershins, loop, radius ):
"Get the centers of the loop which go around in the given direction."
centers = getCentersFromLoop( loop, radius )
return getLoopsFromLoopsDirection( isWiddershins, centers )
def getCentersFromPoints( points, radius ):
"Get the centers from the points."
circleNodes = getCircleNodesFromPoints( points, abs( radius ) )
return getCentersFromCircleNodes( circleNodes, abs( radius ) )
def getCircleIntersectionsFromCircleNodes( circleNodes ):
"Get all the circle intersections which exist between all the circle nodes."
if len( circleNodes ) < 1:
return []
circleIntersections = []
index = 0
pixelTable = {}
for circleNode in circleNodes:
euclidean.addElementToPixelListFromPoint( circleNode, pixelTable, circleNode.circle )
accumulatedCircleNodeTable = {}
for circleNodeIndex in xrange( len( circleNodes ) ):
circleNodeBehind = circleNodes[ circleNodeIndex ]
circleNodeIndexMinusOne = circleNodeIndex - 1
if circleNodeIndexMinusOne >= 0:
circleNodeAdditional = circleNodes[ circleNodeIndexMinusOne ]
euclidean.addElementToPixelListFromPoint( circleNodeAdditional, accumulatedCircleNodeTable, 0.5 * circleNodeAdditional.circle )
withinNodes = circleNodeBehind.getWithinNodes( accumulatedCircleNodeTable )
for circleNodeAhead in withinNodes:
circleIntersectionForward = CircleIntersection( circleNodeAhead, index, circleNodeBehind )
if not circleIntersectionForward.isWithinCircles( pixelTable ):
circleIntersections.append( circleIntersectionForward )
circleNodeBehind.circleIntersections.append( circleIntersectionForward )
index += 1
circleIntersectionBackward = CircleIntersection( circleNodeBehind, index, circleNodeAhead )
if not circleIntersectionBackward.isWithinCircles( pixelTable ):
circleIntersections.append( circleIntersectionBackward )
circleNodeAhead.circleIntersections.append( circleIntersectionBackward )
index += 1
return circleIntersections
def getCircleIntersectionLoops( circleIntersections ):
"Get all the loops going through the circle intersections."
circleIntersectionLoops = []
for circleIntersection in circleIntersections:
if not circleIntersection.steppedOn:
circleIntersectionLoop = [ circleIntersection ]
circleIntersectionLoops.append( circleIntersectionLoop )
addCircleIntersectionLoop( circleIntersectionLoop, circleIntersections )
return circleIntersectionLoops
def getCircleNodesFromLoop( loop, radius, thresholdRatio = 0.9 ):
"Get the circle nodes from every point on a loop and between points."
radius = abs( radius )
points = getPointsFromLoop( loop, radius, thresholdRatio )
return getCircleNodesFromPoints( points, radius )
def getCircleNodesFromPoints( points, radius ):
"Get the circle nodes from a path."
circleNodes = []
oneOverRadius = 1.0 / radius
points = euclidean.getAwayPoints( points, 0.001 * radius )
for point in points:
circleNodes.append( CircleNode( point * oneOverRadius, len( circleNodes ) ) )
return circleNodes
def getInsetFromClockwiseTriple( aheadAbsolute, behindAbsolute, center, radius ):
"Get loop inset from clockwise triple, out from widdershins loop."
originalCenterMinusBehind = euclidean.getNormalized( center - behindAbsolute )
reverseRoundZAngle = complex( originalCenterMinusBehind.real, - originalCenterMinusBehind.imag )
rotatedAheadAbsolute = aheadAbsolute * reverseRoundZAngle
rotatedBehindAbsolute = behindAbsolute * reverseRoundZAngle
rotatedCenter = center * reverseRoundZAngle
aheadIntersection = getIntersectionAtInset( rotatedAheadAbsolute, rotatedCenter, radius )
behindIntersection = getIntersectionAtInset( rotatedCenter, rotatedBehindAbsolute, radius )
centerMinusAhead = rotatedCenter - rotatedAheadAbsolute
if abs( centerMinusAhead.imag ) < abs( 0.00001 * centerMinusAhead.real ):
between = 0.5 * ( aheadIntersection + behindIntersection )
return originalCenterMinusBehind * between
yMinusAhead = behindIntersection.imag - aheadIntersection.imag
x = aheadIntersection.real + yMinusAhead * centerMinusAhead.real / centerMinusAhead.imag
insetFromClockwiseTriple = originalCenterMinusBehind * complex( x, behindIntersection.imag )
insetMinusOriginal = insetFromClockwiseTriple - center
distance = abs( insetMinusOriginal )
maximumDistance = 2.0 * radius
if distance < maximumDistance:
return insetFromClockwiseTriple
return center + maximumDistance / distance * insetMinusOriginal
def getInsetFromClockwiseLoop( loop, radius ):
"Get loop inset from clockwise loop, out from widdershins loop."
insetLoop = []
for pointIndex in xrange( len( loop ) ):
behindAbsolute = loop[ ( pointIndex + len( loop ) - 1 ) % len( loop ) ]
center = loop[ pointIndex ]
aheadAbsolute = loop[ ( pointIndex + 1 ) % len( loop ) ]
insetLoop.append( getInsetFromClockwiseTriple( aheadAbsolute, behindAbsolute, center, radius ) )
return insetLoop
def getInsetLoopsFromLoop( inset, loop, thresholdRatio = 0.9 ):
"Get the inset loops, which might overlap."
isInset = inset > 0
insetLoops = []
isLoopWiddershins = euclidean.isWiddershins( loop )
arounds = getAroundsFromLoop( loop, inset, thresholdRatio )
for around in arounds:
leftPoint = euclidean.getLeftPoint( around )
shouldBeWithin = ( isInset == isLoopWiddershins )
if euclidean.isPointInsideLoop( loop, leftPoint ) == shouldBeWithin:
if isLoopWiddershins != euclidean.isWiddershins( around ):
around.reverse()
insetLoops.append( around )
return insetLoops
def getInsetLoopsFromLoops( inset, loops ):
"Get the inset loops, which might overlap."
insetLoops = []
for loop in loops:
insetLoops += getInsetLoopsFromLoop( inset, loop )
return insetLoops
def getInsetSeparateLoopsFromLoops( inset, loops, thresholdRatio = 0.9 ):
"Get the separate inset loops."
isInset = inset > 0
insetSeparateLoops = []
radius = abs( inset )
arounds = getAroundsFromLoops( loops, radius, thresholdRatio )
for around in arounds:
leftPoint = euclidean.getLeftPoint( around )
if isInset == euclidean.isInFilledRegion( loops, leftPoint ):
if isInset:
around.reverse()
insetSeparateLoops.append( around )
return insetSeparateLoops
def getIntersectionAtInset( ahead, behind, inset ):
"Get circle intersection loop at inset from segment."
aheadMinusBehind = 0.5 * ( ahead - behind )
rotatedClockwiseQuarter = complex( aheadMinusBehind.imag, - aheadMinusBehind.real )
rotatedClockwiseQuarter *= inset / abs( rotatedClockwiseQuarter )
return aheadMinusBehind + behind + rotatedClockwiseQuarter
def getLargestInsetLoopFromLoop( loop, radius ):
"Get the largest inset loop from the loop."
loops = getInsetLoopsFromLoop( radius, loop )
return euclidean.getLargestLoop( loops )
def getLargestInsetLoopFromLoopNoMatterWhat( loop, radius ):
"Get the largest inset loop from the loop, even if the radius has to be shrunk and even if there is still no inset loop."
largestInsetLoop = getLargestInsetLoopFromLoop( loop, radius )
if largestInsetLoop != None:
return largestInsetLoop
largestInsetLoop = getLargestInsetLoopFromLoop( loop, 0.55 * radius )
if largestInsetLoop != None:
return largestInsetLoop
largestInsetLoop = getLargestInsetLoopFromLoop( loop, 0.35 * radius )
if largestInsetLoop != None:
return largestInsetLoop
largestInsetLoop = getLargestInsetLoopFromLoop( loop, 0.2 * radius )
if largestInsetLoop != None:
return largestInsetLoop
print( 'This should never happen, there should always be a largestInsetLoop in getLargestInsetLoopFromLoopNoMatterWhat in intercircle.' )
print( loop )
return loop
def getLoopsFromLoopsDirection( isWiddershins, loops ):
"Get the loops going round in a given direction."
directionalLoops = []
for loop in loops:
if euclidean.isWiddershins( loop ) == isWiddershins:
directionalLoops.append( loop )
return directionalLoops
def getPointsFromLoop( loop, radius, thresholdRatio = 0.9 ):
"Get the points from every point on a loop and between points."
radius = abs( radius )
points = []
for pointIndex in xrange( len( loop ) ):
pointBegin = loop[ pointIndex ]
pointEnd = loop[ ( pointIndex + 1 ) % len( loop ) ]
points.append( pointBegin )
addPointsFromSegment( pointBegin, pointEnd, points, radius, thresholdRatio )
return points
def getPointsFromLoops( loops, radius, thresholdRatio = 0.9 ):
"Get the points from every point on a loop and between points."
points = []
for loop in loops:
points += getPointsFromLoop( loop, radius, thresholdRatio )
return points
def getSimplifiedInsetFromClockwiseLoop( loop, radius ):
"Get loop inset from clockwise loop, out from widdershins loop."
return getWithoutIntersections( euclidean.getSimplifiedLoop( getInsetFromClockwiseLoop( loop, radius ), radius ) )
def getWithoutIntersections( loop ):
"Get loop without intersections."
lastLoopLength = len( loop )
while lastLoopLength > 3:
removeIntersection( loop )
if len( loop ) == lastLoopLength:
return loop
lastLoopLength = len( loop )
return loop
def isLargeSameDirection( inset, loop, radius ):
"Determine if the inset is in the same direction as the loop and it is large enough."
if euclidean.isWiddershins( inset ) != euclidean.isWiddershins( loop ):
return False
return euclidean.getMaximumSpan( inset ) > 2.01 * abs( radius )
def isLoopIntersectingLoop( anotherLoop, loop ):
"Determine if the a loop is intersecting another loop."
for pointIndex in xrange( len( loop ) ):
pointFirst = loop[ pointIndex ]
pointSecond = loop[ ( pointIndex + 1 ) % len( loop ) ]
segment = pointFirst - pointSecond
normalizedSegment = euclidean.getNormalized( segment )
segmentYMirror = complex( normalizedSegment.real, - normalizedSegment.imag )
segmentFirstPoint = segmentYMirror * pointFirst
segmentSecondPoint = segmentYMirror * pointSecond
if euclidean.isLoopIntersectingInsideXSegment( anotherLoop, segmentFirstPoint.real, segmentSecondPoint.real, segmentYMirror, segmentFirstPoint.imag ):
return True
return False
def orbitsAreLarge( loop, temperatureChangeTime ):
"Determine if the orbits are large enough."
if len( loop ) < 1:
print( 'Zero length loop which was skipped over, this should never happen.' )
return False
return temperatureChangeTime > 1.5
def removeIntersection( loop ):
"Get loop without the first intersection."
withoutIntersection = []
for pointIndex in xrange( len( loop ) ):
behind = loop[ ( pointIndex + len( loop ) - 1 ) % len( loop ) ]
behindEnd = loop[ ( pointIndex + len( loop ) - 2 ) % len( loop ) ]
behindMidpoint = 0.5 * ( behind + behindEnd )
ahead = loop[ pointIndex ]
aheadEnd = loop[ ( pointIndex + 1 ) % len( loop ) ]
aheadMidpoint = 0.5 * ( ahead + aheadEnd )
normalizedSegment = behind - behindMidpoint
normalizedSegmentLength = abs( normalizedSegment )
if normalizedSegmentLength > 0.0:
normalizedSegment /= normalizedSegmentLength
segmentYMirror = complex( normalizedSegment.real, - normalizedSegment.imag )
behindRotated = segmentYMirror * behind
behindMidpointRotated = segmentYMirror * behindMidpoint
aheadRotated = segmentYMirror * ahead
aheadMidpointRotated = segmentYMirror * aheadMidpoint
y = behindRotated.imag
isYAboveFirst = y > aheadRotated.imag
isYAboveSecond = y > aheadMidpointRotated.imag
if isYAboveFirst != isYAboveSecond:
xIntersection = euclidean.getXIntersection( aheadRotated, aheadMidpointRotated, y )
if xIntersection > min( behindMidpointRotated.real, behindRotated.real ) and xIntersection < max( behindMidpointRotated.real, behindRotated.real ):
intersectionPoint = normalizedSegment * complex( xIntersection, y )
loop[ ( pointIndex + len( loop ) - 1 ) % len( loop ) ] = intersectionPoint
del loop[ pointIndex ]
return
class BoundingLoop:
"A class to hold a bounding loop composed of a minimum complex, a maximum complex and an outset loop."
def __eq__( self, other ):
"Determine whether this bounding loop is identical to other one."
if other == None:
return False
return self.minimum == other.minimum and self.maximum == other.maximum and self.loop == other.loop
def __repr__( self ):
"Get the string representation of this bounding loop."
return '%s, %s, %s' % ( self.minimum, self.maximum, self.loop )
def getFromLoop( self, loop ):
"Get the bounding loop from a path."
self.loop = loop
self.maximum = euclidean.getMaximumFromPoints( loop )
self.minimum = euclidean.getMinimumFromPoints( loop )
return self
def getOutsetBoundingLoop( self, outsetDistance ):
"Outset the bounding rectangle and loop by a distance."
outsetBoundingLoop = BoundingLoop()
outsetBoundingLoop.maximum = self.maximum + complex( outsetDistance, outsetDistance )
outsetBoundingLoop.minimum = self.minimum - complex( outsetDistance, outsetDistance )
greaterThanOutsetDistance = 1.1 * outsetDistance
centers = getCentersFromLoopDirection( True, self.loop, greaterThanOutsetDistance )
outsetBoundingLoop.loop = getSimplifiedInsetFromClockwiseLoop( centers[ 0 ], outsetDistance )
return outsetBoundingLoop
def isEntirelyInsideAnother( self, anotherBoundingLoop ):
"Determine if this bounding loop is entirely inside another bounding loop."
if self.minimum.imag < anotherBoundingLoop.minimum.imag or self.minimum.real < anotherBoundingLoop.minimum.real:
return False
if self.maximum.imag > anotherBoundingLoop.maximum.imag or self.maximum.real > anotherBoundingLoop.maximum.real:
return False
for point in self.loop:
if euclidean.getNumberOfIntersectionsToLeft( anotherBoundingLoop.loop, point ) % 2 == 0:
return False
return not isLoopIntersectingLoop( anotherBoundingLoop.loop, self.loop ) #later check for intersection on only acute angles
def isOverlappingAnother( self, anotherBoundingLoop ):
"Determine if this bounding loop is intersecting another bounding loop."
if self.isRectangleMissingAnother( anotherBoundingLoop ):
return False
for point in self.loop:
if euclidean.getNumberOfIntersectionsToLeft( anotherBoundingLoop.loop, point ) % 2 == 1:
return True
for point in anotherBoundingLoop.loop:
if euclidean.getNumberOfIntersectionsToLeft( self.loop, point ) % 2 == 1:
return True
return isLoopIntersectingLoop( anotherBoundingLoop.loop, self.loop ) #later check for intersection on only acute angles
def isOverlappingAnotherInList( self, boundingLoops ):
"Determine if this bounding loop is intersecting another bounding loop in a list."
for boundingLoop in boundingLoops:
if self.isOverlappingAnother( boundingLoop ):
return True
return False
def isRectangleMissingAnother( self, anotherBoundingLoop ):
"Determine if the rectangle of this bounding loop is missing the rectangle of another bounding loop."
if self.maximum.imag < anotherBoundingLoop.minimum.imag or self.maximum.real < anotherBoundingLoop.minimum.real:
return True
return self.minimum.imag > anotherBoundingLoop.maximum.imag or self.minimum.real > anotherBoundingLoop.maximum.real
class CircleIntersection:
"An intersection of two complex circles."
def __init__( self, circleNodeAhead, index, circleNodeBehind ):
self.aheadMinusBehind = 0.5 * ( circleNodeAhead.circle - circleNodeBehind.circle )
self.circleNodeAhead = circleNodeAhead
self.circleNodeBehind = circleNodeBehind
self.index = index
self.steppedOn = False
demichordWidth = math.sqrt( 1.0 - self.aheadMinusBehind.real * self.aheadMinusBehind.real - self.aheadMinusBehind.imag * self.aheadMinusBehind.imag )
rotatedClockwiseQuarter = complex( self.aheadMinusBehind.imag, - self.aheadMinusBehind.real )
rotatedClockwiseQuarterLength = abs( rotatedClockwiseQuarter )
if rotatedClockwiseQuarterLength == 0:
print( 'this should never happen, rotatedClockwiseQuarter in getDemichord in intercircle is 0' )
print( circleNodeAhead.circle )
print( circleNodeBehind.circle )
self.demichord = 0.0
else:
self.demichord = rotatedClockwiseQuarter * demichordWidth / rotatedClockwiseQuarterLength
self.positionRelativeToBehind = self.aheadMinusBehind + self.demichord
def __repr__( self ):
"Get the string representation of this CircleIntersection."
return '%s, %s, %s, %s, %s' % ( self.index, self.getAbsolutePosition(), self.circleNodeBehind.index, self.circleNodeAhead.index, self.getCircleIntersectionAhead().index )
def addToList( self, circleIntersectionPath ):
"Add this to the circle intersection path, setting stepped on to be true."
self.steppedOn = True
circleIntersectionPath.append( self )
def getAbsolutePosition( self ):
"Get the absolute position."
return self.positionRelativeToBehind + self.circleNodeBehind.circle
def getCircleIntersectionAhead( self ):
"Get the first circle intersection on the circle node ahead."
circleIntersections = self.circleNodeAhead.circleIntersections
circleIntersectionAhead = None
largestDot = - 999999999.0
for circleIntersection in circleIntersections:
if not circleIntersection.steppedOn:
circleIntersectionRelativeToMidpoint = euclidean.getNormalized( circleIntersection.positionRelativeToBehind + self.aheadMinusBehind )
dot = euclidean.getDotProduct( self.demichord, circleIntersectionRelativeToMidpoint )
if dot > largestDot:
largestDot = dot
circleIntersectionAhead = circleIntersection
if circleIntersectionAhead == None:
print( 'this should never happen, circleIntersectionAhead in intercircle is None' )
print( self.circleNodeAhead.circle )
for circleIntersection in circleIntersections:
print( circleIntersection.circleNodeAhead.circle )
return circleIntersectionAhead
def isWithinCircles( self, pixelTable ):
"Determine if this circle intersection is within the circle node circles."
absolutePosition = self.getAbsolutePosition()
squareValues = euclidean.getSquareValuesFromPoint( pixelTable, absolutePosition )
for squareValue in squareValues:
if abs( squareValue.circle - absolutePosition ) < 1.0:
if squareValue != self.circleNodeAhead and squareValue != self.circleNodeBehind:
return True
return False
class CircleNode:
"A complex node of complex circle intersections."
def __init__( self, circle, index ):
self.circle = circle
self.circleIntersections = []
self.index = index
def __repr__( self ):
"Get the string representation of this CircleNode."
return '%s, %s' % ( self.index, self.circle )
def getWithinNodes( self, pixelTable ):
"Get the nodes this circle node is within."
withinNodes = []
squareValues = euclidean.getSquareValuesFromPoint( pixelTable, 0.5 * self.circle )
for squareValue in squareValues:
if abs( self.circle - squareValue.circle ) < 2.0:
withinNodes.append( squareValue )
return withinNodes
|