libSBNW
 All Classes Namespaces Files Functions Variables Typedefs Macros Modules Pages
geom.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 __GRAPHFAB_MATH_GEOM_H_
37 #define __GRAPHFAB_MATH_GEOM_H_
38 
39 //== INCLUDES ========================================================================
40 
42 #include "graphfab/layout/point.h"
43 #include "graphfab/layout/box.h"
44 #include "graphfab/math/sign_mag.h"
45 
46 #include <math.h>
47 
48 //-- C++ code --
49 #ifdef __cplusplus
50 
51 namespace Graphfab {
52 
54  inline Real deg2r(const Real deg) {
55  const Real pi = 3.14159;
56  return deg*pi/180.;
57  }
58 
60  inline Point computeCubic(const Point& alpha, const Point& beta, const Point& gamma, const Point& delta, Real t) {
61  return alpha*t*t*t + beta*t*t + gamma*t + delta;
62  }
63 
65  inline Point new2ndPos(const Point& first, const Point& second, const Real deg, const Real dist, const bool rel_dist) {
66  Real h, o, a, x;
67  Real hnew, onew, anew;
68 
69  o = second.y - first.y;
70  a = second.x - first.x;
71  h = sqrt(pow(a,2.) + pow(o,2.));
72 
73  if(rel_dist)
74  hnew = h + h*dist;
75  else
76  hnew = h + dist;
77 
78  const Real ep = 1e-6;
79 
80  if(mag(a) > ep)
81  x = atan(o/a);
82  else
83  x = sign(o)*3.14159/2.;
84 
85  onew = hnew * sin(x + deg2r(deg));
86  anew = hnew * cos(x + deg2r(deg));
87 
88  if(second.x >= first.x)
89  return Point(first.x + anew, first.y + onew);
90  else
91  return Point(first.x - anew, first.y - onew);
92  }
93 
94  // bounding box-based
95  Point calcCurveBackup(const Point& src, const Point& cent, const Box& ext, Real dist = 20);
96 
97  class Line2Desc {
98  public:
99  Line2Desc(const Point& start, const Point& end);
100 
101  Real getA() const { return A_; }
102 
103  Real getB() const { return B_; }
104 
105  Real getC() const { return C_; }
106 
107  protected:
108  Real A_, B_, C_;
109 
110  _GraphfabExport friend std::ostream& operator<<(std::ostream& o, const Line2Desc& c);
111  };
112 
113  _GraphfabExport std::ostream& operator<<(std::ostream& o, const Line2Desc& c);
114 
115  class CubicBezier2Desc {
116  public:
117  CubicBezier2Desc(const Point& start, const Point& c1, const Point& c2, const Point& end);
118 
120  Point p(Real t) const;
121 
123  Point getCP(int n) const;
124 
125  protected:
126  Point P0_, P1_, P2_, P3_;
127 
128  _GraphfabExport friend std::ostream& operator<<(std::ostream& o, const CubicBezier2Desc& c);
129  };
130 
131  _GraphfabExport std::ostream& operator<<(std::ostream& o, const CubicBezier2Desc& c);
132 
133  // http://www.particleincell.com/blog/2013/cubic-line-intersection/
134  class CubicBezierIntersection {
135  public:
136  CubicBezierIntersection(const Line2Desc& l, const CubicBezier2Desc& c);
137 
138  const std::vector<Real>& getIntersectionPoints() const { return r_; }
139 
140  protected:
141  std::vector<Real> r_;
142  };
143 
144 // class LinearIntersectionResults {
145 // public:
146 // const Point& p() { return p_; }
147 // bool exists() { return v_; }
148 //
149 // protected:
150 // Point p_;
151 // bool v_;
152 //
153 // friend class LinearIntersection;
154 // };
155 
156  class LinearIntersection {
157  public:
158  LinearIntersection(const Point& pbegin, const Point& pend, const Point& qbegin, const Point& qend);
159 
160  const Point& p() { return p_; }
161  bool exists() { return v_; }
162 
163  protected:
164  Point p_;
165  bool v_;
166  };
167 
168 }
169 
170 #else // __cplusplus
171 
172 
173 
174 #endif // __cplusplus
175 
176 #endif
SAGITTARIUS_REAL Real
Make Real visible to C. Consider letting Real lie in top namespace.
Definition: SagittariusCommon.h:136
Sign & magnitude for reals.
Definition: SagittariusCommon.cpp:38
A box.
First file included.