|
From: <is...@us...> - 2008-08-21 01:11:39
|
Revision: 3395
http://personalrobots.svn.sourceforge.net/personalrobots/?rev=3395&view=rev
Author: isucan
Date: 2008-08-21 01:11:49 +0000 (Thu, 21 Aug 2008)
Log Message:
-----------
few more updates
Modified Paths:
--------------
pkg/trunk/motion_planning/ompl/code/ompl/base/util/random.h
pkg/trunk/motion_planning/ompl/code/ompl/base/util/src/random.cpp
pkg/trunk/util/random_utils/src/test.cpp
Modified: pkg/trunk/motion_planning/ompl/code/ompl/base/util/random.h
===================================================================
--- pkg/trunk/motion_planning/ompl/code/ompl/base/util/random.h 2008-08-21 01:05:27 UTC (rev 3394)
+++ pkg/trunk/motion_planning/ompl/code/ompl/base/util/random.h 2008-08-21 01:11:49 UTC (rev 3395)
@@ -24,12 +24,15 @@
void random_init(rngState *state);
/** Uniform random number generator */
- double uniform(double lower_bound, double upper_bound);
- double uniform(rngState *state, double lower_bound, double upper_bound);
-
+ double uniform(double lower_bound = 0.0, double upper_bound = 1.0);
+ double uniform(rngState *state, double lower_bound = 0.0, double upper_bound = 1.0);
+
int uniformInt(int lower_bound, int upper_bound);
int uniformInt(rngState *state, int lower_bound, int upper_bound);
+ bool uniformBool(void);
+ bool uniformBool(rngState *state);
+
/** Gaussian random number generator */
double gaussian(double mean, double stddev);
double gaussian(rngState *state, double mean, double stddev);
Modified: pkg/trunk/motion_planning/ompl/code/ompl/base/util/src/random.cpp
===================================================================
--- pkg/trunk/motion_planning/ompl/code/ompl/base/util/src/random.cpp 2008-08-21 01:05:27 UTC (rev 3394)
+++ pkg/trunk/motion_planning/ompl/code/ompl/base/util/src/random.cpp 2008-08-21 01:11:49 UTC (rev 3395)
@@ -42,9 +42,11 @@
#include "ompl/base/util/random.h"
+static ompl::random_utils::rngState rState;
+
void ompl::random_utils::random_init(void)
{
- srandom(time(NULL));
+ random_init(&rState);
}
void ompl::random_utils::random_init(rngState *state)
@@ -65,14 +67,11 @@
double ompl::random_utils::uniform(double lower_bound, double upper_bound)
{
- double r = (double)random();
- return (upper_bound - lower_bound)
- * r / ((double)(RAND_MAX) + 1.0)
- + lower_bound;
+ return uniform(&rState, lower_bound, upper_bound);
}
double ompl::random_utils::uniform(rngState *state, double lower_bound,
- double upper_bound)
+ double upper_bound)
{
return (upper_bound - lower_bound)
* (double)rand_r(&state->seed) / ((double)(RAND_MAX) + 1.0)
@@ -81,27 +80,28 @@
int ompl::random_utils::uniformInt(int lower_bound, int upper_bound)
{
- return (int)ompl::random_utils::uniform((double)lower_bound,
- (double)(upper_bound + 1));
+ return uniformInt(&rState, lower_bound, upper_bound);
}
int ompl::random_utils::uniformInt(rngState *state, int lower_bound, int upper_bound)
{
return (int)ompl::random_utils::uniform(state, (double)lower_bound,
- (double)(upper_bound + 1));
+ (double)(upper_bound + 1));
}
+bool ompl::random_utils::uniformBool(void)
+{
+ return uniformBool(&rState);
+}
+
+bool ompl::random_utils::uniformBool(rngState *state)
+{
+ return uniform(state, 0.0, 1.0) <= 0.5;
+}
+
double ompl::random_utils::gaussian(double mean, double stddev)
{
- double x1, x2, w;
- do
- {
- x1 = ompl::random_utils::uniform(-1, 1);
- x2 = ompl::random_utils::uniform(-1, 1);
- w = x1*x1 + x2*x2;
- } while (w >= 1.0 || w == 0.0);
- w = sqrt(-2.0 * log(w) / w);
- return x1 * w * stddev + mean;
+ return gaussian(&rState, mean, stddev);
}
double ompl::random_utils::gaussian(rngState *state, double mean, double stddev)
@@ -117,9 +117,9 @@
double x1, x2, w;
do
{
- x1 = ompl::random_utils::uniform(-1, 1);
- x2 = ompl::random_utils::uniform(-1, 1);
- w = x1*x1 + x2*x2;
+ x1 = uniform(state, -1.0, 1.0);
+ x2 = uniform(state, -1.0, 1.0);
+ w = x1 * x1 + x2 * x2;
} while (w >= 1.0 || w == 0.0);
w = sqrt(-2.0 * log(w) / w);
state->gaussian.valid = true;
@@ -129,23 +129,38 @@
}
double ompl::random_utils::bounded_gaussian(double mean, double stddev,
- double max_stddev)
+ double max_stddev)
{
- double sample, max_s = max_stddev * stddev;
- do
- {
- sample = ompl::random_utils::gaussian(mean, stddev);
- } while (fabs(sample - mean) > max_s);
- return sample;
+ return bounded_gaussian(&rState, mean, stddev, max_stddev);
}
double ompl::random_utils::bounded_gaussian(rngState *state, double mean,
- double stddev, double max_stddev)
+ double stddev, double max_stddev)
{
double sample, max_s = max_stddev * stddev;
do
{
- sample = ompl::random_utils::gaussian(state, mean, stddev);
+ sample = gaussian(state, mean, stddev);
} while (fabs(sample - mean) > max_s);
return sample;
}
+
+// From: "Uniform Random Rotations", Ken Shoemake, Graphics Gems III,
+// pg. 124-132
+void ompl::random_utils::quaternion(double value[4])
+{
+ quaternion(&rState, value);
+}
+
+void ompl::random_utils::quaternion(rngState* state, double value[4])
+{
+ double x0 = uniform(state);
+ double r1 = sqrt(1.0 - x0), r2 = sqrt(x0);
+ double t1 = 2.0 * M_PI * uniform(state), t2 = 2.0 * M_PI * uniform(state);
+ double c1 = cos(t1), s1 = sin(t1);
+ double c2 = cos(t2), s2 = sin(t2);
+ value[0] = s1 * r1;
+ value[1] = c1 * r1;
+ value[2] = s2 * r2;
+ value[3] = c2 * r2;
+}
Modified: pkg/trunk/util/random_utils/src/test.cpp
===================================================================
--- pkg/trunk/util/random_utils/src/test.cpp 2008-08-21 01:05:27 UTC (rev 3394)
+++ pkg/trunk/util/random_utils/src/test.cpp 2008-08-21 01:11:49 UTC (rev 3395)
@@ -1,3 +1,39 @@
+/*********************************************************************
+* Software License Agreement (BSD License)
+*
+* Copyright (c) 2008, Willow Garage, Inc.
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions
+* are met:
+*
+* * Redistributions of source code must retain the above copyright
+* notice, this list of conditions and the following disclaimer.
+* * Redistributions in binary form must reproduce the above
+* copyright notice, this list of conditions and the following
+* disclaimer in the documentation and/or other materials provided
+* with the distribution.
+* * Neither the name of the Willow Garage nor the names of its
+* contributors may be used to endorse or promote products derived
+* from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+* POSSIBILITY OF SUCH DAMAGE.
+*********************************************************************/
+
+/** \Author Ioan Sucan */
+
#include <cstdio>
#include <cstdlib>
#include <cmath>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|