Source: utilities/getDimensions.js

import { Configuration } from '../configurator/configuration.js';
import { Block } from '../package/block/block.js';

import {
    Box3,
    Vector3,
    Box3Helper,
    Quaternion,
	Group
} from '../../node_modules/three/build/three.module.js';


//DE CREATE BOUNDING BOXES STAAT NU NOG DUBBEL!! HIER VERWIJDEREN.

/**
 * @param {Configuration} configuration - source object
 */

function getDimensions( configuration ){

	var configContent = configuration.content.main.medium

	//test log
	//console.log( configuration )
	// console.log( configuration.blockInstances )
	//console.log( configContent )
	// console.log( configContent.children[0].getWorldPosition(vec) )
	// console.log( configContent.children[0].getWorldQuaternion(quat) )

	//CREATE BOUNDINGBOXES ------------------------------

	// var vec = new Vector3
	// var quat = new Quaternion

	// //group to create box3 for the entire configuration
	// var dimGroup = new Group()

	// //array for all created Box3 of blocks
	// var allBoxes =[ ]

	// //check for block or configuration
	// if ( configuration instanceof Block ){

	// 	configContent = configContent.block
		
	// 	var boundingBox = configuration.boundingBox.clone()  

	// 	//get world position & rotation
	// 	boundingBox.position.copy( configContent.getWorldPosition(vec) )        //apply world position
	// 	boundingBox.position.add( configuration.boundingBox.position )     		//apply origin
	// 	boundingBox.quaternion.copy( configContent.getWorldQuaternion(quat) )   //apply world rotation
	// 	dimGroup.add( boundingBox )
	// 	var box = new Box3().setFromObject( boundingBox )
	// 	allBoxes.push(box)
	// }
	// else if ( configuration instanceof Configuration ){

	// 	//loop through all blockinstances and create Box3
	// 	for( var i = 0; i<configuration.blockInstances.length; i++ ){

	// 		//get the origial block boudingbox
	// 		var boundingBox = configuration.blockInstances[i].block.boundingBox.clone()                //deze nog checken op id origin?

	// 		//get world position & rotation
	// 		boundingBox.position.copy( configContent.children[i].getWorldPosition(vec) )        //apply world position
	// 		boundingBox.position.add( configuration.blockInstances[i].block.boundingBox.position )     //apply origin
	// 		boundingBox.quaternion.copy( configContent.children[i].getWorldQuaternion(quat) )   //apply world rotation
	// 		dimGroup.add( boundingBox )
	// 		var box = new Box3().setFromObject( boundingBox )
	// 		allBoxes.push(box)

	// 		//add boxHelper                                    
	// 	};

	// };


	//var boxGroup = new Box3().setFromObject( dimGroup )
	//console.log( boxGroup.min, boxGroup.max ) 


	//GET TOTAL DIMENSIONS -----------------------------------

	var vec = new Vector3
	var totalCenter = configuration.boundingBox.boundingBox.getCenter( vec ) 
	//console.log( totalCenter )

	var totalBase = new Vector3( totalCenter.x, 0, totalCenter.z )
	//console.log( totalBase )
	
	var totalMin = configuration.boundingBox.boundingBox.min
	var totalMax = configuration.boundingBox.boundingBox.max

	//console.log( totalMin )
	//console.log( totalMax  )
	
	var totalSize = configuration.boundingBox.boundingBox.getSize( vec )
	//console.log( totalSize  )

	var totalWidth =    Math.round( configuration.boundingBox.boundingBox.getSize( vec ).x * 100) /// 100;  
	var totalHeight =   Math.round( configuration.boundingBox.boundingBox.getSize( vec ).y * 100) // 100;      
	var totalDepth =    Math.round( configuration.boundingBox.boundingBox.getSize( vec ).z * 100) // 100; 

	//console.log( totalWidth + "cm" )
	//console.log( totalHeight + "cm" )
	//console.log( totalDepth + "cm" )


	// //GET SUB DIMENSIONS -------------------------------------

	// //create intersection boxes at all 6 sides

	// //base vectors
	// var vecMin = new Vector3(-0.1, -0.1, -0.1)
	// var vecMax = new Vector3(0.1, 0.1, 0.1)

	// //left 
	// var leftMin = totalMin.clone().add(vecMin) 
	// var leftMax = new Vector3( totalMin.x, totalMax.y, totalMax.z ).add( vecMax )
	// var leftSectionBox = new Box3( leftMin, leftMax )

	// //front 
	// var frontMin = new Vector3( totalMin.x, totalMin.y, totalMax.z ).add( vecMin ) 
	// var frontMax = new Vector3( totalMax.x, totalMax.y, totalMax.z ).add( vecMax )
	// var frontSectionBox = new Box3( frontMin, frontMax )

	// //right
	// var rightMin = new Vector3( totalMax.x, totalMin.y, totalMin.z ).add( vecMin ) 
	// var rightMax = new Vector3( totalMax.x, totalMax.y, totalMax.z ).add( vecMax )
	// var rightSectionBox = new Box3( rightMin, rightMax )

	// //back
	// var backMin = new Vector3( totalMin.x, totalMin.y, totalMax.z ).add( vecMin ) 
	// var backMax = new Vector3( totalMax.x, totalMax.y, totalMax.z ).add( vecMax )
	// var backSectionBox = new Box3( backMin, backMax )

	// //get base points from specified section per side (left, front, right, back )

	// function getBasePoints( allBoxes, sectionBox, side ){
		
	// 	var intersectedBoxes = []
	// 	//get intersected boxes on the left
	// 	for (var i = 0; i<allBoxes.length; i++ ){
	// 		if ( sectionBox.intersectsBox( allBoxes[i]) ){
	// 			intersectedBoxes.push( allBoxes[i]  )
	// 		}
	// 	}
	
	// 	var basePoints = []

	// 	//totalMin + the z points on the left
	// 	for (var i = 0; i<intersectedBoxes.length; i++ ){
	// 		switch( side ){
	// 			case "left": 
	// 				basePoints.push( {
	// 					start:	new Vector3( totalMin.x, totalMin.y, intersectedBoxes[i].min.z ),
	// 					end:	new Vector3( totalMin.x, totalMin.y, intersectedBoxes[i].max.z ) 
	// 				} )
	// 				break;
	// 			case "front":
	// 				basePoints.push( {
	// 					start: 	new Vector3( intersectedBoxes[i].min.x, totalMin.y, totalMax.z ),
	// 					end: 	new Vector3( intersectedBoxes[i].max.x, totalMin.y,  totalMax.z ) 
	// 				});
	// 				break;  
	// 			case "right":
	// 				basePoints.push( {
	// 					start: 	new Vector3( totalMax.x, totalMin.y, intersectedBoxes[i].min.z ),
	// 					end:	new Vector3( totalMax.x, totalMin.y, intersectedBoxes[i].max.z ) 
	// 				})
	// 				break;       
	// 			case "back":
	// 				basePoints.push( {
	// 					start: 	new Vector3( intersectedBoxes[i].min.x, totalMin.y, totalMin.z ),
	// 					end:	new Vector3( intersectedBoxes[i].max.x, totalMin.y,  totalMin.z )
	// 				})
	// 				break;    
	// 		}
	// 	}

	// 	return basePoints;

	// }

	//POINTS FOR DIM LINES
	var dimLeft = [{
		start:  new Vector3( totalMin.x, totalMin.y, totalMax.z),
		end: new Vector3( totalMin.x, totalMin.y, totalMin.z) 
	}];

	var dimBack = [{
		start:  new Vector3( totalMin.x, totalMin.y, totalMin.z),
		end: new Vector3( totalMax.x, totalMin.y, totalMin.z)  
	}];

	var dimRight = [{
		start:  new Vector3( totalMax.x, totalMin.y, totalMin.z),
		end: new Vector3( totalMax.x, totalMin.y, totalMax.z) 
	}];

	var dimFront = [{
		start:  new Vector3( totalMin.x, totalMin.y, totalMax.z),
		end:  new Vector3( totalMax.x, totalMin.y, totalMax.z) 
	}];
	
	var dimHeight = [{
		start: new Vector3( totalMin.x, totalMin.y, totalMin.z),
		end: new Vector3( totalMin.x, totalMax.y, totalMin.z) 
	}];

	// //POINTS FOR SUB DIM LINES
	// var dimPointsLeft = getBasePoints( allBoxes, leftSectionBox, "left" )
	// var dimPointsFront = getBasePoints( allBoxes, frontSectionBox, "front" )
	// var dimPointsRight = getBasePoints( allBoxes, rightSectionBox, "right" )
	// var dimPointsBack = getBasePoints( allBoxes, backSectionBox, "back" )


	return {

		center: totalCenter,

		base: totalBase,
		min: totalMin,
		max: totalMax,

		width: totalWidth,
		height: totalHeight,
		depth: totalDepth,

		dimLeft: dimLeft,		// dimension points
		dimBack: dimBack,
		dimRight: dimRight,
		dimFront: dimFront,
		dimHeight: dimHeight,
		// dimPointsLeft: dimPointsLeft,
		// dimPointsFront: dimPointsFront,
		// dimPointsRight: dimPointsRight,
		// dimPointsBack: dimPointsBack,
	}

}


export{ getDimensions }