Shinya Kitaoka 810553
// #pragma once // could not use by INCLUDE_HPP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef RASTER_EDGE_ITERATOR_H
Toshihiro Shimizu 890ddd
#define RASTER_EDGE_ITERATOR_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace TRop {
Shinya Kitaoka 120a6e
namespace borders {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*********************************************************************************************************
Toshihiro Shimizu 890ddd
//    RasterEdgeIterator class
Toshihiro Shimizu 890ddd
//*********************************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Shinya Kitaoka 120a6e
  The RasterEdgeIterator class models a forward iterator traversing a border of
Shinya Kitaoka 120a6e
  a
Toshihiro Shimizu 890ddd
  raster image.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
template <typename pixelselector=""></typename>
Shinya Kitaoka 120a6e
class RasterEdgeIterator {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  typedef PixelSelector selector_type;
Shinya Kitaoka 120a6e
  typedef typename PixelSelector::pixel_type pixel_type;
Shinya Kitaoka 120a6e
  typedef typename PixelSelector::value_type value_type;
Shinya Kitaoka 120a6e
  typedef TRasterT<pixel_type> raster_type;</pixel_type>
Shinya Kitaoka 120a6e
  typedef TRasterPT<pixel_type> raster_typeP;</pixel_type>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  enum {
Shinya Kitaoka 120a6e
    STRAIGHT        = 0x0,
Shinya Kitaoka 120a6e
    LEFT            = 0x1,
Shinya Kitaoka 120a6e
    RIGHT           = 0x2,
Shinya Kitaoka 120a6e
    AMBIGUOUS       = 0x4,
Shinya Kitaoka 120a6e
    AMBIGUOUS_LEFT  = LEFT | AMBIGUOUS,
Shinya Kitaoka 120a6e
    AMBIGUOUS_RIGHT = RIGHT | AMBIGUOUS,
Shinya Kitaoka 120a6e
    UNKNOWN         = 0x8
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  raster_typeP m_ras;
Shinya Kitaoka 120a6e
  selector_type m_selector;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int m_lx_1, m_ly_1, m_wrap;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  value_type m_leftColor, m_rightColor, m_elbowColor;
Shinya Kitaoka 120a6e
  pixel_type *m_leftPix, *m_rightPix;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool m_rightSide;
Shinya Kitaoka 120a6e
  int m_turn;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPoint m_pos, m_dir;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RasterEdgeIterator(const raster_typeP &rin, const selector_type &selector,
Shinya Kitaoka 120a6e
                     const TPoint &pos, const TPoint &dir,
Shinya Kitaoka 120a6e
                     int adherence = RIGHT);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void setEdge(const TPoint &pos, const TPoint &dir);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const raster_typeP &raster() const { return m_ras; }
Shinya Kitaoka 120a6e
  const selector_type &selector() const { return m_selector; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const TPoint &pos() const { return m_pos; }
Shinya Kitaoka 120a6e
  const TPoint &dir() const { return m_dir; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const value_type &leftColor() const { return m_leftColor; }
Shinya Kitaoka 120a6e
  const value_type &rightColor() const { return m_rightColor; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const value_type &color() const {
Shinya Kitaoka 120a6e
    return m_rightSide ? m_rightColor : m_leftColor;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  const value_type &otherColor() const {
Shinya Kitaoka 120a6e
    return m_rightSide ? m_leftColor : m_rightColor;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  const value_type &elbowColor() const { return m_elbowColor; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  pixel_type *leftPix() const { return m_leftPix; }
Shinya Kitaoka 120a6e
  pixel_type *rightPix() const { return m_rightPix; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  pixel_type *pix() const { return m_rightSide ? m_rightPix : m_leftPix; }
Shinya Kitaoka 120a6e
  pixel_type *otherPix() const { return m_rightSide ? m_leftPix : m_rightPix; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int turn() const { return m_turn; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void setAdherence(int side) { m_rightSide = (side == RIGHT); }
Shinya Kitaoka 120a6e
  int adherence() const { return m_rightSide ? RIGHT : LEFT; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  RasterEdgeIterator &operator++();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool operator==(const RasterEdgeIterator &it) const {
Shinya Kitaoka 120a6e
    return m_pos == it.m_pos && m_dir == it.m_dir;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool operator!=(const RasterEdgeIterator &it) const {
Shinya Kitaoka 120a6e
    return !operator==(it);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  void pixels(pixel_type *&pixLeft, pixel_type *&pixRight);
Shinya Kitaoka 120a6e
  void colors(value_type &leftColor, value_type &rightColor);
Shinya Kitaoka 120a6e
  void turn(const value_type &newLeftColor, const value_type &newRightColor);
Shinya Kitaoka 120a6e
  void turnLeft() {
Shinya Kitaoka 120a6e
    int temp = m_dir.x;
Shinya Kitaoka 120a6e
    m_dir.x  = -m_dir.y;
Shinya Kitaoka 120a6e
    m_dir.y  = temp;
Shinya Kitaoka 120a6e
    m_turn   = LEFT;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void turnRight() {
Shinya Kitaoka 120a6e
    int temp = m_dir.x;
Shinya Kitaoka 120a6e
    m_dir.x  = m_dir.y;
Shinya Kitaoka 120a6e
    m_dir.y  = -temp;
Shinya Kitaoka 120a6e
    m_turn   = RIGHT;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void turnAmbiguous(const value_type &newLeftColor,
Shinya Kitaoka 120a6e
                     const value_type &newRightColor);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
}  // namespace TRop::borders
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // RASTER_EDGE_ITERATOR_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=====================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef INCLUDE_HPP
Toshihiro Shimizu 890ddd
#include "raster_edge_iterator.hpp"
Shinya Kitaoka 120a6e
#endif  // INCLUDE_HPP