<commit_before>"
A layout that packs rectangles in as amall space as possible (at least it tries to)

Instance Variables
	padding:	<Number>
	freeSpace:	<Set of Rectamgles>
	bounds:	<Point>

padding
	- padding around each element

freeSpace
	- collection of spaces that can be used for next element insertion
	
bounds
	- bounding box of all currently processed elements

"
Class {
	#name : #RTRectanglePackLayout,
	#superclass : #RTLayout,
	#instVars : [
		'freeSpace',
		'padding',
		'bounds'
	],
	#category : #'Roassal2-Layouts'
}

{ #category : #'space-calculation' }
RTRectanglePackLayout >> bestSpaceFor: anElement [
	| elementsPadedBox suitableSpaces |
	
	elementsPadedBox := anElement encompassingRectangle extendBy: padding * 2.
	
	suitableSpaces := freeSpace select: [ :space |
		elementsPadedBox width <= space width and:
			[ elementsPadedBox height <= space height ] ].
	
	^ suitableSpaces
		fold: [ :best :current |
			| bestExt currentExt | 
			((currentExt := self extentByInserting: elementsPadedBox into: current) <
			(bestExt := self extentByInserting: elementsPadedBox  into: best) or: [ 
				currentExt = bestExt and: [
					current halfPerimeter < best halfPerimeter ] ])
						ifTrue: [ current ]
						ifFalse: [ best ] ]
]

{ #category : #accessing }
RTRectanglePackLayout >> boundingBox [
	^ bounds
]

{ #category : #hook }
RTRectanglePackLayout >> doExecute: elements [
	| sortedElements |
	bounds := 0@0.
		
	freeSpace := Set new.
	freeSpace add: (0@0 corner: 
		(elements inject: 0@0 into: [ :sum :el |
			sum + el extent + (padding * 2) ])).
	
	sortedElements := SortedCollection sortBlock:  [ :larger :smaller |
		larger  encompassingRectangle halfPerimeter >
		smaller encompassingRectangle halfPerimeter ].
	sortedElements addAll: elements.
	
	sortedElements do: [ :element |
		self packIn: element ]
]

{ #category : #'space-calculation' }
RTRectanglePackLayout >> extentByInserting: aRectangle into: aSpace [
	| futureCorner |<commit_msg>Added progress bar to rectangle packing layout<commit_after>"
A layout that packs rectangles in as amall space as possible (at least it tries to)

Instance Variables
	padding:	<Number>
	freeSpace:	<Set of Rectamgles>
	bounds:	<Point>

padding
	- padding around each element

freeSpace
	- collection of spaces that can be used for next element insertion
	
bounds
	- bounding box of all currently processed elements

"
Class {
	#name : #RTRectanglePackLayout,
	#superclass : #RTLayout,
	#instVars : [
		'freeSpace',
		'padding',
		'bounds'
	],
	#category : #'Roassal2-Layouts'
}

{ #category : #'space-calculation' }
RTRectanglePackLayout >> bestSpaceFor: anElement [
	| elementsPadedBox suitableSpaces |
	
	elementsPadedBox := anElement encompassingRectangle extendBy: padding * 2.
	
	suitableSpaces := freeSpace select: [ :space |
		elementsPadedBox width <= space width and:
			[ elementsPadedBox height <= space height ] ].
	
	^ suitableSpaces
		fold: [ :best :current |
			| bestExt currentExt | 
			((currentExt := self extentByInserting: elementsPadedBox into: current) <
			(bestExt := self extentByInserting: elementsPadedBox  into: best) or: [ 
				currentExt = bestExt and: [
					current halfPerimeter < best halfPerimeter ] ])
						ifTrue: [ current ]
						ifFalse: [ best ] ]
]

{ #category : #accessing }
RTRectanglePackLayout >> boundingBox [
	^ bounds
]

{ #category : #hook }
RTRectanglePackLayout >> doExecute: elements [
	| sortedElements |
	bounds := 0@0.
		
	freeSpace := Set new.
	freeSpace add: (0@0 corner: 
		(elements inject: 0@0 into: [ :sum :el |
			sum + el extent + (padding * 2) ])).
	
	sortedElements := SortedCollection sortBlock:  [ :larger :smaller |
		larger  encompassingRectangle halfPerimeter >
		smaller encompassingRectangle halfPerimeter ].
	sortedElements addAll: elements.
	
	sortedElements
		do: [ :element | self packIn: element ]
		displayingProgress: 'Laying out elements'
]

{ #category : #'space-calculation' }
RTRectanglePackLayout >> extentByInserting: aRectangle into: aSpace [
	| futureCorner |
{'fraction_non_alphanumeric': 0.10221337975627953, 'fraction_numerical': 0.004476498383486694, 'mean_word_length': 4.149807938540333, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7139830', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1272, 'n_words': 366}