Mondschein Engine  0.3.0
beziercurve.cpp
1 /* Mondschein Engine is a fast, powerful, and easy-to-use 3D realtime rendering engine.
2  *
3  * Copyright (C) 2009-2013 by Andreas Amereller
4  * E-Mail: andreas.amereller.dev@googlemail.com
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include "core/beziercurve.h"
22 
23 using namespace mondschein;
24 using namespace math;
25 
26 Beziercurve::Beziercurve() : points()
27 {
28  return;
29 }
30 
31 Beziercurve::Beziercurve(const std::vector<Eigen::Vector4d> &_p) : points(_p)
32 {
33  return;
34 }
35 
36 Beziercurve::Beziercurve(Beziercurve_c _c) : points(_c->points)
37 {
38  return;
39 }
40 
41 Beziercurve::~Beziercurve()
42 {
43  return;
44 }
45 
46 Beziercurve &Beziercurve::operator=(Beziercurve_c _c)
47 {
48  points=_c->points;
49  return *this;
50 }
51 
52 Eigen::Vector3d Beziercurve::operator()(float64 _t) const
53 {
54  try
55  {
56  return get_point(_t);
57  }
58  catch (exception &e)
59  {
60  std::string err(*boost::get_error_info<exception_error>(e));
61  err+=", called in function\n\t";
62  err+="Eigen::Vector3d mondschein::math::Beziercurve::operator()(float64 _t) const";
63  throw boost::enable_current_exception(e) << exception_error(err);
64  }
65 }
66 
67 void Beziercurve::set_points(const std::vector<Eigen::Vector4d> &_p)
68 {
69  points=_p;
70  return;
71 }
72 
73 std::vector<Eigen::Vector4d> Beziercurve::get_points() const
74 {
75  return points;
76 }
77 
79 {
80  return points.size()-1;
81 }
82 
83 Eigen::Vector3d Beziercurve::get_point(float64 _t) const
84 {
85  if (_t<0) _t=0;
86  else if (_t>1) _t=1;
87 
88  try
89  {
90  /* Preparing the point container */
91  std::vector<Eigen::Vector3d> v;
92  v.clear();
93  for (uint32 i=0; i<points.size(); ++i) v.push_back(Eigen::Vector3d(points.at(i)(0)*points.at(i)(3),
94  points.at(i)(1)*points.at(i)(3),
95  points.at(i)(2)*points.at(i)(3)));
96 
97  /* Perform an iterative deCasteljau algorithm */
98  for (uint32 j=1; j<v.size(); ++j)
99  {
100  for (uint32 k=0; k<v.size()-j; ++k)
101  {
102  v.at(j)=(1-_t)*v.at(j)+_t*v.at(j+1);
103  }
104  }
105  return v.at(0);
106  }
107  catch (std::exception &e)
108  {
109  std::string err("Mondschein Engine ERROR: ");
110  err+=e.what();
111  err+=", exception raised in function\n\t";
112  err+="Eigen::Vector3d mondschein::math::Beziercurve::get_curve_point(float64 _t) const";
113  throw boost::enable_current_exception(exception()) << exception_error(err);
114  }
115 }