Fawkes API  Fawkes Development Version
field.cpp
1 /***************************************************************************
2  * field.cpp - Encapsulates a soccer field
3  *
4  * Created: Tue Sep 23 12:00:00 2008
5  * Copyright 2008 Christof Rath <christof.rath@gmail.com>
6  *
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include <core/exceptions/software.h>
23 #include <fvutils/draw/field.h>
24 
25 #include <cmath>
26 #include <cstring>
27 #include <stdio.h>
28 
29 using namespace fawkes;
30 
31 namespace firevision {
32 
33 /** @class Field <fvutils/draw/field.h>
34  * This class is used to describe a soccer field.
35  *
36  * @fn const FieldLines& Field::get_lines() const
37  * Field lines getter
38  * @return the field lines object
39  *
40  * @author Christof Rath
41  */
42 
43 /** Dummy constructor */
44 Field::Field(FieldLines *lines, bool manage_lines_memory)
45 {
46  lines_ = lines;
47  manage_lines_memory_ = manage_lines_memory;
48 }
49 
50 /**
51  * Destructor.
52  */
53 Field::~Field()
54 {
55  if (manage_lines_memory_)
56  delete lines_;
57 }
58 
59 /**
60  * Field length getter
61  * @return the length of the soccer field
62  */
63 float
64 Field::get_field_length() const
65 {
66  return lines_->get_field_length();
67 }
68 
69 /**
70  * Field width getter
71  * @return the width of the soccer field
72  */
73 float
74 Field::get_field_width() const
75 {
76  return lines_->get_field_width();
77 }
78 
79 /**
80  * Prints the information to the console
81  * @param in_mm if true all units that have been [m] are now [mm]
82  */
83 void
84 Field::print(bool in_mm) const
85 {
86  printf("Field lines (start-x -y end-x -y):\n==================================\n");
87  for (FieldLines::const_iterator it = lines_->begin(); it != lines_->end(); ++it) {
88  if (in_mm)
89  printf("%d %d %d %d\n",
90  static_cast<int>(it->start.x * 1000),
91  static_cast<int>(it->start.y * 1000),
92  static_cast<int>(it->end.x * 1000),
93  static_cast<int>(it->end.y * 1000));
94  else
95  printf("%0.03f %0.03f %0.03f %0.03f\n", it->start.x, it->start.y, it->end.x, it->end.y);
96  }
97  printf("\n");
98 
99  printf("Field circles (center-x/y radius start/end "
100  "angle):\n=============================================\n");
101  for (field_circles_t::const_iterator it = lines_->get_circles().begin();
102  it != lines_->get_circles().end();
103  ++it) {
104  if (in_mm)
105  printf("%d %d %d %0.03f %0.03f\n",
106  static_cast<int>(it->center.x * 1000),
107  static_cast<int>(it->center.y * 1000),
108  static_cast<int>(it->radius * 1000),
109  it->start_phi,
110  it->end_phi);
111  else
112  printf("%0.03f %0.03f %0.03f %0.03f %0.03f\n",
113  it->center.x,
114  it->center.y,
115  it->radius,
116  it->start_phi,
117  it->end_phi);
118  }
119  printf("\n\n");
120 }
121 
122 /**
123  * Returns the corresponding Field object
124  *
125  * @param field_name the name of the field
126  * @param field_length the area of interest around the field
127  * @param field_width the area of interest around the field
128  * @return the Field object pointer
129  */
130 Field *
131 Field::field_for_name(std::string field_name, float field_length, float field_width)
132 {
133  if (field_name == "Field6x4")
134  return new Field(new FieldLines6x4(field_length, field_width));
135  else if (field_name == "FieldCityTower")
136  return new Field(new FieldLinesCityTower(field_length, field_width));
137  else if (field_name == "FieldCityTowerSeminar")
138  return new Field(new FieldLinesCityTowerSeminar(field_length, field_width));
139  else
141  "Unknown field name! Please set field_name to a valid value (see field.h)");
142 }
143 
144 } // end namespace firevision
fawkes::IllegalArgumentException
Definition: software.h:85
firevision::FieldLinesCityTower
Definition: field_lines.h:92
firevision::FieldLines6x4
Definition: field_lines.h:82
fawkes
firevision::Field
Definition: field.h:41
firevision::FieldLinesCityTowerSeminar
Definition: field_lines.h:102