sgdk::Compass<> Class Template Reference
[Compass Module]

#include <swiss_gd_knife/math/geom/compass.hpp>

List of all members.


Detailed Description

template<>
class sgdk::Compass<>

The example below illustrates the usage of this class.

// std::ostream and std::cout
#include <iostream>
// sgdk::ZeroDirection, sgdk::PositiveRotation, and sgdk::Compass
#include <swiss_gd_knife/math/geom/compass.hpp>

typedef sgdk::Compass<>
        SampleCompass;

/*
 * Outputs the compass.getDirection() call's parameters and its result.
 */
void
    coutDirection(
        const SampleCompass& compass
      , const double dx
      , const double dy
    )
{
    std::cout << "<" << dx << ", " << dy << "> = ";
    std::cout << compass.getDirection(dx, dy) << std::endl;
}

/*
 * Runs the specified example's invocations of the getDirection()
 * function of the specified Compass with its current zero-direction,
 * positive-rotation, and direction-count settings.
 */
void sample(const SampleCompass& compass, const unsigned int trial)
{
    std::cout << "Number of directions = ";
    std::cout << compass.getDirectionCount() << std::endl;

    switch (trial)
    {
        case 1:
            coutDirection(compass, 0, 1);
            coutDirection(compass, 1, 0);
            coutDirection(compass, 0, -1);
            coutDirection(compass, -1, 0);
            coutDirection(compass, 1, 2);
            coutDirection(compass, 2, 1);
            coutDirection(compass, 2, -1);
            coutDirection(compass, 1, -2);
            coutDirection(compass, -1, -2);
            coutDirection(compass, -2, -1);
            coutDirection(compass, -2, 1);
            coutDirection(compass, -1, 2);
            break;

        case 2:
            coutDirection(compass, 1, 5);
            coutDirection(compass, 3, 4);
            coutDirection(compass, 5, 1);
            coutDirection(compass, 5, -1);
            coutDirection(compass, 3, -4);
            coutDirection(compass, 1, -5);
            coutDirection(compass, -1, -5);
            coutDirection(compass, -3, -4);
            coutDirection(compass, -5, -1);
            coutDirection(compass, -5, 1);
            coutDirection(compass, -3, 4);
            coutDirection(compass, -1, 5);
            break;

        case 3:
            coutDirection(compass, 0, 1);
            coutDirection(compass, 1, 1);
            coutDirection(compass, 1, 0);
            coutDirection(compass, 1, -1);
            coutDirection(compass, 0, -1);
            coutDirection(compass, -1, -1);
            coutDirection(compass, -1, 0);
            coutDirection(compass, -1, 1);
            break;

        default:
            coutDirection(compass, 0, 2);
            coutDirection(compass, 1, 2);
            coutDirection(compass, 2, 2);
            coutDirection(compass, 2, 1);
            coutDirection(compass, 2, 0);
            coutDirection(compass, 2, -1);
            coutDirection(compass, 2, -2);
            coutDirection(compass, 1, -2);
            coutDirection(compass, 0, -2);
            coutDirection(compass, -1, -2);
            coutDirection(compass, -2, -2);
            coutDirection(compass, -2, -1);
            coutDirection(compass, -2, 0);
            coutDirection(compass, -2, 1);
            coutDirection(compass, -2, 2);
            coutDirection(compass, -1, 2);
            break;
    }

    std::cout << std::endl;
}

/*
 * Runs the examples on the specified Compass with its current zero-direction
 * and positive-rotation settings.  Groups the output directions by direction
 * count.
 */
void sample(SampleCompass& compass)
{
    // Basic cases.
    compass.setDirectionCount(4);
    sample(compass, 1);
    compass.setDirectionCount(5);
    sample(compass, 2);
    compass.setDirectionCount(8);
    sample(compass, 3);
    compass.setDirectionCount(10);
    sample(compass, 2);
    compass.setDirectionCount(16);
    sample(compass, 0);

    // Degenerate cases.
    compass.setDirectionCount(1);
    sample(compass, 1);
    compass.setDirectionCount(2);
    sample(compass, 1);
    compass.setDirectionCount(3);
    sample(compass, 0);

    // See how a "real-world" compass works.
    compass.setDirectionCount(360);
    sample(compass, 0);

    // Unreal Tournament angles.
    compass.setDirectionCount(65536);
    sample(compass, 0);
}

