/**********************************************************************
 * $Id: OffsetCurveBuilder.h 3301 2011-04-27 09:42:31Z strk $
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.refractions.net
 *
 * Copyright (C) 2009-2011  Sandro Santilli <strk@keybit.net>
 * Copyright (C) 2006-2007 Refractions Research Inc.
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Public Licence as published
 * by the Free Software Foundation. 
 * See the COPYING file for more information.
 *
 **********************************************************************
 *
 * Last port: operation/buffer/OffsetCurveBuilder.java r378 (JTS-1.12)
 *
 **********************************************************************/

#ifndef GEOS_OP_BUFFER_OFFSETCURVEBUILDER_H
#define GEOS_OP_BUFFER_OFFSETCURVEBUILDER_H

#include <geos/export.h>

#include <geos/operation/buffer/BufferParameters.h> // for composition
#include <geos/operation/buffer/OffsetSegmentGenerator.h> 

#include <vector>
#include <memory> // for auto_ptr

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
#endif

// Forward declarations
namespace geos {
	namespace geom {
		class CoordinateSequence;
		class PrecisionModel;
	}
}

namespace geos {
namespace operation { // geos.operation
namespace buffer { // geos.operation.buffer

/**
 * \class OffsetCurveBuilder opBuffer.h geos/opBuffer.h
 *
 * \brief
 * Computes the raw offset curve for a
 * single Geometry component (ring, line or point).
 *
 * A raw offset curve line is not noded -
 * it may contain self-intersections (and usually will).
 * The final buffer polygon is computed by forming a topological graph
 * of all the noded raw curves and tracing outside contours.
 * The points in the raw curve are rounded to a given geom::PrecisionModel.
 *
 */
class GEOS_DLL OffsetCurveBuilder {
public:

	/*
	 * @param nBufParams buffer parameters, this object will
	 *                   keep a reference to the passed parameters
	 *                   so caller must make sure the object is
	 *                   kept alive for the whole lifetime of
	 *                   the buffer builder.
	 */
	OffsetCurveBuilder(const geom::PrecisionModel *newPrecisionModel,
			const BufferParameters& nBufParams)
		:
		distance(0.0),
		precisionModel(newPrecisionModel),
		bufParams(nBufParams)
  {}

  /**
   * Gets the buffer parameters being used to generate the curve.
   *
   * @return the buffer parameters being used
   */
  const BufferParameters& getBufferParameters() const
  {
    return bufParams;
  }

	/**
	 * This method handles single points as well as lines.
	 * Lines are assumed to <b>not</b> be closed (the function will not
	 * fail for closed lines, but will generate superfluous line caps).
	 *
	 * @param lineList the std::vector to which the newly created
	 *                 CoordinateSequences will be pushed_back.
	 *                 Caller is responsible to delete these new elements.
	 */
	void getLineCurve(const geom::CoordinateSequence* inputPts,
	                  double distance,
	                  std::vector<geom::CoordinateSequence*>& lineList);

	/**
	 * This method handles single points as well as lines.
	 *
	 * Lines are assumed to <b>not</b> be closed (the function will not
	 * fail for closed lines, but will generate superfluous line caps).
	 *
	 * @param lineList the std::vector to which newly created
	 *                 CoordinateSequences will be pushed_back.
	 *                 Caller will be responsible to delete them.
	 * @param leftSide indicates that the left side buffer will be
	 *                 obtained/skipped
	 * @param rightSide indicates that the right side buffer will
	 *                  be obtained/skipped
	 *
	 * NOTE: this is a GEOS extension
	 */
	void getSingleSidedLineCurve(const geom::CoordinateSequence* inputPts, 
	     double distance, std::vector<geom::CoordinateSequence*>& lineList,
	     bool leftSide, bool rightSide ) ;

	/**
	 * This method handles the degenerate cases of single points and lines,
	 * as well as rings.
	 *
	 * @param lineList the std::vector to which CoordinateSequences will
	 *                 be pushed_back
	 */
	void getRingCurve(const geom::CoordinateSequence *inputPts, int side,
	                  double distance,
	                  std::vector<geom::CoordinateSequence*>& lineList);


private:

	double distance;

	const geom::PrecisionModel* precisionModel;

	const BufferParameters& bufParams; 

	/**
	 * Use a value which results in a potential distance error which is
	 * significantly less than the error due to
	 * the quadrant segment discretization.
	 * For QS = 8 a value of 100 is reasonable.
	 * This should produce a maximum of 1% distance error.
	 */
	static const double SIMPLIFY_FACTOR; // 100.0;

	/** \brief
	 * Computes the distance tolerance to use during input
	 * line simplification.
	 *
	 * @param distance the buffer distance
	 * @return the simplification tolerance
	 */
	double simplifyTolerance(double bufDistance);

	void computeLineBufferCurve(const geom::CoordinateSequence& inputPts,
                              OffsetSegmentGenerator& segGen);

  void computeSingleSidedBufferCurve(const geom::CoordinateSequence& inputPts,
                                     bool isRightSide,
                                     OffsetSegmentGenerator& segGen);

	void computeRingBufferCurve(const geom::CoordinateSequence& inputPts,
	                            int side, OffsetSegmentGenerator& segGen);

  std::auto_ptr<OffsetSegmentGenerator> getSegGen(double dist);

  void computePointCurve(const geom::Coordinate& pt,
                         OffsetSegmentGenerator& segGen);


    // Declare type as noncopyable
    OffsetCurveBuilder(const OffsetCurveBuilder& other);
    OffsetCurveBuilder& operator=(const OffsetCurveBuilder& rhs);
};

} // namespace geos::operation::buffer
} // namespace geos::operation
} // namespace geos

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#endif // ndef GEOS_OP_BUFFER_OFFSETCURVEBUILDER_H

