Program Listing for File shapes.h

Return to documentation for file (/tmp/ws/src/geometric_shapes/include/geometric_shapes/shapes.h)

// Copyright 2008 Willow Garage, Inc.
//
// 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, Inc. 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 HOLDER 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 */

#ifndef GEOMETRIC_SHAPES_SHAPES_
#define GEOMETRIC_SHAPES_SHAPES_

#include <cstdlib>
#include <vector>
#include <iostream>
#include <memory>
#include <string>

namespace octomap
{
class OcTree;
}

namespace shapes
{
enum ShapeType
{
  UNKNOWN_SHAPE,
  SPHERE,
  CYLINDER,
  CONE,
  BOX,
  PLANE,
  MESH,
  OCTREE
};

/* convert above enum to printable */
std::ostream& operator<<(std::ostream& ss, ShapeType type);

class Shape
{
public:
  Shape();
  virtual ~Shape();

  virtual Shape* clone() const = 0;

  virtual void print(std::ostream& out = std::cout) const;

  void scale(double scale);

  void padd(double padding);

  virtual void scaleAndPadd(double scale, double padd) = 0;

  virtual bool isFixed() const;

  ShapeType type;
};

class Sphere : public Shape
{
public:
  Sphere();

  Sphere(double r);

  static const std::string STRING_NAME;

  void scaleAndPadd(double scale, double padd) override;
  Sphere* clone() const override;
  void print(std::ostream& out = std::cout) const override;

  double radius;
};

class Cylinder : public Shape
{
public:
  using Shape::padd;
  using Shape::scale;

  Cylinder();

  Cylinder(double r, double l);

  static const std::string STRING_NAME;

  void scale(double scaleRadius, double scaleLength);

  void padd(double paddRadius, double paddLength);

  void scaleAndPadd(double scaleRadius, double scaleLength, double paddRadius, double paddLength);

  void scaleAndPadd(double scale, double padd) override;
  Cylinder* clone() const override;
  void print(std::ostream& out = std::cout) const override;

  double length;

  double radius;
};

class Cone : public Shape
{
public:
  using Shape::padd;
  using Shape::scale;

  Cone();
  Cone(double r, double l);

  static const std::string STRING_NAME;

  void scale(double scaleRadius, double scaleLength);

  void padd(double paddRadius, double paddLength);

  void scaleAndPadd(double scaleRadius, double scaleLength, double paddRadius, double paddLength);

  void scaleAndPadd(double scale, double padd) override;
  Cone* clone() const override;
  void print(std::ostream& out = std::cout) const override;

  double length;

  double radius;
};

class Box : public Shape
{
public:
  using Shape::padd;
  using Shape::scale;

  Box();
  Box(double x, double y, double z);

  static const std::string STRING_NAME;

  void scale(double scaleX, double scaleY, double scaleZ);

  void padd(double paddX, double paddY, double paddZ);

  void scaleAndPadd(double scaleX, double scaleY, double scaleZ, double paddX, double paddY, double paddZ);

  void scaleAndPadd(double scale, double padd) override;
  Box* clone() const override;
  void print(std::ostream& out = std::cout) const override;

  double size[3];
};

class Mesh : public Shape
{
public:
  using Shape::padd;
  using Shape::scale;

  Mesh();
  Mesh(unsigned int v_count, unsigned int t_count);
  ~Mesh() override;

  static const std::string STRING_NAME;

  void scale(double scaleX, double scaleY, double scaleZ);

  void padd(double paddX, double paddY, double paddZ);

  void scaleAndPadd(double scaleX, double scaleY, double scaleZ, double paddX, double paddY, double paddZ);

  void scaleAndPadd(double scale, double padd) override;
  Mesh* clone() const override;
  void print(std::ostream& out = std::cout) const override;

  void computeTriangleNormals();

  void computeVertexNormals();

  void mergeVertices(double threshold);

  unsigned int vertex_count;

  double* vertices;

  unsigned int triangle_count;

  unsigned int* triangles;

  double* triangle_normals;

  double* vertex_normals;
};

class Plane : public Shape
{
public:
  Plane();
  Plane(double pa, double pb, double pc, double pd);

  static const std::string STRING_NAME;

  Plane* clone() const override;
  void print(std::ostream& out = std::cout) const override;
  void scaleAndPadd(double scale, double padd) override;
  bool isFixed() const override;

  double a, b, c, d;
};

class OcTree : public Shape
{
public:
  OcTree();
  OcTree(const std::shared_ptr<const octomap::OcTree>& t);

  static const std::string STRING_NAME;

  OcTree* clone() const override;
  void print(std::ostream& out = std::cout) const override;
  void scaleAndPadd(double scale, double padd) override;
  bool isFixed() const override;

  std::shared_ptr<const octomap::OcTree> octree;
};

typedef std::shared_ptr<Shape> ShapePtr;

typedef std::shared_ptr<const Shape> ShapeConstPtr;
}  // namespace shapes

#endif