Sizecalculator Documentation
Size Calculator Controller Documentation
File: /controllers/sizecalculator.php
Purpose: Mathematical tool for calculating optimal piece cutting from boards/sheets
Last Updated: December 21, 2024
Total Functions: 1
Lines of Code: ~94
---
๐ Overview
The Size Calculator Controller is a specialized mathematical utility tool designed for manufacturing and cutting optimization. It calculates the maximum number of pieces that can be cut from a larger board or sheet material by:
- โข Testing both orientations (normal and rotated)
- โข Calculating optimal cutting patterns
- โข Handling remainder space utilization
- โข Providing visual result comparison
- โข Optimizing material usage efficiency
This tool is particularly useful for:
- โข Woodworking and carpentry shops
- โข Sheet metal fabrication
- โข Glass cutting operations
- โข Textile pattern cutting
- โข Any manufacturing requiring efficient material utilization
Primary Functions
- โ Calculate pieces per board in normal orientation
- โ Calculate pieces per board in rotated orientation
- โ Determine optimal orientation automatically
- โ Handle remainder space optimization
- โ Provide detailed cutting analysis
- โ Display results with visual representation
Related Controllers
- โข productionController.php - Production planning
- โข materialController.php - Material management
- โข cuttingController.php - Cutting operations
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns |
|---|---|---|
| No database operations | This is a pure calculation tool | N/A |
| Table Name | Purpose | Key Columns |
|---|---|---|
| None | Mathematical utility only | N/A |
๐ Key Functions
1. Default Action - Calculator Form Display
Location: Line 38
Purpose: Display size calculation input form
Process Flow:
1. Include authentication check
2. Display calculator input template
3. Set up form fields for board and piece dimensions
Template: sizecalculatorview/calc.html
---
2. calc - Perform Size Calculations
Location: Line 43
Purpose: Calculate optimal cutting patterns and display results
Function Signature:
// Parameters from POST
$boardDim1 = (float) filter_input(INPUT_POST, "boardDim1"); // Board length
$boardDim2 = (float) filter_input(INPUT_POST, "boardDim2"); // Board width
$pieceDim1 = (float) filter_input(INPUT_POST, "pieceDim1"); // Piece length
$pieceDim2 = (float) filter_input(INPUT_POST, "pieceDim2"); // Piece width
Process Flow:
1. Extract and validate input dimensions
2. Calculate pieces using normal orientation: calcSize(boardDim1, boardDim2, pieceDim1, pieceDim2)
3. Calculate pieces using rotated orientation: calcSize(boardDim1, boardDim2, pieceDim2, pieceDim1)
4. Determine optimal orientation (highest piece count)
5. Assign results to template variables
6. Display results page
Calculation Logic:
$r1 = calcSize($boardDim1, $boardDim2, $pieceDim1, $pieceDim2); // Normal
$r2 = calcSize($boardDim1, $boardDim2, $pieceDim2, $pieceDim1); // Rotated
$best = $r1;
if ($r2 > $r1) {
$best = $r2; // Choose orientation with more pieces
}
---
3. calcSize() - Core Calculation Algorithm
Location: Line 80
Purpose: Calculate maximum pieces that fit in given board dimensions
Function Signature:
function calcSize($a1, $b1, $a2, $b2) {
// $a1, $b1 = board dimensions (length, width)
// $a2, $b2 = piece dimensions (length, width)
}
Algorithm Implementation:
function calcSize($a1, $b1, $a2, $b2) {
// Primary cutting pattern
$a4 = floor($a1 / $a2); // How many pieces fit along board length
$b4 = floor($b1 / $b2); // How many pieces fit along board width
$c4 = $a4 * $b4; // Total pieces in primary pattern
// Remainder space utilization
$b5 = $b1 - ($b4 * $b2); // Remaining width after primary cuts
$a8 = floor($a1 / $b2); // Pieces in remainder (rotated)
$b8 = floor($b5 / $a2); // Pieces along remainder width
$c8 = $a8 * $b8; // Additional pieces from remainder
return ($c4 + $c8); // Total optimized pieces
}
---
๐ Workflows
Workflow 1: Size Calculation Process
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default | Display size calculator input form | |
| `do=calc` | `calcSize()` | Process calculations and show results | |
| `do=sucess` | Success page | Display operation success message | |
| `do=error` | Error page | Display operation error message |
Calculate (do=calc):
- โข
boardDim1- Board length dimension (float) - โข
boardDim2- Board width dimension (float) - โข
pieceDim1- Piece length dimension (float) - โข
pieceDim2- Piece width dimension (float)
Template Variables Assigned
Results Page:
- โข
$dim1, $dim2- Normal orientation piece dimensions - โข
$r1- Pieces count in normal orientation - โข
$dim11, $dim22- Rotated orientation piece dimensions - โข
$r2- Pieces count in rotated orientation - โข
$best- Maximum pieces achievable (optimal choice)
---
๐งฎ Calculation Methods
Primary Cutting Pattern Algorithm
// Calculate how many pieces fit along each dimension
$a4 = floor($a1 / $a2); // Pieces along board length
$b4 = floor($b1 / $b2); // Pieces along board width
$c4 = $a4 * $b4; // Total pieces in regular grid
Remainder Space Optimization
// Calculate remaining space after primary cuts
$b5 = $b1 - ($b4 * $b2); // Unused width
// Try to fit rotated pieces in remainder space
$a8 = floor($a1 / $b2); // Rotated pieces along length
$b8 = floor($b5 / $a2); // Rotated pieces in remainder width
$c8 = $a8 * $b8; // Additional pieces from remainder
// Total optimized cutting
return ($c4 + $c8);
Orientation Comparison
$r1 = calcSize($boardDim1, $boardDim2, $pieceDim1, $pieceDim2); // Normal
$r2 = calcSize($boardDim1, $boardDim2, $pieceDim2, $pieceDim1); // Rotated
$best = ($r2 > $r1) ? $r2 : $r1; // Optimal orientation
Example Calculation
Board: 120 cm ร 80 cm
Piece: 30 cm ร 25 cm
Normal Orientation:
- Primary: floor(120/30) ร floor(80/25) = 4 ร 3 = 12 pieces
- Remainder: 80 - (3ร25) = 5 cm remaining width
- Additional: floor(120/25) ร floor(5/30) = 4 ร 0 = 0 pieces
- Total R1: 12 + 0 = 12 pieces
Rotated Orientation (25ร30):
- Primary: floor(120/25) ร floor(80/30) = 4 ร 2 = 8 pieces
- Remainder: 80 - (2ร30) = 20 cm remaining width
- Additional: floor(120/30) ร floor(20/25) = 4 ร 0 = 0 pieces
- Total R2: 8 + 0 = 8 pieces
Best: 12 pieces (normal orientation)
---
๐ Security & Permissions
Authentication Requirements
- โข All actions require authentication via
include_once("../public/authentication.php") - โข Session-based access control
Input Validation
// Proper input filtering and type casting
$boardDim1 = (float) filter_input(INPUT_POST, "boardDim1");
$boardDim2 = (float) filter_input(INPUT_POST, "boardDim2");
$pieceDim1 = (float) filter_input(INPUT_POST, "pieceDim1");
$pieceDim2 = (float) filter_input(INPUT_POST, "pieceDim2");
Mathematical Validation
- โข Uses
floor()function for integer piece counts - โข Prevents division by zero through positive dimension requirements
- โข Handles floating-point precision appropriately
---
๐ Performance Considerations
Calculation Efficiency
1. Algorithm Complexity: O(1) - Constant time calculations
2. Memory Usage: Minimal - only stores dimension variables
3. Processing Speed: Very fast - simple mathematical operations
Optimization Potential
// Current implementation is already optimized for simplicity
// Could be enhanced with:
// 1. Multiple cutting pattern testing
// 2. Waste percentage calculations
// 3. Cost optimization factors
// 4. Material grain direction considerations
Scalability
- โข Pure mathematical tool - no database overhead
- โข Can handle very large dimension values
- โข Suitable for high-frequency calculations
---
๐ Common Issues & Troubleshooting
1. Division by Zero
Issue: Application crash when piece dimensions are 0
Cause: Missing input validation for zero values
Prevention:
if ($pieceDim1 <= 0 || $pieceDim2 <= 0 || $boardDim1 <= 0 || $boardDim2 <= 0) {
throw new Exception("All dimensions must be positive numbers");
}
2. Piece Larger Than Board
Issue: Calculations return 0 when piece exceeds board size
Cause: Normal mathematical behavior but may confuse users
Enhancement:
if ($pieceDim1 > $boardDim1 && $pieceDim2 > $boardDim2) {
$message = "Piece dimensions exceed board size in both orientations";
}
3. Floating Point Precision
Issue: Unexpected results with very small decimal differences
Cause: Floating point arithmetic precision
Fix:
// Round dimensions to reasonable precision
$boardDim1 = round((float) filter_input(INPUT_POST, "boardDim1"), 2);
$boardDim2 = round((float) filter_input(INPUT_POST, "boardDim2"), 2);
4. Template Display Issues
Issue: Results not showing properly
Cause: Template variable assignment errors
Debug:
// Debug template assignments
echo "R1: $r1, R2: $r2, Best: $best";
$smarty->assign("debug_values", compact('r1', 'r2', 'best'));
---
๐งช Testing Scenarios
Test Case 1: Perfect Fit Scenario
Board: 100 ร 60
Piece: 20 ร 15
Expected: 5 ร 4 = 20 pieces (no waste)
Verify: Both orientations should be tested
Test Case 2: Optimization Required
Board: 120 ร 80
Piece: 35 ร 25
Normal: 3 ร 3 = 9 pieces
Rotated: 4 ร 2 = 8 pieces
Expected: Best = 9 (normal orientation)
Test Case 3: Remainder Space Utilization
Board: 100 ร 50
Piece: 30 ร 20
Primary: 3 ร 2 = 6 pieces
Remainder: 50 - (2ร20) = 10 cm
Additional: floor(100/20) ร floor(10/30) = 5 ร 0 = 0
Test both orientations and remainder calculations
Test Case 4: Edge Cases
- Equal dimensions (square pieces on square boards)
- Piece larger than board in one dimension
- Very small decimal dimensions
- Large dimension values (stress test)
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข Manufacturing Optimization - Production planning tools
- โข Mathematical Functions - PHP mathematical operations
- โข Template System - Smarty template documentation
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When manufacturing optimization requirements change