|
@@ -62,6 +62,51 @@ Punkt3D Sphere::getPos() const {
|
62
|
62
|
return pos;
|
63
|
63
|
}
|
64
|
64
|
|
|
65
|
+int Sphere::getIntersectionParam(const Ray &ray, float *param1, float *param2) {
|
|
66
|
+ int numerg;
|
|
67
|
+ // ax^2 + bx + c = 0
|
|
68
|
+ segl::Punkt3D ehv(1.0f, 1.0f, 1.0f);
|
|
69
|
+ float a = ray.dir*ehv;
|
|
70
|
+ float b = 2*(ray.pos*ehv + pos*ehv);
|
|
71
|
+ float c = ray.pos*ray.pos + pos*pos - 2*(ray.pos*pos) - radius*radius;
|
|
72
|
+
|
|
73
|
+ float p = b/a;
|
|
74
|
+ float q = c/a;
|
|
75
|
+
|
|
76
|
+ if((p*p)/4.0f<q) {
|
|
77
|
+ numerg = 0;
|
|
78
|
+ } else if((p*p)/4.0f==q) {
|
|
79
|
+ numerg = 1;
|
|
80
|
+ if(param1!=0) {
|
|
81
|
+ *param1 = -p/2.0f;
|
|
82
|
+ }
|
|
83
|
+ } else {
|
|
84
|
+ numerg = 2;
|
|
85
|
+ if(param1!=0 || param2!=0) {
|
|
86
|
+ if(param1!=0) {
|
|
87
|
+ *param1 = -p/2.0f + sqrt(((p*p)/4.0f)-q);
|
|
88
|
+ }
|
|
89
|
+ if(param2!=0) {
|
|
90
|
+ *param2 = -p/2.0f - sqrt(((p*p)/4.0f)-q);
|
|
91
|
+ }
|
|
92
|
+ }
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ return numerg;
|
|
96
|
+}
|
|
97
|
+
|
|
98
|
+int Sphere::getIntersectionPoints(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b) {
|
|
99
|
+ float pa, pb;
|
|
100
|
+ int numerg = getIntersectionParam(ray, &pa, &pb);
|
|
101
|
+ if(a)
|
|
102
|
+ *a = ray.get(pa);
|
|
103
|
+
|
|
104
|
+ if(b)
|
|
105
|
+ *b = ray.get(pb);
|
|
106
|
+
|
|
107
|
+ return numerg;
|
|
108
|
+}
|
|
109
|
+
|
65
|
110
|
Ray::Ray() {
|
66
|
111
|
dir.set(0.0f, 1.0f, 0.0f);
|
67
|
112
|
}
|
|
@@ -139,14 +184,6 @@ float Ray::dist(Punkt3D p) const {
|
139
|
184
|
return abs(p - get( getParam(p) ));
|
140
|
185
|
}
|
141
|
186
|
|
142
|
|
-int getIntersectionParam(const Ray &ray, float *param1, float *param2) {
|
143
|
|
-
|
144
|
|
-}
|
145
|
|
-
|
146
|
|
-int getIntersectionParam(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b) {
|
147
|
|
-
|
148
|
|
-}
|
149
|
|
-
|
150
|
187
|
float Ray::getParam(Punkt3D p, bool onray) const {
|
151
|
188
|
if(onray) {
|
152
|
189
|
if(!onRay(p))
|