cOrientationExterneRigide AJ2Xml(const cAvionJauneDocument & anAJD)
{
   return From_Std_RAff_C2M(AJ2R3d(anAJD),true);
}
cMA_AffineOrient::cMA_AffineOrient
(
    cAppliMICMAC &               anAppli,
    cModeleAnalytiqueComp & aModele,
    const cGeomDiscFPx &    aGeoTer,
    bool                    L1,
    const Pt2di &               aSzM,
    const ElPackHomologue &     aPackHom
)  :
   mAppli     (anAppli),
   mSzM       (aSzM),
   mImResidu  (mSzM.x,mSzM.y),
   mTImResidu (mImResidu),
   mImPds     (mSzM.x,mSzM.y),
   mTImPds    (mImPds),
   mGeoTer (aGeoTer),
   mPas    (aModele.Modele().PasCalcul()),
   mOri1   (aModele.mGeom1.GetOriNN()),
   mOri2   (aModele.mGeom2.GetOriNN()),
   mOC1    (new cOrientationConique(mOri1->StdExportCalibGlob())),
   mOC2    (new cOrientationConique(mOri2->StdExportCalibGlob())),
   // OO mOC1    (mOri1->OC() ? new cOrientationConique(*(mOri1->OC())) : 0),
   // OO mOC2    (mOri2->OC() ? new cOrientationConique(*(mOri2->OC())) : 0),
   mNbPtMin  (aModele.Modele().NbPtMinValideEqOriRel().Val()),
   mNewOri1 (*(mOri1->Dupl())),
   mNewOri2 (*(mOri2->Dupl())),
   mCam1   (*mOri1),
   mCam2   (*mOri2),
   mPack   (aPackHom),
   mSetEq  (
              (L1 ? cNameSpaceEqF::eSysL1Barrodale : cNameSpaceEqF::eSysPlein),
              mPack.size()
           ),
   mChR    (Pt3dr(0,0,0),0,0,0),
   // mSetEq (cNameSpaceEqF::eSysL1Barrodale,mPack.size()),

             // On va donner des points corriges de  la dist :
   // mPIF    (mSetEq.NewParamIntrNoDist(1.0,Pt2dr(0,0))),
   mPIF    (mSetEq.NewParamIntrNoDist(true,new CamStenopeIdeale(true,1.0,Pt2dr(0,0),aNoPAF))),

   
   mCamF1  (mPIF->NewCam
                 (
                      cNameSpaceEqF::eRotFigee,
                      mChR * mCam1.Orient().inv()
                 )
            ),
    mCamF2  (mPIF->NewCam
                 (
                      cNameSpaceEqF::eRotBaseU,
                      mChR * mCam2.Orient().inv(),
                      mCamF1
                 )
             ),
    mCpl12   (mSetEq.NewCpleCam(*mCamF1,*mCamF2)),
    mFocale  (mOri1->Focale())
{


   mSetEq.SetClosed();
   mPIF->SetFocFree(false);
   mPIF->SetPPFree(false);

   int aNbEtape = 9;
   int aFlag = 0;
   for 
   (
      std::list<int>::const_iterator itB = aModele.Modele().NumsAngleFiges().begin();
      itB != aModele.Modele().NumsAngleFiges().end();
      itB++
   )
      aFlag  |= 1 << *itB;


   for (int aK=0 ; aK<aNbEtape ; aK++)
   {
      mCamF2->RF().SetFlagAnglFige(aFlag);
/*
       mCamF2->RF().SetModeRot
       (
            (aK<3) ?
            cNameSpaceEqF::eRotCOptFige :
            cNameSpaceEqF::eRotBaseU
       );
*/
       OneItere(aK==(aNbEtape-1)); 
   }
   std::string  aName =    mAppli.FullDirResult() 
                         + std::string("Residus_Dz")
                         +  ToString(round_ni(mGeoTer.DeZoom()))
                         + std::string("_")
                         + mAppli.NameChantier() 
                         + std::string(".tif");
    Tiff_Im aFileResidu
            (
                aName.c_str(),
                mSzM,
                GenIm::u_int1,
                Tiff_Im::No_Compr,
                Tiff_Im::RGB
            );


   mNewOri2.SetOrientation(mCamF2->RF().CurRot());
   Pt3dr  aPMoy = CalcPtMoy(mNewOri1,mNewOri2);
   mNewOri1.SetAltiSol(aPMoy.z);
   mNewOri2.SetAltiSol(aPMoy.z);

   
   std::string aNAu = aModele.Modele().AutomSelExportOri().Val();
   std::string aNEx1 = aModele.Modele().AutomNamesExportOri1().Val();
   std::string aNEx2 = aModele.Modele().AutomNamesExportOri2().Val();
   std::string aNI1 = mAppli.PDV1()->Name();
   std::string aNI2 = mAppli.PDV2()->Name();
   std::string aNOri1 =   mAppli.FullDirGeom()
                        + StdNameFromCple(aModele.AutomExport(),aNAu,aNEx1,"@",aNI1,aNI2);
   std::string aNOri2 =   mAppli.FullDirGeom()
                        + StdNameFromCple(aModele.AutomExport(),aNAu,aNEx2,"@",aNI1,aNI2);

   bool aXmlRes = false;

   if (StdPostfix(aNOri1)== "xml")
   {
       aXmlRes = true;
       ELISE_ASSERT
       (
              (StdPostfix(aNOri2)=="xml")
	   && (mOC1!=0) && (mOC2!=0),
	   "Incoherence in  XML export for cMA_AffineOrient"
       );
       // Les points de verifs, si ils existent n'ont pas de raison d'etre transposables
       mOC1->Verif().SetNoInit();
       mOC2->Verif().SetNoInit();

       ElRotation3D aR1 = mCamF1->RF().CurRot();
       ElRotation3D aR2 = mCamF2->RF().CurRot();
       mOC2->Externe()  = From_Std_RAff_C2M(aR2,mOC2->Externe().ParamRotation().CodageMatr().IsInit());

       mOC1->Externe().AltiSol().SetVal(aPMoy.z);
       mOC2->Externe().AltiSol().SetVal(aPMoy.z);

       mOC1->Externe().Profondeur().SetVal(ProfFromCam(aR1.inv(),aPMoy));
       mOC2->Externe().Profondeur().SetVal(ProfFromCam(aR2.inv(),aPMoy));

   }

   TestOri1Ori2(true,*mOri1,*mOri2);
   TestOri1Ori2(true,mNewOri1,mNewOri2);
   std::cout << "ZMoyen = " << aPMoy.z  << "\n";

   Fonc_Num aFoK = (mImPds.in()>0);
   Fonc_Num aFRes = Max(0,Min(255,128.0 +mImResidu.in()*20));
   ELISE_COPY
   (
        aFileResidu.all_pts(),
          aFoK*its_to_rgb(Virgule(aFRes,3.14,32*Abs(mImResidu.in()>0.5)))
        + (1-aFoK)*Virgule(255,0,0),
        aFileResidu.out()
   );

   bool Exp1 =  aModele.Modele().AutomNamesExportOri1().IsInit();
   bool Exp2 =  aModele.Modele().AutomNamesExportOri2().IsInit();
   ELISE_ASSERT(Exp1 == Exp2,"Incoherence in AutomNamesExportOri");
   if (Exp1)
   {
      std::cout << "EXPORT   OOOOOOOOOOOOOOORI\n";
      AssertAutomSelExportOriIsInit(aModele.Modele());

      if(aXmlRes)
      {
          MakeFileXML(*mOC1,aNOri1,"MicMacForAPERO");
          MakeFileXML(*mOC2,aNOri2,"MicMacForAPERO");
      }
      else
      {
          ELISE_ASSERT(false,"Cannot write ori ");
          // OO mNewOri1.write_txt(aNOri1.c_str());
          // OO mNewOri2.write_txt(aNOri2.c_str());
      }
   }


   
   if (aModele.Modele().SigmaPixPdsExport().ValWithDef(-1) > 0)
   {
      double aPds = aModele.Modele().SigmaPixPdsExport().Val();
      for 
      (
         ElPackHomologue::iterator iT = mPack.begin();
         iT != mPack.end();
         iT++
      )
      {
          Pt2dr aQ1  = mCam1.F2toPtDirRayonL3(iT->P1());
          Pt2dr aQ2  = mCam2.F2toPtDirRayonL3(iT->P2());
          double aL =  ElAbs(mCpl12->ResiduSigneP1P2(aQ1,aQ2))*mFocale;
          double aP = ElSquare(aPds)/(ElSquare(aPds)+ElSquare(aL));
          iT->Pds() *= aP;
      }
   }
}