TriloBot Simulator (C++, Qt4, Flex, Readline, Boost)
Detail souboru
Zdrojový kód
/*
* Copyright (C) 2008 Jakub Filak <xfilak01@stud.fit.vutbr.cz>
*
* This file is part of rob08
*
* rob08 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* rob08 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rob08. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GEOMETRY_H__
#define __GEOMETRY_H__
#include <iostream>
#include <vector>
/**
* 2D point in cartesian coord system
*/
struct Point
{
/// X coord
double x;
/// Y coord
double y;
Point( double cx=.0, double cy=.0 )
: x(cx),
y(cy)
{}
Point( const Point& rhs )
: x(rhs.x),
y(rhs.y)
{}
Point& operator=( const Point& rhs )
{
if( this != &rhs) {
x = rhs.x;
y = rhs.y;
}
return *this;
}
friend std::ostream& operator<<( std::ostream& output, const struct Point& rhs ) {
output << "[" << std::endl
<< rhs.x << std::endl
<< "," << std::endl
<< rhs.y << std::endl
<< "];";
return output;
}
friend std::istream& operator>>( std::istream& input, struct Point& rhs ) {
std::string tmp;
input >> tmp >> rhs.x >> tmp >> rhs.y >> tmp;
return input;
}
friend bool operator==( const Point& lhs, const Point& rhs ){
double round = 1e-2;
double dx = lhs.x - rhs.x;
double dy = lhs.y - rhs.y;
return (dx < round && dx > -round ) && ( dy < round && dy > -round );
}
std::ostream& Write( std::ostream& str ) const {
return str << "[" << this->x << "," << this->y << "]";
}
virtual ~Point() {}
/**
* Euclidean point distance
*/
double Distance( const Point& p, int sign=1 ) const;
};
typedef std::vector<Point> PointsVector;
/**
* 2D position defined by point in cartesian coord system and angle
*/
struct Position : public Point
{
Position( double cx=.0, double cy=.0, double cangle=.0 )
: Point( cx, cy ), angle( cangle )
{}
Position( const Point& p, double cangle=.0)
: Point( p ), angle( cangle )
{}
Position( const Position& p )
: Point( p )
{ *this = p; }
Position& operator=( const Position& p )
{
if( this != &p ) {
Point::operator=( p );
angle = p.angle;
}
return *this;
}
/*
Position& operator=( const Point& p )
{
if( this != &p ) {
Point::operator=( p );
angle = .0;
}
return *this;
}
*/
friend std::ostream& operator<<( std::ostream& output, const struct Position& rhs ) {
output << "{" << std::endl;
output << (Point&)rhs << std::endl;
output << rhs.angle << std::endl
<< "};";
return output;
}
friend std::istream& operator>>( std::istream& input, struct Position& rhs ) {
std::string tmp;
input >> tmp >> (Point&)rhs >> rhs.angle >> tmp;
return input;
}
virtual ~Position() {}
/// Orientation angle
double angle;
};
/**
* Class representing vector in space made from
* point on the begin and point on the end
*/
struct Vector
{
Vector( const struct Point& s, const struct Point& e )
: begin( s ), direction( .0 )
{
direction = (e.y-s.y)/(e.x-s.x);
}
Vector( const struct Point& s, double angle );
Vector( const struct Position& p );
Vector( const Vector& rhs )
: begin( rhs.begin ), direction( rhs.direction )
{}
Vector& operator=( const Vector& rhs ) {
if( this != &rhs ) {
begin = rhs.begin;
direction = rhs.direction;
}
return *this;
}
/**
* Computes intersect point
*/
bool Intersect( const Vector&, struct Point& ) const;
bool IsVertical() const;
bool IsHorizontal() const;
double Y0() const;
struct Point begin;
double direction;
};
std::ostream& operator<<( std::ostream&, const Vector& );
struct Circle
{
Circle( const Point& c, const double r )
: center( c ), radius( r )
{}
Circle( const Circle& rhs )
: center( rhs.center ), radius( rhs.radius )
{}
Circle& operator=( const Circle& rhs ) {
if( this != &rhs ) {
this->center = rhs.center;
this->radius = rhs.radius;
}
return *this;
}
PointsVector Intersects( const Circle& second ) const;
PointsVector Intersects( const Vector& second ) const;
double Distance( const Position&, const Point&, int sign=1 , int last=1) const;
Point center;
double radius;
};
std::ostream& operator<<(std::ostream& output, const Circle& c );
namespace GFuncs
{
double RoundToDigits( double number, unsigned digits=5 );
bool IsSameDouble( double lhs, double rhs );
double StandardizeAngle( double angle );
double DegToRad( double deg );
double RadToDeg( double rad );
double RadiusToAngle( double radius, double r );
Position Transform( const struct Position& base, const Position& p );
}
#endif