13 using namespace se2ez;
22 unsigned int height,
Mode mode)
26 , limitA_({robot->getLowerLimits()[
indexA_], robot->getUpperLimits()[
indexA_]})
33 ,
image_(std::make_shared<ompl::PPM>())
34 ,
state_(std::make_shared<State>(robot))
35 ,
temp_(std::make_shared<State>(robot))
44 unsigned int width,
unsigned int height,
Mode mode)
45 :
CSpaceGrid(robot, robot->getFrameDataConst(jointA)->index, robot->getFrameDataConst(jointB)->index,
57 for (
unsigned int c = 0; c <
cores_; ++c)
60 auto cm = std::make_shared<Box2DCollisionManager>(
robot_);
61 auto state = std::make_shared<State>(
robot_);
62 double &jA = state->data[
indexA_];
63 double &jB = state->data[
indexB_];
103 for (
unsigned int i = chunk.
x; i < chunk.
x + chunk.
w; ++i)
107 for (
unsigned int j = chunk.
y; j < chunk.
y + chunk.
h; ++j)
112 auto &pixel =
image_->getPixel(i, j);
114 bool collision =
false;
115 unsigned char rc = 0, gc = 0, bc = 0;
122 auto sd = cm->distance(state);
123 double d = sd.distance;
158 auto sd = cm->distance(state);
159 if (sd.distance >= 0)
174 collision = cm->collide(state);
175 rc = gc = bc = (collision) ? 0 : 255;
179 pixel = ompl::PPM::Color{rc, gc, bc};
182 callback(pixel, state, collision);
202 for (
unsigned int c = 0; c <
cores_; ++c)
219 unsigned int remainX = std::min<unsigned int>({
xChunk_, width_ - x});
220 unsigned int remainY = std::min<unsigned int>({
yChunk_, height_ - y});
222 chunks_.emplace(x, y, remainX, remainY);
239 for (
unsigned int i = 0; i <
cores_; ++i)
280 return image_->getPixel(coord.x, coord.y);
288 return {.x = (
unsigned int)x, .y = (
unsigned int)y};
296 return {.x = x, .y = y};
304 auto scratch = std::make_shared<State>(
robot_);
308 for (
const auto &line : lines)
310 unsigned int ax = line.a.x, ay = line.a.y, bx = line.b.x, by = line.b.y;
317 if ((bx - ax) <=
width_ / 2)
327 unsigned int ma = ay + ((int)by - (
int)ay) * at / tt;
344 for (
const auto &line : lines)
346 unsigned int ax = line.a.x, ay = line.a.y, bx = line.b.x, by = line.b.y;
363 unsigned int ma = ax + ((int)bx - (
int)ax) * at / tt;
388 bool working =
false;
389 for (
unsigned int i = 0; i <
cores_; ++i)
391 return working or not
chunks_.empty();
409 return temp_->data.norm();
417 return (
double)(maxChunks_ -
chunks_.size()) / maxChunks_;
422 auto framep =
robot_->getFrame(frame);
423 Eigen::Vector4d color{0, 0, 0, 0};
424 for (
const auto &geometry : framep->getGeometry())
425 color += geometry->color;
427 color /= (
double)framep->getGeometry().size();
Container class for image chunk information to worker threads.
void initialize()
Initialize all internal constructs.
const unsigned int indexB_
Joint index for Y-axis.
A shared pointer wrapper for se2ez::State.
Coordinates for the robot.
const double maxDepth_
Maximum value for signed distance color map.
unsigned int getWidth() const
Return width of grid.
StatePtr state_
State provided through compute().
bool isComputing() const
Checks if any computation is happening.
std::shared_ptr< ompl::PPM > image_
Image itself.
unsigned int x
x coordinate.
void cancel()
Cancels any remaining computation.
unsigned int maxChunks_
Maximum number of chunks in queue.
std::vector< bool > newState_
Has each thread updated to new base state for computation?
GridCoords getCoords(const StatePtr &state)
Get the pixel coordinate for a configuration.
Mode
Drawing mode for the grid.
T hardware_concurrency(T... args)
const unsigned int yChunk_
Size of Y chunk for threading.
const unsigned int xChunk_
Size of X chunk for threading.
void compute(bool block=true, const StatePtr &base=nullptr, const GridCallback &callback={})
Compute a PPM image of the configuration space of two joints, jointA and jointB.
const unsigned int width_
Width of image.
double progress() const
Returns a value in [0, 1] of the progress of the current computation.
double distanceToSlice(const StatePtr &state)
Returns the distance along the dimensions of the robot not captured by the current C-Space slice...
std::vector< bool > working_
Which threads are currently working?
StatePtr temp_
Temporary state.
std::condition_variable work_
Condition variable that there is work to do.
std::mutex statelock_
Mutex for state.
void waitToComplete()
Wait for all grid computation to complete.
void grayscale(double s, double &r, double &g, double &b)
Maps a scalar s in [0, 1] to greyscale.
void turbo(double s, double &r, double &g, double &b)
Maps a scalar s in [0, 1] to the Turbo colormap.
Eigen::Vector4d getFrameColor(const std::string &frame) const
Gets the average color of a frame.
const double minDepth_
Minimum value for signed distance color map.
CSpaceGrid(const RobotPtr &robot, unsigned int jointA, unsigned int jointB, unsigned int width=500, unsigned int height=500, Mode mode=COLLISION)
Constructor.
const RobotPtr robot_
Robot to use.
std::condition_variable complete_
Condition variable to notify when complete.
static const unsigned int cores_
Number of cores on the machine.
const unsigned int height_
Height of image.
std::queue< Chunk > chunks_
Queue of chunks.
unsigned int y
Starting y coordinate of chunk.
Use viridis color palette for signed distance.
Draw collisions as black pixels.
A shared pointer wrapper for se2ez::Robot.
ompl::PPM::Color & getPixel(const StatePtr &state)
Get the pixel coordinate for a configuration.
const Eigen::Vector2d limitB_
Joint limits for second joint (1-DoF)
const Mode mode_
Render mode.
unsigned int w
Width of chunk.
Signed distance, but penetrations are colored by geometry.
Color obstacles by geometry color.
const bool contB_
Is joint B continuous?
std::vector< GridLine > getLines(const StatePtr &a, const StatePtr &b)
Get the line segments between two points on the grid. Can have two due to wrapping.
double remap(double a1, double a2, double av, double b1, double b2)
Remap a value av in the interval a1, a2 to the interval b1, b2.
std::shared_ptr< ompl::PPM > getImage() const
Get the current computed PPM image.
GridCallback callback_
Callback function to use for extra drawing.
const unsigned int indexA_
Joint index for X-axis.
std::vector< std::thread * > threads_
Threads.
bool done_
Notify threads object is to be destroyed.
unsigned int x
Starting x coordinate of chunk.
unsigned int getHeight() const
Return height of grid.
unsigned int h
Height of chunk.
const Eigen::Vector2d limitA_
Joint limits for first joint (1-DoF)
void emplaceChunks()
Emplaces all images chunks into work queue.
const bool contA_
Is joint A continuous?
Helper class to compute and draw C-Space images from arbitrary robots.
T emplace_back(T... args)