mt
 All Classes Files Functions Enumerations Groups Pages
mt_variant.h
1 /***************************************************************************
2  * Copyright (C) 2006 by Adolfo Rodriguez *
3  * adolfo.rodriguez@upc.edu *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 
23 
24 // HEADER GUARD
25 #ifndef MT_MT_VARIANT_H
26 #define MT_MT_VARIANT_H
27 
28 // C++ STANDARD HEADERS
29 #include <string>
30 #include <utility>
31 
32 // BOOST LIBRARY HEADERS
33 #include <boost/variant.hpp>
34 
35 // MT LIBRARY HEADERS
36 #include <mt/empty.h>
37 #include <mt/vector3.h>
38 #include <mt/unit3.h>
39 #include <mt/point3.h>
40 #include <mt/line3.h>
41 #include <mt/plane3.h>
42 #include <mt/sphere3.h>
43 #include <mt/cylinder3.h>
44 #include <mt/circle3.h>
45 #include <mt/ellipse3.h>
46 #include <mt/transform_utility.h>
47 
48 
50 
51 namespace mt
52 {
53 
54 
56 
82 
83 
84 typedef boost::variant<Empty,
85  Vector3,
86  Unit3,
87  Point3,
88  Line3,
89  Plane3,
90  Sphere3,
91  Cylinder3,
92  Circle3,
93  Ellipse3> MtVariant;
94 
95 
97 
98 /*
99 bool operator!=(const MtVariant& geom1,
100  const MtVariant& geom2);
101 */
102 
103 std::string getTypeName(const MtVariant& geom);
104 
105 bool isEmpty(const MtVariant& geom);
106 
107 
109 MtVariant apply(const Transform& tr,
110  const MtVariant& geom);
111 
112 
114 
115 class NameVisitor : public boost::static_visitor<std::string>
116 {
117 public:
118  std::string operator()(const Vector3& v) const {return "vector";}
119  std::string operator()(const Unit3& u) const {return "unit vector";}
120  std::string operator()(const Point3& p) const {return "point";}
121  std::string operator()(const Line3& L) const {return "line";}
122  std::string operator()(const Plane3& P) const {return "plane";}
123  std::string operator()(const Sphere3& s) const {return "sphere";}
124  std::string operator()(const Cylinder3& c) const {return "cylinder";}
125  std::string operator()(const Circle3& C) const {return "circle";}
126  std::string operator()(const Ellipse3& E) const {return "ellipse";}
127  template<class T>
128  std::string operator()(const T&) const {return "";}
129 };
130 
131 
132 class TransformVisitor : public boost::static_visitor<MtVariant>
133 {
134 public:
135  TransformVisitor(const Transform& tr) : m_tr(tr) {}
136 
137  template<class T>
138  MtVariant operator()(const T& t) const {return MtVariant(apply(m_tr, t));}
139 
140 private:
141  Transform m_tr;
142 };
143 
144 
146 
147 /*
148 inline bool operator!=(const MtVariant& geom1,
149  const MtVariant& geom2)
150 {
151  return std::rel_ops::operator!=(geom1, geom2);
152 }
153 */
154 
155 
156 inline std::string getTypeName(const MtVariant& geom)
157 {
158  return boost::apply_visitor(NameVisitor(), geom);
159 }
160 
161 
162 inline bool isEmpty(const MtVariant& geom)
163 {
164  return boost::apply_visitor(EmptyVisitor(), geom);
165 }
166 
167 
168 inline MtVariant apply(const Transform& tr,
169  const MtVariant& geom)
170 {
171  return boost::apply_visitor(TransformVisitor(tr), geom);
172 }
173 
174 } // mt
175 
176 #endif // MT_MT_VARIANT_H
177 
178