Make seed fixed for random number generator when using <texture mark="random"/>

Also improve documentation regarding texture height and width attributes.

PiperOrigin-RevId: 648427167
Change-Id: I59a0fe75d3526f8563a5252584ed60ef7fb8fd70
This commit is contained in:
Yuval Tassa
2024-07-01 11:10:24 -07:00
committed by Copybara-Service
parent c1ade85490
commit f2c3be470f
3 changed files with 56 additions and 20 deletions
+15 -5
View File
@@ -19,11 +19,11 @@
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <optional>
#include <random>
#include <sstream>
#include <string>
#include <string_view>
@@ -3227,9 +3227,15 @@ mjCTexture::~mjCTexture() {
// insert random dots
static void randomdot(unsigned char* rgb, const double* markrgb,
int width, int height, double probability) {
// make distribution using fixed seed
std::mt19937_64 rng;
rng.seed(42);
std::uniform_real_distribution<double> dist(0, 1);
// sample
for (int r=0; r<height; r++) {
for (int c=0; c<width; c++) {
if (rand()<probability*RAND_MAX) {
if (dist(rng)<probability) {
for (int j=0; j<3; j++) {
rgb[3*(r*width+c)+j] = (mjtByte)(255*markrgb[j]);
}
@@ -3779,14 +3785,18 @@ void mjCTexture::Compile(const mjVFS* vfs) {
// builtin
if (builtin != mjBUILTIN_NONE) {
// check size
if (width<1 || height<1) {
throw mjCError(this, "Invalid width or height of builtin texture");
// check width
if (width<1) {
throw mjCError(this, "Invalid width of builtin texture");
}
// adjust height of cube texture
if (type != mjTEXTURE_2D) {
height = 6*width;
} else {
if (height<1) {
throw mjCError(this, "Invalid height of builtin texture");
}
}
// allocate data