1 | /* ------------------------------------------------------------------------- |
---|
2 | * Kutils.c |
---|
3 | * |
---|
4 | * --- miscellaneous functions for raytracing |
---|
5 | * |
---|
6 | * $Id: Kutils.cc 119 2003-09-23 13:19:04Z pallas $ |
---|
7 | * $Id: Kutils.c,v 1.0 2003/09/21 J. W. Watts |
---|
8 | * ------------------------------------------------------------------------- |
---|
9 | */ |
---|
10 | #include "TMath.h" |
---|
11 | #include "Kutils.hh" |
---|
12 | |
---|
13 | /* |
---|
14 | * ------------------------------------------------------------------------ |
---|
15 | * binary search |
---|
16 | * find an index "i" that satisfy a[i] <= x < a[i+1] |
---|
17 | * ------------------------------------------------------------------------ |
---|
18 | */ |
---|
19 | Int_t Krt_bsearch(Double_t a[], Double_t x, Int_t left, Int_t right) |
---|
20 | { |
---|
21 | Int_t center; |
---|
22 | |
---|
23 | if(x<a[left] || a[right]<x) |
---|
24 | return -1; |
---|
25 | |
---|
26 | while(right-left>1) |
---|
27 | { |
---|
28 | center=(left+right)/2; |
---|
29 | if(x<a[center]) |
---|
30 | {right=center;} |
---|
31 | else |
---|
32 | {left=center;} |
---|
33 | } |
---|
34 | |
---|
35 | return left; |
---|
36 | } |
---|
37 | |
---|
38 | /* ------------------------------------------------------------------------ |
---|
39 | * This function solves the quadratic equation |
---|
40 | * a[0]*x^2 + a[1]*x + a[2] = 0 |
---|
41 | * and returns the number of real roots and the roots themselves |
---|
42 | * in x[0] and x[1]. |
---|
43 | * ------------------------------------------------------------------------ |
---|
44 | */ |
---|
45 | Int_t KSolveQuadratic(Double_t aa[3], Double_t x[2]) |
---|
46 | { |
---|
47 | Double_t d, t, a, b, c; |
---|
48 | |
---|
49 | a= aa[0]; |
---|
50 | b=-aa[1]; |
---|
51 | c= aa[2]; |
---|
52 | |
---|
53 | if(a==0.0) |
---|
54 | { |
---|
55 | if(b==0.0) |
---|
56 | return 0; |
---|
57 | |
---|
58 | x[0]=c/b; |
---|
59 | x[1]=x[0]; |
---|
60 | |
---|
61 | return 1; |
---|
62 | } |
---|
63 | |
---|
64 | /* Discriminant */ |
---|
65 | d = b*b-4.0*a*c; |
---|
66 | if(d<0.0) |
---|
67 | return 0; |
---|
68 | |
---|
69 | d=TMath::Sqrt(d); |
---|
70 | |
---|
71 | t=2.0*a; |
---|
72 | |
---|
73 | if(b>0.0) |
---|
74 | { |
---|
75 | x[1]=(b+d)/t; |
---|
76 | x[0]=(c/a)/x[1]; |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | x[0]=(b-d)/t; |
---|
81 | x[1]=(c/a)/x[0]; |
---|
82 | } |
---|
83 | |
---|
84 | if(d==0.0) |
---|
85 | return 1; |
---|
86 | else |
---|
87 | return 2; |
---|
88 | } |
---|
89 | |
---|
90 | /* |
---|
91 | * ------------------------------------------------------------------------ |
---|
92 | * calculate inner product of two vectors |
---|
93 | * ------------------------------------------------------------------------ |
---|
94 | */ |
---|
95 | Double_t Kvdot(Double_t a[3], Double_t b[3]) |
---|
96 | { |
---|
97 | return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]; |
---|
98 | } |
---|