Last change
on this file since 117 was
117,
checked in by moretto, 11 years ago
|
ESAF version compilable on mac OS
|
File size:
2.0 KB
|
Line | |
---|
1 | /* ------------------------------------------------------------------------- |
---|
2 | * utils.c |
---|
3 | * |
---|
4 | * --- miscellaneous functions for raytracing |
---|
5 | * |
---|
6 | * Copyright (c) 2000-2005 N.Sakaki, Y.Takizawa, Y.Kawasaki |
---|
7 | * All rights reserved. |
---|
8 | * $Id: utils.c,v 1.3 2005/04/04 00:36:27 sakaki Exp sakaki $ |
---|
9 | * ------------------------------------------------------------------------- |
---|
10 | */ |
---|
11 | #include <math.h> |
---|
12 | #include "Mutils.hh" |
---|
13 | |
---|
14 | /* |
---|
15 | * ------------------------------------------------------------------------ |
---|
16 | * binary search |
---|
17 | * find an index "i" that satisfy a[i] <= x < a[i+1] |
---|
18 | * ------------------------------------------------------------------------ |
---|
19 | */ |
---|
20 | int Mrt_bsearch(double a[], double x, int left, int right) |
---|
21 | { |
---|
22 | int center; |
---|
23 | |
---|
24 | if(x<a[left]) |
---|
25 | return -1; |
---|
26 | if(x>a[right]) |
---|
27 | return right+1; |
---|
28 | |
---|
29 | while(right-left>1) |
---|
30 | { |
---|
31 | center=(left+right)/2; |
---|
32 | if(x<a[center]) |
---|
33 | {right=center;} |
---|
34 | else |
---|
35 | {left=center;} |
---|
36 | } |
---|
37 | |
---|
38 | return left; |
---|
39 | } |
---|
40 | |
---|
41 | /* ------------------------------------------------------------------------ |
---|
42 | * This function solves the quadratic equation |
---|
43 | * a[0]*x^2 + a[1]*x + a[2] = 0 |
---|
44 | * and returns the number of real roots and the roots themselves |
---|
45 | * in x[0] and x[1]. |
---|
46 | * ------------------------------------------------------------------------ |
---|
47 | */ |
---|
48 | int MSolveQuadratic(double aa[3], double x[2]) |
---|
49 | { |
---|
50 | double d, t, a, b, c; |
---|
51 | |
---|
52 | a= aa[0]; |
---|
53 | b=-aa[1]; |
---|
54 | c= aa[2]; |
---|
55 | |
---|
56 | if(a==0.0) |
---|
57 | { |
---|
58 | if(b==0.0) |
---|
59 | return 0; |
---|
60 | |
---|
61 | x[0]=c/b; |
---|
62 | x[1]=x[0]; |
---|
63 | |
---|
64 | return 1; |
---|
65 | } |
---|
66 | |
---|
67 | /* Discriminant */ |
---|
68 | d = b*b-4.0*a*c; |
---|
69 | if(d<0.0) |
---|
70 | return 0; |
---|
71 | |
---|
72 | d=sqrt(d); |
---|
73 | |
---|
74 | t=2.0*a; |
---|
75 | |
---|
76 | if(b>0.0) |
---|
77 | { |
---|
78 | x[1]=(b+d)/t; |
---|
79 | x[0]=(c/a)/x[1]; |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | x[0]=(b-d)/t; |
---|
84 | x[1]=(c/a)/x[0]; |
---|
85 | } |
---|
86 | |
---|
87 | if(d==0.0) |
---|
88 | return 1; |
---|
89 | else |
---|
90 | return 2; |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | * ------------------------------------------------------------------------ |
---|
95 | * calculate inner product of two vectors |
---|
96 | * ------------------------------------------------------------------------ |
---|
97 | */ |
---|
98 | double Mvdot(double a[3], double b[3]) |
---|
99 | { |
---|
100 | return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]; |
---|
101 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.