/*
 * Runs the examples.
 */
int main()
{
    std::cout << "ZeroDirection = Y, PositiveRotation = CW" << std::endl;
    SampleCompass
        compass(
            4
          , sgdk::ZeroDirection::POSITIVE_Y_AXIS
          , sgdk::PositiveRotation::CLOCKWISE
        );
    sample(compass);

    std::cout << "ZeroDirection = Y, PositiveRotation = CCW" << std::endl;
    compass.setPositiveRotation(sgdk::PositiveRotation::COUNTERCLOCKWISE);
    sample(compass);

    std::cout << "ZeroDirection = -Y, PositiveRotation = CW" << std::endl;
    compass.setZeroDirection(sgdk::ZeroDirection::NEGATIVE_Y_AXIS);
    compass.setPositiveRotation(sgdk::PositiveRotation::CLOCKWISE);
    sample(compass);

    std::cout << "ZeroDirection = -Y, PositiveRotation = CCW" << std::endl;
    compass.setPositiveRotation(sgdk::PositiveRotation::COUNTERCLOCKWISE);
    sample(compass);

    std::cout << "ZeroDirection = X, PositiveRotation = CW" << std::endl;
    compass.setZeroDirection(sgdk::ZeroDirection::POSITIVE_X_AXIS);
    compass.setPositiveRotation(sgdk::PositiveRotation::CLOCKWISE);
    sample(compass);

    std::cout << "ZeroDirection = X, PositiveRotation = CCW" << std::endl;
    compass.setPositiveRotation(sgdk::PositiveRotation::COUNTERCLOCKWISE);
    sample(compass);

    std::cout << "ZeroDirection = -X, PositiveRotation = CW" << std::endl;
    compass.setZeroDirection(sgdk::ZeroDirection::NEGATIVE_X_AXIS);
    compass.setPositiveRotation(sgdk::PositiveRotation::CLOCKWISE);
    sample(compass);

    std::cout << "ZeroDirection = -X, PositiveRotation = CCW" << std::endl;
    compass.setPositiveRotation(sgdk::PositiveRotation::COUNTERCLOCKWISE);
    sample(compass);

    return 0;
}


Template parameters

Parameter Description Default
Direction The return type of getDirection(). unsigned int
DirectionChange The return type of getDirectionChange(). int
PiConstant The type of the function object returning the constant value PI. Pi
TangentFunction The type of the function object returning the tangent of an angle. Tangent


Model of:


Type requirements



Public Types

typedef implementation_defined RealNumber

Public Member Functions

 Compass (const Direction direction_count=4, const ZeroDirection &zero_direction=ZeroDirection::POSITIVE_Y_AXIS, const PositiveRotation &positive_rotation=PositiveRotation::CLOCKWISE, const RealNumber zero_tolerance=0.00001f)
 The default constructor.
 Compass (const Compass &copy)
 The copy constructor.
Compassoperator= (const Compass &copy)
 The assignment operator.
Direction getDirectionCount () const
Direction getDirection (const RealNumber dx, const RealNumber dy) const
DirectionChange getDirectionChange (const RealNumber dx1, const RealNumber dy1, const RealNumber dx2, const RealNumber dy2) const
void setDirectionCount (const Direction direction_count)
void setZeroDirection (const ZeroDirection &zero_direction)
void setPositiveRotation (const PositiveRotation &positive_rotation)


Member Typedef Documentation

typedef implementation_defined sgdk::Compass<>::RealNumber
 

The type of the arguments for getDirection().


Constructor & Destructor Documentation

