OpendTect-6_4  6.4
coord.h
Go to the documentation of this file.
1 #ifndef coord_h
2 #define coord_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: A.H.Bril
9  Date: 21-6-1996
10  Contents: Positions: Coordinates
11  RCS: $Id$
12 ________________________________________________________________________
13 
14 -*/
15 
16 #include "basicmod.h"
17 #include "geometry.h"
18 #include "undefval.h"
19 
20 
25 mExpClass(Basic) Coord : public Geom::Point2D<Pos::Ordinate_Type>
26 {
27 public:
28 
31 
33  : Geom::Point2D<OrdType>( p ) {}
34  Coord() : Geom::Point2D<OrdType>( 0, 0 ) {}
35  Coord( OrdType cx, OrdType cy )
36  : Geom::Point2D<OrdType>( cx, cy ) {}
37 
38  bool operator==( const Coord& crd ) const
39  { return mIsEqual(x,crd.x,mDefEps)
40  && mIsEqual(y,crd.y,mDefEps); }
41  bool operator!=( const Coord& crd ) const
42  { return ! (crd == *this); }
43  bool operator<(const Coord&crd) const
44  { return x<crd.x || (x==crd.x && y<crd.y); }
45  bool operator>(const Coord&crd) const
46  { return x>crd.x || (x==crd.x && y>crd.y); }
47 
48  DistType horDistTo(const Coord&) const;
49  DistType sqHorDistTo(const Coord&) const;
51  DistType angle(const Coord& from,const Coord& to) const;
52  DistType cosAngle(const Coord& from,const Coord& to) const;
54  //
55  Coord normalize() const;
56  OrdType dot(const Coord&) const;
57 
58  const char* toString() const;
59  const char* toPrettyString(int nrdec=2) const;
60  bool fromString(const char*);
61 
62  static const Coord& udf();
63  inline bool isUdf() const { return !isDefined(); }
64 };
65 
66 
67 
72 mExpClass(Basic) Coord3 : public Coord
73 {
74 public:
75 
76  Coord3() : z(0) {}
77  Coord3(const Coord& a, OrdType z_ )
78  : Coord(a), z(z_) {}
79  Coord3(const Coord3& xyz )
80  : Coord( xyz.x, xyz.y )
81  , z( xyz.z ) {}
82  Coord3( OrdType x_, OrdType y_, OrdType z_ )
83  : Coord(x_,y_), z(z_) {}
84 
85  OrdType& operator[]( int idx )
86  { return idx ? (idx==1 ? y : z) : x; }
87  OrdType operator[]( int idx ) const
88  { return idx ? (idx==1 ? y : z) : x; }
89 
90  inline Coord3 operator+(const Coord3&) const;
91  inline Coord3 operator-(const Coord3&) const;
92  inline Coord3 operator-() const;
93  inline Coord3 operator*(double) const;
94  inline Coord3 operator/(double) const;
95  inline Coord3 scaleBy( const Coord3& ) const;
96  inline Coord3 unScaleBy( const Coord3& ) const;
97 
98  inline Coord3& operator+=(const Coord3&);
99  inline Coord3& operator-=(const Coord3&);
100  inline Coord3& operator/=(double);
101  inline Coord3& operator*=(double);
102  inline Coord& coord() { return *this; }
103  inline const Coord& coord() const { return *this; }
104 
105  inline bool operator==(const Coord3&) const;
106  inline bool operator!=(const Coord3&) const;
107  bool isSameAs(const Coord3&, const Coord3&) const;
108 
109  DistType distTo(const Coord3&) const;
110  DistType sqDistTo(const Coord3&) const;
111 
112  inline DistType dot(const Coord3&) const;
113  inline Coord3 cross(const Coord3&) const;
114  DistType abs() const;
115  DistType sqAbs() const;
116  inline Coord3 normalize() const;
117 
118  static const Coord3& udf();
119  inline bool isDefined() const;
120  inline bool isUdf() const { return !isDefined(); }
121 
122  const char* toString() const;
123  bool fromString(const char*);
124 
126 
127 };
128 
129 
130 inline Coord3 operator*( double f, const Coord3& b )
131 { return Coord3(b.x*f, b.y*f, b.z*f ); }
132 
133 
134 namespace Values {
135 
140 template<>
142 {
143 public:
144  static Coord val() { return Coord::udf(); }
145  static bool hasUdf() { return true; }
146  static bool isUdf( Coord crd ) { return !crd.isDefined(); }
147  static void setUdf( Coord& crd ) { crd = Coord::udf(); }
148 };
149 
150 
155 template<>
157 {
158 public:
159  static Coord3 val() { return Coord3::udf(); }
160  static bool hasUdf() { return true; }
161  static bool isUdf( Coord3 crd ) { return !crd.isDefined(); }
162  static void setUdf( Coord3& crd ) { crd = Coord3::udf(); }
163 };
164 
165 } // namespace Values
166 
167 
168 inline bool Coord3::operator==( const Coord3& b ) const
169 {
170  const DistType dx = x-b.x; const DistType dy = y-b.y;
171  const DistType dz = z-b.z;
172  return mIsZero(dx,mDefEps) && mIsZero(dy,mDefEps) && mIsZero(dz,mDefEps);
173 }
174 
175 
176 inline bool Coord3::operator!=( const Coord3& b ) const
177 {
178  return !(b==*this);
179 }
180 
181 inline bool Coord3::isDefined() const
182 {
184 }
185 
186 
187 inline Coord3 Coord3::operator+( const Coord3& p ) const
188 {
189  return Coord3( x+p.x, y+p.y, z+p.z );
190 }
191 
192 
193 inline Coord3 Coord3::operator-( const Coord3& p ) const
194 {
195  return Coord3( x-p.x, y-p.y, z-p.z );
196 }
197 
198 
199 inline Coord3 Coord3::operator-() const
200 {
201  return Coord3( -x, -y, -z );
202 }
203 
204 
205 inline Coord3 Coord3::operator*( double factor ) const
206 { return Coord3( x*factor, y*factor, z*factor ); }
207 
208 
209 inline Coord3 Coord3::operator/( double denominator ) const
210 { return Coord3( x/denominator, y/denominator, z/denominator ); }
211 
212 
213 inline Coord3 Coord3::scaleBy( const Coord3& factor ) const
214 { return Coord3( x*factor.x, y*factor.y, z*factor.z ); }
215 
216 
217 inline Coord3 Coord3::unScaleBy( const Coord3& denominator ) const
218 { return Coord3( x/denominator.x, y/denominator.y, z/denominator.z ); }
219 
220 
221 inline Coord3& Coord3::operator+=( const Coord3& p )
222 {
223  x += p.x; y += p.y; z += p.z;
224  return *this;
225 }
226 
227 
228 inline Coord3& Coord3::operator-=( const Coord3& p )
229 {
230  x -= p.x; y -= p.y; z -= p.z;
231  return *this;
232 }
233 
234 
235 inline Coord3& Coord3::operator*=( double factor )
236 {
237  x *= factor; y *= factor; z *= factor;
238  return *this;
239 }
240 
241 
242 inline Coord3& Coord3::operator/=( double denominator )
243 {
244  x /= denominator; y /= denominator; z /= denominator;
245  return *this;
246 }
247 
248 
249 inline Coord::DistType Coord3::dot(const Coord3& b) const
250 { return x*b.x + y*b.y + z*b.z; }
251 
252 
253 inline Coord3 Coord3::cross(const Coord3& b) const
254 { return Coord3( y*b.z-z*b.y, z*b.x-x*b.z, x*b.y-y*b.x ); }
255 
256 
257 inline Coord3 Coord3::normalize() const
258 {
259  const DistType absval = abs();
260  if ( absval < 1e-10 )
261  return *this;
262 
263  return *this / absval;
264 }
265 
266 
267 #endif
#define mExpClass(module)
Definition: commondefs.h:160
Coord(const Geom::Point2D< OrdType > &p)
Definition: coord.h:32
bool operator==(const Coord &crd) const
Definition: coord.h:38
int operator-(const DateInfo &di1, const DateInfo &di2)
Definition: dateinfo.h:129
static bool isUdf(Coord3 crd)
Definition: coord.h:161
static void setUdf(Coord &crd)
Definition: coord.h:147
T to(const F &fr)
Definition: convert.h:33
bool operator==(const ArrayNDInfo &a1, const ArrayNDInfo &a2)
Definition: arrayndinfo.h:53
Coord3 operator+(const Coord3 &) const
Definition: coord.h:187
Templatized undefined and initialisation (i.e. null) values.
Definition: color.h:106
DistType dot(const Coord3 &) const
Definition: coord.h:249
Coord3 scaleBy(const Coord3 &) const
Definition: coord.h:213
bool operator==(const Coord3 &) const
Definition: coord.h:168
OrdType operator[](int idx) const
Definition: coord.h:87
#define mIsZero(x, eps)
Definition: commondefs.h:53
Coord3 operator-() const
Definition: coord.h:199
Coord3 & operator*=(double)
Definition: coord.h:235
bool isUdf() const
Definition: coord.h:120
Coord3(const Coord &a, OrdType z_)
Definition: coord.h:77
static Coord val()
Definition: coord.h:144
bool operator!=(const Coord &crd) const
Definition: coord.h:41
bool operator<(const Coord &crd) const
Definition: coord.h:43
static const Coord3 & udf()
bool isDefined() const
Definition: coord.h:181
static bool hasUdf()
Definition: coord.h:145
Pos::Distance_Type DistType
Definition: coord.h:30
A cartesian coordinate in 2D space.
Definition: coord.h:25
Coord3 unScaleBy(const Coord3 &) const
Definition: coord.h:217
Coord3 operator*(double) const
Definition: coord.h:205
static bool isUdf(Coord crd)
Definition: coord.h:146
#define mIsEqual(x, y, eps)
Definition: commondefs.h:54
Coord & coord()
Definition: coord.h:102
double Distance_Type
Definition: commontypes.h:55
Coord3 & operator/=(double)
Definition: coord.h:242
Coord3 cross(const Coord3 &) const
Definition: coord.h:253
double Ordinate_Type
Definition: commontypes.h:54
Coord3 normalize() const
Definition: coord.h:257
T y
Definition: geometry.h:67
bool isDefined() const
Definition: geometry.h:352
const Coord & coord() const
Definition: coord.h:103
OrdType & operator[](int idx)
Definition: coord.h:85
Coord3(OrdType x_, OrdType y_, OrdType z_)
Definition: coord.h:82
Position.
Definition: commontypes.h:42
bool operator>(const Coord &crd) const
Definition: coord.h:45
bool operator!=(const ArrayNDInfo &a1, const ArrayNDInfo &a2)
Definition: arrayndinfo.h:62
bool isUdf() const
Definition: coord.h:63
Coord(OrdType cx, OrdType cy)
Definition: coord.h:35
#define mDefEps
Definition: commondefs.h:58
A cartesian coordinate in 3D space.
Definition: coord.h:72
static const Coord & udf()
OrdType z
Definition: coord.h:125
bool operator!=(const Coord3 &) const
Definition: coord.h:176
static Coord3 val()
Definition: coord.h:159
Coord3 operator*(double f, const Coord3 &b)
Definition: coord.h:130
Coord3()
Definition: coord.h:76
T x
Definition: geometry.h:66
static void setUdf(Coord3 &crd)
Definition: coord.h:162
Coord3 & operator+=(const Coord3 &)
Definition: coord.h:221
Definition: geometry.h:19
#define mClass(module)
Definition: commondefs.h:164
Basic point class.
Definition: geometry.h:27
Coord()
Definition: coord.h:34
bool isUdf(const T &t)
Definition: undefval.h:243
static bool hasUdf()
Definition: coord.h:160
Pos::Ordinate_Type OrdType
Definition: coord.h:29
Export_Basic const char * toString(ViewStyle)
Coord3 & operator-=(const Coord3 &)
Definition: coord.h:228
Templatized undefined values.
Definition: undefval.h:51
Coord3(const Coord3 &xyz)
Definition: coord.h:79
Coord3 operator/(double) const
Definition: coord.h:209

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019