├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── TMT ├── .gitignore ├── CarlCore │ ├── CarlCore.cpp │ ├── CarlCore.h │ ├── CarlCore.i │ ├── CarlCore.sln │ ├── CarlCore.vcxproj │ ├── CarlCore.vcxproj.filters │ ├── CarlMain.cpp │ ├── Makefile │ ├── anim │ │ ├── CarlKinCharacter.cpp │ │ ├── CarlKinCharacter.h │ │ ├── CarlKinTree.cpp │ │ └── CarlKinTree.h │ ├── render │ │ ├── DrawCharacterExtend.cpp │ │ ├── DrawCharacterExtend.h │ │ ├── DrawKinTreeExtend.cpp │ │ ├── DrawKinTreeExtend.h │ │ ├── DrawUtilExtend.cpp │ │ ├── DrawUtilExtend.h │ │ ├── MeshUtilExtend.cpp │ │ └── MeshUtilExtend.h │ ├── scenes │ │ ├── CarlRLScene.cpp │ │ ├── CarlRLScene.h │ │ ├── CarlRLSceneSimChar.cpp │ │ ├── CarlRLSceneSimChar.h │ │ ├── CarlSceneKinChar.cpp │ │ ├── CarlSceneKinChar.h │ │ ├── CarlSceneTaskHeading.cpp │ │ ├── CarlSceneTaskHeading.h │ │ ├── CarlSceneTaskImitate.cpp │ │ ├── CarlSceneTaskImitate.h │ │ ├── CarlSceneTaskSpeed.cpp │ │ ├── CarlSceneTaskSpeed.h │ │ ├── DrawCarlRLScene.cpp │ │ ├── DrawCarlRLScene.h │ │ ├── DrawCarlScene.cpp │ │ ├── DrawCarlScene.h │ │ ├── DrawCarlSceneKinChar.cpp │ │ ├── DrawCarlSceneKinChar.h │ │ ├── DrawCarlSceneTaskHeading.cpp │ │ ├── DrawCarlSceneTaskHeading.h │ │ ├── DrawCarlSceneTaskImitate.cpp │ │ ├── DrawCarlSceneTaskImitate.h │ │ ├── DrawCarlSceneTaskSpeed.cpp │ │ ├── DrawCarlSceneTaskSpeed.h │ │ ├── DrawTMTSceneTemplateController.cpp │ │ ├── DrawTMTSceneTemplateController.h │ │ ├── DrawTMTSceneTransitionRecorder.cpp │ │ ├── DrawTMTSceneTransitionRecorder.h │ │ ├── TMTSceneTemplateController.cpp │ │ ├── TMTSceneTemplateController.h │ │ ├── TMTSceneTransitionRecorder.cpp │ │ └── TMTSceneTransitionRecorder.h │ └── util │ │ ├── FileUtilExtend.cpp │ │ ├── FileUtilExtend.h │ │ ├── MathUtilExtend.cpp │ │ ├── MathUtilExtend.h │ │ ├── MotionTrajectory.cpp │ │ ├── MotionTrajectory.h │ │ ├── PathApproximator.cpp │ │ ├── PathApproximator.h │ │ ├── PathFollower.cpp │ │ ├── PathFollower.h │ │ └── tqdm.h ├── TMT_Build_Transition_Tensor.py ├── TMT_Collect_Transitions.py ├── TMT_Optimizer.py ├── TMT_Runner.py ├── args │ ├── record_dog3d_transitions_args.txt │ ├── record_humanoid3d_transitions_args.txt │ ├── run_dog3d_template_controller_canter_args.txt │ ├── run_dog3d_template_controller_jump_args.txt │ ├── run_dog3d_template_controller_pace_args.txt │ ├── run_dog3d_template_controller_trot_args.txt │ ├── run_dog3d_unified_controller_args.txt │ ├── run_humanoid3d_template_controller_jog_args.txt │ ├── run_humanoid3d_template_controller_run_args.txt │ ├── run_humanoid3d_template_controller_walk_args.txt │ └── run_humanoid3d_unified_controller_args.txt ├── data │ ├── agents │ │ ├── ct_agent_dog_template_controller_canter.txt │ │ ├── ct_agent_dog_template_controller_jump.txt │ │ ├── ct_agent_dog_template_controller_pace.txt │ │ ├── ct_agent_dog_template_controller_trot.txt │ │ ├── ct_agent_dog_unified_controller.txt │ │ ├── ct_agent_humanoid_template_controller_jog_ppo.txt │ │ ├── ct_agent_humanoid_template_controller_run_ppo.txt │ │ ├── ct_agent_humanoid_template_controller_walk_ppo.txt │ │ └── ct_agent_humanoid_unified_controller_ppo.txt │ ├── cache │ │ └── link.txt │ ├── characters │ │ ├── dog3d.txt │ │ └── humanoid3d.txt │ ├── controllers │ │ ├── dog3d_ctrl.txt │ │ └── humanoid3d_ctrl.txt │ ├── motions │ │ ├── dog3d_canter.txt │ │ ├── dog3d_jump.txt │ │ ├── dog3d_pace.txt │ │ ├── dog3d_trot.txt │ │ ├── humanoid3d_jog.txt │ │ ├── humanoid3d_run.txt │ │ └── humanoid3d_walk.txt │ ├── normalizer │ │ └── normalizer.npz │ ├── policies │ │ ├── dog3d_canter.ckpt.data-00000-of-00001 │ │ ├── dog3d_canter.ckpt.index │ │ ├── dog3d_jump.ckpt.data-00000-of-00001 │ │ ├── dog3d_jump.ckpt.index │ │ ├── dog3d_pace.ckpt.data-00000-of-00001 │ │ ├── dog3d_pace.ckpt.index │ │ ├── dog3d_trot.ckpt.data-00000-of-00001 │ │ ├── dog3d_trot.ckpt.index │ │ ├── humanoid3d_jog.ckpt.data-00000-of-00001 │ │ ├── humanoid3d_jog.ckpt.index │ │ ├── humanoid3d_run.ckpt.data-00000-of-00001 │ │ ├── humanoid3d_run.ckpt.index │ │ ├── humanoid3d_walk.ckpt.data-00000-of-00001 │ │ └── humanoid3d_walk.ckpt.index │ ├── shaders │ │ ├── Depth_PS.glsl │ │ ├── DownSample_PS.glsl │ │ ├── DrawTexture_PS.glsl │ │ ├── FullScreenQuad_VS.glsl │ │ ├── Lighting_Lambert_PS.glsl │ │ ├── Mesh_VS.glsl │ │ ├── PostProcessing_PS.glsl │ │ ├── Sky_PS.glsl │ │ ├── Sky_VS.glsl │ │ ├── VertColor_PS.glsl │ │ └── im3d.glsl │ ├── terrain │ │ └── plane.txt │ ├── textures │ │ └── grid0.png │ ├── trajectories │ │ ├── dog3d_canter_trajectory.txt │ │ ├── dog3d_jump_trajectory.txt │ │ ├── dog3d_pace_trajectory.txt │ │ ├── dog3d_trot_trajectory.txt │ │ ├── humanoid3d_jog_trajectory.txt │ │ ├── humanoid3d_run_trajectory.txt │ │ └── humanoid3d_walk_trajectory.txt │ └── trans-tensors │ │ ├── precomputed_tensor_dog_ptcj.pkl │ │ └── precomputed_tensor_humanoid_wjr.pkl ├── plot_transition_tensor.ipynb ├── requirements.txt └── scripts │ ├── __init__.py │ ├── agent_builder.py │ ├── carl_env.py │ ├── carl_ppo_agent.py │ ├── carl_rl_world.py │ ├── deepmimic_ppo_agent.py │ ├── fc_2layers_16units.py │ ├── fc_2layers_512units.py │ ├── fc_3layers_512units_256units_gating.py │ ├── fc_3layers_512units_256units_reduced_state_gating.py │ ├── mpi_util_extend.py │ ├── net_builder.py │ ├── tf_util_extend.py │ ├── tmt_carl_agent.py │ ├── tmt_deepmimic_agent.py │ ├── tmt_util.py │ └── transition_motion_tensor.py └── images └── teaser.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *.mp4 3 | 4 | .vscode/ 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "DeepMimic"] 2 | path = DeepMimic 3 | url = https://github.com/xbpeng/DeepMimic 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Inventec Corporation 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /TMT/.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | 3 | *.opensdf 4 | *.sdf 5 | *.user 6 | *.exe 7 | *.VC.opendb 8 | *.psess 9 | *.pdb 10 | *.deps 11 | *.VC.db 12 | *.iobj 13 | *.ipdb 14 | *.pyd 15 | *_wrap.cpp 16 | *.htm 17 | *.dll 18 | *.so* 19 | *.o 20 | 21 | .vs/ 22 | __pycache__/ 23 | output/ 24 | CarlCore/objs/** 25 | results/ 26 | x64/ 27 | Debug/ 28 | Release/external/ 29 | gmake/ 30 | lib/ 31 | external/ 32 | Backup/ 33 | data/intermediate/ 34 | venv/ 35 | .ipynb_checkpoints/ 36 | .vscode/ 37 | 38 | CarlMain 39 | CarlCore/CarlCore.py 40 | 41 | CarlCore/CarlCore.o 42 | CarlCore/CarlCore_wrap.o 43 | CarlCore/CarlCore_wrap.cxx 44 | CarlCore/_CarlCore.so 45 | 46 | .project 47 | .pydevproject 48 | 49 | build_c.bat 50 | 51 | -------------------------------------------------------------------------------- /TMT/CarlCore/CarlCore.cpp: -------------------------------------------------------------------------------- 1 | #include "CarlCore.h" 2 | #include "scenes/CarlSceneKinChar.h" 3 | #include "scenes/CarlSceneTaskImitate.h" 4 | #include "scenes/CarlSceneTaskSpeed.h" 5 | #include "scenes/CarlSceneTaskHeading.h" 6 | #include "scenes/TMTSceneTemplateController.h" 7 | #include "scenes/TMTSceneTransitionRecorder.h" 8 | #include "scenes/DrawCarlSceneKinChar.h" 9 | #include "scenes/DrawCarlSceneTaskImitate.h" 10 | #include "scenes/DrawCarlSceneTaskSpeed.h" 11 | #include "scenes/DrawCarlSceneTaskHeading.h" 12 | #include "scenes/DrawTMTSceneTemplateController.h" 13 | #include "scenes/DrawTMTSceneTransitionRecorder.h" 14 | #include "scenes/SceneBuilder.h" 15 | #include "render/DrawUtilExtend.h" 16 | 17 | 18 | cCarlCore::cCarlCore(bool enable_draw) : cDeepMimicCore(enable_draw) 19 | { 20 | 21 | } 22 | 23 | cCarlCore::~cCarlCore() 24 | { 25 | 26 | } 27 | 28 | void cCarlCore::Init() 29 | { 30 | if (EnableDraw()) 31 | { 32 | cDrawUtil::InitDrawUtil(); 33 | cDrawUtilExtend::InitDrawUtilExtend(); 34 | InitFrameBuffer(); 35 | } 36 | SetupScene(); 37 | } 38 | 39 | void cCarlCore::Reshape(int w, int h) 40 | { 41 | cDeepMimicCore::Reshape(w, h); 42 | cDrawUtilExtend::Reshape(w, h); 43 | } 44 | 45 | bool cCarlCore::IsCarlRLScene() const 46 | { 47 | const auto& carl_rl_scene = GetCarlRLScene(); 48 | return carl_rl_scene != nullptr; 49 | } 50 | 51 | double cCarlCore::RecordPhase(int agent_id) const 52 | { 53 | const auto& carl_rl_scene = GetCarlRLScene(); 54 | if (carl_rl_scene != nullptr) 55 | { 56 | double phase = 0; 57 | carl_rl_scene->RecordPhase(agent_id, phase); 58 | 59 | return phase; 60 | } 61 | return 0; 62 | } 63 | 64 | int cCarlCore::RecordMotionLabel(int agent_id) const 65 | { 66 | const auto& carl_rl_scene = GetCarlRLScene(); 67 | if (carl_rl_scene != nullptr) 68 | { 69 | int motion_label = 0; 70 | carl_rl_scene->RecordMotionLabel(agent_id, motion_label); 71 | 72 | return motion_label; 73 | } 74 | return 0; 75 | } 76 | 77 | std::vector cCarlCore::RecordGoalTarget(int agent_id) const 78 | { 79 | const auto& carl_rl_scene = GetCarlRLScene(); 80 | if (carl_rl_scene != nullptr) 81 | { 82 | Eigen::VectorXd goal; 83 | carl_rl_scene->RecordGoalTarget(agent_id, goal); 84 | 85 | std::vector out_goal; 86 | ConvertVector(goal, out_goal); 87 | return out_goal; 88 | } 89 | return std::vector(0); 90 | } 91 | 92 | void cCarlCore::LogGatingWeights(int agent_id, const std::vector& weights) 93 | { 94 | 95 | } 96 | 97 | void cCarlCore::LogPrimitivesMeanStd(int agent_id, int num_primitives, const std::vector& means, const std::vector& stds) 98 | { 99 | 100 | } 101 | 102 | bool cCarlCore::SwitchMotion(int agent_id, int target_motion, double target_phase) 103 | { 104 | const auto& carl_rl_scene = GetCarlRLScene(); 105 | if (carl_rl_scene != nullptr) 106 | { 107 | return carl_rl_scene->SwitchMotion(agent_id, target_motion, target_phase); 108 | } 109 | return false; 110 | } 111 | 112 | void cCarlCore::SetupScene() 113 | { 114 | ClearScene(); 115 | 116 | std::string scene_name = ""; 117 | mArgParser->ParseString("scene", scene_name); 118 | if (scene_name == "") 119 | { 120 | printf("No scene specified\n"); 121 | assert(false); 122 | } 123 | 124 | mScene = nullptr; 125 | mRLScene = nullptr; 126 | mCarlRLScene = nullptr; 127 | if (EnableDraw()) 128 | { 129 | if (scene_name == "kin_char") 130 | { 131 | mScene = std::shared_ptr(new cDrawCarlSceneKinChar()); 132 | } 133 | else if (scene_name == "task_imitate") 134 | { 135 | mScene = std::shared_ptr(new cDrawCarlSceneTaskImitate()); 136 | } 137 | else if (scene_name == "task_speed") 138 | { 139 | mScene = std::shared_ptr(new cDrawCarlSceneTaskSpeed()); 140 | } 141 | else if (scene_name == "task_heading") 142 | { 143 | mScene = std::shared_ptr(new cDrawCarlSceneTaskHeading()); 144 | } 145 | else if (scene_name == "template_controller") 146 | { 147 | mScene = std::shared_ptr(new cDrawTMTSceneTemplateController()); 148 | } 149 | else if (scene_name == "transition_recorder") 150 | { 151 | mScene = std::shared_ptr(new cDrawTMTSceneTransitionRecorder()); 152 | } 153 | else 154 | { 155 | cSceneBuilder::BuildDrawScene(scene_name, mScene); 156 | } 157 | } 158 | else 159 | { 160 | if (scene_name == "kin_char") 161 | { 162 | mScene = std::shared_ptr(new cCarlSceneKinChar()); 163 | } 164 | else if (scene_name == "task_imitate") 165 | { 166 | mScene = std::shared_ptr(new cCarlSceneTaskImitate()); 167 | } 168 | else if (scene_name == "task_speed") 169 | { 170 | mScene = std::shared_ptr(new cCarlSceneTaskSpeed()); 171 | } 172 | else if (scene_name == "task_heading") 173 | { 174 | mScene = std::shared_ptr(new cCarlSceneTaskHeading()); 175 | } 176 | else if (scene_name == "template_controller") 177 | { 178 | mScene = std::shared_ptr(new cTMTSceneTemplateController()); 179 | } 180 | else if (scene_name == "transition_recorder") 181 | { 182 | mScene = std::shared_ptr(new cTMTSceneTransitionRecorder()); 183 | } 184 | else 185 | { 186 | cSceneBuilder::BuildScene(scene_name, mScene); 187 | } 188 | } 189 | 190 | if (mScene != nullptr) 191 | { 192 | mRLScene = std::dynamic_pointer_cast(mScene); 193 | mCarlRLScene = std::dynamic_pointer_cast(mScene); 194 | mScene->ParseArgs(mArgParser); 195 | mScene->Init(); 196 | printf("Loaded scene: %s\n", mScene->GetName().c_str()); 197 | } 198 | } 199 | 200 | const std::shared_ptr& cCarlCore::GetCarlRLScene() const 201 | { 202 | return mCarlRLScene; 203 | } 204 | -------------------------------------------------------------------------------- /TMT/CarlCore/CarlCore.h: -------------------------------------------------------------------------------- 1 | #include "DeepMimicCore.h" 2 | #include "scenes/CarlRLScene.h" 3 | 4 | 5 | class cCarlCore: public cDeepMimicCore 6 | { 7 | public: 8 | cCarlCore(bool enable_draw); 9 | virtual ~cCarlCore(); 10 | 11 | virtual void Init(); 12 | virtual void Reshape(int w, int h); 13 | virtual bool IsCarlRLScene() const; 14 | virtual double RecordPhase(int agent_id) const; 15 | virtual int RecordMotionLabel(int agent_id) const; 16 | virtual std::vector RecordGoalTarget(int agent_id) const; 17 | virtual void LogGatingWeights(int agent_id, const std::vector& weights); 18 | virtual void LogPrimitivesMeanStd(int agent_id, int num_primitives, const std::vector& means, const std::vector& stds); 19 | virtual bool SwitchMotion(int agent_id, int target_motion, double target_phase); 20 | 21 | protected: 22 | 23 | std::shared_ptr mCarlRLScene; 24 | 25 | virtual void SetupScene(); 26 | virtual const std::shared_ptr& GetCarlRLScene() const; 27 | }; 28 | -------------------------------------------------------------------------------- /TMT/CarlCore/CarlCore.i: -------------------------------------------------------------------------------- 1 | %module CarlCore 2 | 3 | %{ 4 | #include 5 | #include "CarlCore.h" 6 | %} 7 | 8 | %include "std_string.i" 9 | %include "std_vector.i" 10 | 11 | namespace std 12 | { 13 | %template(IntVector) vector; 14 | %template(DoubleVector) vector; 15 | %template(StringVector) vector; 16 | %template(ConstCharVector) vector; 17 | } 18 | 19 | %rename(at) operator[]; 20 | %rename(add) operator+; 21 | %rename(multiply) operator*; 22 | %rename(divide) operator/; 23 | %rename(subtract) operator-; 24 | %rename(lessThan) operator<; 25 | %rename(equal) operator==; 26 | %rename(notEqual) operator!=; 27 | %rename(incrementAdd) operator+=; 28 | %rename(incrementSubtract) operator-=; 29 | %rename(incrementMultiply) operator*=; 30 | %rename(incrementDivide) operator/=; 31 | 32 | %include "../../DeepMimic/DeepMimicCore/DeepMimicCore.h" 33 | %include "CarlCore.h" 34 | -------------------------------------------------------------------------------- /TMT/CarlCore/CarlCore.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28010.2026 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CarlCore", "CarlCore.vcxproj", "{43011113-B968-462C-B99A-539E3E98BC15}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release_Swig|x64 = Release_Swig|x64 13 | Release_Swig|x86 = Release_Swig|x86 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {43011113-B968-462C-B99A-539E3E98BC15}.Debug|x64.ActiveCfg = Debug|x64 19 | {43011113-B968-462C-B99A-539E3E98BC15}.Debug|x64.Build.0 = Debug|x64 20 | {43011113-B968-462C-B99A-539E3E98BC15}.Debug|x86.ActiveCfg = Debug|Win32 21 | {43011113-B968-462C-B99A-539E3E98BC15}.Debug|x86.Build.0 = Debug|Win32 22 | {43011113-B968-462C-B99A-539E3E98BC15}.Release_Swig|x64.ActiveCfg = Release_Swig|x64 23 | {43011113-B968-462C-B99A-539E3E98BC15}.Release_Swig|x64.Build.0 = Release_Swig|x64 24 | {43011113-B968-462C-B99A-539E3E98BC15}.Release_Swig|x86.ActiveCfg = Release_Swig|Win32 25 | {43011113-B968-462C-B99A-539E3E98BC15}.Release_Swig|x86.Build.0 = Release_Swig|Win32 26 | {43011113-B968-462C-B99A-539E3E98BC15}.Release|x64.ActiveCfg = Release|x64 27 | {43011113-B968-462C-B99A-539E3E98BC15}.Release|x64.Build.0 = Release|x64 28 | {43011113-B968-462C-B99A-539E3E98BC15}.Release|x86.ActiveCfg = Release|Win32 29 | {43011113-B968-462C-B99A-539E3E98BC15}.Release|x86.Build.0 = Release|Win32 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {5AC5F7F0-8808-49A6-808F-50891A27D4E8} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /TMT/CarlCore/Makefile: -------------------------------------------------------------------------------- 1 | BIN=CarlMain 2 | OBJ_DIR = objs 3 | MKDIR = mkdir -p 4 | 5 | DEEPMIMIC_DIR = ../../DeepMimic/DeepMimicCore 6 | 7 | EIGEN_DIR = /usr/local/include/eigen3 8 | BULLET_INC_DIR = /usr/local/include/bullet 9 | 10 | PYTHON_INC = /home/aic/miniconda3/envs/env_carl/include/python3.7m 11 | PYTHON_LIB = /home/aic/miniconda3/envs/env_carl/lib/ -lpython3.7m 12 | 13 | INC = -I./ \ 14 | -I$(DEEPMIMIC_DIR) \ 15 | -I$(EIGEN_DIR) \ 16 | -I$(BULLET_INC_DIR) 17 | 18 | LIBS = -lGLEW -lGL -lGLU -lglut -lBulletDynamics -lBulletCollision -lLinearMath -lm -lstdc++ 19 | 20 | CC = clang++ 21 | CFLAGS = -std=c++11 -O3 -Wall -fPIC 22 | 23 | DEEPMIMIC_SRC_DIRS = $(DEEPMIMIC_DIR)/util/ \ 24 | $(DEEPMIMIC_DIR)/util/json/ \ 25 | $(DEEPMIMIC_DIR)/anim/ \ 26 | $(DEEPMIMIC_DIR)/sim/ \ 27 | $(DEEPMIMIC_DIR)/render/ \ 28 | $(DEEPMIMIC_DIR)/render/lodepng/ \ 29 | $(DEEPMIMIC_DIR)/scenes/ \ 30 | $(DEEPMIMIC_DIR)/scenes/arm/ \ 31 | $(DEEPMIMIC_DIR)/scenes/particle/ \ 32 | $(DEEPMIMIC_DIR)/scenes/pendulum/ 33 | 34 | CARL_SRC_DIRS = scenes/ \ 35 | util/ \ 36 | anim/ \ 37 | sim/ \ 38 | render/ 39 | 40 | DEEPMIMIC_SRC_FILES = $(DEEPMIMIC_DIR)/DeepMimicCore.cpp \ 41 | $(foreach dir, $(DEEPMIMIC_SRC_DIRS), $(wildcard $(dir)*.cpp)) 42 | 43 | CARL_SRC_FILES = $(BIN).cpp \ 44 | CarlCore.cpp \ 45 | $(foreach dir, $(CARL_SRC_DIRS), $(wildcard $(dir)*.cpp)) 46 | 47 | OUT_DIRS = $(patsubst $(DEEPMIMIC_DIR)%, $(OBJ_DIR)%, $(DEEPMIMIC_SRC_DIRS)) 48 | 49 | OBJ = $(patsubst $(DEEPMIMIC_DIR)%, $(OBJ_DIR)%, $(patsubst %.cpp, %.o, $(DEEPMIMIC_SRC_FILES))) \ 50 | $(addprefix $(OBJ_DIR)/, $(patsubst %.cpp, %.o, $(CARL_SRC_FILES))) 51 | 52 | 53 | default: build 54 | 55 | build: $(BIN) 56 | 57 | print: 58 | echo $(OUT_DIRS) 59 | echo $(OBJ) 60 | 61 | python: $(OUT_DIRS) $(OBJ) 62 | swig -c++ -python CarlCore.i 63 | $(CC) -c -g $(CFLAGS) $(INC) $(LIBS) CarlCore.cpp CarlCore_wrap.cxx -I$(PYTHON_INC) 64 | $(CC) -shared $(CFLAGS) $(OBJ) $(LIBS) CarlCore_wrap.o -o _CarlCore.so -L$(PYTHON_LIB) 65 | 66 | $(OBJ_DIR)/%.o: $(DEEPMIMIC_DIR)/%.cpp 67 | $(CC) -c -g $(CFLAGS) $(INC) -o $@ $< 68 | 69 | $(OBJ_DIR)/%.o: %.cpp 70 | $(CC) -c -g $(CFLAGS) $(INC) -o $@ $< 71 | 72 | $(BIN): $(OUT_DIRS) $(OBJ) 73 | $(CC) -g $(CFLAGS) $(INC) $(OBJ) $(LIBS) -o $@ 74 | 75 | $(OUT_DIRS): 76 | $(MKDIR) $(OUT_DIRS) 77 | 78 | clean: 79 | - rm -f -r $(OUT_DIRS) $(BIN) $(OBJ) $(OBJ_DIR) CarlCore_wrap.cxx _CarlCore.so 80 | -------------------------------------------------------------------------------- /TMT/CarlCore/anim/CarlKinCharacter.cpp: -------------------------------------------------------------------------------- 1 | #include "CarlKinCharacter.h" 2 | #include "CarlKinTree.h" 3 | 4 | 5 | bool cCarlKinCharacter::LoadSkeleton(const Json::Value& root) 6 | { 7 | return cCarlKinTree::Load(root, mJointMat); 8 | } 9 | 10 | int cCarlKinCharacter::GetStateSize() const 11 | { 12 | int pose_size = GetNumDof() - 5; // remove root.pos.x, root.pos.z, root.rot.x, root.rot.y and root.rot.z 13 | int vel_size = GetNumDof(); 14 | return pose_size + vel_size; 15 | } 16 | 17 | void cCarlKinCharacter::RecordState(double time, Eigen::VectorXd& out_state) const 18 | { 19 | int state_size = GetStateSize(); 20 | out_state = std::numeric_limits::quiet_NaN() * Eigen::VectorXd::Ones(state_size); 21 | Eigen::VectorXd pose; 22 | Eigen::VectorXd vel; 23 | CalcPose(time, pose); 24 | CalcVel(time, vel); 25 | int pose_offset = 0; 26 | int pose_size = GetNumDof() - 5; // remove root.pos.x, root.pos.z, root.rot.x, root.rot.y and root.rot.z 27 | int vel_offset = pose_offset + pose_size; 28 | int vel_size = GetNumDof(); 29 | 30 | out_state[0] = pose[1]; // root.pos.y 31 | out_state.segment(1, pose_size - 1) = pose.segment(6, pose.rows() - 6); 32 | out_state.segment(vel_offset, vel_size) = vel; 33 | } 34 | -------------------------------------------------------------------------------- /TMT/CarlCore/anim/CarlKinCharacter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "anim/KinCharacter.h" 4 | 5 | class cCarlKinCharacter : virtual public cKinCharacter 6 | { 7 | public: 8 | 9 | virtual bool LoadSkeleton(const Json::Value& root); 10 | virtual int GetStateSize() const; 11 | virtual void RecordState(double time, Eigen::VectorXd& out_state) const; 12 | 13 | protected: 14 | 15 | 16 | }; 17 | -------------------------------------------------------------------------------- /TMT/CarlCore/anim/CarlKinTree.cpp: -------------------------------------------------------------------------------- 1 | #include "CarlKinTree.h" 2 | 3 | const std::string gJointsKey = "Joints"; 4 | const std::string gJointDescExtraKeys[cCarlKinTree::eJointDescExtraMax] = 5 | { 6 | "EnableContact", 7 | "ContactThreshold", 8 | "ContactOffsetX", 9 | "ContactOffsetY", 10 | "ContactOffsetZ" 11 | }; 12 | 13 | 14 | bool cCarlKinTree::Load(const Json::Value& root, Eigen::MatrixXd& out_joint_mat) 15 | { 16 | bool succ = false; 17 | 18 | if (!root[gJointsKey].isNull()) 19 | { 20 | Json::Value joints = root[gJointsKey]; 21 | int num_joints = joints.size(); 22 | 23 | out_joint_mat.resize(num_joints, eJointDescMax + eJointDescExtraMax); 24 | 25 | for (int j = 0; j < num_joints; ++j) 26 | { 27 | tJointDesc curr_joint_desc = tJointDesc::Zero(); 28 | tJointDescExtra curr_joint_desc_extra = tJointDescExtra::Zero(); 29 | 30 | Json::Value joint_json = joints.get(j, 0); 31 | succ = ParseJoint(joint_json, curr_joint_desc); 32 | succ &= ParseJointExtra(joint_json, curr_joint_desc_extra); 33 | if (succ) 34 | { 35 | tJointDescJointed curr_joint_desc_joined = tJointDescJointed::Zero(); 36 | curr_joint_desc_joined << curr_joint_desc, curr_joint_desc_extra; 37 | out_joint_mat.row(j) = curr_joint_desc_joined; 38 | } 39 | else 40 | { 41 | printf("Failed to parse joint %i\n", j); 42 | return false; 43 | } 44 | } 45 | 46 | for (int j = 0; j < num_joints; ++j) 47 | { 48 | const auto& curr_desc = out_joint_mat.row(j); 49 | int parent_id = static_cast(curr_desc(eJointDescParent)); 50 | if (parent_id >= j) 51 | { 52 | printf("Parent id must be < child id, parent id: %i, child id: %i\n", parent_id, j); 53 | out_joint_mat.resize(0, 0); 54 | assert(false); 55 | 56 | return false; 57 | } 58 | 59 | out_joint_mat.row(j) = curr_desc; 60 | } 61 | 62 | PostProcessJointMat(out_joint_mat); 63 | } 64 | 65 | return succ; 66 | } 67 | 68 | bool cCarlKinTree::IsFootContact(const Eigen::MatrixXd& joint_mat, int joint_id) 69 | { 70 | double foot_contact_val = joint_mat(joint_id, eJointDescMax + eJointDescExtraEnableContact); 71 | return foot_contact_val != 0; 72 | } 73 | 74 | double cCarlKinTree::GetContactThreshold(const Eigen::MatrixXd& joint_mat, int joint_id) 75 | { 76 | double contact_threshold = joint_mat(joint_id, eJointDescMax + eJointDescExtraContactThreshold); 77 | return contact_threshold; 78 | } 79 | 80 | tVector cCarlKinTree::GetContactOffset(const Eigen::MatrixXd& joint_mat, int joint_id) 81 | { 82 | return tVector(joint_mat(joint_id, eJointDescMax + eJointDescExtraContactOffsetX), 83 | joint_mat(joint_id, eJointDescMax + eJointDescExtraContactOffsetY), 84 | joint_mat(joint_id, eJointDescMax + eJointDescExtraContactOffsetZ), 1); 85 | } 86 | 87 | bool cCarlKinTree::ParseJointExtra(const Json::Value& root, tJointDescExtra& out_joint_desc_extra) 88 | { 89 | out_joint_desc_extra = BuildJointDescExtra(); 90 | for (int i = 0; i < eJointDescExtraMax; ++i) 91 | { 92 | const std::string& key = gJointDescExtraKeys[i]; 93 | if (!root[key].isNull()) 94 | { 95 | out_joint_desc_extra[i] = root[key].asDouble(); 96 | } 97 | } 98 | return true; 99 | } 100 | 101 | cCarlKinTree::tJointDescExtra cCarlKinTree::BuildJointDescExtra() 102 | { 103 | tJointDescExtra desc; 104 | desc(eJointDescExtraEnableContact) = 0; 105 | desc(eJointDescExtraContactThreshold) = 0; 106 | desc(eJointDescExtraContactOffsetX) = 0; 107 | desc(eJointDescExtraContactOffsetY) = 0; 108 | desc(eJointDescExtraContactOffsetZ) = 0; 109 | return desc; 110 | } 111 | -------------------------------------------------------------------------------- /TMT/CarlCore/anim/CarlKinTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | #include "anim/KinTree.h" 5 | 6 | class cCarlKinTree: virtual public cKinTree 7 | { 8 | public: 9 | 10 | enum eJointDescExtra 11 | { 12 | eJointDescExtraEnableContact, 13 | eJointDescExtraContactThreshold, 14 | eJointDescExtraContactOffsetX, 15 | eJointDescExtraContactOffsetY, 16 | eJointDescExtraContactOffsetZ, 17 | eJointDescExtraMax 18 | }; 19 | typedef Eigen::Matrix tJointDescExtra; 20 | typedef Eigen::Matrix tJointDescJointed; 21 | 22 | static bool Load(const Json::Value& root, Eigen::MatrixXd& out_joint_mat); 23 | 24 | static bool IsFootContact(const Eigen::MatrixXd& joint_mat, int joint_id); 25 | static double GetContactThreshold(const Eigen::MatrixXd& joint_mat, int joint_id); 26 | static tVector GetContactOffset(const Eigen::MatrixXd& joint_mat, int joint_id); 27 | static tJointDescExtra BuildJointDescExtra(); 28 | 29 | protected: 30 | 31 | static bool ParseJointExtra(const Json::Value& root, tJointDescExtra& out_joint_desc_extra); 32 | }; 33 | -------------------------------------------------------------------------------- /TMT/CarlCore/render/DrawCharacterExtend.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawCharacterExtend.h" 2 | #include "DrawKinTreeExtend.h" 3 | 4 | void cDrawCharacterExtend::DrawBones(const cCharacter& character, double link_width, const tVector& fill_col, const tVector& bone_col) 5 | { 6 | glDisable(GL_DEPTH_TEST); 7 | const Eigen::MatrixXd& joint_mat = character.GetJointMat(); 8 | const Eigen::VectorXd& pose = character.GetPose(); 9 | cDrawKinTreeExtend::Draw(joint_mat, pose, link_width, fill_col, bone_col); 10 | glEnable(GL_DEPTH_TEST); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /TMT/CarlCore/render/DrawCharacterExtend.h: -------------------------------------------------------------------------------- 1 | #pragma onces 2 | 3 | #include "render/DrawCharacter.h" 4 | 5 | class cDrawCharacterExtend 6 | { 7 | public: 8 | 9 | static void DrawBones(const cCharacter& character, double link_width, const tVector& fill_col, const tVector& bone_col); 10 | 11 | protected: 12 | 13 | }; 14 | -------------------------------------------------------------------------------- /TMT/CarlCore/render/DrawKinTreeExtend.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawKinTreeExtend.h" 2 | #include "render/DrawUtilExtend.h" 3 | 4 | 5 | void cDrawKinTreeExtend::Draw(const Eigen::MatrixXd& joint_desc, const Eigen::VectorXd& pose, double link_width, const tVector& fill_col, const tVector& line_col) 6 | { 7 | int root_id = cKinTree::GetRoot(joint_desc); 8 | cDrawUtil::SetLineWidth(1); 9 | DrawTree(joint_desc, pose, root_id, link_width, fill_col, line_col); 10 | } 11 | 12 | void cDrawKinTreeExtend::DrawTree(const Eigen::MatrixXd& joint_desc, const Eigen::VectorXd& pose, int joint_id, double link_width, 13 | const tVector& fill_col, const tVector& line_col) 14 | { 15 | const double node_radius = link_width; 16 | 17 | if (joint_id != cKinTree::gInvalidJointID) 18 | { 19 | bool has_parent = cKinTree::HasParent(joint_desc, joint_id); 20 | if (has_parent) 21 | { 22 | tVector attach_pt = cKinTree::GetAttachPt(joint_desc, joint_id); 23 | double len = attach_pt.norm(); 24 | 25 | tVector attach_dir = attach_pt.normalized(); 26 | const tVector up = tVector(0, 1, 0, 0); 27 | tMatrix rot_mat = cMathUtil::DirToRotMat(attach_dir, up); 28 | 29 | tVector pos = tVector::Zero(); 30 | float bone_width = 12.5f * link_width * len; 31 | tVector size = tVector(bone_width, len, bone_width, 0); 32 | 33 | // draw link 34 | cDrawUtil::PushMatrixView(); 35 | cDrawUtil::Translate(pos); 36 | cDrawUtil::MultMatrixView(rot_mat); 37 | cDrawUtil::Rotate(0.5 * M_PI, tVector(1, 0, 0, 0)); 38 | 39 | cDrawUtil::SetColor(tVector(line_col[0], line_col[1], line_col[2], line_col[3])); 40 | cDrawUtilExtend::DrawBone(tVector::Zero(), size, cDrawUtil::eDrawSolid); 41 | 42 | if (line_col[3] > 0) 43 | { 44 | cDrawUtil::SetColor(tVector(line_col[0] * 1.2, line_col[1] * 1.2, line_col[2] * 1.2, line_col[3])); 45 | } 46 | 47 | cDrawUtil::PopMatrixView(); 48 | } 49 | 50 | cDrawUtil::PushMatrixView(); 51 | tMatrix m = cKinTree::ChildParentTrans(joint_desc, pose, joint_id); 52 | cDrawUtil::MultMatrixView(m); 53 | 54 | // draw node 55 | cDrawUtil::SetColor(tVector(fill_col[0], fill_col[1], fill_col[2], fill_col[3])); 56 | cDrawUtil::DrawSphere(node_radius * 0.5f); 57 | 58 | Eigen::VectorXi children; 59 | cKinTree::FindChildren(joint_desc, joint_id, children); 60 | for (int i = 0; i < children.size(); ++i) 61 | { 62 | int child_id = children[i]; 63 | DrawTree(joint_desc, pose, child_id, link_width, fill_col, line_col); 64 | } 65 | 66 | cDrawUtil::PopMatrixView(); 67 | } 68 | } -------------------------------------------------------------------------------- /TMT/CarlCore/render/DrawKinTreeExtend.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "render/DrawKinTree.h" 4 | 5 | 6 | class cDrawKinTreeExtend 7 | { 8 | public: 9 | 10 | static void Draw(const Eigen::MatrixXd& joint_desc, const Eigen::VectorXd& pose, double link_width, const tVector& fill_col, const tVector& line_col); 11 | static void DrawTree(const Eigen::MatrixXd& joint_desc, const Eigen::VectorXd& pose, int joint_id, double link_width, const tVector& fill_col, const tVector& line_col); 12 | 13 | protected: 14 | 15 | }; 16 | -------------------------------------------------------------------------------- /TMT/CarlCore/render/DrawUtilExtend.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawUtilExtend.h" 2 | #include "render/MeshUtilExtend.h" 3 | 4 | int g_ScreenWidth = 800; 5 | int g_ScreenHeight = static_cast(g_ScreenWidth * 9.0 / 16.0); 6 | 7 | 8 | std::unique_ptr cDrawUtilExtend::gBoneSolidMesh = nullptr; 9 | std::unique_ptr cDrawUtilExtend::gBoneWireMesh = nullptr; 10 | 11 | 12 | void cDrawUtilExtend::InitDrawUtilExtend() 13 | { 14 | BuildMeshes(); 15 | } 16 | 17 | void cDrawUtilExtend::BeginDrawString() 18 | { 19 | glMatrixMode(GL_PROJECTION); 20 | glPushMatrix(); 21 | glLoadIdentity(); 22 | 23 | glMatrixMode(GL_MODELVIEW); 24 | glPushMatrix(); 25 | glLoadIdentity(); 26 | 27 | glPointSize(1.5f); 28 | glLineWidth(1.5f); 29 | glColor4f(0.0f, 0.0f, 0.0f, 1.0f); 30 | 31 | cDrawUtil::UnbindDefaultProg(); 32 | } 33 | 34 | void cDrawUtilExtend::EndDrawString() 35 | { 36 | cDrawUtil::BindDefaultProg(); 37 | 38 | glMatrixMode(GL_PROJECTION); 39 | glPopMatrix(); 40 | 41 | glMatrixMode(GL_MODELVIEW); 42 | glPopMatrix(); 43 | } 44 | 45 | void cDrawUtilExtend::DrawString(float x, float y, char* string, tVector color) 46 | { 47 | #ifdef _WIN64 48 | return; 49 | #endif 50 | 51 | const float aspect = (float)g_ScreenWidth / g_ScreenHeight; 52 | const float text_size = 0.09f; 53 | const tVector scale = tVector(text_size / aspect, text_size, 1, 0); 54 | 55 | std::istringstream str_stream(string); 56 | std::string curr_str = ""; 57 | 58 | glPushMatrix(); 59 | const float default_scale = 0.0045f; 60 | 61 | float _x = ((x / g_ScreenWidth) - 0.5f) * 2.0f; 62 | float _y = ((y / g_ScreenHeight) - 0.5f) * 2.0f; 63 | 64 | glTranslatef(_x, _y, 0); 65 | glScalef(default_scale * scale[0], default_scale * scale[1], scale[2]); 66 | 67 | while (std::getline(str_stream, curr_str)) 68 | { 69 | glPushMatrix(); 70 | for (size_t i = 0; i < curr_str.size(); ++i) 71 | { 72 | glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, curr_str[i]); 73 | } 74 | glPopMatrix(); 75 | 76 | glTranslatef(0, -1 / default_scale, 0); 77 | } 78 | glPopMatrix(); 79 | } 80 | 81 | void cDrawUtilExtend::BuildTexts() 82 | { 83 | 84 | } 85 | 86 | void cDrawUtilExtend::BuildMeshes() 87 | { 88 | cMeshUtilExtend::BuildBoneSolidMesh(gBoneSolidMesh); 89 | cMeshUtilExtend::BuildBoneWireMesh(gBoneWireMesh); 90 | } 91 | 92 | void cDrawUtilExtend::DrawArrow2D(const tVector& start, const tVector& end, double head_size) 93 | { 94 | GLboolean prev_enable; 95 | glGetBooleanv(GL_CULL_FACE, &prev_enable); 96 | glDisable(GL_CULL_FACE); 97 | 98 | tVector dir = tVector(0, 1, 0, 0); 99 | double dir_len = 0; 100 | if (start != end) 101 | { 102 | dir = end - start; 103 | dir_len = dir.norm(); 104 | dir /= dir_len; 105 | } 106 | 107 | dir[3] = 0; 108 | tVector axis = tVector(0, 1, 0, 0); 109 | tVector tangent = axis.cross3(dir); 110 | tangent.normalize(); 111 | 112 | const double width = head_size * 0.1854; 113 | tVector body_end = end - dir * head_size; 114 | 115 | tVector a = start - width * tangent; 116 | tVector b = body_end - width * tangent; 117 | tVector c = body_end + width * tangent; 118 | tVector d = start + width * tangent; 119 | cDrawUtil::DrawQuad(a, b, c, d); 120 | 121 | tVector e0 = body_end - tangent * head_size * 0.5f; 122 | tVector e1 = body_end + tangent * head_size * 0.5f; 123 | cDrawUtil::DrawQuad(end, e1, e0, end); 124 | 125 | if (prev_enable) 126 | { 127 | glEnable(GL_CULL_FACE); 128 | } 129 | } 130 | 131 | void cDrawUtilExtend::DrawXyzAxes() 132 | { 133 | const float line_length = 1.0f; 134 | const float y_offset = 0.005f; 135 | 136 | cDrawUtil::SetColor(tVector(1, 0, 0, 1)); 137 | cDrawUtil::DrawLine(tVector(0, y_offset, 0, 0), tVector(line_length, y_offset, 0, 0)); 138 | 139 | cDrawUtil::SetColor(tVector(0, 1, 0, 1)); 140 | cDrawUtil::DrawLine(tVector(0, y_offset, 0, 0), tVector(0, line_length + y_offset, 0, 0)); 141 | 142 | cDrawUtil::SetColor(tVector(0, 0, 1, 1)); 143 | cDrawUtil::DrawLine(tVector(0, y_offset, 0, 0), tVector(0, y_offset, line_length, 0)); 144 | } 145 | 146 | void cDrawUtilExtend::DrawBone(const tVector& pos, const tVector& size, cDrawUtil::eDrawMode draw_mode /*= eDrawSolid*/) 147 | { 148 | if (draw_mode == cDrawUtil::eDrawWire || draw_mode == cDrawUtil::eDrawWireSimple) 149 | { 150 | DrawBoneWire(pos, size); 151 | } 152 | else if (draw_mode == cDrawUtil::eDrawSolid) 153 | { 154 | DrawBoneSolid(pos, size); 155 | } 156 | else 157 | { 158 | assert(false); // unsupported draw mode 159 | } 160 | } 161 | 162 | void cDrawUtilExtend::DrawBoneSolid(const tVector& pos, const tVector& size) 163 | { 164 | cDrawUtil::PushMatrixView(); 165 | cDrawUtil::Translate(pos); 166 | cDrawUtil::Scale(size); 167 | gBoneSolidMesh->Draw(GL_TRIANGLES); 168 | cDrawUtil::PopMatrixView(); 169 | } 170 | 171 | void cDrawUtilExtend::DrawBoneWire(const tVector& pos, const tVector& size) 172 | { 173 | cDrawUtil::PushMatrixView(); 174 | cDrawUtil::Translate(pos); 175 | cDrawUtil::Scale(size); 176 | gBoneWireMesh->Draw(GL_LINES); 177 | cDrawUtil::PopMatrixView(); 178 | } 179 | 180 | void cDrawUtilExtend::Reshape(int w, int h) 181 | { 182 | BuildTexts(); 183 | } 184 | 185 | void cDrawUtilExtend::CheckOpenGLError(int line, std::string src) 186 | { 187 | GLenum err; 188 | while ((err = glGetError()) != GL_NO_ERROR) 189 | { 190 | const GLubyte *str = gluErrorString(err); 191 | if (str) 192 | printf("OpenGL error %d (line %d in %s): %s\n", err, line, src.c_str(), str); 193 | else 194 | printf("OpenGL error %d (line %d in %s): NULL\n", err, line, src.c_str()); 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /TMT/CarlCore/render/DrawUtilExtend.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "render/DrawUtil.h" 4 | 5 | #define CHECK_GL_ERROR cDrawUtilExtend::CheckOpenGLError(__LINE__, __FILE__); 6 | 7 | class cDrawUtilExtend 8 | { 9 | public: 10 | 11 | static void InitDrawUtilExtend(); 12 | static void BeginDrawString(); 13 | static void EndDrawString(); 14 | static void DrawString(float x, float y, char* string, tVector color = tVector(0, 0, 0, 1)); 15 | static void BuildTexts(); 16 | static void BuildMeshes(); 17 | static void DrawArrow2D(const tVector& start, const tVector& end, double head_size); 18 | static void DrawXyzAxes(); 19 | static void DrawBone(const tVector& pos, const tVector& size, cDrawUtil::eDrawMode draw_mode = cDrawUtil::eDrawSolid); 20 | static void Reshape(int w, int h); 21 | 22 | static void CheckOpenGLError(int line, std::string src); 23 | 24 | protected: 25 | 26 | static std::unique_ptr gBoneSolidMesh; 27 | static std::unique_ptr gBoneWireMesh; 28 | 29 | static void DrawBoneSolid(const tVector& pos, const tVector& size); 30 | static void DrawBoneWire(const tVector& pos, const tVector& size); 31 | }; 32 | -------------------------------------------------------------------------------- /TMT/CarlCore/render/MeshUtilExtend.cpp: -------------------------------------------------------------------------------- 1 | #include "MeshUtilExtend.h" 2 | 3 | 4 | void cMeshUtilExtend::BuildBoneSolidMesh(std::unique_ptr& out_mesh) 5 | { 6 | const int num_faces = 8; 7 | const int vert_size = num_faces * 3 * cMeshUtil::gPosDim; 8 | const int norm_size = num_faces * 3 * cMeshUtil::gNormDim; 9 | const int coord_size = num_faces * 3 * cMeshUtil::gCoordDim; 10 | const int idx_size = num_faces * 3; 11 | const float size = 1.0f / 7.0f; 12 | 13 | const float vert_data[vert_size] = { 14 | -size, 0.200000, -size, // 3 15 | size, 0.200000, -size, // 4 16 | 0, 0, 0, // 6 17 | 18 | size, 0.200000, -size, // 4 19 | -size, 0.200000, -size, // 3 20 | 0, 1, 0, // 5 21 | 22 | -size, 0.200000, size, // 1 23 | size, 0.200000, size, // 2 24 | 0, 1, 0, // 5 25 | 26 | size, 0.200000, size, // 2 27 | size, 0.200000, -size, // 4 28 | 0, 1, 0, // 5 29 | 30 | -size, 0.200000, -size, // 3 31 | -size, 0.200000, size, // 1 32 | 0, 1, 0, // 5 33 | 34 | size, 0.200000, size, // 2 35 | -size, 0.200000, size, // 1 36 | 0, 0, 0, // 6 37 | 38 | size, 0.200000, -size, // 4 39 | size, 0.200000, size, // 2 40 | 0, 0, 0, // 6 41 | 42 | -size, 0.200000, size, // 1 43 | -size, 0.200000, -size, // 3 44 | 0, 0, 0, // 6 45 | }; 46 | 47 | const float norm_data[vert_size] = { 48 | 0.0000, -0.5812, -0.8137, // 1 49 | 0.0000, -0.5812, -0.8137, // 1 50 | 0.0000, -0.5812, -0.8137, // 1 51 | 52 | 0.0000, 0.1758, -0.9844, // 2 53 | 0.0000, 0.1758, -0.9844, // 2 54 | 0.0000, 0.1758, -0.9844, // 2 55 | 56 | 0.0000, 0.1758, 0.9844, // 3 57 | 0.0000, 0.1758, 0.9844, // 3 58 | 0.0000, 0.1758, 0.9844, // 3 59 | 60 | 0.9844, 0.1758, 0.0000, // 4 61 | 0.9844, 0.1758, 0.0000, // 4 62 | 0.9844, 0.1758, 0.0000, // 4 63 | 64 | -0.9844, 0.1758, 0.0000, // 5 65 | -0.9844, 0.1758, 0.0000, // 5 66 | -0.9844, 0.1758, 0.0000, // 5 67 | 68 | 0.0000, -0.5812, 0.8137, // 6 69 | 0.0000, -0.5812, 0.8137, // 6 70 | 0.0000, -0.5812, 0.8137, // 6 71 | 72 | 0.8137, -0.5812, 0.0000, // 7 73 | 0.8137, -0.5812, 0.0000, // 7 74 | 0.8137, -0.5812, 0.0000, // 7 75 | 76 | -0.8137, -0.5812, 0.0000, // 8 77 | -0.8137, -0.5812, 0.0000, // 8 78 | -0.8137, -0.5812, 0.0000, // 8 79 | }; 80 | 81 | 82 | const float coord_data[coord_size] = { 83 | 0, 0, 84 | 1, 0, 85 | 1, 1, 86 | 87 | 1, 1, 88 | 0, 1, 89 | 0, 0, 90 | 91 | 1, 0, 92 | 1, 1, 93 | 0, 1, 94 | 95 | 0, 1, 96 | 0, 0, 97 | 1, 0, 98 | 99 | 0, 0, 100 | 1, 0, 101 | 1, 1, 102 | 103 | 1, 1, 104 | 0, 1, 105 | 0, 0, 106 | 107 | 0, 0, 108 | 1, 0, 109 | 1, 1, 110 | 111 | 1, 1, 112 | 0, 1, 113 | 0, 0, 114 | }; 115 | 116 | int idx_data[idx_size]; 117 | for (int i = 0; i < idx_size; ++i) 118 | { 119 | idx_data[i] = i; 120 | } 121 | 122 | out_mesh = std::unique_ptr(new cDrawMesh()); 123 | cMeshUtil::BuildDrawMesh(vert_data, vert_size, norm_data, norm_size, coord_data, coord_size, idx_data, idx_size, out_mesh.get()); 124 | } 125 | 126 | void cMeshUtilExtend::BuildBoneWireMesh(std::unique_ptr& out_mesh) 127 | { 128 | const int num_edges = 12; 129 | const int vert_size = num_edges * 2 * cMeshUtil::gPosDim; 130 | const int norm_size = num_edges * 2 * cMeshUtil::gNormDim; 131 | const int coord_size = num_edges * 2 * cMeshUtil::gCoordDim; 132 | const int idx_size = num_edges * 2; 133 | const float size = 1.0f / 7.0f; 134 | 135 | const float vert_data[vert_size] = { 136 | 0, 0, 0, 137 | -size, 0.2f, -size, 138 | 139 | 0, 0, 0, 140 | size, 0.2f, -size, 141 | 142 | 0, 0, 0, 143 | -size, 0.2f, size, 144 | 145 | 0, 0, 0, 146 | size, 0.2f, size, 147 | 148 | -size, 0.2f, -size, 149 | 0, 1, 0, 150 | 151 | size, 0.2f, -size, 152 | 0, 1, 0, 153 | 154 | -size, 0.2f, size, 155 | 0, 1, 0, 156 | 157 | size, 0.2f, size, 158 | 0, 1, 0, 159 | 160 | -size, 0.2f, -size, 161 | size, 0.2f, -size, 162 | 163 | size, 0.2f, -size, 164 | size, 0.2f, size, 165 | 166 | size, 0.2f, size, 167 | -size, 0.2f, size, 168 | 169 | -size, 0.2f, size, 170 | -size, 0.2f, -size 171 | }; 172 | 173 | const float norm_data[vert_size] = { 174 | 0, 1, 0, // top 175 | 0, 1, 0, 176 | 0, 1, 0, 177 | 0, 1, 0, 178 | 0, 1, 0, 179 | 0, 1, 0, 180 | 0, 1, 0, 181 | 0, 1, 0, 182 | 183 | 0, -1, 0, // bottom 184 | 0, -1, 0, 185 | 0, -1, 0, 186 | 0, -1, 0, 187 | 0, -1, 0, 188 | 0, -1, 0, 189 | 0, -1, 0, 190 | 0, -1, 0, 191 | 192 | 1, 0, 0, // front 193 | 1, 0, 0, 194 | 195 | -1, 0, 0, // back 196 | -1, 0, 0, 197 | 198 | 0, 0, -1, // left 199 | 0, 0, -1, 200 | 201 | 0, 0, 1, // right 202 | 0, 0, 1 203 | }; 204 | 205 | 206 | const float coord_data[vert_size] = { 207 | 0, 0, // top 208 | 1, 0, 209 | 1, 0, 210 | 1, 1, 211 | 1, 1, 212 | 0, 1, 213 | 0, 1, 214 | 0, 0, 215 | 216 | 1, 0, // bottom 217 | 1, 1, 218 | 1, 1, 219 | 0, 1, 220 | 0, 1, 221 | 0, 0, 222 | 0, 0, 223 | 1, 0, 224 | 225 | 0, 0, // front 226 | 0, 1, 227 | 228 | 0, 0, // back 229 | 0, 1, 230 | 231 | 0, 1, // left 232 | 0, 0, 233 | 234 | 0, 1, // right 235 | 0, 0 236 | }; 237 | 238 | int idx_data[idx_size]; 239 | for (int i = 0; i < idx_size; ++i) 240 | { 241 | idx_data[i] = i; 242 | } 243 | 244 | out_mesh = std::unique_ptr(new cDrawMesh()); 245 | cMeshUtil::BuildDrawMesh(vert_data, vert_size, norm_data, norm_size, coord_data, coord_size, idx_data, idx_size, out_mesh.get()); 246 | } 247 | 248 | -------------------------------------------------------------------------------- /TMT/CarlCore/render/MeshUtilExtend.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "render/MeshUtil.h" 4 | 5 | 6 | class cMeshUtilExtend 7 | { 8 | public: 9 | 10 | static void BuildBoneSolidMesh(std::unique_ptr& out_mesh); 11 | static void BuildBoneWireMesh(std::unique_ptr& out_mesh); 12 | 13 | protected: 14 | 15 | }; 16 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/CarlRLScene.cpp: -------------------------------------------------------------------------------- 1 | #include "CarlRLScene.h" 2 | 3 | void cCarlRLScene::LogGatingWeights(int agent_id, const std::vector& weights) 4 | { 5 | 6 | } 7 | 8 | void cCarlRLScene::LogPrimitivesMeanStd(int agent_id, int num_primitives, const std::vector& means, const std::vector& stds) 9 | { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/CarlRLScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "scenes/RLScene.h" 4 | 5 | class cCarlRLScene : virtual public cRLScene 6 | { 7 | public: 8 | 9 | virtual void RecordPhase(int agent_id, double& out_phase) const = 0; 10 | virtual void RecordMotionLabel(int agent_id, int& out_motion_label) const = 0; 11 | virtual void RecordGoalTarget(int agent_id, Eigen::VectorXd& out_goal) const = 0; 12 | virtual void LogGatingWeights(int agent_id, const std::vector& weights); 13 | virtual void LogPrimitivesMeanStd(int agent_id, int num_primitives, const std::vector& means, const std::vector& stds); 14 | virtual bool SwitchMotion(int agent_id, int target_motion, double target_phase) = 0; 15 | 16 | protected: 17 | 18 | }; 19 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/CarlRLSceneSimChar.cpp: -------------------------------------------------------------------------------- 1 | #include "CarlRLSceneSimChar.h" 2 | 3 | void cCarlRLSceneSimChar::RecordPhase(int agent_id, double& out_phase) const 4 | { 5 | out_phase = 0; 6 | } 7 | 8 | void cCarlRLSceneSimChar::RecordMotionLabel(int agent_id, int& out_motion_label) const 9 | { 10 | out_motion_label = 0; 11 | } 12 | 13 | void cCarlRLSceneSimChar::RecordGoalTarget(int agent_id, Eigen::VectorXd& out_goal) const 14 | { 15 | 16 | } 17 | 18 | void cCarlRLSceneSimChar::LogGatingWeights(int agent_id, const std::vector& weights) 19 | { 20 | 21 | } 22 | 23 | void cCarlRLSceneSimChar::LogPrimitivesMeanStd(int agent_id, int num_primitives, const std::vector& means, const std::vector& stds) 24 | { 25 | 26 | } 27 | 28 | double cCarlRLSceneSimChar::CalcReward(int agent_id) const 29 | { 30 | return 0; 31 | } 32 | 33 | bool cCarlRLSceneSimChar::SwitchMotion(int agent_id, int target_motion, double target_phase) 34 | { 35 | return false; 36 | } 37 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/CarlRLSceneSimChar.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "scenes/RLSceneSimChar.h" 4 | #include "CarlRLScene.h" 5 | 6 | class cCarlRLSceneSimChar: virtual public cRLSceneSimChar, virtual public cCarlRLScene 7 | { 8 | public: 9 | 10 | virtual void RecordPhase(int agent_id, double& out_phase) const; 11 | virtual void RecordMotionLabel(int agent_id, int& out_motion_label) const; 12 | virtual void RecordGoalTarget(int agent_id, Eigen::VectorXd& out_goal) const; 13 | virtual void LogGatingWeights(int agent_id, const std::vector& weights); 14 | virtual void LogPrimitivesMeanStd(int agent_id, int num_primitives, const std::vector& means, const std::vector& stds); 15 | virtual double CalcReward(int agent_id) const; 16 | virtual bool SwitchMotion(int agent_id, int target_motion, double target_phase); 17 | 18 | protected: 19 | 20 | 21 | }; 22 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/CarlSceneKinChar.cpp: -------------------------------------------------------------------------------- 1 | #include "CarlSceneKinChar.h" 2 | 3 | cCarlSceneKinChar::cCarlSceneKinChar() 4 | { 5 | } 6 | 7 | cCarlSceneKinChar::~cCarlSceneKinChar() 8 | { 9 | } 10 | 11 | void cCarlSceneKinChar::Init() 12 | { 13 | bool succ = BuildCharacters(); 14 | } 15 | 16 | void cCarlSceneKinChar::ParseArgs(const std::shared_ptr& parser) 17 | { 18 | cScene::ParseArgs(parser); 19 | ParseCharParams(parser, mCharParams); 20 | } 21 | 22 | void cCarlSceneKinChar::Reset() 23 | { 24 | ResetCharacters(); 25 | } 26 | 27 | void cCarlSceneKinChar::Clear() 28 | { 29 | mChar.reset(); 30 | } 31 | 32 | void cCarlSceneKinChar::Update(double time_elapsed) 33 | { 34 | UpdateCharacters(time_elapsed); 35 | } 36 | 37 | const std::shared_ptr& cCarlSceneKinChar::GetCharacter() const 38 | { 39 | return mChar; 40 | } 41 | 42 | tVector cCarlSceneKinChar::GetCharPos() const 43 | { 44 | return GetCharacter()->GetRootPos(); 45 | } 46 | 47 | double cCarlSceneKinChar::GetTime() const 48 | { 49 | return GetCharacter()->GetTime(); 50 | } 51 | 52 | std::string cCarlSceneKinChar::GetName() const 53 | { 54 | return "Kinematic Char"; 55 | } 56 | 57 | void cCarlSceneKinChar::ParseCharParams(const std::shared_ptr& parser, cCarlKinCharacter::tParams& out_params) const 58 | { 59 | std::string char_file = ""; 60 | std::string motion_file = ""; 61 | std::string state_file = ""; 62 | double init_pos_xs = 0; 63 | 64 | bool succ = parser->ParseString("character_file", char_file); 65 | 66 | if (succ) 67 | { 68 | parser->ParseString("state_file", state_file); 69 | parser->ParseString("motion_file", motion_file); 70 | parser->ParseDouble("char_init_pos_x", init_pos_xs); 71 | 72 | out_params.mCharFile = char_file; 73 | out_params.mMotionFile = motion_file; 74 | out_params.mStateFile = state_file; 75 | out_params.mOrigin[0] = init_pos_xs; 76 | } 77 | else 78 | { 79 | printf("No character file provided\n"); 80 | } 81 | } 82 | 83 | bool cCarlSceneKinChar::BuildCharacters() 84 | { 85 | mChar.reset(); 86 | 87 | auto& params = mCharParams; 88 | params.mID = 0; 89 | params.mLoadDrawShapes = true; 90 | 91 | bool succ = BuildCharacter(params, mChar); 92 | 93 | return succ; 94 | } 95 | 96 | bool cCarlSceneKinChar::BuildCharacter(const cCarlKinCharacter::tParams& params, std::shared_ptr& out_char) const 97 | { 98 | out_char = std::shared_ptr(new cCarlKinCharacter()); 99 | bool succ = out_char->Init(params); 100 | return succ; 101 | } 102 | 103 | void cCarlSceneKinChar::ResetCharacters() 104 | { 105 | mChar->Reset(); 106 | } 107 | 108 | void cCarlSceneKinChar::UpdateCharacters(double timestep) 109 | { 110 | mChar->Update(timestep); 111 | } 112 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/CarlSceneKinChar.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "scenes/Scene.h" 4 | #include "anim/CarlKinCharacter.h" 5 | 6 | class cCarlSceneKinChar: virtual public cScene 7 | { 8 | public: 9 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 10 | 11 | cCarlSceneKinChar(); 12 | virtual ~cCarlSceneKinChar(); 13 | 14 | virtual void Init(); 15 | virtual void ParseArgs(const std::shared_ptr& parser); 16 | virtual void Reset(); 17 | virtual void Clear(); 18 | 19 | virtual void Update(double time_elapsed); 20 | 21 | virtual const std::shared_ptr& GetCharacter() const; 22 | virtual tVector GetCharPos() const; 23 | virtual double GetTime() const; 24 | 25 | virtual std::string GetName() const; 26 | 27 | protected: 28 | cCarlKinCharacter::tParams mCharParams; 29 | std::shared_ptr mChar; 30 | 31 | virtual void ParseCharParams(const std::shared_ptr& parser, cCarlKinCharacter::tParams& out_params) const; 32 | 33 | virtual bool BuildCharacters(); 34 | virtual bool BuildCharacter(const cCarlKinCharacter::tParams& params, std::shared_ptr& out_char) const; 35 | virtual void ResetCharacters(); 36 | virtual void UpdateCharacters(double timestep); 37 | }; 38 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/CarlSceneTaskHeading.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "CarlRLSceneSimChar.h" 4 | #include "anim/KinCharacter.h" 5 | 6 | class cCarlSceneTaskHeading : virtual public cCarlRLSceneSimChar 7 | { 8 | public: 9 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 10 | 11 | cCarlSceneTaskHeading(); 12 | virtual ~cCarlSceneTaskHeading(); 13 | 14 | virtual void ParseArgs(const std::shared_ptr& parser); 15 | virtual void Init(); 16 | 17 | virtual const std::shared_ptr& GetKinChar() const; 18 | virtual void EnableRandRotReset(bool enable); 19 | virtual bool EnabledRandRotReset() const; 20 | virtual void EnableRandHeading(bool enable); 21 | virtual void SetTargetHeading(double heading); 22 | virtual double GetTargetHeading() const; 23 | virtual double GetTargetVelocity() const; 24 | virtual void SetSimCharRotation(double heading); 25 | 26 | virtual void RecordGoal(int agent_id, Eigen::VectorXd& out_goal) const; 27 | virtual void RecordGoalTarget(int agent_id, Eigen::VectorXd& out_goal) const; 28 | int GetGoalSize(int agent_id) const; 29 | void BuildGoalOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const; 30 | void BuildGoalNormGroups(int agent_id, Eigen::VectorXi& out_groups) const; 31 | 32 | virtual double CalcReward(int agent_id) const; 33 | 34 | virtual std::string GetName() const; 35 | 36 | protected: 37 | 38 | typedef struct { 39 | double fromTime; 40 | double toTime; 41 | double fromDirection; 42 | double toDirection; 43 | } DirectionCtrlInterval; 44 | 45 | std::string mMotionFile; 46 | std::shared_ptr mKinChar; 47 | 48 | bool mEnableRandRotReset; 49 | bool mEnableRandHeading; 50 | bool mEnableRootRotFail; 51 | double mTargetHeading; 52 | double mTargetVelocity; 53 | int mUpdateCount; 54 | 55 | virtual bool BuildCharacters(); 56 | virtual void BuildKinChars(); 57 | virtual bool BuildKinCharacter(int id, std::shared_ptr& out_char) const; 58 | 59 | virtual void UpdateCharacters(double timestep); 60 | virtual void UpdateTargetHeading(double timestep); 61 | 62 | virtual double GetKinTime() const; 63 | virtual void SyncCharacters(double heading); 64 | virtual bool EnableSyncChar() const; 65 | virtual void ResetCharacters(); 66 | virtual void ResetKinChar(); 67 | 68 | virtual double CalcRandKinResetTime(); 69 | virtual double CalcRewardHeading(const cSimCharacter& sim_char, const double target_heading) const; 70 | }; -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/CarlSceneTaskImitate.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "CarlRLSceneSimChar.h" 4 | #include "anim/CarlKinCharacter.h" 5 | 6 | class cCarlSceneTaskImitate : virtual public cCarlRLSceneSimChar 7 | { 8 | public: 9 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 10 | 11 | cCarlSceneTaskImitate(); 12 | virtual ~cCarlSceneTaskImitate(); 13 | 14 | virtual void ParseArgs(const std::shared_ptr& parser); 15 | virtual void Init(); 16 | 17 | virtual const std::shared_ptr& GetKinChar() const; 18 | virtual void EnableRandRotReset(bool enable); 19 | virtual bool EnabledRandRotReset() const; 20 | 21 | virtual void EnableRandomKinChar(bool enable); 22 | virtual void EnableSyncKinCharRootInUpdate(bool enable); 23 | 24 | virtual void RecordGoal(int agent_id, Eigen::VectorXd& out_goal) const; 25 | virtual void RecordGoalTarget(int agent_id, Eigen::VectorXd& out_goal) const; 26 | int GetGoalSize(int agent_id) const; 27 | void BuildGoalOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const; 28 | void BuildGoalNormGroups(int agent_id, Eigen::VectorXi& out_groups) const; 29 | int GetNumActions(int agent_id) const; 30 | 31 | int GetNumContacts() const; 32 | std::vector GetKinCharContacts() const; 33 | std::vector GetSimCharContacts(const Eigen::MatrixXd& joint_mat) const; 34 | 35 | virtual double CalcReward(int agent_id) const; 36 | virtual eTerminate CheckTerminate(int agent_id) const; 37 | 38 | virtual std::string GetName() const; 39 | 40 | virtual void RotateKinChar(); 41 | 42 | protected: 43 | 44 | std::string mMotionFile; 45 | std::shared_ptr mKinChar; 46 | 47 | Eigen::VectorXd mJointWeights; 48 | bool mEnableRandRotReset; 49 | bool mSyncCharRootPos; 50 | bool mSyncCharRootRot; 51 | bool mEnableRootRotFail; 52 | bool mEnableRandomKinChar; 53 | bool mSyncCharRootInUpdate; 54 | double mHoldEndFrame; 55 | double mTimestep; 56 | double mNextKinCharPhase; 57 | double mNextEndTime; 58 | 59 | std::vector mGaitJoints; 60 | 61 | virtual bool BuildCharacters(); 62 | 63 | virtual void CalcJointWeights(const std::shared_ptr& character, Eigen::VectorXd& out_weights) const; 64 | virtual bool BuildController(const cCtrlBuilder::tCtrlParams& ctrl_params, std::shared_ptr& out_ctrl); 65 | virtual void BuildKinChar(); 66 | virtual bool BuildKinCharacter(int id, std::string motion_file, std::shared_ptr& out_char) const; 67 | virtual void UpdateCharacters(double timestep); 68 | virtual void UpdateKinChar(double timestep); 69 | 70 | virtual void ResetCharacters(); 71 | virtual void ResetKinChar(); 72 | virtual void SyncCharacters(); 73 | virtual bool EnableSyncChar() const; 74 | virtual void InitCharacterPosFixed(const std::shared_ptr& out_char); 75 | 76 | virtual void InitGaits(); 77 | virtual void InitJointWeights(); 78 | virtual void ResolveCharGroundIntersect(); 79 | virtual void ResolveCharGroundIntersect(const std::shared_ptr& out_char) const; 80 | virtual void SyncKinCharRoot(); 81 | virtual void SyncKinCharNewCycle(const cSimCharacter& sim_char, cCarlKinCharacter& out_kin_char) const; 82 | 83 | virtual double GetKinTime() const; 84 | virtual bool CheckKinNewCycle(double timestep) const; 85 | virtual bool HasFallen(const cSimCharacter& sim_char) const; 86 | virtual bool CheckRootRotFail(const cSimCharacter& sim_char) const; 87 | virtual bool CheckRootRotFail(const cSimCharacter& sim_char, const cCarlKinCharacter& kin_char) const; 88 | 89 | virtual void UpdateNextRandKinChar(bool rand_phase); 90 | 91 | virtual double CalcRewardImitate(const cSimCharacter& sim_char, const cCarlKinCharacter& ref_char) const; 92 | }; -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/CarlSceneTaskSpeed.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "CarlRLSceneSimChar.h" 4 | #include "anim/KinCharacter.h" 5 | 6 | class cCarlSceneTaskSpeed : virtual public cCarlRLSceneSimChar 7 | { 8 | public: 9 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 10 | 11 | cCarlSceneTaskSpeed(); 12 | virtual ~cCarlSceneTaskSpeed(); 13 | 14 | virtual void ParseArgs(const std::shared_ptr& parser); 15 | virtual void Init(); 16 | 17 | virtual const std::shared_ptr& GetKinChar() const; 18 | virtual void EnableRandRotReset(bool enable); 19 | virtual bool EnabledRandRotReset() const; 20 | 21 | virtual void EnableRandVelocity(bool enable); 22 | virtual void SetTargetVelocity(double velocity); 23 | virtual double GetTargetVelocity() const; 24 | virtual double GetTargetHeading() const; 25 | 26 | virtual void RecordGoal(int agent_id, Eigen::VectorXd& out_goal) const; 27 | virtual void RecordGoalTarget(int agent_id, Eigen::VectorXd& out_goal) const; 28 | int GetGoalSize(int agent_id) const; 29 | void BuildGoalOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const; 30 | void BuildGoalNormGroups(int agent_id, Eigen::VectorXi& out_groups) const; 31 | 32 | virtual double CalcReward(int agent_id) const; 33 | 34 | virtual std::string GetName() const; 35 | 36 | protected: 37 | 38 | typedef struct { 39 | double fromTime; 40 | double toTime; 41 | double fromSpeed; 42 | double toSpeed; 43 | } SpeedCtrlInterval; 44 | 45 | std::string mMotionFile; 46 | std::shared_ptr mKinChar; 47 | 48 | bool mEnableRandRotReset; 49 | bool mEnableRandVelocity; 50 | bool mEnableRootRotFail; 51 | double mTargetVelocity; 52 | double mTargetHeading; 53 | double mMinRandVelocity; 54 | double mMaxRandVelocity; 55 | int mUpdateCount; 56 | 57 | virtual bool BuildCharacters(); 58 | virtual void BuildKinChars(); 59 | virtual bool BuildKinCharacter(int id, std::shared_ptr& out_char) const; 60 | 61 | virtual void UpdateCharacters(double timestep); 62 | virtual void UpdateTargetVelocity(double timestep); 63 | 64 | virtual double GetKinTime() const; 65 | virtual void SyncCharacters(); 66 | virtual bool EnableSyncChar() const; 67 | virtual void ResetCharacters(); 68 | virtual void ResetKinChar(); 69 | 70 | virtual double CalcRandKinResetTime(); 71 | virtual double CalcRewardVelocity(const cSimCharacter& sim_char, const double target_velocity) const; 72 | }; -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlRLScene.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawCarlRLScene.h" 2 | 3 | cDrawCarlRLScene::cDrawCarlRLScene() 4 | { 5 | } 6 | 7 | cDrawCarlRLScene::~cDrawCarlRLScene() 8 | { 9 | } 10 | 11 | void cDrawCarlRLScene::Init() 12 | { 13 | cCarlRLScene::Init(); 14 | cDrawCarlScene::Init(); 15 | } 16 | 17 | int cDrawCarlRLScene::GetNumAgents() const 18 | { 19 | return GetCarlRLScene()->GetNumAgents(); 20 | } 21 | 22 | bool cDrawCarlRLScene::NeedNewAction(int agent_id) const 23 | { 24 | return GetCarlRLScene()->NeedNewAction(agent_id); 25 | } 26 | 27 | void cDrawCarlRLScene::RecordState(int agent_id, Eigen::VectorXd& out_state) const 28 | { 29 | GetCarlRLScene()->RecordState(agent_id, out_state); 30 | } 31 | 32 | void cDrawCarlRLScene::RecordGoal(int agent_id, Eigen::VectorXd& out_goal) const 33 | { 34 | GetCarlRLScene()->RecordGoal(agent_id, out_goal); 35 | } 36 | 37 | void cDrawCarlRLScene::RecordPhase(int agent_id, double& out_phase) const 38 | { 39 | GetCarlRLScene()->RecordPhase(agent_id, out_phase); 40 | } 41 | 42 | void cDrawCarlRLScene::RecordMotionLabel(int agent_id, int& out_motion_label) const 43 | { 44 | GetCarlRLScene()->RecordMotionLabel(agent_id, out_motion_label); 45 | } 46 | 47 | void cDrawCarlRLScene::RecordGoalTarget(int agent_id, Eigen::VectorXd& out_goal) const 48 | { 49 | GetCarlRLScene()->RecordGoalTarget(agent_id, out_goal); 50 | } 51 | 52 | void cDrawCarlRLScene::SetAction(int agent_id, const Eigen::VectorXd& action) 53 | { 54 | GetCarlRLScene()->SetAction(agent_id, action); 55 | } 56 | 57 | bool cDrawCarlRLScene::SwitchMotion(int agent_id, int target_motion, double target_phase) 58 | { 59 | return GetCarlRLScene()->SwitchMotion(agent_id, target_motion, target_phase); 60 | } 61 | 62 | eActionSpace cDrawCarlRLScene::GetActionSpace(int agent_id) const 63 | { 64 | return GetCarlRLScene()->GetActionSpace(agent_id); 65 | } 66 | 67 | int cDrawCarlRLScene::GetStateSize(int agent_id) const 68 | { 69 | return GetCarlRLScene()->GetStateSize(agent_id); 70 | } 71 | 72 | int cDrawCarlRLScene::GetGoalSize(int agent_id) const 73 | { 74 | return GetCarlRLScene()->GetGoalSize(agent_id); 75 | } 76 | 77 | int cDrawCarlRLScene::GetActionSize(int agent_id) const 78 | { 79 | return GetCarlRLScene()->GetActionSize(agent_id); 80 | } 81 | 82 | int cDrawCarlRLScene::GetNumActions(int agent_id) const 83 | { 84 | return GetCarlRLScene()->GetNumActions(agent_id); 85 | } 86 | 87 | void cDrawCarlRLScene::BuildStateOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const 88 | { 89 | GetCarlRLScene()->BuildStateOffsetScale(agent_id, out_offset, out_scale); 90 | } 91 | 92 | void cDrawCarlRLScene::BuildGoalOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const 93 | { 94 | GetCarlRLScene()->BuildGoalOffsetScale(agent_id, out_offset, out_scale); 95 | } 96 | 97 | void cDrawCarlRLScene::BuildActionOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const 98 | { 99 | GetCarlRLScene()->BuildActionOffsetScale(agent_id, out_offset, out_scale); 100 | } 101 | 102 | void cDrawCarlRLScene::BuildActionBounds(int agent_id, Eigen::VectorXd& out_min, Eigen::VectorXd& out_max) const 103 | { 104 | GetCarlRLScene()->BuildActionBounds(agent_id, out_min, out_max); 105 | } 106 | 107 | void cDrawCarlRLScene::BuildStateNormGroups(int agent_id, Eigen::VectorXi& out_groups) const 108 | { 109 | GetCarlRLScene()->BuildStateNormGroups(agent_id, out_groups); 110 | } 111 | 112 | void cDrawCarlRLScene::BuildGoalNormGroups(int agent_id, Eigen::VectorXi& out_groups) const 113 | { 114 | GetCarlRLScene()->BuildGoalNormGroups(agent_id, out_groups); 115 | } 116 | 117 | double cDrawCarlRLScene::CalcReward(int agent_id) const 118 | { 119 | return GetCarlRLScene()->CalcReward(agent_id); 120 | } 121 | 122 | double cDrawCarlRLScene::GetRewardMin(int agent_id) const 123 | { 124 | return GetCarlRLScene()->GetRewardMin(agent_id); 125 | } 126 | 127 | double cDrawCarlRLScene::GetRewardMax(int agent_id) const 128 | { 129 | return GetCarlRLScene()->GetRewardMax(agent_id); 130 | } 131 | 132 | double cDrawCarlRLScene::GetRewardFail(int agent_id) 133 | { 134 | return GetCarlRLScene()->GetRewardFail(agent_id); 135 | } 136 | 137 | double cDrawCarlRLScene::GetRewardSucc(int agent_id) 138 | { 139 | return GetCarlRLScene()->GetRewardSucc(agent_id); 140 | } 141 | 142 | bool cDrawCarlRLScene::IsEpisodeEnd() const 143 | { 144 | return GetCarlRLScene()->IsEpisodeEnd(); 145 | } 146 | 147 | cDrawCarlRLScene::eTerminate cDrawCarlRLScene::CheckTerminate(int agent_id) const 148 | { 149 | return GetCarlRLScene()->CheckTerminate(agent_id); 150 | } 151 | 152 | bool cDrawCarlRLScene::CheckValidEpisode() const 153 | { 154 | return GetCarlRLScene()->CheckValidEpisode(); 155 | } 156 | 157 | void cDrawCarlRLScene::SetMode(eMode mode) 158 | { 159 | return GetCarlRLScene()->SetMode(mode); 160 | } 161 | 162 | void cDrawCarlRLScene::SetSampleCount(int count) 163 | { 164 | return GetCarlRLScene()->SetSampleCount(count); 165 | } 166 | 167 | void cDrawCarlRLScene::LogVal(int agent_id, double val) 168 | { 169 | GetCarlRLScene()->LogVal(agent_id, val); 170 | } 171 | 172 | void cDrawCarlRLScene::LogGatingWeights(int agent_id, const std::vector& weights) 173 | { 174 | GetCarlRLScene()->LogGatingWeights(agent_id, weights); 175 | } 176 | 177 | void cDrawCarlRLScene::LogPrimitivesMeanStd(int agent_id, int num_primitives, const std::vector& means, const std::vector& stds) 178 | { 179 | GetCarlRLScene()->LogPrimitivesMeanStd(agent_id, num_primitives, means, stds); 180 | } 181 | 182 | std::string cDrawCarlRLScene::GetName() const 183 | { 184 | return GetCarlRLScene()->GetName(); 185 | } 186 | 187 | void cDrawCarlRLScene::BuildScene(std::shared_ptr& out_scene) const 188 | { 189 | out_scene = std::shared_ptr(new cCarlRLSceneSimChar()); 190 | } 191 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlRLScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "DrawCarlScene.h" 4 | #include "CarlRLScene.h" 5 | #include "CarlRLSceneSimChar.h" 6 | #include "scenes/SceneSimChar.h" 7 | 8 | class cDrawCarlRLScene: virtual public cCarlRLScene, virtual public cDrawCarlScene 9 | { 10 | public: 11 | virtual ~cDrawCarlRLScene(); 12 | 13 | virtual void Init(); 14 | 15 | virtual int GetNumAgents() const; 16 | virtual bool NeedNewAction(int agent_id) const; 17 | virtual void RecordState(int agent_id, Eigen::VectorXd& out_state) const; 18 | virtual void RecordGoal(int agent_id, Eigen::VectorXd& out_goal) const; 19 | virtual void RecordPhase(int agent_id, double& out_phase) const; 20 | virtual void RecordMotionLabel(int agent_id, int& out_motion_label) const; 21 | virtual void RecordGoalTarget(int agent_id, Eigen::VectorXd& out_goal) const; 22 | virtual void SetAction(int agent_id, const Eigen::VectorXd& action); 23 | virtual bool SwitchMotion(int agent_id, int target_motion, double target_phase); 24 | 25 | virtual eActionSpace GetActionSpace(int agent_id) const; 26 | virtual int GetStateSize(int agent_id) const; 27 | virtual int GetGoalSize(int agent_id) const; 28 | virtual int GetActionSize(int agent_id) const; 29 | virtual int GetNumActions(int agent_id) const; 30 | 31 | virtual void BuildStateOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const; 32 | virtual void BuildGoalOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const; 33 | virtual void BuildActionOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const; 34 | virtual void BuildActionBounds(int agent_id, Eigen::VectorXd& out_min, Eigen::VectorXd& out_max) const; 35 | 36 | virtual void BuildStateNormGroups(int agent_id, Eigen::VectorXi& out_groups) const; 37 | virtual void BuildGoalNormGroups(int agent_id, Eigen::VectorXi& out_groups) const; 38 | 39 | virtual double CalcReward(int agent_id) const; 40 | virtual double GetRewardMin(int agent_id) const; 41 | virtual double GetRewardMax(int agent_id) const; 42 | virtual double GetRewardFail(int agent_id); 43 | virtual double GetRewardSucc(int agent_id); 44 | 45 | virtual bool IsEpisodeEnd() const; 46 | virtual eTerminate CheckTerminate(int agent_id) const; 47 | virtual bool CheckValidEpisode() const; 48 | virtual void SetMode(eMode mode); 49 | 50 | virtual void SetSampleCount(int count); 51 | virtual void LogVal(int agent_id, double val); 52 | virtual void LogGatingWeights(int agent_id, const std::vector& weights); 53 | virtual void LogPrimitivesMeanStd(int agent_id, int num_primitives, const std::vector& means, const std::vector& stds); 54 | 55 | virtual std::string GetName() const; 56 | 57 | protected: 58 | 59 | cDrawCarlRLScene(); 60 | virtual cRLScene* GetRLScene() const = 0; 61 | virtual cCarlRLScene* GetCarlRLScene() const = 0; 62 | virtual void BuildScene(std::shared_ptr& out_scene) const; 63 | 64 | std::shared_ptr mCarlScene; 65 | }; -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlScene.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawCarlScene.h" 2 | #include "util/MathUtil.h" 3 | 4 | // camera attributes 5 | const tVector gCameraPosition = tVector(20, 0, 0, 0); 6 | const tVector gCameraFocus = tVector(0, gCameraPosition[1], gCameraPosition[2], 0.0); 7 | const tVector gCameraUp = tVector(0, 1, 0, 0); 8 | const double gViewWidth = 12; 9 | const double gViewHeight = gViewWidth * 9.0 / 16.0; 10 | const double gViewNearZ = 0.1; 11 | const double gViewFarZ = 500; 12 | 13 | extern int g_ScreenWidth; 14 | extern int g_ScreenHeight; 15 | 16 | cDrawCarlScene::cDrawCarlScene() : cDrawScene::cDrawScene() 17 | { 18 | mCamPosition = gCameraPosition; 19 | mCamUp = gCameraUp; 20 | mCamZoom = 0; 21 | } 22 | 23 | cDrawCarlScene::~cDrawCarlScene() 24 | { 25 | } 26 | 27 | void cDrawCarlScene::ParseArgs(const std::shared_ptr& parser) 28 | { 29 | cDrawScene::ParseArgs(parser); 30 | 31 | ParseCamView(parser); 32 | } 33 | 34 | void cDrawCarlScene::ParseCamView(const std::shared_ptr& parser) 35 | { 36 | std::vector values; 37 | parser->ParseDoubles("cam_position", values); 38 | if (values.size() >= 3) 39 | { 40 | mCamPosition[0] = values[0]; 41 | mCamPosition[1] = values[1]; 42 | mCamPosition[2] = values[2]; 43 | mCamPosition = mCamPosition.normalized() * (gCameraPosition - gCameraFocus).norm(); 44 | } 45 | parser->ParseDoubles("cam_up", values); 46 | if (values.size() >= 3) 47 | { 48 | mCamUp[0] = values[0]; 49 | mCamUp[1] = values[1]; 50 | mCamUp[2] = values[2]; 51 | } 52 | parser->ParseDouble("cam_zoom", mCamZoom); 53 | } 54 | 55 | void cDrawCarlScene::InitCamera() 56 | { 57 | cCamera::eProj proj = cCamera::eProjPerspective; 58 | mCamera = cCamera(proj, mCamPosition, gCameraFocus, mCamUp, 59 | gViewWidth, gViewHeight, gViewNearZ, gViewFarZ); 60 | 61 | // Hack: Set camera zoom 62 | const float zoom_level = 0.05f; 63 | int n_steps = ceil(mCamZoom / zoom_level); 64 | for (int i = 0; i < n_steps; ++i) 65 | { 66 | if (zoom_level > 0) 67 | { 68 | mCamera.MouseClick(4, 0, 0, 0); 69 | } 70 | else 71 | { 72 | mCamera.MouseClick(3, 0, 0, 0); 73 | } 74 | } 75 | } 76 | 77 | void cDrawCarlScene::Reshape(int w, int h) 78 | { 79 | cDrawScene::Reshape(w, h); 80 | 81 | g_ScreenWidth = w; 82 | g_ScreenHeight = h; 83 | } 84 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "scenes/DrawScene.h" 4 | 5 | class cDrawCarlScene: virtual public cDrawScene 6 | { 7 | public: 8 | 9 | ~cDrawCarlScene(); 10 | virtual void ParseArgs(const std::shared_ptr& parser); 11 | 12 | protected: 13 | 14 | cDrawCarlScene(); 15 | virtual void ParseCamView(const std::shared_ptr& parser); 16 | virtual void Reshape(int w, int h); 17 | virtual void InitCamera(); 18 | 19 | tVector mCamPosition; 20 | tVector mCamUp; 21 | double mCamZoom; 22 | }; 23 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlSceneKinChar.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "DrawCarlScene.h" 4 | #include "CarlSceneKinChar.h" 5 | 6 | class cDrawCarlSceneKinChar : virtual public cDrawCarlScene 7 | { 8 | public: 9 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 10 | 11 | cDrawCarlSceneKinChar(); 12 | virtual ~cDrawCarlSceneKinChar(); 13 | 14 | virtual void Init(); 15 | virtual void Reset(); 16 | virtual void Clear(); 17 | virtual void Update(double time_elapsed); 18 | 19 | virtual void Keyboard(unsigned char key, double device_x, double device_y); 20 | 21 | std::string GetName() const; 22 | 23 | protected: 24 | std::shared_ptr mScene; 25 | bool mEnableDrawGaitPattern; 26 | bool mEnableDrawCharacterBody; 27 | std::vector mColors; 28 | std::vector> mGaits; 29 | std::vector mGaitJoints; 30 | 31 | virtual void BuildScene(std::shared_ptr& out_scene) const; 32 | virtual void SetupScene(std::shared_ptr& out_scene); 33 | virtual void UpdateScene(double time_elapsed); 34 | 35 | virtual tVector GetCamTrackPos() const; 36 | virtual tVector GetCamStillPos() const; 37 | virtual tVector GetDefaultCamFocus() const; 38 | 39 | virtual void DrawGround() const; 40 | virtual void DrawGround3D() const; 41 | virtual void DrawCharacters() const; 42 | virtual void DrawInfo() const; 43 | virtual void DrawInfoText() const; 44 | virtual void DrawInfoGaitPattern() const; 45 | virtual void DrawMisc() const; 46 | virtual void DrawEndEffectors() const; 47 | 48 | virtual void ToggleGaitPattern(); 49 | virtual void ToggleDrawCharacterBody(); 50 | 51 | virtual void InitGaits(); 52 | virtual void UpdateGaits(); 53 | }; 54 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlSceneTaskHeading.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawCarlSceneTaskHeading.h" 2 | #include 3 | #include "render/DrawCharacter.h" 4 | #include "render/DrawUtilExtend.h" 5 | #include "sim/SimBox.h" 6 | #include "scenes/SceneSimChar.h" 7 | #include "util/json/json.h" 8 | #include "util/MathUtilExtend.h" 9 | #include "util/FileUtilExtend.h" 10 | #include "util/JsonUtil.h" 11 | 12 | extern int g_ScreenWidth; 13 | extern int g_ScreenHeight; 14 | 15 | const tVector gCurrentHeadingArrowColor = tVector(1, 0, 0, 0.5); 16 | const tVector gTargetHeadingArrowColor = tVector(0, 1, 0, 0.5); 17 | const double gMoveSpeed = 0.1; 18 | 19 | cDrawCarlSceneTaskHeading::cDrawCarlSceneTaskHeading() 20 | { 21 | mCtrlMode = CtrlMode::eManual; 22 | mEnableRandomHeading = false; 23 | } 24 | 25 | cDrawCarlSceneTaskHeading::~cDrawCarlSceneTaskHeading() 26 | { 27 | } 28 | 29 | void cDrawCarlSceneTaskHeading::ParseArgs(const std::shared_ptr& parser) 30 | { 31 | cDrawSceneSimChar::ParseArgs(parser); 32 | cDrawCarlRLScene::ParseArgs(parser); 33 | 34 | std::string path_filename; 35 | parser->ParseString("path_filename", path_filename); 36 | std::vector ctrl_ptrs = cPathFollower::ParsePathCtrlPoints(path_filename); 37 | mPathFollower.Init(ctrl_ptrs); 38 | } 39 | 40 | void cDrawCarlSceneTaskHeading::Init() 41 | { 42 | cDrawSceneSimChar::Init(); 43 | cRLScene::Init(); 44 | 45 | cCarlSceneTaskHeading* scene = dynamic_cast(mScene.get()); 46 | scene->EnableRandHeading(mEnableRandomHeading); 47 | } 48 | 49 | void cDrawCarlSceneTaskHeading::Clear() 50 | { 51 | cDrawSceneSimChar::Clear(); 52 | cDrawCarlRLScene::Clear(); 53 | mPathFollower.Clear(); 54 | } 55 | 56 | bool cDrawCarlSceneTaskHeading::IsEpisodeEnd() const 57 | { 58 | return cDrawCarlRLScene::IsEpisodeEnd(); 59 | } 60 | 61 | bool cDrawCarlSceneTaskHeading::CheckValidEpisode() const 62 | { 63 | return cDrawCarlRLScene::CheckValidEpisode(); 64 | } 65 | 66 | void cDrawCarlSceneTaskHeading::Update(double time_elapsed) 67 | { 68 | cDrawSceneSimChar::Update(time_elapsed); 69 | 70 | if (mCtrlMode == CtrlMode::ePathFollow) 71 | { 72 | mPathFollower.Update(mScene->GetCharPos()); 73 | cCarlSceneTaskHeading* scene = dynamic_cast(mScene.get()); 74 | double heading = CalcHeading(); 75 | scene->SetTargetHeading(heading); 76 | } 77 | } 78 | 79 | void cDrawCarlSceneTaskHeading::Keyboard(unsigned char key, double device_x, double device_y) 80 | { 81 | switch (key) 82 | { 83 | case 's': 84 | ToggleCtrlMode(); 85 | break; 86 | case 'q': 87 | MoveTargetHeading(gMoveSpeed * 5); 88 | break; 89 | case 'e': 90 | MoveTargetHeading(-gMoveSpeed * 5); 91 | break; 92 | case 'a': 93 | MoveTargetHeading(gMoveSpeed); 94 | break; 95 | case 'd': 96 | MoveTargetHeading(-gMoveSpeed); 97 | break; 98 | default: 99 | cDrawSceneSimChar::Keyboard(key, device_x, device_y); 100 | break; 101 | } 102 | } 103 | 104 | std::string cDrawCarlSceneTaskHeading::GetName() const 105 | { 106 | return cDrawCarlRLScene::GetName(); 107 | } 108 | 109 | cRLScene* cDrawCarlSceneTaskHeading::GetRLScene() const 110 | { 111 | return dynamic_cast(mScene.get()); 112 | } 113 | 114 | cCarlRLScene* cDrawCarlSceneTaskHeading::GetCarlRLScene() const 115 | { 116 | return dynamic_cast(mScene.get()); 117 | } 118 | 119 | void cDrawCarlSceneTaskHeading::ToggleCtrlMode() 120 | { 121 | mCtrlMode = (CtrlMode)((mCtrlMode + 1) % CtrlMode::eMaxMode); 122 | if (mCtrlMode == CtrlMode::eManual) 123 | { 124 | printf("Ctrl Mode: Manual\n"); 125 | } 126 | else if (mCtrlMode == CtrlMode::eRandom) 127 | { 128 | printf("Ctrl Mode: Random\n"); 129 | } 130 | else if (mCtrlMode == CtrlMode::ePathFollow) 131 | { 132 | printf("Ctrl Mode: Path Following\n"); 133 | } 134 | 135 | cCarlSceneTaskHeading* scene = dynamic_cast(mScene.get()); 136 | mEnableRandomHeading = (mCtrlMode == CtrlMode::eRandom); 137 | scene->EnableRandHeading(mEnableRandomHeading); 138 | } 139 | 140 | void cDrawCarlSceneTaskHeading::MoveTargetHeading(double delta) 141 | { 142 | cCarlSceneTaskHeading* scene = dynamic_cast(mScene.get()); 143 | double heading = scene->GetTargetHeading(); 144 | heading += delta; 145 | heading = cMathUtilExtend::ClampEuler(heading); 146 | scene->SetTargetHeading(heading); 147 | } 148 | 149 | void cDrawCarlSceneTaskHeading::DrawInfo() const 150 | { 151 | DrawInfoText(); 152 | } 153 | 154 | void cDrawCarlSceneTaskHeading::DrawInfoText() const 155 | { 156 | cDrawUtilExtend::BeginDrawString(); 157 | { 158 | std::string mode_str = ""; 159 | if (mCtrlMode == CtrlMode::eManual) 160 | { 161 | mode_str = "Manual"; 162 | } 163 | else if (mCtrlMode == CtrlMode::eRandom) 164 | { 165 | mode_str = "Random"; 166 | } 167 | else if (mCtrlMode == CtrlMode::ePathFollow) 168 | { 169 | mode_str = "FollowPath"; 170 | } 171 | 172 | std::shared_ptr scene = std::dynamic_pointer_cast(mScene); 173 | double reward = scene->CalcReward(0); 174 | double target_heading = scene->GetTargetHeading(); 175 | 176 | char str[64]; 177 | float screen_y = g_ScreenHeight * 0.95; 178 | float screen_x = g_ScreenHeight * 0.02; 179 | sprintf(str, "Mode: %s\nTarget heading: %.3f rad\nReward: %.3f", mode_str.c_str(), target_heading, reward); 180 | cDrawUtilExtend::DrawString(screen_x, screen_y, str); 181 | 182 | const std::shared_ptr sim_char = mScene->GetCharacter(); 183 | tVector v_com = sim_char->CalcCOMVel().normalized(); 184 | double heading = atan2(-v_com[2], v_com[0]); 185 | 186 | sprintf(str, "Heading = %.2f rad", heading); 187 | 188 | tVector world_pos = sim_char->CalcCOM(); 189 | world_pos[1] += 0.4; 190 | tVector screen_pos = cMathUtilExtend::World2Screen(mCamera, world_pos); 191 | screen_pos[0] -= g_ScreenWidth * 0.1f; 192 | cDrawUtilExtend::DrawString(screen_pos[0], screen_pos[1], str, tVector(0, 0, 0, 1)); 193 | } 194 | cDrawUtilExtend::EndDrawString(); 195 | } 196 | 197 | void cDrawCarlSceneTaskHeading::DrawMisc() const 198 | { 199 | if (mEnableTrace) 200 | { 201 | DrawTrace(); 202 | } 203 | 204 | if (mCtrlMode == CtrlMode::ePathFollow) 205 | { 206 | mPathFollower.Draw(); 207 | } 208 | 209 | cDrawUtilExtend::DrawXyzAxes(); 210 | } 211 | 212 | void cDrawCarlSceneTaskHeading::BuildScene(std::shared_ptr& out_scene) const 213 | { 214 | out_scene = std::shared_ptr(new cCarlSceneTaskHeading()); 215 | } 216 | 217 | void cDrawCarlSceneTaskHeading::DrawCharacters() const 218 | { 219 | cDrawSceneSimChar::DrawCharacters(); 220 | DrawHeading(); 221 | } 222 | 223 | void cDrawCarlSceneTaskHeading::DrawHeading() const 224 | { 225 | std::shared_ptr scene = std::dynamic_pointer_cast(GetScene()); 226 | const std::shared_ptr character = scene->GetCharacter(); 227 | const double target_heading = scene->GetTargetHeading(); 228 | const double arrow_size = 0.2; 229 | tVector delta_y = tVector(0, 0.0001, 0, 0); 230 | tVector root_pos = character->GetRootPos(); 231 | tVector start, end; 232 | 233 | // Draw target heading 234 | start = tVector(root_pos[0], 0, root_pos[2], 0) + delta_y * 1; 235 | end = tVector(cos(target_heading), 0, -sin(target_heading), 0) + start; 236 | cDrawUtil::SetColor(gTargetHeadingArrowColor); 237 | cDrawUtilExtend::DrawArrow2D(start, end, arrow_size); 238 | 239 | // Draw center-of-mass velocity 240 | tVector v_com = character->CalcCOMVel(); 241 | v_com[1] = 0; 242 | v_com[3] = 0; 243 | start = tVector(root_pos[0], 0, root_pos[2], 0) + delta_y * 2; 244 | end = v_com + start; 245 | cDrawUtil::SetColor(gCurrentHeadingArrowColor); 246 | cDrawUtilExtend::DrawArrow2D(start, end, arrow_size); 247 | } 248 | 249 | void cDrawCarlSceneTaskHeading::ResetScene() 250 | { 251 | cDrawSceneSimChar::ResetScene(); 252 | mPathFollower.Reset(); 253 | } 254 | 255 | double cDrawCarlSceneTaskHeading::CalcHeading() const 256 | { 257 | tVector dir = mPathFollower.GetProgressPoint() - mScene->GetCharPos(); 258 | dir = dir.normalized(); 259 | double heading = std::atan2(-dir[2], dir[0]); 260 | return heading; 261 | } 262 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlSceneTaskHeading.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "DrawCarlRLScene.h" 4 | #include "CarlSceneTaskHeading.h" 5 | #include "scenes/DrawSceneSimChar.h" 6 | #include "util/PathFollower.h" 7 | 8 | class cDrawCarlSceneTaskHeading : virtual public cDrawCarlRLScene, virtual public cDrawSceneSimChar 9 | { 10 | public: 11 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 12 | 13 | cDrawCarlSceneTaskHeading(); 14 | virtual ~cDrawCarlSceneTaskHeading(); 15 | 16 | virtual void ParseArgs(const std::shared_ptr& parser); 17 | 18 | virtual void Init(); 19 | virtual void Clear(); 20 | virtual bool IsEpisodeEnd() const; 21 | virtual bool CheckValidEpisode() const; 22 | 23 | virtual void Update(double time_elapsed); 24 | virtual void Keyboard(unsigned char key, double device_x, double device_y); 25 | 26 | virtual std::string GetName() const; 27 | 28 | protected: 29 | 30 | enum CtrlMode 31 | { 32 | eManual, 33 | eRandom, 34 | ePathFollow, 35 | eMaxMode 36 | }; 37 | 38 | CtrlMode mCtrlMode; 39 | bool mEnableRandomHeading; 40 | cPathFollower mPathFollower; 41 | 42 | virtual cRLScene* GetRLScene() const; 43 | virtual cCarlRLScene* GetCarlRLScene() const; 44 | virtual void BuildScene(std::shared_ptr& out_scene) const; 45 | 46 | virtual void ToggleCtrlMode(); 47 | virtual void MoveTargetHeading(double delta); 48 | 49 | virtual void DrawInfo() const; 50 | virtual void DrawInfoText() const; 51 | virtual void DrawMisc() const; 52 | virtual void DrawCharacters() const; 53 | virtual void DrawHeading() const; 54 | virtual void ResetScene(); 55 | 56 | virtual double CalcHeading() const; 57 | }; 58 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlSceneTaskImitate.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "DrawCarlRLScene.h" 4 | #include "CarlSceneTaskImitate.h" 5 | #include "scenes/DrawSceneSimChar.h" 6 | #include "anim/KinCharacter.h" 7 | 8 | class cDrawCarlSceneTaskImitate : virtual public cDrawCarlRLScene, virtual public cDrawSceneSimChar 9 | { 10 | public: 11 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 12 | 13 | cDrawCarlSceneTaskImitate(); 14 | virtual ~cDrawCarlSceneTaskImitate(); 15 | 16 | virtual void ParseArgs(const std::shared_ptr& parser); 17 | 18 | virtual void Init(); 19 | virtual void Clear(); 20 | virtual void Update(double timestep); 21 | virtual bool IsEpisodeEnd() const; 22 | virtual bool CheckValidEpisode() const; 23 | 24 | virtual void Keyboard(unsigned char key, double device_x, double device_y); 25 | virtual void DrawKinChar(bool enable); 26 | 27 | virtual std::string GetName() const; 28 | 29 | protected: 30 | 31 | bool mDrawKinChar; 32 | bool mEnableRandomAction; 33 | bool mEnableSyncRootInUpdate; 34 | std::vector> mKinGaits; 35 | std::vector> mSimGaits; 36 | 37 | virtual cRLScene* GetRLScene() const; 38 | virtual cCarlRLScene* GetCarlRLScene() const; 39 | virtual const std::shared_ptr& GetKinChar() const; 40 | 41 | virtual void BuildScene(std::shared_ptr& out_scene) const; 42 | virtual void DrawCharacters() const; 43 | virtual void DrawKinCharacters() const; 44 | virtual void DrawKinCharacter(const std::shared_ptr& kin_char) const; 45 | virtual void DrawInfo() const; 46 | virtual void DrawInfoText() const; 47 | virtual void DrawGaitPatternInfo() const; 48 | 49 | virtual void ToggleRandomAction(); 50 | virtual void ToggleSyncRootPosition(); 51 | 52 | virtual void InitGaits(); 53 | virtual void UpdateGait(); 54 | }; 55 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlSceneTaskSpeed.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawCarlSceneTaskSpeed.h" 2 | #include 3 | #include "CarlSceneTaskSpeed.h" 4 | #include "render/DrawCharacter.h" 5 | #include "render/DrawUtilExtend.h" 6 | #include "sim/DeepMimicCharController.h" 7 | #include "scenes/SceneSimChar.h" 8 | #include "util/json/json.h" 9 | #include "util/FileUtilExtend.h" 10 | #include "util/JsonUtil.h" 11 | #include "util/MathUtilExtend.h" 12 | 13 | extern int g_ScreenWidth; 14 | extern int g_ScreenHeight; 15 | 16 | const tVector gCurrentVelocityArrowColor = tVector(1, 0, 0, 0.5); 17 | const tVector gTargetVelocityArrowColor = tVector(0, 1, 0, 0.5); 18 | const double gMoveSpeed = 0.05; 19 | 20 | cDrawCarlSceneTaskSpeed::cDrawCarlSceneTaskSpeed() 21 | { 22 | mCtrlMode = CtrlMode::eManual; 23 | mEnableRandomVelocity = false; 24 | } 25 | 26 | cDrawCarlSceneTaskSpeed::~cDrawCarlSceneTaskSpeed() 27 | { 28 | } 29 | 30 | void cDrawCarlSceneTaskSpeed::ParseArgs(const std::shared_ptr& parser) 31 | { 32 | cDrawSceneSimChar::ParseArgs(parser); 33 | cDrawCarlRLScene::ParseArgs(parser); 34 | } 35 | 36 | void cDrawCarlSceneTaskSpeed::Init() 37 | { 38 | cDrawSceneSimChar::Init(); 39 | cDrawCarlRLScene::Init(); 40 | 41 | cCarlSceneTaskSpeed* scene = dynamic_cast(mScene.get()); 42 | scene->EnableRandVelocity(mEnableRandomVelocity); 43 | scene->SetTargetVelocity(1.0); 44 | } 45 | 46 | void cDrawCarlSceneTaskSpeed::Clear() 47 | { 48 | cDrawSceneSimChar::Clear(); 49 | cDrawCarlRLScene::Clear(); 50 | } 51 | 52 | bool cDrawCarlSceneTaskSpeed::IsEpisodeEnd() const 53 | { 54 | return cDrawCarlRLScene::IsEpisodeEnd(); 55 | } 56 | 57 | bool cDrawCarlSceneTaskSpeed::CheckValidEpisode() const 58 | { 59 | return cDrawCarlRLScene::CheckValidEpisode(); 60 | } 61 | 62 | void cDrawCarlSceneTaskSpeed::Update(double time_elapsed) 63 | { 64 | cDrawSceneSimChar::Update(time_elapsed); 65 | } 66 | 67 | void cDrawCarlSceneTaskSpeed::Keyboard(unsigned char key, double device_x, double device_y) 68 | { 69 | switch (key) 70 | { 71 | case 's': 72 | ToggleCtrlMode(); 73 | break; 74 | case 'a': 75 | AddTargetVelocity(gMoveSpeed); 76 | break; 77 | case 'd': 78 | AddTargetVelocity(-gMoveSpeed); 79 | break; 80 | case 'q': 81 | AddTargetVelocity(gMoveSpeed * 10); 82 | break; 83 | case 'e': 84 | AddTargetVelocity(-gMoveSpeed * 10); 85 | break; 86 | default: 87 | cDrawSceneSimChar::Keyboard(key, device_x, device_y); 88 | break; 89 | } 90 | } 91 | 92 | std::string cDrawCarlSceneTaskSpeed::GetName() const 93 | { 94 | return cDrawCarlRLScene::GetName(); 95 | } 96 | 97 | cRLScene* cDrawCarlSceneTaskSpeed::GetRLScene() const 98 | { 99 | return dynamic_cast(mScene.get()); 100 | } 101 | 102 | cCarlRLScene* cDrawCarlSceneTaskSpeed::GetCarlRLScene() const 103 | { 104 | return dynamic_cast(mScene.get()); 105 | } 106 | 107 | void cDrawCarlSceneTaskSpeed::ToggleCtrlMode() 108 | { 109 | mCtrlMode = (CtrlMode)((mCtrlMode + 1) % CtrlMode::eMaxMode); 110 | if (mCtrlMode == CtrlMode::eManual) 111 | { 112 | printf("Ctrl Mode: Manual\n"); 113 | } 114 | else if (mCtrlMode == CtrlMode::eRandom) 115 | { 116 | printf("Ctrl Mode: Random\n"); 117 | } 118 | 119 | cCarlSceneTaskSpeed* scene = dynamic_cast(mScene.get()); 120 | mEnableRandomVelocity = (mCtrlMode == CtrlMode::eRandom); 121 | scene->EnableRandVelocity(mEnableRandomVelocity); 122 | } 123 | 124 | void cDrawCarlSceneTaskSpeed::AddTargetVelocity(double delta) 125 | { 126 | cCarlSceneTaskSpeed* scene = dynamic_cast(mScene.get()); 127 | double velocity = scene->GetTargetVelocity(); 128 | velocity += delta; 129 | scene->SetTargetVelocity(velocity); 130 | } 131 | 132 | void cDrawCarlSceneTaskSpeed::DrawInfo() const 133 | { 134 | DrawInfoText(); 135 | } 136 | 137 | void cDrawCarlSceneTaskSpeed::DrawInfoText() const 138 | { 139 | cDrawUtilExtend::BeginDrawString(); 140 | { 141 | std::string mode_str = ""; 142 | if (mCtrlMode == CtrlMode::eManual) 143 | { 144 | mode_str = "Manual"; 145 | } 146 | else if (mCtrlMode == CtrlMode::eRandom) 147 | { 148 | mode_str = "Random"; 149 | } 150 | 151 | const std::shared_ptr vel_scene = std::dynamic_pointer_cast(mScene); 152 | double target_velocity = vel_scene->GetTargetVelocity(); 153 | double reward = vel_scene->CalcReward(0); 154 | 155 | char str[64]; 156 | float screen_y = g_ScreenHeight * 0.95; 157 | float screen_x = g_ScreenHeight * 0.02; 158 | sprintf(str, "Mode: %s\nTarget speed: %.3f m/s\nReward: %.3f", mode_str.c_str(), target_velocity, reward); 159 | cDrawUtilExtend::DrawString(screen_x, screen_y, str); 160 | 161 | const std::shared_ptr sim_char = mScene->GetCharacter(); 162 | tVector world_pos = sim_char->CalcCOM(); 163 | world_pos[1] += 0.4; 164 | float speed = sim_char->CalcCOMVel().norm(); 165 | sprintf(str, "Speed = %.2f m/s", speed); 166 | tVector screen_pos = cMathUtilExtend::World2Screen(mCamera, world_pos); 167 | screen_pos[0] -= g_ScreenWidth * 0.1f; 168 | cDrawUtilExtend::DrawString(screen_pos[0], screen_pos[1], str, tVector(0, 0, 0, 1)); 169 | } 170 | cDrawUtilExtend::EndDrawString(); 171 | } 172 | 173 | void cDrawCarlSceneTaskSpeed::DrawMisc() const 174 | { 175 | if (mEnableTrace) 176 | { 177 | DrawTrace(); 178 | } 179 | 180 | cDrawUtilExtend::DrawXyzAxes(); 181 | } 182 | 183 | void cDrawCarlSceneTaskSpeed::BuildScene(std::shared_ptr& out_scene) const 184 | { 185 | out_scene = std::shared_ptr(new cCarlSceneTaskSpeed()); 186 | } 187 | 188 | void cDrawCarlSceneTaskSpeed::DrawCharacters() const 189 | { 190 | cDrawSceneSimChar::DrawCharacters(); 191 | DrawHeading(); 192 | } 193 | 194 | void cDrawCarlSceneTaskSpeed::DrawHeading() const 195 | { 196 | std::shared_ptr scene = std::dynamic_pointer_cast(GetScene()); 197 | const std::shared_ptr character = scene->GetCharacter(); 198 | const double target_heading = scene->GetTargetHeading(); 199 | const double current_heading = character->CalcHeading(); 200 | const double target_velocity = scene->GetTargetVelocity(); 201 | const double current_velocity = character->CalcCOMVel().norm(); 202 | const double arrow_size = 0.2; 203 | tVector delta_y = tVector(0, 0.0001, 0, 0); 204 | tVector root_pos = character->GetRootPos(); 205 | tVector start, end; 206 | 207 | // Draw target velocity 208 | start = tVector(root_pos[0], 0, root_pos[2], 0) + delta_y * 2; 209 | end = tVector(cos(target_heading), 0, -sin(target_heading), 0) * sqrt(target_velocity) + start; 210 | cDrawUtil::SetColor(gTargetVelocityArrowColor); 211 | cDrawUtilExtend::DrawArrow2D(start, end, arrow_size); 212 | 213 | // Draw center-of-mass velocity 214 | tVector v_com_norm = character->CalcCOMVel().normalized(); 215 | v_com_norm[1] = 0; 216 | v_com_norm[3] = 0; 217 | start = tVector(root_pos[0], 0, root_pos[2], 0) + delta_y * 3; 218 | end = v_com_norm * sqrt(current_velocity) + start; 219 | cDrawUtil::SetColor(gCurrentVelocityArrowColor); 220 | cDrawUtilExtend::DrawArrow2D(start, end, arrow_size); 221 | } 222 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawCarlSceneTaskSpeed.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "DrawCarlRLScene.h" 4 | #include "CarlSceneTaskSpeed.h" 5 | #include "scenes/DrawSceneSimChar.h" 6 | 7 | class cDrawCarlSceneTaskSpeed : virtual public cDrawCarlRLScene, virtual public cDrawSceneSimChar 8 | { 9 | public: 10 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 11 | 12 | cDrawCarlSceneTaskSpeed(); 13 | virtual ~cDrawCarlSceneTaskSpeed(); 14 | 15 | virtual void ParseArgs(const std::shared_ptr& parser); 16 | 17 | virtual void Init(); 18 | virtual void Clear(); 19 | virtual bool IsEpisodeEnd() const; 20 | virtual bool CheckValidEpisode() const; 21 | 22 | virtual void Update(double time_elapsed); 23 | virtual void Keyboard(unsigned char key, double device_x, double device_y); 24 | 25 | virtual std::string GetName() const; 26 | 27 | protected: 28 | 29 | enum CtrlMode 30 | { 31 | eManual, 32 | eRandom, 33 | eMaxMode 34 | }; 35 | 36 | CtrlMode mCtrlMode; 37 | bool mEnableRandomVelocity; 38 | 39 | virtual cRLScene* GetRLScene() const; 40 | virtual cCarlRLScene* GetCarlRLScene() const; 41 | virtual void BuildScene(std::shared_ptr& out_scene) const; 42 | 43 | virtual void ToggleCtrlMode(); 44 | virtual void AddTargetVelocity(double delta); 45 | 46 | virtual void DrawInfo() const; 47 | virtual void DrawInfoText() const; 48 | virtual void DrawMisc() const; 49 | virtual void DrawCharacters() const; 50 | virtual void DrawHeading() const; 51 | }; 52 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawTMTSceneTemplateController.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "scenes/DrawCarlRLScene.h" 4 | #include "scenes/DrawSceneSimChar.h" 5 | #include "scenes/TMTSceneTemplateController.h" 6 | #include "util/CircularBuffer.h" 7 | 8 | class cDrawTMTSceneTemplateController : virtual public cDrawCarlRLScene, virtual public cDrawSceneSimChar 9 | { 10 | public: 11 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 12 | 13 | cDrawTMTSceneTemplateController(); 14 | virtual ~cDrawTMTSceneTemplateController(); 15 | 16 | virtual void Init(); 17 | virtual void Clear(); 18 | virtual bool IsEpisodeEnd() const; 19 | virtual bool CheckValidEpisode() const; 20 | 21 | virtual void Update(double time_elapsed); 22 | virtual void Keyboard(unsigned char key, double device_x, double device_y); 23 | 24 | virtual std::string GetName() const; 25 | 26 | protected: 27 | 28 | bool mEnableDrawPhaseInfo; 29 | cCircularBuffer> mPhaseLog; 30 | 31 | bool mEnableDrawContactsInfo; 32 | std::vector> mSimFootContactStates; 33 | 34 | virtual cRLScene* GetRLScene() const; 35 | virtual cCarlRLScene* GetCarlRLScene() const; 36 | virtual const std::shared_ptr& GetKinChar() const; 37 | 38 | virtual void CtrlHeading(double delta); // Horizontal Angle (x, z) 39 | 40 | virtual void DrawInfo() const; 41 | virtual void DrawInfoText() const; 42 | virtual void DrawPhaseInfo() const; 43 | virtual void DrawFootContactsInfo() const; 44 | virtual void DrawMisc() const; 45 | virtual void BuildScene(std::shared_ptr& out_scene) const; 46 | virtual void DrawCharacters() const; 47 | virtual void DrawKinCharacter() const; 48 | virtual void DrawHeading() const; 49 | virtual void DrawFootContactPoints() const; 50 | 51 | virtual void ResetScene(); 52 | virtual tVector GetCamTrackPos() const; 53 | 54 | virtual void InitFootContacts(); 55 | virtual void UpdateFootContacts(); 56 | }; 57 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawTMTSceneTransitionRecorder.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawTMTSceneTransitionRecorder.h" 2 | #include "TMTSceneTransitionRecorder.h" 3 | 4 | cDrawTMTSceneTransitionRecorder::cDrawTMTSceneTransitionRecorder() : cDrawTMTSceneTemplateController() 5 | { 6 | } 7 | 8 | cDrawTMTSceneTransitionRecorder::~cDrawTMTSceneTransitionRecorder() 9 | { 10 | } 11 | 12 | std::string cDrawTMTSceneTransitionRecorder::GetName() const 13 | { 14 | return "Transition Recorder"; 15 | } 16 | 17 | void cDrawTMTSceneTransitionRecorder::BuildScene(std::shared_ptr& out_scene) const 18 | { 19 | out_scene = std::shared_ptr(new cTMTSceneTransitionRecorder()); 20 | } 21 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/DrawTMTSceneTransitionRecorder.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "scenes/DrawTMTSceneTemplateController.h" 4 | 5 | class cDrawTMTSceneTransitionRecorder : virtual public cDrawTMTSceneTemplateController 6 | { 7 | public: 8 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 9 | 10 | cDrawTMTSceneTransitionRecorder(); 11 | virtual ~cDrawTMTSceneTransitionRecorder(); 12 | 13 | virtual std::string GetName() const; 14 | 15 | protected: 16 | 17 | virtual void BuildScene(std::shared_ptr& out_scene) const; 18 | 19 | }; 20 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/TMTSceneTemplateController.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "scenes/CarlRLSceneSimChar.h" 4 | #include "anim/CarlKinCharacter.h" 5 | #include "util/MotionTrajectory.h" 6 | 7 | class cTMTSceneTemplateController : virtual public cCarlRLSceneSimChar 8 | { 9 | public: 10 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 11 | 12 | enum eMotionType 13 | { 14 | eMotionTypeCanter, 15 | eMotionTypeJump, 16 | eMotionTypePace, 17 | eMotionTypeTrot, 18 | eMotionTypeGeneric, 19 | eMotionTypeMax 20 | }; 21 | 22 | enum eControlMode 23 | { 24 | ePhaseCtrl, 25 | eHighLevelCtrl 26 | }; 27 | 28 | cTMTSceneTemplateController(); 29 | virtual ~cTMTSceneTemplateController(); 30 | 31 | virtual void ParseArgs(const std::shared_ptr& parser); 32 | virtual void Init(); 33 | 34 | virtual const eMotionType GetMotionType() const; 35 | virtual const std::shared_ptr& GetKinChar() const; 36 | virtual const std::shared_ptr& GetKinChar(const int char_idx) const; 37 | virtual void EnableRandRotReset(bool enable); 38 | virtual bool EnabledRandRotReset() const; 39 | 40 | virtual bool SwitchMotion(int agent_id, int target_motion, double target_phase); 41 | 42 | virtual void SyncKinCharRoot(); 43 | virtual void SyncKinCharNewCycle(const cSimCharacter& sim_char, cCarlKinCharacter& out_kin_char) const; 44 | virtual void SetTargetVelocity(tVector vel); 45 | virtual tVector GetTargetVelocity() const; 46 | virtual tVector GetTargetPosition() const; 47 | virtual Eigen::VectorXd GetKinCharState() const; 48 | virtual tVector GetKinVcom() const; 49 | virtual double GetPhase() const; 50 | virtual double GetLocalPhase() const; 51 | virtual int GetMotion() const; 52 | virtual void SetTargetHeading(double heading); 53 | virtual double GetTargetHeading() const; 54 | virtual eControlMode GetControlMode() const; 55 | virtual std::string GetName() const; 56 | 57 | virtual void RecordPhase(int agent_id, double& out_phase) const; 58 | virtual void RecordGoal(int agent_id, Eigen::VectorXd& out_goal) const; 59 | virtual void RecordGoalTarget(int agent_id, Eigen::VectorXd& out_goal) const; 60 | virtual void RecordMotionLabel(int agent_id, int& out_motion_label) const; 61 | 62 | virtual int GetGoalSize(int agent_id) const; 63 | virtual void BuildGoalOffsetScale(int agent_id, Eigen::VectorXd& out_offset, Eigen::VectorXd& out_scale) const; 64 | virtual void BuildGoalNormGroups(int agent_id, Eigen::VectorXi& out_groups) const; 65 | virtual void ResolveCharGroundIntersect(); 66 | 67 | virtual int GetNumMotions() const; 68 | virtual double CalcReward(int agent_id) const; 69 | virtual void CtrlHeading(int agent_id, double delta); 70 | 71 | virtual double GetLossEnergyConsumption() const; 72 | virtual double GetLossContactForce() const; 73 | virtual double GetRewardControl() const; 74 | virtual double GetRewardSpeed() const; 75 | virtual double GetRewardHeading() const; 76 | virtual double GetRewardHeight() const; 77 | 78 | virtual bool CheckContactCycleStart() const; 79 | virtual std::vector GetFootContacts() const; 80 | virtual std::vector GetFootContactJoints() const; 81 | virtual int GetContactIndex() const; 82 | virtual double GetSimCharPhase() const; 83 | 84 | protected: 85 | 86 | std::vector mMotionFiles; 87 | std::vector mMotionNames; 88 | std::vector mTrajectoryFiles; 89 | std::vector> mKinChars; 90 | std::vector mMotionTrajectories; 91 | std::vector mGoalVector; 92 | std::vector mFootContactIndices; 93 | std::deque mContactCycleStartTimes; 94 | 95 | bool mEnableRandRotReset; 96 | bool mEnableRandRotation; 97 | bool mEnableRootRotFail; 98 | bool mSyncCharRootPos; 99 | bool mSyncCharRootRot; 100 | int mUpdateVelocityCount; 101 | int mUpdateTargetHeadingCount; 102 | int mUpdateContactCycleStartTimesCount; 103 | int mKinCharIndex; 104 | int mPrevContactIndex; 105 | double mNextKinCharPhase; 106 | double mNextEndTime; 107 | 108 | double mTargetHeading; 109 | tVector mTargetVelocity; 110 | tVector mTargetPosition; 111 | tVector mStartPosition; 112 | 113 | bool mSyncCharRootInUpdate; 114 | double mKinCharPhase; 115 | std::vector mKinCharMotionTypes; 116 | eControlMode mCtrlMode; 117 | 118 | virtual bool BuildCharacters(); 119 | virtual void BuildKinChars(); 120 | virtual bool BuildKinCharacter(int id, std::shared_ptr& out_char) const; 121 | virtual void BuildKinCharsVcomTable(std::string output_file); 122 | virtual void BuildMotionTrajectories(); 123 | virtual bool BuildController(const cCtrlBuilder::tCtrlParams& ctrl_params, std::shared_ptr& out_ctrl); 124 | virtual void InitFootContacts(); 125 | 126 | virtual void UpdateTimers(double timestep); 127 | virtual void UpdateCharacters(double timestep); 128 | virtual void UpdateKinChar(double timestep); 129 | virtual void UpdateTargetVelocity(double timestep); 130 | virtual void UpdateNextRandKinChar(bool rand_phase); 131 | 132 | virtual double GetKinTime() const; 133 | virtual void SyncCharacters(); 134 | virtual bool EnableSyncChar() const; 135 | virtual void ResetCharacters(); 136 | virtual void ResetKinChar(); 137 | virtual void ResetKinChar(int motion_idx, double phase); 138 | 139 | virtual bool HasFallen(const cSimCharacter& sim_char) const; 140 | virtual bool CheckRootRotFail(const cSimCharacter& sim_char, tVector target_velocity) const; 141 | 142 | virtual double CalcRewardVelocity(const cSimCharacter& sim_char, const tVector target_velocity) const; 143 | virtual double CalcRewardEnergyConsumption(const cSimCharacter& sim_char) const; 144 | virtual double CalcRewardContactForce(const cSimCharacter& sim_char) const; 145 | virtual double CalcRewardSpeed(const cSimCharacter& sim_char, const tVector target_velocity) const; 146 | virtual double CalcRewardHeading(const cSimCharacter& sim_char, const tVector target_velocity) const; 147 | virtual double CalcRewardHeight(const cSimCharacter& sim_char, const tVector target_position) const; 148 | }; 149 | -------------------------------------------------------------------------------- /TMT/CarlCore/scenes/TMTSceneTransitionRecorder.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "scenes/TMTSceneTemplateController.h" 4 | #include "util/tqdm.h" 5 | #include 6 | #include 7 | 8 | class cTMTSceneTransitionRecorder : virtual public cTMTSceneTemplateController 9 | { 10 | public: 11 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW 12 | 13 | cTMTSceneTransitionRecorder(); 14 | virtual ~cTMTSceneTransitionRecorder(); 15 | 16 | virtual void ParseArgs(const std::shared_ptr& parser); 17 | virtual void Init(); 18 | virtual std::string GetName() const; 19 | 20 | virtual bool CheckValidEpisode() const; 21 | virtual bool NeedNewAction(int agent_id) const; 22 | virtual void SetAction(int agent_id, const Eigen::VectorXd& action); 23 | 24 | protected: 25 | 26 | typedef struct 27 | { 28 | int srcMotion; 29 | int dstMotion; 30 | double srcPhase; 31 | double dstPhase; 32 | double transPhase; 33 | tVector srcVelocity; 34 | tVector dstVelocity; 35 | int transition_index; 36 | double transition_phase; 37 | bool isFallen; 38 | bool isValid; 39 | std::vector losses_energy; 40 | std::vector speed_rewards; 41 | std::vector heading_rewards; 42 | std::vector height_rewards; 43 | std::vector positions; 44 | std::vector> foot_contacts; 45 | } TransitionSample; 46 | 47 | int mExpIndex; 48 | int mUpdateCounter; 49 | int mNumCollectTransitionSamples; 50 | int mUpdateRecordCharStateCount; 51 | double mPreTransitionTime; 52 | double mPostTransitionTime; 53 | bool mHasSwitched; 54 | bool mHasVelocityExploded; 55 | bool mHasFallen; 56 | bool mNeedUpdateAction; 57 | std::string mOutputFilename; 58 | std::vector mTransitionSamples; 59 | tqdm mProgressBar; 60 | 61 | virtual void InitTransitionSample(int exp_id); 62 | virtual void BuildCollectTransitionTrajectories(); 63 | virtual void GenerateRandTransitionSample(TransitionSample &trans_sample) const; 64 | virtual void WriteCollectTransitionTrajectories(std::string output_filename, int start_idx, int end_idx); 65 | virtual bool CheckTransitionSuccess() const; 66 | virtual bool CheckCharacterFallen(const std::shared_ptr sim_char) const; 67 | 68 | virtual void UpdateCharacters(double timestep); 69 | virtual void PostUpdate(double timestep); 70 | virtual void ResetCharacters(); 71 | virtual void OnEvaluationEnd(); 72 | virtual void SyncPose(int kin_index, double motion_time); 73 | }; 74 | -------------------------------------------------------------------------------- /TMT/CarlCore/util/FileUtilExtend.cpp: -------------------------------------------------------------------------------- 1 | #include "FileUtilExtend.h" 2 | #include 3 | 4 | 5 | std::string cFileUtilExtend::GetCurrentTimeStamp() 6 | { 7 | time_t rawtime; 8 | struct tm * timeinfo; 9 | char buffer[80]; 10 | 11 | time (&rawtime); 12 | timeinfo = localtime(&rawtime); 13 | 14 | strftime(buffer, sizeof(buffer), "%Y%m%d_%H%M%S", timeinfo); 15 | 16 | return std::string(buffer); 17 | } 18 | -------------------------------------------------------------------------------- /TMT/CarlCore/util/FileUtilExtend.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "util/FileUtil.h" 4 | 5 | class cFileUtilExtend 6 | { 7 | public: 8 | 9 | static std::string GetCurrentTimeStamp(); 10 | 11 | protected: 12 | 13 | }; 14 | -------------------------------------------------------------------------------- /TMT/CarlCore/util/MathUtilExtend.cpp: -------------------------------------------------------------------------------- 1 | #include "MathUtilExtend.h" 2 | 3 | 4 | extern int g_ScreenWidth; 5 | extern int g_ScreenHeight; 6 | 7 | 8 | double cMathUtilExtend::ClampEuler(double theta_in) 9 | { 10 | double theta_out = theta_in; 11 | if (theta_out > M_PI) 12 | { 13 | theta_out -= 2 * M_PI; 14 | } 15 | else if (theta_out <= -M_PI) 16 | { 17 | theta_out += 2 * M_PI; 18 | } 19 | return theta_out; 20 | } 21 | 22 | tVector cMathUtilExtend::World2Screen(const cCamera& camera, const tVector& world_pos) 23 | { 24 | tVector view_pos = camera.BuildWorldViewMatrix() * tVector(world_pos[0], world_pos[1], world_pos[2], 1); 25 | tVector proj_pos = camera.BuildProjMatrix() * view_pos; 26 | tVector clip_pos = tVector(proj_pos) / proj_pos[3]; 27 | tVector screen_pos = tVector((clip_pos[0] + 1) * 0.5 * g_ScreenWidth, (clip_pos[1] + 1) * 0.5 * g_ScreenHeight, 0, 0); 28 | return screen_pos; 29 | } 30 | -------------------------------------------------------------------------------- /TMT/CarlCore/util/MathUtilExtend.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "util/MathUtil.h" 4 | #include "render/Camera.h" 5 | 6 | 7 | class cMathUtilExtend 8 | { 9 | public: 10 | 11 | static double ClampEuler(double theta_in); 12 | static tVector World2Screen(const cCamera& camera, const tVector& world_pos); 13 | 14 | }; 15 | -------------------------------------------------------------------------------- /TMT/CarlCore/util/MotionTrajectory.cpp: -------------------------------------------------------------------------------- 1 | #include "MotionTrajectory.h" 2 | #include "util/FileUtil.h" 3 | 4 | cMotionTrajectory::cMotionTrajectory() 5 | { 6 | mTimes.clear(); 7 | mCumulativeTimes.clear(); 8 | mVelocities.clear(); 9 | mLocalCyclePeaks.clear(); 10 | } 11 | 12 | cMotionTrajectory::~cMotionTrajectory() 13 | { 14 | } 15 | 16 | bool cMotionTrajectory::Load(std::string filename) 17 | { 18 | std::ifstream fin(filename); 19 | if (fin.is_open()) 20 | { 21 | // first line stores indices of cycle peaks 22 | std::string line; 23 | std::getline(fin, line); 24 | 25 | std::istringstream in(line); 26 | std::string token; 27 | mLocalCyclePeaks.clear(); 28 | while (std::getline(in, token, ' ')) 29 | { 30 | int val = std::stoi(token); 31 | // printf("%s %d\n", token.c_str(), val); 32 | if (val >= 0) 33 | { 34 | mLocalCyclePeaks.push_back(val); 35 | } 36 | } 37 | 38 | double t = 0; 39 | tVector v = tVector::Zero(); 40 | while (fin >> t >> v[0] >> v[1] >> v[2]) 41 | { 42 | mTimes.push_back(t); 43 | mVelocities.push_back(v); 44 | // printf("%.3f, %.3f, %.3f, %.3f\n", t, v[0], v[1], v[2]); 45 | } 46 | fin.close(); 47 | 48 | mCumulativeTimes.resize(mTimes.size(), 0); 49 | for (int i = 0; i < mTimes.size(); ++i) 50 | { 51 | if (i > 0) 52 | { 53 | mCumulativeTimes[i] = mCumulativeTimes[i-1] + mTimes[i]; 54 | } 55 | else 56 | { 57 | mCumulativeTimes[i] = mTimes[i]; 58 | } 59 | } 60 | 61 | return true; 62 | } 63 | else 64 | { 65 | return false; 66 | } 67 | } 68 | 69 | tVector cMotionTrajectory::Sample(double phase) const 70 | { 71 | tVector vel; 72 | int idx_lower = std::floor(phase * (mVelocities.size() - 1)); 73 | int idx_upper = std::ceil(phase * (mVelocities.size() - 1)); 74 | if (idx_lower != idx_upper) 75 | { 76 | double t = phase * (mVelocities.size() - 1) - idx_lower; 77 | vel = mVelocities[idx_lower] * (1 - t) + mVelocities[idx_upper] * t; 78 | } 79 | else 80 | { 81 | vel = mVelocities[idx_lower]; 82 | } 83 | return vel; 84 | } 85 | 86 | double cMotionTrajectory::GetDuration() const 87 | { 88 | return mCumulativeTimes.back(); 89 | } 90 | 91 | double cMotionTrajectory::GetPhaseStep(double timestep) const 92 | { 93 | double phase_step = timestep / GetDuration(); 94 | return phase_step; 95 | } 96 | 97 | double cMotionTrajectory::CalcLocalPhase(double phase) const 98 | { 99 | double local_phase = 0; 100 | if (mLocalCyclePeaks.size() > 1) 101 | { 102 | double pos = phase * mVelocities.size(); 103 | 104 | double cycle_length = 0; 105 | for (int i = 1; i < mLocalCyclePeaks.size() - 1; ++i) 106 | { 107 | cycle_length += mLocalCyclePeaks[i + 1] - mLocalCyclePeaks[i]; 108 | } 109 | cycle_length /= (mLocalCyclePeaks.size() - 2); 110 | 111 | std::vector distances; 112 | std::vector indices; 113 | for (int i = 0; i < mLocalCyclePeaks.size(); ++i) 114 | { 115 | double dist = pos - mLocalCyclePeaks[i]; 116 | distances.push_back(dist); 117 | if (dist > 0) 118 | { 119 | indices.push_back(i); 120 | } 121 | } 122 | 123 | if (indices.size() == 0) 124 | { 125 | local_phase = (cycle_length - (mLocalCyclePeaks[0] - pos)) / cycle_length; 126 | } 127 | else if (indices.size() == mLocalCyclePeaks.size()) 128 | { 129 | local_phase = (pos - mLocalCyclePeaks.back()) / cycle_length; 130 | } 131 | else 132 | { 133 | int idx = indices.back(); 134 | local_phase = distances[idx] / (mLocalCyclePeaks[idx+1] - mLocalCyclePeaks[idx]); 135 | } 136 | } 137 | else 138 | { 139 | local_phase = phase; 140 | } 141 | return local_phase; 142 | } 143 | -------------------------------------------------------------------------------- /TMT/CarlCore/util/MotionTrajectory.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "util/MathUtil.h" 4 | 5 | class cMotionTrajectory 6 | { 7 | public: 8 | 9 | cMotionTrajectory(); 10 | virtual ~cMotionTrajectory(); 11 | 12 | virtual bool Load(std::string filename); 13 | virtual tVector Sample(double phase) const; 14 | virtual double GetDuration() const; 15 | virtual double GetPhaseStep(double timestep) const; 16 | virtual double CalcLocalPhase(double phase) const; 17 | 18 | protected: 19 | 20 | std::vector mTimes; 21 | std::vector mCumulativeTimes; 22 | std::vector mVelocities; 23 | std::vector mLocalCyclePeaks; 24 | }; -------------------------------------------------------------------------------- /TMT/CarlCore/util/PathApproximator.cpp: -------------------------------------------------------------------------------- 1 | #include "PathApproximator.h" 2 | 3 | int BinarySearch(const std::vector& array, double value) 4 | { 5 | auto it = upper_bound(array.begin(), array.end(), value, [](double a, double b){ return a < b; }); 6 | int index = std::distance(array.begin(), it); 7 | return index; 8 | } 9 | 10 | PathApproximator::PathApproximator() 11 | { 12 | } 13 | 14 | PathApproximator::~PathApproximator() 15 | { 16 | } 17 | 18 | void PathApproximator::AddPoints(const std::vector& points) 19 | { 20 | mPoints.resize(points.size()); 21 | for (int i = 0; i < points.size(); ++i) 22 | { 23 | mPoints[i] = points[i]; 24 | } 25 | 26 | BuildIntegralDistances(); 27 | } 28 | 29 | void PathApproximator::BuildIntegralDistances() 30 | { 31 | mIntegralDistances.clear(); 32 | mIntegralDistances.resize(mPoints.size(), 0); 33 | for (int i = 1; i < mPoints.size(); ++i) 34 | { 35 | double distance = (mPoints[i] - mPoints[i - 1]).norm(); 36 | mIntegralDistances[i] = distance + mIntegralDistances[i - 1]; 37 | } 38 | } 39 | 40 | tVector PathApproximator::GetPoint(double t) const 41 | { 42 | assert(t >= 0 && t <= 1); 43 | float target_distance = GetTotalDistance() * t; 44 | int closest_index = BinarySearch(mIntegralDistances, target_distance); 45 | tVector point = mPoints[closest_index]; 46 | return point; 47 | } 48 | 49 | const std::vector& PathApproximator::GetPoints() const 50 | { 51 | return mPoints; 52 | } 53 | 54 | double PathApproximator::GetTotalDistance() const 55 | { 56 | return mIntegralDistances[mIntegralDistances.size() - 1]; 57 | } -------------------------------------------------------------------------------- /TMT/CarlCore/util/PathApproximator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "util/MathUtil.h" 5 | 6 | class PathApproximator 7 | { 8 | public: 9 | PathApproximator(); 10 | ~PathApproximator(); 11 | 12 | void AddPoints(const std::vector& points); 13 | tVector GetPoint(double t) const; 14 | const std::vector& GetPoints() const; 15 | double GetTotalDistance() const; 16 | 17 | private: 18 | std::vector mPoints; 19 | std::vector mIntegralDistances; 20 | 21 | void BuildIntegralDistances(); 22 | }; 23 | -------------------------------------------------------------------------------- /TMT/CarlCore/util/PathFollower.cpp: -------------------------------------------------------------------------------- 1 | #include "PathFollower.h" 2 | #include "render/DrawUtil.h" 3 | #include "util/MathUtilExtend.h" 4 | #include 5 | 6 | const int gNumLineSegments = 1000; 7 | const double gAgentDistance = 1.0; 8 | const double gProgressSpeed = 0.01; 9 | const tVector gLineColor = tVector(0, 0, 0, 0.5); 10 | const tVector gCtrlPtColor = tVector(0, 1, 0, 0.5); 11 | const tVector gProgressPtColor = tVector(1, 0, 0, 0.5); 12 | 13 | cPathFollower::cPathFollower() 14 | { 15 | mProgress = 0; 16 | } 17 | 18 | cPathFollower::~cPathFollower() 19 | { 20 | } 21 | 22 | void cPathFollower::Init(std::vector ctrl_pts) 23 | { 24 | mPath = std::shared_ptr(new PathApproximator()); 25 | mPath->AddPoints(ctrl_pts); 26 | } 27 | 28 | void cPathFollower::Update(tVector position) 29 | { 30 | double diff = (GetProgressPoint() - position).norm(); 31 | mProgress += (diff < gAgentDistance) ? gProgressSpeed : 0; 32 | if (mProgress >= 0.999) 33 | { 34 | mProgress = 0.999; 35 | } 36 | } 37 | 38 | void cPathFollower::Reset() 39 | { 40 | mProgress = 0.05; 41 | } 42 | 43 | void cPathFollower::Clear() 44 | { 45 | 46 | } 47 | 48 | tVector cPathFollower::GetProgressPoint() const 49 | { 50 | return mPath->GetPoint(mProgress); 51 | } 52 | 53 | double cPathFollower::GetResetHeading() const 54 | { 55 | tVector p0 = mPath->GetPoint(0); 56 | tVector p1 = mPath->GetPoint(gProgressSpeed); 57 | tVector heading_dir = (p1 - p0).normalized(); 58 | double heading = cMathUtilExtend::ClampEuler(atan2(-heading_dir[2], heading_dir[0])); 59 | return heading; 60 | } 61 | 62 | void cPathFollower::Draw() const 63 | { 64 | DrawPath(); 65 | DrawProgress(); 66 | } 67 | 68 | void cPathFollower::DrawPath() const 69 | { 70 | double step = 1.0 / (gNumLineSegments-1); 71 | std::vector points = mPath->GetPoints(); 72 | 73 | cDrawUtil::SetLineWidth(3.0f); 74 | cDrawUtil::SetColor(gLineColor); 75 | for (int i = 1; i < points.size(); ++i) 76 | { 77 | tVector a = points[i - 1]; 78 | tVector b = points[i]; 79 | a[1] += 0.01; 80 | b[1] += 0.01; 81 | cDrawUtil::DrawLine(a, b); 82 | } 83 | } 84 | 85 | void cPathFollower::DrawProgress() const 86 | { 87 | tVector p = mPath->GetPoint(mProgress); 88 | cDrawUtil::PushMatrixView(); 89 | cDrawUtil::SetColor(gProgressPtColor); 90 | cDrawUtil::Translate(p); 91 | cDrawUtil::DrawSphere(0.05f); 92 | cDrawUtil::PopMatrixView(); 93 | } 94 | 95 | std::vector cPathFollower::ParsePathCtrlPoints(std::string filename) 96 | { 97 | std::vector points; 98 | std::ifstream fin(filename); 99 | if (fin.is_open()) 100 | { 101 | tVector p = tVector::Zero(); 102 | while (fin >> p[0] >> p[1] >> p[2]) 103 | { 104 | points.push_back(p); 105 | } 106 | fin.close(); 107 | } 108 | else 109 | { 110 | printf("Path point file %s not found\n"); 111 | assert(0); 112 | } 113 | return points; 114 | } 115 | -------------------------------------------------------------------------------- /TMT/CarlCore/util/PathFollower.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "sim/SimObj.h" 4 | #include "util/PathApproximator.h" 5 | 6 | class cPathFollower 7 | { 8 | public: 9 | cPathFollower(); 10 | ~cPathFollower(); 11 | 12 | virtual void Init(std::vector ctrl_pts); 13 | virtual void Update(tVector position); 14 | virtual void Reset(); 15 | virtual void Clear(); 16 | 17 | virtual tVector GetProgressPoint() const; 18 | virtual double GetResetHeading() const; 19 | 20 | virtual void Draw() const; 21 | 22 | static std::vector ParsePathCtrlPoints(std::string filename); 23 | 24 | protected: 25 | 26 | double mProgress; 27 | 28 | virtual void DrawPath() const; 29 | virtual void DrawProgress() const; 30 | 31 | std::shared_ptr mPath; 32 | }; 33 | 34 | -------------------------------------------------------------------------------- /TMT/CarlCore/util/tqdm.h: -------------------------------------------------------------------------------- 1 | #ifndef TQDM_H 2 | #define TQDM_H 3 | #if _MSC_VER 4 | #include 5 | #define isatty _isatty 6 | #else 7 | #include 8 | #endif 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | class tqdm { 21 | private: 22 | // time, iteration counters and deques for rate calculations 23 | std::chrono::time_point t_first = std::chrono::system_clock::now(); 24 | std::chrono::time_point t_old = std::chrono::system_clock::now(); 25 | int n_old = 0; 26 | std::vector deq_t; 27 | std::vector deq_n; 28 | int nupdates = 0; 29 | int total_ = 0; 30 | int period = 1; 31 | unsigned int smoothing = 50; 32 | bool use_ema = true; 33 | float alpha_ema = 0.1; 34 | 35 | std::vector bars = {" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"}; 36 | 37 | bool in_screen = (system("test $STY") == 0); 38 | bool in_tmux = (system("test $TMUX") == 0); 39 | bool is_tty = isatty(1); 40 | bool use_colors = true; 41 | bool color_transition = true; 42 | int width = 40; 43 | 44 | std::string right_pad = "▏"; 45 | std::string label = ""; 46 | 47 | void hsv_to_rgb(float h, float s, float v, int& r, int& g, int& b) { 48 | if (s < 1e-6) { 49 | v *= 255.; 50 | r = v; g = v; b = v; 51 | } 52 | int i = (int)(h*6.0); 53 | float f = (h*6.)-i; 54 | int p = (int)(255.0*(v*(1.-s))); 55 | int q = (int)(255.0*(v*(1.-s*f))); 56 | int t = (int)(255.0*(v*(1.-s*(1.-f)))); 57 | v *= 255; 58 | i %= 6; 59 | int vi = (int)v; 60 | if (i == 0) { r = vi; g = t; b = p; } 61 | else if (i == 1) { r = q; g = vi; b = p; } 62 | else if (i == 2) { r = p; g = vi; b = t; } 63 | else if (i == 3) { r = p; g = q; b = vi; } 64 | else if (i == 4) { r = t; g = p; b = vi; } 65 | else if (i == 5) { r = vi; g = p; b = q; } 66 | } 67 | 68 | public: 69 | tqdm() { 70 | if (in_screen) { 71 | set_theme_basic(); 72 | color_transition = false; 73 | } else if (in_tmux) { 74 | color_transition = false; 75 | } 76 | } 77 | 78 | void reset() { 79 | t_first = std::chrono::system_clock::now(); 80 | t_old = std::chrono::system_clock::now(); 81 | n_old = 0; 82 | deq_t.clear(); 83 | deq_n.clear(); 84 | period = 1; 85 | nupdates = 0; 86 | total_ = 0; 87 | label = ""; 88 | } 89 | 90 | void set_theme_line() { bars = {"─", "─", "─", "╾", "╾", "╾", "╾", "━", "═"}; } 91 | void set_theme_circle() { bars = {" ", "◓", "◑", "◒", "◐", "◓", "◑", "◒", "#"}; } 92 | void set_theme_braille() { bars = {" ", "⡀", "⡄", "⡆", "⡇", "⡏", "⡟", "⡿", "⣿" }; } 93 | void set_theme_braille_spin() { bars = {" ", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠇", "⠿" }; } 94 | void set_theme_vertical() { bars = {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", "█"}; } 95 | void set_theme_basic() { 96 | bars = {" ", " ", " ", " ", " ", " ", " ", " ", "#"}; 97 | right_pad = "|"; 98 | } 99 | void set_label(std::string label_) { label = label_; } 100 | void disable_colors() { 101 | color_transition = false; 102 | use_colors = false; 103 | } 104 | 105 | void finish() { 106 | progress(total_,total_); 107 | printf("\n"); 108 | fflush(stdout); 109 | } 110 | void progress(int curr, int tot) { 111 | if(is_tty && (curr%period == 0)) { 112 | total_ = tot; 113 | nupdates++; 114 | auto now = std::chrono::system_clock::now(); 115 | double dt = ((std::chrono::duration)(now - t_old)).count(); 116 | double dt_tot = ((std::chrono::duration)(now - t_first)).count(); 117 | int dn = curr - n_old; 118 | n_old = curr; 119 | t_old = now; 120 | if (deq_n.size() >= smoothing) deq_n.erase(deq_n.begin()); 121 | if (deq_t.size() >= smoothing) deq_t.erase(deq_t.begin()); 122 | deq_t.push_back(dt); 123 | deq_n.push_back(dn); 124 | 125 | double avgrate = 0.; 126 | if (use_ema) { 127 | avgrate = deq_n[0] / deq_t[0]; 128 | for (unsigned int i = 1; i < deq_t.size(); i++) { 129 | double r = 1.0*deq_n[i]/deq_t[i]; 130 | avgrate = alpha_ema*r + (1.0-alpha_ema)*avgrate; 131 | } 132 | } else { 133 | double dtsum = std::accumulate(deq_t.begin(),deq_t.end(),0.); 134 | int dnsum = std::accumulate(deq_n.begin(),deq_n.end(),0.); 135 | avgrate = dnsum/dtsum; 136 | } 137 | 138 | // learn an appropriate period length to avoid spamming stdout 139 | // and slowing down the loop, shoot for ~25Hz and smooth over 3 seconds 140 | if (nupdates > 10) { 141 | period = (int)( std::min(std::max((1.0/25)*curr/dt_tot,1.0), 5e5)); 142 | smoothing = 25*3; 143 | } 144 | double peta = (tot-curr)/avgrate; 145 | double pct = (double)curr/(tot*0.01); 146 | if( ( tot - curr ) <= period ) { 147 | pct = 100.0; 148 | avgrate = tot/dt_tot; 149 | curr = tot; 150 | peta = 0; 151 | } 152 | 153 | double fills = ((double)curr / tot * width); 154 | int ifills = (int)fills; 155 | 156 | printf("\015 "); 157 | if (use_colors) { 158 | if (color_transition) { 159 | // red (hue=0) to green (hue=1/3) 160 | int r = 255, g = 255, b = 255; 161 | hsv_to_rgb(0.0+0.01*pct/3,0.65,1.0, r,g,b); 162 | printf("\033[38;2;%d;%d;%dm ", r, g, b); 163 | } else { 164 | printf("\033[32m "); 165 | } 166 | } 167 | for (int i = 0; i < ifills; i++) std::cout << bars[8]; 168 | if (!in_screen && (curr != tot)) printf("%s",bars[(int)(8.0*(fills-ifills))]); 169 | for (int i = 0; i < width-ifills-1; i++) std::cout << bars[0]; 170 | printf("%s ", right_pad.c_str()); 171 | if (use_colors) printf("\033[1m\033[31m"); 172 | printf("%4.1f%% ", pct); 173 | if (use_colors) printf("\033[34m"); 174 | 175 | std::string unit = "Hz"; 176 | double div = 1.; 177 | if (avgrate > 1e6) { 178 | unit = "MHz"; div = 1.0e6; 179 | } else if (avgrate > 1e3) { 180 | unit = "kHz"; div = 1.0e3; 181 | } 182 | printf("[%4d/%4d | %3.1f %s | %.0fs<%.0fs] ", curr,tot, avgrate/div, unit.c_str(), dt_tot, peta); 183 | printf("%s ", label.c_str()); 184 | if (use_colors) printf("\033[0m\033[32m\033[0m\015 "); 185 | 186 | if( ( tot - curr ) > period ) fflush(stdout); 187 | } 188 | } 189 | }; 190 | #endif -------------------------------------------------------------------------------- /TMT/TMT_Build_Transition_Tensor.py: -------------------------------------------------------------------------------- 1 | import os 2 | import argparse 3 | import time 4 | from glob import glob 5 | import sys 6 | sys.path.append("./scripts/") 7 | from transition_motion_tensor import TransitionMotionTensor as TMT 8 | 9 | 10 | def main(args): 11 | w_stability = float(args['w_stability']) 12 | w_time = float(args['w_time']) 13 | dt_stability = float(args['dt_stability']) 14 | alive_thres = float(args['alive_thres']) 15 | input_path = str(args['input_path']) 16 | tensor_name = os.path.splitext(os.path.basename(input_path))[0] 17 | 18 | transition_files = glob("{0}/*.json".format(input_path)) 19 | tensor = TMT(tensor_name, cache_dir='./data/cache') 20 | tensor.load_from_json(transition_files) 21 | tensor.compute_quality(w_time=w_time, w_stability=w_stability, dt_stability=dt_stability) 22 | tensor.save_precomputed_tensor(fps=30, alive_th=alive_thres) 23 | return 24 | 25 | 26 | if __name__ == '__main__': 27 | parser = argparse.ArgumentParser() 28 | parser.add_argument('--w_stability', default=0.015) 29 | parser.add_argument('--w_time', default=1) 30 | parser.add_argument('--dt_stability', default=0.01) 31 | parser.add_argument('--alive_thres', default=0.9) 32 | parser.add_argument('--input_path', required=True) 33 | args = parser.parse_args() 34 | 35 | start = time.time() 36 | main(vars(args)) 37 | elapsed_time = time.time() - start 38 | print('Finished in %.1f seconds.' % (elapsed_time)) 39 | -------------------------------------------------------------------------------- /TMT/TMT_Collect_Transitions.py: -------------------------------------------------------------------------------- 1 | import os 2 | import argparse 3 | import math 4 | import subprocess 5 | import time 6 | 7 | 8 | def on_exp_finished_callback(input_files, output_files, stdout_files, output_path): 9 | args_path = output_path + '/args' 10 | 11 | if not os.path.exists(args_path): 12 | os.makedirs(args_path) 13 | 14 | # Move input_files to args_path 15 | for input_file in input_files: 16 | if os.path.isfile(input_file): 17 | os.rename(input_file, args_path + '/' + os.path.basename(input_file)) 18 | return 19 | 20 | 21 | def main(args): 22 | num_samples = int(args['num_samples']) 23 | num_workers = int(args['num_workers']) 24 | output_path = str(args['output_path']) 25 | stdout_path = output_path + '/stdout_logs' 26 | num_samples_per_workers = math.ceil(num_samples / num_workers) 27 | exp_filename = os.path.basename(output_path) 28 | template_args = '' 29 | 30 | if not os.path.exists(output_path): 31 | os.makedirs(output_path) 32 | 33 | if not os.path.exists(stdout_path): 34 | os.makedirs(stdout_path) 35 | 36 | with open(str(args['input_filename']), 'r') as fin: 37 | template_args = fin.read() 38 | process_list = [] 39 | output_files = [] 40 | input_files = [] 41 | stdout_files = [] 42 | for i in range(num_workers): 43 | output_filename = '%s/%s_worker%d.json' % (output_path, exp_filename, i) 44 | args_filename = './args/%s_worker%d.txt' % (exp_filename, i) 45 | rand_seed = int(time.time() + 1000 * i) 46 | args_str = template_args.replace('{NUM_TRANSITION_SAMPLES}', str(num_samples_per_workers)) \ 47 | .replace('{OUTPUT_FILENAME}', str(output_filename)) \ 48 | .replace('{RAND_SEED}', str(rand_seed)) 49 | with open(args_filename, 'w') as fin: 50 | fin.write(args_str) 51 | 52 | proc = None 53 | args = ['python', 'TMT_Optimizer.py', '--arg_file', args_filename] 54 | if i == 0: 55 | proc = subprocess.Popen(args) 56 | else: 57 | stdout_filename = "%s/stdout_workers%d.txt" % (stdout_path, i) 58 | with open(stdout_filename, "wb") as out: 59 | proc = subprocess.Popen(args, stdout=out, stderr=out) 60 | stdout_files.append(stdout_filename) 61 | process_list.append(proc) 62 | input_files.append(args_filename) 63 | output_files.append(output_filename) 64 | 65 | while(True): 66 | num_proc_done = 0 67 | for i, proc in enumerate(process_list): 68 | if proc.poll() is not None: 69 | num_proc_done += 1 70 | 71 | if num_proc_done == len(process_list): 72 | on_exp_finished_callback(input_files, output_files, stdout_files, output_path) 73 | break 74 | 75 | time.sleep(5) 76 | 77 | 78 | if __name__ == '__main__': 79 | parser = argparse.ArgumentParser() 80 | parser.add_argument("--num_workers", default=1) 81 | parser.add_argument("--num_samples", required=True) 82 | parser.add_argument("--input_filename", required=True) 83 | parser.add_argument("--output_path", required=True) 84 | args = parser.parse_args() 85 | 86 | start = time.time() 87 | main(vars(args)) 88 | elapsed_time = time.time() - start 89 | print('Finished in %.1f seconds.' % (elapsed_time)) 90 | -------------------------------------------------------------------------------- /TMT/TMT_Optimizer.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import sys 3 | sys.path.append('../DeepMimic/') 4 | sys.path.append('scripts/') 5 | from carl_env import CarlEnv 6 | from carl_rl_world import RLWorld 7 | from util.logger import Logger 8 | from util.arg_parser import ArgParser 9 | from TMT_Runner import update_world, update_timestep, build_world 10 | import util.mpi_util as MPIUtil 11 | 12 | args = [] 13 | world = None 14 | iter = 0 15 | 16 | def run(max_iters=None): 17 | global update_timestep 18 | global world 19 | global iter 20 | 21 | done = False 22 | while not (done): 23 | update_world(world, update_timestep) 24 | done = world.isDone() 25 | 26 | iter += 1 27 | if max_iters: done = iter >= max_iters 28 | 29 | return 30 | 31 | def shutdown(): 32 | global world 33 | 34 | Logger.print('Shutting down...') 35 | world.shutdown() 36 | return 37 | 38 | def main(): 39 | global args 40 | global world 41 | 42 | # Command line arguments 43 | args = sys.argv[1:] 44 | 45 | arg_parser = ArgParser() 46 | arg_parser.load_args(args) 47 | max_iters = arg_parser.parse_int('max_iters', None) 48 | 49 | world = build_world(args, enable_draw=False) 50 | 51 | run(max_iters) 52 | shutdown() 53 | 54 | return 55 | 56 | if __name__ == '__main__': 57 | main() 58 | -------------------------------------------------------------------------------- /TMT/args/record_dog3d_transitions_args.txt: -------------------------------------------------------------------------------- 1 | --scene transition_recorder 2 | 3 | --rand_seed {RAND_SEED} 4 | 5 | --num_update_substeps 10 6 | --num_sim_substeps 2 7 | --world_scale 4 8 | 9 | --terrain_file data/terrain/plane.txt 10 | 11 | --cam_position 0.135827, 1.649429, -8.645550 12 | --cam_up -0.002943 0.982287 0.187358 13 | --cam_zoom 1.0 14 | 15 | --char_types general 16 | --character_files data/characters/dog3d.txt 17 | --enable_char_soft_contact false 18 | --enable_char_contact_fall false 19 | --fall_contact_bodies 0 1 2 3 4 5 6 7 11 12 16 20 20 | 21 | --char_ctrls ct_pd 22 | --char_ctrl_files data/controllers/dog3d_ctrl.txt 23 | --motion_files data/motions/dog3d_pace.txt data/motions/dog3d_trot.txt data/motions/dog3d_canter.txt data/motions/dog3d_jump.txt 24 | --sync_char_root_pos false 25 | --sync_char_root_rot false 26 | --enable_root_rot_fail false 27 | 28 | --agent_files data/agents/ct_agent_dog_unified_controller.txt 29 | --train_agents false 30 | --normalizer_file data/normalizer/normalizer.npz 31 | 32 | --control_mode high_level 33 | --trajectory_files data/trajectories/dog3d_pace_trajectory.txt data/trajectories/dog3d_trot_trajectory.txt data/trajectories/dog3d_canter_trajectory.txt data/trajectories/dog3d_jump_trajectory.txt 34 | --motion_names Pace Trot Canter Jump 35 | 36 | --primitive_models data/policies/dog3d_pace.ckpt data/policies/dog3d_trot.ckpt data/policies/dog3d_canter.ckpt data/policies/dog3d_jump.ckpt 37 | --gating_models data/policies/dog3d_pace.ckpt data/policies/dog3d_trot.ckpt data/policies/dog3d_canter.ckpt data/policies/dog3d_jump.ckpt 38 | 39 | --pre_transition_time 1 40 | --post_transition_time 5 41 | --eval_output_filename {OUTPUT_FILENAME} 42 | --num_collect_transition_samples {NUM_TRANSITION_SAMPLES} 43 | -------------------------------------------------------------------------------- /TMT/args/record_humanoid3d_transitions_args.txt: -------------------------------------------------------------------------------- 1 | --scene transition_recorder 2 | 3 | --rand_seed {RAND_SEED} 4 | 5 | --num_update_substeps 10 6 | --num_sim_substeps 2 7 | --world_scale 4 8 | 9 | --terrain_file data/terrain/plane.txt 10 | 11 | --cam_position 0.135827, 1.649429, -8.645550 12 | --cam_up -0.002943 0.982287 0.187358 13 | --cam_zoom 1.0 14 | 15 | --char_types general 16 | --character_files data/characters/humanoid3d.txt 17 | --enable_char_soft_contact false 18 | --enable_char_contact_fall false 19 | --fall_contact_bodies 0 1 2 3 4 6 7 8 9 10 12 13 14 20 | 21 | --char_ctrls ct_pd 22 | --char_ctrl_files data/controllers/humanoid3d_ctrl.txt 23 | --motion_files data/motions/humanoid3d_walk.txt data/motions/humanoid3d_jog.txt data/motions/humanoid3d_run.txt 24 | --sync_char_root_pos true 25 | --sync_char_root_rot false 26 | --enable_root_rot_fail false 27 | 28 | --agent_files data/agents/ct_agent_humanoid_unified_controller_ppo.txt 29 | --train_agents false 30 | 31 | --motion_names Walk Jog Run 32 | --model_files data/policies/humanoid3d_walk.ckpt data/policies/humanoid3d_jog.ckpt data/policies/humanoid3d_run.ckpt 33 | 34 | --trajectory_files data/trajectories/humanoid3d_walk_trajectory.txt data/trajectories/humanoid3d_jog_trajectory.txt data/trajectories/humanoid3d_run_trajectory.txt 35 | --control_mode phase 36 | 37 | --pre_transition_time 1 38 | --post_transition_time 10 39 | --eval_output_filename {OUTPUT_FILENAME} 40 | --num_collect_transition_samples {NUM_TRANSITION_SAMPLES} 41 | -------------------------------------------------------------------------------- /TMT/args/run_dog3d_template_controller_canter_args.txt: -------------------------------------------------------------------------------- 1 | --scene template_controller 2 | 3 | --num_update_substeps 10 4 | --num_sim_substeps 2 5 | --world_scale 4 6 | 7 | --terrain_file data/terrain/plane.txt 8 | 9 | --cam_position 0.135827, 1.649429, -8.645550 10 | --cam_up -0.002943 0.982287 0.187358 11 | --cam_zoom 1.0 12 | 13 | --char_types general 14 | --character_files data/characters/dog3d.txt 15 | --enable_char_soft_contact false 16 | --enable_char_contact_fall true 17 | --fall_contact_bodies 0 1 2 3 4 5 6 7 11 12 16 20 18 | --track_gait_pattern_joint_indices 10 15 19 23 19 | 20 | --char_ctrls ct_pd 21 | --char_ctrl_files data/controllers/dog3d_ctrl.txt 22 | --motion_files data/motions/dog3d_canter.txt 23 | --sync_char_root_pos true 24 | --sync_char_root_rot true 25 | #--enable_root_rot_fail false 26 | 27 | --control_mode high_level 28 | --trajectory_files data/trajectories/dog3d_canter_trajectory.txt 29 | --motion_names Canter 30 | 31 | --agent_files data/agents/ct_agent_dog_template_controller_canter.txt 32 | --train_agents false 33 | --normalizer_file data/normalizer/normalizer.npz 34 | 35 | --primitive_models data/policies/dog3d_canter.ckpt 36 | --gating_models data/policies/dog3d_canter.ckpt 37 | -------------------------------------------------------------------------------- /TMT/args/run_dog3d_template_controller_jump_args.txt: -------------------------------------------------------------------------------- 1 | --scene template_controller 2 | 3 | --num_update_substeps 10 4 | --num_sim_substeps 2 5 | --world_scale 4 6 | 7 | --terrain_file data/terrain/plane.txt 8 | 9 | --cam_position 0.135827, 1.649429, -8.645550 10 | --cam_up -0.002943 0.982287 0.187358 11 | --cam_zoom 1.0 12 | 13 | --char_types general 14 | --character_files data/characters/dog3d.txt 15 | --enable_char_soft_contact false 16 | --enable_char_contact_fall false 17 | --fall_contact_bodies 0 1 2 3 4 5 6 7 11 12 16 20 18 | --track_gait_pattern_joint_indices 10 15 19 23 19 | 20 | --char_ctrls ct_pd 21 | --char_ctrl_files data/controllers/dog3d_ctrl.txt 22 | --motion_files data/motions/dog3d_jump.txt 23 | --sync_char_root_pos false 24 | --sync_char_root_rot false 25 | #--enable_root_rot_fail false 26 | 27 | --control_mode high_level 28 | --trajectory_files data/trajectories/dog3d_jump_trajectory.txt 29 | --motion_names Jump 30 | 31 | --agent_files data/agents/ct_agent_dog_template_controller_jump.txt 32 | --train_agents false 33 | --normalizer_file data/normalizer/normalizer.npz 34 | 35 | --primitive_models data/policies/dog3d_jump.ckpt 36 | --gating_models data/policies/dog3d_jump.ckpt 37 | -------------------------------------------------------------------------------- /TMT/args/run_dog3d_template_controller_pace_args.txt: -------------------------------------------------------------------------------- 1 | --scene template_controller 2 | 3 | --num_update_substeps 10 4 | --num_sim_substeps 2 5 | --world_scale 4 6 | 7 | --terrain_file data/terrain/plane.txt 8 | 9 | --cam_position 0.135827, 1.649429, -8.645550 10 | --cam_up -0.002943 0.982287 0.187358 11 | --cam_zoom 1.0 12 | 13 | --char_types general 14 | --character_files data/characters/dog3d.txt 15 | --enable_char_soft_contact false 16 | --enable_char_contact_fall true 17 | --fall_contact_bodies 0 1 2 3 4 5 6 7 11 12 16 20 18 | --track_gait_pattern_joint_indices 10 15 19 23 19 | 20 | --char_ctrls ct_pd 21 | --char_ctrl_files data/controllers/dog3d_ctrl.txt 22 | --motion_files data/motions/dog3d_pace.txt 23 | --sync_char_root_pos false 24 | --sync_char_root_rot false 25 | #--enable_root_rot_fail false 26 | 27 | --control_mode high_level 28 | --trajectory_files data/trajectories/dog3d_pace_trajectory.txt 29 | --motion_names Pace 30 | 31 | --agent_files data/agents/ct_agent_dog_template_controller_pace.txt 32 | --train_agents false 33 | --normalizer_file data/normalizer/normalizer.npz 34 | 35 | --primitive_models data/policies/dog3d_pace.ckpt 36 | --gating_models data/policies/dog3d_pace.ckpt 37 | -------------------------------------------------------------------------------- /TMT/args/run_dog3d_template_controller_trot_args.txt: -------------------------------------------------------------------------------- 1 | --scene template_controller 2 | 3 | --num_update_substeps 10 4 | --num_sim_substeps 2 5 | --world_scale 4 6 | 7 | --terrain_file data/terrain/plane.txt 8 | 9 | --cam_position 0.135827, 1.649429, -8.645550 10 | --cam_up -0.002943 0.982287 0.187358 11 | --cam_zoom 1.0 12 | 13 | --char_types general 14 | --character_files data/characters/dog3d.txt 15 | --enable_char_soft_contact false 16 | --enable_char_contact_fall true 17 | --fall_contact_bodies 0 1 2 3 4 5 6 7 11 12 16 20 18 | --track_gait_pattern_joint_indices 10 15 19 23 19 | 20 | --char_ctrls ct_pd 21 | --char_ctrl_files data/controllers/dog3d_ctrl.txt 22 | --motion_files data/motions/dog3d_trot.txt 23 | --sync_char_root_pos true 24 | --sync_char_root_rot true 25 | #--enable_root_rot_fail false 26 | 27 | --control_mode high_level 28 | --trajectory_files data/trajectories/dog3d_trot_trajectory.txt 29 | --motion_names Trot 30 | 31 | --agent_files data/agents/ct_agent_dog_template_controller_trot.txt 32 | --train_agents false 33 | --normalizer_file data/normalizer/normalizer.npz 34 | 35 | --primitive_models data/policies/dog3d_trot.ckpt 36 | --gating_models data/policies/dog3d_trot.ckpt 37 | -------------------------------------------------------------------------------- /TMT/args/run_dog3d_unified_controller_args.txt: -------------------------------------------------------------------------------- 1 | --scene template_controller 2 | 3 | --num_update_substeps 10 4 | --num_sim_substeps 2 5 | --world_scale 4 6 | 7 | --terrain_file data/terrain/plane.txt 8 | 9 | --cam_position 0.135827, 1.649429, -8.645550 10 | --cam_up -0.002943 0.982287 0.187358 11 | --cam_zoom 1.0 12 | 13 | --char_types general 14 | --character_files data/characters/dog3d.txt 15 | --enable_char_soft_contact false 16 | --enable_char_contact_fall false 17 | --fall_contact_bodies 0 1 2 3 4 5 6 7 11 12 16 20 18 | 19 | --char_ctrls ct_pd 20 | --char_ctrl_files data/controllers/dog3d_ctrl.txt 21 | --motion_files data/motions/dog3d_pace.txt data/motions/dog3d_trot.txt data/motions/dog3d_canter.txt data/motions/dog3d_jump.txt 22 | --sync_char_root_pos false 23 | --sync_char_root_rot false 24 | --enable_root_rot_fail false 25 | 26 | --agent_files data/agents/ct_agent_dog_unified_controller.txt 27 | --train_agents false 28 | --normalizer_file data/normalizer/normalizer.npz 29 | 30 | --control_mode high_level 31 | --trajectory_files data/trajectories/dog3d_pace_trajectory.txt data/trajectories/dog3d_trot_trajectory.txt data/trajectories/dog3d_canter_trajectory.txt data/trajectories/dog3d_jump_trajectory.txt 32 | --motion_names Pace Trot Canter Jump 33 | 34 | --primitive_models data/policies/dog3d_pace.ckpt data/policies/dog3d_trot.ckpt data/policies/dog3d_canter.ckpt data/policies/dog3d_jump.ckpt 35 | --gating_models data/policies/dog3d_pace.ckpt data/policies/dog3d_trot.ckpt data/policies/dog3d_canter.ckpt data/policies/dog3d_jump.ckpt 36 | 37 | --transition_tensor_file data/trans-tensors/precomputed_tensor_dog_ptcj.pkl -------------------------------------------------------------------------------- /TMT/args/run_humanoid3d_template_controller_jog_args.txt: -------------------------------------------------------------------------------- 1 | --scene template_controller 2 | 3 | --num_update_substeps 10 4 | --num_sim_substeps 2 5 | --world_scale 4 6 | 7 | --terrain_file data/terrain/plane.txt 8 | 9 | --cam_position 0.135827, 1.649429, -8.645550 10 | --cam_up -0.002943 0.982287 0.187358 11 | --cam_zoom 1.0 12 | 13 | --char_types general 14 | --character_files data/characters/humanoid3d.txt 15 | --enable_char_soft_contact false 16 | --fall_contact_bodies 0 1 2 3 4 6 7 8 9 10 12 13 14 17 | 18 | --char_ctrls ct_pd 19 | --char_ctrl_files data/controllers/humanoid3d_ctrl.txt 20 | --motion_files data/motions/humanoid3d_jog.txt 21 | --sync_char_root_pos true 22 | --sync_char_root_rot false 23 | 24 | --agent_files data/agents/ct_agent_humanoid_template_controller_jog_ppo.txt 25 | --train_agents false 26 | 27 | --motion_names Jog 28 | --model_files data/policies/humanoid3d_jog.ckpt 29 | 30 | --trajectory_files data/trajectories/humanoid3d_jog_trajectory.txt 31 | --control_mode phase 32 | -------------------------------------------------------------------------------- /TMT/args/run_humanoid3d_template_controller_run_args.txt: -------------------------------------------------------------------------------- 1 | --scene template_controller 2 | 3 | --num_update_substeps 10 4 | --num_sim_substeps 2 5 | --world_scale 4 6 | 7 | --terrain_file data/terrain/plane.txt 8 | 9 | --cam_position 0.135827, 1.649429, -8.645550 10 | --cam_up -0.002943 0.982287 0.187358 11 | --cam_zoom 1.0 12 | 13 | --char_types general 14 | --character_files data/characters/humanoid3d.txt 15 | --enable_char_soft_contact false 16 | --fall_contact_bodies 0 1 2 3 4 6 7 8 9 10 12 13 14 17 | 18 | --char_ctrls ct_pd 19 | --char_ctrl_files data/controllers/humanoid3d_ctrl.txt 20 | --motion_files data/motions/humanoid3d_run.txt 21 | --sync_char_root_pos true 22 | --sync_char_root_rot false 23 | 24 | --agent_files data/agents/ct_agent_humanoid_template_controller_run_ppo.txt 25 | --train_agents false 26 | 27 | --motion_names Run 28 | --model_files data/policies/humanoid3d_run.ckpt 29 | 30 | --trajectory_files data/trajectories/humanoid3d_run_trajectory.txt 31 | --control_mode phase 32 | -------------------------------------------------------------------------------- /TMT/args/run_humanoid3d_template_controller_walk_args.txt: -------------------------------------------------------------------------------- 1 | --scene template_controller 2 | 3 | --num_update_substeps 10 4 | --num_sim_substeps 2 5 | --world_scale 4 6 | 7 | --terrain_file data/terrain/plane.txt 8 | 9 | --cam_position 0.135827, 1.649429, -8.645550 10 | --cam_up -0.002943 0.982287 0.187358 11 | --cam_zoom 1.0 12 | 13 | --char_types general 14 | --character_files data/characters/humanoid3d.txt 15 | --enable_char_soft_contact false 16 | --fall_contact_bodies 0 1 2 3 4 6 7 8 9 10 12 13 14 17 | 18 | --char_ctrls ct_pd 19 | --char_ctrl_files data/controllers/humanoid3d_ctrl.txt 20 | --motion_files data/motions/humanoid3d_walk.txt 21 | --sync_char_root_pos true 22 | --sync_char_root_rot false 23 | 24 | --agent_files data/agents/ct_agent_humanoid_template_controller_walk_ppo.txt 25 | --train_agents false 26 | 27 | --motion_names Walk 28 | --model_files data/policies/humanoid3d_walk.ckpt 29 | 30 | --trajectory_files data/trajectories/humanoid3d_walk_trajectory.txt 31 | --control_mode phase 32 | -------------------------------------------------------------------------------- /TMT/args/run_humanoid3d_unified_controller_args.txt: -------------------------------------------------------------------------------- 1 | --scene template_controller 2 | 3 | --num_update_substeps 10 4 | --num_sim_substeps 2 5 | --world_scale 4 6 | 7 | --terrain_file data/terrain/plane.txt 8 | 9 | --cam_position 0.135827, 1.649429, -8.645550 10 | --cam_up -0.002943 0.982287 0.187358 11 | --cam_zoom 1.0 12 | 13 | --char_types general 14 | --character_files data/characters/humanoid3d.txt 15 | --enable_char_soft_contact false 16 | --enable_char_contact_fall false 17 | --fall_contact_bodies 0 1 2 3 4 6 7 8 9 10 12 13 14 18 | 19 | --char_ctrls ct_pd 20 | --char_ctrl_files data/controllers/humanoid3d_ctrl.txt 21 | --motion_files data/motions/humanoid3d_walk.txt data/motions/humanoid3d_jog.txt data/motions/humanoid3d_run.txt 22 | --sync_char_root_pos true 23 | --sync_char_root_rot false 24 | 25 | --agent_files data/agents/ct_agent_humanoid_unified_controller_ppo.txt 26 | --train_agents false 27 | 28 | --motion_names Walk Jog Run 29 | --model_files data/policies/humanoid3d_walk.ckpt data/policies/humanoid3d_jog.ckpt data/policies/humanoid3d_run.ckpt 30 | 31 | --trajectory_files data/trajectories/humanoid3d_walk_trajectory.txt data/trajectories/humanoid3d_jog_trajectory.txt data/trajectories/humanoid3d_run_trajectory.txt 32 | --control_mode phase 33 | 34 | --transition_tensor_file data/trans-tensors/precomputed_tensor_humanoid_wjr.pkl 35 | -------------------------------------------------------------------------------- /TMT/data/agents/ct_agent_dog_template_controller_canter.txt: -------------------------------------------------------------------------------- 1 | { 2 | "AgentType": "CarlPPO", 3 | 4 | "Primitives": [4], 5 | "MotionNames": ["Canter"], 6 | 7 | "Pretraining": false, 8 | "InitTotalSamples": 0, 9 | 10 | "ActorPrimitivesNet": "fc_2layers_512units", 11 | "ActorGatingNet": "fc_3layers_512units_256units_reduced_state_gating", 12 | "ActorPrimitivesNetHiddenSize": 256, 13 | "ActorStepsize": 0.00005, 14 | "ActorMomentum": 0.9, 15 | "ActorWeightDecay": 0.0005, 16 | "ActorInitOutputScale": 0.01, 17 | "ActorOptimizer": "momentum", 18 | "GatingRegularizationLambda": 0.1, 19 | 20 | "CriticNet": "fc_2layers_1024units", 21 | "CriticStepsize": 0.01, 22 | "CriticMomentum": 0.9, 23 | "CriticWeightDecay": 0, 24 | "CriticOptimizer": "momentum", 25 | 26 | "UpdatePeriod": 1, 27 | "ItersPerUpdate": 1, 28 | "Discount": 0.99, 29 | "BatchSize": 4096, 30 | "MiniBatchSize": 256, 31 | "Epochs": 1, 32 | "ReplayBufferSize": 500000, 33 | "InitSamples": 1, 34 | "NormalizerSamples": 1000000, 35 | 36 | "RatioClip": 0.02, 37 | "NormAdvClip": 4, 38 | "TDLambda": 0.95, 39 | 40 | "OutputIters": 10, 41 | "IntOutputIters": 200, 42 | "TestEpisodes": 32, 43 | 44 | "ExpAnnealSamples": 256000000, 45 | 46 | "ExpParamsBeg": 47 | { 48 | "Rate": 1, 49 | "InitActionRate": 1, 50 | "Noise": 0.05, 51 | "NoiseInternal": 0, 52 | "Temp": 0.1 53 | }, 54 | 55 | "ExpParamsEnd": 56 | { 57 | "Rate": 0.2, 58 | "InitActionRate": 0.01, 59 | "Noise": 0.05, 60 | "NoiseInternal": 0, 61 | "Temp": 0.001 62 | } 63 | } -------------------------------------------------------------------------------- /TMT/data/agents/ct_agent_dog_template_controller_jump.txt: -------------------------------------------------------------------------------- 1 | { 2 | "AgentType": "CarlPPO", 3 | 4 | "Primitives": [8], 5 | "MotionNames": ["Jump"], 6 | 7 | "Pretraining": false, 8 | "InitTotalSamples": 0, 9 | 10 | "ActorPrimitivesNet": "fc_2layers_512units", 11 | "ActorGatingNet": "fc_3layers_512units_256units_reduced_state_gating", 12 | "ActorPrimitivesNetHiddenSize": 256, 13 | "ActorStepsize": 0.00005, 14 | "ActorMomentum": 0.9, 15 | "ActorWeightDecay": 0.0005, 16 | "ActorInitOutputScale": 0.01, 17 | "ActorOptimizer": "momentum", 18 | "GatingRegularizationLambda": 0.1, 19 | 20 | "CriticNet": "fc_2layers_1024units", 21 | "CriticStepsize": 0.01, 22 | "CriticMomentum": 0.9, 23 | "CriticWeightDecay": 0, 24 | "CriticOptimizer": "momentum", 25 | 26 | "UpdatePeriod": 1, 27 | "ItersPerUpdate": 1, 28 | "Discount": 0.99, 29 | "BatchSize": 4096, 30 | "MiniBatchSize": 256, 31 | "Epochs": 1, 32 | "ReplayBufferSize": 500000, 33 | "InitSamples": 1, 34 | "NormalizerSamples": 1000000, 35 | 36 | "RatioClip": 0.02, 37 | "NormAdvClip": 4, 38 | "TDLambda": 0.95, 39 | 40 | "OutputIters": 10, 41 | "IntOutputIters": 200, 42 | "TestEpisodes": 32, 43 | 44 | "ExpAnnealSamples": 256000000, 45 | 46 | "ExpParamsBeg": 47 | { 48 | "Rate": 1, 49 | "InitActionRate": 1, 50 | "Noise": 0.05, 51 | "NoiseInternal": 0, 52 | "Temp": 0.1 53 | }, 54 | 55 | "ExpParamsEnd": 56 | { 57 | "Rate": 0.2, 58 | "InitActionRate": 0.01, 59 | "Noise": 0.05, 60 | "NoiseInternal": 0, 61 | "Temp": 0.001 62 | } 63 | } -------------------------------------------------------------------------------- /TMT/data/agents/ct_agent_dog_template_controller_pace.txt: -------------------------------------------------------------------------------- 1 | { 2 | "AgentType": "CarlPPO", 3 | 4 | "Primitives": [4], 5 | "MotionNames": ["Pace"], 6 | 7 | "Pretraining": false, 8 | "InitTotalSamples": 0, 9 | 10 | "ActorPrimitivesNet": "fc_2layers_512units", 11 | "ActorGatingNet": "fc_3layers_512units_256units_reduced_state_gating", 12 | "ActorPrimitivesNetHiddenSize": 256, 13 | "ActorStepsize": 0.00005, 14 | "ActorMomentum": 0.9, 15 | "ActorWeightDecay": 0.0005, 16 | "ActorInitOutputScale": 0.01, 17 | "ActorOptimizer": "momentum", 18 | "GatingRegularizationLambda": 0.1, 19 | 20 | "CriticNet": "fc_2layers_1024units", 21 | "CriticStepsize": 0.01, 22 | "CriticMomentum": 0.9, 23 | "CriticWeightDecay": 0, 24 | "CriticOptimizer": "momentum", 25 | 26 | "UpdatePeriod": 1, 27 | "ItersPerUpdate": 1, 28 | "Discount": 0.99, 29 | "BatchSize": 4096, 30 | "MiniBatchSize": 256, 31 | "Epochs": 1, 32 | "ReplayBufferSize": 500000, 33 | "InitSamples": 1, 34 | "NormalizerSamples": 1000000, 35 | 36 | "RatioClip": 0.02, 37 | "NormAdvClip": 4, 38 | "TDLambda": 0.95, 39 | 40 | "OutputIters": 10, 41 | "IntOutputIters": 200, 42 | "TestEpisodes": 32, 43 | 44 | "ExpAnnealSamples": 256000000, 45 | 46 | "ExpParamsBeg": 47 | { 48 | "Rate": 1, 49 | "InitActionRate": 1, 50 | "Noise": 0.05, 51 | "NoiseInternal": 0, 52 | "Temp": 0.1 53 | }, 54 | 55 | "ExpParamsEnd": 56 | { 57 | "Rate": 0.2, 58 | "InitActionRate": 0.01, 59 | "Noise": 0.05, 60 | "NoiseInternal": 0, 61 | "Temp": 0.001 62 | } 63 | } -------------------------------------------------------------------------------- /TMT/data/agents/ct_agent_dog_template_controller_trot.txt: -------------------------------------------------------------------------------- 1 | { 2 | "AgentType": "CarlPPO", 3 | 4 | "Primitives": [4], 5 | "MotionNames": ["Trot"], 6 | 7 | "Pretraining": false, 8 | "InitTotalSamples": 0, 9 | 10 | "ActorPrimitivesNet": "fc_2layers_512units", 11 | "ActorGatingNet": "fc_3layers_512units_256units_reduced_state_gating", 12 | "ActorPrimitivesNetHiddenSize": 256, 13 | "ActorStepsize": 0.00005, 14 | "ActorMomentum": 0.9, 15 | "ActorWeightDecay": 0.0005, 16 | "ActorInitOutputScale": 0.01, 17 | "ActorOptimizer": "momentum", 18 | "GatingRegularizationLambda": 0.1, 19 | 20 | "CriticNet": "fc_2layers_1024units", 21 | "CriticStepsize": 0.01, 22 | "CriticMomentum": 0.9, 23 | "CriticWeightDecay": 0, 24 | "CriticOptimizer": "momentum", 25 | 26 | "UpdatePeriod": 1, 27 | "ItersPerUpdate": 1, 28 | "Discount": 0.99, 29 | "BatchSize": 4096, 30 | "MiniBatchSize": 256, 31 | "Epochs": 1, 32 | "ReplayBufferSize": 500000, 33 | "InitSamples": 1, 34 | "NormalizerSamples": 1000000, 35 | 36 | "RatioClip": 0.02, 37 | "NormAdvClip": 4, 38 | "TDLambda": 0.95, 39 | 40 | "OutputIters": 10, 41 | "IntOutputIters": 200, 42 | "TestEpisodes": 32, 43 | 44 | "ExpAnnealSamples": 256000000, 45 | 46 | "ExpParamsBeg": 47 | { 48 | "Rate": 1, 49 | "InitActionRate": 1, 50 | "Noise": 0.05, 51 | "NoiseInternal": 0, 52 | "Temp": 0.1 53 | }, 54 | 55 | "ExpParamsEnd": 56 | { 57 | "Rate": 0.2, 58 | "InitActionRate": 0.01, 59 | "Noise": 0.05, 60 | "NoiseInternal": 0, 61 | "Temp": 0.001 62 | } 63 | } -------------------------------------------------------------------------------- /TMT/data/agents/ct_agent_dog_unified_controller.txt: -------------------------------------------------------------------------------- 1 | { 2 | "AgentType": "TMT_CARL_Agent", 3 | 4 | "Primitives": [4, 4, 4, 8], 5 | "MotionNames": ["Pace", "Trot", "Canter", "Jump"], 6 | 7 | "Pretraining": false, 8 | "InitTotalSamples": 0, 9 | 10 | "ActorPrimitivesNet": "fc_2layers_512units", 11 | "ActorGatingNet": "fc_3layers_512units_256units_reduced_state_gating", 12 | "ActorPrimitivesNetHiddenSize": 256, 13 | "ActorStepsize": 0.00005, 14 | "ActorMomentum": 0.9, 15 | "ActorWeightDecay": 0.0005, 16 | "ActorInitOutputScale": 0.01, 17 | "ActorOptimizer": "momentum", 18 | "GatingRegularizationLambda": 0.1, 19 | 20 | "CriticNet": "fc_2layers_1024units", 21 | "CriticStepsize": 0.01, 22 | "CriticMomentum": 0.9, 23 | "CriticWeightDecay": 0, 24 | "CriticOptimizer": "momentum", 25 | 26 | "UpdatePeriod": 1, 27 | "ItersPerUpdate": 1, 28 | "Discount": 0.99, 29 | "BatchSize": 4096, 30 | "MiniBatchSize": 256, 31 | "Epochs": 1, 32 | "ReplayBufferSize": 500000, 33 | "InitSamples": 1, 34 | "NormalizerSamples": 1000000, 35 | 36 | "RatioClip": 0.02, 37 | "NormAdvClip": 4, 38 | "TDLambda": 0.95, 39 | 40 | "OutputIters": 10, 41 | "IntOutputIters": 200, 42 | "TestEpisodes": 32, 43 | 44 | "ExpAnnealSamples": 256000000, 45 | 46 | "ExpParamsBeg": 47 | { 48 | "Rate": 1, 49 | "InitActionRate": 1, 50 | "Noise": 0.05, 51 | "NoiseInternal": 0, 52 | "Temp": 0.1 53 | }, 54 | 55 | "ExpParamsEnd": 56 | { 57 | "Rate": 0.2, 58 | "InitActionRate": 0.01, 59 | "Noise": 0.05, 60 | "NoiseInternal": 0, 61 | "Temp": 0.001 62 | } 63 | } -------------------------------------------------------------------------------- /TMT/data/agents/ct_agent_humanoid_template_controller_jog_ppo.txt: -------------------------------------------------------------------------------- 1 | { 2 | "AgentType": "DeepMimicPPO", 3 | 4 | "MotionNames": ["Jog"], 5 | 6 | "ActorNet": "fc_2layers_1024units", 7 | "ActorStepsize": 0.0000025, 8 | "ActorMomentum": 0.9, 9 | "ActorWeightDecay": 0.0005, 10 | "ActorInitOutputScale": 0.01, 11 | 12 | "CriticNet": "fc_2layers_1024units", 13 | "CriticStepsize": 0.01, 14 | "CriticMomentum": 0.9, 15 | "CriticWeightDecay": 0, 16 | 17 | "UpdatePeriod": 1, 18 | "ItersPerUpdate": 1, 19 | "Discount": 0.95, 20 | "BatchSize": 4096, 21 | "MiniBatchSize": 256, 22 | "Epochs": 1, 23 | "ReplayBufferSize": 500000, 24 | "InitSamples": 1, 25 | "NormalizerSamples": 1000000, 26 | 27 | "RatioClip": 0.2, 28 | "NormAdvClip": 4, 29 | "TDLambda": 0.95, 30 | 31 | "OutputIters": 10, 32 | "IntOutputIters": 200, 33 | "TestEpisodes": 32, 34 | 35 | "ExpAnnealSamples": 64000000, 36 | 37 | "ExpParamsBeg": 38 | { 39 | "Rate": 1, 40 | "InitActionRate": 1, 41 | "Noise": 0.05, 42 | "NoiseInternal": 0, 43 | "Temp": 0.1 44 | }, 45 | 46 | "ExpParamsEnd": 47 | { 48 | "Rate": 0.2, 49 | "InitActionRate": 0.01, 50 | "Noise": 0.05, 51 | "NoiseInternal": 0, 52 | "Temp": 0.001 53 | } 54 | } -------------------------------------------------------------------------------- /TMT/data/agents/ct_agent_humanoid_template_controller_run_ppo.txt: -------------------------------------------------------------------------------- 1 | { 2 | "AgentType": "DeepMimicPPO", 3 | 4 | "MotionNames": ["Run"], 5 | 6 | "ActorNet": "fc_2layers_1024units", 7 | "ActorStepsize": 0.0000025, 8 | "ActorMomentum": 0.9, 9 | "ActorWeightDecay": 0.0005, 10 | "ActorInitOutputScale": 0.01, 11 | 12 | "CriticNet": "fc_2layers_1024units", 13 | "CriticStepsize": 0.01, 14 | "CriticMomentum": 0.9, 15 | "CriticWeightDecay": 0, 16 | 17 | "UpdatePeriod": 1, 18 | "ItersPerUpdate": 1, 19 | "Discount": 0.95, 20 | "BatchSize": 4096, 21 | "MiniBatchSize": 256, 22 | "Epochs": 1, 23 | "ReplayBufferSize": 500000, 24 | "InitSamples": 1, 25 | "NormalizerSamples": 1000000, 26 | 27 | "RatioClip": 0.2, 28 | "NormAdvClip": 4, 29 | "TDLambda": 0.95, 30 | 31 | "OutputIters": 10, 32 | "IntOutputIters": 200, 33 | "TestEpisodes": 32, 34 | 35 | "ExpAnnealSamples": 64000000, 36 | 37 | "ExpParamsBeg": 38 | { 39 | "Rate": 1, 40 | "InitActionRate": 1, 41 | "Noise": 0.05, 42 | "NoiseInternal": 0, 43 | "Temp": 0.1 44 | }, 45 | 46 | "ExpParamsEnd": 47 | { 48 | "Rate": 0.2, 49 | "InitActionRate": 0.01, 50 | "Noise": 0.05, 51 | "NoiseInternal": 0, 52 | "Temp": 0.001 53 | } 54 | } -------------------------------------------------------------------------------- /TMT/data/agents/ct_agent_humanoid_template_controller_walk_ppo.txt: -------------------------------------------------------------------------------- 1 | { 2 | "AgentType": "DeepMimicPPO", 3 | 4 | "MotionNames": ["Walk"], 5 | 6 | "ActorNet": "fc_2layers_1024units", 7 | "ActorStepsize": 0.0000025, 8 | "ActorMomentum": 0.9, 9 | "ActorWeightDecay": 0.0005, 10 | "ActorInitOutputScale": 0.01, 11 | 12 | "CriticNet": "fc_2layers_1024units", 13 | "CriticStepsize": 0.01, 14 | "CriticMomentum": 0.9, 15 | "CriticWeightDecay": 0, 16 | 17 | "UpdatePeriod": 1, 18 | "ItersPerUpdate": 1, 19 | "Discount": 0.95, 20 | "BatchSize": 4096, 21 | "MiniBatchSize": 256, 22 | "Epochs": 1, 23 | "ReplayBufferSize": 500000, 24 | "InitSamples": 1, 25 | "NormalizerSamples": 1000000, 26 | 27 | "RatioClip": 0.2, 28 | "NormAdvClip": 4, 29 | "TDLambda": 0.95, 30 | 31 | "OutputIters": 10, 32 | "IntOutputIters": 200, 33 | "TestEpisodes": 32, 34 | 35 | "ExpAnnealSamples": 64000000, 36 | 37 | "ExpParamsBeg": 38 | { 39 | "Rate": 1, 40 | "InitActionRate": 1, 41 | "Noise": 0.05, 42 | "NoiseInternal": 0, 43 | "Temp": 0.1 44 | }, 45 | 46 | "ExpParamsEnd": 47 | { 48 | "Rate": 0.2, 49 | "InitActionRate": 0.01, 50 | "Noise": 0.05, 51 | "NoiseInternal": 0, 52 | "Temp": 0.001 53 | } 54 | } -------------------------------------------------------------------------------- /TMT/data/agents/ct_agent_humanoid_unified_controller_ppo.txt: -------------------------------------------------------------------------------- 1 | { 2 | "AgentType": "TMT_DeepMimic_Agent", 3 | 4 | "MotionNames": ["Walk", "Jog", "Run"], 5 | 6 | "ActorNet": "fc_2layers_1024units", 7 | "ActorStepsize": 0.0000025, 8 | "ActorMomentum": 0.9, 9 | "ActorWeightDecay": 0.0005, 10 | "ActorInitOutputScale": 0.01, 11 | 12 | "CriticNet": "fc_2layers_1024units", 13 | "CriticStepsize": 0.01, 14 | "CriticMomentum": 0.9, 15 | "CriticWeightDecay": 0, 16 | 17 | "UpdatePeriod": 1, 18 | "ItersPerUpdate": 1, 19 | "Discount": 0.95, 20 | "BatchSize": 4096, 21 | "MiniBatchSize": 256, 22 | "Epochs": 1, 23 | "ReplayBufferSize": 500000, 24 | "InitSamples": 1, 25 | "NormalizerSamples": 1000000, 26 | 27 | "RatioClip": 0.2, 28 | "NormAdvClip": 4, 29 | "TDLambda": 0.95, 30 | 31 | "OutputIters": 10, 32 | "IntOutputIters": 200, 33 | "TestEpisodes": 32, 34 | 35 | "ExpAnnealSamples": 64000000, 36 | 37 | "ExpParamsBeg": 38 | { 39 | "Rate": 1, 40 | "InitActionRate": 1, 41 | "Noise": 0.05, 42 | "NoiseInternal": 0, 43 | "Temp": 0.1 44 | }, 45 | 46 | "ExpParamsEnd": 47 | { 48 | "Rate": 0.2, 49 | "InitActionRate": 0.01, 50 | "Noise": 0.05, 51 | "NoiseInternal": 0, 52 | "Temp": 0.001 53 | } 54 | } -------------------------------------------------------------------------------- /TMT/data/cache/link.txt: -------------------------------------------------------------------------------- 1 | https://drive.google.com/file/d/1c_saJpzP1uO6fZwAhLe0tzfRdDlpLtMu/view?usp=sharing -------------------------------------------------------------------------------- /TMT/data/controllers/dog3d_ctrl.txt: -------------------------------------------------------------------------------- 1 | { 2 | "UpdateRate": 30, 3 | "EnablePhaseInput": false, 4 | "RecordWorldRootPos": false, 5 | "RecordWorldRootRot": false, 6 | 7 | "RightJoints": [11, 12, 13, 14, 15, 20, 21, 22, 23], 8 | "LeftJoints": [6, 7, 8, 9, 10, 16, 17, 18, 19], 9 | 10 | "PDControllers": 11 | [ 12 | { 13 | "ID": 0, 14 | "Name": "Hips", 15 | "Kp": 0, 16 | "Kd": 0, 17 | "TargetTheta0": 0, 18 | "UseWorldCoord": 0 19 | }, 20 | { 21 | "ID": 1, 22 | "Name": "Spine", 23 | "Kp": 1000, 24 | "Kd": 100, 25 | "TargetTheta0": 0, 26 | "UseWorldCoord": 0 27 | }, 28 | { 29 | "ID": 2, 30 | "Name": "Spine1", 31 | "Kp": 800, 32 | "Kd": 80, 33 | "TargetTheta0": 0, 34 | "UseWorldCoord": 0 35 | }, 36 | { 37 | "ID": 3, 38 | "Name": "Neck", 39 | "Kp": 100, 40 | "Kd": 10, 41 | "TargetTheta0": 0, 42 | "UseWorldCoord": 0 43 | }, 44 | { 45 | "ID": 4, 46 | "Name": "Head", 47 | "Kp": 50, 48 | "Kd": 5, 49 | "TargetTheta0": 0, 50 | "UseWorldCoord": 0 51 | }, 52 | { 53 | "ID": 5, 54 | "Name": "Head_end", 55 | "Kp": 0, 56 | "Kd": 0, 57 | "TargetTheta0": 0, 58 | "UseWorldCoord": 0 59 | }, 60 | { 61 | "ID": 6, 62 | "Name": "LeftShoulder", 63 | "Kp": 50, 64 | "Kd": 5, 65 | "TargetTheta0": 0, 66 | "UseWorldCoord": 0 67 | }, 68 | { 69 | "ID": 7, 70 | "Name": "LeftArm", 71 | "Kp": 400, 72 | "Kd": 40, 73 | "TargetTheta0": 0, 74 | "UseWorldCoord": 0 75 | }, 76 | { 77 | "ID": 8, 78 | "Name": "LeftForeArm", 79 | "Kp": 300, 80 | "Kd": 30, 81 | "TargetTheta0": 0, 82 | "UseWorldCoord": 0 83 | }, 84 | { 85 | "ID": 9, 86 | "Name": "LeftHand", 87 | "Kp": 200, 88 | "Kd": 20, 89 | "TargetTheta0": 0, 90 | "UseWorldCoord": 0 91 | }, 92 | { 93 | "ID": 10, 94 | "Name": "LeftHand_end", 95 | "Kp": 0, 96 | "Kd": 0, 97 | "TargetTheta0": 0, 98 | "UseWorldCoord": 0 99 | }, 100 | { 101 | "ID": 11, 102 | "Name": "RightShoulder", 103 | "Kp": 50, 104 | "Kd": 5, 105 | "TargetTheta0": 0, 106 | "UseWorldCoord": 0 107 | }, 108 | { 109 | "ID": 12, 110 | "Name": "RightArm", 111 | "Kp": 400, 112 | "Kd": 40, 113 | "TargetTheta0": 0, 114 | "UseWorldCoord": 0 115 | }, 116 | { 117 | "ID": 13, 118 | "Name": "RightForeArm", 119 | "Kp": 300, 120 | "Kd": 30, 121 | "TargetTheta0": 0, 122 | "UseWorldCoord": 0 123 | }, 124 | { 125 | "ID": 14, 126 | "Name": "RightHand", 127 | "Kp": 200, 128 | "Kd": 20, 129 | "TargetTheta0": 0, 130 | "UseWorldCoord": 0 131 | }, 132 | { 133 | "ID": 15, 134 | "Name": "RightHand_end", 135 | "Kp": 0, 136 | "Kd": 0, 137 | "TargetTheta0": 0, 138 | "UseWorldCoord": 0 139 | }, 140 | { 141 | "ID": 16, 142 | "Name": "LeftUpLeg", 143 | "Kp": 600, 144 | "Kd": 60, 145 | "TargetTheta0": 0, 146 | "UseWorldCoord": 0 147 | }, 148 | { 149 | "ID": 17, 150 | "Name": "LeftLeg", 151 | "Kp": 500, 152 | "Kd": 50, 153 | "TargetTheta0": 0, 154 | "UseWorldCoord": 0 155 | }, 156 | { 157 | "ID": 18, 158 | "Name": "LeftFoot", 159 | "Kp": 400, 160 | "Kd": 40, 161 | "TargetTheta0": 0, 162 | "UseWorldCoord": 0 163 | }, 164 | { 165 | "ID": 19, 166 | "Name": "LeftFoot_end", 167 | "Kp": 0, 168 | "Kd": 0, 169 | "TargetTheta0": 0, 170 | "UseWorldCoord": 0 171 | }, 172 | { 173 | "ID": 20, 174 | "Name": "RightUpLeg", 175 | "Kp": 600, 176 | "Kd": 60, 177 | "TargetTheta0": 0, 178 | "UseWorldCoord": 0 179 | }, 180 | { 181 | "ID": 21, 182 | "Name": "RightLeg", 183 | "Kp": 500, 184 | "Kd": 50, 185 | "TargetTheta0": 0, 186 | "UseWorldCoord": 0 187 | }, 188 | { 189 | "ID": 22, 190 | "Name": "RightFoot", 191 | "Kp": 400, 192 | "Kd": 40, 193 | "TargetTheta0": 0, 194 | "UseWorldCoord": 0 195 | }, 196 | { 197 | "ID": 23, 198 | "Name": "RightFoot_end", 199 | "Kp": 0, 200 | "Kd": 0, 201 | "TargetTheta0": 0, 202 | "UseWorldCoord": 0 203 | }, 204 | { 205 | "ID": 24, 206 | "Name": "Tail", 207 | "Kp": 100, 208 | "Kd": 10, 209 | "TargetTheta0": 0, 210 | "UseWorldCoord": 0 211 | }, 212 | { 213 | "ID": 25, 214 | "Name": "Tail1", 215 | "Kp": 100, 216 | "Kd": 10, 217 | "TargetTheta0": 0, 218 | "UseWorldCoord": 0 219 | }, 220 | { 221 | "ID": 26, 222 | "Name": "Tail1_end", 223 | "Kp": 0, 224 | "Kd": 0, 225 | "TargetTheta0": 0, 226 | "UseWorldCoord": 0 227 | } 228 | ] 229 | } -------------------------------------------------------------------------------- /TMT/data/controllers/humanoid3d_ctrl.txt: -------------------------------------------------------------------------------- 1 | { 2 | "UpdateRate": 30, 3 | "EnablePhaseInput": true, 4 | "RecordWorldRootPos": false, 5 | "RecordWorldRootRot": true, 6 | 7 | "RightJoints": [3, 4, 5, 6, 7, 8], 8 | "LeftJoints": [9, 10, 11, 12, 13, 14], 9 | 10 | "PDControllers": 11 | [ 12 | { 13 | "ID": 0, 14 | "Name": "root", 15 | "Kp": 0, 16 | "Kd": 0, 17 | "TargetTheta0": 0, 18 | "UseWorldCoord": 0 19 | }, 20 | { 21 | "ID": 1, 22 | "Name": "chest", 23 | "Kp": 1000, 24 | "Kd": 100, 25 | "TargetTheta0": 0, 26 | "UseWorldCoord": 0 27 | }, 28 | { 29 | "ID": 2, 30 | "Name": "neck", 31 | "Kp": 100, 32 | "Kd": 10, 33 | "TargetTheta0": 0, 34 | "UseWorldCoord": 0 35 | }, 36 | { 37 | "ID": 3, 38 | "Name": "right_hip", 39 | "Kp": 500, 40 | "Kd": 50, 41 | "TargetTheta0": 0, 42 | "UseWorldCoord": 0 43 | }, 44 | { 45 | "ID": 4, 46 | "Name": "right_knee", 47 | "Kp": 500, 48 | "Kd": 50, 49 | "TargetTheta0": 0, 50 | "UseWorldCoord": 0 51 | }, 52 | { 53 | "ID": 5, 54 | "Name": "right_ankle", 55 | "Kp": 400, 56 | "Kd": 40, 57 | "TargetTheta0": 0, 58 | "UseWorldCoord": 0 59 | }, 60 | { 61 | "ID": 6, 62 | "Name": "right_shoulder", 63 | "Kp": 400, 64 | "Kd": 40, 65 | "TargetTheta0": 0, 66 | "UseWorldCoord": 0 67 | }, 68 | { 69 | "ID": 7, 70 | "Name": "right_elbow", 71 | "Kp": 300, 72 | "Kd": 30, 73 | "TargetTheta0": 0, 74 | "UseWorldCoord": 0 75 | }, 76 | { 77 | "ID": 8, 78 | "Name": "right_wrist", 79 | "Kp": 0, 80 | "Kd": 0, 81 | "TargetTheta0": 0, 82 | "UseWorldCoord": 0 83 | }, 84 | { 85 | "ID": 9, 86 | "Name": "left_hip", 87 | "Kp": 500, 88 | "Kd": 50, 89 | "TargetTheta0": 0, 90 | "UseWorldCoord": 0 91 | }, 92 | { 93 | "ID": 10, 94 | "Name": "left_knee", 95 | "Kp": 500, 96 | "Kd": 50, 97 | "TargetTheta0": 0, 98 | "UseWorldCoord": 0 99 | }, 100 | { 101 | "ID": 11, 102 | "Name": "left_ankle", 103 | "Kp": 400, 104 | "Kd": 40, 105 | "TargetTheta0": 0, 106 | "UseWorldCoord": 0 107 | }, 108 | { 109 | "ID": 12, 110 | "Name": "left_shoulder", 111 | "Kp": 400, 112 | "Kd": 40, 113 | "TargetTheta0": 0, 114 | "UseWorldCoord": 0 115 | }, 116 | { 117 | "ID": 13, 118 | "Name": "left_elbow", 119 | "Kp": 300, 120 | "Kd": 30, 121 | "TargetTheta0": 0, 122 | "UseWorldCoord": 0 123 | }, 124 | { 125 | "ID": 14, 126 | "Name": "left_wrist", 127 | "Kp": 0, 128 | "Kd": 0, 129 | "TargetTheta0": 0, 130 | "UseWorldCoord": 0 131 | } 132 | ] 133 | } -------------------------------------------------------------------------------- /TMT/data/normalizer/normalizer.npz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/normalizer/normalizer.npz -------------------------------------------------------------------------------- /TMT/data/policies/dog3d_canter.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/dog3d_canter.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /TMT/data/policies/dog3d_canter.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/dog3d_canter.ckpt.index -------------------------------------------------------------------------------- /TMT/data/policies/dog3d_jump.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/dog3d_jump.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /TMT/data/policies/dog3d_jump.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/dog3d_jump.ckpt.index -------------------------------------------------------------------------------- /TMT/data/policies/dog3d_pace.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/dog3d_pace.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /TMT/data/policies/dog3d_pace.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/dog3d_pace.ckpt.index -------------------------------------------------------------------------------- /TMT/data/policies/dog3d_trot.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/dog3d_trot.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /TMT/data/policies/dog3d_trot.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/dog3d_trot.ckpt.index -------------------------------------------------------------------------------- /TMT/data/policies/humanoid3d_jog.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/humanoid3d_jog.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /TMT/data/policies/humanoid3d_jog.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/humanoid3d_jog.ckpt.index -------------------------------------------------------------------------------- /TMT/data/policies/humanoid3d_run.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/humanoid3d_run.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /TMT/data/policies/humanoid3d_run.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/humanoid3d_run.ckpt.index -------------------------------------------------------------------------------- /TMT/data/policies/humanoid3d_walk.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/humanoid3d_walk.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /TMT/data/policies/humanoid3d_walk.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/policies/humanoid3d_walk.ckpt.index -------------------------------------------------------------------------------- /TMT/data/shaders/Depth_PS.glsl: -------------------------------------------------------------------------------- 1 | #version 130 2 | 3 | in vec3 WorldPosition; 4 | out vec4 outColor; 5 | 6 | void main() 7 | { 8 | outColor = vec4( 1.f, 1.f, 1.f, 1.f ); 9 | } -------------------------------------------------------------------------------- /TMT/data/shaders/DownSample_PS.glsl: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform sampler2D gBufferTex; 4 | in vec2 TexCoord; 5 | 6 | out vec4 outColor; 7 | 8 | void main(void) { 9 | outColor = texture(gBufferTex, TexCoord.xy); 10 | } -------------------------------------------------------------------------------- /TMT/data/shaders/DrawTexture_PS.glsl: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | uniform sampler2D gTex; 4 | 5 | in vec2 TexCoord; 6 | 7 | out vec4 outColor; 8 | 9 | void main(void) 10 | { 11 | vec3 color = texture(gTex, TexCoord.xy).rgb; 12 | outColor = vec4(color, 1.0); 13 | } 14 | -------------------------------------------------------------------------------- /TMT/data/shaders/FullScreenQuad_VS.glsl: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec3 inPosition; 4 | in vec3 inNormal; 5 | in vec2 inTexCoord; 6 | 7 | out vec3 Normal; 8 | out vec2 TexCoord; 9 | 10 | void main(void) { 11 | gl_Position = vec4(inPosition.xy, 0.0, 1.0); 12 | Normal = inNormal; 13 | TexCoord = inTexCoord; 14 | } -------------------------------------------------------------------------------- /TMT/data/shaders/Lighting_Lambert_PS.glsl: -------------------------------------------------------------------------------- 1 | #version 130 2 | 3 | uniform sampler2D gTexture; 4 | uniform sampler2D gShadowTex; 5 | 6 | uniform mat4 gShadowProj; 7 | uniform vec3 gLightDir; 8 | uniform vec3 gLightColour; 9 | uniform vec3 gAmbientColour; 10 | uniform vec4 gMaterialData; 11 | uniform vec4 gFogColor; 12 | uniform vec4 gFogData; 13 | 14 | in vec3 ViewPos; 15 | in vec3 Normal; 16 | in vec4 VertColor; 17 | in vec2 TexCoord; 18 | 19 | out vec4 outColor; 20 | 21 | float CalcDiffuse(vec3 world_space_normal, vec3 light_direction) // Roughness value [0 ~ 1], 0 is same as Lambertian 22 | { 23 | vec3 n = world_space_normal; 24 | vec3 l = light_direction; 25 | float l_dot_n = dot( l, n); 26 | float diffuse = clamp(l_dot_n, 0, 1); 27 | return diffuse; 28 | } 29 | 30 | float CalcSpecular( 31 | const vec3 world_normal, 32 | const vec3 light_direction, 33 | const vec3 view_direction) 34 | { 35 | 36 | vec3 n = world_normal; 37 | vec3 v = view_direction; 38 | vec3 l = light_direction; 39 | vec3 h = normalize(view_direction + light_direction); 40 | 41 | float n_dot_h = dot(n, h); 42 | float spec = clamp(n_dot_h, 0, 1); 43 | spec = pow(spec, 10); 44 | return 0.0; 45 | } 46 | 47 | vec3 CalculateBRDF( vec3 normal, vec3 light_dir, 48 | vec3 light_colour, vec3 view_dir, float roughness, 49 | vec3 albedo ) 50 | { 51 | float diffuse_coef = CalcDiffuse(normal, light_dir); 52 | float spec_coef = CalcSpecular(normal, light_dir, view_dir); 53 | 54 | return diffuse_coef * light_colour * albedo 55 | + spec_coef * light_colour; 56 | } 57 | 58 | vec2 RotateDirections(vec2 Dir, float theta) 59 | { 60 | float cos_theta = cos(theta); 61 | float sin_theta = sin(theta); 62 | return vec2(Dir.x*cos_theta - Dir.y*sin_theta, 63 | Dir.x*sin_theta + Dir.y*cos_theta); 64 | } 65 | 66 | float CalculateShadow(vec3 view_pos, vec3 normal) 67 | { 68 | float bias = 0.04f; 69 | vec4 position = vec4(view_pos + bias * normal, 1.f); 70 | 71 | vec3 shadow_coord = (gShadowProj * position).xyz; 72 | float depth = ( shadow_coord.z * 0.5 + 0.5 );// + 0.01f; 73 | depth = min(depth, 1); 74 | 75 | shadow_coord.xy = shadow_coord.xy * 0.5f + 0.5f; 76 | 77 | float sample_depth = ( texture( gShadowTex, shadow_coord.xy).r ); 78 | sample_depth = (depth <= sample_depth) ? 1.f : 0.f; 79 | 80 | 81 | // pcf taps for anti-aliasing 82 | const int num_samples = 16; 83 | vec2 poissonDisk[num_samples] = vec2[]( vec2( -0.6474742f, 0.6028621f ), 84 | vec2( 0.0939157f, 0.6783564f ), 85 | vec2( -0.3371512f, 0.04865054f ), 86 | vec2( -0.4010732f, 0.914994f ), 87 | vec2( -0.2793565f, 0.4456959f ), 88 | vec2( -0.6683437f, -0.1396244f ), 89 | vec2( -0.6369296f, -0.6966243f ), 90 | vec2( -0.2684143f, -0.3756073f ), 91 | vec2( 0.1146429f, -0.8692533f ), 92 | vec2( 0.0697926f, 0.01110036f ), 93 | vec2( 0.4677842f, 0.5375957f ), 94 | vec2( 0.377133f, -0.3518053f ), 95 | vec2( 0.6722369f, 0.03702459f ), 96 | vec2( 0.6890426f, -0.5889201f ), 97 | vec2( -0.8208677f, 0.2444565f ), 98 | vec2( 0.8431721f, 0.3903837f )); 99 | float shadow_map_size = 2048; 100 | float r = 2; 101 | 102 | const vec4 noise_params = vec4(1, 1, 0.1, 0.2); 103 | vec2 seed = view_pos.xy * noise_params.xy + noise_params.zw; 104 | float rand_theta = fract(sin(dot(seed, vec2(12.9898,78.233))) * 43.5453); 105 | rand_theta = 2 * 3.14159f * rand_theta - 3.14159f; 106 | 107 | for ( int i = 0; i < num_samples; ++i ) 108 | { 109 | vec2 dir = poissonDisk[i]; 110 | dir = RotateDirections(dir, rand_theta); 111 | vec2 tap_coord = shadow_coord.xy + r * dir / shadow_map_size; 112 | float tap = texture( gShadowTex, tap_coord ).r; 113 | sample_depth += ( depth <= tap ) ? 1.f : 0.f; 114 | } 115 | 116 | float shadow_coef = sample_depth / (num_samples + 1); 117 | return shadow_coef; 118 | } 119 | 120 | vec3 CalcAmbient(vec3 normal, vec3 albedo) 121 | { 122 | vec3 ambient = gAmbientColour; 123 | //ambient = vec3(0.6, 0.6, 0.6); 124 | ambient *= albedo; 125 | return ambient; 126 | } 127 | 128 | float CalcFog(vec3 pos) 129 | { 130 | float fog_cutoff = gFogData.x; 131 | float fade_distsq = gFogData.y; 132 | float dist = length(pos); 133 | dist = max(0, dist - fog_cutoff); 134 | float fog = 1 - exp(-fade_distsq * dist * dist); 135 | return fog; 136 | } 137 | 138 | void main() 139 | { 140 | float roughness = gMaterialData.x; 141 | bool enable_albedo_tex = gMaterialData.y != 0; 142 | vec3 view_dir = -normalize(ViewPos); 143 | vec3 norm = normalize(Normal); 144 | 145 | vec3 albedo = VertColor.rgb; 146 | if (enable_albedo_tex) 147 | { 148 | vec4 tex_col = texture(gTexture, TexCoord); 149 | albedo.rgb = albedo.rgb * tex_col.rgb; 150 | } 151 | 152 | float shadow_coef = CalculateShadow(ViewPos, norm); 153 | vec3 light_colour = gLightColour; 154 | light_colour *= shadow_coef; 155 | vec3 light_result = CalculateBRDF(norm, gLightDir, 156 | light_colour, view_dir, roughness, 157 | albedo); 158 | 159 | vec3 ambient = CalcAmbient(norm, albedo.rgb); 160 | light_result += ambient; 161 | 162 | //float fog = CalcFog(ViewPos); 163 | //fog *= gFogColor.w; 164 | //light_result = mix(light_result, gFogColor.xyz, fog); 165 | 166 | outColor = vec4(light_result, VertColor[3]); 167 | } -------------------------------------------------------------------------------- /TMT/data/shaders/Mesh_VS.glsl: -------------------------------------------------------------------------------- 1 | #version 130 2 | 3 | uniform mat4 uModelViewMatrix; 4 | uniform mat4 uProjectionMatrix; 5 | uniform vec4 uColor; 6 | 7 | in vec3 inPosition; 8 | in vec3 inNormal; 9 | in vec2 inTexCoord; 10 | 11 | out vec3 ViewPos; 12 | out vec3 Normal; 13 | out vec4 VertColor; 14 | out vec2 TexCoord; 15 | 16 | void main() 17 | { 18 | vec4 view_pos = uModelViewMatrix * vec4(inPosition.xyz, 1.f); 19 | vec4 WVP_Pos = uProjectionMatrix * view_pos; 20 | 21 | gl_Position = WVP_Pos; 22 | ViewPos = view_pos.xyz; 23 | Normal = normalize(uModelViewMatrix * vec4(inNormal.xyz, 0.f)).xyz; 24 | VertColor = uColor; 25 | TexCoord = inTexCoord.xy; 26 | } 27 | -------------------------------------------------------------------------------- /TMT/data/shaders/PostProcessing_PS.glsl: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | uniform sampler2D gBufferTex; 4 | uniform sampler2D gTonemapLUT; 5 | 6 | in vec2 TexCoord; 7 | 8 | out vec4 outColor; 9 | 10 | 11 | // Filmic tonemapping 12 | const float _ExposureEV = exp(0.25 * 0.69314718055994530941723212145818); 13 | const vec3 _LogLut_Params = vec3(1.0 / 1024.0, 1.0 / 32.0, 31.0); 14 | #define COLOR_GRADING_PRECISE_LOG 0 15 | struct ParamsLogC 16 | { 17 | float cut; 18 | float a; 19 | float b; 20 | float c; 21 | float d; 22 | float e; 23 | float f; 24 | }; 25 | const ParamsLogC LogC = ParamsLogC(0.011361, 5.555556, 0.047996, 0.244161, 0.386036, 5.301883, 0.092819); 26 | float log10(float n) 27 | { 28 | const float kLogBase10 = 0.30102999566; // 1.0 / log2(10.0) 29 | return log2( n ) * kLogBase10; 30 | } 31 | vec3 log10(vec3 n) 32 | { 33 | const float kLogBase10 = 0.30102999566; // 1.0 / log2(10.0) 34 | return log2( n ) * kLogBase10; 35 | } 36 | float LinearToLogC_Precise(float x) 37 | { 38 | float o; 39 | if (x > LogC.cut) 40 | o = LogC.c * log10(LogC.a * x + LogC.b) + LogC.d; 41 | else 42 | o = LogC.e * x + LogC.f; 43 | return o; 44 | } 45 | vec3 LinearToLogC(vec3 x) 46 | { 47 | #if COLOR_GRADING_PRECISE_LOG 48 | return vec3( 49 | LinearToLogC_Precise(x.x), 50 | LinearToLogC_Precise(x.y), 51 | LinearToLogC_Precise(x.z) 52 | ); 53 | #else 54 | return vec3(LogC.c) * log10(LogC.a * x + LogC.b) + vec3(LogC.d); 55 | #endif 56 | } 57 | vec3 ApplyLut2d(sampler2D tex, vec3 uvw, vec3 scaleOffset) 58 | { 59 | // Strip format where `height = sqrt(width)` 60 | uvw.z *= scaleOffset.z; 61 | float shift = floor(uvw.z); 62 | uvw.xy = uvw.xy * scaleOffset.z * scaleOffset.xy + scaleOffset.xy * 0.5; 63 | uvw.x += shift * scaleOffset.y; 64 | uvw.xyz = mix(texture(tex, uvw.xy).rgb, texture(tex, uvw.xy + vec2(scaleOffset.y, 0)).rgb, uvw.z - shift); 65 | return uvw; 66 | } 67 | 68 | 69 | // Vignette effect 70 | const float intensity = 0.25 * 3; 71 | const float smoothness = 0.2 * 5; 72 | const float roundness = 1.0; 73 | #define saturate(x) clamp((x), 0.0, 1.0) 74 | 75 | 76 | #define USE_COLOR_GRADING 0 77 | #define USE_VIGNETTE_EFFECT 1 78 | 79 | 80 | void main(void) 81 | { 82 | vec3 color = texture(gBufferTex, TexCoord.xy).rgb; 83 | 84 | // Color grading (Filmic tonemapping) 85 | #if USE_COLOR_GRADING 86 | color *= _ExposureEV; 87 | vec3 colorLogC = saturate(LinearToLogC(color)); 88 | color = ApplyLut2d(gTonemapLUT, colorLogC, _LogLut_Params); 89 | #endif 90 | 91 | // Vignette effect from Unity postprocessing Stack 92 | #if USE_VIGNETTE_EFFECT 93 | vec2 d = abs(TexCoord - vec2(0.5, 0.5)) * intensity; 94 | d = pow(saturate(d), vec2(roundness)); 95 | float vfactor = pow(saturate(1.0 - dot(d, d)), smoothness); 96 | color *= vfactor; 97 | #endif 98 | 99 | outColor = vec4(color, 1.0); 100 | } -------------------------------------------------------------------------------- /TMT/data/shaders/Sky_PS.glsl: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec3 vWorldPosition; 4 | in vec3 vSunDirection; 5 | in float vSunfade; 6 | in vec3 vBetaR; 7 | in vec3 vBetaM; 8 | in float vSunE; 9 | 10 | out vec4 outColor; 11 | 12 | const float luminance = 0.6; 13 | const float mieDirectionalG = 0.8; 14 | 15 | const vec3 cameraPos = vec3( 0.0, 0.0, 0.0 ); 16 | 17 | // constants for atmospheric scattering 18 | const float pi = 3.141592653589793238462643383279502884197169; 19 | 20 | const float n = 1.0003; // refractive index of air 21 | const float N = 2.545E25; // number of molecules per unit volume for air at 288.15K and 1013mb (sea level -45 celsius) 22 | 23 | // optical length at zenith for molecules 24 | const float rayleighZenithLength = 8.4E3; 25 | const float mieZenithLength = 1.25E3; 26 | const vec3 up = vec3( 0.0, 1.0, 0.0 ); 27 | // 66 arc seconds -> degrees, and the cosine of that 28 | const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324; 29 | 30 | // 3.0 / ( 16.0 * pi ) 31 | const float THREE_OVER_SIXTEENPI = 0.05968310365946075; 32 | // 1.0 / ( 4.0 * pi ) 33 | const float ONE_OVER_FOURPI = 0.07957747154594767; 34 | 35 | float rayleighPhase( float cosTheta ) { 36 | return THREE_OVER_SIXTEENPI * ( 1.0 + pow( cosTheta, 2.0 ) ); 37 | } 38 | 39 | float hgPhase( float cosTheta, float g ) { 40 | float g2 = pow( g, 2.0 ); 41 | float inverse = 1.0 / pow( 1.0 - 2.0 * g * cosTheta + g2, 1.5 ); 42 | return ONE_OVER_FOURPI * ( ( 1.0 - g2 ) * inverse ); 43 | } 44 | 45 | // Filmic ToneMapping http://filmicgames.com/archives/75 46 | const float A = 0.15; 47 | const float B = 0.50; 48 | const float C = 0.10; 49 | const float D = 0.20; 50 | const float E = 0.02; 51 | const float F = 0.30; 52 | 53 | const float whiteScale = 1.0748724675633854; // 1.0 / Uncharted2Tonemap(1000.0) 54 | 55 | vec3 Uncharted2Tonemap( vec3 x ) { 56 | return ( ( x * ( A * x + C * B ) + D * E ) / ( x * ( A * x + B ) + D * F ) ) - E / F; 57 | } 58 | 59 | 60 | void main() 61 | { 62 | // optical length 63 | // cutoff angle at 90 to avoid singularity in next formula. 64 | float zenithAngle = acos( max( 0.0, dot( up, normalize( vWorldPosition - cameraPos ) ) ) ); 65 | float inverse = 1.0 / ( cos( zenithAngle ) + 0.15 * pow( 93.885 - ( ( zenithAngle * 180.0 ) / pi ), -1.253 ) ); 66 | float sR = rayleighZenithLength * inverse; 67 | float sM = mieZenithLength * inverse; 68 | 69 | // combined extinction factor 70 | vec3 Fex = exp( -( vBetaR * sR + vBetaM * sM ) ); 71 | 72 | // in scattering 73 | float cosTheta = dot( normalize( vWorldPosition - cameraPos ), vSunDirection ); 74 | 75 | float rPhase = rayleighPhase( cosTheta * 0.5 + 0.5 ); 76 | vec3 betaRTheta = vBetaR * rPhase; 77 | 78 | float mPhase = hgPhase( cosTheta, mieDirectionalG ); 79 | vec3 betaMTheta = vBetaM * mPhase; 80 | 81 | vec3 Lin = pow( vSunE * ( ( betaRTheta + betaMTheta ) / ( vBetaR + vBetaM ) ) * ( 1.0 - Fex ), vec3( 1.5 ) ); 82 | Lin *= mix( vec3( 1.0 ), pow( vSunE * ( ( betaRTheta + betaMTheta ) / ( vBetaR + vBetaM ) ) * Fex, vec3( 1.0 / 2.0 ) ), clamp( pow( 1.0 - dot( up, vSunDirection ), 5.0 ), 0.0, 1.0 ) ); 83 | 84 | // nightsky 85 | vec3 direction = normalize( vWorldPosition - cameraPos ); 86 | float theta = acos( direction.y ); // elevation --> y-axis, [-pi/2, pi/2] 87 | float phi = atan( direction.z, direction.x ); // azimuth --> x-axis [-pi/2, pi/2] 88 | vec2 uv = vec2( phi, theta ) / vec2( 2.0 * pi, pi ) + vec2( 0.5, 0.0 ); 89 | vec3 L0 = vec3( 0.1 ) * Fex; 90 | 91 | // composition + solar disc 92 | float sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta ); 93 | L0 += ( vSunE * 19000.0 * Fex ) * sundisk; 94 | 95 | vec3 texColor = ( Lin + L0 ) * 0.04 + vec3( 0.0, 0.0003, 0.00075 ); 96 | 97 | vec3 curr = Uncharted2Tonemap( ( log2( 2.0 / pow( luminance, 4.0 ) ) ) * texColor ); 98 | vec3 color = curr * whiteScale; 99 | 100 | vec3 retColor = pow( color, vec3( 1.0 / ( 1.2 + ( 1.2 * vSunfade ) ) ) ); 101 | 102 | //gl_FragColor = vec4( retColor, 1.0 ); 103 | outColor = vec4( retColor, 1.0 ); 104 | } -------------------------------------------------------------------------------- /TMT/data/shaders/Sky_VS.glsl: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | const vec3 sunPosition = vec3(0.6, 0.67, 0.45); 4 | const float rayleigh = 0.3; 5 | const float turbidity = 20; 6 | const float mieCoefficient = 0.003; 7 | 8 | uniform mat4 modelMatrix; 9 | uniform mat4 modelViewMatrix; 10 | uniform mat4 projectionMatrix; 11 | 12 | in vec3 position; 13 | in vec3 normal; 14 | in vec2 texCoord; 15 | 16 | out vec3 vWorldPosition; 17 | out vec3 vSunDirection; 18 | out float vSunfade; 19 | out vec3 vBetaR; 20 | out vec3 vBetaM; 21 | out float vSunE; 22 | 23 | const vec3 up = vec3( 0.0, 1.0, 0.0 ); 24 | 25 | // constants for atmospheric scattering 26 | const float e = 2.71828182845904523536028747135266249775724709369995957; 27 | const float pi = 3.141592653589793238462643383279502884197169; 28 | 29 | // wavelength of used primaries, according to preetham 30 | const vec3 lambda = vec3( 680E-9, 550E-9, 450E-9 ); 31 | // this pre-calcuation replaces older TotalRayleigh(vec3 lambda) function: 32 | // (8.0 * pow(pi, 3.0) * pow(pow(n, 2.0) - 1.0, 2.0) * (6.0 + 3.0 * pn)) / (3.0 * N * pow(lambda, vec3(4.0)) * (6.0 - 7.0 * pn)) 33 | const vec3 totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 ); 34 | 35 | // mie stuff 36 | // K coefficient for the primaries 37 | const float v = 4.0; 38 | const vec3 K = vec3( 0.686, 0.678, 0.666 ); 39 | // MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K 40 | const vec3 MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 ); 41 | 42 | // earth shadow hack 43 | // cutoffAngle = pi / 1.95; 44 | const float cutoffAngle = 1.6110731556870734; 45 | const float steepness = 1.5; 46 | const float EE = 1000.0; 47 | 48 | float sunIntensity( float zenithAngleCos ) { 49 | zenithAngleCos = clamp( zenithAngleCos, -1.0, 1.0 ); 50 | return EE * max( 0.0, 1.0 - pow( e, -( ( cutoffAngle - acos( zenithAngleCos ) ) / steepness ) ) ); 51 | } 52 | 53 | vec3 totalMie( float T ) { 54 | float c = ( 0.2 * T ) * 10E-18; 55 | return 0.434 * c * MieConst; 56 | } 57 | 58 | void main() 59 | { 60 | vec4 worldPosition = modelMatrix * vec4( position, 1.0 ); 61 | vWorldPosition = worldPosition.xyz; 62 | 63 | gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); 64 | gl_Position.z = gl_Position.w; // set z to camera.far 65 | 66 | vSunDirection = normalize( sunPosition ); 67 | 68 | vSunE = sunIntensity( dot( vSunDirection, up ) ); 69 | 70 | vSunfade = 1.0 - clamp( 1.0 - exp( ( sunPosition.y / 450000.0 ) ), 0.0, 1.0 ); 71 | 72 | float rayleighCoefficient = rayleigh - ( 1.0 * ( 1.0 - vSunfade ) ); 73 | 74 | // extinction (absorbtion + out scattering) 75 | // rayleigh coefficients 76 | vBetaR = totalRayleigh * rayleighCoefficient; 77 | 78 | // mie coefficients 79 | vBetaM = totalMie( turbidity ) * mieCoefficient; 80 | } -------------------------------------------------------------------------------- /TMT/data/shaders/VertColor_PS.glsl: -------------------------------------------------------------------------------- 1 | #version 130 2 | 3 | in vec4 VertColor; 4 | 5 | out vec4 outColor; 6 | 7 | void main() 8 | { 9 | outColor = VertColor; 10 | } -------------------------------------------------------------------------------- /TMT/data/shaders/im3d.glsl: -------------------------------------------------------------------------------- 1 | #if !defined(POINTS) && !defined(LINES) && !defined(TRIANGLES) 2 | #error No primitive type defined 3 | #endif 4 | #if !defined(VERTEX_SHADER) && !defined(GEOMETRY_SHADER) && !defined(FRAGMENT_SHADER) 5 | #error No shader stage defined 6 | #endif 7 | 8 | #define VertexData \ 9 | _VertexData { \ 10 | noperspective float m_edgeDistance; \ 11 | noperspective float m_size; \ 12 | smooth vec4 m_color; \ 13 | } 14 | 15 | #define kAntialiasing 2.0 16 | 17 | #ifdef VERTEX_SHADER 18 | uniform mat4 uViewProjMatrix; 19 | 20 | layout(location=0) in vec4 aPositionSize; 21 | layout(location=1) in vec4 aColor; 22 | 23 | out VertexData vData; 24 | 25 | void main() 26 | { 27 | vData.m_color = aColor.abgr; // swizzle to correct endianness 28 | #if !defined(TRIANGLES) 29 | vData.m_color.a *= smoothstep(0.0, 1.0, aPositionSize.w / kAntialiasing); 30 | #endif 31 | vData.m_size = max(aPositionSize.w, kAntialiasing); 32 | gl_Position = uViewProjMatrix * vec4(aPositionSize.xyz, 1.0); 33 | #if defined(POINTS) 34 | gl_PointSize = vData.m_size; 35 | #endif 36 | } 37 | #endif 38 | 39 | #ifdef GEOMETRY_SHADER 40 | // expand line -> triangle strip 41 | layout(lines) in; 42 | layout(triangle_strip, max_vertices = 4) out; 43 | 44 | uniform vec2 uViewport; 45 | 46 | in VertexData vData[]; 47 | out VertexData vDataOut; 48 | 49 | void main() 50 | { 51 | vec2 pos0 = gl_in[0].gl_Position.xy / gl_in[0].gl_Position.w; 52 | vec2 pos1 = gl_in[1].gl_Position.xy / gl_in[1].gl_Position.w; 53 | 54 | vec2 dir = pos0 - pos1; 55 | dir = normalize(vec2(dir.x, dir.y * uViewport.y / uViewport.x)); // correct for aspect ratio 56 | vec2 tng0 = vec2(-dir.y, dir.x); 57 | vec2 tng1 = tng0 * vData[1].m_size / uViewport; 58 | tng0 = tng0 * vData[0].m_size / uViewport; 59 | 60 | // line start 61 | gl_Position = vec4((pos0 - tng0) * gl_in[0].gl_Position.w, gl_in[0].gl_Position.zw); 62 | vDataOut.m_edgeDistance = -vData[0].m_size; 63 | vDataOut.m_size = vData[0].m_size; 64 | vDataOut.m_color = vData[0].m_color; 65 | EmitVertex(); 66 | 67 | gl_Position = vec4((pos0 + tng0) * gl_in[0].gl_Position.w, gl_in[0].gl_Position.zw); 68 | vDataOut.m_color = vData[0].m_color; 69 | vDataOut.m_edgeDistance = vData[0].m_size; 70 | vDataOut.m_size = vData[0].m_size; 71 | EmitVertex(); 72 | 73 | // line end 74 | gl_Position = vec4((pos1 - tng1) * gl_in[1].gl_Position.w, gl_in[1].gl_Position.zw); 75 | vDataOut.m_edgeDistance = -vData[1].m_size; 76 | vDataOut.m_size = vData[1].m_size; 77 | vDataOut.m_color = vData[1].m_color; 78 | EmitVertex(); 79 | 80 | gl_Position = vec4((pos1 + tng1) * gl_in[1].gl_Position.w, gl_in[1].gl_Position.zw); 81 | vDataOut.m_color = vData[1].m_color; 82 | vDataOut.m_size = vData[1].m_size; 83 | vDataOut.m_edgeDistance = vData[1].m_size; 84 | EmitVertex(); 85 | } 86 | #endif 87 | 88 | #ifdef FRAGMENT_SHADER 89 | in VertexData vData; 90 | 91 | layout(location=0) out vec4 fResult; 92 | 93 | void main() 94 | { 95 | fResult = vData.m_color; 96 | 97 | #if defined(LINES) 98 | float d = abs(vData.m_edgeDistance) / vData.m_size; 99 | d = smoothstep(1.0, 1.0 - (kAntialiasing / vData.m_size), d); 100 | fResult.a *= d; 101 | 102 | #elif defined(POINTS) 103 | float d = length(gl_PointCoord.xy - vec2(0.5)); 104 | d = smoothstep(0.5, 0.5 - (kAntialiasing / vData.m_size), d); 105 | fResult.a *= d; 106 | 107 | #endif 108 | } 109 | #endif 110 | -------------------------------------------------------------------------------- /TMT/data/terrain/plane.txt: -------------------------------------------------------------------------------- 1 | { 2 | "Type": "plane" 3 | } -------------------------------------------------------------------------------- /TMT/data/textures/grid0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/textures/grid0.png -------------------------------------------------------------------------------- /TMT/data/trans-tensors/precomputed_tensor_dog_ptcj.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/trans-tensors/precomputed_tensor_dog_ptcj.pkl -------------------------------------------------------------------------------- /TMT/data/trans-tensors/precomputed_tensor_humanoid_wjr.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/TMT/data/trans-tensors/precomputed_tensor_humanoid_wjr.pkl -------------------------------------------------------------------------------- /TMT/requirements.txt: -------------------------------------------------------------------------------- 1 | numpy<1.19.0,>=1.16.0 2 | PyOpenGL==3.1.5 3 | PyOpenGL-accelerate==3.1.5 4 | tensorflow==1.15.5 5 | mpi4py>=3.0.0 6 | scikit-learn>=0.23.2 7 | pandas>=1.1.1 8 | matplotlib>=3.3.1 9 | tqdm>=4.48.2 -------------------------------------------------------------------------------- /TMT/scripts/__init__.py: -------------------------------------------------------------------------------- 1 | from . import * -------------------------------------------------------------------------------- /TMT/scripts/agent_builder.py: -------------------------------------------------------------------------------- 1 | import json 2 | import numpy as np 3 | from learning.ppo_agent import PPOAgent 4 | from carl_ppo_agent import CarlPPOAgent 5 | from tmt_carl_agent import TMTCarlAgent 6 | from tmt_deepmimic_agent import TMTDeepMimicAgent 7 | from deepmimic_ppo_agent import DeepMimicPPOAgent 8 | 9 | import sys 10 | sys.path.append('../DeepMimic/') 11 | import learning.agent_builder as DMAgentBuilder 12 | 13 | AGENT_TYPE_KEY = "AgentType" 14 | 15 | def build_agent(world, id, file, seed=None): 16 | agent = None 17 | with open(file) as data_file: 18 | json_data = json.load(data_file) 19 | 20 | assert AGENT_TYPE_KEY in json_data 21 | agent_type = json_data[AGENT_TYPE_KEY] 22 | 23 | if agent_type == CarlPPOAgent.NAME: 24 | agent = CarlPPOAgent(world, id, json_data, seed) 25 | elif agent_type == DeepMimicPPOAgent.NAME: 26 | agent = DeepMimicPPOAgent(world, id, json_data, seed) 27 | elif agent_type == TMTCarlAgent.NAME: 28 | agent = TMTCarlAgent(world, id, json_data, seed) 29 | elif agent_type == TMTDeepMimicAgent.NAME: 30 | agent = TMTDeepMimicAgent(world, id, json_data, seed) 31 | else: 32 | agent = DMAgentBuilder.build_agent(world, id, file) 33 | 34 | return agent 35 | -------------------------------------------------------------------------------- /TMT/scripts/carl_env.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from CarlCore import CarlCore 3 | from env.env import Env 4 | from env.action_space import ActionSpace 5 | 6 | class CarlEnv(Env): 7 | def __init__(self, args, arg_parser, enable_draw): 8 | super().__init__(args, enable_draw) 9 | 10 | self._core = CarlCore.cCarlCore(enable_draw) 11 | 12 | rand_seed = arg_parser.parse_int('rand_seed', -1) 13 | if rand_seed < 0: 14 | rand_seed = np.random.randint(np.iinfo(np.int32).max) 15 | self._core.SeedRand(rand_seed) 16 | 17 | self._core.ParseArgs(args) 18 | self._core.Init() 19 | return 20 | 21 | def update(self, timestep): 22 | self._core.Update(timestep) 23 | 24 | def reset(self): 25 | self._core.Reset() 26 | 27 | def get_time(self): 28 | return self._core.GetTime() 29 | 30 | def get_name(self): 31 | return self._core.GetName() 32 | 33 | # rendering and UI interface 34 | def draw(self): 35 | self._core.Draw() 36 | 37 | def keyboard(self, key, x, y): 38 | self._core.Keyboard(key, x, y) 39 | 40 | def mouse_click(self, button, state, x, y): 41 | self._core.MouseClick(button, state, x, y) 42 | 43 | def mouse_move(self, x, y): 44 | self._core.MouseMove(x, y) 45 | 46 | def reshape(self, w, h): 47 | self._core.Reshape(w, h) 48 | 49 | def shutdown(self): 50 | self._core.Shutdown() 51 | 52 | def is_done(self): 53 | return self._core.IsDone() 54 | 55 | def set_playback_speed(self, speed): 56 | self._core.SetPlaybackSpeed(speed) 57 | 58 | def set_updates_per_sec(self, updates_per_sec): 59 | self._core.SetUpdatesPerSec(updates_per_sec) 60 | 61 | def get_win_width(self): 62 | return self._core.GetWinWidth() 63 | 64 | def get_win_height(self): 65 | return self._core.GetWinHeight() 66 | 67 | def get_num_update_substeps(self): 68 | return self._core.GetNumUpdateSubsteps() 69 | 70 | # rl interface 71 | def is_rl_scene(self): 72 | return self._core.IsRLScene() 73 | 74 | def get_num_agents(self): 75 | return self._core.GetNumAgents() 76 | 77 | def need_new_action(self, agent_id): 78 | return self._core.NeedNewAction(agent_id) 79 | 80 | def record_state(self, agent_id): 81 | return np.array(self._core.RecordState(agent_id)) 82 | 83 | def record_goal(self, agent_id): 84 | return np.array(self._core.RecordGoal(agent_id)) 85 | 86 | def record_phase(self, agent_id): 87 | return self._core.RecordPhase(agent_id) 88 | 89 | def record_motion_label(self, agent_id): 90 | return self._core.RecordMotionLabel(agent_id) 91 | 92 | def record_goal_target(self, agent_id): 93 | return np.array(self._core.RecordGoalTarget(agent_id)) 94 | 95 | def get_action_space(self, agent_id): 96 | return ActionSpace(self._core.GetActionSpace(agent_id)) 97 | 98 | def set_action(self, agent_id, action): 99 | return self._core.SetAction(agent_id, action.tolist()) 100 | 101 | def switch_motion(self, agent_id, target_motion, target_phase): 102 | return self._core.SwitchMotion(agent_id, target_motion, target_phase) 103 | 104 | def get_state_size(self, agent_id): 105 | return self._core.GetStateSize(agent_id) 106 | 107 | def get_goal_size(self, agent_id): 108 | return self._core.GetGoalSize(agent_id) 109 | 110 | def get_action_size(self, agent_id): 111 | return self._core.GetActionSize(agent_id) 112 | 113 | def get_num_actions(self, agent_id): 114 | return self._core.GetNumActions(agent_id) 115 | 116 | def build_state_offset(self, agent_id): 117 | return np.array(self._core.BuildStateOffset(agent_id)) 118 | 119 | def build_state_scale(self, agent_id): 120 | return np.array(self._core.BuildStateScale(agent_id)) 121 | 122 | def build_goal_offset(self, agent_id): 123 | return np.array(self._core.BuildGoalOffset(agent_id)) 124 | 125 | def build_goal_scale(self, agent_id): 126 | return np.array(self._core.BuildGoalScale(agent_id)) 127 | 128 | def build_action_offset(self, agent_id): 129 | return np.array(self._core.BuildActionOffset(agent_id)) 130 | 131 | def build_action_scale(self, agent_id): 132 | return np.array(self._core.BuildActionScale(agent_id)) 133 | 134 | def build_action_bound_min(self, agent_id): 135 | return np.array(self._core.BuildActionBoundMin(agent_id)) 136 | 137 | def build_action_bound_max(self, agent_id): 138 | return np.array(self._core.BuildActionBoundMax(agent_id)) 139 | 140 | def build_state_norm_groups(self, agent_id): 141 | return np.array(self._core.BuildStateNormGroups(agent_id)) 142 | 143 | def build_goal_norm_groups(self, agent_id): 144 | return np.array(self._core.BuildGoalNormGroups(agent_id)) 145 | 146 | def calc_reward(self, agent_id): 147 | return self._core.CalcReward(agent_id) 148 | 149 | def get_reward_min(self, agent_id): 150 | return self._core.GetRewardMin(agent_id) 151 | 152 | def get_reward_max(self, agent_id): 153 | return self._core.GetRewardMax(agent_id) 154 | 155 | def get_reward_fail(self, agent_id): 156 | return self._core.GetRewardFail(agent_id) 157 | 158 | def get_reward_succ(self, agent_id): 159 | return self._core.GetRewardSucc(agent_id) 160 | 161 | def is_episode_end(self): 162 | return self._core.IsEpisodeEnd() 163 | 164 | def check_terminate(self, agent_id): 165 | return Env.Terminate(self._core.CheckTerminate(agent_id)) 166 | 167 | def check_valid_episode(self): 168 | return self._core.CheckValidEpisode() 169 | 170 | def log_val(self, agent_id, val): 171 | self._core.LogVal(agent_id, float(val)) 172 | return 173 | 174 | def log_gating_weights(self, agent_id, weights): 175 | self._core.LogGatingWeights(agent_id, weights.tolist()) 176 | return 177 | 178 | def log_primitives_means_stds(self, agent_id, num_primitives, means, stds): 179 | self._core.LogPrimitivesMeanStd(agent_id, num_primitives, means, stds) 180 | return 181 | 182 | def set_sample_count(self, count): 183 | self._core.SetSampleCount(count) 184 | return 185 | 186 | def set_mode(self, mode): 187 | self._core.SetMode(mode.value) 188 | return 189 | -------------------------------------------------------------------------------- /TMT/scripts/carl_rl_world.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import agent_builder as AgentBuilder 3 | import learning.tf_util as TFUtil 4 | from learning.rl_agent import RLAgent 5 | from tmt_carl_agent import TMTCarlAgent 6 | from tmt_deepmimic_agent import TMTDeepMimicAgent 7 | from util.logger import Logger 8 | import learning.rl_world as DeepMimicRLWorld 9 | 10 | 11 | class RLWorld(DeepMimicRLWorld.RLWorld): 12 | 13 | def __init__(self, env, arg_parser): 14 | TFUtil.disable_gpu() 15 | 16 | self.env = env 17 | self.arg_parser = arg_parser 18 | self._enable_training = True 19 | self.train_agents = [] 20 | self.parse_args(arg_parser) 21 | 22 | self.build_agents() 23 | return 24 | 25 | def build_agents(self): 26 | num_agents = self.env.get_num_agents() 27 | self.agents = [] 28 | 29 | Logger.print('') 30 | Logger.print('Num Agents: {:d}'.format(num_agents)) 31 | 32 | agent_files = self.arg_parser.parse_strings('agent_files') 33 | assert(len(agent_files) == num_agents or len(agent_files) == 0) 34 | 35 | model_files = self.arg_parser.parse_strings('model_files') 36 | motion_names = self.arg_parser.parse_strings('motion_names') 37 | primitive_models = self.arg_parser.parse_strings('primitive_models') 38 | gating_models = self.arg_parser.parse_strings('gating_models') 39 | normalizer_file = self.arg_parser.parse_string('normalizer_file') 40 | transition_tensor_file = self.arg_parser.parse_string('transition_tensor_file') 41 | 42 | output_path = self.arg_parser.parse_string('output_path') 43 | int_output_path = self.arg_parser.parse_string('int_output_path') 44 | 45 | global_rand_seed = self.arg_parser.parse_int('rand_seed') 46 | 47 | for i in range(num_agents): 48 | curr_file = agent_files[i] 49 | curr_agent = self._build_agent(i, curr_file, global_rand_seed) 50 | 51 | if curr_agent is not None: 52 | curr_agent.output_dir = output_path 53 | curr_agent.int_output_dir = int_output_path 54 | Logger.print(str(curr_agent)) 55 | 56 | if len(model_files) == len(motion_names): 57 | curr_agent.load_actor_models(motion_names, model_files) 58 | elif len(gating_models) == len(primitive_models) == len(motion_names): 59 | for motion_name, primitive_model_path, gating_model_path in zip(motion_names, primitive_models, gating_models): 60 | curr_agent.load_primitive_model(motion_name, primitive_model_path) 61 | curr_agent.load_gating_model(motion_name, gating_model_path) 62 | 63 | if normalizer_file: 64 | curr_agent.load_normalizer(normalizer_file) 65 | 66 | if transition_tensor_file and (type(curr_agent) == TMTCarlAgent or type(curr_agent) == TMTDeepMimicAgent): 67 | curr_agent.load_transition_tensor(transition_tensor_file) 68 | 69 | self.agents.append(curr_agent) 70 | Logger.print('') 71 | 72 | self.set_enable_training(self.enable_training) 73 | return 74 | 75 | def _build_agent(self, id, agent_file, seed): 76 | Logger.print('Agent {:d}: {}'.format(id, agent_file)) 77 | if agent_file == 'none': 78 | agent = None 79 | else: 80 | agent = AgentBuilder.build_agent(self, id, agent_file, seed) 81 | assert (agent != None), 'Failed to build agent {:d} from: {}'.format(id, agent_file) 82 | 83 | return agent 84 | 85 | def isDone(self): 86 | isDone = False 87 | for agent in self.agents: 88 | if (agent is not None): 89 | isDone |= agent.isDone() 90 | return isDone 91 | 92 | def keyboard(self, key, x, y): 93 | key_val = int.from_bytes(key, byteorder='big') 94 | self.env.keyboard(key_val, x, y) 95 | 96 | for agent in self.agents: 97 | if type(agent) == TMTCarlAgent or type(agent) == TMTDeepMimicAgent: 98 | agent.keyboard(key) 99 | return 100 | -------------------------------------------------------------------------------- /TMT/scripts/fc_2layers_16units.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tf_util_extend as TFUtilExtend 3 | 4 | NAME = "fc_2layers_16units" 5 | 6 | def build_net(input_tf, reuse=False): 7 | layers = [16, 8] 8 | activation = tf.nn.leaky_relu 9 | net_count = 0 10 | 11 | h, net_count = TFUtilExtend.fc_net(input_tf, net_count, layers, activation=activation, reuse=reuse) 12 | h = activation(h) 13 | return h -------------------------------------------------------------------------------- /TMT/scripts/fc_2layers_512units.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tf_util_extend as TFUtilExtend 3 | 4 | NAME = "fc_2layers_512units" 5 | 6 | def build_net(input_tfs, reuse=False): 7 | layers = [512, 256] 8 | activation = tf.nn.relu 9 | 10 | input_tf = tf.concat(axis=-1, values=input_tfs) 11 | h, _ = TFUtilExtend.fc_net(input_tf, 0, layers, activation=activation, reuse=reuse) 12 | h = activation(h) 13 | return h -------------------------------------------------------------------------------- /TMT/scripts/fc_3layers_512units_256units_gating.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tf_util_extend as TFUtilExtend 3 | 4 | NAME = "fc_3layers_512units_256units_gating" 5 | 6 | def build_net(input_tfs, reuse=False): 7 | state_layers = [512, 256] 8 | goal_layers = [512, 256] 9 | combined_layer = 256 10 | activation = tf.nn.relu 11 | net_count = 0 12 | 13 | # State branch 14 | h_state, net_count = TFUtilExtend.fc_net(input_tfs[0], net_count, state_layers, activation=activation, reuse=reuse) 15 | h_state = activation(h_state) 16 | 17 | # Goal branch 18 | h_goal, net_count = TFUtilExtend.fc_net(input_tfs[1], net_count, goal_layers, activation=activation, reuse=reuse) 19 | h_goal = activation(h_goal) 20 | 21 | # Combine the the input branches 22 | h = tf.concat(axis=-1, values=[h_state, h_goal]) 23 | h, _ = TFUtilExtend.fc_net(h, net_count, [combined_layer], activation=activation, reuse=reuse) 24 | return h -------------------------------------------------------------------------------- /TMT/scripts/fc_3layers_512units_256units_reduced_state_gating.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tf_util_extend as TFUtilExtend 3 | 4 | NAME = "fc_3layers_512units_256units_reduced_state_gating" 5 | 6 | def build_net(input_tfs, reuse=False): 7 | state_layers = [128, 32] 8 | goal_layers = [512, 256] 9 | combined_layer = 256 10 | activation = tf.nn.relu 11 | net_count = 0 12 | 13 | # State branch 14 | h_state, net_count = TFUtilExtend.fc_net(input_tfs[0], net_count, state_layers, activation=activation, reuse=reuse) 15 | h_state = activation(h_state) 16 | 17 | # Goal branch 18 | h_goal, net_count = TFUtilExtend.fc_net(input_tfs[1], net_count, goal_layers, activation=activation, reuse=reuse) 19 | h_goal = activation(h_goal) 20 | 21 | # Combine the the input branches 22 | h = tf.concat(axis=-1, values=[h_state, h_goal]) 23 | h, _ = TFUtilExtend.fc_net(h, net_count, [combined_layer], activation=activation, reuse=reuse) 24 | h = activation(h) 25 | 26 | return h -------------------------------------------------------------------------------- /TMT/scripts/mpi_util_extend.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from mpi4py import MPI 3 | 4 | ROOT_PROC_RANK = 0 5 | 6 | def gather(x): 7 | return MPI.COMM_WORLD.gather(x, root=ROOT_PROC_RANK) 8 | -------------------------------------------------------------------------------- /TMT/scripts/net_builder.py: -------------------------------------------------------------------------------- 1 | import learning.nets.fc_2layers_1024units as fc_2layers_1024units 2 | import fc_2layers_512units as fc_2layers_512units 3 | import fc_3layers_512units_256units_gating as fc_3layers_512units_256units_gating 4 | import fc_3layers_512units_256units_reduced_state_gating as fc_3layers_512units_256units_reduced_state_gating 5 | import fc_2layers_16units as fc_2layers_16units 6 | 7 | def build_net(net_name, input_tfs, reuse=False): 8 | net = None 9 | 10 | if (net_name == fc_2layers_1024units.NAME): 11 | net = fc_2layers_1024units.build_net(input_tfs, reuse) 12 | elif (net_name == fc_3layers_512units_256units_gating.NAME): 13 | net = fc_3layers_512units_256units_gating.build_net(input_tfs, reuse) 14 | elif (net_name == fc_3layers_512units_256units_reduced_state_gating.NAME): 15 | net = fc_3layers_512units_256units_reduced_state_gating.build_net(input_tfs, reuse) 16 | elif (net_name == fc_2layers_512units.NAME): 17 | net = fc_2layers_512units.build_net(input_tfs, reuse) 18 | elif (net_name == fc_2layers_16units.NAME): 19 | net = fc_2layers_16units.build_net(input_tfs, reuse) 20 | else: 21 | assert False, 'Unsupported net: ' + net_name 22 | 23 | return net 24 | -------------------------------------------------------------------------------- /TMT/scripts/tf_util_extend.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import os 4 | 5 | xavier_initializer = tf.contrib.layers.xavier_initializer() 6 | 7 | def fc_net(input, net_count, layers_sizes, activation, reuse=None, flatten=False): # build fully connected network 8 | curr_tf = input 9 | for i, size in enumerate(layers_sizes): 10 | with tf.variable_scope(str(net_count), reuse=reuse): 11 | curr_tf = tf.layers.dense(inputs=curr_tf, 12 | units=size, 13 | kernel_initializer=xavier_initializer, 14 | activation = activation if i < len(layers_sizes)-1 else None) 15 | net_count += 1 16 | 17 | if flatten: 18 | curr_tf = tf.contrib.layers.flatten(curr_tf) 19 | 20 | return curr_tf, net_count 21 | 22 | def conv1d_net(input, net_count, kernals_sizes, filters_sizes, activation, reuse=None, flatten=False): # build 1D convolutional network 23 | curr_tf = input 24 | for i, size in enumerate(kernals_sizes): 25 | with tf.variable_scope(str(net_count), reuse=reuse): 26 | curr_tf = tf.layers.conv1d(inputs=curr_tf, 27 | filters=filters_sizes[i], 28 | kernel_size=size, 29 | kernel_initializer=xavier_initializer, 30 | activation = activation if i < len(kernals_sizes)-1 else None) 31 | net_count += 1 32 | 33 | if flatten: 34 | curr_tf = tf.contrib.layers.flatten(curr_tf) 35 | 36 | return curr_tf, net_count 37 | 38 | def conv2d_net(input, net_count, kernals_sizes, filters_sizes, activation, reuse=None, flatten=False): # build 2D convolutional network 39 | curr_tf = input 40 | for i, size in enumerate(kernals_sizes): 41 | with tf.variable_scope(str(net_count), reuse=reuse): 42 | curr_tf = tf.layers.conv2d(inputs=curr_tf, 43 | filters=filters_sizes[i], 44 | kernel_size=[size, size], 45 | kernel_initializer=xavier_initializer, 46 | activation = activation if i < len(kernals_sizes)-1 else None) 47 | net_count += 1 48 | 49 | if flatten: 50 | curr_tf = tf.contrib.layers.flatten(curr_tf) 51 | 52 | return curr_tf, net_count 53 | -------------------------------------------------------------------------------- /TMT/scripts/tmt_carl_agent.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import glob 4 | import time 5 | import numpy as np 6 | import pickle as pkl 7 | 8 | from env.env import Env 9 | from carl_ppo_agent import CarlPPOAgent 10 | 11 | 12 | class TMTCarlAgent(CarlPPOAgent): 13 | NAME = "TMT_CARL_Agent" 14 | 15 | Transition_Phase_Threshold = 2e-3 16 | Force_Update_Action_After_Switch_Motion = True 17 | Transition_Expired_Time = 2 18 | 19 | def __init__(self, world, id, json_data, seed): 20 | self.transition_queue = [] 21 | self.motion_prev = '' 22 | self.prev_switch_motion_time = 0 23 | self.transition_queue_update_time = 0 24 | self.transition_tensor = None 25 | super().__init__(world, id, json_data, seed) 26 | return 27 | 28 | def load_transition_tensor(self, tmt_file): 29 | self.transition_tensor = pkl.load(open(tmt_file, "rb")) 30 | return 31 | 32 | def reset(self): 33 | self.transition_queue.clear() 34 | super().reset() 35 | return 36 | 37 | def update(self, timestep): 38 | self._update_transition_queue(timestep) 39 | super().update(timestep) 40 | return 41 | 42 | def _update_transition_queue(self, timestep): 43 | wall_time = self._get_time() 44 | 45 | if len(self.transition_queue) > 0: 46 | src_phase = self._record_phase() 47 | head = self.transition_queue[0] 48 | target_src_phase, target_dst_phase, src_motion, dst_motion = head['src_phase'], head['dst_phase'], head['src_motion'], head['dst_motion'] 49 | # print("Transition Queue Head: {src_motion: %s, dst_motion: %s, src_phase: %.3f, dst_phase: %.3f, wall_time: %.3f}, wall_time: %.3f, src_phase: %.3f" % \ 50 | # (src_motion, dst_motion, target_src_phase, target_dst_phase, head['wall_time'], wall_time, src_phase)) 51 | 52 | # delete expired transitions 53 | if wall_time - head['wall_time'] > self.Transition_Expired_Time: 54 | self.transition_queue = self.transition_queue[1:] 55 | print('pop expired transitions from queue') 56 | return 57 | 58 | if np.abs(src_phase - target_src_phase) < self.Transition_Phase_Threshold: 59 | print("Switch motion {}: {:.3f} - {}: {:.3f} (wall_time={:.6f})\n".format(src_motion, src_phase, dst_motion, target_dst_phase, self._get_time())) 60 | dst_motion_index = self.motion_names.index(dst_motion) 61 | _ = self.switch_motion(src_motion, dst_motion_index, target_dst_phase) 62 | self.transition_queue = self.transition_queue[1:] 63 | 64 | self.transition_queue_update_time += timestep 65 | if self.transition_queue_update_time >= 1./60: 66 | self.transition_queue_update_time -= 1./60 67 | src_motion = self._record_motion_label() 68 | src_motion = self.motion_names[src_motion] 69 | if src_motion == 'Jump' and (wall_time - self.prev_switch_motion_time) > 0.8: 70 | if self.motion_prev == 'Trot': 71 | self.keyboard(b'w') 72 | elif self.motion_prev == 'Canter': 73 | self.keyboard(b'q') 74 | return 75 | 76 | def keyboard(self, key): 77 | src_motion = self._record_motion_label() 78 | src_motion = self.motion_names[src_motion] 79 | src_phase = self._record_phase() 80 | 81 | ret = None 82 | if key == b'q': 83 | ret = self.query_transition(src_motion, src_phase, 'Canter') 84 | elif key == b'e' and src_motion != 'Pace': 85 | ret = self.query_transition(src_motion, src_phase, 'Jump') 86 | if key == b'w': 87 | ret = self.query_transition(src_motion, src_phase, 'Trot') 88 | elif key == b'a' or key == b'd': 89 | ret = self.query_transition(src_motion, src_phase, 'Pace') 90 | 91 | if ret: 92 | target_src_phase, target_dst_phase, dst_motion = ret['src_phase'], ret['dst_phase'], ret['dst_motion'] 93 | if np.abs(src_phase - target_src_phase) < self.Transition_Phase_Threshold: 94 | print("Switch motion {}: {:.3f} - {}: {:.3f}\n".format(src_motion, src_phase, dst_motion, target_dst_phase)) 95 | dst_motion_index = self.motion_names.index(dst_motion) 96 | _ = self.switch_motion(src_motion, dst_motion_index, target_dst_phase) 97 | else: 98 | self.transition_queue.append(ret) 99 | print("Transition {}: {:.3f} - {}: {:.3f} is added to transiton queue".format(src_motion, target_src_phase, dst_motion, target_dst_phase)) 100 | return 101 | 102 | def query_transition(self, src_motion, src_phase, dst_motion): 103 | if self.transition_tensor is None: return None 104 | if src_phase < 0: return None 105 | if src_motion == dst_motion: return None 106 | if len(self.transition_queue) > 0 and self.transition_queue[0]['src_motion'] == src_motion and self.transition_queue[0]['dst_motion'] == dst_motion: return None 107 | 108 | transition_key = "{}_{}".format(src_motion, dst_motion) 109 | if transition_key not in self.transition_tensor or len(self.transition_tensor[transition_key]) == 0: 110 | return None 111 | 112 | # Look for the source phase 113 | osrc_phases = [] 114 | osrc_phase_idxs = [] 115 | for idx, tup in enumerate(self.transition_tensor[transition_key]): 116 | osrc_phases.append(tup[0]) 117 | osrc_phase_idxs.append(idx) 118 | 119 | if len(osrc_phases) == 0: 120 | return None 121 | 122 | osrc_phases = np.array(osrc_phases, dtype=np.float32) 123 | osrc_phases = np.concatenate([osrc_phases, osrc_phases + 1]) 124 | _phase_indices = np.where((osrc_phases - src_phase) > 0)[0] % len(osrc_phase_idxs) 125 | if len(_phase_indices) == 0: 126 | return None 127 | src_phase_idx = osrc_phase_idxs[_phase_indices[0]] # Use the closest next phase 128 | # print(osrc_phases - src_phase, src_phase_idx) 129 | 130 | ret = self.transition_tensor[transition_key][src_phase_idx] 131 | osrc_phase, odst_phase = ret[0], ret[1] 132 | 133 | if osrc_phase >= 0: 134 | out_dict = { 135 | "src_motion": src_motion, 136 | "dst_motion": dst_motion, 137 | "src_phase": osrc_phase, 138 | "dst_phase": odst_phase, 139 | "wall_time": self._get_time() 140 | } 141 | return out_dict 142 | return None 143 | 144 | def switch_motion(self, src_motion, dst_motion, dst_phase): 145 | wall_time = self._get_time() 146 | self.motion_prev = src_motion 147 | self.prev_switch_motion_time = wall_time 148 | succ = self._switch_motion(dst_motion, float(dst_phase)) 149 | if self.Force_Update_Action_After_Switch_Motion: 150 | self._update_new_action() 151 | return succ 152 | 153 | def _record_phase(self): 154 | motion_label = self.world.env.record_phase(self.id) 155 | return motion_label 156 | 157 | def _switch_motion(self, target_motion, target_phase): 158 | succ = self.world.env.switch_motion(self.id, target_motion, target_phase) 159 | return succ 160 | 161 | def _get_time(self): 162 | return self.world.env.get_time() 163 | -------------------------------------------------------------------------------- /TMT/scripts/tmt_deepmimic_agent.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle as pkl 3 | from env.env import Env 4 | from deepmimic_ppo_agent import DeepMimicPPOAgent 5 | import random 6 | 7 | 8 | class TMTDeepMimicAgent(DeepMimicPPOAgent): 9 | NAME = "TMT_DeepMimic_Agent" 10 | 11 | Transition_Phase_Threshold = 2e-3 12 | Force_Update_Action_After_Switch_Motion = True 13 | Transition_Expired_Time = 2 14 | 15 | def __init__(self, world, id, json_data, seed): 16 | self.transition_queue = [] 17 | self.motion_prev = '' 18 | self.prev_switch_motion_time = 0 19 | self.transition_queue_update_time = 0 20 | self.transition_tensor = None 21 | super().__init__(world, id, json_data, seed) 22 | return 23 | 24 | def load_transition_tensor(self, tmt_file): 25 | self.transition_tensor = pkl.load(open(tmt_file, "rb")) 26 | return 27 | 28 | def reset(self): 29 | self.transition_queue.clear() 30 | super().reset() 31 | return 32 | 33 | def update(self, timestep): 34 | self._update_transition_queue(timestep) 35 | super().update(timestep) 36 | return 37 | 38 | def _update_transition_queue(self, timestep): 39 | wall_time = self._get_time() 40 | 41 | if len(self.transition_queue) > 0: 42 | src_phase = self._record_phase() 43 | head = self.transition_queue[0] 44 | target_src_phase, target_dst_phase, src_motion, dst_motion = head['src_phase'], head['dst_phase'], head['src_motion'], head['dst_motion'] 45 | # print("Transition Queue Head: {src_motion: %s, dst_motion: %s, src_phase: %.3f, dst_phase: %.3f, wall_time: %.3f}, wall_time: %.3f, src_phase: %.3f" % \ 46 | # (src_motion, dst_motion, target_src_phase, target_dst_phase, head['wall_time'], wall_time, src_phase)) 47 | 48 | # delete expired transitions 49 | if wall_time - head['wall_time'] > self.Transition_Expired_Time: 50 | self.transition_queue = self.transition_queue[1:] 51 | print('pop expired transitions from queue') 52 | return 53 | 54 | if np.abs(src_phase - target_src_phase) < self.Transition_Phase_Threshold: 55 | print("Switch motion {}: {:.3f} - {}: {:.3f} (wall_time={:.6f})\n".format(src_motion, src_phase, dst_motion, target_dst_phase, self._get_time())) 56 | dst_motion_index = self.motion_names.index(dst_motion) 57 | _ = self.switch_motion(src_motion, dst_motion_index, target_dst_phase) 58 | self.transition_queue = self.transition_queue[1:] 59 | return 60 | 61 | def keyboard(self, key): 62 | src_motion_idx = self._record_motion_label() 63 | src_motion = self.motion_names[src_motion_idx] 64 | src_phase = self._record_phase() 65 | 66 | ret = None 67 | key_char = key.decode("utf-8") 68 | if key_char >= '1' and key_char <= '9': 69 | dst_motion_index = int(key_char) - 1 70 | if src_motion_idx != dst_motion_index and dst_motion_index < len(self.motion_names): 71 | ret = self.query_transition(src_motion, src_phase, self.motion_names[dst_motion_index]) 72 | 73 | if ret: 74 | target_src_phase, target_dst_phase, dst_motion = ret['src_phase'], ret['dst_phase'], ret['dst_motion'] 75 | if np.abs(src_phase - target_src_phase) < self.Transition_Phase_Threshold: 76 | print("Switch motion {}: {:.3f} - {}: {:.3f}\n".format(src_motion, src_phase, dst_motion, target_dst_phase)) 77 | dst_motion_index = self.motion_names.index(dst_motion) 78 | _ = self.switch_motion(src_motion, dst_motion_index, target_dst_phase) 79 | else: 80 | self.transition_queue.append(ret) 81 | print("Transition {}: {:.3f} - {}: {:.3f} is added to transiton queue".format(src_motion, target_src_phase, dst_motion, target_dst_phase)) 82 | return 83 | 84 | def query_transition(self, src_motion, src_phase, dst_motion): 85 | if self.transition_tensor is None: return None 86 | if len(self.transition_queue) > 0 and self.transition_queue[0]['src_motion'] == src_motion and self.transition_queue[0]['dst_motion'] == dst_motion: return None 87 | 88 | transition_key = "{}_{}".format(src_motion, dst_motion) 89 | if transition_key not in self.transition_tensor or len(self.transition_tensor[transition_key]) == 0: 90 | return None 91 | 92 | # Look for the source phase 93 | osrc_phases = [] 94 | osrc_phase_idxs = [] 95 | for idx, tup in enumerate(self.transition_tensor[transition_key]): 96 | osrc_phases.append(tup[0]) 97 | osrc_phase_idxs.append(idx) 98 | 99 | if len(osrc_phases) == 0: 100 | return None 101 | 102 | osrc_phases = np.array(osrc_phases, dtype=np.float32) 103 | osrc_phases = np.concatenate([osrc_phases, osrc_phases + 1]) 104 | _phase_indices = np.where((osrc_phases - src_phase) > 0)[0] % len(osrc_phase_idxs) 105 | if len(_phase_indices) == 0: 106 | return None 107 | src_phase_idx = osrc_phase_idxs[_phase_indices[0]] # Use the closest next phase 108 | ret = self.transition_tensor[transition_key][src_phase_idx] 109 | osrc_phase, odst_phase = ret[0], ret[1] 110 | 111 | if osrc_phase >= 0: 112 | out_dict = { 113 | "src_motion": src_motion, 114 | "dst_motion": dst_motion, 115 | "src_phase": osrc_phase, 116 | "dst_phase": odst_phase, 117 | "wall_time": self._get_time() 118 | } 119 | return out_dict 120 | return None 121 | 122 | def switch_motion(self, src_motion, dst_motion, dst_phase): 123 | wall_time = self._get_time() 124 | self.motion_prev = src_motion 125 | self.prev_switch_motion_time = wall_time 126 | succ = self._switch_motion(dst_motion, float(dst_phase)) 127 | if self.Force_Update_Action_After_Switch_Motion: 128 | self._update_new_action() 129 | return succ 130 | 131 | def _record_phase(self): 132 | motion_label = self.world.env.record_phase(self.id) 133 | return motion_label 134 | 135 | def _switch_motion(self, target_motion, target_phase): 136 | succ = self.world.env.switch_motion(self.id, target_motion, target_phase) 137 | return succ 138 | 139 | def _get_time(self): 140 | return self.world.env.get_time() 141 | 142 | def isDone(self): 143 | return False -------------------------------------------------------------------------------- /images/teaser.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inventec-ai-center/transition_motion_tensor/902814c399b9e7a62230ef240e40616eb579b514/images/teaser.jpg --------------------------------------------------------------------------------