sgdk::Compass<>::Compass const Direction  direction_count = 4,
const ZeroDirection zero_direction = ZeroDirection::POSITIVE_Y_AXIS,
const PositiveRotation positive_rotation = PositiveRotation::CLOCKWISE,
const RealNumber  zero_tolerance = 0.00001f
[inline, explicit]
 

Constructs a new Compass.

Parameters:
direction_count the number of possible rotation outputs. The default value is 4.
zero_direction the "zero-degree direction" identified by this Compass. The default is ZeroDirection::POSITIVE_Y_AXIS.
positive_rotation the rotational direction that this Compass identifies as upward from zero_direction. The default is PositiveRotation::CLOCKWISE.
zero_tolerance the threshold used to internally check for special divide-by-zero cases. The default value is 0.00001f.
See also:
setDirectionCount(), setZeroDirection(), setPositiveRotation()

sgdk::Compass<>::Compass const Compass<> &  copy  )  [inline]
 

Constructs a new Compass that will be internally equivalent to the copy at this point in time.

Parameters:
copy the Compass being copied.


Member Function Documentation

Compass& sgdk::Compass<>::operator= const Compass<> &  copy  )  [inline]
 

Sets this Compass to be internally equivalent to the copy.

Parameters:
copy the Compass being copied.
Returns:
a reference to this Compass.

Direction sgdk::Compass<>::getDirectionCount  )  const [inline]
 

Returns the number of possible rotation outputs for this Compass.

Returns:
the number of possible values that getDirection() can return.

Direction sgdk::Compass<>::getDirection const RealNumber  dx,
const RealNumber  dy
const
 

The direction-finding function. Returns zero if exactly one of the following conditions holds:

Returns getDirectionCount() / 2 if getDirectionCount() returns an even number and exactly one of the following conditions holds:

Returns a value between zero and getDirectionCount() / 2 if exactly one of the following conditions holds:

Returns a value between getDirectionCount() / 2 and getDirectionCount() if exactly one of the following conditions holds:

Parameters:
dx the change in x-coordinate value from the point of origin of this Compass.
dy the change in y-coordinate value from the point of origin of this Compass.
Returns:
the closest discrete direction.
Invariant:
0 <= getDirection(dx,dy)

getDirection(dx,dy) < getDirectionCount()

See also:
getDirectionChange()

DirectionChange sgdk::Compass<>::getDirectionChange const RealNumber  dx1,
const RealNumber  dy1,
const RealNumber  dx2,
const RealNumber  dy2
const
 

Returns the change in direction between getDirection(dx1,dy1) and getDirection(dx2,dy2) as defined by this Compass.

Parameters:
dx1 the starting change in x-coordinate value from the point of origin of this Compass.
dy1 the starting change in y-coordinate value from the point of origin of this Compass.
dx2 the ending change in x-coordinate value from the point of origin of this Compass.
dy2 the ending change in y-coordinate value from the point of origin of this Compass.
Returns:
a negative number if this Compass defines the resulting change as downward, a positive number if this Compass defines the change as upward, or zero if there is no perceptible change. A change to the opposite direction is considered upward; if getDirectionCount() returns an even number, then this particular getDirectionChange() call will return half that value.

void sgdk::Compass<>::setDirectionCount const Direction  direction_count  )  [inline]
 

Sets the number of possible rotation outputs.

Parameters:
direction_count the new number of possible rotation outputs.
See also:
Compass()

void sgdk::Compass<>::setZeroDirection const ZeroDirection zero_direction  )  [inline]
 

Sets the zero-degree direction for this Compass.

Parameters:
zero_direction the zero-degree direction to be identified by this Compass.
See also:
Compass()

void sgdk::Compass<>::setPositiveRotation const PositiveRotation positive_rotation  )  [inline]
 

Sets the positive rotational direction for this Compass.

Parameters:
positive_rotation the rotational direction that this Compass will identify as upward from the zero-degree direction.
See also:
Compass()


Swiss GD Knife is hosted by SourceForge.net.