CloudyDay
 All Classes Functions Variables Enumerations
CloudCreateVolume.h
1 #pragma once
2 #include <osg/Node>
3 #include <osgDB/ReadFile>
4 
5 #include <string>
6 #include <vector>
7 
8 namespace osgCloudyDay
9 {
17  //http://www.softsurfer.com/Archive/algorithm_0105/algorithm_0105.htm#intersect_RayTriangle%28%29
18  // Input: a ray R, and a triangle T
19  // Output: *I = intersection point (when it exists)
20  // Return: -1 = triangle is degenerate (a segment or point)
21  // 0 = disjoint (no intersect)
22  // 1 = intersect in unique point I1
23  // 2 = are in the same plane
24  inline int intersect_RayTriangle( osg::Vec3* R, osg::Vec3* T, osg::Vec3& I )
25  {
26  osg::Vec3 u, v, n; // triangle vectors
27  osg::Vec3 dir, w0, w; // ray vectors
28  float r, a, b; // params to calc ray-plane intersect
29 
30  // get triangle edge vectors and plane normal
31  u = T[1] - T[0];
32  v = T[2] - T[0];
33  n = u ^ v; // cross product
34  if (n == osg::Vec3()) // triangle is degenerate
35  return -1; // do not deal with this case
36 
37  dir = R[1] - R[0]; // ray direction vector
38  w0 = R[0] - T[0];
39  a = -n * w0;
40  b = n * dir;
41 
42  if (fabs(b) < 0.00000001) { // ray is parallel to triangle plane
43  if (a == 0) return 2; // ray lies in triangle plane
44  else return 0; // ray disjoint from plane
45  }
46 
47  // get intersect point of ray with triangle plane
48  r = a / b;
49  if (r < 0.0) // ray goes away from triangle
50  return 0; // => no intersect
51  // for a segment, also test if (r > 1.0) => no intersect
52 
53  I = R[0] + (dir * r); // intersect point of ray and plane
54 
55  // is I inside T?
56  float uu, uv, vv, wu, wv, D;
57  uu = u * u;
58  uv = u * v;
59  vv = v * v;
60  w = I - T[0];
61  wu = w * u;
62  wv = w * v;
63  D = uv * uv - uu * vv;
64 
65  // get and test parametric coords
66  float s, t;
67  s = (uv * wv - vv * wu) / D;
68  if (s < 0.0 || s > 1.0) // I is outside T
69  return 0;
70  t = (uv * wu - uu * wv) / D;
71  if (t < 0.0 || (s + t) > 1.0) // I is outside T
72  return 0;
73 
74  return 1; // I is in T
75  }
76 
82  {
83  public:
87  CloudCreateVolume(void);
91  ~CloudCreateVolume(void);
92 
98  osg::Node* LoadingVolume(std::string path);
99 
105  bool PointInside(osg::Vec3 point);
106 
110  std::vector<std::pair<osg::Vec3, osg::Vec3>> m_aabb_boundbox;
111 
112  private:
113  osg::ref_ptr<osg::Node> object;
114  };
115 }