Program: 92194
Project: 92194
Buggy source:
/** 
 * Creates a transform from a base to a derived CS using an existing parameterized transform. This convenience constructor   {@linkplain #createConcatenatedTransform concatenates} the given parameterizedtransform with any other transform required for performing units changes and ordinates swapping. <p>The given  {@code parameterized} transform shall expect{@linkplain org.apache.sis.referencing.cs.AxesConvention#NORMALIZED normalized} input coordinates andproduce normalized output coordinates. See  {@link org.apache.sis.referencing.cs.AxesConvention} for moreinformation about what Apache SIS means by "normalized".</p> <div class="note"><b>Example:</b> The most typical examples of transforms with normalized inputs/outputs are normalized map projections expecting (<cite>longitude</cite>, <cite>latitude</cite>) inputs in degrees and calculating (<cite>x</cite>, <cite>y</cite>) coordinates in metres, both of them with ( {@linkplain org.opengis.referencing.cs.AxisDirection#EAST East},  {@linkplain org.opengis.referencing.cs.AxisDirection#NORTH North}) axis orientations.</div>
 * @param baseCS        The source coordinate system.
 * @param parameterized A <cite>base to derived</cite> transform for normalized input and output coordinates.
 * @param derivedCS     The target coordinate system.
 * @return The transform from {@code baseCS} to {@code derivedCS}, including unit conversions and axis swapping.
 * @throws FactoryException if the object creation failed. This exception is thrownif some required parameter has not been supplied, or has illegal value.
 * @see org.apache.sis.referencing.cs.AxesConvention#NORMALIZED
 */
public MathTransform createBaseToDerived(final CoordinateSystem baseCS,final MathTransform parameterized,final CoordinateSystem derivedCS) throws FactoryException {
  final Matrix swap1, swap3;
  try {
    swap1=CoordinateSystems.swapAndScaleAxes(baseCS,CoordinateSystems.normalize(baseCS));
    swap3=CoordinateSystems.swapAndScaleAxes(CoordinateSystems.normalize(derivedCS),derivedCS);
  }
 catch (  IllegalArgumentException|ConversionException cause) {
    throw new FactoryException(cause);
  }
  MathTransform step1=createAffineTransform(swap1);
  MathTransform step3=createAffineTransform(swap3);
  MathTransform step2=parameterized;
  final int numTrailingOrdinates=step3.getSourceDimensions() - step2.getTargetDimensions();
  if (numTrailingOrdinates > 0) {
    step2=createPassThroughTransform(0,step2,numTrailingOrdinates);
  }
  final int sourceDim=step1.getTargetDimensions();
  final int targetDim=step2.getSourceDimensions();
  if (sourceDim > targetDim) {
    final Matrix drop=Matrices.createDiagonal(targetDim + 1,sourceDim + 1);
    drop.setElement(targetDim,sourceDim,1);
    step1=createConcatenatedTransform(createAffineTransform(drop),step1);
  }
  MathTransform mt=createConcatenatedTransform(createConcatenatedTransform(step1,step2),step3);
  if (parameterized instanceof ParameterizedAffine) {
    mt=((ParameterizedAffine)parameterized).newTransform(mt);
  }
  return mt;
}

Fixed version (ground truth):
/** 
 * Creates a transform from a base to a derived CS using an existing parameterized transform. This convenience constructor   {@linkplain #createConcatenatedTransform concatenates} the given parameterizedtransform with any other transform required for performing units changes and ordinates swapping. <p>The given  {@code parameterized} transform shall expect{@linkplain org.apache.sis.referencing.cs.AxesConvention#NORMALIZED normalized} input coordinates andproduce normalized output coordinates. See  {@link org.apache.sis.referencing.cs.AxesConvention} for moreinformation about what Apache SIS means by "normalized".</p> <div class="note"><b>Example:</b> The most typical examples of transforms with normalized inputs/outputs are normalized map projections expecting (<cite>longitude</cite>, <cite>latitude</cite>) inputs in degrees and calculating (<cite>x</cite>, <cite>y</cite>) coordinates in metres, both of them with ( {@linkplain org.opengis.referencing.cs.AxisDirection#EAST East},  {@linkplain org.opengis.referencing.cs.AxisDirection#NORTH North}) axis orientations.</div>
 * @param baseCS        The source coordinate system.
 * @param parameterized A <cite>base to derived</cite> transform for normalized input and output coordinates.
 * @param derivedCS     The target coordinate system.
 * @return The transform from {@code baseCS} to {@code derivedCS}, including unit conversions and axis swapping.
 * @throws FactoryException if the object creation failed. This exception is thrownif some required parameter has not been supplied, or has illegal value.
 * @see org.apache.sis.referencing.cs.AxesConvention#NORMALIZED
 */
public MathTransform createBaseToDerived(final CoordinateSystem baseCS,final MathTransform parameterized,final CoordinateSystem derivedCS) throws FactoryException {
  final Matrix swap1, swap3;
  try {
    swap1=CoordinateSystems.swapAndScaleAxes(baseCS,CoordinateSystems.normalize(baseCS));
    swap3=CoordinateSystems.swapAndScaleAxes(CoordinateSystems.normalize(derivedCS),derivedCS);
  }
 catch (  IllegalArgumentException|ConversionException cause) {
    throw new FactoryException(cause);
  }
  MathTransform step1=createAffineTransform(swap1);
  MathTransform step3=createAffineTransform(swap3);
  MathTransform step2=parameterized;
  final int numTrailingOrdinates=step3.getSourceDimensions() - step2.getTargetDimensions();
  if (numTrailingOrdinates > 0) {
    step2=createPassThroughTransform(0,step2,numTrailingOrdinates);
  }
  final int sourceDim=step1.getTargetDimensions();
  final int targetDim=step2.getSourceDimensions();
  if (sourceDim > targetDim) {
    final Matrix drop=Matrices.createDiagonal(targetDim + 1,sourceDim + 1);
    drop.setElement(targetDim,sourceDim,1);
    step1=createConcatenatedTransform(createAffineTransform(drop),step1);
  }
  MathTransform mt=createConcatenatedTransform(createConcatenatedTransform(step1,step2),step3);
  if (parameterized instanceof ParameterizedAffine && !(mt instanceof ParameterizedAffine)) {
    mt=((ParameterizedAffine)parameterized).newTransform(mt);
  }
  return mt;
}