VPTissue Reference Manual
Geom.h
Go to the documentation of this file.
1 #ifndef GEOM_H_INCLUDED
2 #define GEOM_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
17  /*
18  * Some of the procedures are adapted from Dan Sunday's Geometry Algorithms
19  * code. See http://geomalgorithms.com/index.html. When this is the case,
20  * the appropriate url is mentioned in comment.
21  *
22  * Copyright 2000 softSurfer, 2012 Dan Sunday
23  * This code may be freely used, distributed and modified for any purpose
24  * providing that this copyright notice is included with it.
25  * SoftSurfer makes no warranty for this code, and cannot be held
26  * liable for any real or imagined damage resulting from its use.
27  * Users of this code must verify correctness for their application.
28  */
34 #include "array3.h"
35 #include "bio/Node.h"
38 
39 #include <array>
40 #include <tuple>
41 
42 namespace SimPT_Sim {
43 
44 using namespace SimPT_Sim::Util;
45 
49 class Geom
50 {
51 public:
56  static std::tuple<double, double, double>
57  CircumCircle(double x1, double y1, double x2, double y2, double x3, double y3);
58 
62  static inline std::array<double, 3> Intersection(
63  const std::array<double, 3>& v1, const std::array<double, 3>& v2,
64  const std::array<double, 3>& v3, const std::array<double, 3>& v4)
65  {
66  const double denom = (v4[1] - v3[1]) * (v2[0] - v1[0]) - (v4[0] - v3[0]) * (v2[1] - v1[1]);
67  const double ua = ((v4[0] - v3[0]) * (v1[1] - v3[1]) - (v4[1] - v3[1]) * (v1[0] - v3[0])) / denom;
68  return v1 + ua * (v2 - v1);
69  }
70 
76  static inline int
77  isLeft(const std::array<double, 3>& P0, const std::array<double, 3>& P1, const std::array<double, 3>& P2 )
78  {
79  return (P1[0] - P0[0])*(P2[1] - P0[1]) - (P2[0] - P0[0])*(P1[1] - P0[1]);
80  }
81 
89  static inline int
90  wn_PnPoly(const std::array<double, 3>& p, const std::vector<Node*>& nodes)
91  {
92  using std::array;
93  using namespace SimPT_Sim::Container;
94 
95  int wn = 0; // winding number counter
96  auto it = make_const_circular(nodes);
97 
98  // loop through all edges of the polygon
99  do { // edge from e_a to e_b
100  array<double, 3> e_a = *(*it);
101  array<double, 3> e_b = *(*next(it));
102 
103  if (e_a[1] <= p[1]) { // start y <= p.y
104  if (e_b[1] > p[1]) { // an upward crossing
105  if (isLeft( e_a, e_b, p) > 0) { // p left of edge
106  ++wn; // have valid up intersect
107  }
108  }
109  } else { // start y > p.y (no test needed)
110  if (e_b[1] <= p[1]) { // a downward crossing
111  if (isLeft( e_a, e_b, p) < 0) { // p right of edge
112  --wn; // have valid down intersect
113  }
114  }
115  }
116 
117  } while(++it != nodes.begin());
118  return wn;
119  }
120 
121  };
122 }
123 
124 #endif // end-of-include-guard
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
static int isLeft(const std::array< double, 3 > &P0, const std::array< double, 3 > &P1, const std::array< double, 3 > &P2)
IsLeft tests if a point is Left|On|Right of an infinite line.
Definition: Geom.h:77
Namespace for the core simulator.
Namespace for container related classes.
static std::array< double, 3 > Intersection(const std::array< double, 3 > &v1, const std::array< double, 3 > &v2, const std::array< double, 3 > &v3, const std::array< double, 3 > &v4)
Intersection of two lines in x-y plane.s.
Definition: Geom.h:62
Interface for Node.
ConstCircularIterator class and helper functions.
Extending std with arithmetic for std::array.
CircularIterator< T > next(CircularIterator< T > i)
Helper yields the position the iterator would have if moved forward (in circular fashion) by 1 positi...
ConstCircularIterator< typename T::const_iterator > make_const_circular(const T &c)
Helper produces const circular iterator whose range corresponds to the begin and end iterators of a c...
static int wn_PnPoly(const std::array< double, 3 > &p, const std::vector< Node * > &nodes)
Calculates winding number to test whether a point is inside polygon (wn=0 iff p outside).
Definition: Geom.h:90
Provides geometric functions.
Definition: Geom.h:49
CircularIterator class and helper functions.