Merge pull request #201 from aftersomemath:refactor-simulate

PiperOrigin-RevId: 465044152
Change-Id: I54e65a30764a444dbd175260314d685fd7ffa6a1
This commit is contained in:
Saran Tunyasuvunakool
2022-08-03 15:32:15 +01:00
25 changed files with 3351 additions and 2286 deletions
-82
View File
@@ -70,14 +70,6 @@ if(MUJOCO_HARDEN)
endif()
endif()
# Utility library
add_library(uitools STATIC)
target_sources(uitools PRIVATE uitools.h uitools.c)
target_include_directories(uitools PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_options(uitools PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
target_link_libraries(uitools PUBLIC glfw mujoco::mujoco)
target_link_options(uitools PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS})
# Build sample binaries
add_executable(compile compile.cc)
target_compile_options(compile PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
@@ -126,85 +118,13 @@ target_link_libraries(
)
target_link_options(record PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS})
if(APPLE)
set(SIMULATE_RESOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../dist/mujoco.icns)
elseif(WIN32)
set(SIMULATE_RESOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../dist/appicon.rc)
else()
set(SIMULATE_RESOURCE_FILES "")
endif()
add_executable(simulate simulate.cc array_safety.h ${SIMULATE_RESOURCE_FILES})
target_compile_options(simulate PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
if(WIN32)
add_custom_command(
TARGET simulate
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/../dist/mujoco.ico
${CMAKE_CURRENT_SOURCE_DIR}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E rm ${CMAKE_CURRENT_SOURCE_DIR}/mujoco.ico
)
endif()
target_link_libraries(
simulate
mujoco::mujoco
uitools
glfw
Threads::Threads
)
target_link_options(simulate PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS})
if(APPLE)
target_sources(simulate PRIVATE macos_save.mm)
target_link_libraries(simulate "-framework Cocoa")
endif()
if(APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS)
set_target_properties(
simulate
PROPERTIES INSTALL_RPATH @executable_path/../Frameworks
BUILD_WITH_INSTALL_RPATH TRUE
RESOURCE ${SIMULATE_RESOURCE_FILES}
MACOSX_BUNDLE TRUE
MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/../dist/Info.plist.simulate.in
MACOSX_BUNDLE_BUNDLE_NAME "MuJoCo"
MACOSX_BUNDLE_GUI_IDENTIFIER "org.mujoco.app"
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_INFO_STRING ${PROJECT_VERSION}
MACOSX_BUNDLE_LONG_VERSION_STRING ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION}
MACOSX_BUNDLE_ICON_FILE "mujoco.icns"
MACOSX_BUNDLE_COPYRIGHT "Copyright 2021 DeepMind Technologies Limited."
)
macro(embed_in_bundle target)
add_dependencies(${target} simulate)
set_target_properties(
${target}
PROPERTIES INSTALL_RPATH @executable_path/../Frameworks
BUILD_WITH_INSTALL_RPATH TRUE
RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:simulate>
)
endmacro()
embed_in_bundle(basic simulate)
embed_in_bundle(compile simulate)
embed_in_bundle(derivative simulate)
embed_in_bundle(record simulate)
embed_in_bundle(testspeed simulate)
embed_in_bundle(testxml simulate)
# Embed mujoco.framework inside the App bundle.
add_custom_command(
TARGET simulate
POST_BUILD
COMMAND mkdir -p $<TARGET_FILE_DIR:simulate>/../Frameworks
COMMAND rm -rf $<TARGET_FILE_DIR:simulate>/../Frameworks/mujoco.framework
COMMAND cp -a $<TARGET_FILE_DIR:mujoco::mujoco>/../../../mujoco.framework
$<TARGET_FILE_DIR:simulate>/../Frameworks/
)
endif()
# Do not install if macOS Bundles are created as RPATH is managed manually there.
@@ -225,7 +145,6 @@ if(_INSTALL_SAMPLES)
record
testspeed
testxml
simulate
INSTALL_DIRECTORY
"${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}"
LIB_DIRS
@@ -241,7 +160,6 @@ if(_INSTALL_SAMPLES)
record
testspeed
testxml
simulate
EXPORT ${PROJECT_NAME}
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT samples
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples
-3
View File
@@ -11,6 +11,3 @@ all:
$(CXX) $(COMMON) derivative.cc -lmujoco -fopenmp -o ../bin/derivative
$(CXX) $(COMMON) basic.cc -lmujoco -lglfw -o ../bin/basic
$(CXX) $(COMMON) record.cc -lmujoco -lglfw -o ../bin/record
$(CC) -c -O2 -I../include uitools.c
$(CXX) $(COMMON) uitools.o simulate.cc -lmujoco -lglfw -o ../bin/simulate
rm *.o
-4
View File
@@ -17,7 +17,3 @@ all:
clang++ $(ALLFLAGS) derivative.cc -framework mujoco -o derivative
clang++ $(ALLFLAGS) basic.cc -framework mujoco -lglfw -o basic
clang++ $(ALLFLAGS) record.cc -framework mujoco -lglfw -o record
clang -c $(CFLAGS) uitools.c
clang++ -c $(CXXFLAGS) macos_save.mm
clang++ $(ALLFLAGS) simulate.cc uitools.o macos_save.o -framework mujoco -framework Cocoa -lglfw -o simulate
rm *.o
-1
View File
@@ -15,5 +15,4 @@ all:
cl $(COMMON) derivative.cc /openmp ../lib/mujoco.lib
cl $(COMMON) basic.cc ../lib/glfw3dll.lib ../lib/mujoco.lib
cl $(COMMON) record.cc ../lib/glfw3dll.lib ../lib/mujoco.lib
cl $(COMMON) simulate.cc uitools.c ../lib/glfw3dll.lib ../lib/mujoco.lib
del *.obj
+5 -3
View File
@@ -58,8 +58,10 @@ findorfetch(
EXCLUDE_FROM_ALL
)
option(MUJOCO_SAMPLES_STATIC_GLFW "Link MuJoCo sample apps against GLFW statically." ON)
if(MUJOCO_SAMPLES_STATIC_GLFW)
option(MUJOCO_EXTRAS_STATIC_GLFW
"Link MuJoCo sample apps and simulate libraries against GLFW statically." ON
)
if(MUJOCO_EXTRAS_STATIC_GLFW)
set(BUILD_SHARED_LIBS_OLD ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS
OFF
@@ -88,7 +90,7 @@ findorfetch(
EXCLUDE_FROM_ALL
)
if(MUJOCO_SAMPLES_STATIC_GLFW)
if(MUJOCO_EXTRAS_STATIC_GLFW)
set(BUILD_SHARED_LIBS
${BUILD_SHARED_LIBS_OLD}
CACHE BOOL "Build SHARED libraries" FORCE
-33
View File
@@ -1,33 +0,0 @@
// Copyright 2022 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <sstream>
#include <string>
#include <Cocoa/Cocoa.h>
std::string getSavePath(const char* filename) {
NSSavePanel* panel = [NSSavePanel savePanel];
NSURL* userDocumentsDir = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask].firstObject;
[panel setDirectoryURL:userDocumentsDir];
[panel setNameFieldStringValue:[NSString stringWithUTF8String:filename]];
if ([panel runModal] == NSModalResponseOK) {
std::ostringstream s;
s << [panel.URL.path cStringUsingEncoding:NSUTF8StringEncoding];
return s.str();
} else {
return "";
}
}
-2185
View File
File diff suppressed because it is too large Load Diff
-313
View File
@@ -1,313 +0,0 @@
// Copyright 2021 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "uitools.h"
#include <stdio.h>
#include <string.h>
//-------------------------------- Internal GLFW callbacks ------------------------------
// update state
static void uiUpdateState(GLFWwindow* wnd) {
// extract data from user pointer
uiUserPointer* ptr = (uiUserPointer*)glfwGetWindowUserPointer(wnd);
mjuiState* state = ptr->state;
// mouse buttons
state->left = (glfwGetMouseButton(wnd, GLFW_MOUSE_BUTTON_LEFT)==GLFW_PRESS);
state->right = (glfwGetMouseButton(wnd, GLFW_MOUSE_BUTTON_RIGHT)==GLFW_PRESS);
state->middle = (glfwGetMouseButton(wnd, GLFW_MOUSE_BUTTON_MIDDLE)==GLFW_PRESS);
// keyboard modifiers
state->control = (glfwGetKey(wnd, GLFW_KEY_LEFT_CONTROL)==GLFW_PRESS ||
glfwGetKey(wnd, GLFW_KEY_RIGHT_CONTROL)==GLFW_PRESS);
state->shift = (glfwGetKey(wnd, GLFW_KEY_LEFT_SHIFT)==GLFW_PRESS ||
glfwGetKey(wnd, GLFW_KEY_RIGHT_SHIFT)==GLFW_PRESS);
state->alt = (glfwGetKey(wnd, GLFW_KEY_LEFT_ALT)==GLFW_PRESS ||
glfwGetKey(wnd, GLFW_KEY_RIGHT_ALT)==GLFW_PRESS);
// swap left and right if Alt
if (state->alt) {
int tmp = state->left;
state->left = state->right;
state->right = tmp;
}
// get mouse position, scale by buffer-to-window ratio
double x, y;
glfwGetCursorPos(wnd, &x, &y);
x *= ptr->buffer2window;
y *= ptr->buffer2window;
// invert y to match OpenGL convention
y = state->rect[0].height - y;
// save
state->dx = x - state->x;
state->dy = y - state->y;
state->x = x;
state->y = y;
// find mouse rectangle
state->mouserect = mjr_findRect(mju_round(x), mju_round(y), state->nrect-1, state->rect+1) + 1;
}
// keyboard
static void uiKeyboard(GLFWwindow* wnd, int key, int scancode, int act, int mods) {
// release: nothing to do
if (act==GLFW_RELEASE) {
return;
}
// extract data from user pointer
uiUserPointer* ptr = (uiUserPointer*)glfwGetWindowUserPointer(wnd);
mjuiState* state = ptr->state;
// update state
uiUpdateState(wnd);
// set key info
state->type = mjEVENT_KEY;
state->key = key;
state->keytime = glfwGetTime();
// application-specific processing
ptr->uiEvent(state);
}
// mouse button
static void uiMouseButton(GLFWwindow* wnd, int button, int act, int mods) {
// extract data from user pointer
uiUserPointer* ptr = (uiUserPointer*)glfwGetWindowUserPointer(wnd);
mjuiState* state = ptr->state;
// update state
uiUpdateState(wnd);
// translate button
if (button==GLFW_MOUSE_BUTTON_LEFT) {
button = mjBUTTON_LEFT;
} else if (button==GLFW_MOUSE_BUTTON_RIGHT) {
button = mjBUTTON_RIGHT;
} else {
button = mjBUTTON_MIDDLE;
}
// swap left and right if Alt
if (glfwGetKey(wnd, GLFW_KEY_LEFT_ALT)==GLFW_PRESS ||
glfwGetKey(wnd, GLFW_KEY_RIGHT_ALT)==GLFW_PRESS) {
if (button==mjBUTTON_LEFT) {
button = mjBUTTON_RIGHT;
} else if (button==mjBUTTON_RIGHT) {
button = mjBUTTON_LEFT;
}
}
// press
if (act==GLFW_PRESS) {
// detect doubleclick: 250 ms
if (button==state->button && glfwGetTime()-state->buttontime<0.25) {
state->doubleclick = 1;
} else {
state->doubleclick = 0;
}
// set info
state->type = mjEVENT_PRESS;
state->button = button;
state->buttontime = glfwGetTime();
// start dragging
if (state->mouserect) {
state->dragbutton = state->button;
state->dragrect = state->mouserect;
}
}
// release
else {
state->type = mjEVENT_RELEASE;
}
// application-specific processing
ptr->uiEvent(state);
// stop dragging after application processing
if (state->type==mjEVENT_RELEASE) {
state->dragrect = 0;
state->dragbutton = 0;
}
}
// mouse move
static void uiMouseMove(GLFWwindow* wnd, double xpos, double ypos) {
// extract data from user pointer
uiUserPointer* ptr = (uiUserPointer*)glfwGetWindowUserPointer(wnd);
mjuiState* state = ptr->state;
// no buttons down: nothing to do
if (!state->left && !state->right && !state->middle) {
return;
}
// update state
uiUpdateState(wnd);
// set move info
state->type = mjEVENT_MOVE;
// application-specific processing
ptr->uiEvent(state);
}
// scroll
static void uiScroll(GLFWwindow* wnd, double xoffset, double yoffset) {
// extract data from user pointer
uiUserPointer* ptr = (uiUserPointer*)glfwGetWindowUserPointer(wnd);
mjuiState* state = ptr->state;
// update state
uiUpdateState(wnd);
// set scroll info, scale by buffer-to-window ratio
state->type = mjEVENT_SCROLL;
state->sx = xoffset * ptr->buffer2window;
state->sy = yoffset * ptr->buffer2window;
// application-specific processing
ptr->uiEvent(state);
}
// resize
static void uiResize(GLFWwindow* wnd, int width, int height) {
// extract data from user pointer
uiUserPointer* ptr = (uiUserPointer*)glfwGetWindowUserPointer(wnd);
mjuiState* state = ptr->state;
// set layout
ptr->uiLayout(state);
// update state
uiUpdateState(wnd);
// set resize info
state->type = mjEVENT_RESIZE;
// stop dragging
state->dragbutton = 0;
state->dragrect = 0;
// application-specific processing (unless called with 0,0 from uiModify)
if (width && height) {
ptr->uiEvent(state);
}
}
//----------------------------------- Public API ----------------------------------------
// Compute suitable font scale.
int uiFontScale(GLFWwindow* wnd) {
// compute framebuffer-to-window ratio
int width_win, width_buf, height;
glfwGetWindowSize(wnd, &width_win, &height);
glfwGetFramebufferSize(wnd, &width_buf, &height);
double b2w = (double)width_buf / (double)width_win;
// compute PPI
int width_MM, height_MM;
glfwGetMonitorPhysicalSize(glfwGetPrimaryMonitor(), &width_MM, &height_MM);
int width_vmode = glfwGetVideoMode(glfwGetPrimaryMonitor())->width;
double PPI = 25.4 * b2w * (double)width_vmode / (double)width_MM;
// estimate font scaling, guard against unrealistic PPI
int fs;
if (width_buf>width_win) {
fs = mju_round(b2w * 100);
} else if (PPI>50 && PPI<350) {
fs = mju_round(PPI);
} else {
fs = 150;
}
fs = mju_round(fs * 0.02) * 50;
fs = mjMIN(300, mjMAX(100, fs));
return fs;
}
// Set internal and user-supplied UI callbacks in GLFW window.
void uiSetCallback(GLFWwindow* wnd, mjuiState* state,
uiEventFn uiEvent, uiLayoutFn uiLayout) {
// make container with user-supplied objects and set window pointer
uiUserPointer* ptr = (uiUserPointer*) mju_malloc(sizeof(uiUserPointer));
ptr->state = state;
ptr->uiEvent = uiEvent;
ptr->uiLayout = uiLayout;
glfwSetWindowUserPointer(wnd, ptr);
// compute framebuffer-to-window pixel ratio
int width_win, width_buf, height;
glfwGetWindowSize(wnd, &width_win, &height);
glfwGetFramebufferSize(wnd, &width_buf, &height);
ptr->buffer2window = (double)width_buf / (double)width_win;
// set internal callbacks
glfwSetKeyCallback(wnd, uiKeyboard);
glfwSetCursorPosCallback(wnd, uiMouseMove);
glfwSetMouseButtonCallback(wnd, uiMouseButton);
glfwSetScrollCallback(wnd, uiScroll);
glfwSetWindowSizeCallback(wnd, uiResize);
}
// Clear UI callbacks in GLFW window.
void uiClearCallback(GLFWwindow* wnd) {
// clear container
if (glfwGetWindowUserPointer(wnd)) {
mju_free(glfwGetWindowUserPointer(wnd));
glfwSetWindowUserPointer(wnd, NULL);
}
// clear internal callbacks
glfwSetKeyCallback(wnd, NULL);
glfwSetCursorPosCallback(wnd, NULL);
glfwSetMouseButtonCallback(wnd, NULL);
glfwSetScrollCallback(wnd, NULL);
glfwSetWindowSizeCallback(wnd, NULL);
}
// Modify UI structure.
void uiModify(GLFWwindow* wnd, mjUI* ui, mjuiState* state, mjrContext* con) {
mjui_resize(ui, con);
mjr_addAux(ui->auxid, ui->width, ui->maxheight, ui->spacing.samples, con);
uiResize(wnd, 0, 0);
mjui_update(-1, -1, ui, state, con);
}
-59
View File
@@ -1,59 +0,0 @@
// Copyright 2021 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MUJOCO_UITOOLS_H_
#define MUJOCO_UITOOLS_H_
#include <GLFW/glfw3.h>
#include <mujoco/mujoco.h>
// this is a C-API
#if defined(__cplusplus)
extern "C" {
#endif
// User-supplied callback function types.
typedef void (*uiEventFn)(mjuiState* state);
typedef void (*uiLayoutFn)(mjuiState* state);
// Container for GLFW window pointer.
struct _uiUserPointer {
mjuiState* state;
uiEventFn uiEvent;
uiLayoutFn uiLayout;
double buffer2window;
};
typedef struct _uiUserPointer uiUserPointer;
// Set internal and user-supplied UI callbacks in GLFW window.
void uiSetCallback(GLFWwindow* wnd, mjuiState* state,
uiEventFn uiEvent, uiLayoutFn uiLayout);
// Clear UI callbacks in GLFW window.
void uiClearCallback(GLFWwindow* wnd);
// Compute suitable font scale.
int uiFontScale(GLFWwindow* wnd);
// Modify UI structure.
void uiModify(GLFWwindow* wnd, mjUI* ui, mjuiState* state, mjrContext* con);
#if defined(__cplusplus)
}
#endif
#endif // MUJOCO_UITOOLS_H_