Point.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef POINT_H_
32 #define POINT_H_
33 
34 
35 //#include <vector>
36 //#include <iostream>
37 //#include <sstream>
38 //#include <string>
39 
40 #include <drain/UniTuple.h>
41 #include <drain/Type.h>
42 
43 namespace drain
44 {
45 
46 
47 template <class T>
48 struct Point2D : public drain::UniTuple<T,2> {
49 
50  T &x;
51  T &y;
52 
53  Point2D(T x=0, T y=0) : x(this->next()), y(this->next()){
54  this->set(x, y);
55  };
56 
57  Point2D(const Point2D & p) : x(this->next()), y(this->next()){
58  this->set(p.tuple());
59  };
60 
61  // Reference
62  template <size_t N>
63  Point2D(drain::UniTuple<T,N> & tuple, size_t i) :
64  drain::UniTuple<T,2>(tuple, i), // start in new pos!
65  x(this->next()), y(this->next()){
66  };
67 
68  Point2D & operator=(const Point2D & p){
69  this->set(p.tuple());
70  return *this;
71  }
72 
73  // set(x, y);
74  template <class T2>
75  Point2D<T> & setLocation(T2 x, T2 y){
76  this->set(x, y);
77  // this->x = static_cast<T>(x);
78  // this->y = static_cast<T>(y);
79  return *this;
80  }
81 
82  template <class T2>
83  Point2D<T> & setLocation(const Point2D<T2> & p){
84  this->set(p);
85  // this->x = static_cast<T>(p.x);
86  // this->y = static_cast<T>(p.y);
87  return *this;
88  }
89 
90 };
91 
92 template <class T>
93 struct Point3D : public drain::UniTuple<T,3> {
94 
95  T &x;
96  T &y;
97  T &z;
98 
99  Point3D(T x=0, T y=0, T z=0) : x(this->at(0)=x), y(this->at(1)=y), z(this->at(2)=z){
100  };
101 
102  Point3D(const Point3D & p) : x(this->at(0)=p.x), y(this->at(1)=p.y), z(this->at(2)=p.z){
103  };
104 
105  Point3D & operator=(const Point3D & p){
106  this->set(p.tuple());
107  return *this;
108  }
109 
110  // Consider generalized (for smaller tuples?)
111  template <class T2>
112  Point3D & operator=(const drain::UniTuple<T2,3> & p){
113  this->set(p.tuple());
114  return *this;
115  }
116 
117  template <class T2>
118  void setLocation(const T2 & x, const T2 & y, const T2 & z){
119  this->x = static_cast<T>(x);
120  this->y = static_cast<T>(y);
121  this->z = static_cast<T>(z);
122  }
123 
124  template <class T2>
125  void setLocation(const Point3D<T2> & p){
126  *this = p;
127  }
128 
129 
130 };
131 
132 //template <class T>
133 DRAIN_TYPENAME_T(Point2D, T);
134 
135 //template <class T>
136 DRAIN_TYPENAME_T(Point3D, T);
137 
138 
139 /*
140 template <class T>
141 std::ostream &operator<<(std::ostream &ostr,const drain::Point2D<T> &p)
142 {
143  ostr << '[' << p.x << ',' << p.y << ']';
144  return ostr;
145 }
146 
147 template <class T>
148 std::ostream &operator<<(std::ostream &ostr,const drain::Point3D<T> &p)
149 {
150  ostr << '[' << p.x << ',' << p.y << ',' << p.z << ']';
151  return ostr;
152 }
153 */
154 
155 // }
156 
157 } // drain::
158 
159 
160 #endif /*POINT_H_*/
const S & at(size_t i) const
Return const reference to element i.
Definition: TupleBase.h:97
Tuple of N elements of type T.
Definition: UniTuple.h:65
Definition: DataSelector.cpp:1277
Definition: Point.h:48
Definition: Point.h:93