#pragma once#ifdef _WIN32#pragma warning( disable: 4996 )#endif// std#include<vector>#include<string>#include<random>// itk#include<itkImage.h>#include<itkCommand.h>#include<Eigen/Eigen>// shapeworks particle system#include"ParticleSystem/itkParticleSystem.h"#include"ParticleSystem/Sampler.h"#include"ParticleSystem/ParticleProcrustesRegistration.h"#include"ParticleSystem/itkParticleGoodBadAssessment.h"#include"ParticleSystem/itkParticleVectorFunction.h"#include"ParticleSystem/DomainType.h"#include"ParticleSystem/MeshWrapper.h"#include"ParticleSystem/OptimizationVisualizer.h"#include<Libs/Project/Project.h>namespaceshapeworks{classProject;classMatrixContainer{public:Eigen::MatrixXdmatrix_;};classOptimize{public:usingImageType=itk::Image<float,3>;usingVectorType=itk::ParticleVectorFunction<3>::VectorType;usingMatrixType=Eigen::MatrixXd;Optimize();virtual~Optimize();boolRun();boolLoadParameterFile(std::stringfilename);boolSetUpOptimize(ProjectHandleprojectFile);voidSetProject(std::shared_ptr<Project>project);voidSetIterationCallbackFunction(conststd::function<void(void)>&f){this->m_iter_callback=f;}voidAbortOptimization();boolGetAborted();virtualstd::vector<std::vector<itk::Point<double>>>GetLocalPoints();virtualstd::vector<std::vector<itk::Point<double>>>GetGlobalPoints();voidSetCutPlanes(std::vector<std::array<itk::Point<double>,3>>cut_planes);voidSetVerbosity(intverbosity_level);voidSetDomainsPerShape(intdomains_per_shape);intGetDomainsPerShape();voidSetDomainType(shapeworks::DomainTypetype);shapeworks::DomainTypeGetDomainType();voidSetNumberOfParticles(std::vector<int>number_of_particles);std::vector<int>GetNumberOfParticles();voidSetTransformFile(std::stringfilename);std::stringGetTransformFile();voidSetPrefixTransformFile(std::stringprefix_transform_file);std::stringGetPrefixTransformFile();voidSetOutputDir(std::stringoutput_dir);voidSetOutputTransformFile(std::stringoutput_transform_file);voidSetOutputIndividualTransformFiles(boolvalue);voidSetUseMeshBasedAttributes(booluse_mesh_based_attributes);boolGetUseMeshBasedAttributes();voidSetUseXYZ(std::vector<bool>use_xyz);voidSetUseNormals(std::vector<bool>use_normals);voidSetAttributesPerDomain(std::vector<int>attributes_per_domain);std::vector<int>GetAttributesPerDomain();voidSetDistributionDomainID(intdistribution_domain_id);intGetDistributionDomainID();voidSetOutputCuttingPlaneFile(std::stringoutput_cutting_plane_file);voidSetUseCuttingPlanes(booluse_cutting_planes);voidSetCuttingPlane(unsignedinti,constvnl_vector_fixed<double,3>&va,constvnl_vector_fixed<double,3>&vb,constvnl_vector_fixed<double,3>&vc);voidSetProcessingMode(intmode);voidSetAdaptivityMode(intadaptivity_mode);voidSetMeshFFCMode(intmesh_ffc_mode){m_mesh_ffc_mode=mesh_ffc_mode;m_sampler->SetMeshFFCMode(mesh_ffc_mode);}voidSetAdaptivityStrength(doubleadaptivity_strength);voidSetPairwisePotentialType(intpairwise_potential_type);voidSetTimePtsPerSubject(inttime_pts_per_subject);intGetTimePtsPerSubject();voidSetOptimizationIterations(intoptimization_iterations);voidSetOptimizationIterationsCompleted(intoptimization_iterations_completed);voidSetIterationsPerSplit(intiterations_per_split);voidSetInitializationCriterion(doubleinit_criterion);voidSetOptimizationCriterion(doubleopt_criterion);voidSetUseShapeStatisticsInInit(booluse_shape_statistics_in_init);voidSetProcrustesInterval(intprocrustes_interval);voidSetProcrustesScaling(boolprocrustes_scaling);voidSetProcrustesRotationTranslation(boolprocrustes_rotation_translation);voidSetRelativeWeighting(doublerelative_weighting);voidSetInitialRelativeWeighting(doubleinitial_relative_weighting);voidSetStartingRegularization(doublestarting_regularization);voidSetEndingRegularization(doubleending_regularization);voidSetRecomputeRegularizationInterval(intrecompute_regularization_interval);voidSetSaveInitSplits(boolsave_init_splits);voidSetCheckpointingInterval(intcheckpointing_interval);voidSetKeepCheckpoints(intkeep_checkpoints);voidSetCotanSigmaFactor(doublecotan_sigma_factor);voidSetUseRegression(booluse_regression);voidSetUseMixedEffects(booluse_mixed_effects);voidSetNormalAngle(doublenormal_angle);voidSetPerformGoodBad(boolperform_good_bad);voidSetLogEnergy(boollog_energy);voidAddImage(ImageType::Pointerimage,std::stringname="");voidAddMesh(vtkSmartPointer<vtkPolyData>poly_data);voidAddContour(vtkSmartPointer<vtkPolyData>poly_data);voidSetFilenames(conststd::vector<std::string>&filenames);voidSetPointFiles(conststd::vector<std::string>&point_files);intGetNumShapes();voidSetMeshFiles(conststd::vector<std::string>&mesh_files);voidSetAttributeScales(conststd::vector<double>&scales);voidSetFeaFiles(conststd::vector<std::string>&files);voidSetFeaGradFiles(conststd::vector<std::string>&files);voidSetFidsFiles(conststd::vector<std::string>&files);voidSetParticleFlags(std::vector<int>flags);voidSetDomainFlags(std::vector<int>flags);voidSetSharedBoundaryEnabled(boolenabled);voidSetSharedBoundaryWeight(doubleweight);conststd::vector<int>&GetDomainFlags();voidSetFileOutputEnabled(boolenabled);std::vector<bool>GetUseXYZ();std::vector<bool>GetUseNormals();voidSetNarrowBand(doublev);doubleGetNarrowBand();voidSetUseShapeStatisticsAfter(intnum_particles);intGetUseShapeStatisticsAfter();voidPrintParamInfo();std::shared_ptr<Sampler>GetSampler(){returnm_sampler;}MatrixContainerGetParticleSystem();voidSetPythonFile(std::stringfilename);voidSetGeodesicsEnabled(boolis_enabled);voidSetGeodesicsCacheSizeMultiplier(size_tn);shapeworks::OptimizationVisualizer&GetVisualizer();voidSetShowVisualizer(boolshow);boolGetShowVisualizer();boolGetMeshFFCMode(){returnm_mesh_ffc_mode;}vnl_vector_fixed<double,3>TransformPoint(intdomain,vnl_vector_fixed<double,3>input);protected:virtualvoidSetIterationCallback();voidRunProcrustes();voidOptimizeStart();voidOptimizerStop();voidReadTransformFile();voidReadPrefixTransformFile(conststd::string&s);voidInitializeSampler();doubleGetMinNeighborhoodRadius();voidAddSinglePoint();voidInitialize();voidAddAdaptivity();voidRunOptimize();voidSetInitialCorrespondenceMode();virtualvoidIterateCallback(itk::Object*,constitk::EventObject&);voidComputeEnergyAfterIteration();voidSetCotanSigma();voidWriteTransformFile(intiter=-1)const;voidWriteTransformFile(std::stringiter_prefix)const;voidWriteTransformFiles(intiter=-1)const;voidWriteTransformFiles(std::stringiter_prefix)const;voidWritePointFiles(intiter=-1);voidWritePointFiles(std::stringiter_prefix);voidWritePointFilesWithFeatures(intiter=-1);voidWritePointFilesWithFeatures(std::stringiter_prefix);voidWriteEnergyFiles();voidWriteCuttingPlanePoints(intiter=-1);voidWriteParameters(std::stringoutput_dir="");voidReportBadParticles();voidSetParameters();voidWriteModes();voidPrintStartMessage(std::stringstr,unsignedintvlevel=0)const;voidPrintDoneMessage(unsignedintvlevel=0)const;virtualvoidUpdateExportablePoints();virtualstd::vector<std::vector<std::vector<double>>>GetProcrustesTransforms();voidUpdateProject();// return a checkpoint dir for the current iterationstd::stringGetCheckpointDir();std::shared_ptr<Sampler>m_sampler;ParticleProcrustesRegistration::Pointerm_procrustes;itk::ParticleGoodBadAssessment<float,3>::Pointerm_good_bad;unsignedintm_verbosity_level=0;std::vector<std::vector<itk::Point<double>>>m_local_points,m_global_points;intm_checkpoint_counter=0;intm_procrustes_counter=0;intm_saturation_counter=0;boolm_disable_procrustes=true;boolm_use_cutting_planes=false;boolm_optimizing=false;boolm_use_regression=false;boolm_use_mixed_effects=false;// IO Parametersunsignedintm_domains_per_shape=1;shapeworks::DomainTypem_domain_type=shapeworks::DomainType::Image;std::vector<int>m_number_of_particles;std::stringm_transform_file;std::stringm_prefix_transform_file;std::stringm_output_dir;std::stringm_output_transform_file;boolm_output_transform_files=false;boolm_mesh_based_attributes=false;std::vector<bool>m_use_xyz;std::vector<bool>m_use_normals;std::vector<int>m_attributes_per_domain;intm_distribution_domain_id=-1;std::stringm_output_cutting_plane_file;// Optimization Parametersintm_processing_mode=3;intm_adaptivity_mode=0;doublem_adaptivity_strength=0.0;intm_pairwise_potential_type=0;// 0 - gaussian (Cates work), 1 - modified cotangent (Meyer),boolm_mesh_ffc_mode=0;unsignedintm_timepts_per_subject=1;intm_optimization_iterations=2000;intm_optimization_iterations_completed=0;intm_iterations_per_split=1000;doublem_initialization_criterion=1e-6;doublem_optimization_criterion=1e-6;boolm_use_shape_statistics_in_init=false;unsignedintm_procrustes_interval=3;boolm_procrustes_scaling=true;boolm_procrustes_rotation_translation=true;doublem_relative_weighting=1.0;doublem_initial_relative_weighting=0.05;doublem_starting_regularization=1000;doublem_ending_regularization=1.0;intm_recompute_regularization_interval=1;boolm_save_init_splits=false;unsignedintm_checkpointing_interval=50;intm_keep_checkpoints=0;doublem_cotan_sigma_factor=5.0;std::vector<int>m_particle_flags;std::vector<int>m_domain_flags;doublem_narrow_band=0.0;boolm_narrow_band_set=false;boolm_fixed_domains_present=false;intm_use_shape_statistics_after=-1;std::stringm_python_filename;boolm_geodesics_enabled=false;// geodesics disabled by defaultsize_tm_geodesic_cache_size_multiplier=0;// 0 => VtkMeshWrapper will use a heuristic to determine cache size// Keeps track of which state the optimization is in.unsignedintm_mode=0;/* m_spacing is used to scale the random update vector for particle splitting. */doublem_spacing=0;std::vector<std::string>m_filenames;intm_num_shapes=0;std::vector<double>m_energy_a;std::vector<double>m_energy_b;std::vector<double>m_total_energy;boolm_log_energy=false;std::stringm_str_energy;// GoodBadAssessmentstd::vector<std::vector<int>>m_bad_ids;doublem_normal_angle=itk::Math::pi/2.0;boolm_perform_good_bad=false;std::vector<int>m_cutting_planes_per_input;std::vector<int>m_spheres_per_input;boolm_file_output_enabled=true;boolm_aborted=false;std::vector<std::array<itk::Point<double>,3>>m_cut_planes;//itk::MemberCommand<Optimize>::Pointer m_iterate_command;intm_total_iterations=0;intm_iteration_count=0;intm_split_number{0};std::mt19937m_rand{42};std::function<void(void)>m_iter_callback;boolshow_visualizer=false;shapeworks::OptimizationVisualizervisualizer;std::shared_ptr<Project>project_;};}