2 // Copyright (C) 2005 Philipp <balinor@pnxs.de>
3 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "object/path.hpp"
24 #include "lisp/list_iterator.hpp"
25 #include "util/log.hpp"
38 Path::read(const Reader& reader)
40 lisp::ListIterator iter(&reader);
44 if(iter.item() == "mode") {
45 std::string mode_string;
46 if(!iter.value()->get(mode_string))
47 throw std::runtime_error("Pathmode not a string");
49 if(mode_string == "oneshot")
51 else if(mode_string == "pingpong")
53 else if(mode_string == "circular")
56 std::ostringstream msg;
57 msg << "Unknown pathmode '" << mode_string << "' found";
58 throw std::runtime_error(msg.str());
63 if(iter.item() != "node") {
64 log_warning << "unknown token '" << iter.item() << "' in Path nodes list. Ignored." << std::endl;
67 const lisp::Lisp* node_lisp = iter.lisp();
69 // each new node will inherit all values from the last one
72 if( (!node_lisp->get("x", node.position.x) ||
73 !node_lisp->get("y", node.position.y)))
74 throw std::runtime_error("Path node without x and y coordinate specified");
75 node_lisp->get("time", node.time);
78 throw std::runtime_error("Path node with non-positive time");
80 nodes.push_back(node);
84 throw std::runtime_error("Path with zero nodes");
88 Path::get_base() const
93 return nodes[0].position;
97 Path::get_nearest_node_no(Vector reference_point) const
99 int nearest_node_id = -1;
100 float nearest_node_dist = 0;
102 for (std::vector<Node>::const_iterator i = nodes.begin(); i != nodes.end(); i++, id++) {
103 float dist = (i->position - reference_point).norm();
104 if ((nearest_node_id == -1) || (dist < nearest_node_dist)) {
105 nearest_node_id = id;
106 nearest_node_dist = dist;
109 return nearest_node_id;
113 Path::get_farthest_node_no(Vector reference_point) const
115 int farthest_node_id = -1;
116 float farthest_node_dist = 0;
118 for (std::vector<Node>::const_iterator i = nodes.begin(); i != nodes.end(); i++, id++) {
119 float dist = (i->position - reference_point).norm();
120 if ((farthest_node_id == -1) || (dist > farthest_node_dist)) {
121 farthest_node_id = id;
122 farthest_node_dist = dist;
125 return farthest_node_id;