Merge pull request #649 from teyrana/641-fix-cg
[fixes #641] MassCalculator now accounts for rotations in the tree
This commit is contained in:
		
						commit
						5e264c8604
					
				@ -54,7 +54,7 @@ public class MassCalculation {
 | 
				
			|||||||
	// =========== Instance Functions ========================
 | 
						// =========== Instance Functions ========================
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	public void merge( final MassCalculation other ) {
 | 
						public void merge( final MassCalculation other ) {
 | 
				
			||||||
		if( 0 < other.getMass()) {
 | 
							if( MIN_MASS < other.getMass()) {
 | 
				
			||||||
			// Adjust Center-of-mass
 | 
								// Adjust Center-of-mass
 | 
				
			||||||
			this.addMass( other.getCM() );
 | 
								this.addMass( other.getCM() );
 | 
				
			||||||
			this.bodies.addAll( other.bodies );
 | 
								this.bodies.addAll( other.bodies );
 | 
				
			||||||
@ -66,7 +66,7 @@ public class MassCalculation {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	public void addMass( final Coordinate pointMass ) {
 | 
						public void addMass( final Coordinate pointMass ) {
 | 
				
			||||||
		if( 0 == this.centerOfMass.weight ){
 | 
							if( MIN_MASS > this.centerOfMass.weight ){
 | 
				
			||||||
		    this.centerOfMass = pointMass;
 | 
							    this.centerOfMass = pointMass;
 | 
				
			||||||
		}else {
 | 
							}else {
 | 
				
			||||||
			this.centerOfMass = this.centerOfMass.average( pointMass);
 | 
								this.centerOfMass = this.centerOfMass.average( pointMass);
 | 
				
			||||||
@ -266,19 +266,26 @@ public class MassCalculation {
 | 
				
			|||||||
		final RocketComponent component = this.root;
 | 
							final RocketComponent component = this.root;
 | 
				
			||||||
		final Transformation parentTransform = this.transform;
 | 
							final Transformation parentTransform = this.transform;
 | 
				
			||||||
		final int instanceCount = component.getInstanceCount();
 | 
							final int instanceCount = component.getInstanceCount();
 | 
				
			||||||
		Coordinate[] instanceLocations = component.getInstanceLocations();
 | 
							final Coordinate[] allInstanceOffsets = component.getInstanceLocations();
 | 
				
			||||||
		
 | 
							final double[] allInstanceAngles = component.getInstanceAngles();
 | 
				
			||||||
//		// vvv DEBUG
 | 
					
 | 
				
			||||||
//		if( this.config.isComponentActive(component) ){
 | 
							// // vvv DEBUG
 | 
				
			||||||
//			System.err.println(String.format( "%s[%s]....", prefix, component.getName()));
 | 
							// if( this.config.isComponentActive(component) ){
 | 
				
			||||||
//		}
 | 
							// 	System.err.println(String.format( "%s>>[%s]....", prefix, component.getName()));
 | 
				
			||||||
 | 
							// }
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		// iterate over the aggregated instances for the whole tree.
 | 
							// iterate over the aggregated instances for the whole tree.
 | 
				
			||||||
		MassCalculation children = this.copy(component, parentTransform );
 | 
							MassCalculation children = this.copy(component, parentTransform );
 | 
				
			||||||
		for( int instanceNumber = 0; instanceNumber < instanceCount; ++instanceNumber) {
 | 
							for( int currentInstanceNumber = 0; currentInstanceNumber < instanceCount; ++currentInstanceNumber) {
 | 
				
			||||||
			Coordinate currentLocation = instanceLocations[instanceNumber];
 | 
								final Coordinate currentInstanceOffset = allInstanceOffsets[currentInstanceNumber];
 | 
				
			||||||
			Transformation currentTransform = parentTransform.applyTransformation( Transformation.getTranslationTransform( currentLocation ));
 | 
								final Transformation offsetTransform = Transformation.getTranslationTransform( currentInstanceOffset );
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
 | 
								final double currentInstanceAngle = allInstanceAngles[currentInstanceNumber];
 | 
				
			||||||
 | 
								final Transformation angleTransform = Transformation.getAxialRotation(currentInstanceAngle);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								final Transformation currentTransform = parentTransform.applyTransformation(offsetTransform)
 | 
				
			||||||
 | 
																			.applyTransformation(angleTransform);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			for (RocketComponent child : component.getChildren()) {
 | 
								for (RocketComponent child : component.getChildren()) {
 | 
				
			||||||
				// child data, relative to rocket reference frame
 | 
									// child data, relative to rocket reference frame
 | 
				
			||||||
				MassCalculation eachChild = copy( child, currentTransform);
 | 
									MassCalculation eachChild = copy( child, currentTransform);
 | 
				
			||||||
@ -291,10 +298,10 @@ public class MassCalculation {
 | 
				
			|||||||
			}			
 | 
								}			
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		if( 0 < children.getMass() ) {
 | 
							if( MIN_MASS < children.getMass() ) {
 | 
				
			||||||
			this.merge( children );
 | 
								this.merge( children );
 | 
				
			||||||
//		// vvv DEBUG			
 | 
								// // vvv DEBUG
 | 
				
			||||||
//			System.err.println(String.format( "%s....assembly mass (incl/children):  %s", prefix, this.toCMDebug()));
 | 
								// System.err.println(String.format( "%s....assembly mass (incl/children):  %s", prefix, this.toCMDebug()));
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		if (this.config.isComponentActive(component) ){
 | 
							if (this.config.isComponentActive(component) ){
 | 
				
			||||||
@ -302,8 +309,9 @@ public class MassCalculation {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			if (!component.getOverrideSubcomponents()) {
 | 
								if (!component.getOverrideSubcomponents()) {
 | 
				
			||||||
				if (component.isMassOverridden()) {
 | 
									if (component.isMassOverridden()) {
 | 
				
			||||||
//					System.err.println("mass override=" + component.getOverrideMass());
 | 
					 | 
				
			||||||
					compCM = compCM.setWeight(MathUtil.max(component.getOverrideMass(), MIN_MASS));
 | 
										compCM = compCM.setWeight(MathUtil.max(component.getOverrideMass(), MIN_MASS));
 | 
				
			||||||
 | 
										// // vvv DEBUG
 | 
				
			||||||
 | 
										// System.err.println(String.format( "%s....mass overridden to:  %s", prefix, compCM.toPreciseString()));
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				if (component.isCGOverridden())
 | 
									if (component.isCGOverridden())
 | 
				
			||||||
					compCM = compCM.setXYZ(component.getOverrideCG());
 | 
										compCM = compCM.setXYZ(component.getOverrideCG());
 | 
				
			||||||
@ -318,10 +326,10 @@ public class MassCalculation {
 | 
				
			|||||||
			RigidBody componentInertia = new RigidBody( compCM, compIx, compIt, compIt );
 | 
								RigidBody componentInertia = new RigidBody( compCM, compIx, compIt, compIt );
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
			this.addInertia( componentInertia );
 | 
								this.addInertia( componentInertia );
 | 
				
			||||||
// vvv DEBUG			
 | 
								// // vvv DEBUG
 | 
				
			||||||
//			if( 0 < compCM.weight ) {
 | 
								// if( 0 < compCM.weight ) {
 | 
				
			||||||
//				System.err.println(String.format( "%s....componentData:            %s", prefix, compCM.toPreciseString() ));
 | 
								// 	System.err.println(String.format( "%s....componentData:            %s", prefix, compCM.toPreciseString() ));
 | 
				
			||||||
//			}
 | 
								// }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if (component.getOverrideSubcomponents()) {				
 | 
								if (component.getOverrideSubcomponents()) {				
 | 
				
			||||||
				if (component.isMassOverridden()) {
 | 
									if (component.isMassOverridden()) {
 | 
				
			||||||
@ -337,11 +345,11 @@ public class MassCalculation {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
//		// vvv DEBUG
 | 
							// // vvv DEBUG
 | 
				
			||||||
//		if( this.config.isComponentActive(component) && 0 < this.getMass() ) {
 | 
							// if( this.config.isComponentActive(component) && 0 < this.getMass() ) {
 | 
				
			||||||
//			System.err.println(String.format( "%s....<< return assemblyData:   %s (tree @%s)", prefix, this.toCMDebug(), component.getName() ));
 | 
							// 	System.err.println(String.format( "%s....<< return data @ %s:   %s", prefix, component.getName(), this.toCMDebug() ));
 | 
				
			||||||
//		}
 | 
							// }
 | 
				
			||||||
//		// ^^^ DEBUG
 | 
							// // ^^^ DEBUG
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		return this;
 | 
							return this;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -390,7 +398,7 @@ public class MassCalculation {
 | 
				
			|||||||
			}			
 | 
								}			
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		if( 0 < children.getMass() ) {
 | 
							if( MIN_MASS < children.getMass() ) {
 | 
				
			||||||
			this.merge( children );
 | 
								this.merge( children );
 | 
				
			||||||
			//System.err.println(String.format( "%s....assembly mass (incl/children):  %s", prefix, this.toCMDebug()));
 | 
								//System.err.println(String.format( "%s....assembly mass (incl/children):  %s", prefix, this.toCMDebug()));
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user