field.cpp

00001 /***************************************************************************
00002  *  field.cpp - Encapsulates a soccer field
00003  *
00004  *  Created:  Tue Sep 23 12:00:00 2008
00005  *  Copyright 2008 Christof Rath <christof.rath@gmail.com>
00006  *
00007  ****************************************************************************/
00008 
00009 /*  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU Library General Public License for more details.
00018  *
00019  *  Read the full text in the LICENSE.GPL file in the doc directory.
00020  */
00021 
00022 #include <fvutils/draw/field.h>
00023 
00024 #include <core/exceptions/software.h>
00025 
00026 #include <cmath>
00027 #include <cstring>
00028 #include <stdio.h>
00029 
00030 using namespace fawkes;
00031 
00032 namespace firevision {
00033 #if 0 /* just to make Emacs auto-indent happy */
00034 }
00035 #endif
00036 
00037 /** @class Field <fvutils/draw/field.h>
00038  * This class is used to describe a soccer field.
00039  *
00040  * @fn const FieldLines& Field::get_lines() const
00041  * Field lines getter
00042  * @return the field lines object
00043  *
00044  * @author Christof Rath
00045  */
00046 
00047 /** Dummy constructor */
00048 Field::Field(FieldLines *lines, bool manage_lines_memory)
00049 {
00050   __lines = lines;
00051   __manage_lines_memory = manage_lines_memory;
00052 }
00053 
00054 /**
00055  * Destructor.
00056  */
00057 Field::~Field()
00058 {
00059   if (__manage_lines_memory) delete __lines;
00060 }
00061 
00062 
00063 /**
00064  * Field length getter
00065  * @return the length of the soccer field
00066  */
00067 float
00068 Field::get_field_length()const
00069 {
00070   return __lines->get_field_length();
00071 }
00072 
00073 
00074 /**
00075  * Field width getter
00076  * @return the width of the soccer field
00077  */
00078 float
00079 Field::get_field_width() const
00080 {
00081   return __lines->get_field_width();
00082 }
00083 
00084 
00085 /**
00086  * Prints the information to the console
00087  * @param in_mm if true all units that have been [m] are now [mm]
00088  */
00089 void
00090 Field::print(bool in_mm) const
00091 {
00092   printf("Field lines (start-x -y end-x -y):\n==================================\n");
00093   for (FieldLines::const_iterator it = __lines->begin(); it != __lines->end(); ++it) {
00094     if (in_mm) printf("%d %d %d %d\n", static_cast<int>(it->start.x * 1000), static_cast<int>(it->start.y * 1000), static_cast<int>(it->end.x * 1000), static_cast<int>(it->end.y * 1000));
00095     else       printf("%0.03f %0.03f %0.03f %0.03f\n", it->start.x, it->start.y, it->end.x, it->end.y);
00096   }
00097   printf("\n");
00098 
00099   printf("Field circles (center-x/y radius start/end angle):\n=============================================\n");
00100   for (field_circles_t::const_iterator it = __lines->get_circles().begin(); it != __lines->get_circles().end(); ++it) {
00101     if (in_mm) printf("%d %d %d %0.03f %0.03f\n", static_cast<int>(it->center.x * 1000), static_cast<int>(it->center.y * 1000), static_cast<int>(it->radius * 1000), it->start_phi, it->end_phi);
00102     else       printf("%0.03f %0.03f %0.03f %0.03f %0.03f\n", it->center.x, it->center.y, it->radius, it->start_phi, it->end_phi);
00103   }
00104   printf("\n\n");
00105 }
00106 
00107 /**
00108  * Returns the corresponding Field object
00109  *
00110  * @param field_name the name of the field
00111  * @param field_length the area of interest around the field
00112  * @param field_width the area of interest around the field
00113  * @return the Field object pointer
00114  */
00115 Field*
00116 Field::field_for_name(std::string field_name, float field_length, float field_width)
00117 {
00118   if (field_name == "Field6x4") return new Field(new FieldLines6x4(field_length, field_width));
00119   else if (field_name == "FieldCityTower") return new Field(new FieldLinesCityTower(field_length, field_width));
00120   else if (field_name == "FieldCityTowerSeminar") return new Field(new FieldLinesCityTowerSeminar(field_length, field_width));
00121   else throw fawkes::IllegalArgumentException("Unknown field name! Please set field_name to a valid value (see field.h)");
00122 }
00123 
00124 } // end namespace firevision

Generated on Tue Feb 22 13:32:15 2011 for Fawkes API by  doxygen 1.4.7