libSBNW
All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Modules Pages
point.h
1 /*== SAGITTARIUS =====================================================================
2  * Copyright (c) 2012, Jesse K Medley
3  * All rights reserved.
4 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of The University of Washington nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 //== FILEDOC =========================================================================
29 
34 //== BEGINNING OF CODE ===============================================================
35 
36 #ifndef __SBNW_LAYOUT_POINT_H_
37 #define __SBNW_LAYOUT_POINT_H_
38 
39 //== INCLUDES ========================================================================
40 
42 
43 #include <math.h>
44 
45 //-- C++ code --
46 #ifdef __cplusplus
47 
48 #include <iostream>
49 
50 namespace Graphfab {
51 
52  struct Point; // a pain...
53 
55  Point operator- (const Point& p, const Point& q);
56  Point operator+ (const Point& p, const Point& q);
57 
59  Point operator* (const Point& p, const Real s);
60  Point operator* (const Real s, const Point& p);
61 
63  Point operator/ (const Point& p, const Real s);
64 
66  std::ostream& operator<< (std::ostream&, const Point& p);
67 
68  struct Point {
70  Point() {x = y = 0;}
71 
73  Point(Real x_, Real y_)
74  : x(x_), y(y_) {}
75 
76  static Point polar(Real mag, Real theta);
77 
79  Point operator-() const {
80  return Point(-x, -y);
81  }
82 
84  Real mag() const {
85  return sqrt(x*x + y*y);
86  }
87 
89  Real mag2() const {
90  return x*x + y*y;
91  }
92 
94  Real theta() const {
95  const Real ep = 1e-4;
96  if (std::abs(x) < ep) {
97  if (std::abs(y) < ep)
98  return 0;
99  if (y > 0)
100  return pi/2.;
101  else
102  return -pi/2.;
103  }
104 
105  Real t = atan(y/x);
106 
107  if (x > 0)
108  return t;
109  else
110  return t+pi;
111  }
112 
114  Point squareTerms() const {
115  return Point(x*x, y*y);
116  }
117 
119  Point sqrtTerms() const {
120  AT(x >= 0 && y >= 0, "Cannot take negative square root");
121  return Point(sqrt(x), sqrt(y));
122  }
123 
125  Point capMag(const Real cap) const {
126  Real m = mag2();
127  Real xx = x, yy = y;
128  if(m > cap*cap) {
129  m = sqrt(m);
130  xx *= cap/m;
131  yy *= cap/m;
132  }
133  return Point(xx,yy);
134  }
135 
139  void capMag2_(const Real cap2) {
140  Real m = mag2();
141  if(m > cap2) {
142  m = sqrt(cap2/m);
143  x *= m;
144  y *= m;
145  }
146  }
147 
149  Point normed() const {
150  Real o = mag();
151  if(o < 1e-6)
152  return *this;
153  return (*this)*(1./o);
154  }
155 
157  void norm_() {
158  Real o = mag();
159  if(o < 1e-6)
160  return;
161  x /= o;
162  y /= o;
163  }
164 
165  Point operator = (const Point& p) { x =p.x; y = p.y; return *this; }
166 
167  Point operator+= (const Point& p) { x+=p.x; y+=p.y; return *this; }
168 
169  Point operator-= (const Point& p) { x-=p.x; y-=p.y; return *this; }
170 
171  static Real min(Real x, Real y) { return x < y ? x : y; }
172  static Real max(Real x, Real y) { return x < y ? y : x; }
173 
174  // element-wise min
175  static Point emin(const Point& u, const Point& v) {
176  return Point(min(u.x, v.x), min(u.y,v.y));
177  }
178 
179  // element-wise max
180  static Point emax(const Point& u, const Point& v) {
181  return Point(max(u.x, v.x), max(u.y,v.y));
182  }
183 
185  Point dextro() const {
186  return polar(mag(), theta() - 0.5*pi);
187  }
188 
190  Point sinister() const {
191  return polar(mag(), theta() + 0.5*pi);
192  }
193 
194  std::string rep() const;
195 
196  Real x;
197  Real y;
198  };
199 
200 }
201 
202 #endif
203 
204 #endif
SAGITTARIUS_REAL Real
Make Real visible to C. Consider letting Real lie in top namespace.
Definition: SagittariusCommon.h:136
Definition: SagittariusCommon.cpp:38
First file included.